hc
2023-12-11 d2ccde1c8e90d38cee87a1b0309ad2827f3fd30d
kernel/net/core/secure_seq.c
....@@ -1,10 +1,10 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * Copyright (C) 2016 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
34 */
45
56 #include <linux/kernel.h>
67 #include <linux/init.h>
7
-#include <linux/cryptohash.h>
88 #include <linux/module.h>
99 #include <linux/cache.h>
1010 #include <linux/random.h>
....@@ -21,6 +21,8 @@
2121
2222 static siphash_key_t net_secret __read_mostly;
2323 static siphash_key_t ts_secret __read_mostly;
24
+
25
+#define EPHEMERAL_PORT_SHUFFLE_PERIOD (10 * HZ)
2426
2527 static __always_inline void net_secret_init(void)
2628 {
....@@ -62,7 +64,7 @@
6264 .daddr = *(struct in6_addr *)daddr,
6365 };
6466
65
- if (net->ipv4.sysctl_tcp_timestamps != 1)
67
+ if (READ_ONCE(net->ipv4.sysctl_tcp_timestamps) != 1)
6668 return 0;
6769
6870 ts_secret_init();
....@@ -94,17 +96,19 @@
9496 }
9597 EXPORT_SYMBOL(secure_tcpv6_seq);
9698
97
-u32 secure_ipv6_port_ephemeral(const __be32 *saddr, const __be32 *daddr,
99
+u64 secure_ipv6_port_ephemeral(const __be32 *saddr, const __be32 *daddr,
98100 __be16 dport)
99101 {
100102 const struct {
101103 struct in6_addr saddr;
102104 struct in6_addr daddr;
105
+ unsigned int timeseed;
103106 __be16 dport;
104107 } __aligned(SIPHASH_ALIGNMENT) combined = {
105108 .saddr = *(struct in6_addr *)saddr,
106109 .daddr = *(struct in6_addr *)daddr,
107
- .dport = dport
110
+ .timeseed = jiffies / EPHEMERAL_PORT_SHUFFLE_PERIOD,
111
+ .dport = dport,
108112 };
109113 net_secret_init();
110114 return siphash(&combined, offsetofend(typeof(combined), dport),
....@@ -116,7 +120,7 @@
116120 #ifdef CONFIG_INET
117121 u32 secure_tcp_ts_off(const struct net *net, __be32 saddr, __be32 daddr)
118122 {
119
- if (net->ipv4.sysctl_tcp_timestamps != 1)
123
+ if (READ_ONCE(net->ipv4.sysctl_tcp_timestamps) != 1)
120124 return 0;
121125
122126 ts_secret_init();
....@@ -142,11 +146,13 @@
142146 }
143147 EXPORT_SYMBOL_GPL(secure_tcp_seq);
144148
145
-u32 secure_ipv4_port_ephemeral(__be32 saddr, __be32 daddr, __be16 dport)
149
+u64 secure_ipv4_port_ephemeral(__be32 saddr, __be32 daddr, __be16 dport)
146150 {
147151 net_secret_init();
148
- return siphash_3u32((__force u32)saddr, (__force u32)daddr,
149
- (__force u16)dport, &net_secret);
152
+ return siphash_4u32((__force u32)saddr, (__force u32)daddr,
153
+ (__force u16)dport,
154
+ jiffies / EPHEMERAL_PORT_SHUFFLE_PERIOD,
155
+ &net_secret);
150156 }
151157 EXPORT_SYMBOL_GPL(secure_ipv4_port_ephemeral);
152158 #endif