Skip to contentSkip to conversation

netfilter: nft_set_rbtree: narrow down range completion to anonymous sets

PATCH In review Pablo Neira Ayuso 2 participants 5 messages Oct 7 – Oct 10

Subsystems (2)
lore
Share & tools
lore

Pablo Neira Ayuso

author maintainer
#0 lore raw

PATCH 1/1 netfilter: nft_set_rbtree: narrow down range completion to anonymous sets

For anonymous sets, when adjacent ranges are found, the end element is not added to the set to pack the set representation. Use next start element to complete this interval.

Jérémy Jean says:

Consider two ranges in a netfilter verdict map:
* keys 10-19 accept packets, and
* keys 30-39 jump to chain 'victim'.
Now, delete the end marker at 20. The lookup array now uses element 30 as the first range's end, so an INTERVAL_END query for key 15 returns element 30, including its jump verdict and chain pointer. This can make KASAN reports a UAF after the GC releases reference chain for 30 that has expired:

  BUG: KASAN: slab-use-after-free in nft_verdict_dump+0xdd/0x110
  Read of size 8 at addr ff110000009dfb58 by task repro/50
  ...
  Call Trace:
  ...
   nft_verdict_dump+0xdd/0x110
   nft_data_dump+0x4d/0x80
   nf_tables_fill_setelem+0x30b/0x8a0
   ? __asan_memcpy+0x3c/0x60
   nf_tables_fill_setelem_info+0x1b0/0x200
   nft_get_set_elem+0x3fd/0x4b0
  ...
  Freed by task 24:
  ...
   kfree+0x1a5/0x280
   nf_tables_trans_destroy_work+0x2cb/0x520
Fixes 2aa34191f06f ("netfilter: nft_set_rbtree: use binary search array in get command")
---
 net/netfilter/nft_set_rbtree.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
net/netfilter/nft_set_rbtree.c +2 -1
index 12431b55752f..fbe1750f1ca2 100644
@@ -1091,7 +1091,8 @@ static void nft_rbtree_commit(struct nft_set *set)
 		 * representation. Use next start element to complete this
 		 * interval.
 		 */
-		if (nft_rbtree_interval_start(rbe) &&
+		if (nft_set_is_anonymous(set) &&
+		    nft_rbtree_interval_start(rbe) &&
 		    nft_rbtree_interval_start(prev_rbe) &&
 		    priv->array_next->intervals[num_intervals].from)
 			priv->array_next->intervals[num_intervals++].to = &prev_rbe->ext;
Signature · 1 line
2.47.3
maintainer
#1 lore raw
Quoted 6 lines · Pablo Neira Ayuso

For anonymous sets, when adjacent ranges are found, the end element is not added to the set to pack the set representation. Use next start element to complete this interval.

Jérémy Jean says:

Quoted 8 lines

Consider two ranges in a netfilter verdict map:
* keys 10-19 accept packets, and
* keys 30-39 jump to chain 'victim'.
Now, delete the end marker at 20. The lookup array now uses element 30 as the first range's end, so an INTERVAL_END query for key 15 returns element 30, including its jump verdict and chain pointer. This can make KASAN reports a UAF after the GC releases reference chain for 30 that has expired:

Dumb question: How is it possible to remove an element if the map is anonymous?

author maintainer
#2 lore raw
Quoted 16 lines · Florian Westphal
Quoted 6 lines · Pablo Neira Ayuso

For anonymous sets, when adjacent ranges are found, the end element is not added to the set to pack the set representation. Use next start element to complete this interval.

Jérémy Jean says:

Quoted 8 lines

Consider two ranges in a netfilter verdict map:
* keys 10-19 accept packets, and
* keys 30-39 jump to chain 'victim'.
Now, delete the end marker at 20. The lookup array now uses element 30 as the first range's end, so an INTERVAL_END query for key 15 returns element 30, including its jump verdict and chain pointer. This can make KASAN reports a UAF after the GC releases reference chain for 30 that has expired:

Quoted 2 lines

Dumb question: How is it possible to remove an element if the map is anonymous?

I suspect this is a strange combination which cannot be reached from userspace.

author maintainer
#3 lore raw
Quoted 17 lines · Pablo Neira Ayuso
Quoted 16 lines · Florian Westphal
Quoted 14 lines · Pablo Neira Ayuso

For anonymous sets, when adjacent ranges are found, the end element is not added to the set to pack the set representation. Use next start element to complete this interval.

Jérémy Jean says:

Consider two ranges in a netfilter verdict map:
* keys 10-19 accept packets, and
* keys 30-39 jump to chain 'victim'.
Now, delete the end marker at 20. The lookup array now uses element 30 as the first range's end, so an INTERVAL_END query for key 15 returns element 30, including its jump verdict and chain pointer. This can make KASAN reports a UAF after the GC releases reference chain for 30 that has expired:

Quoted 5 lines
Quoted 2 lines

Dumb question: How is it possible to remove an element if the map is anonymous?

I suspect this is a strange combination which cannot be reached from userspace.

I mean, via nft.

maintainer
#4 lore raw
Quoted 18 lines · Pablo Neira Ayuso
Quoted 17 lines · Pablo Neira Ayuso
Quoted 16 lines · Florian Westphal
Quoted 14 lines · Pablo Neira Ayuso

For anonymous sets, when adjacent ranges are found, the end element is not added to the set to pack the set representation. Use next start element to complete this interval.

Jérémy Jean says:

Consider two ranges in a netfilter verdict map:
* keys 10-19 accept packets, and
* keys 30-39 jump to chain 'victim'.
Now, delete the end marker at 20. The lookup array now uses element 30 as the first range's end, so an INTERVAL_END query for key 15 returns element 30, including its jump verdict and chain pointer. This can make KASAN reports a UAF after the GC releases reference chain for 30 that has expired:

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

Dumb question: How is it possible to remove an element if the map is anonymous?

I suspect this is a strange combination which cannot be reached from userspace.

I mean, via nft.

Even with netlink API: DELSETELEM has:

        if (nft_set_is_anonymous(set))
                return -EOPNOTSUPP;

Jérémy, can you share what your reproducer is doing to get the rbtree map into the bad state?

Thanks!

Just to be clear: this patch is fine, but I a) wonder if there is some other bug elsewhere, and b) if we can make a test case for this for either nftables.git or knft so that this is covered.