hc
2024-10-22 8ac6c7a54ed1b98d142dce24b11c6de6a1e239a5
kernel/net/rxrpc/rxkad.c
....@@ -1,12 +1,8 @@
1
+// SPDX-License-Identifier: GPL-2.0-or-later
12 /* Kerberos-based RxRPC security
23 *
34 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
45 * Written by David Howells (dhowells@redhat.com)
5
- *
6
- * This program is free software; you can redistribute it and/or
7
- * modify it under the terms of the GNU General Public License
8
- * as published by the Free Software Foundation; either version
9
- * 2 of the License, or (at your option) any later version.
106 */
117
128 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
....@@ -46,7 +42,8 @@
4642 * alloc routine, but since we have it to hand, we use it to decrypt RESPONSE
4743 * packets
4844 */
49
-static struct crypto_skcipher *rxkad_ci;
45
+static struct crypto_sync_skcipher *rxkad_ci;
46
+static struct skcipher_request *rxkad_ci_req;
5047 static DEFINE_MUTEX(rxkad_ci_mutex);
5148
5249 /*
....@@ -54,7 +51,7 @@
5451 */
5552 static int rxkad_init_connection_security(struct rxrpc_connection *conn)
5653 {
57
- struct crypto_skcipher *ci;
54
+ struct crypto_sync_skcipher *ci;
5855 struct rxrpc_key_token *token;
5956 int ret;
6057
....@@ -63,14 +60,14 @@
6360 token = conn->params.key->payload.data[0];
6461 conn->security_ix = token->security_index;
6562
66
- ci = crypto_alloc_skcipher("pcbc(fcrypt)", 0, CRYPTO_ALG_ASYNC);
63
+ ci = crypto_alloc_sync_skcipher("pcbc(fcrypt)", 0, 0);
6764 if (IS_ERR(ci)) {
6865 _debug("no cipher");
6966 ret = PTR_ERR(ci);
7067 goto error;
7168 }
7269
73
- if (crypto_skcipher_setkey(ci, token->kad->session_key,
70
+ if (crypto_sync_skcipher_setkey(ci, token->kad->session_key,
7471 sizeof(token->kad->session_key)) < 0)
7572 BUG();
7673
....@@ -103,8 +100,8 @@
103100 */
104101 static int rxkad_prime_packet_security(struct rxrpc_connection *conn)
105102 {
103
+ struct skcipher_request *req;
106104 struct rxrpc_key_token *token;
107
- SKCIPHER_REQUEST_ON_STACK(req, conn->cipher);
108105 struct scatterlist sg;
109106 struct rxrpc_crypt iv;
110107 __be32 *tmpbuf;
....@@ -119,6 +116,12 @@
119116 if (!tmpbuf)
120117 return -ENOMEM;
121118
119
+ req = skcipher_request_alloc(&conn->cipher->base, GFP_NOFS);
120
+ if (!req) {
121
+ kfree(tmpbuf);
122
+ return -ENOMEM;
123
+ }
124
+
122125 token = conn->params.key->payload.data[0];
123126 memcpy(&iv, token->kad->session_key, sizeof(iv));
124127
....@@ -128,16 +131,45 @@
128131 tmpbuf[3] = htonl(conn->security_ix);
129132
130133 sg_init_one(&sg, tmpbuf, tmpsize);
131
- skcipher_request_set_tfm(req, conn->cipher);
134
+ skcipher_request_set_sync_tfm(req, conn->cipher);
132135 skcipher_request_set_callback(req, 0, NULL, NULL);
133136 skcipher_request_set_crypt(req, &sg, &sg, tmpsize, iv.x);
134137 crypto_skcipher_encrypt(req);
135
- skcipher_request_zero(req);
138
+ skcipher_request_free(req);
136139
137140 memcpy(&conn->csum_iv, tmpbuf + 2, sizeof(conn->csum_iv));
138141 kfree(tmpbuf);
139142 _leave(" = 0");
140143 return 0;
144
+}
145
+
146
+/*
147
+ * Allocate and prepare the crypto request on a call. For any particular call,
148
+ * this is called serially for the packets, so no lock should be necessary.
149
+ */
150
+static struct skcipher_request *rxkad_get_call_crypto(struct rxrpc_call *call)
151
+{
152
+ struct crypto_skcipher *tfm = &call->conn->cipher->base;
153
+ struct skcipher_request *cipher_req = call->cipher_req;
154
+
155
+ if (!cipher_req) {
156
+ cipher_req = skcipher_request_alloc(tfm, GFP_NOFS);
157
+ if (!cipher_req)
158
+ return NULL;
159
+ call->cipher_req = cipher_req;
160
+ }
161
+
162
+ return cipher_req;
163
+}
164
+
165
+/*
166
+ * Clean up the crypto on a call.
167
+ */
168
+static void rxkad_free_call_crypto(struct rxrpc_call *call)
169
+{
170
+ if (call->cipher_req)
171
+ skcipher_request_free(call->cipher_req);
172
+ call->cipher_req = NULL;
141173 }
142174
143175 /*
....@@ -167,7 +199,7 @@
167199 memset(&iv, 0, sizeof(iv));
168200
169201 sg_init_one(&sg, sechdr, 8);
170
- skcipher_request_set_tfm(req, call->conn->cipher);
202
+ skcipher_request_set_sync_tfm(req, call->conn->cipher);
171203 skcipher_request_set_callback(req, 0, NULL, NULL);
172204 skcipher_request_set_crypt(req, &sg, &sg, 8, iv.x);
173205 crypto_skcipher_encrypt(req);
....@@ -191,10 +223,8 @@
191223 struct rxrpc_skb_priv *sp;
192224 struct rxrpc_crypt iv;
193225 struct scatterlist sg[16];
194
- struct sk_buff *trailer;
195226 unsigned int len;
196227 u16 check;
197
- int nsg;
198228 int err;
199229
200230 sp = rxrpc_skb(skb);
....@@ -212,21 +242,20 @@
212242 memcpy(&iv, token->kad->session_key, sizeof(iv));
213243
214244 sg_init_one(&sg[0], sechdr, sizeof(rxkhdr));
215
- skcipher_request_set_tfm(req, call->conn->cipher);
245
+ skcipher_request_set_sync_tfm(req, call->conn->cipher);
216246 skcipher_request_set_callback(req, 0, NULL, NULL);
217247 skcipher_request_set_crypt(req, &sg[0], &sg[0], sizeof(rxkhdr), iv.x);
218248 crypto_skcipher_encrypt(req);
219249
220250 /* we want to encrypt the skbuff in-place */
221
- nsg = skb_cow_data(skb, 0, &trailer);
222
- err = -ENOMEM;
223
- if (nsg < 0 || nsg > 16)
251
+ err = -EMSGSIZE;
252
+ if (skb_shinfo(skb)->nr_frags > 16)
224253 goto out;
225254
226255 len = data_size + call->conn->size_align - 1;
227256 len &= ~(call->conn->size_align - 1);
228257
229
- sg_init_table(sg, nsg);
258
+ sg_init_table(sg, ARRAY_SIZE(sg));
230259 err = skb_to_sgvec(skb, sg, 0, len);
231260 if (unlikely(err < 0))
232261 goto out;
....@@ -250,7 +279,7 @@
250279 void *sechdr)
251280 {
252281 struct rxrpc_skb_priv *sp;
253
- SKCIPHER_REQUEST_ON_STACK(req, call->conn->cipher);
282
+ struct skcipher_request *req;
254283 struct rxrpc_crypt iv;
255284 struct scatterlist sg;
256285 u32 x, y;
....@@ -269,6 +298,10 @@
269298 if (ret < 0)
270299 return ret;
271300
301
+ req = rxkad_get_call_crypto(call);
302
+ if (!req)
303
+ return -ENOMEM;
304
+
272305 /* continue encrypting from where we left off */
273306 memcpy(&iv, call->conn->csum_iv.x, sizeof(iv));
274307
....@@ -279,7 +312,7 @@
279312 call->crypto_buf[1] = htonl(x);
280313
281314 sg_init_one(&sg, call->crypto_buf, 8);
282
- skcipher_request_set_tfm(req, call->conn->cipher);
315
+ skcipher_request_set_sync_tfm(req, call->conn->cipher);
283316 skcipher_request_set_callback(req, 0, NULL, NULL);
284317 skcipher_request_set_crypt(req, &sg, &sg, 8, iv.x);
285318 crypto_skcipher_encrypt(req);
....@@ -323,11 +356,10 @@
323356 struct rxkad_level1_hdr sechdr;
324357 struct rxrpc_crypt iv;
325358 struct scatterlist sg[16];
326
- struct sk_buff *trailer;
327359 bool aborted;
328360 u32 data_size, buf;
329361 u16 check;
330
- int nsg, ret;
362
+ int ret;
331363
332364 _enter("");
333365
....@@ -340,11 +372,7 @@
340372 /* Decrypt the skbuff in-place. TODO: We really want to decrypt
341373 * directly into the target buffer.
342374 */
343
- nsg = skb_cow_data(skb, 0, &trailer);
344
- if (nsg < 0 || nsg > 16)
345
- goto nomem;
346
-
347
- sg_init_table(sg, nsg);
375
+ sg_init_table(sg, ARRAY_SIZE(sg));
348376 ret = skb_to_sgvec(skb, sg, offset, 8);
349377 if (unlikely(ret < 0))
350378 return ret;
....@@ -352,7 +380,7 @@
352380 /* start the decryption afresh */
353381 memset(&iv, 0, sizeof(iv));
354382
355
- skcipher_request_set_tfm(req, call->conn->cipher);
383
+ skcipher_request_set_sync_tfm(req, call->conn->cipher);
356384 skcipher_request_set_callback(req, 0, NULL, NULL);
357385 skcipher_request_set_crypt(req, sg, sg, 8, iv.x);
358386 crypto_skcipher_decrypt(req);
....@@ -392,10 +420,6 @@
392420 if (aborted)
393421 rxrpc_send_abort_packet(call);
394422 return -EPROTO;
395
-
396
-nomem:
397
- _leave(" = -ENOMEM");
398
- return -ENOMEM;
399423 }
400424
401425 /*
....@@ -410,7 +434,6 @@
410434 struct rxkad_level2_hdr sechdr;
411435 struct rxrpc_crypt iv;
412436 struct scatterlist _sg[4], *sg;
413
- struct sk_buff *trailer;
414437 bool aborted;
415438 u32 data_size, buf;
416439 u16 check;
....@@ -427,12 +450,11 @@
427450 /* Decrypt the skbuff in-place. TODO: We really want to decrypt
428451 * directly into the target buffer.
429452 */
430
- nsg = skb_cow_data(skb, 0, &trailer);
431
- if (nsg < 0)
432
- goto nomem;
433
-
434453 sg = _sg;
435
- if (unlikely(nsg > 4)) {
454
+ nsg = skb_shinfo(skb)->nr_frags + 1;
455
+ if (nsg <= 4) {
456
+ nsg = 4;
457
+ } else {
436458 sg = kmalloc_array(nsg, sizeof(*sg), GFP_NOIO);
437459 if (!sg)
438460 goto nomem;
....@@ -450,7 +472,7 @@
450472 token = call->conn->params.key->payload.data[0];
451473 memcpy(&iv, token->kad->session_key, sizeof(iv));
452474
453
- skcipher_request_set_tfm(req, call->conn->cipher);
475
+ skcipher_request_set_sync_tfm(req, call->conn->cipher);
454476 skcipher_request_set_callback(req, 0, NULL, NULL);
455477 skcipher_request_set_crypt(req, sg, sg, len, iv.x);
456478 crypto_skcipher_decrypt(req);
....@@ -506,7 +528,7 @@
506528 unsigned int offset, unsigned int len,
507529 rxrpc_seq_t seq, u16 expected_cksum)
508530 {
509
- SKCIPHER_REQUEST_ON_STACK(req, call->conn->cipher);
531
+ struct skcipher_request *req;
510532 struct rxrpc_crypt iv;
511533 struct scatterlist sg;
512534 bool aborted;
....@@ -519,6 +541,10 @@
519541 if (!call->conn->cipher)
520542 return 0;
521543
544
+ req = rxkad_get_call_crypto(call);
545
+ if (!req)
546
+ return -ENOMEM;
547
+
522548 /* continue encrypting from where we left off */
523549 memcpy(&iv, call->conn->csum_iv.x, sizeof(iv));
524550
....@@ -529,7 +555,7 @@
529555 call->crypto_buf[1] = htonl(x);
530556
531557 sg_init_one(&sg, call->crypto_buf, 8);
532
- skcipher_request_set_tfm(req, call->conn->cipher);
558
+ skcipher_request_set_sync_tfm(req, call->conn->cipher);
533559 skcipher_request_set_callback(req, 0, NULL, NULL);
534560 skcipher_request_set_crypt(req, &sg, &sg, 8, iv.x);
535561 crypto_skcipher_encrypt(req);
....@@ -622,9 +648,9 @@
622648 u32 serial;
623649 int ret;
624650
625
- _enter("{%d,%x}", conn->debug_id, key_serial(conn->params.key));
651
+ _enter("{%d,%x}", conn->debug_id, key_serial(conn->server_key));
626652
627
- ret = key_validate(conn->params.key);
653
+ ret = key_validate(conn->server_key);
628654 if (ret < 0)
629655 return ret;
630656
....@@ -751,24 +777,29 @@
751777 /*
752778 * encrypt the response packet
753779 */
754
-static void rxkad_encrypt_response(struct rxrpc_connection *conn,
755
- struct rxkad_response *resp,
756
- const struct rxkad_key *s2)
780
+static int rxkad_encrypt_response(struct rxrpc_connection *conn,
781
+ struct rxkad_response *resp,
782
+ const struct rxkad_key *s2)
757783 {
758
- SKCIPHER_REQUEST_ON_STACK(req, conn->cipher);
784
+ struct skcipher_request *req;
759785 struct rxrpc_crypt iv;
760786 struct scatterlist sg[1];
787
+
788
+ req = skcipher_request_alloc(&conn->cipher->base, GFP_NOFS);
789
+ if (!req)
790
+ return -ENOMEM;
761791
762792 /* continue encrypting from where we left off */
763793 memcpy(&iv, s2->session_key, sizeof(iv));
764794
765795 sg_init_table(sg, 1);
766796 sg_set_buf(sg, &resp->encrypted, sizeof(resp->encrypted));
767
- skcipher_request_set_tfm(req, conn->cipher);
797
+ skcipher_request_set_sync_tfm(req, conn->cipher);
768798 skcipher_request_set_callback(req, 0, NULL, NULL);
769799 skcipher_request_set_crypt(req, sg, sg, sizeof(resp->encrypted), iv.x);
770800 crypto_skcipher_encrypt(req);
771
- skcipher_request_zero(req);
801
+ skcipher_request_free(req);
802
+ return 0;
772803 }
773804
774805 /*
....@@ -843,8 +874,9 @@
843874
844875 /* calculate the response checksum and then do the encryption */
845876 rxkad_calc_response_checksum(resp);
846
- rxkad_encrypt_response(conn, resp, token->kad);
847
- ret = rxkad_send_response(conn, &sp->hdr, resp, token->kad);
877
+ ret = rxkad_encrypt_response(conn, resp, token->kad);
878
+ if (ret == 0)
879
+ ret = rxkad_send_response(conn, &sp->hdr, resp, token->kad);
848880 kfree(resp);
849881 return ret;
850882
....@@ -1021,25 +1053,23 @@
10211053 struct rxkad_response *resp,
10221054 const struct rxrpc_crypt *session_key)
10231055 {
1024
- SKCIPHER_REQUEST_ON_STACK(req, rxkad_ci);
1056
+ struct skcipher_request *req = rxkad_ci_req;
10251057 struct scatterlist sg[1];
10261058 struct rxrpc_crypt iv;
10271059
10281060 _enter(",,%08x%08x",
10291061 ntohl(session_key->n[0]), ntohl(session_key->n[1]));
10301062
1031
- ASSERT(rxkad_ci != NULL);
1032
-
10331063 mutex_lock(&rxkad_ci_mutex);
1034
- if (crypto_skcipher_setkey(rxkad_ci, session_key->x,
1035
- sizeof(*session_key)) < 0)
1064
+ if (crypto_sync_skcipher_setkey(rxkad_ci, session_key->x,
1065
+ sizeof(*session_key)) < 0)
10361066 BUG();
10371067
10381068 memcpy(&iv, session_key, sizeof(iv));
10391069
10401070 sg_init_table(sg, 1);
10411071 sg_set_buf(sg, &resp->encrypted, sizeof(resp->encrypted));
1042
- skcipher_request_set_tfm(req, rxkad_ci);
1072
+ skcipher_request_set_sync_tfm(req, rxkad_ci);
10431073 skcipher_request_set_callback(req, 0, NULL, NULL);
10441074 skcipher_request_set_crypt(req, sg, sg, sizeof(resp->encrypted), iv.x);
10451075 crypto_skcipher_decrypt(req);
....@@ -1107,7 +1137,7 @@
11071137 ret = -ENOMEM;
11081138 ticket = kmalloc(ticket_len, GFP_NOFS);
11091139 if (!ticket)
1110
- goto temporary_error;
1140
+ goto temporary_error_free_resp;
11111141
11121142 eproto = tracepoint_string("rxkad_tkt_short");
11131143 abort_code = RXKADPACKETSHORT;
....@@ -1139,7 +1169,7 @@
11391169 if (response->encrypted.checksum != csum)
11401170 goto protocol_error_free;
11411171
1142
- spin_lock(&conn->channel_lock);
1172
+ spin_lock(&conn->bundle->channel_lock);
11431173 for (i = 0; i < RXRPC_MAXCALLS; i++) {
11441174 struct rxrpc_call *call;
11451175 u32 call_id = ntohl(response->encrypted.call_id[i]);
....@@ -1156,13 +1186,13 @@
11561186 if (call_id > conn->channels[i].call_counter) {
11571187 call = rcu_dereference_protected(
11581188 conn->channels[i].call,
1159
- lockdep_is_held(&conn->channel_lock));
1189
+ lockdep_is_held(&conn->bundle->channel_lock));
11601190 if (call && call->state < RXRPC_CALL_COMPLETE)
11611191 goto protocol_error_unlock;
11621192 conn->channels[i].call_counter = call_id;
11631193 }
11641194 }
1165
- spin_unlock(&conn->channel_lock);
1195
+ spin_unlock(&conn->bundle->channel_lock);
11661196
11671197 eproto = tracepoint_string("rxkad_rsp_seq");
11681198 abort_code = RXKADOUTOFSEQUENCE;
....@@ -1189,7 +1219,7 @@
11891219 return 0;
11901220
11911221 protocol_error_unlock:
1192
- spin_unlock(&conn->channel_lock);
1222
+ spin_unlock(&conn->bundle->channel_lock);
11931223 protocol_error_free:
11941224 kfree(ticket);
11951225 protocol_error:
....@@ -1200,6 +1230,7 @@
12001230
12011231 temporary_error_free_ticket:
12021232 kfree(ticket);
1233
+temporary_error_free_resp:
12031234 kfree(response);
12041235 temporary_error:
12051236 /* Ignore the response packet if we got a temporary error such as
....@@ -1217,7 +1248,7 @@
12171248 _enter("");
12181249
12191250 if (conn->cipher)
1220
- crypto_free_skcipher(conn->cipher);
1251
+ crypto_free_sync_skcipher(conn->cipher);
12211252 }
12221253
12231254 /*
....@@ -1225,10 +1256,26 @@
12251256 */
12261257 static int rxkad_init(void)
12271258 {
1259
+ struct crypto_sync_skcipher *tfm;
1260
+ struct skcipher_request *req;
1261
+
12281262 /* pin the cipher we need so that the crypto layer doesn't invoke
12291263 * keventd to go get it */
1230
- rxkad_ci = crypto_alloc_skcipher("pcbc(fcrypt)", 0, CRYPTO_ALG_ASYNC);
1231
- return PTR_ERR_OR_ZERO(rxkad_ci);
1264
+ tfm = crypto_alloc_sync_skcipher("pcbc(fcrypt)", 0, 0);
1265
+ if (IS_ERR(tfm))
1266
+ return PTR_ERR(tfm);
1267
+
1268
+ req = skcipher_request_alloc(&tfm->base, GFP_KERNEL);
1269
+ if (!req)
1270
+ goto nomem_tfm;
1271
+
1272
+ rxkad_ci_req = req;
1273
+ rxkad_ci = tfm;
1274
+ return 0;
1275
+
1276
+nomem_tfm:
1277
+ crypto_free_sync_skcipher(tfm);
1278
+ return -ENOMEM;
12321279 }
12331280
12341281 /*
....@@ -1236,8 +1283,8 @@
12361283 */
12371284 static void rxkad_exit(void)
12381285 {
1239
- if (rxkad_ci)
1240
- crypto_free_skcipher(rxkad_ci);
1286
+ crypto_free_sync_skcipher(rxkad_ci);
1287
+ skcipher_request_free(rxkad_ci_req);
12411288 }
12421289
12431290 /*
....@@ -1246,12 +1293,14 @@
12461293 const struct rxrpc_security rxkad = {
12471294 .name = "rxkad",
12481295 .security_index = RXRPC_SECURITY_RXKAD,
1296
+ .no_key_abort = RXKADUNKNOWNKEY,
12491297 .init = rxkad_init,
12501298 .exit = rxkad_exit,
12511299 .init_connection_security = rxkad_init_connection_security,
12521300 .prime_packet_security = rxkad_prime_packet_security,
12531301 .secure_packet = rxkad_secure_packet,
12541302 .verify_packet = rxkad_verify_packet,
1303
+ .free_call_crypto = rxkad_free_call_crypto,
12551304 .locate_data = rxkad_locate_data,
12561305 .issue_challenge = rxkad_issue_challenge,
12571306 .respond_to_challenge = rxkad_respond_to_challenge,