Commit 77aec5e1 authored by Gustavo A. R. Silva's avatar Gustavo A. R. Silva Committed by David S. Miller

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: default avatarGustavo A. R. Silva <gustavoars@kernel.org>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent 26b4b2d9
...@@ -852,9 +852,6 @@ static int u32_change(struct net *net, struct sk_buff *in_skb, ...@@ -852,9 +852,6 @@ static int u32_change(struct net *net, struct sk_buff *in_skb,
u32 htid, flags = 0; u32 htid, flags = 0;
size_t sel_size; size_t sel_size;
int err; int err;
#ifdef CONFIG_CLS_U32_PERF
size_t size;
#endif
if (!opt) { if (!opt) {
if (handle) { if (handle) {
...@@ -1022,15 +1019,15 @@ static int u32_change(struct net *net, struct sk_buff *in_skb, ...@@ -1022,15 +1019,15 @@ static int u32_change(struct net *net, struct sk_buff *in_skb,
goto erridr; goto erridr;
} }
n = kzalloc(offsetof(typeof(*n), sel) + sel_size, GFP_KERNEL); n = kzalloc(struct_size(n, sel.keys, s->nkeys), GFP_KERNEL);
if (n == NULL) { if (n == NULL) {
err = -ENOBUFS; err = -ENOBUFS;
goto erridr; goto erridr;
} }
#ifdef CONFIG_CLS_U32_PERF #ifdef CONFIG_CLS_U32_PERF
size = sizeof(struct tc_u32_pcnt) + s->nkeys * sizeof(u64); n->pf = __alloc_percpu(struct_size(n->pf, kcnts, s->nkeys),
n->pf = __alloc_percpu(size, __alignof__(struct tc_u32_pcnt)); __alignof__(struct tc_u32_pcnt));
if (!n->pf) { if (!n->pf) {
err = -ENOBUFS; err = -ENOBUFS;
goto errfree; goto errfree;
...@@ -1294,8 +1291,7 @@ static int u32_dump(struct net *net, struct tcf_proto *tp, void *fh, ...@@ -1294,8 +1291,7 @@ static int u32_dump(struct net *net, struct tcf_proto *tp, void *fh,
int cpu; int cpu;
#endif #endif
if (nla_put(skb, TCA_U32_SEL, if (nla_put(skb, TCA_U32_SEL, struct_size(&n->sel, keys, n->sel.nkeys),
sizeof(n->sel) + n->sel.nkeys*sizeof(struct tc_u32_key),
&n->sel)) &n->sel))
goto nla_put_failure; goto nla_put_failure;
...@@ -1345,9 +1341,7 @@ static int u32_dump(struct net *net, struct tcf_proto *tp, void *fh, ...@@ -1345,9 +1341,7 @@ static int u32_dump(struct net *net, struct tcf_proto *tp, void *fh,
goto nla_put_failure; goto nla_put_failure;
} }
#ifdef CONFIG_CLS_U32_PERF #ifdef CONFIG_CLS_U32_PERF
gpf = kzalloc(sizeof(struct tc_u32_pcnt) + gpf = kzalloc(struct_size(gpf, kcnts, n->sel.nkeys), GFP_KERNEL);
n->sel.nkeys * sizeof(u64),
GFP_KERNEL);
if (!gpf) if (!gpf)
goto nla_put_failure; goto nla_put_failure;
...@@ -1361,9 +1355,7 @@ static int u32_dump(struct net *net, struct tcf_proto *tp, void *fh, ...@@ -1361,9 +1355,7 @@ static int u32_dump(struct net *net, struct tcf_proto *tp, void *fh,
gpf->kcnts[i] += pf->kcnts[i]; gpf->kcnts[i] += pf->kcnts[i];
} }
if (nla_put_64bit(skb, TCA_U32_PCNT, if (nla_put_64bit(skb, TCA_U32_PCNT, struct_size(gpf, kcnts, n->sel.nkeys),
sizeof(struct tc_u32_pcnt) +
n->sel.nkeys * sizeof(u64),
gpf, TCA_U32_PAD)) { gpf, TCA_U32_PAD)) {
kfree(gpf); kfree(gpf);
goto nla_put_failure; goto nla_put_failure;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment