Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp6136768iog; Thu, 23 Jun 2022 12:06:58 -0700 (PDT) X-Google-Smtp-Source: AGRyM1uejJe2NyUTLLTJNxdP9S8A7HeDA59uy6I54ua9jmCvtpKJtVEJwvD2HRF2BckfmfKSfBH9 X-Received: by 2002:a17:90a:d904:b0:1ec:730c:bcac with SMTP id c4-20020a17090ad90400b001ec730cbcacmr5476168pjv.93.1656011217939; Thu, 23 Jun 2022 12:06:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1656011217; cv=none; d=google.com; s=arc-20160816; b=fUBV4SIvLbByBr6oclAi3B/AWbou1RpCTcc3ISvOljaxXTo24sDSFBS6e3AibdJADT 8Kf/gjZqtSrYCshmsoOqBCM3A2MFUEE18YO3T8URfgeJK1dKGeybXqIQVsmnr6tFbm3i UmgmRC1x/kO7CSTMZJOrXVCtiwCwQfmBjNRhUR2iHRz4Xg9ev7yCJNjixtHFswmaeS+z BCSk0RoNN4lbU3w3nCpwdL7JTrGp3sUkhhW5z3EmV8wClOznBAGtmglKnT6pgF6iTAzb yqhMWjcVy75aZx9O02s3QpUKDf6VwK6BGg4hhm7Zso/8Gls/qqNi8IjAxYF0VZYIYV+1 FkMg== 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=kW+S4V5YhQomzH7AjTyn6PD+PEORZM7Qum5KEY4ENSY=; b=kjC53rtUbt1wephJkbxiVBozFvxz8aBQjSD5lcSnSCGfVOJzaf9nV8Fn8uxM05U3xa wHzcjr0Qw5M6/pKDcCR9ZiZE3FEB9O2w7bOss5hxJMDftIHJdgI+HXEASmkShwsn6CeD fO8P8IXXaZBV6X+0I724A2egajWK2tkbRm23X3Gbs6yggEFjDatCb2di9olxS9G38IJ2 51ho603fGGh1bZMp7YwexTIxi1mDfQMkyaEisBHo9aOlnccnGKV0AWYIECUNypTGd6KI LdRUmwzkeP5JK1ioLemFbEyroedWicdNJ41clk9LUXxbsfx0p5uwaiInCbisKum3Xm1c epGg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=mOlwFcKj; 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 c36-20020a630d24000000b0040cc39c0356si14998074pgl.548.2022.06.23.12.06.46; Thu, 23 Jun 2022 12:06:57 -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=mOlwFcKj; 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 S232758AbiFWRRB (ORCPT + 99 others); Thu, 23 Jun 2022 13:17:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39194 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233714AbiFWRNO (ORCPT ); Thu, 23 Jun 2022 13:13:14 -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 8CE8D4F1CD; Thu, 23 Jun 2022 09:59:03 -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 CE4A8B8249A; Thu, 23 Jun 2022 16:59:01 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3AD0DC3411B; Thu, 23 Jun 2022 16:58:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1656003540; bh=cBOv8hjlO7Jg88ZZ0AFqVPo4tENAv9nmdUnh2smA/h0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=mOlwFcKjNmcTrXKU+Dwj0k7vfmGnegq5KDepq6HojEv9ATVd9CHdZSoz+BuswBaZQ fFVX/CnCPH327rcBgcaBTM183McJ63rEcr4DBnCMcE8VPn+bZJg1zG1qDFERVysWXi CyLW6ZfDgNyc5VgsU+g/2BsaEVDqt0z3fq4fb/60= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Eric Dumazet , David Dworken , Willem de Bruijn , "David S. Miller" , Ben Hutchings Subject: [PATCH 4.9 257/264] tcp: change source port randomizarion at connect() time Date: Thu, 23 Jun 2022 18:44:10 +0200 Message-Id: <20220623164351.338190438@linuxfoundation.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220623164344.053938039@linuxfoundation.org> References: <20220623164344.053938039@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.7 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: Eric Dumazet commit 190cc82489f46f9d88e73c81a47e14f80a791e1a upstream. RFC 6056 (Recommendations for Transport-Protocol Port Randomization) provides good summary of why source selection needs extra care. David Dworken reminded us that linux implements Algorithm 3 as described in RFC 6056 3.3.3 Quoting David : In the context of the web, this creates an interesting info leak where websites can count how many TCP connections a user's computer is establishing over time. For example, this allows a website to count exactly how many subresources a third party website loaded. This also allows: - Distinguishing between different users behind a VPN based on distinct source port ranges. - Tracking users over time across multiple networks. - Covert communication channels between different browsers/browser profiles running on the same computer - Tracking what applications are running on a computer based on the pattern of how fast source ports are getting incremented. Section 3.3.4 describes an enhancement, that reduces attackers ability to use the basic information currently stored into the shared 'u32 hint'. This change also decreases collision rate when multiple applications need to connect() to different destinations. Signed-off-by: Eric Dumazet Reported-by: David Dworken Cc: Willem de Bruijn Signed-off-by: David S. Miller [bwh: Backported to 4.9: adjust context] Signed-off-by: Ben Hutchings Signed-off-by: Greg Kroah-Hartman --- net/ipv4/inet_hashtables.c | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) --- a/net/ipv4/inet_hashtables.c +++ b/net/ipv4/inet_hashtables.c @@ -537,6 +537,17 @@ void inet_unhash(struct sock *sk) } EXPORT_SYMBOL_GPL(inet_unhash); +/* RFC 6056 3.3.4. Algorithm 4: Double-Hash Port Selection Algorithm + * Note that we use 32bit integers (vs RFC 'short integers') + * because 2^16 is not a multiple of num_ephemeral and this + * property might be used by clever attacker. + * RFC claims using TABLE_LENGTH=10 buckets gives an improvement, + * we use 256 instead to really give more isolation and + * privacy, this only consumes 1 KB of kernel memory. + */ +#define INET_TABLE_PERTURB_SHIFT 8 +static u32 table_perturb[1 << INET_TABLE_PERTURB_SHIFT]; + int __inet_hash_connect(struct inet_timewait_death_row *death_row, struct sock *sk, u32 port_offset, int (*check_established)(struct inet_timewait_death_row *, @@ -550,7 +561,7 @@ int __inet_hash_connect(struct inet_time struct inet_bind_bucket *tb; u32 remaining, offset; int ret, i, low, high; - static u32 hint; + u32 index; if (port) { head = &hinfo->bhash[inet_bhashfn(net, port, @@ -575,7 +586,10 @@ int __inet_hash_connect(struct inet_time if (likely(remaining > 1)) remaining &= ~1U; - offset = (hint + port_offset) % remaining; + net_get_random_once(table_perturb, sizeof(table_perturb)); + index = hash_32(port_offset, INET_TABLE_PERTURB_SHIFT); + + offset = (READ_ONCE(table_perturb[index]) + port_offset) % remaining; /* In first pass we try ports of @low parity. * inet_csk_get_port() does the opposite choice. */ @@ -628,7 +642,7 @@ next_port: return -EADDRNOTAVAIL; ok: - hint += i + 2; + WRITE_ONCE(table_perturb[index], READ_ONCE(table_perturb[index]) + i + 2); /* Head lock still held and bh's disabled */ inet_bind_hash(sk, tb, port);