David Howells | ab3c358 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 1 | /* Large capacity key type |
| 2 | * |
| 3 | * Copyright (C) 2013 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 Licence |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the Licence, or (at your option) any later version. |
| 10 | */ |
| 11 | |
David Howells | ab3c358 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 12 | #include <linux/init.h> |
| 13 | #include <linux/seq_file.h> |
| 14 | #include <linux/file.h> |
| 15 | #include <linux/shmem_fs.h> |
| 16 | #include <linux/err.h> |
Kirill Marinushkin | 13100a7 | 2016-04-12 19:54:58 +0100 | [diff] [blame] | 17 | #include <linux/scatterlist.h> |
David Howells | ab3c358 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 18 | #include <keys/user-type.h> |
| 19 | #include <keys/big_key-type.h> |
Kirill Marinushkin | 13100a7 | 2016-04-12 19:54:58 +0100 | [diff] [blame] | 20 | #include <crypto/rng.h> |
David Howells | ab3c358 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 21 | |
David Howells | ab3c358 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 22 | /* |
David Howells | 146aa8b | 2015-10-21 14:04:48 +0100 | [diff] [blame] | 23 | * Layout of key payload words. |
| 24 | */ |
| 25 | enum { |
| 26 | big_key_data, |
| 27 | big_key_path, |
| 28 | big_key_path_2nd_part, |
| 29 | big_key_len, |
| 30 | }; |
| 31 | |
| 32 | /* |
Kirill Marinushkin | 13100a7 | 2016-04-12 19:54:58 +0100 | [diff] [blame] | 33 | * Crypto operation with big_key data |
| 34 | */ |
| 35 | enum big_key_op { |
| 36 | BIG_KEY_ENC, |
| 37 | BIG_KEY_DEC, |
| 38 | }; |
| 39 | |
| 40 | /* |
David Howells | ab3c358 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 41 | * If the data is under this limit, there's no point creating a shm file to |
| 42 | * hold it as the permanently resident metadata for the shmem fs will be at |
| 43 | * least as large as the data. |
| 44 | */ |
| 45 | #define BIG_KEY_FILE_THRESHOLD (sizeof(struct inode) + sizeof(struct dentry)) |
| 46 | |
| 47 | /* |
Kirill Marinushkin | 13100a7 | 2016-04-12 19:54:58 +0100 | [diff] [blame] | 48 | * Key size for big_key data encryption |
| 49 | */ |
| 50 | #define ENC_KEY_SIZE 16 |
| 51 | |
| 52 | /* |
David Howells | ab3c358 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 53 | * big_key defined keys take an arbitrary string as the description and an |
| 54 | * arbitrary blob of data as the payload |
| 55 | */ |
| 56 | struct key_type key_type_big_key = { |
| 57 | .name = "big_key", |
David Howells | 002edaf | 2014-07-18 18:56:36 +0100 | [diff] [blame] | 58 | .preparse = big_key_preparse, |
| 59 | .free_preparse = big_key_free_preparse, |
| 60 | .instantiate = generic_key_instantiate, |
David Howells | ab3c358 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 61 | .revoke = big_key_revoke, |
| 62 | .destroy = big_key_destroy, |
| 63 | .describe = big_key_describe, |
| 64 | .read = big_key_read, |
| 65 | }; |
| 66 | |
| 67 | /* |
Kirill Marinushkin | 13100a7 | 2016-04-12 19:54:58 +0100 | [diff] [blame] | 68 | * Crypto names for big_key data encryption |
| 69 | */ |
| 70 | static const char big_key_rng_name[] = "stdrng"; |
| 71 | static const char big_key_alg_name[] = "ecb(aes)"; |
| 72 | |
| 73 | /* |
| 74 | * Crypto algorithms for big_key data encryption |
| 75 | */ |
| 76 | static struct crypto_rng *big_key_rng; |
| 77 | static struct crypto_blkcipher *big_key_blkcipher; |
| 78 | |
| 79 | /* |
| 80 | * Generate random key to encrypt big_key data |
| 81 | */ |
| 82 | static inline int big_key_gen_enckey(u8 *key) |
| 83 | { |
| 84 | return crypto_rng_get_bytes(big_key_rng, key, ENC_KEY_SIZE); |
| 85 | } |
| 86 | |
| 87 | /* |
| 88 | * Encrypt/decrypt big_key data |
| 89 | */ |
| 90 | static int big_key_crypt(enum big_key_op op, u8 *data, size_t datalen, u8 *key) |
| 91 | { |
| 92 | int ret = -EINVAL; |
| 93 | struct scatterlist sgio; |
| 94 | struct blkcipher_desc desc; |
| 95 | |
| 96 | if (crypto_blkcipher_setkey(big_key_blkcipher, key, ENC_KEY_SIZE)) { |
| 97 | ret = -EAGAIN; |
| 98 | goto error; |
| 99 | } |
| 100 | |
| 101 | desc.flags = 0; |
| 102 | desc.tfm = big_key_blkcipher; |
| 103 | |
| 104 | sg_init_one(&sgio, data, datalen); |
| 105 | |
| 106 | if (op == BIG_KEY_ENC) |
| 107 | ret = crypto_blkcipher_encrypt(&desc, &sgio, &sgio, datalen); |
| 108 | else |
| 109 | ret = crypto_blkcipher_decrypt(&desc, &sgio, &sgio, datalen); |
| 110 | |
| 111 | error: |
| 112 | return ret; |
| 113 | } |
| 114 | |
| 115 | /* |
David Howells | 002edaf | 2014-07-18 18:56:36 +0100 | [diff] [blame] | 116 | * Preparse a big key |
David Howells | ab3c358 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 117 | */ |
David Howells | 002edaf | 2014-07-18 18:56:36 +0100 | [diff] [blame] | 118 | int big_key_preparse(struct key_preparsed_payload *prep) |
David Howells | ab3c358 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 119 | { |
David Howells | 146aa8b | 2015-10-21 14:04:48 +0100 | [diff] [blame] | 120 | struct path *path = (struct path *)&prep->payload.data[big_key_path]; |
David Howells | ab3c358 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 121 | struct file *file; |
Kirill Marinushkin | 13100a7 | 2016-04-12 19:54:58 +0100 | [diff] [blame] | 122 | u8 *enckey; |
| 123 | u8 *data = NULL; |
David Howells | ab3c358 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 124 | ssize_t written; |
| 125 | size_t datalen = prep->datalen; |
| 126 | int ret; |
| 127 | |
| 128 | ret = -EINVAL; |
| 129 | if (datalen <= 0 || datalen > 1024 * 1024 || !prep->data) |
| 130 | goto error; |
| 131 | |
| 132 | /* Set an arbitrary quota */ |
David Howells | 002edaf | 2014-07-18 18:56:36 +0100 | [diff] [blame] | 133 | prep->quotalen = 16; |
David Howells | ab3c358 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 134 | |
David Howells | 146aa8b | 2015-10-21 14:04:48 +0100 | [diff] [blame] | 135 | prep->payload.data[big_key_len] = (void *)(unsigned long)datalen; |
David Howells | ab3c358 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 136 | |
| 137 | if (datalen > BIG_KEY_FILE_THRESHOLD) { |
| 138 | /* Create a shmem file to store the data in. This will permit the data |
| 139 | * to be swapped out if needed. |
| 140 | * |
Kirill Marinushkin | 13100a7 | 2016-04-12 19:54:58 +0100 | [diff] [blame] | 141 | * File content is stored encrypted with randomly generated key. |
David Howells | ab3c358 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 142 | */ |
Kirill Marinushkin | 13100a7 | 2016-04-12 19:54:58 +0100 | [diff] [blame] | 143 | size_t enclen = ALIGN(datalen, crypto_blkcipher_blocksize(big_key_blkcipher)); |
| 144 | |
| 145 | /* prepare aligned data to encrypt */ |
| 146 | data = kmalloc(enclen, GFP_KERNEL); |
| 147 | if (!data) |
| 148 | return -ENOMEM; |
| 149 | |
| 150 | memcpy(data, prep->data, datalen); |
| 151 | memset(data + datalen, 0x00, enclen - datalen); |
| 152 | |
| 153 | /* generate random key */ |
| 154 | enckey = kmalloc(ENC_KEY_SIZE, GFP_KERNEL); |
| 155 | if (!enckey) { |
| 156 | ret = -ENOMEM; |
David Howells | 002edaf | 2014-07-18 18:56:36 +0100 | [diff] [blame] | 157 | goto error; |
Wei Yongjun | d2b8697 | 2013-10-30 11:23:02 +0800 | [diff] [blame] | 158 | } |
David Howells | ab3c358 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 159 | |
Kirill Marinushkin | 13100a7 | 2016-04-12 19:54:58 +0100 | [diff] [blame] | 160 | ret = big_key_gen_enckey(enckey); |
| 161 | if (ret) |
| 162 | goto err_enckey; |
| 163 | |
| 164 | /* encrypt aligned data */ |
| 165 | ret = big_key_crypt(BIG_KEY_ENC, data, enclen, enckey); |
| 166 | if (ret) |
| 167 | goto err_enckey; |
| 168 | |
| 169 | /* save aligned data to file */ |
| 170 | file = shmem_kernel_file_setup("", enclen, 0); |
| 171 | if (IS_ERR(file)) { |
| 172 | ret = PTR_ERR(file); |
| 173 | goto err_enckey; |
| 174 | } |
| 175 | |
| 176 | written = kernel_write(file, data, enclen, 0); |
| 177 | if (written != enclen) { |
David Howells | 97826c8 | 2013-11-13 16:51:06 +0000 | [diff] [blame] | 178 | ret = written; |
David Howells | ab3c358 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 179 | if (written >= 0) |
| 180 | ret = -ENOMEM; |
| 181 | goto err_fput; |
| 182 | } |
| 183 | |
| 184 | /* Pin the mount and dentry to the key so that we can open it again |
| 185 | * later |
| 186 | */ |
Kirill Marinushkin | 13100a7 | 2016-04-12 19:54:58 +0100 | [diff] [blame] | 187 | prep->payload.data[big_key_data] = enckey; |
David Howells | ab3c358 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 188 | *path = file->f_path; |
| 189 | path_get(path); |
| 190 | fput(file); |
Kirill Marinushkin | 13100a7 | 2016-04-12 19:54:58 +0100 | [diff] [blame] | 191 | kfree(data); |
David Howells | ab3c358 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 192 | } else { |
| 193 | /* Just store the data in a buffer */ |
| 194 | void *data = kmalloc(datalen, GFP_KERNEL); |
Kirill Marinushkin | 13100a7 | 2016-04-12 19:54:58 +0100 | [diff] [blame] | 195 | |
David Howells | 002edaf | 2014-07-18 18:56:36 +0100 | [diff] [blame] | 196 | if (!data) |
| 197 | return -ENOMEM; |
David Howells | ab3c358 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 198 | |
David Howells | 146aa8b | 2015-10-21 14:04:48 +0100 | [diff] [blame] | 199 | prep->payload.data[big_key_data] = data; |
| 200 | memcpy(data, prep->data, prep->datalen); |
David Howells | ab3c358 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 201 | } |
| 202 | return 0; |
| 203 | |
| 204 | err_fput: |
| 205 | fput(file); |
Kirill Marinushkin | 13100a7 | 2016-04-12 19:54:58 +0100 | [diff] [blame] | 206 | err_enckey: |
| 207 | kfree(enckey); |
David Howells | ab3c358 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 208 | error: |
Kirill Marinushkin | 13100a7 | 2016-04-12 19:54:58 +0100 | [diff] [blame] | 209 | kfree(data); |
David Howells | ab3c358 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 210 | return ret; |
| 211 | } |
| 212 | |
| 213 | /* |
David Howells | 002edaf | 2014-07-18 18:56:36 +0100 | [diff] [blame] | 214 | * Clear preparsement. |
| 215 | */ |
| 216 | void big_key_free_preparse(struct key_preparsed_payload *prep) |
| 217 | { |
| 218 | if (prep->datalen > BIG_KEY_FILE_THRESHOLD) { |
David Howells | 146aa8b | 2015-10-21 14:04:48 +0100 | [diff] [blame] | 219 | struct path *path = (struct path *)&prep->payload.data[big_key_path]; |
Kirill Marinushkin | 13100a7 | 2016-04-12 19:54:58 +0100 | [diff] [blame] | 220 | |
David Howells | 002edaf | 2014-07-18 18:56:36 +0100 | [diff] [blame] | 221 | path_put(path); |
David Howells | 002edaf | 2014-07-18 18:56:36 +0100 | [diff] [blame] | 222 | } |
Kirill Marinushkin | 13100a7 | 2016-04-12 19:54:58 +0100 | [diff] [blame] | 223 | kfree(prep->payload.data[big_key_data]); |
David Howells | 002edaf | 2014-07-18 18:56:36 +0100 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | /* |
David Howells | ab3c358 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 227 | * dispose of the links from a revoked keyring |
| 228 | * - called with the key sem write-locked |
| 229 | */ |
| 230 | void big_key_revoke(struct key *key) |
| 231 | { |
David Howells | 146aa8b | 2015-10-21 14:04:48 +0100 | [diff] [blame] | 232 | struct path *path = (struct path *)&key->payload.data[big_key_path]; |
David Howells | ab3c358 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 233 | |
| 234 | /* clear the quota */ |
| 235 | key_payload_reserve(key, 0); |
David Howells | 146aa8b | 2015-10-21 14:04:48 +0100 | [diff] [blame] | 236 | if (key_is_instantiated(key) && |
| 237 | (size_t)key->payload.data[big_key_len] > BIG_KEY_FILE_THRESHOLD) |
David Howells | ab3c358 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 238 | vfs_truncate(path, 0); |
| 239 | } |
| 240 | |
| 241 | /* |
| 242 | * dispose of the data dangling from the corpse of a big_key key |
| 243 | */ |
| 244 | void big_key_destroy(struct key *key) |
| 245 | { |
David Howells | 146aa8b | 2015-10-21 14:04:48 +0100 | [diff] [blame] | 246 | size_t datalen = (size_t)key->payload.data[big_key_len]; |
| 247 | |
Kirill Marinushkin | 13100a7 | 2016-04-12 19:54:58 +0100 | [diff] [blame] | 248 | if (datalen > BIG_KEY_FILE_THRESHOLD) { |
David Howells | 146aa8b | 2015-10-21 14:04:48 +0100 | [diff] [blame] | 249 | struct path *path = (struct path *)&key->payload.data[big_key_path]; |
Kirill Marinushkin | 13100a7 | 2016-04-12 19:54:58 +0100 | [diff] [blame] | 250 | |
David Howells | ab3c358 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 251 | path_put(path); |
| 252 | path->mnt = NULL; |
| 253 | path->dentry = NULL; |
David Howells | ab3c358 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 254 | } |
Kirill Marinushkin | 13100a7 | 2016-04-12 19:54:58 +0100 | [diff] [blame] | 255 | kfree(key->payload.data[big_key_data]); |
| 256 | key->payload.data[big_key_data] = NULL; |
David Howells | ab3c358 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | /* |
| 260 | * describe the big_key key |
| 261 | */ |
| 262 | void big_key_describe(const struct key *key, struct seq_file *m) |
| 263 | { |
David Howells | 146aa8b | 2015-10-21 14:04:48 +0100 | [diff] [blame] | 264 | size_t datalen = (size_t)key->payload.data[big_key_len]; |
David Howells | ab3c358 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 265 | |
| 266 | seq_puts(m, key->description); |
| 267 | |
| 268 | if (key_is_instantiated(key)) |
David Howells | 146aa8b | 2015-10-21 14:04:48 +0100 | [diff] [blame] | 269 | seq_printf(m, ": %zu [%s]", |
David Howells | ab3c358 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 270 | datalen, |
| 271 | datalen > BIG_KEY_FILE_THRESHOLD ? "file" : "buff"); |
| 272 | } |
| 273 | |
| 274 | /* |
| 275 | * read the key data |
| 276 | * - the key's semaphore is read-locked |
| 277 | */ |
| 278 | long big_key_read(const struct key *key, char __user *buffer, size_t buflen) |
| 279 | { |
David Howells | 146aa8b | 2015-10-21 14:04:48 +0100 | [diff] [blame] | 280 | size_t datalen = (size_t)key->payload.data[big_key_len]; |
David Howells | ab3c358 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 281 | long ret; |
| 282 | |
| 283 | if (!buffer || buflen < datalen) |
| 284 | return datalen; |
| 285 | |
| 286 | if (datalen > BIG_KEY_FILE_THRESHOLD) { |
David Howells | 146aa8b | 2015-10-21 14:04:48 +0100 | [diff] [blame] | 287 | struct path *path = (struct path *)&key->payload.data[big_key_path]; |
David Howells | ab3c358 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 288 | struct file *file; |
Kirill Marinushkin | 13100a7 | 2016-04-12 19:54:58 +0100 | [diff] [blame] | 289 | u8 *data; |
| 290 | u8 *enckey = (u8 *)key->payload.data[big_key_data]; |
| 291 | size_t enclen = ALIGN(datalen, crypto_blkcipher_blocksize(big_key_blkcipher)); |
| 292 | |
| 293 | data = kmalloc(enclen, GFP_KERNEL); |
| 294 | if (!data) |
| 295 | return -ENOMEM; |
David Howells | ab3c358 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 296 | |
| 297 | file = dentry_open(path, O_RDONLY, current_cred()); |
Kirill Marinushkin | 13100a7 | 2016-04-12 19:54:58 +0100 | [diff] [blame] | 298 | if (IS_ERR(file)) { |
| 299 | ret = PTR_ERR(file); |
| 300 | goto error; |
| 301 | } |
David Howells | ab3c358 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 302 | |
Kirill Marinushkin | 13100a7 | 2016-04-12 19:54:58 +0100 | [diff] [blame] | 303 | /* read file to kernel and decrypt */ |
| 304 | ret = kernel_read(file, 0, data, enclen); |
| 305 | if (ret >= 0 && ret != enclen) { |
David Howells | ab3c358 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 306 | ret = -EIO; |
Kirill Marinushkin | 13100a7 | 2016-04-12 19:54:58 +0100 | [diff] [blame] | 307 | goto err_fput; |
| 308 | } |
| 309 | |
| 310 | ret = big_key_crypt(BIG_KEY_DEC, data, enclen, enckey); |
| 311 | if (ret) |
| 312 | goto err_fput; |
| 313 | |
| 314 | ret = datalen; |
| 315 | |
| 316 | /* copy decrypted data to user */ |
| 317 | if (copy_to_user(buffer, data, datalen) != 0) |
| 318 | ret = -EFAULT; |
| 319 | |
| 320 | err_fput: |
| 321 | fput(file); |
| 322 | error: |
| 323 | kfree(data); |
David Howells | ab3c358 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 324 | } else { |
| 325 | ret = datalen; |
David Howells | 146aa8b | 2015-10-21 14:04:48 +0100 | [diff] [blame] | 326 | if (copy_to_user(buffer, key->payload.data[big_key_data], |
| 327 | datalen) != 0) |
David Howells | ab3c358 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 328 | ret = -EFAULT; |
| 329 | } |
| 330 | |
| 331 | return ret; |
| 332 | } |
| 333 | |
Kirill Marinushkin | 13100a7 | 2016-04-12 19:54:58 +0100 | [diff] [blame] | 334 | /* |
| 335 | * Register key type |
| 336 | */ |
David Howells | ab3c358 | 2013-09-24 10:35:18 +0100 | [diff] [blame] | 337 | static int __init big_key_init(void) |
| 338 | { |
| 339 | return register_key_type(&key_type_big_key); |
| 340 | } |
Kirill Marinushkin | 13100a7 | 2016-04-12 19:54:58 +0100 | [diff] [blame] | 341 | |
| 342 | /* |
| 343 | * Initialize big_key crypto and RNG algorithms |
| 344 | */ |
| 345 | static int __init big_key_crypto_init(void) |
| 346 | { |
| 347 | int ret = -EINVAL; |
| 348 | |
| 349 | /* init RNG */ |
| 350 | big_key_rng = crypto_alloc_rng(big_key_rng_name, 0, 0); |
| 351 | if (IS_ERR(big_key_rng)) { |
| 352 | big_key_rng = NULL; |
| 353 | return -EFAULT; |
| 354 | } |
| 355 | |
| 356 | /* seed RNG */ |
| 357 | ret = crypto_rng_reset(big_key_rng, NULL, crypto_rng_seedsize(big_key_rng)); |
| 358 | if (ret) |
| 359 | goto error; |
| 360 | |
| 361 | /* init block cipher */ |
| 362 | big_key_blkcipher = crypto_alloc_blkcipher(big_key_alg_name, 0, 0); |
| 363 | if (IS_ERR(big_key_blkcipher)) { |
| 364 | big_key_blkcipher = NULL; |
| 365 | ret = -EFAULT; |
| 366 | goto error; |
| 367 | } |
| 368 | |
| 369 | return 0; |
| 370 | |
| 371 | error: |
| 372 | crypto_free_rng(big_key_rng); |
| 373 | big_key_rng = NULL; |
| 374 | return ret; |
| 375 | } |
| 376 | |
Paul Gortmaker | a1f2bdf | 2015-12-09 17:37:15 -0500 | [diff] [blame] | 377 | device_initcall(big_key_init); |
Kirill Marinushkin | 13100a7 | 2016-04-12 19:54:58 +0100 | [diff] [blame] | 378 | late_initcall(big_key_crypto_init); |