exec: don't WARN for racy path_noexec check

Both i_mode and noexec checks wrapped in WARN_ON stem from an artifact
of the previous implementation. They used to legitimately check for the
condition, but that got moved up in two commits:
633fb6ac39 ("exec: move S_ISREG() check earlier")
0fd338b2d2 ("exec: move path_noexec() check earlier")

Instead of being removed said checks are WARN_ON'ed instead, which
has some debug value.

However, the spurious path_noexec check is racy, resulting in
unwarranted warnings should someone race with setting the noexec flag.

One can note there is more to perm-checking whether execve is allowed
and none of the conditions are guaranteed to still hold after they were
tested for.

Additionally this does not validate whether the code path did any perm
checking to begin with -- it will pass if the inode happens to be
regular.

Keep the redundant path_noexec() check even though it's mindless
nonsense checking for guarantee that isn't given so drop the WARN.

Reword the commentary and do small tidy ups while here.

Signed-off-by: Mateusz Guzik <mjguzik@gmail.com>
Link: https://lore.kernel.org/r/20240805131721.765484-1-mjguzik@gmail.com
[brauner: keep redundant path_noexec() check]
Signed-off-by: Christian Brauner <brauner@kernel.org>
This commit is contained in:
Mateusz Guzik 2024-08-05 15:17:21 +02:00 committed by Christian Brauner
parent 2ce051550f
commit 16febd5eb3

View File

@ -145,13 +145,11 @@ SYSCALL_DEFINE1(uselib, const char __user *, library)
goto out; goto out;
/* /*
* may_open() has already checked for this, so it should be * Check do_open_execat() for an explanation.
* impossible to trip now. But we need to be extra cautious
* and check again at the very end too.
*/ */
error = -EACCES; error = -EACCES;
if (WARN_ON_ONCE(!S_ISREG(file_inode(file)->i_mode) || if (WARN_ON_ONCE(!S_ISREG(file_inode(file)->i_mode)) ||
path_noexec(&file->f_path))) path_noexec(&file->f_path))
goto exit; goto exit;
error = -ENOEXEC; error = -ENOEXEC;
@ -954,7 +952,6 @@ EXPORT_SYMBOL(transfer_args_to_stack);
static struct file *do_open_execat(int fd, struct filename *name, int flags) static struct file *do_open_execat(int fd, struct filename *name, int flags)
{ {
struct file *file; struct file *file;
int err;
struct open_flags open_exec_flags = { struct open_flags open_exec_flags = {
.open_flag = O_LARGEFILE | O_RDONLY | __FMODE_EXEC, .open_flag = O_LARGEFILE | O_RDONLY | __FMODE_EXEC,
.acc_mode = MAY_EXEC, .acc_mode = MAY_EXEC,
@ -971,24 +968,20 @@ static struct file *do_open_execat(int fd, struct filename *name, int flags)
file = do_filp_open(fd, name, &open_exec_flags); file = do_filp_open(fd, name, &open_exec_flags);
if (IS_ERR(file)) if (IS_ERR(file))
goto out; return file;
/* /*
* may_open() has already checked for this, so it should be * In the past the regular type check was here. It moved to may_open() in
* impossible to trip now. But we need to be extra cautious * 633fb6ac3980 ("exec: move S_ISREG() check earlier"). Since then it is
* and check again at the very end too. * an invariant that all non-regular files error out before we get here.
*/ */
err = -EACCES; if (WARN_ON_ONCE(!S_ISREG(file_inode(file)->i_mode)) ||
if (WARN_ON_ONCE(!S_ISREG(file_inode(file)->i_mode) || path_noexec(&file->f_path)) {
path_noexec(&file->f_path))) fput(file);
goto exit; return ERR_PTR(-EACCES);
}
out:
return file; return file;
exit:
fput(file);
return ERR_PTR(err);
} }
/** /**