Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp4743101pxj; Wed, 12 May 2021 12:11:27 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxWuFKOv/Bx3VwuVFpv98YXYZePFSQtdpdywLUH3i++d25z7axzpykX2lN8Dd8YcADkjE54 X-Received: by 2002:a05:6402:11c7:: with SMTP id j7mr45967505edw.129.1620846687114; Wed, 12 May 2021 12:11:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1620846687; cv=none; d=google.com; s=arc-20160816; b=aZGmNB+h43IL8mtmlSch8W/ElbGVbqmW+qt3F3r1OREsxIh22tapoC2hGDSRUM0KTM Q3kUJ4S9q8OabrIOKb0AyLV7frkwsuOA4+Iu9OdGr2Zxzck2hLvKVIPNqUaWL4sY5WB1 ZKQxW2sr7dQ7KST26ip3nLqw7Q61HdjntMC0iQMI7fBleCqGz1OcarA7qTlYEGgDUGyQ aHkJ6NsTOEzu5a/5jW6wpLQQabGlHY6OItJ2GkA7mpe7Kq8DqU7uOO7n1yrNHi9zIekl d2xCKL3Bg5BP+1gv8svJDHERAHbxwW5cmj0McvTkbxTa8/zw99/khLPAFAwORu+H0xhI cSyQ== 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=t5010ZK2vXq/jG8/ZHJA5m8Q4h2UZxdNe/Lk/Vrne+g=; b=DFPC6Urs9KcJZzQy57eGyjzGlfRFeXKjGM9HyRnCQvM/sTSSq20pJrGPfNBVwyPrVW epjox7kiKK1nyHS7/ayxW3c+4SDXX8qTe7U+QwFZvlTZoZUss+dqOPIodoDRvJStlGcC n7CyGEj61iQDXtaA2Xlt6BgJEfhf2ahaJH07XwM+Uhvn8nqUxn8iS7w2FhQ095WzKOkY 3fgBpAze5bGOpfE33gvoz27HZovPBnau3LB2wGtTLyG8KXNdRNE9biNhhtlJOu573xGm piYZcDCSgoEqviXnZGfyLEmuT9MbMd2zG1ox2giogQp0v4gBgice+F+2XYifRw52ts45 So7A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=X2ByBSmT; 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 ox25si244290ejb.739.2021.05.12.12.11.02; Wed, 12 May 2021 12:11:27 -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=X2ByBSmT; 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 S1359538AbhELSxa (ORCPT + 99 others); Wed, 12 May 2021 14:53:30 -0400 Received: from mail.kernel.org ([198.145.29.99]:35800 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243972AbhELQmU (ORCPT ); Wed, 12 May 2021 12:42:20 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id E7F1061C59; Wed, 12 May 2021 16:08:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1620835736; bh=ZShG+ObzJmjdX/xgBJjP8RYnjAvCDmoQRaT7DiYI0D4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=X2ByBSmT7xTx4SXiKcD4hTPb76V9sfIF8QVZJ39BkTh8uPXPpAZXsiF0Hx0U4KV4Y JanE8+GcKIM2x/25rW2Pmg8m9ht1215i/zcpxSL4K/asJK6qkr99nLdOSRmqXHToIY 63NE50cMXjuv5VstLZMNy1DYrYa9qhqORtYWl5Do= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Amit Klein , Eric Dumazet , Willy Tarreau , "David S. Miller" , Sasha Levin Subject: [PATCH 5.12 465/677] inet: use bigger hash table for IP ID generation Date: Wed, 12 May 2021 16:48:31 +0200 Message-Id: <20210512144852.814762983@linuxfoundation.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210512144837.204217980@linuxfoundation.org> References: <20210512144837.204217980@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: Eric Dumazet [ Upstream commit aa6dd211e4b1dde9d5dc25d699d35f789ae7eeba ] In commit 73f156a6e8c1 ("inetpeer: get rid of ip_id_count") I used a very small hash table that could be abused by patient attackers to reveal sensitive information. Switch to a dynamic sizing, depending on RAM size. Typical big hosts will now use 128x more storage (2 MB) to get a similar increase in security and reduction of hash collisions. As a bonus, use of alloc_large_system_hash() spreads allocated memory among all NUMA nodes. Fixes: 73f156a6e8c1 ("inetpeer: get rid of ip_id_count") Reported-by: Amit Klein Signed-off-by: Eric Dumazet Cc: Willy Tarreau Signed-off-by: David S. Miller Signed-off-by: Sasha Levin --- net/ipv4/route.c | 42 ++++++++++++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/net/ipv4/route.c b/net/ipv4/route.c index bba150fdd265..d635b4f32d34 100644 --- a/net/ipv4/route.c +++ b/net/ipv4/route.c @@ -66,6 +66,7 @@ #include #include #include +#include #include #include #include @@ -478,8 +479,10 @@ static void ipv4_confirm_neigh(const struct dst_entry *dst, const void *daddr) __ipv4_confirm_neigh(dev, *(__force u32 *)pkey); } -#define IP_IDENTS_SZ 2048u - +/* Hash tables of size 2048..262144 depending on RAM size. + * Each bucket uses 8 bytes. + */ +static u32 ip_idents_mask __read_mostly; static atomic_t *ip_idents __read_mostly; static u32 *ip_tstamps __read_mostly; @@ -489,12 +492,16 @@ static u32 *ip_tstamps __read_mostly; */ u32 ip_idents_reserve(u32 hash, int segs) { - u32 *p_tstamp = ip_tstamps + hash % IP_IDENTS_SZ; - atomic_t *p_id = ip_idents + hash % IP_IDENTS_SZ; - u32 old = READ_ONCE(*p_tstamp); - u32 now = (u32)jiffies; + u32 bucket, old, now = (u32)jiffies; + atomic_t *p_id; + u32 *p_tstamp; u32 delta = 0; + bucket = hash & ip_idents_mask; + p_tstamp = ip_tstamps + bucket; + p_id = ip_idents + bucket; + old = READ_ONCE(*p_tstamp); + if (old != now && cmpxchg(p_tstamp, old, now) == old) delta = prandom_u32_max(now - old); @@ -3553,18 +3560,25 @@ struct ip_rt_acct __percpu *ip_rt_acct __read_mostly; int __init ip_rt_init(void) { + void *idents_hash; int cpu; - ip_idents = kmalloc_array(IP_IDENTS_SZ, sizeof(*ip_idents), - GFP_KERNEL); - if (!ip_idents) - panic("IP: failed to allocate ip_idents\n"); + /* For modern hosts, this will use 2 MB of memory */ + idents_hash = alloc_large_system_hash("IP idents", + sizeof(*ip_idents) + sizeof(*ip_tstamps), + 0, + 16, /* one bucket per 64 KB */ + HASH_ZERO, + NULL, + &ip_idents_mask, + 2048, + 256*1024); + + ip_idents = idents_hash; - prandom_bytes(ip_idents, IP_IDENTS_SZ * sizeof(*ip_idents)); + prandom_bytes(ip_idents, (ip_idents_mask + 1) * sizeof(*ip_idents)); - ip_tstamps = kcalloc(IP_IDENTS_SZ, sizeof(*ip_tstamps), GFP_KERNEL); - if (!ip_tstamps) - panic("IP: failed to allocate ip_tstamps\n"); + ip_tstamps = idents_hash + (ip_idents_mask + 1) * sizeof(*ip_idents); for_each_possible_cpu(cpu) { struct uncached_list *ul = &per_cpu(rt_uncached_list, cpu); -- 2.30.2