Skip to content

Commit 6fa8c01

Browse files
kaberdavem330
authored andcommitted
[NET_SCHED]: Use nla_policy for attribute validation in classifiers
Signed-off-by: Patrick McHardy <kaber@trash.net> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 27a3421 commit 6fa8c01

6 files changed

Lines changed: 59 additions & 68 deletions

File tree

‎net/sched/cls_basic.c‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,11 @@ static int basic_delete(struct tcf_proto *tp, unsigned long arg)
129129
return -ENOENT;
130130
}
131131

132+
static const struct nla_policy basic_policy[TCA_BASIC_MAX + 1] = {
133+
[TCA_BASIC_CLASSID] = { .type = NLA_U32 },
134+
[TCA_BASIC_EMATCHES] = { .type = NLA_NESTED },
135+
};
136+
132137
static inline int basic_set_parms(struct tcf_proto *tp, struct basic_filter *f,
133138
unsigned long base, struct nlattr **tb,
134139
struct nlattr *est)
@@ -137,10 +142,6 @@ static inline int basic_set_parms(struct tcf_proto *tp, struct basic_filter *f,
137142
struct tcf_exts e;
138143
struct tcf_ematch_tree t;
139144

140-
if (tb[TCA_BASIC_CLASSID])
141-
if (nla_len(tb[TCA_BASIC_CLASSID]) < sizeof(u32))
142-
return err;
143-
144145
err = tcf_exts_validate(tp, tb, est, &e, &basic_ext_map);
145146
if (err < 0)
146147
return err;
@@ -174,7 +175,8 @@ static int basic_change(struct tcf_proto *tp, unsigned long base, u32 handle,
174175
if (tca[TCA_OPTIONS] == NULL)
175176
return -EINVAL;
176177

177-
err = nla_parse_nested(tb, TCA_BASIC_MAX, tca[TCA_OPTIONS], NULL);
178+
err = nla_parse_nested(tb, TCA_BASIC_MAX, tca[TCA_OPTIONS],
179+
basic_policy);
178180
if (err < 0)
179181
return err;
180182

‎net/sched/cls_fw.c‎

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,12 @@ static int fw_delete(struct tcf_proto *tp, unsigned long arg)
186186
return -EINVAL;
187187
}
188188

189+
static const struct nla_policy fw_policy[TCA_FW_MAX + 1] = {
190+
[TCA_FW_CLASSID] = { .type = NLA_U32 },
191+
[TCA_FW_INDEV] = { .type = NLA_STRING, .len = IFNAMSIZ },
192+
[TCA_FW_MASK] = { .type = NLA_U32 },
193+
};
194+
189195
static int
190196
fw_change_attrs(struct tcf_proto *tp, struct fw_filter *f,
191197
struct nlattr **tb, struct nlattr **tca, unsigned long base)
@@ -201,8 +207,6 @@ fw_change_attrs(struct tcf_proto *tp, struct fw_filter *f,
201207

202208
err = -EINVAL;
203209
if (tb[TCA_FW_CLASSID]) {
204-
if (nla_len(tb[TCA_FW_CLASSID]) != sizeof(u32))
205-
goto errout;
206210
f->res.classid = nla_get_u32(tb[TCA_FW_CLASSID]);
207211
tcf_bind_filter(tp, &f->res, base);
208212
}
@@ -216,8 +220,6 @@ fw_change_attrs(struct tcf_proto *tp, struct fw_filter *f,
216220
#endif /* CONFIG_NET_CLS_IND */
217221

218222
if (tb[TCA_FW_MASK]) {
219-
if (nla_len(tb[TCA_FW_MASK]) != sizeof(u32))
220-
goto errout;
221223
mask = nla_get_u32(tb[TCA_FW_MASK]);
222224
if (mask != head->mask)
223225
goto errout;
@@ -246,7 +248,7 @@ static int fw_change(struct tcf_proto *tp, unsigned long base,
246248
if (!opt)
247249
return handle ? -EINVAL : 0;
248250

249-
err = nla_parse_nested(tb, TCA_FW_MAX, opt, NULL);
251+
err = nla_parse_nested(tb, TCA_FW_MAX, opt, fw_policy);
250252
if (err < 0)
251253
return err;
252254

@@ -261,11 +263,8 @@ static int fw_change(struct tcf_proto *tp, unsigned long base,
261263

262264
if (head == NULL) {
263265
u32 mask = 0xFFFFFFFF;
264-
if (tb[TCA_FW_MASK]) {
265-
if (nla_len(tb[TCA_FW_MASK]) != sizeof(u32))
266-
return -EINVAL;
266+
if (tb[TCA_FW_MASK])
267267
mask = nla_get_u32(tb[TCA_FW_MASK]);
268-
}
269268

270269
head = kzalloc(sizeof(struct fw_head), GFP_KERNEL);
271270
if (head == NULL)

‎net/sched/cls_route.c‎

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,13 @@ static int route4_delete(struct tcf_proto *tp, unsigned long arg)
323323
return 0;
324324
}
325325

326+
static const struct nla_policy route4_policy[TCA_ROUTE4_MAX + 1] = {
327+
[TCA_ROUTE4_CLASSID] = { .type = NLA_U32 },
328+
[TCA_ROUTE4_TO] = { .type = NLA_U32 },
329+
[TCA_ROUTE4_FROM] = { .type = NLA_U32 },
330+
[TCA_ROUTE4_IIF] = { .type = NLA_U32 },
331+
};
332+
326333
static int route4_set_parms(struct tcf_proto *tp, unsigned long base,
327334
struct route4_filter *f, u32 handle, struct route4_head *head,
328335
struct nlattr **tb, struct nlattr *est, int new)
@@ -339,15 +346,9 @@ static int route4_set_parms(struct tcf_proto *tp, unsigned long base,
339346
return err;
340347

341348
err = -EINVAL;
342-
if (tb[TCA_ROUTE4_CLASSID])
343-
if (nla_len(tb[TCA_ROUTE4_CLASSID]) < sizeof(u32))
344-
goto errout;
345-
346349
if (tb[TCA_ROUTE4_TO]) {
347350
if (new && handle & 0x8000)
348351
goto errout;
349-
if (nla_len(tb[TCA_ROUTE4_TO]) < sizeof(u32))
350-
goto errout;
351352
to = nla_get_u32(tb[TCA_ROUTE4_TO]);
352353
if (to > 0xFF)
353354
goto errout;
@@ -357,15 +358,11 @@ static int route4_set_parms(struct tcf_proto *tp, unsigned long base,
357358
if (tb[TCA_ROUTE4_FROM]) {
358359
if (tb[TCA_ROUTE4_IIF])
359360
goto errout;
360-
if (nla_len(tb[TCA_ROUTE4_FROM]) < sizeof(u32))
361-
goto errout;
362361
id = nla_get_u32(tb[TCA_ROUTE4_FROM]);
363362
if (id > 0xFF)
364363
goto errout;
365364
nhandle |= id << 16;
366365
} else if (tb[TCA_ROUTE4_IIF]) {
367-
if (nla_len(tb[TCA_ROUTE4_IIF]) < sizeof(u32))
368-
goto errout;
369366
id = nla_get_u32(tb[TCA_ROUTE4_IIF]);
370367
if (id > 0x7FFF)
371368
goto errout;
@@ -440,7 +437,7 @@ static int route4_change(struct tcf_proto *tp, unsigned long base,
440437
if (opt == NULL)
441438
return handle ? -EINVAL : 0;
442439

443-
err = nla_parse_nested(tb, TCA_ROUTE4_MAX, opt, NULL);
440+
err = nla_parse_nested(tb, TCA_ROUTE4_MAX, opt, route4_policy);
444441
if (err < 0)
445442
return err;
446443

‎net/sched/cls_rsvp.h‎

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,15 @@ static u32 gen_tunnel(struct rsvp_head *data)
397397
return 0;
398398
}
399399

400+
static const struct nla_policy rsvp_policy[TCA_RSVP_MAX + 1] = {
401+
[TCA_RSVP_CLASSID] = { .type = NLA_U32 },
402+
[TCA_RSVP_DST] = { .type = NLA_BINARY,
403+
.len = RSVP_DST_LEN * sizeof(u32) },
404+
[TCA_RSVP_SRC] = { .type = NLA_BINARY,
405+
.len = RSVP_DST_LEN * sizeof(u32) },
406+
[TCA_RSVP_PINFO] = { .len = sizeof(struct tc_rsvp_pinfo) },
407+
};
408+
400409
static int rsvp_change(struct tcf_proto *tp, unsigned long base,
401410
u32 handle,
402411
struct nlattr **tca,
@@ -416,7 +425,7 @@ static int rsvp_change(struct tcf_proto *tp, unsigned long base,
416425
if (opt == NULL)
417426
return handle ? -EINVAL : 0;
418427

419-
err = nla_parse_nested(tb, TCA_RSVP_MAX, opt, NULL);
428+
err = nla_parse_nested(tb, TCA_RSVP_MAX, opt, rsvp_policy);
420429
if (err < 0)
421430
return err;
422431

@@ -452,30 +461,17 @@ static int rsvp_change(struct tcf_proto *tp, unsigned long base,
452461

453462
h2 = 16;
454463
if (tb[TCA_RSVP_SRC-1]) {
455-
err = -EINVAL;
456-
if (nla_len(tb[TCA_RSVP_SRC-1]) != sizeof(f->src))
457-
goto errout;
458464
memcpy(f->src, nla_data(tb[TCA_RSVP_SRC-1]), sizeof(f->src));
459465
h2 = hash_src(f->src);
460466
}
461467
if (tb[TCA_RSVP_PINFO-1]) {
462-
err = -EINVAL;
463-
if (nla_len(tb[TCA_RSVP_PINFO-1]) < sizeof(struct tc_rsvp_pinfo))
464-
goto errout;
465468
pinfo = nla_data(tb[TCA_RSVP_PINFO-1]);
466469
f->spi = pinfo->spi;
467470
f->tunnelhdr = pinfo->tunnelhdr;
468471
}
469-
if (tb[TCA_RSVP_CLASSID-1]) {
470-
err = -EINVAL;
471-
if (nla_len(tb[TCA_RSVP_CLASSID-1]) != 4)
472-
goto errout;
472+
if (tb[TCA_RSVP_CLASSID-1])
473473
f->res.classid = nla_get_u32(tb[TCA_RSVP_CLASSID-1]);
474-
}
475474

476-
err = -EINVAL;
477-
if (nla_len(tb[TCA_RSVP_DST-1]) != sizeof(f->src))
478-
goto errout;
479475
dst = nla_data(tb[TCA_RSVP_DST-1]);
480476
h1 = hash_dst(dst, pinfo ? pinfo->protocol : 0, pinfo ? pinfo->tunnelid : 0);
481477

‎net/sched/cls_tcindex.c‎

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,14 @@ valid_perfect_hash(struct tcindex_data *p)
193193
return p->hash > (p->mask >> p->shift);
194194
}
195195

196+
static const struct nla_policy tcindex_policy[TCA_TCINDEX_MAX + 1] = {
197+
[TCA_TCINDEX_HASH] = { .type = NLA_U32 },
198+
[TCA_TCINDEX_MASK] = { .type = NLA_U16 },
199+
[TCA_TCINDEX_SHIFT] = { .type = NLA_U32 },
200+
[TCA_TCINDEX_FALL_THROUGH] = { .type = NLA_U32 },
201+
[TCA_TCINDEX_CLASSID] = { .type = NLA_U32 },
202+
};
203+
196204
static int
197205
tcindex_set_parms(struct tcf_proto *tp, unsigned long base, u32 handle,
198206
struct tcindex_data *p, struct tcindex_filter_result *r,
@@ -217,24 +225,14 @@ tcindex_set_parms(struct tcf_proto *tp, unsigned long base, u32 handle,
217225
else
218226
memset(&cr, 0, sizeof(cr));
219227

220-
err = -EINVAL;
221-
if (tb[TCA_TCINDEX_HASH]) {
222-
if (nla_len(tb[TCA_TCINDEX_HASH]) < sizeof(u32))
223-
goto errout;
228+
if (tb[TCA_TCINDEX_HASH])
224229
cp.hash = nla_get_u32(tb[TCA_TCINDEX_HASH]);
225-
}
226230

227-
if (tb[TCA_TCINDEX_MASK]) {
228-
if (nla_len(tb[TCA_TCINDEX_MASK]) < sizeof(u16))
229-
goto errout;
231+
if (tb[TCA_TCINDEX_MASK])
230232
cp.mask = nla_get_u16(tb[TCA_TCINDEX_MASK]);
231-
}
232233

233-
if (tb[TCA_TCINDEX_SHIFT]) {
234-
if (nla_len(tb[TCA_TCINDEX_SHIFT]) < sizeof(int))
235-
goto errout;
234+
if (tb[TCA_TCINDEX_SHIFT])
236235
cp.shift = nla_get_u32(tb[TCA_TCINDEX_SHIFT]);
237-
}
238236

239237
err = -EBUSY;
240238
/* Hash already allocated, make sure that we still meet the
@@ -248,11 +246,8 @@ tcindex_set_parms(struct tcf_proto *tp, unsigned long base, u32 handle,
248246
goto errout;
249247

250248
err = -EINVAL;
251-
if (tb[TCA_TCINDEX_FALL_THROUGH]) {
252-
if (nla_len(tb[TCA_TCINDEX_FALL_THROUGH]) < sizeof(u32))
253-
goto errout;
249+
if (tb[TCA_TCINDEX_FALL_THROUGH])
254250
cp.fall_through = nla_get_u32(tb[TCA_TCINDEX_FALL_THROUGH]);
255-
}
256251

257252
if (!cp.hash) {
258253
/* Hash not specified, use perfect hash if the upper limit
@@ -358,7 +353,7 @@ tcindex_change(struct tcf_proto *tp, unsigned long base, u32 handle,
358353
if (!opt)
359354
return 0;
360355

361-
err = nla_parse_nested(tb, TCA_TCINDEX_MAX, opt, NULL);
356+
err = nla_parse_nested(tb, TCA_TCINDEX_MAX, opt, tcindex_policy);
362357
if (err < 0)
363358
return err;
364359

‎net/sched/cls_u32.c‎

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,16 @@ static u32 gen_new_kid(struct tc_u_hnode *ht, u32 handle)
460460
return handle|(i>0xFFF ? 0xFFF : i);
461461
}
462462

463+
static const struct nla_policy u32_policy[TCA_U32_MAX + 1] = {
464+
[TCA_U32_CLASSID] = { .type = NLA_U32 },
465+
[TCA_U32_HASH] = { .type = NLA_U32 },
466+
[TCA_U32_LINK] = { .type = NLA_U32 },
467+
[TCA_U32_DIVISOR] = { .type = NLA_U32 },
468+
[TCA_U32_SEL] = { .len = sizeof(struct tc_u32_sel) },
469+
[TCA_U32_INDEV] = { .type = NLA_STRING, .len = IFNAMSIZ },
470+
[TCA_U32_MARK] = { .len = sizeof(struct tc_u32_mark) },
471+
};
472+
463473
static int u32_set_parms(struct tcf_proto *tp, unsigned long base,
464474
struct tc_u_hnode *ht,
465475
struct tc_u_knode *n, struct nlattr **tb,
@@ -531,7 +541,7 @@ static int u32_change(struct tcf_proto *tp, unsigned long base, u32 handle,
531541
if (opt == NULL)
532542
return handle ? -EINVAL : 0;
533543

534-
err = nla_parse_nested(tb, TCA_U32_MAX, opt, NULL);
544+
err = nla_parse_nested(tb, TCA_U32_MAX, opt, u32_policy);
535545
if (err < 0)
536546
return err;
537547

@@ -593,8 +603,7 @@ static int u32_change(struct tcf_proto *tp, unsigned long base, u32 handle,
593603
} else
594604
handle = gen_new_kid(ht, htid);
595605

596-
if (tb[TCA_U32_SEL] == NULL ||
597-
nla_len(tb[TCA_U32_SEL]) < sizeof(struct tc_u32_sel))
606+
if (tb[TCA_U32_SEL] == NULL)
598607
return -EINVAL;
599608

600609
s = nla_data(tb[TCA_U32_SEL]);
@@ -620,13 +629,6 @@ static int u32_change(struct tcf_proto *tp, unsigned long base, u32 handle,
620629
if (tb[TCA_U32_MARK]) {
621630
struct tc_u32_mark *mark;
622631

623-
if (nla_len(tb[TCA_U32_MARK]) < sizeof(struct tc_u32_mark)) {
624-
#ifdef CONFIG_CLS_U32_PERF
625-
kfree(n->pf);
626-
#endif
627-
kfree(n);
628-
return -EINVAL;
629-
}
630632
mark = nla_data(tb[TCA_U32_MARK]);
631633
memcpy(&n->mark, mark, sizeof(struct tc_u32_mark));
632634
n->mark.success = 0;

0 commit comments

Comments
 (0)