Linux Security Modules development
 help / color / mirror / Atom feed
* [PATCH 0/1] selinux,smack: don't bypass permissions check in inode_setsecctx hook
@ 2024-08-28 19:51 Scott Mayhew
  2024-08-28 19:51 ` [PATCH 1/1] " Scott Mayhew
  0 siblings, 1 reply; 12+ messages in thread
From: Scott Mayhew @ 2024-08-28 19:51 UTC (permalink / raw)
  To: paul, stephen.smalley.work, casey
  Cc: chuck.lever, marek.gresko, selinux, linux-security-module,
	linux-nfs

Marek Gresko reports that the root user on an NFS client is able to
change the security labels on files on an NFS filesystem that is
exported with root squashing enabled.

I wasn't able to do a bisect on this issue... partially because when I
go back far enough I have trouble building a kernel that actually boots
in a KVM guest, but also because not all of the related code landed in
mainline at the same time (more on that in a bit).

Although I wasn't able to do a bisect, I do believe this behavior goes
all the way back to the introduction of labeled NFS.  I was able to
reproduce this behavior on all versions of RHEL, going all the way back
to RHEL 7.0, which was based on a 3.10 kernel with post-3.10 code
(including the labeled NFS code) backported on top.

The v1 posting of the labeled NFS patchset actually mentions a similar
issue related to root squashing:
https://lore.kernel.org/lkml/7e0fb38c0802280622o75a474deg38157ff6aace16b@mail.gmail.com/t/

There is no mention of root squashing issues (fixed or otherwise) in
subsequent postings.

The inode_setsecctx hooks first appear in v2:
https://lore.kernel.org/all/1221511278-28051-1-git-send-email-dpquigl@tycho.nsa.gov/
The posting mentions other mailing list discussions related to the
hooks, but I'm not really able to find anything specific on why the
caller of the setsecctx hook would be expected to perform the
permissions checking rather than doing it in the hook itself.

The inode_setsecctx hook was merged in September 2009 (merge commit
f6f79190866d).  It looks like this was for labeling support on sysfs,
which used the inode_getsecctx and inode_notifysecctx hooks, but not the
inode_setsecctx hook.  It doesn't look like there was any user of the
inode_setsecctx hook until the labeled NFS code was merged in July 2013
(merge commit 0ff08ba5d066).

The end of the kerneldoc comment for __vfs_setxattr_noperm() states:

 *  This function requires the caller to lock the inode's i_mutex before it
 *  is executed. It also assumes that the caller will make the appropriate
 *  permission checks.

nfsd_setattr() does do permissions checking via fh_verify() and
nfsd_permission(), but those don't do all the same permissions checks
that are done by security_inode_setxattr() and its related LSM hooks do.

Since nfsd_setattr() is the only consumer of security_inode_setsecctx(),
simplest solution appears to be to replace the call to
__vfs_setxattr_noperm() with a call to __vfs_setxattr_locked().  This
fixes the above issue and has the added benefit of causing nfsd to
recall conflicting delegations on a file when a client tries to change
its security label.

I guess it's also worth mentioning that when nfsd rejects root's attempt
to set a label on a file on a root squashed export, the error on the
wire is NFS4ERR_PERM rather than NFS4ERR_ACCESS.

-Scott

Scott Mayhew (1):
  selinux,smack: don't bypass permissions check in inode_setsecctx hook

 security/selinux/hooks.c   | 4 ++--
 security/smack/smack_lsm.c | 4 ++--
 2 files changed, 4 insertions(+), 4 deletions(-)

-- 
2.46.0


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [PATCH 1/1] selinux,smack: don't bypass permissions check in inode_setsecctx hook
  2024-08-28 19:51 [PATCH 0/1] selinux,smack: don't bypass permissions check in inode_setsecctx hook Scott Mayhew
@ 2024-08-28 19:51 ` Scott Mayhew
  2024-08-28 20:03   ` Jeff Layton
                     ` (3 more replies)
  0 siblings, 4 replies; 12+ messages in thread
From: Scott Mayhew @ 2024-08-28 19:51 UTC (permalink / raw)
  To: paul, stephen.smalley.work, casey
  Cc: chuck.lever, marek.gresko, selinux, linux-security-module,
	linux-nfs

Marek Gresko reports that the root user on an NFS client is able to
change the security labels on files on an NFS filesystem that is
exported with root squashing enabled.

The end of the kerneldoc comment for __vfs_setxattr_noperm() states:

 *  This function requires the caller to lock the inode's i_mutex before it
 *  is executed. It also assumes that the caller will make the appropriate
 *  permission checks.

nfsd_setattr() does do permissions checking via fh_verify() and
nfsd_permission(), but those don't do all the same permissions checks
that are done by security_inode_setxattr() and its related LSM hooks do.

Since nfsd_setattr() is the only consumer of security_inode_setsecctx(),
simplest solution appears to be to replace the call to
__vfs_setxattr_noperm() with a call to __vfs_setxattr_locked().  This
fixes the above issue and has the added benefit of causing nfsd to
recall conflicting delegations on a file when a client tries to change
its security label.

Reported-by: Marek Gresko <marek.gresko@protonmail.com>
Link: https://bugzilla.kernel.org/show_bug.cgi?id=218809
Signed-off-by: Scott Mayhew <smayhew@redhat.com>
---
 security/selinux/hooks.c   | 4 ++--
 security/smack/smack_lsm.c | 4 ++--
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
index bfa61e005aac..400eca4ad0fb 100644
--- a/security/selinux/hooks.c
+++ b/security/selinux/hooks.c
@@ -6660,8 +6660,8 @@ static int selinux_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen
  */
 static int selinux_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
 {
-	return __vfs_setxattr_noperm(&nop_mnt_idmap, dentry, XATTR_NAME_SELINUX,
-				     ctx, ctxlen, 0);
+	return __vfs_setxattr_locked(&nop_mnt_idmap, dentry, XATTR_NAME_SELINUX,
+				     ctx, ctxlen, 0, NULL);
 }
 
 static int selinux_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
diff --git a/security/smack/smack_lsm.c b/security/smack/smack_lsm.c
index 4164699cd4f6..002a1b9ed83a 100644
--- a/security/smack/smack_lsm.c
+++ b/security/smack/smack_lsm.c
@@ -4880,8 +4880,8 @@ static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
 
 static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
 {
-	return __vfs_setxattr_noperm(&nop_mnt_idmap, dentry, XATTR_NAME_SMACK,
-				     ctx, ctxlen, 0);
+	return __vfs_setxattr_locked(&nop_mnt_idmap, dentry, XATTR_NAME_SMACK,
+				     ctx, ctxlen, 0, NULL);
 }
 
 static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
-- 
2.46.0


^ permalink raw reply related	[flat|nested] 12+ messages in thread

* Re: [PATCH 1/1] selinux,smack: don't bypass permissions check in inode_setsecctx hook
  2024-08-28 19:51 ` [PATCH 1/1] " Scott Mayhew
@ 2024-08-28 20:03   ` Jeff Layton
  2024-08-28 21:05   ` Paul Moore
                     ` (2 subsequent siblings)
  3 siblings, 0 replies; 12+ messages in thread
From: Jeff Layton @ 2024-08-28 20:03 UTC (permalink / raw)
  To: Scott Mayhew, paul, stephen.smalley.work, casey
  Cc: chuck.lever, marek.gresko, selinux, linux-security-module,
	linux-nfs

On Wed, 2024-08-28 at 15:51 -0400, Scott Mayhew wrote:
> Marek Gresko reports that the root user on an NFS client is able to
> change the security labels on files on an NFS filesystem that is
> exported with root squashing enabled.
> 
> The end of the kerneldoc comment for __vfs_setxattr_noperm() states:
> 
>  *  This function requires the caller to lock the inode's i_mutex before it
>  *  is executed. It also assumes that the caller will make the appropriate
>  *  permission checks.
> 
> nfsd_setattr() does do permissions checking via fh_verify() and
> nfsd_permission(), but those don't do all the same permissions checks
> that are done by security_inode_setxattr() and its related LSM hooks do.
> 
> Since nfsd_setattr() is the only consumer of security_inode_setsecctx(),
> simplest solution appears to be to replace the call to
> __vfs_setxattr_noperm() with a call to __vfs_setxattr_locked().  This
> fixes the above issue and has the added benefit of causing nfsd to
> recall conflicting delegations on a file when a client tries to change
> its security label.
> 
> Reported-by: Marek Gresko <marek.gresko@protonmail.com>
> Link: https://bugzilla.kernel.org/show_bug.cgi?id=218809
> Signed-off-by: Scott Mayhew <smayhew@redhat.com>
> ---
>  security/selinux/hooks.c   | 4 ++--
>  security/smack/smack_lsm.c | 4 ++--
>  2 files changed, 4 insertions(+), 4 deletions(-)
> 
> diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
> index bfa61e005aac..400eca4ad0fb 100644
> --- a/security/selinux/hooks.c
> +++ b/security/selinux/hooks.c
> @@ -6660,8 +6660,8 @@ static int selinux_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen
>   */
>  static int selinux_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
>  {
> -	return __vfs_setxattr_noperm(&nop_mnt_idmap, dentry, XATTR_NAME_SELINUX,
> -				     ctx, ctxlen, 0);
> +	return __vfs_setxattr_locked(&nop_mnt_idmap, dentry, XATTR_NAME_SELINUX,
> +				     ctx, ctxlen, 0, NULL);
>  }
>  
>  static int selinux_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
> diff --git a/security/smack/smack_lsm.c b/security/smack/smack_lsm.c
> index 4164699cd4f6..002a1b9ed83a 100644
> --- a/security/smack/smack_lsm.c
> +++ b/security/smack/smack_lsm.c
> @@ -4880,8 +4880,8 @@ static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
>  
>  static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
>  {
> -	return __vfs_setxattr_noperm(&nop_mnt_idmap, dentry, XATTR_NAME_SMACK,
> -				     ctx, ctxlen, 0);
> +	return __vfs_setxattr_locked(&nop_mnt_idmap, dentry, XATTR_NAME_SMACK,
> +				     ctx, ctxlen, 0, NULL);
>  }
>  
>  static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)

Acked-by: Jeff Layton <jlayton@kernel.org>

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH 1/1] selinux,smack: don't bypass permissions check in inode_setsecctx hook
  2024-08-28 19:51 ` [PATCH 1/1] " Scott Mayhew
  2024-08-28 20:03   ` Jeff Layton
@ 2024-08-28 21:05   ` Paul Moore
  2024-08-28 21:28     ` Casey Schaufler
  2024-08-28 23:19     ` Paul Moore
  2024-08-28 21:08   ` Chuck Lever
  2024-08-29 11:15   ` Jeff Layton
  3 siblings, 2 replies; 12+ messages in thread
From: Paul Moore @ 2024-08-28 21:05 UTC (permalink / raw)
  To: Scott Mayhew
  Cc: stephen.smalley.work, casey, chuck.lever, marek.gresko, selinux,
	linux-security-module, linux-nfs

On Wed, Aug 28, 2024 at 3:51 PM Scott Mayhew <smayhew@redhat.com> wrote:
>
> Marek Gresko reports that the root user on an NFS client is able to
> change the security labels on files on an NFS filesystem that is
> exported with root squashing enabled.
>
> The end of the kerneldoc comment for __vfs_setxattr_noperm() states:
>
>  *  This function requires the caller to lock the inode's i_mutex before it
>  *  is executed. It also assumes that the caller will make the appropriate
>  *  permission checks.
>
> nfsd_setattr() does do permissions checking via fh_verify() and
> nfsd_permission(), but those don't do all the same permissions checks
> that are done by security_inode_setxattr() and its related LSM hooks do.
>
> Since nfsd_setattr() is the only consumer of security_inode_setsecctx(),
> simplest solution appears to be to replace the call to
> __vfs_setxattr_noperm() with a call to __vfs_setxattr_locked().  This
> fixes the above issue and has the added benefit of causing nfsd to
> recall conflicting delegations on a file when a client tries to change
> its security label.
>
> Reported-by: Marek Gresko <marek.gresko@protonmail.com>
> Link: https://bugzilla.kernel.org/show_bug.cgi?id=218809
> Signed-off-by: Scott Mayhew <smayhew@redhat.com>
> ---
>  security/selinux/hooks.c   | 4 ++--
>  security/smack/smack_lsm.c | 4 ++--
>  2 files changed, 4 insertions(+), 4 deletions(-)

Thanks Scott, this looks good to me, but since it touches Smack too
I'd also like to get Casey's ACK on this patch; if for some reason we
don't hear from Casey after a bit I'll go ahead and merge it.
Speaking of merging, since this touches both SELinux and Smack I'll
likely pull this in via the LSM tree, with a marking for the stable
kernels, if anyone has any objections to that please let me know.

-- 
paul-moore.com

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH 1/1] selinux,smack: don't bypass permissions check in inode_setsecctx hook
  2024-08-28 19:51 ` [PATCH 1/1] " Scott Mayhew
  2024-08-28 20:03   ` Jeff Layton
  2024-08-28 21:05   ` Paul Moore
@ 2024-08-28 21:08   ` Chuck Lever
  2024-08-29 11:15   ` Jeff Layton
  3 siblings, 0 replies; 12+ messages in thread
From: Chuck Lever @ 2024-08-28 21:08 UTC (permalink / raw)
  To: Scott Mayhew
  Cc: paul, stephen.smalley.work, casey, marek.gresko, selinux,
	linux-security-module, linux-nfs

On Wed, Aug 28, 2024 at 03:51:29PM -0400, Scott Mayhew wrote:
> Marek Gresko reports that the root user on an NFS client is able to
> change the security labels on files on an NFS filesystem that is
> exported with root squashing enabled.
> 
> The end of the kerneldoc comment for __vfs_setxattr_noperm() states:
> 
>  *  This function requires the caller to lock the inode's i_mutex before it
>  *  is executed. It also assumes that the caller will make the appropriate
>  *  permission checks.
> 
> nfsd_setattr() does do permissions checking via fh_verify() and
> nfsd_permission(), but those don't do all the same permissions checks
> that are done by security_inode_setxattr() and its related LSM hooks do.
> 
> Since nfsd_setattr() is the only consumer of security_inode_setsecctx(),
> simplest solution appears to be to replace the call to
> __vfs_setxattr_noperm() with a call to __vfs_setxattr_locked().  This
> fixes the above issue and has the added benefit of causing nfsd to
> recall conflicting delegations on a file when a client tries to change
> its security label.
> 
> Reported-by: Marek Gresko <marek.gresko@protonmail.com>
> Link: https://bugzilla.kernel.org/show_bug.cgi?id=218809
> Signed-off-by: Scott Mayhew <smayhew@redhat.com>
> ---
>  security/selinux/hooks.c   | 4 ++--
>  security/smack/smack_lsm.c | 4 ++--
>  2 files changed, 4 insertions(+), 4 deletions(-)
> 
> diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
> index bfa61e005aac..400eca4ad0fb 100644
> --- a/security/selinux/hooks.c
> +++ b/security/selinux/hooks.c
> @@ -6660,8 +6660,8 @@ static int selinux_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen
>   */
>  static int selinux_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
>  {
> -	return __vfs_setxattr_noperm(&nop_mnt_idmap, dentry, XATTR_NAME_SELINUX,
> -				     ctx, ctxlen, 0);
> +	return __vfs_setxattr_locked(&nop_mnt_idmap, dentry, XATTR_NAME_SELINUX,
> +				     ctx, ctxlen, 0, NULL);
>  }
>  
>  static int selinux_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
> diff --git a/security/smack/smack_lsm.c b/security/smack/smack_lsm.c
> index 4164699cd4f6..002a1b9ed83a 100644
> --- a/security/smack/smack_lsm.c
> +++ b/security/smack/smack_lsm.c
> @@ -4880,8 +4880,8 @@ static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
>  
>  static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
>  {
> -	return __vfs_setxattr_noperm(&nop_mnt_idmap, dentry, XATTR_NAME_SMACK,
> -				     ctx, ctxlen, 0);
> +	return __vfs_setxattr_locked(&nop_mnt_idmap, dentry, XATTR_NAME_SMACK,
> +				     ctx, ctxlen, 0, NULL);
>  }
>  
>  static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
> -- 
> 2.46.0
> 

Nice, thorough work, Scott.

Reviewed-by: Chuck Lever <chuck.lever@oracle.com>

-- 
Chuck Lever

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH 1/1] selinux,smack: don't bypass permissions check in inode_setsecctx hook
  2024-08-28 21:05   ` Paul Moore
@ 2024-08-28 21:28     ` Casey Schaufler
  2024-08-28 23:08       ` Paul Moore
  2024-08-28 23:19     ` Paul Moore
  1 sibling, 1 reply; 12+ messages in thread
From: Casey Schaufler @ 2024-08-28 21:28 UTC (permalink / raw)
  To: Paul Moore, Scott Mayhew
  Cc: stephen.smalley.work, chuck.lever, marek.gresko, selinux,
	linux-security-module, linux-nfs, Casey Schaufler

On 8/28/2024 2:05 PM, Paul Moore wrote:
> On Wed, Aug 28, 2024 at 3:51 PM Scott Mayhew <smayhew@redhat.com> wrote:
>> Marek Gresko reports that the root user on an NFS client is able to
>> change the security labels on files on an NFS filesystem that is
>> exported with root squashing enabled.
>>
>> The end of the kerneldoc comment for __vfs_setxattr_noperm() states:
>>
>>  *  This function requires the caller to lock the inode's i_mutex before it
>>  *  is executed. It also assumes that the caller will make the appropriate
>>  *  permission checks.
>>
>> nfsd_setattr() does do permissions checking via fh_verify() and
>> nfsd_permission(), but those don't do all the same permissions checks
>> that are done by security_inode_setxattr() and its related LSM hooks do.
>>
>> Since nfsd_setattr() is the only consumer of security_inode_setsecctx(),
>> simplest solution appears to be to replace the call to
>> __vfs_setxattr_noperm() with a call to __vfs_setxattr_locked().  This
>> fixes the above issue and has the added benefit of causing nfsd to
>> recall conflicting delegations on a file when a client tries to change
>> its security label.
>>
>> Reported-by: Marek Gresko <marek.gresko@protonmail.com>
>> Link: https://bugzilla.kernel.org/show_bug.cgi?id=218809
>> Signed-off-by: Scott Mayhew <smayhew@redhat.com>
>> ---
>>  security/selinux/hooks.c   | 4 ++--
>>  security/smack/smack_lsm.c | 4 ++--
>>  2 files changed, 4 insertions(+), 4 deletions(-)
> Thanks Scott, this looks good to me, but since it touches Smack too
> I'd also like to get Casey's ACK on this patch;

Testing labeled NFS has always been a challenge for the somewhat
limited resources available to the Smack project. I'll Ack the patch,
but won't claim to have tested it.

>  if for some reason we
> don't hear from Casey after a bit I'll go ahead and merge it.
> Speaking of merging, since this touches both SELinux and Smack I'll
> likely pull this in via the LSM tree, with a marking for the stable
> kernels, if anyone has any objections to that please let me know.
>

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH 1/1] selinux,smack: don't bypass permissions check in inode_setsecctx hook
  2024-08-28 21:28     ` Casey Schaufler
@ 2024-08-28 23:08       ` Paul Moore
  0 siblings, 0 replies; 12+ messages in thread
From: Paul Moore @ 2024-08-28 23:08 UTC (permalink / raw)
  To: Casey Schaufler
  Cc: Scott Mayhew, stephen.smalley.work, chuck.lever, marek.gresko,
	selinux, linux-security-module, linux-nfs

On Wed, Aug 28, 2024 at 5:29 PM Casey Schaufler <casey@schaufler-ca.com> wrote:
> On 8/28/2024 2:05 PM, Paul Moore wrote:
> > On Wed, Aug 28, 2024 at 3:51 PM Scott Mayhew <smayhew@redhat.com> wrote:
> >> Marek Gresko reports that the root user on an NFS client is able to
> >> change the security labels on files on an NFS filesystem that is
> >> exported with root squashing enabled.
> >>
> >> The end of the kerneldoc comment for __vfs_setxattr_noperm() states:
> >>
> >>  *  This function requires the caller to lock the inode's i_mutex before it
> >>  *  is executed. It also assumes that the caller will make the appropriate
> >>  *  permission checks.
> >>
> >> nfsd_setattr() does do permissions checking via fh_verify() and
> >> nfsd_permission(), but those don't do all the same permissions checks
> >> that are done by security_inode_setxattr() and its related LSM hooks do.
> >>
> >> Since nfsd_setattr() is the only consumer of security_inode_setsecctx(),
> >> simplest solution appears to be to replace the call to
> >> __vfs_setxattr_noperm() with a call to __vfs_setxattr_locked().  This
> >> fixes the above issue and has the added benefit of causing nfsd to
> >> recall conflicting delegations on a file when a client tries to change
> >> its security label.
> >>
> >> Reported-by: Marek Gresko <marek.gresko@protonmail.com>
> >> Link: https://bugzilla.kernel.org/show_bug.cgi?id=218809
> >> Signed-off-by: Scott Mayhew <smayhew@redhat.com>
> >> ---
> >>  security/selinux/hooks.c   | 4 ++--
> >>  security/smack/smack_lsm.c | 4 ++--
> >>  2 files changed, 4 insertions(+), 4 deletions(-)
> > Thanks Scott, this looks good to me, but since it touches Smack too
> > I'd also like to get Casey's ACK on this patch;
>
> Testing labeled NFS has always been a challenge for the somewhat
> limited resources available to the Smack project. I'll Ack the patch,
> but won't claim to have tested it.

Understood, thanks for the quick reply.

-- 
paul-moore.com

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH 1/1] selinux,smack: don't bypass permissions check in inode_setsecctx hook
  2024-08-28 21:05   ` Paul Moore
  2024-08-28 21:28     ` Casey Schaufler
@ 2024-08-28 23:19     ` Paul Moore
  2024-08-29 14:16       ` Chuck Lever III
  1 sibling, 1 reply; 12+ messages in thread
From: Paul Moore @ 2024-08-28 23:19 UTC (permalink / raw)
  To: Scott Mayhew
  Cc: stephen.smalley.work, casey, chuck.lever, marek.gresko, selinux,
	linux-security-module, linux-nfs

On Wed, Aug 28, 2024 at 5:05 PM Paul Moore <paul@paul-moore.com> wrote:
> On Wed, Aug 28, 2024 at 3:51 PM Scott Mayhew <smayhew@redhat.com> wrote:
> >
> > Marek Gresko reports that the root user on an NFS client is able to
> > change the security labels on files on an NFS filesystem that is
> > exported with root squashing enabled.
> >
> > The end of the kerneldoc comment for __vfs_setxattr_noperm() states:
> >
> >  *  This function requires the caller to lock the inode's i_mutex before it
> >  *  is executed. It also assumes that the caller will make the appropriate
> >  *  permission checks.
> >
> > nfsd_setattr() does do permissions checking via fh_verify() and
> > nfsd_permission(), but those don't do all the same permissions checks
> > that are done by security_inode_setxattr() and its related LSM hooks do.
> >
> > Since nfsd_setattr() is the only consumer of security_inode_setsecctx(),
> > simplest solution appears to be to replace the call to
> > __vfs_setxattr_noperm() with a call to __vfs_setxattr_locked().  This
> > fixes the above issue and has the added benefit of causing nfsd to
> > recall conflicting delegations on a file when a client tries to change
> > its security label.
> >
> > Reported-by: Marek Gresko <marek.gresko@protonmail.com>
> > Link: https://bugzilla.kernel.org/show_bug.cgi?id=218809
> > Signed-off-by: Scott Mayhew <smayhew@redhat.com>
> > ---
> >  security/selinux/hooks.c   | 4 ++--
> >  security/smack/smack_lsm.c | 4 ++--
> >  2 files changed, 4 insertions(+), 4 deletions(-)
>
> Thanks Scott, this looks good to me, but since it touches Smack too
> I'd also like to get Casey's ACK on this patch; if for some reason we
> don't hear from Casey after a bit I'll go ahead and merge it.
> Speaking of merging, since this touches both SELinux and Smack I'll
> likely pull this in via the LSM tree, with a marking for the stable
> kernels, if anyone has any objections to that please let me know.

Merged into lsm/stable-6.11 so we can get this into linux-next and the
automated SELinux testing, assuming all goes we'll I'll send this up
to Linus later this week.  Thanks all!

-- 
paul-moore.com

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH 1/1] selinux,smack: don't bypass permissions check in inode_setsecctx hook
  2024-08-28 19:51 ` [PATCH 1/1] " Scott Mayhew
                     ` (2 preceding siblings ...)
  2024-08-28 21:08   ` Chuck Lever
@ 2024-08-29 11:15   ` Jeff Layton
  2024-08-29 13:03     ` Stephen Smalley
  3 siblings, 1 reply; 12+ messages in thread
From: Jeff Layton @ 2024-08-29 11:15 UTC (permalink / raw)
  To: Scott Mayhew, paul, stephen.smalley.work, casey
  Cc: chuck.lever, marek.gresko, selinux, linux-security-module,
	linux-nfs

On Wed, 2024-08-28 at 15:51 -0400, Scott Mayhew wrote:
> Marek Gresko reports that the root user on an NFS client is able to
> change the security labels on files on an NFS filesystem that is
> exported with root squashing enabled.
> 
> The end of the kerneldoc comment for __vfs_setxattr_noperm() states:
> 
>  *  This function requires the caller to lock the inode's i_mutex before it
>  *  is executed. It also assumes that the caller will make the appropriate
>  *  permission checks.
> 
> nfsd_setattr() does do permissions checking via fh_verify() and
> nfsd_permission(), but those don't do all the same permissions checks
> that are done by security_inode_setxattr() and its related LSM hooks do.
> 
> Since nfsd_setattr() is the only consumer of security_inode_setsecctx(),
> simplest solution appears to be to replace the call to
> __vfs_setxattr_noperm() with a call to __vfs_setxattr_locked().  This
> fixes the above issue and has the added benefit of causing nfsd to
> recall conflicting delegations on a file when a client tries to change
> its security label.
> 
> Reported-by: Marek Gresko <marek.gresko@protonmail.com>
> Link: https://bugzilla.kernel.org/show_bug.cgi?id=218809
> Signed-off-by: Scott Mayhew <smayhew@redhat.com>
> ---
>  security/selinux/hooks.c   | 4 ++--
>  security/smack/smack_lsm.c | 4 ++--
>  2 files changed, 4 insertions(+), 4 deletions(-)
> 
> diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
> index bfa61e005aac..400eca4ad0fb 100644
> --- a/security/selinux/hooks.c
> +++ b/security/selinux/hooks.c
> @@ -6660,8 +6660,8 @@ static int selinux_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen
>   */
>  static int selinux_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
>  {
> -	return __vfs_setxattr_noperm(&nop_mnt_idmap, dentry, XATTR_NAME_SELINUX,
> -				     ctx, ctxlen, 0);
> +	return __vfs_setxattr_locked(&nop_mnt_idmap, dentry, XATTR_NAME_SELINUX,
> +				     ctx, ctxlen, 0, NULL);
>  }
>  
>  static int selinux_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
> diff --git a/security/smack/smack_lsm.c b/security/smack/smack_lsm.c
> index 4164699cd4f6..002a1b9ed83a 100644
> --- a/security/smack/smack_lsm.c
> +++ b/security/smack/smack_lsm.c
> @@ -4880,8 +4880,8 @@ static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
>  
>  static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
>  {
> -	return __vfs_setxattr_noperm(&nop_mnt_idmap, dentry, XATTR_NAME_SMACK,
> -				     ctx, ctxlen, 0);
> +	return __vfs_setxattr_locked(&nop_mnt_idmap, dentry, XATTR_NAME_SMACK,
> +				     ctx, ctxlen, 0, NULL);
>  }
>  
>  static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)

Reviewed-by: Jeff Layton <jlayton@kernel.org>

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH 1/1] selinux,smack: don't bypass permissions check in inode_setsecctx hook
  2024-08-29 11:15   ` Jeff Layton
@ 2024-08-29 13:03     ` Stephen Smalley
  0 siblings, 0 replies; 12+ messages in thread
From: Stephen Smalley @ 2024-08-29 13:03 UTC (permalink / raw)
  To: Jeff Layton
  Cc: Scott Mayhew, paul, casey, chuck.lever, marek.gresko, selinux,
	linux-security-module, linux-nfs

On Thu, Aug 29, 2024 at 7:15 AM Jeff Layton <jlayton@kernel.org> wrote:
>
> On Wed, 2024-08-28 at 15:51 -0400, Scott Mayhew wrote:
> > Marek Gresko reports that the root user on an NFS client is able to
> > change the security labels on files on an NFS filesystem that is
> > exported with root squashing enabled.
> >
> > The end of the kerneldoc comment for __vfs_setxattr_noperm() states:
> >
> >  *  This function requires the caller to lock the inode's i_mutex before it
> >  *  is executed. It also assumes that the caller will make the appropriate
> >  *  permission checks.
> >
> > nfsd_setattr() does do permissions checking via fh_verify() and
> > nfsd_permission(), but those don't do all the same permissions checks
> > that are done by security_inode_setxattr() and its related LSM hooks do.
> >
> > Since nfsd_setattr() is the only consumer of security_inode_setsecctx(),
> > simplest solution appears to be to replace the call to
> > __vfs_setxattr_noperm() with a call to __vfs_setxattr_locked().  This
> > fixes the above issue and has the added benefit of causing nfsd to
> > recall conflicting delegations on a file when a client tries to change
> > its security label.
> >
> > Reported-by: Marek Gresko <marek.gresko@protonmail.com>
> > Link: https://bugzilla.kernel.org/show_bug.cgi?id=218809
> > Signed-off-by: Scott Mayhew <smayhew@redhat.com>
> > ---
> >  security/selinux/hooks.c   | 4 ++--
> >  security/smack/smack_lsm.c | 4 ++--
> >  2 files changed, 4 insertions(+), 4 deletions(-)
> >
> > diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
> > index bfa61e005aac..400eca4ad0fb 100644
> > --- a/security/selinux/hooks.c
> > +++ b/security/selinux/hooks.c
> > @@ -6660,8 +6660,8 @@ static int selinux_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen
> >   */
> >  static int selinux_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
> >  {
> > -     return __vfs_setxattr_noperm(&nop_mnt_idmap, dentry, XATTR_NAME_SELINUX,
> > -                                  ctx, ctxlen, 0);
> > +     return __vfs_setxattr_locked(&nop_mnt_idmap, dentry, XATTR_NAME_SELINUX,
> > +                                  ctx, ctxlen, 0, NULL);
> >  }
> >
> >  static int selinux_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
> > diff --git a/security/smack/smack_lsm.c b/security/smack/smack_lsm.c
> > index 4164699cd4f6..002a1b9ed83a 100644
> > --- a/security/smack/smack_lsm.c
> > +++ b/security/smack/smack_lsm.c
> > @@ -4880,8 +4880,8 @@ static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
> >
> >  static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
> >  {
> > -     return __vfs_setxattr_noperm(&nop_mnt_idmap, dentry, XATTR_NAME_SMACK,
> > -                                  ctx, ctxlen, 0);
> > +     return __vfs_setxattr_locked(&nop_mnt_idmap, dentry, XATTR_NAME_SMACK,
> > +                                  ctx, ctxlen, 0, NULL);
> >  }
> >
> >  static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
>
> Reviewed-by: Jeff Layton <jlayton@kernel.org>

Reviewed-by: Stephen Smalley <stephen.smalley.work@gmail.com>
Tested-by: Stephen Smalley <stephen.smalley.work@gmail.com>

Passes all the NFS tests in the selinux-testsuite, and also correctly
denies root the ability to relabel a file on a root_squash mount but
allows a normal user to do so (as expected).

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH 1/1] selinux,smack: don't bypass permissions check in inode_setsecctx hook
  2024-08-28 23:19     ` Paul Moore
@ 2024-08-29 14:16       ` Chuck Lever III
  2024-08-29 14:51         ` Paul Moore
  0 siblings, 1 reply; 12+ messages in thread
From: Chuck Lever III @ 2024-08-29 14:16 UTC (permalink / raw)
  To: Paul Moore
  Cc: Scott Mayhew, Stephen Smalley, casey@schaufler-ca.com,
	marek.gresko@protonmail.com, selinux@vger.kernel.org,
	linux-security-module@vger.kernel.org, Linux NFS Mailing List



> On Aug 28, 2024, at 7:19 PM, Paul Moore <paul@paul-moore.com> wrote:
> 
> On Wed, Aug 28, 2024 at 5:05 PM Paul Moore <paul@paul-moore.com> wrote:
>> On Wed, Aug 28, 2024 at 3:51 PM Scott Mayhew <smayhew@redhat.com> wrote:
>>> 
>>> Marek Gresko reports that the root user on an NFS client is able to
>>> change the security labels on files on an NFS filesystem that is
>>> exported with root squashing enabled.
>>> 
>>> The end of the kerneldoc comment for __vfs_setxattr_noperm() states:
>>> 
>>> *  This function requires the caller to lock the inode's i_mutex before it
>>> *  is executed. It also assumes that the caller will make the appropriate
>>> *  permission checks.
>>> 
>>> nfsd_setattr() does do permissions checking via fh_verify() and
>>> nfsd_permission(), but those don't do all the same permissions checks
>>> that are done by security_inode_setxattr() and its related LSM hooks do.
>>> 
>>> Since nfsd_setattr() is the only consumer of security_inode_setsecctx(),
>>> simplest solution appears to be to replace the call to
>>> __vfs_setxattr_noperm() with a call to __vfs_setxattr_locked().  This
>>> fixes the above issue and has the added benefit of causing nfsd to
>>> recall conflicting delegations on a file when a client tries to change
>>> its security label.
>>> 
>>> Reported-by: Marek Gresko <marek.gresko@protonmail.com>
>>> Link: https://bugzilla.kernel.org/show_bug.cgi?id=218809
>>> Signed-off-by: Scott Mayhew <smayhew@redhat.com>
>>> ---
>>> security/selinux/hooks.c   | 4 ++--
>>> security/smack/smack_lsm.c | 4 ++--
>>> 2 files changed, 4 insertions(+), 4 deletions(-)
>> 
>> Thanks Scott, this looks good to me, but since it touches Smack too
>> I'd also like to get Casey's ACK on this patch; if for some reason we
>> don't hear from Casey after a bit I'll go ahead and merge it.
>> Speaking of merging, since this touches both SELinux and Smack I'll
>> likely pull this in via the LSM tree, with a marking for the stable
>> kernels, if anyone has any objections to that please let me know.
> 
> Merged into lsm/stable-6.11 so we can get this into linux-next and the
> automated SELinux testing, assuming all goes we'll I'll send this up
> to Linus later this week.  Thanks all!

Paul, may I recommend adding Cc: stable once your testing passes?


--
Chuck Lever



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH 1/1] selinux,smack: don't bypass permissions check in inode_setsecctx hook
  2024-08-29 14:16       ` Chuck Lever III
@ 2024-08-29 14:51         ` Paul Moore
  0 siblings, 0 replies; 12+ messages in thread
From: Paul Moore @ 2024-08-29 14:51 UTC (permalink / raw)
  To: Chuck Lever III
  Cc: Scott Mayhew, Stephen Smalley, casey@schaufler-ca.com,
	marek.gresko@protonmail.com, selinux@vger.kernel.org,
	linux-security-module@vger.kernel.org, Linux NFS Mailing List

On Thu, Aug 29, 2024 at 10:16 AM Chuck Lever III <chuck.lever@oracle.com> wrote:
> > On Aug 28, 2024, at 7:19 PM, Paul Moore <paul@paul-moore.com> wrote:
> >
> > Merged into lsm/stable-6.11 so we can get this into linux-next and the
> > automated SELinux testing, assuming all goes we'll I'll send this up
> > to Linus later this week.  Thanks all!
>
> Paul, may I recommend adding Cc: stable once your testing passes?

Yep, I did that yesterday before merging.  Typically if I merge
anything into one of the stable-X.Y branches it gets a stable too.
I'll update the commit in a few minutes with the tags from Jeff and
Stephen.

https://git.kernel.org/pub/scm/linux/kernel/git/pcmoore/lsm.git/log/?h=stable-6.11

-- 
paul-moore.com

^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2024-08-29 14:52 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-08-28 19:51 [PATCH 0/1] selinux,smack: don't bypass permissions check in inode_setsecctx hook Scott Mayhew
2024-08-28 19:51 ` [PATCH 1/1] " Scott Mayhew
2024-08-28 20:03   ` Jeff Layton
2024-08-28 21:05   ` Paul Moore
2024-08-28 21:28     ` Casey Schaufler
2024-08-28 23:08       ` Paul Moore
2024-08-28 23:19     ` Paul Moore
2024-08-29 14:16       ` Chuck Lever III
2024-08-29 14:51         ` Paul Moore
2024-08-28 21:08   ` Chuck Lever
2024-08-29 11:15   ` Jeff Layton
2024-08-29 13:03     ` Stephen Smalley

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox