1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* Request key authorisation token key definition. 3 * 4 * Copyright (C) 2005 Red Hat, Inc. All Rights Reserved. 5 * Written by David Howells (dhowells@redhat.com) 6 * 7 * See Documentation/security/keys/request-key.rst 8 */ 9 10 #include <linux/sched.h> 11 #include <linux/err.h> 12 #include <linux/seq_file.h> 13 #include <linux/slab.h> 14 #include <linux/uaccess.h> 15 #include "internal.h" 16 #include <keys/request_key_auth-type.h> 17 18 static int request_key_auth_preparse(struct key_preparsed_payload *); 19 static void request_key_auth_free_preparse(struct key_preparsed_payload *); 20 static int request_key_auth_instantiate(struct key *, 21 struct key_preparsed_payload *); 22 static void request_key_auth_describe(const struct key *, struct seq_file *); 23 static void request_key_auth_revoke(struct key *); 24 static void request_key_auth_destroy(struct key *); 25 static long request_key_auth_read(const struct key *, char *, size_t); 26 static void request_key_auth_rcu_disposal(struct rcu_head *); 27 28 /* 29 * The request-key authorisation key type definition. 30 */ 31 struct key_type key_type_request_key_auth = { 32 .name = ".request_key_auth", 33 .def_datalen = sizeof(struct request_key_auth), 34 .preparse = request_key_auth_preparse, 35 .free_preparse = request_key_auth_free_preparse, 36 .instantiate = request_key_auth_instantiate, 37 .describe = request_key_auth_describe, 38 .revoke = request_key_auth_revoke, 39 .destroy = request_key_auth_destroy, 40 .read = request_key_auth_read, 41 }; 42 43 static int request_key_auth_preparse(struct key_preparsed_payload *prep) 44 { 45 return 0; 46 } 47 48 static void request_key_auth_free_preparse(struct key_preparsed_payload *prep) 49 { 50 } 51 52 /* 53 * Instantiate a request-key authorisation key. 54 */ 55 static int request_key_auth_instantiate(struct key *key, 56 struct key_preparsed_payload *prep) 57 { 58 rcu_assign_keypointer(key, (struct request_key_auth *)prep->data); 59 return 0; 60 } 61 62 /* 63 * Describe an authorisation token. 64 */ 65 static void request_key_auth_describe(const struct key *key, 66 struct seq_file *m) 67 { 68 struct request_key_auth *rka = dereference_key_rcu(key); 69 70 if (!rka) 71 return; 72 73 seq_puts(m, "key:"); 74 seq_puts(m, key->description); 75 if (key_is_positive(key)) 76 seq_printf(m, " pid:%d ci:%zu", rka->pid, rka->callout_len); 77 } 78 79 /* 80 * Read the callout_info data (retrieves the callout information). 81 * - the key's semaphore is read-locked 82 */ 83 static long request_key_auth_read(const struct key *key, 84 char *buffer, size_t buflen) 85 { 86 struct request_key_auth *rka = dereference_key_locked(key); 87 size_t datalen; 88 long ret; 89 90 if (!rka) 91 return -EKEYREVOKED; 92 93 datalen = rka->callout_len; 94 ret = datalen; 95 96 /* we can return the data as is */ 97 if (buffer && buflen > 0) { 98 if (buflen > datalen) 99 buflen = datalen; 100 101 memcpy(buffer, rka->callout_info, buflen); 102 } 103 104 return ret; 105 } 106 107 static void free_request_key_auth(struct request_key_auth *rka) 108 { 109 if (!rka) 110 return; 111 key_put(rka->target_key); 112 key_put(rka->dest_keyring); 113 if (rka->cred) 114 put_cred(rka->cred); 115 kfree(rka->callout_info); 116 kfree(rka); 117 } 118 119 /* 120 * Take a reference to the request-key authorisation payload so callers can 121 * drop authkey->sem before doing operations that may sleep. 122 */ 123 struct request_key_auth *request_key_auth_get(struct key *authkey) 124 { 125 struct request_key_auth *rka; 126 127 down_read(&authkey->sem); 128 rka = dereference_key_locked(authkey); 129 if (rka && !test_bit(KEY_FLAG_REVOKED, &authkey->flags)) 130 refcount_inc(&rka->usage); 131 else 132 rka = NULL; 133 up_read(&authkey->sem); 134 135 return rka; 136 } 137 138 void request_key_auth_put(struct request_key_auth *rka) 139 { 140 if (rka && refcount_dec_and_test(&rka->usage)) 141 call_rcu(&rka->rcu, request_key_auth_rcu_disposal); 142 } 143 144 /* 145 * Dispose of the request_key_auth record under RCU conditions 146 */ 147 static void request_key_auth_rcu_disposal(struct rcu_head *rcu) 148 { 149 struct request_key_auth *rka = 150 container_of(rcu, struct request_key_auth, rcu); 151 152 free_request_key_auth(rka); 153 } 154 155 /* 156 * Handle revocation of an authorisation token key. 157 * 158 * Called with the key sem write-locked. 159 */ 160 static void request_key_auth_revoke(struct key *key) 161 { 162 struct request_key_auth *rka = dereference_key_locked(key); 163 164 kenter("{%d}", key->serial); 165 if (!rka) 166 return; 167 rcu_assign_keypointer(key, NULL); 168 request_key_auth_put(rka); 169 } 170 171 /* 172 * Destroy an instantiation authorisation token key. 173 */ 174 static void request_key_auth_destroy(struct key *key) 175 { 176 struct request_key_auth *rka = rcu_access_pointer(key->payload.rcu_data0); 177 178 kenter("{%d}", key->serial); 179 if (rka) { 180 rcu_assign_keypointer(key, NULL); 181 request_key_auth_put(rka); 182 } 183 } 184 185 /* 186 * Create an authorisation token for /sbin/request-key or whoever to gain 187 * access to the caller's security data. 188 */ 189 struct key *request_key_auth_new(struct key *target, const char *op, 190 const void *callout_info, size_t callout_len, 191 struct key *dest_keyring) 192 { 193 struct request_key_auth *rka, *irka; 194 const struct cred *cred = current_cred(); 195 struct key *authkey = NULL; 196 char desc[20]; 197 int ret = -ENOMEM; 198 199 kenter("%d,", target->serial); 200 201 /* allocate a auth record */ 202 rka = kzalloc_obj(*rka); 203 if (!rka) 204 goto error; 205 refcount_set(&rka->usage, 1); 206 rka->callout_info = kmemdup(callout_info, callout_len, GFP_KERNEL); 207 if (!rka->callout_info) 208 goto error_free_rka; 209 rka->callout_len = callout_len; 210 strscpy(rka->op, op, sizeof(rka->op)); 211 212 /* see if the calling process is already servicing the key request of 213 * another process */ 214 if (cred->request_key_auth) { 215 /* it is - use that instantiation context here too */ 216 down_read(&cred->request_key_auth->sem); 217 218 /* if the auth key has been revoked, then the key we're 219 * servicing is already instantiated */ 220 if (test_bit(KEY_FLAG_REVOKED, 221 &cred->request_key_auth->flags)) { 222 up_read(&cred->request_key_auth->sem); 223 ret = -EKEYREVOKED; 224 goto error_free_rka; 225 } 226 227 irka = cred->request_key_auth->payload.data[0]; 228 rka->cred = get_cred(irka->cred); 229 rka->pid = irka->pid; 230 231 up_read(&cred->request_key_auth->sem); 232 } 233 else { 234 /* it isn't - use this process as the context */ 235 rka->cred = get_cred(cred); 236 rka->pid = current->pid; 237 } 238 239 rka->target_key = key_get(target); 240 rka->dest_keyring = key_get(dest_keyring); 241 242 /* allocate the auth key */ 243 sprintf(desc, "%x", target->serial); 244 245 authkey = key_alloc(&key_type_request_key_auth, desc, 246 cred->fsuid, cred->fsgid, cred, 247 KEY_POS_VIEW | KEY_POS_READ | KEY_POS_SEARCH | KEY_POS_LINK | 248 KEY_USR_VIEW, KEY_ALLOC_NOT_IN_QUOTA, NULL); 249 if (IS_ERR(authkey)) { 250 ret = PTR_ERR(authkey); 251 goto error_free_rka; 252 } 253 254 /* construct the auth key */ 255 ret = key_instantiate_and_link(authkey, rka, 0, NULL, NULL); 256 if (ret < 0) 257 goto error_put_authkey; 258 259 kleave(" = {%d,%d}", authkey->serial, refcount_read(&authkey->usage)); 260 return authkey; 261 262 error_put_authkey: 263 key_put(authkey); 264 error_free_rka: 265 free_request_key_auth(rka); 266 error: 267 kleave("= %d", ret); 268 return ERR_PTR(ret); 269 } 270 271 /* 272 * Search the current process's keyrings for the authorisation key for 273 * instantiation of a key. 274 */ 275 struct key *key_get_instantiation_authkey(key_serial_t target_id) 276 { 277 char description[16]; 278 struct keyring_search_context ctx = { 279 .index_key.type = &key_type_request_key_auth, 280 .index_key.description = description, 281 .cred = current_cred(), 282 .match_data.cmp = key_default_cmp, 283 .match_data.raw_data = description, 284 .match_data.lookup_type = KEYRING_SEARCH_LOOKUP_DIRECT, 285 .flags = (KEYRING_SEARCH_DO_STATE_CHECK | 286 KEYRING_SEARCH_RECURSE), 287 }; 288 struct key *authkey; 289 key_ref_t authkey_ref; 290 291 ctx.index_key.desc_len = sprintf(description, "%x", target_id); 292 293 rcu_read_lock(); 294 authkey_ref = search_process_keyrings_rcu(&ctx); 295 rcu_read_unlock(); 296 297 if (IS_ERR(authkey_ref)) { 298 authkey = ERR_CAST(authkey_ref); 299 if (authkey == ERR_PTR(-EAGAIN)) 300 authkey = ERR_PTR(-ENOKEY); 301 goto error; 302 } 303 304 authkey = key_ref_to_ptr(authkey_ref); 305 if (test_bit(KEY_FLAG_REVOKED, &authkey->flags)) { 306 key_put(authkey); 307 authkey = ERR_PTR(-EKEYREVOKED); 308 } 309 310 error: 311 return authkey; 312 } 313