mirror of
https://github.com/torvalds/linux.git
synced 2024-11-18 18:11:56 +00:00
fae2708174
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+ #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 <mcroce@redhat.com>
Fixes: 5c5670fae4
("net/sched: Introduce sample tc action")
Signed-off-by: Davide Caratti <dcaratti@redhat.com>
Acked-by: Yotam Gigi <yotam.gi@gmail.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
301 lines
7.6 KiB
C
301 lines
7.6 KiB
C
/*
|
|
* net/sched/act_sample.c - Packet sampling tc action
|
|
* Copyright (c) 2017 Yotam Gigi <yotamg@mellanox.com>
|
|
*
|
|
* This program is free software; you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License version 2 as
|
|
* published by the Free Software Foundation.
|
|
*/
|
|
|
|
#include <linux/types.h>
|
|
#include <linux/kernel.h>
|
|
#include <linux/string.h>
|
|
#include <linux/errno.h>
|
|
#include <linux/skbuff.h>
|
|
#include <linux/rtnetlink.h>
|
|
#include <linux/module.h>
|
|
#include <linux/init.h>
|
|
#include <linux/gfp.h>
|
|
#include <net/net_namespace.h>
|
|
#include <net/netlink.h>
|
|
#include <net/pkt_sched.h>
|
|
#include <linux/tc_act/tc_sample.h>
|
|
#include <net/tc_act/tc_sample.h>
|
|
#include <net/psample.h>
|
|
#include <net/pkt_cls.h>
|
|
|
|
#include <linux/if_arp.h>
|
|
|
|
static unsigned int sample_net_id;
|
|
static struct tc_action_ops act_sample_ops;
|
|
|
|
static const struct nla_policy sample_policy[TCA_SAMPLE_MAX + 1] = {
|
|
[TCA_SAMPLE_PARMS] = { .len = sizeof(struct tc_sample) },
|
|
[TCA_SAMPLE_RATE] = { .type = NLA_U32 },
|
|
[TCA_SAMPLE_TRUNC_SIZE] = { .type = NLA_U32 },
|
|
[TCA_SAMPLE_PSAMPLE_GROUP] = { .type = NLA_U32 },
|
|
};
|
|
|
|
static int tcf_sample_init(struct net *net, struct nlattr *nla,
|
|
struct nlattr *est, struct tc_action **a, int ovr,
|
|
int bind, bool rtnl_held, struct tcf_proto *tp,
|
|
struct netlink_ext_ack *extack)
|
|
{
|
|
struct tc_action_net *tn = net_generic(net, sample_net_id);
|
|
struct nlattr *tb[TCA_SAMPLE_MAX + 1];
|
|
struct psample_group *psample_group;
|
|
struct tcf_chain *goto_ch = NULL;
|
|
u32 psample_group_num, rate;
|
|
struct tc_sample *parm;
|
|
struct tcf_sample *s;
|
|
bool exists = false;
|
|
int ret, err;
|
|
|
|
if (!nla)
|
|
return -EINVAL;
|
|
ret = nla_parse_nested(tb, TCA_SAMPLE_MAX, nla, sample_policy, NULL);
|
|
if (ret < 0)
|
|
return ret;
|
|
if (!tb[TCA_SAMPLE_PARMS] || !tb[TCA_SAMPLE_RATE] ||
|
|
!tb[TCA_SAMPLE_PSAMPLE_GROUP])
|
|
return -EINVAL;
|
|
|
|
parm = nla_data(tb[TCA_SAMPLE_PARMS]);
|
|
|
|
err = tcf_idr_check_alloc(tn, &parm->index, a, bind);
|
|
if (err < 0)
|
|
return err;
|
|
exists = err;
|
|
if (exists && bind)
|
|
return 0;
|
|
|
|
if (!exists) {
|
|
ret = tcf_idr_create(tn, parm->index, est, a,
|
|
&act_sample_ops, bind, true);
|
|
if (ret) {
|
|
tcf_idr_cleanup(tn, parm->index);
|
|
return ret;
|
|
}
|
|
ret = ACT_P_CREATED;
|
|
} else if (!ovr) {
|
|
tcf_idr_release(*a, bind);
|
|
return -EEXIST;
|
|
}
|
|
err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
|
|
if (err < 0)
|
|
goto release_idr;
|
|
|
|
rate = nla_get_u32(tb[TCA_SAMPLE_RATE]);
|
|
if (!rate) {
|
|
NL_SET_ERR_MSG(extack, "invalid sample rate");
|
|
err = -EINVAL;
|
|
goto put_chain;
|
|
}
|
|
psample_group_num = nla_get_u32(tb[TCA_SAMPLE_PSAMPLE_GROUP]);
|
|
psample_group = psample_group_get(net, psample_group_num);
|
|
if (!psample_group) {
|
|
err = -ENOMEM;
|
|
goto put_chain;
|
|
}
|
|
|
|
s = to_sample(*a);
|
|
|
|
spin_lock_bh(&s->tcf_lock);
|
|
goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
|
|
s->rate = rate;
|
|
s->psample_group_num = psample_group_num;
|
|
RCU_INIT_POINTER(s->psample_group, psample_group);
|
|
|
|
if (tb[TCA_SAMPLE_TRUNC_SIZE]) {
|
|
s->truncate = true;
|
|
s->trunc_size = nla_get_u32(tb[TCA_SAMPLE_TRUNC_SIZE]);
|
|
}
|
|
spin_unlock_bh(&s->tcf_lock);
|
|
if (goto_ch)
|
|
tcf_chain_put_by_act(goto_ch);
|
|
|
|
if (ret == ACT_P_CREATED)
|
|
tcf_idr_insert(tn, *a);
|
|
return ret;
|
|
put_chain:
|
|
if (goto_ch)
|
|
tcf_chain_put_by_act(goto_ch);
|
|
release_idr:
|
|
tcf_idr_release(*a, bind);
|
|
return err;
|
|
}
|
|
|
|
static void tcf_sample_cleanup(struct tc_action *a)
|
|
{
|
|
struct tcf_sample *s = to_sample(a);
|
|
struct psample_group *psample_group;
|
|
|
|
/* last reference to action, no need to lock */
|
|
psample_group = rcu_dereference_protected(s->psample_group, 1);
|
|
RCU_INIT_POINTER(s->psample_group, NULL);
|
|
if (psample_group)
|
|
psample_group_put(psample_group);
|
|
}
|
|
|
|
static bool tcf_sample_dev_ok_push(struct net_device *dev)
|
|
{
|
|
switch (dev->type) {
|
|
case ARPHRD_TUNNEL:
|
|
case ARPHRD_TUNNEL6:
|
|
case ARPHRD_SIT:
|
|
case ARPHRD_IPGRE:
|
|
case ARPHRD_VOID:
|
|
case ARPHRD_NONE:
|
|
return false;
|
|
default:
|
|
return true;
|
|
}
|
|
}
|
|
|
|
static int tcf_sample_act(struct sk_buff *skb, const struct tc_action *a,
|
|
struct tcf_result *res)
|
|
{
|
|
struct tcf_sample *s = to_sample(a);
|
|
struct psample_group *psample_group;
|
|
int retval;
|
|
int size;
|
|
int iif;
|
|
int oif;
|
|
|
|
tcf_lastuse_update(&s->tcf_tm);
|
|
bstats_cpu_update(this_cpu_ptr(s->common.cpu_bstats), skb);
|
|
retval = READ_ONCE(s->tcf_action);
|
|
|
|
psample_group = rcu_dereference_bh(s->psample_group);
|
|
|
|
/* randomly sample packets according to rate */
|
|
if (psample_group && (prandom_u32() % s->rate == 0)) {
|
|
if (!skb_at_tc_ingress(skb)) {
|
|
iif = skb->skb_iif;
|
|
oif = skb->dev->ifindex;
|
|
} else {
|
|
iif = skb->dev->ifindex;
|
|
oif = 0;
|
|
}
|
|
|
|
/* on ingress, the mac header gets popped, so push it back */
|
|
if (skb_at_tc_ingress(skb) && tcf_sample_dev_ok_push(skb->dev))
|
|
skb_push(skb, skb->mac_len);
|
|
|
|
size = s->truncate ? s->trunc_size : skb->len;
|
|
psample_sample_packet(psample_group, skb, size, iif, oif,
|
|
s->rate);
|
|
|
|
if (skb_at_tc_ingress(skb) && tcf_sample_dev_ok_push(skb->dev))
|
|
skb_pull(skb, skb->mac_len);
|
|
}
|
|
|
|
return retval;
|
|
}
|
|
|
|
static int tcf_sample_dump(struct sk_buff *skb, struct tc_action *a,
|
|
int bind, int ref)
|
|
{
|
|
unsigned char *b = skb_tail_pointer(skb);
|
|
struct tcf_sample *s = to_sample(a);
|
|
struct tc_sample opt = {
|
|
.index = s->tcf_index,
|
|
.refcnt = refcount_read(&s->tcf_refcnt) - ref,
|
|
.bindcnt = atomic_read(&s->tcf_bindcnt) - bind,
|
|
};
|
|
struct tcf_t t;
|
|
|
|
spin_lock_bh(&s->tcf_lock);
|
|
opt.action = s->tcf_action;
|
|
if (nla_put(skb, TCA_SAMPLE_PARMS, sizeof(opt), &opt))
|
|
goto nla_put_failure;
|
|
|
|
tcf_tm_dump(&t, &s->tcf_tm);
|
|
if (nla_put_64bit(skb, TCA_SAMPLE_TM, sizeof(t), &t, TCA_SAMPLE_PAD))
|
|
goto nla_put_failure;
|
|
|
|
if (nla_put_u32(skb, TCA_SAMPLE_RATE, s->rate))
|
|
goto nla_put_failure;
|
|
|
|
if (s->truncate)
|
|
if (nla_put_u32(skb, TCA_SAMPLE_TRUNC_SIZE, s->trunc_size))
|
|
goto nla_put_failure;
|
|
|
|
if (nla_put_u32(skb, TCA_SAMPLE_PSAMPLE_GROUP, s->psample_group_num))
|
|
goto nla_put_failure;
|
|
spin_unlock_bh(&s->tcf_lock);
|
|
|
|
return skb->len;
|
|
|
|
nla_put_failure:
|
|
spin_unlock_bh(&s->tcf_lock);
|
|
nlmsg_trim(skb, b);
|
|
return -1;
|
|
}
|
|
|
|
static int tcf_sample_walker(struct net *net, struct sk_buff *skb,
|
|
struct netlink_callback *cb, int type,
|
|
const struct tc_action_ops *ops,
|
|
struct netlink_ext_ack *extack)
|
|
{
|
|
struct tc_action_net *tn = net_generic(net, sample_net_id);
|
|
|
|
return tcf_generic_walker(tn, skb, cb, type, ops, extack);
|
|
}
|
|
|
|
static int tcf_sample_search(struct net *net, struct tc_action **a, u32 index)
|
|
{
|
|
struct tc_action_net *tn = net_generic(net, sample_net_id);
|
|
|
|
return tcf_idr_search(tn, a, index);
|
|
}
|
|
|
|
static struct tc_action_ops act_sample_ops = {
|
|
.kind = "sample",
|
|
.id = TCA_ID_SAMPLE,
|
|
.owner = THIS_MODULE,
|
|
.act = tcf_sample_act,
|
|
.dump = tcf_sample_dump,
|
|
.init = tcf_sample_init,
|
|
.cleanup = tcf_sample_cleanup,
|
|
.walk = tcf_sample_walker,
|
|
.lookup = tcf_sample_search,
|
|
.size = sizeof(struct tcf_sample),
|
|
};
|
|
|
|
static __net_init int sample_init_net(struct net *net)
|
|
{
|
|
struct tc_action_net *tn = net_generic(net, sample_net_id);
|
|
|
|
return tc_action_net_init(tn, &act_sample_ops);
|
|
}
|
|
|
|
static void __net_exit sample_exit_net(struct list_head *net_list)
|
|
{
|
|
tc_action_net_exit(net_list, sample_net_id);
|
|
}
|
|
|
|
static struct pernet_operations sample_net_ops = {
|
|
.init = sample_init_net,
|
|
.exit_batch = sample_exit_net,
|
|
.id = &sample_net_id,
|
|
.size = sizeof(struct tc_action_net),
|
|
};
|
|
|
|
static int __init sample_init_module(void)
|
|
{
|
|
return tcf_register_action(&act_sample_ops, &sample_net_ops);
|
|
}
|
|
|
|
static void __exit sample_cleanup_module(void)
|
|
{
|
|
tcf_unregister_action(&act_sample_ops, &sample_net_ops);
|
|
}
|
|
|
|
module_init(sample_init_module);
|
|
module_exit(sample_cleanup_module);
|
|
|
|
MODULE_AUTHOR("Yotam Gigi <yotam.gi@gmail.com>");
|
|
MODULE_DESCRIPTION("Packet sampling action");
|
|
MODULE_LICENSE("GPL v2");
|