Skip to content

Commit 089fd33

Browse files
tobluxKernel Patches Daemon
authored and
Kernel Patches Daemon
committed
bpf: Replace offsetof() with struct_size()
Compared to offsetof(), struct_size() provides additional compile-time checks for structs with flexible arrays (e.g., __must_be_array()). No functional changes intended. Signed-off-by: Thorsten Blum <[email protected]>
1 parent 6bce87f commit 089fd33

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

kernel/bpf/btf.c

+9-10
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <linux/bsearch.h>
2727
#include <linux/kobject.h>
2828
#include <linux/sysfs.h>
29+
#include <linux/overflow.h>
2930

3031
#include <net/netfilter/nf_bpf_link.h>
3132

@@ -3957,7 +3958,7 @@ struct btf_record *btf_parse_fields(const struct btf *btf, const struct btf_type
39573958
/* This needs to be kzalloc to zero out padding and unused fields, see
39583959
* comment in btf_record_equal.
39593960
*/
3960-
rec = kzalloc(offsetof(struct btf_record, fields[cnt]), GFP_KERNEL | __GFP_NOWARN);
3961+
rec = kzalloc(struct_size(rec, fields, cnt), GFP_KERNEL | __GFP_NOWARN);
39613962
if (!rec)
39623963
return ERR_PTR(-ENOMEM);
39633964

@@ -5583,7 +5584,7 @@ btf_parse_struct_metas(struct bpf_verifier_log *log, struct btf *btf)
55835584
if (id < 0)
55845585
continue;
55855586

5586-
new_aof = krealloc(aof, offsetof(struct btf_id_set, ids[aof->cnt + 1]),
5587+
new_aof = krealloc(aof, struct_size(new_aof, ids, aof->cnt + 1),
55875588
GFP_KERNEL | __GFP_NOWARN);
55885589
if (!new_aof) {
55895590
ret = -ENOMEM;
@@ -5610,7 +5611,7 @@ btf_parse_struct_metas(struct bpf_verifier_log *log, struct btf *btf)
56105611
if (ret != BTF_FIELD_FOUND)
56115612
continue;
56125613

5613-
new_aof = krealloc(aof, offsetof(struct btf_id_set, ids[aof->cnt + 1]),
5614+
new_aof = krealloc(aof, struct_size(new_aof, ids, aof->cnt + 1),
56145615
GFP_KERNEL | __GFP_NOWARN);
56155616
if (!new_aof) {
56165617
ret = -ENOMEM;
@@ -5647,7 +5648,7 @@ btf_parse_struct_metas(struct bpf_verifier_log *log, struct btf *btf)
56475648
continue;
56485649
parse:
56495650
tab_cnt = tab ? tab->cnt : 0;
5650-
new_tab = krealloc(tab, offsetof(struct btf_struct_metas, types[tab_cnt + 1]),
5651+
new_tab = krealloc(tab, struct_size(new_tab, types, tab_cnt + 1),
56515652
GFP_KERNEL | __GFP_NOWARN);
56525653
if (!new_tab) {
56535654
ret = -ENOMEM;
@@ -8559,7 +8560,7 @@ static int btf_populate_kfunc_set(struct btf *btf, enum btf_kfunc_hook hook,
85598560

85608561
/* Grow set */
85618562
set = krealloc(tab->sets[hook],
8562-
offsetof(struct btf_id_set8, pairs[set_cnt + add_set->cnt]),
8563+
struct_size(set, pairs, set_cnt + add_set->cnt),
85638564
GFP_KERNEL | __GFP_NOWARN);
85648565
if (!set) {
85658566
ret = -ENOMEM;
@@ -8845,7 +8846,7 @@ int register_btf_id_dtor_kfuncs(const struct btf_id_dtor_kfunc *dtors, u32 add_c
88458846
}
88468847

88478848
tab = krealloc(btf->dtor_kfunc_tab,
8848-
offsetof(struct btf_id_dtor_kfunc_tab, dtors[tab_cnt + add_cnt]),
8849+
struct_size(tab, dtors, tab_cnt + add_cnt),
88498850
GFP_KERNEL | __GFP_NOWARN);
88508851
if (!tab) {
88518852
ret = -ENOMEM;
@@ -9403,8 +9404,7 @@ btf_add_struct_ops(struct btf *btf, struct bpf_struct_ops *st_ops,
94039404

94049405
tab = btf->struct_ops_tab;
94059406
if (!tab) {
9406-
tab = kzalloc(offsetof(struct btf_struct_ops_tab, ops[4]),
9407-
GFP_KERNEL);
9407+
tab = kzalloc(struct_size(tab, ops, 4), GFP_KERNEL);
94089408
if (!tab)
94099409
return -ENOMEM;
94109410
tab->capacity = 4;
@@ -9417,8 +9417,7 @@ btf_add_struct_ops(struct btf *btf, struct bpf_struct_ops *st_ops,
94179417

94189418
if (tab->cnt == tab->capacity) {
94199419
new_tab = krealloc(tab,
9420-
offsetof(struct btf_struct_ops_tab,
9421-
ops[tab->capacity * 2]),
9420+
struct_size(tab, ops, tab->capacity * 2),
94229421
GFP_KERNEL);
94239422
if (!new_tab)
94249423
return -ENOMEM;

0 commit comments

Comments
 (0)