user_defined.c 4.5 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/* 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/sched.h>
#include <linux/slab.h>
#include <linux/seq_file.h>
#include <linux/err.h>
18
#include <keys/user-type.h>
L
Linus Torvalds 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
#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,
	.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
EXPORT_SYMBOL_GPL(user_instantiate);

72 73 74 75 76 77 78 79 80 81 82 83 84 85
/*****************************************************************************/
/*
 * 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 已提交
86 87 88
/*****************************************************************************/
/*
 * update a user defined key
89
 * - the key's semaphore is write-locked
L
Linus Torvalds 已提交
90
 */
91
int user_update(struct key *key, const void *data, size_t datalen)
L
Linus Torvalds 已提交
92
{
93
	struct user_key_payload *upayload, *zap;
L
Linus Torvalds 已提交
94 95 96 97 98 99
	int ret;

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

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

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

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

	ret = key_payload_reserve(key, datalen);

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

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

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

} /* end user_update() */

128 129
EXPORT_SYMBOL_GPL(user_update);

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

} /* end user_match() */

140 141
EXPORT_SYMBOL_GPL(user_match);

L
Linus Torvalds 已提交
142 143 144 145
/*****************************************************************************/
/*
 * dispose of the data dangling from the corpse of a user
 */
146
void user_destroy(struct key *key)
L
Linus Torvalds 已提交
147
{
148 149 150
	struct user_key_payload *upayload = key->payload.data;

	kfree(upayload);
L
Linus Torvalds 已提交
151 152 153

} /* end user_destroy() */

154 155
EXPORT_SYMBOL_GPL(user_destroy);

L
Linus Torvalds 已提交
156 157
/*****************************************************************************/
/*
158
 * describe the user key
L
Linus Torvalds 已提交
159
 */
160
void user_describe(const struct key *key, struct seq_file *m)
L
Linus Torvalds 已提交
161 162 163 164 165 166 167
{
	seq_puts(m, key->description);

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

} /* end user_describe() */

168 169
EXPORT_SYMBOL_GPL(user_describe);

L
Linus Torvalds 已提交
170 171 172
/*****************************************************************************/
/*
 * read the key data
173
 * - the key's semaphore is read-locked
L
Linus Torvalds 已提交
174
 */
175
long user_read(const struct key *key, char __user *buffer, size_t buflen)
L
Linus Torvalds 已提交
176
{
177 178 179 180 181
	struct user_key_payload *upayload;
	long ret;

	upayload = rcu_dereference(key->payload.data);
	ret = upayload->datalen;
L
Linus Torvalds 已提交
182 183 184

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

188
		if (copy_to_user(buffer, upayload->data, buflen) != 0)
L
Linus Torvalds 已提交
189 190 191 192 193 194
			ret = -EFAULT;
	}

	return ret;

} /* end user_read() */
195 196

EXPORT_SYMBOL_GPL(user_read);