diff mbox series

lsm:fix a missing-check bug in smack_sb_eat_lsm_opts()

Message ID 20210416095303.530-1-hbut_tan@163.com (mailing list archive)
State New, archived
Headers show
Series lsm:fix a missing-check bug in smack_sb_eat_lsm_opts() | expand

Commit Message

Zhongjun Tan April 16, 2021, 9:53 a.m. UTC
From: Zhongjun Tan <tanzhongjun@yulong.com>

In smack_sb_eat_lsm_opts(), 'arg' is allocated by kmemdup_nul().
It returns NULL when fails. So 'arg' should be checked. And 'mnt_opts'
should be freed when error.

Signed-off-by: Zhongjun Tan <tanzhongjun@yulong.com>
---
 security/smack/smack_lsm.c | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)

Comments

Al Viro April 16, 2021, 1:36 p.m. UTC | #1
On Fri, Apr 16, 2021 at 05:53:03PM +0800,  Zhongjun Tan wrote:

> @@ -710,13 +711,14 @@ static int smack_sb_eat_lsm_opts(char *options, void **mnt_opts)
>  		token = match_opt_prefix(from, len, &arg);
>  		if (token != Opt_error) {
>  			arg = kmemdup_nul(arg, from + len - arg, GFP_KERNEL);
> +			if (!arg) {
> +				rc = -ENOMEM;
> +				goto free_mnt_opts;
>  			rc = smack_add_opt(token, arg, mnt_opts);

			if (arg)
	  			rc = smack_add_opt(token, arg, mnt_opts);
			else
				rc = -ENOMEM;

and no other changes are needed anywhere...
Zhongjun Tan April 19, 2021, 2:58 a.m. UTC | #2
On Fri, 16 Apr 2021 13:36:01 +0000
Al Viro <viro@zeniv.linux.org.uk> wrote:

> On Fri, Apr 16, 2021 at 05:53:03PM +0800,  Zhongjun Tan wrote:
> 
> > @@ -710,13 +711,14 @@ static int smack_sb_eat_lsm_opts(char
> > *options, void **mnt_opts) token = match_opt_prefix(from, len,
> > &arg); if (token != Opt_error) {
> >  			arg = kmemdup_nul(arg, from + len - arg,
> > GFP_KERNEL);
> > +			if (!arg) {
> > +				rc = -ENOMEM;
> > +				goto free_mnt_opts;
> >  			rc = smack_add_opt(token, arg, mnt_opts);  
> 
> 			if (arg)
> 	  			rc = smack_add_opt(token, arg,
> mnt_opts); else
> 				rc = -ENOMEM;
> 
> and no other changes are needed anywhere...

update patch v3 , just four codes and no other changes are needed.
diff mbox series

Patch

diff --git a/security/smack/smack_lsm.c b/security/smack/smack_lsm.c
index 223a6da..0d5439f 100644
--- a/security/smack/smack_lsm.c
+++ b/security/smack/smack_lsm.c
@@ -696,10 +696,11 @@  static int smack_sb_eat_lsm_opts(char *options, void **mnt_opts)
 {
 	char *from = options, *to = options;
 	bool first = true;
+	int rc;
 
 	while (1) {
 		char *next = strchr(from, ',');
-		int token, len, rc;
+		int token, len;
 		char *arg = NULL;
 
 		if (next)
@@ -710,13 +711,14 @@  static int smack_sb_eat_lsm_opts(char *options, void **mnt_opts)
 		token = match_opt_prefix(from, len, &arg);
 		if (token != Opt_error) {
 			arg = kmemdup_nul(arg, from + len - arg, GFP_KERNEL);
+			if (!arg) {
+				rc = -ENOMEM;
+				goto free_mnt_opts;
 			rc = smack_add_opt(token, arg, mnt_opts);
+			}
 			if (unlikely(rc)) {
 				kfree(arg);
-				if (*mnt_opts)
-					smack_free_mnt_opts(*mnt_opts);
-				*mnt_opts = NULL;
-				return rc;
+				goto free_mnt_opts;
 			}
 		} else {
 			if (!first) {	// copy with preceding comma
@@ -734,6 +736,13 @@  static int smack_sb_eat_lsm_opts(char *options, void **mnt_opts)
 	}
 	*to = '\0';
 	return 0;
+
+free_mnt_opts:
+	if (*mnt_opts) {
+		smack_free_mnt_opts(*mnt_opts);
+		*mnt_opts = NULL;
+	}
+	return rc;
 }
 
 /**