From patchwork Thu Jun 17 08:19:18 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Amit Klein X-Patchwork-Id: 462959 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.8 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 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 0009AC2B9F4 for ; Thu, 17 Jun 2021 08:19:22 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id CE6146113C for ; Thu, 17 Jun 2021 08:19:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229931AbhFQIV3 (ORCPT ); Thu, 17 Jun 2021 04:21:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37280 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229842AbhFQIV2 (ORCPT ); Thu, 17 Jun 2021 04:21:28 -0400 Received: from mail-wm1-x329.google.com (mail-wm1-x329.google.com [IPv6:2a00:1450:4864:20::329]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6F61CC061574; Thu, 17 Jun 2021 01:19:20 -0700 (PDT) Received: by mail-wm1-x329.google.com with SMTP id t11-20020a1cc30b0000b02901cec841b6a0so3785281wmf.0; Thu, 17 Jun 2021 01:19:20 -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=m23FkcTpSbNnpGC+SlBcypLFRWWqdG3+ErURqb57YnE=; b=miIM9//NWleAyVq9NBLFq/rFMnM1knoTUZ+2/qxJu/iT9OqQtkGla6HDlcHvIoES5+ TmRQauP+1eX6mRVYeAcbyYwYUXfyo6uQBlNr21y9pB8FwA3lq8fXHAcVOxev41WGEttH bZPuB9g4qEPLRfzbWwHdXny6pXJz8bJzu5vKpTN95A9Rqin8tXWiYIE04veWrggd8kT+ biJ/fDcWNe1Ovkyz7bCoqkqxJnPCEYaXZ/q/gTZLm0TO/6OYjr+VL0Vrv3oYeZXiScV9 DxRgLm7jw4SSSXdxUJvotnNimvwOOwBlCFx+z/BRYurwExKuwvSg32z3T/BkXWq8mtOC St4Q== 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=m23FkcTpSbNnpGC+SlBcypLFRWWqdG3+ErURqb57YnE=; b=hb1EizEvaQoizPOwG+9VynsXV0wLDXai9Lobf5B6lPnOQACQto8OvbPZNuHURxbuqj JNyc+9ZL7jPeL6HLNN/jUC0BrG2Y8l7fkb11gzSwtUmc9OoFhqpq1qHuKmM7I8F0mxOW byo6exEKlTVmlAe/r9tbYNdXzA0WnAbS3dqyAE8HyZ7VXqCiyN6tairWzrx1Rog2PKh0 +6GYJQd0uaWtgXciXUhESYsVMX6+6nxl7dlvqOdoC2NT56EgC2oY1AHJ74g/5qhRxUSR gtxMlUazMLARpWrcO52EdDZFsnWeozDtmsCaMZi0ZyLzfUpLHuHnjQ/jbjtfXK7Z0hUO OU9w== X-Gm-Message-State: AOAM531fVK09Dr5MkaiuZBYYkWvVTt2a6MzubJOqEkNQHzVOZamHBEK0 PjdPo3qQWDJ1umJYNMM8SeoOh9ar7vcBX4Nu X-Google-Smtp-Source: ABdhPJzMLK29L+nxWeJsp/F9zI3AW8VTwosq64Vcj8KgXQFqQjkPx7TCVss4ZExsAhmMTX+LKitd5w== X-Received: by 2002:a7b:c346:: with SMTP id l6mr3550663wmj.109.1623917958820; Thu, 17 Jun 2021 01:19:18 -0700 (PDT) Received: from DESKTOP-A66711V ([5.29.25.101]) by smtp.gmail.com with ESMTPSA id z12sm4573522wrw.97.2021.06.17.01.19.17 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 17 Jun 2021 01:19:18 -0700 (PDT) Message-ID: <60cb0586.1c69fb81.8015b.37a1@mx.google.com> Date: Thu, 17 Jun 2021 01:19:18 -0700 (PDT) X-Google-Original-Date: 17 Jun 2021 11:19:17 +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.19] inet: use bigger hash table for IP ID generation (backported to 4.19) Precedence: bulk List-ID: X-Mailing-List: stable@vger.kernel.org Subject: inet: use bigger hash table for IP ID generation (backported to 4.19) From: Amit Klein This is a backport to 4.19 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, 28 insertions(+), 14 deletions(-) (limited to 'net/ipv4/route.c') diff --git a/net/ipv4/route.c b/net/ipv4/route.c index 0470442ff61d6..ea916df1bbf5e 100644 --- a/net/ipv4/route.c +++ b/net/ipv4/route.c @@ -66,6 +66,7 @@ #include #include #include +#include #include #include #include @@ -452,8 +453,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; @@ -463,12 +466,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); @@ -3557,18 +3564,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);