blob: 6f12de4ce549b9d32e02ab7df622f38a385726ef [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/* user_defined.c: user defined key type
3 *
4 * Copyright (C) 2004 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 */
7
Paul Gortmaker876979c2018-12-09 15:36:29 -05008#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/slab.h>
11#include <linux/seq_file.h>
12#include <linux/err.h>
David Howells2aa349f2005-10-30 15:02:42 -080013#include <keys/user-type.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080014#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include "internal.h"
16
Jeff Layton9f6ed2c2012-01-17 16:09:11 -050017static int logon_vet_description(const char *desc);
18
Linus Torvalds1da177e2005-04-16 15:20:36 -070019/*
20 * user defined keys take an arbitrary string as the description and an
21 * arbitrary blob of data as the payload
22 */
23struct key_type key_type_user = {
David Howells4bdf0bc2013-09-24 10:35:15 +010024 .name = "user",
David Howellsf9167782014-07-18 18:56:35 +010025 .preparse = user_preparse,
26 .free_preparse = user_free_preparse,
27 .instantiate = generic_key_instantiate,
David Howells4bdf0bc2013-09-24 10:35:15 +010028 .update = user_update,
David Howells4bdf0bc2013-09-24 10:35:15 +010029 .revoke = user_revoke,
30 .destroy = user_destroy,
31 .describe = user_describe,
32 .read = user_read,
Linus Torvalds1da177e2005-04-16 15:20:36 -070033};
34
Michael Halcrow16c29b62005-06-23 22:00:58 -070035EXPORT_SYMBOL_GPL(key_type_user);
36
Linus Torvalds1da177e2005-04-16 15:20:36 -070037/*
Jeff Layton9f6ed2c2012-01-17 16:09:11 -050038 * This key type is essentially the same as key_type_user, but it does
39 * not define a .read op. This is suitable for storing username and
40 * password pairs in the keyring that you do not want to be readable
41 * from userspace.
42 */
43struct key_type key_type_logon = {
44 .name = "logon",
David Howellsf9167782014-07-18 18:56:35 +010045 .preparse = user_preparse,
46 .free_preparse = user_free_preparse,
47 .instantiate = generic_key_instantiate,
Jeff Layton9f6ed2c2012-01-17 16:09:11 -050048 .update = user_update,
Jeff Layton9f6ed2c2012-01-17 16:09:11 -050049 .revoke = user_revoke,
50 .destroy = user_destroy,
51 .describe = user_describe,
52 .vet_description = logon_vet_description,
53};
54EXPORT_SYMBOL_GPL(key_type_logon);
55
56/*
David Howellsf9167782014-07-18 18:56:35 +010057 * Preparse a user defined key payload
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 */
David Howellsf9167782014-07-18 18:56:35 +010059int user_preparse(struct key_preparsed_payload *prep)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060{
David Howells76d8aea2005-06-23 22:00:49 -070061 struct user_key_payload *upayload;
David Howellscf7f6012012-09-13 13:06:29 +010062 size_t datalen = prep->datalen;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
David Howellscf7f6012012-09-13 13:06:29 +010064 if (datalen <= 0 || datalen > 32767 || !prep->data)
David Howellsf9167782014-07-18 18:56:35 +010065 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
David Howells76d8aea2005-06-23 22:00:49 -070067 upayload = kmalloc(sizeof(*upayload) + datalen, GFP_KERNEL);
68 if (!upayload)
David Howellsf9167782014-07-18 18:56:35 +010069 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
David Howells76d8aea2005-06-23 22:00:49 -070071 /* attach the data */
David Howellsf9167782014-07-18 18:56:35 +010072 prep->quotalen = datalen;
David Howells146aa8b2015-10-21 14:04:48 +010073 prep->payload.data[0] = upayload;
David Howells76d8aea2005-06-23 22:00:49 -070074 upayload->datalen = datalen;
David Howellscf7f6012012-09-13 13:06:29 +010075 memcpy(upayload->data, prep->data, datalen);
David Howellsf9167782014-07-18 18:56:35 +010076 return 0;
David Howellsa8b17ed2011-01-20 16:38:27 +000077}
David Howellsf9167782014-07-18 18:56:35 +010078EXPORT_SYMBOL_GPL(user_preparse);
David Howells31204ed2006-06-26 00:24:51 -070079
David Howellsf9167782014-07-18 18:56:35 +010080/*
81 * Free a preparse of a user defined key payload
82 */
83void user_free_preparse(struct key_preparsed_payload *prep)
84{
Eric Biggers6966c742017-06-08 14:49:04 +010085 kzfree(prep->payload.data[0]);
David Howellsf9167782014-07-18 18:56:35 +010086}
87EXPORT_SYMBOL_GPL(user_free_preparse);
David Howells2aa349f2005-10-30 15:02:42 -080088
Eric Biggers6966c742017-06-08 14:49:04 +010089static void user_free_payload_rcu(struct rcu_head *head)
90{
91 struct user_key_payload *payload;
92
93 payload = container_of(head, struct user_key_payload, rcu);
94 kzfree(payload);
95}
96
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 Howells898de7d2016-04-12 19:54:58 +0100103 struct user_key_payload *zap = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 int ret;
105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 /* check the quota and attach the new data */
David Howells898de7d2016-04-12 19:54:58 +0100107 ret = key_payload_reserve(key, prep->datalen);
108 if (ret < 0)
109 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
David Howells898de7d2016-04-12 19:54:58 +0100111 /* attach the new data, displacing the old */
112 key->expiry = prep->expiry;
David Howells363b02d2017-10-04 16:43:25 +0100113 if (key_is_positive(key))
David Howells0837e492017-03-01 15:11:23 +0000114 zap = dereference_key_locked(key);
David Howells898de7d2016-04-12 19:54:58 +0100115 rcu_assign_keypointer(key, prep->payload.data[0]);
116 prep->payload.data[0] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
David Howells9f35a332011-11-15 22:09:45 +0000118 if (zap)
Eric Biggers6966c742017-06-08 14:49:04 +0100119 call_rcu(&zap->rcu, user_free_payload_rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000121}
David Howells2aa349f2005-10-30 15:02:42 -0800122EXPORT_SYMBOL_GPL(user_update);
123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124/*
David Howells31204ed2006-06-26 00:24:51 -0700125 * dispose of the links from a revoked keyring
126 * - called with the key sem write-locked
127 */
128void user_revoke(struct key *key)
129{
David Howells0837e492017-03-01 15:11:23 +0000130 struct user_key_payload *upayload = user_key_payload_locked(key);
David Howells31204ed2006-06-26 00:24:51 -0700131
132 /* clear the quota */
133 key_payload_reserve(key, 0);
134
135 if (upayload) {
Mimi Zoharf6b24572012-01-18 10:03:14 +0000136 rcu_assign_keypointer(key, NULL);
Eric Biggers6966c742017-06-08 14:49:04 +0100137 call_rcu(&upayload->rcu, user_free_payload_rcu);
David Howells31204ed2006-06-26 00:24:51 -0700138 }
David Howellsa8b17ed2011-01-20 16:38:27 +0000139}
David Howells31204ed2006-06-26 00:24:51 -0700140
141EXPORT_SYMBOL(user_revoke);
142
David Howells31204ed2006-06-26 00:24:51 -0700143/*
144 * dispose of the data dangling from the corpse of a user key
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 */
David Howells2aa349f2005-10-30 15:02:42 -0800146void user_destroy(struct key *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147{
David Howells146aa8b2015-10-21 14:04:48 +0100148 struct user_key_payload *upayload = key->payload.data[0];
David Howells76d8aea2005-06-23 22:00:49 -0700149
Eric Biggers6966c742017-06-08 14:49:04 +0100150 kzfree(upayload);
David Howellsa8b17ed2011-01-20 16:38:27 +0000151}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
David Howells2aa349f2005-10-30 15:02:42 -0800153EXPORT_SYMBOL_GPL(user_destroy);
154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155/*
David Howells76d8aea2005-06-23 22:00:49 -0700156 * describe the user key
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 */
David Howells2aa349f2005-10-30 15:02:42 -0800158void user_describe(const struct key *key, struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159{
160 seq_puts(m, key->description);
David Howells363b02d2017-10-04 16:43:25 +0100161 if (key_is_positive(key))
David Howells78b72802011-03-11 17:57:23 +0000162 seq_printf(m, ": %u", key->datalen);
David Howellsa8b17ed2011-01-20 16:38:27 +0000163}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
David Howells2aa349f2005-10-30 15:02:42 -0800165EXPORT_SYMBOL_GPL(user_describe);
166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167/*
168 * read the key data
David Howells76d8aea2005-06-23 22:00:49 -0700169 * - the key's semaphore is read-locked
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 */
David Howells2aa349f2005-10-30 15:02:42 -0800171long user_read(const struct key *key, char __user *buffer, size_t buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172{
David Howells146aa8b2015-10-21 14:04:48 +0100173 const struct user_key_payload *upayload;
David Howells76d8aea2005-06-23 22:00:49 -0700174 long ret;
175
David Howells0837e492017-03-01 15:11:23 +0000176 upayload = user_key_payload_locked(key);
David Howells76d8aea2005-06-23 22:00:49 -0700177 ret = upayload->datalen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
179 /* we can return the data as is */
180 if (buffer && buflen > 0) {
David Howells76d8aea2005-06-23 22:00:49 -0700181 if (buflen > upayload->datalen)
182 buflen = upayload->datalen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
David Howells76d8aea2005-06-23 22:00:49 -0700184 if (copy_to_user(buffer, upayload->data, buflen) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 ret = -EFAULT;
186 }
187
188 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000189}
David Howells2aa349f2005-10-30 15:02:42 -0800190
191EXPORT_SYMBOL_GPL(user_read);
Jeff Layton9f6ed2c2012-01-17 16:09:11 -0500192
193/* Vet the description for a "logon" key */
194static int logon_vet_description(const char *desc)
195{
196 char *p;
197
198 /* require a "qualified" description string */
199 p = strchr(desc, ':');
200 if (!p)
201 return -EINVAL;
202
203 /* also reject description with ':' as first char */
204 if (p == desc)
205 return -EINVAL;
206
207 return 0;
208}