hc
2024-02-19 1c055e55a242a33e574e48be530e06770a210dcd
kernel/net/sunrpc/auth_gss/gss_krb5_mech.c
....@@ -1,3 +1,4 @@
1
+// SPDX-License-Identifier: BSD-3-Clause
12 /*
23 * linux/net/sunrpc/gss_krb5_mech.c
34 *
....@@ -6,32 +7,6 @@
67 *
78 * Andy Adamson <andros@umich.edu>
89 * J. Bruce Fields <bfields@umich.edu>
9
- *
10
- * Redistribution and use in source and binary forms, with or without
11
- * modification, are permitted provided that the following conditions
12
- * are met:
13
- *
14
- * 1. Redistributions of source code must retain the above copyright
15
- * notice, this list of conditions and the following disclaimer.
16
- * 2. Redistributions in binary form must reproduce the above copyright
17
- * notice, this list of conditions and the following disclaimer in the
18
- * documentation and/or other materials provided with the distribution.
19
- * 3. Neither the name of the University nor the names of its
20
- * contributors may be used to endorse or promote products derived
21
- * from this software without specific prior written permission.
22
- *
23
- * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
24
- * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25
- * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26
- * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
30
- * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31
- * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32
- * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34
- *
3510 */
3611
3712 #include <crypto/hash.h>
....@@ -55,6 +30,7 @@
5530 static struct gss_api_mech gss_kerberos_mech; /* forward declaration */
5631
5732 static const struct gss_krb5_enctype supported_gss_krb5_enctypes[] = {
33
+#ifndef CONFIG_SUNRPC_DISABLE_INSECURE_ENCTYPES
5834 /*
5935 * DES (All DES enctypes are mapped to the same gss functionality)
6036 */
....@@ -76,27 +52,7 @@
7652 .cksumlength = 8,
7753 .keyed_cksum = 0,
7854 },
79
- /*
80
- * RC4-HMAC
81
- */
82
- {
83
- .etype = ENCTYPE_ARCFOUR_HMAC,
84
- .ctype = CKSUMTYPE_HMAC_MD5_ARCFOUR,
85
- .name = "rc4-hmac",
86
- .encrypt_name = "ecb(arc4)",
87
- .cksum_name = "hmac(md5)",
88
- .encrypt = krb5_encrypt,
89
- .decrypt = krb5_decrypt,
90
- .mk_key = NULL,
91
- .signalg = SGN_ALG_HMAC_MD5,
92
- .sealalg = SEAL_ALG_MICROSOFT_RC4,
93
- .keybytes = 16,
94
- .keylength = 16,
95
- .blocksize = 1,
96
- .conflen = 8,
97
- .cksumlength = 8,
98
- .keyed_cksum = 1,
99
- },
55
+#endif /* CONFIG_SUNRPC_DISABLE_INSECURE_ENCTYPES */
10056 /*
10157 * 3DES
10258 */
....@@ -191,7 +147,7 @@
191147
192148 static inline const void *
193149 get_key(const void *p, const void *end,
194
- struct krb5_ctx *ctx, struct crypto_skcipher **res)
150
+ struct krb5_ctx *ctx, struct crypto_sync_skcipher **res)
195151 {
196152 struct xdr_netobj key;
197153 int alg;
....@@ -219,15 +175,14 @@
219175 if (IS_ERR(p))
220176 goto out_err;
221177
222
- *res = crypto_alloc_skcipher(ctx->gk5e->encrypt_name, 0,
223
- CRYPTO_ALG_ASYNC);
178
+ *res = crypto_alloc_sync_skcipher(ctx->gk5e->encrypt_name, 0, 0);
224179 if (IS_ERR(*res)) {
225180 printk(KERN_WARNING "gss_kerberos_mech: unable to initialize "
226181 "crypto algorithm %s\n", ctx->gk5e->encrypt_name);
227182 *res = NULL;
228183 goto out_err_free_key;
229184 }
230
- if (crypto_skcipher_setkey(*res, key.data, key.len)) {
185
+ if (crypto_sync_skcipher_setkey(*res, key.data, key.len)) {
231186 printk(KERN_WARNING "gss_kerberos_mech: error setting key for "
232187 "crypto algorithm %s\n", ctx->gk5e->encrypt_name);
233188 goto out_err_free_tfm;
....@@ -237,7 +192,7 @@
237192 return p;
238193
239194 out_err_free_tfm:
240
- crypto_free_skcipher(*res);
195
+ crypto_free_sync_skcipher(*res);
241196 out_err_free_key:
242197 kfree(key.data);
243198 p = ERR_PTR(-EINVAL);
....@@ -248,7 +203,9 @@
248203 static int
249204 gss_import_v1_context(const void *p, const void *end, struct krb5_ctx *ctx)
250205 {
206
+ u32 seq_send;
251207 int tmp;
208
+ u32 time32;
252209
253210 p = simple_get_bytes(p, end, &ctx->initiate, sizeof(ctx->initiate));
254211 if (IS_ERR(p))
....@@ -286,12 +243,15 @@
286243 p = ERR_PTR(-ENOSYS);
287244 goto out_err;
288245 }
289
- p = simple_get_bytes(p, end, &ctx->endtime, sizeof(ctx->endtime));
246
+ p = simple_get_bytes(p, end, &time32, sizeof(time32));
290247 if (IS_ERR(p))
291248 goto out_err;
292
- p = simple_get_bytes(p, end, &ctx->seq_send, sizeof(ctx->seq_send));
249
+ /* unsigned 32-bit time overflows in year 2106 */
250
+ ctx->endtime = (time64_t)time32;
251
+ p = simple_get_bytes(p, end, &seq_send, sizeof(seq_send));
293252 if (IS_ERR(p))
294253 goto out_err;
254
+ atomic_set(&ctx->seq_send, seq_send);
295255 p = simple_get_netobj(p, end, &ctx->mech_used);
296256 if (IS_ERR(p))
297257 goto out_err;
....@@ -309,30 +269,30 @@
309269 return 0;
310270
311271 out_err_free_key2:
312
- crypto_free_skcipher(ctx->seq);
272
+ crypto_free_sync_skcipher(ctx->seq);
313273 out_err_free_key1:
314
- crypto_free_skcipher(ctx->enc);
274
+ crypto_free_sync_skcipher(ctx->enc);
315275 out_err_free_mech:
316276 kfree(ctx->mech_used.data);
317277 out_err:
318278 return PTR_ERR(p);
319279 }
320280
321
-static struct crypto_skcipher *
281
+static struct crypto_sync_skcipher *
322282 context_v2_alloc_cipher(struct krb5_ctx *ctx, const char *cname, u8 *key)
323283 {
324
- struct crypto_skcipher *cp;
284
+ struct crypto_sync_skcipher *cp;
325285
326
- cp = crypto_alloc_skcipher(cname, 0, CRYPTO_ALG_ASYNC);
286
+ cp = crypto_alloc_sync_skcipher(cname, 0, 0);
327287 if (IS_ERR(cp)) {
328288 dprintk("gss_kerberos_mech: unable to initialize "
329289 "crypto algorithm %s\n", cname);
330290 return NULL;
331291 }
332
- if (crypto_skcipher_setkey(cp, key, ctx->gk5e->keylength)) {
292
+ if (crypto_sync_skcipher_setkey(cp, key, ctx->gk5e->keylength)) {
333293 dprintk("gss_kerberos_mech: error setting key for "
334294 "crypto algorithm %s\n", cname);
335
- crypto_free_skcipher(cp);
295
+ crypto_free_sync_skcipher(cp);
336296 return NULL;
337297 }
338298 return cp;
....@@ -386,86 +346,11 @@
386346 return 0;
387347
388348 out_free_enc:
389
- crypto_free_skcipher(ctx->enc);
349
+ crypto_free_sync_skcipher(ctx->enc);
390350 out_free_seq:
391
- crypto_free_skcipher(ctx->seq);
351
+ crypto_free_sync_skcipher(ctx->seq);
392352 out_err:
393353 return -EINVAL;
394
-}
395
-
396
-/*
397
- * Note that RC4 depends on deriving keys using the sequence
398
- * number or the checksum of a token. Therefore, the final keys
399
- * cannot be calculated until the token is being constructed!
400
- */
401
-static int
402
-context_derive_keys_rc4(struct krb5_ctx *ctx)
403
-{
404
- struct crypto_shash *hmac;
405
- char sigkeyconstant[] = "signaturekey";
406
- int slen = strlen(sigkeyconstant) + 1; /* include null terminator */
407
- struct shash_desc *desc;
408
- int err;
409
-
410
- dprintk("RPC: %s: entered\n", __func__);
411
- /*
412
- * derive cksum (aka Ksign) key
413
- */
414
- hmac = crypto_alloc_shash(ctx->gk5e->cksum_name, 0, 0);
415
- if (IS_ERR(hmac)) {
416
- dprintk("%s: error %ld allocating hash '%s'\n",
417
- __func__, PTR_ERR(hmac), ctx->gk5e->cksum_name);
418
- err = PTR_ERR(hmac);
419
- goto out_err;
420
- }
421
-
422
- err = crypto_shash_setkey(hmac, ctx->Ksess, ctx->gk5e->keylength);
423
- if (err)
424
- goto out_err_free_hmac;
425
-
426
-
427
- desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(hmac), GFP_NOFS);
428
- if (!desc) {
429
- dprintk("%s: failed to allocate hash descriptor for '%s'\n",
430
- __func__, ctx->gk5e->cksum_name);
431
- err = -ENOMEM;
432
- goto out_err_free_hmac;
433
- }
434
-
435
- desc->tfm = hmac;
436
- desc->flags = 0;
437
-
438
- err = crypto_shash_digest(desc, sigkeyconstant, slen, ctx->cksum);
439
- kzfree(desc);
440
- if (err)
441
- goto out_err_free_hmac;
442
- /*
443
- * allocate hash, and skciphers for data and seqnum encryption
444
- */
445
- ctx->enc = crypto_alloc_skcipher(ctx->gk5e->encrypt_name, 0,
446
- CRYPTO_ALG_ASYNC);
447
- if (IS_ERR(ctx->enc)) {
448
- err = PTR_ERR(ctx->enc);
449
- goto out_err_free_hmac;
450
- }
451
-
452
- ctx->seq = crypto_alloc_skcipher(ctx->gk5e->encrypt_name, 0,
453
- CRYPTO_ALG_ASYNC);
454
- if (IS_ERR(ctx->seq)) {
455
- crypto_free_skcipher(ctx->enc);
456
- err = PTR_ERR(ctx->seq);
457
- goto out_err_free_hmac;
458
- }
459
-
460
- dprintk("RPC: %s: returning success\n", __func__);
461
-
462
- err = 0;
463
-
464
-out_err_free_hmac:
465
- crypto_free_shash(hmac);
466
-out_err:
467
- dprintk("RPC: %s: returning %d\n", __func__, err);
468
- return err;
469354 }
470355
471356 static int
....@@ -564,7 +449,7 @@
564449 context_v2_alloc_cipher(ctx, "cbc(aes)",
565450 ctx->acceptor_seal);
566451 if (ctx->acceptor_enc_aux == NULL) {
567
- crypto_free_skcipher(ctx->initiator_enc_aux);
452
+ crypto_free_sync_skcipher(ctx->initiator_enc_aux);
568453 goto out_free_acceptor_enc;
569454 }
570455 }
....@@ -572,9 +457,9 @@
572457 return 0;
573458
574459 out_free_acceptor_enc:
575
- crypto_free_skcipher(ctx->acceptor_enc);
460
+ crypto_free_sync_skcipher(ctx->acceptor_enc);
576461 out_free_initiator_enc:
577
- crypto_free_skcipher(ctx->initiator_enc);
462
+ crypto_free_sync_skcipher(ctx->initiator_enc);
578463 out_err:
579464 return -EINVAL;
580465 }
....@@ -583,24 +468,29 @@
583468 gss_import_v2_context(const void *p, const void *end, struct krb5_ctx *ctx,
584469 gfp_t gfp_mask)
585470 {
471
+ u64 seq_send64;
586472 int keylen;
473
+ u32 time32;
587474
588475 p = simple_get_bytes(p, end, &ctx->flags, sizeof(ctx->flags));
589476 if (IS_ERR(p))
590477 goto out_err;
591478 ctx->initiate = ctx->flags & KRB5_CTX_FLAG_INITIATOR;
592479
593
- p = simple_get_bytes(p, end, &ctx->endtime, sizeof(ctx->endtime));
480
+ p = simple_get_bytes(p, end, &time32, sizeof(time32));
594481 if (IS_ERR(p))
595482 goto out_err;
596
- p = simple_get_bytes(p, end, &ctx->seq_send64, sizeof(ctx->seq_send64));
483
+ /* unsigned 32-bit time overflows in year 2106 */
484
+ ctx->endtime = (time64_t)time32;
485
+ p = simple_get_bytes(p, end, &seq_send64, sizeof(seq_send64));
597486 if (IS_ERR(p))
598487 goto out_err;
488
+ atomic64_set(&ctx->seq_send64, seq_send64);
599489 /* set seq_send for use by "older" enctypes */
600
- ctx->seq_send = ctx->seq_send64;
601
- if (ctx->seq_send64 != ctx->seq_send) {
602
- dprintk("%s: seq_send64 %lx, seq_send %x overflow?\n", __func__,
603
- (unsigned long)ctx->seq_send64, ctx->seq_send);
490
+ atomic_set(&ctx->seq_send, seq_send64);
491
+ if (seq_send64 != atomic_read(&ctx->seq_send)) {
492
+ dprintk("%s: seq_send64 %llx, seq_send %x overflow?\n", __func__,
493
+ seq_send64, atomic_read(&ctx->seq_send));
604494 p = ERR_PTR(-EINVAL);
605495 goto out_err;
606496 }
....@@ -639,8 +529,6 @@
639529 switch (ctx->enctype) {
640530 case ENCTYPE_DES3_CBC_RAW:
641531 return context_derive_keys_des3(ctx, gfp_mask);
642
- case ENCTYPE_ARCFOUR_HMAC:
643
- return context_derive_keys_rc4(ctx);
644532 case ENCTYPE_AES128_CTS_HMAC_SHA1_96:
645533 case ENCTYPE_AES256_CTS_HMAC_SHA1_96:
646534 return context_derive_keys_new(ctx, gfp_mask);
....@@ -655,7 +543,7 @@
655543 static int
656544 gss_import_sec_context_kerberos(const void *p, size_t len,
657545 struct gss_ctx *ctx_id,
658
- time_t *endtime,
546
+ time64_t *endtime,
659547 gfp_t gfp_mask)
660548 {
661549 const void *end = (const void *)((const char *)p + len);
....@@ -686,12 +574,12 @@
686574 gss_delete_sec_context_kerberos(void *internal_ctx) {
687575 struct krb5_ctx *kctx = internal_ctx;
688576
689
- crypto_free_skcipher(kctx->seq);
690
- crypto_free_skcipher(kctx->enc);
691
- crypto_free_skcipher(kctx->acceptor_enc);
692
- crypto_free_skcipher(kctx->initiator_enc);
693
- crypto_free_skcipher(kctx->acceptor_enc_aux);
694
- crypto_free_skcipher(kctx->initiator_enc_aux);
577
+ crypto_free_sync_skcipher(kctx->seq);
578
+ crypto_free_sync_skcipher(kctx->enc);
579
+ crypto_free_sync_skcipher(kctx->acceptor_enc);
580
+ crypto_free_sync_skcipher(kctx->initiator_enc);
581
+ crypto_free_sync_skcipher(kctx->acceptor_enc_aux);
582
+ crypto_free_sync_skcipher(kctx->initiator_enc_aux);
695583 kfree(kctx->mech_used.data);
696584 kfree(kctx);
697585 }