netfilter: nf_tables: missing objects with no memcg accounting
PATCH 1/2 netfilter: nf_tables: missing objects with no memcg accounting
From: Pablo Neira Ayuso <[email protected]>
[ Upstream commit 69e687cea79fc99a17dfb0116c8644b9391b915e ]
Several ruleset objects are still not using GFP_KERNEL_ACCOUNT for memory accounting, update them. This includes:
- catchall elements
- compat match large info area
- log prefix
- meta secctx
- numgen counters
- pipapo set backend datastructure
- tunnel private objects
--- net/netfilter/nf_tables_api.c | 2 +- net/netfilter/nft_compat.c | 6 +++--- net/netfilter/nft_log.c | 2 +- net/netfilter/nft_meta.c | 2 +- net/netfilter/nft_numgen.c | 2 +- net/netfilter/nft_set_pipapo.c | 10 +++++----- net/netfilter/nft_tunnel.c | 5 +++-- 7 files changed, 15 insertions(+), 14 deletions(-)
net/netfilter/nf_tables_api.c +1 -1
@@ -6615,7 +6615,7 @@ static int nft_setelem_catchall_insert(const struct net *net, } } - catchall =kmalloc (sizeof (*catchall),GFP_KERNEL ); + catchall =kmalloc (sizeof (*catchall),GFP_KERNEL_ACCOUNT );if (!catchall)return -ENOMEM ;
net/netfilter/nft_compat.c +3 -3
@@ -535,7 +535,7 @@ nft_match_large_init(const struct nft_ctx *ctx, const struct nft_expr *expr,struct xt_match *m = expr->ops->data;int ret; - priv->info =kmalloc (XT_ALIGN (m->matchsize),GFP_KERNEL ); + priv->info =kmalloc (XT_ALIGN (m->matchsize),GFP_KERNEL_ACCOUNT );if (!priv->info)return -ENOMEM ;
@@ -808,7 +808,7 @@ nft_match_select_ops(const struct nft_ctx *ctx, goto err; } - ops =kzalloc (sizeof (struct nft_expr_ops ),GFP_KERNEL ); + ops =kzalloc (sizeof (struct nft_expr_ops ),GFP_KERNEL_ACCOUNT );if (!ops) { err = -ENOMEM ; goto err;
@@ -898,7 +898,7 @@ nft_target_select_ops(const struct nft_ctx *ctx, goto err; } - ops =kzalloc (sizeof (struct nft_expr_ops ),GFP_KERNEL ); + ops =kzalloc (sizeof (struct nft_expr_ops ),GFP_KERNEL_ACCOUNT );if (!ops) { err = -ENOMEM ; goto err;
net/netfilter/nft_log.c +1 -1
@@ -163,7 +163,7 @@ static int nft_log_init(const struct nft_ctx *ctx, nla = tb[NFTA_LOG_PREFIX ];if (nla !=NULL ) { - priv->prefix =kmalloc (nla_len (nla) +1 ,GFP_KERNEL ); + priv->prefix =kmalloc (nla_len (nla) +1 ,GFP_KERNEL_ACCOUNT );if (priv->prefix ==NULL )return -ENOMEM ;nla_strscpy (priv->prefix, nla,nla_len (nla) +1 );
net/netfilter/nft_meta.c +1 -1
@@ -952,7 +952,7 @@ static int nft_secmark_obj_init(const struct nft_ctx *ctx,if (tb[NFTA_SECMARK_CTX ] ==NULL )return -EINVAL ; - priv->ctx =nla_strdup (tb[NFTA_SECMARK_CTX ],GFP_KERNEL ); + priv->ctx =nla_strdup (tb[NFTA_SECMARK_CTX ],GFP_KERNEL_ACCOUNT );if (!priv->ctx)return -ENOMEM ;
net/netfilter/nft_numgen.c +1 -1
@@ -66,7 +66,7 @@ static int nft_ng_inc_init(const struct nft_ctx *ctx,if (priv->offset + priv->modulus -1 < priv->offset)return -EOVERFLOW ; - priv->counter =kmalloc (sizeof (*priv->counter),GFP_KERNEL ); + priv->counter =kmalloc (sizeof (*priv->counter),GFP_KERNEL_ACCOUNT );if (!priv->counter)return -ENOMEM ;
net/netfilter/nft_set_pipapo.c +5 -5
@@ -869,7 +869,7 @@ static void pipapo_lt_bits_adjust(struct nft_pipapo_field *f)return ; } - new_lt =kvzalloc (lt_size +NFT_PIPAPO_ALIGN_HEADROOM ,GFP_KERNEL ); + new_lt =kvzalloc (lt_size +NFT_PIPAPO_ALIGN_HEADROOM ,GFP_KERNEL_ACCOUNT );if (!new_lt)return ;
@@ -1145,7 +1145,7 @@ static int pipapo_realloc_scratch(struct nft_pipapo_match *clone, scratch =kzalloc_node (struct_size (scratch, map, bsize_max *2 ) +NFT_PIPAPO_ALIGN_HEADROOM , -GFP_KERNEL ,cpu_to_node (i)); +GFP_KERNEL_ACCOUNT ,cpu_to_node (i));if (!scratch) {/* On failure, there's no need to undo previous * allocations: this means that some scratch maps have
@@ -1318,7 +1318,7 @@ static struct nft_pipapo_match *pipapo_clone(struct nft_pipapo_match *old)struct nft_pipapo_match *new;int i; - new =kmalloc (struct_size (new, f, old->field_count),GFP_KERNEL ); + new =kmalloc (struct_size (new, f, old->field_count),GFP_KERNEL_ACCOUNT );if (!new)return ERR_PTR (-ENOMEM );
@@ -1348,7 +1348,7 @@ static struct nft_pipapo_match *pipapo_clone(struct nft_pipapo_match *old) new_lt =kvzalloc (src->groups *NFT_PIPAPO_BUCKETS (src->bb) * src->bsize *sizeof (*dst->lt) +NFT_PIPAPO_ALIGN_HEADROOM , -GFP_KERNEL ); +GFP_KERNEL_ACCOUNT );if (!new_lt) goto out_lt;
@@ -1359,7 +1359,7 @@ static struct nft_pipapo_match *pipapo_clone(struct nft_pipapo_match *old) src->bsize *sizeof (*dst->lt) * src->groups * NFT_PIPAPO_BUCKETS(src->bb)); - dst->mt =kvmalloc (src->rules *sizeof (*src->mt),GFP_KERNEL ); + dst->mt =kvmalloc (src->rules *sizeof (*src->mt),GFP_KERNEL_ACCOUNT );if (!dst->mt) goto out_mt;
net/netfilter/nft_tunnel.c +3 -2
@@ -503,13 +503,14 @@ static int nft_tunnel_obj_init(const struct nft_ctx *ctx,return err; } - md =metadata_dst_alloc (priv->opts.len,METADATA_IP_TUNNEL ,GFP_KERNEL ); + md =metadata_dst_alloc (priv->opts.len,METADATA_IP_TUNNEL , +GFP_KERNEL_ACCOUNT );if (!md)return -ENOMEM ;memcpy (&md->u.tun_info, &info,sizeof (info));#ifdef CONFIG_DST_CACHE - err =dst_cache_init (&md->u.tun_info.dst_cache,GFP_KERNEL ); + err =dst_cache_init (&md->u.tun_info.dst_cache,GFP_KERNEL_ACCOUNT );if (err <0 ) {metadata_dst_free (md);return err;
Signature · 1 line
2.43.0
PATCH 2/2 netfilter: nft_set_pipapo: prevent overflow in lookup table allocation
From: Pablo Neira Ayuso <[email protected]>
[ Upstream commit 4c5c6aa9967dbe55bd017bb509885928d0f31206 ]
When calculating the lookup table size, ensure the following multiplication does not overflow:
- desc->field_len[] maximum value is U8_MAX multiplied by NFT_PIPAPO_GROUPS_PER_BYTE(f) that can be 2, worst case.
- NFT_PIPAPO_BUCKETS(f->bb) is 2^8, worst case.
- sizeof(unsigned long), from sizeof(*f->lt), lt in struct nft_pipapo_field.
Then, use check_mul_overflow() to multiply by bucket size and then use check_add_overflow() to the alignment for avx2 (if needed). Finally, add lt_size_check_overflow() helper and use it to consolidate this.
While at it, replace leftover allocation using the GFP_KERNEL to GFP_KERNEL_ACCOUNT for consistency, in pipapo_resize().
--- net/netfilter/nft_set_pipapo.c | 58 ++++++++++++++++++++++++++-------- 1 file changed, 44 insertions(+), 14 deletions(-)
net/netfilter/nft_set_pipapo.c +44 -14
@@ -610,6 +610,30 @@ static void *nft_pipapo_get(const struct net *net, const struct nft_set *set, nft_genmask_cur(net), get_jiffies_64()); } + +/** +* lt_calculate_size() - Get storage size for lookup table with overflow check +* @groups: Amount of bit groups +* @bb: Number of bits grouped together in lookup table buckets +* @bsize: Size of each bucket in lookup table, in longs +* +* Return: allocation size including alignment overhead, negative on overflow +*/ +static ssize_t lt_calculate_size (unsigned int groups,unsigned int bb, +unsigned int bsize) +{ +ssize_t ret = groups *NFT_PIPAPO_BUCKETS (bb) *sizeof (long ); + +if (check_mul_overflow (ret, bsize, &ret)) +return -1 ; +if (check_add_overflow (ret,NFT_PIPAPO_ALIGN_HEADROOM , &ret)) +return -1 ; +if (ret >INT_MAX ) +return -1 ; + +return ret; +} +/** * pipapo_resize() - Resize lookup or mapping table, or both * @f: Field containing lookup and mapping tables
@@ -628,6 +652,7 @@ static int pipapo_resize(struct nft_pipapo_field *f, int old_rules, int rules)union nft_pipapo_map_bucket *new_mt, *old_mt = f->mt;size_t new_bucket_size, copy;int group, bucket; +ssize_t lt_size; new_bucket_size =DIV_ROUND_UP (rules,BITS_PER_LONG );#ifdef NFT_PIPAPO_ALIGN
@@ -643,10 +668,11 @@ static int pipapo_resize(struct nft_pipapo_field *f, int old_rules, int rules)else copy = new_bucket_size; - new_lt =kvzalloc (f->groups *NFT_PIPAPO_BUCKETS (f->bb) * - new_bucket_size *sizeof (*new_lt) + -NFT_PIPAPO_ALIGN_HEADROOM , -GFP_KERNEL ); + lt_size =lt_calculate_size (f->groups, f->bb, new_bucket_size); +if (lt_size <0 ) +return -ENOMEM ; + + new_lt =kvzalloc (lt_size,GFP_KERNEL_ACCOUNT );if (!new_lt)return -ENOMEM ;
@@ -840,7 +866,7 @@ static void pipapo_lt_bits_adjust(struct nft_pipapo_field *f) {unsigned long *new_lt;int groups, bb; -size_t lt_size; +ssize_t lt_size; lt_size = f->groups *NFT_PIPAPO_BUCKETS (f->bb) * f->bsize *sizeof (*f->lt);
@@ -850,15 +876,17 @@ static void pipapo_lt_bits_adjust(struct nft_pipapo_field *f) groups = f->groups *2 ; bb =NFT_PIPAPO_GROUP_BITS_LARGE_SET ; - lt_size = groups *NFT_PIPAPO_BUCKETS (bb) * f->bsize * -sizeof (*f->lt); + lt_size =lt_calculate_size (groups, bb, f->bsize); +if (lt_size <0 ) +return ; }else if (f->bb ==NFT_PIPAPO_GROUP_BITS_LARGE_SET && lt_size <NFT_PIPAPO_LT_SIZE_LOW ) { groups = f->groups /2 ; bb =NFT_PIPAPO_GROUP_BITS_SMALL_SET ; - lt_size = groups *NFT_PIPAPO_BUCKETS (bb) * f->bsize * -sizeof (*f->lt); + lt_size =lt_calculate_size (groups, bb, f->bsize); +if (lt_size <0 ) +return ;/* Don't increase group width if the resulting lookup table size * would exceed the upper size threshold for a "small" set.
@@ -869,7 +897,7 @@ static void pipapo_lt_bits_adjust(struct nft_pipapo_field *f)return ; } - new_lt =kvzalloc (lt_size +NFT_PIPAPO_ALIGN_HEADROOM ,GFP_KERNEL_ACCOUNT ); + new_lt =kvzalloc (lt_size,GFP_KERNEL_ACCOUNT );if (!new_lt)return ;
@@ -1342,13 +1370,15 @@ static struct nft_pipapo_match *pipapo_clone(struct nft_pipapo_match *old)for (i =0 ; i < old->field_count; i++) {unsigned long *new_lt; +ssize_t lt_size;memcpy (dst, src, offsetof(struct nft_pipapo_field , lt)); - new_lt =kvzalloc (src->groups *NFT_PIPAPO_BUCKETS (src->bb) * - src->bsize *sizeof (*dst->lt) + -NFT_PIPAPO_ALIGN_HEADROOM , -GFP_KERNEL_ACCOUNT ); + lt_size =lt_calculate_size (src->groups, src->bb, src->bsize); +if (lt_size <0 ) + goto out_lt; + + new_lt =kvzalloc (lt_size,GFP_KERNEL_ACCOUNT );if (!new_lt) goto out_lt;
Signature · 1 line
2.43.0