1 /* Large capacity key type
4 * Copyright (C) 2013 Red Hat, Inc. All Rights Reserved.
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.
13 #define pr_fmt(fmt) "big_key: "fmt
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>
19 #include <linux/scatterlist.h>
20 #include <linux/random.h>
21 #include <keys/user-type.h>
22 #include <keys/big_key-type.h>
23 #include <crypto/aead.h>
26 * Layout of key payload words.
31 big_key_path_2nd_part,
36 * Crypto operation with big_key data
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.
48 #define BIG_KEY_FILE_THRESHOLD (sizeof(struct inode) + sizeof(struct dentry))
51 * Key size for big_key data encryption
53 #define ENC_KEY_SIZE 32
56 * Authentication tag length
58 #define ENC_AUTHTAG_SIZE 16
61 * big_key defined keys take an arbitrary string as the description and an
62 * arbitrary blob of data as the payload
64 struct key_type key_type_big_key = {
66 .preparse = big_key_preparse,
67 .free_preparse = big_key_free_preparse,
68 .instantiate = generic_key_instantiate,
69 .revoke = big_key_revoke,
70 .destroy = big_key_destroy,
71 .describe = big_key_describe,
73 /* no ->update(); don't add it without changing big_key_crypt() nonce */
77 * Crypto names for big_key data authenticated encryption
79 static const char big_key_alg_name[] = "gcm(aes)";
82 * Crypto algorithms for big_key data authenticated encryption
84 static struct crypto_aead *big_key_aead;
87 * Since changing the key affects the entire object, we need a mutex.
89 static DEFINE_MUTEX(big_key_aead_lock);
92 * Encrypt/decrypt big_key data
94 static int big_key_crypt(enum big_key_op op, u8 *data, size_t datalen, u8 *key)
97 struct scatterlist sgio;
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.
105 u8 zero_nonce[crypto_aead_ivsize(big_key_aead)];
107 aead_req = aead_request_alloc(big_key_aead, GFP_KERNEL);
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);
117 mutex_lock(&big_key_aead_lock);
118 if (crypto_aead_setkey(big_key_aead, key, ENC_KEY_SIZE)) {
122 if (op == BIG_KEY_ENC)
123 ret = crypto_aead_encrypt(aead_req);
125 ret = crypto_aead_decrypt(aead_req);
127 mutex_unlock(&big_key_aead_lock);
128 aead_request_free(aead_req);
135 int big_key_preparse(struct key_preparsed_payload *prep)
137 struct path *path = (struct path *)&prep->payload.data[big_key_path];
142 size_t datalen = prep->datalen;
146 if (datalen <= 0 || datalen > 1024 * 1024 || !prep->data)
149 /* Set an arbitrary quota */
152 prep->payload.data[big_key_len] = (void *)(unsigned long)datalen;
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.
158 * File content is stored encrypted with randomly generated key.
160 size_t enclen = datalen + ENC_AUTHTAG_SIZE;
163 data = kmalloc(enclen, GFP_KERNEL);
166 memcpy(data, prep->data, datalen);
168 /* generate random key */
169 enckey = kmalloc(ENC_KEY_SIZE, GFP_KERNEL);
174 ret = get_random_bytes_wait(enckey, ENC_KEY_SIZE);
178 /* encrypt aligned data */
179 ret = big_key_crypt(BIG_KEY_ENC, data, datalen, enckey);
183 /* save aligned data to file */
184 file = shmem_kernel_file_setup("", enclen, 0);
190 written = kernel_write(file, data, enclen, &pos);
191 if (written != enclen) {
198 /* Pin the mount and dentry to the key so that we can open it again
201 prep->payload.data[big_key_data] = enckey;
202 *path = file->f_path;
207 /* Just store the data in a buffer */
208 void *data = kmalloc(datalen, GFP_KERNEL);
213 prep->payload.data[big_key_data] = data;
214 memcpy(data, prep->data, prep->datalen);
228 * Clear preparsement.
230 void big_key_free_preparse(struct key_preparsed_payload *prep)
232 if (prep->datalen > BIG_KEY_FILE_THRESHOLD) {
233 struct path *path = (struct path *)&prep->payload.data[big_key_path];
237 kzfree(prep->payload.data[big_key_data]);
241 * dispose of the links from a revoked keyring
242 * - called with the key sem write-locked
244 void big_key_revoke(struct key *key)
246 struct path *path = (struct path *)&key->payload.data[big_key_path];
248 /* clear the quota */
249 key_payload_reserve(key, 0);
250 if (key_is_positive(key) &&
251 (size_t)key->payload.data[big_key_len] > BIG_KEY_FILE_THRESHOLD)
252 vfs_truncate(path, 0);
256 * dispose of the data dangling from the corpse of a big_key key
258 void big_key_destroy(struct key *key)
260 size_t datalen = (size_t)key->payload.data[big_key_len];
262 if (datalen > BIG_KEY_FILE_THRESHOLD) {
263 struct path *path = (struct path *)&key->payload.data[big_key_path];
269 kzfree(key->payload.data[big_key_data]);
270 key->payload.data[big_key_data] = NULL;
274 * describe the big_key key
276 void big_key_describe(const struct key *key, struct seq_file *m)
278 size_t datalen = (size_t)key->payload.data[big_key_len];
280 seq_puts(m, key->description);
282 if (key_is_positive(key))
283 seq_printf(m, ": %zu [%s]",
285 datalen > BIG_KEY_FILE_THRESHOLD ? "file" : "buff");
290 * - the key's semaphore is read-locked
292 long big_key_read(const struct key *key, char __user *buffer, size_t buflen)
294 size_t datalen = (size_t)key->payload.data[big_key_len];
297 if (!buffer || buflen < datalen)
300 if (datalen > BIG_KEY_FILE_THRESHOLD) {
301 struct path *path = (struct path *)&key->payload.data[big_key_path];
304 u8 *enckey = (u8 *)key->payload.data[big_key_data];
305 size_t enclen = datalen + ENC_AUTHTAG_SIZE;
308 data = kmalloc(enclen, GFP_KERNEL);
312 file = dentry_open(path, O_RDONLY, current_cred());
318 /* read file to kernel and decrypt */
319 ret = kernel_read(file, data, enclen, &pos);
320 if (ret >= 0 && ret != enclen) {
325 ret = big_key_crypt(BIG_KEY_DEC, data, enclen, enckey);
331 /* copy decrypted data to user */
332 if (copy_to_user(buffer, data, datalen) != 0)
341 if (copy_to_user(buffer, key->payload.data[big_key_data],
352 static int __init big_key_init(void)
356 /* init block cipher */
357 big_key_aead = crypto_alloc_aead(big_key_alg_name, 0, CRYPTO_ALG_ASYNC);
358 if (IS_ERR(big_key_aead)) {
359 ret = PTR_ERR(big_key_aead);
360 pr_err("Can't alloc crypto: %d\n", ret);
363 ret = crypto_aead_setauthsize(big_key_aead, ENC_AUTHTAG_SIZE);
365 pr_err("Can't set crypto auth tag len: %d\n", ret);
369 ret = register_key_type(&key_type_big_key);
371 pr_err("Can't register type: %d\n", ret);
378 crypto_free_aead(big_key_aead);
382 late_initcall(big_key_init);