KEYS: Remove key_type::def_lookup_type
[pandora-kernel.git] / security / keys / user_defined.c
1 /* user_defined.c: user defined key type
2  *
3  * Copyright (C) 2004 Red Hat, Inc. All Rights Reserved.
4  * Written by David Howells (dhowells@redhat.com)
5  *
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  */
11
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>
19 #include "internal.h"
20
21 static int logon_vet_description(const char *desc);
22
23 /*
24  * user defined keys take an arbitrary string as the description and an
25  * arbitrary blob of data as the payload
26  */
27 struct key_type key_type_user = {
28         .name                   = "user",
29         .preparse               = user_preparse,
30         .free_preparse          = user_free_preparse,
31         .instantiate            = generic_key_instantiate,
32         .update                 = user_update,
33         .match                  = user_match,
34         .revoke                 = user_revoke,
35         .destroy                = user_destroy,
36         .describe               = user_describe,
37         .read                   = user_read,
38 };
39
40 EXPORT_SYMBOL_GPL(key_type_user);
41
42 /*
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
46  * from userspace.
47  */
48 struct key_type key_type_logon = {
49         .name                   = "logon",
50         .preparse               = user_preparse,
51         .free_preparse          = user_free_preparse,
52         .instantiate            = generic_key_instantiate,
53         .update                 = user_update,
54         .match                  = user_match,
55         .revoke                 = user_revoke,
56         .destroy                = user_destroy,
57         .describe               = user_describe,
58         .vet_description        = logon_vet_description,
59 };
60 EXPORT_SYMBOL_GPL(key_type_logon);
61
62 /*
63  * Preparse a user defined key payload
64  */
65 int user_preparse(struct key_preparsed_payload *prep)
66 {
67         struct user_key_payload *upayload;
68         size_t datalen = prep->datalen;
69
70         if (datalen <= 0 || datalen > 32767 || !prep->data)
71                 return -EINVAL;
72
73         upayload = kmalloc(sizeof(*upayload) + datalen, GFP_KERNEL);
74         if (!upayload)
75                 return -ENOMEM;
76
77         /* attach the data */
78         prep->quotalen = datalen;
79         prep->payload[0] = upayload;
80         upayload->datalen = datalen;
81         memcpy(upayload->data, prep->data, datalen);
82         return 0;
83 }
84 EXPORT_SYMBOL_GPL(user_preparse);
85
86 /*
87  * Free a preparse of a user defined key payload
88  */
89 void user_free_preparse(struct key_preparsed_payload *prep)
90 {
91         kfree(prep->payload[0]);
92 }
93 EXPORT_SYMBOL_GPL(user_free_preparse);
94
95 /*
96  * update a user defined key
97  * - the key's semaphore is write-locked
98  */
99 int user_update(struct key *key, struct key_preparsed_payload *prep)
100 {
101         struct user_key_payload *upayload, *zap;
102         size_t datalen = prep->datalen;
103         int ret;
104
105         ret = -EINVAL;
106         if (datalen <= 0 || datalen > 32767 || !prep->data)
107                 goto error;
108
109         /* construct a replacement payload */
110         ret = -ENOMEM;
111         upayload = kmalloc(sizeof(*upayload) + datalen, GFP_KERNEL);
112         if (!upayload)
113                 goto error;
114
115         upayload->datalen = datalen;
116         memcpy(upayload->data, prep->data, datalen);
117
118         /* check the quota and attach the new data */
119         zap = upayload;
120
121         ret = key_payload_reserve(key, datalen);
122
123         if (ret == 0) {
124                 /* attach the new data, displacing the old */
125                 zap = key->payload.data;
126                 rcu_assign_keypointer(key, upayload);
127                 key->expiry = 0;
128         }
129
130         if (zap)
131                 kfree_rcu(zap, rcu);
132
133 error:
134         return ret;
135 }
136
137 EXPORT_SYMBOL_GPL(user_update);
138
139 /*
140  * match users on their name
141  */
142 int user_match(const struct key *key, const struct key_match_data *match_data)
143 {
144         return strcmp(key->description, match_data->raw_data) == 0;
145 }
146
147 EXPORT_SYMBOL_GPL(user_match);
148
149 /*
150  * dispose of the links from a revoked keyring
151  * - called with the key sem write-locked
152  */
153 void user_revoke(struct key *key)
154 {
155         struct user_key_payload *upayload = key->payload.data;
156
157         /* clear the quota */
158         key_payload_reserve(key, 0);
159
160         if (upayload) {
161                 rcu_assign_keypointer(key, NULL);
162                 kfree_rcu(upayload, rcu);
163         }
164 }
165
166 EXPORT_SYMBOL(user_revoke);
167
168 /*
169  * dispose of the data dangling from the corpse of a user key
170  */
171 void user_destroy(struct key *key)
172 {
173         struct user_key_payload *upayload = key->payload.data;
174
175         kfree(upayload);
176 }
177
178 EXPORT_SYMBOL_GPL(user_destroy);
179
180 /*
181  * describe the user key
182  */
183 void user_describe(const struct key *key, struct seq_file *m)
184 {
185         seq_puts(m, key->description);
186         if (key_is_instantiated(key))
187                 seq_printf(m, ": %u", key->datalen);
188 }
189
190 EXPORT_SYMBOL_GPL(user_describe);
191
192 /*
193  * read the key data
194  * - the key's semaphore is read-locked
195  */
196 long user_read(const struct key *key, char __user *buffer, size_t buflen)
197 {
198         struct user_key_payload *upayload;
199         long ret;
200
201         upayload = rcu_dereference_key(key);
202         ret = upayload->datalen;
203
204         /* we can return the data as is */
205         if (buffer && buflen > 0) {
206                 if (buflen > upayload->datalen)
207                         buflen = upayload->datalen;
208
209                 if (copy_to_user(buffer, upayload->data, buflen) != 0)
210                         ret = -EFAULT;
211         }
212
213         return ret;
214 }
215
216 EXPORT_SYMBOL_GPL(user_read);
217
218 /* Vet the description for a "logon" key */
219 static int logon_vet_description(const char *desc)
220 {
221         char *p;
222
223         /* require a "qualified" description string */
224         p = strchr(desc, ':');
225         if (!p)
226                 return -EINVAL;
227
228         /* also reject description with ':' as first char */
229         if (p == desc)
230                 return -EINVAL;
231
232         return 0;
233 }