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