blob: 5bbfdebb7acfb79436d615851ec91d57c2d74c49 [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,
David Howells31204ed2006-06-26 00:24:51 -070031 .revoke = user_revoke,
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 .destroy = user_destroy,
33 .describe = user_describe,
34 .read = user_read,
35};
36
Michael Halcrow16c29b62005-06-23 22:00:58 -070037EXPORT_SYMBOL_GPL(key_type_user);
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039/*****************************************************************************/
40/*
41 * instantiate a user defined key
42 */
David Howells2aa349f2005-10-30 15:02:42 -080043int user_instantiate(struct key *key, const void *data, size_t datalen)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044{
David Howells76d8aea2005-06-23 22:00:49 -070045 struct user_key_payload *upayload;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 int ret;
47
48 ret = -EINVAL;
49 if (datalen <= 0 || datalen > 32767 || !data)
50 goto error;
51
52 ret = key_payload_reserve(key, datalen);
53 if (ret < 0)
54 goto error;
55
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 ret = -ENOMEM;
David Howells76d8aea2005-06-23 22:00:49 -070057 upayload = kmalloc(sizeof(*upayload) + datalen, GFP_KERNEL);
58 if (!upayload)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 goto error;
60
David Howells76d8aea2005-06-23 22:00:49 -070061 /* attach the data */
62 upayload->datalen = datalen;
63 memcpy(upayload->data, data, datalen);
64 rcu_assign_pointer(key->payload.data, upayload);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 ret = 0;
66
David Howells2aa349f2005-10-30 15:02:42 -080067error:
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 return ret;
69
70} /* end user_instantiate() */
David Howells31204ed2006-06-26 00:24:51 -070071
David Howells2aa349f2005-10-30 15:02:42 -080072EXPORT_SYMBOL_GPL(user_instantiate);
73
Linus Torvalds1da177e2005-04-16 15:20:36 -070074/*****************************************************************************/
75/*
David Howells76d8aea2005-06-23 22:00:49 -070076 * dispose of the old data from an updated user defined key
77 */
78static void user_update_rcu_disposal(struct rcu_head *rcu)
79{
80 struct user_key_payload *upayload;
81
82 upayload = container_of(rcu, struct user_key_payload, rcu);
83
84 kfree(upayload);
85
86} /* end user_update_rcu_disposal() */
87
88/*****************************************************************************/
89/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 * update a user defined key
David Howells76d8aea2005-06-23 22:00:49 -070091 * - the key's semaphore is write-locked
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 */
David Howells2aa349f2005-10-30 15:02:42 -080093int user_update(struct key *key, const void *data, size_t datalen)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094{
David Howells76d8aea2005-06-23 22:00:49 -070095 struct user_key_payload *upayload, *zap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 int ret;
97
98 ret = -EINVAL;
99 if (datalen <= 0 || datalen > 32767 || !data)
100 goto error;
101
David Howells76d8aea2005-06-23 22:00:49 -0700102 /* construct a replacement payload */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 ret = -ENOMEM;
David Howells76d8aea2005-06-23 22:00:49 -0700104 upayload = kmalloc(sizeof(*upayload) + datalen, GFP_KERNEL);
105 if (!upayload)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 goto error;
107
David Howells76d8aea2005-06-23 22:00:49 -0700108 upayload->datalen = datalen;
109 memcpy(upayload->data, data, datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
111 /* check the quota and attach the new data */
David Howells76d8aea2005-06-23 22:00:49 -0700112 zap = upayload;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114 ret = key_payload_reserve(key, datalen);
115
116 if (ret == 0) {
117 /* attach the new data, displacing the old */
118 zap = key->payload.data;
David Howells76d8aea2005-06-23 22:00:49 -0700119 rcu_assign_pointer(key->payload.data, upayload);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 key->expiry = 0;
121 }
122
David Howells76d8aea2005-06-23 22:00:49 -0700123 call_rcu(&zap->rcu, user_update_rcu_disposal);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
David Howells2aa349f2005-10-30 15:02:42 -0800125error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 return ret;
127
128} /* end user_update() */
129
David Howells2aa349f2005-10-30 15:02:42 -0800130EXPORT_SYMBOL_GPL(user_update);
131
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132/*****************************************************************************/
133/*
134 * match users on their name
135 */
David Howells2aa349f2005-10-30 15:02:42 -0800136int user_match(const struct key *key, const void *description)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137{
138 return strcmp(key->description, description) == 0;
139
140} /* end user_match() */
141
David Howells2aa349f2005-10-30 15:02:42 -0800142EXPORT_SYMBOL_GPL(user_match);
143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144/*****************************************************************************/
145/*
David Howells31204ed2006-06-26 00:24:51 -0700146 * dispose of the links from a revoked keyring
147 * - called with the key sem write-locked
148 */
149void user_revoke(struct key *key)
150{
151 struct user_key_payload *upayload = key->payload.data;
152
153 /* clear the quota */
154 key_payload_reserve(key, 0);
155
156 if (upayload) {
157 rcu_assign_pointer(key->payload.data, NULL);
158 call_rcu(&upayload->rcu, user_update_rcu_disposal);
159 }
160
161} /* end user_revoke() */
162
163EXPORT_SYMBOL(user_revoke);
164
165/*****************************************************************************/
166/*
167 * dispose of the data dangling from the corpse of a user key
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 */
David Howells2aa349f2005-10-30 15:02:42 -0800169void user_destroy(struct key *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170{
David Howells76d8aea2005-06-23 22:00:49 -0700171 struct user_key_payload *upayload = key->payload.data;
172
173 kfree(upayload);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
175} /* end user_destroy() */
176
David Howells2aa349f2005-10-30 15:02:42 -0800177EXPORT_SYMBOL_GPL(user_destroy);
178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179/*****************************************************************************/
180/*
David Howells76d8aea2005-06-23 22:00:49 -0700181 * describe the user key
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 */
David Howells2aa349f2005-10-30 15:02:42 -0800183void user_describe(const struct key *key, struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
185 seq_puts(m, key->description);
186
187 seq_printf(m, ": %u", key->datalen);
188
189} /* end user_describe() */
190
David Howells2aa349f2005-10-30 15:02:42 -0800191EXPORT_SYMBOL_GPL(user_describe);
192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193/*****************************************************************************/
194/*
195 * read the key data
David Howells76d8aea2005-06-23 22:00:49 -0700196 * - the key's semaphore is read-locked
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 */
David Howells2aa349f2005-10-30 15:02:42 -0800198long user_read(const struct key *key, char __user *buffer, size_t buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
David Howells76d8aea2005-06-23 22:00:49 -0700200 struct user_key_payload *upayload;
201 long ret;
202
203 upayload = rcu_dereference(key->payload.data);
204 ret = upayload->datalen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
206 /* we can return the data as is */
207 if (buffer && buflen > 0) {
David Howells76d8aea2005-06-23 22:00:49 -0700208 if (buflen > upayload->datalen)
209 buflen = upayload->datalen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
David Howells76d8aea2005-06-23 22:00:49 -0700211 if (copy_to_user(buffer, upayload->data, buflen) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 ret = -EFAULT;
213 }
214
215 return ret;
216
217} /* end user_read() */
David Howells2aa349f2005-10-30 15:02:42 -0800218
219EXPORT_SYMBOL_GPL(user_read);