blob: eee340011f2bd54d7b12f3f28a25ec3f8396b5fd [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* user_defined.c: user defined key type
2 *
3 * Copyright (C) 2004 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 */
11
12#include <linux/module.h>
13#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/slab.h>
15#include <linux/seq_file.h>
16#include <linux/err.h>
David Howells2aa349f2005-10-30 15:02:42 -080017#include <keys/user-type.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <asm/uaccess.h>
19#include "internal.h"
20
Jeff Layton9f6ed2c2012-01-17 16:09:11 -050021static int logon_vet_description(const char *desc);
22
Linus Torvalds1da177e2005-04-16 15:20:36 -070023/*
24 * user defined keys take an arbitrary string as the description and an
25 * arbitrary blob of data as the payload
26 */
27struct key_type key_type_user = {
David Howells4bdf0bc2013-09-24 10:35:15 +010028 .name = "user",
29 .def_lookup_type = KEYRING_SEARCH_LOOKUP_DIRECT,
David Howellsf9167782014-07-18 18:56:35 +010030 .preparse = user_preparse,
31 .free_preparse = user_free_preparse,
32 .instantiate = generic_key_instantiate,
David Howells4bdf0bc2013-09-24 10:35:15 +010033 .update = user_update,
34 .match = user_match,
35 .revoke = user_revoke,
36 .destroy = user_destroy,
37 .describe = user_describe,
38 .read = user_read,
Linus Torvalds1da177e2005-04-16 15:20:36 -070039};
40
Michael Halcrow16c29b62005-06-23 22:00:58 -070041EXPORT_SYMBOL_GPL(key_type_user);
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043/*
Jeff Layton9f6ed2c2012-01-17 16:09:11 -050044 * This key type is essentially the same as key_type_user, but it does
45 * not define a .read op. This is suitable for storing username and
46 * password pairs in the keyring that you do not want to be readable
47 * from userspace.
48 */
49struct key_type key_type_logon = {
50 .name = "logon",
David Howells4bdf0bc2013-09-24 10:35:15 +010051 .def_lookup_type = KEYRING_SEARCH_LOOKUP_DIRECT,
David Howellsf9167782014-07-18 18:56:35 +010052 .preparse = user_preparse,
53 .free_preparse = user_free_preparse,
54 .instantiate = generic_key_instantiate,
Jeff Layton9f6ed2c2012-01-17 16:09:11 -050055 .update = user_update,
56 .match = user_match,
57 .revoke = user_revoke,
58 .destroy = user_destroy,
59 .describe = user_describe,
60 .vet_description = logon_vet_description,
61};
62EXPORT_SYMBOL_GPL(key_type_logon);
63
64/*
David Howellsf9167782014-07-18 18:56:35 +010065 * Preparse a user defined key payload
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 */
David Howellsf9167782014-07-18 18:56:35 +010067int user_preparse(struct key_preparsed_payload *prep)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068{
David Howells76d8aea2005-06-23 22:00:49 -070069 struct user_key_payload *upayload;
David Howellscf7f6012012-09-13 13:06:29 +010070 size_t datalen = prep->datalen;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
David Howellscf7f6012012-09-13 13:06:29 +010072 if (datalen <= 0 || datalen > 32767 || !prep->data)
David Howellsf9167782014-07-18 18:56:35 +010073 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
David Howells76d8aea2005-06-23 22:00:49 -070075 upayload = kmalloc(sizeof(*upayload) + datalen, GFP_KERNEL);
76 if (!upayload)
David Howellsf9167782014-07-18 18:56:35 +010077 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
David Howells76d8aea2005-06-23 22:00:49 -070079 /* attach the data */
David Howellsf9167782014-07-18 18:56:35 +010080 prep->quotalen = datalen;
81 prep->payload[0] = upayload;
David Howells76d8aea2005-06-23 22:00:49 -070082 upayload->datalen = datalen;
David Howellscf7f6012012-09-13 13:06:29 +010083 memcpy(upayload->data, prep->data, datalen);
David Howellsf9167782014-07-18 18:56:35 +010084 return 0;
David Howellsa8b17ed2011-01-20 16:38:27 +000085}
David Howellsf9167782014-07-18 18:56:35 +010086EXPORT_SYMBOL_GPL(user_preparse);
David Howells31204ed2006-06-26 00:24:51 -070087
David Howellsf9167782014-07-18 18:56:35 +010088/*
89 * Free a preparse of a user defined key payload
90 */
91void user_free_preparse(struct key_preparsed_payload *prep)
92{
93 kfree(prep->payload[0]);
94}
95EXPORT_SYMBOL_GPL(user_free_preparse);
David Howells2aa349f2005-10-30 15:02:42 -080096
Linus Torvalds1da177e2005-04-16 15:20:36 -070097/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 * update a user defined key
David Howells76d8aea2005-06-23 22:00:49 -070099 * - the key's semaphore is write-locked
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 */
David Howellscf7f6012012-09-13 13:06:29 +0100101int user_update(struct key *key, struct key_preparsed_payload *prep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102{
David Howells76d8aea2005-06-23 22:00:49 -0700103 struct user_key_payload *upayload, *zap;
David Howellscf7f6012012-09-13 13:06:29 +0100104 size_t datalen = prep->datalen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 int ret;
106
107 ret = -EINVAL;
David Howellscf7f6012012-09-13 13:06:29 +0100108 if (datalen <= 0 || datalen > 32767 || !prep->data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 goto error;
110
David Howells76d8aea2005-06-23 22:00:49 -0700111 /* construct a replacement payload */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 ret = -ENOMEM;
David Howells76d8aea2005-06-23 22:00:49 -0700113 upayload = kmalloc(sizeof(*upayload) + datalen, GFP_KERNEL);
114 if (!upayload)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 goto error;
116
David Howells76d8aea2005-06-23 22:00:49 -0700117 upayload->datalen = datalen;
David Howellscf7f6012012-09-13 13:06:29 +0100118 memcpy(upayload->data, prep->data, datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120 /* check the quota and attach the new data */
David Howells76d8aea2005-06-23 22:00:49 -0700121 zap = upayload;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
123 ret = key_payload_reserve(key, datalen);
124
125 if (ret == 0) {
126 /* attach the new data, displacing the old */
127 zap = key->payload.data;
Mimi Zoharf6b24572012-01-18 10:03:14 +0000128 rcu_assign_keypointer(key, upayload);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 key->expiry = 0;
130 }
131
David Howells9f35a332011-11-15 22:09:45 +0000132 if (zap)
133 kfree_rcu(zap, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
David Howells2aa349f2005-10-30 15:02:42 -0800135error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000137}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
David Howells2aa349f2005-10-30 15:02:42 -0800139EXPORT_SYMBOL_GPL(user_update);
140
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141/*
142 * match users on their name
143 */
David Howells2aa349f2005-10-30 15:02:42 -0800144int user_match(const struct key *key, const void *description)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145{
146 return strcmp(key->description, description) == 0;
David Howellsa8b17ed2011-01-20 16:38:27 +0000147}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
David Howells2aa349f2005-10-30 15:02:42 -0800149EXPORT_SYMBOL_GPL(user_match);
150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151/*
David Howells31204ed2006-06-26 00:24:51 -0700152 * dispose of the links from a revoked keyring
153 * - called with the key sem write-locked
154 */
155void user_revoke(struct key *key)
156{
157 struct user_key_payload *upayload = key->payload.data;
158
159 /* clear the quota */
160 key_payload_reserve(key, 0);
161
162 if (upayload) {
Mimi Zoharf6b24572012-01-18 10:03:14 +0000163 rcu_assign_keypointer(key, NULL);
Lai Jiangshan3acb4582011-03-18 12:11:07 +0800164 kfree_rcu(upayload, rcu);
David Howells31204ed2006-06-26 00:24:51 -0700165 }
David Howellsa8b17ed2011-01-20 16:38:27 +0000166}
David Howells31204ed2006-06-26 00:24:51 -0700167
168EXPORT_SYMBOL(user_revoke);
169
David Howells31204ed2006-06-26 00:24:51 -0700170/*
171 * dispose of the data dangling from the corpse of a user key
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 */
David Howells2aa349f2005-10-30 15:02:42 -0800173void user_destroy(struct key *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174{
David Howells76d8aea2005-06-23 22:00:49 -0700175 struct user_key_payload *upayload = key->payload.data;
176
177 kfree(upayload);
David Howellsa8b17ed2011-01-20 16:38:27 +0000178}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
David Howells2aa349f2005-10-30 15:02:42 -0800180EXPORT_SYMBOL_GPL(user_destroy);
181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182/*
David Howells76d8aea2005-06-23 22:00:49 -0700183 * describe the user key
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 */
David Howells2aa349f2005-10-30 15:02:42 -0800185void user_describe(const struct key *key, struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186{
187 seq_puts(m, key->description);
David Howells78b72802011-03-11 17:57:23 +0000188 if (key_is_instantiated(key))
189 seq_printf(m, ": %u", key->datalen);
David Howellsa8b17ed2011-01-20 16:38:27 +0000190}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
David Howells2aa349f2005-10-30 15:02:42 -0800192EXPORT_SYMBOL_GPL(user_describe);
193
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194/*
195 * read the key data
David Howells76d8aea2005-06-23 22:00:49 -0700196 * - the key's semaphore is read-locked
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 */
David Howells2aa349f2005-10-30 15:02:42 -0800198long user_read(const struct key *key, char __user *buffer, size_t buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
David Howells76d8aea2005-06-23 22:00:49 -0700200 struct user_key_payload *upayload;
201 long ret;
202
David Howells633e8042011-03-07 15:05:51 +0000203 upayload = rcu_dereference_key(key);
David Howells76d8aea2005-06-23 22:00:49 -0700204 ret = upayload->datalen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
206 /* we can return the data as is */
207 if (buffer && buflen > 0) {
David Howells76d8aea2005-06-23 22:00:49 -0700208 if (buflen > upayload->datalen)
209 buflen = upayload->datalen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
David Howells76d8aea2005-06-23 22:00:49 -0700211 if (copy_to_user(buffer, upayload->data, buflen) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 ret = -EFAULT;
213 }
214
215 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000216}
David Howells2aa349f2005-10-30 15:02:42 -0800217
218EXPORT_SYMBOL_GPL(user_read);
Jeff Layton9f6ed2c2012-01-17 16:09:11 -0500219
220/* Vet the description for a "logon" key */
221static int logon_vet_description(const char *desc)
222{
223 char *p;
224
225 /* require a "qualified" description string */
226 p = strchr(desc, ':');
227 if (!p)
228 return -EINVAL;
229
230 /* also reject description with ':' as first char */
231 if (p == desc)
232 return -EINVAL;
233
234 return 0;
235}