hc
2024-02-20 102a0743326a03cd1a1202ceda21e175b7d3575c
kernel/security/keys/request_key_auth.c
....@@ -1,17 +1,12 @@
1
+// SPDX-License-Identifier: GPL-2.0-or-later
12 /* Request key authorisation token key definition.
23 *
34 * Copyright (C) 2005 Red Hat, Inc. All Rights Reserved.
45 * Written by David Howells (dhowells@redhat.com)
56 *
6
- * This program is free software; you can redistribute it and/or
7
- * modify it under the terms of the GNU General Public License
8
- * as published by the Free Software Foundation; either version
9
- * 2 of the License, or (at your option) any later version.
10
- *
117 * See Documentation/security/keys/request-key.rst
128 */
139
14
-#include <linux/module.h>
1510 #include <linux/sched.h>
1611 #include <linux/err.h>
1712 #include <linux/seq_file.h>
....@@ -59,7 +54,7 @@
5954 static int request_key_auth_instantiate(struct key *key,
6055 struct key_preparsed_payload *prep)
6156 {
62
- key->payload.data[0] = (struct request_key_auth *)prep->data;
57
+ rcu_assign_keypointer(key, (struct request_key_auth *)prep->data);
6358 return 0;
6459 }
6560
....@@ -69,7 +64,7 @@
6964 static void request_key_auth_describe(const struct key *key,
7065 struct seq_file *m)
7166 {
72
- struct request_key_auth *rka = get_request_key_auth(key);
67
+ struct request_key_auth *rka = dereference_key_rcu(key);
7368
7469 if (!rka)
7570 return;
....@@ -87,7 +82,7 @@
8782 static long request_key_auth_read(const struct key *key,
8883 char *buffer, size_t buflen)
8984 {
90
- struct request_key_auth *rka = get_request_key_auth(key);
85
+ struct request_key_auth *rka = dereference_key_locked(key);
9186 size_t datalen;
9287 long ret;
9388
....@@ -108,23 +103,6 @@
108103 return ret;
109104 }
110105
111
-/*
112
- * Handle revocation of an authorisation token key.
113
- *
114
- * Called with the key sem write-locked.
115
- */
116
-static void request_key_auth_revoke(struct key *key)
117
-{
118
- struct request_key_auth *rka = get_request_key_auth(key);
119
-
120
- kenter("{%d}", key->serial);
121
-
122
- if (rka->cred) {
123
- put_cred(rka->cred);
124
- rka->cred = NULL;
125
- }
126
-}
127
-
128106 static void free_request_key_auth(struct request_key_auth *rka)
129107 {
130108 if (!rka)
....@@ -138,15 +116,42 @@
138116 }
139117
140118 /*
119
+ * Dispose of the request_key_auth record under RCU conditions
120
+ */
121
+static void request_key_auth_rcu_disposal(struct rcu_head *rcu)
122
+{
123
+ struct request_key_auth *rka =
124
+ container_of(rcu, struct request_key_auth, rcu);
125
+
126
+ free_request_key_auth(rka);
127
+}
128
+
129
+/*
130
+ * Handle revocation of an authorisation token key.
131
+ *
132
+ * Called with the key sem write-locked.
133
+ */
134
+static void request_key_auth_revoke(struct key *key)
135
+{
136
+ struct request_key_auth *rka = dereference_key_locked(key);
137
+
138
+ kenter("{%d}", key->serial);
139
+ rcu_assign_keypointer(key, NULL);
140
+ call_rcu(&rka->rcu, request_key_auth_rcu_disposal);
141
+}
142
+
143
+/*
141144 * Destroy an instantiation authorisation token key.
142145 */
143146 static void request_key_auth_destroy(struct key *key)
144147 {
145
- struct request_key_auth *rka = get_request_key_auth(key);
148
+ struct request_key_auth *rka = rcu_access_pointer(key->payload.rcu_data0);
146149
147150 kenter("{%d}", key->serial);
148
-
149
- free_request_key_auth(rka);
151
+ if (rka) {
152
+ rcu_assign_keypointer(key, NULL);
153
+ call_rcu(&rka->rcu, request_key_auth_rcu_disposal);
154
+ }
150155 }
151156
152157 /*
....@@ -158,7 +163,7 @@
158163 struct key *dest_keyring)
159164 {
160165 struct request_key_auth *rka, *irka;
161
- const struct cred *cred = current->cred;
166
+ const struct cred *cred = current_cred();
162167 struct key *authkey = NULL;
163168 char desc[20];
164169 int ret = -ENOMEM;
....@@ -210,7 +215,7 @@
210215
211216 authkey = key_alloc(&key_type_request_key_auth, desc,
212217 cred->fsuid, cred->fsgid, cred,
213
- KEY_POS_VIEW | KEY_POS_READ | KEY_POS_SEARCH |
218
+ KEY_POS_VIEW | KEY_POS_READ | KEY_POS_SEARCH | KEY_POS_LINK |
214219 KEY_USR_VIEW, KEY_ALLOC_NOT_IN_QUOTA, NULL);
215220 if (IS_ERR(authkey)) {
216221 ret = PTR_ERR(authkey);
....@@ -248,14 +253,17 @@
248253 .match_data.cmp = key_default_cmp,
249254 .match_data.raw_data = description,
250255 .match_data.lookup_type = KEYRING_SEARCH_LOOKUP_DIRECT,
251
- .flags = KEYRING_SEARCH_DO_STATE_CHECK,
256
+ .flags = (KEYRING_SEARCH_DO_STATE_CHECK |
257
+ KEYRING_SEARCH_RECURSE),
252258 };
253259 struct key *authkey;
254260 key_ref_t authkey_ref;
255261
256262 ctx.index_key.desc_len = sprintf(description, "%x", target_id);
257263
258
- authkey_ref = search_process_keyrings(&ctx);
264
+ rcu_read_lock();
265
+ authkey_ref = search_process_keyrings_rcu(&ctx);
266
+ rcu_read_unlock();
259267
260268 if (IS_ERR(authkey_ref)) {
261269 authkey = ERR_CAST(authkey_ref);