x86, quirk: Fix SB600 revision check
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / security / keys / user_defined.c
blob02807fb163401bcbc9ff5cfc74bd6576b06ec918
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>
19 #include "internal.h"
22 * user defined keys take an arbitrary string as the description and an
23 * arbitrary blob of data as the payload
25 struct key_type key_type_user = {
26 .name = "user",
27 .instantiate = user_instantiate,
28 .update = user_update,
29 .match = user_match,
30 .revoke = user_revoke,
31 .destroy = user_destroy,
32 .describe = user_describe,
33 .read = user_read,
36 EXPORT_SYMBOL_GPL(key_type_user);
39 * instantiate a user defined key
41 int user_instantiate(struct key *key, const void *data, size_t datalen)
43 struct user_key_payload *upayload;
44 int ret;
46 ret = -EINVAL;
47 if (datalen <= 0 || datalen > 32767 || !data)
48 goto error;
50 ret = key_payload_reserve(key, datalen);
51 if (ret < 0)
52 goto error;
54 ret = -ENOMEM;
55 upayload = kmalloc(sizeof(*upayload) + datalen, GFP_KERNEL);
56 if (!upayload)
57 goto error;
59 /* attach the data */
60 upayload->datalen = datalen;
61 memcpy(upayload->data, data, datalen);
62 rcu_assign_pointer(key->payload.data, upayload);
63 ret = 0;
65 error:
66 return ret;
69 EXPORT_SYMBOL_GPL(user_instantiate);
72 * dispose of the old data from an updated user defined key
74 static void user_update_rcu_disposal(struct rcu_head *rcu)
76 struct user_key_payload *upayload;
78 upayload = container_of(rcu, struct user_key_payload, rcu);
80 kfree(upayload);
84 * update a user defined key
85 * - the key's semaphore is write-locked
87 int user_update(struct key *key, const void *data, size_t datalen)
89 struct user_key_payload *upayload, *zap;
90 int ret;
92 ret = -EINVAL;
93 if (datalen <= 0 || datalen > 32767 || !data)
94 goto error;
96 /* construct a replacement payload */
97 ret = -ENOMEM;
98 upayload = kmalloc(sizeof(*upayload) + datalen, GFP_KERNEL);
99 if (!upayload)
100 goto error;
102 upayload->datalen = datalen;
103 memcpy(upayload->data, data, datalen);
105 /* check the quota and attach the new data */
106 zap = upayload;
108 ret = key_payload_reserve(key, datalen);
110 if (ret == 0) {
111 /* attach the new data, displacing the old */
112 zap = key->payload.data;
113 rcu_assign_pointer(key->payload.data, upayload);
114 key->expiry = 0;
117 call_rcu(&zap->rcu, user_update_rcu_disposal);
119 error:
120 return ret;
123 EXPORT_SYMBOL_GPL(user_update);
126 * match users on their name
128 int user_match(const struct key *key, const void *description)
130 return strcmp(key->description, description) == 0;
133 EXPORT_SYMBOL_GPL(user_match);
136 * dispose of the links from a revoked keyring
137 * - called with the key sem write-locked
139 void user_revoke(struct key *key)
141 struct user_key_payload *upayload = key->payload.data;
143 /* clear the quota */
144 key_payload_reserve(key, 0);
146 if (upayload) {
147 rcu_assign_pointer(key->payload.data, NULL);
148 call_rcu(&upayload->rcu, user_update_rcu_disposal);
152 EXPORT_SYMBOL(user_revoke);
155 * dispose of the data dangling from the corpse of a user key
157 void user_destroy(struct key *key)
159 struct user_key_payload *upayload = key->payload.data;
161 kfree(upayload);
164 EXPORT_SYMBOL_GPL(user_destroy);
167 * describe the user key
169 void user_describe(const struct key *key, struct seq_file *m)
171 seq_puts(m, key->description);
173 seq_printf(m, ": %u", key->datalen);
176 EXPORT_SYMBOL_GPL(user_describe);
179 * read the key data
180 * - the key's semaphore is read-locked
182 long user_read(const struct key *key, char __user *buffer, size_t buflen)
184 struct user_key_payload *upayload;
185 long ret;
187 upayload = rcu_dereference_protected(
188 key->payload.data, rwsem_is_locked(&((struct key *)key)->sem));
189 ret = upayload->datalen;
191 /* we can return the data as is */
192 if (buffer && buflen > 0) {
193 if (buflen > upayload->datalen)
194 buflen = upayload->datalen;
196 if (copy_to_user(buffer, upayload->data, buflen) != 0)
197 ret = -EFAULT;
200 return ret;
203 EXPORT_SYMBOL_GPL(user_read);