Skip to content

Commit 77aec5e

Browse files
GustavoARSilvadavem330
authored andcommitted
net/sched: cls_u32: Use struct_size() helper
Make use of the struct_size() helper, in multiple places, instead of an open-coded version in order to avoid any potential type mistakes and protect against potential integer overflows. Also, remove unnecessary object identifier size. Signed-off-by: Gustavo A. R. Silva <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 26b4b2d commit 77aec5e

File tree

1 file changed

+6
-14
lines changed

1 file changed

+6
-14
lines changed

net/sched/cls_u32.c

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -852,9 +852,6 @@ static int u32_change(struct net *net, struct sk_buff *in_skb,
852852
u32 htid, flags = 0;
853853
size_t sel_size;
854854
int err;
855-
#ifdef CONFIG_CLS_U32_PERF
856-
size_t size;
857-
#endif
858855

859856
if (!opt) {
860857
if (handle) {
@@ -1022,15 +1019,15 @@ static int u32_change(struct net *net, struct sk_buff *in_skb,
10221019
goto erridr;
10231020
}
10241021

1025-
n = kzalloc(offsetof(typeof(*n), sel) + sel_size, GFP_KERNEL);
1022+
n = kzalloc(struct_size(n, sel.keys, s->nkeys), GFP_KERNEL);
10261023
if (n == NULL) {
10271024
err = -ENOBUFS;
10281025
goto erridr;
10291026
}
10301027

10311028
#ifdef CONFIG_CLS_U32_PERF
1032-
size = sizeof(struct tc_u32_pcnt) + s->nkeys * sizeof(u64);
1033-
n->pf = __alloc_percpu(size, __alignof__(struct tc_u32_pcnt));
1029+
n->pf = __alloc_percpu(struct_size(n->pf, kcnts, s->nkeys),
1030+
__alignof__(struct tc_u32_pcnt));
10341031
if (!n->pf) {
10351032
err = -ENOBUFS;
10361033
goto errfree;
@@ -1294,8 +1291,7 @@ static int u32_dump(struct net *net, struct tcf_proto *tp, void *fh,
12941291
int cpu;
12951292
#endif
12961293

1297-
if (nla_put(skb, TCA_U32_SEL,
1298-
sizeof(n->sel) + n->sel.nkeys*sizeof(struct tc_u32_key),
1294+
if (nla_put(skb, TCA_U32_SEL, struct_size(&n->sel, keys, n->sel.nkeys),
12991295
&n->sel))
13001296
goto nla_put_failure;
13011297

@@ -1345,9 +1341,7 @@ static int u32_dump(struct net *net, struct tcf_proto *tp, void *fh,
13451341
goto nla_put_failure;
13461342
}
13471343
#ifdef CONFIG_CLS_U32_PERF
1348-
gpf = kzalloc(sizeof(struct tc_u32_pcnt) +
1349-
n->sel.nkeys * sizeof(u64),
1350-
GFP_KERNEL);
1344+
gpf = kzalloc(struct_size(gpf, kcnts, n->sel.nkeys), GFP_KERNEL);
13511345
if (!gpf)
13521346
goto nla_put_failure;
13531347

@@ -1361,9 +1355,7 @@ static int u32_dump(struct net *net, struct tcf_proto *tp, void *fh,
13611355
gpf->kcnts[i] += pf->kcnts[i];
13621356
}
13631357

1364-
if (nla_put_64bit(skb, TCA_U32_PCNT,
1365-
sizeof(struct tc_u32_pcnt) +
1366-
n->sel.nkeys * sizeof(u64),
1358+
if (nla_put_64bit(skb, TCA_U32_PCNT, struct_size(gpf, kcnts, n->sel.nkeys),
13671359
gpf, TCA_U32_PAD)) {
13681360
kfree(gpf);
13691361
goto nla_put_failure;

0 commit comments

Comments
 (0)