[PATCH] fs: smb: client: Add missing check for kstrdup()

Haoxiang Li posted 1 patch 1 year, 5 months ago
fs/smb/client/fs_context.c | 2 ++
1 file changed, 2 insertions(+)
[PATCH] fs: smb: client: Add missing check for kstrdup()
Posted by Haoxiang Li 1 year, 5 months ago
Add check for kstrdup() in smb3_reconfigure in order to guarantee
the success of allocation.

Fixes: c1eb537bf456 ("cifs: allow changing password during remount")
Signed-off-by: Haoxiang Li <make24@iscas.ac.cn>
---
 fs/smb/client/fs_context.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/fs/smb/client/fs_context.c b/fs/smb/client/fs_context.c
index 3bbac925d076..8253b615b8ce 100644
--- a/fs/smb/client/fs_context.c
+++ b/fs/smb/client/fs_context.c
@@ -920,6 +920,8 @@ static int smb3_reconfigure(struct fs_context *fc)
 		ses->password = kstrdup(ctx->password, GFP_KERNEL);
 		kfree_sensitive(ses->password2);
 		ses->password2 = kstrdup(ctx->password2, GFP_KERNEL);
+		if (!ses->password || !ses->password2)
+			return ERR_PTR(rc);
 	}
 	STEAL_STRING(cifs_sb, ctx, domainname);
 	STEAL_STRING(cifs_sb, ctx, nodename);
-- 
2.25.1
Re: [PATCH] fs: smb: client: Add missing check for kstrdup()
Posted by Markus Elfring 1 year, 5 months ago
> Add check for kstrdup() in smb3_reconfigure in order to guarantee

      checks?             calls?             ()


> the success of allocation.

I suggest to take further patch/code review concerns better into account.


…
> Signed-off-by: Haoxiang Li <make24@iscas.ac.cn>

Will requirements be reconsidered once more for the Developer's Certificate of Origin?
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/submitting-patches.rst?h=v6.10-rc6#n398


How do you think about to use a summary phrase like “Complete error handling
in smb3_reconfigure()”?


…
> +++ b/fs/smb/client/fs_context.c
> @@ -920,6 +920,8 @@ static int smb3_reconfigure(struct fs_context *fc)
>  		ses->password = kstrdup(ctx->password, GFP_KERNEL);
>  		kfree_sensitive(ses->password2);
>  		ses->password2 = kstrdup(ctx->password2, GFP_KERNEL);
> +		if (!ses->password || !ses->password2)
> +			return ERR_PTR(rc);
>  	}
…

How do you think about to avoid also a memory leak here?

Regards,
Markus
[PATCH v2] fs_context.c: smb3_reconfigure: Handle kstrdup failures for passwords
Posted by Henrique Carvalho 1 year, 1 month ago
In smb3_reconfigure(), after duplicating ctx->password and
ctx->password2 with kstrdup(), we need to check for allocation
failures.

If ses->password allocation fails, return -ENOMEM.
If ses->password2 allocation fails, free ses->password, set it
to NULL, and return -ENOMEM.

Fixes: c1eb537bf456 ("cifs: allow changing password during remount")
Signed-off-by: Haoxiang Li <make24@iscas.ac.cn>
Signed-off-by: Henrique Carvalho <henrique.carvalho@suse.com>
---
V1 -> V2: Decoupled checks for ses->password and ses->password2. Ensured
ses->password is freed and set to NULL if ses->password2 allocation
fails. Corrected return value. Improved commit message.

 fs/smb/client/fs_context.c | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/fs/smb/client/fs_context.c b/fs/smb/client/fs_context.c
index 28c4e576d460a..5c5a52019efad 100644
--- a/fs/smb/client/fs_context.c
+++ b/fs/smb/client/fs_context.c
@@ -920,8 +920,15 @@ static int smb3_reconfigure(struct fs_context *fc)
 	else  {
 		kfree_sensitive(ses->password);
 		ses->password = kstrdup(ctx->password, GFP_KERNEL);
+		if (!ses->password)
+			return -ENOMEM;
 		kfree_sensitive(ses->password2);
 		ses->password2 = kstrdup(ctx->password2, GFP_KERNEL);
+		if (!ses->password2) {
+			kfree_sensitive(ses->password);
+			ses->password = NULL;
+			return -ENOMEM;
+		}
 	}
 	STEAL_STRING(cifs_sb, ctx, domainname);
 	STEAL_STRING(cifs_sb, ctx, nodename);
-- 
2.46.0