linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] tipc: Avoid copying bytes beyond the supplied data
@ 2019-05-20  3:45 Chris Packham
  2019-05-22  7:47 ` Ying Xue
  0 siblings, 1 reply; 4+ messages in thread
From: Chris Packham @ 2019-05-20  3:45 UTC (permalink / raw)
  To: jon.maloy, ying.xue, davem, niveditas98
  Cc: netdev, tipc-discussion, linux-kernel, Chris Packham

TLV_SET is called with a data pointer and a len parameter that tells us
how many bytes are pointed to by data. When invoking memcpy() we need
to careful to only copy len bytes.

Previously we would copy TLV_LENGTH(len) bytes which would copy an extra
4 bytes past the end of the data pointer which newer GCC versions
complain about.

 In file included from test.c:17:
 In function 'TLV_SET',
     inlined from 'test' at test.c:186:5:
 /usr/include/linux/tipc_config.h:317:3:
 warning: 'memcpy' forming offset [33, 36] is out of the bounds [0, 32]
 of object 'bearer_name' with type 'char[32]' [-Warray-bounds]
     memcpy(TLV_DATA(tlv_ptr), data, tlv_len);
     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 test.c: In function 'test':
 test.c::161:10: note:
 'bearer_name' declared here
     char bearer_name[TIPC_MAX_BEARER_NAME];
          ^~~~~~~~~~~

We still want to ensure any padding bytes at the end are initialised, do
this with a explicit memset() rather than copy bytes past the end of
data. Apply the same logic to TCM_SET.

Signed-off-by: Chris Packham <chris.packham@alliedtelesis.co.nz>
---

Changes in v2:
- Ensure padding bytes are initialised in both TLV_SET and TCM_SET

 include/uapi/linux/tipc_config.h | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/include/uapi/linux/tipc_config.h b/include/uapi/linux/tipc_config.h
index 4b2c93b1934c..4955e1a9f1bc 100644
--- a/include/uapi/linux/tipc_config.h
+++ b/include/uapi/linux/tipc_config.h
@@ -307,8 +307,10 @@ static inline int TLV_SET(void *tlv, __u16 type, void *data, __u16 len)
 	tlv_ptr = (struct tlv_desc *)tlv;
 	tlv_ptr->tlv_type = htons(type);
 	tlv_ptr->tlv_len  = htons(tlv_len);
-	if (len && data)
-		memcpy(TLV_DATA(tlv_ptr), data, tlv_len);
+	if (len && data) {
+		memcpy(TLV_DATA(tlv_ptr), data, len);
+		memset(TLV_DATA(tlv_ptr) + len, 0, TLV_SPACE(len) - tlv_len);
+	}
 	return TLV_SPACE(len);
 }
 
@@ -405,8 +407,10 @@ static inline int TCM_SET(void *msg, __u16 cmd, __u16 flags,
 	tcm_hdr->tcm_len   = htonl(msg_len);
 	tcm_hdr->tcm_type  = htons(cmd);
 	tcm_hdr->tcm_flags = htons(flags);
-	if (data_len && data)
+	if (data_len && data) {
 		memcpy(TCM_DATA(msg), data, data_len);
+		memset(TCM_DATA(msg) + data_len, 0, TCM_SPACE(data_len) - msg_len);
+	}
 	return TCM_SPACE(data_len);
 }
 
-- 
2.21.0


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

* Re: [PATCH v2] tipc: Avoid copying bytes beyond the supplied data
  2019-05-20  3:45 [PATCH v2] tipc: Avoid copying bytes beyond the supplied data Chris Packham
@ 2019-05-22  7:47 ` Ying Xue
  2019-05-22 20:46   ` Chris Packham
  0 siblings, 1 reply; 4+ messages in thread
From: Ying Xue @ 2019-05-22  7:47 UTC (permalink / raw)
  To: Chris Packham, jon.maloy, davem, niveditas98
  Cc: netdev, tipc-discussion, linux-kernel

On 5/20/19 11:45 AM, Chris Packham wrote:
> TLV_SET is called with a data pointer and a len parameter that tells us
> how many bytes are pointed to by data. When invoking memcpy() we need
> to careful to only copy len bytes.
> 
> Previously we would copy TLV_LENGTH(len) bytes which would copy an extra
> 4 bytes past the end of the data pointer which newer GCC versions
> complain about.
> 
>  In file included from test.c:17:
>  In function 'TLV_SET',
>      inlined from 'test' at test.c:186:5:
>  /usr/include/linux/tipc_config.h:317:3:
>  warning: 'memcpy' forming offset [33, 36] is out of the bounds [0, 32]
>  of object 'bearer_name' with type 'char[32]' [-Warray-bounds]
>      memcpy(TLV_DATA(tlv_ptr), data, tlv_len);
>      ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>  test.c: In function 'test':
>  test.c::161:10: note:
>  'bearer_name' declared here
>      char bearer_name[TIPC_MAX_BEARER_NAME];
>           ^~~~~~~~~~~
> 
> We still want to ensure any padding bytes at the end are initialised, do
> this with a explicit memset() rather than copy bytes past the end of
> data. Apply the same logic to TCM_SET.
> 
> Signed-off-by: Chris Packham <chris.packham@alliedtelesis.co.nz>

Acked-by: Ying Xue <ying.xue@windriver.com>


But please make the same changes in usr/include/linux/tipc_config.h

> ---
> 
> Changes in v2:
> - Ensure padding bytes are initialised in both TLV_SET and TCM_SET
> 
>  include/uapi/linux/tipc_config.h | 10 +++++++---
>  1 file changed, 7 insertions(+), 3 deletions(-)
> 
> diff --git a/include/uapi/linux/tipc_config.h b/include/uapi/linux/tipc_config.h
> index 4b2c93b1934c..4955e1a9f1bc 100644
> --- a/include/uapi/linux/tipc_config.h
> +++ b/include/uapi/linux/tipc_config.h
> @@ -307,8 +307,10 @@ static inline int TLV_SET(void *tlv, __u16 type, void *data, __u16 len)
>  	tlv_ptr = (struct tlv_desc *)tlv;
>  	tlv_ptr->tlv_type = htons(type);
>  	tlv_ptr->tlv_len  = htons(tlv_len);
> -	if (len && data)
> -		memcpy(TLV_DATA(tlv_ptr), data, tlv_len);
> +	if (len && data) {
> +		memcpy(TLV_DATA(tlv_ptr), data, len);
> +		memset(TLV_DATA(tlv_ptr) + len, 0, TLV_SPACE(len) - tlv_len);
> +	}
>  	return TLV_SPACE(len);
>  }
>  
> @@ -405,8 +407,10 @@ static inline int TCM_SET(void *msg, __u16 cmd, __u16 flags,
>  	tcm_hdr->tcm_len   = htonl(msg_len);
>  	tcm_hdr->tcm_type  = htons(cmd);
>  	tcm_hdr->tcm_flags = htons(flags);
> -	if (data_len && data)
> +	if (data_len && data) {
>  		memcpy(TCM_DATA(msg), data, data_len);
> +		memset(TCM_DATA(msg) + data_len, 0, TCM_SPACE(data_len) - msg_len);
> +	}
>  	return TCM_SPACE(data_len);
>  }
>  
> 

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

* Re: [PATCH v2] tipc: Avoid copying bytes beyond the supplied data
  2019-05-22  7:47 ` Ying Xue
@ 2019-05-22 20:46   ` Chris Packham
  2019-05-23 10:20     ` Ying Xue
  0 siblings, 1 reply; 4+ messages in thread
From: Chris Packham @ 2019-05-22 20:46 UTC (permalink / raw)
  To: Ying Xue, jon.maloy, davem, niveditas98
  Cc: netdev, tipc-discussion, linux-kernel

Hi Ying,

On 22/05/19 7:58 PM, Ying Xue wrote:
> On 5/20/19 11:45 AM, Chris Packham wrote:
>> TLV_SET is called with a data pointer and a len parameter that tells us
>> how many bytes are pointed to by data. When invoking memcpy() we need
>> to careful to only copy len bytes.
>>
>> Previously we would copy TLV_LENGTH(len) bytes which would copy an extra
>> 4 bytes past the end of the data pointer which newer GCC versions
>> complain about.
>>
>>   In file included from test.c:17:
>>   In function 'TLV_SET',
>>       inlined from 'test' at test.c:186:5:
>>   /usr/include/linux/tipc_config.h:317:3:
>>   warning: 'memcpy' forming offset [33, 36] is out of the bounds [0, 32]
>>   of object 'bearer_name' with type 'char[32]' [-Warray-bounds]
>>       memcpy(TLV_DATA(tlv_ptr), data, tlv_len);
>>       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>>   test.c: In function 'test':
>>   test.c::161:10: note:
>>   'bearer_name' declared here
>>       char bearer_name[TIPC_MAX_BEARER_NAME];
>>            ^~~~~~~~~~~
>>
>> We still want to ensure any padding bytes at the end are initialised, do
>> this with a explicit memset() rather than copy bytes past the end of
>> data. Apply the same logic to TCM_SET.
>>
>> Signed-off-by: Chris Packham <chris.packham@alliedtelesis.co.nz>
> 
> Acked-by: Ying Xue <ying.xue@windriver.com>
> 
> 
> But please make the same changes in usr/include/linux/tipc_config.h
> 

I don't understand this comment. There is no usr/include in the kernel 
source.

On most distros that is generated from include/uapi in the kernel source 
and packaged as part of libc or a kernel-headers package. So once this 
patch is accepted and makes it into the distros 
/usr/include/linux/tipc_config.h will have this fix.

Adding Cc: stable might help get it there sooner. But I wanted to get it 
reviewed/accepted first.

>> ---
>>
>> Changes in v2:
>> - Ensure padding bytes are initialised in both TLV_SET and TCM_SET
>>
>>   include/uapi/linux/tipc_config.h | 10 +++++++---
>>   1 file changed, 7 insertions(+), 3 deletions(-)
>>
>> diff --git a/include/uapi/linux/tipc_config.h b/include/uapi/linux/tipc_config.h
>> index 4b2c93b1934c..4955e1a9f1bc 100644
>> --- a/include/uapi/linux/tipc_config.h
>> +++ b/include/uapi/linux/tipc_config.h
>> @@ -307,8 +307,10 @@ static inline int TLV_SET(void *tlv, __u16 type, void *data, __u16 len)
>>   	tlv_ptr = (struct tlv_desc *)tlv;
>>   	tlv_ptr->tlv_type = htons(type);
>>   	tlv_ptr->tlv_len  = htons(tlv_len);
>> -	if (len && data)
>> -		memcpy(TLV_DATA(tlv_ptr), data, tlv_len);
>> +	if (len && data) {
>> +		memcpy(TLV_DATA(tlv_ptr), data, len);
>> +		memset(TLV_DATA(tlv_ptr) + len, 0, TLV_SPACE(len) - tlv_len);
>> +	}
>>   	return TLV_SPACE(len);
>>   }
>>   
>> @@ -405,8 +407,10 @@ static inline int TCM_SET(void *msg, __u16 cmd, __u16 flags,
>>   	tcm_hdr->tcm_len   = htonl(msg_len);
>>   	tcm_hdr->tcm_type  = htons(cmd);
>>   	tcm_hdr->tcm_flags = htons(flags);
>> -	if (data_len && data)
>> +	if (data_len && data) {
>>   		memcpy(TCM_DATA(msg), data, data_len);
>> +		memset(TCM_DATA(msg) + data_len, 0, TCM_SPACE(data_len) - msg_len);
>> +	}
>>   	return TCM_SPACE(data_len);
>>   }
>>   
>>
> 


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

* Re: [PATCH v2] tipc: Avoid copying bytes beyond the supplied data
  2019-05-22 20:46   ` Chris Packham
@ 2019-05-23 10:20     ` Ying Xue
  0 siblings, 0 replies; 4+ messages in thread
From: Ying Xue @ 2019-05-23 10:20 UTC (permalink / raw)
  To: Chris Packham, jon.maloy, davem, niveditas98
  Cc: netdev, tipc-discussion, linux-kernel

On 5/23/19 4:46 AM, Chris Packham wrote:
> On most distros that is generated from include/uapi in the kernel source 
> and packaged as part of libc or a kernel-headers package. So once this 
> patch is accepted and makes it into the distros 
> /usr/include/linux/tipc_config.h will have this fix.

Thanks for the clarification. You are right, so it's unnecessary to make
any change.

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

end of thread, other threads:[~2019-05-23 10:32 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-05-20  3:45 [PATCH v2] tipc: Avoid copying bytes beyond the supplied data Chris Packham
2019-05-22  7:47 ` Ying Xue
2019-05-22 20:46   ` Chris Packham
2019-05-23 10:20     ` Ying Xue

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).