Skip to contentSkip to conversation

nf_tables: nft_dynset: fix possible stateful expression memleak in error path

PATCH Superseded Pablo Neira Ayuso 2 participants 4 messages Mar 12

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Pablo Neira Ayuso

author maintainer
#0 lore raw

PATCH 1/1 nf_tables: nft_dynset: fix possible stateful expression memleak in error path

If cloning the second stateful expression in the element via GFP_ATOMIC fails, then the first stateful expression remains in place without being released.

  unreferenced object (percpu) 0x607b97e9cab8 (size 16):
    comm "softirq", pid 0, jiffies 4294931867 hex dump (first 16 bytes on cpu 3):
      00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 backtrace (crc 0):

       pcpu_alloc_noprof+0x453/0xd80
       nft_counter_clone+0x9c/0x190 [nf_tables]
       nft_expr_clone+0x8f/0x1b0 [nf_tables]
       nft_dynset_new+0x2cb/0x5f0 [nf_tables]
       nft_rhash_update+0x236/0x11c0 [nf_tables]
       nft_dynset_eval+0x11f/0x670 [nf_tables]
       nft_do_chain+0x253/0x1700 [nf_tables]
       nft_do_chain_ipv4+0x18d/0x270 [nf_tables]
       nf_hook_slow+0xaa/0x1e0
       ip_local_deliver+0x209/0x330

Pass NULL to nft_set_elem_expr_destroy() given stateful expressions do not require context at this stage.

Fixes 563125a73ac3 ("netfilter: nftables: generalize set extension to support for several expressions")

---
Compile-tested only at this stage.

 include/net/netfilter/nf_tables.h | 2 ++
 net/netfilter/nf_tables_api.c     | 4 ++--
 net/netfilter/nft_dynset.c        | 7 ++++++-
 3 files changed, 10 insertions(+), 3 deletions(-)
include/net/netfilter/nf_tables.h +2 -0
index ea6f29ad7888..3c8a60ec1cc4 100644
@@ -878,6 +878,8 @@ struct nft_elem_priv *nft_set_elem_init(const struct nft_set *set,
 					u64 timeout, u64 expiration, gfp_t gfp);
 int nft_set_elem_expr_clone(const struct nft_ctx *ctx, struct nft_set *set,
 			    struct nft_expr *expr_array[]);
+void nft_set_elem_expr_destroy(const struct nft_ctx *ctx,
+			       struct nft_set_elem_expr *elem_expr);
 void nft_set_elem_destroy(const struct nft_set *set,
 			  const struct nft_elem_priv *elem_priv,
 			  bool destroy_expr);
net/netfilter/nf_tables_api.c +2 -2
index 15801a9a099e..2f19c155069e 100644
@@ -6747,8 +6747,8 @@ static void __nft_set_elem_expr_destroy(const struct nft_ctx *ctx,
 	}
 }
 
-static void nft_set_elem_expr_destroy(const struct nft_ctx *ctx,
-				      struct nft_set_elem_expr *elem_expr)
+void nft_set_elem_expr_destroy(const struct nft_ctx *ctx,
+			       struct nft_set_elem_expr *elem_expr)
 {
 	struct nft_expr *expr;
 	u32 size;
net/netfilter/nft_dynset.c +6 -1
index 7807d8129664..31c5a5b52ce1 100644
@@ -36,12 +36,17 @@ static int nft_dynset_expr_setup(const struct nft_dynset *priv,
 	for (i = 0; i < priv->num_exprs; i++) {
 		expr = nft_setelem_expr_at(elem_expr, elem_expr->size);
 		if (nft_expr_clone(expr, priv->expr_array[i], GFP_ATOMIC) < 0)
-			return -1;
+			goto err_out;
 
 		elem_expr->size += priv->expr_array[i]->ops->size;
 	}
 
 	return 0;
+err_out:
+	/* Stateful expression do not need context, pass NULL. */
+	nft_set_elem_expr_destroy(NULL, elem_expr);
+
+	return -1;
 }
 
 struct nft_elem_priv *nft_dynset_new(struct nft_set *set,
Signature · 1 line
2.47.3
maintainer
#1 lore raw
Quoted 20 lines · Pablo Neira Ayuso

If cloning the second stateful expression in the element via GFP_ATOMIC fails, then the first stateful expression remains in place without being released.

  unreferenced object (percpu) 0x607b97e9cab8 (size 16):
    comm "softirq", pid 0, jiffies 4294931867 hex dump (first 16 bytes on cpu 3):
      00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 backtrace (crc 0):

       pcpu_alloc_noprof+0x453/0xd80
       nft_counter_clone+0x9c/0x190 [nf_tables]
       nft_expr_clone+0x8f/0x1b0 [nf_tables]
       nft_dynset_new+0x2cb/0x5f0 [nf_tables]
       nft_rhash_update+0x236/0x11c0 [nf_tables]
       nft_dynset_eval+0x11f/0x670 [nf_tables]
       nft_do_chain+0x253/0x1700 [nf_tables]
       nft_do_chain_ipv4+0x18d/0x270 [nf_tables]
       nf_hook_slow+0xaa/0x1e0
       ip_local_deliver+0x209/0x330
Quoted 2 lines

Pass NULL to nft_set_elem_expr_destroy() given stateful expressions do not require context at this stage.

static void nft_connlimit_do_destroy(const struct nft_ctx *ctx,
struct nft_connlimit *priv)

{
        nf_ct_netns_put(ctx->net, ctx->family);
        nf_conncount_cache_free(priv->list);
        kfree(priv->list);
}

I think minimal fake context could work though, the clone wasn't exposed to other cpus yet.

Other than this patch looks correct to me.

author maintainer
#2 lore raw
Quoted 32 lines · Florian Westphal
Quoted 20 lines · Pablo Neira Ayuso

If cloning the second stateful expression in the element via GFP_ATOMIC fails, then the first stateful expression remains in place without being released.

  unreferenced object (percpu) 0x607b97e9cab8 (size 16):
    comm "softirq", pid 0, jiffies 4294931867 hex dump (first 16 bytes on cpu 3):
      00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 backtrace (crc 0):

       pcpu_alloc_noprof+0x453/0xd80
       nft_counter_clone+0x9c/0x190 [nf_tables]
       nft_expr_clone+0x8f/0x1b0 [nf_tables]
       nft_dynset_new+0x2cb/0x5f0 [nf_tables]
       nft_rhash_update+0x236/0x11c0 [nf_tables]
       nft_dynset_eval+0x11f/0x670 [nf_tables]
       nft_do_chain+0x253/0x1700 [nf_tables]
       nft_do_chain_ipv4+0x18d/0x270 [nf_tables]
       nf_hook_slow+0xaa/0x1e0
       ip_local_deliver+0x209/0x330
Quoted 2 lines

Pass NULL to nft_set_elem_expr_destroy() given stateful expressions do not require context at this stage.

static void nft_connlimit_do_destroy(const struct nft_ctx *ctx,
struct nft_connlimit *priv)

{
        nf_ct_netns_put(ctx->net, ctx->family);
        nf_conncount_cache_free(priv->list);
        kfree(priv->list);
}
Quoted 4 lines

I think minimal fake context could work though, the clone wasn't exposed to other cpus yet.

Other than this patch looks correct to me.

It is following this path:

static void nft_connlimit_destroy_clone(const struct nft_ctx *ctx,
const struct nft_expr *expr)

{
        struct nft_connlimit *priv = nft_expr_priv(expr);
 
        nf_conncount_cache_free(priv->list);
        kfree(priv->list);
}

But I will post a v2 setting nft_ctx anyway.

maintainer
#3 lore raw
Quoted 40 lines · Pablo Neira Ayuso
Quoted 32 lines · Florian Westphal
Quoted 20 lines · Pablo Neira Ayuso

If cloning the second stateful expression in the element via GFP_ATOMIC fails, then the first stateful expression remains in place without being released.

  unreferenced object (percpu) 0x607b97e9cab8 (size 16):
    comm "softirq", pid 0, jiffies 4294931867 hex dump (first 16 bytes on cpu 3):
      00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 backtrace (crc 0):

       pcpu_alloc_noprof+0x453/0xd80
       nft_counter_clone+0x9c/0x190 [nf_tables]
       nft_expr_clone+0x8f/0x1b0 [nf_tables]
       nft_dynset_new+0x2cb/0x5f0 [nf_tables]
       nft_rhash_update+0x236/0x11c0 [nf_tables]
       nft_dynset_eval+0x11f/0x670 [nf_tables]
       nft_do_chain+0x253/0x1700 [nf_tables]
       nft_do_chain_ipv4+0x18d/0x270 [nf_tables]
       nf_hook_slow+0xaa/0x1e0
       ip_local_deliver+0x209/0x330
Quoted 2 lines

Pass NULL to nft_set_elem_expr_destroy() given stateful expressions do not require context at this stage.

static void nft_connlimit_do_destroy(const struct nft_ctx *ctx,
struct nft_connlimit *priv)

{
        nf_ct_netns_put(ctx->net, ctx->family);
        nf_conncount_cache_free(priv->list);
        kfree(priv->list);
}
Quoted 4 lines

I think minimal fake context could work though, the clone wasn't exposed to other cpus yet.

Other than this patch looks correct to me.

It is following this path:

Quoted 8 lines

static void nft_connlimit_destroy_clone(const struct nft_ctx *ctx,
const struct nft_expr *expr)

{
        struct nft_connlimit *priv = nft_expr_priv(expr);
 
        nf_conncount_cache_free(priv->list);
        kfree(priv->list);
}

Oh, I see -- you are right, this follows the destroy_expr special case.

Quoted 1 line

But I will post a v2 setting nft_ctx anyway.

Thanks, I find it hard to assert correctness otherwise.