Received: by 2002:a05:6a11:4021:0:0:0:0 with SMTP id ky33csp2352916pxb; Mon, 20 Sep 2021 19:50:32 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwbITyXaykFhC5vVCpXhl3mLhM8n3wSZHDhRyeqRNDDfjUMBz80lmN3PVTIr/zmhoEKZAc4 X-Received: by 2002:a17:906:9801:: with SMTP id lm1mr31852584ejb.432.1632192632581; Mon, 20 Sep 2021 19:50:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1632192632; cv=none; d=google.com; s=arc-20160816; b=B4DyC9iCOyOyaSfvGbnMBdJ4cRkfnKCNLJQAyhqLZ+4LsQcIjTZF5llfXCMU6MxGmp Xuc304lu8H+5VTMuMnAEKXlKbGkoBQzyWhq4tdBE2Daq4eL4XQ87AYRVcKZ98+fDu5ND H3COZ5K1O2+VCBVRfJYWRpJb0LJMWtLxTA2c3A8QfCT6woNOgSehBOHlelsRht+mcMbl l3ZoMD3nU6Twc14arzxqzfxiD8ZKbi1O5NqCiy7GjPOI9edC9OD64vZ+9+kNBTVCpSH/ QRS40LJHqF6MxNjHEzpvdp4QtSbqNnrq5gAqzzRcqsqDqXypslatLkejpw5+Z4N7VvRe 5Ikg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=OHPjxHlMXqJDfcaLBLBxrhAYvJ83C1LNOJs6Uv59ZaA=; b=p1SGAnsOvQRiGSYaa0XtHrsON3IQSg5VWaz1keFtHA5F4CmAk35iQaM1+bf06/oQAK XUj+im0IyQmdeYfcwpKvywh45qqR/+ZijU+3M7u088UR757RZkmYT2yxDUaXbW4R35t8 d5Q8BP13BFjNaDubuOvAH9bpPJ4t7eXCHmJu/vT9JSHZiBraTEV9MCeoUfJKdXfn0zfp nXtmUDEMwnEmmxQJHQYmluaKnrvZM2BMXq6BbVlK/azCaGkWsAVgaOs/fStj/zijWLFL ukSJMmSKfEKUbn+yWDggdz+QvqFleLfpdqXb/f9TWzeCAU8Ou+nU9BuSKLxdeYsqBX0B 0i3A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=AAdJv1mi; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id ca15si18274799edb.208.2021.09.20.19.50.09; Mon, 20 Sep 2021 19:50:32 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=AAdJv1mi; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1380994AbhITSfJ (ORCPT + 99 others); Mon, 20 Sep 2021 14:35:09 -0400 Received: from mail.kernel.org ([198.145.29.99]:49716 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1379872AbhITSbA (ORCPT ); Mon, 20 Sep 2021 14:31:00 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 94447632F5; Mon, 20 Sep 2021 17:27:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1632158852; bh=6you6jM5IneUAQtuSVKKOeeEHM1yVNaNq0/kkbNxMDQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=AAdJv1mirXJfsEj656s1BEmICmPNGQ6uzZ2tRauEtuxxtDW59TRuCcB4tYqi5Hip6 H2G1U2pTulg4PF3kVVrzLHhRfuvmBHQJSzdfgExVqRE9+/8cxFhlJ6yHQO3r4WRgkR +/E4NlprtmznY2aE1ytPOPUp0UyvwJPLiClRrNd4= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Pavel Skripkin , Florian Westphal , Pablo Neira Ayuso , Sasha Levin , syzbot+649e339fa6658ee623d3@syzkaller.appspotmail.com Subject: [PATCH 5.10 085/122] netfilter: nft_ct: protect nft_ct_pcpu_template_refcnt with mutex Date: Mon, 20 Sep 2021 18:44:17 +0200 Message-Id: <20210920163918.576789653@linuxfoundation.org> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20210920163915.757887582@linuxfoundation.org> References: <20210920163915.757887582@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Pavel Skripkin [ 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 Acked-by: Florian Westphal Signed-off-by: Pablo Neira Ayuso Signed-off-by: Sasha Levin --- net/netfilter/nft_ct.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/net/netfilter/nft_ct.c b/net/netfilter/nft_ct.c index 7af822a02ce9..7fcb73ac2e6e 100644 --- a/net/netfilter/nft_ct.c +++ b/net/netfilter/nft_ct.c @@ -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 -- 2.30.2