user_defined.c 3.9 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
	/* attach the data */
	upayload->datalen = datalen;
	memcpy(upayload->data, data, datalen);
62
	rcu_assign_keypointer(key, 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);

L
Linus Torvalds 已提交
71 72
/*
 * update a user defined key
73
 * - the key's semaphore is write-locked
L
Linus Torvalds 已提交
74
 */
75
int user_update(struct key *key, const void *data, size_t datalen)
L
Linus Torvalds 已提交
76
{
77
	struct user_key_payload *upayload, *zap;
L
Linus Torvalds 已提交
78 79 80 81 82 83
	int ret;

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

84
	/* construct a replacement payload */
L
Linus Torvalds 已提交
85
	ret = -ENOMEM;
86 87
	upayload = kmalloc(sizeof(*upayload) + datalen, GFP_KERNEL);
	if (!upayload)
L
Linus Torvalds 已提交
88 89
		goto error;

90 91
	upayload->datalen = datalen;
	memcpy(upayload->data, data, datalen);
L
Linus Torvalds 已提交
92 93

	/* check the quota and attach the new data */
94
	zap = upayload;
L
Linus Torvalds 已提交
95 96 97 98 99 100

	ret = key_payload_reserve(key, datalen);

	if (ret == 0) {
		/* attach the new data, displacing the old */
		zap = key->payload.data;
101
		rcu_assign_keypointer(key, upayload);
L
Linus Torvalds 已提交
102 103 104
		key->expiry = 0;
	}

105 106
	if (zap)
		kfree_rcu(zap, rcu);
L
Linus Torvalds 已提交
107

108
error:
L
Linus Torvalds 已提交
109
	return ret;
110
}
L
Linus Torvalds 已提交
111

112 113
EXPORT_SYMBOL_GPL(user_update);

L
Linus Torvalds 已提交
114 115 116
/*
 * match users on their name
 */
117
int user_match(const struct key *key, const void *description)
L
Linus Torvalds 已提交
118 119
{
	return strcmp(key->description, description) == 0;
120
}
L
Linus Torvalds 已提交
121

122 123
EXPORT_SYMBOL_GPL(user_match);

L
Linus Torvalds 已提交
124
/*
125 126 127 128 129 130 131 132 133 134 135
 * 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) {
136
		rcu_assign_keypointer(key, NULL);
137
		kfree_rcu(upayload, rcu);
138
	}
139
}
140 141 142 143 144

EXPORT_SYMBOL(user_revoke);

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

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

153 154
EXPORT_SYMBOL_GPL(user_destroy);

L
Linus Torvalds 已提交
155
/*
156
 * describe the user key
L
Linus Torvalds 已提交
157
 */
158
void user_describe(const struct key *key, struct seq_file *m)
L
Linus Torvalds 已提交
159 160
{
	seq_puts(m, key->description);
D
David Howells 已提交
161 162
	if (key_is_instantiated(key))
		seq_printf(m, ": %u", key->datalen);
163
}
L
Linus Torvalds 已提交
164

165 166
EXPORT_SYMBOL_GPL(user_describe);

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

176
	upayload = rcu_dereference_key(key);
177
	ret = upayload->datalen;
L
Linus Torvalds 已提交
178 179 180

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

184
		if (copy_to_user(buffer, upayload->data, buflen) != 0)
L
Linus Torvalds 已提交
185 186 187 188
			ret = -EFAULT;
	}

	return ret;
189
}
190 191

EXPORT_SYMBOL_GPL(user_read);