History

net: ipv4: move tcp_fastopen server side code to SipHash library

Using a bare block cipher in non-crypto code is almost always a bad idea, not only for security reasons (and we've seen some examples of this in the kernel in the past), but also for performance reasons. In the TCP fastopen case, we call into the bare AES block cipher one or two times (depending on whether the connection is IPv4 or IPv6). On most systems, this results in a call chain such as crypto_cipher_encrypt_one(ctx, dst, src) crypto_cipher_crt(tfm)->cit_encrypt_one(crypto_cipher_tfm(tfm), ...); aesni_encrypt kernel_fpu_begin(); aesni_enc(ctx, dst, src); // asm routine kernel_fpu_end(); It is highly unlikely that the use of special AES instructions has a benefit in this case, especially since we are doing the above twice for IPv6 connections, instead of using a transform which can process the entire input in one go. We could switch to the cbcmac(aes) shash, which would at least get rid of the duplicated overhead in *some* cases (i.e., today, only arm64 has an accelerated implementation of cbcmac(aes), while x86 will end up using the generic cbcmac template wrapping the AES-NI cipher, which basically ends up doing exactly the above). However, in the given context, it makes more sense to use a light-weight MAC algorithm that is more suitable for the purpose at hand, such as SipHash. Since the output size of SipHash already matches our chosen value for TCP_FASTOPEN_COOKIE_SIZE, and given that it accepts arbitrary input sizes, this greatly simplifies the code as well. NOTE: Server farms backing a single server IP for load balancing purposes and sharing a single fastopen key will be adversely affected by this change unless all systems in the pool receive their kernel upgrades at the same time.

Ard Biesheuvel committed 7 years ago

Commit c681eda1 parent 6a6b5c84 files · +36 −80Browse files

Unified · Split

4 files
  1. include/linux/tcp.h +1 −6
  2. include/net/tcp.h +4 −6
  3. net/Kconfig +0 −2
  4. net/ipv4/tcp_fastopen.c +31 −66
include/linux/tcp.h+1−6
@@ -58,12 +58,7 @@ static inline unsigned int tcp_optlen(const struct sk_buff *skb) · 57 unmodified lines
5858
5959 /* TCP Fast Open Cookie as stored in memory */
6060 struct tcp_fastopen_cookie {
61− union {
62− u8 val[TCP_FASTOPEN_COOKIE_MAX];
63−#if IS_ENABLED(CONFIG_IPV6)
64− struct in6_addr addr;
65−#endif
66− };
61+ u64 val[TCP_FASTOPEN_COOKIE_MAX / sizeof(u64)];
6762 s8 len;
6863 bool exp; /* In RFC6994 experimental option format */
6964 };
include/net/tcp.h+4−6
@@ -1628,9 +1628,9 @@ bool tcp_fastopen_defer_connect(struct sock *sk, int *err); · 1627 unmodified lines
16281628
16291629 /* Fastopen key context */
16301630 struct tcp_fastopen_context {
1631− struct crypto_cipher *tfm[TCP_FASTOPEN_KEY_MAX];
1632− __u8 key[TCP_FASTOPEN_KEY_BUF_LENGTH];
1633− struct rcu_head rcu;
1631+ __u8 key[TCP_FASTOPEN_KEY_MAX][TCP_FASTOPEN_KEY_LENGTH];
1632+ int num;
1633+ struct rcu_head rcu;
16341634 };
16351635
16361636 extern unsigned int sysctl_tcp_fastopen_blackhole_timeout;
@@ -1665,9 +1665,7 @@ bool tcp_fastopen_cookie_match(const struct tcp_fastopen_cookie *foc, · 28 unmodified lines
16651665 static inline
16661666 int tcp_fastopen_context_len(const struct tcp_fastopen_context *ctx)
16671667 {
1668− if (ctx->tfm[1])
1669− return 2;
1670− return 1;
1668+ return ctx->num;
16711669 }
16721670
16731671 /* Latencies incurred by various limits for a sender. They are
net/Kconfig+0−2
@@ -67,8 +67,6 @@ source "net/xdp/Kconfig" · 66 unmodified lines
6767
6868 config INET
6969 bool "TCP/IP networking"
70− select CRYPTO
71− select CRYPTO_AES
7270 ---help---
7371 These are the protocols used on the Internet and on most local
7472 Ethernets. It is highly recommended to say Y here (this will enlarge
net/ipv4/tcp_fastopen.c+31−66
@@ -7,6 +7,7 @@ · 6 unmodified lines
77 #include <linux/tcp.h>
88 #include <linux/rcupdate.h>
99 #include <linux/rculist.h>
10+#include <linux/siphash.h>
1011 #include <net/inetpeer.h>
1112 #include <net/tcp.h>
1213
@@ -37,14 +38,8 @@ static void tcp_fastopen_ctx_free(struct rcu_head *head) · 24 unmodified lines
3738 {
3839 struct tcp_fastopen_context *ctx =
3940 container_of(head, struct tcp_fastopen_context, rcu);
40− int i;
4141
42− /* We own ctx, thus no need to hold the Fastopen-lock */
43− for (i = 0; i < TCP_FASTOPEN_KEY_MAX; i++) {
44− if (ctx->tfm[i])
45− crypto_free_cipher(ctx->tfm[i]);
46− }
47− kfree(ctx);
42+ kzfree(ctx);
4843 }
4944
5045 void tcp_fastopen_destroy_cipher(struct sock *sk)
@@ -72,41 +67,6 @@ void tcp_fastopen_ctx_destroy(struct net *net) · 21 unmodified lines
7267 call_rcu(&ctxt->rcu, tcp_fastopen_ctx_free);
7368 }
7469
75−static struct tcp_fastopen_context *tcp_fastopen_alloc_ctx(void *primary_key,
76− void *backup_key,
77− unsigned int len)
78−{
79− struct tcp_fastopen_context *new_ctx;
80− void *key = primary_key;
81− int err, i;
82−
83− new_ctx = kmalloc(sizeof(*new_ctx), GFP_KERNEL);
84− if (!new_ctx)
85− return ERR_PTR(-ENOMEM);
86− for (i = 0; i < TCP_FASTOPEN_KEY_MAX; i++)
87− new_ctx->tfm[i] = NULL;
88− for (i = 0; i < (backup_key ? 2 : 1); i++) {
89− new_ctx->tfm[i] = crypto_alloc_cipher("aes", 0, 0);
90− if (IS_ERR(new_ctx->tfm[i])) {
91− err = PTR_ERR(new_ctx->tfm[i]);
92− new_ctx->tfm[i] = NULL;
93− pr_err("TCP: TFO aes cipher alloc error: %d\n", err);
94− goto out;
95− }
96− err = crypto_cipher_setkey(new_ctx->tfm[i], key, len);
97− if (err) {
98− pr_err("TCP: TFO cipher key error: %d\n", err);
99− goto out;
100− }
101− memcpy(&new_ctx->key[i * TCP_FASTOPEN_KEY_LENGTH], key, len);
102− key = backup_key;
103− }
104− return new_ctx;
105−out:
106− tcp_fastopen_ctx_free(&new_ctx->rcu);
107− return ERR_PTR(err);
108−}
109−
11070 int tcp_fastopen_reset_cipher(struct net *net, struct sock *sk,
11171 void *primary_key, void *backup_key,
11272 unsigned int len)
@@ -115,11 +75,20 @@ int tcp_fastopen_reset_cipher(struct net *net, struct sock *sk, · 2 unmodified lines
11575 struct fastopen_queue *q;
11676 int err = 0;
11777
118− ctx = tcp_fastopen_alloc_ctx(primary_key, backup_key, len);
119− if (IS_ERR(ctx)) {
120− err = PTR_ERR(ctx);
78+ ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
79+ if (!ctx) {
80+ err = -ENOMEM;
12181 goto out;
12282 }
83+
84+ memcpy(ctx->key[0], primary_key, len);
85+ if (backup_key) {
86+ memcpy(ctx->key[1], backup_key, len);
87+ ctx->num = 2;
88+ } else {
89+ ctx->num = 1;
90+ }
91+
12392 spin_lock(&net->ipv4.tcp_fastopen_ctx_lock);
12493 if (sk) {
12594 q = &inet_csk(sk)->icsk_accept_queue.fastopenq;
@@ -141,31 +110,30 @@ out: · 15 unmodified lines
141110
142111 static bool __tcp_fastopen_cookie_gen_cipher(struct request_sock *req,
143112 struct sk_buff *syn,
144− struct crypto_cipher *tfm,
113+ const u8 *key,
145114 struct tcp_fastopen_cookie *foc)
146115 {
116+ BUILD_BUG_ON(TCP_FASTOPEN_KEY_LENGTH != sizeof(siphash_key_t));
117+ BUILD_BUG_ON(TCP_FASTOPEN_COOKIE_SIZE != sizeof(u64));
118+
147119 if (req->rsk_ops->family == AF_INET) {
148120 const struct iphdr *iph = ip_hdr(syn);
149− __be32 path[4] = { iph->saddr, iph->daddr, 0, 0 };
150121
151− crypto_cipher_encrypt_one(tfm, foc->val, (void *)path);
122+ foc->val[0] = siphash(&iph->saddr,
123+ sizeof(iph->saddr) +
124+ sizeof(iph->daddr),
125+ (const siphash_key_t *)key);
152126 foc->len = TCP_FASTOPEN_COOKIE_SIZE;
153127 return true;
154128 }
155−
156129 #if IS_ENABLED(CONFIG_IPV6)
157130 if (req->rsk_ops->family == AF_INET6) {
158131 const struct ipv6hdr *ip6h = ipv6_hdr(syn);
159− struct tcp_fastopen_cookie tmp;
160− struct in6_addr *buf;
161− int i;
162−
163− crypto_cipher_encrypt_one(tfm, tmp.val,
164− (void *)&ip6h->saddr);
165− buf = &tmp.addr;
166− for (i = 0; i < 4; i++)
167− buf->s6_addr32[i] ^= ip6h->daddr.s6_addr32[i];
168− crypto_cipher_encrypt_one(tfm, foc->val, (void *)buf);
132+
133+ foc->val[0] = siphash(&ip6h->saddr,
134+ sizeof(ip6h->saddr) +
135+ sizeof(ip6h->daddr),
136+ (const siphash_key_t *)key);
169137 foc->len = TCP_FASTOPEN_COOKIE_SIZE;
170138 return true;
171139 }
@@ -173,11 +141,8 @@ static bool __tcp_fastopen_cookie_gen_cipher(struct request_sock *req, · 1 unmodified line
173141 return false;
174142 }
175143
176−/* Generate the fastopen cookie by doing aes128 encryption on both
177− * the source and destination addresses. Pad 0s for IPv4 or IPv4-mapped-IPv6
178− * addresses. For the longer IPv6 addresses use CBC-MAC.
179− *
180− * XXX (TFO) - refactor when TCP_FASTOPEN_COOKIE_SIZE != AES_BLOCK_SIZE.
144+/* Generate the fastopen cookie by applying SipHash to both the source and
145+ * destination addresses.
181146 */
182147 static void tcp_fastopen_cookie_gen(struct sock *sk,
183148 struct request_sock *req,
@@ -189,7 +154,7 @@ static void tcp_fastopen_cookie_gen(struct sock *sk, · 5 unmodified lines
189154 rcu_read_lock();
190155 ctx = tcp_fastopen_get_ctx(sk);
191156 if (ctx)
192− __tcp_fastopen_cookie_gen_cipher(req, syn, ctx->tfm[0], foc);
157+ __tcp_fastopen_cookie_gen_cipher(req, syn, ctx->key[0], foc);
193158 rcu_read_unlock();
194159 }
195160
@@ -253,7 +218,7 @@ static int tcp_fastopen_cookie_gen_check(struct sock *sk, · 57 unmodified lines
253218 if (!ctx)
254219 goto out;
255220 for (i = 0; i < tcp_fastopen_context_len(ctx); i++) {
256− __tcp_fastopen_cookie_gen_cipher(req, syn, ctx->tfm[i], foc);
221+ __tcp_fastopen_cookie_gen_cipher(req, syn, ctx->key[i], foc);
257222 if (tcp_fastopen_cookie_match(foc, orig)) {
258223 ret = i + 1;
259224 goto out;

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t