blob: 69ff52c08e97bb0eab715b1ea61c709e4f80faad [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/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 * update a user defined key
David Howells76d8aea2005-06-23 22:00:49 -070073 * - the key's semaphore is write-locked
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 */
David Howells2aa349f2005-10-30 15:02:42 -080075int user_update(struct key *key, const void *data, size_t datalen)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076{
David Howells76d8aea2005-06-23 22:00:49 -070077 struct user_key_payload *upayload, *zap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 int ret;
79
80 ret = -EINVAL;
81 if (datalen <= 0 || datalen > 32767 || !data)
82 goto error;
83
David Howells76d8aea2005-06-23 22:00:49 -070084 /* construct a replacement payload */
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 ret = -ENOMEM;
David Howells76d8aea2005-06-23 22:00:49 -070086 upayload = kmalloc(sizeof(*upayload) + datalen, GFP_KERNEL);
87 if (!upayload)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 goto error;
89
David Howells76d8aea2005-06-23 22:00:49 -070090 upayload->datalen = datalen;
91 memcpy(upayload->data, data, datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
93 /* check the quota and attach the new data */
David Howells76d8aea2005-06-23 22:00:49 -070094 zap = upayload;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96 ret = key_payload_reserve(key, datalen);
97
98 if (ret == 0) {
99 /* attach the new data, displacing the old */
100 zap = key->payload.data;
David Howells76d8aea2005-06-23 22:00:49 -0700101 rcu_assign_pointer(key->payload.data, upayload);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 key->expiry = 0;
103 }
104
David Howells9f35a332011-11-15 22:09:45 +0000105 if (zap)
106 kfree_rcu(zap, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
David Howells2aa349f2005-10-30 15:02:42 -0800108error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000110}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
David Howells2aa349f2005-10-30 15:02:42 -0800112EXPORT_SYMBOL_GPL(user_update);
113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114/*
115 * match users on their name
116 */
David Howells2aa349f2005-10-30 15:02:42 -0800117int user_match(const struct key *key, const void *description)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118{
119 return strcmp(key->description, description) == 0;
David Howellsa8b17ed2011-01-20 16:38:27 +0000120}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
David Howells2aa349f2005-10-30 15:02:42 -0800122EXPORT_SYMBOL_GPL(user_match);
123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124/*
David Howells31204ed2006-06-26 00:24:51 -0700125 * dispose of the links from a revoked keyring
126 * - called with the key sem write-locked
127 */
128void user_revoke(struct key *key)
129{
130 struct user_key_payload *upayload = key->payload.data;
131
132 /* clear the quota */
133 key_payload_reserve(key, 0);
134
135 if (upayload) {
136 rcu_assign_pointer(key->payload.data, NULL);
Lai Jiangshan3acb4582011-03-18 12:11:07 +0800137 kfree_rcu(upayload, rcu);
David Howells31204ed2006-06-26 00:24:51 -0700138 }
David Howellsa8b17ed2011-01-20 16:38:27 +0000139}
David Howells31204ed2006-06-26 00:24:51 -0700140
141EXPORT_SYMBOL(user_revoke);
142
David Howells31204ed2006-06-26 00:24:51 -0700143/*
144 * dispose of the data dangling from the corpse of a user key
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 */
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);
David Howellsa8b17ed2011-01-20 16:38:27 +0000151}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
David Howells2aa349f2005-10-30 15:02:42 -0800153EXPORT_SYMBOL_GPL(user_destroy);
154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155/*
David Howells76d8aea2005-06-23 22:00:49 -0700156 * describe the user key
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 */
David Howells2aa349f2005-10-30 15:02:42 -0800158void user_describe(const struct key *key, struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159{
160 seq_puts(m, key->description);
David Howells78b72802011-03-11 17:57:23 +0000161 if (key_is_instantiated(key))
162 seq_printf(m, ": %u", key->datalen);
David Howellsa8b17ed2011-01-20 16:38:27 +0000163}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
David Howells2aa349f2005-10-30 15:02:42 -0800165EXPORT_SYMBOL_GPL(user_describe);
166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167/*
168 * read the key data
David Howells76d8aea2005-06-23 22:00:49 -0700169 * - the key's semaphore is read-locked
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 */
David Howells2aa349f2005-10-30 15:02:42 -0800171long user_read(const struct key *key, char __user *buffer, size_t buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172{
David Howells76d8aea2005-06-23 22:00:49 -0700173 struct user_key_payload *upayload;
174 long ret;
175
David Howells633e8042011-03-07 15:05:51 +0000176 upayload = rcu_dereference_key(key);
David Howells76d8aea2005-06-23 22:00:49 -0700177 ret = upayload->datalen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
179 /* we can return the data as is */
180 if (buffer && buflen > 0) {
David Howells76d8aea2005-06-23 22:00:49 -0700181 if (buflen > upayload->datalen)
182 buflen = upayload->datalen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
David Howells76d8aea2005-06-23 22:00:49 -0700184 if (copy_to_user(buffer, upayload->data, buflen) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 ret = -EFAULT;
186 }
187
188 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000189}
David Howells2aa349f2005-10-30 15:02:42 -0800190
191EXPORT_SYMBOL_GPL(user_read);