Skip to content
This repository was archived by the owner on Nov 8, 2023. It is now read-only.

Commit 9118796

Browse files
GustavoARSilvajmberg-intel
authored andcommitted
wifi: mac80211: Add __counted_by for struct ieee802_11_elems and use struct_size()
Prepare for the coming implementation by GCC and Clang of the __counted_by attribute. Flexible array members annotated with __counted_by can have their accesses bounds-checked at run-time via CONFIG_UBSAN_BOUNDS (for array indexing) and CONFIG_FORTIFY_SOURCE (for strcpy/memcpy-family functions). While there, use struct_size() helper, instead of the open-coded version, to calculate the size for the allocation of the whole flexible structure including, of course, the flexible-array member. This code was found with the help of Coccinelle, and audited and fixed manually. Signed-off-by: Gustavo A. R. Silva <[email protected]> Reviewed-by: Kees Cook <[email protected]> Link: https://lore.kernel.org/r/ZSQ/jcmTAf/PKHg/@work Signed-off-by: Johannes Berg <[email protected]>
1 parent 3c8aaaa commit 9118796

File tree

2 files changed

+2
-2
lines changed

2 files changed

+2
-2
lines changed

net/mac80211/ieee80211_i.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1746,7 +1746,7 @@ struct ieee802_11_elems {
17461746
*/
17471747
size_t scratch_len;
17481748
u8 *scratch_pos;
1749-
u8 scratch[];
1749+
u8 scratch[] __counted_by(scratch_len);
17501750
};
17511751

17521752
static inline struct ieee80211_local *hw_to_local(

net/mac80211/util.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1625,7 +1625,7 @@ ieee802_11_parse_elems_full(struct ieee80211_elems_parse_params *params)
16251625
int nontransmitted_profile_len = 0;
16261626
size_t scratch_len = 3 * params->len;
16271627

1628-
elems = kzalloc(sizeof(*elems) + scratch_len, GFP_ATOMIC);
1628+
elems = kzalloc(struct_size(elems, scratch, scratch_len), GFP_ATOMIC);
16291629
if (!elems)
16301630
return NULL;
16311631
elems->ie_start = params->start;

0 commit comments

Comments
 (0)