[PATCH] netfilter: ipset: add ip_set lock to ip_set_test

From: xiaolinkui
Date: Wed Sep 27 2023 - 09:24:00 EST


From: Linkui Xiao <xiaolinkui@xxxxxxxxxx>

If the ip_set is not locked during ip_set_test, the following situations
may occur:

CPU0 CPU1
ip_rcv->
ip_rcv_finish->
ip_local_deliver->
nf_hook_slow->
iptable_filter_hook->
ipt_do_table->
set_match_v4->
ip_set_test-> list_set_destroy->
hash_net4_kadt-> set->data = NULL
h = set->data
.cidr = INIT_CIDR(h->nets[0].cidr[0], HOST_MASK)

The set->data is empty, continuing to access set->data will result in a
kernel NULL pointer. The call trace is as follows:

[2350616.024418] Call trace:
[2350616.024670] hash_net4_kadt+0x38/0x148 [ip_set_hash_net]
[2350616.025147] ip_set_test+0xbc/0x230 [ip_set]
[2350616.025549] set_match_v4+0xac/0xd0 [xt_set]
[2350616.025951] ipt_do_table+0x32c/0x678 [ip_tables]
[2350616.026391] iptable_filter_hook+0x30/0x40 [iptable_filter]
[2350616.026905] nf_hook_slow+0x50/0x100
[2350616.027256] ip_local_deliver+0xd4/0xe8
[2350616.027616] ip_rcv_finish+0x90/0xb0
[2350616.027961] ip_rcv+0x50/0xb0
[2350616.028261] __netif_receive_skb_one_core+0x58/0x68
[2350616.028716] __netif_receive_skb+0x28/0x80
[2350616.029098] netif_receive_skb_internal+0x3c/0xa8
[2350616.029533] napi_gro_receive+0xf8/0x170
[2350616.029898] receive_buf+0xec/0xa08 [virtio_net]
[2350616.030323] virtnet_poll+0x144/0x310 [virtio_net]
[2350616.030761] net_rx_action+0x158/0x3a0
[2350616.031124] __do_softirq+0x11c/0x33c
[2350616.031470] irq_exit+0x11c/0x128
[2350616.031793] __handle_domain_irq+0x6c/0xc0
[2350616.032172] gic_handle_irq+0x6c/0x170
[2350616.032528] el1_irq+0xb8/0x140
[2350616.032835] arch_cpu_idle+0x38/0x1c0
[2350616.033183] default_idle_call+0x24/0x58
[2350616.033549] do_idle+0x1a4/0x268
[2350616.033859] cpu_startup_entry+0x28/0x78
[2350616.034234] secondary_start_kernel+0x17c/0x1c8

Signed-off-by: Linkui Xiao <xiaolinkui@xxxxxxxxxx>
---
net/netfilter/ipset/ip_set_core.c | 2 ++
1 file changed, 2 insertions(+)

diff --git a/net/netfilter/ipset/ip_set_core.c b/net/netfilter/ipset/ip_set_core.c
index 35d2f9c9ada0..46f4f47e29e4 100644
--- a/net/netfilter/ipset/ip_set_core.c
+++ b/net/netfilter/ipset/ip_set_core.c
@@ -747,7 +747,9 @@ ip_set_test(ip_set_id_t index, const struct sk_buff *skb,
!(opt->family == set->family || set->family == NFPROTO_UNSPEC))
return 0;

+ ip_set_lock(set);
ret = set->variant->kadt(set, skb, par, IPSET_TEST, opt);
+ ip_set_unlock(set);

if (ret == -EAGAIN) {
/* Type requests element to be completed */
--
2.17.1