user_defined.c 4.8 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, const void *data, size_t datalen)
L
Linus Torvalds 已提交
62
{
63
	struct user_key_payload *upayload;
L
Linus Torvalds 已提交
64 65 66 67 68 69 70 71 72 73 74
	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;
75 76
	upayload = kmalloc(sizeof(*upayload) + datalen, GFP_KERNEL);
	if (!upayload)
L
Linus Torvalds 已提交
77 78
		goto error;

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

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

89 90
EXPORT_SYMBOL_GPL(user_instantiate);

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

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

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

110 111
	upayload->datalen = datalen;
	memcpy(upayload->data, data, datalen);
L
Linus Torvalds 已提交
112 113

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

	ret = key_payload_reserve(key, datalen);

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

125 126
	if (zap)
		kfree_rcu(zap, rcu);
L
Linus Torvalds 已提交
127

128
error:
L
Linus Torvalds 已提交
129
	return ret;
130
}
L
Linus Torvalds 已提交
131

132 133
EXPORT_SYMBOL_GPL(user_update);

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

142 143
EXPORT_SYMBOL_GPL(user_match);

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

EXPORT_SYMBOL(user_revoke);

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

	kfree(upayload);
171
}
L
Linus Torvalds 已提交
172

173 174
EXPORT_SYMBOL_GPL(user_destroy);

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

185 186
EXPORT_SYMBOL_GPL(user_describe);

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

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

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

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

	return ret;
209
}
210 211

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

/* 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;
}