All of lore.kernel.org
 help / color / mirror / Atom feed
* [RFC][PATCH] security: Make the selinux setxattr and removexattr hooks behave
@ 2017-09-28 22:34 Eric W. Biederman
  2017-09-29  1:16 ` Casey Schaufler
                   ` (2 more replies)
  0 siblings, 3 replies; 39+ messages in thread
From: Eric W. Biederman @ 2017-09-28 22:34 UTC (permalink / raw)
  To: linux-security-module


It looks like once upon a time a long time ago selinux copied code
from cap_inode_removexattr and cap_inode_setxattr into
selinux_inode_setotherxattr.  However the code has now diverged and
selinux is implementing a policy that is quite different than
cap_inode_setxattr and cap_inode_removexattr especially when it comes
to the security.capable xattr.

To keep things working and to make the comments in security/security.c
correct when the xattr is securit.capable, call cap_inode_setxattr
or cap_inode_removexattr as appropriate.

I suspect there is a larger conversation to be had here but this
is enough to keep selinux from implementing a non-sense hard coded
policy that breaks other parts of the kernel.

Signed-off-by: "Eric W. Biederman" <ebiederm@xmission.com>
---
 security/selinux/hooks.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
index f5d304736852..edf4bd292dc7 100644
--- a/security/selinux/hooks.c
+++ b/security/selinux/hooks.c
@@ -3167,6 +3167,9 @@ static int selinux_inode_setxattr(struct dentry *dentry, const char *name,
 	u32 newsid, sid = current_sid();
 	int rc = 0;
 
+	if (strcmp(name, XATTR_NAME_CAPS) == 0)
+		return cap_inode_setxattr(dentry, name, value, size, flags);
+
 	if (strcmp(name, XATTR_NAME_SELINUX))
 		return selinux_inode_setotherxattr(dentry, name);
 
@@ -3282,6 +3285,9 @@ static int selinux_inode_listxattr(struct dentry *dentry)
 
 static int selinux_inode_removexattr(struct dentry *dentry, const char *name)
 {
+	if (strcmp(name, XATTR_NAME_CAPS) == 0)
+		return cap_inode_removexattr(dentry, name);
+
 	if (strcmp(name, XATTR_NAME_SELINUX))
 		return selinux_inode_setotherxattr(dentry, name);
 
-- 
2.14.1

--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in
the body of a message to majordomo at vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

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

* Re: [RFC][PATCH] security: Make the selinux setxattr and removexattr hooks behave
       [not found] ` <87tvzmqwoi.fsf-aS9lmoZGLiVWk0Htik3J/w@public.gmane.org>
@ 2017-09-29  1:16   ` Casey Schaufler
  2017-09-29 12:36   ` Stephen Smalley
  1 sibling, 0 replies; 39+ messages in thread
From: Casey Schaufler @ 2017-09-29  1:16 UTC (permalink / raw)
  To: Eric W. Biederman, linux-security-module-u79uwXL29TY76Z2rM5mHXA
  Cc: Paul Moore, Linux Containers, Chris Wright, James Morris,
	Casey Schaufler, Eric Paris, Stephen Smalley

On 9/28/2017 3:34 PM, Eric W. Biederman wrote:
> It looks like once upon a time a long time ago selinux copied code
> from cap_inode_removexattr and cap_inode_setxattr into
> selinux_inode_setotherxattr.  However the code has now diverged and
> selinux is implementing a policy that is quite different than
> cap_inode_setxattr and cap_inode_removexattr especially when it comes
> to the security.capable xattr.

What leads you to believe that this isn't intentional?
It's most likely the case that this change occurred as
part of the first round module stacking change. What behavior
do you see that you're unhappy with?

>
> To keep things working

Which "things"? How are they not "working"?

>  and to make the comments in security/security.c
> correct when the xattr is securit.capable, call cap_inode_setxattr
> or cap_inode_removexattr as appropriate.
>
> I suspect there is a larger conversation to be had here but this
> is enough to keep selinux from implementing a non-sense hard coded
> policy that breaks other parts of the kernel.

Specifics, please. Since I can't guess what problem you've
encountered I can't tell if it's here, in the infrastructure,
or in your perception of what constitutes "broken".

>
> Signed-off-by: "Eric W. Biederman" <ebiederm-aS9lmoZGLiVWk0Htik3J/w@public.gmane.org>
> ---
>  security/selinux/hooks.c | 6 ++++++
>  1 file changed, 6 insertions(+)
>
> diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
> index f5d304736852..edf4bd292dc7 100644
> --- a/security/selinux/hooks.c
> +++ b/security/selinux/hooks.c
> @@ -3167,6 +3167,9 @@ static int selinux_inode_setxattr(struct dentry *dentry, const char *name,
>  	u32 newsid, sid = current_sid();
>  	int rc = 0;
>  
> +	if (strcmp(name, XATTR_NAME_CAPS) == 0)
> +		return cap_inode_setxattr(dentry, name, value, size, flags);
> +

No. Don't even think of contemplating considering embedding the cap
attribute check in the SELinux code. cap_inode_setxattr() is called in
the infrastructure. 

>  	if (strcmp(name, XATTR_NAME_SELINUX))
>  		return selinux_inode_setotherxattr(dentry, name);
>  
> @@ -3282,6 +3285,9 @@ static int selinux_inode_listxattr(struct dentry *dentry)
>  
>  static int selinux_inode_removexattr(struct dentry *dentry, const char *name)
>  {
> +	if (strcmp(name, XATTR_NAME_CAPS) == 0)
> +		return cap_inode_removexattr(dentry, name);
> +
>  	if (strcmp(name, XATTR_NAME_SELINUX))
>  		return selinux_inode_setotherxattr(dentry, name);
>  


.

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

* [RFC][PATCH] security: Make the selinux setxattr and removexattr hooks behave
  2017-09-28 22:34 [RFC][PATCH] security: Make the selinux setxattr and removexattr hooks behave Eric W. Biederman
@ 2017-09-29  1:16 ` Casey Schaufler
  2017-09-29 14:18   ` Stephen Smalley
       [not found]   ` <1913d5c4-64ef-36c1-e8ad-c779ff5c7995-iSGtlc1asvQWG2LlvL+J4A@public.gmane.org>
  2017-09-29 12:36 ` Stephen Smalley
       [not found] ` <87tvzmqwoi.fsf-aS9lmoZGLiVWk0Htik3J/w@public.gmane.org>
  2 siblings, 2 replies; 39+ messages in thread
From: Casey Schaufler @ 2017-09-29  1:16 UTC (permalink / raw)
  To: linux-security-module

On 9/28/2017 3:34 PM, Eric W. Biederman wrote:
> It looks like once upon a time a long time ago selinux copied code
> from cap_inode_removexattr and cap_inode_setxattr into
> selinux_inode_setotherxattr.  However the code has now diverged and
> selinux is implementing a policy that is quite different than
> cap_inode_setxattr and cap_inode_removexattr especially when it comes
> to the security.capable xattr.

What leads you to believe that this isn't intentional?
It's most likely the case that this change occurred as
part of the first round module stacking change. What behavior
do you see that you're unhappy with?

>
> To keep things working

Which "things"? How are they not "working"?

>  and to make the comments in security/security.c
> correct when the xattr is securit.capable, call cap_inode_setxattr
> or cap_inode_removexattr as appropriate.
>
> I suspect there is a larger conversation to be had here but this
> is enough to keep selinux from implementing a non-sense hard coded
> policy that breaks other parts of the kernel.

Specifics, please. Since I can't guess what problem you've
encountered I can't tell if it's here, in the infrastructure,
or in your perception of what constitutes "broken".

>
> Signed-off-by: "Eric W. Biederman" <ebiederm@xmission.com>
> ---
>  security/selinux/hooks.c | 6 ++++++
>  1 file changed, 6 insertions(+)
>
> diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
> index f5d304736852..edf4bd292dc7 100644
> --- a/security/selinux/hooks.c
> +++ b/security/selinux/hooks.c
> @@ -3167,6 +3167,9 @@ static int selinux_inode_setxattr(struct dentry *dentry, const char *name,
>  	u32 newsid, sid = current_sid();
>  	int rc = 0;
>  
> +	if (strcmp(name, XATTR_NAME_CAPS) == 0)
> +		return cap_inode_setxattr(dentry, name, value, size, flags);
> +

No. Don't even think of contemplating considering embedding the cap
attribute check in the SELinux code. cap_inode_setxattr() is called in
the infrastructure. 

>  	if (strcmp(name, XATTR_NAME_SELINUX))
>  		return selinux_inode_setotherxattr(dentry, name);
>  
> @@ -3282,6 +3285,9 @@ static int selinux_inode_listxattr(struct dentry *dentry)
>  
>  static int selinux_inode_removexattr(struct dentry *dentry, const char *name)
>  {
> +	if (strcmp(name, XATTR_NAME_CAPS) == 0)
> +		return cap_inode_removexattr(dentry, name);
> +
>  	if (strcmp(name, XATTR_NAME_SELINUX))
>  		return selinux_inode_setotherxattr(dentry, name);
>  


.
--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in
the body of a message to majordomo at vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

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

* Re: [RFC][PATCH] security: Make the selinux setxattr and removexattr hooks behave
       [not found] ` <87tvzmqwoi.fsf-aS9lmoZGLiVWk0Htik3J/w@public.gmane.org>
  2017-09-29  1:16   ` [RFC][PATCH] security: Make the selinux setxattr and removexattr hooks behave Casey Schaufler
@ 2017-09-29 12:36   ` Stephen Smalley
  1 sibling, 0 replies; 39+ messages in thread
From: Stephen Smalley @ 2017-09-29 12:36 UTC (permalink / raw)
  To: Eric W. Biederman, linux-security-module-u79uwXL29TY76Z2rM5mHXA
  Cc: Paul Moore, Linux Containers, Chris Wright, James Morris,
	Casey Schaufler, Eric Paris

On Thu, 2017-09-28 at 17:34 -0500, Eric W. Biederman wrote:
> It looks like once upon a time a long time ago selinux copied code
> from cap_inode_removexattr and cap_inode_setxattr into
> selinux_inode_setotherxattr.  However the code has now diverged and
> selinux is implementing a policy that is quite different than
> cap_inode_setxattr and cap_inode_removexattr especially when it comes
> to the security.capable xattr.
> 
> To keep things working and to make the comments in
> security/security.c
> correct when the xattr is securit.capable, call cap_inode_setxattr
> or cap_inode_removexattr as appropriate.
> 
> I suspect there is a larger conversation to be had here but this
> is enough to keep selinux from implementing a non-sense hard coded
> policy that breaks other parts of the kernel.

Originally SELinux called the cap functions directly since there was no
stacking support in the infrastructure and one had to manually stack a
secondary module internally.  inode_setxattr and inode_removexattr
however were special cases because the cap functions would check
CAP_SYS_ADMIN for any non-capability attributes in the security.*
namespace, and we don't want to impose that requirement on setting
security.selinux.  Thus, we inlined the capabilities logic into the
selinux hook functions and adapted it appropriately.  When the stacking
support was introduced, it had to also special case these hooks so that
only the primary module's hook is used for the same reason; otherwise,
the kernel would end up applying a CAP_SYS_ADMIN check on setting
security.selinux.  Your change below is almost but not quite right
since it only calls the cap functions when setting the capability
attribute; the residual problem is that it will then skip the SELinux
FILE__SETATTR (file setattr) permission check when setting those
attributes, which we want to retain.  So you need to only return early
if cap_inode_setxattr()/removexattr() return an error; otherwise, you
need to proceed to the SELinux check, and you can then delete the
duplicated logic from selinux_inode_setotherxattr().  At which point it
just becomes a call to dentry_has_perm() and you can just inline that
into selinux_inode_setxattr() and selinux_inode_removexattr().

> 
> Signed-off-by: "Eric W. Biederman" <ebiederm@xmission.com>
> ---
>  security/selinux/hooks.c | 6 ++++++
>  1 file changed, 6 insertions(+)
> 
> diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
> index f5d304736852..edf4bd292dc7 100644
> --- a/security/selinux/hooks.c
> +++ b/security/selinux/hooks.c
> @@ -3167,6 +3167,9 @@ static int selinux_inode_setxattr(struct dentry
> *dentry, const char *name,
>  	u32 newsid, sid = current_sid();
>  	int rc = 0;
>  
> +	if (strcmp(name, XATTR_NAME_CAPS) == 0)
> +		return cap_inode_setxattr(dentry, name, value, size,
> flags);
> +
>  	if (strcmp(name, XATTR_NAME_SELINUX))
>  		return selinux_inode_setotherxattr(dentry, name);
>  
> @@ -3282,6 +3285,9 @@ static int selinux_inode_listxattr(struct
> dentry *dentry)
>  
>  static int selinux_inode_removexattr(struct dentry *dentry, const
> char *name)
>  {
> +	if (strcmp(name, XATTR_NAME_CAPS) == 0)
> +		return cap_inode_removexattr(dentry, name);
> +
>  	if (strcmp(name, XATTR_NAME_SELINUX))
>  		return selinux_inode_setotherxattr(dentry, name);
>  
_______________________________________________
Containers mailing list
Containers@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/containers

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

* [RFC][PATCH] security: Make the selinux setxattr and removexattr hooks behave
  2017-09-28 22:34 [RFC][PATCH] security: Make the selinux setxattr and removexattr hooks behave Eric W. Biederman
  2017-09-29  1:16 ` Casey Schaufler
@ 2017-09-29 12:36 ` Stephen Smalley
  2017-10-02  3:26   ` Eric W. Biederman
                     ` (2 more replies)
       [not found] ` <87tvzmqwoi.fsf-aS9lmoZGLiVWk0Htik3J/w@public.gmane.org>
  2 siblings, 3 replies; 39+ messages in thread
From: Stephen Smalley @ 2017-09-29 12:36 UTC (permalink / raw)
  To: linux-security-module

On Thu, 2017-09-28 at 17:34 -0500, Eric W. Biederman wrote:
> It looks like once upon a time a long time ago selinux copied code
> from cap_inode_removexattr and cap_inode_setxattr into
> selinux_inode_setotherxattr.??However the code has now diverged and
> selinux is implementing a policy that is quite different than
> cap_inode_setxattr and cap_inode_removexattr especially when it comes
> to the security.capable xattr.
> 
> To keep things working and to make the comments in
> security/security.c
> correct when the xattr is securit.capable, call cap_inode_setxattr
> or cap_inode_removexattr as appropriate.
> 
> I suspect there is a larger conversation to be had here but this
> is enough to keep selinux from implementing a non-sense hard coded
> policy that breaks other parts of the kernel.

Originally SELinux called the cap functions directly since there was no
stacking support in the infrastructure and one had to manually stack a
secondary module internally.  inode_setxattr and inode_removexattr
however were special cases because the cap functions would check
CAP_SYS_ADMIN for any non-capability attributes in the security.*
namespace, and we don't want to impose that requirement on setting
security.selinux.  Thus, we inlined the capabilities logic into the
selinux hook functions and adapted it appropriately.  When the stacking
support was introduced, it had to also special case these hooks so that
only the primary module's hook is used for the same reason; otherwise,
the kernel would end up applying a CAP_SYS_ADMIN check on setting
security.selinux.  Your change below is almost but not quite right
since it only calls the cap functions when setting the capability
attribute; the residual problem is that it will then skip the SELinux
FILE__SETATTR (file setattr) permission check when setting those
attributes, which we want to retain.  So you need to only return early
if cap_inode_setxattr()/removexattr() return an error; otherwise, you
need to proceed to the SELinux check, and you can then delete the
duplicated logic from selinux_inode_setotherxattr().  At which point it
just becomes a call to dentry_has_perm() and you can just inline that
into selinux_inode_setxattr() and selinux_inode_removexattr().

> 
> Signed-off-by: "Eric W. Biederman" <ebiederm@xmission.com>
> ---
> ?security/selinux/hooks.c | 6 ++++++
> ?1 file changed, 6 insertions(+)
> 
> diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
> index f5d304736852..edf4bd292dc7 100644
> --- a/security/selinux/hooks.c
> +++ b/security/selinux/hooks.c
> @@ -3167,6 +3167,9 @@ static int selinux_inode_setxattr(struct dentry
> *dentry, const char *name,
> ?	u32 newsid, sid = current_sid();
> ?	int rc = 0;
> ?
> +	if (strcmp(name, XATTR_NAME_CAPS) == 0)
> +		return cap_inode_setxattr(dentry, name, value, size,
> flags);
> +
> ?	if (strcmp(name, XATTR_NAME_SELINUX))
> ?		return selinux_inode_setotherxattr(dentry, name);
> ?
> @@ -3282,6 +3285,9 @@ static int selinux_inode_listxattr(struct
> dentry *dentry)
> ?
> ?static int selinux_inode_removexattr(struct dentry *dentry, const
> char *name)
> ?{
> +	if (strcmp(name, XATTR_NAME_CAPS) == 0)
> +		return cap_inode_removexattr(dentry, name);
> +
> ?	if (strcmp(name, XATTR_NAME_SELINUX))
> ?		return selinux_inode_setotherxattr(dentry, name);
> ?
--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in
the body of a message to majordomo at vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

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

* Re: [RFC][PATCH] security: Make the selinux setxattr and removexattr hooks behave
       [not found]   ` <1913d5c4-64ef-36c1-e8ad-c779ff5c7995-iSGtlc1asvQWG2LlvL+J4A@public.gmane.org>
@ 2017-09-29 14:18     ` Stephen Smalley
  0 siblings, 0 replies; 39+ messages in thread
From: Stephen Smalley @ 2017-09-29 14:18 UTC (permalink / raw)
  To: Casey Schaufler, Eric W. Biederman,
	linux-security-module-u79uwXL29TY76Z2rM5mHXA
  Cc: Paul Moore, Linux Containers, Chris Wright, James Morris, Eric Paris

On Thu, 2017-09-28 at 18:16 -0700, Casey Schaufler wrote:
> On 9/28/2017 3:34 PM, Eric W. Biederman wrote:
> > It looks like once upon a time a long time ago selinux copied code
> > from cap_inode_removexattr and cap_inode_setxattr into
> > selinux_inode_setotherxattr.  However the code has now diverged and
> > selinux is implementing a policy that is quite different than
> > cap_inode_setxattr and cap_inode_removexattr especially when it
> > comes
> > to the security.capable xattr.
> 
> What leads you to believe that this isn't intentional?
> It's most likely the case that this change occurred as
> part of the first round module stacking change. What behavior
> do you see that you're unhappy with?
> 
> > 
> > To keep things working
> 
> Which "things"? How are they not "working"?
> 
> >  and to make the comments in security/security.c
> > correct when the xattr is securit.capable, call cap_inode_setxattr
> > or cap_inode_removexattr as appropriate.
> > 
> > I suspect there is a larger conversation to be had here but this
> > is enough to keep selinux from implementing a non-sense hard coded
> > policy that breaks other parts of the kernel.
> 
> Specifics, please. Since I can't guess what problem you've
> encountered I can't tell if it's here, in the infrastructure,
> or in your perception of what constitutes "broken".
> 
> > 
> > Signed-off-by: "Eric W. Biederman" <ebiederm@xmission.com>
> > ---
> >  security/selinux/hooks.c | 6 ++++++
> >  1 file changed, 6 insertions(+)
> > 
> > diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
> > index f5d304736852..edf4bd292dc7 100644
> > --- a/security/selinux/hooks.c
> > +++ b/security/selinux/hooks.c
> > @@ -3167,6 +3167,9 @@ static int selinux_inode_setxattr(struct
> > dentry *dentry, const char *name,
> >  	u32 newsid, sid = current_sid();
> >  	int rc = 0;
> >  
> > +	if (strcmp(name, XATTR_NAME_CAPS) == 0)
> > +		return cap_inode_setxattr(dentry, name, value,
> > size, flags);
> > +
> 
> No. Don't even think of contemplating considering embedding the cap
> attribute check in the SELinux code. cap_inode_setxattr() is called
> in
> the infrastructure.

Except that it isn't, not if any other security module is enabled and
implements those hooks, to prevent imposing CAP_SYS_ADMIN checks when
setting security.selinux or security.SMACK*.

An alternative approach to fixing this would be to change the cap
functions to only apply their checks if setting the capability
attribute and defer any checks on other security.* attributes to either
the security framework or the other security modules.  Then the
framework could always call all the modules on the inode_setxattr and
inode_removexattr hooks as with other hooks.  The security framework
would then need to ensure that a check is still applied when setting
security.* attributes if it isn't already handled by one of the enabled
security modules, as you don't want unprivileged userspace to be able
to set arbitrary security.foo attributes or to set up security.selinux
or security.SMACK* attributes if those modules happen to be disabled.

>  
> 
> >  	if (strcmp(name, XATTR_NAME_SELINUX))
> >  		return selinux_inode_setotherxattr(dentry, name);
> >  
> > @@ -3282,6 +3285,9 @@ static int selinux_inode_listxattr(struct
> > dentry *dentry)
> >  
> >  static int selinux_inode_removexattr(struct dentry *dentry, const
> > char *name)
> >  {
> > +	if (strcmp(name, XATTR_NAME_CAPS) == 0)
> > +		return cap_inode_removexattr(dentry, name);
> > +
> >  	if (strcmp(name, XATTR_NAME_SELINUX))
> >  		return selinux_inode_setotherxattr(dentry, name);
> >  
> 
> 
> .
_______________________________________________
Containers mailing list
Containers@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/containers

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

* [RFC][PATCH] security: Make the selinux setxattr and removexattr hooks behave
  2017-09-29  1:16 ` Casey Schaufler
@ 2017-09-29 14:18   ` Stephen Smalley
  2017-09-29 15:46     ` Casey Schaufler
       [not found]     ` <1506694737.5571.9.camel-+05T5uksL2qpZYMLLGbcSA@public.gmane.org>
       [not found]   ` <1913d5c4-64ef-36c1-e8ad-c779ff5c7995-iSGtlc1asvQWG2LlvL+J4A@public.gmane.org>
  1 sibling, 2 replies; 39+ messages in thread
From: Stephen Smalley @ 2017-09-29 14:18 UTC (permalink / raw)
  To: linux-security-module

On Thu, 2017-09-28 at 18:16 -0700, Casey Schaufler wrote:
> On 9/28/2017 3:34 PM, Eric W. Biederman wrote:
> > It looks like once upon a time a long time ago selinux copied code
> > from cap_inode_removexattr and cap_inode_setxattr into
> > selinux_inode_setotherxattr.??However the code has now diverged and
> > selinux is implementing a policy that is quite different than
> > cap_inode_setxattr and cap_inode_removexattr especially when it
> > comes
> > to the security.capable xattr.
> 
> What leads you to believe that this isn't intentional?
> It's most likely the case that this change occurred as
> part of the first round module stacking change. What behavior
> do you see that you're unhappy with?
> 
> > 
> > To keep things working
> 
> Which "things"? How are they not "working"?
> 
> > ?and to make the comments in security/security.c
> > correct when the xattr is securit.capable, call cap_inode_setxattr
> > or cap_inode_removexattr as appropriate.
> > 
> > I suspect there is a larger conversation to be had here but this
> > is enough to keep selinux from implementing a non-sense hard coded
> > policy that breaks other parts of the kernel.
> 
> Specifics, please. Since I can't guess what problem you've
> encountered I can't tell if it's here, in the infrastructure,
> or in your perception of what constitutes "broken".
> 
> > 
> > Signed-off-by: "Eric W. Biederman" <ebiederm@xmission.com>
> > ---
> > ?security/selinux/hooks.c | 6 ++++++
> > ?1 file changed, 6 insertions(+)
> > 
> > diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
> > index f5d304736852..edf4bd292dc7 100644
> > --- a/security/selinux/hooks.c
> > +++ b/security/selinux/hooks.c
> > @@ -3167,6 +3167,9 @@ static int selinux_inode_setxattr(struct
> > dentry *dentry, const char *name,
> > ?	u32 newsid, sid = current_sid();
> > ?	int rc = 0;
> > ?
> > +	if (strcmp(name, XATTR_NAME_CAPS) == 0)
> > +		return cap_inode_setxattr(dentry, name, value,
> > size, flags);
> > +
> 
> No. Don't even think of contemplating considering embedding the cap
> attribute check in the SELinux code. cap_inode_setxattr() is called
> in
> the infrastructure.

Except that it isn't, not if any other security module is enabled and
implements those hooks, to prevent imposing CAP_SYS_ADMIN checks when
setting security.selinux or security.SMACK*.

An alternative approach to fixing this would be to change the cap
functions to only apply their checks if setting the capability
attribute and defer any checks on other security.* attributes to either
the security framework or the other security modules.  Then the
framework could always call all the modules on the inode_setxattr and
inode_removexattr hooks as with other hooks.  The security framework
would then need to ensure that a check is still applied when setting
security.* attributes if it isn't already handled by one of the enabled
security modules, as you don't want unprivileged userspace to be able
to set arbitrary security.foo attributes or to set up security.selinux
or security.SMACK* attributes if those modules happen to be disabled.

> ?
> 
> > ?	if (strcmp(name, XATTR_NAME_SELINUX))
> > ?		return selinux_inode_setotherxattr(dentry, name);
> > ?
> > @@ -3282,6 +3285,9 @@ static int selinux_inode_listxattr(struct
> > dentry *dentry)
> > ?
> > ?static int selinux_inode_removexattr(struct dentry *dentry, const
> > char *name)
> > ?{
> > +	if (strcmp(name, XATTR_NAME_CAPS) == 0)
> > +		return cap_inode_removexattr(dentry, name);
> > +
> > ?	if (strcmp(name, XATTR_NAME_SELINUX))
> > ?		return selinux_inode_setotherxattr(dentry, name);
> > ?
> 
> 
> .
--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in
the body of a message to majordomo at vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

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

* Re: [RFC][PATCH] security: Make the selinux setxattr and removexattr hooks behave
       [not found]     ` <1506694737.5571.9.camel-+05T5uksL2qpZYMLLGbcSA@public.gmane.org>
@ 2017-09-29 15:46       ` Casey Schaufler
  0 siblings, 0 replies; 39+ messages in thread
From: Casey Schaufler @ 2017-09-29 15:46 UTC (permalink / raw)
  To: Stephen Smalley, Eric W. Biederman,
	linux-security-module-u79uwXL29TY76Z2rM5mHXA
  Cc: Paul Moore, Linux Containers, Chris Wright, James Morris,
	Casey Schaufler, Eric Paris

On 9/29/2017 7:18 AM, Stephen Smalley wrote:
> On Thu, 2017-09-28 at 18:16 -0700, Casey Schaufler wrote:
>> On 9/28/2017 3:34 PM, Eric W. Biederman wrote:
>>> It looks like once upon a time a long time ago selinux copied code
>>> from cap_inode_removexattr and cap_inode_setxattr into
>>> selinux_inode_setotherxattr.  However the code has now diverged and
>>> selinux is implementing a policy that is quite different than
>>> cap_inode_setxattr and cap_inode_removexattr especially when it
>>> comes
>>> to the security.capable xattr.
>> What leads you to believe that this isn't intentional?
>> It's most likely the case that this change occurred as
>> part of the first round module stacking change. What behavior
>> do you see that you're unhappy with?
>>
>>> To keep things working
>> Which "things"? How are they not "working"?
>>
>>>  and to make the comments in security/security.c
>>> correct when the xattr is securit.capable, call cap_inode_setxattr
>>> or cap_inode_removexattr as appropriate.
>>>
>>> I suspect there is a larger conversation to be had here but this
>>> is enough to keep selinux from implementing a non-sense hard coded
>>> policy that breaks other parts of the kernel.
>> Specifics, please. Since I can't guess what problem you've
>> encountered I can't tell if it's here, in the infrastructure,
>> or in your perception of what constitutes "broken".
>>
>>> Signed-off-by: "Eric W. Biederman" <ebiederm@xmission.com>
>>> ---
>>>  security/selinux/hooks.c | 6 ++++++
>>>  1 file changed, 6 insertions(+)
>>>
>>> diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
>>> index f5d304736852..edf4bd292dc7 100644
>>> --- a/security/selinux/hooks.c
>>> +++ b/security/selinux/hooks.c
>>> @@ -3167,6 +3167,9 @@ static int selinux_inode_setxattr(struct
>>> dentry *dentry, const char *name,
>>>  	u32 newsid, sid = current_sid();
>>>  	int rc = 0;
>>>  
>>> +	if (strcmp(name, XATTR_NAME_CAPS) == 0)
>>> +		return cap_inode_setxattr(dentry, name, value,
>>> size, flags);
>>> +
>> No. Don't even think of contemplating considering embedding the cap
>> attribute check in the SELinux code. cap_inode_setxattr() is called
>> in
>> the infrastructure.
> Except that it isn't, not if any other security module is enabled and
> implements those hooks, to prevent imposing CAP_SYS_ADMIN checks when
> setting security.selinux or security.SMACK*.

OK. Yes, this bit of the infrastructure is some of the
worst I've done in a long time. This is a case where we
already need special case stacking infrastructure. It looks
like we'll have to separate setting the cap attribute from
checking the cap state in order to make this work. In any
case, the security_inode_setxattr() code is where the change
belongs. There will likely be fallout changes in the modules,
including the cap module.
 

> An alternative approach to fixing this would be to change the cap
> functions to only apply their checks if setting the capability
> attribute and defer any checks on other security.* attributes to either
> the security framework or the other security modules.  Then the
> framework could always call all the modules on the inode_setxattr and
> inode_removexattr hooks as with other hooks.  The security framework
> would then need to ensure that a check is still applied when setting
> security.* attributes if it isn't already handled by one of the enabled
> security modules, as you don't want unprivileged userspace to be able
> to set arbitrary security.foo attributes or to set up security.selinux
> or security.SMACK* attributes if those modules happen to be disabled.

Agreed. This isn't a two line change. Grumble.

I can guess at what the problem might be, but I hate making
assumptions when I go to fix a problem. I will start looking
at a patch, but it would really help if I could say for sure
what I'm out to accomplish. It may be obvious to the casual
observer, but that description has not been applied to me very
often.

>
>>  
>>
>>>  	if (strcmp(name, XATTR_NAME_SELINUX))
>>>  		return selinux_inode_setotherxattr(dentry, name);
>>>  
>>> @@ -3282,6 +3285,9 @@ static int selinux_inode_listxattr(struct
>>> dentry *dentry)
>>>  
>>>  static int selinux_inode_removexattr(struct dentry *dentry, const
>>> char *name)
>>>  {
>>> +	if (strcmp(name, XATTR_NAME_CAPS) == 0)
>>> +		return cap_inode_removexattr(dentry, name);
>>> +
>>>  	if (strcmp(name, XATTR_NAME_SELINUX))
>>>  		return selinux_inode_setotherxattr(dentry, name);
>>>  
>>
>> .
> --
> To unsubscribe from this list: send the line "unsubscribe linux-security-module" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
>


.
_______________________________________________
Containers mailing list
Containers@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/containers

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

* [RFC][PATCH] security: Make the selinux setxattr and removexattr hooks behave
  2017-09-29 14:18   ` Stephen Smalley
@ 2017-09-29 15:46     ` Casey Schaufler
       [not found]       ` <6f293107-6ff9-c4c7-f682-207a546c5061-iSGtlc1asvQWG2LlvL+J4A@public.gmane.org>
  2017-09-30 16:22       ` Eric W. Biederman
       [not found]     ` <1506694737.5571.9.camel-+05T5uksL2qpZYMLLGbcSA@public.gmane.org>
  1 sibling, 2 replies; 39+ messages in thread
From: Casey Schaufler @ 2017-09-29 15:46 UTC (permalink / raw)
  To: linux-security-module

On 9/29/2017 7:18 AM, Stephen Smalley wrote:
> On Thu, 2017-09-28 at 18:16 -0700, Casey Schaufler wrote:
>> On 9/28/2017 3:34 PM, Eric W. Biederman wrote:
>>> It looks like once upon a time a long time ago selinux copied code
>>> from cap_inode_removexattr and cap_inode_setxattr into
>>> selinux_inode_setotherxattr.??However the code has now diverged and
>>> selinux is implementing a policy that is quite different than
>>> cap_inode_setxattr and cap_inode_removexattr especially when it
>>> comes
>>> to the security.capable xattr.
>> What leads you to believe that this isn't intentional?
>> It's most likely the case that this change occurred as
>> part of the first round module stacking change. What behavior
>> do you see that you're unhappy with?
>>
>>> To keep things working
>> Which "things"? How are they not "working"?
>>
>>> ?and to make the comments in security/security.c
>>> correct when the xattr is securit.capable, call cap_inode_setxattr
>>> or cap_inode_removexattr as appropriate.
>>>
>>> I suspect there is a larger conversation to be had here but this
>>> is enough to keep selinux from implementing a non-sense hard coded
>>> policy that breaks other parts of the kernel.
>> Specifics, please. Since I can't guess what problem you've
>> encountered I can't tell if it's here, in the infrastructure,
>> or in your perception of what constitutes "broken".
>>
>>> Signed-off-by: "Eric W. Biederman" <ebiederm@xmission.com>
>>> ---
>>> ?security/selinux/hooks.c | 6 ++++++
>>> ?1 file changed, 6 insertions(+)
>>>
>>> diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
>>> index f5d304736852..edf4bd292dc7 100644
>>> --- a/security/selinux/hooks.c
>>> +++ b/security/selinux/hooks.c
>>> @@ -3167,6 +3167,9 @@ static int selinux_inode_setxattr(struct
>>> dentry *dentry, const char *name,
>>> ?	u32 newsid, sid = current_sid();
>>> ?	int rc = 0;
>>> ?
>>> +	if (strcmp(name, XATTR_NAME_CAPS) == 0)
>>> +		return cap_inode_setxattr(dentry, name, value,
>>> size, flags);
>>> +
>> No. Don't even think of contemplating considering embedding the cap
>> attribute check in the SELinux code. cap_inode_setxattr() is called
>> in
>> the infrastructure.
> Except that it isn't, not if any other security module is enabled and
> implements those hooks, to prevent imposing CAP_SYS_ADMIN checks when
> setting security.selinux or security.SMACK*.

OK. Yes, this bit of the infrastructure is some of the
worst I've done in a long time. This is a case where we
already need special case stacking infrastructure. It looks
like we'll have to separate setting the cap attribute from
checking the cap state in order to make this work. In any
case, the security_inode_setxattr() code is where the change
belongs. There will likely be fallout changes in the modules,
including the cap module.
?

> An alternative approach to fixing this would be to change the cap
> functions to only apply their checks if setting the capability
> attribute and defer any checks on other security.* attributes to either
> the security framework or the other security modules.  Then the
> framework could always call all the modules on the inode_setxattr and
> inode_removexattr hooks as with other hooks.  The security framework
> would then need to ensure that a check is still applied when setting
> security.* attributes if it isn't already handled by one of the enabled
> security modules, as you don't want unprivileged userspace to be able
> to set arbitrary security.foo attributes or to set up security.selinux
> or security.SMACK* attributes if those modules happen to be disabled.

Agreed. This isn't a two line change. Grumble.

I can guess at what the problem might be, but I hate making
assumptions when I go to fix a problem. I will start looking
at a patch, but it would really help if I could say for sure
what I'm out to accomplish. It may be obvious to the casual
observer, but that description has not been applied to me very
often.

>
>> ?
>>
>>> ?	if (strcmp(name, XATTR_NAME_SELINUX))
>>> ?		return selinux_inode_setotherxattr(dentry, name);
>>> ?
>>> @@ -3282,6 +3285,9 @@ static int selinux_inode_listxattr(struct
>>> dentry *dentry)
>>> ?
>>> ?static int selinux_inode_removexattr(struct dentry *dentry, const
>>> char *name)
>>> ?{
>>> +	if (strcmp(name, XATTR_NAME_CAPS) == 0)
>>> +		return cap_inode_removexattr(dentry, name);
>>> +
>>> ?	if (strcmp(name, XATTR_NAME_SELINUX))
>>> ?		return selinux_inode_setotherxattr(dentry, name);
>>> ?
>>
>> .
> --
> To unsubscribe from this list: send the line "unsubscribe linux-security-module" in
> the body of a message to majordomo at vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
>


.
--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in
the body of a message to majordomo at vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

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

* Re: [RFC][PATCH] security: Make the selinux setxattr and removexattr hooks behave
       [not found]       ` <6f293107-6ff9-c4c7-f682-207a546c5061-iSGtlc1asvQWG2LlvL+J4A@public.gmane.org>
@ 2017-09-30 16:22         ` Eric W. Biederman
  0 siblings, 0 replies; 39+ messages in thread
From: Eric W. Biederman @ 2017-09-30 16:22 UTC (permalink / raw)
  To: Casey Schaufler
  Cc: Paul Moore, Linux Containers, Chris Wright,
	linux-security-module-u79uwXL29TY76Z2rM5mHXA, James Morris,
	Eric Paris, Stephen Smalley

Casey Schaufler <casey@schaufler-ca.com> writes:

> On 9/29/2017 7:18 AM, Stephen Smalley wrote:
>> On Thu, 2017-09-28 at 18:16 -0700, Casey Schaufler wrote:
>>> On 9/28/2017 3:34 PM, Eric W. Biederman wrote:
>>>> It looks like once upon a time a long time ago selinux copied code
>>>> from cap_inode_removexattr and cap_inode_setxattr into
>>>> selinux_inode_setotherxattr.  However the code has now diverged and
>>>> selinux is implementing a policy that is quite different than
>>>> cap_inode_setxattr and cap_inode_removexattr especially when it
>>>> comes
>>>> to the security.capable xattr.
>>> What leads you to believe that this isn't intentional?
>>> It's most likely the case that this change occurred as
>>> part of the first round module stacking change. What behavior
>>> do you see that you're unhappy with?
>>>
>>>> To keep things working
>>> Which "things"? How are they not "working"?
>>>
>>>>  and to make the comments in security/security.c
>>>> correct when the xattr is securit.capable, call cap_inode_setxattr
>>>> or cap_inode_removexattr as appropriate.
>>>>
>>>> I suspect there is a larger conversation to be had here but this
>>>> is enough to keep selinux from implementing a non-sense hard coded
>>>> policy that breaks other parts of the kernel.
>>> Specifics, please. Since I can't guess what problem you've
>>> encountered I can't tell if it's here, in the infrastructure,
>>> or in your perception of what constitutes "broken".
>>>
>>>> Signed-off-by: "Eric W. Biederman" <ebiederm@xmission.com>
>>>> ---
>>>>  security/selinux/hooks.c | 6 ++++++
>>>>  1 file changed, 6 insertions(+)
>>>>
>>>> diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
>>>> index f5d304736852..edf4bd292dc7 100644
>>>> --- a/security/selinux/hooks.c
>>>> +++ b/security/selinux/hooks.c
>>>> @@ -3167,6 +3167,9 @@ static int selinux_inode_setxattr(struct
>>>> dentry *dentry, const char *name,
>>>>  	u32 newsid, sid = current_sid();
>>>>  	int rc = 0;
>>>>  
>>>> +	if (strcmp(name, XATTR_NAME_CAPS) == 0)
>>>> +		return cap_inode_setxattr(dentry, name, value,
>>>> size, flags);
>>>> +
>>> No. Don't even think of contemplating considering embedding the cap
>>> attribute check in the SELinux code. cap_inode_setxattr() is called
>>> in
>>> the infrastructure.
>> Except that it isn't, not if any other security module is enabled and
>> implements those hooks, to prevent imposing CAP_SYS_ADMIN checks when
>> setting security.selinux or security.SMACK*.
>
> OK. Yes, this bit of the infrastructure is some of the
> worst I've done in a long time. This is a case where we
> already need special case stacking infrastructure. It looks
> like we'll have to separate setting the cap attribute from
> checking the cap state in order to make this work. In any
> case, the security_inode_setxattr() code is where the change
> belongs. There will likely be fallout changes in the modules,
> including the cap module.
>  
>
>> An alternative approach to fixing this would be to change the cap
>> functions to only apply their checks if setting the capability
>> attribute and defer any checks on other security.* attributes to either
>> the security framework or the other security modules.  Then the
>> framework could always call all the modules on the inode_setxattr and
>> inode_removexattr hooks as with other hooks.  The security framework
>> would then need to ensure that a check is still applied when setting
>> security.* attributes if it isn't already handled by one of the enabled
>> security modules, as you don't want unprivileged userspace to be able
>> to set arbitrary security.foo attributes or to set up security.selinux
>> or security.SMACK* attributes if those modules happen to be disabled.
>
> Agreed. This isn't a two line change. Grumble.
>
> I can guess at what the problem might be, but I hate making
> assumptions when I go to fix a problem. I will start looking
> at a patch, but it would really help if I could say for sure
> what I'm out to accomplish. It may be obvious to the casual
> observer, but that description has not been applied to me very
> often.

Apologies for the delayed reply.

I am looking at security_inode_setxattr.

For setting attributes in the security.* the generic code in fs/xattr.c
applies no permission checks.

Each security module that implements an xattr in security.* then imposes
it's own policy on it's own attribute.

For smack the basic rule is smack_privileged(CAP_MAC_ADMIN).
For selinux the basic rule is inode_or_owner_capable(inode).
For commoncap the basic rule is capable_wrt_inode_uidgid(inode, CAP_SETFCAP).

commoncap also applies a default policity to setting security.* xattrs.
ns_capable(dentry->d_sb->s_userns, CAP_SYS_ADMIN).

smack reuses that default policy by calling cap_inode_setxattr if it
isn't a smack security.* xattr.

selinux has what looks like an old copy of the commoncap checks for
the security.* in selinux_inode_setotherxattr.  Testing for
capable(CAP_SETFCAP) for security.capable and capable(CAP_SYS_ADMIN)
for the others.

With the added complication that selinux calls
selinux_inode_setotherxattr also for the remove_xattr case.  So fixing
this in selinux_inode_setotherxattr is not appropriate.

I believe selinux also has general policy hooks it applies to all
invocations of setxattr.

So I think to really fix this we need to separate the cases of is this
your security modules attribute from general policy checks added by the
security modules.  Perhaps something like this for
security_inode_setxattr:

Hmm.  Looking at least ima also has the distinction between protecting
it's own xattr writes and running generaly security module policy on
xattr writes.

int security_inode_setxattr(struct dentry *dentry, const char *name,
			    const void *value, size_t size, int flags)
{
	int ret = 0;

	if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
		return 0;

	if (strncmp(name, XATTR_SECURITY_PREFIX,
			sizeof(XATTR_SECURITY_PREFIX) - 1) == 0) {
		/* Call the security modules and see if they all return
                 * -EOPNOTSUPP if so apply the default permission
                 * check of ns_capable(dentry->d_sb->s_user_ns, CAP_SYS_ADMIN)
                 * otherwise if one of the security modules supports
		 * this attribute (signaled by returning something other
		 * -EOPNOTSUPP) then set ret to that result.
                 *
                 * The security modules include at least smack, selinux,
		 * commoncap, ima, and evm.
                 */
                 ret = magic_inode_protect_setxattr(dentry, name, value, size);
        }
	if (ret)
		return ret;

        /* Run all of the security module policy against this setxattr call */
        return magic_inode_policy_setxattr(dentry, name, value, size);
}

Eric
_______________________________________________
Containers mailing list
Containers@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/containers

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

* [RFC][PATCH] security: Make the selinux setxattr and removexattr hooks behave
  2017-09-29 15:46     ` Casey Schaufler
       [not found]       ` <6f293107-6ff9-c4c7-f682-207a546c5061-iSGtlc1asvQWG2LlvL+J4A@public.gmane.org>
@ 2017-09-30 16:22       ` Eric W. Biederman
  2017-09-30 17:01         ` Casey Schaufler
  1 sibling, 1 reply; 39+ messages in thread
From: Eric W. Biederman @ 2017-09-30 16:22 UTC (permalink / raw)
  To: linux-security-module

Casey Schaufler <casey@schaufler-ca.com> writes:

> On 9/29/2017 7:18 AM, Stephen Smalley wrote:
>> On Thu, 2017-09-28 at 18:16 -0700, Casey Schaufler wrote:
>>> On 9/28/2017 3:34 PM, Eric W. Biederman wrote:
>>>> It looks like once upon a time a long time ago selinux copied code
>>>> from cap_inode_removexattr and cap_inode_setxattr into
>>>> selinux_inode_setotherxattr.??However the code has now diverged and
>>>> selinux is implementing a policy that is quite different than
>>>> cap_inode_setxattr and cap_inode_removexattr especially when it
>>>> comes
>>>> to the security.capable xattr.
>>> What leads you to believe that this isn't intentional?
>>> It's most likely the case that this change occurred as
>>> part of the first round module stacking change. What behavior
>>> do you see that you're unhappy with?
>>>
>>>> To keep things working
>>> Which "things"? How are they not "working"?
>>>
>>>> ?and to make the comments in security/security.c
>>>> correct when the xattr is securit.capable, call cap_inode_setxattr
>>>> or cap_inode_removexattr as appropriate.
>>>>
>>>> I suspect there is a larger conversation to be had here but this
>>>> is enough to keep selinux from implementing a non-sense hard coded
>>>> policy that breaks other parts of the kernel.
>>> Specifics, please. Since I can't guess what problem you've
>>> encountered I can't tell if it's here, in the infrastructure,
>>> or in your perception of what constitutes "broken".
>>>
>>>> Signed-off-by: "Eric W. Biederman" <ebiederm@xmission.com>
>>>> ---
>>>> ?security/selinux/hooks.c | 6 ++++++
>>>> ?1 file changed, 6 insertions(+)
>>>>
>>>> diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
>>>> index f5d304736852..edf4bd292dc7 100644
>>>> --- a/security/selinux/hooks.c
>>>> +++ b/security/selinux/hooks.c
>>>> @@ -3167,6 +3167,9 @@ static int selinux_inode_setxattr(struct
>>>> dentry *dentry, const char *name,
>>>> ?	u32 newsid, sid = current_sid();
>>>> ?	int rc = 0;
>>>> ?
>>>> +	if (strcmp(name, XATTR_NAME_CAPS) == 0)
>>>> +		return cap_inode_setxattr(dentry, name, value,
>>>> size, flags);
>>>> +
>>> No. Don't even think of contemplating considering embedding the cap
>>> attribute check in the SELinux code. cap_inode_setxattr() is called
>>> in
>>> the infrastructure.
>> Except that it isn't, not if any other security module is enabled and
>> implements those hooks, to prevent imposing CAP_SYS_ADMIN checks when
>> setting security.selinux or security.SMACK*.
>
> OK. Yes, this bit of the infrastructure is some of the
> worst I've done in a long time. This is a case where we
> already need special case stacking infrastructure. It looks
> like we'll have to separate setting the cap attribute from
> checking the cap state in order to make this work. In any
> case, the security_inode_setxattr() code is where the change
> belongs. There will likely be fallout changes in the modules,
> including the cap module.
> ?
>
>> An alternative approach to fixing this would be to change the cap
>> functions to only apply their checks if setting the capability
>> attribute and defer any checks on other security.* attributes to either
>> the security framework or the other security modules.  Then the
>> framework could always call all the modules on the inode_setxattr and
>> inode_removexattr hooks as with other hooks.  The security framework
>> would then need to ensure that a check is still applied when setting
>> security.* attributes if it isn't already handled by one of the enabled
>> security modules, as you don't want unprivileged userspace to be able
>> to set arbitrary security.foo attributes or to set up security.selinux
>> or security.SMACK* attributes if those modules happen to be disabled.
>
> Agreed. This isn't a two line change. Grumble.
>
> I can guess at what the problem might be, but I hate making
> assumptions when I go to fix a problem. I will start looking
> at a patch, but it would really help if I could say for sure
> what I'm out to accomplish. It may be obvious to the casual
> observer, but that description has not been applied to me very
> often.

Apologies for the delayed reply.

I am looking at security_inode_setxattr.

For setting attributes in the security.* the generic code in fs/xattr.c
applies no permission checks.

Each security module that implements an xattr in security.* then imposes
it's own policy on it's own attribute.

For smack the basic rule is smack_privileged(CAP_MAC_ADMIN).
For selinux the basic rule is inode_or_owner_capable(inode).
For commoncap the basic rule is capable_wrt_inode_uidgid(inode, CAP_SETFCAP).

commoncap also applies a default policity to setting security.* xattrs.
ns_capable(dentry->d_sb->s_userns, CAP_SYS_ADMIN).

smack reuses that default policy by calling cap_inode_setxattr if it
isn't a smack security.* xattr.

selinux has what looks like an old copy of the commoncap checks for
the security.* in selinux_inode_setotherxattr.  Testing for
capable(CAP_SETFCAP) for security.capable and capable(CAP_SYS_ADMIN)
for the others.

With the added complication that selinux calls
selinux_inode_setotherxattr also for the remove_xattr case.  So fixing
this in selinux_inode_setotherxattr is not appropriate.

I believe selinux also has general policy hooks it applies to all
invocations of setxattr.

So I think to really fix this we need to separate the cases of is this
your security modules attribute from general policy checks added by the
security modules.  Perhaps something like this for
security_inode_setxattr:

Hmm.  Looking at least ima also has the distinction between protecting
it's own xattr writes and running generaly security module policy on
xattr writes.

int security_inode_setxattr(struct dentry *dentry, const char *name,
			    const void *value, size_t size, int flags)
{
	int ret = 0;

	if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
		return 0;

	if (strncmp(name, XATTR_SECURITY_PREFIX,
			sizeof(XATTR_SECURITY_PREFIX) - 1) == 0) {
		/* Call the security modules and see if they all return
                 * -EOPNOTSUPP if so apply the default permission
                 * check of ns_capable(dentry->d_sb->s_user_ns, CAP_SYS_ADMIN)
                 * otherwise if one of the security modules supports
		 * this attribute (signaled by returning something other
		 * -EOPNOTSUPP) then set ret to that result.
                 *
                 * The security modules include at least smack, selinux,
		 * commoncap, ima, and evm.
                 */
                 ret = magic_inode_protect_setxattr(dentry, name, value, size);
        }
	if (ret)
		return ret;

        /* Run all of the security module policy against this setxattr call */
        return magic_inode_policy_setxattr(dentry, name, value, size);
}

Eric
--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in
the body of a message to majordomo at vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

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

* [RFC][PATCH] security: Make the selinux setxattr and removexattr hooks behave
  2017-09-30 16:22       ` Eric W. Biederman
@ 2017-09-30 17:01         ` Casey Schaufler
  2017-09-30 20:40           ` Eric W. Biederman
       [not found]           ` <db1c58f3-5a01-5276-eba7-5aac7cdcbcf5-iSGtlc1asvQWG2LlvL+J4A@public.gmane.org>
  0 siblings, 2 replies; 39+ messages in thread
From: Casey Schaufler @ 2017-09-30 17:01 UTC (permalink / raw)
  To: linux-security-module

On 9/30/2017 9:22 AM, Eric W. Biederman wrote:
> Casey Schaufler <casey@schaufler-ca.com> writes:
>
>> On 9/29/2017 7:18 AM, Stephen Smalley wrote:
>>> On Thu, 2017-09-28 at 18:16 -0700, Casey Schaufler wrote:
>>>> On 9/28/2017 3:34 PM, Eric W. Biederman wrote:
>>>>> It looks like once upon a time a long time ago selinux copied code
>>>>> from cap_inode_removexattr and cap_inode_setxattr into
>>>>> selinux_inode_setotherxattr.??However the code has now diverged and
>>>>> selinux is implementing a policy that is quite different than
>>>>> cap_inode_setxattr and cap_inode_removexattr especially when it
>>>>> comes
>>>>> to the security.capable xattr.
>>>> What leads you to believe that this isn't intentional?
>>>> It's most likely the case that this change occurred as
>>>> part of the first round module stacking change. What behavior
>>>> do you see that you're unhappy with?
>>>>
>>>>> To keep things working
>>>> Which "things"? How are they not "working"?
>>>>
>>>>> ?and to make the comments in security/security.c
>>>>> correct when the xattr is securit.capable, call cap_inode_setxattr
>>>>> or cap_inode_removexattr as appropriate.
>>>>>
>>>>> I suspect there is a larger conversation to be had here but this
>>>>> is enough to keep selinux from implementing a non-sense hard coded
>>>>> policy that breaks other parts of the kernel.
>>>> Specifics, please. Since I can't guess what problem you've
>>>> encountered I can't tell if it's here, in the infrastructure,
>>>> or in your perception of what constitutes "broken".
>>>>
>>>>> Signed-off-by: "Eric W. Biederman" <ebiederm@xmission.com>
>>>>> ---
>>>>> ?security/selinux/hooks.c | 6 ++++++
>>>>> ?1 file changed, 6 insertions(+)
>>>>>
>>>>> diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
>>>>> index f5d304736852..edf4bd292dc7 100644
>>>>> --- a/security/selinux/hooks.c
>>>>> +++ b/security/selinux/hooks.c
>>>>> @@ -3167,6 +3167,9 @@ static int selinux_inode_setxattr(struct
>>>>> dentry *dentry, const char *name,
>>>>> ?	u32 newsid, sid = current_sid();
>>>>> ?	int rc = 0;
>>>>> ?
>>>>> +	if (strcmp(name, XATTR_NAME_CAPS) == 0)
>>>>> +		return cap_inode_setxattr(dentry, name, value,
>>>>> size, flags);
>>>>> +
>>>> No. Don't even think of contemplating considering embedding the cap
>>>> attribute check in the SELinux code. cap_inode_setxattr() is called
>>>> in
>>>> the infrastructure.
>>> Except that it isn't, not if any other security module is enabled and
>>> implements those hooks, to prevent imposing CAP_SYS_ADMIN checks when
>>> setting security.selinux or security.SMACK*.
>> OK. Yes, this bit of the infrastructure is some of the
>> worst I've done in a long time. This is a case where we
>> already need special case stacking infrastructure. It looks
>> like we'll have to separate setting the cap attribute from
>> checking the cap state in order to make this work. In any
>> case, the security_inode_setxattr() code is where the change
>> belongs. There will likely be fallout changes in the modules,
>> including the cap module.
>> ?
>>
>>> An alternative approach to fixing this would be to change the cap
>>> functions to only apply their checks if setting the capability
>>> attribute and defer any checks on other security.* attributes to either
>>> the security framework or the other security modules.  Then the
>>> framework could always call all the modules on the inode_setxattr and
>>> inode_removexattr hooks as with other hooks.  The security framework
>>> would then need to ensure that a check is still applied when setting
>>> security.* attributes if it isn't already handled by one of the enabled
>>> security modules, as you don't want unprivileged userspace to be able
>>> to set arbitrary security.foo attributes or to set up security.selinux
>>> or security.SMACK* attributes if those modules happen to be disabled.
>> Agreed. This isn't a two line change. Grumble.
>>
>> I can guess at what the problem might be, but I hate making
>> assumptions when I go to fix a problem. I will start looking
>> at a patch, but it would really help if I could say for sure
>> what I'm out to accomplish. It may be obvious to the casual
>> observer, but that description has not been applied to me very
>> often.
> Apologies for the delayed reply.
>
> I am looking at security_inode_setxattr.
>
> For setting attributes in the security.* the generic code in fs/xattr.c
> applies no permission checks.
>
> Each security module that implements an xattr in security.* then imposes
> it's own policy on it's own attribute.
>
> For smack the basic rule is smack_privileged(CAP_MAC_ADMIN).
> For selinux the basic rule is inode_or_owner_capable(inode).
> For commoncap the basic rule is capable_wrt_inode_uidgid(inode, CAP_SETFCAP).
>
> commoncap also applies a default policity to setting security.* xattrs.
> ns_capable(dentry->d_sb->s_userns, CAP_SYS_ADMIN).
>
> smack reuses that default policy by calling cap_inode_setxattr if it
> isn't a smack security.* xattr.
>
> selinux has what looks like an old copy of the commoncap checks for
> the security.* in selinux_inode_setotherxattr.  Testing for
> capable(CAP_SETFCAP) for security.capable and capable(CAP_SYS_ADMIN)
> for the others.
>
> With the added complication that selinux calls
> selinux_inode_setotherxattr also for the remove_xattr case.  So fixing
> this in selinux_inode_setotherxattr is not appropriate.
>
> I believe selinux also has general policy hooks it applies to all
> invocations of setxattr.
>
> So I think to really fix this we need to separate the cases of is this
> your security modules attribute from general policy checks added by the
> security modules.  Perhaps something like this for
> security_inode_setxattr:
>
> Hmm.  Looking at least ima also has the distinction between protecting
> it's own xattr writes and running generaly security module policy on
> xattr writes.
>
> int security_inode_setxattr(struct dentry *dentry, const char *name,
> 			    const void *value, size_t size, int flags)
> {
> 	int ret = 0;
>
> 	if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
> 		return 0;
>
> 	if (strncmp(name, XATTR_SECURITY_PREFIX,
> 			sizeof(XATTR_SECURITY_PREFIX) - 1) == 0) {
> 		/* Call the security modules and see if they all return
>                  * -EOPNOTSUPP if so apply the default permission
>                  * check of ns_capable(dentry->d_sb->s_user_ns, CAP_SYS_ADMIN)
>                  * otherwise if one of the security modules supports
> 		 * this attribute (signaled by returning something other
> 		 * -EOPNOTSUPP) then set ret to that result.
>                  *
>                  * The security modules include at least smack, selinux,
> 		 * commoncap, ima, and evm.
>                  */
>                  ret = magic_inode_protect_setxattr(dentry, name, value, size);
>         }
> 	if (ret)
> 		return ret;
>
>         /* Run all of the security module policy against this setxattr call */
>         return magic_inode_policy_setxattr(dentry, name, value, size);
> }
>
> Eric

Yup, that's pretty much what I'm thinking. It's unfortunate
that the magic_ API isn't fully implemented. There's going to
be a good deal of code surgery instead. Is there an observed
problem today? This is going to have to get addressed for stacking,
so if there isn't a behavioral issue that impacts something real
I would like to defer spending significant time on it. Do you have
a case where this is not working correctly?


.
--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in
the body of a message to majordomo at vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

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

* Re: [RFC][PATCH] security: Make the selinux setxattr and removexattr hooks behave
       [not found]           ` <db1c58f3-5a01-5276-eba7-5aac7cdcbcf5-iSGtlc1asvQWG2LlvL+J4A@public.gmane.org>
@ 2017-09-30 20:40             ` Eric W. Biederman
  0 siblings, 0 replies; 39+ messages in thread
From: Eric W. Biederman @ 2017-09-30 20:40 UTC (permalink / raw)
  To: Casey Schaufler
  Cc: Paul Moore, Linux Containers, Chris Wright,
	linux-security-module-u79uwXL29TY76Z2rM5mHXA, James Morris,
	Eric Paris, Stephen Smalley

Casey Schaufler <casey@schaufler-ca.com> writes:

> On 9/30/2017 9:22 AM, Eric W. Biederman wrote:
>> Casey Schaufler <casey@schaufler-ca.com> writes:
>>
>>> On 9/29/2017 7:18 AM, Stephen Smalley wrote:
>>>> On Thu, 2017-09-28 at 18:16 -0700, Casey Schaufler wrote:
>>>>> On 9/28/2017 3:34 PM, Eric W. Biederman wrote:
>>>>>> It looks like once upon a time a long time ago selinux copied code
>>>>>> from cap_inode_removexattr and cap_inode_setxattr into
>>>>>> selinux_inode_setotherxattr.  However the code has now diverged and
>>>>>> selinux is implementing a policy that is quite different than
>>>>>> cap_inode_setxattr and cap_inode_removexattr especially when it
>>>>>> comes
>>>>>> to the security.capable xattr.
>>>>> What leads you to believe that this isn't intentional?
>>>>> It's most likely the case that this change occurred as
>>>>> part of the first round module stacking change. What behavior
>>>>> do you see that you're unhappy with?
>>>>>
>>>>>> To keep things working
>>>>> Which "things"? How are they not "working"?
>>>>>
>>>>>>  and to make the comments in security/security.c
>>>>>> correct when the xattr is securit.capable, call cap_inode_setxattr
>>>>>> or cap_inode_removexattr as appropriate.
>>>>>>
>>>>>> I suspect there is a larger conversation to be had here but this
>>>>>> is enough to keep selinux from implementing a non-sense hard coded
>>>>>> policy that breaks other parts of the kernel.
>>>>> Specifics, please. Since I can't guess what problem you've
>>>>> encountered I can't tell if it's here, in the infrastructure,
>>>>> or in your perception of what constitutes "broken".
>>>>>
>>>>>> Signed-off-by: "Eric W. Biederman" <ebiederm@xmission.com>
>>>>>> ---
>>>>>>  security/selinux/hooks.c | 6 ++++++
>>>>>>  1 file changed, 6 insertions(+)
>>>>>>
>>>>>> diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
>>>>>> index f5d304736852..edf4bd292dc7 100644
>>>>>> --- a/security/selinux/hooks.c
>>>>>> +++ b/security/selinux/hooks.c
>>>>>> @@ -3167,6 +3167,9 @@ static int selinux_inode_setxattr(struct
>>>>>> dentry *dentry, const char *name,
>>>>>>  	u32 newsid, sid = current_sid();
>>>>>>  	int rc = 0;
>>>>>>  
>>>>>> +	if (strcmp(name, XATTR_NAME_CAPS) == 0)
>>>>>> +		return cap_inode_setxattr(dentry, name, value,
>>>>>> size, flags);
>>>>>> +
>>>>> No. Don't even think of contemplating considering embedding the cap
>>>>> attribute check in the SELinux code. cap_inode_setxattr() is called
>>>>> in
>>>>> the infrastructure.
>>>> Except that it isn't, not if any other security module is enabled and
>>>> implements those hooks, to prevent imposing CAP_SYS_ADMIN checks when
>>>> setting security.selinux or security.SMACK*.
>>> OK. Yes, this bit of the infrastructure is some of the
>>> worst I've done in a long time. This is a case where we
>>> already need special case stacking infrastructure. It looks
>>> like we'll have to separate setting the cap attribute from
>>> checking the cap state in order to make this work. In any
>>> case, the security_inode_setxattr() code is where the change
>>> belongs. There will likely be fallout changes in the modules,
>>> including the cap module.
>>>  
>>>
>>>> An alternative approach to fixing this would be to change the cap
>>>> functions to only apply their checks if setting the capability
>>>> attribute and defer any checks on other security.* attributes to either
>>>> the security framework or the other security modules.  Then the
>>>> framework could always call all the modules on the inode_setxattr and
>>>> inode_removexattr hooks as with other hooks.  The security framework
>>>> would then need to ensure that a check is still applied when setting
>>>> security.* attributes if it isn't already handled by one of the enabled
>>>> security modules, as you don't want unprivileged userspace to be able
>>>> to set arbitrary security.foo attributes or to set up security.selinux
>>>> or security.SMACK* attributes if those modules happen to be disabled.
>>> Agreed. This isn't a two line change. Grumble.
>>>
>>> I can guess at what the problem might be, but I hate making
>>> assumptions when I go to fix a problem. I will start looking
>>> at a patch, but it would really help if I could say for sure
>>> what I'm out to accomplish. It may be obvious to the casual
>>> observer, but that description has not been applied to me very
>>> often.
>> Apologies for the delayed reply.
>>
>> I am looking at security_inode_setxattr.
>>
>> For setting attributes in the security.* the generic code in fs/xattr.c
>> applies no permission checks.
>>
>> Each security module that implements an xattr in security.* then imposes
>> it's own policy on it's own attribute.
>>
>> For smack the basic rule is smack_privileged(CAP_MAC_ADMIN).
>> For selinux the basic rule is inode_or_owner_capable(inode).
>> For commoncap the basic rule is capable_wrt_inode_uidgid(inode, CAP_SETFCAP).
>>
>> commoncap also applies a default policity to setting security.* xattrs.
>> ns_capable(dentry->d_sb->s_userns, CAP_SYS_ADMIN).
>>
>> smack reuses that default policy by calling cap_inode_setxattr if it
>> isn't a smack security.* xattr.
>>
>> selinux has what looks like an old copy of the commoncap checks for
>> the security.* in selinux_inode_setotherxattr.  Testing for
>> capable(CAP_SETFCAP) for security.capable and capable(CAP_SYS_ADMIN)
>> for the others.
>>
>> With the added complication that selinux calls
>> selinux_inode_setotherxattr also for the remove_xattr case.  So fixing
>> this in selinux_inode_setotherxattr is not appropriate.
>>
>> I believe selinux also has general policy hooks it applies to all
>> invocations of setxattr.
>>
>> So I think to really fix this we need to separate the cases of is this
>> your security modules attribute from general policy checks added by the
>> security modules.  Perhaps something like this for
>> security_inode_setxattr:
>>
>> Hmm.  Looking at least ima also has the distinction between protecting
>> it's own xattr writes and running generaly security module policy on
>> xattr writes.
>>
>> int security_inode_setxattr(struct dentry *dentry, const char *name,
>> 			    const void *value, size_t size, int flags)
>> {
>> 	int ret = 0;
>>
>> 	if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
>> 		return 0;
>>
>> 	if (strncmp(name, XATTR_SECURITY_PREFIX,
>> 			sizeof(XATTR_SECURITY_PREFIX) - 1) == 0) {
>> 		/* Call the security modules and see if they all return
>>                  * -EOPNOTSUPP if so apply the default permission
>>                  * check of ns_capable(dentry->d_sb->s_user_ns, CAP_SYS_ADMIN)
>>                  * otherwise if one of the security modules supports
>> 		 * this attribute (signaled by returning something other
>> 		 * -EOPNOTSUPP) then set ret to that result.
>>                  *
>>                  * The security modules include at least smack, selinux,
>> 		 * commoncap, ima, and evm.
>>                  */
>>                  ret = magic_inode_protect_setxattr(dentry, name, value, size);
>>         }
>> 	if (ret)
>> 		return ret;
>>
>>         /* Run all of the security module policy against this setxattr call */
>>         return magic_inode_policy_setxattr(dentry, name, value, size);
>> }
>>
>> Eric
>
> Yup, that's pretty much what I'm thinking. It's unfortunate
> that the magic_ API isn't fully implemented. There's going to
> be a good deal of code surgery instead. Is there an observed
> problem today? This is going to have to get addressed for stacking,
> so if there isn't a behavioral issue that impacts something real
> I would like to defer spending significant time on it. Do you have
> a case where this is not working correctly?

Merged as of 4.14-rc1 is the support for user namespace root to set
sercurity.capable.  This fails when selinux is loaded.

removexattr has the same problem and the code is a little less
convoluted in that case.

Not being able to set the capability when you should be able to is
very noticable.  Like running into a brick wall noticable.

Which is where the minimal patch for selinux comes in.  I think it
solves the exact case in question, even if it isn't the perfect long
term solution.

Eric

_______________________________________________
Containers mailing list
Containers@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/containers

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

* [RFC][PATCH] security: Make the selinux setxattr and removexattr hooks behave
  2017-09-30 17:01         ` Casey Schaufler
@ 2017-09-30 20:40           ` Eric W. Biederman
  2017-09-30 23:22             ` Casey Schaufler
       [not found]             ` <87d167ncms.fsf-aS9lmoZGLiVWk0Htik3J/w@public.gmane.org>
       [not found]           ` <db1c58f3-5a01-5276-eba7-5aac7cdcbcf5-iSGtlc1asvQWG2LlvL+J4A@public.gmane.org>
  1 sibling, 2 replies; 39+ messages in thread
From: Eric W. Biederman @ 2017-09-30 20:40 UTC (permalink / raw)
  To: linux-security-module

Casey Schaufler <casey@schaufler-ca.com> writes:

> On 9/30/2017 9:22 AM, Eric W. Biederman wrote:
>> Casey Schaufler <casey@schaufler-ca.com> writes:
>>
>>> On 9/29/2017 7:18 AM, Stephen Smalley wrote:
>>>> On Thu, 2017-09-28 at 18:16 -0700, Casey Schaufler wrote:
>>>>> On 9/28/2017 3:34 PM, Eric W. Biederman wrote:
>>>>>> It looks like once upon a time a long time ago selinux copied code
>>>>>> from cap_inode_removexattr and cap_inode_setxattr into
>>>>>> selinux_inode_setotherxattr.??However the code has now diverged and
>>>>>> selinux is implementing a policy that is quite different than
>>>>>> cap_inode_setxattr and cap_inode_removexattr especially when it
>>>>>> comes
>>>>>> to the security.capable xattr.
>>>>> What leads you to believe that this isn't intentional?
>>>>> It's most likely the case that this change occurred as
>>>>> part of the first round module stacking change. What behavior
>>>>> do you see that you're unhappy with?
>>>>>
>>>>>> To keep things working
>>>>> Which "things"? How are they not "working"?
>>>>>
>>>>>> ?and to make the comments in security/security.c
>>>>>> correct when the xattr is securit.capable, call cap_inode_setxattr
>>>>>> or cap_inode_removexattr as appropriate.
>>>>>>
>>>>>> I suspect there is a larger conversation to be had here but this
>>>>>> is enough to keep selinux from implementing a non-sense hard coded
>>>>>> policy that breaks other parts of the kernel.
>>>>> Specifics, please. Since I can't guess what problem you've
>>>>> encountered I can't tell if it's here, in the infrastructure,
>>>>> or in your perception of what constitutes "broken".
>>>>>
>>>>>> Signed-off-by: "Eric W. Biederman" <ebiederm@xmission.com>
>>>>>> ---
>>>>>> ?security/selinux/hooks.c | 6 ++++++
>>>>>> ?1 file changed, 6 insertions(+)
>>>>>>
>>>>>> diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
>>>>>> index f5d304736852..edf4bd292dc7 100644
>>>>>> --- a/security/selinux/hooks.c
>>>>>> +++ b/security/selinux/hooks.c
>>>>>> @@ -3167,6 +3167,9 @@ static int selinux_inode_setxattr(struct
>>>>>> dentry *dentry, const char *name,
>>>>>> ?	u32 newsid, sid = current_sid();
>>>>>> ?	int rc = 0;
>>>>>> ?
>>>>>> +	if (strcmp(name, XATTR_NAME_CAPS) == 0)
>>>>>> +		return cap_inode_setxattr(dentry, name, value,
>>>>>> size, flags);
>>>>>> +
>>>>> No. Don't even think of contemplating considering embedding the cap
>>>>> attribute check in the SELinux code. cap_inode_setxattr() is called
>>>>> in
>>>>> the infrastructure.
>>>> Except that it isn't, not if any other security module is enabled and
>>>> implements those hooks, to prevent imposing CAP_SYS_ADMIN checks when
>>>> setting security.selinux or security.SMACK*.
>>> OK. Yes, this bit of the infrastructure is some of the
>>> worst I've done in a long time. This is a case where we
>>> already need special case stacking infrastructure. It looks
>>> like we'll have to separate setting the cap attribute from
>>> checking the cap state in order to make this work. In any
>>> case, the security_inode_setxattr() code is where the change
>>> belongs. There will likely be fallout changes in the modules,
>>> including the cap module.
>>> ?
>>>
>>>> An alternative approach to fixing this would be to change the cap
>>>> functions to only apply their checks if setting the capability
>>>> attribute and defer any checks on other security.* attributes to either
>>>> the security framework or the other security modules.  Then the
>>>> framework could always call all the modules on the inode_setxattr and
>>>> inode_removexattr hooks as with other hooks.  The security framework
>>>> would then need to ensure that a check is still applied when setting
>>>> security.* attributes if it isn't already handled by one of the enabled
>>>> security modules, as you don't want unprivileged userspace to be able
>>>> to set arbitrary security.foo attributes or to set up security.selinux
>>>> or security.SMACK* attributes if those modules happen to be disabled.
>>> Agreed. This isn't a two line change. Grumble.
>>>
>>> I can guess at what the problem might be, but I hate making
>>> assumptions when I go to fix a problem. I will start looking
>>> at a patch, but it would really help if I could say for sure
>>> what I'm out to accomplish. It may be obvious to the casual
>>> observer, but that description has not been applied to me very
>>> often.
>> Apologies for the delayed reply.
>>
>> I am looking at security_inode_setxattr.
>>
>> For setting attributes in the security.* the generic code in fs/xattr.c
>> applies no permission checks.
>>
>> Each security module that implements an xattr in security.* then imposes
>> it's own policy on it's own attribute.
>>
>> For smack the basic rule is smack_privileged(CAP_MAC_ADMIN).
>> For selinux the basic rule is inode_or_owner_capable(inode).
>> For commoncap the basic rule is capable_wrt_inode_uidgid(inode, CAP_SETFCAP).
>>
>> commoncap also applies a default policity to setting security.* xattrs.
>> ns_capable(dentry->d_sb->s_userns, CAP_SYS_ADMIN).
>>
>> smack reuses that default policy by calling cap_inode_setxattr if it
>> isn't a smack security.* xattr.
>>
>> selinux has what looks like an old copy of the commoncap checks for
>> the security.* in selinux_inode_setotherxattr.  Testing for
>> capable(CAP_SETFCAP) for security.capable and capable(CAP_SYS_ADMIN)
>> for the others.
>>
>> With the added complication that selinux calls
>> selinux_inode_setotherxattr also for the remove_xattr case.  So fixing
>> this in selinux_inode_setotherxattr is not appropriate.
>>
>> I believe selinux also has general policy hooks it applies to all
>> invocations of setxattr.
>>
>> So I think to really fix this we need to separate the cases of is this
>> your security modules attribute from general policy checks added by the
>> security modules.  Perhaps something like this for
>> security_inode_setxattr:
>>
>> Hmm.  Looking at least ima also has the distinction between protecting
>> it's own xattr writes and running generaly security module policy on
>> xattr writes.
>>
>> int security_inode_setxattr(struct dentry *dentry, const char *name,
>> 			    const void *value, size_t size, int flags)
>> {
>> 	int ret = 0;
>>
>> 	if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
>> 		return 0;
>>
>> 	if (strncmp(name, XATTR_SECURITY_PREFIX,
>> 			sizeof(XATTR_SECURITY_PREFIX) - 1) == 0) {
>> 		/* Call the security modules and see if they all return
>>                  * -EOPNOTSUPP if so apply the default permission
>>                  * check of ns_capable(dentry->d_sb->s_user_ns, CAP_SYS_ADMIN)
>>                  * otherwise if one of the security modules supports
>> 		 * this attribute (signaled by returning something other
>> 		 * -EOPNOTSUPP) then set ret to that result.
>>                  *
>>                  * The security modules include at least smack, selinux,
>> 		 * commoncap, ima, and evm.
>>                  */
>>                  ret = magic_inode_protect_setxattr(dentry, name, value, size);
>>         }
>> 	if (ret)
>> 		return ret;
>>
>>         /* Run all of the security module policy against this setxattr call */
>>         return magic_inode_policy_setxattr(dentry, name, value, size);
>> }
>>
>> Eric
>
> Yup, that's pretty much what I'm thinking. It's unfortunate
> that the magic_ API isn't fully implemented. There's going to
> be a good deal of code surgery instead. Is there an observed
> problem today? This is going to have to get addressed for stacking,
> so if there isn't a behavioral issue that impacts something real
> I would like to defer spending significant time on it. Do you have
> a case where this is not working correctly?

Merged as of 4.14-rc1 is the support for user namespace root to set
sercurity.capable.  This fails when selinux is loaded.

removexattr has the same problem and the code is a little less
convoluted in that case.

Not being able to set the capability when you should be able to is
very noticable.  Like running into a brick wall noticable.

Which is where the minimal patch for selinux comes in.  I think it
solves the exact case in question, even if it isn't the perfect long
term solution.

Eric

--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in
the body of a message to majordomo at vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

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

* Re: [RFC][PATCH] security: Make the selinux setxattr and removexattr hooks behave
       [not found]             ` <87d167ncms.fsf-aS9lmoZGLiVWk0Htik3J/w@public.gmane.org>
@ 2017-09-30 23:22               ` Casey Schaufler
  0 siblings, 0 replies; 39+ messages in thread
From: Casey Schaufler @ 2017-09-30 23:22 UTC (permalink / raw)
  To: Eric W. Biederman
  Cc: Paul Moore, Linux Containers, Chris Wright,
	linux-security-module-u79uwXL29TY76Z2rM5mHXA, James Morris,
	Casey Schaufler, Eric Paris, Stephen Smalley

On 9/30/2017 1:40 PM, Eric W. Biederman wrote:
> Casey Schaufler <casey@schaufler-ca.com> writes:
>
>> On 9/30/2017 9:22 AM, Eric W. Biederman wrote:
>>> Casey Schaufler <casey@schaufler-ca.com> writes:
>>>
>>>> On 9/29/2017 7:18 AM, Stephen Smalley wrote:
>>>>> On Thu, 2017-09-28 at 18:16 -0700, Casey Schaufler wrote:
>>>>>> On 9/28/2017 3:34 PM, Eric W. Biederman wrote:
>>>>>>> It looks like once upon a time a long time ago selinux copied code
>>>>>>> from cap_inode_removexattr and cap_inode_setxattr into
>>>>>>> selinux_inode_setotherxattr.  However the code has now diverged and
>>>>>>> selinux is implementing a policy that is quite different than
>>>>>>> cap_inode_setxattr and cap_inode_removexattr especially when it
>>>>>>> comes
>>>>>>> to the security.capable xattr.
>>>>>> What leads you to believe that this isn't intentional?
>>>>>> It's most likely the case that this change occurred as
>>>>>> part of the first round module stacking change. What behavior
>>>>>> do you see that you're unhappy with?
>>>>>>
>>>>>>> To keep things working
>>>>>> Which "things"? How are they not "working"?
>>>>>>
>>>>>>>  and to make the comments in security/security.c
>>>>>>> correct when the xattr is securit.capable, call cap_inode_setxattr
>>>>>>> or cap_inode_removexattr as appropriate.
>>>>>>>
>>>>>>> I suspect there is a larger conversation to be had here but this
>>>>>>> is enough to keep selinux from implementing a non-sense hard coded
>>>>>>> policy that breaks other parts of the kernel.
>>>>>> Specifics, please. Since I can't guess what problem you've
>>>>>> encountered I can't tell if it's here, in the infrastructure,
>>>>>> or in your perception of what constitutes "broken".
>>>>>>
>>>>>>> Signed-off-by: "Eric W. Biederman" <ebiederm@xmission.com>
>>>>>>> ---
>>>>>>>  security/selinux/hooks.c | 6 ++++++
>>>>>>>  1 file changed, 6 insertions(+)
>>>>>>>
>>>>>>> diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
>>>>>>> index f5d304736852..edf4bd292dc7 100644
>>>>>>> --- a/security/selinux/hooks.c
>>>>>>> +++ b/security/selinux/hooks.c
>>>>>>> @@ -3167,6 +3167,9 @@ static int selinux_inode_setxattr(struct
>>>>>>> dentry *dentry, const char *name,
>>>>>>>  	u32 newsid, sid = current_sid();
>>>>>>>  	int rc = 0;
>>>>>>>  
>>>>>>> +	if (strcmp(name, XATTR_NAME_CAPS) == 0)
>>>>>>> +		return cap_inode_setxattr(dentry, name, value,
>>>>>>> size, flags);
>>>>>>> +
>>>>>> No. Don't even think of contemplating considering embedding the cap
>>>>>> attribute check in the SELinux code. cap_inode_setxattr() is called
>>>>>> in
>>>>>> the infrastructure.
>>>>> Except that it isn't, not if any other security module is enabled and
>>>>> implements those hooks, to prevent imposing CAP_SYS_ADMIN checks when
>>>>> setting security.selinux or security.SMACK*.
>>>> OK. Yes, this bit of the infrastructure is some of the
>>>> worst I've done in a long time. This is a case where we
>>>> already need special case stacking infrastructure. It looks
>>>> like we'll have to separate setting the cap attribute from
>>>> checking the cap state in order to make this work. In any
>>>> case, the security_inode_setxattr() code is where the change
>>>> belongs. There will likely be fallout changes in the modules,
>>>> including the cap module.
>>>>  
>>>>
>>>>> An alternative approach to fixing this would be to change the cap
>>>>> functions to only apply their checks if setting the capability
>>>>> attribute and defer any checks on other security.* attributes to either
>>>>> the security framework or the other security modules.  Then the
>>>>> framework could always call all the modules on the inode_setxattr and
>>>>> inode_removexattr hooks as with other hooks.  The security framework
>>>>> would then need to ensure that a check is still applied when setting
>>>>> security.* attributes if it isn't already handled by one of the enabled
>>>>> security modules, as you don't want unprivileged userspace to be able
>>>>> to set arbitrary security.foo attributes or to set up security.selinux
>>>>> or security.SMACK* attributes if those modules happen to be disabled.
>>>> Agreed. This isn't a two line change. Grumble.
>>>>
>>>> I can guess at what the problem might be, but I hate making
>>>> assumptions when I go to fix a problem. I will start looking
>>>> at a patch, but it would really help if I could say for sure
>>>> what I'm out to accomplish. It may be obvious to the casual
>>>> observer, but that description has not been applied to me very
>>>> often.
>>> Apologies for the delayed reply.
>>>
>>> I am looking at security_inode_setxattr.
>>>
>>> For setting attributes in the security.* the generic code in fs/xattr.c
>>> applies no permission checks.
>>>
>>> Each security module that implements an xattr in security.* then imposes
>>> it's own policy on it's own attribute.
>>>
>>> For smack the basic rule is smack_privileged(CAP_MAC_ADMIN).
>>> For selinux the basic rule is inode_or_owner_capable(inode).
>>> For commoncap the basic rule is capable_wrt_inode_uidgid(inode, CAP_SETFCAP).
>>>
>>> commoncap also applies a default policity to setting security.* xattrs.
>>> ns_capable(dentry->d_sb->s_userns, CAP_SYS_ADMIN).
>>>
>>> smack reuses that default policy by calling cap_inode_setxattr if it
>>> isn't a smack security.* xattr.
>>>
>>> selinux has what looks like an old copy of the commoncap checks for
>>> the security.* in selinux_inode_setotherxattr.  Testing for
>>> capable(CAP_SETFCAP) for security.capable and capable(CAP_SYS_ADMIN)
>>> for the others.
>>>
>>> With the added complication that selinux calls
>>> selinux_inode_setotherxattr also for the remove_xattr case.  So fixing
>>> this in selinux_inode_setotherxattr is not appropriate.
>>>
>>> I believe selinux also has general policy hooks it applies to all
>>> invocations of setxattr.
>>>
>>> So I think to really fix this we need to separate the cases of is this
>>> your security modules attribute from general policy checks added by the
>>> security modules.  Perhaps something like this for
>>> security_inode_setxattr:
>>>
>>> Hmm.  Looking at least ima also has the distinction between protecting
>>> it's own xattr writes and running generaly security module policy on
>>> xattr writes.
>>>
>>> int security_inode_setxattr(struct dentry *dentry, const char *name,
>>> 			    const void *value, size_t size, int flags)
>>> {
>>> 	int ret = 0;
>>>
>>> 	if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
>>> 		return 0;
>>>
>>> 	if (strncmp(name, XATTR_SECURITY_PREFIX,
>>> 			sizeof(XATTR_SECURITY_PREFIX) - 1) == 0) {
>>> 		/* Call the security modules and see if they all return
>>>                  * -EOPNOTSUPP if so apply the default permission
>>>                  * check of ns_capable(dentry->d_sb->s_user_ns, CAP_SYS_ADMIN)
>>>                  * otherwise if one of the security modules supports
>>> 		 * this attribute (signaled by returning something other
>>> 		 * -EOPNOTSUPP) then set ret to that result.
>>>                  *
>>>                  * The security modules include at least smack, selinux,
>>> 		 * commoncap, ima, and evm.
>>>                  */
>>>                  ret = magic_inode_protect_setxattr(dentry, name, value, size);
>>>         }
>>> 	if (ret)
>>> 		return ret;
>>>
>>>         /* Run all of the security module policy against this setxattr call */
>>>         return magic_inode_policy_setxattr(dentry, name, value, size);
>>> }
>>>
>>> Eric
>> Yup, that's pretty much what I'm thinking. It's unfortunate
>> that the magic_ API isn't fully implemented. There's going to
>> be a good deal of code surgery instead. Is there an observed
>> problem today? This is going to have to get addressed for stacking,
>> so if there isn't a behavioral issue that impacts something real
>> I would like to defer spending significant time on it. Do you have
>> a case where this is not working correctly?
> Merged as of 4.14-rc1 is the support for user namespace root to set
> sercurity.capable.  This fails when selinux is loaded.

OK. Is the failure unique to SELinux, or does it fail with
Smack as well?

> removexattr has the same problem and the code is a little less
> convoluted in that case.

Right. Because removexattr is a simpler situation.

> Not being able to set the capability when you should be able to is
> very noticable.  Like running into a brick wall noticable.

Ah, now you've identified the problem. Yes, I would agree that you've
uncovered an undesirable behavior.

> Which is where the minimal patch for selinux comes in.  I think it
> solves the exact case in question, even if it isn't the perfect long
> term solution.

If the problem is unique to SELinux I can see your logic. If it
isn't, that is, if it's also a problem with any other security
module, there either needs to be a fix for that/those module/s
as well or a "real" fix.

I'm not opposed to the SELinux short term fix if you can say
that that's the only module with the problem.

>
> Eric
>
> --
> To unsubscribe from this list: send the line "unsubscribe linux-security-module" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
>


.
_______________________________________________
Containers mailing list
Containers@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/containers

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

* [RFC][PATCH] security: Make the selinux setxattr and removexattr hooks behave
  2017-09-30 20:40           ` Eric W. Biederman
@ 2017-09-30 23:22             ` Casey Schaufler
  2017-10-01  1:02               ` Eric W. Biederman
       [not found]               ` <bf18e641-91ed-0d75-f514-c059b5dfbb14-iSGtlc1asvQWG2LlvL+J4A@public.gmane.org>
       [not found]             ` <87d167ncms.fsf-aS9lmoZGLiVWk0Htik3J/w@public.gmane.org>
  1 sibling, 2 replies; 39+ messages in thread
From: Casey Schaufler @ 2017-09-30 23:22 UTC (permalink / raw)
  To: linux-security-module

On 9/30/2017 1:40 PM, Eric W. Biederman wrote:
> Casey Schaufler <casey@schaufler-ca.com> writes:
>
>> On 9/30/2017 9:22 AM, Eric W. Biederman wrote:
>>> Casey Schaufler <casey@schaufler-ca.com> writes:
>>>
>>>> On 9/29/2017 7:18 AM, Stephen Smalley wrote:
>>>>> On Thu, 2017-09-28 at 18:16 -0700, Casey Schaufler wrote:
>>>>>> On 9/28/2017 3:34 PM, Eric W. Biederman wrote:
>>>>>>> It looks like once upon a time a long time ago selinux copied code
>>>>>>> from cap_inode_removexattr and cap_inode_setxattr into
>>>>>>> selinux_inode_setotherxattr.??However the code has now diverged and
>>>>>>> selinux is implementing a policy that is quite different than
>>>>>>> cap_inode_setxattr and cap_inode_removexattr especially when it
>>>>>>> comes
>>>>>>> to the security.capable xattr.
>>>>>> What leads you to believe that this isn't intentional?
>>>>>> It's most likely the case that this change occurred as
>>>>>> part of the first round module stacking change. What behavior
>>>>>> do you see that you're unhappy with?
>>>>>>
>>>>>>> To keep things working
>>>>>> Which "things"? How are they not "working"?
>>>>>>
>>>>>>> ?and to make the comments in security/security.c
>>>>>>> correct when the xattr is securit.capable, call cap_inode_setxattr
>>>>>>> or cap_inode_removexattr as appropriate.
>>>>>>>
>>>>>>> I suspect there is a larger conversation to be had here but this
>>>>>>> is enough to keep selinux from implementing a non-sense hard coded
>>>>>>> policy that breaks other parts of the kernel.
>>>>>> Specifics, please. Since I can't guess what problem you've
>>>>>> encountered I can't tell if it's here, in the infrastructure,
>>>>>> or in your perception of what constitutes "broken".
>>>>>>
>>>>>>> Signed-off-by: "Eric W. Biederman" <ebiederm@xmission.com>
>>>>>>> ---
>>>>>>> ?security/selinux/hooks.c | 6 ++++++
>>>>>>> ?1 file changed, 6 insertions(+)
>>>>>>>
>>>>>>> diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
>>>>>>> index f5d304736852..edf4bd292dc7 100644
>>>>>>> --- a/security/selinux/hooks.c
>>>>>>> +++ b/security/selinux/hooks.c
>>>>>>> @@ -3167,6 +3167,9 @@ static int selinux_inode_setxattr(struct
>>>>>>> dentry *dentry, const char *name,
>>>>>>> ?	u32 newsid, sid = current_sid();
>>>>>>> ?	int rc = 0;
>>>>>>> ?
>>>>>>> +	if (strcmp(name, XATTR_NAME_CAPS) == 0)
>>>>>>> +		return cap_inode_setxattr(dentry, name, value,
>>>>>>> size, flags);
>>>>>>> +
>>>>>> No. Don't even think of contemplating considering embedding the cap
>>>>>> attribute check in the SELinux code. cap_inode_setxattr() is called
>>>>>> in
>>>>>> the infrastructure.
>>>>> Except that it isn't, not if any other security module is enabled and
>>>>> implements those hooks, to prevent imposing CAP_SYS_ADMIN checks when
>>>>> setting security.selinux or security.SMACK*.
>>>> OK. Yes, this bit of the infrastructure is some of the
>>>> worst I've done in a long time. This is a case where we
>>>> already need special case stacking infrastructure. It looks
>>>> like we'll have to separate setting the cap attribute from
>>>> checking the cap state in order to make this work. In any
>>>> case, the security_inode_setxattr() code is where the change
>>>> belongs. There will likely be fallout changes in the modules,
>>>> including the cap module.
>>>> ?
>>>>
>>>>> An alternative approach to fixing this would be to change the cap
>>>>> functions to only apply their checks if setting the capability
>>>>> attribute and defer any checks on other security.* attributes to either
>>>>> the security framework or the other security modules.  Then the
>>>>> framework could always call all the modules on the inode_setxattr and
>>>>> inode_removexattr hooks as with other hooks.  The security framework
>>>>> would then need to ensure that a check is still applied when setting
>>>>> security.* attributes if it isn't already handled by one of the enabled
>>>>> security modules, as you don't want unprivileged userspace to be able
>>>>> to set arbitrary security.foo attributes or to set up security.selinux
>>>>> or security.SMACK* attributes if those modules happen to be disabled.
>>>> Agreed. This isn't a two line change. Grumble.
>>>>
>>>> I can guess at what the problem might be, but I hate making
>>>> assumptions when I go to fix a problem. I will start looking
>>>> at a patch, but it would really help if I could say for sure
>>>> what I'm out to accomplish. It may be obvious to the casual
>>>> observer, but that description has not been applied to me very
>>>> often.
>>> Apologies for the delayed reply.
>>>
>>> I am looking at security_inode_setxattr.
>>>
>>> For setting attributes in the security.* the generic code in fs/xattr.c
>>> applies no permission checks.
>>>
>>> Each security module that implements an xattr in security.* then imposes
>>> it's own policy on it's own attribute.
>>>
>>> For smack the basic rule is smack_privileged(CAP_MAC_ADMIN).
>>> For selinux the basic rule is inode_or_owner_capable(inode).
>>> For commoncap the basic rule is capable_wrt_inode_uidgid(inode, CAP_SETFCAP).
>>>
>>> commoncap also applies a default policity to setting security.* xattrs.
>>> ns_capable(dentry->d_sb->s_userns, CAP_SYS_ADMIN).
>>>
>>> smack reuses that default policy by calling cap_inode_setxattr if it
>>> isn't a smack security.* xattr.
>>>
>>> selinux has what looks like an old copy of the commoncap checks for
>>> the security.* in selinux_inode_setotherxattr.  Testing for
>>> capable(CAP_SETFCAP) for security.capable and capable(CAP_SYS_ADMIN)
>>> for the others.
>>>
>>> With the added complication that selinux calls
>>> selinux_inode_setotherxattr also for the remove_xattr case.  So fixing
>>> this in selinux_inode_setotherxattr is not appropriate.
>>>
>>> I believe selinux also has general policy hooks it applies to all
>>> invocations of setxattr.
>>>
>>> So I think to really fix this we need to separate the cases of is this
>>> your security modules attribute from general policy checks added by the
>>> security modules.  Perhaps something like this for
>>> security_inode_setxattr:
>>>
>>> Hmm.  Looking at least ima also has the distinction between protecting
>>> it's own xattr writes and running generaly security module policy on
>>> xattr writes.
>>>
>>> int security_inode_setxattr(struct dentry *dentry, const char *name,
>>> 			    const void *value, size_t size, int flags)
>>> {
>>> 	int ret = 0;
>>>
>>> 	if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
>>> 		return 0;
>>>
>>> 	if (strncmp(name, XATTR_SECURITY_PREFIX,
>>> 			sizeof(XATTR_SECURITY_PREFIX) - 1) == 0) {
>>> 		/* Call the security modules and see if they all return
>>>                  * -EOPNOTSUPP if so apply the default permission
>>>                  * check of ns_capable(dentry->d_sb->s_user_ns, CAP_SYS_ADMIN)
>>>                  * otherwise if one of the security modules supports
>>> 		 * this attribute (signaled by returning something other
>>> 		 * -EOPNOTSUPP) then set ret to that result.
>>>                  *
>>>                  * The security modules include at least smack, selinux,
>>> 		 * commoncap, ima, and evm.
>>>                  */
>>>                  ret = magic_inode_protect_setxattr(dentry, name, value, size);
>>>         }
>>> 	if (ret)
>>> 		return ret;
>>>
>>>         /* Run all of the security module policy against this setxattr call */
>>>         return magic_inode_policy_setxattr(dentry, name, value, size);
>>> }
>>>
>>> Eric
>> Yup, that's pretty much what I'm thinking. It's unfortunate
>> that the magic_ API isn't fully implemented. There's going to
>> be a good deal of code surgery instead. Is there an observed
>> problem today? This is going to have to get addressed for stacking,
>> so if there isn't a behavioral issue that impacts something real
>> I would like to defer spending significant time on it. Do you have
>> a case where this is not working correctly?
> Merged as of 4.14-rc1 is the support for user namespace root to set
> sercurity.capable.  This fails when selinux is loaded.

OK. Is the failure unique to SELinux, or does it fail with
Smack as well?

> removexattr has the same problem and the code is a little less
> convoluted in that case.

Right. Because removexattr is a simpler situation.

> Not being able to set the capability when you should be able to is
> very noticable.  Like running into a brick wall noticable.

Ah, now you've identified the problem. Yes, I would agree that you've
uncovered an undesirable behavior.

> Which is where the minimal patch for selinux comes in.  I think it
> solves the exact case in question, even if it isn't the perfect long
> term solution.

If the problem is unique to SELinux I can see your logic. If it
isn't, that is, if it's also a problem with any other security
module, there either needs to be a fix for that/those module/s
as well or a "real" fix.

I'm not opposed to the SELinux short term fix if you can say
that that's the only module with the problem.

>
> Eric
>
> --
> To unsubscribe from this list: send the line "unsubscribe linux-security-module" in
> the body of a message to majordomo at vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
>


.
--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in
the body of a message to majordomo at vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

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

* Re: [RFC][PATCH] security: Make the selinux setxattr and removexattr hooks behave
       [not found]               ` <bf18e641-91ed-0d75-f514-c059b5dfbb14-iSGtlc1asvQWG2LlvL+J4A@public.gmane.org>
@ 2017-10-01  1:02                 ` Eric W. Biederman
  0 siblings, 0 replies; 39+ messages in thread
From: Eric W. Biederman @ 2017-10-01  1:02 UTC (permalink / raw)
  To: Casey Schaufler
  Cc: Paul Moore, Linux Containers, Chris Wright,
	linux-security-module-u79uwXL29TY76Z2rM5mHXA, James Morris,
	Eric Paris, Stephen Smalley

Casey Schaufler <casey@schaufler-ca.com> writes:

> On 9/30/2017 1:40 PM, Eric W. Biederman wrote:
>> Casey Schaufler <casey@schaufler-ca.com> writes:
>>
>>> On 9/30/2017 9:22 AM, Eric W. Biederman wrote:
>>>> Casey Schaufler <casey@schaufler-ca.com> writes:
>>>>
>>>>> On 9/29/2017 7:18 AM, Stephen Smalley wrote:
>>>>>> On Thu, 2017-09-28 at 18:16 -0700, Casey Schaufler wrote:
>>>>>>> On 9/28/2017 3:34 PM, Eric W. Biederman wrote:
>>>>>>>> It looks like once upon a time a long time ago selinux copied code
>>>>>>>> from cap_inode_removexattr and cap_inode_setxattr into
>>>>>>>> selinux_inode_setotherxattr.  However the code has now diverged and
>>>>>>>> selinux is implementing a policy that is quite different than
>>>>>>>> cap_inode_setxattr and cap_inode_removexattr especially when it
>>>>>>>> comes
>>>>>>>> to the security.capable xattr.
>>>>>>> What leads you to believe that this isn't intentional?
>>>>>>> It's most likely the case that this change occurred as
>>>>>>> part of the first round module stacking change. What behavior
>>>>>>> do you see that you're unhappy with?
>>>>>>>
>>>>>>>> To keep things working
>>>>>>> Which "things"? How are they not "working"?
>>>>>>>
>>>>>>>>  and to make the comments in security/security.c
>>>>>>>> correct when the xattr is securit.capable, call cap_inode_setxattr
>>>>>>>> or cap_inode_removexattr as appropriate.
>>>>>>>>
>>>>>>>> I suspect there is a larger conversation to be had here but this
>>>>>>>> is enough to keep selinux from implementing a non-sense hard coded
>>>>>>>> policy that breaks other parts of the kernel.
>>>>>>> Specifics, please. Since I can't guess what problem you've
>>>>>>> encountered I can't tell if it's here, in the infrastructure,
>>>>>>> or in your perception of what constitutes "broken".
>>>>>>>
>>>>>>>> Signed-off-by: "Eric W. Biederman" <ebiederm@xmission.com>
>>>>>>>> ---
>>>>>>>>  security/selinux/hooks.c | 6 ++++++
>>>>>>>>  1 file changed, 6 insertions(+)
>>>>>>>>
>>>>>>>> diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
>>>>>>>> index f5d304736852..edf4bd292dc7 100644
>>>>>>>> --- a/security/selinux/hooks.c
>>>>>>>> +++ b/security/selinux/hooks.c
>>>>>>>> @@ -3167,6 +3167,9 @@ static int selinux_inode_setxattr(struct
>>>>>>>> dentry *dentry, const char *name,
>>>>>>>>  	u32 newsid, sid = current_sid();
>>>>>>>>  	int rc = 0;
>>>>>>>>  
>>>>>>>> +	if (strcmp(name, XATTR_NAME_CAPS) == 0)
>>>>>>>> +		return cap_inode_setxattr(dentry, name, value,
>>>>>>>> size, flags);
>>>>>>>> +
>>>>>>> No. Don't even think of contemplating considering embedding the cap
>>>>>>> attribute check in the SELinux code. cap_inode_setxattr() is called
>>>>>>> in
>>>>>>> the infrastructure.
>>>>>> Except that it isn't, not if any other security module is enabled and
>>>>>> implements those hooks, to prevent imposing CAP_SYS_ADMIN checks when
>>>>>> setting security.selinux or security.SMACK*.
>>>>> OK. Yes, this bit of the infrastructure is some of the
>>>>> worst I've done in a long time. This is a case where we
>>>>> already need special case stacking infrastructure. It looks
>>>>> like we'll have to separate setting the cap attribute from
>>>>> checking the cap state in order to make this work. In any
>>>>> case, the security_inode_setxattr() code is where the change
>>>>> belongs. There will likely be fallout changes in the modules,
>>>>> including the cap module.
>>>>>  
>>>>>
>>>>>> An alternative approach to fixing this would be to change the cap
>>>>>> functions to only apply their checks if setting the capability
>>>>>> attribute and defer any checks on other security.* attributes to either
>>>>>> the security framework or the other security modules.  Then the
>>>>>> framework could always call all the modules on the inode_setxattr and
>>>>>> inode_removexattr hooks as with other hooks.  The security framework
>>>>>> would then need to ensure that a check is still applied when setting
>>>>>> security.* attributes if it isn't already handled by one of the enabled
>>>>>> security modules, as you don't want unprivileged userspace to be able
>>>>>> to set arbitrary security.foo attributes or to set up security.selinux
>>>>>> or security.SMACK* attributes if those modules happen to be disabled.
>>>>> Agreed. This isn't a two line change. Grumble.
>>>>>
>>>>> I can guess at what the problem might be, but I hate making
>>>>> assumptions when I go to fix a problem. I will start looking
>>>>> at a patch, but it would really help if I could say for sure
>>>>> what I'm out to accomplish. It may be obvious to the casual
>>>>> observer, but that description has not been applied to me very
>>>>> often.
>>>> Apologies for the delayed reply.
>>>>
>>>> I am looking at security_inode_setxattr.
>>>>
>>>> For setting attributes in the security.* the generic code in fs/xattr.c
>>>> applies no permission checks.
>>>>
>>>> Each security module that implements an xattr in security.* then imposes
>>>> it's own policy on it's own attribute.
>>>>
>>>> For smack the basic rule is smack_privileged(CAP_MAC_ADMIN).
>>>> For selinux the basic rule is inode_or_owner_capable(inode).
>>>> For commoncap the basic rule is capable_wrt_inode_uidgid(inode, CAP_SETFCAP).
>>>>
>>>> commoncap also applies a default policity to setting security.* xattrs.
>>>> ns_capable(dentry->d_sb->s_userns, CAP_SYS_ADMIN).
>>>>
>>>> smack reuses that default policy by calling cap_inode_setxattr if it
>>>> isn't a smack security.* xattr.
>>>>
>>>> selinux has what looks like an old copy of the commoncap checks for
>>>> the security.* in selinux_inode_setotherxattr.  Testing for
>>>> capable(CAP_SETFCAP) for security.capable and capable(CAP_SYS_ADMIN)
>>>> for the others.
>>>>
>>>> With the added complication that selinux calls
>>>> selinux_inode_setotherxattr also for the remove_xattr case.  So fixing
>>>> this in selinux_inode_setotherxattr is not appropriate.
>>>>
>>>> I believe selinux also has general policy hooks it applies to all
>>>> invocations of setxattr.
>>>>
>>>> So I think to really fix this we need to separate the cases of is this
>>>> your security modules attribute from general policy checks added by the
>>>> security modules.  Perhaps something like this for
>>>> security_inode_setxattr:
>>>>
>>>> Hmm.  Looking at least ima also has the distinction between protecting
>>>> it's own xattr writes and running generaly security module policy on
>>>> xattr writes.
>>>>
>>>> int security_inode_setxattr(struct dentry *dentry, const char *name,
>>>> 			    const void *value, size_t size, int flags)
>>>> {
>>>> 	int ret = 0;
>>>>
>>>> 	if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
>>>> 		return 0;
>>>>
>>>> 	if (strncmp(name, XATTR_SECURITY_PREFIX,
>>>> 			sizeof(XATTR_SECURITY_PREFIX) - 1) == 0) {
>>>> 		/* Call the security modules and see if they all return
>>>>                  * -EOPNOTSUPP if so apply the default permission
>>>>                  * check of ns_capable(dentry->d_sb->s_user_ns, CAP_SYS_ADMIN)
>>>>                  * otherwise if one of the security modules supports
>>>> 		 * this attribute (signaled by returning something other
>>>> 		 * -EOPNOTSUPP) then set ret to that result.
>>>>                  *
>>>>                  * The security modules include at least smack, selinux,
>>>> 		 * commoncap, ima, and evm.
>>>>                  */
>>>>                  ret = magic_inode_protect_setxattr(dentry, name, value, size);
>>>>         }
>>>> 	if (ret)
>>>> 		return ret;
>>>>
>>>>         /* Run all of the security module policy against this setxattr call */
>>>>         return magic_inode_policy_setxattr(dentry, name, value, size);
>>>> }
>>>>
>>>> Eric
>>> Yup, that's pretty much what I'm thinking. It's unfortunate
>>> that the magic_ API isn't fully implemented. There's going to
>>> be a good deal of code surgery instead. Is there an observed
>>> problem today? This is going to have to get addressed for stacking,
>>> so if there isn't a behavioral issue that impacts something real
>>> I would like to defer spending significant time on it. Do you have
>>> a case where this is not working correctly?
>> Merged as of 4.14-rc1 is the support for user namespace root to set
>> sercurity.capable.  This fails when selinux is loaded.
>
> OK. Is the failure unique to SELinux, or does it fail with
> Smack as well?

I don't have a smack configuration handy, but reading through
the code smack setxattr the permission checks for all xattrs
that are not smack xattrs to cap_inode_setxattr.

So smack and commoncap combined will not fail.

smack and selinux will result in people who should be able to set
selinux xattrs not being able to.  That however is less of an immediate
problem.

>> removexattr has the same problem and the code is a little less
>> convoluted in that case.
>
> Right. Because removexattr is a simpler situation.
>
>> Not being able to set the capability when you should be able to is
>> very noticable.  Like running into a brick wall noticable.
>
> Ah, now you've identified the problem. Yes, I would agree that you've
> uncovered an undesirable behavior.

Apologies for not being clearer earlier, but I was still in shock from
running into a brick wall.

>> Which is where the minimal patch for selinux comes in.  I think it
>> solves the exact case in question, even if it isn't the perfect long
>> term solution.
>
> If the problem is unique to SELinux I can see your logic. If it
> isn't, that is, if it's also a problem with any other security
> module, there either needs to be a fix for that/those module/s
> as well or a "real" fix.
>
> I'm not opposed to the SELinux short term fix if you can say
> that that's the only module with the problem.

So far there are exactly two implementation of
LSM_HOOK_INIT(inode_setxattr, ...)

So as a practical case it looks like combination with selinux is the
only case where the problem will be observed right now.  And it makes
the code at least somewhat match the comments in
security_inode_setxattr.

Eric


_______________________________________________
Containers mailing list
Containers@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/containers

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

* [RFC][PATCH] security: Make the selinux setxattr and removexattr hooks behave
  2017-09-30 23:22             ` Casey Schaufler
@ 2017-10-01  1:02               ` Eric W. Biederman
  2017-10-01 18:52                 ` Casey Schaufler
       [not found]               ` <bf18e641-91ed-0d75-f514-c059b5dfbb14-iSGtlc1asvQWG2LlvL+J4A@public.gmane.org>
  1 sibling, 1 reply; 39+ messages in thread
From: Eric W. Biederman @ 2017-10-01  1:02 UTC (permalink / raw)
  To: linux-security-module

Casey Schaufler <casey@schaufler-ca.com> writes:

> On 9/30/2017 1:40 PM, Eric W. Biederman wrote:
>> Casey Schaufler <casey@schaufler-ca.com> writes:
>>
>>> On 9/30/2017 9:22 AM, Eric W. Biederman wrote:
>>>> Casey Schaufler <casey@schaufler-ca.com> writes:
>>>>
>>>>> On 9/29/2017 7:18 AM, Stephen Smalley wrote:
>>>>>> On Thu, 2017-09-28 at 18:16 -0700, Casey Schaufler wrote:
>>>>>>> On 9/28/2017 3:34 PM, Eric W. Biederman wrote:
>>>>>>>> It looks like once upon a time a long time ago selinux copied code
>>>>>>>> from cap_inode_removexattr and cap_inode_setxattr into
>>>>>>>> selinux_inode_setotherxattr.??However the code has now diverged and
>>>>>>>> selinux is implementing a policy that is quite different than
>>>>>>>> cap_inode_setxattr and cap_inode_removexattr especially when it
>>>>>>>> comes
>>>>>>>> to the security.capable xattr.
>>>>>>> What leads you to believe that this isn't intentional?
>>>>>>> It's most likely the case that this change occurred as
>>>>>>> part of the first round module stacking change. What behavior
>>>>>>> do you see that you're unhappy with?
>>>>>>>
>>>>>>>> To keep things working
>>>>>>> Which "things"? How are they not "working"?
>>>>>>>
>>>>>>>> ?and to make the comments in security/security.c
>>>>>>>> correct when the xattr is securit.capable, call cap_inode_setxattr
>>>>>>>> or cap_inode_removexattr as appropriate.
>>>>>>>>
>>>>>>>> I suspect there is a larger conversation to be had here but this
>>>>>>>> is enough to keep selinux from implementing a non-sense hard coded
>>>>>>>> policy that breaks other parts of the kernel.
>>>>>>> Specifics, please. Since I can't guess what problem you've
>>>>>>> encountered I can't tell if it's here, in the infrastructure,
>>>>>>> or in your perception of what constitutes "broken".
>>>>>>>
>>>>>>>> Signed-off-by: "Eric W. Biederman" <ebiederm@xmission.com>
>>>>>>>> ---
>>>>>>>> ?security/selinux/hooks.c | 6 ++++++
>>>>>>>> ?1 file changed, 6 insertions(+)
>>>>>>>>
>>>>>>>> diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
>>>>>>>> index f5d304736852..edf4bd292dc7 100644
>>>>>>>> --- a/security/selinux/hooks.c
>>>>>>>> +++ b/security/selinux/hooks.c
>>>>>>>> @@ -3167,6 +3167,9 @@ static int selinux_inode_setxattr(struct
>>>>>>>> dentry *dentry, const char *name,
>>>>>>>> ?	u32 newsid, sid = current_sid();
>>>>>>>> ?	int rc = 0;
>>>>>>>> ?
>>>>>>>> +	if (strcmp(name, XATTR_NAME_CAPS) == 0)
>>>>>>>> +		return cap_inode_setxattr(dentry, name, value,
>>>>>>>> size, flags);
>>>>>>>> +
>>>>>>> No. Don't even think of contemplating considering embedding the cap
>>>>>>> attribute check in the SELinux code. cap_inode_setxattr() is called
>>>>>>> in
>>>>>>> the infrastructure.
>>>>>> Except that it isn't, not if any other security module is enabled and
>>>>>> implements those hooks, to prevent imposing CAP_SYS_ADMIN checks when
>>>>>> setting security.selinux or security.SMACK*.
>>>>> OK. Yes, this bit of the infrastructure is some of the
>>>>> worst I've done in a long time. This is a case where we
>>>>> already need special case stacking infrastructure. It looks
>>>>> like we'll have to separate setting the cap attribute from
>>>>> checking the cap state in order to make this work. In any
>>>>> case, the security_inode_setxattr() code is where the change
>>>>> belongs. There will likely be fallout changes in the modules,
>>>>> including the cap module.
>>>>> ?
>>>>>
>>>>>> An alternative approach to fixing this would be to change the cap
>>>>>> functions to only apply their checks if setting the capability
>>>>>> attribute and defer any checks on other security.* attributes to either
>>>>>> the security framework or the other security modules.  Then the
>>>>>> framework could always call all the modules on the inode_setxattr and
>>>>>> inode_removexattr hooks as with other hooks.  The security framework
>>>>>> would then need to ensure that a check is still applied when setting
>>>>>> security.* attributes if it isn't already handled by one of the enabled
>>>>>> security modules, as you don't want unprivileged userspace to be able
>>>>>> to set arbitrary security.foo attributes or to set up security.selinux
>>>>>> or security.SMACK* attributes if those modules happen to be disabled.
>>>>> Agreed. This isn't a two line change. Grumble.
>>>>>
>>>>> I can guess at what the problem might be, but I hate making
>>>>> assumptions when I go to fix a problem. I will start looking
>>>>> at a patch, but it would really help if I could say for sure
>>>>> what I'm out to accomplish. It may be obvious to the casual
>>>>> observer, but that description has not been applied to me very
>>>>> often.
>>>> Apologies for the delayed reply.
>>>>
>>>> I am looking at security_inode_setxattr.
>>>>
>>>> For setting attributes in the security.* the generic code in fs/xattr.c
>>>> applies no permission checks.
>>>>
>>>> Each security module that implements an xattr in security.* then imposes
>>>> it's own policy on it's own attribute.
>>>>
>>>> For smack the basic rule is smack_privileged(CAP_MAC_ADMIN).
>>>> For selinux the basic rule is inode_or_owner_capable(inode).
>>>> For commoncap the basic rule is capable_wrt_inode_uidgid(inode, CAP_SETFCAP).
>>>>
>>>> commoncap also applies a default policity to setting security.* xattrs.
>>>> ns_capable(dentry->d_sb->s_userns, CAP_SYS_ADMIN).
>>>>
>>>> smack reuses that default policy by calling cap_inode_setxattr if it
>>>> isn't a smack security.* xattr.
>>>>
>>>> selinux has what looks like an old copy of the commoncap checks for
>>>> the security.* in selinux_inode_setotherxattr.  Testing for
>>>> capable(CAP_SETFCAP) for security.capable and capable(CAP_SYS_ADMIN)
>>>> for the others.
>>>>
>>>> With the added complication that selinux calls
>>>> selinux_inode_setotherxattr also for the remove_xattr case.  So fixing
>>>> this in selinux_inode_setotherxattr is not appropriate.
>>>>
>>>> I believe selinux also has general policy hooks it applies to all
>>>> invocations of setxattr.
>>>>
>>>> So I think to really fix this we need to separate the cases of is this
>>>> your security modules attribute from general policy checks added by the
>>>> security modules.  Perhaps something like this for
>>>> security_inode_setxattr:
>>>>
>>>> Hmm.  Looking at least ima also has the distinction between protecting
>>>> it's own xattr writes and running generaly security module policy on
>>>> xattr writes.
>>>>
>>>> int security_inode_setxattr(struct dentry *dentry, const char *name,
>>>> 			    const void *value, size_t size, int flags)
>>>> {
>>>> 	int ret = 0;
>>>>
>>>> 	if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
>>>> 		return 0;
>>>>
>>>> 	if (strncmp(name, XATTR_SECURITY_PREFIX,
>>>> 			sizeof(XATTR_SECURITY_PREFIX) - 1) == 0) {
>>>> 		/* Call the security modules and see if they all return
>>>>                  * -EOPNOTSUPP if so apply the default permission
>>>>                  * check of ns_capable(dentry->d_sb->s_user_ns, CAP_SYS_ADMIN)
>>>>                  * otherwise if one of the security modules supports
>>>> 		 * this attribute (signaled by returning something other
>>>> 		 * -EOPNOTSUPP) then set ret to that result.
>>>>                  *
>>>>                  * The security modules include at least smack, selinux,
>>>> 		 * commoncap, ima, and evm.
>>>>                  */
>>>>                  ret = magic_inode_protect_setxattr(dentry, name, value, size);
>>>>         }
>>>> 	if (ret)
>>>> 		return ret;
>>>>
>>>>         /* Run all of the security module policy against this setxattr call */
>>>>         return magic_inode_policy_setxattr(dentry, name, value, size);
>>>> }
>>>>
>>>> Eric
>>> Yup, that's pretty much what I'm thinking. It's unfortunate
>>> that the magic_ API isn't fully implemented. There's going to
>>> be a good deal of code surgery instead. Is there an observed
>>> problem today? This is going to have to get addressed for stacking,
>>> so if there isn't a behavioral issue that impacts something real
>>> I would like to defer spending significant time on it. Do you have
>>> a case where this is not working correctly?
>> Merged as of 4.14-rc1 is the support for user namespace root to set
>> sercurity.capable.  This fails when selinux is loaded.
>
> OK. Is the failure unique to SELinux, or does it fail with
> Smack as well?

I don't have a smack configuration handy, but reading through
the code smack setxattr the permission checks for all xattrs
that are not smack xattrs to cap_inode_setxattr.

So smack and commoncap combined will not fail.

smack and selinux will result in people who should be able to set
selinux xattrs not being able to.  That however is less of an immediate
problem.

>> removexattr has the same problem and the code is a little less
>> convoluted in that case.
>
> Right. Because removexattr is a simpler situation.
>
>> Not being able to set the capability when you should be able to is
>> very noticable.  Like running into a brick wall noticable.
>
> Ah, now you've identified the problem. Yes, I would agree that you've
> uncovered an undesirable behavior.

Apologies for not being clearer earlier, but I was still in shock from
running into a brick wall.

>> Which is where the minimal patch for selinux comes in.  I think it
>> solves the exact case in question, even if it isn't the perfect long
>> term solution.
>
> If the problem is unique to SELinux I can see your logic. If it
> isn't, that is, if it's also a problem with any other security
> module, there either needs to be a fix for that/those module/s
> as well or a "real" fix.
>
> I'm not opposed to the SELinux short term fix if you can say
> that that's the only module with the problem.

So far there are exactly two implementation of
LSM_HOOK_INIT(inode_setxattr, ...)

So as a practical case it looks like combination with selinux is the
only case where the problem will be observed right now.  And it makes
the code at least somewhat match the comments in
security_inode_setxattr.

Eric


--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in
the body of a message to majordomo at vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

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

* [RFC][PATCH] security: Make the selinux setxattr and removexattr hooks behave
  2017-10-01  1:02               ` Eric W. Biederman
@ 2017-10-01 18:52                 ` Casey Schaufler
  2017-10-01 19:54                   ` Serge E. Hallyn
  2017-10-01 22:11                   ` Eric W. Biederman
  0 siblings, 2 replies; 39+ messages in thread
From: Casey Schaufler @ 2017-10-01 18:52 UTC (permalink / raw)
  To: linux-security-module

On 9/30/2017 6:02 PM, Eric W. Biederman wrote:
> Casey Schaufler <casey@schaufler-ca.com> writes:
>
>> On 9/30/2017 1:40 PM, Eric W. Biederman wrote:
>>> Casey Schaufler <casey@schaufler-ca.com> writes:
>>>
>>>> On 9/30/2017 9:22 AM, Eric W. Biederman wrote:
>>>>> Casey Schaufler <casey@schaufler-ca.com> writes:
>>>>>
>>>>>> On 9/29/2017 7:18 AM, Stephen Smalley wrote:
>>>>>>> On Thu, 2017-09-28 at 18:16 -0700, Casey Schaufler wrote:
>>>>>>>> On 9/28/2017 3:34 PM, Eric W. Biederman wrote:
>>>>>>>>> It looks like once upon a time a long time ago selinux copied code
>>>>>>>>> from cap_inode_removexattr and cap_inode_setxattr into
>>>>>>>>> selinux_inode_setotherxattr.??However the code has now diverged and
>>>>>>>>> selinux is implementing a policy that is quite different than
>>>>>>>>> cap_inode_setxattr and cap_inode_removexattr especially when it
>>>>>>>>> comes
>>>>>>>>> to the security.capable xattr.
>>>>>>>> What leads you to believe that this isn't intentional?
>>>>>>>> It's most likely the case that this change occurred as
>>>>>>>> part of the first round module stacking change. What behavior
>>>>>>>> do you see that you're unhappy with?
>>>>>>>>
>>>>>>>>> To keep things working
>>>>>>>> Which "things"? How are they not "working"?
>>>>>>>>
>>>>>>>>> ?and to make the comments in security/security.c
>>>>>>>>> correct when the xattr is securit.capable, call cap_inode_setxattr
>>>>>>>>> or cap_inode_removexattr as appropriate.
>>>>>>>>>
>>>>>>>>> I suspect there is a larger conversation to be had here but this
>>>>>>>>> is enough to keep selinux from implementing a non-sense hard coded
>>>>>>>>> policy that breaks other parts of the kernel.
>>>>>>>> Specifics, please. Since I can't guess what problem you've
>>>>>>>> encountered I can't tell if it's here, in the infrastructure,
>>>>>>>> or in your perception of what constitutes "broken".
>>>>>>>>
>>>>>>>>> Signed-off-by: "Eric W. Biederman" <ebiederm@xmission.com>
>>>>>>>>> ---
>>>>>>>>> ?security/selinux/hooks.c | 6 ++++++
>>>>>>>>> ?1 file changed, 6 insertions(+)
>>>>>>>>>
>>>>>>>>> diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
>>>>>>>>> index f5d304736852..edf4bd292dc7 100644
>>>>>>>>> --- a/security/selinux/hooks.c
>>>>>>>>> +++ b/security/selinux/hooks.c
>>>>>>>>> @@ -3167,6 +3167,9 @@ static int selinux_inode_setxattr(struct
>>>>>>>>> dentry *dentry, const char *name,
>>>>>>>>> ?	u32 newsid, sid = current_sid();
>>>>>>>>> ?	int rc = 0;
>>>>>>>>> ?
>>>>>>>>> +	if (strcmp(name, XATTR_NAME_CAPS) == 0)
>>>>>>>>> +		return cap_inode_setxattr(dentry, name, value,
>>>>>>>>> size, flags);
>>>>>>>>> +
>>>>>>>> No. Don't even think of contemplating considering embedding the cap
>>>>>>>> attribute check in the SELinux code. cap_inode_setxattr() is called
>>>>>>>> in
>>>>>>>> the infrastructure.
>>>>>>> Except that it isn't, not if any other security module is enabled and
>>>>>>> implements those hooks, to prevent imposing CAP_SYS_ADMIN checks when
>>>>>>> setting security.selinux or security.SMACK*.
>>>>>> OK. Yes, this bit of the infrastructure is some of the
>>>>>> worst I've done in a long time. This is a case where we
>>>>>> already need special case stacking infrastructure. It looks
>>>>>> like we'll have to separate setting the cap attribute from
>>>>>> checking the cap state in order to make this work. In any
>>>>>> case, the security_inode_setxattr() code is where the change
>>>>>> belongs. There will likely be fallout changes in the modules,
>>>>>> including the cap module.
>>>>>> ?
>>>>>>
>>>>>>> An alternative approach to fixing this would be to change the cap
>>>>>>> functions to only apply their checks if setting the capability
>>>>>>> attribute and defer any checks on other security.* attributes to either
>>>>>>> the security framework or the other security modules.  Then the
>>>>>>> framework could always call all the modules on the inode_setxattr and
>>>>>>> inode_removexattr hooks as with other hooks.  The security framework
>>>>>>> would then need to ensure that a check is still applied when setting
>>>>>>> security.* attributes if it isn't already handled by one of the enabled
>>>>>>> security modules, as you don't want unprivileged userspace to be able
>>>>>>> to set arbitrary security.foo attributes or to set up security.selinux
>>>>>>> or security.SMACK* attributes if those modules happen to be disabled.
>>>>>> Agreed. This isn't a two line change. Grumble.
>>>>>>
>>>>>> I can guess at what the problem might be, but I hate making
>>>>>> assumptions when I go to fix a problem. I will start looking
>>>>>> at a patch, but it would really help if I could say for sure
>>>>>> what I'm out to accomplish. It may be obvious to the casual
>>>>>> observer, but that description has not been applied to me very
>>>>>> often.
>>>>> Apologies for the delayed reply.
>>>>>
>>>>> I am looking at security_inode_setxattr.
>>>>>
>>>>> For setting attributes in the security.* the generic code in fs/xattr.c
>>>>> applies no permission checks.
>>>>>
>>>>> Each security module that implements an xattr in security.* then imposes
>>>>> it's own policy on it's own attribute.
>>>>>
>>>>> For smack the basic rule is smack_privileged(CAP_MAC_ADMIN).
>>>>> For selinux the basic rule is inode_or_owner_capable(inode).
>>>>> For commoncap the basic rule is capable_wrt_inode_uidgid(inode, CAP_SETFCAP).
>>>>>
>>>>> commoncap also applies a default policity to setting security.* xattrs.
>>>>> ns_capable(dentry->d_sb->s_userns, CAP_SYS_ADMIN).
>>>>>
>>>>> smack reuses that default policy by calling cap_inode_setxattr if it
>>>>> isn't a smack security.* xattr.
>>>>>
>>>>> selinux has what looks like an old copy of the commoncap checks for
>>>>> the security.* in selinux_inode_setotherxattr.  Testing for
>>>>> capable(CAP_SETFCAP) for security.capable and capable(CAP_SYS_ADMIN)
>>>>> for the others.
>>>>>
>>>>> With the added complication that selinux calls
>>>>> selinux_inode_setotherxattr also for the remove_xattr case.  So fixing
>>>>> this in selinux_inode_setotherxattr is not appropriate.
>>>>>
>>>>> I believe selinux also has general policy hooks it applies to all
>>>>> invocations of setxattr.
>>>>>
>>>>> So I think to really fix this we need to separate the cases of is this
>>>>> your security modules attribute from general policy checks added by the
>>>>> security modules.  Perhaps something like this for
>>>>> security_inode_setxattr:
>>>>>
>>>>> Hmm.  Looking at least ima also has the distinction between protecting
>>>>> it's own xattr writes and running generaly security module policy on
>>>>> xattr writes.
>>>>>
>>>>> int security_inode_setxattr(struct dentry *dentry, const char *name,
>>>>> 			    const void *value, size_t size, int flags)
>>>>> {
>>>>> 	int ret = 0;
>>>>>
>>>>> 	if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
>>>>> 		return 0;
>>>>>
>>>>> 	if (strncmp(name, XATTR_SECURITY_PREFIX,
>>>>> 			sizeof(XATTR_SECURITY_PREFIX) - 1) == 0) {
>>>>> 		/* Call the security modules and see if they all return
>>>>>                  * -EOPNOTSUPP if so apply the default permission
>>>>>                  * check of ns_capable(dentry->d_sb->s_user_ns, CAP_SYS_ADMIN)
>>>>>                  * otherwise if one of the security modules supports
>>>>> 		 * this attribute (signaled by returning something other
>>>>> 		 * -EOPNOTSUPP) then set ret to that result.
>>>>>                  *
>>>>>                  * The security modules include at least smack, selinux,
>>>>> 		 * commoncap, ima, and evm.
>>>>>                  */
>>>>>                  ret = magic_inode_protect_setxattr(dentry, name, value, size);
>>>>>         }
>>>>> 	if (ret)
>>>>> 		return ret;
>>>>>
>>>>>         /* Run all of the security module policy against this setxattr call */
>>>>>         return magic_inode_policy_setxattr(dentry, name, value, size);
>>>>> }
>>>>>
>>>>> Eric
>>>> Yup, that's pretty much what I'm thinking. It's unfortunate
>>>> that the magic_ API isn't fully implemented. There's going to
>>>> be a good deal of code surgery instead. Is there an observed
>>>> problem today? This is going to have to get addressed for stacking,
>>>> so if there isn't a behavioral issue that impacts something real
>>>> I would like to defer spending significant time on it. Do you have
>>>> a case where this is not working correctly?
>>> Merged as of 4.14-rc1 is the support for user namespace root to set
>>> sercurity.capable.  This fails when selinux is loaded.
>> OK. Is the failure unique to SELinux, or does it fail with
>> Smack as well?
> I don't have a smack configuration handy, but reading through
> the code smack setxattr the permission checks for all xattrs
> that are not smack xattrs to cap_inode_setxattr.

It's not hard to configure Smack. But, if you have a test case
I can run it for you.

> So smack and commoncap combined will not fail.
>
> smack and selinux will result in people who should be able to set
> selinux xattrs not being able to.  That however is less of an immediate
> problem.

That's not currently a problem as you can't configure
them both to be enabled.

>
>>> removexattr has the same problem and the code is a little less
>>> convoluted in that case.
>> Right. Because removexattr is a simpler situation.
>>
>>> Not being able to set the capability when you should be able to is
>>> very noticable.  Like running into a brick wall noticable.
>> Ah, now you've identified the problem. Yes, I would agree that you've
>> uncovered an undesirable behavior.
> Apologies for not being clearer earlier, but I was still in shock from
> running into a brick wall.

You clearly don't work in security is running into a brick
wall is a shocking experience :)

I'm kind of surprised that the capability changes got sent
upstream without SELinux ever being tested. That's a very common
configuration in the Android enabled world.

>
>>> Which is where the minimal patch for selinux comes in.  I think it
>>> solves the exact case in question, even if it isn't the perfect long
>>> term solution.
>> If the problem is unique to SELinux I can see your logic. If it
>> isn't, that is, if it's also a problem with any other security
>> module, there either needs to be a fix for that/those module/s
>> as well or a "real" fix.
>>
>> I'm not opposed to the SELinux short term fix if you can say
>> that that's the only module with the problem.
> So far there are exactly two implementation of
> LSM_HOOK_INIT(inode_setxattr, ...)
>
> So as a practical case it looks like combination with selinux is the
> only case where the problem will be observed right now.  And it makes
> the code at least somewhat match the comments in
> security_inode_setxattr.
>
> Eric
>
>
>


.
--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in
the body of a message to majordomo at vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

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

* [RFC][PATCH] security: Make the selinux setxattr and removexattr hooks behave
  2017-10-01 18:52                 ` Casey Schaufler
@ 2017-10-01 19:54                   ` Serge E. Hallyn
  2017-10-01 22:11                   ` Eric W. Biederman
  1 sibling, 0 replies; 39+ messages in thread
From: Serge E. Hallyn @ 2017-10-01 19:54 UTC (permalink / raw)
  To: linux-security-module

On Sun, Oct 01, 2017 at 11:52:29AM -0700, Casey Schaufler wrote:
> I'm kind of surprised that the capability changes got sent
> upstream without SELinux ever being tested. That's a very common
> configuration in the Android enabled world.

hm, I'm sorry about that.  I'd intended years ago to get ubuntu with selinux
running again, but never found enough time.  Perhaps i should set up an Oracle
linux VM for such testing.  Would be useful in several ways..

-serge
--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in
the body of a message to majordomo at vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

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

* [RFC][PATCH] security: Make the selinux setxattr and removexattr hooks behave
  2017-10-01 18:52                 ` Casey Schaufler
  2017-10-01 19:54                   ` Serge E. Hallyn
@ 2017-10-01 22:11                   ` Eric W. Biederman
  1 sibling, 0 replies; 39+ messages in thread
From: Eric W. Biederman @ 2017-10-01 22:11 UTC (permalink / raw)
  To: linux-security-module

Casey Schaufler <casey@schaufler-ca.com> writes:

> On 9/30/2017 6:02 PM, Eric W. Biederman wrote:
>> I don't have a smack configuration handy, but reading through
>> the code smack setxattr the permission checks for all xattrs
>> that are not smack xattrs to cap_inode_setxattr.
>
> It's not hard to configure Smack. But, if you have a test case
> I can run it for you.

All I did was take /bin/ping from a RHEL or equally a fedora code base
where it is setcap, and copied it with rsync as root in a user namespace
and looked at the xattr.

>From memory:
$ cd
$ unshare -Ur
# rsync -Xp /bin/ping ping

>> So smack and commoncap combined will not fail.
>>
>> smack and selinux will result in people who should be able to set
>> selinux xattrs not being able to.  That however is less of an immediate
>> problem.
>
> That's not currently a problem as you can't configure
> them both to be enabled.

Like I said not immediate.

> You clearly don't work in security is running into a brick
> wall is a shocking experience :)

The shock was that the security code was so b0rked.

Eric
--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in
the body of a message to majordomo at vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

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

* Re: [RFC][PATCH] security: Make the selinux setxattr and removexattr hooks behave
       [not found]   ` <1506688601.5571.1.camel-+05T5uksL2qpZYMLLGbcSA@public.gmane.org>
@ 2017-10-02  3:26     ` Eric W. Biederman
  2017-10-02 14:38     ` [PATCH] selinux: Perform both commoncap and selinux xattr checks Eric W. Biederman
  1 sibling, 0 replies; 39+ messages in thread
From: Eric W. Biederman @ 2017-10-02  3:26 UTC (permalink / raw)
  To: Stephen Smalley
  Cc: Paul Moore, Linux Containers, Chris Wright,
	linux-security-module-u79uwXL29TY76Z2rM5mHXA, James Morris,
	Casey Schaufler, Eric Paris

Stephen Smalley <sds@tycho.nsa.gov> writes:

> On Thu, 2017-09-28 at 17:34 -0500, Eric W. Biederman wrote:
>> It looks like once upon a time a long time ago selinux copied code
>> from cap_inode_removexattr and cap_inode_setxattr into
>> selinux_inode_setotherxattr.  However the code has now diverged and
>> selinux is implementing a policy that is quite different than
>> cap_inode_setxattr and cap_inode_removexattr especially when it comes
>> to the security.capable xattr.
>> 
>> To keep things working and to make the comments in
>> security/security.c
>> correct when the xattr is securit.capable, call cap_inode_setxattr
>> or cap_inode_removexattr as appropriate.
>> 
>> I suspect there is a larger conversation to be had here but this
>> is enough to keep selinux from implementing a non-sense hard coded
>> policy that breaks other parts of the kernel.
>
> Originally SELinux called the cap functions directly since there was no
> stacking support in the infrastructure and one had to manually stack a
> secondary module internally.  inode_setxattr and inode_removexattr
> however were special cases because the cap functions would check
> CAP_SYS_ADMIN for any non-capability attributes in the security.*
> namespace, and we don't want to impose that requirement on setting
> security.selinux.  Thus, we inlined the capabilities logic into the
> selinux hook functions and adapted it appropriately.  When the stacking
> support was introduced, it had to also special case these hooks so that
> only the primary module's hook is used for the same reason; otherwise,
> the kernel would end up applying a CAP_SYS_ADMIN check on setting
> security.selinux.  Your change below is almost but not quite right
> since it only calls the cap functions when setting the capability
> attribute; the residual problem is that it will then skip the SELinux
> FILE__SETATTR (file setattr) permission check when setting those
> attributes, which we want to retain.  So you need to only return early
> if cap_inode_setxattr()/removexattr() return an error; otherwise, you
> need to proceed to the SELinux check, and you can then delete the
> duplicated logic from selinux_inode_setotherxattr().  At which point it
> just becomes a call to dentry_has_perm() and you can just inline that
> into selinux_inode_setxattr() and selinux_inode_removexattr().

I will look at that.

Thank you,
Eric
_______________________________________________
Containers mailing list
Containers@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/containers

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

* [RFC][PATCH] security: Make the selinux setxattr and removexattr hooks behave
  2017-09-29 12:36 ` Stephen Smalley
@ 2017-10-02  3:26   ` Eric W. Biederman
  2017-10-02 14:38   ` [PATCH] selinux: Perform both commoncap and selinux xattr checks Eric W. Biederman
       [not found]   ` <1506688601.5571.1.camel-+05T5uksL2qpZYMLLGbcSA@public.gmane.org>
  2 siblings, 0 replies; 39+ messages in thread
From: Eric W. Biederman @ 2017-10-02  3:26 UTC (permalink / raw)
  To: linux-security-module

Stephen Smalley <sds@tycho.nsa.gov> writes:

> On Thu, 2017-09-28 at 17:34 -0500, Eric W. Biederman wrote:
>> It looks like once upon a time a long time ago selinux copied code
>> from cap_inode_removexattr and cap_inode_setxattr into
>> selinux_inode_setotherxattr.??However the code has now diverged and
>> selinux is implementing a policy that is quite different than
>> cap_inode_setxattr and cap_inode_removexattr especially when it comes
>> to the security.capable xattr.
>> 
>> To keep things working and to make the comments in
>> security/security.c
>> correct when the xattr is securit.capable, call cap_inode_setxattr
>> or cap_inode_removexattr as appropriate.
>> 
>> I suspect there is a larger conversation to be had here but this
>> is enough to keep selinux from implementing a non-sense hard coded
>> policy that breaks other parts of the kernel.
>
> Originally SELinux called the cap functions directly since there was no
> stacking support in the infrastructure and one had to manually stack a
> secondary module internally.  inode_setxattr and inode_removexattr
> however were special cases because the cap functions would check
> CAP_SYS_ADMIN for any non-capability attributes in the security.*
> namespace, and we don't want to impose that requirement on setting
> security.selinux.  Thus, we inlined the capabilities logic into the
> selinux hook functions and adapted it appropriately.  When the stacking
> support was introduced, it had to also special case these hooks so that
> only the primary module's hook is used for the same reason; otherwise,
> the kernel would end up applying a CAP_SYS_ADMIN check on setting
> security.selinux.  Your change below is almost but not quite right
> since it only calls the cap functions when setting the capability
> attribute; the residual problem is that it will then skip the SELinux
> FILE__SETATTR (file setattr) permission check when setting those
> attributes, which we want to retain.  So you need to only return early
> if cap_inode_setxattr()/removexattr() return an error; otherwise, you
> need to proceed to the SELinux check, and you can then delete the
> duplicated logic from selinux_inode_setotherxattr().  At which point it
> just becomes a call to dentry_has_perm() and you can just inline that
> into selinux_inode_setxattr() and selinux_inode_removexattr().

I will look at that.

Thank you,
Eric
--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in
the body of a message to majordomo at vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

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

* [PATCH] selinux: Perform both commoncap and selinux xattr checks
       [not found]   ` <1506688601.5571.1.camel-+05T5uksL2qpZYMLLGbcSA@public.gmane.org>
  2017-10-02  3:26     ` [RFC][PATCH] security: Make the selinux setxattr and removexattr hooks behave Eric W. Biederman
@ 2017-10-02 14:38     ` Eric W. Biederman
  1 sibling, 0 replies; 39+ messages in thread
From: Eric W. Biederman @ 2017-10-02 14:38 UTC (permalink / raw)
  To: Stephen Smalley
  Cc: Paul Moore, Linux Containers, Chris Wright,
	linux-security-module-u79uwXL29TY76Z2rM5mHXA, James Morris,
	Casey Schaufler, Eric Paris


When selinux is loaded the relax permission checks for writing
security.capable are not honored.  Which keeps file capabilities
from being used in user namespaces.

Stephen Smalley <sds-+05T5uksL2qpZYMLLGbcSA@public.gmane.org> writes:
> Originally SELinux called the cap functions directly since there was no
> stacking support in the infrastructure and one had to manually stack a
> secondary module internally.  inode_setxattr and inode_removexattr
> however were special cases because the cap functions would check
> CAP_SYS_ADMIN for any non-capability attributes in the security.*
> namespace, and we don't want to impose that requirement on setting
> security.selinux.  Thus, we inlined the capabilities logic into the
> selinux hook functions and adapted it appropriately.

Now that the permission checks in commoncap have evolved this
inlining of their contents has become a problem.  So restructure
selinux_inode_removexattr, and selinux_inode_setxattr to call
both the corresponding cap_inode_ function and dentry_has_perm
when the attribute is not a selinux security xattr.   This ensures
the policies of both commoncap and selinux are enforced.

This results in smack and selinux having the same basic structure
for setxattr and removexattr.  Performing their own special permission
checks when it is their modules xattr being written to, and deferring
to commoncap when that is not the case.  Then finally performing their
generic module policy on all xattr writes.

This structure is fine when you only consider stacking with the
commoncap lsm, but it becomes a problem if two lsms that don't want
the commoncap security checks on their own attributes need to be
stack.  This means there will need to be updates in the future as lsm
stacking is improved, but at least now the structure between smack and
selinux is common making the code easier to refactor.

This change also has the effect that selinux_linux_setotherxattr becomes
unnecessary so it is removed.

Fixes: 8db6c34f1dbc ("Introduce v3 namespaced file capabilities")
Fixes: 7bbf0e052b76 ("[PATCH] selinux merge")
Historical Tree: https://git.kernel.org/pub/scm/linux/kernel/git/tglx/history.git
Signed-off-by: "Eric W. Biederman" <ebiederm-aS9lmoZGLiVWk0Htik3J/w@public.gmane.org>
---

While this fixes some things this isn't a regression so it should be
able to wait until the next merge window to be merged.   Would you like
to take this through the selinux tree?  Or shall I take it through mine?

security/selinux/hooks.c | 43 ++++++++++++++++++-------------------------
 1 file changed, 18 insertions(+), 25 deletions(-)

diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
index f5d304736852..c78dbec627f6 100644
--- a/security/selinux/hooks.c
+++ b/security/selinux/hooks.c
@@ -3124,27 +3124,6 @@ static int selinux_inode_getattr(const struct path *path)
 	return path_has_perm(current_cred(), path, FILE__GETATTR);
 }
 
-static int selinux_inode_setotherxattr(struct dentry *dentry, const char *name)
-{
-	const struct cred *cred = current_cred();
-
-	if (!strncmp(name, XATTR_SECURITY_PREFIX,
-		     sizeof XATTR_SECURITY_PREFIX - 1)) {
-		if (!strcmp(name, XATTR_NAME_CAPS)) {
-			if (!capable(CAP_SETFCAP))
-				return -EPERM;
-		} else if (!capable(CAP_SYS_ADMIN)) {
-			/* A different attribute in the security namespace.
-			   Restrict to administrator. */
-			return -EPERM;
-		}
-	}
-
-	/* Not an attribute we recognize, so just check the
-	   ordinary setattr permission. */
-	return dentry_has_perm(cred, dentry, FILE__SETATTR);
-}
-
 static bool has_cap_mac_admin(bool audit)
 {
 	const struct cred *cred = current_cred();
@@ -3167,8 +3146,15 @@ static int selinux_inode_setxattr(struct dentry *dentry, const char *name,
 	u32 newsid, sid = current_sid();
 	int rc = 0;
 
-	if (strcmp(name, XATTR_NAME_SELINUX))
-		return selinux_inode_setotherxattr(dentry, name);
+	if (strcmp(name, XATTR_NAME_SELINUX)) {
+		rc = cap_inode_setxattr(dentry, name, value, size, flags);
+		if (rc)
+			return rc;
+
+		/* Not an attribute we recognize, so just check the
+		   ordinary setattr permission. */
+		return dentry_has_perm(current_cred(), dentry, FILE__SETATTR);
+	}
 
 	sbsec = inode->i_sb->s_security;
 	if (!(sbsec->flags & SBLABEL_MNT))
@@ -3282,8 +3268,15 @@ static int selinux_inode_listxattr(struct dentry *dentry)
 
 static int selinux_inode_removexattr(struct dentry *dentry, const char *name)
 {
-	if (strcmp(name, XATTR_NAME_SELINUX))
-		return selinux_inode_setotherxattr(dentry, name);
+	if (strcmp(name, XATTR_NAME_SELINUX)) {
+		int rc = cap_inode_removexattr(dentry, name);
+		if (rc)
+			return rc;
+
+		/* Not an attribute we recognize, so just check the
+		   ordinary setattr permission. */
+		return dentry_has_perm(current_cred(), dentry, FILE__SETATTR);
+	}
 
 	/* No one is allowed to remove a SELinux security label.
 	   You can change the label, but all data must be labeled. */
-- 
2.14.1

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

* [PATCH] selinux: Perform both commoncap and selinux xattr checks
  2017-09-29 12:36 ` Stephen Smalley
  2017-10-02  3:26   ` Eric W. Biederman
@ 2017-10-02 14:38   ` Eric W. Biederman
       [not found]     ` <873771ipib.fsf_-_-aS9lmoZGLiVWk0Htik3J/w@public.gmane.org>
                       ` (2 more replies)
       [not found]   ` <1506688601.5571.1.camel-+05T5uksL2qpZYMLLGbcSA@public.gmane.org>
  2 siblings, 3 replies; 39+ messages in thread
From: Eric W. Biederman @ 2017-10-02 14:38 UTC (permalink / raw)
  To: linux-security-module


When selinux is loaded the relax permission checks for writing
security.capable are not honored.  Which keeps file capabilities
from being used in user namespaces.

Stephen Smalley <sds@tycho.nsa.gov> writes:
> Originally SELinux called the cap functions directly since there was no
> stacking support in the infrastructure and one had to manually stack a
> secondary module internally.  inode_setxattr and inode_removexattr
> however were special cases because the cap functions would check
> CAP_SYS_ADMIN for any non-capability attributes in the security.*
> namespace, and we don't want to impose that requirement on setting
> security.selinux.  Thus, we inlined the capabilities logic into the
> selinux hook functions and adapted it appropriately.

Now that the permission checks in commoncap have evolved this
inlining of their contents has become a problem.  So restructure
selinux_inode_removexattr, and selinux_inode_setxattr to call
both the corresponding cap_inode_ function and dentry_has_perm
when the attribute is not a selinux security xattr.   This ensures
the policies of both commoncap and selinux are enforced.

This results in smack and selinux having the same basic structure
for setxattr and removexattr.  Performing their own special permission
checks when it is their modules xattr being written to, and deferring
to commoncap when that is not the case.  Then finally performing their
generic module policy on all xattr writes.

This structure is fine when you only consider stacking with the
commoncap lsm, but it becomes a problem if two lsms that don't want
the commoncap security checks on their own attributes need to be
stack.  This means there will need to be updates in the future as lsm
stacking is improved, but at least now the structure between smack and
selinux is common making the code easier to refactor.

This change also has the effect that selinux_linux_setotherxattr becomes
unnecessary so it is removed.

Fixes: 8db6c34f1dbc ("Introduce v3 namespaced file capabilities")
Fixes: 7bbf0e052b76 ("[PATCH] selinux merge")
Historical Tree: https://git.kernel.org/pub/scm/linux/kernel/git/tglx/history.git
Signed-off-by: "Eric W. Biederman" <ebiederm@xmission.com>
---

While this fixes some things this isn't a regression so it should be
able to wait until the next merge window to be merged.   Would you like
to take this through the selinux tree?  Or shall I take it through mine?

security/selinux/hooks.c | 43 ++++++++++++++++++-------------------------
 1 file changed, 18 insertions(+), 25 deletions(-)

diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
index f5d304736852..c78dbec627f6 100644
--- a/security/selinux/hooks.c
+++ b/security/selinux/hooks.c
@@ -3124,27 +3124,6 @@ static int selinux_inode_getattr(const struct path *path)
 	return path_has_perm(current_cred(), path, FILE__GETATTR);
 }
 
-static int selinux_inode_setotherxattr(struct dentry *dentry, const char *name)
-{
-	const struct cred *cred = current_cred();
-
-	if (!strncmp(name, XATTR_SECURITY_PREFIX,
-		     sizeof XATTR_SECURITY_PREFIX - 1)) {
-		if (!strcmp(name, XATTR_NAME_CAPS)) {
-			if (!capable(CAP_SETFCAP))
-				return -EPERM;
-		} else if (!capable(CAP_SYS_ADMIN)) {
-			/* A different attribute in the security namespace.
-			   Restrict to administrator. */
-			return -EPERM;
-		}
-	}
-
-	/* Not an attribute we recognize, so just check the
-	   ordinary setattr permission. */
-	return dentry_has_perm(cred, dentry, FILE__SETATTR);
-}
-
 static bool has_cap_mac_admin(bool audit)
 {
 	const struct cred *cred = current_cred();
@@ -3167,8 +3146,15 @@ static int selinux_inode_setxattr(struct dentry *dentry, const char *name,
 	u32 newsid, sid = current_sid();
 	int rc = 0;
 
-	if (strcmp(name, XATTR_NAME_SELINUX))
-		return selinux_inode_setotherxattr(dentry, name);
+	if (strcmp(name, XATTR_NAME_SELINUX)) {
+		rc = cap_inode_setxattr(dentry, name, value, size, flags);
+		if (rc)
+			return rc;
+
+		/* Not an attribute we recognize, so just check the
+		   ordinary setattr permission. */
+		return dentry_has_perm(current_cred(), dentry, FILE__SETATTR);
+	}
 
 	sbsec = inode->i_sb->s_security;
 	if (!(sbsec->flags & SBLABEL_MNT))
@@ -3282,8 +3268,15 @@ static int selinux_inode_listxattr(struct dentry *dentry)
 
 static int selinux_inode_removexattr(struct dentry *dentry, const char *name)
 {
-	if (strcmp(name, XATTR_NAME_SELINUX))
-		return selinux_inode_setotherxattr(dentry, name);
+	if (strcmp(name, XATTR_NAME_SELINUX)) {
+		int rc = cap_inode_removexattr(dentry, name);
+		if (rc)
+			return rc;
+
+		/* Not an attribute we recognize, so just check the
+		   ordinary setattr permission. */
+		return dentry_has_perm(current_cred(), dentry, FILE__SETATTR);
+	}
 
 	/* No one is allowed to remove a SELinux security label.
 	   You can change the label, but all data must be labeled. */
-- 
2.14.1

--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in
the body of a message to majordomo at vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

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

* Re: [PATCH] selinux: Perform both commoncap and selinux xattr checks
       [not found]     ` <873771ipib.fsf_-_-aS9lmoZGLiVWk0Htik3J/w@public.gmane.org>
@ 2017-10-02 15:52       ` Serge E. Hallyn
  2017-10-03 16:24       ` Stephen Smalley
  2017-10-03 21:08         ` Paul Moore
  2 siblings, 0 replies; 39+ messages in thread
From: Serge E. Hallyn @ 2017-10-02 15:52 UTC (permalink / raw)
  To: Eric W. Biederman
  Cc: Paul Moore, Linux Containers, Chris Wright,
	linux-security-module-u79uwXL29TY76Z2rM5mHXA, James Morris,
	Casey Schaufler, Eric Paris, Stephen Smalley

Quoting Eric W. Biederman (ebiederm-aS9lmoZGLiVWk0Htik3J/w@public.gmane.org):
> -UID: 28009                                                  
> Status: O
> 
> 
> When selinux is loaded the relax permission checks for writing
> security.capable are not honored.  Which keeps file capabilities
> from being used in user namespaces.
> 
> Stephen Smalley <sds-+05T5uksL2qpZYMLLGbcSA@public.gmane.org> writes:
> > Originally SELinux called the cap functions directly since there was no
> > stacking support in the infrastructure and one had to manually stack a
> > secondary module internally.  inode_setxattr and inode_removexattr
> > however were special cases because the cap functions would check
> > CAP_SYS_ADMIN for any non-capability attributes in the security.*
> > namespace, and we don't want to impose that requirement on setting
> > security.selinux.  Thus, we inlined the capabilities logic into the
> > selinux hook functions and adapted it appropriately.
> 
> Now that the permission checks in commoncap have evolved this
> inlining of their contents has become a problem.  So restructure
> selinux_inode_removexattr, and selinux_inode_setxattr to call
> both the corresponding cap_inode_ function and dentry_has_perm
> when the attribute is not a selinux security xattr.   This ensures
> the policies of both commoncap and selinux are enforced.
> 
> This results in smack and selinux having the same basic structure
> for setxattr and removexattr.  Performing their own special permission
> checks when it is their modules xattr being written to, and deferring
> to commoncap when that is not the case.  Then finally performing their
> generic module policy on all xattr writes.
> 
> This structure is fine when you only consider stacking with the
> commoncap lsm, but it becomes a problem if two lsms that don't want
> the commoncap security checks on their own attributes need to be
> stack.  This means there will need to be updates in the future as lsm
> stacking is improved, but at least now the structure between smack and
> selinux is common making the code easier to refactor.
> 
> This change also has the effect that selinux_linux_setotherxattr becomes
> unnecessary so it is removed.
> 
> Fixes: 8db6c34f1dbc ("Introduce v3 namespaced file capabilities")
> Fixes: 7bbf0e052b76 ("[PATCH] selinux merge")
> Historical Tree: https://git.kernel.org/pub/scm/linux/kernel/git/tglx/history.git
> Signed-off-by: "Eric W. Biederman" <ebiederm-aS9lmoZGLiVWk0Htik3J/w@public.gmane.org>

This is hairy, but it looks right:

Reviewed-by: Serge Hallyn <serge-A9i7LUbDfNHQT0dZR+AlfA@public.gmane.org>

thanks,
-serge

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

* [PATCH] selinux: Perform both commoncap and selinux xattr checks
  2017-10-02 14:38   ` [PATCH] selinux: Perform both commoncap and selinux xattr checks Eric W. Biederman
       [not found]     ` <873771ipib.fsf_-_-aS9lmoZGLiVWk0Htik3J/w@public.gmane.org>
@ 2017-10-02 15:52     ` Serge E. Hallyn
  2017-10-03 16:24     ` Stephen Smalley
  2 siblings, 0 replies; 39+ messages in thread
From: Serge E. Hallyn @ 2017-10-02 15:52 UTC (permalink / raw)
  To: linux-security-module

Quoting Eric W. Biederman (ebiederm at xmission.com):
> -UID: 28009                                                  
> Status: O
> 
> 
> When selinux is loaded the relax permission checks for writing
> security.capable are not honored.  Which keeps file capabilities
> from being used in user namespaces.
> 
> Stephen Smalley <sds@tycho.nsa.gov> writes:
> > Originally SELinux called the cap functions directly since there was no
> > stacking support in the infrastructure and one had to manually stack a
> > secondary module internally.  inode_setxattr and inode_removexattr
> > however were special cases because the cap functions would check
> > CAP_SYS_ADMIN for any non-capability attributes in the security.*
> > namespace, and we don't want to impose that requirement on setting
> > security.selinux.  Thus, we inlined the capabilities logic into the
> > selinux hook functions and adapted it appropriately.
> 
> Now that the permission checks in commoncap have evolved this
> inlining of their contents has become a problem.  So restructure
> selinux_inode_removexattr, and selinux_inode_setxattr to call
> both the corresponding cap_inode_ function and dentry_has_perm
> when the attribute is not a selinux security xattr.   This ensures
> the policies of both commoncap and selinux are enforced.
> 
> This results in smack and selinux having the same basic structure
> for setxattr and removexattr.  Performing their own special permission
> checks when it is their modules xattr being written to, and deferring
> to commoncap when that is not the case.  Then finally performing their
> generic module policy on all xattr writes.
> 
> This structure is fine when you only consider stacking with the
> commoncap lsm, but it becomes a problem if two lsms that don't want
> the commoncap security checks on their own attributes need to be
> stack.  This means there will need to be updates in the future as lsm
> stacking is improved, but at least now the structure between smack and
> selinux is common making the code easier to refactor.
> 
> This change also has the effect that selinux_linux_setotherxattr becomes
> unnecessary so it is removed.
> 
> Fixes: 8db6c34f1dbc ("Introduce v3 namespaced file capabilities")
> Fixes: 7bbf0e052b76 ("[PATCH] selinux merge")
> Historical Tree: https://git.kernel.org/pub/scm/linux/kernel/git/tglx/history.git
> Signed-off-by: "Eric W. Biederman" <ebiederm@xmission.com>

This is hairy, but it looks right:

Reviewed-by: Serge Hallyn <serge@hallyn.com>

thanks,
-serge
--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in
the body of a message to majordomo at vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

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

* Re: [PATCH] selinux: Perform both commoncap and selinux xattr checks
       [not found]     ` <873771ipib.fsf_-_-aS9lmoZGLiVWk0Htik3J/w@public.gmane.org>
  2017-10-02 15:52       ` Serge E. Hallyn
@ 2017-10-03 16:24       ` Stephen Smalley
  2017-10-03 21:08         ` Paul Moore
  2 siblings, 0 replies; 39+ messages in thread
From: Stephen Smalley @ 2017-10-03 16:24 UTC (permalink / raw)
  To: Eric W. Biederman
  Cc: Paul Moore, Linux Containers, Chris Wright,
	linux-security-module-u79uwXL29TY76Z2rM5mHXA, James Morris,
	Casey Schaufler, Eric Paris

On Mon, 2017-10-02 at 09:38 -0500, Eric W. Biederman wrote:
> When selinux is loaded the relax permission checks for writing
> security.capable are not honored.  Which keeps file capabilities
> from being used in user namespaces.
> 
> Stephen Smalley <sds@tycho.nsa.gov> writes:
> > Originally SELinux called the cap functions directly since there
> > was no
> > stacking support in the infrastructure and one had to manually
> > stack a
> > secondary module internally.  inode_setxattr and inode_removexattr
> > however were special cases because the cap functions would check
> > CAP_SYS_ADMIN for any non-capability attributes in the security.*
> > namespace, and we don't want to impose that requirement on setting
> > security.selinux.  Thus, we inlined the capabilities logic into the
> > selinux hook functions and adapted it appropriately.
> 
> Now that the permission checks in commoncap have evolved this
> inlining of their contents has become a problem.  So restructure
> selinux_inode_removexattr, and selinux_inode_setxattr to call
> both the corresponding cap_inode_ function and dentry_has_perm
> when the attribute is not a selinux security xattr.   This ensures
> the policies of both commoncap and selinux are enforced.
> 
> This results in smack and selinux having the same basic structure
> for setxattr and removexattr.  Performing their own special
> permission
> checks when it is their modules xattr being written to, and deferring
> to commoncap when that is not the case.  Then finally performing
> their
> generic module policy on all xattr writes.
> 
> This structure is fine when you only consider stacking with the
> commoncap lsm, but it becomes a problem if two lsms that don't want
> the commoncap security checks on their own attributes need to be
> stack.  This means there will need to be updates in the future as lsm
> stacking is improved, but at least now the structure between smack
> and
> selinux is common making the code easier to refactor.
> 
> This change also has the effect that selinux_linux_setotherxattr
> becomes
> unnecessary so it is removed.
> 
> Fixes: 8db6c34f1dbc ("Introduce v3 namespaced file capabilities")
> Fixes: 7bbf0e052b76 ("[PATCH] selinux merge")
> Historical Tree: https://git.kernel.org/pub/scm/linux/kernel/git/tglx
> /history.git
> Signed-off-by: "Eric W. Biederman" <ebiederm@xmission.com>

Acked-by: Stephen Smalley <sds@tycho.nsa.gov>

> ---
> 
> While this fixes some things this isn't a regression so it should be
> able to wait until the next merge window to be merged.   Would you
> like
> to take this through the selinux tree?  Or shall I take it through
> mine?

Deferring to Paul Moore on this, since he maintains the selinux tree.

> 
> security/selinux/hooks.c | 43 ++++++++++++++++++---------------------
> ----
>  1 file changed, 18 insertions(+), 25 deletions(-)
> 
> diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
> index f5d304736852..c78dbec627f6 100644
> --- a/security/selinux/hooks.c
> +++ b/security/selinux/hooks.c
> @@ -3124,27 +3124,6 @@ static int selinux_inode_getattr(const struct
> path *path)
>  	return path_has_perm(current_cred(), path, FILE__GETATTR);
>  }
>  
> -static int selinux_inode_setotherxattr(struct dentry *dentry, const
> char *name)
> -{
> -	const struct cred *cred = current_cred();
> -
> -	if (!strncmp(name, XATTR_SECURITY_PREFIX,
> -		     sizeof XATTR_SECURITY_PREFIX - 1)) {
> -		if (!strcmp(name, XATTR_NAME_CAPS)) {
> -			if (!capable(CAP_SETFCAP))
> -				return -EPERM;
> -		} else if (!capable(CAP_SYS_ADMIN)) {
> -			/* A different attribute in the security
> namespace.
> -			   Restrict to administrator. */
> -			return -EPERM;
> -		}
> -	}
> -
> -	/* Not an attribute we recognize, so just check the
> -	   ordinary setattr permission. */
> -	return dentry_has_perm(cred, dentry, FILE__SETATTR);
> -}
> -
>  static bool has_cap_mac_admin(bool audit)
>  {
>  	const struct cred *cred = current_cred();
> @@ -3167,8 +3146,15 @@ static int selinux_inode_setxattr(struct
> dentry *dentry, const char *name,
>  	u32 newsid, sid = current_sid();
>  	int rc = 0;
>  
> -	if (strcmp(name, XATTR_NAME_SELINUX))
> -		return selinux_inode_setotherxattr(dentry, name);
> +	if (strcmp(name, XATTR_NAME_SELINUX)) {
> +		rc = cap_inode_setxattr(dentry, name, value, size,
> flags);
> +		if (rc)
> +			return rc;
> +
> +		/* Not an attribute we recognize, so just check the
> +		   ordinary setattr permission. */
> +		return dentry_has_perm(current_cred(), dentry,
> FILE__SETATTR);
> +	}
>  
>  	sbsec = inode->i_sb->s_security;
>  	if (!(sbsec->flags & SBLABEL_MNT))
> @@ -3282,8 +3268,15 @@ static int selinux_inode_listxattr(struct
> dentry *dentry)
>  
>  static int selinux_inode_removexattr(struct dentry *dentry, const
> char *name)
>  {
> -	if (strcmp(name, XATTR_NAME_SELINUX))
> -		return selinux_inode_setotherxattr(dentry, name);
> +	if (strcmp(name, XATTR_NAME_SELINUX)) {
> +		int rc = cap_inode_removexattr(dentry, name);
> +		if (rc)
> +			return rc;
> +
> +		/* Not an attribute we recognize, so just check the
> +		   ordinary setattr permission. */
> +		return dentry_has_perm(current_cred(), dentry,
> FILE__SETATTR);
> +	}
>  
>  	/* No one is allowed to remove a SELinux security label.
>  	   You can change the label, but all data must be labeled.
> */
_______________________________________________
Containers mailing list
Containers@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/containers

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

* [PATCH] selinux: Perform both commoncap and selinux xattr checks
  2017-10-02 14:38   ` [PATCH] selinux: Perform both commoncap and selinux xattr checks Eric W. Biederman
       [not found]     ` <873771ipib.fsf_-_-aS9lmoZGLiVWk0Htik3J/w@public.gmane.org>
  2017-10-02 15:52     ` Serge E. Hallyn
@ 2017-10-03 16:24     ` Stephen Smalley
  2 siblings, 0 replies; 39+ messages in thread
From: Stephen Smalley @ 2017-10-03 16:24 UTC (permalink / raw)
  To: linux-security-module

On Mon, 2017-10-02 at 09:38 -0500, Eric W. Biederman wrote:
> When selinux is loaded the relax permission checks for writing
> security.capable are not honored.??Which keeps file capabilities
> from being used in user namespaces.
> 
> Stephen Smalley <sds@tycho.nsa.gov> writes:
> > Originally SELinux called the cap functions directly since there
> > was no
> > stacking support in the infrastructure and one had to manually
> > stack a
> > secondary module internally.??inode_setxattr and inode_removexattr
> > however were special cases because the cap functions would check
> > CAP_SYS_ADMIN for any non-capability attributes in the security.*
> > namespace, and we don't want to impose that requirement on setting
> > security.selinux.??Thus, we inlined the capabilities logic into the
> > selinux hook functions and adapted it appropriately.
> 
> Now that the permission checks in commoncap have evolved this
> inlining of their contents has become a problem.??So restructure
> selinux_inode_removexattr, and selinux_inode_setxattr to call
> both the corresponding cap_inode_ function and dentry_has_perm
> when the attribute is not a selinux security xattr.???This ensures
> the policies of both commoncap and selinux are enforced.
> 
> This results in smack and selinux having the same basic structure
> for setxattr and removexattr.??Performing their own special
> permission
> checks when it is their modules xattr being written to, and deferring
> to commoncap when that is not the case.??Then finally performing
> their
> generic module policy on all xattr writes.
> 
> This structure is fine when you only consider stacking with the
> commoncap lsm, but it becomes a problem if two lsms that don't want
> the commoncap security checks on their own attributes need to be
> stack.??This means there will need to be updates in the future as lsm
> stacking is improved, but at least now the structure between smack
> and
> selinux is common making the code easier to refactor.
> 
> This change also has the effect that selinux_linux_setotherxattr
> becomes
> unnecessary so it is removed.
> 
> Fixes: 8db6c34f1dbc ("Introduce v3 namespaced file capabilities")
> Fixes: 7bbf0e052b76 ("[PATCH] selinux merge")
> Historical Tree: https://git.kernel.org/pub/scm/linux/kernel/git/tglx
> /history.git
> Signed-off-by: "Eric W. Biederman" <ebiederm@xmission.com>

Acked-by: Stephen Smalley <sds@tycho.nsa.gov>

> ---
> 
> While this fixes some things this isn't a regression so it should be
> able to wait until the next merge window to be merged.???Would you
> like
> to take this through the selinux tree???Or shall I take it through
> mine?

Deferring to Paul Moore on this, since he maintains the selinux tree.

> 
> security/selinux/hooks.c | 43 ++++++++++++++++++---------------------
> ----
> ?1 file changed, 18 insertions(+), 25 deletions(-)
> 
> diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
> index f5d304736852..c78dbec627f6 100644
> --- a/security/selinux/hooks.c
> +++ b/security/selinux/hooks.c
> @@ -3124,27 +3124,6 @@ static int selinux_inode_getattr(const struct
> path *path)
> ?	return path_has_perm(current_cred(), path, FILE__GETATTR);
> ?}
> ?
> -static int selinux_inode_setotherxattr(struct dentry *dentry, const
> char *name)
> -{
> -	const struct cred *cred = current_cred();
> -
> -	if (!strncmp(name, XATTR_SECURITY_PREFIX,
> -		?????sizeof XATTR_SECURITY_PREFIX - 1)) {
> -		if (!strcmp(name, XATTR_NAME_CAPS)) {
> -			if (!capable(CAP_SETFCAP))
> -				return -EPERM;
> -		} else if (!capable(CAP_SYS_ADMIN)) {
> -			/* A different attribute in the security
> namespace.
> -			???Restrict to administrator. */
> -			return -EPERM;
> -		}
> -	}
> -
> -	/* Not an attribute we recognize, so just check the
> -	???ordinary setattr permission. */
> -	return dentry_has_perm(cred, dentry, FILE__SETATTR);
> -}
> -
> ?static bool has_cap_mac_admin(bool audit)
> ?{
> ?	const struct cred *cred = current_cred();
> @@ -3167,8 +3146,15 @@ static int selinux_inode_setxattr(struct
> dentry *dentry, const char *name,
> ?	u32 newsid, sid = current_sid();
> ?	int rc = 0;
> ?
> -	if (strcmp(name, XATTR_NAME_SELINUX))
> -		return selinux_inode_setotherxattr(dentry, name);
> +	if (strcmp(name, XATTR_NAME_SELINUX)) {
> +		rc = cap_inode_setxattr(dentry, name, value, size,
> flags);
> +		if (rc)
> +			return rc;
> +
> +		/* Not an attribute we recognize, so just check the
> +		???ordinary setattr permission. */
> +		return dentry_has_perm(current_cred(), dentry,
> FILE__SETATTR);
> +	}
> ?
> ?	sbsec = inode->i_sb->s_security;
> ?	if (!(sbsec->flags & SBLABEL_MNT))
> @@ -3282,8 +3268,15 @@ static int selinux_inode_listxattr(struct
> dentry *dentry)
> ?
> ?static int selinux_inode_removexattr(struct dentry *dentry, const
> char *name)
> ?{
> -	if (strcmp(name, XATTR_NAME_SELINUX))
> -		return selinux_inode_setotherxattr(dentry, name);
> +	if (strcmp(name, XATTR_NAME_SELINUX)) {
> +		int rc = cap_inode_removexattr(dentry, name);
> +		if (rc)
> +			return rc;
> +
> +		/* Not an attribute we recognize, so just check the
> +		???ordinary setattr permission. */
> +		return dentry_has_perm(current_cred(), dentry,
> FILE__SETATTR);
> +	}
> ?
> ?	/* No one is allowed to remove a SELinux security label.
> ?	???You can change the label, but all data must be labeled.
> */
--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in
the body of a message to majordomo at vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

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

* Re: [PATCH] selinux: Perform both commoncap and selinux xattr checks
  2017-10-02 14:38   ` [PATCH] selinux: Perform both commoncap and selinux xattr checks Eric W. Biederman
       [not found]     ` <873771ipib.fsf_-_-aS9lmoZGLiVWk0Htik3J/w@public.gmane.org>
@ 2017-10-03 21:08         ` Paul Moore
  2017-10-03 16:24     ` Stephen Smalley
  2 siblings, 0 replies; 39+ messages in thread
From: Paul Moore @ 2017-10-03 21:08 UTC (permalink / raw)
  To: Eric W. Biederman, Stephen Smalley
  Cc: selinux-+05T5uksL2qpZYMLLGbcSA, Linux Containers, Chris Wright,
	linux-security-module-u79uwXL29TY76Z2rM5mHXA, James Morris,
	Casey Schaufler, Eric Paris

On Mon, Oct 2, 2017 at 10:38 AM, Eric W. Biederman
<ebiederm-aS9lmoZGLiVWk0Htik3J/w@public.gmane.org> wrote:
>
> When selinux is loaded the relax permission checks for writing
> security.capable are not honored.  Which keeps file capabilities
> from being used in user namespaces.
>
> Stephen Smalley <sds-+05T5uksL2qpZYMLLGbcSA@public.gmane.org> writes:
>> Originally SELinux called the cap functions directly since there was no
>> stacking support in the infrastructure and one had to manually stack a
>> secondary module internally.  inode_setxattr and inode_removexattr
>> however were special cases because the cap functions would check
>> CAP_SYS_ADMIN for any non-capability attributes in the security.*
>> namespace, and we don't want to impose that requirement on setting
>> security.selinux.  Thus, we inlined the capabilities logic into the
>> selinux hook functions and adapted it appropriately.
>
> Now that the permission checks in commoncap have evolved this
> inlining of their contents has become a problem.  So restructure
> selinux_inode_removexattr, and selinux_inode_setxattr to call
> both the corresponding cap_inode_ function and dentry_has_perm
> when the attribute is not a selinux security xattr.   This ensures
> the policies of both commoncap and selinux are enforced.
>
> This results in smack and selinux having the same basic structure
> for setxattr and removexattr.  Performing their own special permission
> checks when it is their modules xattr being written to, and deferring
> to commoncap when that is not the case.  Then finally performing their
> generic module policy on all xattr writes.
>
> This structure is fine when you only consider stacking with the
> commoncap lsm, but it becomes a problem if two lsms that don't want
> the commoncap security checks on their own attributes need to be
> stack.  This means there will need to be updates in the future as lsm
> stacking is improved, but at least now the structure between smack and
> selinux is common making the code easier to refactor.
>
> This change also has the effect that selinux_linux_setotherxattr becomes
> unnecessary so it is removed.
>
> Fixes: 8db6c34f1dbc ("Introduce v3 namespaced file capabilities")
> Fixes: 7bbf0e052b76 ("[PATCH] selinux merge")
> Historical Tree: https://git.kernel.org/pub/scm/linux/kernel/git/tglx/history.git
> Signed-off-by: "Eric W. Biederman" <ebiederm-aS9lmoZGLiVWk0Htik3J/w@public.gmane.org>
> ---
>
> While this fixes some things this isn't a regression so it should be
> able to wait until the next merge window to be merged.   Would you like
> to take this through the selinux tree?  Or shall I take it through mine?
>
> security/selinux/hooks.c | 43 ++++++++++++++++++-------------------------
>  1 file changed, 18 insertions(+), 25 deletions(-)

This patch looks sane to me and I believe it addresses Stephen's
concerns, but I'll wait on him to comment on-list.

As far as how this gets merged, I'd much prefer to take this via the
SELinux tree given that all of the changes are internal to SELinux.

> diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
> index f5d304736852..c78dbec627f6 100644
> --- a/security/selinux/hooks.c
> +++ b/security/selinux/hooks.c
> @@ -3124,27 +3124,6 @@ static int selinux_inode_getattr(const struct path *path)
>         return path_has_perm(current_cred(), path, FILE__GETATTR);
>  }
>
> -static int selinux_inode_setotherxattr(struct dentry *dentry, const char *name)
> -{
> -       const struct cred *cred = current_cred();
> -
> -       if (!strncmp(name, XATTR_SECURITY_PREFIX,
> -                    sizeof XATTR_SECURITY_PREFIX - 1)) {
> -               if (!strcmp(name, XATTR_NAME_CAPS)) {
> -                       if (!capable(CAP_SETFCAP))
> -                               return -EPERM;
> -               } else if (!capable(CAP_SYS_ADMIN)) {
> -                       /* A different attribute in the security namespace.
> -                          Restrict to administrator. */
> -                       return -EPERM;
> -               }
> -       }
> -
> -       /* Not an attribute we recognize, so just check the
> -          ordinary setattr permission. */
> -       return dentry_has_perm(cred, dentry, FILE__SETATTR);
> -}
> -
>  static bool has_cap_mac_admin(bool audit)
>  {
>         const struct cred *cred = current_cred();
> @@ -3167,8 +3146,15 @@ static int selinux_inode_setxattr(struct dentry *dentry, const char *name,
>         u32 newsid, sid = current_sid();
>         int rc = 0;
>
> -       if (strcmp(name, XATTR_NAME_SELINUX))
> -               return selinux_inode_setotherxattr(dentry, name);
> +       if (strcmp(name, XATTR_NAME_SELINUX)) {
> +               rc = cap_inode_setxattr(dentry, name, value, size, flags);
> +               if (rc)
> +                       return rc;
> +
> +               /* Not an attribute we recognize, so just check the
> +                  ordinary setattr permission. */
> +               return dentry_has_perm(current_cred(), dentry, FILE__SETATTR);
> +       }
>
>         sbsec = inode->i_sb->s_security;
>         if (!(sbsec->flags & SBLABEL_MNT))
> @@ -3282,8 +3268,15 @@ static int selinux_inode_listxattr(struct dentry *dentry)
>
>  static int selinux_inode_removexattr(struct dentry *dentry, const char *name)
>  {
> -       if (strcmp(name, XATTR_NAME_SELINUX))
> -               return selinux_inode_setotherxattr(dentry, name);
> +       if (strcmp(name, XATTR_NAME_SELINUX)) {
> +               int rc = cap_inode_removexattr(dentry, name);
> +               if (rc)
> +                       return rc;
> +
> +               /* Not an attribute we recognize, so just check the
> +                  ordinary setattr permission. */
> +               return dentry_has_perm(current_cred(), dentry, FILE__SETATTR);
> +       }
>
>         /* No one is allowed to remove a SELinux security label.
>            You can change the label, but all data must be labeled. */
> --
> 2.14.1
>



-- 
paul moore
www.paul-moore.com

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

* Re: [PATCH] selinux: Perform both commoncap and selinux xattr checks
@ 2017-10-03 21:08         ` Paul Moore
  0 siblings, 0 replies; 39+ messages in thread
From: Paul Moore @ 2017-10-03 21:08 UTC (permalink / raw)
  To: Eric W. Biederman, Stephen Smalley
  Cc: linux-security-module, Linux Containers, Serge Hallyn,
	Chris Wright, James Morris, Eric Paris, Casey Schaufler, selinux

On Mon, Oct 2, 2017 at 10:38 AM, Eric W. Biederman
<ebiederm@xmission.com> wrote:
>
> When selinux is loaded the relax permission checks for writing
> security.capable are not honored.  Which keeps file capabilities
> from being used in user namespaces.
>
> Stephen Smalley <sds@tycho.nsa.gov> writes:
>> Originally SELinux called the cap functions directly since there was no
>> stacking support in the infrastructure and one had to manually stack a
>> secondary module internally.  inode_setxattr and inode_removexattr
>> however were special cases because the cap functions would check
>> CAP_SYS_ADMIN for any non-capability attributes in the security.*
>> namespace, and we don't want to impose that requirement on setting
>> security.selinux.  Thus, we inlined the capabilities logic into the
>> selinux hook functions and adapted it appropriately.
>
> Now that the permission checks in commoncap have evolved this
> inlining of their contents has become a problem.  So restructure
> selinux_inode_removexattr, and selinux_inode_setxattr to call
> both the corresponding cap_inode_ function and dentry_has_perm
> when the attribute is not a selinux security xattr.   This ensures
> the policies of both commoncap and selinux are enforced.
>
> This results in smack and selinux having the same basic structure
> for setxattr and removexattr.  Performing their own special permission
> checks when it is their modules xattr being written to, and deferring
> to commoncap when that is not the case.  Then finally performing their
> generic module policy on all xattr writes.
>
> This structure is fine when you only consider stacking with the
> commoncap lsm, but it becomes a problem if two lsms that don't want
> the commoncap security checks on their own attributes need to be
> stack.  This means there will need to be updates in the future as lsm
> stacking is improved, but at least now the structure between smack and
> selinux is common making the code easier to refactor.
>
> This change also has the effect that selinux_linux_setotherxattr becomes
> unnecessary so it is removed.
>
> Fixes: 8db6c34f1dbc ("Introduce v3 namespaced file capabilities")
> Fixes: 7bbf0e052b76 ("[PATCH] selinux merge")
> Historical Tree: https://git.kernel.org/pub/scm/linux/kernel/git/tglx/history.git
> Signed-off-by: "Eric W. Biederman" <ebiederm@xmission.com>
> ---
>
> While this fixes some things this isn't a regression so it should be
> able to wait until the next merge window to be merged.   Would you like
> to take this through the selinux tree?  Or shall I take it through mine?
>
> security/selinux/hooks.c | 43 ++++++++++++++++++-------------------------
>  1 file changed, 18 insertions(+), 25 deletions(-)

This patch looks sane to me and I believe it addresses Stephen's
concerns, but I'll wait on him to comment on-list.

As far as how this gets merged, I'd much prefer to take this via the
SELinux tree given that all of the changes are internal to SELinux.

> diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
> index f5d304736852..c78dbec627f6 100644
> --- a/security/selinux/hooks.c
> +++ b/security/selinux/hooks.c
> @@ -3124,27 +3124,6 @@ static int selinux_inode_getattr(const struct path *path)
>         return path_has_perm(current_cred(), path, FILE__GETATTR);
>  }
>
> -static int selinux_inode_setotherxattr(struct dentry *dentry, const char *name)
> -{
> -       const struct cred *cred = current_cred();
> -
> -       if (!strncmp(name, XATTR_SECURITY_PREFIX,
> -                    sizeof XATTR_SECURITY_PREFIX - 1)) {
> -               if (!strcmp(name, XATTR_NAME_CAPS)) {
> -                       if (!capable(CAP_SETFCAP))
> -                               return -EPERM;
> -               } else if (!capable(CAP_SYS_ADMIN)) {
> -                       /* A different attribute in the security namespace.
> -                          Restrict to administrator. */
> -                       return -EPERM;
> -               }
> -       }
> -
> -       /* Not an attribute we recognize, so just check the
> -          ordinary setattr permission. */
> -       return dentry_has_perm(cred, dentry, FILE__SETATTR);
> -}
> -
>  static bool has_cap_mac_admin(bool audit)
>  {
>         const struct cred *cred = current_cred();
> @@ -3167,8 +3146,15 @@ static int selinux_inode_setxattr(struct dentry *dentry, const char *name,
>         u32 newsid, sid = current_sid();
>         int rc = 0;
>
> -       if (strcmp(name, XATTR_NAME_SELINUX))
> -               return selinux_inode_setotherxattr(dentry, name);
> +       if (strcmp(name, XATTR_NAME_SELINUX)) {
> +               rc = cap_inode_setxattr(dentry, name, value, size, flags);
> +               if (rc)
> +                       return rc;
> +
> +               /* Not an attribute we recognize, so just check the
> +                  ordinary setattr permission. */
> +               return dentry_has_perm(current_cred(), dentry, FILE__SETATTR);
> +       }
>
>         sbsec = inode->i_sb->s_security;
>         if (!(sbsec->flags & SBLABEL_MNT))
> @@ -3282,8 +3268,15 @@ static int selinux_inode_listxattr(struct dentry *dentry)
>
>  static int selinux_inode_removexattr(struct dentry *dentry, const char *name)
>  {
> -       if (strcmp(name, XATTR_NAME_SELINUX))
> -               return selinux_inode_setotherxattr(dentry, name);
> +       if (strcmp(name, XATTR_NAME_SELINUX)) {
> +               int rc = cap_inode_removexattr(dentry, name);
> +               if (rc)
> +                       return rc;
> +
> +               /* Not an attribute we recognize, so just check the
> +                  ordinary setattr permission. */
> +               return dentry_has_perm(current_cred(), dentry, FILE__SETATTR);
> +       }
>
>         /* No one is allowed to remove a SELinux security label.
>            You can change the label, but all data must be labeled. */
> --
> 2.14.1
>



-- 
paul moore
www.paul-moore.com

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

* [PATCH] selinux: Perform both commoncap and selinux xattr checks
@ 2017-10-03 21:08         ` Paul Moore
  0 siblings, 0 replies; 39+ messages in thread
From: Paul Moore @ 2017-10-03 21:08 UTC (permalink / raw)
  To: linux-security-module

On Mon, Oct 2, 2017 at 10:38 AM, Eric W. Biederman
<ebiederm@xmission.com> wrote:
>
> When selinux is loaded the relax permission checks for writing
> security.capable are not honored.  Which keeps file capabilities
> from being used in user namespaces.
>
> Stephen Smalley <sds@tycho.nsa.gov> writes:
>> Originally SELinux called the cap functions directly since there was no
>> stacking support in the infrastructure and one had to manually stack a
>> secondary module internally.  inode_setxattr and inode_removexattr
>> however were special cases because the cap functions would check
>> CAP_SYS_ADMIN for any non-capability attributes in the security.*
>> namespace, and we don't want to impose that requirement on setting
>> security.selinux.  Thus, we inlined the capabilities logic into the
>> selinux hook functions and adapted it appropriately.
>
> Now that the permission checks in commoncap have evolved this
> inlining of their contents has become a problem.  So restructure
> selinux_inode_removexattr, and selinux_inode_setxattr to call
> both the corresponding cap_inode_ function and dentry_has_perm
> when the attribute is not a selinux security xattr.   This ensures
> the policies of both commoncap and selinux are enforced.
>
> This results in smack and selinux having the same basic structure
> for setxattr and removexattr.  Performing their own special permission
> checks when it is their modules xattr being written to, and deferring
> to commoncap when that is not the case.  Then finally performing their
> generic module policy on all xattr writes.
>
> This structure is fine when you only consider stacking with the
> commoncap lsm, but it becomes a problem if two lsms that don't want
> the commoncap security checks on their own attributes need to be
> stack.  This means there will need to be updates in the future as lsm
> stacking is improved, but at least now the structure between smack and
> selinux is common making the code easier to refactor.
>
> This change also has the effect that selinux_linux_setotherxattr becomes
> unnecessary so it is removed.
>
> Fixes: 8db6c34f1dbc ("Introduce v3 namespaced file capabilities")
> Fixes: 7bbf0e052b76 ("[PATCH] selinux merge")
> Historical Tree: https://git.kernel.org/pub/scm/linux/kernel/git/tglx/history.git
> Signed-off-by: "Eric W. Biederman" <ebiederm@xmission.com>
> ---
>
> While this fixes some things this isn't a regression so it should be
> able to wait until the next merge window to be merged.   Would you like
> to take this through the selinux tree?  Or shall I take it through mine?
>
> security/selinux/hooks.c | 43 ++++++++++++++++++-------------------------
>  1 file changed, 18 insertions(+), 25 deletions(-)

This patch looks sane to me and I believe it addresses Stephen's
concerns, but I'll wait on him to comment on-list.

As far as how this gets merged, I'd much prefer to take this via the
SELinux tree given that all of the changes are internal to SELinux.

> diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
> index f5d304736852..c78dbec627f6 100644
> --- a/security/selinux/hooks.c
> +++ b/security/selinux/hooks.c
> @@ -3124,27 +3124,6 @@ static int selinux_inode_getattr(const struct path *path)
>         return path_has_perm(current_cred(), path, FILE__GETATTR);
>  }
>
> -static int selinux_inode_setotherxattr(struct dentry *dentry, const char *name)
> -{
> -       const struct cred *cred = current_cred();
> -
> -       if (!strncmp(name, XATTR_SECURITY_PREFIX,
> -                    sizeof XATTR_SECURITY_PREFIX - 1)) {
> -               if (!strcmp(name, XATTR_NAME_CAPS)) {
> -                       if (!capable(CAP_SETFCAP))
> -                               return -EPERM;
> -               } else if (!capable(CAP_SYS_ADMIN)) {
> -                       /* A different attribute in the security namespace.
> -                          Restrict to administrator. */
> -                       return -EPERM;
> -               }
> -       }
> -
> -       /* Not an attribute we recognize, so just check the
> -          ordinary setattr permission. */
> -       return dentry_has_perm(cred, dentry, FILE__SETATTR);
> -}
> -
>  static bool has_cap_mac_admin(bool audit)
>  {
>         const struct cred *cred = current_cred();
> @@ -3167,8 +3146,15 @@ static int selinux_inode_setxattr(struct dentry *dentry, const char *name,
>         u32 newsid, sid = current_sid();
>         int rc = 0;
>
> -       if (strcmp(name, XATTR_NAME_SELINUX))
> -               return selinux_inode_setotherxattr(dentry, name);
> +       if (strcmp(name, XATTR_NAME_SELINUX)) {
> +               rc = cap_inode_setxattr(dentry, name, value, size, flags);
> +               if (rc)
> +                       return rc;
> +
> +               /* Not an attribute we recognize, so just check the
> +                  ordinary setattr permission. */
> +               return dentry_has_perm(current_cred(), dentry, FILE__SETATTR);
> +       }
>
>         sbsec = inode->i_sb->s_security;
>         if (!(sbsec->flags & SBLABEL_MNT))
> @@ -3282,8 +3268,15 @@ static int selinux_inode_listxattr(struct dentry *dentry)
>
>  static int selinux_inode_removexattr(struct dentry *dentry, const char *name)
>  {
> -       if (strcmp(name, XATTR_NAME_SELINUX))
> -               return selinux_inode_setotherxattr(dentry, name);
> +       if (strcmp(name, XATTR_NAME_SELINUX)) {
> +               int rc = cap_inode_removexattr(dentry, name);
> +               if (rc)
> +                       return rc;
> +
> +               /* Not an attribute we recognize, so just check the
> +                  ordinary setattr permission. */
> +               return dentry_has_perm(current_cred(), dentry, FILE__SETATTR);
> +       }
>
>         /* No one is allowed to remove a SELinux security label.
>            You can change the label, but all data must be labeled. */
> --
> 2.14.1
>



-- 
paul moore
www.paul-moore.com
--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in
the body of a message to majordomo at vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

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

* Re: [PATCH] selinux: Perform both commoncap and selinux xattr checks
       [not found]         ` <CAHC9VhTzDKbP-h=GBaCTYOM9Sm=3C=nhNghmPoCRZitCpJj6YA-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
@ 2017-10-03 21:26           ` Eric W. Biederman
  0 siblings, 0 replies; 39+ messages in thread
From: Eric W. Biederman @ 2017-10-03 21:26 UTC (permalink / raw)
  To: Paul Moore
  Cc: selinux-+05T5uksL2qpZYMLLGbcSA, Linux Containers, Chris Wright,
	linux-security-module-u79uwXL29TY76Z2rM5mHXA, James Morris,
	Casey Schaufler, Eric Paris, Stephen Smalley

Paul Moore <paul-r2n+y4ga6xFZroRs9YW3xA@public.gmane.org> writes:

> On Mon, Oct 2, 2017 at 10:38 AM, Eric W. Biederman
> <ebiederm-aS9lmoZGLiVWk0Htik3J/w@public.gmane.org> wrote:
>>
>> When selinux is loaded the relax permission checks for writing
>> security.capable are not honored.  Which keeps file capabilities
>> from being used in user namespaces.
>>
>> Stephen Smalley <sds-+05T5uksL2qpZYMLLGbcSA@public.gmane.org> writes:
>>> Originally SELinux called the cap functions directly since there was no
>>> stacking support in the infrastructure and one had to manually stack a
>>> secondary module internally.  inode_setxattr and inode_removexattr
>>> however were special cases because the cap functions would check
>>> CAP_SYS_ADMIN for any non-capability attributes in the security.*
>>> namespace, and we don't want to impose that requirement on setting
>>> security.selinux.  Thus, we inlined the capabilities logic into the
>>> selinux hook functions and adapted it appropriately.
>>
>> Now that the permission checks in commoncap have evolved this
>> inlining of their contents has become a problem.  So restructure
>> selinux_inode_removexattr, and selinux_inode_setxattr to call
>> both the corresponding cap_inode_ function and dentry_has_perm
>> when the attribute is not a selinux security xattr.   This ensures
>> the policies of both commoncap and selinux are enforced.
>>
>> This results in smack and selinux having the same basic structure
>> for setxattr and removexattr.  Performing their own special permission
>> checks when it is their modules xattr being written to, and deferring
>> to commoncap when that is not the case.  Then finally performing their
>> generic module policy on all xattr writes.
>>
>> This structure is fine when you only consider stacking with the
>> commoncap lsm, but it becomes a problem if two lsms that don't want
>> the commoncap security checks on their own attributes need to be
>> stack.  This means there will need to be updates in the future as lsm
>> stacking is improved, but at least now the structure between smack and
>> selinux is common making the code easier to refactor.
>>
>> This change also has the effect that selinux_linux_setotherxattr becomes
>> unnecessary so it is removed.
>>
>> Fixes: 8db6c34f1dbc ("Introduce v3 namespaced file capabilities")
>> Fixes: 7bbf0e052b76 ("[PATCH] selinux merge")
>> Historical Tree: https://git.kernel.org/pub/scm/linux/kernel/git/tglx/history.git
>> Signed-off-by: "Eric W. Biederman" <ebiederm-aS9lmoZGLiVWk0Htik3J/w@public.gmane.org>
>> ---
>>
>> While this fixes some things this isn't a regression so it should be
>> able to wait until the next merge window to be merged.   Would you like
>> to take this through the selinux tree?  Or shall I take it through mine?
>>
>> security/selinux/hooks.c | 43 ++++++++++++++++++-------------------------
>>  1 file changed, 18 insertions(+), 25 deletions(-)
>
> This patch looks sane to me and I believe it addresses Stephen's
> concerns, but I'll wait on him to comment on-list.

He has alredy acked this publicly.

I may have skipped Cc'ing the selinux list as the discussion was
originally more general and the selinux list is reported to be
subscribers (not me) only.

> As far as how this gets merged, I'd much prefer to take this via the
> SELinux tree given that all of the changes are internal to SELinux.

Sounds good.  I just care that it get's picked up.

Eric


>> diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
>> index f5d304736852..c78dbec627f6 100644
>> --- a/security/selinux/hooks.c
>> +++ b/security/selinux/hooks.c
>> @@ -3124,27 +3124,6 @@ static int selinux_inode_getattr(const struct path *path)
>>         return path_has_perm(current_cred(), path, FILE__GETATTR);
>>  }
>>
>> -static int selinux_inode_setotherxattr(struct dentry *dentry, const char *name)
>> -{
>> -       const struct cred *cred = current_cred();
>> -
>> -       if (!strncmp(name, XATTR_SECURITY_PREFIX,
>> -                    sizeof XATTR_SECURITY_PREFIX - 1)) {
>> -               if (!strcmp(name, XATTR_NAME_CAPS)) {
>> -                       if (!capable(CAP_SETFCAP))
>> -                               return -EPERM;
>> -               } else if (!capable(CAP_SYS_ADMIN)) {
>> -                       /* A different attribute in the security namespace.
>> -                          Restrict to administrator. */
>> -                       return -EPERM;
>> -               }
>> -       }
>> -
>> -       /* Not an attribute we recognize, so just check the
>> -          ordinary setattr permission. */
>> -       return dentry_has_perm(cred, dentry, FILE__SETATTR);
>> -}
>> -
>>  static bool has_cap_mac_admin(bool audit)
>>  {
>>         const struct cred *cred = current_cred();
>> @@ -3167,8 +3146,15 @@ static int selinux_inode_setxattr(struct dentry *dentry, const char *name,
>>         u32 newsid, sid = current_sid();
>>         int rc = 0;
>>
>> -       if (strcmp(name, XATTR_NAME_SELINUX))
>> -               return selinux_inode_setotherxattr(dentry, name);
>> +       if (strcmp(name, XATTR_NAME_SELINUX)) {
>> +               rc = cap_inode_setxattr(dentry, name, value, size, flags);
>> +               if (rc)
>> +                       return rc;
>> +
>> +               /* Not an attribute we recognize, so just check the
>> +                  ordinary setattr permission. */
>> +               return dentry_has_perm(current_cred(), dentry, FILE__SETATTR);
>> +       }
>>
>>         sbsec = inode->i_sb->s_security;
>>         if (!(sbsec->flags & SBLABEL_MNT))
>> @@ -3282,8 +3268,15 @@ static int selinux_inode_listxattr(struct dentry *dentry)
>>
>>  static int selinux_inode_removexattr(struct dentry *dentry, const char *name)
>>  {
>> -       if (strcmp(name, XATTR_NAME_SELINUX))
>> -               return selinux_inode_setotherxattr(dentry, name);
>> +       if (strcmp(name, XATTR_NAME_SELINUX)) {
>> +               int rc = cap_inode_removexattr(dentry, name);
>> +               if (rc)
>> +                       return rc;
>> +
>> +               /* Not an attribute we recognize, so just check the
>> +                  ordinary setattr permission. */
>> +               return dentry_has_perm(current_cred(), dentry, FILE__SETATTR);
>> +       }
>>
>>         /* No one is allowed to remove a SELinux security label.
>>            You can change the label, but all data must be labeled. */
>> --
>> 2.14.1
>>

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

* Re: [PATCH] selinux: Perform both commoncap and selinux xattr checks
  2017-10-03 21:08         ` Paul Moore
@ 2017-10-03 21:26           ` Eric W. Biederman
  -1 siblings, 0 replies; 39+ messages in thread
From: Eric W. Biederman @ 2017-10-03 21:26 UTC (permalink / raw)
  To: Paul Moore
  Cc: Stephen Smalley, linux-security-module, Linux Containers,
	Serge Hallyn, Chris Wright, James Morris, Eric Paris,
	Casey Schaufler, selinux

Paul Moore <paul@paul-moore.com> writes:

> On Mon, Oct 2, 2017 at 10:38 AM, Eric W. Biederman
> <ebiederm@xmission.com> wrote:
>>
>> When selinux is loaded the relax permission checks for writing
>> security.capable are not honored.  Which keeps file capabilities
>> from being used in user namespaces.
>>
>> Stephen Smalley <sds@tycho.nsa.gov> writes:
>>> Originally SELinux called the cap functions directly since there was no
>>> stacking support in the infrastructure and one had to manually stack a
>>> secondary module internally.  inode_setxattr and inode_removexattr
>>> however were special cases because the cap functions would check
>>> CAP_SYS_ADMIN for any non-capability attributes in the security.*
>>> namespace, and we don't want to impose that requirement on setting
>>> security.selinux.  Thus, we inlined the capabilities logic into the
>>> selinux hook functions and adapted it appropriately.
>>
>> Now that the permission checks in commoncap have evolved this
>> inlining of their contents has become a problem.  So restructure
>> selinux_inode_removexattr, and selinux_inode_setxattr to call
>> both the corresponding cap_inode_ function and dentry_has_perm
>> when the attribute is not a selinux security xattr.   This ensures
>> the policies of both commoncap and selinux are enforced.
>>
>> This results in smack and selinux having the same basic structure
>> for setxattr and removexattr.  Performing their own special permission
>> checks when it is their modules xattr being written to, and deferring
>> to commoncap when that is not the case.  Then finally performing their
>> generic module policy on all xattr writes.
>>
>> This structure is fine when you only consider stacking with the
>> commoncap lsm, but it becomes a problem if two lsms that don't want
>> the commoncap security checks on their own attributes need to be
>> stack.  This means there will need to be updates in the future as lsm
>> stacking is improved, but at least now the structure between smack and
>> selinux is common making the code easier to refactor.
>>
>> This change also has the effect that selinux_linux_setotherxattr becomes
>> unnecessary so it is removed.
>>
>> Fixes: 8db6c34f1dbc ("Introduce v3 namespaced file capabilities")
>> Fixes: 7bbf0e052b76 ("[PATCH] selinux merge")
>> Historical Tree: https://git.kernel.org/pub/scm/linux/kernel/git/tglx/history.git
>> Signed-off-by: "Eric W. Biederman" <ebiederm@xmission.com>
>> ---
>>
>> While this fixes some things this isn't a regression so it should be
>> able to wait until the next merge window to be merged.   Would you like
>> to take this through the selinux tree?  Or shall I take it through mine?
>>
>> security/selinux/hooks.c | 43 ++++++++++++++++++-------------------------
>>  1 file changed, 18 insertions(+), 25 deletions(-)
>
> This patch looks sane to me and I believe it addresses Stephen's
> concerns, but I'll wait on him to comment on-list.

He has alredy acked this publicly.

I may have skipped Cc'ing the selinux list as the discussion was
originally more general and the selinux list is reported to be
subscribers (not me) only.

> As far as how this gets merged, I'd much prefer to take this via the
> SELinux tree given that all of the changes are internal to SELinux.

Sounds good.  I just care that it get's picked up.

Eric


>> diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
>> index f5d304736852..c78dbec627f6 100644
>> --- a/security/selinux/hooks.c
>> +++ b/security/selinux/hooks.c
>> @@ -3124,27 +3124,6 @@ static int selinux_inode_getattr(const struct path *path)
>>         return path_has_perm(current_cred(), path, FILE__GETATTR);
>>  }
>>
>> -static int selinux_inode_setotherxattr(struct dentry *dentry, const char *name)
>> -{
>> -       const struct cred *cred = current_cred();
>> -
>> -       if (!strncmp(name, XATTR_SECURITY_PREFIX,
>> -                    sizeof XATTR_SECURITY_PREFIX - 1)) {
>> -               if (!strcmp(name, XATTR_NAME_CAPS)) {
>> -                       if (!capable(CAP_SETFCAP))
>> -                               return -EPERM;
>> -               } else if (!capable(CAP_SYS_ADMIN)) {
>> -                       /* A different attribute in the security namespace.
>> -                          Restrict to administrator. */
>> -                       return -EPERM;
>> -               }
>> -       }
>> -
>> -       /* Not an attribute we recognize, so just check the
>> -          ordinary setattr permission. */
>> -       return dentry_has_perm(cred, dentry, FILE__SETATTR);
>> -}
>> -
>>  static bool has_cap_mac_admin(bool audit)
>>  {
>>         const struct cred *cred = current_cred();
>> @@ -3167,8 +3146,15 @@ static int selinux_inode_setxattr(struct dentry *dentry, const char *name,
>>         u32 newsid, sid = current_sid();
>>         int rc = 0;
>>
>> -       if (strcmp(name, XATTR_NAME_SELINUX))
>> -               return selinux_inode_setotherxattr(dentry, name);
>> +       if (strcmp(name, XATTR_NAME_SELINUX)) {
>> +               rc = cap_inode_setxattr(dentry, name, value, size, flags);
>> +               if (rc)
>> +                       return rc;
>> +
>> +               /* Not an attribute we recognize, so just check the
>> +                  ordinary setattr permission. */
>> +               return dentry_has_perm(current_cred(), dentry, FILE__SETATTR);
>> +       }
>>
>>         sbsec = inode->i_sb->s_security;
>>         if (!(sbsec->flags & SBLABEL_MNT))
>> @@ -3282,8 +3268,15 @@ static int selinux_inode_listxattr(struct dentry *dentry)
>>
>>  static int selinux_inode_removexattr(struct dentry *dentry, const char *name)
>>  {
>> -       if (strcmp(name, XATTR_NAME_SELINUX))
>> -               return selinux_inode_setotherxattr(dentry, name);
>> +       if (strcmp(name, XATTR_NAME_SELINUX)) {
>> +               int rc = cap_inode_removexattr(dentry, name);
>> +               if (rc)
>> +                       return rc;
>> +
>> +               /* Not an attribute we recognize, so just check the
>> +                  ordinary setattr permission. */
>> +               return dentry_has_perm(current_cred(), dentry, FILE__SETATTR);
>> +       }
>>
>>         /* No one is allowed to remove a SELinux security label.
>>            You can change the label, but all data must be labeled. */
>> --
>> 2.14.1
>>

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

* [PATCH] selinux: Perform both commoncap and selinux xattr checks
@ 2017-10-03 21:26           ` Eric W. Biederman
  0 siblings, 0 replies; 39+ messages in thread
From: Eric W. Biederman @ 2017-10-03 21:26 UTC (permalink / raw)
  To: linux-security-module

Paul Moore <paul@paul-moore.com> writes:

> On Mon, Oct 2, 2017 at 10:38 AM, Eric W. Biederman
> <ebiederm@xmission.com> wrote:
>>
>> When selinux is loaded the relax permission checks for writing
>> security.capable are not honored.  Which keeps file capabilities
>> from being used in user namespaces.
>>
>> Stephen Smalley <sds@tycho.nsa.gov> writes:
>>> Originally SELinux called the cap functions directly since there was no
>>> stacking support in the infrastructure and one had to manually stack a
>>> secondary module internally.  inode_setxattr and inode_removexattr
>>> however were special cases because the cap functions would check
>>> CAP_SYS_ADMIN for any non-capability attributes in the security.*
>>> namespace, and we don't want to impose that requirement on setting
>>> security.selinux.  Thus, we inlined the capabilities logic into the
>>> selinux hook functions and adapted it appropriately.
>>
>> Now that the permission checks in commoncap have evolved this
>> inlining of their contents has become a problem.  So restructure
>> selinux_inode_removexattr, and selinux_inode_setxattr to call
>> both the corresponding cap_inode_ function and dentry_has_perm
>> when the attribute is not a selinux security xattr.   This ensures
>> the policies of both commoncap and selinux are enforced.
>>
>> This results in smack and selinux having the same basic structure
>> for setxattr and removexattr.  Performing their own special permission
>> checks when it is their modules xattr being written to, and deferring
>> to commoncap when that is not the case.  Then finally performing their
>> generic module policy on all xattr writes.
>>
>> This structure is fine when you only consider stacking with the
>> commoncap lsm, but it becomes a problem if two lsms that don't want
>> the commoncap security checks on their own attributes need to be
>> stack.  This means there will need to be updates in the future as lsm
>> stacking is improved, but at least now the structure between smack and
>> selinux is common making the code easier to refactor.
>>
>> This change also has the effect that selinux_linux_setotherxattr becomes
>> unnecessary so it is removed.
>>
>> Fixes: 8db6c34f1dbc ("Introduce v3 namespaced file capabilities")
>> Fixes: 7bbf0e052b76 ("[PATCH] selinux merge")
>> Historical Tree: https://git.kernel.org/pub/scm/linux/kernel/git/tglx/history.git
>> Signed-off-by: "Eric W. Biederman" <ebiederm@xmission.com>
>> ---
>>
>> While this fixes some things this isn't a regression so it should be
>> able to wait until the next merge window to be merged.   Would you like
>> to take this through the selinux tree?  Or shall I take it through mine?
>>
>> security/selinux/hooks.c | 43 ++++++++++++++++++-------------------------
>>  1 file changed, 18 insertions(+), 25 deletions(-)
>
> This patch looks sane to me and I believe it addresses Stephen's
> concerns, but I'll wait on him to comment on-list.

He has alredy acked this publicly.

I may have skipped Cc'ing the selinux list as the discussion was
originally more general and the selinux list is reported to be
subscribers (not me) only.

> As far as how this gets merged, I'd much prefer to take this via the
> SELinux tree given that all of the changes are internal to SELinux.

Sounds good.  I just care that it get's picked up.

Eric


>> diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
>> index f5d304736852..c78dbec627f6 100644
>> --- a/security/selinux/hooks.c
>> +++ b/security/selinux/hooks.c
>> @@ -3124,27 +3124,6 @@ static int selinux_inode_getattr(const struct path *path)
>>         return path_has_perm(current_cred(), path, FILE__GETATTR);
>>  }
>>
>> -static int selinux_inode_setotherxattr(struct dentry *dentry, const char *name)
>> -{
>> -       const struct cred *cred = current_cred();
>> -
>> -       if (!strncmp(name, XATTR_SECURITY_PREFIX,
>> -                    sizeof XATTR_SECURITY_PREFIX - 1)) {
>> -               if (!strcmp(name, XATTR_NAME_CAPS)) {
>> -                       if (!capable(CAP_SETFCAP))
>> -                               return -EPERM;
>> -               } else if (!capable(CAP_SYS_ADMIN)) {
>> -                       /* A different attribute in the security namespace.
>> -                          Restrict to administrator. */
>> -                       return -EPERM;
>> -               }
>> -       }
>> -
>> -       /* Not an attribute we recognize, so just check the
>> -          ordinary setattr permission. */
>> -       return dentry_has_perm(cred, dentry, FILE__SETATTR);
>> -}
>> -
>>  static bool has_cap_mac_admin(bool audit)
>>  {
>>         const struct cred *cred = current_cred();
>> @@ -3167,8 +3146,15 @@ static int selinux_inode_setxattr(struct dentry *dentry, const char *name,
>>         u32 newsid, sid = current_sid();
>>         int rc = 0;
>>
>> -       if (strcmp(name, XATTR_NAME_SELINUX))
>> -               return selinux_inode_setotherxattr(dentry, name);
>> +       if (strcmp(name, XATTR_NAME_SELINUX)) {
>> +               rc = cap_inode_setxattr(dentry, name, value, size, flags);
>> +               if (rc)
>> +                       return rc;
>> +
>> +               /* Not an attribute we recognize, so just check the
>> +                  ordinary setattr permission. */
>> +               return dentry_has_perm(current_cred(), dentry, FILE__SETATTR);
>> +       }
>>
>>         sbsec = inode->i_sb->s_security;
>>         if (!(sbsec->flags & SBLABEL_MNT))
>> @@ -3282,8 +3268,15 @@ static int selinux_inode_listxattr(struct dentry *dentry)
>>
>>  static int selinux_inode_removexattr(struct dentry *dentry, const char *name)
>>  {
>> -       if (strcmp(name, XATTR_NAME_SELINUX))
>> -               return selinux_inode_setotherxattr(dentry, name);
>> +       if (strcmp(name, XATTR_NAME_SELINUX)) {
>> +               int rc = cap_inode_removexattr(dentry, name);
>> +               if (rc)
>> +                       return rc;
>> +
>> +               /* Not an attribute we recognize, so just check the
>> +                  ordinary setattr permission. */
>> +               return dentry_has_perm(current_cred(), dentry, FILE__SETATTR);
>> +       }
>>
>>         /* No one is allowed to remove a SELinux security label.
>>            You can change the label, but all data must be labeled. */
>> --
>> 2.14.1
>>
--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in
the body of a message to majordomo at vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

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

* Re: [PATCH] selinux: Perform both commoncap and selinux xattr checks
       [not found]           ` <87a8179b3u.fsf-aS9lmoZGLiVWk0Htik3J/w@public.gmane.org>
@ 2017-10-04 14:53             ` Paul Moore
  0 siblings, 0 replies; 39+ messages in thread
From: Paul Moore @ 2017-10-04 14:53 UTC (permalink / raw)
  To: Eric W. Biederman
  Cc: selinux-+05T5uksL2qpZYMLLGbcSA, Linux Containers, Chris Wright,
	linux-security-module-u79uwXL29TY76Z2rM5mHXA, James Morris,
	Casey Schaufler, Eric Paris, Stephen Smalley

On Tue, Oct 3, 2017 at 5:26 PM, Eric W. Biederman <ebiederm-aS9lmoZGLiVWk0Htik3J/w@public.gmane.org> wrote:
> Paul Moore <paul-r2n+y4ga6xFZroRs9YW3xA@public.gmane.org> writes:
>
>> On Mon, Oct 2, 2017 at 10:38 AM, Eric W. Biederman
>> <ebiederm-aS9lmoZGLiVWk0Htik3J/w@public.gmane.org> wrote:
>>>
>>> When selinux is loaded the relax permission checks for writing
>>> security.capable are not honored.  Which keeps file capabilities
>>> from being used in user namespaces.
>>>
>>> Stephen Smalley <sds-+05T5uksL2qpZYMLLGbcSA@public.gmane.org> writes:
>>>> Originally SELinux called the cap functions directly since there was no
>>>> stacking support in the infrastructure and one had to manually stack a
>>>> secondary module internally.  inode_setxattr and inode_removexattr
>>>> however were special cases because the cap functions would check
>>>> CAP_SYS_ADMIN for any non-capability attributes in the security.*
>>>> namespace, and we don't want to impose that requirement on setting
>>>> security.selinux.  Thus, we inlined the capabilities logic into the
>>>> selinux hook functions and adapted it appropriately.
>>>
>>> Now that the permission checks in commoncap have evolved this
>>> inlining of their contents has become a problem.  So restructure
>>> selinux_inode_removexattr, and selinux_inode_setxattr to call
>>> both the corresponding cap_inode_ function and dentry_has_perm
>>> when the attribute is not a selinux security xattr.   This ensures
>>> the policies of both commoncap and selinux are enforced.
>>>
>>> This results in smack and selinux having the same basic structure
>>> for setxattr and removexattr.  Performing their own special permission
>>> checks when it is their modules xattr being written to, and deferring
>>> to commoncap when that is not the case.  Then finally performing their
>>> generic module policy on all xattr writes.
>>>
>>> This structure is fine when you only consider stacking with the
>>> commoncap lsm, but it becomes a problem if two lsms that don't want
>>> the commoncap security checks on their own attributes need to be
>>> stack.  This means there will need to be updates in the future as lsm
>>> stacking is improved, but at least now the structure between smack and
>>> selinux is common making the code easier to refactor.
>>>
>>> This change also has the effect that selinux_linux_setotherxattr becomes
>>> unnecessary so it is removed.
>>>
>>> Fixes: 8db6c34f1dbc ("Introduce v3 namespaced file capabilities")
>>> Fixes: 7bbf0e052b76 ("[PATCH] selinux merge")
>>> Historical Tree: https://git.kernel.org/pub/scm/linux/kernel/git/tglx/history.git
>>> Signed-off-by: "Eric W. Biederman" <ebiederm-aS9lmoZGLiVWk0Htik3J/w@public.gmane.org>
>>> ---
>>>
>>> While this fixes some things this isn't a regression so it should be
>>> able to wait until the next merge window to be merged.   Would you like
>>> to take this through the selinux tree?  Or shall I take it through mine?
>>>
>>> security/selinux/hooks.c | 43 ++++++++++++++++++-------------------------
>>>  1 file changed, 18 insertions(+), 25 deletions(-)
>>
>> This patch looks sane to me and I believe it addresses Stephen's
>> concerns, but I'll wait on him to comment on-list.
>
> He has alredy acked this publicly.

So he did, for some reason I missed it in this thread, my apologies.

> I may have skipped Cc'ing the selinux list as the discussion was
> originally more general and the selinux list is reported to be
> subscribers (not me) only.

The list is quasi-open, non-members can post, they are just moderated.
That said I know the mailing list has been having some problems
lately.

>> As far as how this gets merged, I'd much prefer to take this via the
>> SELinux tree given that all of the changes are internal to SELinux.
>
> Sounds good.  I just care that it get's picked up.

Yep.  I just merged it into the SELinux next branch and I'll be
sending this up during the next merge window.

-- 
paul moore
www.paul-moore.com

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

* Re: [PATCH] selinux: Perform both commoncap and selinux xattr checks
  2017-10-03 21:26           ` Eric W. Biederman
@ 2017-10-04 14:53             ` Paul Moore
  -1 siblings, 0 replies; 39+ messages in thread
From: Paul Moore @ 2017-10-04 14:53 UTC (permalink / raw)
  To: Eric W. Biederman
  Cc: Stephen Smalley, linux-security-module, Linux Containers,
	Serge Hallyn, Chris Wright, James Morris, Eric Paris,
	Casey Schaufler, selinux

On Tue, Oct 3, 2017 at 5:26 PM, Eric W. Biederman <ebiederm@xmission.com> wrote:
> Paul Moore <paul@paul-moore.com> writes:
>
>> On Mon, Oct 2, 2017 at 10:38 AM, Eric W. Biederman
>> <ebiederm@xmission.com> wrote:
>>>
>>> When selinux is loaded the relax permission checks for writing
>>> security.capable are not honored.  Which keeps file capabilities
>>> from being used in user namespaces.
>>>
>>> Stephen Smalley <sds@tycho.nsa.gov> writes:
>>>> Originally SELinux called the cap functions directly since there was no
>>>> stacking support in the infrastructure and one had to manually stack a
>>>> secondary module internally.  inode_setxattr and inode_removexattr
>>>> however were special cases because the cap functions would check
>>>> CAP_SYS_ADMIN for any non-capability attributes in the security.*
>>>> namespace, and we don't want to impose that requirement on setting
>>>> security.selinux.  Thus, we inlined the capabilities logic into the
>>>> selinux hook functions and adapted it appropriately.
>>>
>>> Now that the permission checks in commoncap have evolved this
>>> inlining of their contents has become a problem.  So restructure
>>> selinux_inode_removexattr, and selinux_inode_setxattr to call
>>> both the corresponding cap_inode_ function and dentry_has_perm
>>> when the attribute is not a selinux security xattr.   This ensures
>>> the policies of both commoncap and selinux are enforced.
>>>
>>> This results in smack and selinux having the same basic structure
>>> for setxattr and removexattr.  Performing their own special permission
>>> checks when it is their modules xattr being written to, and deferring
>>> to commoncap when that is not the case.  Then finally performing their
>>> generic module policy on all xattr writes.
>>>
>>> This structure is fine when you only consider stacking with the
>>> commoncap lsm, but it becomes a problem if two lsms that don't want
>>> the commoncap security checks on their own attributes need to be
>>> stack.  This means there will need to be updates in the future as lsm
>>> stacking is improved, but at least now the structure between smack and
>>> selinux is common making the code easier to refactor.
>>>
>>> This change also has the effect that selinux_linux_setotherxattr becomes
>>> unnecessary so it is removed.
>>>
>>> Fixes: 8db6c34f1dbc ("Introduce v3 namespaced file capabilities")
>>> Fixes: 7bbf0e052b76 ("[PATCH] selinux merge")
>>> Historical Tree: https://git.kernel.org/pub/scm/linux/kernel/git/tglx/history.git
>>> Signed-off-by: "Eric W. Biederman" <ebiederm@xmission.com>
>>> ---
>>>
>>> While this fixes some things this isn't a regression so it should be
>>> able to wait until the next merge window to be merged.   Would you like
>>> to take this through the selinux tree?  Or shall I take it through mine?
>>>
>>> security/selinux/hooks.c | 43 ++++++++++++++++++-------------------------
>>>  1 file changed, 18 insertions(+), 25 deletions(-)
>>
>> This patch looks sane to me and I believe it addresses Stephen's
>> concerns, but I'll wait on him to comment on-list.
>
> He has alredy acked this publicly.

So he did, for some reason I missed it in this thread, my apologies.

> I may have skipped Cc'ing the selinux list as the discussion was
> originally more general and the selinux list is reported to be
> subscribers (not me) only.

The list is quasi-open, non-members can post, they are just moderated.
That said I know the mailing list has been having some problems
lately.

>> As far as how this gets merged, I'd much prefer to take this via the
>> SELinux tree given that all of the changes are internal to SELinux.
>
> Sounds good.  I just care that it get's picked up.

Yep.  I just merged it into the SELinux next branch and I'll be
sending this up during the next merge window.

-- 
paul moore
www.paul-moore.com

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

* [PATCH] selinux: Perform both commoncap and selinux xattr checks
@ 2017-10-04 14:53             ` Paul Moore
  0 siblings, 0 replies; 39+ messages in thread
From: Paul Moore @ 2017-10-04 14:53 UTC (permalink / raw)
  To: linux-security-module

On Tue, Oct 3, 2017 at 5:26 PM, Eric W. Biederman <ebiederm@xmission.com> wrote:
> Paul Moore <paul@paul-moore.com> writes:
>
>> On Mon, Oct 2, 2017 at 10:38 AM, Eric W. Biederman
>> <ebiederm@xmission.com> wrote:
>>>
>>> When selinux is loaded the relax permission checks for writing
>>> security.capable are not honored.  Which keeps file capabilities
>>> from being used in user namespaces.
>>>
>>> Stephen Smalley <sds@tycho.nsa.gov> writes:
>>>> Originally SELinux called the cap functions directly since there was no
>>>> stacking support in the infrastructure and one had to manually stack a
>>>> secondary module internally.  inode_setxattr and inode_removexattr
>>>> however were special cases because the cap functions would check
>>>> CAP_SYS_ADMIN for any non-capability attributes in the security.*
>>>> namespace, and we don't want to impose that requirement on setting
>>>> security.selinux.  Thus, we inlined the capabilities logic into the
>>>> selinux hook functions and adapted it appropriately.
>>>
>>> Now that the permission checks in commoncap have evolved this
>>> inlining of their contents has become a problem.  So restructure
>>> selinux_inode_removexattr, and selinux_inode_setxattr to call
>>> both the corresponding cap_inode_ function and dentry_has_perm
>>> when the attribute is not a selinux security xattr.   This ensures
>>> the policies of both commoncap and selinux are enforced.
>>>
>>> This results in smack and selinux having the same basic structure
>>> for setxattr and removexattr.  Performing their own special permission
>>> checks when it is their modules xattr being written to, and deferring
>>> to commoncap when that is not the case.  Then finally performing their
>>> generic module policy on all xattr writes.
>>>
>>> This structure is fine when you only consider stacking with the
>>> commoncap lsm, but it becomes a problem if two lsms that don't want
>>> the commoncap security checks on their own attributes need to be
>>> stack.  This means there will need to be updates in the future as lsm
>>> stacking is improved, but at least now the structure between smack and
>>> selinux is common making the code easier to refactor.
>>>
>>> This change also has the effect that selinux_linux_setotherxattr becomes
>>> unnecessary so it is removed.
>>>
>>> Fixes: 8db6c34f1dbc ("Introduce v3 namespaced file capabilities")
>>> Fixes: 7bbf0e052b76 ("[PATCH] selinux merge")
>>> Historical Tree: https://git.kernel.org/pub/scm/linux/kernel/git/tglx/history.git
>>> Signed-off-by: "Eric W. Biederman" <ebiederm@xmission.com>
>>> ---
>>>
>>> While this fixes some things this isn't a regression so it should be
>>> able to wait until the next merge window to be merged.   Would you like
>>> to take this through the selinux tree?  Or shall I take it through mine?
>>>
>>> security/selinux/hooks.c | 43 ++++++++++++++++++-------------------------
>>>  1 file changed, 18 insertions(+), 25 deletions(-)
>>
>> This patch looks sane to me and I believe it addresses Stephen's
>> concerns, but I'll wait on him to comment on-list.
>
> He has alredy acked this publicly.

So he did, for some reason I missed it in this thread, my apologies.

> I may have skipped Cc'ing the selinux list as the discussion was
> originally more general and the selinux list is reported to be
> subscribers (not me) only.

The list is quasi-open, non-members can post, they are just moderated.
That said I know the mailing list has been having some problems
lately.

>> As far as how this gets merged, I'd much prefer to take this via the
>> SELinux tree given that all of the changes are internal to SELinux.
>
> Sounds good.  I just care that it get's picked up.

Yep.  I just merged it into the SELinux next branch and I'll be
sending this up during the next merge window.

-- 
paul moore
www.paul-moore.com
--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in
the body of a message to majordomo at vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

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

* [RFC][PATCH] security: Make the selinux setxattr and removexattr hooks behave
@ 2017-09-28 22:34 Eric W. Biederman
  0 siblings, 0 replies; 39+ messages in thread
From: Eric W. Biederman @ 2017-09-28 22:34 UTC (permalink / raw)
  To: linux-security-module-u79uwXL29TY76Z2rM5mHXA
  Cc: Paul Moore, Linux Containers, Chris Wright, James Morris,
	Casey Schaufler, Eric Paris, Stephen Smalley


It looks like once upon a time a long time ago selinux copied code
from cap_inode_removexattr and cap_inode_setxattr into
selinux_inode_setotherxattr.  However the code has now diverged and
selinux is implementing a policy that is quite different than
cap_inode_setxattr and cap_inode_removexattr especially when it comes
to the security.capable xattr.

To keep things working and to make the comments in security/security.c
correct when the xattr is securit.capable, call cap_inode_setxattr
or cap_inode_removexattr as appropriate.

I suspect there is a larger conversation to be had here but this
is enough to keep selinux from implementing a non-sense hard coded
policy that breaks other parts of the kernel.

Signed-off-by: "Eric W. Biederman" <ebiederm-aS9lmoZGLiVWk0Htik3J/w@public.gmane.org>
---
 security/selinux/hooks.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
index f5d304736852..edf4bd292dc7 100644
--- a/security/selinux/hooks.c
+++ b/security/selinux/hooks.c
@@ -3167,6 +3167,9 @@ static int selinux_inode_setxattr(struct dentry *dentry, const char *name,
 	u32 newsid, sid = current_sid();
 	int rc = 0;
 
+	if (strcmp(name, XATTR_NAME_CAPS) == 0)
+		return cap_inode_setxattr(dentry, name, value, size, flags);
+
 	if (strcmp(name, XATTR_NAME_SELINUX))
 		return selinux_inode_setotherxattr(dentry, name);
 
@@ -3282,6 +3285,9 @@ static int selinux_inode_listxattr(struct dentry *dentry)
 
 static int selinux_inode_removexattr(struct dentry *dentry, const char *name)
 {
+	if (strcmp(name, XATTR_NAME_CAPS) == 0)
+		return cap_inode_removexattr(dentry, name);
+
 	if (strcmp(name, XATTR_NAME_SELINUX))
 		return selinux_inode_setotherxattr(dentry, name);
 
-- 
2.14.1

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

end of thread, other threads:[~2017-10-04 15:01 UTC | newest]

Thread overview: 39+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-09-28 22:34 [RFC][PATCH] security: Make the selinux setxattr and removexattr hooks behave Eric W. Biederman
2017-09-29  1:16 ` Casey Schaufler
2017-09-29 14:18   ` Stephen Smalley
2017-09-29 15:46     ` Casey Schaufler
     [not found]       ` <6f293107-6ff9-c4c7-f682-207a546c5061-iSGtlc1asvQWG2LlvL+J4A@public.gmane.org>
2017-09-30 16:22         ` Eric W. Biederman
2017-09-30 16:22       ` Eric W. Biederman
2017-09-30 17:01         ` Casey Schaufler
2017-09-30 20:40           ` Eric W. Biederman
2017-09-30 23:22             ` Casey Schaufler
2017-10-01  1:02               ` Eric W. Biederman
2017-10-01 18:52                 ` Casey Schaufler
2017-10-01 19:54                   ` Serge E. Hallyn
2017-10-01 22:11                   ` Eric W. Biederman
     [not found]               ` <bf18e641-91ed-0d75-f514-c059b5dfbb14-iSGtlc1asvQWG2LlvL+J4A@public.gmane.org>
2017-10-01  1:02                 ` Eric W. Biederman
     [not found]             ` <87d167ncms.fsf-aS9lmoZGLiVWk0Htik3J/w@public.gmane.org>
2017-09-30 23:22               ` Casey Schaufler
     [not found]           ` <db1c58f3-5a01-5276-eba7-5aac7cdcbcf5-iSGtlc1asvQWG2LlvL+J4A@public.gmane.org>
2017-09-30 20:40             ` Eric W. Biederman
     [not found]     ` <1506694737.5571.9.camel-+05T5uksL2qpZYMLLGbcSA@public.gmane.org>
2017-09-29 15:46       ` Casey Schaufler
     [not found]   ` <1913d5c4-64ef-36c1-e8ad-c779ff5c7995-iSGtlc1asvQWG2LlvL+J4A@public.gmane.org>
2017-09-29 14:18     ` Stephen Smalley
2017-09-29 12:36 ` Stephen Smalley
2017-10-02  3:26   ` Eric W. Biederman
2017-10-02 14:38   ` [PATCH] selinux: Perform both commoncap and selinux xattr checks Eric W. Biederman
     [not found]     ` <873771ipib.fsf_-_-aS9lmoZGLiVWk0Htik3J/w@public.gmane.org>
2017-10-02 15:52       ` Serge E. Hallyn
2017-10-03 16:24       ` Stephen Smalley
2017-10-03 21:08       ` Paul Moore
2017-10-03 21:08         ` Paul Moore
2017-10-03 21:08         ` Paul Moore
     [not found]         ` <CAHC9VhTzDKbP-h=GBaCTYOM9Sm=3C=nhNghmPoCRZitCpJj6YA-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2017-10-03 21:26           ` Eric W. Biederman
2017-10-03 21:26         ` Eric W. Biederman
2017-10-03 21:26           ` Eric W. Biederman
     [not found]           ` <87a8179b3u.fsf-aS9lmoZGLiVWk0Htik3J/w@public.gmane.org>
2017-10-04 14:53             ` Paul Moore
2017-10-04 14:53           ` Paul Moore
2017-10-04 14:53             ` Paul Moore
2017-10-02 15:52     ` Serge E. Hallyn
2017-10-03 16:24     ` Stephen Smalley
     [not found]   ` <1506688601.5571.1.camel-+05T5uksL2qpZYMLLGbcSA@public.gmane.org>
2017-10-02  3:26     ` [RFC][PATCH] security: Make the selinux setxattr and removexattr hooks behave Eric W. Biederman
2017-10-02 14:38     ` [PATCH] selinux: Perform both commoncap and selinux xattr checks Eric W. Biederman
     [not found] ` <87tvzmqwoi.fsf-aS9lmoZGLiVWk0Htik3J/w@public.gmane.org>
2017-09-29  1:16   ` [RFC][PATCH] security: Make the selinux setxattr and removexattr hooks behave Casey Schaufler
2017-09-29 12:36   ` Stephen Smalley
2017-09-28 22:34 Eric W. Biederman

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.