blob: e446acba73d325a1d222a0a130c7f23f6a4232a9 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* 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
21static int user_instantiate(struct key *key, const void *data, size_t datalen);
22static int user_duplicate(struct key *key, const struct key *source);
23static int user_update(struct key *key, const void *data, size_t datalen);
24static int user_match(const struct key *key, const void *criterion);
25static void user_destroy(struct key *key);
26static void user_describe(const struct key *user, struct seq_file *m);
27static 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 */
34struct 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
David Howells76d8aea2005-06-23 22:00:49 -070045struct 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
Michael Halcrow16c29b62005-06-23 22:00:58 -070051EXPORT_SYMBOL_GPL(key_type_user);
52
Linus Torvalds1da177e2005-04-16 15:20:36 -070053/*****************************************************************************/
54/*
55 * instantiate a user defined key
56 */
57static int user_instantiate(struct key *key, const void *data, size_t datalen)
58{
David Howells76d8aea2005-06-23 22:00:49 -070059 struct user_key_payload *upayload;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 int ret;
61
62 ret = -EINVAL;
63 if (datalen <= 0 || datalen > 32767 || !data)
64 goto error;
65
66 ret = key_payload_reserve(key, datalen);
67 if (ret < 0)
68 goto error;
69
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 ret = -ENOMEM;
David Howells76d8aea2005-06-23 22:00:49 -070071 upayload = kmalloc(sizeof(*upayload) + datalen, GFP_KERNEL);
72 if (!upayload)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 goto error;
74
David Howells76d8aea2005-06-23 22:00:49 -070075 /* attach the data */
76 upayload->datalen = datalen;
77 memcpy(upayload->data, data, datalen);
78 rcu_assign_pointer(key->payload.data, upayload);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 ret = 0;
80
81 error:
82 return ret;
83
84} /* end user_instantiate() */
85
86/*****************************************************************************/
87/*
88 * duplicate a user defined key
David Howells76d8aea2005-06-23 22:00:49 -070089 * - both keys' semaphores are locked against further modification
90 * - the new key cannot yet be accessed
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 */
92static int user_duplicate(struct key *key, const struct key *source)
93{
David Howells76d8aea2005-06-23 22:00:49 -070094 struct user_key_payload *upayload, *spayload;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 int ret;
96
97 /* just copy the payload */
98 ret = -ENOMEM;
David Howells76d8aea2005-06-23 22:00:49 -070099 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
David Howells76d8aea2005-06-23 22:00:49 -0700104 upayload->datalen = key->datalen = spayload->datalen;
105 memcpy(upayload->data, spayload->data, key->datalen);
106
107 key->payload.data = upayload;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 ret = 0;
109 }
110
111 return ret;
112
113} /* end user_duplicate() */
114
115/*****************************************************************************/
116/*
David Howells76d8aea2005-06-23 22:00:49 -0700117 * dispose of the old data from an updated user defined key
118 */
119static void user_update_rcu_disposal(struct rcu_head *rcu)
120{
121 struct user_key_payload *upayload;
122
123 upayload = container_of(rcu, struct user_key_payload, rcu);
124
125 kfree(upayload);
126
127} /* end user_update_rcu_disposal() */
128
129/*****************************************************************************/
130/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 * update a user defined key
David Howells76d8aea2005-06-23 22:00:49 -0700132 * - the key's semaphore is write-locked
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 */
134static int user_update(struct key *key, const void *data, size_t datalen)
135{
David Howells76d8aea2005-06-23 22:00:49 -0700136 struct user_key_payload *upayload, *zap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 int ret;
138
139 ret = -EINVAL;
140 if (datalen <= 0 || datalen > 32767 || !data)
141 goto error;
142
David Howells76d8aea2005-06-23 22:00:49 -0700143 /* construct a replacement payload */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 ret = -ENOMEM;
David Howells76d8aea2005-06-23 22:00:49 -0700145 upayload = kmalloc(sizeof(*upayload) + datalen, GFP_KERNEL);
146 if (!upayload)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 goto error;
148
David Howells76d8aea2005-06-23 22:00:49 -0700149 upayload->datalen = datalen;
150 memcpy(upayload->data, data, datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
152 /* check the quota and attach the new data */
David Howells76d8aea2005-06-23 22:00:49 -0700153 zap = upayload;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
155 ret = key_payload_reserve(key, datalen);
156
157 if (ret == 0) {
158 /* attach the new data, displacing the old */
159 zap = key->payload.data;
David Howells76d8aea2005-06-23 22:00:49 -0700160 rcu_assign_pointer(key->payload.data, upayload);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 key->expiry = 0;
162 }
163
David Howells76d8aea2005-06-23 22:00:49 -0700164 call_rcu(&zap->rcu, user_update_rcu_disposal);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
166 error:
167 return ret;
168
169} /* end user_update() */
170
171/*****************************************************************************/
172/*
173 * match users on their name
174 */
175static int user_match(const struct key *key, const void *description)
176{
177 return strcmp(key->description, description) == 0;
178
179} /* end user_match() */
180
181/*****************************************************************************/
182/*
183 * dispose of the data dangling from the corpse of a user
184 */
185static void user_destroy(struct key *key)
186{
David Howells76d8aea2005-06-23 22:00:49 -0700187 struct user_key_payload *upayload = key->payload.data;
188
189 kfree(upayload);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
191} /* end user_destroy() */
192
193/*****************************************************************************/
194/*
David Howells76d8aea2005-06-23 22:00:49 -0700195 * describe the user key
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 */
197static void user_describe(const struct key *key, struct seq_file *m)
198{
199 seq_puts(m, key->description);
200
201 seq_printf(m, ": %u", key->datalen);
202
203} /* end user_describe() */
204
205/*****************************************************************************/
206/*
207 * read the key data
David Howells76d8aea2005-06-23 22:00:49 -0700208 * - the key's semaphore is read-locked
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 */
210static long user_read(const struct key *key,
211 char __user *buffer, size_t buflen)
212{
David Howells76d8aea2005-06-23 22:00:49 -0700213 struct user_key_payload *upayload;
214 long ret;
215
216 upayload = rcu_dereference(key->payload.data);
217 ret = upayload->datalen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
219 /* we can return the data as is */
220 if (buffer && buflen > 0) {
David Howells76d8aea2005-06-23 22:00:49 -0700221 if (buflen > upayload->datalen)
222 buflen = upayload->datalen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
David Howells76d8aea2005-06-23 22:00:49 -0700224 if (copy_to_user(buffer, upayload->data, buflen) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 ret = -EFAULT;
226 }
227
228 return ret;
229
230} /* end user_read() */