blob: c6ca8662a4685bd162205938eafae39bef25fbbf [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 * instantiate a user defined key
40 */
David Howells2aa349f2005-10-30 15:02:42 -080041int user_instantiate(struct key *key, const void *data, size_t datalen)
Linus Torvalds1da177e2005-04-16 15:20:36 -070042{
David Howells76d8aea2005-06-23 22:00:49 -070043 struct user_key_payload *upayload;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 int ret;
45
46 ret = -EINVAL;
47 if (datalen <= 0 || datalen > 32767 || !data)
48 goto error;
49
50 ret = key_payload_reserve(key, datalen);
51 if (ret < 0)
52 goto error;
53
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 ret = -ENOMEM;
David Howells76d8aea2005-06-23 22:00:49 -070055 upayload = kmalloc(sizeof(*upayload) + datalen, GFP_KERNEL);
56 if (!upayload)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 goto error;
58
David Howells76d8aea2005-06-23 22:00:49 -070059 /* attach the data */
60 upayload->datalen = datalen;
61 memcpy(upayload->data, data, datalen);
62 rcu_assign_pointer(key->payload.data, upayload);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 ret = 0;
64
David Howells2aa349f2005-10-30 15:02:42 -080065error:
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +000067}
David Howells31204ed2006-06-26 00:24:51 -070068
David Howells2aa349f2005-10-30 15:02:42 -080069EXPORT_SYMBOL_GPL(user_instantiate);
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071/*
David Howells76d8aea2005-06-23 22:00:49 -070072 * dispose of the old data from an updated user defined key
73 */
74static void user_update_rcu_disposal(struct rcu_head *rcu)
75{
76 struct user_key_payload *upayload;
77
78 upayload = container_of(rcu, struct user_key_payload, rcu);
79
80 kfree(upayload);
David Howellsa8b17ed2011-01-20 16:38:27 +000081}
David Howells76d8aea2005-06-23 22:00:49 -070082
David Howells76d8aea2005-06-23 22:00:49 -070083/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 * update a user defined key
David Howells76d8aea2005-06-23 22:00:49 -070085 * - the key's semaphore is write-locked
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 */
David Howells2aa349f2005-10-30 15:02:42 -080087int user_update(struct key *key, const void *data, size_t datalen)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088{
David Howells76d8aea2005-06-23 22:00:49 -070089 struct user_key_payload *upayload, *zap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 int ret;
91
92 ret = -EINVAL;
93 if (datalen <= 0 || datalen > 32767 || !data)
94 goto error;
95
David Howells76d8aea2005-06-23 22:00:49 -070096 /* construct a replacement payload */
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 ret = -ENOMEM;
David Howells76d8aea2005-06-23 22:00:49 -070098 upayload = kmalloc(sizeof(*upayload) + datalen, GFP_KERNEL);
99 if (!upayload)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 goto error;
101
David Howells76d8aea2005-06-23 22:00:49 -0700102 upayload->datalen = datalen;
103 memcpy(upayload->data, data, datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
105 /* check the quota and attach the new data */
David Howells76d8aea2005-06-23 22:00:49 -0700106 zap = upayload;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108 ret = key_payload_reserve(key, datalen);
109
110 if (ret == 0) {
111 /* attach the new data, displacing the old */
112 zap = key->payload.data;
David Howells76d8aea2005-06-23 22:00:49 -0700113 rcu_assign_pointer(key->payload.data, upayload);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 key->expiry = 0;
115 }
116
David Howells76d8aea2005-06-23 22:00:49 -0700117 call_rcu(&zap->rcu, user_update_rcu_disposal);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
David Howells2aa349f2005-10-30 15:02:42 -0800119error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000121}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
David Howells2aa349f2005-10-30 15:02:42 -0800123EXPORT_SYMBOL_GPL(user_update);
124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125/*
126 * match users on their name
127 */
David Howells2aa349f2005-10-30 15:02:42 -0800128int user_match(const struct key *key, const void *description)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129{
130 return strcmp(key->description, description) == 0;
David Howellsa8b17ed2011-01-20 16:38:27 +0000131}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
David Howells2aa349f2005-10-30 15:02:42 -0800133EXPORT_SYMBOL_GPL(user_match);
134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135/*
David Howells31204ed2006-06-26 00:24:51 -0700136 * dispose of the links from a revoked keyring
137 * - called with the key sem write-locked
138 */
139void user_revoke(struct key *key)
140{
141 struct user_key_payload *upayload = key->payload.data;
142
143 /* clear the quota */
144 key_payload_reserve(key, 0);
145
146 if (upayload) {
147 rcu_assign_pointer(key->payload.data, NULL);
148 call_rcu(&upayload->rcu, user_update_rcu_disposal);
149 }
David Howellsa8b17ed2011-01-20 16:38:27 +0000150}
David Howells31204ed2006-06-26 00:24:51 -0700151
152EXPORT_SYMBOL(user_revoke);
153
David Howells31204ed2006-06-26 00:24:51 -0700154/*
155 * dispose of the data dangling from the corpse of a user key
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 */
David Howells2aa349f2005-10-30 15:02:42 -0800157void user_destroy(struct key *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
David Howells76d8aea2005-06-23 22:00:49 -0700159 struct user_key_payload *upayload = key->payload.data;
160
161 kfree(upayload);
David Howellsa8b17ed2011-01-20 16:38:27 +0000162}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
David Howells2aa349f2005-10-30 15:02:42 -0800164EXPORT_SYMBOL_GPL(user_destroy);
165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166/*
David Howells76d8aea2005-06-23 22:00:49 -0700167 * describe the user key
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 */
David Howells2aa349f2005-10-30 15:02:42 -0800169void user_describe(const struct key *key, struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170{
171 seq_puts(m, key->description);
172
173 seq_printf(m, ": %u", key->datalen);
David Howellsa8b17ed2011-01-20 16:38:27 +0000174}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
David Howells2aa349f2005-10-30 15:02:42 -0800176EXPORT_SYMBOL_GPL(user_describe);
177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178/*
179 * read the key data
David Howells76d8aea2005-06-23 22:00:49 -0700180 * - the key's semaphore is read-locked
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 */
David Howells2aa349f2005-10-30 15:02:42 -0800182long user_read(const struct key *key, char __user *buffer, size_t buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
David Howells76d8aea2005-06-23 22:00:49 -0700184 struct user_key_payload *upayload;
185 long ret;
186
David Howells633e8042011-03-07 15:05:51 +0000187 upayload = rcu_dereference_key(key);
David Howells76d8aea2005-06-23 22:00:49 -0700188 ret = upayload->datalen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
190 /* we can return the data as is */
191 if (buffer && buflen > 0) {
David Howells76d8aea2005-06-23 22:00:49 -0700192 if (buflen > upayload->datalen)
193 buflen = upayload->datalen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
David Howells76d8aea2005-06-23 22:00:49 -0700195 if (copy_to_user(buffer, upayload->data, buflen) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 ret = -EFAULT;
197 }
198
199 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000200}
David Howells2aa349f2005-10-30 15:02:42 -0800201
202EXPORT_SYMBOL_GPL(user_read);