26
26
#include <linux/bsearch.h>
27
27
#include <linux/kobject.h>
28
28
#include <linux/sysfs.h>
29
+ #include <linux/overflow.h>
29
30
30
31
#include <net/netfilter/nf_bpf_link.h>
31
32
@@ -3957,7 +3958,7 @@ struct btf_record *btf_parse_fields(const struct btf *btf, const struct btf_type
3957
3958
/* This needs to be kzalloc to zero out padding and unused fields, see
3958
3959
* comment in btf_record_equal.
3959
3960
*/
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 );
3961
3962
if (!rec )
3962
3963
return ERR_PTR (- ENOMEM );
3963
3964
@@ -5583,7 +5584,7 @@ btf_parse_struct_metas(struct bpf_verifier_log *log, struct btf *btf)
5583
5584
if (id < 0 )
5584
5585
continue ;
5585
5586
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 ),
5587
5588
GFP_KERNEL | __GFP_NOWARN );
5588
5589
if (!new_aof ) {
5589
5590
ret = - ENOMEM ;
@@ -5610,7 +5611,7 @@ btf_parse_struct_metas(struct bpf_verifier_log *log, struct btf *btf)
5610
5611
if (ret != BTF_FIELD_FOUND )
5611
5612
continue ;
5612
5613
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 ),
5614
5615
GFP_KERNEL | __GFP_NOWARN );
5615
5616
if (!new_aof ) {
5616
5617
ret = - ENOMEM ;
@@ -5647,7 +5648,7 @@ btf_parse_struct_metas(struct bpf_verifier_log *log, struct btf *btf)
5647
5648
continue ;
5648
5649
parse :
5649
5650
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 ),
5651
5652
GFP_KERNEL | __GFP_NOWARN );
5652
5653
if (!new_tab ) {
5653
5654
ret = - ENOMEM ;
@@ -8559,7 +8560,7 @@ static int btf_populate_kfunc_set(struct btf *btf, enum btf_kfunc_hook hook,
8559
8560
8560
8561
/* Grow set */
8561
8562
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 ),
8563
8564
GFP_KERNEL | __GFP_NOWARN );
8564
8565
if (!set ) {
8565
8566
ret = - ENOMEM ;
@@ -8845,7 +8846,7 @@ int register_btf_id_dtor_kfuncs(const struct btf_id_dtor_kfunc *dtors, u32 add_c
8845
8846
}
8846
8847
8847
8848
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 ),
8849
8850
GFP_KERNEL | __GFP_NOWARN );
8850
8851
if (!tab ) {
8851
8852
ret = - ENOMEM ;
@@ -9403,8 +9404,7 @@ btf_add_struct_ops(struct btf *btf, struct bpf_struct_ops *st_ops,
9403
9404
9404
9405
tab = btf -> struct_ops_tab ;
9405
9406
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 );
9408
9408
if (!tab )
9409
9409
return - ENOMEM ;
9410
9410
tab -> capacity = 4 ;
@@ -9417,8 +9417,7 @@ btf_add_struct_ops(struct btf *btf, struct bpf_struct_ops *st_ops,
9417
9417
9418
9418
if (tab -> cnt == tab -> capacity ) {
9419
9419
new_tab = krealloc (tab ,
9420
- offsetof(struct btf_struct_ops_tab ,
9421
- ops [tab -> capacity * 2 ]),
9420
+ struct_size (tab , ops , tab -> capacity * 2 ),
9422
9421
GFP_KERNEL );
9423
9422
if (!new_tab )
9424
9423
return - ENOMEM ;
0 commit comments