hc
2024-12-19 9370bb92b2d16684ee45cf24e879c93c509162da
kernel/security/keys/request_key.c
....@@ -38,9 +38,12 @@
3838 #ifdef CONFIG_KEYS_REQUEST_CACHE
3939 struct task_struct *t = current;
4040
41
- key_put(t->cached_requested_key);
42
- t->cached_requested_key = key_get(key);
43
- set_tsk_thread_flag(t, TIF_NOTIFY_RESUME);
41
+ /* Do not cache key if it is a kernel thread */
42
+ if (!(t->flags & PF_KTHREAD)) {
43
+ key_put(t->cached_requested_key);
44
+ t->cached_requested_key = key_get(key);
45
+ set_tsk_thread_flag(t, TIF_NOTIFY_RESUME);
46
+ }
4447 #endif
4548 }
4649
....@@ -398,17 +401,21 @@
398401 set_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags);
399402
400403 if (dest_keyring) {
401
- ret = __key_link_lock(dest_keyring, &ctx->index_key);
404
+ ret = __key_link_lock(dest_keyring, &key->index_key);
402405 if (ret < 0)
403406 goto link_lock_failed;
404
- ret = __key_link_begin(dest_keyring, &ctx->index_key, &edit);
405
- if (ret < 0)
406
- goto link_prealloc_failed;
407407 }
408408
409
- /* attach the key to the destination keyring under lock, but we do need
409
+ /*
410
+ * Attach the key to the destination keyring under lock, but we do need
410411 * to do another check just in case someone beat us to it whilst we
411
- * waited for locks */
412
+ * waited for locks.
413
+ *
414
+ * The caller might specify a comparison function which looks for keys
415
+ * that do not exactly match but are still equivalent from the caller's
416
+ * perspective. The __key_link_begin() operation must be done only after
417
+ * an actual key is determined.
418
+ */
412419 mutex_lock(&key_construction_mutex);
413420
414421 rcu_read_lock();
....@@ -417,12 +424,16 @@
417424 if (!IS_ERR(key_ref))
418425 goto key_already_present;
419426
420
- if (dest_keyring)
427
+ if (dest_keyring) {
428
+ ret = __key_link_begin(dest_keyring, &key->index_key, &edit);
429
+ if (ret < 0)
430
+ goto link_alloc_failed;
421431 __key_link(dest_keyring, key, &edit);
432
+ }
422433
423434 mutex_unlock(&key_construction_mutex);
424435 if (dest_keyring)
425
- __key_link_end(dest_keyring, &ctx->index_key, edit);
436
+ __key_link_end(dest_keyring, &key->index_key, edit);
426437 mutex_unlock(&user->cons_lock);
427438 *_key = key;
428439 kleave(" = 0 [%d]", key_serial(key));
....@@ -435,10 +446,13 @@
435446 mutex_unlock(&key_construction_mutex);
436447 key = key_ref_to_ptr(key_ref);
437448 if (dest_keyring) {
449
+ ret = __key_link_begin(dest_keyring, &key->index_key, &edit);
450
+ if (ret < 0)
451
+ goto link_alloc_failed_unlocked;
438452 ret = __key_link_check_live_key(dest_keyring, key);
439453 if (ret == 0)
440454 __key_link(dest_keyring, key, &edit);
441
- __key_link_end(dest_keyring, &ctx->index_key, edit);
455
+ __key_link_end(dest_keyring, &key->index_key, edit);
442456 if (ret < 0)
443457 goto link_check_failed;
444458 }
....@@ -453,8 +467,10 @@
453467 kleave(" = %d [linkcheck]", ret);
454468 return ret;
455469
456
-link_prealloc_failed:
457
- __key_link_end(dest_keyring, &ctx->index_key, edit);
470
+link_alloc_failed:
471
+ mutex_unlock(&key_construction_mutex);
472
+link_alloc_failed_unlocked:
473
+ __key_link_end(dest_keyring, &key->index_key, edit);
458474 link_lock_failed:
459475 mutex_unlock(&user->cons_lock);
460476 key_put(key);