Received: by 2002:a05:6a10:6d25:0:0:0:0 with SMTP id gq37csp1902878pxb; Mon, 13 Sep 2021 07:51:33 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyxUwZPx3rGB81ehtTK6c6S8L/49TYt3bVui1fwvglawMQyiOpmA5yS2AW+cwemEIf2UFHK X-Received: by 2002:a05:6402:186:: with SMTP id r6mr13325175edv.37.1631544693044; Mon, 13 Sep 2021 07:51:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1631544693; cv=none; d=google.com; s=arc-20160816; b=0UXEHFHCeO/qglWVn5xzt9ulMdGaLI6V54Hgzhx9ox9/wrLse+m4ILpguQ9TjscTGy d8+Af9PH1nv+ccBWJDoX6Y6NzHF3TGSZ4xD3nPt2HaheNhcTUsi84DrxUEDkfrDj83uD Ns4MaDDqBkT0QnQajHv4USu17CcOArYQcerMQtbTGQ3YNlHwGijPlm1txDImDf4rbmGh ip0yIwlhBggWnbgeeKMbnUXg/zs1/GZjzdICBQ2BGjyPZPD9lrgCZ3leCATmMrtiuNvE XBsidnNvxBM9zFvC0rb2pbwrv9QmPRP6p6jtDUTdZKkGq3bAc9uZ872k/Y57+tK3/Aoe 7Z5g== 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=HRCzrhg5UUd6TwDgJWHek1MTFd9mWAScCVaMDG2xopA=; b=Q/Gi+tvCZECjdpagm/Qlmq9DsVFxUVFasoefsmAmtLdT4xA5aQTfb5mNeohpz+Y9Dm 8VGU+u16ur0dPCM8UHDWaefQnRPeuKsz9TKEzvNcPF1WQdxhzHEu84T0QLby0e4MmLqZ 9NdIv+UdkDe3cOZSpoCYCBjdH97Aj2nyeQGM6zvfVKeAHXTZEQgZwcOQTLSIV6GmA73X 7J1bOdWw0RjIcftc2A5WLaSTS/TR3stKAicSUEsRnqWgmUeA24JS8Vih7bTF1ELUbPBk k8Rove1H/anCnIFItP1fiDk27grfd6JedxtfaFrqSZK65XHpgXMzTXCLY4izxeWDP4W6 W79Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="ZQN/i7mJ"; 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 hx5si6243947ejc.475.2021.09.13.07.51.08; Mon, 13 Sep 2021 07:51:33 -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="ZQN/i7mJ"; 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 S1347596AbhIMOte (ORCPT + 99 others); Mon, 13 Sep 2021 10:49:34 -0400 Received: from mail.kernel.org ([198.145.29.99]:58820 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1346429AbhIMOmY (ORCPT ); Mon, 13 Sep 2021 10:42:24 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id BE73360FC0; Mon, 13 Sep 2021 13:56:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1631541404; bh=ADpUUaGWT4rDmZtP+JGSTn9crAueMULCxVt0q+bOqj8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ZQN/i7mJ0JD1vUVQ0QEyGTnscLpqxvPFQsEDes1yRXOAg4erQ4+T+oX8vAzOUCQxu JP0gIl6iBz0KaznETPXuByKnetAY+rXQl91uJmUTmjvlbHFvDwlWKQzS6iui/q4qLr 5Ji9hmsIbsykWY+iSi4QUbuZ+GxG4zQtltnnclM8= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Eric Dumazet , Keyu Man , Willy Tarreau , "David S. Miller" , David Ahern , Sasha Levin Subject: [PATCH 5.14 285/334] ipv4: make exception cache less predictible Date: Mon, 13 Sep 2021 15:15:39 +0200 Message-Id: <20210913131123.057726452@linuxfoundation.org> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20210913131113.390368911@linuxfoundation.org> References: <20210913131113.390368911@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 67d6d681e15b578c1725bad8ad079e05d1c48a8e ] Even after commit 6457378fe796 ("ipv4: use siphash instead of Jenkins in fnhe_hashfun()"), an attacker can still use brute force to learn some secrets from a victim linux host. One way to defeat these attacks is to make the max depth of the hash table bucket a random value. Before this patch, each bucket of the hash table used to store exceptions could contain 6 items under attack. After the patch, each bucket would contains a random number of items, between 6 and 10. The attacker can no longer infer secrets. This is slightly increasing memory size used by the hash table, by 50% in average, we do not expect this to be a problem. This patch is more complex than the prior one (IPv6 equivalent), because IPv4 was reusing the oldest entry. Since we need to be able to evict more than one entry per update_or_create_fnhe() call, I had to replace fnhe_oldest() with fnhe_remove_oldest(). Also note that we will queue extra kfree_rcu() calls under stress, which hopefully wont be a too big issue. Fixes: 4895c771c7f0 ("ipv4: Add FIB nexthop exceptions.") Signed-off-by: Eric Dumazet Reported-by: Keyu Man Cc: Willy Tarreau Signed-off-by: David S. Miller Reviewed-by: David Ahern Tested-by: David Ahern Signed-off-by: David S. Miller Signed-off-by: Sasha Levin --- net/ipv4/route.c | 46 ++++++++++++++++++++++++++++++---------------- 1 file changed, 30 insertions(+), 16 deletions(-) diff --git a/net/ipv4/route.c b/net/ipv4/route.c index a6f20ee35335..225714b5efc0 100644 --- a/net/ipv4/route.c +++ b/net/ipv4/route.c @@ -586,18 +586,25 @@ static void fnhe_flush_routes(struct fib_nh_exception *fnhe) } } -static struct fib_nh_exception *fnhe_oldest(struct fnhe_hash_bucket *hash) +static void fnhe_remove_oldest(struct fnhe_hash_bucket *hash) { - struct fib_nh_exception *fnhe, *oldest; + struct fib_nh_exception __rcu **fnhe_p, **oldest_p; + struct fib_nh_exception *fnhe, *oldest = NULL; - oldest = rcu_dereference(hash->chain); - for (fnhe = rcu_dereference(oldest->fnhe_next); fnhe; - fnhe = rcu_dereference(fnhe->fnhe_next)) { - if (time_before(fnhe->fnhe_stamp, oldest->fnhe_stamp)) + for (fnhe_p = &hash->chain; ; fnhe_p = &fnhe->fnhe_next) { + fnhe = rcu_dereference_protected(*fnhe_p, + lockdep_is_held(&fnhe_lock)); + if (!fnhe) + break; + if (!oldest || + time_before(fnhe->fnhe_stamp, oldest->fnhe_stamp)) { oldest = fnhe; + oldest_p = fnhe_p; + } } fnhe_flush_routes(oldest); - return oldest; + *oldest_p = oldest->fnhe_next; + kfree_rcu(oldest, rcu); } static u32 fnhe_hashfun(__be32 daddr) @@ -676,16 +683,21 @@ static void update_or_create_fnhe(struct fib_nh_common *nhc, __be32 daddr, if (rt) fill_route_from_fnhe(rt, fnhe); } else { - if (depth > FNHE_RECLAIM_DEPTH) - fnhe = fnhe_oldest(hash); - else { - fnhe = kzalloc(sizeof(*fnhe), GFP_ATOMIC); - if (!fnhe) - goto out_unlock; - - fnhe->fnhe_next = hash->chain; - rcu_assign_pointer(hash->chain, fnhe); + /* Randomize max depth to avoid some side channels attacks. */ + int max_depth = FNHE_RECLAIM_DEPTH + + prandom_u32_max(FNHE_RECLAIM_DEPTH); + + while (depth > max_depth) { + fnhe_remove_oldest(hash); + depth--; } + + fnhe = kzalloc(sizeof(*fnhe), GFP_ATOMIC); + if (!fnhe) + goto out_unlock; + + fnhe->fnhe_next = hash->chain; + fnhe->fnhe_genid = genid; fnhe->fnhe_daddr = daddr; fnhe->fnhe_gw = gw; @@ -693,6 +705,8 @@ static void update_or_create_fnhe(struct fib_nh_common *nhc, __be32 daddr, fnhe->fnhe_mtu_locked = lock; fnhe->fnhe_expires = max(1UL, expires); + rcu_assign_pointer(hash->chain, fnhe); + /* Exception created; mark the cached routes for the nexthop * stale, so anyone caching it rechecks if this exception * applies to them. -- 2.30.2