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