netfilter: nft_set_hash: fix potential NULL deref in nft_rhash_deactivate
Subsystems (3)
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.
--- net/netfilter/nft_set_hash.c | 3 +++ 1 file changed, 3 insertions(+)
net/netfilter/nft_set_hash.c +3 -0
@@ -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
Florian Westphal
maintainerQuoted 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.