user_defined.c 5.0 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
/*
 * 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 = {
28 29 30 31 32 33 34 35 36
	.name			= "user",
	.def_lookup_type	= KEYRING_SEARCH_LOOKUP_DIRECT,
	.instantiate		= user_instantiate,
	.update			= user_update,
	.match			= user_match,
	.revoke			= user_revoke,
	.destroy		= user_destroy,
	.describe		= user_describe,
	.read			= user_read,
L
Linus Torvalds 已提交
37 38
};

39 40
EXPORT_SYMBOL_GPL(key_type_user);

J
Jeff Layton 已提交
41 42 43 44 45 46 47 48
/*
 * 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",
49
	.def_lookup_type	= KEYRING_SEARCH_LOOKUP_DIRECT,
J
Jeff Layton 已提交
50 51 52 53 54 55 56 57 58 59
	.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 已提交
60 61 62
/*
 * instantiate a user defined key
 */
63
int user_instantiate(struct key *key, struct key_preparsed_payload *prep)
L
Linus Torvalds 已提交
64
{
65
	struct user_key_payload *upayload;
66
	size_t datalen = prep->datalen;
L
Linus Torvalds 已提交
67 68 69
	int ret;

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

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

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

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

88
error:
L
Linus Torvalds 已提交
89
	return ret;
90
}
91

92 93
EXPORT_SYMBOL_GPL(user_instantiate);

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

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

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

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

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

	ret = key_payload_reserve(key, datalen);

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

129 130
	if (zap)
		kfree_rcu(zap, rcu);
L
Linus Torvalds 已提交
131

132
error:
L
Linus Torvalds 已提交
133
	return ret;
134
}
L
Linus Torvalds 已提交
135

136 137
EXPORT_SYMBOL_GPL(user_update);

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

146 147
EXPORT_SYMBOL_GPL(user_match);

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

EXPORT_SYMBOL(user_revoke);

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

	kfree(upayload);
175
}
L
Linus Torvalds 已提交
176

177 178
EXPORT_SYMBOL_GPL(user_destroy);

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

189 190
EXPORT_SYMBOL_GPL(user_describe);

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

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

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

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

	return ret;
213
}
214 215

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

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