Received: by 2002:a05:6a10:9afc:0:0:0:0 with SMTP id t28csp3117533pxm; Mon, 28 Feb 2022 12:23:22 -0800 (PST) X-Google-Smtp-Source: ABdhPJyZYChIidEwNtEmWpNACORimJlee/wt0fwihqmTH16mEsevs9Rjudz/rdgI/HBzt/SwCl6f X-Received: by 2002:a63:790d:0:b0:373:cc0b:5b6a with SMTP id u13-20020a63790d000000b00373cc0b5b6amr18800350pgc.119.1646079801983; Mon, 28 Feb 2022 12:23:21 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1646079801; cv=none; d=google.com; s=arc-20160816; b=sTw7+ObiBGqSFhN7yiL6TLqzrEBjf4NYEtzuHN1xLXPAZmhK2k8fUG63Z/R+OAXplM Dm/tj/LYKRnYYn769b8ATm70iwn/SrmYsCiGhvq9VpDh0BxKGWbPwe7yadVshR4JWbPU REl5uUQ28ufJhQovoeQ1yHD3bHMGNxcLv2WzTsbIwh2c5quI8fYOIilAmdKLxQTLOeU4 HzYl1Ji9xJrgA/oLx+LKRAf+oJ1iRFQ9OTfAJsVE4FOi0qP7uWZOJ7cvmFTj/rdsh9Qv MiLXjKb0bXPOg0ufwP3/Ay1+TuUzKGyLIC/HZ0C0sAog3pMwD7EVdgYTpCjYYt13gCof pKBQ== 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=dE3t8im3OicOzgRjb/57HmOk8IZE9Ok+G+U9rxM8tgI=; b=T/wy6wsdXG7HnLkGFFcUKqGFgtMcyl0mhmrhpIC4VuXD6sYVYwFk5G8Bwu1epERrDH sAF0AH4evYN688Udx9ZCHBpVmFy/tij07IH/zN0FB7gnlgWxJIGLWM680Er25T39xUbi SGbYpWfd3LpGCl5auaQbD0YA15On2j8fER7iIdtryk5LNYHy6DaDaz5qatnae/8Ms6qy /ksuELbPXKyCgX2tjd3gq0eNqy1Qilo0pD2DqtHawD/LjM4aDXnOnsyGWJZiYPkbgU8o uFv6yx8tWAivU4tGKev1SUgCI5ki+RMTkaOtvuz4AWCdQ8SgvQiZI3PKRHYedqolGQd4 JlCQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=Er3MjgJL; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 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 lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id z196-20020a6333cd000000b00373fe5555f2si9902451pgz.407.2022.02.28.12.23.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Feb 2022 12:23:21 -0800 (PST) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=Er3MjgJL; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 877CD1CF093; Mon, 28 Feb 2022 11:42:29 -0800 (PST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240353AbiB1RyO (ORCPT + 99 others); Mon, 28 Feb 2022 12:54:14 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55642 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238994AbiB1Rue (ORCPT ); Mon, 28 Feb 2022 12:50:34 -0500 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C5B9B8C7CE; Mon, 28 Feb 2022 09:39:08 -0800 (PST) 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 ams.source.kernel.org (Postfix) with ESMTPS id 77F5BB815A6; Mon, 28 Feb 2022 17:39:07 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id BAA96C340E7; Mon, 28 Feb 2022 17:39:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1646069946; bh=0leDAzX86teA78GwaoD+ubeZaW6X+jtdR+Zlfk2BI4o=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Er3MjgJLQIaqrzWl+ad/kIubJWe4FS7XvI5Y41dk5fZUiiQ7FGLFoYTEqfz1kNNZJ uqHVEMWBQrq8iaCX1PMx5q/SGfPB7eMMrCOpfTrPLhWJVF4/5gxwiUn2FdjcpmwMUx vGGsbT1KnRYLD+WlFl77prMgQ8seJ7ZleDzsAk74= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, syzbot+e918523f77e62790d6d9@syzkaller.appspotmail.com, Pablo Neira Ayuso Subject: [PATCH 5.15 069/139] netfilter: nf_tables: unregister flowtable hooks on netns exit Date: Mon, 28 Feb 2022 18:24:03 +0100 Message-Id: <20220228172354.970125703@linuxfoundation.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220228172347.614588246@linuxfoundation.org> References: <20220228172347.614588246@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=unavailable 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: Pablo Neira Ayuso commit 6069da443bf65f513bb507bb21e2f87cfb1ad0b6 upstream. Unregister flowtable hooks before they are releases via nf_tables_flowtable_destroy() otherwise hook core reports UAF. BUG: KASAN: use-after-free in nf_hook_entries_grow+0x5a7/0x700 net/netfilter/core.c:142 net/netfilter/core.c:142 Read of size 4 at addr ffff8880736f7438 by task syz-executor579/3666 CPU: 0 PID: 3666 Comm: syz-executor579 Not tainted 5.16.0-rc5-syzkaller #0 Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 01/01/2011 Call Trace: __dump_stack lib/dump_stack.c:88 [inline] __dump_stack lib/dump_stack.c:88 [inline] lib/dump_stack.c:106 dump_stack_lvl+0x1dc/0x2d8 lib/dump_stack.c:106 lib/dump_stack.c:106 print_address_description+0x65/0x380 mm/kasan/report.c:247 mm/kasan/report.c:247 __kasan_report mm/kasan/report.c:433 [inline] __kasan_report mm/kasan/report.c:433 [inline] mm/kasan/report.c:450 kasan_report+0x19a/0x1f0 mm/kasan/report.c:450 mm/kasan/report.c:450 nf_hook_entries_grow+0x5a7/0x700 net/netfilter/core.c:142 net/netfilter/core.c:142 __nf_register_net_hook+0x27e/0x8d0 net/netfilter/core.c:429 net/netfilter/core.c:429 nf_register_net_hook+0xaa/0x180 net/netfilter/core.c:571 net/netfilter/core.c:571 nft_register_flowtable_net_hooks+0x3c5/0x730 net/netfilter/nf_tables_api.c:7232 net/netfilter/nf_tables_api.c:7232 nf_tables_newflowtable+0x2022/0x2cf0 net/netfilter/nf_tables_api.c:7430 net/netfilter/nf_tables_api.c:7430 nfnetlink_rcv_batch net/netfilter/nfnetlink.c:513 [inline] nfnetlink_rcv_skb_batch net/netfilter/nfnetlink.c:634 [inline] nfnetlink_rcv_batch net/netfilter/nfnetlink.c:513 [inline] net/netfilter/nfnetlink.c:652 nfnetlink_rcv_skb_batch net/netfilter/nfnetlink.c:634 [inline] net/netfilter/nfnetlink.c:652 nfnetlink_rcv+0x10e6/0x2550 net/netfilter/nfnetlink.c:652 net/netfilter/nfnetlink.c:652 __nft_release_hook() calls nft_unregister_flowtable_net_hooks() which only unregisters the hooks, then after RCU grace period, it is guaranteed that no packets add new entries to the flowtable (no flow offload rules and flowtable hooks are reachable from packet path), so it is safe to call nf_flow_table_free() which cleans up the remaining entries from the flowtable (both software and hardware) and it unbinds the flow_block. Fixes: ff4bf2f42a40 ("netfilter: nf_tables: add nft_unregister_flowtable_hook()") Reported-by: syzbot+e918523f77e62790d6d9@syzkaller.appspotmail.com Signed-off-by: Pablo Neira Ayuso Signed-off-by: Greg Kroah-Hartman --- net/netfilter/nf_tables_api.c | 3 +++ 1 file changed, 3 insertions(+) --- a/net/netfilter/nf_tables_api.c +++ b/net/netfilter/nf_tables_api.c @@ -9574,10 +9574,13 @@ EXPORT_SYMBOL_GPL(__nft_release_basechai static void __nft_release_hook(struct net *net, struct nft_table *table) { + struct nft_flowtable *flowtable; struct nft_chain *chain; list_for_each_entry(chain, &table->chains, list) nf_tables_unregister_hook(net, table, chain); + list_for_each_entry(flowtable, &table->flowtables, list) + nft_unregister_flowtable_net_hooks(net, &flowtable->hook_list); } static void __nft_release_hooks(struct net *net)