Skip to contentSkip to conversation

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

PATCH Superseded Gen Zhang 2 participants 3 messages May 31, 2019 – Jun 1, 2019

Subsystems (4)

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Gen Zhang

author
#0 lore raw

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

In selinux_sb_eat_lsm_opts(), 'arg' is allocated by kmemdup_nul(). It returns NULL when fails. So 'arg' should be checked.

Fixes 99dbbb593fe6 ("selinux: rewrite selinux_sb_eat_lsm_opts()")

---

security/selinux/hooks.c +2 -0
index 3ec702c..5a9e959 100644
@@ -2635,6 +2635,8 @@ static int selinux_sb_eat_lsm_opts(char *options, void **mnt_opts)
 						*q++ = c;
 				}
 				arg = kmemdup_nul(arg, q - arg, GFP_KERNEL);
+				if (!arg)
+					return -ENOMEM;
 			}
 			rc = selinux_add_opt(token, arg, mnt_opts);
 			if (unlikely(rc)) {
maintainer
#1 lore raw
Quoted 7 lines · Gen Zhang

In selinux_sb_eat_lsm_opts(), 'arg' is allocated by kmemdup_nul(). It returns NULL when fails. So 'arg' should be checked.

Fixes 99dbbb593fe6 ("selinux: rewrite selinux_sb_eat_lsm_opts()")

One quick note about the subject line, instead of using "hooks:" you should use "selinux:" since this is specific to SELinux. If the patch did apply to the LSM framework as a whole, I would suggest using "lsm:" instead of "hooks:" as "hooks" is too ambiguous of a prefix.

security/selinux/hooks.c static int selinux_sb_eat_lsm_opts(char *options, void **mnt_opts) · line 2639

Quoted 10 lines
security/selinux/hooks.c +2 -0
index 3ec702c..5a9e959 100644
@@ -2635,6 +2635,8 @@ static int selinux_sb_eat_lsm_opts(char *options, void **mnt_opts)
                                                *q++ = c;
                                }
                                arg = kmemdup_nul(arg, q - arg, GFP_KERNEL);
+                               if (!arg)
+                                       return -ENOMEM;

It seems like we should also check for, and potentially free *mnt_opts as the selinux_add_opt() error handling does just below this change, yes? If that is the case we might want to move that error handling code to the bottom of the function and jump there on error.

Quoted 3 lines
security/selinux/hooks.c +0 -0
                        }
                        rc = selinux_add_opt(token, arg, mnt_opts);
                        if (unlikely(rc)) {
Signature · 2 lines
paul moore
www.paul-moore.com
author
#2 lore raw
Quoted 30 lines · Paul Moore
Quoted 7 lines · Gen Zhang

In selinux_sb_eat_lsm_opts(), 'arg' is allocated by kmemdup_nul(). It returns NULL when fails. So 'arg' should be checked.

Fixes 99dbbb593fe6 ("selinux: rewrite selinux_sb_eat_lsm_opts()")

One quick note about the subject line, instead of using "hooks:" you should use "selinux:" since this is specific to SELinux. If the patch did apply to the LSM framework as a whole, I would suggest using "lsm:" instead of "hooks:" as "hooks" is too ambiguous of a prefix.

security/selinux/hooks.c static int selinux_sb_eat_lsm_opts(char *options, void **mnt_opts) · line 2639

Quoted 10 lines
security/selinux/hooks.c +2 -0
index 3ec702c..5a9e959 100644
@@ -2635,6 +2635,8 @@ static int selinux_sb_eat_lsm_opts(char *options, void **mnt_opts)
                                                *q++ = c;
                                }
                                arg = kmemdup_nul(arg, q - arg, GFP_KERNEL);
+                               if (!arg)
+                                       return -ENOMEM;

It seems like we should also check for, and potentially free *mnt_opts as the selinux_add_opt() error handling does just below this change, yes? If that is the case we might want to move that error handling code to the bottom of the function and jump there on error.

Quoted 7 lines
Quoted 3 lines
security/selinux/hooks.c +0 -0
                        }
                        rc = selinux_add_opt(token, arg, mnt_opts);
                        if (unlikely(rc)) {
Signature · 2 lines
paul moore
www.paul-moore.com

Yes, I agree with that. And I will work on this to resubmit.

Thanks
Gen