1 From 998c1eb43f8ea522a08fea1e866dfa68208c5a7c Mon Sep 17 00:00:00 2001
2 From: Florian Westphal <fw@strlen.de>
3 Date: Tue, 5 Feb 2019 12:16:18 +0100
4 Subject: netfilter: nft_compat: don't use refcount_inc on newly allocated
7 [ Upstream commit 947e492c0fc2132ae5fca081a9c2952ccaab0404 ]
9 When I moved the refcount to refcount_t type I missed the fact that
10 refcount_inc() will result in use-after-free warning with
11 CONFIG_REFCOUNT_FULL=y builds.
13 The correct fix would be to init the reference count to 1 at allocation
14 time, but, unfortunately we cannot do this, as we can't undo that
15 in case something else fails later in the batch.
17 So only solution I see is to special-case the 'new entry' condition
18 and replace refcount_inc() with a "delayed" refcount_set(1) in this case,
21 The .activate callback can be removed to simplify things, we only
22 need to make sure that deactivate() decrements/unlinks the entry
23 from the list at end of transaction phase (commit or abort).
25 Fixes: 12c44aba6618 ("netfilter: nft_compat: use refcnt_t type for nft_xt reference count")
26 Reported-by: Jordan Glover <Golden_Miller83@protonmail.ch>
27 Signed-off-by: Florian Westphal <fw@strlen.de>
28 Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
29 Signed-off-by: Sasha Levin <sashal@kernel.org>
31 net/netfilter/nft_compat.c | 62 ++++++++++++++------------------------
32 1 file changed, 23 insertions(+), 39 deletions(-)
34 diff --git a/net/netfilter/nft_compat.c b/net/netfilter/nft_compat.c
35 index 5fd94f7fdb94..0a4bad55a8aa 100644
36 --- a/net/netfilter/nft_compat.c
37 +++ b/net/netfilter/nft_compat.c
38 @@ -61,6 +61,21 @@ static struct nft_compat_net *nft_compat_pernet(struct net *net)
39 return net_generic(net, nft_compat_net_id);
42 +static void nft_xt_get(struct nft_xt *xt)
44 + /* refcount_inc() warns on 0 -> 1 transition, but we can't
45 + * init the reference count to 1 in .select_ops -- we can't
46 + * undo such an increase when another expression inside the same
47 + * rule fails afterwards.
49 + if (xt->listcnt == 0)
50 + refcount_set(&xt->refcnt, 1);
52 + refcount_inc(&xt->refcnt);
57 static bool nft_xt_put(struct nft_xt *xt)
59 if (refcount_dec_and_test(&xt->refcnt)) {
60 @@ -291,7 +306,7 @@ nft_target_init(const struct nft_ctx *ctx, const struct nft_expr *expr,
63 nft_xt = container_of(expr->ops, struct nft_xt, ops);
64 - refcount_inc(&nft_xt->refcnt);
69 @@ -505,7 +520,7 @@ __nft_match_init(const struct nft_ctx *ctx, const struct nft_expr *expr,
72 nft_xt = container_of(expr->ops, struct nft_xt, ops);
73 - refcount_inc(&nft_xt->refcnt);
78 @@ -559,45 +574,16 @@ nft_match_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr)
79 __nft_match_destroy(ctx, expr, nft_expr_priv(expr));
82 -static void nft_compat_activate(const struct nft_ctx *ctx,
83 - const struct nft_expr *expr,
84 - struct list_head *h)
86 - struct nft_xt *xt = container_of(expr->ops, struct nft_xt, ops);
88 - if (xt->listcnt == 0)
89 - list_add(&xt->head, h);
94 -static void nft_compat_activate_mt(const struct nft_ctx *ctx,
95 - const struct nft_expr *expr)
97 - struct nft_compat_net *cn = nft_compat_pernet(ctx->net);
99 - nft_compat_activate(ctx, expr, &cn->nft_match_list);
102 -static void nft_compat_activate_tg(const struct nft_ctx *ctx,
103 - const struct nft_expr *expr)
105 - struct nft_compat_net *cn = nft_compat_pernet(ctx->net);
107 - nft_compat_activate(ctx, expr, &cn->nft_target_list);
110 static void nft_compat_deactivate(const struct nft_ctx *ctx,
111 const struct nft_expr *expr,
112 enum nft_trans_phase phase)
114 struct nft_xt *xt = container_of(expr->ops, struct nft_xt, ops);
116 - if (phase == NFT_TRANS_COMMIT)
119 - if (--xt->listcnt == 0)
120 - list_del_init(&xt->head);
121 + if (phase == NFT_TRANS_ABORT || phase == NFT_TRANS_COMMIT) {
122 + if (--xt->listcnt == 0)
123 + list_del_init(&xt->head);
128 @@ -853,7 +839,6 @@ nft_match_select_ops(const struct nft_ctx *ctx,
129 nft_match->ops.eval = nft_match_eval;
130 nft_match->ops.init = nft_match_init;
131 nft_match->ops.destroy = nft_match_destroy;
132 - nft_match->ops.activate = nft_compat_activate_mt;
133 nft_match->ops.deactivate = nft_compat_deactivate;
134 nft_match->ops.dump = nft_match_dump;
135 nft_match->ops.validate = nft_match_validate;
136 @@ -871,7 +856,7 @@ nft_match_select_ops(const struct nft_ctx *ctx,
138 nft_match->ops.size = matchsize;
140 - nft_match->listcnt = 1;
141 + nft_match->listcnt = 0;
142 list_add(&nft_match->head, &cn->nft_match_list);
144 return &nft_match->ops;
145 @@ -958,7 +943,6 @@ nft_target_select_ops(const struct nft_ctx *ctx,
146 nft_target->ops.size = NFT_EXPR_SIZE(XT_ALIGN(target->targetsize));
147 nft_target->ops.init = nft_target_init;
148 nft_target->ops.destroy = nft_target_destroy;
149 - nft_target->ops.activate = nft_compat_activate_tg;
150 nft_target->ops.deactivate = nft_compat_deactivate;
151 nft_target->ops.dump = nft_target_dump;
152 nft_target->ops.validate = nft_target_validate;
153 @@ -969,7 +953,7 @@ nft_target_select_ops(const struct nft_ctx *ctx,
155 nft_target->ops.eval = nft_target_eval_xt;
157 - nft_target->listcnt = 1;
158 + nft_target->listcnt = 0;
159 list_add(&nft_target->head, &cn->nft_target_list);
161 return &nft_target->ops;