Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp828229rwb; Mon, 26 Sep 2022 06:25:35 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7xJ8zjlnyMlbgmgSRQHr8ZILg1rg/8UymvzfSKEM/qDBo8RDqSQit/1uma/8a5u27U02I4 X-Received: by 2002:a05:6402:3596:b0:450:c4d9:a04b with SMTP id y22-20020a056402359600b00450c4d9a04bmr22570704edc.218.1664198734786; Mon, 26 Sep 2022 06:25:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1664198734; cv=none; d=google.com; s=arc-20160816; b=x8jLqn27X2NxEF5ikKIiNP2X7V0RstINHIDArJXJBOxIhJwjSLEaL297nnT3yJR5iQ RoDMTpg7z3UwI5RnNnWrz+QrFPC7+QodE8w9DK8+bhjQpUVdTff3YFTMyNIaRRHYF1vf sfk1Sy15y9kIeosDFga6R9D9KGlp1mKnfSxkWGcdM4hcWwCvs+ReEt1ZkYnaAvQZhkAj qrA1RXEt6k3XgWZUOo1QUtkR0EGUMFjDmZEgGWjRYXCvat2it8KWa6w2z6RHBpx/VM2D B91jGyTXtxWAHy8n1+UUT3TrjbrxpXQRbgc0nDXo1e0Q/dS/+uWeNsx+7oQCrr0L8S7a UfGg== 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=Ko1eVVcX3oHCbdChuSayEhGzV5yADeFhF1ye9o+2Z8s=; b=vW1dMCm2eU6ZXF3w6r9mzrvwAacYi6wO5vT0xd1iEgUg5LwQulT5FiGgiOqzVTSMOU e17QZp02GiMHmn+gqBuntFsLFxM+h3VOlRlP49pgM4TwTOmU+whzPMcD7+Fvrx5xDZL3 gOhmTL6jp9wecrmHnsNzZcrE2BagwbABeG8x9mb+CC3W1RY2heLRgL7thqWh+y+Rw/RU aDK3YawQW8prc8SQXBE4WeOVhu6U1UfLxR9ojLlS0VVDri6WHdgI/zWoTdDBHgQAFKu2 maCmVjse++kUR19h2YnxHObFMkzB2HtH1ExlpbBx9m6NF3hxihRiE6VHAn1Rkz77Gn4N 1SgA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=r5FIVtzQ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id go16-20020a1709070d9000b007804e2f5bf5si22693202ejc.193.2022.09.26.06.25.08; Mon, 26 Sep 2022 06:25:34 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=r5FIVtzQ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 S239433AbiIZL6z (ORCPT + 99 others); Mon, 26 Sep 2022 07:58:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52404 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238603AbiIZLxb (ORCPT ); Mon, 26 Sep 2022 07:53:31 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7AA5E1EECA; Mon, 26 Sep 2022 03:49:39 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 34E2660A52; Mon, 26 Sep 2022 10:49:39 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4334CC433C1; Mon, 26 Sep 2022 10:49:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1664189378; bh=IJyVcokS76eYcgjYU/QcDdw9VqFByW+ywwfO1tCMjz8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=r5FIVtzQXO+ggMRI5fPN4b/JYmp9d7F2anv8icohc3JDLWE+sAnwp2llsKa5k/woN SwGEK+zlTIF8z/hwg8RMSmOPS67x/r21nA4Z5mUSBEzJLKPL0NdVBxEv8tkDUtxEXC m0sO65zflDf2JneYe+szXMFx1izcRtRv3TEG+GhQ= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, syzbot , Tetsuo Handa , Florian Westphal , Sasha Levin Subject: [PATCH 5.19 137/207] netfilter: nf_tables: fix nft_counters_enabled underflow at nf_tables_addchain() Date: Mon, 26 Sep 2022 12:12:06 +0200 Message-Id: <20220926100812.649459985@linuxfoundation.org> X-Mailer: git-send-email 2.37.3 In-Reply-To: <20220926100806.522017616@linuxfoundation.org> References: <20220926100806.522017616@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.2 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Tetsuo Handa [ Upstream commit 921ebde3c0d22c8cba74ce8eb3cc4626abff1ccd ] syzbot is reporting underflow of nft_counters_enabled counter at nf_tables_addchain() [1], for commit 43eb8949cfdffa76 ("netfilter: nf_tables: do not leave chain stats enabled on error") missed that nf_tables_chain_destroy() after nft_basechain_init() in the error path of nf_tables_addchain() decrements the counter because nft_basechain_init() makes nft_is_base_chain() return true by setting NFT_CHAIN_BASE flag. Increment the counter immediately after returning from nft_basechain_init(). Link: https://syzkaller.appspot.com/bug?extid=b5d82a651b71cd8a75ab [1] Reported-by: syzbot Signed-off-by: Tetsuo Handa Tested-by: syzbot Fixes: 43eb8949cfdffa76 ("netfilter: nf_tables: do not leave chain stats enabled on error") Signed-off-by: Florian Westphal Signed-off-by: Sasha Levin --- net/netfilter/nf_tables_api.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/net/netfilter/nf_tables_api.c b/net/netfilter/nf_tables_api.c index 848cc81d6992..3aaf36df69d4 100644 --- a/net/netfilter/nf_tables_api.c +++ b/net/netfilter/nf_tables_api.c @@ -2197,7 +2197,6 @@ static int nf_tables_addchain(struct nft_ctx *ctx, u8 family, u8 genmask, struct netlink_ext_ack *extack) { const struct nlattr * const *nla = ctx->nla; - struct nft_stats __percpu *stats = NULL; struct nft_table *table = ctx->table; struct nft_base_chain *basechain; struct net *net = ctx->net; @@ -2212,6 +2211,7 @@ static int nf_tables_addchain(struct nft_ctx *ctx, u8 family, u8 genmask, return -EOVERFLOW; if (nla[NFTA_CHAIN_HOOK]) { + struct nft_stats __percpu *stats = NULL; struct nft_chain_hook hook; if (flags & NFT_CHAIN_BINDING) @@ -2245,6 +2245,8 @@ static int nf_tables_addchain(struct nft_ctx *ctx, u8 family, u8 genmask, kfree(basechain); return err; } + if (stats) + static_branch_inc(&nft_counters_enabled); } else { if (flags & NFT_CHAIN_BASE) return -EINVAL; @@ -2319,9 +2321,6 @@ static int nf_tables_addchain(struct nft_ctx *ctx, u8 family, u8 genmask, goto err_unregister_hook; } - if (stats) - static_branch_inc(&nft_counters_enabled); - table->use++; return 0; -- 2.35.1