Skip to contentSkip to conversation

netfilter: nft_set_hash: fix potential NULL deref in nft_rhash_deactivate

PATCH In review Melbin K Mathew 2 participants 2 messages Dec 3, 2025

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 netfilter: nft_set_hash: fix potential NULL deref in nft_rhash_deactivate

In nft_rhash_deactivate(), rhashtable_lookup() may return NULL when the set element is not found, but the function unconditionally returns &he->priv.

Dereferencing a member of a NULL pointer is undefined behavior in C. Although the current struct layout places 'priv' at offset 0 (making this behave like returning NULL), this is fragile and relies on implementation details.

Make the NULL case explicit and return NULL when the lookup fails.

Fixes c07b3b683133 ("netfilter: nf_tables: add rhashtable set backend")
---
 net/netfilter/nft_set_hash.c | 3 +++
 1 file changed, 3 insertions(+)
net/netfilter/nft_set_hash.c +3 -0
index ba01ce75d6de..9ff25ebf93cf 100644
@@ -231,6 +231,9 @@ nft_rhash_deactivate(const struct net *net, const struct nft_set *set,
 
 	rcu_read_unlock();
 
+	if (!he)
+		return NULL;
+
 	return &he->priv;
 }
 
Signature · 1 line
2.45.2
maintainer
#1 lore raw
Quoted 3 lines · Melbin K Mathew

In nft_rhash_deactivate(), rhashtable_lookup() may return NULL when the set element is not found, but the function unconditionally returns &he->priv.

Which is equal to 'return NULL' in that case.

Quoted 1 line

Dereferencing a member of a NULL pointer is undefined behavior in C.

&he->priv doesn't dereference he, it returns the address of the member.

Quoted 3 lines

Although the current struct layout places 'priv' at offset 0 (making this behave like returning NULL), this is fragile and relies on implementation details.

Its not fragile, this file has:

        BUILD_BUG_ON(offsetof(struct nft_rhash_elem, priv) != 0);

to ensure 'priv is first member' requirement.