diff mbox series

[5.10,130/154] block: Check ADMIN before NICE for IOPRIO_CLASS_RT

Message ID 20211124115706.507376250@linuxfoundation.org (mailing list archive)
State New, archived
Headers show
Series None | expand

Commit Message

Greg KH Nov. 24, 2021, 11:58 a.m. UTC
From: Alistair Delva <adelva@google.com>

commit 94c4b4fd25e6c3763941bdec3ad54f2204afa992 upstream.

Booting to Android userspace on 5.14 or newer triggers the following
SELinux denial:

avc: denied { sys_nice } for comm="init" capability=23
     scontext=u:r:init:s0 tcontext=u:r:init:s0 tclass=capability
     permissive=0

Init is PID 0 running as root, so it already has CAP_SYS_ADMIN. For
better compatibility with older SEPolicy, check ADMIN before NICE.

Fixes: 9d3a39a5f1e4 ("block: grant IOPRIO_CLASS_RT to CAP_SYS_NICE")
Signed-off-by: Alistair Delva <adelva@google.com>
Cc: Khazhismel Kumykov <khazhy@google.com>
Cc: Bart Van Assche <bvanassche@acm.org>
Cc: Serge Hallyn <serge@hallyn.com>
Cc: Jens Axboe <axboe@kernel.dk>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: Paul Moore <paul@paul-moore.com>
Cc: selinux@vger.kernel.org
Cc: linux-security-module@vger.kernel.org
Cc: kernel-team@android.com
Cc: stable@vger.kernel.org # v5.14+
Reviewed-by: Bart Van Assche <bvanassche@acm.org>
Acked-by: Serge Hallyn <serge@hallyn.com>
Link: https://lore.kernel.org/r/20211115181655.3608659-1-adelva@google.com
Signed-off-by: Jens Axboe <axboe@kernel.dk>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
---
 block/ioprio.c |    9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

Comments

Jari Ruusu Nov. 24, 2021, 2:22 p.m. UTC | #1
Greg Kroah-Hartman wrote:
> From: Alistair Delva <adelva@google.com>
> 
> commit 94c4b4fd25e6c3763941bdec3ad54f2204afa992 upstream.
 [SNIP]
> --- a/block/ioprio.c
> +++ b/block/ioprio.c
> @@ -69,7 +69,14 @@ int ioprio_check_cap(int ioprio)
> 
>         switch (class) {
>                 case IOPRIO_CLASS_RT:
> -                       if (!capable(CAP_SYS_NICE) && !capable(CAP_SYS_ADMIN))
> +                       /*
> +                        * Originally this only checked for CAP_SYS_ADMIN,
> +                        * which was implicitly allowed for pid 0 by security
> +                        * modules such as SELinux. Make sure we check
> +                        * CAP_SYS_ADMIN first to avoid a denial/avc for
> +                        * possibly missing CAP_SYS_NICE permission.
> +                        */
> +                       if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_NICE))
>                                 return -EPERM;
>                         fallthrough;
>                         /* rt has prio field too */

What exactly is above patch trying to fix?
It does not change control flow at all, and added comment is misleading.
Greg KH Nov. 24, 2021, 3:31 p.m. UTC | #2
On Wed, Nov 24, 2021 at 04:22:50PM +0200, Jari Ruusu wrote:
> Greg Kroah-Hartman wrote:
> > From: Alistair Delva <adelva@google.com>
> > 
> > commit 94c4b4fd25e6c3763941bdec3ad54f2204afa992 upstream.
>  [SNIP]
> > --- a/block/ioprio.c
> > +++ b/block/ioprio.c
> > @@ -69,7 +69,14 @@ int ioprio_check_cap(int ioprio)
> > 
> >         switch (class) {
> >                 case IOPRIO_CLASS_RT:
> > -                       if (!capable(CAP_SYS_NICE) && !capable(CAP_SYS_ADMIN))
> > +                       /*
> > +                        * Originally this only checked for CAP_SYS_ADMIN,
> > +                        * which was implicitly allowed for pid 0 by security
> > +                        * modules such as SELinux. Make sure we check
> > +                        * CAP_SYS_ADMIN first to avoid a denial/avc for
> > +                        * possibly missing CAP_SYS_NICE permission.
> > +                        */
> > +                       if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_NICE))
> >                                 return -EPERM;
> >                         fallthrough;
> >                         /* rt has prio field too */
> 
> What exactly is above patch trying to fix?
> It does not change control flow at all, and added comment is misleading.

See the thread on the mailing list for what it does and why it is
needed.

It does change the result when selinux is enabled.

thanks,

greg k-h
Serge E. Hallyn Nov. 24, 2021, 5:33 p.m. UTC | #3
On Wed, Nov 24, 2021 at 04:31:22PM +0100, Greg Kroah-Hartman wrote:
> On Wed, Nov 24, 2021 at 04:22:50PM +0200, Jari Ruusu wrote:
> > Greg Kroah-Hartman wrote:
> > > From: Alistair Delva <adelva@google.com>
> > > 
> > > commit 94c4b4fd25e6c3763941bdec3ad54f2204afa992 upstream.
> >  [SNIP]
> > > --- a/block/ioprio.c
> > > +++ b/block/ioprio.c
> > > @@ -69,7 +69,14 @@ int ioprio_check_cap(int ioprio)
> > > 
> > >         switch (class) {
> > >                 case IOPRIO_CLASS_RT:
> > > -                       if (!capable(CAP_SYS_NICE) && !capable(CAP_SYS_ADMIN))
> > > +                       /*
> > > +                        * Originally this only checked for CAP_SYS_ADMIN,
> > > +                        * which was implicitly allowed for pid 0 by security
> > > +                        * modules such as SELinux. Make sure we check
> > > +                        * CAP_SYS_ADMIN first to avoid a denial/avc for
> > > +                        * possibly missing CAP_SYS_NICE permission.
> > > +                        */
> > > +                       if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_NICE))
> > >                                 return -EPERM;
> > >                         fallthrough;
> > >                         /* rt has prio field too */
> > 
> > What exactly is above patch trying to fix?
> > It does not change control flow at all, and added comment is misleading.
> 
> See the thread on the mailing list for what it does and why it is
> needed.
> 
> It does change the result when selinux is enabled.
> 
> thanks,
> 
> greg k-h

The case where we create a newer more fine grained capability which is a
sub-cap of a broader capability like CAP_SYS_ADMIN is analogous.  See
check_syslog_permissions() for instance.

So I think a helper like

int capable_either_or(int cap1, int cap2) {
	if (has_capability_noaudit(current, cap1))
		return 0;
	return capable(cap2);
}

might be worthwhile.
Greg KH Nov. 24, 2021, 6:15 p.m. UTC | #4
On Wed, Nov 24, 2021 at 11:33:11AM -0600, Serge E. Hallyn wrote:
> On Wed, Nov 24, 2021 at 04:31:22PM +0100, Greg Kroah-Hartman wrote:
> > On Wed, Nov 24, 2021 at 04:22:50PM +0200, Jari Ruusu wrote:
> > > Greg Kroah-Hartman wrote:
> > > > From: Alistair Delva <adelva@google.com>
> > > > 
> > > > commit 94c4b4fd25e6c3763941bdec3ad54f2204afa992 upstream.
> > >  [SNIP]
> > > > --- a/block/ioprio.c
> > > > +++ b/block/ioprio.c
> > > > @@ -69,7 +69,14 @@ int ioprio_check_cap(int ioprio)
> > > > 
> > > >         switch (class) {
> > > >                 case IOPRIO_CLASS_RT:
> > > > -                       if (!capable(CAP_SYS_NICE) && !capable(CAP_SYS_ADMIN))
> > > > +                       /*
> > > > +                        * Originally this only checked for CAP_SYS_ADMIN,
> > > > +                        * which was implicitly allowed for pid 0 by security
> > > > +                        * modules such as SELinux. Make sure we check
> > > > +                        * CAP_SYS_ADMIN first to avoid a denial/avc for
> > > > +                        * possibly missing CAP_SYS_NICE permission.
> > > > +                        */
> > > > +                       if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_NICE))
> > > >                                 return -EPERM;
> > > >                         fallthrough;
> > > >                         /* rt has prio field too */
> > > 
> > > What exactly is above patch trying to fix?
> > > It does not change control flow at all, and added comment is misleading.
> > 
> > See the thread on the mailing list for what it does and why it is
> > needed.
> > 
> > It does change the result when selinux is enabled.
> > 
> > thanks,
> > 
> > greg k-h
> 
> The case where we create a newer more fine grained capability which is a
> sub-cap of a broader capability like CAP_SYS_ADMIN is analogous.  See
> check_syslog_permissions() for instance.
> 
> So I think a helper like
> 
> int capable_either_or(int cap1, int cap2) {
> 	if (has_capability_noaudit(current, cap1))
> 		return 0;
> 	return capable(cap2);
> }
> 
> might be worthwhile.

Sure, feel free to work on that and submit it, but for now, this change
is needed.

thanks,

greg k-h
Christian Göttsche Nov. 24, 2021, 6:34 p.m. UTC | #5
On Wed, 24 Nov 2021 at 19:16, Greg Kroah-Hartman
<gregkh@linuxfoundation.org> wrote:
>
> On Wed, Nov 24, 2021 at 11:33:11AM -0600, Serge E. Hallyn wrote:
> > On Wed, Nov 24, 2021 at 04:31:22PM +0100, Greg Kroah-Hartman wrote:
> > > On Wed, Nov 24, 2021 at 04:22:50PM +0200, Jari Ruusu wrote:
> > > > Greg Kroah-Hartman wrote:
> > > > > From: Alistair Delva <adelva@google.com>
> > > > >
> > > > > commit 94c4b4fd25e6c3763941bdec3ad54f2204afa992 upstream.
> > > >  [SNIP]
> > > > > --- a/block/ioprio.c
> > > > > +++ b/block/ioprio.c
> > > > > @@ -69,7 +69,14 @@ int ioprio_check_cap(int ioprio)
> > > > >
> > > > >         switch (class) {
> > > > >                 case IOPRIO_CLASS_RT:
> > > > > -                       if (!capable(CAP_SYS_NICE) && !capable(CAP_SYS_ADMIN))
> > > > > +                       /*
> > > > > +                        * Originally this only checked for CAP_SYS_ADMIN,
> > > > > +                        * which was implicitly allowed for pid 0 by security
> > > > > +                        * modules such as SELinux. Make sure we check
> > > > > +                        * CAP_SYS_ADMIN first to avoid a denial/avc for
> > > > > +                        * possibly missing CAP_SYS_NICE permission.
> > > > > +                        */
> > > > > +                       if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_NICE))
> > > > >                                 return -EPERM;
> > > > >                         fallthrough;
> > > > >                         /* rt has prio field too */
> > > >
> > > > What exactly is above patch trying to fix?
> > > > It does not change control flow at all, and added comment is misleading.
> > >
> > > See the thread on the mailing list for what it does and why it is
> > > needed.
> > >
> > > It does change the result when selinux is enabled.
> > >
> > > thanks,
> > >
> > > greg k-h
> >
> > The case where we create a newer more fine grained capability which is a
> > sub-cap of a broader capability like CAP_SYS_ADMIN is analogous.  See
> > check_syslog_permissions() for instance.
> >
> > So I think a helper like
> >
> > int capable_either_or(int cap1, int cap2) {
> >       if (has_capability_noaudit(current, cap1))
> >               return 0;
> >       return capable(cap2);
> > }
> >
> > might be worthwhile.
>

I proposed an early prototype at
https://patchwork.kernel.org/project/selinux/patch/20211116112437.43412-1-cgzones@googlemail.com/

> Sure, feel free to work on that and submit it, but for now, this change
> is needed.
>

I would argue this change is not necessary since the actual syscall
still succeeds as this is only an informative avc denial message about
a failed capability check. But this ship has sailed...

> thanks,
>
> greg k-h
Serge E. Hallyn Nov. 24, 2021, 11:29 p.m. UTC | #6
On Wed, Nov 24, 2021 at 07:15:35PM +0100, Greg Kroah-Hartman wrote:
> On Wed, Nov 24, 2021 at 11:33:11AM -0600, Serge E. Hallyn wrote:
> > On Wed, Nov 24, 2021 at 04:31:22PM +0100, Greg Kroah-Hartman wrote:
> > > On Wed, Nov 24, 2021 at 04:22:50PM +0200, Jari Ruusu wrote:
> > > > Greg Kroah-Hartman wrote:
> > > > > From: Alistair Delva <adelva@google.com>
> > > > > 
> > > > > commit 94c4b4fd25e6c3763941bdec3ad54f2204afa992 upstream.
> > > >  [SNIP]
> > > > > --- a/block/ioprio.c
> > > > > +++ b/block/ioprio.c
> > > > > @@ -69,7 +69,14 @@ int ioprio_check_cap(int ioprio)
> > > > > 
> > > > >         switch (class) {
> > > > >                 case IOPRIO_CLASS_RT:
> > > > > -                       if (!capable(CAP_SYS_NICE) && !capable(CAP_SYS_ADMIN))
> > > > > +                       /*
> > > > > +                        * Originally this only checked for CAP_SYS_ADMIN,
> > > > > +                        * which was implicitly allowed for pid 0 by security
> > > > > +                        * modules such as SELinux. Make sure we check
> > > > > +                        * CAP_SYS_ADMIN first to avoid a denial/avc for
> > > > > +                        * possibly missing CAP_SYS_NICE permission.
> > > > > +                        */
> > > > > +                       if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_NICE))
> > > > >                                 return -EPERM;
> > > > >                         fallthrough;
> > > > >                         /* rt has prio field too */
> > > > 
> > > > What exactly is above patch trying to fix?
> > > > It does not change control flow at all, and added comment is misleading.
> > > 
> > > See the thread on the mailing list for what it does and why it is
> > > needed.
> > > 
> > > It does change the result when selinux is enabled.
> > > 
> > > thanks,
> > > 
> > > greg k-h
> > 
> > The case where we create a newer more fine grained capability which is a
> > sub-cap of a broader capability like CAP_SYS_ADMIN is analogous.  See
> > check_syslog_permissions() for instance.
> > 
> > So I think a helper like
> > 
> > int capable_either_or(int cap1, int cap2) {
> > 	if (has_capability_noaudit(current, cap1))
> > 		return 0;
> > 	return capable(cap2);
> > }
> > 
> > might be worthwhile.
> 
> Sure, feel free to work on that and submit it, but for now, this change
> is needed.

Sorry I misread the subject and thought this was just a resubmission.

FWIW I had acked an earlier version of this.

-serge
Serge E. Hallyn Nov. 24, 2021, 11:30 p.m. UTC | #7
On Wed, Nov 24, 2021 at 07:34:50PM +0100, Christian Göttsche wrote:
> On Wed, 24 Nov 2021 at 19:16, Greg Kroah-Hartman
> <gregkh@linuxfoundation.org> wrote:
> >
> > On Wed, Nov 24, 2021 at 11:33:11AM -0600, Serge E. Hallyn wrote:
> > > On Wed, Nov 24, 2021 at 04:31:22PM +0100, Greg Kroah-Hartman wrote:
> > > > On Wed, Nov 24, 2021 at 04:22:50PM +0200, Jari Ruusu wrote:
> > > > > Greg Kroah-Hartman wrote:
> > > > > > From: Alistair Delva <adelva@google.com>
> > > > > >
> > > > > > commit 94c4b4fd25e6c3763941bdec3ad54f2204afa992 upstream.
> > > > >  [SNIP]
> > > > > > --- a/block/ioprio.c
> > > > > > +++ b/block/ioprio.c
> > > > > > @@ -69,7 +69,14 @@ int ioprio_check_cap(int ioprio)
> > > > > >
> > > > > >         switch (class) {
> > > > > >                 case IOPRIO_CLASS_RT:
> > > > > > -                       if (!capable(CAP_SYS_NICE) && !capable(CAP_SYS_ADMIN))
> > > > > > +                       /*
> > > > > > +                        * Originally this only checked for CAP_SYS_ADMIN,
> > > > > > +                        * which was implicitly allowed for pid 0 by security
> > > > > > +                        * modules such as SELinux. Make sure we check
> > > > > > +                        * CAP_SYS_ADMIN first to avoid a denial/avc for
> > > > > > +                        * possibly missing CAP_SYS_NICE permission.
> > > > > > +                        */
> > > > > > +                       if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_NICE))
> > > > > >                                 return -EPERM;
> > > > > >                         fallthrough;
> > > > > >                         /* rt has prio field too */
> > > > >
> > > > > What exactly is above patch trying to fix?
> > > > > It does not change control flow at all, and added comment is misleading.
> > > >
> > > > See the thread on the mailing list for what it does and why it is
> > > > needed.
> > > >
> > > > It does change the result when selinux is enabled.
> > > >
> > > > thanks,
> > > >
> > > > greg k-h
> > >
> > > The case where we create a newer more fine grained capability which is a
> > > sub-cap of a broader capability like CAP_SYS_ADMIN is analogous.  See
> > > check_syslog_permissions() for instance.
> > >
> > > So I think a helper like
> > >
> > > int capable_either_or(int cap1, int cap2) {
> > >       if (has_capability_noaudit(current, cap1))
> > >               return 0;
> > >       return capable(cap2);
> > > }
> > >
> > > might be worthwhile.
> >
> 
> I proposed an early prototype at
> https://patchwork.kernel.org/project/selinux/patch/20211116112437.43412-1-cgzones@googlemail.com/

I never saw this.  Would you mind resending as a standalone patch?

(I do have comments, but this thread seems the wrong place)
diff mbox series

Patch

--- a/block/ioprio.c
+++ b/block/ioprio.c
@@ -69,7 +69,14 @@  int ioprio_check_cap(int ioprio)
 
 	switch (class) {
 		case IOPRIO_CLASS_RT:
-			if (!capable(CAP_SYS_NICE) && !capable(CAP_SYS_ADMIN))
+			/*
+			 * Originally this only checked for CAP_SYS_ADMIN,
+			 * which was implicitly allowed for pid 0 by security
+			 * modules such as SELinux. Make sure we check
+			 * CAP_SYS_ADMIN first to avoid a denial/avc for
+			 * possibly missing CAP_SYS_NICE permission.
+			 */
+			if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_NICE))
 				return -EPERM;
 			fallthrough;
 			/* rt has prio field too */