Received: by 2002:a05:6358:489b:b0:bb:da1:e618 with SMTP id x27csp6758356rwn; Tue, 13 Sep 2022 08:38:19 -0700 (PDT) X-Google-Smtp-Source: AA6agR55Vh38zy8zxtsIaJX2RfmMuKKDLLN1t/V5MAVXQu/9C0+1GsLK7OCFlPQVwjrmfMJAvDj6 X-Received: by 2002:a05:6402:2905:b0:452:2604:ae96 with SMTP id ee5-20020a056402290500b004522604ae96mr5525851edb.31.1663083499664; Tue, 13 Sep 2022 08:38:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1663083499; cv=none; d=google.com; s=arc-20160816; b=Nt1FBKsjiVz2SrXNSlr4bXtNVzmrBsByiRY0ZD7qztBtN7BpuzA2xLPM4IGuKkHVJZ z46ZWrski4C4epTp8vN7KJ86ga/0aE4BfYlhCKo1Km3fgGFj8IAeT/5vH5jwpQkUCMe8 xKLJhmAekYiULCRMPObXVoqscHCIwQK7Fbeb2Z08se4TP9k59r9mMZVOWjHrjBo1wz9P Nh7R5ubafFLlgabK7Wb/+4kG2rAyRxWF3ax+oak8yfEcmSPdsHYvvdORcjlPmDlcMHHJ g8G0vevdjUMpHE2MjmlHMjLQE/G7fyBI1s2LYer/RfUWf2nOloc2HINDZO6DmWEb3QCT RGEw== 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=4yNxGK0aCIUuD0eqXCe77N57jDqRLgAA9Jqoxzd2+2o=; b=lAMZtGkSFbrQG8XBTfNVr3ahrLaIpNmvzfOPVcYWbODwyEYzUx13toM6y8rMM6pHEw eqKYwMECpoSPCH5sYCQ54c7E5dKra4Mq/53h/vmfqeye7RNGnLiSlESGuVZAVBaxCjEo O+HY78UgmLdUdqHfgPkGZ1yfs3AdhHomTNqaXwW+FXmwMHFtOiaKr3GPOYKYoF48LVXk Fye5IwkY0mk7vqU+0oQHw9XY+7rt0+HjTPM8qzERZqhsKRE0VfpJn050vwZ2Op/7McLf QGEYfIdNxkwHzMSsayv7cH0juxnBHqC1BkyFTrMk1EjnaLpl8wctAjPlOigUggXqcOaf 6aOg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=Cc3K+CAs; 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 fj4-20020a1709069c8400b0073d8e4e8c77si9488911ejc.1005.2022.09.13.08.37.49; Tue, 13 Sep 2022 08:38:19 -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=Cc3K+CAs; 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 S235158AbiIMPC5 (ORCPT + 99 others); Tue, 13 Sep 2022 11:02:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40428 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235114AbiIMPA5 (ORCPT ); Tue, 13 Sep 2022 11:00:57 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A909F61731; Tue, 13 Sep 2022 07:29:19 -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 ams.source.kernel.org (Postfix) with ESMTPS id 0EE62B80F9E; Tue, 13 Sep 2022 14:28:55 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 63152C433C1; Tue, 13 Sep 2022 14:28:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1663079333; bh=pPytlG+urWdjOEllwwtWCLR1Feo1RA8rtUtroA3jK0E=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Cc3K+CAs2I/uTa91tflxKIfwI43dTsC9IEyvYYaczbKDjsV2QWtzhcqRpRRKkJlOb QbRNNe8FsgoTZ8eOlUOrFW2qvAAz3H/qeze/tsHbCZ8SjoqfNnDnHSJ3DXiR8C91cE xxFeM3eOQ0T+UYWrHKx8xdlCY7qqKUzOett6Gmqk= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Harsh Modi , Florian Westphal , Pablo Neira Ayuso , Sasha Levin Subject: [PATCH 5.4 094/108] netfilter: br_netfilter: Drop dst references before setting. Date: Tue, 13 Sep 2022 16:07:05 +0200 Message-Id: <20220913140357.657145472@linuxfoundation.org> X-Mailer: git-send-email 2.37.3 In-Reply-To: <20220913140353.549108748@linuxfoundation.org> References: <20220913140353.549108748@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.1 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,T_SCC_BODY_TEXT_LINE 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: Harsh Modi [ Upstream commit d047283a7034140ea5da759a494fd2274affdd46 ] The IPv6 path already drops dst in the daddr changed case, but the IPv4 path does not. This change makes the two code paths consistent. Further, it is possible that there is already a metadata_dst allocated from ingress that might already be attached to skbuff->dst while following the bridge path. If it is not released before setting a new metadata_dst, it will be leaked. This is similar to what is done in bpf_set_tunnel_key() or ip6_route_input(). It is important to note that the memory being leaked is not the dst being set in the bridge code, but rather memory allocated from some other code path that is not being freed correctly before the skb dst is overwritten. An example of the leakage fixed by this commit found using kmemleak: unreferenced object 0xffff888010112b00 (size 256): comm "softirq", pid 0, jiffies 4294762496 (age 32.012s) hex dump (first 32 bytes): 00 00 00 00 00 00 00 00 80 16 f1 83 ff ff ff ff ................ e1 4e f6 82 ff ff ff ff 00 00 00 00 00 00 00 00 .N.............. backtrace: [<00000000d79567ea>] metadata_dst_alloc+0x1b/0xe0 [<00000000be113e13>] udp_tun_rx_dst+0x174/0x1f0 [<00000000a36848f4>] geneve_udp_encap_recv+0x350/0x7b0 [<00000000d4afb476>] udp_queue_rcv_one_skb+0x380/0x560 [<00000000ac064aea>] udp_unicast_rcv_skb+0x75/0x90 [<000000009a8ee8c5>] ip_protocol_deliver_rcu+0xd8/0x230 [<00000000ef4980bb>] ip_local_deliver_finish+0x7a/0xa0 [<00000000d7533c8c>] __netif_receive_skb_one_core+0x89/0xa0 [<00000000a879497d>] process_backlog+0x93/0x190 [<00000000e41ade9f>] __napi_poll+0x28/0x170 [<00000000b4c0906b>] net_rx_action+0x14f/0x2a0 [<00000000b20dd5d4>] __do_softirq+0xf4/0x305 [<000000003a7d7e15>] __irq_exit_rcu+0xc3/0x140 [<00000000968d39a2>] sysvec_apic_timer_interrupt+0x9e/0xc0 [<000000009e920794>] asm_sysvec_apic_timer_interrupt+0x16/0x20 [<000000008942add0>] native_safe_halt+0x13/0x20 Florian Westphal says: "Original code was likely fine because nothing ever did set a skb->dst entry earlier than bridge in those days." Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2") Signed-off-by: Harsh Modi Acked-by: Florian Westphal Signed-off-by: Pablo Neira Ayuso Signed-off-by: Sasha Levin --- net/bridge/br_netfilter_hooks.c | 2 ++ net/bridge/br_netfilter_ipv6.c | 1 + 2 files changed, 3 insertions(+) diff --git a/net/bridge/br_netfilter_hooks.c b/net/bridge/br_netfilter_hooks.c index 19726d81025d5..01e33724d10c3 100644 --- a/net/bridge/br_netfilter_hooks.c +++ b/net/bridge/br_netfilter_hooks.c @@ -384,6 +384,7 @@ static int br_nf_pre_routing_finish(struct net *net, struct sock *sk, struct sk_ /* - Bridged-and-DNAT'ed traffic doesn't * require ip_forwarding. */ if (rt->dst.dev == dev) { + skb_dst_drop(skb); skb_dst_set(skb, &rt->dst); goto bridged_dnat; } @@ -413,6 +414,7 @@ static int br_nf_pre_routing_finish(struct net *net, struct sock *sk, struct sk_ kfree_skb(skb); return 0; } + skb_dst_drop(skb); skb_dst_set_noref(skb, &rt->dst); } diff --git a/net/bridge/br_netfilter_ipv6.c b/net/bridge/br_netfilter_ipv6.c index e4e0c836c3f51..6b07f30675bb0 100644 --- a/net/bridge/br_netfilter_ipv6.c +++ b/net/bridge/br_netfilter_ipv6.c @@ -197,6 +197,7 @@ static int br_nf_pre_routing_finish_ipv6(struct net *net, struct sock *sk, struc kfree_skb(skb); return 0; } + skb_dst_drop(skb); skb_dst_set_noref(skb, &rt->dst); } -- 2.35.1