Linux CIFS filesystem development
 help / color / mirror / Atom feed
* [PATCH] cifs: remove misleading strncpy: each name has length < 16
       [not found] ` <1345481724-30108-1-git-send-email-jim-Oxw1nKZkIVjk1uMJSBkQmQ@public.gmane.org>
@ 2012-08-20 16:55   ` Jim Meyering
  2012-08-20 17:36     ` Bastien ROUCARIES
       [not found]     ` <1345481724-30108-6-git-send-email-jim-Oxw1nKZkIVjk1uMJSBkQmQ@public.gmane.org>
  0 siblings, 2 replies; 4+ messages in thread
From: Jim Meyering @ 2012-08-20 16:55 UTC (permalink / raw)
  To: linux-kernel-u79uwXL29TY76Z2rM5mHXA
  Cc: Jim Meyering, Steve French, linux-cifs-u79uwXL29TY76Z2rM5mHXA,
	samba-technical-w/Ol4Ecudpl8XjKLYN78aQ

From: Jim Meyering <meyering-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org>

Each of the protocols[i].name strings (statically declared above)
has length less than 16, so this use of strncpy is misleading:
  strncpy(pSMB->DialectsArray+count, protocols[i].name, 16);
Besides, if a new name were added with length N >= 16, the existing
strncpy-using code would be buggy, creating a ->DialectsArray buffer
containing N-16+1 unset bytes where the NUL terminator should have
been.  Instead, traverse the name only once go get its length,
use a BUG_ON assertion to enforce the length restriction
and use memcpy to perform the copy.

Signed-off-by: Jim Meyering <meyering-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org>
---
 fs/cifs/cifssmb.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/fs/cifs/cifssmb.c b/fs/cifs/cifssmb.c
index 074923c..16a9018 100644
--- a/fs/cifs/cifssmb.c
+++ b/fs/cifs/cifssmb.c
@@ -441,8 +441,10 @@ CIFSSMBNegotiate(const unsigned int xid, struct cifs_ses *ses)

 	count = 0;
 	for (i = 0; i < CIFS_NUM_PROT; i++) {
-		strncpy(pSMB->DialectsArray+count, protocols[i].name, 16);
-		count += strlen(protocols[i].name) + 1;
+		size_t len = strlen(protocols[i].name);
+		BUG_ON(len >= 16);
+		memcpy(pSMB->DialectsArray+count, protocols[i].name, len + 1);
+		count += len + 1;
 		/* null at end of source and target buffers anyway */
 	}
 	inc_rfc1001_len(pSMB, count);
-- 
1.7.12

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

* Re: [PATCH] cifs: remove misleading strncpy: each name has length < 16
  2012-08-20 16:55   ` [PATCH] cifs: remove misleading strncpy: each name has length < 16 Jim Meyering
@ 2012-08-20 17:36     ` Bastien ROUCARIES
       [not found]       ` <CAE2SPAbBmRov9qK2HiBQBQXaZpfJ8pmZJ-PL18FEyoZhzDza4A-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
       [not found]     ` <1345481724-30108-6-git-send-email-jim-Oxw1nKZkIVjk1uMJSBkQmQ@public.gmane.org>
  1 sibling, 1 reply; 4+ messages in thread
From: Bastien ROUCARIES @ 2012-08-20 17:36 UTC (permalink / raw)
  To: Jim Meyering
  Cc: Jim Meyering, linux-cifs, Steve French, samba-technical,
	linux-kernel

Le 20 août 2012 19:29, "Jim Meyering" <jim@meyering.net> a écrit :
>
> From: Jim Meyering <meyering@redhat.com>
>
> Each of the protocols[i].name strings (statically declared above)
> has length less than 16, so this use of strncpy is misleading:
>   strncpy(pSMB->DialectsArray+count, protocols[i].name, 16);
> Besides, if a new name were added with length N >= 16, the existing
> strncpy-using code would be buggy, creating a ->DialectsArray buffer
> containing N-16+1 unset bytes where the NUL terminator should have
> been.  Instead, traverse the name only once go get its length,
> use a BUG_ON assertion to enforce the length restriction
> and use memcpy to perform the copy.
>

Could you use ARRAY_SIZE instead of hard coding 16?
> Signed-off-by: Jim Meyering <meyering@redhat.com>
> ---
>  fs/cifs/cifssmb.c | 6 ++++--
>  1 file changed, 4 insertions(+), 2 deletions(-)
>
> diff --git a/fs/cifs/cifssmb.c b/fs/cifs/cifssmb.c
> index 074923c..16a9018 100644
> --- a/fs/cifs/cifssmb.c
> +++ b/fs/cifs/cifssmb.c
> @@ -441,8 +441,10 @@ CIFSSMBNegotiate(const unsigned int xid, struct
cifs_ses *ses)
>
>         count = 0;
>         for (i = 0; i < CIFS_NUM_PROT; i++) {
> -               strncpy(pSMB->DialectsArray+count, protocols[i].name, 16);
> -               count += strlen(protocols[i].name) + 1;
> +               size_t len = strlen(protocols[i].name);
> +               BUG_ON(len >= 16);
> +               memcpy(pSMB->DialectsArray+count, protocols[i].name, len
+ 1);
> +               count += len + 1;
>                 /* null at end of source and target buffers anyway */
>         }
>         inc_rfc1001_len(pSMB, count);
> --
> 1.7.12
>
> --
> To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
> Please read the FAQ at  http://www.tux.org/lkml/

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

* Re: [PATCH] cifs: remove misleading strncpy: each name has length < 16
       [not found]       ` <CAE2SPAbBmRov9qK2HiBQBQXaZpfJ8pmZJ-PL18FEyoZhzDza4A-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
@ 2012-08-20 18:40         ` Jim Meyering
  0 siblings, 0 replies; 4+ messages in thread
From: Jim Meyering @ 2012-08-20 18:40 UTC (permalink / raw)
  To: Bastien ROUCARIES
  Cc: linux-cifs-u79uwXL29TY76Z2rM5mHXA,
	linux-kernel-u79uwXL29TY76Z2rM5mHXA,
	samba-technical-w/Ol4Ecudpl8XjKLYN78aQ, Steve French

Bastien ROUCARIES wrote:
> Le 20 août 2012 19:29, "Jim Meyering" <jim-Oxw1nKZkIVjk1uMJSBkQmQ@public.gmane.org> a écrit :
>>
>> From: Jim Meyering <meyering-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org>
>>
>> Each of the protocols[i].name strings (statically declared above)
>> has length less than 16, so this use of strncpy is misleading:
>>   strncpy(pSMB->DialectsArray+count, protocols[i].name, 16);
>> Besides, if a new name were added with length N >= 16, the existing
>> strncpy-using code would be buggy, creating a ->DialectsArray buffer
>> containing N-16+1 unset bytes where the NUL terminator should have
>> been.  Instead, traverse the name only once go get its length,
>> use a BUG_ON assertion to enforce the length restriction
>> and use memcpy to perform the copy.
>
> Could you use ARRAY_SIZE instead of hard coding 16?

Not that I can see: the DialectsArray member is declared like this:

    typedef struct negotiate_req {
            struct smb_hdr hdr;	/* wct = 0 */
            __le16 ByteCount;
            unsigned char DialectsArray[1];
    } __attribute__((packed)) NEGOTIATE_REQ;

...
>> diff --git a/fs/cifs/cifssmb.c b/fs/cifs/cifssmb.c
>> index 074923c..16a9018 100644
>> --- a/fs/cifs/cifssmb.c
>> +++ b/fs/cifs/cifssmb.c
>> @@ -441,8 +441,10 @@ CIFSSMBNegotiate(const unsigned int xid, struct cifs_ses
> *ses)
>>
>>         count = 0;
>>         for (i = 0; i < CIFS_NUM_PROT; i++) {
>> -               strncpy(pSMB->DialectsArray+count, protocols[i].name, 16);
>> -               count += strlen(protocols[i].name) + 1;
>> +               size_t len = strlen(protocols[i].name);
>> +               BUG_ON(len >= 16);
>> +               memcpy(pSMB->DialectsArray+count, protocols[i].name, len + 1);
>> +               count += len + 1;

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

* Re: [PATCH] cifs: remove misleading strncpy: each name has length < 16
       [not found]     ` <1345481724-30108-6-git-send-email-jim-Oxw1nKZkIVjk1uMJSBkQmQ@public.gmane.org>
@ 2012-08-20 18:41       ` Jim Meyering
  0 siblings, 0 replies; 4+ messages in thread
From: Jim Meyering @ 2012-08-20 18:41 UTC (permalink / raw)
  To: linux-kernel-u79uwXL29TY76Z2rM5mHXA
  Cc: Steve French, linux-cifs-u79uwXL29TY76Z2rM5mHXA,
	samba-technical-w/Ol4Ecudpl8XjKLYN78aQ

Jim Meyering wrote:

> From: Jim Meyering <meyering-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org>
>
> Each of the protocols[i].name strings (statically declared above)
> has length less than 16, so this use of strncpy is misleading:
>   strncpy(pSMB->DialectsArray+count, protocols[i].name, 16);
> Besides, if a new name were added with length N >= 16, the existing
> strncpy-using code would be buggy, creating a ->DialectsArray buffer
> containing N-16+1 unset bytes where the NUL terminator should have
> been.  Instead, traverse the name only once go get its length,

s/go/to/, of course.

> use a BUG_ON assertion to enforce the length restriction
> and use memcpy to perform the copy.
>
> Signed-off-by: Jim Meyering <meyering-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org>
> ---
>  fs/cifs/cifssmb.c | 6 ++++--
>  1 file changed, 4 insertions(+), 2 deletions(-)
>
> diff --git a/fs/cifs/cifssmb.c b/fs/cifs/cifssmb.c
> index 074923c..16a9018 100644
> --- a/fs/cifs/cifssmb.c
> +++ b/fs/cifs/cifssmb.c
> @@ -441,8 +441,10 @@ CIFSSMBNegotiate(const unsigned int xid, struct cifs_ses *ses)
>
>  	count = 0;
>  	for (i = 0; i < CIFS_NUM_PROT; i++) {
> -		strncpy(pSMB->DialectsArray+count, protocols[i].name, 16);
> -		count += strlen(protocols[i].name) + 1;
> +		size_t len = strlen(protocols[i].name);
> +		BUG_ON(len >= 16);
> +		memcpy(pSMB->DialectsArray+count, protocols[i].name, len + 1);
> +		count += len + 1;
>  		/* null at end of source and target buffers anyway */
>  	}
>  	inc_rfc1001_len(pSMB, count);

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

end of thread, other threads:[~2012-08-20 18:41 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <1345481724-30108-1-git-send-email-jim@meyering.net>
     [not found] ` <1345481724-30108-1-git-send-email-jim-Oxw1nKZkIVjk1uMJSBkQmQ@public.gmane.org>
2012-08-20 16:55   ` [PATCH] cifs: remove misleading strncpy: each name has length < 16 Jim Meyering
2012-08-20 17:36     ` Bastien ROUCARIES
     [not found]       ` <CAE2SPAbBmRov9qK2HiBQBQXaZpfJ8pmZJ-PL18FEyoZhzDza4A-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2012-08-20 18:40         ` Jim Meyering
     [not found]     ` <1345481724-30108-6-git-send-email-jim-Oxw1nKZkIVjk1uMJSBkQmQ@public.gmane.org>
2012-08-20 18:41       ` Jim Meyering

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox