mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git
synced 2024-08-26 10:49:33 +00:00
![Taehee Yoo](/assets/img/avatar_default.png)
basechain->stats is rcu protected data which is updated from
nft_chain_stats_replace(). This function is executed from the commit
phase which holds the pernet nf_tables commit mutex - not the global
nfnetlink subsystem mutex.
Test commands to reproduce the problem are:
%iptables-nft -I INPUT
%iptables-nft -Z
%iptables-nft -Z
This patch uses RCU calls to handle basechain->stats updates to fix a
splat that looks like:
[89279.358755] =============================
[89279.363656] WARNING: suspicious RCU usage
[89279.368458] 4.20.0-rc2+ #44 Tainted: G W L
[89279.374661] -----------------------------
[89279.379542] net/netfilter/nf_tables_api.c:1404 suspicious rcu_dereference_protected() usage!
[...]
[89279.406556] 1 lock held by iptables-nft/5225:
[89279.411728] #0: 00000000bf45a000 (&net->nft.commit_mutex){+.+.}, at: nf_tables_valid_genid+0x1f/0x70 [nf_tables]
[89279.424022] stack backtrace:
[89279.429236] CPU: 0 PID: 5225 Comm: iptables-nft Tainted: G W L 4.20.0-rc2+ #44
[89279.430135] Call Trace:
[89279.430135] dump_stack+0xc9/0x16b
[89279.430135] ? show_regs_print_info+0x5/0x5
[89279.430135] ? lockdep_rcu_suspicious+0x117/0x160
[89279.430135] nft_chain_commit_update+0x4ea/0x640 [nf_tables]
[89279.430135] ? sched_clock_local+0xd4/0x140
[89279.430135] ? check_flags.part.35+0x440/0x440
[89279.430135] ? __rhashtable_remove_fast.constprop.67+0xec0/0xec0 [nf_tables]
[89279.430135] ? sched_clock_cpu+0x126/0x170
[89279.430135] ? find_held_lock+0x39/0x1c0
[89279.430135] ? hlock_class+0x140/0x140
[89279.430135] ? is_bpf_text_address+0x5/0xf0
[89279.430135] ? check_flags.part.35+0x440/0x440
[89279.430135] ? __lock_is_held+0xb4/0x140
[89279.430135] nf_tables_commit+0x2555/0x39c0 [nf_tables]
Fixes: f102d66b33
("netfilter: nf_tables: use dedicated mutex to guard transactions")
Signed-off-by: Taehee Yoo <ap420073@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
297 lines
6.8 KiB
C
297 lines
6.8 KiB
C
/*
|
|
* Copyright (c) 2008 Patrick McHardy <kaber@trash.net>
|
|
*
|
|
* 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.
|
|
*
|
|
* Development of this code funded by Astaro AG (http://www.astaro.com/)
|
|
*/
|
|
|
|
#include <linux/kernel.h>
|
|
#include <linux/module.h>
|
|
#include <linux/init.h>
|
|
#include <linux/list.h>
|
|
#include <linux/rculist.h>
|
|
#include <linux/skbuff.h>
|
|
#include <linux/netlink.h>
|
|
#include <linux/netfilter.h>
|
|
#include <linux/static_key.h>
|
|
#include <linux/netfilter/nfnetlink.h>
|
|
#include <linux/netfilter/nf_tables.h>
|
|
#include <net/netfilter/nf_tables_core.h>
|
|
#include <net/netfilter/nf_tables.h>
|
|
#include <net/netfilter/nf_log.h>
|
|
|
|
static noinline void __nft_trace_packet(struct nft_traceinfo *info,
|
|
const struct nft_chain *chain,
|
|
enum nft_trace_types type)
|
|
{
|
|
const struct nft_pktinfo *pkt = info->pkt;
|
|
|
|
if (!info->trace || !pkt->skb->nf_trace)
|
|
return;
|
|
|
|
info->chain = chain;
|
|
info->type = type;
|
|
|
|
nft_trace_notify(info);
|
|
}
|
|
|
|
static inline void nft_trace_packet(struct nft_traceinfo *info,
|
|
const struct nft_chain *chain,
|
|
const struct nft_rule *rule,
|
|
enum nft_trace_types type)
|
|
{
|
|
if (static_branch_unlikely(&nft_trace_enabled)) {
|
|
info->rule = rule;
|
|
__nft_trace_packet(info, chain, type);
|
|
}
|
|
}
|
|
|
|
static void nft_cmp_fast_eval(const struct nft_expr *expr,
|
|
struct nft_regs *regs)
|
|
{
|
|
const struct nft_cmp_fast_expr *priv = nft_expr_priv(expr);
|
|
u32 mask = nft_cmp_fast_mask(priv->len);
|
|
|
|
if ((regs->data[priv->sreg] & mask) == priv->data)
|
|
return;
|
|
regs->verdict.code = NFT_BREAK;
|
|
}
|
|
|
|
static bool nft_payload_fast_eval(const struct nft_expr *expr,
|
|
struct nft_regs *regs,
|
|
const struct nft_pktinfo *pkt)
|
|
{
|
|
const struct nft_payload *priv = nft_expr_priv(expr);
|
|
const struct sk_buff *skb = pkt->skb;
|
|
u32 *dest = ®s->data[priv->dreg];
|
|
unsigned char *ptr;
|
|
|
|
if (priv->base == NFT_PAYLOAD_NETWORK_HEADER)
|
|
ptr = skb_network_header(skb);
|
|
else {
|
|
if (!pkt->tprot_set)
|
|
return false;
|
|
ptr = skb_network_header(skb) + pkt->xt.thoff;
|
|
}
|
|
|
|
ptr += priv->offset;
|
|
|
|
if (unlikely(ptr + priv->len > skb_tail_pointer(skb)))
|
|
return false;
|
|
|
|
*dest = 0;
|
|
if (priv->len == 2)
|
|
*(u16 *)dest = *(u16 *)ptr;
|
|
else if (priv->len == 4)
|
|
*(u32 *)dest = *(u32 *)ptr;
|
|
else
|
|
*(u8 *)dest = *(u8 *)ptr;
|
|
return true;
|
|
}
|
|
|
|
DEFINE_STATIC_KEY_FALSE(nft_counters_enabled);
|
|
|
|
static noinline void nft_update_chain_stats(const struct nft_chain *chain,
|
|
const struct nft_pktinfo *pkt)
|
|
{
|
|
struct nft_base_chain *base_chain;
|
|
struct nft_stats *stats;
|
|
|
|
base_chain = nft_base_chain(chain);
|
|
if (!rcu_access_pointer(base_chain->stats))
|
|
return;
|
|
|
|
local_bh_disable();
|
|
stats = this_cpu_ptr(rcu_dereference(base_chain->stats));
|
|
if (stats) {
|
|
u64_stats_update_begin(&stats->syncp);
|
|
stats->pkts++;
|
|
stats->bytes += pkt->skb->len;
|
|
u64_stats_update_end(&stats->syncp);
|
|
}
|
|
local_bh_enable();
|
|
}
|
|
|
|
struct nft_jumpstack {
|
|
const struct nft_chain *chain;
|
|
struct nft_rule *const *rules;
|
|
};
|
|
|
|
static void expr_call_ops_eval(const struct nft_expr *expr,
|
|
struct nft_regs *regs,
|
|
struct nft_pktinfo *pkt)
|
|
{
|
|
unsigned long e = (unsigned long)expr->ops->eval;
|
|
|
|
if (e == (unsigned long)nft_meta_get_eval)
|
|
nft_meta_get_eval(expr, regs, pkt);
|
|
else if (e == (unsigned long)nft_lookup_eval)
|
|
nft_lookup_eval(expr, regs, pkt);
|
|
else
|
|
expr->ops->eval(expr, regs, pkt);
|
|
}
|
|
|
|
unsigned int
|
|
nft_do_chain(struct nft_pktinfo *pkt, void *priv)
|
|
{
|
|
const struct nft_chain *chain = priv, *basechain = chain;
|
|
const struct net *net = nft_net(pkt);
|
|
struct nft_rule *const *rules;
|
|
const struct nft_rule *rule;
|
|
const struct nft_expr *expr, *last;
|
|
struct nft_regs regs;
|
|
unsigned int stackptr = 0;
|
|
struct nft_jumpstack jumpstack[NFT_JUMP_STACK_SIZE];
|
|
bool genbit = READ_ONCE(net->nft.gencursor);
|
|
struct nft_traceinfo info;
|
|
|
|
info.trace = false;
|
|
if (static_branch_unlikely(&nft_trace_enabled))
|
|
nft_trace_init(&info, pkt, ®s.verdict, basechain);
|
|
do_chain:
|
|
if (genbit)
|
|
rules = rcu_dereference(chain->rules_gen_1);
|
|
else
|
|
rules = rcu_dereference(chain->rules_gen_0);
|
|
|
|
next_rule:
|
|
rule = *rules;
|
|
regs.verdict.code = NFT_CONTINUE;
|
|
for (; *rules ; rules++) {
|
|
rule = *rules;
|
|
nft_rule_for_each_expr(expr, last, rule) {
|
|
if (expr->ops == &nft_cmp_fast_ops)
|
|
nft_cmp_fast_eval(expr, ®s);
|
|
else if (expr->ops != &nft_payload_fast_ops ||
|
|
!nft_payload_fast_eval(expr, ®s, pkt))
|
|
expr_call_ops_eval(expr, ®s, pkt);
|
|
|
|
if (regs.verdict.code != NFT_CONTINUE)
|
|
break;
|
|
}
|
|
|
|
switch (regs.verdict.code) {
|
|
case NFT_BREAK:
|
|
regs.verdict.code = NFT_CONTINUE;
|
|
continue;
|
|
case NFT_CONTINUE:
|
|
nft_trace_packet(&info, chain, rule,
|
|
NFT_TRACETYPE_RULE);
|
|
continue;
|
|
}
|
|
break;
|
|
}
|
|
|
|
switch (regs.verdict.code & NF_VERDICT_MASK) {
|
|
case NF_ACCEPT:
|
|
case NF_DROP:
|
|
case NF_QUEUE:
|
|
case NF_STOLEN:
|
|
nft_trace_packet(&info, chain, rule,
|
|
NFT_TRACETYPE_RULE);
|
|
return regs.verdict.code;
|
|
}
|
|
|
|
switch (regs.verdict.code) {
|
|
case NFT_JUMP:
|
|
if (WARN_ON_ONCE(stackptr >= NFT_JUMP_STACK_SIZE))
|
|
return NF_DROP;
|
|
jumpstack[stackptr].chain = chain;
|
|
jumpstack[stackptr].rules = rules + 1;
|
|
stackptr++;
|
|
/* fall through */
|
|
case NFT_GOTO:
|
|
nft_trace_packet(&info, chain, rule,
|
|
NFT_TRACETYPE_RULE);
|
|
|
|
chain = regs.verdict.chain;
|
|
goto do_chain;
|
|
case NFT_CONTINUE:
|
|
/* fall through */
|
|
case NFT_RETURN:
|
|
nft_trace_packet(&info, chain, rule,
|
|
NFT_TRACETYPE_RETURN);
|
|
break;
|
|
default:
|
|
WARN_ON(1);
|
|
}
|
|
|
|
if (stackptr > 0) {
|
|
stackptr--;
|
|
chain = jumpstack[stackptr].chain;
|
|
rules = jumpstack[stackptr].rules;
|
|
goto next_rule;
|
|
}
|
|
|
|
nft_trace_packet(&info, basechain, NULL, NFT_TRACETYPE_POLICY);
|
|
|
|
if (static_branch_unlikely(&nft_counters_enabled))
|
|
nft_update_chain_stats(basechain, pkt);
|
|
|
|
return nft_base_chain(basechain)->policy;
|
|
}
|
|
EXPORT_SYMBOL_GPL(nft_do_chain);
|
|
|
|
static struct nft_expr_type *nft_basic_types[] = {
|
|
&nft_imm_type,
|
|
&nft_cmp_type,
|
|
&nft_lookup_type,
|
|
&nft_bitwise_type,
|
|
&nft_byteorder_type,
|
|
&nft_payload_type,
|
|
&nft_dynset_type,
|
|
&nft_range_type,
|
|
&nft_meta_type,
|
|
&nft_rt_type,
|
|
&nft_exthdr_type,
|
|
};
|
|
|
|
static struct nft_object_type *nft_basic_objects[] = {
|
|
#ifdef CONFIG_NETWORK_SECMARK
|
|
&nft_secmark_obj_type,
|
|
#endif
|
|
};
|
|
|
|
int __init nf_tables_core_module_init(void)
|
|
{
|
|
int err, i, j = 0;
|
|
|
|
for (i = 0; i < ARRAY_SIZE(nft_basic_objects); i++) {
|
|
err = nft_register_obj(nft_basic_objects[i]);
|
|
if (err)
|
|
goto err;
|
|
}
|
|
|
|
for (j = 0; j < ARRAY_SIZE(nft_basic_types); j++) {
|
|
err = nft_register_expr(nft_basic_types[j]);
|
|
if (err)
|
|
goto err;
|
|
}
|
|
|
|
return 0;
|
|
|
|
err:
|
|
while (j-- > 0)
|
|
nft_unregister_expr(nft_basic_types[j]);
|
|
|
|
while (i-- > 0)
|
|
nft_unregister_obj(nft_basic_objects[i]);
|
|
|
|
return err;
|
|
}
|
|
|
|
void nf_tables_core_module_exit(void)
|
|
{
|
|
int i;
|
|
|
|
i = ARRAY_SIZE(nft_basic_types);
|
|
while (i-- > 0)
|
|
nft_unregister_expr(nft_basic_types[i]);
|
|
|
|
i = ARRAY_SIZE(nft_basic_objects);
|
|
while (i-- > 0)
|
|
nft_unregister_obj(nft_basic_objects[i]);
|
|
}
|