inet: make exception handling less predictible
Subsystems (4)
cover inet: make exception handling less predictible
From: Eric Dumazet <[email protected]>
This second round of patches is addressing Keyu Man recommendations to make linux hosts more robust against a class of brute force attacks.
Eric Dumazet (2):
ipv6: make exception cache less predictible
ipv4: make exception cache less predictible
net/ipv4/route.c | 44 +++++++++++++++++++++++++++++--------------- net/ipv6/route.c | 5 ++++- 2 files changed, 33 insertions(+), 16 deletions(-)
Signature · 1 line
2.33.0.259.gc128427fd7-goog
PATCH 1/2 ipv6: make exception cache less predictible
From: Eric Dumazet <[email protected]>
Even after commit 4785305c05b2 ("ipv6: use siphash in rt6_exception_hash()"), 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, we do not expect this to be a problem.
Following patch is dealing with the same issue in IPv4.
--- net/ipv6/route.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
net/ipv6/route.c +4 -1
@@ -1657,6 +1657,7 @@ static int rt6_insert_exception(struct rt6_info *nrt,struct in6_addr *src_key =NULL ;struct rt6_exception *rt6_ex;struct fib6_nh *nh = res->nh; +int max_depth;int err =0 ;spin_lock_bh (&rt6_exception_lock);
@@ -1711,7 +1712,9 @@ static int rt6_insert_exception(struct rt6_info *nrt, bucket->depth++; net->ipv6.rt6_stats->fib_rt_cache++; -if (bucket->depth >FIB6_MAX_DEPTH ) +/* Randomize max depth to avoid some side channels attacks. */ + max_depth =FIB6_MAX_DEPTH +prandom_u32_max (FIB6_MAX_DEPTH ); +while (bucket->depth > max_depth)rt6_exception_remove_oldest (bucket); out:
Signature · 1 line
2.33.0.259.gc128427fd7-goog
Re: [PATCH net 1/2] ipv6: make exception cache less predictible
Quoted 20 lines · Eric Dumazet
From: Eric Dumazet <[email protected]>
Even after commit
4785305c05b2("ipv6: use siphash inrt6_exception_hash()"), 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, we do not expect this to be a problem.
Following patch is dealing with the same issue in IPv4.
Quoted 9 lines
Fixes 35732d01fe31 ("ipv6: introduce a hash table to store dst cache")Signed-off-by Eric Dumazet <[email protected]>Reported-by Keyu Man <[email protected]>Cc Wei Wang <[email protected]>Cc Martin KaFai Lau <[email protected]>--- net/ipv6/route.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
Wei Wang
Reviewed-byRe: [PATCH net 1/2] ipv6: make exception cache less predictible
Quoted 32 lines · David Ahern
Quoted 20 lines · Eric Dumazet
From: Eric Dumazet <[email protected]>
Even after commit
4785305c05b2("ipv6: use siphash inrt6_exception_hash()"), 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, we do not expect this to be a problem.
Following patch is dealing with the same issue in IPv4.
Quoted 9 lines
Fixes 35732d01fe31 ("ipv6: introduce a hash table to store dst cache")Signed-off-by Eric Dumazet <[email protected]>Reported-by Keyu Man <[email protected]>Cc Wei Wang <[email protected]>Cc Martin KaFai Lau <[email protected]>--- net/ipv6/route.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
Quoted 2 lines
Reviewed-by David Ahern <[email protected]>
Thanks Eric!
PATCH 2/2 ipv4: make exception cache less predictible
In reply to Eric DumazetFrom: Eric Dumazet <[email protected]>
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.
--- net/ipv4/route.c | 44 +++++++++++++++++++++++++++++--------------- 1 file changed, 29 insertions(+), 15 deletions(-)
net/ipv4/route.c +29 -15
@@ -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; +/* Randomize max depth to avoid some side channels attacks. */ +int max_depth =FNHE_RECLAIM_DEPTH + +prandom_u32_max (FNHE_RECLAIM_DEPTH ); - fnhe->fnhe_next = hash->chain; -rcu_assign_pointer (hash->chain, fnhe); +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.
Signature · 1 line
2.33.0.259.gc128427fd7-goog
Re: [PATCH net 2/2] ipv4: make exception cache less predictible
Quoted 27 lines · Eric Dumazet
From: Eric Dumazet <[email protected]>
Even after commit
6457378fe796("ipv4: use siphash instead of Jenkins infnhe_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 perupdate_or_create_fnhe()call, I had to replacefnhe_oldest()withfnhe_remove_oldest().Also note that we will queue extra
kfree_rcu()calls under stress, which hopefully wont be a too big issue.
Quoted 9 lines
Fixes 4895c771c7f0 ("ipv4: Add FIB nexthop exceptions.")Signed-off-by Eric Dumazet <[email protected]>Reported-by Keyu Man <[email protected]>Cc Willy Tarreau <[email protected]>Signed-off-by David S. Miller <[email protected]>--- net/ipv4/route.c | 44 +++++++++++++++++++++++++++++--------------- 1 file changed, 29 insertions(+), 15 deletions(-)
Dan Carpenter
build botRe: [PATCH net 2/2] ipv4: make exception cache less predictible
In reply to Eric DumazetCC: kbuild-all(a)lists.01.org In-Reply-To: <[email protected]> References: <[email protected]> TO: Eric Dumazet <[email protected]>…
CC: kbuild-all(a)lists.01.org
In-Reply-To: <[email protected]>
References: <[email protected]>
TO: Eric Dumazet <[email protected]>
TO: "David S . Miller" <[email protected]>
TO: Jakub Kicinski <[email protected]>
CC: netdev <[email protected]>
CC: Eric Dumazet <[email protected]>
CC: Willy Tarreau <[email protected]>
CC: Keyu Man <[email protected]>
CC: David Ahern <[email protected]>
Hi Eric,
I love your patch! Perhaps something to improve:
[auto build test WARNING on net/master]
url: https://github.com/0day-ci/linux/commits/Eric-Dumazet/inet-make-exception-handling-less-predictible/20210830-061726 base: https://git.kernel.org/pub/scm/linux/kernel/git/davem/net.git 57f780f1c43362b86fd23d20bd940e2468237716
:::::: branch date: 4 hours ago
:::::: commit date: 4 hours ago
config: x86_64-randconfig-m001-20210829 (attached as .config)
compiler: gcc-9 (Debian 9.3.0-22) 9.3.0
If you fix the issue, kindly add following tag as appropriate
smatch warnings:net/ipv4/route.c:606 fnhe_remove_oldest() error: potentially dereferencing uninitialized 'oldest_p'.
vim +/oldest_p +606 net/ipv4/route.c
2ffae99d1fac272 Timo Teräs 2013-06-27 588
adf305d00ec06cb Eric Dumazet 2021-08-29 589 static void fnhe_remove_oldest(struct fnhe_hash_bucket *hash)
4895c771c7f006b David S. Miller 2012-07-17 590 {
adf305d00ec06cb Eric Dumazet 2021-08-29 591 struct fib_nh_exception __rcu **fnhe_p, **oldest_p;
adf305d00ec06cb Eric Dumazet 2021-08-29 592 struct fib_nh_exception *fnhe, *oldest = NULL;
4895c771c7f006b David S. Miller 2012-07-17 593
adf305d00ec06cb Eric Dumazet 2021-08-29 594 for (fnhe_p = &hash->chain; ; fnhe_p = &fnhe->fnhe_next) {
adf305d00ec06cb Eric Dumazet 2021-08-29 595 fnhe = rcu_dereference_protected(*fnhe_p,
adf305d00ec06cb Eric Dumazet 2021-08-29 596 lockdep_is_held(&fnhe_lock));
adf305d00ec06cb Eric Dumazet 2021-08-29 597 if (!fnhe)
adf305d00ec06cb Eric Dumazet 2021-08-29 598 break;
adf305d00ec06cb Eric Dumazet 2021-08-29 599 if (!oldest ||
adf305d00ec06cb Eric Dumazet 2021-08-29 600 time_before(fnhe->fnhe_stamp, oldest->fnhe_stamp)) {
4895c771c7f006b David S. Miller 2012-07-17 601 oldest = fnhe;
adf305d00ec06cb Eric Dumazet 2021-08-29 602 oldest_p = fnhe_p;
adf305d00ec06cb Eric Dumazet 2021-08-29 603 }
4895c771c7f006b David S. Miller 2012-07-17 604 }
2ffae99d1fac272 Timo Teräs 2013-06-27 605 fnhe_flush_routes(oldest);
adf305d00ec06cb Eric Dumazet 2021-08-29 @606 *oldest_p = oldest->fnhe_next;
adf305d00ec06cb Eric Dumazet 2021-08-29 607 kfree_rcu(oldest, rcu);
4895c771c7f006b David S. Miller 2012-07-17 608 }4895c771c7f006b David S. Miller 2012-07-17 609
---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org
Thanks Eric and others for fixing the bug!
Keyu Man
Quoted 13 lines · Eric Dumazet
From: Eric Dumazet <[email protected]>
This second round of patches is addressing Keyu Man recommendations to make linux hosts more robust against a class of brute force attacks.
Eric Dumazet (2):
ipv6: make exception cache less predictible
ipv4: make exception cache less predictiblenet/ipv4/route.c | 44 +++++++++++++++++++++++++++++--------------- net/ipv6/route.c | 5 ++++- 2 files changed, 33 insertions(+), 16 deletions(-)