Received: by 2002:a5d:9c59:0:0:0:0:0 with SMTP id 25csp129700iof; Sun, 5 Jun 2022 23:15:07 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw7sqFK59/lkNzTWJAnql++/gviE8HUyU4+UaMHz1JmbopEogHS9Yg4TYsxK17NUmYmn4OW X-Received: by 2002:a63:3f07:0:b0:3fd:5ae5:8bbe with SMTP id m7-20020a633f07000000b003fd5ae58bbemr9378128pga.205.1654496107203; Sun, 05 Jun 2022 23:15:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1654496107; cv=none; d=google.com; s=arc-20160816; b=MSoaTbH/mmSS0Bm0Gqsoa8l0T2OdfcbDRQNY3voah5vefMnqdCG1WWWObXdNyC2pif 2X+XjzWaU9CEaidAFUAS7dX34XriNOZF/qLJDoWnPBw6YBN09kkMG0dMKSSdAdXCgKYa /uEDV/jC4BYBZT5Yf7gAS2oidTVpgGmgLxRKQ4vh5IGljAXSrnkjdpvvrLzUn7tvjVDh DMxr/PjKheJgYyKF9d0udxT88ZoyWXOnYvGOE2SBoxiA8Q3AxsY1esxKjfl3vKCQNmwH K8yQy5H1lyOjkYrkTOgciGEu9cYMQabyxQAcg5fMCh1VZDZUxxCPl064uIRHih/ORxv4 Ijag== 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=GJBrEtD+inydH0mdq9ICFi5r99RjVPdHpffu5VeLhhA=; b=KVLadXvzhnrr9KkSIfRob8Z9QXPnjny9JOdVkEdVg85jI1lmcCo0cQk7Mq9sX2J4F1 1EftJ+PXvNcdfLfG8QKbL1pZyMaGoegVPx9a/c0zziStmZWrKuXQgKwkmpw0mVgmsscl AMRuAL9GkjLfFxKupHLkF0MrUUqLzeALTAGws73B3SLxyAIr02GIPstpKLV7+OexhU2o /HNUiUqZhpVbBXb6NqGzYFFI9ThYgRrxVUEJJEmsSvS79SrzSFWmKLBMeMEo5Wyf6gsV zha5PPbkeM54Af4MFm6moBsuAV8Su4NpN7NeSZcGYexe1ZpDuJaqZ3zhs0PCwTKsEzpN cGlA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=ktcSZbIQ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1: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 lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id f4-20020a170902860400b0016786f2d1d8si1065268plo.475.2022.06.05.23.15.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 05 Jun 2022 23:15:07 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=ktcSZbIQ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1: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: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id AF310D809B; Sun, 5 Jun 2022 21:54:18 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345074AbiFCRox (ORCPT + 99 others); Fri, 3 Jun 2022 13:44:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57686 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344838AbiFCRn6 (ORCPT ); Fri, 3 Jun 2022 13:43:58 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E8BAC54036; Fri, 3 Jun 2022 10:42:26 -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 dfw.source.kernel.org (Postfix) with ESMTPS id 5FD6A61B01; Fri, 3 Jun 2022 17:42:26 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4576BC36AE5; Fri, 3 Jun 2022 17:42:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1654278145; bh=GLAym5QDd84vfdwkI3wAoy6957Q/5/Y62Zd7C3u27Jk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ktcSZbIQdYF80HK79LiSc5zZI+j0ICjGVWa4LAnw0bJoYp2xWYAaMEfWxc0nW7ZUf pZeRy8npw5b5xWLWi9EzgD1cFeYWzD1Z/NV7MozMviBrsB+T134OlmGHRfszLN4pIF KeHuyhm+Bvtn3eOxkQBymdi5+goYlbkuwIi577Ls= 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" , Stefan Ghinea Subject: [PATCH 4.19 03/30] tcp: change source port randomizarion at connect() time Date: Fri, 3 Jun 2022 19:39:31 +0200 Message-Id: <20220603173815.192639169@linuxfoundation.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220603173815.088143764@linuxfoundation.org> References: <20220603173815.088143764@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=-3.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=unavailable 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 [SG: Adjusted context] Signed-off-by: Stefan Ghinea 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 @@ -714,6 +714,17 @@ unlock: } 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 *, @@ -727,7 +738,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, @@ -752,7 +763,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. */ @@ -805,7 +819,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);