Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp212442iog; Fri, 17 Jun 2022 02:05:43 -0700 (PDT) X-Google-Smtp-Source: AGRyM1sZjCClzS3eJuj+JSYpC5qAVu6pJJhXR3WQ8OB3XihD+E/zIRMfxUdu22XyM/wtNs5KvdES X-Received: by 2002:a05:6402:3484:b0:42e:1f3b:4a4c with SMTP id v4-20020a056402348400b0042e1f3b4a4cmr11021969edc.326.1655456742829; Fri, 17 Jun 2022 02:05:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655456742; cv=none; d=google.com; s=arc-20160816; b=DbWdcIW5kapjCHOJ3BghUoep0Ibha0xckaNc1FnDvndEj15Y15i9oPmfsps7R1wu1/ w5tx0enX/yTudtVI6w9hWYPm3nP4pOC8pnn5menwzTNWfHZ3kiJFWfBpOAZqlV+MN2QT 8Oo9pvSV0n9kfRQ3BzATKRdfr5qHFVDgppEwszdNHkqtUKngq7jaDaT5Jo2eC+5oQ25G tSzuaoSpdncIdEIOsDqf3B5U6sjzH32fhP2NaOI3AKg4WsruKvd1Gei6bhPxhpwl7kXS RNyRXXEuznxdR2M3g66SGlAhMo+/4ZKYbLqBaheJ+0YzyvOEYlYm20tn5EYMu4xq5feI KMuA== 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=uMIp+sTEmYt4+z62ji/2rY5lq6EHo829V5rMuQt5ZXw=; b=y37KA8hndt7sH7z57NKemDBjQUcN4HnH34wW8Cmj0CVMBWS3PzNV/q/BGceg/MDlGd SltNbzBc8Chbxa1Rsg3hk0eI+pAzE2P7GLOcZOuE5BOBnxCzM6ql9bPGMfuVoPg2igoP JfZxq4wymXGIbB7JKJwTM1wjX2m0Y9v7VMba+1fHjBNnhbIH0o6lF51nusGIk04V6y/6 36Uj1MvSoBi3avxa0PL7iEyfiBKfOEk1TiFcpWDBd5M8klrsa9qU+IxkgdW+DZqwWb9K hjL5OHrIFtwcNPOxPmfmgLlCl6KCKKIwexpLeWHwvfCZyrWHl9o4YDUbI+h+iuPYHwj3 t7Vg== 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id h14-20020aa7cdce000000b0042e17781f86si4343636edw.268.2022.06.17.02.05.05; Fri, 17 Jun 2022 02:05:42 -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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1381443AbiFQI5b (ORCPT + 99 others); Fri, 17 Jun 2022 04:57:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44408 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1381427AbiFQI53 (ORCPT ); Fri, 17 Jun 2022 04:57:29 -0400 Received: from relay9-d.mail.gandi.net (relay9-d.mail.gandi.net [217.70.183.199]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A690C5250B; Fri, 17 Jun 2022 01:57:27 -0700 (PDT) Received: (Authenticated sender: pbl@bestov.io) by mail.gandi.net (Postfix) with ESMTPSA id 99015FF803; Fri, 17 Jun 2022 08:57:19 +0000 (UTC) From: Riccardo Paolo Bestetti To: Carlos Llamas , "David S . Miller" , Hideaki YOSHIFUJI , David Ahern , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Riccardo Paolo Bestetti Cc: kernel-team@android.com, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, Miaohe Lin , =?UTF-8?q?Maciej=20=C5=BBenczykowski?= Subject: [PATCH v2] ipv4: ping: fix bind address validity check Date: Fri, 17 Jun 2022 10:54:35 +0200 Message-Id: <20220617085435.193319-1-pbl@bestov.io> X-Mailer: git-send-email 2.36.1 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.6 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H2,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 Commit 8ff978b8b222 ("ipv4/raw: support binding to nonlocal addresses") introduced a helper function to fold duplicated validity checks of bind addresses into inet_addr_valid_or_nonlocal(). However, this caused an unintended regression in ping_check_bind_addr(), which previously would reject binding to multicast and broadcast addresses, but now these are both incorrectly allowed as reported in [1]. This patch restores the original check. A simple reordering is done to improve readability and make it evident that multicast and broadcast addresses should not be allowed. Also, add an early exit for INADDR_ANY which replaces lost behavior added by commit 0ce779a9f501 ("net: Avoid unnecessary inet_addr_type() call when addr is INADDR_ANY"). Furthermore, this patch introduces regression selftests to catch these specific cases. [1] https://lore.kernel.org/netdev/CANP3RGdkAcDyAZoT1h8Gtuu0saq+eOrrTiWbxnOs+5zn+cpyKg@mail.gmail.com/ Fixes: 8ff978b8b222 ("ipv4/raw: support binding to nonlocal addresses") Cc: Miaohe Lin Reported-by: Maciej Żenczykowski Signed-off-by: Carlos Llamas Signed-off-by: Riccardo Paolo Bestetti --- This patch is sent as a follow-up to the discussion on the v1 by Carlos Llamas. Original thread: https://lore.kernel.org/netdev/20220617020213.1881452-1-cmllamas@google.com/ net/ipv4/ping.c | 10 ++++--- tools/testing/selftests/net/fcnal-test.sh | 33 +++++++++++++++++++++++ 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/net/ipv4/ping.c b/net/ipv4/ping.c index 1a43ca73f94d..3c6101def7d6 100644 --- a/net/ipv4/ping.c +++ b/net/ipv4/ping.c @@ -319,12 +319,16 @@ static int ping_check_bind_addr(struct sock *sk, struct inet_sock *isk, pr_debug("ping_check_bind_addr(sk=%p,addr=%pI4,port=%d)\n", sk, &addr->sin_addr.s_addr, ntohs(addr->sin_port)); + if (addr->sin_addr.s_addr == htonl(INADDR_ANY)) + return 0; + tb_id = l3mdev_fib_table_by_index(net, sk->sk_bound_dev_if) ? : tb_id; chk_addr_ret = inet_addr_type_table(net, addr->sin_addr.s_addr, tb_id); - if (!inet_addr_valid_or_nonlocal(net, inet_sk(sk), - addr->sin_addr.s_addr, - chk_addr_ret)) + if (chk_addr_ret == RTN_MULTICAST || + chk_addr_ret == RTN_BROADCAST || + (chk_addr_ret != RTN_LOCAL && + !inet_can_nonlocal_bind(net, isk))) return -EADDRNOTAVAIL; #if IS_ENABLED(CONFIG_IPV6) diff --git a/tools/testing/selftests/net/fcnal-test.sh b/tools/testing/selftests/net/fcnal-test.sh index 54701c8b0cd7..75223b63e3c8 100755 --- a/tools/testing/selftests/net/fcnal-test.sh +++ b/tools/testing/selftests/net/fcnal-test.sh @@ -70,6 +70,10 @@ NSB_LO_IP6=2001:db8:2::2 NL_IP=172.17.1.1 NL_IP6=2001:db8:4::1 +# multicast and broadcast addresses +MCAST_IP=224.0.0.1 +BCAST_IP=255.255.255.255 + MD5_PW=abc123 MD5_WRONG_PW=abc1234 @@ -308,6 +312,9 @@ addr2str() 127.0.0.1) echo "loopback";; ::1) echo "IPv6 loopback";; + ${BCAST_IP}) echo "broadcast";; + ${MCAST_IP}) echo "multicast";; + ${NSA_IP}) echo "ns-A IP";; ${NSA_IP6}) echo "ns-A IPv6";; ${NSA_LO_IP}) echo "ns-A loopback IP";; @@ -1800,6 +1807,19 @@ ipv4_addr_bind_novrf() run_cmd nettest -s -R -P icmp -f -l ${a} -I ${NSA_DEV} -b log_test_addr ${a} $? 0 "Raw socket bind to nonlocal address after device bind" + # + # check that ICMP sockets cannot bind to broadcast and multicast addresses + # + a=${BCAST_IP} + log_start + run_cmd nettest -s -R -P icmp -l ${a} -b + log_test_addr ${a} $? 1 "ICMP socket bind to broadcast address" + + a=${MCAST_IP} + log_start + run_cmd nettest -s -R -P icmp -f -l ${a} -b + log_test_addr ${a} $? 1 "ICMP socket bind to multicast address" + # # tcp sockets # @@ -1857,6 +1877,19 @@ ipv4_addr_bind_vrf() run_cmd nettest -s -R -P icmp -f -l ${a} -I ${VRF} -b log_test_addr ${a} $? 0 "Raw socket bind to nonlocal address after VRF bind" + # + # check that ICMP sockets cannot bind to broadcast and multicast addresses + # + a=${BCAST_IP} + log_start + run_cmd nettest -s -R -P icmp -l ${a} -I ${VRF} -b + log_test_addr ${a} $? 1 "ICMP socket bind to broadcast address after VRF bind" + + a=${MCAST_IP} + log_start + run_cmd nettest -s -R -P icmp -f -l ${a} -I ${VRF} -b + log_test_addr ${a} $? 1 "ICMP socket bind to multicast address after VRF bind" + # # tcp sockets # -- 2.36.1