[SPARC]: Kill BOOTME_SINGLE.
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / security / keys / user_defined.c
blob5bbfdebb7acfb79436d615851ec91d57c2d74c49
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/sched.h>
15 #include <linux/slab.h>
16 #include <linux/seq_file.h>
17 #include <linux/err.h>
18 #include <keys/user-type.h>
19 #include <asm/uaccess.h>
20 #include "internal.h"
23 * user defined keys take an arbitrary string as the description and an
24 * arbitrary blob of data as the payload
26 struct key_type key_type_user = {
27 .name = "user",
28 .instantiate = user_instantiate,
29 .update = user_update,
30 .match = user_match,
31 .revoke = user_revoke,
32 .destroy = user_destroy,
33 .describe = user_describe,
34 .read = user_read,
37 EXPORT_SYMBOL_GPL(key_type_user);
39 /*****************************************************************************/
41 * instantiate a user defined key
43 int user_instantiate(struct key *key, const void *data, size_t datalen)
45 struct user_key_payload *upayload;
46 int ret;
48 ret = -EINVAL;
49 if (datalen <= 0 || datalen > 32767 || !data)
50 goto error;
52 ret = key_payload_reserve(key, datalen);
53 if (ret < 0)
54 goto error;
56 ret = -ENOMEM;
57 upayload = kmalloc(sizeof(*upayload) + datalen, GFP_KERNEL);
58 if (!upayload)
59 goto error;
61 /* attach the data */
62 upayload->datalen = datalen;
63 memcpy(upayload->data, data, datalen);
64 rcu_assign_pointer(key->payload.data, upayload);
65 ret = 0;
67 error:
68 return ret;
70 } /* end user_instantiate() */
72 EXPORT_SYMBOL_GPL(user_instantiate);
74 /*****************************************************************************/
76 * dispose of the old data from an updated user defined key
78 static void user_update_rcu_disposal(struct rcu_head *rcu)
80 struct user_key_payload *upayload;
82 upayload = container_of(rcu, struct user_key_payload, rcu);
84 kfree(upayload);
86 } /* end user_update_rcu_disposal() */
88 /*****************************************************************************/
90 * update a user defined key
91 * - the key's semaphore is write-locked
93 int user_update(struct key *key, const void *data, size_t datalen)
95 struct user_key_payload *upayload, *zap;
96 int ret;
98 ret = -EINVAL;
99 if (datalen <= 0 || datalen > 32767 || !data)
100 goto error;
102 /* construct a replacement payload */
103 ret = -ENOMEM;
104 upayload = kmalloc(sizeof(*upayload) + datalen, GFP_KERNEL);
105 if (!upayload)
106 goto error;
108 upayload->datalen = datalen;
109 memcpy(upayload->data, data, datalen);
111 /* check the quota and attach the new data */
112 zap = upayload;
114 ret = key_payload_reserve(key, datalen);
116 if (ret == 0) {
117 /* attach the new data, displacing the old */
118 zap = key->payload.data;
119 rcu_assign_pointer(key->payload.data, upayload);
120 key->expiry = 0;
123 call_rcu(&zap->rcu, user_update_rcu_disposal);
125 error:
126 return ret;
128 } /* end user_update() */
130 EXPORT_SYMBOL_GPL(user_update);
132 /*****************************************************************************/
134 * match users on their name
136 int user_match(const struct key *key, const void *description)
138 return strcmp(key->description, description) == 0;
140 } /* end user_match() */
142 EXPORT_SYMBOL_GPL(user_match);
144 /*****************************************************************************/
146 * dispose of the links from a revoked keyring
147 * - called with the key sem write-locked
149 void user_revoke(struct key *key)
151 struct user_key_payload *upayload = key->payload.data;
153 /* clear the quota */
154 key_payload_reserve(key, 0);
156 if (upayload) {
157 rcu_assign_pointer(key->payload.data, NULL);
158 call_rcu(&upayload->rcu, user_update_rcu_disposal);
161 } /* end user_revoke() */
163 EXPORT_SYMBOL(user_revoke);
165 /*****************************************************************************/
167 * dispose of the data dangling from the corpse of a user key
169 void user_destroy(struct key *key)
171 struct user_key_payload *upayload = key->payload.data;
173 kfree(upayload);
175 } /* end user_destroy() */
177 EXPORT_SYMBOL_GPL(user_destroy);
179 /*****************************************************************************/
181 * describe the user key
183 void user_describe(const struct key *key, struct seq_file *m)
185 seq_puts(m, key->description);
187 seq_printf(m, ": %u", key->datalen);
189 } /* end user_describe() */
191 EXPORT_SYMBOL_GPL(user_describe);
193 /*****************************************************************************/
195 * read the key data
196 * - the key's semaphore is read-locked
198 long user_read(const struct key *key, char __user *buffer, size_t buflen)
200 struct user_key_payload *upayload;
201 long ret;
203 upayload = rcu_dereference(key->payload.data);
204 ret = upayload->datalen;
206 /* we can return the data as is */
207 if (buffer && buflen > 0) {
208 if (buflen > upayload->datalen)
209 buflen = upayload->datalen;
211 if (copy_to_user(buffer, upayload->data, buflen) != 0)
212 ret = -EFAULT;
215 return ret;
217 } /* end user_read() */
219 EXPORT_SYMBOL_GPL(user_read);