Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | */ |
| 7 | |
Paul Gortmaker | 876979c | 2018-12-09 15:36:29 -0500 | [diff] [blame] | 8 | #include <linux/export.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | #include <linux/init.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | #include <linux/slab.h> |
| 11 | #include <linux/seq_file.h> |
| 12 | #include <linux/err.h> |
David Howells | 2aa349f | 2005-10-30 15:02:42 -0800 | [diff] [blame] | 13 | #include <keys/user-type.h> |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 14 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include "internal.h" |
| 16 | |
Jeff Layton | 9f6ed2c | 2012-01-17 16:09:11 -0500 | [diff] [blame] | 17 | static int logon_vet_description(const char *desc); |
| 18 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | /* |
| 20 | * user defined keys take an arbitrary string as the description and an |
| 21 | * arbitrary blob of data as the payload |
| 22 | */ |
| 23 | struct key_type key_type_user = { |
David Howells | 4bdf0bc | 2013-09-24 10:35:15 +0100 | [diff] [blame] | 24 | .name = "user", |
David Howells | f916778 | 2014-07-18 18:56:35 +0100 | [diff] [blame] | 25 | .preparse = user_preparse, |
| 26 | .free_preparse = user_free_preparse, |
| 27 | .instantiate = generic_key_instantiate, |
David Howells | 4bdf0bc | 2013-09-24 10:35:15 +0100 | [diff] [blame] | 28 | .update = user_update, |
David Howells | 4bdf0bc | 2013-09-24 10:35:15 +0100 | [diff] [blame] | 29 | .revoke = user_revoke, |
| 30 | .destroy = user_destroy, |
| 31 | .describe = user_describe, |
| 32 | .read = user_read, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | }; |
| 34 | |
Michael Halcrow | 16c29b6 | 2005-06-23 22:00:58 -0700 | [diff] [blame] | 35 | EXPORT_SYMBOL_GPL(key_type_user); |
| 36 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | /* |
Jeff Layton | 9f6ed2c | 2012-01-17 16:09:11 -0500 | [diff] [blame] | 38 | * 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 | */ |
| 43 | struct key_type key_type_logon = { |
| 44 | .name = "logon", |
David Howells | f916778 | 2014-07-18 18:56:35 +0100 | [diff] [blame] | 45 | .preparse = user_preparse, |
| 46 | .free_preparse = user_free_preparse, |
| 47 | .instantiate = generic_key_instantiate, |
Jeff Layton | 9f6ed2c | 2012-01-17 16:09:11 -0500 | [diff] [blame] | 48 | .update = user_update, |
Jeff Layton | 9f6ed2c | 2012-01-17 16:09:11 -0500 | [diff] [blame] | 49 | .revoke = user_revoke, |
| 50 | .destroy = user_destroy, |
| 51 | .describe = user_describe, |
| 52 | .vet_description = logon_vet_description, |
| 53 | }; |
| 54 | EXPORT_SYMBOL_GPL(key_type_logon); |
| 55 | |
| 56 | /* |
David Howells | f916778 | 2014-07-18 18:56:35 +0100 | [diff] [blame] | 57 | * Preparse a user defined key payload |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | */ |
David Howells | f916778 | 2014-07-18 18:56:35 +0100 | [diff] [blame] | 59 | int user_preparse(struct key_preparsed_payload *prep) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | { |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 61 | struct user_key_payload *upayload; |
David Howells | cf7f601 | 2012-09-13 13:06:29 +0100 | [diff] [blame] | 62 | size_t datalen = prep->datalen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | |
David Howells | cf7f601 | 2012-09-13 13:06:29 +0100 | [diff] [blame] | 64 | if (datalen <= 0 || datalen > 32767 || !prep->data) |
David Howells | f916778 | 2014-07-18 18:56:35 +0100 | [diff] [blame] | 65 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 67 | upayload = kmalloc(sizeof(*upayload) + datalen, GFP_KERNEL); |
| 68 | if (!upayload) |
David Howells | f916778 | 2014-07-18 18:56:35 +0100 | [diff] [blame] | 69 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 71 | /* attach the data */ |
David Howells | f916778 | 2014-07-18 18:56:35 +0100 | [diff] [blame] | 72 | prep->quotalen = datalen; |
David Howells | 146aa8b | 2015-10-21 14:04:48 +0100 | [diff] [blame] | 73 | prep->payload.data[0] = upayload; |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 74 | upayload->datalen = datalen; |
David Howells | cf7f601 | 2012-09-13 13:06:29 +0100 | [diff] [blame] | 75 | memcpy(upayload->data, prep->data, datalen); |
David Howells | f916778 | 2014-07-18 18:56:35 +0100 | [diff] [blame] | 76 | return 0; |
David Howells | a8b17ed | 2011-01-20 16:38:27 +0000 | [diff] [blame] | 77 | } |
David Howells | f916778 | 2014-07-18 18:56:35 +0100 | [diff] [blame] | 78 | EXPORT_SYMBOL_GPL(user_preparse); |
David Howells | 31204ed | 2006-06-26 00:24:51 -0700 | [diff] [blame] | 79 | |
David Howells | f916778 | 2014-07-18 18:56:35 +0100 | [diff] [blame] | 80 | /* |
| 81 | * Free a preparse of a user defined key payload |
| 82 | */ |
| 83 | void user_free_preparse(struct key_preparsed_payload *prep) |
| 84 | { |
Waiman Long | 453431a | 2020-08-06 23:18:13 -0700 | [diff] [blame] | 85 | kfree_sensitive(prep->payload.data[0]); |
David Howells | f916778 | 2014-07-18 18:56:35 +0100 | [diff] [blame] | 86 | } |
| 87 | EXPORT_SYMBOL_GPL(user_free_preparse); |
David Howells | 2aa349f | 2005-10-30 15:02:42 -0800 | [diff] [blame] | 88 | |
Eric Biggers | 6966c74 | 2017-06-08 14:49:04 +0100 | [diff] [blame] | 89 | static 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); |
Waiman Long | 453431a | 2020-08-06 23:18:13 -0700 | [diff] [blame] | 94 | kfree_sensitive(payload); |
Eric Biggers | 6966c74 | 2017-06-08 14:49:04 +0100 | [diff] [blame] | 95 | } |
| 96 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | * update a user defined key |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 99 | * - the key's semaphore is write-locked |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | */ |
David Howells | cf7f601 | 2012-09-13 13:06:29 +0100 | [diff] [blame] | 101 | int user_update(struct key *key, struct key_preparsed_payload *prep) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | { |
David Howells | 898de7d | 2016-04-12 19:54:58 +0100 | [diff] [blame] | 103 | struct user_key_payload *zap = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | int ret; |
| 105 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | /* check the quota and attach the new data */ |
David Howells | 898de7d | 2016-04-12 19:54:58 +0100 | [diff] [blame] | 107 | ret = key_payload_reserve(key, prep->datalen); |
| 108 | if (ret < 0) |
| 109 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | |
David Howells | 898de7d | 2016-04-12 19:54:58 +0100 | [diff] [blame] | 111 | /* attach the new data, displacing the old */ |
| 112 | key->expiry = prep->expiry; |
David Howells | 363b02d | 2017-10-04 16:43:25 +0100 | [diff] [blame] | 113 | if (key_is_positive(key)) |
David Howells | 0837e49 | 2017-03-01 15:11:23 +0000 | [diff] [blame] | 114 | zap = dereference_key_locked(key); |
David Howells | 898de7d | 2016-04-12 19:54:58 +0100 | [diff] [blame] | 115 | rcu_assign_keypointer(key, prep->payload.data[0]); |
| 116 | prep->payload.data[0] = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | |
David Howells | 9f35a33 | 2011-11-15 22:09:45 +0000 | [diff] [blame] | 118 | if (zap) |
Eric Biggers | 6966c74 | 2017-06-08 14:49:04 +0100 | [diff] [blame] | 119 | call_rcu(&zap->rcu, user_free_payload_rcu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | return ret; |
David Howells | a8b17ed | 2011-01-20 16:38:27 +0000 | [diff] [blame] | 121 | } |
David Howells | 2aa349f | 2005-10-30 15:02:42 -0800 | [diff] [blame] | 122 | EXPORT_SYMBOL_GPL(user_update); |
| 123 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | /* |
David Howells | 31204ed | 2006-06-26 00:24:51 -0700 | [diff] [blame] | 125 | * dispose of the links from a revoked keyring |
| 126 | * - called with the key sem write-locked |
| 127 | */ |
| 128 | void user_revoke(struct key *key) |
| 129 | { |
David Howells | 0837e49 | 2017-03-01 15:11:23 +0000 | [diff] [blame] | 130 | struct user_key_payload *upayload = user_key_payload_locked(key); |
David Howells | 31204ed | 2006-06-26 00:24:51 -0700 | [diff] [blame] | 131 | |
| 132 | /* clear the quota */ |
| 133 | key_payload_reserve(key, 0); |
| 134 | |
| 135 | if (upayload) { |
Mimi Zohar | f6b2457 | 2012-01-18 10:03:14 +0000 | [diff] [blame] | 136 | rcu_assign_keypointer(key, NULL); |
Eric Biggers | 6966c74 | 2017-06-08 14:49:04 +0100 | [diff] [blame] | 137 | call_rcu(&upayload->rcu, user_free_payload_rcu); |
David Howells | 31204ed | 2006-06-26 00:24:51 -0700 | [diff] [blame] | 138 | } |
David Howells | a8b17ed | 2011-01-20 16:38:27 +0000 | [diff] [blame] | 139 | } |
David Howells | 31204ed | 2006-06-26 00:24:51 -0700 | [diff] [blame] | 140 | |
| 141 | EXPORT_SYMBOL(user_revoke); |
| 142 | |
David Howells | 31204ed | 2006-06-26 00:24:51 -0700 | [diff] [blame] | 143 | /* |
| 144 | * dispose of the data dangling from the corpse of a user key |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | */ |
David Howells | 2aa349f | 2005-10-30 15:02:42 -0800 | [diff] [blame] | 146 | void user_destroy(struct key *key) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | { |
David Howells | 146aa8b | 2015-10-21 14:04:48 +0100 | [diff] [blame] | 148 | struct user_key_payload *upayload = key->payload.data[0]; |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 149 | |
Waiman Long | 453431a | 2020-08-06 23:18:13 -0700 | [diff] [blame] | 150 | kfree_sensitive(upayload); |
David Howells | a8b17ed | 2011-01-20 16:38:27 +0000 | [diff] [blame] | 151 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | |
David Howells | 2aa349f | 2005-10-30 15:02:42 -0800 | [diff] [blame] | 153 | EXPORT_SYMBOL_GPL(user_destroy); |
| 154 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | /* |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 156 | * describe the user key |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | */ |
David Howells | 2aa349f | 2005-10-30 15:02:42 -0800 | [diff] [blame] | 158 | void user_describe(const struct key *key, struct seq_file *m) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | { |
| 160 | seq_puts(m, key->description); |
David Howells | 363b02d | 2017-10-04 16:43:25 +0100 | [diff] [blame] | 161 | if (key_is_positive(key)) |
David Howells | 78b7280 | 2011-03-11 17:57:23 +0000 | [diff] [blame] | 162 | seq_printf(m, ": %u", key->datalen); |
David Howells | a8b17ed | 2011-01-20 16:38:27 +0000 | [diff] [blame] | 163 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | |
David Howells | 2aa349f | 2005-10-30 15:02:42 -0800 | [diff] [blame] | 165 | EXPORT_SYMBOL_GPL(user_describe); |
| 166 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | /* |
| 168 | * read the key data |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 169 | * - the key's semaphore is read-locked |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | */ |
Waiman Long | d3ec10a | 2020-03-21 21:11:24 -0400 | [diff] [blame] | 171 | long user_read(const struct key *key, char *buffer, size_t buflen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | { |
David Howells | 146aa8b | 2015-10-21 14:04:48 +0100 | [diff] [blame] | 173 | const struct user_key_payload *upayload; |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 174 | long ret; |
| 175 | |
David Howells | 0837e49 | 2017-03-01 15:11:23 +0000 | [diff] [blame] | 176 | upayload = user_key_payload_locked(key); |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 177 | ret = upayload->datalen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | |
| 179 | /* we can return the data as is */ |
| 180 | if (buffer && buflen > 0) { |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 181 | if (buflen > upayload->datalen) |
| 182 | buflen = upayload->datalen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | |
Waiman Long | d3ec10a | 2020-03-21 21:11:24 -0400 | [diff] [blame] | 184 | memcpy(buffer, upayload->data, buflen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | return ret; |
David Howells | a8b17ed | 2011-01-20 16:38:27 +0000 | [diff] [blame] | 188 | } |
David Howells | 2aa349f | 2005-10-30 15:02:42 -0800 | [diff] [blame] | 189 | |
| 190 | EXPORT_SYMBOL_GPL(user_read); |
Jeff Layton | 9f6ed2c | 2012-01-17 16:09:11 -0500 | [diff] [blame] | 191 | |
| 192 | /* Vet the description for a "logon" key */ |
| 193 | static int logon_vet_description(const char *desc) |
| 194 | { |
| 195 | char *p; |
| 196 | |
| 197 | /* require a "qualified" description string */ |
| 198 | p = strchr(desc, ':'); |
| 199 | if (!p) |
| 200 | return -EINVAL; |
| 201 | |
| 202 | /* also reject description with ':' as first char */ |
| 203 | if (p == desc) |
| 204 | return -EINVAL; |
| 205 | |
| 206 | return 0; |
| 207 | } |