user_defined.c 4.2 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* user_defined.c: user defined key type
 *
 * Copyright (C) 2004 Red Hat, Inc. All Rights Reserved.
 * Written by David Howells (dhowells@redhat.com)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version
 * 2 of the License, or (at your option) any later version.
 */

#include <linux/module.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/seq_file.h>
#include <linux/err.h>
17
#include <keys/user-type.h>
L
Linus Torvalds 已提交
18 19 20 21 22 23 24 25 26 27 28 29
#include <asm/uaccess.h>
#include "internal.h"

/*
 * user defined keys take an arbitrary string as the description and an
 * arbitrary blob of data as the payload
 */
struct key_type key_type_user = {
	.name		= "user",
	.instantiate	= user_instantiate,
	.update		= user_update,
	.match		= user_match,
30
	.revoke		= user_revoke,
L
Linus Torvalds 已提交
31 32 33 34 35
	.destroy	= user_destroy,
	.describe	= user_describe,
	.read		= user_read,
};

36 37
EXPORT_SYMBOL_GPL(key_type_user);

L
Linus Torvalds 已提交
38 39 40
/*
 * instantiate a user defined key
 */
41
int user_instantiate(struct key *key, const void *data, size_t datalen)
L
Linus Torvalds 已提交
42
{
43
	struct user_key_payload *upayload;
L
Linus Torvalds 已提交
44 45 46 47 48 49 50 51 52 53 54
	int ret;

	ret = -EINVAL;
	if (datalen <= 0 || datalen > 32767 || !data)
		goto error;

	ret = key_payload_reserve(key, datalen);
	if (ret < 0)
		goto error;

	ret = -ENOMEM;
55 56
	upayload = kmalloc(sizeof(*upayload) + datalen, GFP_KERNEL);
	if (!upayload)
L
Linus Torvalds 已提交
57 58
		goto error;

59 60 61 62
	/* attach the data */
	upayload->datalen = datalen;
	memcpy(upayload->data, data, datalen);
	rcu_assign_pointer(key->payload.data, upayload);
L
Linus Torvalds 已提交
63 64
	ret = 0;

65
error:
L
Linus Torvalds 已提交
66
	return ret;
67
}
68

69 70
EXPORT_SYMBOL_GPL(user_instantiate);

71 72 73 74 75 76 77 78 79 80
/*
 * dispose of the old data from an updated user defined key
 */
static void user_update_rcu_disposal(struct rcu_head *rcu)
{
	struct user_key_payload *upayload;

	upayload = container_of(rcu, struct user_key_payload, rcu);

	kfree(upayload);
81
}
82

L
Linus Torvalds 已提交
83 84
/*
 * update a user defined key
85
 * - the key's semaphore is write-locked
L
Linus Torvalds 已提交
86
 */
87
int user_update(struct key *key, const void *data, size_t datalen)
L
Linus Torvalds 已提交
88
{
89
	struct user_key_payload *upayload, *zap;
L
Linus Torvalds 已提交
90 91 92 93 94 95
	int ret;

	ret = -EINVAL;
	if (datalen <= 0 || datalen > 32767 || !data)
		goto error;

96
	/* construct a replacement payload */
L
Linus Torvalds 已提交
97
	ret = -ENOMEM;
98 99
	upayload = kmalloc(sizeof(*upayload) + datalen, GFP_KERNEL);
	if (!upayload)
L
Linus Torvalds 已提交
100 101
		goto error;

102 103
	upayload->datalen = datalen;
	memcpy(upayload->data, data, datalen);
L
Linus Torvalds 已提交
104 105

	/* check the quota and attach the new data */
106
	zap = upayload;
L
Linus Torvalds 已提交
107 108 109 110 111 112

	ret = key_payload_reserve(key, datalen);

	if (ret == 0) {
		/* attach the new data, displacing the old */
		zap = key->payload.data;
113
		rcu_assign_pointer(key->payload.data, upayload);
L
Linus Torvalds 已提交
114 115 116
		key->expiry = 0;
	}

117
	call_rcu(&zap->rcu, user_update_rcu_disposal);
L
Linus Torvalds 已提交
118

119
error:
L
Linus Torvalds 已提交
120
	return ret;
121
}
L
Linus Torvalds 已提交
122

123 124
EXPORT_SYMBOL_GPL(user_update);

L
Linus Torvalds 已提交
125 126 127
/*
 * match users on their name
 */
128
int user_match(const struct key *key, const void *description)
L
Linus Torvalds 已提交
129 130
{
	return strcmp(key->description, description) == 0;
131
}
L
Linus Torvalds 已提交
132

133 134
EXPORT_SYMBOL_GPL(user_match);

L
Linus Torvalds 已提交
135
/*
136 137 138 139 140 141 142 143 144 145 146 147 148 149
 * dispose of the links from a revoked keyring
 * - called with the key sem write-locked
 */
void user_revoke(struct key *key)
{
	struct user_key_payload *upayload = key->payload.data;

	/* clear the quota */
	key_payload_reserve(key, 0);

	if (upayload) {
		rcu_assign_pointer(key->payload.data, NULL);
		call_rcu(&upayload->rcu, user_update_rcu_disposal);
	}
150
}
151 152 153 154 155

EXPORT_SYMBOL(user_revoke);

/*
 * dispose of the data dangling from the corpse of a user key
L
Linus Torvalds 已提交
156
 */
157
void user_destroy(struct key *key)
L
Linus Torvalds 已提交
158
{
159 160 161
	struct user_key_payload *upayload = key->payload.data;

	kfree(upayload);
162
}
L
Linus Torvalds 已提交
163

164 165
EXPORT_SYMBOL_GPL(user_destroy);

L
Linus Torvalds 已提交
166
/*
167
 * describe the user key
L
Linus Torvalds 已提交
168
 */
169
void user_describe(const struct key *key, struct seq_file *m)
L
Linus Torvalds 已提交
170 171
{
	seq_puts(m, key->description);
D
David Howells 已提交
172 173
	if (key_is_instantiated(key))
		seq_printf(m, ": %u", key->datalen);
174
}
L
Linus Torvalds 已提交
175

176 177
EXPORT_SYMBOL_GPL(user_describe);

L
Linus Torvalds 已提交
178 179
/*
 * read the key data
180
 * - the key's semaphore is read-locked
L
Linus Torvalds 已提交
181
 */
182
long user_read(const struct key *key, char __user *buffer, size_t buflen)
L
Linus Torvalds 已提交
183
{
184 185 186
	struct user_key_payload *upayload;
	long ret;

187
	upayload = rcu_dereference_key(key);
188
	ret = upayload->datalen;
L
Linus Torvalds 已提交
189 190 191

	/* we can return the data as is */
	if (buffer && buflen > 0) {
192 193
		if (buflen > upayload->datalen)
			buflen = upayload->datalen;
L
Linus Torvalds 已提交
194

195
		if (copy_to_user(buffer, upayload->data, buflen) != 0)
L
Linus Torvalds 已提交
196 197 198 199
			ret = -EFAULT;
	}

	return ret;
200
}
201 202

EXPORT_SYMBOL_GPL(user_read);