Skip to content

Commit a273c27

Browse files
edumazetgregkh
authored andcommitted
inet: use bigger hash table for IP ID generation
[ Upstream commit aa6dd21 ] In commit 73f156a ("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: 73f156a ("inetpeer: get rid of ip_id_count") Reported-by: Amit Klein <aksecurity@gmail.com> Signed-off-by: Eric Dumazet <edumazet@google.com> Cc: Willy Tarreau <w@1wt.eu> Signed-off-by: David S. Miller <davem@davemloft.net> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 6b4b3b8 commit a273c27

1 file changed

Lines changed: 28 additions & 14 deletions

File tree

‎net/ipv4/route.c‎

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
#include <linux/types.h>
6767
#include <linux/kernel.h>
6868
#include <linux/mm.h>
69+
#include <linux/memblock.h>
6970
#include <linux/string.h>
7071
#include <linux/socket.h>
7172
#include <linux/sockios.h>
@@ -476,8 +477,10 @@ static void ipv4_confirm_neigh(const struct dst_entry *dst, const void *daddr)
476477
__ipv4_confirm_neigh(dev, *(__force u32 *)pkey);
477478
}
478479

479-
#define IP_IDENTS_SZ 2048u
480-
480+
/* Hash tables of size 2048..262144 depending on RAM size.
481+
* Each bucket uses 8 bytes.
482+
*/
483+
static u32 ip_idents_mask __read_mostly;
481484
static atomic_t *ip_idents __read_mostly;
482485
static u32 *ip_tstamps __read_mostly;
483486

@@ -487,12 +490,16 @@ static u32 *ip_tstamps __read_mostly;
487490
*/
488491
u32 ip_idents_reserve(u32 hash, int segs)
489492
{
490-
u32 *p_tstamp = ip_tstamps + hash % IP_IDENTS_SZ;
491-
atomic_t *p_id = ip_idents + hash % IP_IDENTS_SZ;
492-
u32 old = READ_ONCE(*p_tstamp);
493-
u32 now = (u32)jiffies;
493+
u32 bucket, old, now = (u32)jiffies;
494+
atomic_t *p_id;
495+
u32 *p_tstamp;
494496
u32 delta = 0;
495497

498+
bucket = hash & ip_idents_mask;
499+
p_tstamp = ip_tstamps + bucket;
500+
p_id = ip_idents + bucket;
501+
old = READ_ONCE(*p_tstamp);
502+
496503
if (old != now && cmpxchg(p_tstamp, old, now) == old)
497504
delta = prandom_u32_max(now - old);
498505

@@ -3544,18 +3551,25 @@ struct ip_rt_acct __percpu *ip_rt_acct __read_mostly;
35443551

35453552
int __init ip_rt_init(void)
35463553
{
3554+
void *idents_hash;
35473555
int cpu;
35483556

3549-
ip_idents = kmalloc_array(IP_IDENTS_SZ, sizeof(*ip_idents),
3550-
GFP_KERNEL);
3551-
if (!ip_idents)
3552-
panic("IP: failed to allocate ip_idents\n");
3557+
/* For modern hosts, this will use 2 MB of memory */
3558+
idents_hash = alloc_large_system_hash("IP idents",
3559+
sizeof(*ip_idents) + sizeof(*ip_tstamps),
3560+
0,
3561+
16, /* one bucket per 64 KB */
3562+
HASH_ZERO,
3563+
NULL,
3564+
&ip_idents_mask,
3565+
2048,
3566+
256*1024);
3567+
3568+
ip_idents = idents_hash;
35533569

3554-
prandom_bytes(ip_idents, IP_IDENTS_SZ * sizeof(*ip_idents));
3570+
prandom_bytes(ip_idents, (ip_idents_mask + 1) * sizeof(*ip_idents));
35553571

3556-
ip_tstamps = kcalloc(IP_IDENTS_SZ, sizeof(*ip_tstamps), GFP_KERNEL);
3557-
if (!ip_tstamps)
3558-
panic("IP: failed to allocate ip_tstamps\n");
3572+
ip_tstamps = idents_hash + (ip_idents_mask + 1) * sizeof(*ip_idents);
35593573

35603574
for_each_possible_cpu(cpu) {
35613575
struct uncached_list *ul = &per_cpu(rt_uncached_list, cpu);

0 commit comments

Comments
 (0)