[PATCH] keys: Discard key spinlock and use RCU for key payload
[linux-flexiantxendom0-natty.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/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"
20
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);
29
30 /*
31  * user defined keys take an arbitrary string as the description and an
32  * arbitrary blob of data as the payload
33  */
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,
43 };
44
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 */
49 };
50
51 /*****************************************************************************/
52 /*
53  * instantiate a user defined key
54  */
55 static int user_instantiate(struct key *key, const void *data, size_t datalen)
56 {
57         struct user_key_payload *upayload;
58         int ret;
59
60         ret = -EINVAL;
61         if (datalen <= 0 || datalen > 32767 || !data)
62                 goto error;
63
64         ret = key_payload_reserve(key, datalen);
65         if (ret < 0)
66                 goto error;
67
68         ret = -ENOMEM;
69         upayload = kmalloc(sizeof(*upayload) + datalen, GFP_KERNEL);
70         if (!upayload)
71                 goto error;
72
73         /* attach the data */
74         upayload->datalen = datalen;
75         memcpy(upayload->data, data, datalen);
76         rcu_assign_pointer(key->payload.data, upayload);
77         ret = 0;
78
79  error:
80         return ret;
81
82 } /* end user_instantiate() */
83
84 /*****************************************************************************/
85 /*
86  * duplicate a user defined key
87  * - both keys' semaphores are locked against further modification
88  * - the new key cannot yet be accessed
89  */
90 static int user_duplicate(struct key *key, const struct key *source)
91 {
92         struct user_key_payload *upayload, *spayload;
93         int ret;
94
95         /* just copy the payload */
96         ret = -ENOMEM;
97         upayload = kmalloc(sizeof(*upayload) + source->datalen, GFP_KERNEL);
98         if (upayload) {
99                 spayload = rcu_dereference(source->payload.data);
100                 BUG_ON(source->datalen != spayload->datalen);
101
102                 upayload->datalen = key->datalen = spayload->datalen;
103                 memcpy(upayload->data, spayload->data, key->datalen);
104
105                 key->payload.data = upayload;
106                 ret = 0;
107         }
108
109         return ret;
110
111 } /* end user_duplicate() */
112
113 /*****************************************************************************/
114 /*
115  * dispose of the old data from an updated user defined key
116  */
117 static void user_update_rcu_disposal(struct rcu_head *rcu)
118 {
119         struct user_key_payload *upayload;
120
121         upayload = container_of(rcu, struct user_key_payload, rcu);
122
123         kfree(upayload);
124
125 } /* end user_update_rcu_disposal() */
126
127 /*****************************************************************************/
128 /*
129  * update a user defined key
130  * - the key's semaphore is write-locked
131  */
132 static int user_update(struct key *key, const void *data, size_t datalen)
133 {
134         struct user_key_payload *upayload, *zap;
135         int ret;
136
137         ret = -EINVAL;
138         if (datalen <= 0 || datalen > 32767 || !data)
139                 goto error;
140
141         /* construct a replacement payload */
142         ret = -ENOMEM;
143         upayload = kmalloc(sizeof(*upayload) + datalen, GFP_KERNEL);
144         if (!upayload)
145                 goto error;
146
147         upayload->datalen = datalen;
148         memcpy(upayload->data, data, datalen);
149
150         /* check the quota and attach the new data */
151         zap = upayload;
152
153         ret = key_payload_reserve(key, datalen);
154
155         if (ret == 0) {
156                 /* attach the new data, displacing the old */
157                 zap = key->payload.data;
158                 rcu_assign_pointer(key->payload.data, upayload);
159                 key->expiry = 0;
160         }
161
162         call_rcu(&zap->rcu, user_update_rcu_disposal);
163
164  error:
165         return ret;
166
167 } /* end user_update() */
168
169 /*****************************************************************************/
170 /*
171  * match users on their name
172  */
173 static int user_match(const struct key *key, const void *description)
174 {
175         return strcmp(key->description, description) == 0;
176
177 } /* end user_match() */
178
179 /*****************************************************************************/
180 /*
181  * dispose of the data dangling from the corpse of a user
182  */
183 static void user_destroy(struct key *key)
184 {
185         struct user_key_payload *upayload = key->payload.data;
186
187         kfree(upayload);
188
189 } /* end user_destroy() */
190
191 /*****************************************************************************/
192 /*
193  * describe the user key
194  */
195 static void user_describe(const struct key *key, struct seq_file *m)
196 {
197         seq_puts(m, key->description);
198
199         seq_printf(m, ": %u", key->datalen);
200
201 } /* end user_describe() */
202
203 /*****************************************************************************/
204 /*
205  * read the key data
206  * - the key's semaphore is read-locked
207  */
208 static long user_read(const struct key *key,
209                       char __user *buffer, size_t buflen)
210 {
211         struct user_key_payload *upayload;
212         long ret;
213
214         upayload = rcu_dereference(key->payload.data);
215         ret = upayload->datalen;
216
217         /* we can return the data as is */
218         if (buffer && buflen > 0) {
219                 if (buflen > upayload->datalen)
220                         buflen = upayload->datalen;
221
222                 if (copy_to_user(buffer, upayload->data, buflen) != 0)
223                         ret = -EFAULT;
224         }
225
226         return ret;
227
228 } /* end user_read() */