Message ID | 20231130003704.31928-1-kamatam@amazon.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp68420vqy; Wed, 29 Nov 2023 16:37:26 -0800 (PST) X-Google-Smtp-Source: AGHT+IEGAKEO8OUpC1KalxRSDCC4RcQxyBlbmA8PP4vn4Llbx7SsIRZ4WcdKY+L6tBTpXs82JvTK X-Received: by 2002:a05:6a00:488a:b0:6c4:d4b1:b535 with SMTP id dk10-20020a056a00488a00b006c4d4b1b535mr27214903pfb.9.1701304646392; Wed, 29 Nov 2023 16:37:26 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701304646; cv=none; d=google.com; s=arc-20160816; b=GFHGpsvJW09C9OAiqQrv7WapsLXR71Ze9t2qgk6fZV3PumCR1vfH8RB/bmPwsoD+KZ 6SC9TAXBykz4zvq4vk1HDJjXhMPbW72z1WLBCRcA3Ya7+5A6kuYlz+wkqAhP1qUzKuir MWBOnt4koewXQKnyYiIgT/WPJlE3DMQW92kwSUymWVRp2jHrawJQQ0pjEDhhieZw8AAF owXLj7CVA59btLt7WRMg5/pIDKG24NwPshEX4b03v7DSYXAQxoUHVQO6/fghMu3wXzfR Hx3XFWlEmrfcHZtvambz4jYjaQLiyKSSUh6m6xJ7nHMjzqMG2XLuvdP0RyE50Xd6SYor xl5A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=gZHtxMLuq2rTH5TQ4Whbm6hr8hi2bU/215bnbJKdOTo=; fh=P1QsLULQBTz7LxsZ3AqR/yzAmFY2A5wPtMv4odF0apQ=; b=JhqPKxfVeAy8u7cy4VpoE3iqlXutl2uO0Y2auJ5ik0srPSnD6UuAzrLTfzCQxovd54 55BKkSHoRID+lTzcbzPIEPgA5MPfFhhbRYJTsUJ/0AiaQpcosU5ay3dU5lbfbeeibTjD bqmpnaFRFURhwOUqVjS4Md/ixU4/vIChWJOHqINwL+fjTxgsfAAiFQuQQW58JZxOL8Wv SdA060yhH9ACb+MCmotwdQQeA7cbJgraWzOlk3Y6ntl7fu+BpQNMx2BfXx8j5wQ3oPw2 xaTgbsSyp03psSL0UT3dQpnGAn7FirL/9lyQGVejBJ9FAgqc+tfx/AVOd3BET45IB4/3 VFVw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=SNlQhdaR; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id bs192-20020a6328c9000000b005c20241fdf2si68072pgb.49.2023.11.29.16.37.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 29 Nov 2023 16:37:26 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=SNlQhdaR; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 124E7802C6A8; Wed, 29 Nov 2023 16:37:25 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234913AbjK3AhP (ORCPT <rfc822;realc9580@gmail.com> + 99 others); Wed, 29 Nov 2023 19:37:15 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54058 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229658AbjK3AhO (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Wed, 29 Nov 2023 19:37:14 -0500 Received: from smtp-fw-2101.amazon.com (smtp-fw-2101.amazon.com [72.21.196.25]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 37A81BC; Wed, 29 Nov 2023 16:37:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1701304640; x=1732840640; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=gZHtxMLuq2rTH5TQ4Whbm6hr8hi2bU/215bnbJKdOTo=; b=SNlQhdaRCh9tMWNatxvpPK6iyuIXqVlqYtTwrW4gmC/10NXaWYghZtcc 2aJcTh0c4dQox6s/5yp/mZlNnnUorsEvCC4shbqUhZShrpFgANiBrPT08 xSC1JNER7CAihcYvf0pvkyYpx2GIQXE93f/MSqOmSoNtJHc4ImG4CZtBd M=; X-IronPort-AV: E=Sophos;i="6.04,237,1695686400"; d="scan'208";a="365700991" Received: from iad12-co-svc-p1-lb1-vlan3.amazon.com (HELO email-inbound-relay-iad-1a-m6i4x-9fe6ad2f.us-east-1.amazon.com) ([10.43.8.6]) by smtp-border-fw-2101.iad2.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Nov 2023 00:37:19 +0000 Received: from smtpout.prod.us-west-2.prod.farcaster.email.amazon.dev (iad7-ws-svc-p70-lb3-vlan3.iad.amazon.com [10.32.235.38]) by email-inbound-relay-iad-1a-m6i4x-9fe6ad2f.us-east-1.amazon.com (Postfix) with ESMTPS id 72524803F3; Thu, 30 Nov 2023 00:37:18 +0000 (UTC) Received: from EX19MTAUWB002.ant.amazon.com [10.0.38.20:44372] by smtpin.naws.us-west-2.prod.farcaster.email.amazon.dev [10.0.54.99:2525] with esmtp (Farcaster) id 28d82deb-a504-4c09-95be-bbb50a6ee114; Thu, 30 Nov 2023 00:37:17 +0000 (UTC) X-Farcaster-Flow-ID: 28d82deb-a504-4c09-95be-bbb50a6ee114 Received: from EX19D010UWA004.ant.amazon.com (10.13.138.204) by EX19MTAUWB002.ant.amazon.com (10.250.64.231) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.39; Thu, 30 Nov 2023 00:37:17 +0000 Received: from dev-dsk-kamatam-2b-b66a5860.us-west-2.amazon.com (10.169.6.191) by EX19D010UWA004.ant.amazon.com (10.13.138.204) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.39; Thu, 30 Nov 2023 00:37:17 +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 Date: Thu, 30 Nov 2023 00:37:04 +0000 Message-ID: <20231130003704.31928-1-kamatam@amazon.com> X-Mailer: git-send-email 2.40.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-Originating-IP: [10.169.6.191] X-ClientProxiedBy: EX19D044UWB003.ant.amazon.com (10.13.139.168) To EX19D010UWA004.ant.amazon.com (10.13.138.204) X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE, T_SCC_BODY_TEXT_LINE,UNPARSEABLE_RELAY autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Wed, 29 Nov 2023 16:37:25 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1783947221089065782 X-GMAIL-MSGID: 1783947221089065782 |
Series |
proc: Update inode upon changing task security attribute
|
|
Commit Message
Munehisa Kamata
Nov. 30, 2023, 12:37 a.m. UTC
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(-)
Comments
On 11/29/2023 4:37 PM, Munehisa Kamata wrote: > 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. Please To: or at least Cc: me on all Smack related issues. > > 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 I don't see "current" in /proc/$$/task. Did you mean /proc/$$/attr? > # 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; > + put_pid(pid); > + pid_update_inode(current, &ei->vfs_inode); > + rcu_read_unlock(); > + } > + > out_free: > kfree(page); > out:
Hi Casey, On Wed, 2023-11-29 18:28:55 -0800, Casey Schaufler wrote: > > On 11/29/2023 4:37 PM, Munehisa Kamata wrote: > > 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. > > Please To: or at least Cc: me on all Smack related issues. Will do that next. > > > > 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 > > I don't see "current" in /proc/$$/task. Did you mean /proc/$$/attr? Ahh, yes, I meant /proc/$$/attr/current. Sorry about that... > > # 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; > > + put_pid(pid); > > + pid_update_inode(current, &ei->vfs_inode); > > + rcu_read_unlock(); > > + } > > + > > out_free: > > kfree(page); > > out: >
On 11/29/2023 7:07 PM, Munehisa Kamata wrote: > Hi Casey, > > On Wed, 2023-11-29 18:28:55 -0800, Casey Schaufler wrote: >> On 11/29/2023 4:37 PM, Munehisa Kamata wrote: >>> 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. >> Please To: or at least Cc: me on all Smack related issues. > Will do that next. > >>> 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 >> I don't see "current" in /proc/$$/task. Did you mean /proc/$$/attr? > Ahh, yes, I meant /proc/$$/attr/current. Sorry about that... > >>> # 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 I don't see this behavior. What kernel version are you using? I have a 6.5 kernel. >>> # 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; >>> + put_pid(pid); >>> + pid_update_inode(current, &ei->vfs_inode); >>> + rcu_read_unlock(); >>> + } >>> + >>> out_free: >>> kfree(page); >>> out:
On Thu, 2023-11-30 18:00:13 +0000, Casey Schaufler wrote: > > On 11/29/2023 7:07 PM, Munehisa Kamata wrote: > > Hi Casey, > > > > On Wed, 2023-11-29 18:28:55 -0800, Casey Schaufler wrote: > >> On 11/29/2023 4:37 PM, Munehisa Kamata wrote: > >>> 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. > >> Please To: or at least Cc: me on all Smack related issues. > > Will do that next. > > > >>> 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 > >> I don't see "current" in /proc/$$/task. Did you mean /proc/$$/attr? > > Ahh, yes, I meant /proc/$$/attr/current. Sorry about that... > > > >>> # 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 > > I don't see this behavior. What kernel version are you using? > I have a 6.5 kernel. I verified the behavior with 6.7-rc3. Here is more "raw" log from my machine: [ec2-user@ip-10-0-32-198 ~]$ uname -r 6.7.0-rc3-proc-fix+ [ec2-user@ip-10-0-32-198 ~]$ sudo capsh --drop=cap_mac_override -- [root@ip-10-0-32-198 ec2-user]# mount --bind /proc/$$ dir [root@ip-10-0-32-198 ec2-user]# echo AAA > /proc/$$/attr/current [root@ip-10-0-32-198 ec2-user]# cat /proc/$$/attr/current; echo AAA [root@ip-10-0-32-198 ec2-user]# echo BBB > dir/attr/current [root@ip-10-0-32-198 ec2-user]# cat dir/attr/current cat: dir/attr/current: Permission denied If something frequently scans /proc, such as ps, top or whatever, on your machine, the inode may get updated quickly (i.e. revalidated during path lookup) and then you may only have a short window to observe the behavior. > >>> # 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; > >>> + put_pid(pid); > >>> + pid_update_inode(current, &ei->vfs_inode); > >>> + rcu_read_unlock(); > >>> + } > >>> + > >>> out_free: > >>> kfree(page); > >>> out: >
On 11/30/2023 12:35 PM, Munehisa Kamata wrote: > On Thu, 2023-11-30 18:00:13 +0000, Casey Schaufler wrote: >> On 11/29/2023 7:07 PM, Munehisa Kamata wrote: >>> Hi Casey, >>> >>> On Wed, 2023-11-29 18:28:55 -0800, Casey Schaufler wrote: >>>> On 11/29/2023 4:37 PM, Munehisa Kamata wrote: >>>>> 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. >>>> Please To: or at least Cc: me on all Smack related issues. >>> Will do that next. >>> >>>>> 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 >>>> I don't see "current" in /proc/$$/task. Did you mean /proc/$$/attr? >>> Ahh, yes, I meant /proc/$$/attr/current. Sorry about that... >>> >>>>> # 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 >> I don't see this behavior. What kernel version are you using? >> I have a 6.5 kernel. > I verified the behavior with 6.7-rc3. > > Here is more "raw" log from my machine: > > [ec2-user@ip-10-0-32-198 ~]$ uname -r > 6.7.0-rc3-proc-fix+ > [ec2-user@ip-10-0-32-198 ~]$ sudo capsh --drop=cap_mac_override -- > [root@ip-10-0-32-198 ec2-user]# mount --bind /proc/$$ dir > [root@ip-10-0-32-198 ec2-user]# echo AAA > /proc/$$/attr/current > [root@ip-10-0-32-198 ec2-user]# cat /proc/$$/attr/current; echo > AAA > [root@ip-10-0-32-198 ec2-user]# echo BBB > dir/attr/current > [root@ip-10-0-32-198 ec2-user]# cat dir/attr/current > cat: dir/attr/current: Permission denied > > If something frequently scans /proc, such as ps, top or whatever, on your > machine, the inode may get updated quickly (i.e. revalidated during path > lookup) and then you may only have a short window to observe the behavior. I was able to reproduce the issue with a 6.5 kernel. The window seems to be really short. Would it be completely unreasonable for your sandboxing application to call syncfs(2) after writing to current? > >>>>> # 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; >>>>> + put_pid(pid); >>>>> + pid_update_inode(current, &ei->vfs_inode); >>>>> + rcu_read_unlock(); >>>>> + } >>>>> + >>>>> out_free: >>>>> kfree(page); >>>>> out:
On Thu, 2023-11-30 16:31:11 -0800, Casey Schaufler wrote: > > On 11/30/2023 12:35 PM, Munehisa Kamata wrote: > > On Thu, 2023-11-30 18:00:13 +0000, Casey Schaufler wrote: > >> On 11/29/2023 7:07 PM, Munehisa Kamata wrote: > >>> Hi Casey, > >>> > >>> On Wed, 2023-11-29 18:28:55 -0800, Casey Schaufler wrote: > >>>> On 11/29/2023 4:37 PM, Munehisa Kamata wrote: > >>>>> 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. > >>>> Please To: or at least Cc: me on all Smack related issues. > >>> Will do that next. > >>> > >>>>> 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 > >>>> I don't see "current" in /proc/$$/task. Did you mean /proc/$$/attr? > >>> Ahh, yes, I meant /proc/$$/attr/current. Sorry about that... > >>> > >>>>> # 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 > >> I don't see this behavior. What kernel version are you using? > >> I have a 6.5 kernel. > > I verified the behavior with 6.7-rc3. > > > > Here is more "raw" log from my machine: > > > > [ec2-user@ip-10-0-32-198 ~]$ uname -r > > 6.7.0-rc3-proc-fix+ > > [ec2-user@ip-10-0-32-198 ~]$ sudo capsh --drop=cap_mac_override -- > > [root@ip-10-0-32-198 ec2-user]# mount --bind /proc/$$ dir > > [root@ip-10-0-32-198 ec2-user]# echo AAA > /proc/$$/attr/current > > [root@ip-10-0-32-198 ec2-user]# cat /proc/$$/attr/current; echo > > AAA > > [root@ip-10-0-32-198 ec2-user]# echo BBB > dir/attr/current > > [root@ip-10-0-32-198 ec2-user]# cat dir/attr/current > > cat: dir/attr/current: Permission denied > > > > If something frequently scans /proc, such as ps, top or whatever, on your > > machine, the inode may get updated quickly (i.e. revalidated during path > > lookup) and then you may only have a short window to observe the behavior. > > I was able to reproduce the issue with a 6.5 kernel. The window seems > to be really short. Creating a PID namespace before the bind-mount may make the window lasts longer (or forever). $ sudo unshare -pf --mount-proc > Would it be completely unreasonable for your sandboxing application to > call syncfs(2) after writing to current? It doesn't help. It won't revalidate dentries. > > > >>>>> # 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; > >>>>> + put_pid(pid); > >>>>> + pid_update_inode(current, &ei->vfs_inode); > >>>>> + rcu_read_unlock(); > >>>>> + } > >>>>> + > >>>>> out_free: > >>>>> kfree(page); > >>>>> out: >
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; + put_pid(pid); + pid_update_inode(current, &ei->vfs_inode); + rcu_read_unlock(); + } + out_free: kfree(page); out: