Skip to contentSkip to conversation

hooks: fix a missing-check bug in selinux_add_mnt_opt()

PATCH Superseded Gen Zhang 4 participants 9 messages May 30, 2019 – Jun 1, 2019

Subsystems (3)

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Gen Zhang

author
#0 lore raw

PATCH 1/1 hooks: fix a missing-check bug in selinux_add_mnt_opt()

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

---

security/selinux/hooks.c +5 -2
index 3ec702c..4797c63 100644
@@ -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);
#1 lore raw
Quoted 4 lines · Gen Zhang

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

Please add a Fixes tag here, too:

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

---

security/selinux/hooks.c +0 -0
index 3ec702c..4797c63 100644

security/selinux/hooks.c static int selinux_add_mnt_opt(const char *option, const char *val, int len, · line 1058

Quoted 9 lines
security/selinux/hooks.c +4 -2
@@ -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 ^^^

security/selinux/hooks.c static int selinux_add_mnt_opt(const char *option, const char *val, int len, · line 1062

Quoted 4 lines
security/selinux/hooks.c +1 -0
+       }
        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.

author
#2 lore raw

PATCH v2 1/1 hooks: fix a missing-check bug in selinux_add_mnt_opt()

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

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

---

security/selinux/hooks.c +5 -2
index 3ec702c..4797c63 100644
@@ -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);
maintainer
#3 lore raw
Quoted 6 lines · Gen Zhang

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

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

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

Quoted 5 lines
security/selinux/hooks.c +0 -0
index 3ec702c..4797c63 100644

security/selinux/hooks.c static int selinux_add_mnt_opt(const char *option, const char *val, int len, · line 1058

Quoted 9 lines
security/selinux/hooks.c +4 -2
@@ -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.

Quoted 4 lines
security/selinux/hooks.c +1 -0
+       }
        rc = selinux_add_opt(token, val, mnt_opts);
        if (unlikely(rc)) {
                kfree(val);
Signature · 2 lines
paul moore
www.paul-moore.com
author
#4 lore raw
Quoted 10 lines · Paul Moore
Quoted 6 lines · Gen Zhang

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

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.

Quoted 21 lines
Quoted 5 lines
security/selinux/hooks.c +0 -0
index 3ec702c..4797c63 100644

security/selinux/hooks.c static int selinux_add_mnt_opt(const char *option, const char *val, int len, · line 1058

Quoted 9 lines
security/selinux/hooks.c +4 -2
@@ -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.

Quoted 8 lines
Quoted 4 lines
security/selinux/hooks.c +1 -0
+       }
        rc = selinux_add_opt(token, val, mnt_opts);
        if (unlikely(rc)) {
                kfree(val);
Signature · 2 lines
paul moore
www.paul-moore.com

Thanks
Gen

author
#6 lore raw
Quoted 7 lines · Sergei Shtylyov

Hello!

Quoted 1 line · Gen Zhang

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

Quoted 7 lines
Quoted 4 lines

NULL when fails. So 'val' should be checked.

[...]

MBR, Sergei

#7 lore raw
Quoted 8 lines · Gen Zhang
Quoted 5 lines · Gen Zhang
Quoted 2 lines

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?

Quoted 3 lines

"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. :-)

Quoted 7 lines

Thanks
Gen

Quoted 5 lines
Quoted 4 lines

NULL when fails. So 'val' should be checked.

[...]

MBR, Sergei

author
#8 lore raw
Quoted 22 lines · Sergei Shtylyov
Quoted 8 lines · Gen Zhang
Quoted 5 lines · Gen Zhang
Quoted 2 lines

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?

Quoted 3 lines

"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. :-)

Quoted 3 lines

Thanks
Gen

Quoted 1 line
Quoted 6 lines
Quoted 4 lines
Quoted 4 lines
Quoted 3 lines

NULL when fails. So 'val' should be checked.

[...]

MBR, Sergei

Well, my mistake. Thanks for your comments, Sergei!

Thanks
Gen