Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp702246pxb; Mon, 25 Oct 2021 17:00:35 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxXhbGDYUviPwZUDxTZUQnvNgiIv12NxiHWm3YrdhNEknCAAoO2C+cgReahm5wj/oEQNgxY X-Received: by 2002:a50:aada:: with SMTP id r26mr22003575edc.254.1635206435156; Mon, 25 Oct 2021 17:00:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1635206435; cv=none; d=google.com; s=arc-20160816; b=K4R5DryjS+RC5wRe5oaNmUzBovH6bot0WEHlRYfoRii1D//DkT3Y6EGnCgrscwHohP Iu0b91+zEEQoCVrnl1w6QYLQtPCGUaQKswUA0ci6WGhwUMAjfA0XonOY0J1++ySVJVtO fYRkSfch/gmXA+2ChQFdIMHeNlxg89HkcYe09E7a0m06vu4G29YN1+fYLmZHTTLUmLaJ oJ5/r96xRfZwc5+TEGRnGgwo61JECe7EABoPlU8R0/Xtp11wS96+5ggUjAlmdsf8F36k 9WHGtm+IHFWuCfzeiO9jxAhryKw5VTWRVSAHT3MCQQKwhK3EHYbNI9iKDbbmZECcTOBN VyOQ== 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=Z99zVUuFpyNU3U7x14M0LWkvS0B4qmxr7zHe5HJv9d8=; b=Pv9uVjzWwYJlsPliIELAVKMy5sf4Zu81eJOWqZl+RxNqAZ8eInmO2ccxa13qbpsRgA G/9u7pJfLzLo1vfF+0H+BI6Lg3dlzQHktvtzmChPPh/ZJi+Az0y4L9GXsZG+fS0SjQlb oX6/3LO9YuLggvyBFV1jMaMeZt4/HR+KLYTsbT3oa5+/eOQ+Ya31DrNYIR3pSKY+caAk 0WPyKwDIziUkkCdF/QrjTRjfyB/gIqfXw8gQL+fOu+5GGVYjo5TXkm4HU49pZaGhIuvl wvlWNoxaX6kPp0OGUYq1Rj8AmwoIEKsZTaC1qJDcybT7Xa+a2M4UPGyWTgnP5iqF/2eI h+CQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=I8jajgO6; 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 m5si15132261edp.127.2021.10.25.17.00.11; Mon, 25 Oct 2021 17:00:35 -0700 (PDT) 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=I8jajgO6; 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 S234743AbhJYTkT (ORCPT + 99 others); Mon, 25 Oct 2021 15:40:19 -0400 Received: from mail.kernel.org ([198.145.29.99]:53560 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236386AbhJYTeb (ORCPT ); Mon, 25 Oct 2021 15:34:31 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id D45BD61076; Mon, 25 Oct 2021 19:30:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1635190212; bh=QJWbsBUjiiYg2wkcVaQlEPkz8CYbjwERtgzCpyZLfSQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=I8jajgO6c3B7BeGUiThPxdqyJm4oU7oEISFTNUY4gAdz94H1tg5bnzrVU2hDHKRSj Da1vo2plUcaYvBuc/8xu2+bKp0dPbCNjB5su8Y5U5tbvRVw596lxAFuEGZbgxug3fQ dUGQazDooQJYIz+zIKsa9+jLjCsg/eHE06twnhtg= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Leonard Crestez , David Ahern , "David S. Miller" , Sasha Levin Subject: [PATCH 5.10 19/95] tcp: md5: Fix overlap between vrf and non-vrf keys Date: Mon, 25 Oct 2021 21:14:16 +0200 Message-Id: <20211025190959.710730154@linuxfoundation.org> X-Mailer: git-send-email 2.33.1 In-Reply-To: <20211025190956.374447057@linuxfoundation.org> References: <20211025190956.374447057@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: Leonard Crestez [ Upstream commit 86f1e3a8489f6a0232c1f3bc2bdb379f5ccdecec ] With net.ipv4.tcp_l3mdev_accept=1 it is possible for a listen socket to accept connection from the same client address in different VRFs. It is also possible to set different MD5 keys for these clients which differ only in the tcpm_l3index field. This appears to work when distinguishing between different VRFs but not between non-VRF and VRF connections. In particular: * tcp_md5_do_lookup_exact will match a non-vrf key against a vrf key. This means that adding a key with l3index != 0 after a key with l3index == 0 will cause the earlier key to be deleted. Both keys can be present if the non-vrf key is added later. * _tcp_md5_do_lookup can match a non-vrf key before a vrf key. This casues failures if the passwords differ. Fix this by making tcp_md5_do_lookup_exact perform an actual exact comparison on l3index and by making __tcp_md5_do_lookup perfer vrf-bound keys above other considerations like prefixlen. Fixes: dea53bb80e07 ("tcp: Add l3index to tcp_md5sig_key and md5 functions") Signed-off-by: Leonard Crestez Reviewed-by: David Ahern Signed-off-by: David S. Miller Signed-off-by: Sasha Levin --- net/ipv4/tcp_ipv4.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/net/ipv4/tcp_ipv4.c b/net/ipv4/tcp_ipv4.c index 71395e745bc5..017cd666387f 100644 --- a/net/ipv4/tcp_ipv4.c +++ b/net/ipv4/tcp_ipv4.c @@ -1022,6 +1022,20 @@ static void tcp_v4_reqsk_destructor(struct request_sock *req) DEFINE_STATIC_KEY_FALSE(tcp_md5_needed); EXPORT_SYMBOL(tcp_md5_needed); +static bool better_md5_match(struct tcp_md5sig_key *old, struct tcp_md5sig_key *new) +{ + if (!old) + return true; + + /* l3index always overrides non-l3index */ + if (old->l3index && new->l3index == 0) + return false; + if (old->l3index == 0 && new->l3index) + return true; + + return old->prefixlen < new->prefixlen; +} + /* Find the Key structure for an address. */ struct tcp_md5sig_key *__tcp_md5_do_lookup(const struct sock *sk, int l3index, const union tcp_md5_addr *addr, @@ -1059,8 +1073,7 @@ struct tcp_md5sig_key *__tcp_md5_do_lookup(const struct sock *sk, int l3index, match = false; } - if (match && (!best_match || - key->prefixlen > best_match->prefixlen)) + if (match && better_md5_match(best_match, key)) best_match = key; } return best_match; @@ -1090,7 +1103,7 @@ static struct tcp_md5sig_key *tcp_md5_do_lookup_exact(const struct sock *sk, lockdep_sock_is_held(sk)) { if (key->family != family) continue; - if (key->l3index && key->l3index != l3index) + if (key->l3index != l3index) continue; if (!memcmp(&key->addr, addr, size) && key->prefixlen == prefixlen) -- 2.33.0