Merge branch 'master' of /usr/src/ntfs-2.6/
[sfrench/cifs-2.6.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 <keys/user-type.h>
19 #include <asm/uaccess.h>
20 #include "internal.h"
21
22 /*
23  * user defined keys take an arbitrary string as the description and an
24  * arbitrary blob of data as the payload
25  */
26 struct key_type key_type_user = {
27         .name           = "user",
28         .instantiate    = user_instantiate,
29         .update         = user_update,
30         .match          = user_match,
31         .destroy        = user_destroy,
32         .describe       = user_describe,
33         .read           = user_read,
34 };
35
36 EXPORT_SYMBOL_GPL(key_type_user);
37
38 /*****************************************************************************/
39 /*
40  * instantiate a user defined key
41  */
42 int user_instantiate(struct key *key, const void *data, size_t datalen)
43 {
44         struct user_key_payload *upayload;
45         int ret;
46
47         ret = -EINVAL;
48         if (datalen <= 0 || datalen > 32767 || !data)
49                 goto error;
50
51         ret = key_payload_reserve(key, datalen);
52         if (ret < 0)
53                 goto error;
54
55         ret = -ENOMEM;
56         upayload = kmalloc(sizeof(*upayload) + datalen, GFP_KERNEL);
57         if (!upayload)
58                 goto error;
59
60         /* attach the data */
61         upayload->datalen = datalen;
62         memcpy(upayload->data, data, datalen);
63         rcu_assign_pointer(key->payload.data, upayload);
64         ret = 0;
65
66 error:
67         return ret;
68
69 } /* end user_instantiate() */
70 EXPORT_SYMBOL_GPL(user_instantiate);
71
72 /*****************************************************************************/
73 /*
74  * dispose of the old data from an updated user defined key
75  */
76 static void user_update_rcu_disposal(struct rcu_head *rcu)
77 {
78         struct user_key_payload *upayload;
79
80         upayload = container_of(rcu, struct user_key_payload, rcu);
81
82         kfree(upayload);
83
84 } /* end user_update_rcu_disposal() */
85
86 /*****************************************************************************/
87 /*
88  * update a user defined key
89  * - the key's semaphore is write-locked
90  */
91 int user_update(struct key *key, const void *data, size_t datalen)
92 {
93         struct user_key_payload *upayload, *zap;
94         int ret;
95
96         ret = -EINVAL;
97         if (datalen <= 0 || datalen > 32767 || !data)
98                 goto error;
99
100         /* construct a replacement payload */
101         ret = -ENOMEM;
102         upayload = kmalloc(sizeof(*upayload) + datalen, GFP_KERNEL);
103         if (!upayload)
104                 goto error;
105
106         upayload->datalen = datalen;
107         memcpy(upayload->data, data, datalen);
108
109         /* check the quota and attach the new data */
110         zap = upayload;
111
112         ret = key_payload_reserve(key, datalen);
113
114         if (ret == 0) {
115                 /* attach the new data, displacing the old */
116                 zap = key->payload.data;
117                 rcu_assign_pointer(key->payload.data, upayload);
118                 key->expiry = 0;
119         }
120
121         call_rcu(&zap->rcu, user_update_rcu_disposal);
122
123 error:
124         return ret;
125
126 } /* end user_update() */
127
128 EXPORT_SYMBOL_GPL(user_update);
129
130 /*****************************************************************************/
131 /*
132  * match users on their name
133  */
134 int user_match(const struct key *key, const void *description)
135 {
136         return strcmp(key->description, description) == 0;
137
138 } /* end user_match() */
139
140 EXPORT_SYMBOL_GPL(user_match);
141
142 /*****************************************************************************/
143 /*
144  * dispose of the data dangling from the corpse of a user
145  */
146 void user_destroy(struct key *key)
147 {
148         struct user_key_payload *upayload = key->payload.data;
149
150         kfree(upayload);
151
152 } /* end user_destroy() */
153
154 EXPORT_SYMBOL_GPL(user_destroy);
155
156 /*****************************************************************************/
157 /*
158  * describe the user key
159  */
160 void user_describe(const struct key *key, struct seq_file *m)
161 {
162         seq_puts(m, key->description);
163
164         seq_printf(m, ": %u", key->datalen);
165
166 } /* end user_describe() */
167
168 EXPORT_SYMBOL_GPL(user_describe);
169
170 /*****************************************************************************/
171 /*
172  * read the key data
173  * - the key's semaphore is read-locked
174  */
175 long user_read(const struct key *key, char __user *buffer, size_t buflen)
176 {
177         struct user_key_payload *upayload;
178         long ret;
179
180         upayload = rcu_dereference(key->payload.data);
181         ret = upayload->datalen;
182
183         /* we can return the data as is */
184         if (buffer && buflen > 0) {
185                 if (buflen > upayload->datalen)
186                         buflen = upayload->datalen;
187
188                 if (copy_to_user(buffer, upayload->data, buflen) != 0)
189                         ret = -EFAULT;
190         }
191
192         return ret;
193
194 } /* end user_read() */
195
196 EXPORT_SYMBOL_GPL(user_read);