Skip to content

Commit 5867e20

Browse files
edumazetgregkh
authored andcommitted
ipv4: make exception cache less predictible
[ Upstream commit 67d6d68 ] Even after commit 6457378 ("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: 4895c77 ("ipv4: Add FIB nexthop exceptions.") Signed-off-by: Eric Dumazet <edumazet@google.com> Reported-by: Keyu Man <kman001@ucr.edu> Cc: Willy Tarreau <w@1wt.eu> Signed-off-by: David S. Miller <davem@davemloft.net> Reviewed-by: David Ahern <dsahern@kernel.org> Tested-by: David Ahern <dsahern@kernel.org> Signed-off-by: David S. Miller <davem@davemloft.net> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 8692f0b commit 5867e20

1 file changed

Lines changed: 30 additions & 16 deletions

File tree

‎net/ipv4/route.c‎

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -610,18 +610,25 @@ static void fnhe_flush_routes(struct fib_nh_exception *fnhe)
610610
}
611611
}
612612

613-
static struct fib_nh_exception *fnhe_oldest(struct fnhe_hash_bucket *hash)
613+
static void fnhe_remove_oldest(struct fnhe_hash_bucket *hash)
614614
{
615-
struct fib_nh_exception *fnhe, *oldest;
615+
struct fib_nh_exception __rcu **fnhe_p, **oldest_p;
616+
struct fib_nh_exception *fnhe, *oldest = NULL;
616617

617-
oldest = rcu_dereference(hash->chain);
618-
for (fnhe = rcu_dereference(oldest->fnhe_next); fnhe;
619-
fnhe = rcu_dereference(fnhe->fnhe_next)) {
620-
if (time_before(fnhe->fnhe_stamp, oldest->fnhe_stamp))
618+
for (fnhe_p = &hash->chain; ; fnhe_p = &fnhe->fnhe_next) {
619+
fnhe = rcu_dereference_protected(*fnhe_p,
620+
lockdep_is_held(&fnhe_lock));
621+
if (!fnhe)
622+
break;
623+
if (!oldest ||
624+
time_before(fnhe->fnhe_stamp, oldest->fnhe_stamp)) {
621625
oldest = fnhe;
626+
oldest_p = fnhe_p;
627+
}
622628
}
623629
fnhe_flush_routes(oldest);
624-
return oldest;
630+
*oldest_p = oldest->fnhe_next;
631+
kfree_rcu(oldest, rcu);
625632
}
626633

627634
static u32 fnhe_hashfun(__be32 daddr)
@@ -700,23 +707,30 @@ static void update_or_create_fnhe(struct fib_nh_common *nhc, __be32 daddr,
700707
if (rt)
701708
fill_route_from_fnhe(rt, fnhe);
702709
} else {
703-
if (depth > FNHE_RECLAIM_DEPTH)
704-
fnhe = fnhe_oldest(hash);
705-
else {
706-
fnhe = kzalloc(sizeof(*fnhe), GFP_ATOMIC);
707-
if (!fnhe)
708-
goto out_unlock;
709-
710-
fnhe->fnhe_next = hash->chain;
711-
rcu_assign_pointer(hash->chain, fnhe);
710+
/* Randomize max depth to avoid some side channels attacks. */
711+
int max_depth = FNHE_RECLAIM_DEPTH +
712+
prandom_u32_max(FNHE_RECLAIM_DEPTH);
713+
714+
while (depth > max_depth) {
715+
fnhe_remove_oldest(hash);
716+
depth--;
712717
}
718+
719+
fnhe = kzalloc(sizeof(*fnhe), GFP_ATOMIC);
720+
if (!fnhe)
721+
goto out_unlock;
722+
723+
fnhe->fnhe_next = hash->chain;
724+
713725
fnhe->fnhe_genid = genid;
714726
fnhe->fnhe_daddr = daddr;
715727
fnhe->fnhe_gw = gw;
716728
fnhe->fnhe_pmtu = pmtu;
717729
fnhe->fnhe_mtu_locked = lock;
718730
fnhe->fnhe_expires = max(1UL, expires);
719731

732+
rcu_assign_pointer(hash->chain, fnhe);
733+
720734
/* Exception created; mark the cached routes for the nexthop
721735
* stale, so anyone caching it rechecks if this exception
722736
* applies to them.

0 commit comments

Comments
 (0)