Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /* General persistent per-UID keyrings register
0003  *
0004  * Copyright (C) 2013 Red Hat, Inc. All Rights Reserved.
0005  * Written by David Howells (dhowells@redhat.com)
0006  */
0007 
0008 #include <linux/user_namespace.h>
0009 #include <linux/cred.h>
0010 
0011 #include "internal.h"
0012 
0013 unsigned persistent_keyring_expiry = 3 * 24 * 3600; /* Expire after 3 days of non-use */
0014 
0015 /*
0016  * Create the persistent keyring register for the current user namespace.
0017  *
0018  * Called with the namespace's sem locked for writing.
0019  */
0020 static int key_create_persistent_register(struct user_namespace *ns)
0021 {
0022     struct key *reg = keyring_alloc(".persistent_register",
0023                     KUIDT_INIT(0), KGIDT_INIT(0),
0024                     current_cred(),
0025                     ((KEY_POS_ALL & ~KEY_POS_SETATTR) |
0026                      KEY_USR_VIEW | KEY_USR_READ),
0027                     KEY_ALLOC_NOT_IN_QUOTA, NULL, NULL);
0028     if (IS_ERR(reg))
0029         return PTR_ERR(reg);
0030 
0031     ns->persistent_keyring_register = reg;
0032     return 0;
0033 }
0034 
0035 /*
0036  * Create the persistent keyring for the specified user.
0037  *
0038  * Called with the namespace's sem locked for writing.
0039  */
0040 static key_ref_t key_create_persistent(struct user_namespace *ns, kuid_t uid,
0041                        struct keyring_index_key *index_key)
0042 {
0043     struct key *persistent;
0044     key_ref_t reg_ref, persistent_ref;
0045 
0046     if (!ns->persistent_keyring_register) {
0047         long err = key_create_persistent_register(ns);
0048         if (err < 0)
0049             return ERR_PTR(err);
0050     } else {
0051         reg_ref = make_key_ref(ns->persistent_keyring_register, true);
0052         persistent_ref = find_key_to_update(reg_ref, index_key);
0053         if (persistent_ref)
0054             return persistent_ref;
0055     }
0056 
0057     persistent = keyring_alloc(index_key->description,
0058                    uid, INVALID_GID, current_cred(),
0059                    ((KEY_POS_ALL & ~KEY_POS_SETATTR) |
0060                     KEY_USR_VIEW | KEY_USR_READ),
0061                    KEY_ALLOC_NOT_IN_QUOTA, NULL,
0062                    ns->persistent_keyring_register);
0063     if (IS_ERR(persistent))
0064         return ERR_CAST(persistent);
0065 
0066     return make_key_ref(persistent, true);
0067 }
0068 
0069 /*
0070  * Get the persistent keyring for a specific UID and link it to the nominated
0071  * keyring.
0072  */
0073 static long key_get_persistent(struct user_namespace *ns, kuid_t uid,
0074                    key_ref_t dest_ref)
0075 {
0076     struct keyring_index_key index_key;
0077     struct key *persistent;
0078     key_ref_t reg_ref, persistent_ref;
0079     char buf[32];
0080     long ret;
0081 
0082     /* Look in the register if it exists */
0083     memset(&index_key, 0, sizeof(index_key));
0084     index_key.type = &key_type_keyring;
0085     index_key.description = buf;
0086     index_key.desc_len = sprintf(buf, "_persistent.%u", from_kuid(ns, uid));
0087     key_set_index_key(&index_key);
0088 
0089     if (ns->persistent_keyring_register) {
0090         reg_ref = make_key_ref(ns->persistent_keyring_register, true);
0091         down_read(&ns->keyring_sem);
0092         persistent_ref = find_key_to_update(reg_ref, &index_key);
0093         up_read(&ns->keyring_sem);
0094 
0095         if (persistent_ref)
0096             goto found;
0097     }
0098 
0099     /* It wasn't in the register, so we'll need to create it.  We might
0100      * also need to create the register.
0101      */
0102     down_write(&ns->keyring_sem);
0103     persistent_ref = key_create_persistent(ns, uid, &index_key);
0104     up_write(&ns->keyring_sem);
0105     if (!IS_ERR(persistent_ref))
0106         goto found;
0107 
0108     return PTR_ERR(persistent_ref);
0109 
0110 found:
0111     ret = key_task_permission(persistent_ref, current_cred(), KEY_NEED_LINK);
0112     if (ret == 0) {
0113         persistent = key_ref_to_ptr(persistent_ref);
0114         ret = key_link(key_ref_to_ptr(dest_ref), persistent);
0115         if (ret == 0) {
0116             key_set_timeout(persistent, persistent_keyring_expiry);
0117             ret = persistent->serial;
0118         }
0119     }
0120 
0121     key_ref_put(persistent_ref);
0122     return ret;
0123 }
0124 
0125 /*
0126  * Get the persistent keyring for a specific UID and link it to the nominated
0127  * keyring.
0128  */
0129 long keyctl_get_persistent(uid_t _uid, key_serial_t destid)
0130 {
0131     struct user_namespace *ns = current_user_ns();
0132     key_ref_t dest_ref;
0133     kuid_t uid;
0134     long ret;
0135 
0136     /* -1 indicates the current user */
0137     if (_uid == (uid_t)-1) {
0138         uid = current_uid();
0139     } else {
0140         uid = make_kuid(ns, _uid);
0141         if (!uid_valid(uid))
0142             return -EINVAL;
0143 
0144         /* You can only see your own persistent cache if you're not
0145          * sufficiently privileged.
0146          */
0147         if (!uid_eq(uid, current_uid()) &&
0148             !uid_eq(uid, current_euid()) &&
0149             !ns_capable(ns, CAP_SETUID))
0150             return -EPERM;
0151     }
0152 
0153     /* There must be a destination keyring */
0154     dest_ref = lookup_user_key(destid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE);
0155     if (IS_ERR(dest_ref))
0156         return PTR_ERR(dest_ref);
0157     if (key_ref_to_ptr(dest_ref)->type != &key_type_keyring) {
0158         ret = -ENOTDIR;
0159         goto out_put_dest;
0160     }
0161 
0162     ret = key_get_persistent(ns, uid, dest_ref);
0163 
0164 out_put_dest:
0165     key_ref_put(dest_ref);
0166     return ret;
0167 }