0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #include <linux/export.h>
0011 #include <linux/sched.h>
0012 #include <linux/kmod.h>
0013 #include <linux/err.h>
0014 #include <linux/keyctl.h>
0015 #include <linux/slab.h>
0016 #include <net/net_namespace.h>
0017 #include "internal.h"
0018 #include <keys/request_key_auth-type.h>
0019
0020 #define key_negative_timeout 60
0021
0022 static struct key *check_cached_key(struct keyring_search_context *ctx)
0023 {
0024 #ifdef CONFIG_KEYS_REQUEST_CACHE
0025 struct key *key = current->cached_requested_key;
0026
0027 if (key &&
0028 ctx->match_data.cmp(key, &ctx->match_data) &&
0029 !(key->flags & ((1 << KEY_FLAG_INVALIDATED) |
0030 (1 << KEY_FLAG_REVOKED))))
0031 return key_get(key);
0032 #endif
0033 return NULL;
0034 }
0035
0036 static void cache_requested_key(struct key *key)
0037 {
0038 #ifdef CONFIG_KEYS_REQUEST_CACHE
0039 struct task_struct *t = current;
0040
0041 key_put(t->cached_requested_key);
0042 t->cached_requested_key = key_get(key);
0043 set_tsk_thread_flag(t, TIF_NOTIFY_RESUME);
0044 #endif
0045 }
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056 void complete_request_key(struct key *authkey, int error)
0057 {
0058 struct request_key_auth *rka = get_request_key_auth(authkey);
0059 struct key *key = rka->target_key;
0060
0061 kenter("%d{%d},%d", authkey->serial, key->serial, error);
0062
0063 if (error < 0)
0064 key_negate_and_link(key, key_negative_timeout, NULL, authkey);
0065 else
0066 key_revoke(authkey);
0067 }
0068 EXPORT_SYMBOL(complete_request_key);
0069
0070
0071
0072
0073
0074
0075
0076
0077 static int umh_keys_init(struct subprocess_info *info, struct cred *cred)
0078 {
0079 struct key *keyring = info->data;
0080
0081 return install_session_keyring_to_cred(cred, keyring);
0082 }
0083
0084
0085
0086
0087 static void umh_keys_cleanup(struct subprocess_info *info)
0088 {
0089 struct key *keyring = info->data;
0090 key_put(keyring);
0091 }
0092
0093
0094
0095
0096 static int call_usermodehelper_keys(const char *path, char **argv, char **envp,
0097 struct key *session_keyring, int wait)
0098 {
0099 struct subprocess_info *info;
0100
0101 info = call_usermodehelper_setup(path, argv, envp, GFP_KERNEL,
0102 umh_keys_init, umh_keys_cleanup,
0103 session_keyring);
0104 if (!info)
0105 return -ENOMEM;
0106
0107 key_get(session_keyring);
0108 return call_usermodehelper_exec(info, wait);
0109 }
0110
0111
0112
0113
0114
0115 static int call_sbin_request_key(struct key *authkey, void *aux)
0116 {
0117 static char const request_key[] = "/sbin/request-key";
0118 struct request_key_auth *rka = get_request_key_auth(authkey);
0119 const struct cred *cred = current_cred();
0120 key_serial_t prkey, sskey;
0121 struct key *key = rka->target_key, *keyring, *session, *user_session;
0122 char *argv[9], *envp[3], uid_str[12], gid_str[12];
0123 char key_str[12], keyring_str[3][12];
0124 char desc[20];
0125 int ret, i;
0126
0127 kenter("{%d},{%d},%s", key->serial, authkey->serial, rka->op);
0128
0129 ret = look_up_user_keyrings(NULL, &user_session);
0130 if (ret < 0)
0131 goto error_us;
0132
0133
0134 sprintf(desc, "_req.%u", key->serial);
0135
0136 cred = get_current_cred();
0137 keyring = keyring_alloc(desc, cred->fsuid, cred->fsgid, cred,
0138 KEY_POS_ALL | KEY_USR_VIEW | KEY_USR_READ,
0139 KEY_ALLOC_QUOTA_OVERRUN, NULL, NULL);
0140 put_cred(cred);
0141 if (IS_ERR(keyring)) {
0142 ret = PTR_ERR(keyring);
0143 goto error_alloc;
0144 }
0145
0146
0147 ret = key_link(keyring, authkey);
0148 if (ret < 0)
0149 goto error_link;
0150
0151
0152 sprintf(uid_str, "%d", from_kuid(&init_user_ns, cred->fsuid));
0153 sprintf(gid_str, "%d", from_kgid(&init_user_ns, cred->fsgid));
0154
0155
0156 sprintf(key_str, "%d", key->serial);
0157
0158
0159 sprintf(keyring_str[0], "%d",
0160 cred->thread_keyring ? cred->thread_keyring->serial : 0);
0161
0162 prkey = 0;
0163 if (cred->process_keyring)
0164 prkey = cred->process_keyring->serial;
0165 sprintf(keyring_str[1], "%d", prkey);
0166
0167 session = cred->session_keyring;
0168 if (!session)
0169 session = user_session;
0170 sskey = session->serial;
0171
0172 sprintf(keyring_str[2], "%d", sskey);
0173
0174
0175 i = 0;
0176 envp[i++] = "HOME=/";
0177 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
0178 envp[i] = NULL;
0179
0180
0181 i = 0;
0182 argv[i++] = (char *)request_key;
0183 argv[i++] = (char *)rka->op;
0184 argv[i++] = key_str;
0185 argv[i++] = uid_str;
0186 argv[i++] = gid_str;
0187 argv[i++] = keyring_str[0];
0188 argv[i++] = keyring_str[1];
0189 argv[i++] = keyring_str[2];
0190 argv[i] = NULL;
0191
0192
0193 ret = call_usermodehelper_keys(request_key, argv, envp, keyring,
0194 UMH_WAIT_PROC);
0195 kdebug("usermode -> 0x%x", ret);
0196 if (ret >= 0) {
0197
0198 if (test_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags) ||
0199 key_validate(key) < 0)
0200 ret = -ENOKEY;
0201 else
0202
0203
0204 ret = 0;
0205 }
0206
0207 error_link:
0208 key_put(keyring);
0209
0210 error_alloc:
0211 key_put(user_session);
0212 error_us:
0213 complete_request_key(authkey, ret);
0214 kleave(" = %d", ret);
0215 return ret;
0216 }
0217
0218
0219
0220
0221
0222
0223 static int construct_key(struct key *key, const void *callout_info,
0224 size_t callout_len, void *aux,
0225 struct key *dest_keyring)
0226 {
0227 request_key_actor_t actor;
0228 struct key *authkey;
0229 int ret;
0230
0231 kenter("%d,%p,%zu,%p", key->serial, callout_info, callout_len, aux);
0232
0233
0234 authkey = request_key_auth_new(key, "create", callout_info, callout_len,
0235 dest_keyring);
0236 if (IS_ERR(authkey))
0237 return PTR_ERR(authkey);
0238
0239
0240 actor = call_sbin_request_key;
0241 if (key->type->request_key)
0242 actor = key->type->request_key;
0243
0244 ret = actor(authkey, aux);
0245
0246
0247
0248 WARN_ON(ret < 0 &&
0249 !test_bit(KEY_FLAG_INVALIDATED, &authkey->flags));
0250
0251 key_put(authkey);
0252 kleave(" = %d", ret);
0253 return ret;
0254 }
0255
0256
0257
0258
0259
0260
0261
0262 static int construct_get_dest_keyring(struct key **_dest_keyring)
0263 {
0264 struct request_key_auth *rka;
0265 const struct cred *cred = current_cred();
0266 struct key *dest_keyring = *_dest_keyring, *authkey;
0267 int ret;
0268
0269 kenter("%p", dest_keyring);
0270
0271
0272 if (dest_keyring) {
0273
0274 key_get(dest_keyring);
0275 } else {
0276 bool do_perm_check = true;
0277
0278
0279
0280 switch (cred->jit_keyring) {
0281 case KEY_REQKEY_DEFL_DEFAULT:
0282 case KEY_REQKEY_DEFL_REQUESTOR_KEYRING:
0283 if (cred->request_key_auth) {
0284 authkey = cred->request_key_auth;
0285 down_read(&authkey->sem);
0286 rka = get_request_key_auth(authkey);
0287 if (!test_bit(KEY_FLAG_REVOKED,
0288 &authkey->flags))
0289 dest_keyring =
0290 key_get(rka->dest_keyring);
0291 up_read(&authkey->sem);
0292 if (dest_keyring) {
0293 do_perm_check = false;
0294 break;
0295 }
0296 }
0297
0298 fallthrough;
0299 case KEY_REQKEY_DEFL_THREAD_KEYRING:
0300 dest_keyring = key_get(cred->thread_keyring);
0301 if (dest_keyring)
0302 break;
0303
0304 fallthrough;
0305 case KEY_REQKEY_DEFL_PROCESS_KEYRING:
0306 dest_keyring = key_get(cred->process_keyring);
0307 if (dest_keyring)
0308 break;
0309
0310 fallthrough;
0311 case KEY_REQKEY_DEFL_SESSION_KEYRING:
0312 dest_keyring = key_get(cred->session_keyring);
0313
0314 if (dest_keyring)
0315 break;
0316
0317 fallthrough;
0318 case KEY_REQKEY_DEFL_USER_SESSION_KEYRING:
0319 ret = look_up_user_keyrings(NULL, &dest_keyring);
0320 if (ret < 0)
0321 return ret;
0322 break;
0323
0324 case KEY_REQKEY_DEFL_USER_KEYRING:
0325 ret = look_up_user_keyrings(&dest_keyring, NULL);
0326 if (ret < 0)
0327 return ret;
0328 break;
0329
0330 case KEY_REQKEY_DEFL_GROUP_KEYRING:
0331 default:
0332 BUG();
0333 }
0334
0335
0336
0337
0338
0339
0340
0341
0342
0343
0344 if (dest_keyring && do_perm_check) {
0345 ret = key_permission(make_key_ref(dest_keyring, 1),
0346 KEY_NEED_WRITE);
0347 if (ret) {
0348 key_put(dest_keyring);
0349 return ret;
0350 }
0351 }
0352 }
0353
0354 *_dest_keyring = dest_keyring;
0355 kleave(" [dk %d]", key_serial(dest_keyring));
0356 return 0;
0357 }
0358
0359
0360
0361
0362
0363
0364
0365
0366 static int construct_alloc_key(struct keyring_search_context *ctx,
0367 struct key *dest_keyring,
0368 unsigned long flags,
0369 struct key_user *user,
0370 struct key **_key)
0371 {
0372 struct assoc_array_edit *edit = NULL;
0373 struct key *key;
0374 key_perm_t perm;
0375 key_ref_t key_ref;
0376 int ret;
0377
0378 kenter("%s,%s,,,",
0379 ctx->index_key.type->name, ctx->index_key.description);
0380
0381 *_key = NULL;
0382 mutex_lock(&user->cons_lock);
0383
0384 perm = KEY_POS_VIEW | KEY_POS_SEARCH | KEY_POS_LINK | KEY_POS_SETATTR;
0385 perm |= KEY_USR_VIEW;
0386 if (ctx->index_key.type->read)
0387 perm |= KEY_POS_READ;
0388 if (ctx->index_key.type == &key_type_keyring ||
0389 ctx->index_key.type->update)
0390 perm |= KEY_POS_WRITE;
0391
0392 key = key_alloc(ctx->index_key.type, ctx->index_key.description,
0393 ctx->cred->fsuid, ctx->cred->fsgid, ctx->cred,
0394 perm, flags, NULL);
0395 if (IS_ERR(key))
0396 goto alloc_failed;
0397
0398 set_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags);
0399
0400 if (dest_keyring) {
0401 ret = __key_link_lock(dest_keyring, &ctx->index_key);
0402 if (ret < 0)
0403 goto link_lock_failed;
0404 ret = __key_link_begin(dest_keyring, &ctx->index_key, &edit);
0405 if (ret < 0)
0406 goto link_prealloc_failed;
0407 }
0408
0409
0410
0411
0412 mutex_lock(&key_construction_mutex);
0413
0414 rcu_read_lock();
0415 key_ref = search_process_keyrings_rcu(ctx);
0416 rcu_read_unlock();
0417 if (!IS_ERR(key_ref))
0418 goto key_already_present;
0419
0420 if (dest_keyring)
0421 __key_link(dest_keyring, key, &edit);
0422
0423 mutex_unlock(&key_construction_mutex);
0424 if (dest_keyring)
0425 __key_link_end(dest_keyring, &ctx->index_key, edit);
0426 mutex_unlock(&user->cons_lock);
0427 *_key = key;
0428 kleave(" = 0 [%d]", key_serial(key));
0429 return 0;
0430
0431
0432
0433 key_already_present:
0434 key_put(key);
0435 mutex_unlock(&key_construction_mutex);
0436 key = key_ref_to_ptr(key_ref);
0437 if (dest_keyring) {
0438 ret = __key_link_check_live_key(dest_keyring, key);
0439 if (ret == 0)
0440 __key_link(dest_keyring, key, &edit);
0441 __key_link_end(dest_keyring, &ctx->index_key, edit);
0442 if (ret < 0)
0443 goto link_check_failed;
0444 }
0445 mutex_unlock(&user->cons_lock);
0446 *_key = key;
0447 kleave(" = -EINPROGRESS [%d]", key_serial(key));
0448 return -EINPROGRESS;
0449
0450 link_check_failed:
0451 mutex_unlock(&user->cons_lock);
0452 key_put(key);
0453 kleave(" = %d [linkcheck]", ret);
0454 return ret;
0455
0456 link_prealloc_failed:
0457 __key_link_end(dest_keyring, &ctx->index_key, edit);
0458 link_lock_failed:
0459 mutex_unlock(&user->cons_lock);
0460 key_put(key);
0461 kleave(" = %d [prelink]", ret);
0462 return ret;
0463
0464 alloc_failed:
0465 mutex_unlock(&user->cons_lock);
0466 kleave(" = %ld", PTR_ERR(key));
0467 return PTR_ERR(key);
0468 }
0469
0470
0471
0472
0473 static struct key *construct_key_and_link(struct keyring_search_context *ctx,
0474 const char *callout_info,
0475 size_t callout_len,
0476 void *aux,
0477 struct key *dest_keyring,
0478 unsigned long flags)
0479 {
0480 struct key_user *user;
0481 struct key *key;
0482 int ret;
0483
0484 kenter("");
0485
0486 if (ctx->index_key.type == &key_type_keyring)
0487 return ERR_PTR(-EPERM);
0488
0489 ret = construct_get_dest_keyring(&dest_keyring);
0490 if (ret)
0491 goto error;
0492
0493 user = key_user_lookup(current_fsuid());
0494 if (!user) {
0495 ret = -ENOMEM;
0496 goto error_put_dest_keyring;
0497 }
0498
0499 ret = construct_alloc_key(ctx, dest_keyring, flags, user, &key);
0500 key_user_put(user);
0501
0502 if (ret == 0) {
0503 ret = construct_key(key, callout_info, callout_len, aux,
0504 dest_keyring);
0505 if (ret < 0) {
0506 kdebug("cons failed");
0507 goto construction_failed;
0508 }
0509 } else if (ret == -EINPROGRESS) {
0510 ret = 0;
0511 } else {
0512 goto error_put_dest_keyring;
0513 }
0514
0515 key_put(dest_keyring);
0516 kleave(" = key %d", key_serial(key));
0517 return key;
0518
0519 construction_failed:
0520 key_negate_and_link(key, key_negative_timeout, NULL, NULL);
0521 key_put(key);
0522 error_put_dest_keyring:
0523 key_put(dest_keyring);
0524 error:
0525 kleave(" = %d", ret);
0526 return ERR_PTR(ret);
0527 }
0528
0529
0530
0531
0532
0533
0534
0535
0536
0537
0538
0539
0540
0541
0542
0543
0544
0545
0546
0547
0548
0549
0550
0551
0552
0553
0554
0555
0556
0557
0558 struct key *request_key_and_link(struct key_type *type,
0559 const char *description,
0560 struct key_tag *domain_tag,
0561 const void *callout_info,
0562 size_t callout_len,
0563 void *aux,
0564 struct key *dest_keyring,
0565 unsigned long flags)
0566 {
0567 struct keyring_search_context ctx = {
0568 .index_key.type = type,
0569 .index_key.domain_tag = domain_tag,
0570 .index_key.description = description,
0571 .index_key.desc_len = strlen(description),
0572 .cred = current_cred(),
0573 .match_data.cmp = key_default_cmp,
0574 .match_data.raw_data = description,
0575 .match_data.lookup_type = KEYRING_SEARCH_LOOKUP_DIRECT,
0576 .flags = (KEYRING_SEARCH_DO_STATE_CHECK |
0577 KEYRING_SEARCH_SKIP_EXPIRED |
0578 KEYRING_SEARCH_RECURSE),
0579 };
0580 struct key *key;
0581 key_ref_t key_ref;
0582 int ret;
0583
0584 kenter("%s,%s,%p,%zu,%p,%p,%lx",
0585 ctx.index_key.type->name, ctx.index_key.description,
0586 callout_info, callout_len, aux, dest_keyring, flags);
0587
0588 if (type->match_preparse) {
0589 ret = type->match_preparse(&ctx.match_data);
0590 if (ret < 0) {
0591 key = ERR_PTR(ret);
0592 goto error;
0593 }
0594 }
0595
0596 key = check_cached_key(&ctx);
0597 if (key)
0598 goto error_free;
0599
0600
0601 rcu_read_lock();
0602 key_ref = search_process_keyrings_rcu(&ctx);
0603 rcu_read_unlock();
0604
0605 if (!IS_ERR(key_ref)) {
0606 if (dest_keyring) {
0607 ret = key_task_permission(key_ref, current_cred(),
0608 KEY_NEED_LINK);
0609 if (ret < 0) {
0610 key_ref_put(key_ref);
0611 key = ERR_PTR(ret);
0612 goto error_free;
0613 }
0614 }
0615
0616 key = key_ref_to_ptr(key_ref);
0617 if (dest_keyring) {
0618 ret = key_link(dest_keyring, key);
0619 if (ret < 0) {
0620 key_put(key);
0621 key = ERR_PTR(ret);
0622 goto error_free;
0623 }
0624 }
0625
0626
0627 cache_requested_key(key);
0628 } else if (PTR_ERR(key_ref) != -EAGAIN) {
0629 key = ERR_CAST(key_ref);
0630 } else {
0631
0632
0633 key = ERR_PTR(-ENOKEY);
0634 if (!callout_info)
0635 goto error_free;
0636
0637 key = construct_key_and_link(&ctx, callout_info, callout_len,
0638 aux, dest_keyring, flags);
0639 }
0640
0641 error_free:
0642 if (type->match_free)
0643 type->match_free(&ctx.match_data);
0644 error:
0645 kleave(" = %p", key);
0646 return key;
0647 }
0648
0649
0650
0651
0652
0653
0654
0655
0656
0657
0658
0659
0660 int wait_for_key_construction(struct key *key, bool intr)
0661 {
0662 int ret;
0663
0664 ret = wait_on_bit(&key->flags, KEY_FLAG_USER_CONSTRUCT,
0665 intr ? TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE);
0666 if (ret)
0667 return -ERESTARTSYS;
0668 ret = key_read_state(key);
0669 if (ret < 0)
0670 return ret;
0671 return key_validate(key);
0672 }
0673 EXPORT_SYMBOL(wait_for_key_construction);
0674
0675
0676
0677
0678
0679
0680
0681
0682
0683
0684
0685
0686
0687
0688
0689
0690 struct key *request_key_tag(struct key_type *type,
0691 const char *description,
0692 struct key_tag *domain_tag,
0693 const char *callout_info)
0694 {
0695 struct key *key;
0696 size_t callout_len = 0;
0697 int ret;
0698
0699 if (callout_info)
0700 callout_len = strlen(callout_info);
0701 key = request_key_and_link(type, description, domain_tag,
0702 callout_info, callout_len,
0703 NULL, NULL, KEY_ALLOC_IN_QUOTA);
0704 if (!IS_ERR(key)) {
0705 ret = wait_for_key_construction(key, false);
0706 if (ret < 0) {
0707 key_put(key);
0708 return ERR_PTR(ret);
0709 }
0710 }
0711 return key;
0712 }
0713 EXPORT_SYMBOL(request_key_tag);
0714
0715
0716
0717
0718
0719
0720
0721
0722
0723
0724
0725
0726
0727
0728
0729
0730 struct key *request_key_with_auxdata(struct key_type *type,
0731 const char *description,
0732 struct key_tag *domain_tag,
0733 const void *callout_info,
0734 size_t callout_len,
0735 void *aux)
0736 {
0737 struct key *key;
0738 int ret;
0739
0740 key = request_key_and_link(type, description, domain_tag,
0741 callout_info, callout_len,
0742 aux, NULL, KEY_ALLOC_IN_QUOTA);
0743 if (!IS_ERR(key)) {
0744 ret = wait_for_key_construction(key, false);
0745 if (ret < 0) {
0746 key_put(key);
0747 return ERR_PTR(ret);
0748 }
0749 }
0750 return key;
0751 }
0752 EXPORT_SYMBOL(request_key_with_auxdata);
0753
0754
0755
0756
0757
0758
0759
0760
0761
0762
0763
0764
0765
0766 struct key *request_key_rcu(struct key_type *type,
0767 const char *description,
0768 struct key_tag *domain_tag)
0769 {
0770 struct keyring_search_context ctx = {
0771 .index_key.type = type,
0772 .index_key.domain_tag = domain_tag,
0773 .index_key.description = description,
0774 .index_key.desc_len = strlen(description),
0775 .cred = current_cred(),
0776 .match_data.cmp = key_default_cmp,
0777 .match_data.raw_data = description,
0778 .match_data.lookup_type = KEYRING_SEARCH_LOOKUP_DIRECT,
0779 .flags = (KEYRING_SEARCH_DO_STATE_CHECK |
0780 KEYRING_SEARCH_SKIP_EXPIRED),
0781 };
0782 struct key *key;
0783 key_ref_t key_ref;
0784
0785 kenter("%s,%s", type->name, description);
0786
0787 key = check_cached_key(&ctx);
0788 if (key)
0789 return key;
0790
0791
0792 key_ref = search_process_keyrings_rcu(&ctx);
0793 if (IS_ERR(key_ref)) {
0794 key = ERR_CAST(key_ref);
0795 if (PTR_ERR(key_ref) == -EAGAIN)
0796 key = ERR_PTR(-ENOKEY);
0797 } else {
0798 key = key_ref_to_ptr(key_ref);
0799 cache_requested_key(key);
0800 }
0801
0802 kleave(" = %p", key);
0803 return key;
0804 }
0805 EXPORT_SYMBOL(request_key_rcu);