Received: by 2002:ac0:e34a:0:0:0:0:0 with SMTP id g10csp161935imn; Wed, 27 Jul 2022 18:44:46 -0700 (PDT) X-Google-Smtp-Source: AGRyM1vG2x2jg5NTFPind0faruqHhHxk8IpPBO5lLgqFsZypLF0krIq11fdWUcDKkGDcc941a4ye X-Received: by 2002:a05:6a00:234c:b0:52b:7cb:ed9d with SMTP id j12-20020a056a00234c00b0052b07cbed9dmr24774550pfj.32.1658972685875; Wed, 27 Jul 2022 18:44:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1658972685; cv=none; d=google.com; s=arc-20160816; b=bYba0m2k45O7G/mkGkoQfNvMJcrdlWV9crF3Iw0vP2Y6AfkzeqFXEQl1ASqE2ZVc54 xkImcY/t6my4vNx+QIHVJMORELS3Gr7jdXxU0uVGMeeO4an+ANlUnMHkT9SAgSXxSerX bqqcYw0zmcVBVDcw6ZwopRTj2l/Hkj8am7j/v8N1W9o5K/Cd+lvKE8vXUALUXhMem/+e PDcvR+2BbyH9uC2F1totwKD0QbKxopbXBNRMEvo8KGK/trKhfAzjNS8AY//FYdYLbqmK +PTkDVFYJ19hgJgKzhMYKK6LIyq9A/2BDVVm5guVqnHMM7HAiawsy+fFzZKeVbcBnrge dVBw== 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 :message-id:date:subject:cc:to:from; bh=35VXaHwWP8ktKPgk5rFYdhqEjf97uXWeSjACvyFx8Mg=; b=UhJT+SS3+s8Ex/p42o7RD4Zt2qsQyjYcP7Fe5svV5zwnQbA8wWshjJtMzrd1lv77ij fJka5w/1BIvy5ferICdAPtwd9BkPQdBJIGlY0akB6JFJVz954LsDMngYHf5cAtmC49rg 84kSUvnloLpfiZeQ41qp5mbeLLfgfuBLULMu8m6X3800KYIp+r7k7Gd6hk4YU0yz3cC7 ++dY5DujxRDjDsWUUB270SSzQkGsCE1up3uxINM3A9ollS22R94l1mXQawfk9S8/Wm1G pHMld7ya6lUejLjTMMyYOJOCXE1jWYu2fD0p3US7nDxmUdtGo8dzThnLgwQT9JNx25Te 21Ng== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id r19-20020a63ec53000000b0041a0a9d63c4si22225354pgj.166.2022.07.27.18.44.30; Wed, 27 Jul 2022 18:44:45 -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; 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=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234298AbiG1BdQ (ORCPT + 99 others); Wed, 27 Jul 2022 21:33:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48352 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229511AbiG1BdP (ORCPT ); Wed, 27 Jul 2022 21:33:15 -0400 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C223B402D8; Wed, 27 Jul 2022 18:33:13 -0700 (PDT) Received: from canpemm500006.china.huawei.com (unknown [172.30.72.57]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4LtY492DVrzjXXv; Thu, 28 Jul 2022 09:30:17 +0800 (CST) Received: from ubuntu-82.huawei.com (10.175.104.82) by canpemm500006.china.huawei.com (7.192.105.130) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Thu, 28 Jul 2022 09:33:11 +0800 From: Ziyang Xuan To: , , , , , , CC: Subject: [PATCH net v3] ipv6/addrconf: fix a null-ptr-deref bug for ip6_ptr Date: Thu, 28 Jul 2022 09:33:07 +0800 Message-ID: <20220728013307.656257-1-william.xuanziyang@huawei.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: 7BIT Content-Type: text/plain; charset=US-ASCII X-Originating-IP: [10.175.104.82] X-ClientProxiedBy: dggems705-chm.china.huawei.com (10.3.19.182) To canpemm500006.china.huawei.com (7.192.105.130) X-CFilter-Loop: Reflected X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_MED, 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 Change net device's MTU to smaller than IPV6_MIN_MTU or unregister device while matching route. That may trigger null-ptr-deref bug for ip6_ptr probability as following. ========================================================= BUG: KASAN: null-ptr-deref in find_match.part.0+0x70/0x134 Read of size 4 at addr 0000000000000308 by task ping6/263 CPU: 2 PID: 263 Comm: ping6 Not tainted 5.19.0-rc7+ #14 Call trace: dump_backtrace+0x1a8/0x230 show_stack+0x20/0x70 dump_stack_lvl+0x68/0x84 print_report+0xc4/0x120 kasan_report+0x84/0x120 __asan_load4+0x94/0xd0 find_match.part.0+0x70/0x134 __find_rr_leaf+0x408/0x470 fib6_table_lookup+0x264/0x540 ip6_pol_route+0xf4/0x260 ip6_pol_route_output+0x58/0x70 fib6_rule_lookup+0x1a8/0x330 ip6_route_output_flags_noref+0xd8/0x1a0 ip6_route_output_flags+0x58/0x160 ip6_dst_lookup_tail+0x5b4/0x85c ip6_dst_lookup_flow+0x98/0x120 rawv6_sendmsg+0x49c/0xc70 inet_sendmsg+0x68/0x94 Reproducer as following: Firstly, prepare conditions: $ip netns add ns1 $ip netns add ns2 $ip link add veth1 type veth peer name veth2 $ip link set veth1 netns ns1 $ip link set veth2 netns ns2 $ip netns exec ns1 ip -6 addr add 2001:0db8:0:f101::1/64 dev veth1 $ip netns exec ns2 ip -6 addr add 2001:0db8:0:f101::2/64 dev veth2 $ip netns exec ns1 ifconfig veth1 up $ip netns exec ns2 ifconfig veth2 up $ip netns exec ns1 ip -6 route add 2000::/64 dev veth1 metric 1 $ip netns exec ns2 ip -6 route add 2001::/64 dev veth2 metric 1 Secondly, execute the following two commands in two ssh windows respectively: $ip netns exec ns1 sh $while true; do ip -6 addr add 2001:0db8:0:f101::1/64 dev veth1; ip -6 route add 2000::/64 dev veth1 metric 1; ping6 2000::2; done $ip netns exec ns1 sh $while true; do ip link set veth1 mtu 1000; ip link set veth1 mtu 1500; sleep 5; done It is because ip6_ptr has been assigned to NULL in addrconf_ifdown() firstly, then ip6_ignore_linkdown() accesses ip6_ptr directly without NULL check. cpu0 cpu1 fib6_table_lookup __find_rr_leaf addrconf_notify [ NETDEV_CHANGEMTU ] addrconf_ifdown RCU_INIT_POINTER(dev->ip6_ptr, NULL) find_match ip6_ignore_linkdown So we can add NULL check for ip6_ptr before using in ip6_ignore_linkdown() to fix the null-ptr-deref bug. Fixes: dcd1f572954f ("net/ipv6: Remove fib6_idev") Signed-off-by: Ziyang Xuan Reviewed-by: David Ahern --- v3: - Fix 'Fixes' commit v2: - Use NULL check in ip6_ignore_linkdown() but synchronize_net() in addrconf_ifdown() - Add timing analysis of the problem --- include/net/addrconf.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/include/net/addrconf.h b/include/net/addrconf.h index f7506f08e505..c04f359655b8 100644 --- a/include/net/addrconf.h +++ b/include/net/addrconf.h @@ -405,6 +405,9 @@ static inline bool ip6_ignore_linkdown(const struct net_device *dev) { const struct inet6_dev *idev = __in6_dev_get(dev); + if (unlikely(!idev)) + return true; + return !!idev->cnf.ignore_routes_with_linkdown; } -- 2.25.1