[PATCH] kexec: x86_64 kexec implementation
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / security / keys / user_defined.c
blobe446acba73d325a1d222a0a130c7f23f6a4232a9
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 <asm/uaccess.h>
19 #include "internal.h"
21 static int user_instantiate(struct key *key, const void *data, size_t datalen);
22 static int user_duplicate(struct key *key, const struct key *source);
23 static int user_update(struct key *key, const void *data, size_t datalen);
24 static int user_match(const struct key *key, const void *criterion);
25 static void user_destroy(struct key *key);
26 static void user_describe(const struct key *user, struct seq_file *m);
27 static long user_read(const struct key *key,
28 char __user *buffer, size_t buflen);
31 * user defined keys take an arbitrary string as the description and an
32 * arbitrary blob of data as the payload
34 struct key_type key_type_user = {
35 .name = "user",
36 .instantiate = user_instantiate,
37 .duplicate = user_duplicate,
38 .update = user_update,
39 .match = user_match,
40 .destroy = user_destroy,
41 .describe = user_describe,
42 .read = user_read,
45 struct user_key_payload {
46 struct rcu_head rcu; /* RCU destructor */
47 unsigned short datalen; /* length of this data */
48 char data[0]; /* actual data */
51 EXPORT_SYMBOL_GPL(key_type_user);
53 /*****************************************************************************/
55 * instantiate a user defined key
57 static int user_instantiate(struct key *key, const void *data, size_t datalen)
59 struct user_key_payload *upayload;
60 int ret;
62 ret = -EINVAL;
63 if (datalen <= 0 || datalen > 32767 || !data)
64 goto error;
66 ret = key_payload_reserve(key, datalen);
67 if (ret < 0)
68 goto error;
70 ret = -ENOMEM;
71 upayload = kmalloc(sizeof(*upayload) + datalen, GFP_KERNEL);
72 if (!upayload)
73 goto error;
75 /* attach the data */
76 upayload->datalen = datalen;
77 memcpy(upayload->data, data, datalen);
78 rcu_assign_pointer(key->payload.data, upayload);
79 ret = 0;
81 error:
82 return ret;
84 } /* end user_instantiate() */
86 /*****************************************************************************/
88 * duplicate a user defined key
89 * - both keys' semaphores are locked against further modification
90 * - the new key cannot yet be accessed
92 static int user_duplicate(struct key *key, const struct key *source)
94 struct user_key_payload *upayload, *spayload;
95 int ret;
97 /* just copy the payload */
98 ret = -ENOMEM;
99 upayload = kmalloc(sizeof(*upayload) + source->datalen, GFP_KERNEL);
100 if (upayload) {
101 spayload = rcu_dereference(source->payload.data);
102 BUG_ON(source->datalen != spayload->datalen);
104 upayload->datalen = key->datalen = spayload->datalen;
105 memcpy(upayload->data, spayload->data, key->datalen);
107 key->payload.data = upayload;
108 ret = 0;
111 return ret;
113 } /* end user_duplicate() */
115 /*****************************************************************************/
117 * dispose of the old data from an updated user defined key
119 static void user_update_rcu_disposal(struct rcu_head *rcu)
121 struct user_key_payload *upayload;
123 upayload = container_of(rcu, struct user_key_payload, rcu);
125 kfree(upayload);
127 } /* end user_update_rcu_disposal() */
129 /*****************************************************************************/
131 * update a user defined key
132 * - the key's semaphore is write-locked
134 static int user_update(struct key *key, const void *data, size_t datalen)
136 struct user_key_payload *upayload, *zap;
137 int ret;
139 ret = -EINVAL;
140 if (datalen <= 0 || datalen > 32767 || !data)
141 goto error;
143 /* construct a replacement payload */
144 ret = -ENOMEM;
145 upayload = kmalloc(sizeof(*upayload) + datalen, GFP_KERNEL);
146 if (!upayload)
147 goto error;
149 upayload->datalen = datalen;
150 memcpy(upayload->data, data, datalen);
152 /* check the quota and attach the new data */
153 zap = upayload;
155 ret = key_payload_reserve(key, datalen);
157 if (ret == 0) {
158 /* attach the new data, displacing the old */
159 zap = key->payload.data;
160 rcu_assign_pointer(key->payload.data, upayload);
161 key->expiry = 0;
164 call_rcu(&zap->rcu, user_update_rcu_disposal);
166 error:
167 return ret;
169 } /* end user_update() */
171 /*****************************************************************************/
173 * match users on their name
175 static int user_match(const struct key *key, const void *description)
177 return strcmp(key->description, description) == 0;
179 } /* end user_match() */
181 /*****************************************************************************/
183 * dispose of the data dangling from the corpse of a user
185 static void user_destroy(struct key *key)
187 struct user_key_payload *upayload = key->payload.data;
189 kfree(upayload);
191 } /* end user_destroy() */
193 /*****************************************************************************/
195 * describe the user key
197 static void user_describe(const struct key *key, struct seq_file *m)
199 seq_puts(m, key->description);
201 seq_printf(m, ": %u", key->datalen);
203 } /* end user_describe() */
205 /*****************************************************************************/
207 * read the key data
208 * - the key's semaphore is read-locked
210 static long user_read(const struct key *key,
211 char __user *buffer, size_t buflen)
213 struct user_key_payload *upayload;
214 long ret;
216 upayload = rcu_dereference(key->payload.data);
217 ret = upayload->datalen;
219 /* we can return the data as is */
220 if (buffer && buflen > 0) {
221 if (buflen > upayload->datalen)
222 buflen = upayload->datalen;
224 if (copy_to_user(buffer, upayload->data, buflen) != 0)
225 ret = -EFAULT;
228 return ret;
230 } /* end user_read() */