From patchwork Tue Jun 22 08:57:13 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Amit Klein X-Patchwork-Id: 465584 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 60AB6C48BE5 for ; Tue, 22 Jun 2021 08:57:18 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 428436113E for ; Tue, 22 Jun 2021 08:57:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229840AbhFVI7b (ORCPT ); Tue, 22 Jun 2021 04:59:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53590 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229490AbhFVI7a (ORCPT ); Tue, 22 Jun 2021 04:59:30 -0400 Received: from mail-wr1-x42e.google.com (mail-wr1-x42e.google.com [IPv6:2a00:1450:4864:20::42e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 28853C061574; Tue, 22 Jun 2021 01:57:15 -0700 (PDT) Received: by mail-wr1-x42e.google.com with SMTP id j2so12129568wrs.12; Tue, 22 Jun 2021 01:57:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=message-id:date:mime-version:from:to:subject :content-transfer-encoding; bh=FG0AmbsSXTq8aZKNtsn7qS7XkGzSZ8NL2jrigaWI+cc=; b=dQM1cqmfZIDA4EaULsoVcI3RAMKrX41ul7kX4Wtg3Zdqmj3LBl6x5hY1Cds5MVl6et xj/qBCwARkSOc74ozQepZDW1mE/fP/75TcFJjfJsKZjR7zy8I1emDv/Ja0TBwFyraeN3 6nkAYSAaYyyVnKcO0J5MH4Ls/BeceZqelLtYwjFf11GyvWt/LO3ZLtdWxE7JFdePFjgE vOXIU9Hr5y+C0V9KStzhv5qmyf70pdKE9gS72UzUUJktOfLwhKtAQteKXh/VyyT1Rzwd vyK3FHZeg8xCLEg7I83f4KbylBpbfFNxG8N6v9A1ZAPztlKusWAsn9I6jOZPuOm9tpAW KwZw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:message-id:date:mime-version:from:to:subject :content-transfer-encoding; bh=FG0AmbsSXTq8aZKNtsn7qS7XkGzSZ8NL2jrigaWI+cc=; b=NiAjsM8gc/VWIMf1lVZBer7La1ph6rjNq5BwTmlxL6vcv1qMvd9vqagxZkbi7R0qil ByiA46X4mFwlvid1qU0JhJsFeiSt3urojfY9wtueAW6k3E98ox+30rJAsUgoU0g67tgV 3NLPQSyvdgKRlPDF2CsOLZAL/Iy9Y+8hotBL0IoVCM66ytagf+WDAheWGNIuxjFJZkFx kg6BCzb0lcxPq1auZHxSokTAxRZgDSuwRTvvxgM44eERMjKc23qoNlM2gUVefsmpo265 nexV7jKn5yVYC7j3IzqoNRf0J25mJMW3vsswpdoF35DJDVG2DJR9b0tFxDiN+Qda/GIZ ZdHA== X-Gm-Message-State: AOAM5315YVePrzgKDSFLNUAgBXqkuRuLUMON94Iupx5oSzhCdocK6HJl t2egJYW/GF7ADXu2BBQ6Rtzljm4llwU4Le5G X-Google-Smtp-Source: ABdhPJzObstSbscLENNm/C+3vJyNLfRtj3jIxzaOBOkXNCu3wXIhowoUiYmvG8PechJ7JKAh0zhiEw== X-Received: by 2002:adf:dcca:: with SMTP id x10mr3332298wrm.39.1624352233510; Tue, 22 Jun 2021 01:57:13 -0700 (PDT) Received: from DESKTOP-A66711V ([5.29.25.101]) by smtp.gmail.com with ESMTPSA id p11sm10799591wre.57.2021.06.22.01.57.12 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 22 Jun 2021 01:57:13 -0700 (PDT) Message-ID: <60d1a5e9.1c69fb81.7f729.b892@mx.google.com> Date: Tue, 22 Jun 2021 01:57:13 -0700 (PDT) X-Google-Original-Date: 22 Jun 2021 11:57:13 +0300 MIME-Version: 1.0 From: "Amit Klein" To: linux-kernel@vger.kernel.org, gregkh@linuxfoundation.org, stable@vger.kernel.org, edumazet@google.com, w@1wt.eu, davem@davemloft.net, netdev@vger.kernel.org Subject: [PATCH 4.9] inet: use bigger hash table for IP ID generation (backported to 4.9 and 4.4) Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org Subject: inet: use bigger hash table for IP ID generation (backported to 4.9 and 4.4) From: Amit Klein [ Upstream commit aa6dd211e4b1dde9d5dc25d699d35f789ae7eeba ] This is a backport to 4.9 and 4.4 of the following patch, originally developed by Eric Dumazet. 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 Cc: stable@vger.kernel.org Cc: Eric Dumazet Cc: Willy Tarreau --- net/ipv4/route.c | 42 +++++++++++++++++++++++++++++------------- 1 file changed, 29 insertions(+), 13 deletions(-) diff --git a/net/ipv4/route.c b/net/ipv4/route.c index e9aae46..5350e1b 100644 --- a/net/ipv4/route.c +++ b/net/ipv4/route.c @@ -70,6 +70,7 @@ #include #include #include +#include #include #include #include @@ -463,8 +464,10 @@ static struct neighbour *ipv4_neigh_lookup(const struct dst_entry *dst, return neigh_create(&arp_tbl, pkey, dev); } -#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; @@ -474,12 +477,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 = ACCESS_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 = ACCESS_ONCE(*p_tstamp); + if (old != now && cmpxchg(p_tstamp, old, now) == old) delta = prandom_u32_max(now - old); @@ -2936,18 +2943,27 @@ struct ip_rt_acct __percpu *ip_rt_acct __read_mostly; int __init ip_rt_init(void) { + void *idents_hash; int rc = 0; int cpu; - ip_idents = kmalloc(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 */ + 0, + 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); + memset(ip_tstamps, 0, (ip_idents_mask + 1) * sizeof(*ip_tstamps)); for_each_possible_cpu(cpu) { struct uncached_list *ul = &per_cpu(rt_uncached_list, cpu);