linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] hooks: fix a missing-check bug in selinux_add_mnt_opt()
@ 2019-05-30  8:06 Gen Zhang
  2019-05-30  8:30 ` Ondrej Mosnacek
  2019-05-30  9:11 ` [PATCH] " Sergei Shtylyov
  0 siblings, 2 replies; 9+ messages in thread
From: Gen Zhang @ 2019-05-30  8:06 UTC (permalink / raw)
  To: paul, sds, eparis, ccross; +Cc: selinux, linux-kernel, netdev

In selinux_add_mnt_opt(), 'val' is allcoted by kmemdup_nul(). It returns
NULL when fails. So 'val' should be checked.

Signed-off-by: Gen Zhang <blackgod016574@gmail.com>
---
diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
index 3ec702c..4797c63 100644
--- a/security/selinux/hooks.c
+++ b/security/selinux/hooks.c
@@ -1052,8 +1052,11 @@ static int selinux_add_mnt_opt(const char *option, const char *val, int len,
 	if (token == Opt_error)
 		return -EINVAL;
 
-	if (token != Opt_seclabel)
-		val = kmemdup_nul(val, len, GFP_KERNEL);
+	if (token != Opt_seclabel) {
+			val = kmemdup_nul(val, len, GFP_KERNEL);
+			if (!val)
+				return -ENOMEM;
+	}
 	rc = selinux_add_opt(token, val, mnt_opts);
 	if (unlikely(rc)) {
 		kfree(val);

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

* Re: [PATCH] hooks: fix a missing-check bug in selinux_add_mnt_opt()
  2019-05-30  8:06 [PATCH] hooks: fix a missing-check bug in selinux_add_mnt_opt() Gen Zhang
@ 2019-05-30  8:30 ` Ondrej Mosnacek
  2019-05-30  8:54   ` [PATCH v2] " Gen Zhang
  2019-05-30  9:11 ` [PATCH] " Sergei Shtylyov
  1 sibling, 1 reply; 9+ messages in thread
From: Ondrej Mosnacek @ 2019-05-30  8:30 UTC (permalink / raw)
  To: Gen Zhang
  Cc: Paul Moore, Stephen Smalley, Eric Paris, ccross, selinux,
	Linux kernel mailing list, netdev

On Thu, May 30, 2019 at 10:06 AM Gen Zhang <blackgod016574@gmail.com> wrote:
> In selinux_add_mnt_opt(), 'val' is allcoted by kmemdup_nul(). It returns
> NULL when fails. So 'val' should be checked.
>
> Signed-off-by: Gen Zhang <blackgod016574@gmail.com>

Please add a Fixes tag here, too:

Fixes: 757cbe597fe8 ("LSM: new method: ->sb_add_mnt_opt()")

> ---
> diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
> index 3ec702c..4797c63 100644
> --- a/security/selinux/hooks.c
> +++ b/security/selinux/hooks.c
> @@ -1052,8 +1052,11 @@ static int selinux_add_mnt_opt(const char *option, const char *val, int len,
>         if (token == Opt_error)
>                 return -EINVAL;
>
> -       if (token != Opt_seclabel)
> -               val = kmemdup_nul(val, len, GFP_KERNEL);
> +       if (token != Opt_seclabel) {
> +                       val = kmemdup_nul(val, len, GFP_KERNEL);
> +                       if (!val)
> +                               return -ENOMEM;

There is one extra tab character in the above three lines ^^^

> +       }
>         rc = selinux_add_opt(token, val, mnt_opts);
>         if (unlikely(rc)) {
>                 kfree(val);

Thanks,

--
Ondrej Mosnacek <omosnace at redhat dot com>
Software Engineer, Security Technologies
Red Hat, Inc.

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

* [PATCH v2] hooks: fix a missing-check bug in selinux_add_mnt_opt()
  2019-05-30  8:30 ` Ondrej Mosnacek
@ 2019-05-30  8:54   ` Gen Zhang
  2019-05-31 15:55     ` Paul Moore
  0 siblings, 1 reply; 9+ messages in thread
From: Gen Zhang @ 2019-05-30  8:54 UTC (permalink / raw)
  To: Ondrej Mosnacek
  Cc: Paul Moore, Stephen Smalley, Eric Paris, ccross, selinux,
	Linux kernel mailing list, netdev

In selinux_add_mnt_opt(), 'val' is allcoted by kmemdup_nul(). It returns
NULL when fails. So 'val' should be checked.

Signed-off-by: Gen Zhang <blackgod016574@gmail.com>
Fixes: 757cbe597fe8 ("LSM: new method: ->sb_add_mnt_opt()")
---
diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
index 3ec702c..4797c63 100644
--- a/security/selinux/hooks.c
+++ b/security/selinux/hooks.c
@@ -1052,8 +1052,11 @@ static int selinux_add_mnt_opt(const char *option, const char *val, int len,
 	if (token == Opt_error)
 		return -EINVAL;
 
-	if (token != Opt_seclabel)
-		val = kmemdup_nul(val, len, GFP_KERNEL);
+	if (token != Opt_seclabel) {
+			val = kmemdup_nul(val, len, GFP_KERNEL);
+			if (!val)
+				return -ENOMEM;
+	}
 	rc = selinux_add_opt(token, val, mnt_opts);
 	if (unlikely(rc)) {
 		kfree(val);

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

* Re: [PATCH] hooks: fix a missing-check bug in selinux_add_mnt_opt()
  2019-05-30  8:06 [PATCH] hooks: fix a missing-check bug in selinux_add_mnt_opt() Gen Zhang
  2019-05-30  8:30 ` Ondrej Mosnacek
@ 2019-05-30  9:11 ` Sergei Shtylyov
  2019-05-30  9:18   ` Gen Zhang
  1 sibling, 1 reply; 9+ messages in thread
From: Sergei Shtylyov @ 2019-05-30  9:11 UTC (permalink / raw)
  To: Gen Zhang, paul, sds, eparis, ccross; +Cc: selinux, linux-kernel, netdev

Hello!

On 30.05.2019 11:06, Gen Zhang wrote:

> In selinux_add_mnt_opt(), 'val' is allcoted by kmemdup_nul(). It returns

    Allocated?

> NULL when fails. So 'val' should be checked.
> 
> Signed-off-by: Gen Zhang <blackgod016574@gmail.com>
[...]

MBR, Sergei

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

* Re: [PATCH] hooks: fix a missing-check bug in selinux_add_mnt_opt()
  2019-05-30  9:11 ` [PATCH] " Sergei Shtylyov
@ 2019-05-30  9:18   ` Gen Zhang
  2019-05-30  9:22     ` Sergei Shtylyov
  0 siblings, 1 reply; 9+ messages in thread
From: Gen Zhang @ 2019-05-30  9:18 UTC (permalink / raw)
  To: Sergei Shtylyov; +Cc: paul, sds, eparis, ccross, selinux, linux-kernel, netdev

On Thu, May 30, 2019 at 12:11:33PM +0300, Sergei Shtylyov wrote:
> Hello!
> 
> On 30.05.2019 11:06, Gen Zhang wrote:
> 
> >In selinux_add_mnt_opt(), 'val' is allcoted by kmemdup_nul(). It returns
> 
>    Allocated?
Thanks for your reply, Sergei. I used 'allocated' because kmemdup_nul()
does some allocation in its implementation. And its docs descrips: 
"Return: newly allocated copy of @s with NUL-termination or %NULL in 
case of error". I think it is proper to use 'allocated' here. But it 
could be 'assigned', which is better, right?

Thanks
Gen
> 
> >NULL when fails. So 'val' should be checked.
> >
> >Signed-off-by: Gen Zhang <blackgod016574@gmail.com>
> [...]
> 
> MBR, Sergei

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

* Re: [PATCH] hooks: fix a missing-check bug in selinux_add_mnt_opt()
  2019-05-30  9:18   ` Gen Zhang
@ 2019-05-30  9:22     ` Sergei Shtylyov
  2019-05-30  9:24       ` Gen Zhang
  0 siblings, 1 reply; 9+ messages in thread
From: Sergei Shtylyov @ 2019-05-30  9:22 UTC (permalink / raw)
  To: Gen Zhang; +Cc: paul, sds, eparis, ccross, selinux, linux-kernel, netdev

On 30.05.2019 12:18, Gen Zhang wrote:

>> On 30.05.2019 11:06, Gen Zhang wrote:
>>
>>> In selinux_add_mnt_opt(), 'val' is allcoted by kmemdup_nul(). It returns
>>
>>     Allocated?

> Thanks for your reply, Sergei. I used 'allocated' because kmemdup_nul()
> does some allocation in its implementation. And its docs descrips:

    Describes?

> "Return: newly allocated copy of @s with NUL-termination or %NULL in
> case of error". I think it is proper to use 'allocated' here. But it
> could be 'assigned', which is better, right?

    I was only trying to point out the typos in this word. :-)

> Thanks
> Gen
>>
>>> NULL when fails. So 'val' should be checked.
>>>
>>> Signed-off-by: Gen Zhang <blackgod016574@gmail.com>
>> [...]

MBR, Sergei

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

* Re: [PATCH] hooks: fix a missing-check bug in selinux_add_mnt_opt()
  2019-05-30  9:22     ` Sergei Shtylyov
@ 2019-05-30  9:24       ` Gen Zhang
  0 siblings, 0 replies; 9+ messages in thread
From: Gen Zhang @ 2019-05-30  9:24 UTC (permalink / raw)
  To: Sergei Shtylyov; +Cc: paul, sds, eparis, ccross, selinux, linux-kernel, netdev

On Thu, May 30, 2019 at 12:22:15PM +0300, Sergei Shtylyov wrote:
> On 30.05.2019 12:18, Gen Zhang wrote:
> 
> >>On 30.05.2019 11:06, Gen Zhang wrote:
> >>
> >>>In selinux_add_mnt_opt(), 'val' is allcoted by kmemdup_nul(). It returns
> >>
> >>    Allocated?
> 
> >Thanks for your reply, Sergei. I used 'allocated' because kmemdup_nul()
> >does some allocation in its implementation. And its docs descrips:
> 
>    Describes?
> 
> >"Return: newly allocated copy of @s with NUL-termination or %NULL in
> >case of error". I think it is proper to use 'allocated' here. But it
> >could be 'assigned', which is better, right?
> 
>    I was only trying to point out the typos in this word. :-)
> 
> >Thanks
> >Gen
> >>
> >>>NULL when fails. So 'val' should be checked.
> >>>
> >>>Signed-off-by: Gen Zhang <blackgod016574@gmail.com>
> >>[...]
> 
> MBR, Sergei
Well, my mistake. Thanks for your comments, Sergei!

Thanks
Gen

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

* Re: [PATCH v2] hooks: fix a missing-check bug in selinux_add_mnt_opt()
  2019-05-30  8:54   ` [PATCH v2] " Gen Zhang
@ 2019-05-31 15:55     ` Paul Moore
  2019-06-01  1:54       ` Gen Zhang
  0 siblings, 1 reply; 9+ messages in thread
From: Paul Moore @ 2019-05-31 15:55 UTC (permalink / raw)
  To: Gen Zhang
  Cc: Ondrej Mosnacek, Stephen Smalley, Eric Paris, ccross, selinux,
	Linux kernel mailing list, netdev

On Thu, May 30, 2019 at 4:55 AM Gen Zhang <blackgod016574@gmail.com> wrote:
>
> In selinux_add_mnt_opt(), 'val' is allcoted by kmemdup_nul(). It returns
> NULL when fails. So 'val' should be checked.
>
> Signed-off-by: Gen Zhang <blackgod016574@gmail.com>
> Fixes: 757cbe597fe8 ("LSM: new method: ->sb_add_mnt_opt()")

Previous comments regarding "selinux:" instead of "hooks:" apply here as well.

> diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
> index 3ec702c..4797c63 100644
> --- a/security/selinux/hooks.c
> +++ b/security/selinux/hooks.c
> @@ -1052,8 +1052,11 @@ static int selinux_add_mnt_opt(const char *option, const char *val, int len,
>         if (token == Opt_error)
>                 return -EINVAL;
>
> -       if (token != Opt_seclabel)
> -               val = kmemdup_nul(val, len, GFP_KERNEL);
> +       if (token != Opt_seclabel) {
> +                       val = kmemdup_nul(val, len, GFP_KERNEL);
> +                       if (!val)
> +                               return -ENOMEM;

It looks like this code is only ever called by NFS, which will
eventually clean up mnt_opts via security_free_mnt_opts(), but since
the selinux_add_opt() error handler below cleans up mnt_opts it might
be safer to do the same here in case this function is called multiple
times to add multiple options.

> +       }
>         rc = selinux_add_opt(token, val, mnt_opts);
>         if (unlikely(rc)) {
>                 kfree(val);

-- 
paul moore
www.paul-moore.com

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

* Re: [PATCH v2] hooks: fix a missing-check bug in selinux_add_mnt_opt()
  2019-05-31 15:55     ` Paul Moore
@ 2019-06-01  1:54       ` Gen Zhang
  0 siblings, 0 replies; 9+ messages in thread
From: Gen Zhang @ 2019-06-01  1:54 UTC (permalink / raw)
  To: Paul Moore
  Cc: Ondrej Mosnacek, Stephen Smalley, Eric Paris, ccross, selinux,
	Linux kernel mailing list, netdev

On Fri, May 31, 2019 at 11:55:23AM -0400, Paul Moore wrote:
> On Thu, May 30, 2019 at 4:55 AM Gen Zhang <blackgod016574@gmail.com> wrote:
> >
> > In selinux_add_mnt_opt(), 'val' is allcoted by kmemdup_nul(). It returns
> > NULL when fails. So 'val' should be checked.
> >
> > Signed-off-by: Gen Zhang <blackgod016574@gmail.com>
> > Fixes: 757cbe597fe8 ("LSM: new method: ->sb_add_mnt_opt()")
> 
> Previous comments regarding "selinux:" instead of "hooks:" apply here as well.
> 
Thanks for your comments, Paul. I will make some changes.
> > diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
> > index 3ec702c..4797c63 100644
> > --- a/security/selinux/hooks.c
> > +++ b/security/selinux/hooks.c
> > @@ -1052,8 +1052,11 @@ static int selinux_add_mnt_opt(const char *option, const char *val, int len,
> >         if (token == Opt_error)
> >                 return -EINVAL;
> >
> > -       if (token != Opt_seclabel)
> > -               val = kmemdup_nul(val, len, GFP_KERNEL);
> > +       if (token != Opt_seclabel) {
> > +                       val = kmemdup_nul(val, len, GFP_KERNEL);
> > +                       if (!val)
> > +                               return -ENOMEM;
> 
> It looks like this code is only ever called by NFS, which will
> eventually clean up mnt_opts via security_free_mnt_opts(), but since
> the selinux_add_opt() error handler below cleans up mnt_opts it might
> be safer to do the same here in case this function is called multiple
> times to add multiple options.
> 
> > +       }
> >         rc = selinux_add_opt(token, val, mnt_opts);
> >         if (unlikely(rc)) {
> >                 kfree(val);
> 
> -- 
> paul moore
> www.paul-moore.com
Thanks
Gen

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

end of thread, other threads:[~2019-06-01  1:55 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-05-30  8:06 [PATCH] hooks: fix a missing-check bug in selinux_add_mnt_opt() Gen Zhang
2019-05-30  8:30 ` Ondrej Mosnacek
2019-05-30  8:54   ` [PATCH v2] " Gen Zhang
2019-05-31 15:55     ` Paul Moore
2019-06-01  1:54       ` Gen Zhang
2019-05-30  9:11 ` [PATCH] " Sergei Shtylyov
2019-05-30  9:18   ` Gen Zhang
2019-05-30  9:22     ` Sergei Shtylyov
2019-05-30  9:24       ` Gen Zhang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).