From: Alexey Dobriyan <adobriyan@gmail.com>
To: Munehisa Kamata <kamatam@amazon.com>
Cc: linux-fsdevel@vger.kernel.org,
linux-security-module@vger.kernel.org,
Andrew Morton <akpm@linux-foundation.org>
Subject: Re: Fw: [PATCH] proc: Update inode upon changing task security attribute
Date: Fri, 1 Dec 2023 12:30:00 +0300 [thread overview]
Message-ID: <5f8b18b0-0744-4cf5-9ec5-b0bb0451dd18@p183> (raw)
In-Reply-To: <20231129171122.0171313079ea3afa84762d90@linux-foundation.org>
On Wed, Nov 29, 2023 at 05:11:22PM -0800, Andrew Morton wrote:
>
> fyi...
>
> (yuk!)
>
>
>
> Begin forwarded message:
>
> Date: Thu, 30 Nov 2023 00:37:04 +0000
> From: Munehisa Kamata <kamatam@amazon.com>
> To: <linux-fsdevel@vger.kernel.org>, <linux-security-module@vger.kernel.org>
> Cc: <linux-kernel@vger.kernel.org>, <akpm@linux-foundation.org>, "Munehisa Kamata" <kamatam@amazon.com>
> Subject: [PATCH] proc: Update inode upon changing task security attribute
>
>
> I'm not clear whether VFS is a better (or worse) place[1] to fix the
> problem described below and would like to hear opinion.
>
> If the /proc/[pid] directory is bind-mounted on a system with Smack
> enabled, and if the task updates its current security attribute, the task
> may lose access to files in its own /proc/[pid] through the mountpoint.
>
> $ sudo capsh --drop=cap_mac_override --
> # mkdir -p dir
> # mount --bind /proc/$$ dir
> # echo AAA > /proc/$$/task/current # assuming built-in echo
> # cat /proc/$$/task/current # revalidate
> AAA
> # echo BBB > dir/attr/current
> # cat dir/attr/current
> cat: dir/attr/current: Permission denied
> # ls dir/
> ls: cannot access dir/: Permission denied
> # cat /proc/$$/attr/current # revalidate
> BBB
> # cat dir/attr/current
> BBB
> # echo CCC > /proc/$$/attr/current
> # cat dir/attr/current
> cat: dir/attr/current: Permission denied
>
> This happens because path lookup doesn't revalidate the dentry of the
> /proc/[pid] when traversing the filesystem boundary, so the inode security
> blob of the /proc/[pid] doesn't get updated with the new task security
> attribute. Then, this may lead security modules to deny an access to the
> directory. Looking at the code[2] and the /proc/pid/attr/current entry in
> proc man page, seems like the same could happen with SELinux. Though, I
> didn't find relevant reports.
>
> The steps above are quite artificial. I actually encountered such an
> unexpected denial of access with an in-house application sandbox
> framework; each app has its own dedicated filesystem tree where the
> process's /proc/[pid] is bind-mounted to and the app enters into via
> chroot.
>
> With this patch, writing to /proc/[pid]/attr/current (and its per-security
> module variant) updates the inode security blob of /proc/[pid] or
> /proc/[pid]/task/[tid] (when pid != tid) with the new attribute.
>
> [1] https://lkml.kernel.org/linux-fsdevel/4A2D15AF.8090000@sun.com/
> [2] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/security/selinux/hooks.c#n4220
>
> Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
> Signed-off-by: Munehisa Kamata <kamatam@amazon.com>
> ---
> fs/proc/base.c | 23 ++++++++++++++++++++---
> 1 file changed, 20 insertions(+), 3 deletions(-)
>
> diff --git a/fs/proc/base.c b/fs/proc/base.c
> index dd31e3b6bf77..bdb7bea53475 100644
> --- a/fs/proc/base.c
> +++ b/fs/proc/base.c
> @@ -2741,6 +2741,7 @@ static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
> {
> struct inode * inode = file_inode(file);
> struct task_struct *task;
> + const char *name = file->f_path.dentry->d_name.name;
> void *page;
> int rv;
>
> @@ -2784,10 +2785,26 @@ static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
> if (rv < 0)
> goto out_free;
>
> - rv = security_setprocattr(PROC_I(inode)->op.lsm,
> - file->f_path.dentry->d_name.name, page,
> - count);
> + rv = security_setprocattr(PROC_I(inode)->op.lsm, name, page, count);
> mutex_unlock(¤t->signal->cred_guard_mutex);
> +
> + /*
> + * Update the inode security blob in advance if the task's security
> + * attribute was updated
> + */
> + if (rv > 0 && !strcmp(name, "current")) {
> + struct pid *pid;
> + struct proc_inode *cur, *ei;
> +
> + rcu_read_lock();
> + pid = get_task_pid(current, PIDTYPE_PID);
> + hlist_for_each_entry(cur, &pid->inodes, sibling_inodes)
> + ei = cur;
Should this "break;"? Why is only the last inode in the list updated?
Should it be the first? All of them?
> + put_pid(pid);
> + pid_update_inode(current, &ei->vfs_inode);
> + rcu_read_unlock();
> + }
next parent reply other threads:[~2023-12-01 9:30 UTC|newest]
Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <20231129171122.0171313079ea3afa84762d90@linux-foundation.org>
2023-12-01 9:30 ` Alexey Dobriyan [this message]
2023-12-01 20:59 ` Fw: [PATCH] proc: Update inode upon changing task security attribute Munehisa Kamata
2023-12-01 21:42 ` Casey Schaufler
2023-12-05 22:21 ` Paul Moore
2023-12-05 22:31 ` Casey Schaufler
2023-12-08 2:14 ` Munehisa Kamata
2023-12-08 22:43 ` Paul Moore
2023-12-08 23:21 ` Casey Schaufler
2023-12-08 23:32 ` Paul Moore
2023-12-09 0:24 ` Casey Schaufler
2023-12-09 1:10 ` Munehisa Kamata
2023-12-09 18:10 ` Paul Moore
2023-12-09 21:17 ` Munehisa Kamata
2023-12-10 21:52 ` Paul Moore
2023-12-10 14:45 ` Serge E. Hallyn
2023-12-11 19:27 ` Munehisa Kamata
2023-12-11 19:49 ` Serge E. Hallyn
2023-12-09 18:08 ` Paul Moore
2023-12-09 18:35 ` Casey Schaufler
2023-12-09 22:44 ` Munehisa Kamata
2023-12-10 21:45 ` Paul Moore
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=5f8b18b0-0744-4cf5-9ec5-b0bb0451dd18@p183 \
--to=adobriyan@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=kamatam@amazon.com \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-security-module@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).