| .. | .. |
|---|
| 38 | 38 | #ifdef CONFIG_KEYS_REQUEST_CACHE |
|---|
| 39 | 39 | struct task_struct *t = current; |
|---|
| 40 | 40 | |
|---|
| 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 | + } |
|---|
| 44 | 47 | #endif |
|---|
| 45 | 48 | } |
|---|
| 46 | 49 | |
|---|
| .. | .. |
|---|
| 398 | 401 | set_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags); |
|---|
| 399 | 402 | |
|---|
| 400 | 403 | if (dest_keyring) { |
|---|
| 401 | | - ret = __key_link_lock(dest_keyring, &ctx->index_key); |
|---|
| 404 | + ret = __key_link_lock(dest_keyring, &key->index_key); |
|---|
| 402 | 405 | if (ret < 0) |
|---|
| 403 | 406 | goto link_lock_failed; |
|---|
| 404 | | - ret = __key_link_begin(dest_keyring, &ctx->index_key, &edit); |
|---|
| 405 | | - if (ret < 0) |
|---|
| 406 | | - goto link_prealloc_failed; |
|---|
| 407 | 407 | } |
|---|
| 408 | 408 | |
|---|
| 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 |
|---|
| 410 | 411 | * 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 | + */ |
|---|
| 412 | 419 | mutex_lock(&key_construction_mutex); |
|---|
| 413 | 420 | |
|---|
| 414 | 421 | rcu_read_lock(); |
|---|
| .. | .. |
|---|
| 417 | 424 | if (!IS_ERR(key_ref)) |
|---|
| 418 | 425 | goto key_already_present; |
|---|
| 419 | 426 | |
|---|
| 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; |
|---|
| 421 | 431 | __key_link(dest_keyring, key, &edit); |
|---|
| 432 | + } |
|---|
| 422 | 433 | |
|---|
| 423 | 434 | mutex_unlock(&key_construction_mutex); |
|---|
| 424 | 435 | if (dest_keyring) |
|---|
| 425 | | - __key_link_end(dest_keyring, &ctx->index_key, edit); |
|---|
| 436 | + __key_link_end(dest_keyring, &key->index_key, edit); |
|---|
| 426 | 437 | mutex_unlock(&user->cons_lock); |
|---|
| 427 | 438 | *_key = key; |
|---|
| 428 | 439 | kleave(" = 0 [%d]", key_serial(key)); |
|---|
| .. | .. |
|---|
| 435 | 446 | mutex_unlock(&key_construction_mutex); |
|---|
| 436 | 447 | key = key_ref_to_ptr(key_ref); |
|---|
| 437 | 448 | if (dest_keyring) { |
|---|
| 449 | + ret = __key_link_begin(dest_keyring, &key->index_key, &edit); |
|---|
| 450 | + if (ret < 0) |
|---|
| 451 | + goto link_alloc_failed_unlocked; |
|---|
| 438 | 452 | ret = __key_link_check_live_key(dest_keyring, key); |
|---|
| 439 | 453 | if (ret == 0) |
|---|
| 440 | 454 | __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); |
|---|
| 442 | 456 | if (ret < 0) |
|---|
| 443 | 457 | goto link_check_failed; |
|---|
| 444 | 458 | } |
|---|
| .. | .. |
|---|
| 453 | 467 | kleave(" = %d [linkcheck]", ret); |
|---|
| 454 | 468 | return ret; |
|---|
| 455 | 469 | |
|---|
| 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); |
|---|
| 458 | 474 | link_lock_failed: |
|---|
| 459 | 475 | mutex_unlock(&user->cons_lock); |
|---|
| 460 | 476 | key_put(key); |
|---|