Skip to content

Commit 3fda454

Browse files
edumazetgregkh
authored andcommitted
netfilter: nftables: avoid potential overflows on 32bit arches
commit 6c8774a upstream. User space could ask for very large hash tables, we need to make sure our size computations wont overflow. nf_tables_newset() needs to double check the u64 size will fit into size_t field. Fixes: 0ed6389 ("netfilter: nf_tables: rename set implementations") Signed-off-by: Eric Dumazet <edumazet@google.com> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org> Signed-off-by: Florian Westphal <fw@strlen.de> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent cad6239 commit 3fda454

2 files changed

Lines changed: 10 additions & 7 deletions

File tree

‎net/netfilter/nf_tables_api.c‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4115,6 +4115,7 @@ static int nf_tables_newset(struct net *net, struct sock *nlsk,
41154115
struct nft_table *table;
41164116
struct nft_set *set;
41174117
struct nft_ctx ctx;
4118+
size_t alloc_size;
41184119
char *name;
41194120
u64 size;
41204121
u64 timeout;
@@ -4263,8 +4264,10 @@ static int nf_tables_newset(struct net *net, struct sock *nlsk,
42634264
size = 0;
42644265
if (ops->privsize != NULL)
42654266
size = ops->privsize(nla, &desc);
4266-
4267-
set = kvzalloc(sizeof(*set) + size + udlen, GFP_KERNEL);
4267+
alloc_size = sizeof(*set) + size + udlen;
4268+
if (alloc_size < size)
4269+
return -ENOMEM;
4270+
set = kvzalloc(alloc_size, GFP_KERNEL);
42684271
if (!set)
42694272
return -ENOMEM;
42704273

‎net/netfilter/nft_set_hash.c‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,7 @@ static u64 nft_hash_privsize(const struct nlattr * const nla[],
604604
const struct nft_set_desc *desc)
605605
{
606606
return sizeof(struct nft_hash) +
607-
nft_hash_buckets(desc->size) * sizeof(struct hlist_head);
607+
(u64)nft_hash_buckets(desc->size) * sizeof(struct hlist_head);
608608
}
609609

610610
static int nft_hash_init(const struct nft_set *set,
@@ -644,8 +644,8 @@ static bool nft_hash_estimate(const struct nft_set_desc *desc, u32 features,
644644
return false;
645645

646646
est->size = sizeof(struct nft_hash) +
647-
nft_hash_buckets(desc->size) * sizeof(struct hlist_head) +
648-
desc->size * sizeof(struct nft_hash_elem);
647+
(u64)nft_hash_buckets(desc->size) * sizeof(struct hlist_head) +
648+
(u64)desc->size * sizeof(struct nft_hash_elem);
649649
est->lookup = NFT_SET_CLASS_O_1;
650650
est->space = NFT_SET_CLASS_O_N;
651651

@@ -662,8 +662,8 @@ static bool nft_hash_fast_estimate(const struct nft_set_desc *desc, u32 features
662662
return false;
663663

664664
est->size = sizeof(struct nft_hash) +
665-
nft_hash_buckets(desc->size) * sizeof(struct hlist_head) +
666-
desc->size * sizeof(struct nft_hash_elem);
665+
(u64)nft_hash_buckets(desc->size) * sizeof(struct hlist_head) +
666+
(u64)desc->size * sizeof(struct nft_hash_elem);
667667
est->lookup = NFT_SET_CLASS_O_1;
668668
est->space = NFT_SET_CLASS_O_N;
669669

0 commit comments

Comments
 (0)