Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

netfilter: nft_set_pipapo_avx2: fix skip of expired entries

KASAN reports following splat:
BUG: KASAN: slab-out-of-bounds in pipapo_get_avx2+0x941/0x25d0
Read of size 1 at addr ffff88814c561be0 by task nft/3944
Call Trace:
pipapo_get_avx2+0x941/0x25d0
nft_pipapo_insert+0x440/0x11b0
nf_tables_newsetelem+0x220a/0x3a00
..

This bisects to commit 84c1da7b38d9 ("netfilter: nft_set_pipapo: use AVX2
algorithm for insertions too").

However, that change merely uncovers this bug.

When we find a match but that match has expired or timed out, the AVX2
implementation restarts the full match loop.

At that point, the pointer to the key data has already been changed and
points to the keys last field.
This will then result in out-of-bounds read once its incremented again
for the next field.

The restart logic in AVX2 is different compared to the plain C
implementation, but both should follow the same logic.

The C implementation just calls pipapo_refill() again do check the next
entry. Do the same in the AVX2 implementation.

Note that with this change, due to implementation differences of
pipapo_refill vs. nft_pipapo_avx2_refill, the refill call will return
the same element again. Then, on the next call, it will move to the next
entry as expected. This is because avx2_refill doesn't clear the bitmap
in the 'last' conditional. This is harmless. Expired/timed out elements
are also not expected to be frequent.

selftest is added in a followup commit.

Fixes: 7400b063969b ("nft_set_pipapo: Introduce AVX2-based lookup implementation")
Reviewed-by: Stefano Brivio <sbrivio@redhat.com>
Signed-off-by: Florian Westphal <fw@strlen.de>

+5 -2
+5 -2
net/netfilter/nft_set_pipapo_avx2.c
··· 1179 1179 1180 1180 nft_pipapo_avx2_prepare(); 1181 1181 1182 - next_match: 1183 1182 nft_pipapo_for_each_field(f, i, m) { 1184 1183 bool last = i == m->field_count - 1, first = !i; 1185 1184 int ret = 0; ··· 1225 1226 1226 1227 #undef NFT_SET_PIPAPO_AVX2_LOOKUP 1227 1228 1229 + next_match: 1228 1230 if (ret < 0) { 1229 1231 scratch->map_index = map_index; 1230 1232 kernel_fpu_end(); ··· 1238 1238 1239 1239 e = f->mt[ret].e; 1240 1240 if (unlikely(__nft_set_elem_expired(&e->ext, tstamp) || 1241 - !nft_set_elem_active(&e->ext, genmask))) 1241 + !nft_set_elem_active(&e->ext, genmask))) { 1242 + ret = pipapo_refill(res, f->bsize, f->rules, 1243 + fill, f->mt, last); 1242 1244 goto next_match; 1245 + } 1243 1246 1244 1247 scratch->map_index = map_index; 1245 1248 kernel_fpu_end();