blob: c33d3614a0dbf00724fad2c6b4f9729e274e57e2 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070051/*****************************************************************************/
52/*
53 * instantiate a user defined key
54 */
55static int user_instantiate(struct key *key, const void *data, size_t datalen)
56{
David Howells76d8aea2005-06-23 22:00:49 -070057 struct user_key_payload *upayload;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 ret = -ENOMEM;
David Howells76d8aea2005-06-23 22:00:49 -070069 upayload = kmalloc(sizeof(*upayload) + datalen, GFP_KERNEL);
70 if (!upayload)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 goto error;
72
David Howells76d8aea2005-06-23 22:00:49 -070073 /* attach the data */
74 upayload->datalen = datalen;
75 memcpy(upayload->data, data, datalen);
76 rcu_assign_pointer(key->payload.data, upayload);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 ret = 0;
78
79 error:
80 return ret;
81
82} /* end user_instantiate() */
83
84/*****************************************************************************/
85/*
86 * duplicate a user defined key
David Howells76d8aea2005-06-23 22:00:49 -070087 * - both keys' semaphores are locked against further modification
88 * - the new key cannot yet be accessed
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 */
90static int user_duplicate(struct key *key, const struct key *source)
91{
David Howells76d8aea2005-06-23 22:00:49 -070092 struct user_key_payload *upayload, *spayload;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 int ret;
94
95 /* just copy the payload */
96 ret = -ENOMEM;
David Howells76d8aea2005-06-23 22:00:49 -070097 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
David Howells76d8aea2005-06-23 22:00:49 -0700102 upayload->datalen = key->datalen = spayload->datalen;
103 memcpy(upayload->data, spayload->data, key->datalen);
104
105 key->payload.data = upayload;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 ret = 0;
107 }
108
109 return ret;
110
111} /* end user_duplicate() */
112
113/*****************************************************************************/
114/*
David Howells76d8aea2005-06-23 22:00:49 -0700115 * dispose of the old data from an updated user defined key
116 */
117static 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/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 * update a user defined key
David Howells76d8aea2005-06-23 22:00:49 -0700130 * - the key's semaphore is write-locked
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 */
132static int user_update(struct key *key, const void *data, size_t datalen)
133{
David Howells76d8aea2005-06-23 22:00:49 -0700134 struct user_key_payload *upayload, *zap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 int ret;
136
137 ret = -EINVAL;
138 if (datalen <= 0 || datalen > 32767 || !data)
139 goto error;
140
David Howells76d8aea2005-06-23 22:00:49 -0700141 /* construct a replacement payload */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 ret = -ENOMEM;
David Howells76d8aea2005-06-23 22:00:49 -0700143 upayload = kmalloc(sizeof(*upayload) + datalen, GFP_KERNEL);
144 if (!upayload)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 goto error;
146
David Howells76d8aea2005-06-23 22:00:49 -0700147 upayload->datalen = datalen;
148 memcpy(upayload->data, data, datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
150 /* check the quota and attach the new data */
David Howells76d8aea2005-06-23 22:00:49 -0700151 zap = upayload;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
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;
David Howells76d8aea2005-06-23 22:00:49 -0700158 rcu_assign_pointer(key->payload.data, upayload);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 key->expiry = 0;
160 }
161
David Howells76d8aea2005-06-23 22:00:49 -0700162 call_rcu(&zap->rcu, user_update_rcu_disposal);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
164 error:
165 return ret;
166
167} /* end user_update() */
168
169/*****************************************************************************/
170/*
171 * match users on their name
172 */
173static 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 */
183static void user_destroy(struct key *key)
184{
David Howells76d8aea2005-06-23 22:00:49 -0700185 struct user_key_payload *upayload = key->payload.data;
186
187 kfree(upayload);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
189} /* end user_destroy() */
190
191/*****************************************************************************/
192/*
David Howells76d8aea2005-06-23 22:00:49 -0700193 * describe the user key
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 */
195static 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
David Howells76d8aea2005-06-23 22:00:49 -0700206 * - the key's semaphore is read-locked
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 */
208static long user_read(const struct key *key,
209 char __user *buffer, size_t buflen)
210{
David Howells76d8aea2005-06-23 22:00:49 -0700211 struct user_key_payload *upayload;
212 long ret;
213
214 upayload = rcu_dereference(key->payload.data);
215 ret = upayload->datalen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
217 /* we can return the data as is */
218 if (buffer && buflen > 0) {
David Howells76d8aea2005-06-23 22:00:49 -0700219 if (buflen > upayload->datalen)
220 buflen = upayload->datalen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
David Howells76d8aea2005-06-23 22:00:49 -0700222 if (copy_to_user(buffer, upayload->data, buflen) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 ret = -EFAULT;
224 }
225
226 return ret;
227
228} /* end user_read() */