Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp648177pxb; Tue, 2 Feb 2021 14:18:02 -0800 (PST) X-Google-Smtp-Source: ABdhPJzb79Z8/s7cMNXhG5lEACREVaTehGnWbz5sFxe3CmDJGuqqA1xDlOqjkVl4rORTtHCi+vCE X-Received: by 2002:a17:906:b106:: with SMTP id u6mr121646ejy.313.1612304282532; Tue, 02 Feb 2021 14:18:02 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1612304282; cv=none; d=google.com; s=arc-20160816; b=xhTRWjutAg4uHBGI1mxceiKUiXa//IFxbPiJcoFmfUKzNqXh2KDHuumgiw9VK70ocG GOzz/xjnAdeUzjGS6FktPDSRmleXd0jpwmDmrOi5OlyJizYg+VGZdMvu0pFn3rMjRH3l /AQ4S6sbnIfcDnKmsDcd4meEoWbO2hQ6rD4PmhOUAaLaxmlp+33yMkd+Wms83YsLc9Hy D26CRtx2DlJWCCeIzBjqHcpmLU4ZFYJ7oV+fpOqjFKlw9gHJXUTGuQT0JkUjNp3tHg70 dV0d1pN9Idu98RvXsB08lEb74nIVka/0LFSpTLQt52ZarR9AEZ7uinRl/j7mTff5UYiw RNIg== 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=AwzFchrZ3QZ2XdkfmGXdqCKQQz2nD+xKYciYJkmbgYI=; b=lZEAkQGlWB377YFTuteJmJmhZvJdI+XelAuJIW8aNECjwjFkfQivtrKX1ytWSIbo0j wwB5wbZliTMzXt10qYBdSKs7K9ePnYBF9Lq0UXAJNbHOrgSXFm9ix9t4neRWjutM9ore JO67PYEa+JU6wdT3W8vmHzMp/QOedH8jzMdnyPHb3JIDJR3HhrDvGTf5wG1FsoAVzWX9 X5J3oZsAlDoamFYfG0i3i+a8YfeQRfo5SdrZJtGKp6SMR3B8zN5LhF5WBkKBH4I2fgRt uV6UKPbG/yqO4UUV+CgLJOvT3GzVv+GqLOPT53PVqLCAjHsCpbzC9NVGCZaL2lOpSIB+ b3uw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=I0RnwmJp; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id z5si95736ejp.534.2021.02.02.14.17.37; Tue, 02 Feb 2021 14:18:02 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=I0RnwmJp; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 S233417AbhBBN7z (ORCPT + 99 others); Tue, 2 Feb 2021 08:59:55 -0500 Received: from mail.kernel.org ([198.145.29.99]:40856 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233103AbhBBNxr (ORCPT ); Tue, 2 Feb 2021 08:53:47 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id 8704564FCF; Tue, 2 Feb 2021 13:44:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1612273455; bh=fYfoRWgdk+jUSFkQRvrTYCziREB34YC+M46Ggdj/hiI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=I0RnwmJpsr65AXhAtgGFKgHKXaH26FjBJ44XU4+7qfh3mf8NssOqGofitm/CbPRFB CO4Jb68/HTD0XwNiJlk74KHIDGwTqPAKTnhOhxz8tCCrqqvpY2JdEOL8ii66qrs2z+ GP9YPrk0LkR6RRB9yPDWV3AJMn/7yVBBPJw9MS1E= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Visa Hankala , Florian Westphal , Steffen Klassert , Sasha Levin Subject: [PATCH 5.10 081/142] xfrm: Fix wraparound in xfrm_policy_addr_delta() Date: Tue, 2 Feb 2021 14:37:24 +0100 Message-Id: <20210202133001.049782711@linuxfoundation.org> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20210202132957.692094111@linuxfoundation.org> References: <20210202132957.692094111@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Visa Hankala [ Upstream commit da64ae2d35d3673233f0403b035d4c6acbf71965 ] Use three-way comparison for address components to avoid integer wraparound in the result of xfrm_policy_addr_delta(). This ensures that the search trees are built and traversed correctly. Treat IPv4 and IPv6 similarly by returning 0 when prefixlen == 0. Prefix /0 has only one equivalence class. Fixes: 9cf545ebd591d ("xfrm: policy: store inexact policies in a tree ordered by destination address") Signed-off-by: Visa Hankala Acked-by: Florian Westphal Signed-off-by: Steffen Klassert Signed-off-by: Sasha Levin --- net/xfrm/xfrm_policy.c | 26 +++++++++---- tools/testing/selftests/net/xfrm_policy.sh | 43 ++++++++++++++++++++++ 2 files changed, 61 insertions(+), 8 deletions(-) diff --git a/net/xfrm/xfrm_policy.c b/net/xfrm/xfrm_policy.c index 2f84136af48ab..b74f28cabe24f 100644 --- a/net/xfrm/xfrm_policy.c +++ b/net/xfrm/xfrm_policy.c @@ -793,15 +793,22 @@ static int xfrm_policy_addr_delta(const xfrm_address_t *a, const xfrm_address_t *b, u8 prefixlen, u16 family) { + u32 ma, mb, mask; unsigned int pdw, pbi; int delta = 0; switch (family) { case AF_INET: - if (sizeof(long) == 4 && prefixlen == 0) - return ntohl(a->a4) - ntohl(b->a4); - return (ntohl(a->a4) & ((~0UL << (32 - prefixlen)))) - - (ntohl(b->a4) & ((~0UL << (32 - prefixlen)))); + if (prefixlen == 0) + return 0; + mask = ~0U << (32 - prefixlen); + ma = ntohl(a->a4) & mask; + mb = ntohl(b->a4) & mask; + if (ma < mb) + delta = -1; + else if (ma > mb) + delta = 1; + break; case AF_INET6: pdw = prefixlen >> 5; pbi = prefixlen & 0x1f; @@ -812,10 +819,13 @@ static int xfrm_policy_addr_delta(const xfrm_address_t *a, return delta; } if (pbi) { - u32 mask = ~0u << (32 - pbi); - - delta = (ntohl(a->a6[pdw]) & mask) - - (ntohl(b->a6[pdw]) & mask); + mask = ~0U << (32 - pbi); + ma = ntohl(a->a6[pdw]) & mask; + mb = ntohl(b->a6[pdw]) & mask; + if (ma < mb) + delta = -1; + else if (ma > mb) + delta = 1; } break; default: diff --git a/tools/testing/selftests/net/xfrm_policy.sh b/tools/testing/selftests/net/xfrm_policy.sh index 5922941e70c6c..bdf450eaf60cf 100755 --- a/tools/testing/selftests/net/xfrm_policy.sh +++ b/tools/testing/selftests/net/xfrm_policy.sh @@ -287,6 +287,47 @@ check_hthresh_repeat() return 0 } +# insert non-overlapping policies in a random order and check that +# all of them can be fetched using the traffic selectors. +check_random_order() +{ + local ns=$1 + local log=$2 + + for i in $(seq 100); do + ip -net $ns xfrm policy flush + for j in $(seq 0 16 255 | sort -R); do + ip -net $ns xfrm policy add dst $j.0.0.0/24 dir out priority 10 action allow + done + for j in $(seq 0 16 255); do + if ! ip -net $ns xfrm policy get dst $j.0.0.0/24 dir out > /dev/null; then + echo "FAIL: $log" 1>&2 + return 1 + fi + done + done + + for i in $(seq 100); do + ip -net $ns xfrm policy flush + for j in $(seq 0 16 255 | sort -R); do + local addr=$(printf "e000:0000:%02x00::/56" $j) + ip -net $ns xfrm policy add dst $addr dir out priority 10 action allow + done + for j in $(seq 0 16 255); do + local addr=$(printf "e000:0000:%02x00::/56" $j) + if ! ip -net $ns xfrm policy get dst $addr dir out > /dev/null; then + echo "FAIL: $log" 1>&2 + return 1 + fi + done + done + + ip -net $ns xfrm policy flush + + echo "PASS: $log" + return 0 +} + #check for needed privileges if [ "$(id -u)" -ne 0 ];then echo "SKIP: Need root privileges" @@ -438,6 +479,8 @@ check_exceptions "exceptions and block policies after htresh change to normal" check_hthresh_repeat "policies with repeated htresh change" +check_random_order ns3 "policies inserted in random order" + for i in 1 2 3 4;do ip netns del ns$i;done exit $ret -- 2.27.0