1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* General persistent per-UID keyrings register 3 * 4 * Copyright (C) 2013 Red Hat, Inc. All Rights Reserved. 5 * Written by David Howells (dhowells@redhat.com) 6 */ 7 8 #include <linux/user_namespace.h> 9 #include <linux/cred.h> 10 11 #include "internal.h" 12 13 unsigned persistent_keyring_expiry = 3 * 24 * 3600; /* Expire after 3 days of non-use */ 14 15 /* 16 * Create the persistent keyring register for the current user namespace. 17 * 18 * Called with the namespace's sem locked for writing. 19 */ 20 static int key_create_persistent_register(struct user_namespace *ns) 21 { 22 struct key *reg = keyring_alloc(".persistent_register", 23 KUIDT_INIT(0), KGIDT_INIT(0), 24 current_cred(), 25 ((KEY_POS_ALL & ~KEY_POS_SETATTR) | 26 KEY_USR_VIEW | KEY_USR_READ), 27 KEY_ALLOC_NOT_IN_QUOTA, NULL, NULL); 28 if (IS_ERR(reg)) 29 return PTR_ERR(reg); 30 31 ns->persistent_keyring_register = reg; 32 return 0; 33 } 34 35 /* 36 * Get or create the persistent keyring for the specified user. 37 * 38 * Called with the namespace's sem locked for writing. 39 * 40 * Returns a boolean indicating whether the keyring already existed, 41 * or a negative error. On success, *persistent_ref holds a reference 42 * to the keyring. 43 */ 44 static int key_get_or_create_persistent(struct user_namespace *ns, kuid_t uid, 45 struct keyring_index_key *index_key, 46 key_ref_t *persistent_ref) 47 { 48 struct key *persistent; 49 key_ref_t reg_ref; 50 51 if (!ns->persistent_keyring_register) { 52 int err = key_create_persistent_register(ns); 53 54 if (err < 0) 55 return err; 56 } else { 57 reg_ref = make_key_ref(ns->persistent_keyring_register, true); 58 *persistent_ref = find_key_to_update(reg_ref, index_key); 59 if (*persistent_ref) 60 return 1; 61 } 62 63 persistent = keyring_alloc(index_key->description, 64 uid, INVALID_GID, current_cred(), 65 ((KEY_POS_ALL & ~KEY_POS_SETATTR) | 66 KEY_USR_VIEW | KEY_USR_READ), 67 KEY_ALLOC_NOT_IN_QUOTA, NULL, 68 ns->persistent_keyring_register); 69 if (IS_ERR(persistent)) 70 return PTR_ERR(persistent); 71 72 *persistent_ref = make_key_ref(persistent, true); 73 return 0; 74 } 75 76 /* 77 * Get the persistent keyring for a specific UID and link it to the nominated 78 * keyring. 79 */ 80 static long key_get_persistent(struct user_namespace *ns, kuid_t uid, 81 key_ref_t dest_ref) 82 { 83 struct keyring_index_key index_key; 84 struct key *persistent; 85 key_ref_t reg_ref, persistent_ref; 86 char buf[32]; 87 long ret; 88 bool created = false; 89 90 /* Look in the register if it exists */ 91 memset(&index_key, 0, sizeof(index_key)); 92 index_key.type = &key_type_keyring; 93 index_key.description = buf; 94 index_key.desc_len = sprintf(buf, "_persistent.%u", from_kuid(ns, uid)); 95 key_set_index_key(&index_key); 96 97 if (ns->persistent_keyring_register) { 98 reg_ref = make_key_ref(ns->persistent_keyring_register, true); 99 down_read(&ns->keyring_sem); 100 persistent_ref = find_key_to_update(reg_ref, &index_key); 101 up_read(&ns->keyring_sem); 102 103 if (persistent_ref) 104 goto found; 105 } 106 107 /* It wasn't in the register, so we'll need to create it. We might 108 * also need to create the register. 109 */ 110 down_write(&ns->keyring_sem); 111 ret = key_get_or_create_persistent(ns, uid, &index_key, 112 &persistent_ref); 113 up_write(&ns->keyring_sem); 114 if (ret < 0) 115 return ret; 116 created = ret == 0; 117 118 found: 119 persistent = key_ref_to_ptr(persistent_ref); 120 ret = key_task_permission(persistent_ref, current_cred(), KEY_NEED_LINK); 121 if (ret == 0) 122 ret = key_link(key_ref_to_ptr(dest_ref), persistent); 123 124 if (ret == 0 || created) 125 key_set_timeout(persistent, persistent_keyring_expiry); 126 127 if (ret == 0) 128 ret = persistent->serial; 129 130 key_ref_put(persistent_ref); 131 return ret; 132 } 133 134 /* 135 * Get the persistent keyring for a specific UID and link it to the nominated 136 * keyring. 137 */ 138 long keyctl_get_persistent(uid_t _uid, key_serial_t destid) 139 { 140 struct user_namespace *ns = current_user_ns(); 141 key_ref_t dest_ref; 142 kuid_t uid; 143 long ret; 144 145 /* -1 indicates the current user */ 146 if (_uid == (uid_t)-1) { 147 uid = current_uid(); 148 } else { 149 uid = make_kuid(ns, _uid); 150 if (!uid_valid(uid)) 151 return -EINVAL; 152 153 /* You can only see your own persistent cache if you're not 154 * sufficiently privileged. 155 */ 156 if (!uid_eq(uid, current_uid()) && 157 !uid_eq(uid, current_euid()) && 158 !ns_capable(ns, CAP_SETUID)) 159 return -EPERM; 160 } 161 162 /* There must be a destination keyring */ 163 dest_ref = lookup_user_key(destid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE); 164 if (IS_ERR(dest_ref)) 165 return PTR_ERR(dest_ref); 166 if (key_ref_to_ptr(dest_ref)->type != &key_type_keyring) { 167 ret = -ENOTDIR; 168 goto out_put_dest; 169 } 170 171 ret = key_get_persistent(ns, uid, dest_ref); 172 173 out_put_dest: 174 key_ref_put(dest_ref); 175 return ret; 176 } 177