blob: 7c687d568221cd71a3e863b9c9c50f5cd9352005 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/slab.h>
15#include <linux/seq_file.h>
16#include <linux/err.h>
David Howells2aa349f2005-10-30 15:02:42 -080017#include <keys/user-type.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <asm/uaccess.h>
19#include "internal.h"
20
Linus Torvalds1da177e2005-04-16 15:20:36 -070021/*
22 * user defined keys take an arbitrary string as the description and an
23 * arbitrary blob of data as the payload
24 */
25struct key_type key_type_user = {
26 .name = "user",
27 .instantiate = user_instantiate,
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 .update = user_update,
29 .match = user_match,
David Howells31204ed2006-06-26 00:24:51 -070030 .revoke = user_revoke,
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 .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 Howells31204ed2006-06-26 00:24:51 -070070
David Howells2aa349f2005-10-30 15:02:42 -080071EXPORT_SYMBOL_GPL(user_instantiate);
72
Linus Torvalds1da177e2005-04-16 15:20:36 -070073/*****************************************************************************/
74/*
David Howells76d8aea2005-06-23 22:00:49 -070075 * dispose of the old data from an updated user defined key
76 */
77static void user_update_rcu_disposal(struct rcu_head *rcu)
78{
79 struct user_key_payload *upayload;
80
81 upayload = container_of(rcu, struct user_key_payload, rcu);
82
83 kfree(upayload);
84
85} /* end user_update_rcu_disposal() */
86
87/*****************************************************************************/
88/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 * update a user defined key
David Howells76d8aea2005-06-23 22:00:49 -070090 * - the key's semaphore is write-locked
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 */
David Howells2aa349f2005-10-30 15:02:42 -080092int user_update(struct key *key, const void *data, size_t datalen)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
David Howells76d8aea2005-06-23 22:00:49 -070094 struct user_key_payload *upayload, *zap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 int ret;
96
97 ret = -EINVAL;
98 if (datalen <= 0 || datalen > 32767 || !data)
99 goto error;
100
David Howells76d8aea2005-06-23 22:00:49 -0700101 /* construct a replacement payload */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 ret = -ENOMEM;
David Howells76d8aea2005-06-23 22:00:49 -0700103 upayload = kmalloc(sizeof(*upayload) + datalen, GFP_KERNEL);
104 if (!upayload)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 goto error;
106
David Howells76d8aea2005-06-23 22:00:49 -0700107 upayload->datalen = datalen;
108 memcpy(upayload->data, data, datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
110 /* check the quota and attach the new data */
David Howells76d8aea2005-06-23 22:00:49 -0700111 zap = upayload;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113 ret = key_payload_reserve(key, datalen);
114
115 if (ret == 0) {
116 /* attach the new data, displacing the old */
117 zap = key->payload.data;
David Howells76d8aea2005-06-23 22:00:49 -0700118 rcu_assign_pointer(key->payload.data, upayload);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 key->expiry = 0;
120 }
121
David Howells76d8aea2005-06-23 22:00:49 -0700122 call_rcu(&zap->rcu, user_update_rcu_disposal);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
David Howells2aa349f2005-10-30 15:02:42 -0800124error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 return ret;
126
127} /* end user_update() */
128
David Howells2aa349f2005-10-30 15:02:42 -0800129EXPORT_SYMBOL_GPL(user_update);
130
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131/*****************************************************************************/
132/*
133 * match users on their name
134 */
David Howells2aa349f2005-10-30 15:02:42 -0800135int user_match(const struct key *key, const void *description)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
137 return strcmp(key->description, description) == 0;
138
139} /* end user_match() */
140
David Howells2aa349f2005-10-30 15:02:42 -0800141EXPORT_SYMBOL_GPL(user_match);
142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143/*****************************************************************************/
144/*
David Howells31204ed2006-06-26 00:24:51 -0700145 * dispose of the links from a revoked keyring
146 * - called with the key sem write-locked
147 */
148void user_revoke(struct key *key)
149{
150 struct user_key_payload *upayload = key->payload.data;
151
152 /* clear the quota */
153 key_payload_reserve(key, 0);
154
155 if (upayload) {
156 rcu_assign_pointer(key->payload.data, NULL);
157 call_rcu(&upayload->rcu, user_update_rcu_disposal);
158 }
159
160} /* end user_revoke() */
161
162EXPORT_SYMBOL(user_revoke);
163
164/*****************************************************************************/
165/*
166 * dispose of the data dangling from the corpse of a user key
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 */
David Howells2aa349f2005-10-30 15:02:42 -0800168void user_destroy(struct key *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169{
David Howells76d8aea2005-06-23 22:00:49 -0700170 struct user_key_payload *upayload = key->payload.data;
171
172 kfree(upayload);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174} /* end user_destroy() */
175
David Howells2aa349f2005-10-30 15:02:42 -0800176EXPORT_SYMBOL_GPL(user_destroy);
177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178/*****************************************************************************/
179/*
David Howells76d8aea2005-06-23 22:00:49 -0700180 * describe the user key
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 */
David Howells2aa349f2005-10-30 15:02:42 -0800182void user_describe(const struct key *key, struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
184 seq_puts(m, key->description);
185
186 seq_printf(m, ": %u", key->datalen);
187
188} /* end user_describe() */
189
David Howells2aa349f2005-10-30 15:02:42 -0800190EXPORT_SYMBOL_GPL(user_describe);
191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192/*****************************************************************************/
193/*
194 * read the key data
David Howells76d8aea2005-06-23 22:00:49 -0700195 * - the key's semaphore is read-locked
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 */
David Howells2aa349f2005-10-30 15:02:42 -0800197long user_read(const struct key *key, char __user *buffer, size_t buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198{
David Howells76d8aea2005-06-23 22:00:49 -0700199 struct user_key_payload *upayload;
200 long ret;
201
202 upayload = rcu_dereference(key->payload.data);
203 ret = upayload->datalen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
205 /* we can return the data as is */
206 if (buffer && buflen > 0) {
David Howells76d8aea2005-06-23 22:00:49 -0700207 if (buflen > upayload->datalen)
208 buflen = upayload->datalen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
David Howells76d8aea2005-06-23 22:00:49 -0700210 if (copy_to_user(buffer, upayload->data, buflen) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 ret = -EFAULT;
212 }
213
214 return ret;
215
216} /* end user_read() */
David Howells2aa349f2005-10-30 15:02:42 -0800217
218EXPORT_SYMBOL_GPL(user_read);