user_defined.c 5.0 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 41
/*****************************************************************************/
/*
 * instantiate a user defined key
 */
42
int user_instantiate(struct key *key, const void *data, size_t datalen)
L
Linus Torvalds 已提交
43
{
44
	struct user_key_payload *upayload;
L
Linus Torvalds 已提交
45 46 47 48 49 50 51 52 53 54 55
	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;
56 57
	upayload = kmalloc(sizeof(*upayload) + datalen, GFP_KERNEL);
	if (!upayload)
L
Linus Torvalds 已提交
58 59
		goto error;

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

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

} /* end user_instantiate() */
70

71 72
EXPORT_SYMBOL_GPL(user_instantiate);

73 74 75 76 77 78 79 80 81 82 83 84 85 86
/*****************************************************************************/
/*
 * 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);

} /* end user_update_rcu_disposal() */

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

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

101
	/* construct a replacement payload */
L
Linus Torvalds 已提交
102
	ret = -ENOMEM;
103 104
	upayload = kmalloc(sizeof(*upayload) + datalen, GFP_KERNEL);
	if (!upayload)
L
Linus Torvalds 已提交
105 106
		goto error;

107 108
	upayload->datalen = datalen;
	memcpy(upayload->data, data, datalen);
L
Linus Torvalds 已提交
109 110

	/* check the quota and attach the new data */
111
	zap = upayload;
L
Linus Torvalds 已提交
112 113 114 115 116 117

	ret = key_payload_reserve(key, datalen);

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

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

124
error:
L
Linus Torvalds 已提交
125 126 127 128
	return ret;

} /* end user_update() */

129 130
EXPORT_SYMBOL_GPL(user_update);

L
Linus Torvalds 已提交
131 132 133 134
/*****************************************************************************/
/*
 * match users on their name
 */
135
int user_match(const struct key *key, const void *description)
L
Linus Torvalds 已提交
136 137 138 139 140
{
	return strcmp(key->description, description) == 0;

} /* end user_match() */

141 142
EXPORT_SYMBOL_GPL(user_match);

L
Linus Torvalds 已提交
143 144
/*****************************************************************************/
/*
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
 * 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);
	}

} /* end user_revoke() */

EXPORT_SYMBOL(user_revoke);

/*****************************************************************************/
/*
 * dispose of the data dangling from the corpse of a user key
L
Linus Torvalds 已提交
167
 */
168
void user_destroy(struct key *key)
L
Linus Torvalds 已提交
169
{
170 171 172
	struct user_key_payload *upayload = key->payload.data;

	kfree(upayload);
L
Linus Torvalds 已提交
173 174 175

} /* end user_destroy() */

176 177
EXPORT_SYMBOL_GPL(user_destroy);

L
Linus Torvalds 已提交
178 179
/*****************************************************************************/
/*
180
 * describe the user key
L
Linus Torvalds 已提交
181
 */
182
void user_describe(const struct key *key, struct seq_file *m)
L
Linus Torvalds 已提交
183 184 185 186 187 188 189
{
	seq_puts(m, key->description);

	seq_printf(m, ": %u", key->datalen);

} /* end user_describe() */

190 191
EXPORT_SYMBOL_GPL(user_describe);

L
Linus Torvalds 已提交
192 193 194
/*****************************************************************************/
/*
 * read the key data
195
 * - the key's semaphore is read-locked
L
Linus Torvalds 已提交
196
 */
197
long user_read(const struct key *key, char __user *buffer, size_t buflen)
L
Linus Torvalds 已提交
198
{
199 200 201 202 203
	struct user_key_payload *upayload;
	long ret;

	upayload = rcu_dereference(key->payload.data);
	ret = upayload->datalen;
L
Linus Torvalds 已提交
204 205 206

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

210
		if (copy_to_user(buffer, upayload->data, buflen) != 0)
L
Linus Torvalds 已提交
211 212 213 214 215 216
			ret = -EFAULT;
	}

	return ret;

} /* end user_read() */
217 218

EXPORT_SYMBOL_GPL(user_read);