1 /* user_defined.c: user defined key type
3 * Copyright (C) 2004 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
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.
12 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/slab.h>
15 #include <linux/seq_file.h>
16 #include <linux/err.h>
17 #include <keys/user-type.h>
18 #include <asm/uaccess.h>
21 static int logon_vet_description(const char *desc);
24 * user defined keys take an arbitrary string as the description and an
25 * arbitrary blob of data as the payload
27 struct key_type key_type_user = {
29 .preparse = user_preparse,
30 .free_preparse = user_free_preparse,
31 .instantiate = generic_key_instantiate,
32 .update = user_update,
34 .revoke = user_revoke,
35 .destroy = user_destroy,
36 .describe = user_describe,
40 EXPORT_SYMBOL_GPL(key_type_user);
43 * This key type is essentially the same as key_type_user, but it does
44 * not define a .read op. This is suitable for storing username and
45 * password pairs in the keyring that you do not want to be readable
48 struct key_type key_type_logon = {
50 .preparse = user_preparse,
51 .free_preparse = user_free_preparse,
52 .instantiate = generic_key_instantiate,
53 .update = user_update,
55 .revoke = user_revoke,
56 .destroy = user_destroy,
57 .describe = user_describe,
58 .vet_description = logon_vet_description,
60 EXPORT_SYMBOL_GPL(key_type_logon);
63 * Preparse a user defined key payload
65 int user_preparse(struct key_preparsed_payload *prep)
67 struct user_key_payload *upayload;
68 size_t datalen = prep->datalen;
70 if (datalen <= 0 || datalen > 32767 || !prep->data)
73 upayload = kmalloc(sizeof(*upayload) + datalen, GFP_KERNEL);
78 prep->quotalen = datalen;
79 prep->payload[0] = upayload;
80 upayload->datalen = datalen;
81 memcpy(upayload->data, prep->data, datalen);
84 EXPORT_SYMBOL_GPL(user_preparse);
87 * Free a preparse of a user defined key payload
89 void user_free_preparse(struct key_preparsed_payload *prep)
91 kfree(prep->payload[0]);
93 EXPORT_SYMBOL_GPL(user_free_preparse);
96 * update a user defined key
97 * - the key's semaphore is write-locked
99 int user_update(struct key *key, struct key_preparsed_payload *prep)
101 struct user_key_payload *upayload, *zap;
102 size_t datalen = prep->datalen;
106 if (datalen <= 0 || datalen > 32767 || !prep->data)
109 /* construct a replacement payload */
111 upayload = kmalloc(sizeof(*upayload) + datalen, GFP_KERNEL);
115 upayload->datalen = datalen;
116 memcpy(upayload->data, prep->data, datalen);
118 /* check the quota and attach the new data */
121 ret = key_payload_reserve(key, datalen);
124 /* attach the new data, displacing the old */
125 zap = key->payload.data;
126 rcu_assign_keypointer(key, upayload);
137 EXPORT_SYMBOL_GPL(user_update);
140 * match users on their name
142 int user_match(const struct key *key, const struct key_match_data *match_data)
144 return strcmp(key->description, match_data->raw_data) == 0;
147 EXPORT_SYMBOL_GPL(user_match);
150 * dispose of the links from a revoked keyring
151 * - called with the key sem write-locked
153 void user_revoke(struct key *key)
155 struct user_key_payload *upayload = key->payload.data;
157 /* clear the quota */
158 key_payload_reserve(key, 0);
161 rcu_assign_keypointer(key, NULL);
162 kfree_rcu(upayload, rcu);
166 EXPORT_SYMBOL(user_revoke);
169 * dispose of the data dangling from the corpse of a user key
171 void user_destroy(struct key *key)
173 struct user_key_payload *upayload = key->payload.data;
178 EXPORT_SYMBOL_GPL(user_destroy);
181 * describe the user key
183 void user_describe(const struct key *key, struct seq_file *m)
185 seq_puts(m, key->description);
186 if (key_is_instantiated(key))
187 seq_printf(m, ": %u", key->datalen);
190 EXPORT_SYMBOL_GPL(user_describe);
194 * - the key's semaphore is read-locked
196 long user_read(const struct key *key, char __user *buffer, size_t buflen)
198 struct user_key_payload *upayload;
201 upayload = rcu_dereference_key(key);
202 ret = upayload->datalen;
204 /* we can return the data as is */
205 if (buffer && buflen > 0) {
206 if (buflen > upayload->datalen)
207 buflen = upayload->datalen;
209 if (copy_to_user(buffer, upayload->data, buflen) != 0)
216 EXPORT_SYMBOL_GPL(user_read);
218 /* Vet the description for a "logon" key */
219 static int logon_vet_description(const char *desc)
223 /* require a "qualified" description string */
224 p = strchr(desc, ':');
228 /* also reject description with ':' as first char */