blob: 8e71895b97a7eaa90c4aee3088794d5b827a7cd6 [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>
David Howells2aa349f2005-10-30 15:02:42 -080018#include <keys/user-type.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <asm/uaccess.h>
20#include "internal.h"
21
Linus Torvalds1da177e2005-04-16 15:20:36 -070022/*
23 * user defined keys take an arbitrary string as the description and an
24 * arbitrary blob of data as the payload
25 */
26struct key_type key_type_user = {
27 .name = "user",
28 .instantiate = user_instantiate,
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 .update = user_update,
30 .match = user_match,
31 .destroy = user_destroy,
32 .describe = user_describe,
33 .read = user_read,
34};
35
Michael Halcrow16c29b62005-06-23 22:00:58 -070036EXPORT_SYMBOL_GPL(key_type_user);
37
Linus Torvalds1da177e2005-04-16 15:20:36 -070038/*****************************************************************************/
39/*
40 * instantiate a user defined key
41 */
David Howells2aa349f2005-10-30 15:02:42 -080042int user_instantiate(struct key *key, const void *data, size_t datalen)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043{
David Howells76d8aea2005-06-23 22:00:49 -070044 struct user_key_payload *upayload;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 ret = -ENOMEM;
David Howells76d8aea2005-06-23 22:00:49 -070056 upayload = kmalloc(sizeof(*upayload) + datalen, GFP_KERNEL);
57 if (!upayload)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 goto error;
59
David Howells76d8aea2005-06-23 22:00:49 -070060 /* attach the data */
61 upayload->datalen = datalen;
62 memcpy(upayload->data, data, datalen);
63 rcu_assign_pointer(key->payload.data, upayload);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 ret = 0;
65
David Howells2aa349f2005-10-30 15:02:42 -080066error:
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 return ret;
68
69} /* end user_instantiate() */
David Howells2aa349f2005-10-30 15:02:42 -080070EXPORT_SYMBOL_GPL(user_instantiate);
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072/*****************************************************************************/
73/*
David Howells76d8aea2005-06-23 22:00:49 -070074 * dispose of the old data from an updated user defined key
75 */
76static 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/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 * update a user defined key
David Howells76d8aea2005-06-23 22:00:49 -070089 * - the key's semaphore is write-locked
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 */
David Howells2aa349f2005-10-30 15:02:42 -080091int user_update(struct key *key, const void *data, size_t datalen)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
David Howells76d8aea2005-06-23 22:00:49 -070093 struct user_key_payload *upayload, *zap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 int ret;
95
96 ret = -EINVAL;
97 if (datalen <= 0 || datalen > 32767 || !data)
98 goto error;
99
David Howells76d8aea2005-06-23 22:00:49 -0700100 /* construct a replacement payload */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 ret = -ENOMEM;
David Howells76d8aea2005-06-23 22:00:49 -0700102 upayload = kmalloc(sizeof(*upayload) + datalen, GFP_KERNEL);
103 if (!upayload)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 goto error;
105
David Howells76d8aea2005-06-23 22:00:49 -0700106 upayload->datalen = datalen;
107 memcpy(upayload->data, data, datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
109 /* check the quota and attach the new data */
David Howells76d8aea2005-06-23 22:00:49 -0700110 zap = upayload;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
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;
David Howells76d8aea2005-06-23 22:00:49 -0700117 rcu_assign_pointer(key->payload.data, upayload);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 key->expiry = 0;
119 }
120
David Howells76d8aea2005-06-23 22:00:49 -0700121 call_rcu(&zap->rcu, user_update_rcu_disposal);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
David Howells2aa349f2005-10-30 15:02:42 -0800123error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 return ret;
125
126} /* end user_update() */
127
David Howells2aa349f2005-10-30 15:02:42 -0800128EXPORT_SYMBOL_GPL(user_update);
129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130/*****************************************************************************/
131/*
132 * match users on their name
133 */
David Howells2aa349f2005-10-30 15:02:42 -0800134int user_match(const struct key *key, const void *description)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135{
136 return strcmp(key->description, description) == 0;
137
138} /* end user_match() */
139
David Howells2aa349f2005-10-30 15:02:42 -0800140EXPORT_SYMBOL_GPL(user_match);
141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142/*****************************************************************************/
143/*
144 * dispose of the data dangling from the corpse of a user
145 */
David Howells2aa349f2005-10-30 15:02:42 -0800146void user_destroy(struct key *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147{
David Howells76d8aea2005-06-23 22:00:49 -0700148 struct user_key_payload *upayload = key->payload.data;
149
150 kfree(upayload);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
152} /* end user_destroy() */
153
David Howells2aa349f2005-10-30 15:02:42 -0800154EXPORT_SYMBOL_GPL(user_destroy);
155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156/*****************************************************************************/
157/*
David Howells76d8aea2005-06-23 22:00:49 -0700158 * describe the user key
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 */
David Howells2aa349f2005-10-30 15:02:42 -0800160void user_describe(const struct key *key, struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
162 seq_puts(m, key->description);
163
164 seq_printf(m, ": %u", key->datalen);
165
166} /* end user_describe() */
167
David Howells2aa349f2005-10-30 15:02:42 -0800168EXPORT_SYMBOL_GPL(user_describe);
169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170/*****************************************************************************/
171/*
172 * read the key data
David Howells76d8aea2005-06-23 22:00:49 -0700173 * - the key's semaphore is read-locked
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 */
David Howells2aa349f2005-10-30 15:02:42 -0800175long user_read(const struct key *key, char __user *buffer, size_t buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
David Howells76d8aea2005-06-23 22:00:49 -0700177 struct user_key_payload *upayload;
178 long ret;
179
180 upayload = rcu_dereference(key->payload.data);
181 ret = upayload->datalen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
183 /* we can return the data as is */
184 if (buffer && buflen > 0) {
David Howells76d8aea2005-06-23 22:00:49 -0700185 if (buflen > upayload->datalen)
186 buflen = upayload->datalen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
David Howells76d8aea2005-06-23 22:00:49 -0700188 if (copy_to_user(buffer, upayload->data, buflen) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 ret = -EFAULT;
190 }
191
192 return ret;
193
194} /* end user_read() */
David Howells2aa349f2005-10-30 15:02:42 -0800195
196EXPORT_SYMBOL_GPL(user_read);