History

tipc: Avoid copying bytes beyond the supplied data

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.

Chris Packham committed 7 years ago

Commit 9bbcdb01 parent 13af14d1 file · +7 −3Browse files

Showing the changes to include/uapi/linux (1 of 1 file). Show all

Unified · Split

include/uapi/linux/tipc_config.h+7−3
@@ -307,8 +307,10 @@ static inline int TLV_SET(void *tlv, __u16 type, void *data, __u16 len) · 306 unmodified lines
307307 tlv_ptr = (struct tlv_desc *)tlv;
308308 tlv_ptr->tlv_type = htons(type);
309309 tlv_ptr->tlv_len = htons(tlv_len);
310− if (len && data)
311− memcpy(TLV_DATA(tlv_ptr), data, tlv_len);
310+ if (len && data) {
311+ memcpy(TLV_DATA(tlv_ptr), data, len);
312+ memset(TLV_DATA(tlv_ptr) + len, 0, TLV_SPACE(len) - tlv_len);
313+ }
312314 return TLV_SPACE(len);
313315 }
314316
@@ -405,8 +407,10 @@ static inline int TCM_SET(void *msg, __u16 cmd, __u16 flags, · 90 unmodified lines
405407 tcm_hdr->tcm_len = htonl(msg_len);
406408 tcm_hdr->tcm_type = htons(cmd);
407409 tcm_hdr->tcm_flags = htons(flags);
408− if (data_len && data)
410+ if (data_len && data) {
409411 memcpy(TCM_DATA(msg), data, data_len);
412+ memset(TCM_DATA(msg) + data_len, 0, TCM_SPACE(data_len) - msg_len);
413+ }
410414 return TCM_SPACE(data_len);
411415 }
412416