Skip to content

Commit f36736f

Browse files
ummakynesgregkh
authored andcommitted
netfilter: nf_tables: disallow non-stateful expression in sets earlier
commit 5207780 upstream. Since 3e135cd ("netfilter: nft_dynset: dynamic stateful expression instantiation"), it is possible to attach stateful expressions to set elements. cd5125d ("netfilter: nf_tables: split set destruction in deactivate and destroy phase") introduces conditional destruction on the object to accomodate transaction semantics. nft_expr_init() calls expr->ops->init() first, then check for NFT_STATEFUL_EXPR, this stills allows to initialize a non-stateful lookup expressions which points to a set, which might lead to UAF since the set is not properly detached from the set->binding for this case. Anyway, this combination is non-sense from nf_tables perspective. This patch fixes this problem by checking for NFT_STATEFUL_EXPR before expr->ops->init() is called. The reporter provides a KASAN splat and a poc reproducer (similar to those autogenerated by syzbot to report use-after-free errors). It is unknown to me if they are using syzbot or if they use similar automated tool to locate the bug that they are reporting. For the record, this is the KASAN splat. [ 85.431824] ================================================================== [ 85.432901] BUG: KASAN: use-after-free in nf_tables_bind_set+0x81b/0xa20 [ 85.433825] Write of size 8 at addr ffff8880286f0e98 by task poc/776 [ 85.434756] [ 85.434999] CPU: 1 PID: 776 Comm: poc Tainted: G W 5.18.0+ raspberrypi#2 [ 85.436023] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.14.0-2 04/01/2014 Fixes: 0b2d8a7 ("netfilter: nf_tables: add helper functions for expression handling") Reported-and-tested-by: Aaron Adams <[email protected]> Signed-off-by: Pablo Neira Ayuso <[email protected]> [Ajay: Regenerated the patch for v5.4.y] Signed-off-by: Ajay Kaher <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 28a8060 commit f36736f

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

net/netfilter/nf_tables_api.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2267,27 +2267,31 @@ struct nft_expr *nft_expr_init(const struct nft_ctx *ctx,
22672267

22682268
err = nf_tables_expr_parse(ctx, nla, &info);
22692269
if (err < 0)
2270-
goto err1;
2270+
goto err_expr_parse;
2271+
2272+
err = -EOPNOTSUPP;
2273+
if (!(info.ops->type->flags & NFT_EXPR_STATEFUL))
2274+
goto err_expr_stateful;
22712275

22722276
err = -ENOMEM;
22732277
expr = kzalloc(info.ops->size, GFP_KERNEL);
22742278
if (expr == NULL)
2275-
goto err2;
2279+
goto err_expr_stateful;
22762280

22772281
err = nf_tables_newexpr(ctx, &info, expr);
22782282
if (err < 0)
2279-
goto err3;
2283+
goto err_expr_new;
22802284

22812285
return expr;
2282-
err3:
2286+
err_expr_new:
22832287
kfree(expr);
2284-
err2:
2288+
err_expr_stateful:
22852289
owner = info.ops->type->owner;
22862290
if (info.ops->type->release_ops)
22872291
info.ops->type->release_ops(info.ops);
22882292

22892293
module_put(owner);
2290-
err1:
2294+
err_expr_parse:
22912295
return ERR_PTR(err);
22922296
}
22932297

net/netfilter/nft_dynset.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,9 +204,6 @@ static int nft_dynset_init(const struct nft_ctx *ctx,
204204
return PTR_ERR(priv->expr);
205205

206206
err = -EOPNOTSUPP;
207-
if (!(priv->expr->ops->type->flags & NFT_EXPR_STATEFUL))
208-
goto err1;
209-
210207
if (priv->expr->ops->type->flags & NFT_EXPR_GC) {
211208
if (set->flags & NFT_SET_TIMEOUT)
212209
goto err1;

0 commit comments

Comments
 (0)