Skip to contentSkip to conversation

smb: Work around Clang __bdos() type confusion

PATCH In review Kees Cook 2 participants 4 messages Jan 23, 2024 – Jan 25, 2024

Subsystems (2)
lore
Share & tools
lore

Kees Cook

author
#0 lore raw

PATCH 1/1 smb: Work around Clang __bdos() type confusion

Recent versions of Clang gets confused about the possible size of the "user" allocation, and CONFIG_FORTIFY_SOURCE ends up emitting a warning[1]:

repro.c:126:4: warning: call to '__write_overflow_field' declared with 'warning' attribute: detected write beyond size of field (1st parameter); maybe use struct_group()? [-Wattribute-warning]

  126 |                         __write_overflow_field(p_size_field, size);
      |                         ^

for this memset():

        int len;
        __le16 *user;
	...
        len = ses->user_name ? strlen(ses->user_name) : 0;
        user = kmalloc(2 + (len * 2), GFP_KERNEL);
	...
	if (len) {
		...
	} else {
		memset(user, '\0', 2);
	}

While Clang works on this bug[2], switch to using a direct assignment, which avoids memset() entirely which both simplifies the code and silences the false positive warning. (Making "len" size_t also silences the warning, but the direct assignment seems better.)

---
 fs/smb/client/cifsencrypt.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/smb/client/cifsencrypt.c +1 -1
index ef4c2e3c9fa6..6322f0f68a17 100644
@@ -572,7 +572,7 @@ static int calc_ntlmv2_hash(struct cifs_ses *ses, char *ntlmv2_hash,
 		len = cifs_strtoUTF16(user, ses->user_name, len, nls_cp);
 		UniStrupr(user);
 	} else {
-		memset(user, '\0', 2);
+		*(u16 *)user = 0;
 	}
 
 	rc = crypto_shash_update(ses->server->secmech.hmacmd5,
Signature · 1 line
2.34.1
#1 lore raw
Quoted 48 lines · Kees Cook

Recent versions of Clang gets confused about the possible size of the "user" allocation, and CONFIG_FORTIFY_SOURCE ends up emitting a warning[1]:

repro.c:126:4: warning: call to '__write_overflow_field' declared with 'warning' attribute: detected write beyond size of field (1st parameter); maybe use struct_group()? [-Wattribute-warning]

  126 |                         __write_overflow_field(p_size_field, size);
      |                         ^

for this memset():

        int len;
        __le16 *user;
        ...
        len = ses->user_name ? strlen(ses->user_name) : 0;
        user = kmalloc(2 + (len * 2), GFP_KERNEL);
        ...
        if (len) {
                ...
        } else {
                memset(user, '\0', 2);
        }

While Clang works on this bug[2], switch to using a direct assignment, which avoids memset() entirely which both simplifies the code and silences the false positive warning. (Making "len" size_t also silences the warning, but the direct assignment seems better.)

---
 fs/smb/client/cifsencrypt.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/smb/client/cifsencrypt.c +0 -0
index ef4c2e3c9fa6..6322f0f68a17 100644

fs/smb/client/cifsencrypt.c static int calc_ntlmv2_hash(struct cifs_ses *ses, char *ntlmv2_hash, · line 575

Quoted 5 lines
fs/smb/client/cifsencrypt.c +1 -1
@@ -572,7 +572,7 @@ static int calc_ntlmv2_hash(struct cifs_ses *ses, char *ntlmv2_hash,
                len = cifs_strtoUTF16(user, ses->user_name, len, nls_cp);
                UniStrupr(user);
        } else {
-               memset(user, '\0', 2);
+               *(u16 *)user = 0;

Is 'user' guaranteed to be 16-bit aligned?

Quoted 7 lines
fs/smb/client/cifsencrypt.c +0 -1
        }
 
        rc = crypto_shash_update(ses->server->secmech.hmacmd5,
--

2.34.1

author
#2 lore raw
Quoted 44 lines · Ard Biesheuvel
Quoted 43 lines · Kees Cook

Recent versions of Clang gets confused about the possible size of the "user" allocation, and CONFIG_FORTIFY_SOURCE ends up emitting a warning[1]:

repro.c:126:4: warning: call to '__write_overflow_field' declared with 'warning' attribute: detected write beyond size of field (1st parameter); maybe use struct_group()? [-Wattribute-warning]

  126 |                         __write_overflow_field(p_size_field, size);
      |                         ^

for this memset():

        int len;
        __le16 *user;
        ...
        len = ses->user_name ? strlen(ses->user_name) : 0;
        user = kmalloc(2 + (len * 2), GFP_KERNEL);
        ...
        if (len) {
                ...
        } else {
                memset(user, '\0', 2);
        }

While Clang works on this bug[2], switch to using a direct assignment, which avoids memset() entirely which both simplifies the code and silences the false positive warning. (Making "len" size_t also silences the warning, but the direct assignment seems better.)

---
 fs/smb/client/cifsencrypt.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 12 lines

fs/smb/client/cifsencrypt.c static int calc_ntlmv2_hash(struct cifs_ses *ses, char *ntlmv2_hash, · line 575

Quoted 10 lines
fs/smb/client/cifsencrypt.c +1 -1
index ef4c2e3c9fa6..6322f0f68a17 100644
@@ -572,7 +572,7 @@ static int calc_ntlmv2_hash(struct cifs_ses *ses, char *ntlmv2_hash,
                len = cifs_strtoUTF16(user, ses->user_name, len, nls_cp);
                UniStrupr(user);
        } else {
-               memset(user, '\0', 2);
+               *(u16 *)user = 0;

Is 'user' guaranteed to be 16-bit aligned?

It's the first two bytes of a kmalloced address range, which I'm nearly certain will be sanely aligned, as those allocs are commonly used for holding structs, etc.

-Kees

Signature · 1 line
Kees Cook
#3 lore raw
Quoted 59 lines · Kees Cook
Quoted 44 lines · Ard Biesheuvel
Quoted 43 lines · Kees Cook

Recent versions of Clang gets confused about the possible size of the "user" allocation, and CONFIG_FORTIFY_SOURCE ends up emitting a warning[1]:

repro.c:126:4: warning: call to '__write_overflow_field' declared with 'warning' attribute: detected write beyond size of field (1st parameter); maybe use struct_group()? [-Wattribute-warning]

  126 |                         __write_overflow_field(p_size_field, size);
      |                         ^

for this memset():

        int len;
        __le16 *user;
        ...
        len = ses->user_name ? strlen(ses->user_name) : 0;
        user = kmalloc(2 + (len * 2), GFP_KERNEL);
        ...
        if (len) {
                ...
        } else {
                memset(user, '\0', 2);
        }

While Clang works on this bug[2], switch to using a direct assignment, which avoids memset() entirely which both simplifies the code and silences the false positive warning. (Making "len" size_t also silences the warning, but the direct assignment seems better.)

---
 fs/smb/client/cifsencrypt.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 12 lines

fs/smb/client/cifsencrypt.c static int calc_ntlmv2_hash(struct cifs_ses *ses, char *ntlmv2_hash, · line 575

Quoted 10 lines
fs/smb/client/cifsencrypt.c +1 -1
index ef4c2e3c9fa6..6322f0f68a17 100644
@@ -572,7 +572,7 @@ static int calc_ntlmv2_hash(struct cifs_ses *ses, char *ntlmv2_hash,
                len = cifs_strtoUTF16(user, ses->user_name, len, nls_cp);
                UniStrupr(user);
        } else {
-               memset(user, '\0', 2);
+               *(u16 *)user = 0;

Is 'user' guaranteed to be 16-bit aligned?

Quoted 4 lines

It's the first two bytes of a kmalloced address range, which I'm nearly certain will be sanely aligned, as those allocs are commonly used for holding structs, etc.

Ah yes, this kmalloc() was carefully hidden in the commit log :-)