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 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);

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_pointer(key->payload.data, upayload);
L
Linus Torvalds 已提交
102 103 104
		key->expiry = 0;
	}

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

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

111 112
EXPORT_SYMBOL_GPL(user_update);

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

121 122
EXPORT_SYMBOL_GPL(user_match);

L
Linus Torvalds 已提交
123
/*
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) {
		rcu_assign_pointer(key->payload.data, NULL);
136
		kfree_rcu(upayload, rcu);
137
	}
138
}
139 140 141 142 143

EXPORT_SYMBOL(user_revoke);

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

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

152 153
EXPORT_SYMBOL_GPL(user_destroy);

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

164 165
EXPORT_SYMBOL_GPL(user_describe);

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

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

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

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

	return ret;
188
}
189 190

EXPORT_SYMBOL_GPL(user_read);