[PATCH] cifs: remove misleading strncpy: each name has length < 16

Jim Meyering jim at meyering.net
Mon Aug 20 12:40:10 MDT 2012


Bastien ROUCARIES wrote:
> Le 20 août 2012 19:29, "Jim Meyering" <jim at meyering.net> a écrit :
>>
>> From: Jim Meyering <meyering at 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?

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;


More information about the samba-technical mailing list