netfilter: updates for net
Subsystems (4)
Florian Westphal
maintainercover netfilter: updates for net
Hi,
The following patchset contains Netfilter fixes for *net*. Note that most bugs fixed here stem from 2.6 days, the large PR is not due to an increase in regressions.
1) Fix incorrect reject of set updates with nf_tables pipapo set avx2 backend. This comes with a regression test in patch 2.
3) Fix a syzkaller reported data race in ipset, from Jozsef Kadlecsik.
4) nfnetlink_log needs to zero padding to prevent infoleak to userspace, from Weiming Shi.
5) pay more attention to xtables hook masks + NFPROTO_UNSPEC. UNSPEC+hook_mask is only valid for ipv4, ipv6 and bridge families. This can cause a crash because arp family has different meaning for hook constants. We need to reject rule adds when we have a match that sets both a hook mask and proto_unspec, unless the requesting family is one of ip/ip6/bridge. Also from Weiming Shi.
6) xtables ip6t_rt module never validated that addrnr length is within the allowed array boundary. Reject bogus values. From Ren Wei.
7) Fix high memory usage in rbtree set backend that was unwanted side-effect of the recently added binary search blob. From Pablo Neira Ayuso.
Patches 8 to 12, also from Pablo, address long-standing RCU safety bugs in conntracks handling of expectations: We can never safely defer a conntrack extension area without holding a reference. Yet expectation handling does so in multiple places. Fix this by avoiding the need to look into the master conntrack to begin with and by extending locked sections in a few places.
13) fix use of uninitialized rtp_addr in the sip conntrack helper, also from Weiming Shi.
14) Add stricter netlink policy checks in ctnetlink, from David Carlier. This avoids undefined behaviour when userspace provides huge wscale value.
Please, pull these changes from:
The following changes since commit c4ea7d8907cf72b259bf70bd8c2e791e1c4ff70f:
net: mana: fix use-after-free in add_adev() error path (2026-03-24 21:07:58 -0700)
are available in the Git repository at:
https://git.kernel.org/pub/scm/linux/kernel/git/netfilter/nf.git nf-26-03-25
for you to fetch changes up to 65182deffd243cb451c7e1f532e7de1ed59afbeb:
netfilter: ctnetlink: use netlink policy range checks (2026-03-25 13:36:45 +0100)
----------------------------------------------------------------
netfilter pull request nf-26-03-25
----------------------------------------------------------------
David Carlier (1):
netfilter: ctnetlink: use netlink policy range checks
Florian Westphal (2):
netfilter: nft_set_pipapo_avx2: don't return non-matching entry on expiry
selftests: netfilter: nft_concat_range.sh: add check for flush+reload bug
Jozsef Kadlecsik (1):
netfilter: ipset: Fix data race between add and list header in all hash types
Pablo Neira Ayuso (6):
netfilter: nft_set_rbtree: revisit array resize logic
netfilter: nf_conntrack_expect: honor expectation helper field
netfilter: nf_conntrack_expect: use expect->helper
netfilter: ctnetlink: ensure safe access to master conntrack
netfilter: nf_conntrack_expect: store netns and zone in expectation
netfilter: nf_conntrack_expect: skip expectations in other netns via proc
Ren Wei (1):
netfilter: ip6t_rt: reject oversized addrnr in rt_mt6_check()
Weiming Shi (3):
netfilter: nfnetlink_log: fix uninitialized padding leak in NFULA_PAYLOAD
netfilter: x_tables: reject unsupported families in xt_check_match/xt_check_target
netfilter: nf_conntrack_sip: fix use of uninitialized rtp_addr in process_sdp
include/net/netfilter/nf_conntrack_core.h | 5 + include/net/netfilter/nf_conntrack_expect.h | 20 +++- .../linux/netfilter/nf_conntrack_common.h | 4 + net/ipv6/netfilter/ip6t_rt.c | 4 + net/netfilter/ipset/ip_set_core.c | 4 +- net/netfilter/nf_conntrack_broadcast.c | 2 +- net/netfilter/nf_conntrack_ecache.c | 2 + net/netfilter/nf_conntrack_expect.c | 30 +++++- net/netfilter/nf_conntrack_h323_main.c | 12 +-- net/netfilter/nf_conntrack_helper.c | 11 ++- net/netfilter/nf_conntrack_netlink.c | 73 ++++++++------- net/netfilter/nf_conntrack_proto_tcp.c | 10 +- net/netfilter/nf_conntrack_sip.c | 18 ++-- net/netfilter/nfnetlink_log.c | 8 +- net/netfilter/nft_set_pipapo_avx2.c | 20 ++-- net/netfilter/nft_set_rbtree.c | 92 +++++++++++++++---- net/netfilter/x_tables.c | 36 ++++++++ net/netfilter/xt_devgroup.c | 5 + .../net/netfilter/nft_concat_range.sh | 70 +++++++++++++- 19 files changed, 323 insertions(+), 103 deletions(-)
Signature · 1 line
2.52.0
Florian Westphal
maintainerPATCH 1/14 netfilter: nft_set_pipapo_avx2: don't return non-matching entry on expiry
New test case fails unexpectedly when avx2 matching functions are used.
The test first loads a ranomly generated pipapo set with 'ipv4 . port' key, i.e. nft -f foo.
This works. Then, it reloads the set after a flush:
(echo flush set t s; cat foo) | nft -f -
This is expected to work, because its the same set after all and it was already loaded once.
But with avx2, this fails: nft reports a clashing element.
The reported clash is of following form:
We successfully re-inserted
a . b
c . d
Then we try to insert a . d
avx2 finds the already existing a . d, which (due to 'flush set') is marked as invalid in the new generation. It skips the element and moves to next.
Due to incorrect masking, the skip-step finds the next matching element *only considering the first field*,
i.e. we return the already reinserted "a . b", even though the last field is different and the entry should not have been matched.
No such error is reported for the generic c implementation (no avx2) or when the last field has to use the 'nft_pipapo_avx2_lookup_slow' fallback.
Bisection points to7711f4bb4b36 ("netfilter: nft_set_pipapo: fix range overlap detection") but that fix merely uncovers this bug.
Before this commit, the wrong element is returned, but erronously reported as a full, identical duplicate.
The root-cause is too early return in the avx2 match functions. When we process the last field, we should continue to process data until the entire input size has been consumed to make sure no stale bits remain in the map.
--- net/netfilter/nft_set_pipapo_avx2.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-)
net/netfilter/nft_set_pipapo_avx2.c +10 -10
@@ -242,7 +242,7 @@ static int nft_pipapo_avx2_lookup_4b_2(unsigned long *map, unsigned long *fill, b =nft_pipapo_avx2_refill (i_ul, &map[i_ul], fill, f->mt, last);if (last) -return b; + ret = b;if (unlikely (ret ==-1 )) ret = b /XSAVE_YMM_SIZE ;
@@ -319,7 +319,7 @@ static int nft_pipapo_avx2_lookup_4b_4(unsigned long *map, unsigned long *fill, b =nft_pipapo_avx2_refill (i_ul, &map[i_ul], fill, f->mt, last);if (last) -return b; + ret = b;if (unlikely (ret ==-1 )) ret = b /XSAVE_YMM_SIZE ;
@@ -414,7 +414,7 @@ static int nft_pipapo_avx2_lookup_4b_8(unsigned long *map, unsigned long *fill, b =nft_pipapo_avx2_refill (i_ul, &map[i_ul], fill, f->mt, last);if (last) -return b; + ret = b;if (unlikely (ret ==-1 )) ret = b /XSAVE_YMM_SIZE ;
@@ -505,7 +505,7 @@ static int nft_pipapo_avx2_lookup_4b_12(unsigned long *map, unsigned long *fill, b =nft_pipapo_avx2_refill (i_ul, &map[i_ul], fill, f->mt, last);if (last) -return b; + ret = b;if (unlikely (ret ==-1 )) ret = b /XSAVE_YMM_SIZE ;
@@ -641,7 +641,7 @@ static int nft_pipapo_avx2_lookup_4b_32(unsigned long *map, unsigned long *fill, b =nft_pipapo_avx2_refill (i_ul, &map[i_ul], fill, f->mt, last);if (last) -return b; + ret = b;if (unlikely (ret ==-1 )) ret = b /XSAVE_YMM_SIZE ;
@@ -699,7 +699,7 @@ static int nft_pipapo_avx2_lookup_8b_1(unsigned long *map, unsigned long *fill, b =nft_pipapo_avx2_refill (i_ul, &map[i_ul], fill, f->mt, last);if (last) -return b; + ret = b;if (unlikely (ret ==-1 )) ret = b /XSAVE_YMM_SIZE ;
@@ -764,7 +764,7 @@ static int nft_pipapo_avx2_lookup_8b_2(unsigned long *map, unsigned long *fill, b =nft_pipapo_avx2_refill (i_ul, &map[i_ul], fill, f->mt, last);if (last) -return b; + ret = b;if (unlikely (ret ==-1 )) ret = b /XSAVE_YMM_SIZE ;
@@ -839,7 +839,7 @@ static int nft_pipapo_avx2_lookup_8b_4(unsigned long *map, unsigned long *fill, b =nft_pipapo_avx2_refill (i_ul, &map[i_ul], fill, f->mt, last);if (last) -return b; + ret = b;if (unlikely (ret ==-1 )) ret = b /XSAVE_YMM_SIZE ;
@@ -925,7 +925,7 @@ static int nft_pipapo_avx2_lookup_8b_6(unsigned long *map, unsigned long *fill, b =nft_pipapo_avx2_refill (i_ul, &map[i_ul], fill, f->mt, last);if (last) -return b; + ret = b;if (unlikely (ret ==-1 )) ret = b /XSAVE_YMM_SIZE ;
@@ -1019,7 +1019,7 @@ static int nft_pipapo_avx2_lookup_8b_16(unsigned long *map, unsigned long *fill, b =nft_pipapo_avx2_refill (i_ul, &map[i_ul], fill, f->mt, last);if (last) -return b; + ret = b;if (unlikely (ret ==-1 )) ret = b /XSAVE_YMM_SIZE ;
Signature · 1 line
2.52.0
Florian Westphal
maintainerPATCH 2/14 selftests: netfilter: nft_concat_range.sh: add check for flush+reload bug
In reply to Florian WestphalThis test will fail without
the preceding commit ("netfilter: nft_set_pipapo_avx2: fix match retart if found element is expired"):
reject overlapping range on add 0s [ OK ]
reload with flush /dev/stdin:59:32-52: Error: Could not process rule: File exists
add element inet filter test { 10.0.0.29 . 10.0.2.29 }
--- .../net/netfilter/nft_concat_range.sh | 70 ++++++++++++++++++- 1 file changed, 69 insertions(+), 1 deletion(-)
tools/testing/selftests/net/netfilter/nft_concat_range.sh +69 -1
@@ -29,7 +29,8 @@ TYPES="net_port port_net net6_port port_proto net6_port_mac net6_port_mac_proto net6_port_net6_port net_port_mac_proto_net" # Reported bugs, also described by TYPE_ variables below -BUGS="flush_remove_add reload net_port_proto_match avx2_mismatch doublecreate insert_overlap" +BUGS="flush_remove_add reload net_port_proto_match avx2_mismatch doublecreate + insert_overlap load_flush_load4 load_flush_load8" # List of possible paths to pktgen script from kernel tree for performance tests PKTGEN_SCRIPT_PATHS="
@@ -432,6 +433,30 @@ race_repeat 0 perf_duration 0 " +TYPE_load_flush_load4=" +display reload with flush, 4bit groups +type_spec ipv4_addr . ipv4_addr +chain_spec ip saddr . ip daddr +dst addr4 +proto icmp + +race_repeat 0 + +perf_duration 0 +" + +TYPE_load_flush_load8=" +display reload with flush, 8bit groups +type_spec ipv4_addr . ipv4_addr +chain_spec ip saddr . ip daddr +dst addr4 +proto icmp + +race_repeat 0 + +perf_duration 0 +" + # Set template for all tests, types and rules are filled in depending on test set_template=' flush ruleset
@@ -1997,6 +2022,49 @@ test_bug_insert_overlap() return 0 } +test_bug_load_flush_load4() +{ + local i + + setup veth send_"${proto}" set || return ${ksft_skip} + + for i in $(seq 0 255); do + local addelem="add element inet filter test" + local j + + for j in $(seq 0 20); do + echo "$addelem { 10.$j.0.$i . 10.$j.1.$i }" + echo "$addelem { 10.$j.0.$i . 10.$j.2.$i }" + done + done > "$tmp" + + nft -f "$tmp" || return 1 + + ( echo "flush set inet filter test";cat "$tmp") | nft -f - + [ $? -eq 0 ] || return 1 + + return 0 +} + +test_bug_load_flush_load8() +{ + local i + + setup veth send_"${proto}" set || return ${ksft_skip} + + for i in $(seq 1 100); do + echo "add element inet filter test { 10.0.0.$i . 10.0.1.$i }" + echo "add element inet filter test { 10.0.0.$i . 10.0.2.$i }" + done > "$tmp" + + nft -f "$tmp" || return 1 + + ( echo "flush set inet filter test";cat "$tmp") | nft -f - + [ $? -eq 0 ] || return 1 + + return 0 +} + test_reported_issues() { eval test_bug_"${subtest}" }
Signature · 1 line
2.52.0
Florian Westphal
maintainerPATCH 3/14 netfilter: ipset: Fix data race between add and list header in all hash types
In reply to Florian WestphalFrom: Jozsef Kadlecsik <[email protected]>
The "ipset list -terse" command is actually a dump operation which may run parallel with "ipset add" commands, which can trigger an internal resizing of the hash type of sets just being dumped. However, dumping just the header part of the set was not protected against underlying resizing. Fix it by protecting the header dumping part as well.
--- net/netfilter/ipset/ip_set_core.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
net/netfilter/ipset/ip_set_core.c +2 -2
@@ -1648,13 +1648,13 @@ ip_set_dump_do(struct sk_buff *skb, struct netlink_callback *cb)if (cb->args[IPSET_CB_PROTO ] >IPSET_PROTOCOL_MIN &&nla_put_net16 (skb,IPSET_ATTR_INDEX ,htons (index))) goto nla_put_failure; +if (set->variant->uref) + set->variant->uref (set, cb, true); ret = set->variant->head (set, skb);if (ret <0 ) goto release_refcount;if (dump_flags &IPSET_FLAG_LIST_HEADER ) goto next_set; -if (set->variant->uref) - set->variant->uref (set, cb, true); fallthrough;default : ret = set->variant->list (set, skb, cb);
Signature · 1 line
2.52.0
Florian Westphal
maintainerPATCH 4/14 netfilter: nfnetlink_log: fix uninitialized padding leak in NFULA_PAYLOAD
In reply to Florian WestphalFrom: Weiming Shi <[email protected]>
__build_packet_message() manually constructs the NFULA_PAYLOAD netlink attribute using skb_put() and skb_copy_bits(), bypassing the standard nla_reserve()/nla_put() helpers. While nla_total_size(data_len) bytes are allocated (including NLA alignment padding), only data_len bytes of actual packet data are copied. The trailing nla_padlen(data_len) bytes (1-3 when data_len is not 4-byte aligned) are never initialized, leaking stale heap contents to userspace via the NFLOG netlink socket.
Replace the manual attribute construction with nla_reserve(), which handles the tailroom check, header setup, and padding zeroing via __nla_reserve(). The subsequent skb_copy_bits() fills in the payload data on top of the properly initialized attribute.
--- net/netfilter/nfnetlink_log.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-)
net/netfilter/nfnetlink_log.c +2 -6
@@ -647,15 +647,11 @@ __build_packet_message(struct nfnl_log_net *log,if (data_len) {struct nlattr *nla; -int size =nla_attr_size (data_len); -if (skb_tailroom (inst->skb) <nla_total_size (data_len)) + nla =nla_reserve (inst->skb,NFULA_PAYLOAD , data_len); +if (!nla) goto nla_put_failure; - nla =skb_put (inst->skb,nla_total_size (data_len)); - nla->nla_type =NFULA_PAYLOAD ; - nla->nla_len = size; -if (skb_copy_bits (skb,0 ,nla_data (nla), data_len))BUG (); }
Signature · 1 line
2.52.0
Florian Westphal
maintainerPATCH 5/14 netfilter: x_tables: reject unsupported families in xt_check_match/xt_check_target
In reply to Florian WestphalFrom: Weiming Shi <[email protected]>
xt_match and xt_target structs registered with NFPROTO_UNSPEC can be loaded by any protocol family through nft_compat. When such a match/target sets .hooks to restrict which hooks it may run on, the bitmask uses NF_INET_* constants. This is only correct for families whose hook layout matches NF_INET_*: IPv4, IPv6, INET, and bridge all share the same five hooks (PRE_ROUTING ... POST_ROUTING).
ARP only has three hooks (IN=0, OUT=1, FORWARD=2) with different semantics. Because NF_ARP_OUT == 1 == NF_INET_LOCAL_IN, the .hooks validation silently passes for the wrong reasons, allowing matches to run on ARP chains where the hook assumptions (e.g. state->in being set on input hooks) do not hold. This leads to NULL pointer dereferences; xt_devgroup is one concrete example:
Oops: general protection fault, probably for non-canonical address 0xdffffc0000000044: 0000 [#1] SMP KASAN NOPTI KASAN: null-ptr-deref in range [0x0000000000000220-0x0000000000000227] RIP: 0010:devgroup_mt+0xff/0x350 Call Trace: <TASK> nft_match_eval (net/netfilter/nft_compat.c:407) nft_do_chain (net/netfilter/nf_tables_core.c:285) nft_do_chain_arp (net/netfilter/nft_chain_filter.c:61) nf_hook_slow (net/netfilter/core.c:623) arp_xmit (net/ipv4/arp.c:666) </TASK> Kernel panic - not syncing: Fatal exception in interrupt
Add a helper xt_family_has_inet_hooks() and call it from both xt_check_match() and xt_check_target(): when a UNSPEC match/target declares .hooks, reject families whose hook numbering differs from the NF_INET_* scheme.
Also add .hooks to xt_devgroup so the framework-level check covers it; previously it relied on manual hook validation in checkentry using NF_INET_* constants, which suffers from the same collision.
--- net/netfilter/x_tables.c | 36 ++++++++++++++++++++++++++++++++++++ net/netfilter/xt_devgroup.c | 5 +++++ 2 files changed, 41 insertions(+)
net/netfilter/x_tables.c +36 -0
@@ -477,6 +477,28 @@ int xt_check_proc_name(const char *name, unsigned int size) }EXPORT_SYMBOL (xt_check_proc_name); +/* +* Families whose hook numbering follows the NF_INET_* scheme. +* ARP hooks (IN=0, OUT=1, FORWARD=2) use different semantics and +* collide numerically with NF_INET_* values, so UNSPEC matches/targets +* that declare .hooks must not run on ARP (or any other family whose +* hooks do not follow the INET layout). +*/ +static bool xt_family_has_inet_hooks (u_int8_t family) +{ +switch (family) { +case NFPROTO_IPV4 : +case NFPROTO_IPV6 : +case NFPROTO_BRIDGE : +return true; +case NFPROTO_INET : +/* nftables only */ +return false; +default : +return false; + } +} +int xt_check_match (struct xt_mtchk_param *par,unsigned int size,u16 proto,bool inv_proto) {
@@ -501,6 +523,13 @@ int xt_check_match(struct xt_mtchk_param *par, par->match->table, par->table);return -EINVAL ; } +if (par->match->family ==NFPROTO_UNSPEC && + par->match->hooks && + !xt_family_has_inet_hooks (par->family)) { +pr_info_ratelimited ("%s_tables: %s match: not valid for this family\n" , + xt_prefix[par->family], par->match->name); +return -EINVAL ; + }if (par->match->hooks && (par->hook_mask & ~par->match->hooks) !=0 ) {char used[64 ], allow[64 ];
@@ -1016,6 +1045,13 @@ int xt_check_target(struct xt_tgchk_param *par, par->target->table, par->table);return -EINVAL ; } +if (par->target->family ==NFPROTO_UNSPEC && + par->target->hooks && + !xt_family_has_inet_hooks (par->family)) { +pr_info_ratelimited ("%s_tables: %s target: not valid for this family\n" , + xt_prefix[par->family], par->target->name); +return -EINVAL ; + }if (par->target->hooks && (par->hook_mask & ~par->target->hooks) !=0 ) {char used[64 ], allow[64 ];
net/netfilter/xt_devgroup.c +5 -0
@@ -62,6 +62,11 @@ static struct xt_match devgroup_mt_reg __read_mostly = { .checkentry = devgroup_mt_checkentry, .matchsize = sizeof(struct xt_devgroup_info), .family = NFPROTO_UNSPEC, + .hooks = (1 <<NF_INET_PRE_ROUTING ) | + (1 <<NF_INET_LOCAL_IN ) | + (1 <<NF_INET_FORWARD ) | + (1 <<NF_INET_LOCAL_OUT ) | + (1 << NF_INET_POST_ROUTING), .me = THIS_MODULE };
Signature · 1 line
2.52.0
Florian Westphal
maintainerPATCH 6/14 netfilter: ip6t_rt: reject oversized addrnr in rt_mt6_check()
In reply to Florian WestphalFrom: Ren Wei <[email protected]>
Reject rt match rules whose addrnr exceeds IP6T_RT_HOPS.
rt_mt6() expects addrnr to stay within the bounds of rtinfo->addrs[]. Validate addrnr during rule installation so malformed rules are rejected before the match logic can use an out-of-range value.
--- net/ipv6/netfilter/ip6t_rt.c | 4 ++++ 1 file changed, 4 insertions(+)
net/ipv6/netfilter/ip6t_rt.c +4 -0
@@ -157,6 +157,10 @@ static int rt_mt6_check(const struct xt_mtchk_param *par)pr_debug ("unknown flags %X\n" , rtinfo->invflags);return -EINVAL ; } +if (rtinfo->addrnr >IP6T_RT_HOPS ) { +pr_debug ("too many addresses specified\n" ); +return -EINVAL ; + } if ((rtinfo->flags & (IP6T_RT_RES | IP6T_RT_FST_MASK)) && (!(rtinfo->flags & IP6T_RT_TYP) || (rtinfo->rt_type != 0) ||
Signature · 1 line
2.52.0
Florian Westphal
maintainerPATCH 7/14 netfilter: nft_set_rbtree: revisit array resize logic
In reply to Florian WestphalFrom: Pablo Neira Ayuso <[email protected]>
Chris Arges reports high memory consumption with thousands of containers, this patch revisits the array allocation logic.
For anonymous sets, start by 16 slots (which takes 256 bytes on x86_64). Expand it by x2 until threshold of 512 slots is reached, over that threshold, expand it by x1.5.
For non-anonymous set, start by 1024 slots in the array (which takes 16 Kbytes initially on x86_64). Expand it by x1.5.
Use set->ndeact to subtract deactivated elements when calculating the number of the slots in the array, otherwise the array size array gets increased artifically. Add special case shrink logic to deal with flush set too.
The shrink logic is skipped by anonymous sets.
Use check_add_overflow() to calculate the new array size.
Add a WARN_ON_ONCE check to make sure elements fit into the new array size.
--- net/netfilter/nft_set_rbtree.c | 92 +++++++++++++++++++++++++++------- 1 file changed, 75 insertions(+), 17 deletions(-)
net/netfilter/nft_set_rbtree.c +75 -17
@@ -572,14 +572,12 @@ static struct nft_array *nft_array_alloc(u32 max_intervals)return array; } -#define NFT_ARRAY_EXTRA_SIZE 10240 -/* Similar to nft_rbtree_{u,k}size to hide details to userspace, but consider * packed representation coming from userspace for anonymous sets too. */ static u32 nft_array_elems (const struct nft_set *set) { -u32 nelems =atomic_read (&set->nelems); +u32 nelems =atomic_read (&set->nelems) - set->ndeact;/* Adjacent intervals are represented with a single start element in * anonymous sets, use the current element counter as is.
@@ -595,27 +593,87 @@ static u32 nft_array_elems(const struct nft_set *set)return (nelems /2 ) +2 ; } -static int nft_array_may_resize (const struct nft_set *set) +#define NFT_ARRAY_INITIAL_SIZE 1024 +#define NFT_ARRAY_INITIAL_ANON_SIZE 16 +#define NFT_ARRAY_INITIAL_ANON_THRESH (8192U / sizeof(struct nft_array_interval)) + +static int nft_array_may_resize (const struct nft_set *set,bool flush) { -u32 nelems =nft_array_elems (set), new_max_intervals; +u32 initial_intervals, max_intervals, new_max_intervals, delta; +u32 shrinked_max_intervals, nelems =nft_array_elems (set);struct nft_rbtree *priv =nft_set_priv (set);struct nft_array *array; -if (!priv->array_next) { - array =nft_array_alloc (nelems +NFT_ARRAY_EXTRA_SIZE ); -if (!array) -return -ENOMEM ; +if (nft_set_is_anonymous (set)) + initial_intervals =NFT_ARRAY_INITIAL_ANON_SIZE ; +else + initial_intervals =NFT_ARRAY_INITIAL_SIZE ; + +if (priv->array_next) { + max_intervals = priv->array_next->max_intervals; + new_max_intervals = priv->array_next->max_intervals; + }else { +if (priv->array) { + max_intervals = priv->array->max_intervals; + new_max_intervals = priv->array->max_intervals; + }else { + max_intervals =0 ; + new_max_intervals = initial_intervals; + } + } - priv->array_next = array; +if (nft_set_is_anonymous (set)) + goto maybe_grow; + +if (flush) { +/* Set flush just started, nelems still report elements.*/ + nelems =0 ; + new_max_intervals =NFT_ARRAY_INITIAL_SIZE ; + goto realloc_array; } -if (nelems < priv->array_next->max_intervals) -return 0 ; +if (check_add_overflow (new_max_intervals, new_max_intervals, + &shrinked_max_intervals)) +return -EOVERFLOW ; + + shrinked_max_intervals =DIV_ROUND_UP (shrinked_max_intervals,3 ); - new_max_intervals = priv->array_next->max_intervals +NFT_ARRAY_EXTRA_SIZE ; -if (nft_array_intervals_alloc (priv->array_next, new_max_intervals) <0 ) +if (shrinked_max_intervals >NFT_ARRAY_INITIAL_SIZE && + nelems < shrinked_max_intervals) { + new_max_intervals = shrinked_max_intervals; + goto realloc_array; + } +maybe_grow: +if (nelems > new_max_intervals) { +if (nft_set_is_anonymous (set) && + new_max_intervals <NFT_ARRAY_INITIAL_ANON_THRESH ) { + new_max_intervals <<=1 ; + }else { + delta = new_max_intervals >>1 ; +if (check_add_overflow (new_max_intervals, delta, + &new_max_intervals)) +return -EOVERFLOW ; + } + } + +realloc_array: +if (WARN_ON_ONCE (nelems > new_max_intervals))return -ENOMEM ; +if (priv->array_next) { +if (max_intervals == new_max_intervals) +return 0 ; + +if (nft_array_intervals_alloc (priv->array_next, new_max_intervals) <0 ) +return -ENOMEM ; + }else { + array =nft_array_alloc (new_max_intervals); +if (!array) +return -ENOMEM ; + + priv->array_next = array; + } +return 0 ; }
@@ -630,7 +688,7 @@ static int nft_rbtree_insert(const struct net *net, const struct nft_set *set,nft_rbtree_maybe_reset_start_cookie (priv, tstamp); -if (nft_array_may_resize (set) <0 ) +if (nft_array_may_resize (set, false) <0 )return -ENOMEM ; do {
@@ -741,7 +799,7 @@ nft_rbtree_deactivate(const struct net *net, const struct nft_set *set, nft_rbtree_interval_null(set, this)) priv->start_rbe_cookie =0 ; -if (nft_array_may_resize (set) <0 ) +if (nft_array_may_resize (set, false) <0 )return NULL ;while (parent !=NULL ) {
@@ -811,7 +869,7 @@ static void nft_rbtree_walk(const struct nft_ctx *ctx,switch (iter->type) {case NFT_ITER_UPDATE_CLONE : -if (nft_array_may_resize (set) <0 ) { +if (nft_array_may_resize (set, true) <0 ) { iter->err = -ENOMEM ;break ; }
Signature · 1 line
2.52.0
Florian Westphal
maintainerPATCH 8/14 netfilter: nf_conntrack_expect: honor expectation helper field
In reply to Florian WestphalFrom: Pablo Neira Ayuso <[email protected]>
The expectation helper field is mostly unused. As a result, the netfilter codebase relies on accessing the helper through exp->master.
Always set on the expectation helper field so it can be used to reach the helper.
This is a preparation patches for follow up fixes.
--- include/net/netfilter/nf_conntrack_expect.h | 2 +- net/netfilter/nf_conntrack_broadcast.c | 2 +- net/netfilter/nf_conntrack_expect.c | 6 +++++- net/netfilter/nf_conntrack_h323_main.c | 12 ++++++------ net/netfilter/nf_conntrack_helper.c | 7 ++++++- net/netfilter/nf_conntrack_netlink.c | 2 +- net/netfilter/nf_conntrack_sip.c | 2 +- 7 files changed, 21 insertions(+), 12 deletions(-)
include/net/netfilter/nf_conntrack_expect.h +1 -1
@@ -40,7 +40,7 @@ struct nf_conntrack_expect { struct nf_conntrack_expect *this);/* Helper to assign to new connection */ -struct nf_conntrack_helper *helper; +struct nf_conntrack_helper __rcu *helper;/* The conntrack of the master connection */ struct nf_conn *master;
net/netfilter/nf_conntrack_broadcast.c +1 -1
@@ -70,7 +70,7 @@ int nf_conntrack_broadcast_help(struct sk_buff *skb, exp->expectfn =NULL ; exp->flags =NF_CT_EXPECT_PERMANENT ; exp->class =NF_CT_EXPECT_CLASS_DEFAULT ; - exp->helper =NULL ; +rcu_assign_pointer (exp->helper,nfct_help (ct)->helper);nf_ct_expect_related (exp,0 );nf_ct_expect_put (exp);
net/netfilter/nf_conntrack_expect.c +5 -1
@@ -309,6 +309,10 @@ struct nf_conntrack_expect *nf_ct_expect_alloc(struct nf_conn *me) }EXPORT_SYMBOL_GPL (nf_ct_expect_alloc); +/* This function can only be used from packet path, where accessing +* master's helper is safe, because the packet holds a reference on +* the conntrack object. Never use it from control plane. +*/ void nf_ct_expect_init(struct nf_conntrack_expect *exp, unsigned int class, u_int8_t family, const union nf_inet_addr *saddr,
@@ -325,7 +329,7 @@ void nf_ct_expect_init(struct nf_conntrack_expect *exp, unsigned int class, exp->flags =0 ; exp->class = class; exp->expectfn =NULL ; - exp->helper =NULL ; +rcu_assign_pointer (exp->helper,nfct_help (exp->master)->helper); exp->tuple.src.l3num = family; exp->tuple.dst.protonum = proto;
net/netfilter/nf_conntrack_h323_main.c +6 -6
@@ -643,7 +643,7 @@ static int expect_h245(struct sk_buff *skb, struct nf_conn *ct, &ct->tuplehash[!dir].tuple.src.u3, &ct->tuplehash[!dir].tuple.dst.u3, IPPROTO_TCP, NULL, &port); - exp->helper = &nf_conntrack_helper_h245; +rcu_assign_pointer (exp->helper, &nf_conntrack_helper_h245); nathook =rcu_dereference (nfct_h323_nat_hook); if (memcmp(&ct->tuplehash[dir].tuple.src.u3,
@@ -767,7 +767,7 @@ static int expect_callforwarding(struct sk_buff *skb,nf_ct_expect_init (exp,NF_CT_EXPECT_CLASS_DEFAULT ,nf_ct_l3num (ct), &ct->tuplehash[!dir].tuple.src.u3, &addr,IPPROTO_TCP ,NULL , &port); - exp->helper = nf_conntrack_helper_q931; +rcu_assign_pointer (exp->helper, nf_conntrack_helper_q931); nathook =rcu_dereference (nfct_h323_nat_hook); if (memcmp(&ct->tuplehash[dir].tuple.src.u3,
@@ -1234,7 +1234,7 @@ static int expect_q931(struct sk_buff *skb, struct nf_conn *ct, &ct->tuplehash[!dir].tuple.src.u3 : NULL, &ct->tuplehash[!dir].tuple.dst.u3, IPPROTO_TCP, NULL, &port); - exp->helper = nf_conntrack_helper_q931; +rcu_assign_pointer (exp->helper, nf_conntrack_helper_q931); exp->flags =NF_CT_EXPECT_PERMANENT ;/* Accept multiple calls */ nathook =rcu_dereference (nfct_h323_nat_hook);
@@ -1306,7 +1306,7 @@ static int process_gcf(struct sk_buff *skb, struct nf_conn *ct,nf_ct_expect_init (exp,NF_CT_EXPECT_CLASS_DEFAULT ,nf_ct_l3num (ct), &ct->tuplehash[!dir].tuple.src.u3, &addr,IPPROTO_UDP ,NULL , &port); - exp->helper = nf_conntrack_helper_ras; +rcu_assign_pointer (exp->helper, nf_conntrack_helper_ras);if (nf_ct_expect_related (exp,0 ) ==0 ) {pr_debug ("nf_ct_ras: expect RAS " );
@@ -1523,7 +1523,7 @@ static int process_acf(struct sk_buff *skb, struct nf_conn *ct, &ct->tuplehash[!dir].tuple.src.u3, &addr, IPPROTO_TCP, NULL, &port); exp->flags =NF_CT_EXPECT_PERMANENT ; - exp->helper = nf_conntrack_helper_q931; +rcu_assign_pointer (exp->helper, nf_conntrack_helper_q931);if (nf_ct_expect_related (exp,0 ) ==0 ) {pr_debug ("nf_ct_ras: expect Q.931 " );
@@ -1577,7 +1577,7 @@ static int process_lcf(struct sk_buff *skb, struct nf_conn *ct, &ct->tuplehash[!dir].tuple.src.u3, &addr, IPPROTO_TCP, NULL, &port); exp->flags =NF_CT_EXPECT_PERMANENT ; - exp->helper = nf_conntrack_helper_q931; +rcu_assign_pointer (exp->helper, nf_conntrack_helper_q931);if (nf_ct_expect_related (exp,0 ) ==0 ) {pr_debug ("nf_ct_ras: expect Q.931 " );
net/netfilter/nf_conntrack_helper.c +6 -1
@@ -399,7 +399,7 @@ static bool expect_iter_me(struct nf_conntrack_expect *exp, void *data)const struct nf_conntrack_helper *me = data;const struct nf_conntrack_helper *this; -if (exp->helper == me) +if (rcu_access_pointer (exp->helper) == me)return true; this = rcu_dereference_protected(help->helper,
@@ -421,6 +421,11 @@ void nf_conntrack_helper_unregister(struct nf_conntrack_helper *me)nf_ct_expect_iterate_destroy (expect_iter_me,NULL );nf_ct_iterate_destroy (unhelp, me); + +/* nf_ct_iterate_destroy() does an unconditional synchronize_rcu() as +* last step, this ensures rcu readers of exp->helper are done. +* No need for another synchronize_rcu() here. +*/ }EXPORT_SYMBOL_GPL (nf_conntrack_helper_unregister);
net/netfilter/nf_conntrack_netlink.c +1 -1
@@ -3573,7 +3573,7 @@ ctnetlink_alloc_expect(const struct nlattr * const cda[], struct nf_conn *ct, exp->class = class; exp->master = ct; - exp->helper = helper; +rcu_assign_pointer (exp->helper, helper); exp->tuple = *tuple; exp->mask.src.u3 = mask->src.u3; exp->mask.src.u.all = mask->src.u.all;
net/netfilter/nf_conntrack_sip.c +1 -1
@@ -1297,7 +1297,7 @@ static int process_register_request(struct sk_buff *skb, unsigned int protoff,nf_ct_expect_init (exp,SIP_EXPECT_SIGNALLING ,nf_ct_l3num (ct), saddr, &daddr, proto,NULL , &port); exp->timeout.expires = sip_timeout *HZ ; - exp->helper = helper; +rcu_assign_pointer (exp->helper, helper); exp->flags =NF_CT_EXPECT_PERMANENT |NF_CT_EXPECT_INACTIVE ; hooks =rcu_dereference (nf_nat_sip_hooks);
Signature · 1 line
2.52.0
Florian Westphal
maintainerPATCH 9/14 netfilter: nf_conntrack_expect: use expect->helper
In reply to Florian WestphalFrom: Pablo Neira Ayuso <[email protected]>
Use expect->helper in ctnetlink and /proc to dump the helper name. Using nfct_help() without holding a reference to the master conntrack is unsafe.
Use exp->master->helper in ctnetlink path if userspace does not provide an explicit helper when creating an expectation to retain the existing behaviour. The ctnetlink expectation path holds the reference on the master conntrack and nf_conntrack_expect lock and the nfnetlink glue path refers to the master ct that is attached to the skb.
--- net/netfilter/nf_conntrack_expect.c | 2 +- net/netfilter/nf_conntrack_helper.c | 6 +----- net/netfilter/nf_conntrack_netlink.c | 24 ++++++++++-------------- net/netfilter/nf_conntrack_sip.c | 2 +- 4 files changed, 13 insertions(+), 21 deletions(-)
net/netfilter/nf_conntrack_expect.c +1 -1
@@ -658,7 +658,7 @@ static int exp_seq_show(struct seq_file *s, void *v)if (expect->flags &NF_CT_EXPECT_USERSPACE )seq_printf (s,"%sUSERSPACE" , delim); - helper =rcu_dereference (nfct_help (expect->master)->helper); + helper =rcu_dereference (expect->helper);if (helper) {seq_printf (s,"%s%s" , expect->flags ?" " :"" , helper->name);if (helper->expect_policy[expect->class].name[0 ])
net/netfilter/nf_conntrack_helper.c +1 -5
@@ -395,14 +395,10 @@ EXPORT_SYMBOL_GPL(nf_conntrack_helper_register);static bool expect_iter_me (struct nf_conntrack_expect *exp,void *data) { -struct nf_conn_help *help =nfct_help (exp->master);const struct nf_conntrack_helper *me = data;const struct nf_conntrack_helper *this; -if (rcu_access_pointer (exp->helper) == me) -return true; - - this =rcu_dereference_protected (help->helper, + this =rcu_dereference_protected (exp->helper,lockdep_is_held (&nf_conntrack_expect_lock));return this == me; }
net/netfilter/nf_conntrack_netlink.c +10 -14
@@ -3012,7 +3012,7 @@ ctnetlink_exp_dump_expect(struct sk_buff *skb, {struct nf_conn *master = exp->master;long timeout = ((long )exp->timeout.expires - (long )jiffies) /HZ ; -struct nf_conn_help *help; +struct nf_conntrack_helper *helper;#if IS_ENABLED (CONFIG_NF_NAT )struct nlattr *nest_parms;struct nf_conntrack_tuple nat_tuple = {};
@@ -3057,15 +3057,12 @@ ctnetlink_exp_dump_expect(struct sk_buff *skb, nla_put_be32(skb, CTA_EXPECT_FLAGS, htonl(exp->flags)) || nla_put_be32(skb, CTA_EXPECT_CLASS, htonl(exp->class))) goto nla_put_failure; - help =nfct_help (master); -if (help) { -struct nf_conntrack_helper *helper; - helper =rcu_dereference (help->helper); -if (helper && -nla_put_string (skb,CTA_EXPECT_HELP_NAME , helper->name)) - goto nla_put_failure; - } + helper =rcu_dereference (exp->helper); +if (helper && +nla_put_string (skb,CTA_EXPECT_HELP_NAME , helper->name)) + goto nla_put_failure; + expfn =nf_ct_helper_expectfn_find_by_symbol (exp->expectfn);if (expfn !=NULL &&nla_put_string (skb,CTA_EXPECT_FN , expfn->name))
@@ -3394,12 +3391,9 @@ static int ctnetlink_get_expect(struct sk_buff *skb,static bool expect_iter_name (struct nf_conntrack_expect *exp,void *data) {struct nf_conntrack_helper *helper; -const struct nf_conn_help *m_help;const char *name = data; - m_help =nfct_help (exp->master); - - helper =rcu_dereference (m_help->helper); + helper =rcu_dereference (exp->helper);if (!helper)return false;
@@ -3534,9 +3528,9 @@ ctnetlink_alloc_expect(const struct nlattr * const cda[], struct nf_conn *ct,struct nf_conntrack_tuple *tuple, struct nf_conntrack_tuple *mask) { - u_int32_t class = 0; struct nf_conntrack_expect *exp;struct nf_conn_help *help; +u32 class =0 ;int err; help =nfct_help (ct);
@@ -3573,6 +3567,8 @@ ctnetlink_alloc_expect(const struct nlattr * const cda[], struct nf_conn *ct, exp->class = class; exp->master = ct; +if (!helper) + helper =rcu_dereference (help->helper);rcu_assign_pointer (exp->helper, helper); exp->tuple = *tuple; exp->mask.src.u3 = mask->src.u3;
net/netfilter/nf_conntrack_sip.c +1 -1
@@ -924,7 +924,7 @@ static int set_expected_rtp_rtcp(struct sk_buff *skb, unsigned int protoff, exp =__nf_ct_expect_find (net,nf_ct_zone (ct), &tuple);if (!exp || exp->master == ct || -nfct_help (exp->master)->helper !=nfct_help (ct)->helper || + exp->helper !=nfct_help (ct)->helper || exp->class != class)break ;#if IS_ENABLED (CONFIG_NF_NAT )
Signature · 1 line
2.52.0
Florian Westphal
maintainerPATCH 10/14 netfilter: ctnetlink: ensure safe access to master conntrack
In reply to Florian WestphalFrom: Pablo Neira Ayuso <[email protected]>
Holding reference on the expectation is not sufficient, the master conntrack object can just go away, making exp->master invalid.
To access exp->master safely:
- Grab the nf_conntrack_expect_lock, this gets serialized with clean_from_lists() which also holds this lock when the master conntrack goes away.
- Hold reference on master conntrack via nf_conntrack_find_get(). Not so easy since the master tuple to look up for the master conntrack is not available in the existing problematic paths.
This patch goes for extending the nf_conntrack_expect_lock section to address this issue for simplicity, in the cases that are described below this is just slightly extending the lock section.
The add expectation command already holds a reference to the master conntrack from ctnetlink_create_expect().
However, the delete expectation command needs to grab the spinlock before looking up for the expectation. Expand the existing spinlock section to address this to cover the expectation lookup. Note that, the nf_ct_expect_iterate_net() calls already grabs the spinlock while iterating over the expectation table, which is correct.
The get expectation command needs to grab the spinlock to ensure master conntrack does not go away. This also expands the existing spinlock section to cover the expectation lookup too. I needed to move the netlink skb allocation out of the spinlock to keep it GFP_KERNEL.
For the expectation events, the IPEXP_DESTROY event is already delivered under the spinlock, just move the delivery of IPEXP_NEW under the spinlock too because the master conntrack event cache is reached through exp->master.
While at it, add lockdep notations to help identify what codepaths need to grab the spinlock.
--- include/net/netfilter/nf_conntrack_core.h | 5 ++++ net/netfilter/nf_conntrack_ecache.c | 2 ++ net/netfilter/nf_conntrack_expect.c | 10 +++++++- net/netfilter/nf_conntrack_netlink.c | 28 +++++++++++++++-------- 4 files changed, 35 insertions(+), 10 deletions(-)
include/net/netfilter/nf_conntrack_core.h +5 -0
@@ -83,6 +83,11 @@ void nf_conntrack_lock(spinlock_t *lock);extern spinlock_t nf_conntrack_expect_lock; +static inline void lockdep_nfct_expect_lock_held (void ) +{ +lockdep_assert_held (&nf_conntrack_expect_lock); +} +/* ctnetlink code shared by both ctnetlink and nf_conntrack_bpf */ static inline void __nf_ct_set_timeout (struct nf_conn *ct,u64 timeout)
net/netfilter/nf_conntrack_ecache.c +2 -0
@@ -247,6 +247,8 @@ void nf_ct_expect_event_report(enum ip_conntrack_expect_events event,struct nf_ct_event_notifier *notify;struct nf_conntrack_ecache *e; +lockdep_nfct_expect_lock_held (); +rcu_read_lock (); notify =rcu_dereference (net->ct.nf_conntrack_event_cb);if (!notify)
net/netfilter/nf_conntrack_expect.c +9 -1
@@ -51,6 +51,7 @@ void nf_ct_unlink_expect_report(struct nf_conntrack_expect *exp,struct net *net =nf_ct_exp_net (exp);struct nf_conntrack_net *cnet; +lockdep_nfct_expect_lock_held ();WARN_ON (!master_help);WARN_ON (timer_pending (&exp->timeout));
@@ -118,6 +119,8 @@ nf_ct_exp_equal(const struct nf_conntrack_tuple *tuple,bool nf_ct_remove_expect (struct nf_conntrack_expect *exp) { +lockdep_nfct_expect_lock_held (); +if (timer_delete (&exp->timeout)) {nf_ct_unlink_expect (exp);nf_ct_expect_put (exp);
@@ -177,6 +180,8 @@ nf_ct_find_expectation(struct net *net,struct nf_conntrack_expect *i, *exp =NULL ;unsigned int h; +lockdep_nfct_expect_lock_held (); +if (!cnet->expect_count)return NULL ;
@@ -446,6 +451,8 @@ static inline int __nf_ct_expect_check(struct nf_conntrack_expect *expect,unsigned int h;int ret =0 ; +lockdep_nfct_expect_lock_held (); +if (!master_help) { ret = -ESHUTDOWN ; goto out;
@@ -502,8 +509,9 @@ int nf_ct_expect_related_report(struct nf_conntrack_expect *expect,nf_ct_expect_insert (expect); -spin_unlock_bh (&nf_conntrack_expect_lock);nf_ct_expect_event_report (IPEXP_NEW , expect, portid, report); +spin_unlock_bh (&nf_conntrack_expect_lock); +return 0 ; out:spin_unlock_bh (&nf_conntrack_expect_lock);
net/netfilter/nf_conntrack_netlink.c +19 -9
@@ -3355,31 +3355,37 @@ static int ctnetlink_get_expect(struct sk_buff *skb,if (err <0 )return err; + skb2 =nlmsg_new (NLMSG_DEFAULT_SIZE ,GFP_KERNEL ); +if (!skb2) +return -ENOMEM ; + +spin_lock_bh (&nf_conntrack_expect_lock); exp =nf_ct_expect_find_get (info->net, &zone, &tuple); -if (!exp) +if (!exp) { +spin_unlock_bh (&nf_conntrack_expect_lock); +kfree_skb (skb2);return -ENOENT ; + }if (cda[CTA_EXPECT_ID ]) {__be32 id =nla_get_be32 (cda[CTA_EXPECT_ID ]);if (id !=nf_expect_get_id (exp)) {nf_ct_expect_put (exp); +spin_unlock_bh (&nf_conntrack_expect_lock); +kfree_skb (skb2);return -ENOENT ; } } - skb2 =nlmsg_new (NLMSG_DEFAULT_SIZE ,GFP_KERNEL ); -if (!skb2) { -nf_ct_expect_put (exp); -return -ENOMEM ; - } -rcu_read_lock (); err =ctnetlink_exp_fill_info (skb2,NETLINK_CB (skb).portid, info->nlh->nlmsg_seq,IPCTNL_MSG_EXP_NEW , exp);rcu_read_unlock ();nf_ct_expect_put (exp); +spin_unlock_bh (&nf_conntrack_expect_lock); +if (err <=0 ) {kfree_skb (skb2);return -ENOMEM ;
@@ -3426,22 +3432,26 @@ static int ctnetlink_del_expect(struct sk_buff *skb,if (err <0 )return err; +spin_lock_bh (&nf_conntrack_expect_lock); +/* bump usage count to 2 */ exp =nf_ct_expect_find_get (info->net, &zone, &tuple); -if (!exp) +if (!exp) { +spin_unlock_bh (&nf_conntrack_expect_lock);return -ENOENT ; + }if (cda[CTA_EXPECT_ID ]) {__be32 id =nla_get_be32 (cda[CTA_EXPECT_ID ]);if (id !=nf_expect_get_id (exp)) {nf_ct_expect_put (exp); +spin_unlock_bh (&nf_conntrack_expect_lock);return -ENOENT ; } }/* after list removal, usage count == 1 */ -spin_lock_bh (&nf_conntrack_expect_lock);if (timer_delete (&exp->timeout)) {nf_ct_unlink_expect_report (exp,NETLINK_CB (skb).portid,nlmsg_report (info->nlh));
Signature · 1 line
2.52.0
Pablo Neira Ayuso
maintainerRe: [PATCH net 10/14] netfilter: ctnetlink: ensure safe access to master conntrack
Hi Florian,
Sorry for this late followup incremental fix.
Quoted 4 lines · Florian Westphal
From: Pablo Neira Ayuso <[email protected]>
Holding reference on the expectation is not sufficient, the master conntrack object can just go away, making
exp->masterinvalid.
This patch needs this update for expectations which do not have nfct_help(ct), two cases:
- nft_ct creates
- ip_vs_ftp
See attached incremental patch.
net/netfilter/nf_conntrack_expect.c +11 -2
@@ -325,7 +325,9 @@ void nf_ct_expect_init(struct nf_conntrack_expect *exp, unsigned int class, u_int8_t proto, const __be16 *src, const __be16 *dst) { struct net *net = read_pnet(&exp->master->ct_net); - +struct nf_conntrack_helper *helper; +struct nf_conn *ct = exp->master; +struct nf_conn_help *help;int len;if (family ==AF_INET )
@@ -336,7 +338,14 @@ void nf_ct_expect_init(struct nf_conntrack_expect *exp, unsigned int class, exp->flags =0 ; exp->class = class; exp->expectfn =NULL ; -rcu_assign_pointer (exp->helper,nfct_help (exp->master)->helper); + help =nfct_help (ct); +if (help) { + helper =rcu_dereference (help->helper); +if (helper) +rcu_assign_pointer (exp->helper, help->helper); + }else { + exp->helper =NULL ; + }write_pnet (&exp->net, net); exp->zone = exp->master->zone; exp->tuple.src.l3num = family;
Pablo Neira Ayuso
maintainerRe: [PATCH net 10/14] netfilter: ctnetlink: ensure safe access to master conntrack
Quoted 10 lines · Pablo Neira Ayuso
Hi Florian,
Sorry for this late followup incremental fix.
Quoted 4 lines · Florian Westphal
From: Pablo Neira Ayuso <[email protected]>
Holding reference on the expectation is not sufficient, the master conntrack object can just go away, making
exp->masterinvalid.
Quoted 7 lines
This patch needs this update for expectations which do not have
nfct_help(ct), two cases:-
nft_ctcreates
-ip_vs_ftpSee attached incremental patch.
Sorry. Actually, This incremental fix is for:
[PATCH net 08/14] netfilter: nf_conntrack_expect: honor expectation helper field
for the two cases I mentioned above.
Quoted 31 lines
net/netfilter/nf_conntrack_expect.c +11
-2@@ -325,7 +325,9 @@ void nf_ct_expect_init(struct nf_conntrack_expect *exp, unsigned int class, u_int8_t proto, const __be16 *src, const __be16 *dst) { struct net *net = read_pnet(&exp->master->ct_net); - +struct nf_conntrack_helper *helper; +struct nf_conn *ct = exp->master; +struct nf_conn_help *help;int len;if (family ==AF_INET )@@ -336,7 +338,14 @@ void nf_ct_expect_init(struct nf_conntrack_expect *exp, unsigned int class, exp->flags =0 ; exp->class = class; exp->expectfn =NULL ; -rcu_assign_pointer (exp->helper,nfct_help (exp->master)->helper); + help =nfct_help (ct); +if (help) { + helper =rcu_dereference (help->helper); +if (helper) +rcu_assign_pointer (exp->helper, help->helper); + }else { + exp->helper =NULL ; + }write_pnet (&exp->net, net); exp->zone = exp->master->zone; exp->tuple.src.l3num = family;
Florian Westphal
maintainerRe: [PATCH net 10/14] netfilter: ctnetlink: ensure safe access to master conntrack
In reply to Pablo Neira AyusoQuoted 1 line · Pablo Neira Ayuso
Sorry for this late followup incremental fix.
I'm tired.
Quoted 16 lines
net/netfilter/nf_conntrack_expect.c +3
-1@@ -325,7 +325,9 @@ void nf_ct_expect_init(struct nf_conntrack_expect *exp, unsigned int class, u_int8_t proto, const __be16 *src, const __be16 *dst) { struct net *net = read_pnet(&exp->master->ct_net); - +struct nf_conntrack_helper *helper; +struct nf_conn *ct = exp->master; +struct nf_conn_help *help;int len;if (family ==AF_INET )
net/netfilter/nf_conntrack_expect.c void nf_ct_expect_init(struct nf_conntrack_expect *exp, unsigned int class, · line 341
Quoted 5 lines
net/netfilter/nf_conntrack_expect.c +1
-1@@ -336,7 +338,14 @@ void nf_ct_expect_init(struct nf_conntrack_expect *exp, unsigned int class, exp->flags =0 ; exp->class = class; exp->expectfn =NULL ; -rcu_assign_pointer (exp->helper,nfct_help (exp->master)->helper); + help =nfct_help (ct);
Do we have a reference here? Is that safe?
I'm not looking forward to a new PR.
Pablo Neira Ayuso
maintainerRe: [PATCH net 10/14] netfilter: ctnetlink: ensure safe access to master conntrack
Quoted 19 lines · Florian Westphal
Quoted 1 line · Pablo Neira Ayuso
Sorry for this late followup incremental fix.
I'm tired.
Quoted 14 lines
net/netfilter/nf_conntrack_expect.c +3
-1@@ -325,7 +325,9 @@ void nf_ct_expect_init(struct nf_conntrack_expect *exp, unsigned int class, u_int8_t proto, const __be16 *src, const __be16 *dst) { struct net *net = read_pnet(&exp->master->ct_net); - +struct nf_conntrack_helper *helper; +struct nf_conn *ct = exp->master; +struct nf_conn_help *help;int len;
Quoted 9 lines
net/netfilter/nf_conntrack_expect.c void nf_ct_expect_init(struct nf_conntrack_expect *exp, unsigned int class, · line 341
Quoted 7 lines
net/netfilter/nf_conntrack_expect.c +1
-1if (family ==AF_INET )@@ -336,7 +338,14 @@ void nf_ct_expect_init(struct nf_conntrack_expect *exp, unsigned int class, exp->flags =0 ; exp->class = class; exp->expectfn =NULL ; -rcu_assign_pointer (exp->helper,nfct_help (exp->master)->helper); + help =nfct_help (ct);Do we have a reference here? Is that safe?
This ct is coming from the skb from packet path, while rcu_read_lock() is held, the skb owns this ct.
Quoted 1 line
I'm not looking forward to a new PR.
Apologies.
Florian Westphal
maintainerPATCH 11/14 netfilter: nf_conntrack_expect: store netns and zone in expectation
In reply to Florian WestphalFrom: Pablo Neira Ayuso <[email protected]>
__nf_ct_expect_find() and nf_ct_expect_find_get() are called under rcu_read_lock() but they dereference the master conntrack via exp->master.
Since the expectation does not hold a reference on the master conntrack, this could be dying conntrack or different recycled conntrack than the real master due to SLAB_TYPESAFE_RCU.
Store the netns, the master_tuple and the zone in struct nf_conntrack_expect as a safety measure.
This patch is required by the follow up fix not to dump expectations that do not belong to this netns.
--- include/net/netfilter/nf_conntrack_expect.h | 18 +++++++++++++++++- net/netfilter/nf_conntrack_expect.c | 8 ++++++-- net/netfilter/nf_conntrack_netlink.c | 3 +++ 3 files changed, 26 insertions(+), 3 deletions(-)
include/net/netfilter/nf_conntrack_expect.h +17 -1
@@ -22,10 +22,16 @@ struct nf_conntrack_expect {/* Hash member */ struct hlist_node hnode; +/* Network namespace */ +possible_net_t net; +/* We expect this tuple, with the following mask */ struct nf_conntrack_tuple tuple;struct nf_conntrack_tuple_mask mask; +#ifdef CONFIG_NF_CONNTRACK_ZONES +struct nf_conntrack_zone zone; +#endif /* Usage count. */ refcount_t use;
@@ -62,7 +68,17 @@ struct nf_conntrack_expect {static inline struct net *nf_ct_exp_net (struct nf_conntrack_expect *exp) { -return nf_ct_net (exp->master); +return read_pnet (&exp->net); +} + +static inline bool nf_ct_exp_zone_equal_any (const struct nf_conntrack_expect *a, +const struct nf_conntrack_zone *b) +{ +#ifdef CONFIG_NF_CONNTRACK_ZONES +return a->zone.id == b->id; +#else +return true; +#endif }#define NF_CT_EXP_POLICY_NAME_LEN 16
net/netfilter/nf_conntrack_expect.c +6 -2
@@ -113,8 +113,8 @@ nf_ct_exp_equal(const struct nf_conntrack_tuple *tuple, const struct net *net) {return nf_ct_tuple_mask_cmp (tuple, &i->tuple, &i->mask) && -net_eq (net,nf_ct_net (i->master)) && -nf_ct_zone_equal_any (i->master, zone); +net_eq (net,read_pnet (&i->net)) && +nf_ct_exp_zone_equal_any (i, zone); }bool nf_ct_remove_expect (struct nf_conntrack_expect *exp)
@@ -324,6 +324,8 @@ void nf_ct_expect_init(struct nf_conntrack_expect *exp, unsigned int class,const union nf_inet_addr *daddr, u_int8_t proto, const __be16 *src, const __be16 *dst) { + struct net *net = read_pnet(&exp->master->ct_net); +int len;if (family ==AF_INET )
@@ -335,6 +337,8 @@ void nf_ct_expect_init(struct nf_conntrack_expect *exp, unsigned int class, exp->class = class; exp->expectfn =NULL ;rcu_assign_pointer (exp->helper,nfct_help (exp->master)->helper); +write_pnet (&exp->net, net); + exp->zone = exp->master->zone; exp->tuple.src.l3num = family; exp->tuple.dst.protonum = proto;
net/netfilter/nf_conntrack_netlink.c +3 -0
@@ -3538,6 +3538,7 @@ ctnetlink_alloc_expect(const struct nlattr * const cda[], struct nf_conn *ct,struct nf_conntrack_tuple *tuple, struct nf_conntrack_tuple *mask) { + struct net *net = read_pnet(&ct->ct_net);struct nf_conntrack_expect *exp;struct nf_conn_help *help;u32 class =0 ;
@@ -3577,6 +3578,8 @@ ctnetlink_alloc_expect(const struct nlattr * const cda[], struct nf_conn *ct, exp->class = class; exp->master = ct; +write_pnet (&exp->net, net); + exp->zone = ct->zone;if (!helper) helper =rcu_dereference (help->helper);rcu_assign_pointer (exp->helper, helper);
Signature · 1 line
2.52.0
Florian Westphal
maintainerPATCH 12/14 netfilter: nf_conntrack_expect: skip expectations in other netns via proc
In reply to Florian WestphalFrom: Pablo Neira Ayuso <[email protected]>
Skip expectations that do not reside in this netns.
Similar to e77e6ff502ea ("netfilter: conntrack: do not dump other netns's conntrack entries via proc").
--- net/netfilter/nf_conntrack_expect.c | 4 ++++ 1 file changed, 4 insertions(+)
net/netfilter/nf_conntrack_expect.c +4 -0
@@ -643,11 +643,15 @@ static int exp_seq_show(struct seq_file *s, void *v) {struct nf_conntrack_expect *expect;struct nf_conntrack_helper *helper; +struct net *net =seq_file_net (s);struct hlist_node *n = v;char *delim ="" ; expect = hlist_entry(n, struct nf_conntrack_expect, hnode); +if (!net_eq (nf_ct_exp_net (expect), net)) +return 0 ; +if (expect->timeout.function)seq_printf (s,"%ld " ,timer_pending (&expect->timeout) ? (long )(expect->timeout.expires - jiffies)/HZ :0 );
Signature · 1 line
2.52.0
Florian Westphal
maintainerPATCH 13/14 netfilter: nf_conntrack_sip: fix use of uninitialized rtp_addr in process_sdp
In reply to Florian WestphalFrom: Weiming Shi <[email protected]>
process_sdp() declares union nf_inet_addr rtp_addr on the stack and passes it to the nf_nat_sip sdp_session hook after walking the SDP media descriptions. However rtp_addr is only initialized inside the media loop when a recognized media type with a non-zero port is found.
If the SDP body contains no m= lines, only inactive media sections (m=audio 0 ...) or only unrecognized media types, rtp_addr is never assigned. Despite that, the function still calls hooks->sdp_session() with &rtp_addr, causing nf_nat_sdp_session() to format the stale stack value as an IP address and rewrite the SDP session owner and connection lines with it.
With CONFIG_INIT_STACK_ALL_ZERO (default on most distributions) this results in the session-level o= and c= addresses being rewritten to 0.0.0.0 for inactive SDP sessions. Without stack auto-init the rewritten address is whatever happened to be on the stack.
Fix this by pre-initializing rtp_addr from the session-level connection address (caddr) when available, and tracking via a have_rtp_addr flag whether any valid address was established. Skip the sdp_session hook entirely when no valid address exists.
--- net/netfilter/nf_conntrack_sip.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-)
net/netfilter/nf_conntrack_sip.c +10 -4
@@ -1040,6 +1040,7 @@ static int process_sdp(struct sk_buff *skb, unsigned int protoff,unsigned int port;const struct sdp_media_type *t;int ret =NF_ACCEPT ; +bool have_rtp_addr = false; hooks =rcu_dereference (nf_nat_sip_hooks);
@@ -1056,8 +1057,11 @@ static int process_sdp(struct sk_buff *skb, unsigned int protoff, caddr_len =0 ;if (ct_sip_parse_sdp_addr (ct, *dptr, sdpoff, *datalen,SDP_HDR_CONNECTION ,SDP_HDR_MEDIA , - &matchoff, &matchlen, &caddr) >0 ) + &matchoff, &matchlen, &caddr) >0 ) { caddr_len = matchlen; +memcpy (&rtp_addr, &caddr,sizeof (rtp_addr)); + have_rtp_addr = true; + } mediaoff = sdpoff;for (i =0 ; i <ARRAY_SIZE (sdp_media_types); ) {
@@ -1091,9 +1095,11 @@ static int process_sdp(struct sk_buff *skb, unsigned int protoff, &matchoff, &matchlen, &maddr) > 0) { maddr_len = matchlen;memcpy (&rtp_addr, &maddr,sizeof (rtp_addr)); - } else if (caddr_len) + have_rtp_addr = true; + } else if (caddr_len) {memcpy (&rtp_addr, &caddr,sizeof (rtp_addr)); - else { + have_rtp_addr = true; + } else {nf_ct_helper_log (skb, ct,"cannot parse SDP message" );return NF_DROP ; }
@@ -1125,7 +1131,7 @@ static int process_sdp(struct sk_buff *skb, unsigned int protoff,/* Update session connection and owner addresses */ hooks =rcu_dereference (nf_nat_sip_hooks); -if (hooks && ct->status &IPS_NAT_MASK ) +if (hooks && ct->status &IPS_NAT_MASK && have_rtp_addr) ret = hooks->sdp_session (skb, protoff, dataoff, dptr, datalen, sdpoff, &rtp_addr);
Signature · 1 line
2.52.0
Florian Westphal
maintainerPATCH 14/14 netfilter: ctnetlink: use netlink policy range checks
In reply to Florian WestphalFrom: David Carlier <[email protected]>
Replace manual range and mask validations with netlink policy annotations in ctnetlink code paths, so that the netlink core rejects invalid values early and can generate extack errors.
- CTA_PROTOINFO_TCP_STATE: reject values > TCP_CONNTRACK_SYN_SENT2 at policy level, removing the manual >= TCP_CONNTRACK_MAX check.
- CTA_PROTOINFO_TCP_WSCALE_ORIGINAL/REPLY: reject values > TCP_MAX_WSCALE (14). The normal TCP option parsing path already clamps to this value, but the ctnetlink path accepted 0-255, causing undefined behavior when used as a u32 shift count.
- CTA_FILTER_ORIG_FLAGS/REPLY_FLAGS: use NLA_POLICY_MASK with CTA_FILTER_F_ALL, removing the manual mask checks.
- CTA_EXPECT_FLAGS: use NLA_POLICY_MASK with NF_CT_EXPECT_MASK, adding a new mask define grouping all valid expect flags.
Extracted from a broader nf-next patch by Florian Westphal, scoped to ctnetlink for the fixes tree.
--- .../uapi/linux/netfilter/nf_conntrack_common.h | 4 ++++ net/netfilter/nf_conntrack_netlink.c | 16 +++++----------- net/netfilter/nf_conntrack_proto_tcp.c | 10 +++------- 3 files changed, 12 insertions(+), 18 deletions(-)
include/uapi/linux/netfilter/nf_conntrack_common.h +4 -0
@@ -159,5 +159,9 @@ enum ip_conntrack_expect_events {#define NF_CT_EXPECT_INACTIVE 0x2#define NF_CT_EXPECT_USERSPACE 0x4 +#ifdef __KERNEL__ +#define NF_CT_EXPECT_MASK (NF_CT_EXPECT_PERMANENT | NF_CT_EXPECT_INACTIVE | \ + NF_CT_EXPECT_USERSPACE) +#endif #endif /* _UAPI_NF_CONNTRACK_COMMON_H */
net/netfilter/nf_conntrack_netlink.c +5 -11
@@ -910,8 +910,8 @@ struct ctnetlink_filter { };static const struct nla_policy cta_filter_nla_policy[CTA_FILTER_MAX +1 ] = { - [CTA_FILTER_ORIG_FLAGS ] = { .type =NLA_U32 }, - [CTA_FILTER_REPLY_FLAGS ] = { .type =NLA_U32 }, + [CTA_FILTER_ORIG_FLAGS ] =NLA_POLICY_MASK (NLA_U32 ,CTA_FILTER_F_ALL ), + [CTA_FILTER_REPLY_FLAGS ] =NLA_POLICY_MASK (NLA_U32 ,CTA_FILTER_F_ALL ), }; static int ctnetlink_parse_filter(const struct nlattr *attr,
@@ -925,17 +925,11 @@ static int ctnetlink_parse_filter(const struct nlattr *attr,if (ret)return ret; -if (tb[CTA_FILTER_ORIG_FLAGS ]) { +if (tb[CTA_FILTER_ORIG_FLAGS ]) filter->orig_flags =nla_get_u32 (tb[CTA_FILTER_ORIG_FLAGS ]); -if (filter->orig_flags & ~CTA_FILTER_F_ALL ) -return -EOPNOTSUPP ; - } -if (tb[CTA_FILTER_REPLY_FLAGS ]) { +if (tb[CTA_FILTER_REPLY_FLAGS ]) filter->reply_flags =nla_get_u32 (tb[CTA_FILTER_REPLY_FLAGS ]); -if (filter->reply_flags & ~CTA_FILTER_F_ALL ) -return -EOPNOTSUPP ; - }return 0 ; }
@@ -2634,7 +2628,7 @@ static const struct nla_policy exp_nla_policy[CTA_EXPECT_MAX+1] = { [CTA_EXPECT_HELP_NAME] = { .type = NLA_NUL_STRING, .len = NF_CT_HELPER_NAME_LEN - 1 }, [CTA_EXPECT_ZONE] = { .type = NLA_U16 }, - [CTA_EXPECT_FLAGS] = { .type = NLA_U32 }, + [CTA_EXPECT_FLAGS] = NLA_POLICY_MASK(NLA_BE32, NF_CT_EXPECT_MASK), [CTA_EXPECT_CLASS] = { .type = NLA_U32 }, [CTA_EXPECT_NAT] = { .type = NLA_NESTED }, [CTA_EXPECT_FN] = { .type = NLA_NUL_STRING },
net/netfilter/nf_conntrack_proto_tcp.c +3 -7
@@ -1385,9 +1385,9 @@ static int tcp_to_nlattr(struct sk_buff *skb, struct nlattr *nla, }static const struct nla_policy tcp_nla_policy[CTA_PROTOINFO_TCP_MAX +1 ] = { - [CTA_PROTOINFO_TCP_STATE ] = { .type =NLA_U8 }, - [CTA_PROTOINFO_TCP_WSCALE_ORIGINAL ] = { .type =NLA_U8 }, - [CTA_PROTOINFO_TCP_WSCALE_REPLY ] = { .type =NLA_U8 }, + [CTA_PROTOINFO_TCP_STATE ] =NLA_POLICY_MAX (NLA_U8 ,TCP_CONNTRACK_SYN_SENT2 ), + [CTA_PROTOINFO_TCP_WSCALE_ORIGINAL ] =NLA_POLICY_MAX (NLA_U8 ,TCP_MAX_WSCALE ), + [CTA_PROTOINFO_TCP_WSCALE_REPLY ] =NLA_POLICY_MAX (NLA_U8 ,TCP_MAX_WSCALE ), [CTA_PROTOINFO_TCP_FLAGS_ORIGINAL ] = { .len =sizeof (struct nf_ct_tcp_flags ) }, [CTA_PROTOINFO_TCP_FLAGS_REPLY ] = { .len =sizeof (struct nf_ct_tcp_flags ) }, };
@@ -1414,10 +1414,6 @@ static int nlattr_to_tcp(struct nlattr *cda[], struct nf_conn *ct)if (err <0 )return err; -if (tb[CTA_PROTOINFO_TCP_STATE ] && -nla_get_u8 (tb[CTA_PROTOINFO_TCP_STATE ]) >=TCP_CONNTRACK_MAX ) -return -EINVAL ; -spin_lock_bh (&ct->lock);if (tb[CTA_PROTOINFO_TCP_STATE ]) ct->proto.tcp.state =nla_get_u8 (tb[CTA_PROTOINFO_TCP_STATE ]);
Signature · 1 line
2.52.0
Florian Westphal
maintainerQuoted 5 lines · Florian Westphal
Hi,
The following patchset contains Netfilter fixes for *net*. Note that most bugs fixed here stem from 2.6 days, the large PR is not due to an increase in regressions.
please toss this. I'm not sending a new PR.
I hate my life.
Pablo Neira Ayuso
maintainerQuoted 8 lines · Florian Westphal
Quoted 5 lines · Florian Westphal
Hi,
The following patchset contains Netfilter fixes for *net*. Note that most bugs fixed here stem from 2.6 days, the large PR is not due to an increase in regressions.
please toss this. I'm not sending a new PR.
Will you please collapse this incremental fix to 8/14 in the new PR?
Thanks.
Florian Westphal
maintainerQuoted 1 line · Pablo Neira Ayuso
Will you please collapse this incremental fix to 8/14 in the new PR?
I have no plans to make a new PR.
It took me 6h to get this out the door and it turns out its dog shit. I have no trust in my ability to cook something better.
If I could I would just retire.
Pablo Neira Ayuso
maintainerQuoted 4 lines · Florian Westphal
Quoted 1 line · Pablo Neira Ayuso
Will you please collapse this incremental fix to 8/14 in the new PR?
I have no plans to make a new PR.
I can pick it up, I will post this PR in a few hours.
I am very sorry I found this so late.
Quoted 1 line
It took me 6h to get this out the door and it turns out its dog shit.
Maintainance is a lot work, not very well valued these days unfortunately.
I will take care of it.