2023-07-13 14:30:44

by Aleksa Sarai

[permalink] [raw]
Subject: [PATCH v2] procfs: block chmod on /proc/thread-self/comm

Due to an oversight in commit 1b3044e39a89 ("procfs: fix pthread
cross-thread naming if !PR_DUMPABLE") in switching from REG to NOD,
chmod operations on /proc/thread-self/comm were no longer blocked as
they are on almost all other procfs files.

A very similar situation with /proc/self/environ was used to as a root
exploit a long time ago, but procfs has SB_I_NOEXEC so this is simply a
correctness issue.

Ref: https://lwn.net/Articles/191954/
Ref: 6d76fa58b050 ("Don't allow chmod() on the /proc/<pid>/ files")
Fixes: 1b3044e39a89 ("procfs: fix pthread cross-thread naming if !PR_DUMPABLE")
Cc: [email protected] # v4.7+
Signed-off-by: Aleksa Sarai <[email protected]>
---
v2: removed nolibc selftests as per review
v1: <https://lore.kernel.org/linux-fsdevel/[email protected]/>
---
fs/proc/base.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/fs/proc/base.c b/fs/proc/base.c
index 05452c3b9872..7394229816f3 100644
--- a/fs/proc/base.c
+++ b/fs/proc/base.c
@@ -3583,7 +3583,8 @@ static int proc_tid_comm_permission(struct mnt_idmap *idmap,
}

static const struct inode_operations proc_tid_comm_inode_operations = {
- .permission = proc_tid_comm_permission,
+ .setattr = proc_setattr,
+ .permission = proc_tid_comm_permission,
};

/*
--
2.41.0



2023-07-13 15:30:48

by Christian Brauner

[permalink] [raw]
Subject: Re: [PATCH v2] procfs: block chmod on /proc/thread-self/comm

On Fri, 14 Jul 2023 00:09:58 +1000, Aleksa Sarai wrote:
> Due to an oversight in commit 1b3044e39a89 ("procfs: fix pthread
> cross-thread naming if !PR_DUMPABLE") in switching from REG to NOD,
> chmod operations on /proc/thread-self/comm were no longer blocked as
> they are on almost all other procfs files.
>
> A very similar situation with /proc/self/environ was used to as a root
> exploit a long time ago, but procfs has SB_I_NOEXEC so this is simply a
> correctness issue.
>
> [...]

Just to reiterate: The long term fix to avoid these odd behavioral bugs
in the future is to remove the fallback to simple_setattr() we still
have in notify_change() that happens when no setattr inode operation has
been explicitly defined. To do this we need to change all filesystem
that rely on this fallback to explicitly set simple_setattr() as their
inode operation. Then notify_change() would simply return EOPNOTSUPP
when no setattr iop is defined making such omissions pretty obvious.

But that's a bigger patch. This is a backportable fix for this issue.
Needs long soaking in -next ofc.

---

Applied to the fs.proc.uapi branch of the vfs/vfs.git tree.
Patches in the fs.proc.uapi branch should appear in linux-next soon.

Please report any outstanding bugs that were missed during review in a
new review to the original patch series allowing us to drop it.

It's encouraged to provide Acked-bys and Reviewed-bys even though the
patch has now been applied. If possible patch trailers will be updated.

Note that commit hashes shown below are subject to change due to rebase,
trailer updates or similar. If in doubt, please check the listed branch.

tree: https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git
branch: fs.proc.uapi

[1/1] procfs: block chmod on /proc/thread-self/comm
https://git.kernel.org/vfs/vfs/c/ccf61486fe1e