user_defined.c 4.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
#include <asm/uaccess.h>
#include "internal.h"

J
Jeff Layton 已提交
21 22
static int logon_vet_description(const char *desc);

L
Linus Torvalds 已提交
23 24 25 26 27 28 29 30 31
/*
 * 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,
32
	.revoke		= user_revoke,
L
Linus Torvalds 已提交
33 34 35 36 37
	.destroy	= user_destroy,
	.describe	= user_describe,
	.read		= user_read,
};

38 39
EXPORT_SYMBOL_GPL(key_type_user);

J
Jeff Layton 已提交
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
/*
 * This key type is essentially the same as key_type_user, but it does
 * not define a .read op. This is suitable for storing username and
 * password pairs in the keyring that you do not want to be readable
 * from userspace.
 */
struct key_type key_type_logon = {
	.name			= "logon",
	.instantiate		= user_instantiate,
	.update			= user_update,
	.match			= user_match,
	.revoke			= user_revoke,
	.destroy		= user_destroy,
	.describe		= user_describe,
	.vet_description	= logon_vet_description,
};
EXPORT_SYMBOL_GPL(key_type_logon);

L
Linus Torvalds 已提交
58 59 60
/*
 * instantiate a user defined key
 */
61
int user_instantiate(struct key *key, struct key_preparsed_payload *prep)
L
Linus Torvalds 已提交
62
{
63
	struct user_key_payload *upayload;
64
	size_t datalen = prep->datalen;
L
Linus Torvalds 已提交
65 66 67
	int ret;

	ret = -EINVAL;
68
	if (datalen <= 0 || datalen > 32767 || !prep->data)
L
Linus Torvalds 已提交
69 70 71 72 73 74 75
		goto error;

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

	ret = -ENOMEM;
76 77
	upayload = kmalloc(sizeof(*upayload) + datalen, GFP_KERNEL);
	if (!upayload)
L
Linus Torvalds 已提交
78 79
		goto error;

80 81
	/* attach the data */
	upayload->datalen = datalen;
82
	memcpy(upayload->data, prep->data, datalen);
83
	rcu_assign_keypointer(key, upayload);
L
Linus Torvalds 已提交
84 85
	ret = 0;

86
error:
L
Linus Torvalds 已提交
87
	return ret;
88
}
89

90 91
EXPORT_SYMBOL_GPL(user_instantiate);

L
Linus Torvalds 已提交
92 93
/*
 * update a user defined key
94
 * - the key's semaphore is write-locked
L
Linus Torvalds 已提交
95
 */
96
int user_update(struct key *key, struct key_preparsed_payload *prep)
L
Linus Torvalds 已提交
97
{
98
	struct user_key_payload *upayload, *zap;
99
	size_t datalen = prep->datalen;
L
Linus Torvalds 已提交
100 101 102
	int ret;

	ret = -EINVAL;
103
	if (datalen <= 0 || datalen > 32767 || !prep->data)
L
Linus Torvalds 已提交
104 105
		goto error;

106
	/* construct a replacement payload */
L
Linus Torvalds 已提交
107
	ret = -ENOMEM;
108 109
	upayload = kmalloc(sizeof(*upayload) + datalen, GFP_KERNEL);
	if (!upayload)
L
Linus Torvalds 已提交
110 111
		goto error;

112
	upayload->datalen = datalen;
113
	memcpy(upayload->data, prep->data, datalen);
L
Linus Torvalds 已提交
114 115

	/* check the quota and attach the new data */
116
	zap = upayload;
L
Linus Torvalds 已提交
117 118 119 120 121 122

	ret = key_payload_reserve(key, datalen);

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

127 128
	if (zap)
		kfree_rcu(zap, rcu);
L
Linus Torvalds 已提交
129

130
error:
L
Linus Torvalds 已提交
131
	return ret;
132
}
L
Linus Torvalds 已提交
133

134 135
EXPORT_SYMBOL_GPL(user_update);

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

144 145
EXPORT_SYMBOL_GPL(user_match);

L
Linus Torvalds 已提交
146
/*
147 148 149 150 151 152 153 154 155 156 157
 * 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) {
158
		rcu_assign_keypointer(key, NULL);
159
		kfree_rcu(upayload, rcu);
160
	}
161
}
162 163 164 165 166

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);
173
}
L
Linus Torvalds 已提交
174

175 176
EXPORT_SYMBOL_GPL(user_destroy);

L
Linus Torvalds 已提交
177
/*
178
 * describe the user key
L
Linus Torvalds 已提交
179
 */
180
void user_describe(const struct key *key, struct seq_file *m)
L
Linus Torvalds 已提交
181 182
{
	seq_puts(m, key->description);
D
David Howells 已提交
183 184
	if (key_is_instantiated(key))
		seq_printf(m, ": %u", key->datalen);
185
}
L
Linus Torvalds 已提交
186

187 188
EXPORT_SYMBOL_GPL(user_describe);

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

198
	upayload = rcu_dereference_key(key);
199
	ret = upayload->datalen;
L
Linus Torvalds 已提交
200 201 202

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

206
		if (copy_to_user(buffer, upayload->data, buflen) != 0)
L
Linus Torvalds 已提交
207 208 209 210
			ret = -EFAULT;
	}

	return ret;
211
}
212 213

EXPORT_SYMBOL_GPL(user_read);
J
Jeff Layton 已提交
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230

/* Vet the description for a "logon" key */
static int logon_vet_description(const char *desc)
{
	char *p;

	/* require a "qualified" description string */
	p = strchr(desc, ':');
	if (!p)
		return -EINVAL;

	/* also reject description with ':' as first char */
	if (p == desc)
		return -EINVAL;

	return 0;
}