[rh7,2/3] ms/netfilter: nf_tables: fix use-after-free when deleting compat expressions

Submitted by Konstantin Khorenko on Nov. 25, 2019, 7:58 a.m.

Details

Message ID 20191125075827.1216-2-khorenko@virtuozzo.com
State New
Series "Series without cover letter"
Headers show

Commit Message

Konstantin Khorenko Nov. 25, 2019, 7:58 a.m.
From: Florian Westphal <fw@strlen.de>

nft_compat ops do not have static storage duration, unlike all other
expressions.

When nf_tables_expr_destroy() returns, expr->ops might have been
free'd already, so we need to store next address before calling
expression destructor.

For same reason, we can't deref match pointer after nft_xt_put().

This can be easily reproduced by adding msleep() before
nft_match_destroy() returns.

Fixes: 0ca743a55991 ("netfilter: nf_tables: add compatibility layer for x_tables")
Reported-by: Pablo Neira Ayuso <pablo@netfilter.org>
Signed-off-by: Florian Westphal <fw@strlen.de>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>

Backported in the scope of https://jira.sw.ru/browse/PSBM-99656

(cherry picked from commit 29e3880109e357fdc607b4393f8308cef6af9413)
Signed-off-by: Konstantin Khorenko <khorenko@virtuozzo.com>
---
 net/netfilter/nf_tables_api.c | 5 +++--
 net/netfilter/nft_compat.c    | 3 ++-
 2 files changed, 5 insertions(+), 3 deletions(-)

Patch hide | download patch | download mbox

diff --git a/net/netfilter/nf_tables_api.c b/net/netfilter/nf_tables_api.c
index 1e75d13abab6..613f67f0e8d3 100644
--- a/net/netfilter/nf_tables_api.c
+++ b/net/netfilter/nf_tables_api.c
@@ -2006,7 +2006,7 @@  static int nf_tables_getrule(struct sock *nlsk, struct sk_buff *skb,
 static void nf_tables_rule_destroy(const struct nft_ctx *ctx,
 				   struct nft_rule *rule)
 {
-	struct nft_expr *expr;
+	struct nft_expr *expr, *next;
 
 	/*
 	 * Careful: some expressions might not be initialized in case this
@@ -2014,8 +2014,9 @@  static void nf_tables_rule_destroy(const struct nft_ctx *ctx,
 	 */
 	expr = nft_expr_first(rule);
 	while (expr != nft_expr_last(rule) && expr->ops) {
+		next = nft_expr_next(expr);
 		nf_tables_expr_destroy(ctx, expr);
-		expr = nft_expr_next(expr);
+		expr = next;
 	}
 	kfree(rule);
 }
diff --git a/net/netfilter/nft_compat.c b/net/netfilter/nft_compat.c
index 2efc6a925dc2..be17f603b90d 100644
--- a/net/netfilter/nft_compat.c
+++ b/net/netfilter/nft_compat.c
@@ -448,6 +448,7 @@  nft_match_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr)
 {
 	struct xt_match *match = expr->ops->data;
 	void *info = nft_expr_priv(expr);
+	struct module *me = match->me;
 	struct xt_mtdtor_param par;
 
 	par.net = ctx->net;
@@ -458,7 +459,7 @@  nft_match_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr)
 		par.match->destroy(&par);
 
 	nft_xt_put(container_of(expr->ops, struct nft_xt, ops));
-	module_put(match->me);
+	module_put(me);
 }
 
 static int nft_match_dump(struct sk_buff *skb, const struct nft_expr *expr)