[PATCH] dpt_i2o fix for deadlock condition
[linux-2.6/openmoko-kernel/knife-kernel.git] / security / keys / user_defined.c
blobcbda3b2780a100696b38e7ce47dec9e3eb46cf33
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 .duplicate = user_duplicate,
30 .update = user_update,
31 .match = user_match,
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 * duplicate a user defined key
77 * - both keys' semaphores are locked against further modification
78 * - the new key cannot yet be accessed
80 int user_duplicate(struct key *key, const struct key *source)
82 struct user_key_payload *upayload, *spayload;
83 int ret;
85 /* just copy the payload */
86 ret = -ENOMEM;
87 upayload = kmalloc(sizeof(*upayload) + source->datalen, GFP_KERNEL);
88 if (upayload) {
89 spayload = rcu_dereference(source->payload.data);
90 BUG_ON(source->datalen != spayload->datalen);
92 upayload->datalen = key->datalen = spayload->datalen;
93 memcpy(upayload->data, spayload->data, key->datalen);
95 key->payload.data = upayload;
96 ret = 0;
99 return ret;
101 } /* end user_duplicate() */
103 EXPORT_SYMBOL_GPL(user_duplicate);
105 /*****************************************************************************/
107 * dispose of the old data from an updated user defined key
109 static void user_update_rcu_disposal(struct rcu_head *rcu)
111 struct user_key_payload *upayload;
113 upayload = container_of(rcu, struct user_key_payload, rcu);
115 kfree(upayload);
117 } /* end user_update_rcu_disposal() */
119 /*****************************************************************************/
121 * update a user defined key
122 * - the key's semaphore is write-locked
124 int user_update(struct key *key, const void *data, size_t datalen)
126 struct user_key_payload *upayload, *zap;
127 int ret;
129 ret = -EINVAL;
130 if (datalen <= 0 || datalen > 32767 || !data)
131 goto error;
133 /* construct a replacement payload */
134 ret = -ENOMEM;
135 upayload = kmalloc(sizeof(*upayload) + datalen, GFP_KERNEL);
136 if (!upayload)
137 goto error;
139 upayload->datalen = datalen;
140 memcpy(upayload->data, data, datalen);
142 /* check the quota and attach the new data */
143 zap = upayload;
145 ret = key_payload_reserve(key, datalen);
147 if (ret == 0) {
148 /* attach the new data, displacing the old */
149 zap = key->payload.data;
150 rcu_assign_pointer(key->payload.data, upayload);
151 key->expiry = 0;
154 call_rcu(&zap->rcu, user_update_rcu_disposal);
156 error:
157 return ret;
159 } /* end user_update() */
161 EXPORT_SYMBOL_GPL(user_update);
163 /*****************************************************************************/
165 * match users on their name
167 int user_match(const struct key *key, const void *description)
169 return strcmp(key->description, description) == 0;
171 } /* end user_match() */
173 EXPORT_SYMBOL_GPL(user_match);
175 /*****************************************************************************/
177 * dispose of the data dangling from the corpse of a user
179 void user_destroy(struct key *key)
181 struct user_key_payload *upayload = key->payload.data;
183 kfree(upayload);
185 } /* end user_destroy() */
187 EXPORT_SYMBOL_GPL(user_destroy);
189 /*****************************************************************************/
191 * describe the user key
193 void user_describe(const struct key *key, struct seq_file *m)
195 seq_puts(m, key->description);
197 seq_printf(m, ": %u", key->datalen);
199 } /* end user_describe() */
201 EXPORT_SYMBOL_GPL(user_describe);
203 /*****************************************************************************/
205 * read the key data
206 * - the key's semaphore is read-locked
208 long user_read(const struct key *key, char __user *buffer, size_t buflen)
210 struct user_key_payload *upayload;
211 long ret;
213 upayload = rcu_dereference(key->payload.data);
214 ret = upayload->datalen;
216 /* we can return the data as is */
217 if (buffer && buflen > 0) {
218 if (buflen > upayload->datalen)
219 buflen = upayload->datalen;
221 if (copy_to_user(buffer, upayload->data, buflen) != 0)
222 ret = -EFAULT;
225 return ret;
227 } /* end user_read() */
229 EXPORT_SYMBOL_GPL(user_read);