Merge tag 'keys-acl-20190703' of git://git.kernel.org/pub/scm/linux/kernel/git/dhowel...
[linux-2.6-microblaze.git] / security / keys / request_key_auth.c
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 __user *, size_t);
26
27 static struct key_acl request_key_auth_acl = {
28         .usage  = REFCOUNT_INIT(1),
29         .nr_ace = 2,
30         .possessor_viewable = true,
31         .aces = {
32                 KEY_POSSESSOR_ACE(KEY_ACE_VIEW | KEY_ACE_READ | KEY_ACE_SEARCH |
33                                   KEY_ACE_LINK),
34                 KEY_OWNER_ACE(KEY_ACE_VIEW),
35         }
36 };
37
38 /*
39  * The request-key authorisation key type definition.
40  */
41 struct key_type key_type_request_key_auth = {
42         .name           = ".request_key_auth",
43         .def_datalen    = sizeof(struct request_key_auth),
44         .preparse       = request_key_auth_preparse,
45         .free_preparse  = request_key_auth_free_preparse,
46         .instantiate    = request_key_auth_instantiate,
47         .describe       = request_key_auth_describe,
48         .revoke         = request_key_auth_revoke,
49         .destroy        = request_key_auth_destroy,
50         .read           = request_key_auth_read,
51 };
52
53 static int request_key_auth_preparse(struct key_preparsed_payload *prep)
54 {
55         return 0;
56 }
57
58 static void request_key_auth_free_preparse(struct key_preparsed_payload *prep)
59 {
60 }
61
62 /*
63  * Instantiate a request-key authorisation key.
64  */
65 static int request_key_auth_instantiate(struct key *key,
66                                         struct key_preparsed_payload *prep)
67 {
68         rcu_assign_keypointer(key, (struct request_key_auth *)prep->data);
69         return 0;
70 }
71
72 /*
73  * Describe an authorisation token.
74  */
75 static void request_key_auth_describe(const struct key *key,
76                                       struct seq_file *m)
77 {
78         struct request_key_auth *rka = dereference_key_rcu(key);
79
80         seq_puts(m, "key:");
81         seq_puts(m, key->description);
82         if (key_is_positive(key))
83                 seq_printf(m, " pid:%d ci:%zu", rka->pid, rka->callout_len);
84 }
85
86 /*
87  * Read the callout_info data (retrieves the callout information).
88  * - the key's semaphore is read-locked
89  */
90 static long request_key_auth_read(const struct key *key,
91                                   char __user *buffer, size_t buflen)
92 {
93         struct request_key_auth *rka = dereference_key_locked(key);
94         size_t datalen;
95         long ret;
96
97         datalen = rka->callout_len;
98         ret = datalen;
99
100         /* we can return the data as is */
101         if (buffer && buflen > 0) {
102                 if (buflen > datalen)
103                         buflen = datalen;
104
105                 if (copy_to_user(buffer, rka->callout_info, buflen) != 0)
106                         ret = -EFAULT;
107         }
108
109         return ret;
110 }
111
112 static void free_request_key_auth(struct request_key_auth *rka)
113 {
114         if (!rka)
115                 return;
116         key_put(rka->target_key);
117         key_put(rka->dest_keyring);
118         if (rka->cred)
119                 put_cred(rka->cred);
120         kfree(rka->callout_info);
121         kfree(rka);
122 }
123
124 /*
125  * Dispose of the request_key_auth record under RCU conditions
126  */
127 static void request_key_auth_rcu_disposal(struct rcu_head *rcu)
128 {
129         struct request_key_auth *rka =
130                 container_of(rcu, struct request_key_auth, rcu);
131
132         free_request_key_auth(rka);
133 }
134
135 /*
136  * Handle revocation of an authorisation token key.
137  *
138  * Called with the key sem write-locked.
139  */
140 static void request_key_auth_revoke(struct key *key)
141 {
142         struct request_key_auth *rka = dereference_key_locked(key);
143
144         kenter("{%d}", key->serial);
145         rcu_assign_keypointer(key, NULL);
146         call_rcu(&rka->rcu, request_key_auth_rcu_disposal);
147 }
148
149 /*
150  * Destroy an instantiation authorisation token key.
151  */
152 static void request_key_auth_destroy(struct key *key)
153 {
154         struct request_key_auth *rka = rcu_access_pointer(key->payload.rcu_data0);
155
156         kenter("{%d}", key->serial);
157         if (rka) {
158                 rcu_assign_keypointer(key, NULL);
159                 call_rcu(&rka->rcu, request_key_auth_rcu_disposal);
160         }
161 }
162
163 /*
164  * Create an authorisation token for /sbin/request-key or whoever to gain
165  * access to the caller's security data.
166  */
167 struct key *request_key_auth_new(struct key *target, const char *op,
168                                  const void *callout_info, size_t callout_len,
169                                  struct key *dest_keyring)
170 {
171         struct request_key_auth *rka, *irka;
172         const struct cred *cred = current_cred();
173         struct key *authkey = NULL;
174         char desc[20];
175         int ret = -ENOMEM;
176
177         kenter("%d,", target->serial);
178
179         /* allocate a auth record */
180         rka = kzalloc(sizeof(*rka), GFP_KERNEL);
181         if (!rka)
182                 goto error;
183         rka->callout_info = kmemdup(callout_info, callout_len, GFP_KERNEL);
184         if (!rka->callout_info)
185                 goto error_free_rka;
186         rka->callout_len = callout_len;
187         strlcpy(rka->op, op, sizeof(rka->op));
188
189         /* see if the calling process is already servicing the key request of
190          * another process */
191         if (cred->request_key_auth) {
192                 /* it is - use that instantiation context here too */
193                 down_read(&cred->request_key_auth->sem);
194
195                 /* if the auth key has been revoked, then the key we're
196                  * servicing is already instantiated */
197                 if (test_bit(KEY_FLAG_REVOKED,
198                              &cred->request_key_auth->flags)) {
199                         up_read(&cred->request_key_auth->sem);
200                         ret = -EKEYREVOKED;
201                         goto error_free_rka;
202                 }
203
204                 irka = cred->request_key_auth->payload.data[0];
205                 rka->cred = get_cred(irka->cred);
206                 rka->pid = irka->pid;
207
208                 up_read(&cred->request_key_auth->sem);
209         }
210         else {
211                 /* it isn't - use this process as the context */
212                 rka->cred = get_cred(cred);
213                 rka->pid = current->pid;
214         }
215
216         rka->target_key = key_get(target);
217         rka->dest_keyring = key_get(dest_keyring);
218
219         /* allocate the auth key */
220         sprintf(desc, "%x", target->serial);
221
222         authkey = key_alloc(&key_type_request_key_auth, desc,
223                             cred->fsuid, cred->fsgid, cred,
224                             &request_key_auth_acl,
225                             KEY_ALLOC_NOT_IN_QUOTA, NULL);
226         if (IS_ERR(authkey)) {
227                 ret = PTR_ERR(authkey);
228                 goto error_free_rka;
229         }
230
231         /* construct the auth key */
232         ret = key_instantiate_and_link(authkey, rka, 0, NULL, NULL);
233         if (ret < 0)
234                 goto error_put_authkey;
235
236         kleave(" = {%d,%d}", authkey->serial, refcount_read(&authkey->usage));
237         return authkey;
238
239 error_put_authkey:
240         key_put(authkey);
241 error_free_rka:
242         free_request_key_auth(rka);
243 error:
244         kleave("= %d", ret);
245         return ERR_PTR(ret);
246 }
247
248 /*
249  * Search the current process's keyrings for the authorisation key for
250  * instantiation of a key.
251  */
252 struct key *key_get_instantiation_authkey(key_serial_t target_id)
253 {
254         char description[16];
255         struct keyring_search_context ctx = {
256                 .index_key.type         = &key_type_request_key_auth,
257                 .index_key.description  = description,
258                 .cred                   = current_cred(),
259                 .match_data.cmp         = key_default_cmp,
260                 .match_data.raw_data    = description,
261                 .match_data.lookup_type = KEYRING_SEARCH_LOOKUP_DIRECT,
262                 .flags                  = (KEYRING_SEARCH_DO_STATE_CHECK |
263                                            KEYRING_SEARCH_RECURSE),
264         };
265         struct key *authkey;
266         key_ref_t authkey_ref;
267
268         ctx.index_key.desc_len = sprintf(description, "%x", target_id);
269
270         rcu_read_lock();
271         authkey_ref = search_process_keyrings_rcu(&ctx);
272         rcu_read_unlock();
273
274         if (IS_ERR(authkey_ref)) {
275                 authkey = ERR_CAST(authkey_ref);
276                 if (authkey == ERR_PTR(-EAGAIN))
277                         authkey = ERR_PTR(-ENOKEY);
278                 goto error;
279         }
280
281         authkey = key_ref_to_ptr(authkey_ref);
282         if (test_bit(KEY_FLAG_REVOKED, &authkey->flags)) {
283                 key_put(authkey);
284                 authkey = ERR_PTR(-EKEYREVOKED);
285         }
286
287 error:
288         return authkey;
289 }