diff mbox series

[V2] security/selinux: Always allow FIOCLEX and FIONCLEX

Message ID 20220221131533.74238-1-richard_c_haines@btinternet.com (mailing list archive)
State Changes Requested
Delegated to: Paul Moore
Headers show
Series [V2] security/selinux: Always allow FIOCLEX and FIONCLEX | expand

Commit Message

Richard Haines Feb. 21, 2022, 1:15 p.m. UTC
These ioctls are equivalent to fcntl(fd, F_SETFD, flags), which SELinux
always allows too.  Furthermore, a failed FIOCLEX could result in a file
descriptor being leaked to a process that should not have access to it.

As this patch removes access controls, a policy capability needs to be
enabled in policy to always allow these ioctls.

Based-on-patch-by: Demi Marie Obenour <demiobenour@gmail.com>
Signed-off-by: Richard Haines <richard_c_haines@btinternet.com>
---
V2 Change: Control via a policy capability. See this thread for discussion:
https://lore.kernel.org/selinux/CAHC9VhQEPxYP_KU56gAGNHKQaxucY8gSsHiUB42PVgADBAccRQ@mail.gmail.com/T/#t

With this patch and the polcap enabled, the selinux-testsuite will fail:
ioctl/test at line 47 - Will need a fix.

 security/selinux/hooks.c                   | 7 +++++++
 security/selinux/include/policycap.h       | 1 +
 security/selinux/include/policycap_names.h | 3 ++-
 security/selinux/include/security.h        | 7 +++++++
 4 files changed, 17 insertions(+), 1 deletion(-)

Comments

Paul Moore Feb. 22, 2022, 11:28 p.m. UTC | #1
On Mon, Feb 21, 2022 at 8:15 AM Richard Haines
<richard_c_haines@btinternet.com> wrote:
>
> These ioctls are equivalent to fcntl(fd, F_SETFD, flags), which SELinux
> always allows too.  Furthermore, a failed FIOCLEX could result in a file
> descriptor being leaked to a process that should not have access to it.
>
> As this patch removes access controls, a policy capability needs to be
> enabled in policy to always allow these ioctls.
>
> Based-on-patch-by: Demi Marie Obenour <demiobenour@gmail.com>
> Signed-off-by: Richard Haines <richard_c_haines@btinternet.com>
> ---
> V2 Change: Control via a policy capability. See this thread for discussion:
> https://lore.kernel.org/selinux/CAHC9VhQEPxYP_KU56gAGNHKQaxucY8gSsHiUB42PVgADBAccRQ@mail.gmail.com/T/#t
>
> With this patch and the polcap enabled, the selinux-testsuite will fail:
> ioctl/test at line 47 - Will need a fix.
>
>  security/selinux/hooks.c                   | 7 +++++++
>  security/selinux/include/policycap.h       | 1 +
>  security/selinux/include/policycap_names.h | 3 ++-
>  security/selinux/include/security.h        | 7 +++++++
>  4 files changed, 17 insertions(+), 1 deletion(-)

Thanks Richard for putting together the v2 of this patch.

As far as the test is concerned, it seems like the quick-n-dirty fix
is to simply remove the ioctl(FIOCLEX) test in test_noioctl.c; is
everyone okay with that?  At least that is what I'm going to do with
my local copy that I use to validate the kernel-secnext builds unless
someone has a better patch :)

> diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
> index 5b6895e4f..030c41652 100644
> --- a/security/selinux/hooks.c
> +++ b/security/selinux/hooks.c
> @@ -3745,6 +3745,13 @@ static int selinux_file_ioctl(struct file *file, unsigned int cmd,
>                                             CAP_OPT_NONE, true);
>                 break;
>
> +       case FIOCLEX:
> +       case FIONCLEX:
> +               /* Must always succeed if polcap set, else default: */
> +               if (selinux_policycap_ioctl_skip_cloexec())
> +                       break;
> +               fallthrough;
> +

The break/fallthrough looks like it might be a little more fragile
than necessary, how about something like this:

  case FIOCLEX:
  case FIONCLEX:
    if (!selinux_policycap_ioctl_skip_cloexec())
      error = ioctl_has_perm(cred, file, FILE__IOCTL, (u16) cmd);
      break;

Yes, it does duplicate the default ioctl_has_perm() call, but since we
are effectively deprecating this and locking the FIOCLEX/FIONCLEX
behavior with this policy capability it seems okay to me (and
preferable to relying on the fallthrough).

Thoughts?
Richard Haines Feb. 23, 2022, 11:58 a.m. UTC | #2
On Tue, 2022-02-22 at 18:28 -0500, Paul Moore wrote:
> On Mon, Feb 21, 2022 at 8:15 AM Richard Haines
> <richard_c_haines@btinternet.com> wrote:
> > 
> > These ioctls are equivalent to fcntl(fd, F_SETFD, flags), which
> > SELinux
> > always allows too.  Furthermore, a failed FIOCLEX could result in a
> > file
> > descriptor being leaked to a process that should not have access to
> > it.
> > 
> > As this patch removes access controls, a policy capability needs to
> > be
> > enabled in policy to always allow these ioctls.
> > 
> > Based-on-patch-by: Demi Marie Obenour <demiobenour@gmail.com>
> > Signed-off-by: Richard Haines <richard_c_haines@btinternet.com>
> > ---
> > V2 Change: Control via a policy capability. See this thread for
> > discussion:
> > https://lore.kernel.org/selinux/CAHC9VhQEPxYP_KU56gAGNHKQaxucY8gSsHiUB42PVgADBAccRQ@mail.gmail.com/T/#t
> > 
> > With this patch and the polcap enabled, the selinux-testsuite will
> > fail:
> > ioctl/test at line 47 - Will need a fix.
> > 
> >  security/selinux/hooks.c                   | 7 +++++++
> >  security/selinux/include/policycap.h       | 1 +
> >  security/selinux/include/policycap_names.h | 3 ++-
> >  security/selinux/include/security.h        | 7 +++++++
> >  4 files changed, 17 insertions(+), 1 deletion(-)
> 
> Thanks Richard for putting together the v2 of this patch.
> 
> As far as the test is concerned, it seems like the quick-n-dirty fix
> is to simply remove the ioctl(FIOCLEX) test in test_noioctl.c; is
> everyone okay with that?  At least that is what I'm going to do with
> my local copy that I use to validate the kernel-secnext builds unless
> someone has a better patch :)

To fix this I was planning to submit a patch that would change the
ioctl(FIOCLEX) tests to ioctl(FS_IOC_GETFSLABEL) as that would continue
to test the xperms.

> 
> > diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
> > index 5b6895e4f..030c41652 100644
> > --- a/security/selinux/hooks.c
> > +++ b/security/selinux/hooks.c
> > @@ -3745,6 +3745,13 @@ static int selinux_file_ioctl(struct file
> > *file, unsigned int cmd,
> >                                             CAP_OPT_NONE, true);
> >                 break;
> > 
> > +       case FIOCLEX:
> > +       case FIONCLEX:
> > +               /* Must always succeed if polcap set, else default:
> > */
> > +               if (selinux_policycap_ioctl_skip_cloexec())
> > +                       break;
> > +               fallthrough;
> > +
> 
> The break/fallthrough looks like it might be a little more fragile
> than necessary, how about something like this:
> 
>   case FIOCLEX:
>   case FIONCLEX:
>     if (!selinux_policycap_ioctl_skip_cloexec())
>       error = ioctl_has_perm(cred, file, FILE__IOCTL, (u16) cmd);
>       break;
> 
> Yes, it does duplicate the default ioctl_has_perm() call, but since
> we
> are effectively deprecating this and locking the FIOCLEX/FIONCLEX
> behavior with this policy capability it seems okay to me (and
> preferable to relying on the fallthrough).
> 
> Thoughts?

Yes I did ponder this and in my first attempt I had this before the
switch():

	/* Must always succeed if polcap set */
	if (selinux_policycap_ioctl_skip_cloexec() &&
	    (cmd == FIOCLEX || cmd == FIONCLEX))
		return 0;

 	switch (cmd) {
 	case FIONREAD:
 	case FIBMAP:

but changed to within the switch(), anyway I'm happy to resubmit a
patch either way.

>
Ondrej Mosnacek Feb. 23, 2022, 12:12 p.m. UTC | #3
On Wed, Feb 23, 2022 at 12:58 PM Richard Haines
<richard_c_haines@btinternet.com> wrote:
> On Tue, 2022-02-22 at 18:28 -0500, Paul Moore wrote:
> > On Mon, Feb 21, 2022 at 8:15 AM Richard Haines
> > <richard_c_haines@btinternet.com> wrote:
> > >
> > > These ioctls are equivalent to fcntl(fd, F_SETFD, flags), which
> > > SELinux
> > > always allows too.  Furthermore, a failed FIOCLEX could result in a
> > > file
> > > descriptor being leaked to a process that should not have access to
> > > it.
> > >
> > > As this patch removes access controls, a policy capability needs to
> > > be
> > > enabled in policy to always allow these ioctls.
> > >
> > > Based-on-patch-by: Demi Marie Obenour <demiobenour@gmail.com>
> > > Signed-off-by: Richard Haines <richard_c_haines@btinternet.com>
> > > ---
> > > V2 Change: Control via a policy capability. See this thread for
> > > discussion:
> > > https://lore.kernel.org/selinux/CAHC9VhQEPxYP_KU56gAGNHKQaxucY8gSsHiUB42PVgADBAccRQ@mail.gmail.com/T/#t
> > >
> > > With this patch and the polcap enabled, the selinux-testsuite will
> > > fail:
> > > ioctl/test at line 47 - Will need a fix.
> > >
> > >  security/selinux/hooks.c                   | 7 +++++++
> > >  security/selinux/include/policycap.h       | 1 +
> > >  security/selinux/include/policycap_names.h | 3 ++-
> > >  security/selinux/include/security.h        | 7 +++++++
> > >  4 files changed, 17 insertions(+), 1 deletion(-)
> >
> > Thanks Richard for putting together the v2 of this patch.
> >
> > As far as the test is concerned, it seems like the quick-n-dirty fix
> > is to simply remove the ioctl(FIOCLEX) test in test_noioctl.c; is
> > everyone okay with that?  At least that is what I'm going to do with
> > my local copy that I use to validate the kernel-secnext builds unless
> > someone has a better patch :)
>
> To fix this I was planning to submit a patch that would change the
> ioctl(FIOCLEX) tests to ioctl(FS_IOC_GETFSLABEL) as that would continue
> to test the xperms.

That one seems to be implemented only by some filesystems. Is there
any more generic one we could use?

>
> >
> > > diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
> > > index 5b6895e4f..030c41652 100644
> > > --- a/security/selinux/hooks.c
> > > +++ b/security/selinux/hooks.c
> > > @@ -3745,6 +3745,13 @@ static int selinux_file_ioctl(struct file
> > > *file, unsigned int cmd,
> > >                                             CAP_OPT_NONE, true);
> > >                 break;
> > >
> > > +       case FIOCLEX:
> > > +       case FIONCLEX:
> > > +               /* Must always succeed if polcap set, else default:
> > > */
> > > +               if (selinux_policycap_ioctl_skip_cloexec())
> > > +                       break;
> > > +               fallthrough;
> > > +
> >
> > The break/fallthrough looks like it might be a little more fragile
> > than necessary, how about something like this:
> >
> >   case FIOCLEX:
> >   case FIONCLEX:
> >     if (!selinux_policycap_ioctl_skip_cloexec())
> >       error = ioctl_has_perm(cred, file, FILE__IOCTL, (u16) cmd);
> >       break;
> >
> > Yes, it does duplicate the default ioctl_has_perm() call, but since
> > we
> > are effectively deprecating this and locking the FIOCLEX/FIONCLEX
> > behavior with this policy capability it seems okay to me (and
> > preferable to relying on the fallthrough).
> >
> > Thoughts?
>
> Yes I did ponder this and in my first attempt I had this before the
> switch():
>
>         /* Must always succeed if polcap set */
>         if (selinux_policycap_ioctl_skip_cloexec() &&
>             (cmd == FIOCLEX || cmd == FIONCLEX))
>                 return 0;
>
>         switch (cmd) {
>         case FIONREAD:
>         case FIBMAP:
>
> but changed to within the switch(), anyway I'm happy to resubmit a
> patch either way.

I agree with Paul's suggestion. Better to duplicate the simple call
than to complicate the code flow.
Richard Haines Feb. 23, 2022, 12:43 p.m. UTC | #4
On Wed, 2022-02-23 at 13:12 +0100, Ondrej Mosnacek wrote:
> On Wed, Feb 23, 2022 at 12:58 PM Richard Haines
> <richard_c_haines@btinternet.com> wrote:
> > On Tue, 2022-02-22 at 18:28 -0500, Paul Moore wrote:
> > > On Mon, Feb 21, 2022 at 8:15 AM Richard Haines
> > > <richard_c_haines@btinternet.com> wrote:
> > > > 
> > > > These ioctls are equivalent to fcntl(fd, F_SETFD, flags), which
> > > > SELinux
> > > > always allows too.  Furthermore, a failed FIOCLEX could result
> > > > in a
> > > > file
> > > > descriptor being leaked to a process that should not have
> > > > access to
> > > > it.
> > > > 
> > > > As this patch removes access controls, a policy capability
> > > > needs to
> > > > be
> > > > enabled in policy to always allow these ioctls.
> > > > 
> > > > Based-on-patch-by: Demi Marie Obenour <demiobenour@gmail.com>
> > > > Signed-off-by: Richard Haines <richard_c_haines@btinternet.com>
> > > > ---
> > > > V2 Change: Control via a policy capability. See this thread for
> > > > discussion:
> > > > https://lore.kernel.org/selinux/CAHC9VhQEPxYP_KU56gAGNHKQaxucY8gSsHiUB42PVgADBAccRQ@mail.gmail.com/T/#t
> > > > 
> > > > With this patch and the polcap enabled, the selinux-testsuite
> > > > will
> > > > fail:
> > > > ioctl/test at line 47 - Will need a fix.
> > > > 
> > > >  security/selinux/hooks.c                   | 7 +++++++
> > > >  security/selinux/include/policycap.h       | 1 +
> > > >  security/selinux/include/policycap_names.h | 3 ++-
> > > >  security/selinux/include/security.h        | 7 +++++++
> > > >  4 files changed, 17 insertions(+), 1 deletion(-)
> > > 
> > > Thanks Richard for putting together the v2 of this patch.
> > > 
> > > As far as the test is concerned, it seems like the quick-n-dirty
> > > fix
> > > is to simply remove the ioctl(FIOCLEX) test in test_noioctl.c; is
> > > everyone okay with that?  At least that is what I'm going to do
> > > with
> > > my local copy that I use to validate the kernel-secnext builds
> > > unless
> > > someone has a better patch :)
> > 
> > To fix this I was planning to submit a patch that would change the
> > ioctl(FIOCLEX) tests to ioctl(FS_IOC_GETFSLABEL) as that would
> > continue
> > to test the xperms.
> 
> That one seems to be implemented only by some filesystems. Is there
> any more generic one we could use?

What about  FS_IOC_GETFLAGS

> 
> > 
> > > 
> > > > diff --git a/security/selinux/hooks.c
> > > > b/security/selinux/hooks.c
> > > > index 5b6895e4f..030c41652 100644
> > > > --- a/security/selinux/hooks.c
> > > > +++ b/security/selinux/hooks.c
> > > > @@ -3745,6 +3745,13 @@ static int selinux_file_ioctl(struct
> > > > file
> > > > *file, unsigned int cmd,
> > > >                                             CAP_OPT_NONE,
> > > > true);
> > > >                 break;
> > > > 
> > > > +       case FIOCLEX:
> > > > +       case FIONCLEX:
> > > > +               /* Must always succeed if polcap set, else
> > > > default:
> > > > */
> > > > +               if (selinux_policycap_ioctl_skip_cloexec())
> > > > +                       break;
> > > > +               fallthrough;
> > > > +
> > > 
> > > The break/fallthrough looks like it might be a little more
> > > fragile
> > > than necessary, how about something like this:
> > > 
> > >   case FIOCLEX:
> > >   case FIONCLEX:
> > >     if (!selinux_policycap_ioctl_skip_cloexec())
> > >       error = ioctl_has_perm(cred, file, FILE__IOCTL, (u16) cmd);
> > >       break;
> > > 
> > > Yes, it does duplicate the default ioctl_has_perm() call, but
> > > since
> > > we
> > > are effectively deprecating this and locking the FIOCLEX/FIONCLEX
> > > behavior with this policy capability it seems okay to me (and
> > > preferable to relying on the fallthrough).
> > > 
> > > Thoughts?
> > 
> > Yes I did ponder this and in my first attempt I had this before the
> > switch():
> > 
> >         /* Must always succeed if polcap set */
> >         if (selinux_policycap_ioctl_skip_cloexec() &&
> >             (cmd == FIOCLEX || cmd == FIONCLEX))
> >                 return 0;
> > 
> >         switch (cmd) {
> >         case FIONREAD:
> >         case FIBMAP:
> > 
> > but changed to within the switch(), anyway I'm happy to resubmit a
> > patch either way.
> 
> I agree with Paul's suggestion. Better to duplicate the simple call
> than to complicate the code flow.

Okay will use Paul's.

>
Paul Moore Feb. 23, 2022, 9:36 p.m. UTC | #5
On Wed, Feb 23, 2022 at 7:43 AM Richard Haines
<richard_c_haines@btinternet.com> wrote:
> On Wed, 2022-02-23 at 13:12 +0100, Ondrej Mosnacek wrote:
> > On Wed, Feb 23, 2022 at 12:58 PM Richard Haines
> > <richard_c_haines@btinternet.com> wrote:
> > > On Tue, 2022-02-22 at 18:28 -0500, Paul Moore wrote:
> > > > On Mon, Feb 21, 2022 at 8:15 AM Richard Haines
> > > > <richard_c_haines@btinternet.com> wrote:
> > > > >
> > > > > These ioctls are equivalent to fcntl(fd, F_SETFD, flags), which
> > > > > SELinux
> > > > > always allows too.  Furthermore, a failed FIOCLEX could result
> > > > > in a
> > > > > file
> > > > > descriptor being leaked to a process that should not have
> > > > > access to
> > > > > it.
> > > > >
> > > > > As this patch removes access controls, a policy capability
> > > > > needs to
> > > > > be
> > > > > enabled in policy to always allow these ioctls.
> > > > >
> > > > > Based-on-patch-by: Demi Marie Obenour <demiobenour@gmail.com>
> > > > > Signed-off-by: Richard Haines <richard_c_haines@btinternet.com>
> > > > > ---
> > > > > V2 Change: Control via a policy capability. See this thread for
> > > > > discussion:
> > > > > https://lore.kernel.org/selinux/CAHC9VhQEPxYP_KU56gAGNHKQaxucY8gSsHiUB42PVgADBAccRQ@mail.gmail.com/T/#t
> > > > >
> > > > > With this patch and the polcap enabled, the selinux-testsuite
> > > > > will
> > > > > fail:
> > > > > ioctl/test at line 47 - Will need a fix.
> > > > >
> > > > >  security/selinux/hooks.c                   | 7 +++++++
> > > > >  security/selinux/include/policycap.h       | 1 +
> > > > >  security/selinux/include/policycap_names.h | 3 ++-
> > > > >  security/selinux/include/security.h        | 7 +++++++
> > > > >  4 files changed, 17 insertions(+), 1 deletion(-)
> > > >
> > > > Thanks Richard for putting together the v2 of this patch.
> > > >
> > > > As far as the test is concerned, it seems like the quick-n-dirty
> > > > fix
> > > > is to simply remove the ioctl(FIOCLEX) test in test_noioctl.c; is
> > > > everyone okay with that?  At least that is what I'm going to do
> > > > with
> > > > my local copy that I use to validate the kernel-secnext builds
> > > > unless
> > > > someone has a better patch :)
> > >
> > > To fix this I was planning to submit a patch that would change the
> > > ioctl(FIOCLEX) tests to ioctl(FS_IOC_GETFSLABEL) as that would
> > > continue
> > > to test the xperms.
> >
> > That one seems to be implemented only by some filesystems. Is there
> > any more generic one we could use?
>
> What about  FS_IOC_GETFLAGS

Unless I'm mistaken, FIGETBSZ should be largely fs independent.

> > > > > diff --git a/security/selinux/hooks.c
> > > > > b/security/selinux/hooks.c
> > > > > index 5b6895e4f..030c41652 100644
> > > > > --- a/security/selinux/hooks.c
> > > > > +++ b/security/selinux/hooks.c
> > > > > @@ -3745,6 +3745,13 @@ static int selinux_file_ioctl(struct
> > > > > file
> > > > > *file, unsigned int cmd,
> > > > >                                             CAP_OPT_NONE,
> > > > > true);
> > > > >                 break;
> > > > >
> > > > > +       case FIOCLEX:
> > > > > +       case FIONCLEX:
> > > > > +               /* Must always succeed if polcap set, else
> > > > > default:
> > > > > */
> > > > > +               if (selinux_policycap_ioctl_skip_cloexec())
> > > > > +                       break;
> > > > > +               fallthrough;
> > > > > +
> > > >
> > > > The break/fallthrough looks like it might be a little more
> > > > fragile
> > > > than necessary, how about something like this:
> > > >
> > > >   case FIOCLEX:
> > > >   case FIONCLEX:
> > > >     if (!selinux_policycap_ioctl_skip_cloexec())
> > > >       error = ioctl_has_perm(cred, file, FILE__IOCTL, (u16) cmd);
> > > >       break;
> > > >
> > > > Yes, it does duplicate the default ioctl_has_perm() call, but
> > > > since
> > > > we
> > > > are effectively deprecating this and locking the FIOCLEX/FIONCLEX
> > > > behavior with this policy capability it seems okay to me (and
> > > > preferable to relying on the fallthrough).
> > > >
> > > > Thoughts?
> > >
> > > Yes I did ponder this and in my first attempt I had this before the
> > > switch():
> > >
> > >         /* Must always succeed if polcap set */
> > >         if (selinux_policycap_ioctl_skip_cloexec() &&
> > >             (cmd == FIOCLEX || cmd == FIONCLEX))
> > >                 return 0;
> > >
> > >         switch (cmd) {
> > >         case FIONREAD:
> > >         case FIBMAP:
> > >
> > > but changed to within the switch(), anyway I'm happy to resubmit a
> > > patch either way.
> >
> > I agree with Paul's suggestion. Better to duplicate the simple call
> > than to complicate the code flow.
>
> Okay will use Paul's.

Thanks guys.
Paul Moore Feb. 23, 2022, 9:43 p.m. UTC | #6
On Wed, Feb 23, 2022 at 4:36 PM Paul Moore <paul@paul-moore.com> wrote:
> On Wed, Feb 23, 2022 at 7:43 AM Richard Haines
> <richard_c_haines@btinternet.com> wrote:
> > On Wed, 2022-02-23 at 13:12 +0100, Ondrej Mosnacek wrote:
> > > On Wed, Feb 23, 2022 at 12:58 PM Richard Haines
> > > <richard_c_haines@btinternet.com> wrote:
> > > > On Tue, 2022-02-22 at 18:28 -0500, Paul Moore wrote:
> > > > > On Mon, Feb 21, 2022 at 8:15 AM Richard Haines
> > > > > <richard_c_haines@btinternet.com> wrote:
> > > > > >
> > > > > > These ioctls are equivalent to fcntl(fd, F_SETFD, flags), which
> > > > > > SELinux
> > > > > > always allows too.  Furthermore, a failed FIOCLEX could result
> > > > > > in a
> > > > > > file
> > > > > > descriptor being leaked to a process that should not have
> > > > > > access to
> > > > > > it.
> > > > > >
> > > > > > As this patch removes access controls, a policy capability
> > > > > > needs to
> > > > > > be
> > > > > > enabled in policy to always allow these ioctls.
> > > > > >
> > > > > > Based-on-patch-by: Demi Marie Obenour <demiobenour@gmail.com>
> > > > > > Signed-off-by: Richard Haines <richard_c_haines@btinternet.com>
> > > > > > ---
> > > > > > V2 Change: Control via a policy capability. See this thread for
> > > > > > discussion:
> > > > > > https://lore.kernel.org/selinux/CAHC9VhQEPxYP_KU56gAGNHKQaxucY8gSsHiUB42PVgADBAccRQ@mail.gmail.com/T/#t
> > > > > >
> > > > > > With this patch and the polcap enabled, the selinux-testsuite
> > > > > > will
> > > > > > fail:
> > > > > > ioctl/test at line 47 - Will need a fix.
> > > > > >
> > > > > >  security/selinux/hooks.c                   | 7 +++++++
> > > > > >  security/selinux/include/policycap.h       | 1 +
> > > > > >  security/selinux/include/policycap_names.h | 3 ++-
> > > > > >  security/selinux/include/security.h        | 7 +++++++
> > > > > >  4 files changed, 17 insertions(+), 1 deletion(-)
> > > > >
> > > > > Thanks Richard for putting together the v2 of this patch.
> > > > >
> > > > > As far as the test is concerned, it seems like the quick-n-dirty
> > > > > fix
> > > > > is to simply remove the ioctl(FIOCLEX) test in test_noioctl.c; is
> > > > > everyone okay with that?  At least that is what I'm going to do
> > > > > with
> > > > > my local copy that I use to validate the kernel-secnext builds
> > > > > unless
> > > > > someone has a better patch :)
> > > >
> > > > To fix this I was planning to submit a patch that would change the
> > > > ioctl(FIOCLEX) tests to ioctl(FS_IOC_GETFSLABEL) as that would
> > > > continue
> > > > to test the xperms.
> > >
> > > That one seems to be implemented only by some filesystems. Is there
> > > any more generic one we could use?
> >
> > What about  FS_IOC_GETFLAGS
>
> Unless I'm mistaken, FIGETBSZ should be largely fs independent.

Bah, nevermind, FIGETBSZ ends up in a FILE__GETATTR check.
FS_IOC_GETFLAGS has the same problem.

How about FIOQSIZE?
diff mbox series

Patch

diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
index 5b6895e4f..030c41652 100644
--- a/security/selinux/hooks.c
+++ b/security/selinux/hooks.c
@@ -3745,6 +3745,13 @@  static int selinux_file_ioctl(struct file *file, unsigned int cmd,
 					    CAP_OPT_NONE, true);
 		break;
 
+	case FIOCLEX:
+	case FIONCLEX:
+		/* Must always succeed if polcap set, else default: */
+		if (selinux_policycap_ioctl_skip_cloexec())
+			break;
+		fallthrough;
+
 	/* default case assumes that the command will go
 	 * to the file's ioctl() function.
 	 */
diff --git a/security/selinux/include/policycap.h b/security/selinux/include/policycap.h
index 2ec038efb..44d73dc32 100644
--- a/security/selinux/include/policycap.h
+++ b/security/selinux/include/policycap.h
@@ -11,6 +11,7 @@  enum {
 	POLICYDB_CAPABILITY_CGROUPSECLABEL,
 	POLICYDB_CAPABILITY_NNP_NOSUID_TRANSITION,
 	POLICYDB_CAPABILITY_GENFS_SECLABEL_SYMLINKS,
+	POLICYDB_CAPABILITY_IOCTL_CLOEXEC,
 	__POLICYDB_CAPABILITY_MAX
 };
 #define POLICYDB_CAPABILITY_MAX (__POLICYDB_CAPABILITY_MAX - 1)
diff --git a/security/selinux/include/policycap_names.h b/security/selinux/include/policycap_names.h
index b89289f09..ebd64afe1 100644
--- a/security/selinux/include/policycap_names.h
+++ b/security/selinux/include/policycap_names.h
@@ -12,7 +12,8 @@  const char *selinux_policycap_names[__POLICYDB_CAPABILITY_MAX] = {
 	"always_check_network",
 	"cgroup_seclabel",
 	"nnp_nosuid_transition",
-	"genfs_seclabel_symlinks"
+	"genfs_seclabel_symlinks",
+	"ioctl_skip_cloexec"
 };
 
 #endif /* _SELINUX_POLICYCAP_NAMES_H_ */
diff --git a/security/selinux/include/security.h b/security/selinux/include/security.h
index ac0ece013..8a789c22b 100644
--- a/security/selinux/include/security.h
+++ b/security/selinux/include/security.h
@@ -219,6 +219,13 @@  static inline bool selinux_policycap_genfs_seclabel_symlinks(void)
 	return READ_ONCE(state->policycap[POLICYDB_CAPABILITY_GENFS_SECLABEL_SYMLINKS]);
 }
 
+static inline bool selinux_policycap_ioctl_skip_cloexec(void)
+{
+	struct selinux_state *state = &selinux_state;
+
+	return READ_ONCE(state->policycap[POLICYDB_CAPABILITY_IOCTL_CLOEXEC]);
+}
+
 struct selinux_policy_convert_data;
 
 struct selinux_load_state {