Skip to contentSkip to conversation

smb: Use loff_t for directory position in cached_dirents

PATCH In review Bharath SM 3 participants 20 messages Jun 19, 2025 – Jun 20, 2025

Subsystems (2)
lore
Share & tools
lore

Bharath SM

author
#0 lore raw

PATCH 1/7 smb: Use loff_t for directory position in cached_dirents

Change the pos field in struct cached_dirents from int to loff_t to support large directory offsets. This avoids overflow and matches kernel conventions for directory positions.

---
 fs/smb/client/cached_dir.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/smb/client/cached_dir.h +1 -1
index bc8a812ff95f..a28f7cae3caa 100644
@@ -26,7 +26,7 @@ struct cached_dirents {
 			    * open file instance.
 			    */
 	struct mutex de_mutex;
-	int pos;		 /* Expected ctx->pos */
+	loff_t pos;		 /* Expected ctx->pos */
 	struct list_head entries;
 };
 
Signature · 1 line
2.43.0
author
#1 lore raw

PATCH 2/7 smb: minor fix to use sizeof to initialize flags_string buffer

Replaced hardcoded length with sizeof(flags_string).

---
 fs/smb/client/cifs_debug.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/smb/client/cifs_debug.c +1 -1
index c0196be0e65f..3fdf75737d43 100644
@@ -1105,7 +1105,7 @@ static ssize_t cifs_security_flags_proc_write(struct file *file,
 	if ((count < 1) || (count > 11))
 		return -EINVAL;
 
-	memset(flags_string, 0, 12);
+	memset(flags_string, 0, sizeof(flags_string));
 
 	if (copy_from_user(flags_string, buffer, count))
 		return -EFAULT;
Signature · 1 line
2.43.0
#3 lore raw

Re: [PATCH 2/7] smb: minor fix to use sizeof to initialize flags_string buffer

In reply to Bharath SM
Quoted 22 lines · Bharath SM

Replaced hardcoded length with sizeof(flags_string).

---
 fs/smb/client/cifs_debug.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/smb/client/cifs_debug.c +1 -2
index c0196be0e65f..3fdf75737d43 100644
@@ -1105,7 +1105,7 @@ static ssize_t cifs_security_flags_proc_write(struct file *file,
        if ((count < 1) || (count > 11))
                return -EINVAL;
 
-       memset(flags_string, 0, 12);
+       memset(flags_string, 0, sizeof(flags_string));
 
        if (copy_from_user(flags_string, buffer, count))
                return -EFAULT;
--
Quoted 3 lines

2.43.0

Why not define the size of the flags_string as a constant too?

Signature · 2 lines
Regards,
Shyam
author
#4 lore raw

PATCH 3/7 smb: minor fix to use SMB2_NTLMV2_SESSKEY_SIZE for auth_key size

In reply to Bharath SM

Replaced hardcoded value 16 with SMB2_NTLMV2_SESSKEY_SIZE in the auth_key definition and memcpy call.

---
 fs/smb/client/cifs_ioctl.h | 2 +-
 fs/smb/client/ioctl.c      | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
fs/smb/client/cifs_ioctl.h +1 -1
index 26327442e383..b51ce64fcccf 100644
@@ -61,7 +61,7 @@ struct smb_query_info {
 struct smb3_key_debug_info {
 	__u64	Suid;
 	__u16	cipher_type;
-	__u8	auth_key[16]; /* SMB2_NTLMV2_SESSKEY_SIZE */
+	__u8	auth_key[SMB2_NTLMV2_SESSKEY_SIZE];
 	__u8	smb3encryptionkey[SMB3_SIGN_KEY_SIZE];
 	__u8	smb3decryptionkey[SMB3_SIGN_KEY_SIZE];
 } __packed;
fs/smb/client/ioctl.c +1 -1
index 56439da4f119..0a9935ce05a5 100644
@@ -506,7 +506,7 @@ long cifs_ioctl(struct file *filep, unsigned int command, unsigned long arg)
 				le16_to_cpu(tcon->ses->server->cipher_type);
 			pkey_inf.Suid = tcon->ses->Suid;
 			memcpy(pkey_inf.auth_key, tcon->ses->auth_key.response,
-					16 /* SMB2_NTLMV2_SESSKEY_SIZE */);
+				  SMB2_NTLMV2_SESSKEY_SIZE);
 			memcpy(pkey_inf.smb3decryptionkey,
 			      tcon->ses->smb3decryptionkey, SMB3_SIGN_KEY_SIZE);
 			memcpy(pkey_inf.smb3encryptionkey,
Signature · 1 line
2.43.0
maintainer Reviewed-by on v1 3/7
#5 lore raw

Re: [PATCH 3/7] smb: minor fix to use SMB2_NTLMV2_SESSKEY_SIZE for auth_key size

Quoted 27 lines · Bharath SM

Replaced hardcoded value 16 with SMB2_NTLMV2_SESSKEY_SIZE in the auth_key definition and memcpy call.

---
 fs/smb/client/cifs_ioctl.h | 2 +-
 fs/smb/client/ioctl.c      | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
fs/smb/client/cifs_ioctl.h +1 -1
index 26327442e383..b51ce64fcccf 100644
@@ -61,7 +61,7 @@ struct smb_query_info {
 struct smb3_key_debug_info {
 	__u64	Suid;
 	__u16	cipher_type;
-	__u8	auth_key[16]; /* SMB2_NTLMV2_SESSKEY_SIZE */
+	__u8	auth_key[SMB2_NTLMV2_SESSKEY_SIZE];
 	__u8	smb3encryptionkey[SMB3_SIGN_KEY_SIZE];
 	__u8	smb3decryptionkey[SMB3_SIGN_KEY_SIZE];
 } __packed;
fs/smb/client/ioctl.c +0 -0
index 56439da4f119..0a9935ce05a5 100644

fs/smb/client/ioctl.c long cifs_ioctl(struct file *filep, unsigned int command, unsigned long arg) · line 509

Quoted 5 lines
fs/smb/client/ioctl.c +1 -1
@@ -506,7 +506,7 @@ long cifs_ioctl(struct file *filep, unsigned int command, unsigned long arg)
 				le16_to_cpu(tcon->ses->server->cipher_type);
 			pkey_inf.Suid = tcon->ses->Suid;
 			memcpy(pkey_inf.auth_key, tcon->ses->auth_key.response,
-					16 /* SMB2_NTLMV2_SESSKEY_SIZE */);
+				  SMB2_NTLMV2_SESSKEY_SIZE);
                                  ^ sizeof(pkey_in.auth_key)?
#6 lore raw

Re: [PATCH 3/7] smb: minor fix to use SMB2_NTLMV2_SESSKEY_SIZE for auth_key size

In reply to Bharath SM
Quoted 37 lines · Bharath SM

Replaced hardcoded value 16 with SMB2_NTLMV2_SESSKEY_SIZE in the auth_key definition and memcpy call.

---
 fs/smb/client/cifs_ioctl.h | 2 +-
 fs/smb/client/ioctl.c      | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
fs/smb/client/cifs_ioctl.h +1 -1
index 26327442e383..b51ce64fcccf 100644
@@ -61,7 +61,7 @@ struct smb_query_info {
 struct smb3_key_debug_info {
        __u64   Suid;
        __u16   cipher_type;
-       __u8    auth_key[16]; /* SMB2_NTLMV2_SESSKEY_SIZE */
+       __u8    auth_key[SMB2_NTLMV2_SESSKEY_SIZE];
        __u8    smb3encryptionkey[SMB3_SIGN_KEY_SIZE];
        __u8    smb3decryptionkey[SMB3_SIGN_KEY_SIZE];
 } __packed;
fs/smb/client/ioctl.c +1 -2
index 56439da4f119..0a9935ce05a5 100644
@@ -506,7 +506,7 @@ long cifs_ioctl(struct file *filep, unsigned int command, unsigned long arg)
                                le16_to_cpu(tcon->ses->server->cipher_type);
                        pkey_inf.Suid = tcon->ses->Suid;
                        memcpy(pkey_inf.auth_key, tcon->ses->auth_key.response,
-                                       16 /* SMB2_NTLMV2_SESSKEY_SIZE */);
+                                 SMB2_NTLMV2_SESSKEY_SIZE);
                        memcpy(pkey_inf.smb3decryptionkey,
                              tcon->ses->smb3decryptionkey, SMB3_SIGN_KEY_SIZE);
                        memcpy(pkey_inf.smb3encryptionkey,
--
Quoted 3 lines

2.43.0

Looks good to me.

Signature · 2 lines
Regards,
Shyam
author
#7 lore raw

PATCH 4/7 smb: add NULL check after kzalloc in cifsConvertToUTF16

In reply to Bharath SM

Added a check to return -ENOMEM if kzalloc for wchar_to fails

---
 fs/smb/client/cifs_unicode.c | 2 ++
 1 file changed, 2 insertions(+)
fs/smb/client/cifs_unicode.c +2 -0
index 4cc6e0896fad..7bc2268d6881 100644
@@ -466,6 +466,8 @@ cifsConvertToUTF16(__le16 *target, const char *source, int srclen,
 		return cifs_strtoUTF16(target, source, PATH_MAX, cp);
 
 	wchar_to = kzalloc(6, GFP_KERNEL);
+	if (wchar_to == NULL)
+		return -ENOMEM;
 
 	for (i = 0; i < srclen; j++) {
 		src_char = source[i];
Signature · 1 line
2.43.0
maintainer
#8 lore raw

Re: [PATCH 4/7] smb: add NULL check after kzalloc in cifsConvertToUTF16

Quoted 12 lines · Bharath SM

Added a check to return -ENOMEM if kzalloc for wchar_to fails

---
 fs/smb/client/cifs_unicode.c | 2 ++
 1 file changed, 2 insertions(+)
fs/smb/client/cifs_unicode.c +0 -0
index 4cc6e0896fad..7bc2268d6881 100644

fs/smb/client/cifs_unicode.c cifsConvertToUTF16(__le16 *target, const char *source, int srclen, · line 470

Quoted 5 lines
fs/smb/client/cifs_unicode.c +2 -0
@@ -466,6 +466,8 @@ cifsConvertToUTF16(__le16 *target, const char *source, int srclen,
 		return cifs_strtoUTF16(target, source, PATH_MAX, cp);
 
 	wchar_to = kzalloc(6, GFP_KERNEL);
+	if (wchar_to == NULL)
+		return -ENOMEM;

I wouldn't do that as there are several places that rely on cifsConvertToUTF16() returning >= 0 and some other places that don't even check its return value.

What about having it defined as

        wchar_t wchar_to[3] = {};

and then getting rid of the memory allocation altogether?

author
#9 lore raw

Re: [PATCH 4/7] smb: add NULL check after kzalloc in cifsConvertToUTF16

makes sense. thanks. will update.

Quoted 30 lines · Paulo Alcantara
Quoted 12 lines · Bharath SM

Added a check to return -ENOMEM if kzalloc for wchar_to fails

---
 fs/smb/client/cifs_unicode.c | 2 ++
 1 file changed, 2 insertions(+)
fs/smb/client/cifs_unicode.c +0 -0
index 4cc6e0896fad..7bc2268d6881 100644

fs/smb/client/cifs_unicode.c cifsConvertToUTF16(__le16 *target, const char *source, int srclen, · line 470

Quoted 5 lines
fs/smb/client/cifs_unicode.c +2 -0
@@ -466,6 +466,8 @@ cifsConvertToUTF16(__le16 *target, const char *source, int srclen,
              return cifs_strtoUTF16(target, source, PATH_MAX, cp);
 
      wchar_to = kzalloc(6, GFP_KERNEL);
+     if (wchar_to == NULL)
+             return -ENOMEM;

I wouldn't do that as there are several places that rely on cifsConvertToUTF16() returning >= 0 and some other places that don't even check its return value.

What about having it defined as

        wchar_t wchar_to[3] = {};

and then getting rid of the memory allocation altogether?

#10 lore raw

Re: [PATCH 4/7] smb: add NULL check after kzalloc in cifsConvertToUTF16

In reply to Bharath SM
Quoted 22 lines · Bharath SM

Added a check to return -ENOMEM if kzalloc for wchar_to fails

---
 fs/smb/client/cifs_unicode.c | 2 ++
 1 file changed, 2 insertions(+)
fs/smb/client/cifs_unicode.c +2 -1
index 4cc6e0896fad..7bc2268d6881 100644
@@ -466,6 +466,8 @@ cifsConvertToUTF16(__le16 *target, const char *source, int srclen,
                return cifs_strtoUTF16(target, source, PATH_MAX, cp);
 
        wchar_to = kzalloc(6, GFP_KERNEL);
+       if (wchar_to == NULL)
+               return -ENOMEM;
 
        for (i = 0; i < srclen; j++) {
                src_char = source[i];
--
Quoted 3 lines

2.43.0

Looks good to me.

Signature · 2 lines
Regards,
Shyam
author
#11 lore raw

PATCH 5/7 cifs: minor cleanup to remove unused permission bit macros

In reply to Bharath SM

Dropped READ_BIT, WRITE_BIT, EXEC_BIT, UBITSHIFT,
and GBITSHIFT from cifsacl.h as they are unused

---
 fs/smb/client/cifsacl.h | 7 -------
 1 file changed, 7 deletions(-)
fs/smb/client/cifsacl.h +0 -7
index 31b51a8fc256..3d828f655e97 100644
@@ -11,17 +11,10 @@
 
 #include "../common/smbacl.h"
 
-#define READ_BIT        0x4
-#define WRITE_BIT       0x2
-#define EXEC_BIT        0x1
-
 #define ACL_OWNER_MASK 0700
 #define ACL_GROUP_MASK 0070
 #define ACL_EVERYONE_MASK 0007
 
-#define UBITSHIFT	6
-#define GBITSHIFT	3
-
 /*
  * Security Descriptor length containing DACL with 3 ACEs (one each for
  * owner, group and world).
Signature · 1 line
2.43.0
author
#12 lore raw

PATCH 6/7 smb: Fix potential divide-by-zero issue when iface_min_speed is 0

In reply to Bharath SM

Address potential divide-by-zero issue when iface_min_speed is 0 by adding proper handling to prevent undefined behavior.

---
 fs/smb/client/cifs_debug.c | 2 +-
 fs/smb/client/sess.c       | 4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)
fs/smb/client/cifs_debug.c +1 -1
index 3fdf75737d43..bc56f315e2e0 100644
@@ -599,7 +599,7 @@ static int cifs_debug_data_proc_show(struct seq_file *m, void *v)
 				seq_printf(m, "\n\t%d)", ++j);
 				cifs_dump_iface(m, iface);
 
-				iface_weight = iface->speed / iface_min_speed;
+				iface_weight = iface_min_speed ? (iface->speed / iface_min_speed) : 0;
 				seq_printf(m, "\t\tWeight (cur,total): (%zu,%zu)"
 					   "\n\t\tAllocated channels: %u\n",
 					   iface->weight_fulfilled,
fs/smb/client/sess.c +2 -2
index ec0db32c7d98..697170be6591 100644
@@ -218,7 +218,7 @@ int cifs_try_adding_channels(struct cifs_ses *ses)
 				continue;
 
 			/* check if we already allocated enough channels */
-			iface_weight = iface->speed / iface_min_speed;
+			iface_weight = iface_min_speed ? (iface->speed / iface_min_speed) : 0;
 
 			if (iface->weight_fulfilled >= iface_weight)
 				continue;
@@ -387,7 +387,7 @@ cifs_chan_update_iface(struct cifs_ses *ses, struct TCP_Server_Info *server)
 		}
 
 		/* check if we already allocated enough channels */
-		iface_weight = iface->speed / iface_min_speed;
+		iface_weight = iface_min_speed ? (iface->speed / iface_min_speed) : 0;
 
 		if (iface->weight_fulfilled >= iface_weight)
 			continue;
Signature · 1 line
2.43.0
maintainer
#13 lore raw

Re: [PATCH 6/7] smb: Fix potential divide-by-zero issue when iface_min_speed is 0

Quoted 8 lines · Bharath SM

Address potential divide-by-zero issue when iface_min_speed is 0 by adding proper handling to prevent undefined behavior.

---
 fs/smb/client/cifs_debug.c | 2 +-
 fs/smb/client/sess.c       | 4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)

Isn't be7a6a776695 ("smb: client: fix oops due to unset link speed") enough to prevent that already?

author
#14 lore raw

Re: [PATCH 6/7] smb: Fix potential divide-by-zero issue when iface_min_speed is 0

Quoted 6 lines · Paulo Alcantara
Quoted 3 lines · Bharath SM

Address potential divide-by-zero issue when iface_min_speed is 0 by adding proper handling to prevent undefined behavior.

Quoted 8 lines
Quoted 5 lines
---
 fs/smb/client/cifs_debug.c | 2 +-
 fs/smb/client/sess.c       | 4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)

Isn't be7a6a776695 ("smb: client: fix oops due to unset link speed") enough to prevent that already?

Right, I didn't notice this check. I think this fix should take of the issue.

#15 lore raw

Re: [PATCH 6/7] smb: Fix potential divide-by-zero issue when iface_min_speed is 0

In reply to Bharath SM
Quoted 46 lines · Bharath SM

Address potential divide-by-zero issue when iface_min_speed is 0 by adding proper handling to prevent undefined behavior.

---
 fs/smb/client/cifs_debug.c | 2 +-
 fs/smb/client/sess.c       | 4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)
fs/smb/client/cifs_debug.c +1 -1
index 3fdf75737d43..bc56f315e2e0 100644
@@ -599,7 +599,7 @@ static int cifs_debug_data_proc_show(struct seq_file *m, void *v)
                                seq_printf(m, "\n\t%d)", ++j);
                                cifs_dump_iface(m, iface);
 
-                               iface_weight = iface->speed / iface_min_speed;
+                               iface_weight = iface_min_speed ? (iface->speed / iface_min_speed) : 0;
                                seq_printf(m, "\t\tWeight (cur,total): (%zu,%zu)"
                                           "\n\t\tAllocated channels: %u\n",
                                           iface->weight_fulfilled,
fs/smb/client/sess.c +2 -3
index ec0db32c7d98..697170be6591 100644
@@ -218,7 +218,7 @@ int cifs_try_adding_channels(struct cifs_ses *ses)
                                continue;
 
                        /* check if we already allocated enough channels */
-                       iface_weight = iface->speed / iface_min_speed;
+                       iface_weight = iface_min_speed ? (iface->speed / iface_min_speed) : 0;
 
                        if (iface->weight_fulfilled >= iface_weight)
                                continue;
@@ -387,7 +387,7 @@ cifs_chan_update_iface(struct cifs_ses *ses, struct TCP_Server_Info *server)
                }
 
                /* check if we already allocated enough channels */
-               iface_weight = iface->speed / iface_min_speed;
+               iface_weight = iface_min_speed ? (iface->speed / iface_min_speed) : 0;
 
                if (iface->weight_fulfilled >= iface_weight)
                        continue;
--
Quoted 3 lines

2.43.0

Good spot.
Might want to add the Fixes tag:

Fixes a6d8fb54a515 ("cifs: distribute channels across interfaces

based on speed")

Signature · 2 lines
Regards,
Shyam
author
#16 lore raw

PATCH 7/7 smb: Fix memory allocation and ACL handling in cifs_xattr_set

In reply to Bharath SM

If set_acl is NULL, then there is a chance that memory allocated for pacl memory doesn't get freed. Ensure proper memory deallocation for pacl in cifs_xattr_set to avoid leaks.

---
 fs/smb/client/xattr.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
fs/smb/client/xattr.c +2 -1
index b88fa04f5792..4d2b70020776 100644
@@ -174,6 +174,7 @@ static int cifs_xattr_set(const struct xattr_handler *handler,
 		pacl = kmalloc(size, GFP_KERNEL);
 		if (!pacl) {
 			rc = -ENOMEM;
+			goto out;
 		} else {
 			memcpy(pacl, value, size);
 			if (pTcon->ses->server->ops->set_acl) {
@@ -211,8 +212,8 @@ static int cifs_xattr_set(const struct xattr_handler *handler,
 			}
 			if (rc == 0) /* force revalidate of the inode */
 				CIFS_I(inode)->time = 0;
-			kfree(pacl);
 		}
+		kfree(pacl);
 		break;
 	}
 	}
Signature · 1 line
2.43.0
maintainer
#17 lore raw

Re: [PATCH 7/7] smb: Fix memory allocation and ACL handling in cifs_xattr_set

Hi Bharath,

Quoted 3 lines · Bharath SM

If set_acl is NULL, then there is a chance that memory allocated for pacl memory doesn't get freed. Ensure proper memory deallocation for pacl in cifs_xattr_set to avoid leaks.

If ->set_acl is NULL, then the following is executed

			} else {
				rc = -EOPNOTSUPP;
			}
			if (rc == 0) /* force revalidate of the inode */
				CIFS_I(inode)->time = 0;
			kfree(pacl);

Can you elaborate where the potential leak is?

#19 lore raw
In reply to Bharath SM
Quoted 24 lines · Bharath SM

Change the pos field in struct cached_dirents from int to loff_t to support large directory offsets. This avoids overflow and matches kernel conventions for directory positions.

---
 fs/smb/client/cached_dir.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/smb/client/cached_dir.h +1 -2
index bc8a812ff95f..a28f7cae3caa 100644
@@ -26,7 +26,7 @@ struct cached_dirents {
                            * open file instance.
                            */
        struct mutex de_mutex;
-       int pos;                 /* Expected ctx->pos */
+       loff_t pos;              /* Expected ctx->pos */
        struct list_head entries;
 };
 
--
Quoted 3 lines

2.43.0

Looks good to me.

Signature · 2 lines
Regards,
Shyam