jeroen / RAppArmor

R interfaces to Linux and AppArmor security methods
cran.r-project.org/package=RAppArmor
54 stars 15 forks source link

Unexpected behavior when using eval.secure #26

Closed meztez closed 3 years ago

meztez commented 3 years ago

Since unix::eval_safe uses its parent.frame to evaluate expression. Using eval.secure vs unix::eval_safe is not the same.

See https://stackoverflow.com/questions/65236186/combining-r-plumber-and-rapparmor

eval.secure just pass all parameters to unix::eval_safe. The reason why eval.secure does not work is because eval_safe expects to find your variables in its parent.frame(), which in the case of eval.secure is an empty function body.

eval_safe use of parent.frame()

function (expr, tmp = tempfile("fork"), std_out = stdout(), std_err = stderr(), 
    timeout = 0, priority = NULL, uid = NULL, gid = NULL, rlimits = NULL, 
    profile = NULL, device = pdf) 
{
    orig_expr <- substitute(expr)
    out <- eval_fork(expr = tryCatch({
        if (length(priority)) 
            setpriority(priority)
        if (length(rlimits)) 
            set_rlimits(rlimits)
        if (length(gid)) 
            setgid(gid)
        if (length(uid)) 
            setuid(uid)
        if (length(profile)) 
            aa_change_profile(profile)
        if (length(device)) 
            options(device = device)
        graphics.off()
        options(menu.graphics = FALSE)
------> serialize(withVisible(eval(orig_expr, parent.frame())), 
            NULL)
    }, error = function(e) {
        old_class <- attr(e, "class")
        structure(e, class = c(old_class, "eval_fork_error"))
    }, finally = substitute(graphics.off())), tmp = tmp, timeout = timeout, 
        std_out = std_out, std_err = std_err)
    if (inherits(out, "eval_fork_error")) 
        base::stop(out)
    res <- unserialize(out)
    if (res$visible) 
        res$value
    else invisible(res$value)
}
# parent.frame() in eval_safe when using eval.secure
function (...) 
{
  # nothing here  
  unix::eval_safe(...)
}

# parent.frame() when using eval_safe directly
function(func){
  data <- cbind(rnorm(100),rnorm(100))
  # Your stuff is here
  unix::eval_safe(nrow(data), profile="r-user")
}
jeroen commented 3 years ago

Can you suggest a fix?

meztez commented 3 years ago

I would set eval.secure to unix::eval_safe directly

jeroen commented 3 years ago

Thanks!