]>
Commit | Line | Data |
---|---|---|
2874c5fd | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
1da177e4 LT |
2 | /* user_defined.c: user defined key type |
3 | * | |
4 | * Copyright (C) 2004 Red Hat, Inc. All Rights Reserved. | |
5 | * Written by David Howells ([email protected]) | |
1da177e4 LT |
6 | */ |
7 | ||
876979c9 | 8 | #include <linux/export.h> |
1da177e4 | 9 | #include <linux/init.h> |
1da177e4 LT |
10 | #include <linux/slab.h> |
11 | #include <linux/seq_file.h> | |
12 | #include <linux/err.h> | |
2aa349f6 | 13 | #include <keys/user-type.h> |
7c0f6ba6 | 14 | #include <linux/uaccess.h> |
1da177e4 LT |
15 | #include "internal.h" |
16 | ||
9f6ed2ca JL |
17 | static int logon_vet_description(const char *desc); |
18 | ||
1da177e4 LT |
19 | /* |
20 | * user defined keys take an arbitrary string as the description and an | |
21 | * arbitrary blob of data as the payload | |
22 | */ | |
23 | struct key_type key_type_user = { | |
4bdf0bc3 | 24 | .name = "user", |
f9167789 DH |
25 | .preparse = user_preparse, |
26 | .free_preparse = user_free_preparse, | |
27 | .instantiate = generic_key_instantiate, | |
4bdf0bc3 | 28 | .update = user_update, |
4bdf0bc3 DH |
29 | .revoke = user_revoke, |
30 | .destroy = user_destroy, | |
31 | .describe = user_describe, | |
32 | .read = user_read, | |
1da177e4 LT |
33 | }; |
34 | ||
16c29b67 MH |
35 | EXPORT_SYMBOL_GPL(key_type_user); |
36 | ||
9f6ed2ca JL |
37 | /* |
38 | * This key type is essentially the same as key_type_user, but it does | |
39 | * not define a .read op. This is suitable for storing username and | |
40 | * password pairs in the keyring that you do not want to be readable | |
41 | * from userspace. | |
42 | */ | |
43 | struct key_type key_type_logon = { | |
44 | .name = "logon", | |
f9167789 DH |
45 | .preparse = user_preparse, |
46 | .free_preparse = user_free_preparse, | |
47 | .instantiate = generic_key_instantiate, | |
9f6ed2ca | 48 | .update = user_update, |
9f6ed2ca JL |
49 | .revoke = user_revoke, |
50 | .destroy = user_destroy, | |
51 | .describe = user_describe, | |
52 | .vet_description = logon_vet_description, | |
53 | }; | |
54 | EXPORT_SYMBOL_GPL(key_type_logon); | |
55 | ||
1da177e4 | 56 | /* |
f9167789 | 57 | * Preparse a user defined key payload |
1da177e4 | 58 | */ |
f9167789 | 59 | int user_preparse(struct key_preparsed_payload *prep) |
1da177e4 | 60 | { |
76d8aeab | 61 | struct user_key_payload *upayload; |
cf7f601c | 62 | size_t datalen = prep->datalen; |
1da177e4 | 63 | |
cf7f601c | 64 | if (datalen <= 0 || datalen > 32767 || !prep->data) |
f9167789 | 65 | return -EINVAL; |
1da177e4 | 66 | |
76d8aeab DH |
67 | upayload = kmalloc(sizeof(*upayload) + datalen, GFP_KERNEL); |
68 | if (!upayload) | |
f9167789 | 69 | return -ENOMEM; |
1da177e4 | 70 | |
76d8aeab | 71 | /* attach the data */ |
f9167789 | 72 | prep->quotalen = datalen; |
146aa8b1 | 73 | prep->payload.data[0] = upayload; |
76d8aeab | 74 | upayload->datalen = datalen; |
cf7f601c | 75 | memcpy(upayload->data, prep->data, datalen); |
f9167789 | 76 | return 0; |
a8b17ed0 | 77 | } |
f9167789 | 78 | EXPORT_SYMBOL_GPL(user_preparse); |
31204ed9 | 79 | |
f9167789 DH |
80 | /* |
81 | * Free a preparse of a user defined key payload | |
82 | */ | |
83 | void user_free_preparse(struct key_preparsed_payload *prep) | |
84 | { | |
453431a5 | 85 | kfree_sensitive(prep->payload.data[0]); |
f9167789 DH |
86 | } |
87 | EXPORT_SYMBOL_GPL(user_free_preparse); | |
2aa349f6 | 88 | |
6966c749 EB |
89 | static void user_free_payload_rcu(struct rcu_head *head) |
90 | { | |
91 | struct user_key_payload *payload; | |
92 | ||
93 | payload = container_of(head, struct user_key_payload, rcu); | |
453431a5 | 94 | kfree_sensitive(payload); |
6966c749 EB |
95 | } |
96 | ||
1da177e4 LT |
97 | /* |
98 | * update a user defined key | |
76d8aeab | 99 | * - the key's semaphore is write-locked |
1da177e4 | 100 | */ |
cf7f601c | 101 | int user_update(struct key *key, struct key_preparsed_payload *prep) |
1da177e4 | 102 | { |
898de7d0 | 103 | struct user_key_payload *zap = NULL; |
1da177e4 LT |
104 | int ret; |
105 | ||
1da177e4 | 106 | /* check the quota and attach the new data */ |
898de7d0 DH |
107 | ret = key_payload_reserve(key, prep->datalen); |
108 | if (ret < 0) | |
109 | return ret; | |
110 | ||
111 | /* attach the new data, displacing the old */ | |
112 | key->expiry = prep->expiry; | |
363b02da | 113 | if (key_is_positive(key)) |
0837e49a | 114 | zap = dereference_key_locked(key); |
898de7d0 DH |
115 | rcu_assign_keypointer(key, prep->payload.data[0]); |
116 | prep->payload.data[0] = NULL; | |
1da177e4 | 117 | |
9f35a33b | 118 | if (zap) |
6966c749 | 119 | call_rcu(&zap->rcu, user_free_payload_rcu); |
1da177e4 | 120 | return ret; |
a8b17ed0 | 121 | } |
2aa349f6 DH |
122 | EXPORT_SYMBOL_GPL(user_update); |
123 | ||
1da177e4 | 124 | /* |
31204ed9 DH |
125 | * dispose of the links from a revoked keyring |
126 | * - called with the key sem write-locked | |
127 | */ | |
128 | void user_revoke(struct key *key) | |
129 | { | |
0837e49a | 130 | struct user_key_payload *upayload = user_key_payload_locked(key); |
31204ed9 DH |
131 | |
132 | /* clear the quota */ | |
133 | key_payload_reserve(key, 0); | |
134 | ||
135 | if (upayload) { | |
f6b24579 | 136 | rcu_assign_keypointer(key, NULL); |
6966c749 | 137 | call_rcu(&upayload->rcu, user_free_payload_rcu); |
31204ed9 | 138 | } |
a8b17ed0 | 139 | } |
31204ed9 DH |
140 | |
141 | EXPORT_SYMBOL(user_revoke); | |
142 | ||
31204ed9 DH |
143 | /* |
144 | * dispose of the data dangling from the corpse of a user key | |
1da177e4 | 145 | */ |
2aa349f6 | 146 | void user_destroy(struct key *key) |
1da177e4 | 147 | { |
146aa8b1 | 148 | struct user_key_payload *upayload = key->payload.data[0]; |
76d8aeab | 149 | |
453431a5 | 150 | kfree_sensitive(upayload); |
a8b17ed0 | 151 | } |
1da177e4 | 152 | |
2aa349f6 DH |
153 | EXPORT_SYMBOL_GPL(user_destroy); |
154 | ||
1da177e4 | 155 | /* |
76d8aeab | 156 | * describe the user key |
1da177e4 | 157 | */ |
2aa349f6 | 158 | void user_describe(const struct key *key, struct seq_file *m) |
1da177e4 LT |
159 | { |
160 | seq_puts(m, key->description); | |
363b02da | 161 | if (key_is_positive(key)) |
78b7280c | 162 | seq_printf(m, ": %u", key->datalen); |
a8b17ed0 | 163 | } |
1da177e4 | 164 | |
2aa349f6 DH |
165 | EXPORT_SYMBOL_GPL(user_describe); |
166 | ||
1da177e4 LT |
167 | /* |
168 | * read the key data | |
76d8aeab | 169 | * - the key's semaphore is read-locked |
1da177e4 | 170 | */ |
d3ec10aa | 171 | long user_read(const struct key *key, char *buffer, size_t buflen) |
1da177e4 | 172 | { |
146aa8b1 | 173 | const struct user_key_payload *upayload; |
76d8aeab DH |
174 | long ret; |
175 | ||
0837e49a | 176 | upayload = user_key_payload_locked(key); |
76d8aeab | 177 | ret = upayload->datalen; |
1da177e4 LT |
178 | |
179 | /* we can return the data as is */ | |
180 | if (buffer && buflen > 0) { | |
76d8aeab DH |
181 | if (buflen > upayload->datalen) |
182 | buflen = upayload->datalen; | |
1da177e4 | 183 | |
d3ec10aa | 184 | memcpy(buffer, upayload->data, buflen); |
1da177e4 LT |
185 | } |
186 | ||
187 | return ret; | |
a8b17ed0 | 188 | } |
2aa349f6 DH |
189 | |
190 | EXPORT_SYMBOL_GPL(user_read); | |
9f6ed2ca JL |
191 | |
192 | /* Vet the description for a "logon" key */ | |
193 | static int logon_vet_description(const char *desc) | |
194 | { | |
195 | char *p; | |
196 | ||
197 | /* require a "qualified" description string */ | |
198 | p = strchr(desc, ':'); | |
199 | if (!p) | |
200 | return -EINVAL; | |
201 | ||
202 | /* also reject description with ':' as first char */ | |
203 | if (p == desc) | |
204 | return -EINVAL; | |
205 | ||
206 | return 0; | |
207 | } |