netfilter: nft_ct: protect nft_ct_pcpu_template_refcnt with mutex
authorPavel Skripkin <paskripkin@gmail.com>
Tue, 10 Aug 2021 12:59:20 +0000 (15:59 +0300)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Wed, 22 Sep 2021 10:28:03 +0000 (12:28 +0200)
[ Upstream commit e3245a7b7b34bd2e97f744fd79463add6e9d41f4 ]

Syzbot hit use-after-free in nf_tables_dump_sets. The problem was in
missing lock protection for nft_ct_pcpu_template_refcnt.

Before commit f102d66b335a ("netfilter: nf_tables: use dedicated
mutex to guard transactions") all transactions were serialized by global
mutex, but then global mutex was changed to local per netnamespace
commit_mutex.

This change causes use-after-free bug, when 2 netnamespaces concurently
changing nft_ct_pcpu_template_refcnt without proper locking. Fix it by
adding nft_ct_pcpu_mutex and protect all nft_ct_pcpu_template_refcnt
changes with it.

Fixes: f102d66b335a ("netfilter: nf_tables: use dedicated mutex to guard transactions")
Reported-and-tested-by: syzbot+649e339fa6658ee623d3@syzkaller.appspotmail.com
Signed-off-by: Pavel Skripkin <paskripkin@gmail.com>
Acked-by: Florian Westphal <fw@strlen.de>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
Signed-off-by: Sasha Levin <sashal@kernel.org>
net/netfilter/nft_ct.c

index 7af822a02ce9823460e9abc09873f71d0cf7093a..7fcb73ac2e6ed26129c44cee6f5ea7a8ddb6f89d 100644 (file)
@@ -41,6 +41,7 @@ struct nft_ct_helper_obj  {
 #ifdef CONFIG_NF_CONNTRACK_ZONES
 static DEFINE_PER_CPU(struct nf_conn *, nft_ct_pcpu_template);
 static unsigned int nft_ct_pcpu_template_refcnt __read_mostly;
+static DEFINE_MUTEX(nft_ct_pcpu_mutex);
 #endif
 
 static u64 nft_ct_get_eval_counter(const struct nf_conn_counter *c,
@@ -526,8 +527,10 @@ static void __nft_ct_set_destroy(const struct nft_ctx *ctx, struct nft_ct *priv)
 #endif
 #ifdef CONFIG_NF_CONNTRACK_ZONES
        case NFT_CT_ZONE:
+               mutex_lock(&nft_ct_pcpu_mutex);
                if (--nft_ct_pcpu_template_refcnt == 0)
                        nft_ct_tmpl_put_pcpu();
+               mutex_unlock(&nft_ct_pcpu_mutex);
                break;
 #endif
        default:
@@ -565,9 +568,13 @@ static int nft_ct_set_init(const struct nft_ctx *ctx,
 #endif
 #ifdef CONFIG_NF_CONNTRACK_ZONES
        case NFT_CT_ZONE:
-               if (!nft_ct_tmpl_alloc_pcpu())
+               mutex_lock(&nft_ct_pcpu_mutex);
+               if (!nft_ct_tmpl_alloc_pcpu()) {
+                       mutex_unlock(&nft_ct_pcpu_mutex);
                        return -ENOMEM;
+               }
                nft_ct_pcpu_template_refcnt++;
+               mutex_unlock(&nft_ct_pcpu_mutex);
                len = sizeof(u16);
                break;
 #endif