A filter can bind an existing action by index. If that action was created by a filter of another block with a "goto chain" control action, it still holds a chain of that other block: the bind path returns before any control action handling and leaves goto_chain as is. tcf_action_exec() then sends classification of the binding filter into a chain of the other block. Filters of that chain may carry a tcf_result class bound to the qdisc owning the other block. The qdisc classifying the packet uses that class pointer as one of its own classes, which crashes when the two qdiscs differ, e.g. an htb qdisc reading a drr class. Before commit 85d0966fa57e ("net/sched: prepare TC actions to properly validate the control action"), the generic code instead re-pointed goto_chain to the binding block on every bind, which made the original filter jump across blocks. Reject the bind in tcf_action_init_1() when the action's goto chain belongs to a block other than the one of the binding filter. Doing it there covers the legacy police path of tcf_exts_validate_ex() too. Binding an action whose goto chain is in the same block is unchanged. The issue was discovered by an AI code review agent and confirmed by triggering it on an unmodified kernel built with KASAN, as an unprivileged user in a new user and network namespace: KASAN reports a slab-out-of-bounds read in htb_enqueue() on a drr class, followed by a general protection fault. Reproducing it did not need any kernel modification or error injection. A reproducer is available and will be sent to the maintainers privately. The fix was tested with that reproducer, which no longer triggers, and with the tc-testing selftests for gact, matchall, police, hfsc, qfq, drr and ets, including the cases added in the next patch. The fix was written with the help of the same AI agent. Fixes: db50514f9a9c ("net: sched: add termination action to allow goto chain") Cc: stab…@vger.kernel.org Signed-off-by: Muhammad Umer <mume…@gmail.com> --- net/sched/act_api.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/net/sched/act_api.c b/net/sched/act_api.c index e142d33dc5e..78d72568f8f 100644 --- a/net/sched/act_api.c +++ b/net/sched/act_api.c @@ -1421,6 +1421,20 @@ struct tc_action_ops *tc_action_load_ops(struct nlattr *nla, u32 flags, return a_o; } +static bool tcf_action_goto_chain_foreign(const struct tc_action *a, + const struct tcf_proto *tp) +{ + const struct tcf_chain *chain; + bool foreign; + + rcu_read_lock(); + chain = rcu_dereference(a->goto_chain); + foreign = chain && chain->block != tp->chain->block; + rcu_read_unlock(); + + return foreign; +} + struct tc_action *tcf_action_init_1(struct net *net, struct tcf_proto *tp, struct nlattr *nla, struct nlattr *est, struct tc_action_ops *a_o, int *init_res, @@ -1467,6 +1481,20 @@ struct tc_action *tcf_action_init_1(struct net *net, struct tcf_proto *tp, goto err_out; *init_res = err; + /* A filter may bind an existing action by index. If that action + * jumps to a chain of another block, the filter would continue + * classification there, and the qdisc owning this filter would + * receive tcf_result class pointers of another qdisc. + */ + if ((flags & TCA_ACT_FLAGS_BIND) && tp && err == ACT_P_BOUND && + tcf_action_goto_chain_foreign(a, tp)) { + NL_SET_ERR_MSG(extack, + "Action jumps to a chain of a different block"); + tcf_idr_release(a, true); + err = -EINVAL; + goto err_out; + } + if (!police && tb[TCA_ACT_COOKIE]) tcf_set_action_cookie(&a->user_cookie, user_cookie);
-- 2.53.0