Skip to content

Commit df86c0e

Browse files
corvusaisecgregkh
authored andcommitted
netfilter: nf_tables: don't queue packet path object notifications
commit 7904b94 upstream. All file:line references below are against v7.2-rc4 (ac5b0e5651b1). The trace was captured on 7.2.0-rc6-kasan72rc6 (075b748), where the same lines apply. nft_obj_notify() is exported and reached from the packet path. Its only in-tree caller is nft_quota_obj_eval() (net/netfilter/nft_quota.c:68), which notifies with GFP_ATOMIC while evaluating a rule for a transiting packet, holding no mutex. Since commit 67cc570 ("netfilter: nf_tables: coalesce multiple notifications into one skbuff") that notification is no longer sent immediately. __nft_obj_notify() queues it onto nft_net->notify_list via nft_notify_enqueue() (net/netfilter/nf_tables_api.c:1211), which is a bare list_add_tail(). notify_list has no lock of its own (include/net/netfilter/nf_tables.h:1951), it is serialised by commit_mutex: the six other enqueue sites all run inside a netlink transaction, and the drain in nft_commit_notify() (net/netfilter/nf_tables_api.c:10746) does list_del() + kfree_skb() from nf_tables_commit() with commit_mutex held. Sending packets through a chain that references a depleted quota object therefore races an unlocked list_add_tail() against list_del() + kfree_skb() on another CPU. The WRITE_ONCE(prev->next, new) in __list_add() then stores through an sk_buff that has already been freed: BUG: KASAN: slab-use-after-free in __nft_obj_notify+0x2c5/0x2d0 Write of size 8 at addr ff110001047183c0 by task poc/76 CPU: 0 UID: 1000 PID: 76 Comm: poc Tainted: G W 7.2.0-rc6-kasan72rc6 #4 Call Trace: <IRQ> __nft_obj_notify (include/linux/list.h:164 include/linux/list.h:191 net/netfilter/nf_tables_api.c:1211 net/netfilter/nf_tables_api.c:8743) nft_quota_obj_eval (net/netfilter/nft_quota.c:68) nft_do_chain_inet nf_hook_slow __ip_local_out ip_push_pending_frames udp_send_skb udp_sendmsg __x64_sys_sendto Allocated by task 77: __alloc_skb (net/core/skbuff.c:704) __nft_obj_notify (include/net/netlink.h:1055 net/netfilter/nf_tables_api.c:8731) nft_quota_obj_eval (net/netfilter/nft_quota.c:68) nft_do_chain Freed by task 79: nf_tables_commit (include/linux/skbuff.h:1332 net/netfilter/nf_tables_api.c:10759 net/netfilter/nf_tables_api.c:11185) nfnetlink_rcv_batch (net/netfilter/nfnetlink.c:574) netlink_unicast netlink_sendmsg The buggy address belongs to the cache skbuff_head_cache of size 232 Queueing from the packet path is wrong even leaving the race aside: notify_list is only drained by nft_commit_notify() from nf_tables_commit() (:11185), so a notification enqueued outside a transaction is not sent until some later netlink batch commits, if one ever does. The gfp argument that nft_obj_notify() still takes is a leftover of the pre-67cc570edaa0 behaviour, where this path called nfnetlink_send() directly. Restore that: split the message construction out into nft_obj_notify_alloc() and let each caller decide what to do with the skb. nft_obj_notify(), the exported one reached from the packet path, sends it straight away; nf_tables_obj_notify(), which runs under commit_mutex, keeps queueing it, so transaction notifications are still coalesced. Fixes: 67cc570 ("netfilter: nf_tables: coalesce multiple notifications into one skbuff") Cc: stable@kernel.org Reported-by: TencentOS Corvus AI <corvus@tencent.com> Assisted-by: tencentos-corvus-ai:kimi-k3 Signed-off-by: Fourie Zhang <fouriezhang@tencent.com> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 2a7c2f0 commit df86c0e

1 file changed

Lines changed: 23 additions & 13 deletions

File tree

net/netfilter/nf_tables_api.c

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8200,18 +8200,17 @@ static int nf_tables_delobj(struct sk_buff *skb, const struct nfnl_info *info,
82008200
return nft_delobj(&ctx, obj);
82018201
}
82028202

8203-
static void
8204-
__nft_obj_notify(struct net *net, const struct nft_table *table,
8205-
struct nft_object *obj, u32 portid, u32 seq, int event,
8206-
u16 flags, int family, int report, gfp_t gfp)
8203+
static struct sk_buff *
8204+
nft_obj_notify_alloc(struct net *net, const struct nft_table *table,
8205+
struct nft_object *obj, u32 portid, u32 seq, int event,
8206+
u16 flags, int family, int report, gfp_t gfp)
82078207
{
8208-
struct nftables_pernet *nft_net = nft_pernet(net);
82098208
struct sk_buff *skb;
82108209
int err;
82118210

82128211
if (!report &&
82138212
!nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
8214-
return;
8213+
return NULL;
82158214

82168215
skb = nlmsg_new(NLMSG_GOODSIZE, gfp);
82178216
if (skb == NULL)
@@ -8225,10 +8224,10 @@ __nft_obj_notify(struct net *net, const struct nft_table *table,
82258224
goto err;
82268225
}
82278226

8228-
nft_notify_enqueue(skb, report, &nft_net->notify_list);
8229-
return;
8227+
return skb;
82308228
err:
82318229
nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, -ENOBUFS);
8230+
return NULL;
82328231
}
82338232

82348233
void nft_obj_notify(struct net *net, const struct nft_table *table,
@@ -8237,6 +8236,7 @@ void nft_obj_notify(struct net *net, const struct nft_table *table,
82378236
{
82388237
char *buf = kasprintf(gfp, "%s:%u",
82398238
table->name, nft_base_seq(net));
8239+
struct sk_buff *skb;
82408240

82418241
audit_log_nfcfg(buf,
82428242
family,
@@ -8247,17 +8247,27 @@ void nft_obj_notify(struct net *net, const struct nft_table *table,
82478247
gfp);
82488248
kfree(buf);
82498249

8250-
__nft_obj_notify(net, table, obj, portid, seq, event,
8251-
flags, family, report, gfp);
8250+
/* Called from the packet path, holding no mutex: notify_list is
8251+
* serialised by commit_mutex, so send this notification directly.
8252+
*/
8253+
skb = nft_obj_notify_alloc(net, table, obj, portid, seq, event,
8254+
flags, family, report, gfp);
8255+
if (skb)
8256+
nfnetlink_send(skb, net, portid, NFNLGRP_NFTABLES, report, gfp);
82528257
}
82538258
EXPORT_SYMBOL_GPL(nft_obj_notify);
82548259

82558260
static void nf_tables_obj_notify(const struct nft_ctx *ctx,
82568261
struct nft_object *obj, int event)
82578262
{
8258-
__nft_obj_notify(ctx->net, ctx->table, obj, ctx->portid,
8259-
ctx->seq, event, ctx->flags, ctx->family,
8260-
ctx->report, GFP_KERNEL);
8263+
struct nftables_pernet *nft_net = nft_pernet(ctx->net);
8264+
struct sk_buff *skb;
8265+
8266+
skb = nft_obj_notify_alloc(ctx->net, ctx->table, obj, ctx->portid,
8267+
ctx->seq, event, ctx->flags, ctx->family,
8268+
ctx->report, GFP_KERNEL);
8269+
if (skb)
8270+
nft_notify_enqueue(skb, ctx->report, &nft_net->notify_list);
82618271
}
82628272

82638273
/*

0 commit comments

Comments
 (0)