Skip to content

Commit fae2708

Browse files
dcarattidavem330
authored andcommitted
net/sched: act_sample: fix divide by zero in the traffic path
the control path of 'sample' action does not validate the value of 'rate' provided by the user, but then it uses it as divisor in the traffic path. Validate it in tcf_sample_init(), and return -EINVAL with a proper extack message in case that value is zero, to fix a splat with the script below: # tc f a dev test0 egress matchall action sample rate 0 group 1 index 2 # tc -s a s action sample total acts 1 action order 0: sample rate 1/0 group 1 pipe index 2 ref 1 bind 1 installed 19 sec used 19 sec Action statistics: Sent 0 bytes 0 pkt (dropped 0, overlimits 0 requeues 0) backlog 0b 0p requeues 0 # ping 192.0.2.1 -I test0 -c1 -q divide error: 0000 [#1] SMP PTI CPU: 1 PID: 6192 Comm: ping Not tainted 5.1.0-rc2.diag2+ raspberrypi#591 Hardware name: Red Hat KVM, BIOS 0.5.1 01/01/2011 RIP: 0010:tcf_sample_act+0x9e/0x1e0 [act_sample] Code: 6a f1 85 c0 74 0d 80 3d 83 1a 00 00 00 0f 84 9c 00 00 00 4d 85 e4 0f 84 85 00 00 00 e8 9b d7 9c f1 44 8b 8b e0 00 00 00 31 d2 <41> f7 f1 85 d2 75 70 f6 85 83 00 00 00 10 48 8b 45 10 8b 88 08 01 RSP: 0018:ffffae320190ba30 EFLAGS: 00010246 RAX: 00000000b0677d21 RBX: ffff8af1ed9ec000 RCX: 0000000059a9fe49 RDX: 0000000000000000 RSI: 000000000c7e33b7 RDI: ffff8af23daa0af0 RBP: ffff8af1ee11b200 R08: 0000000074fcaf7e R09: 0000000000000000 R10: 0000000000000050 R11: ffffffffb3088680 R12: ffff8af232307f80 R13: 0000000000000003 R14: ffff8af1ed9ec000 R15: 0000000000000000 FS: 00007fe9c6d2f740(0000) GS:ffff8af23da80000(0000) knlGS:0000000000000000 CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 CR2: 00007fff6772f000 CR3: 00000000746a2004 CR4: 00000000001606e0 Call Trace: tcf_action_exec+0x7c/0x1c0 tcf_classify+0x57/0x160 __dev_queue_xmit+0x3dc/0xd10 ip_finish_output2+0x257/0x6d0 ip_output+0x75/0x280 ip_send_skb+0x15/0x40 raw_sendmsg+0xae3/0x1410 sock_sendmsg+0x36/0x40 __sys_sendto+0x10e/0x140 __x64_sys_sendto+0x24/0x30 do_syscall_64+0x60/0x210 entry_SYSCALL_64_after_hwframe+0x49/0xbe [...] Kernel panic - not syncing: Fatal exception in interrupt Add a TDC selftest to document that 'rate' is now being validated. Reported-by: Matteo Croce <[email protected]> Fixes: 5c5670f ("net/sched: Introduce sample tc action") Signed-off-by: Davide Caratti <[email protected]> Acked-by: Yotam Gigi <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 2ec1ed2 commit fae2708

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

net/sched/act_sample.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ static int tcf_sample_init(struct net *net, struct nlattr *nla,
4545
struct nlattr *tb[TCA_SAMPLE_MAX + 1];
4646
struct psample_group *psample_group;
4747
struct tcf_chain *goto_ch = NULL;
48+
u32 psample_group_num, rate;
4849
struct tc_sample *parm;
49-
u32 psample_group_num;
5050
struct tcf_sample *s;
5151
bool exists = false;
5252
int ret, err;
@@ -85,6 +85,12 @@ static int tcf_sample_init(struct net *net, struct nlattr *nla,
8585
if (err < 0)
8686
goto release_idr;
8787

88+
rate = nla_get_u32(tb[TCA_SAMPLE_RATE]);
89+
if (!rate) {
90+
NL_SET_ERR_MSG(extack, "invalid sample rate");
91+
err = -EINVAL;
92+
goto put_chain;
93+
}
8894
psample_group_num = nla_get_u32(tb[TCA_SAMPLE_PSAMPLE_GROUP]);
8995
psample_group = psample_group_get(net, psample_group_num);
9096
if (!psample_group) {
@@ -96,7 +102,7 @@ static int tcf_sample_init(struct net *net, struct nlattr *nla,
96102

97103
spin_lock_bh(&s->tcf_lock);
98104
goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
99-
s->rate = nla_get_u32(tb[TCA_SAMPLE_RATE]);
105+
s->rate = rate;
100106
s->psample_group_num = psample_group_num;
101107
RCU_INIT_POINTER(s->psample_group, psample_group);
102108

tools/testing/selftests/tc-testing/tc-tests/actions/sample.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,30 @@
143143
"$TC actions flush action sample"
144144
]
145145
},
146+
{
147+
"id": "7571",
148+
"name": "Add sample action with invalid rate",
149+
"category": [
150+
"actions",
151+
"sample"
152+
],
153+
"setup": [
154+
[
155+
"$TC actions flush action sample",
156+
0,
157+
1,
158+
255
159+
]
160+
],
161+
"cmdUnderTest": "$TC actions add action sample rate 0 group 1 index 2",
162+
"expExitCode": "255",
163+
"verifyCmd": "$TC actions get action sample index 2",
164+
"matchPattern": "action order [0-9]+: sample rate 1/0 group 1.*index 2 ref",
165+
"matchCount": "0",
166+
"teardown": [
167+
"$TC actions flush action sample"
168+
]
169+
},
146170
{
147171
"id": "b6d4",
148172
"name": "Add sample action with mandatory arguments and invalid control action",

0 commit comments

Comments
 (0)