All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] smb: Work around Clang __bdos() type confusion
@ 2024-01-23 23:47 Kees Cook
  2024-01-25 12:19 ` Ard Biesheuvel
  0 siblings, 1 reply; 4+ messages in thread
From: Kees Cook @ 2024-01-23 23:47 UTC (permalink / raw)
  To: Steve French
  Cc: Kees Cook, Nathan Chancellor, Paulo Alcantara, Ronnie Sahlberg,
	Shyam Prasad N, Tom Talpey, linux-cifs, llvm, Nick Desaulniers,
	Bill Wendling, Justin Stitt, linux-kernel, linux-hardening

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.)

Reported-by: Nathan Chancellor <nathan@kernel.org>
Closes: https://github.com/ClangBuiltLinux/linux/issues/1966 [1]
Link: https://github.com/llvm/llvm-project/issues/77813 [2]
Cc: Steve French <sfrench@samba.org>
Cc: Paulo Alcantara <pc@manguebit.com>
Cc: Ronnie Sahlberg <ronniesahlberg@gmail.com>
Cc: Shyam Prasad N <sprasad@microsoft.com>
Cc: Tom Talpey <tom@talpey.com>
Cc: linux-cifs@vger.kernel.org
Cc: llvm@lists.linux.dev
Signed-off-by: Kees Cook <keescook@chromium.org>
---
 fs/smb/client/cifsencrypt.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/fs/smb/client/cifsencrypt.c b/fs/smb/client/cifsencrypt.c
index ef4c2e3c9fa6..6322f0f68a17 100644
--- a/fs/smb/client/cifsencrypt.c
+++ b/fs/smb/client/cifsencrypt.c
@@ -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,
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] smb: Work around Clang __bdos() type confusion
  2024-01-23 23:47 [PATCH] smb: Work around Clang __bdos() type confusion Kees Cook
@ 2024-01-25 12:19 ` Ard Biesheuvel
  2024-01-25 18:00   ` Kees Cook
  0 siblings, 1 reply; 4+ messages in thread
From: Ard Biesheuvel @ 2024-01-25 12:19 UTC (permalink / raw)
  To: Kees Cook
  Cc: Steve French, Nathan Chancellor, Paulo Alcantara,
	Ronnie Sahlberg, Shyam Prasad N, Tom Talpey, linux-cifs, llvm,
	Nick Desaulniers, Bill Wendling, Justin Stitt, linux-kernel,
	linux-hardening

On Wed, 24 Jan 2024 at 00:47, Kees Cook <keescook@chromium.org> wrote:
>
> 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.)
>
> Reported-by: Nathan Chancellor <nathan@kernel.org>
> Closes: https://github.com/ClangBuiltLinux/linux/issues/1966 [1]
> Link: https://github.com/llvm/llvm-project/issues/77813 [2]
> Cc: Steve French <sfrench@samba.org>
> Cc: Paulo Alcantara <pc@manguebit.com>
> Cc: Ronnie Sahlberg <ronniesahlberg@gmail.com>
> Cc: Shyam Prasad N <sprasad@microsoft.com>
> Cc: Tom Talpey <tom@talpey.com>
> Cc: linux-cifs@vger.kernel.org
> Cc: llvm@lists.linux.dev
> Signed-off-by: Kees Cook <keescook@chromium.org>
> ---
>  fs/smb/client/cifsencrypt.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/fs/smb/client/cifsencrypt.c b/fs/smb/client/cifsencrypt.c
> index ef4c2e3c9fa6..6322f0f68a17 100644
> --- a/fs/smb/client/cifsencrypt.c
> +++ b/fs/smb/client/cifsencrypt.c
> @@ -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?

>         }
>
>         rc = crypto_shash_update(ses->server->secmech.hmacmd5,
> --
> 2.34.1
>
>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] smb: Work around Clang __bdos() type confusion
  2024-01-25 12:19 ` Ard Biesheuvel
@ 2024-01-25 18:00   ` Kees Cook
  2024-01-25 18:28     ` Ard Biesheuvel
  0 siblings, 1 reply; 4+ messages in thread
From: Kees Cook @ 2024-01-25 18:00 UTC (permalink / raw)
  To: Ard Biesheuvel
  Cc: Steve French, Nathan Chancellor, Paulo Alcantara,
	Ronnie Sahlberg, Shyam Prasad N, Tom Talpey, linux-cifs, llvm,
	Nick Desaulniers, Bill Wendling, Justin Stitt, linux-kernel,
	linux-hardening

On Thu, Jan 25, 2024 at 01:19:19PM +0100, Ard Biesheuvel wrote:
> On Wed, 24 Jan 2024 at 00:47, Kees Cook <keescook@chromium.org> wrote:
> >
> > 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.)
> >
> > Reported-by: Nathan Chancellor <nathan@kernel.org>
> > Closes: https://github.com/ClangBuiltLinux/linux/issues/1966 [1]
> > Link: https://github.com/llvm/llvm-project/issues/77813 [2]
> > Cc: Steve French <sfrench@samba.org>
> > Cc: Paulo Alcantara <pc@manguebit.com>
> > Cc: Ronnie Sahlberg <ronniesahlberg@gmail.com>
> > Cc: Shyam Prasad N <sprasad@microsoft.com>
> > Cc: Tom Talpey <tom@talpey.com>
> > Cc: linux-cifs@vger.kernel.org
> > Cc: llvm@lists.linux.dev
> > Signed-off-by: Kees Cook <keescook@chromium.org>
> > ---
> >  fs/smb/client/cifsencrypt.c | 2 +-
> >  1 file changed, 1 insertion(+), 1 deletion(-)
> >
> > diff --git a/fs/smb/client/cifsencrypt.c b/fs/smb/client/cifsencrypt.c
> > index ef4c2e3c9fa6..6322f0f68a17 100644
> > --- a/fs/smb/client/cifsencrypt.c
> > +++ b/fs/smb/client/cifsencrypt.c
> > @@ -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

-- 
Kees Cook

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] smb: Work around Clang __bdos() type confusion
  2024-01-25 18:00   ` Kees Cook
@ 2024-01-25 18:28     ` Ard Biesheuvel
  0 siblings, 0 replies; 4+ messages in thread
From: Ard Biesheuvel @ 2024-01-25 18:28 UTC (permalink / raw)
  To: Kees Cook
  Cc: Steve French, Nathan Chancellor, Paulo Alcantara,
	Ronnie Sahlberg, Shyam Prasad N, Tom Talpey, linux-cifs, llvm,
	Nick Desaulniers, Bill Wendling, Justin Stitt, linux-kernel,
	linux-hardening

On Thu, 25 Jan 2024 at 19:00, Kees Cook <keescook@chromium.org> wrote:
>
> On Thu, Jan 25, 2024 at 01:19:19PM +0100, Ard Biesheuvel wrote:
> > On Wed, 24 Jan 2024 at 00:47, Kees Cook <keescook@chromium.org> wrote:
> > >
> > > 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.)
> > >
> > > Reported-by: Nathan Chancellor <nathan@kernel.org>
> > > Closes: https://github.com/ClangBuiltLinux/linux/issues/1966 [1]
> > > Link: https://github.com/llvm/llvm-project/issues/77813 [2]
> > > Cc: Steve French <sfrench@samba.org>
> > > Cc: Paulo Alcantara <pc@manguebit.com>
> > > Cc: Ronnie Sahlberg <ronniesahlberg@gmail.com>
> > > Cc: Shyam Prasad N <sprasad@microsoft.com>
> > > Cc: Tom Talpey <tom@talpey.com>
> > > Cc: linux-cifs@vger.kernel.org
> > > Cc: llvm@lists.linux.dev
> > > Signed-off-by: Kees Cook <keescook@chromium.org>
> > > ---
> > >  fs/smb/client/cifsencrypt.c | 2 +-
> > >  1 file changed, 1 insertion(+), 1 deletion(-)
> > >
> > > diff --git a/fs/smb/client/cifsencrypt.c b/fs/smb/client/cifsencrypt.c
> > > index ef4c2e3c9fa6..6322f0f68a17 100644
> > > --- a/fs/smb/client/cifsencrypt.c
> > > +++ b/fs/smb/client/cifsencrypt.c
> > > @@ -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.
>

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

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2024-01-25 18:28 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-01-23 23:47 [PATCH] smb: Work around Clang __bdos() type confusion Kees Cook
2024-01-25 12:19 ` Ard Biesheuvel
2024-01-25 18:00   ` Kees Cook
2024-01-25 18:28     ` Ard Biesheuvel

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.