Skip to content

Commit aa6dd21

Browse files
edumazetdavem330
authored andcommitted
inet: use bigger hash table for IP ID generation
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>
1 parent eb9da2c commit aa6dd21

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>
@@ -452,8 +453,10 @@ static void ipv4_confirm_neigh(const struct dst_entry *dst, const void *daddr)
452453
__ipv4_confirm_neigh(dev, *(__force u32 *)pkey);
453454
}
454455

455-
#define IP_IDENTS_SZ 2048u
456-
456+
/* Hash tables of size 2048..262144 depending on RAM size.
457+
* Each bucket uses 8 bytes.
458+
*/
459+
static u32 ip_idents_mask __read_mostly;
457460
static atomic_t *ip_idents __read_mostly;
458461
static u32 *ip_tstamps __read_mostly;
459462

@@ -463,12 +466,16 @@ static u32 *ip_tstamps __read_mostly;
463466
*/
464467
u32 ip_idents_reserve(u32 hash, int segs)
465468
{
466-
u32 *p_tstamp = ip_tstamps + hash % IP_IDENTS_SZ;
467-
atomic_t *p_id = ip_idents + hash % IP_IDENTS_SZ;
468-
u32 old = READ_ONCE(*p_tstamp);
469-
u32 now = (u32)jiffies;
469+
u32 bucket, old, now = (u32)jiffies;
470+
atomic_t *p_id;
471+
u32 *p_tstamp;
470472
u32 delta = 0;
471473

474+
bucket = hash & ip_idents_mask;
475+
p_tstamp = ip_tstamps + bucket;
476+
p_id = ip_idents + bucket;
477+
old = READ_ONCE(*p_tstamp);
478+
472479
if (old != now && cmpxchg(p_tstamp, old, now) == old)
473480
delta = prandom_u32_max(now - old);
474481

@@ -3557,18 +3564,25 @@ struct ip_rt_acct __percpu *ip_rt_acct __read_mostly;
35573564

35583565
int __init ip_rt_init(void)
35593566
{
3567+
void *idents_hash;
35603568
int cpu;
35613569

3562-
ip_idents = kmalloc_array(IP_IDENTS_SZ, sizeof(*ip_idents),
3563-
GFP_KERNEL);
3564-
if (!ip_idents)
3565-
panic("IP: failed to allocate ip_idents\n");
3570+
/* For modern hosts, this will use 2 MB of memory */
3571+
idents_hash = alloc_large_system_hash("IP idents",
3572+
sizeof(*ip_idents) + sizeof(*ip_tstamps),
3573+
0,
3574+
16, /* one bucket per 64 KB */
3575+
HASH_ZERO,
3576+
NULL,
3577+
&ip_idents_mask,
3578+
2048,
3579+
256*1024);
3580+
3581+
ip_idents = idents_hash;
35663582

3567-
prandom_bytes(ip_idents, IP_IDENTS_SZ * sizeof(*ip_idents));
3583+
prandom_bytes(ip_idents, (ip_idents_mask + 1) * sizeof(*ip_idents));
35683584

3569-
ip_tstamps = kcalloc(IP_IDENTS_SZ, sizeof(*ip_tstamps), GFP_KERNEL);
3570-
if (!ip_tstamps)
3571-
panic("IP: failed to allocate ip_tstamps\n");
3585+
ip_tstamps = idents_hash + (ip_idents_mask + 1) * sizeof(*ip_idents);
35723586

35733587
for_each_possible_cpu(cpu) {
35743588
struct uncached_list *ul = &per_cpu(rt_uncached_list, cpu);

0 commit comments

Comments
 (0)