1ab3c3587SDavid Howells /* Large capacity key type 2ab3c3587SDavid Howells * 3ab3c3587SDavid Howells * Copyright (C) 2013 Red Hat, Inc. All Rights Reserved. 4ab3c3587SDavid Howells * Written by David Howells (dhowells@redhat.com) 5ab3c3587SDavid Howells * 6ab3c3587SDavid Howells * This program is free software; you can redistribute it and/or 7ab3c3587SDavid Howells * modify it under the terms of the GNU General Public Licence 8ab3c3587SDavid Howells * as published by the Free Software Foundation; either version 9ab3c3587SDavid Howells * 2 of the Licence, or (at your option) any later version. 10ab3c3587SDavid Howells */ 11ab3c3587SDavid Howells 127df3e59cSDavid Howells #define pr_fmt(fmt) "big_key: "fmt 13ab3c3587SDavid Howells #include <linux/init.h> 14ab3c3587SDavid Howells #include <linux/seq_file.h> 15ab3c3587SDavid Howells #include <linux/file.h> 16ab3c3587SDavid Howells #include <linux/shmem_fs.h> 17ab3c3587SDavid Howells #include <linux/err.h> 1813100a72SKirill Marinushkin #include <linux/scatterlist.h> 19ab3c3587SDavid Howells #include <keys/user-type.h> 20ab3c3587SDavid Howells #include <keys/big_key-type.h> 2113100a72SKirill Marinushkin #include <crypto/rng.h> 22d56d72c6SHerbert Xu #include <crypto/skcipher.h> 23ab3c3587SDavid Howells 24ab3c3587SDavid Howells /* 25146aa8b1SDavid Howells * Layout of key payload words. 26146aa8b1SDavid Howells */ 27146aa8b1SDavid Howells enum { 28146aa8b1SDavid Howells big_key_data, 29146aa8b1SDavid Howells big_key_path, 30146aa8b1SDavid Howells big_key_path_2nd_part, 31146aa8b1SDavid Howells big_key_len, 32146aa8b1SDavid Howells }; 33146aa8b1SDavid Howells 34146aa8b1SDavid Howells /* 3513100a72SKirill Marinushkin * Crypto operation with big_key data 3613100a72SKirill Marinushkin */ 3713100a72SKirill Marinushkin enum big_key_op { 3813100a72SKirill Marinushkin BIG_KEY_ENC, 3913100a72SKirill Marinushkin BIG_KEY_DEC, 4013100a72SKirill Marinushkin }; 4113100a72SKirill Marinushkin 4213100a72SKirill Marinushkin /* 43ab3c3587SDavid Howells * If the data is under this limit, there's no point creating a shm file to 44ab3c3587SDavid Howells * hold it as the permanently resident metadata for the shmem fs will be at 45ab3c3587SDavid Howells * least as large as the data. 46ab3c3587SDavid Howells */ 47ab3c3587SDavid Howells #define BIG_KEY_FILE_THRESHOLD (sizeof(struct inode) + sizeof(struct dentry)) 48ab3c3587SDavid Howells 49ab3c3587SDavid Howells /* 5013100a72SKirill Marinushkin * Key size for big_key data encryption 5113100a72SKirill Marinushkin */ 5213100a72SKirill Marinushkin #define ENC_KEY_SIZE 16 5313100a72SKirill Marinushkin 5413100a72SKirill Marinushkin /* 55ab3c3587SDavid Howells * big_key defined keys take an arbitrary string as the description and an 56ab3c3587SDavid Howells * arbitrary blob of data as the payload 57ab3c3587SDavid Howells */ 58ab3c3587SDavid Howells struct key_type key_type_big_key = { 59ab3c3587SDavid Howells .name = "big_key", 60002edaf7SDavid Howells .preparse = big_key_preparse, 61002edaf7SDavid Howells .free_preparse = big_key_free_preparse, 62002edaf7SDavid Howells .instantiate = generic_key_instantiate, 63ab3c3587SDavid Howells .revoke = big_key_revoke, 64ab3c3587SDavid Howells .destroy = big_key_destroy, 65ab3c3587SDavid Howells .describe = big_key_describe, 66ab3c3587SDavid Howells .read = big_key_read, 67ab3c3587SDavid Howells }; 68ab3c3587SDavid Howells 69ab3c3587SDavid Howells /* 7013100a72SKirill Marinushkin * Crypto names for big_key data encryption 7113100a72SKirill Marinushkin */ 7213100a72SKirill Marinushkin static const char big_key_rng_name[] = "stdrng"; 7313100a72SKirill Marinushkin static const char big_key_alg_name[] = "ecb(aes)"; 7413100a72SKirill Marinushkin 7513100a72SKirill Marinushkin /* 7613100a72SKirill Marinushkin * Crypto algorithms for big_key data encryption 7713100a72SKirill Marinushkin */ 7813100a72SKirill Marinushkin static struct crypto_rng *big_key_rng; 79d56d72c6SHerbert Xu static struct crypto_skcipher *big_key_skcipher; 8013100a72SKirill Marinushkin 8113100a72SKirill Marinushkin /* 8213100a72SKirill Marinushkin * Generate random key to encrypt big_key data 8313100a72SKirill Marinushkin */ 8413100a72SKirill Marinushkin static inline int big_key_gen_enckey(u8 *key) 8513100a72SKirill Marinushkin { 8613100a72SKirill Marinushkin return crypto_rng_get_bytes(big_key_rng, key, ENC_KEY_SIZE); 8713100a72SKirill Marinushkin } 8813100a72SKirill Marinushkin 8913100a72SKirill Marinushkin /* 9013100a72SKirill Marinushkin * Encrypt/decrypt big_key data 9113100a72SKirill Marinushkin */ 9213100a72SKirill Marinushkin static int big_key_crypt(enum big_key_op op, u8 *data, size_t datalen, u8 *key) 9313100a72SKirill Marinushkin { 9413100a72SKirill Marinushkin int ret = -EINVAL; 9513100a72SKirill Marinushkin struct scatterlist sgio; 96d56d72c6SHerbert Xu SKCIPHER_REQUEST_ON_STACK(req, big_key_skcipher); 9713100a72SKirill Marinushkin 98d56d72c6SHerbert Xu if (crypto_skcipher_setkey(big_key_skcipher, key, ENC_KEY_SIZE)) { 9913100a72SKirill Marinushkin ret = -EAGAIN; 10013100a72SKirill Marinushkin goto error; 10113100a72SKirill Marinushkin } 10213100a72SKirill Marinushkin 103d56d72c6SHerbert Xu skcipher_request_set_tfm(req, big_key_skcipher); 104d56d72c6SHerbert Xu skcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_SLEEP, 105d56d72c6SHerbert Xu NULL, NULL); 10613100a72SKirill Marinushkin 10713100a72SKirill Marinushkin sg_init_one(&sgio, data, datalen); 108d56d72c6SHerbert Xu skcipher_request_set_crypt(req, &sgio, &sgio, datalen, NULL); 10913100a72SKirill Marinushkin 11013100a72SKirill Marinushkin if (op == BIG_KEY_ENC) 111d56d72c6SHerbert Xu ret = crypto_skcipher_encrypt(req); 11213100a72SKirill Marinushkin else 113d56d72c6SHerbert Xu ret = crypto_skcipher_decrypt(req); 114d56d72c6SHerbert Xu 115d56d72c6SHerbert Xu skcipher_request_zero(req); 11613100a72SKirill Marinushkin 11713100a72SKirill Marinushkin error: 11813100a72SKirill Marinushkin return ret; 11913100a72SKirill Marinushkin } 12013100a72SKirill Marinushkin 12113100a72SKirill Marinushkin /* 122002edaf7SDavid Howells * Preparse a big key 123ab3c3587SDavid Howells */ 124002edaf7SDavid Howells int big_key_preparse(struct key_preparsed_payload *prep) 125ab3c3587SDavid Howells { 126146aa8b1SDavid Howells struct path *path = (struct path *)&prep->payload.data[big_key_path]; 127ab3c3587SDavid Howells struct file *file; 12813100a72SKirill Marinushkin u8 *enckey; 12913100a72SKirill Marinushkin u8 *data = NULL; 130ab3c3587SDavid Howells ssize_t written; 131ab3c3587SDavid Howells size_t datalen = prep->datalen; 132ab3c3587SDavid Howells int ret; 133ab3c3587SDavid Howells 134ab3c3587SDavid Howells ret = -EINVAL; 135ab3c3587SDavid Howells if (datalen <= 0 || datalen > 1024 * 1024 || !prep->data) 136ab3c3587SDavid Howells goto error; 137ab3c3587SDavid Howells 138ab3c3587SDavid Howells /* Set an arbitrary quota */ 139002edaf7SDavid Howells prep->quotalen = 16; 140ab3c3587SDavid Howells 141146aa8b1SDavid Howells prep->payload.data[big_key_len] = (void *)(unsigned long)datalen; 142ab3c3587SDavid Howells 143ab3c3587SDavid Howells if (datalen > BIG_KEY_FILE_THRESHOLD) { 144ab3c3587SDavid Howells /* Create a shmem file to store the data in. This will permit the data 145ab3c3587SDavid Howells * to be swapped out if needed. 146ab3c3587SDavid Howells * 14713100a72SKirill Marinushkin * File content is stored encrypted with randomly generated key. 148ab3c3587SDavid Howells */ 149d56d72c6SHerbert Xu size_t enclen = ALIGN(datalen, crypto_skcipher_blocksize(big_key_skcipher)); 150e13ec939SChristoph Hellwig loff_t pos = 0; 15113100a72SKirill Marinushkin 15213100a72SKirill Marinushkin /* prepare aligned data to encrypt */ 15313100a72SKirill Marinushkin data = kmalloc(enclen, GFP_KERNEL); 15413100a72SKirill Marinushkin if (!data) 15513100a72SKirill Marinushkin return -ENOMEM; 15613100a72SKirill Marinushkin 15713100a72SKirill Marinushkin memcpy(data, prep->data, datalen); 15813100a72SKirill Marinushkin memset(data + datalen, 0x00, enclen - datalen); 15913100a72SKirill Marinushkin 16013100a72SKirill Marinushkin /* generate random key */ 16113100a72SKirill Marinushkin enckey = kmalloc(ENC_KEY_SIZE, GFP_KERNEL); 16213100a72SKirill Marinushkin if (!enckey) { 16313100a72SKirill Marinushkin ret = -ENOMEM; 164002edaf7SDavid Howells goto error; 165d2b86970SWei Yongjun } 166ab3c3587SDavid Howells 16713100a72SKirill Marinushkin ret = big_key_gen_enckey(enckey); 16813100a72SKirill Marinushkin if (ret) 16913100a72SKirill Marinushkin goto err_enckey; 17013100a72SKirill Marinushkin 17113100a72SKirill Marinushkin /* encrypt aligned data */ 17213100a72SKirill Marinushkin ret = big_key_crypt(BIG_KEY_ENC, data, enclen, enckey); 17313100a72SKirill Marinushkin if (ret) 17413100a72SKirill Marinushkin goto err_enckey; 17513100a72SKirill Marinushkin 17613100a72SKirill Marinushkin /* save aligned data to file */ 17713100a72SKirill Marinushkin file = shmem_kernel_file_setup("", enclen, 0); 17813100a72SKirill Marinushkin if (IS_ERR(file)) { 17913100a72SKirill Marinushkin ret = PTR_ERR(file); 18013100a72SKirill Marinushkin goto err_enckey; 18113100a72SKirill Marinushkin } 18213100a72SKirill Marinushkin 183e13ec939SChristoph Hellwig written = kernel_write(file, data, enclen, &pos); 18413100a72SKirill Marinushkin if (written != enclen) { 18597826c82SDavid Howells ret = written; 186ab3c3587SDavid Howells if (written >= 0) 187ab3c3587SDavid Howells ret = -ENOMEM; 188ab3c3587SDavid Howells goto err_fput; 189ab3c3587SDavid Howells } 190ab3c3587SDavid Howells 191ab3c3587SDavid Howells /* Pin the mount and dentry to the key so that we can open it again 192ab3c3587SDavid Howells * later 193ab3c3587SDavid Howells */ 19413100a72SKirill Marinushkin prep->payload.data[big_key_data] = enckey; 195ab3c3587SDavid Howells *path = file->f_path; 196ab3c3587SDavid Howells path_get(path); 197ab3c3587SDavid Howells fput(file); 198*91080180SJason A. Donenfeld kzfree(data); 199ab3c3587SDavid Howells } else { 200ab3c3587SDavid Howells /* Just store the data in a buffer */ 201ab3c3587SDavid Howells void *data = kmalloc(datalen, GFP_KERNEL); 20213100a72SKirill Marinushkin 203002edaf7SDavid Howells if (!data) 204002edaf7SDavid Howells return -ENOMEM; 205ab3c3587SDavid Howells 206146aa8b1SDavid Howells prep->payload.data[big_key_data] = data; 207146aa8b1SDavid Howells memcpy(data, prep->data, prep->datalen); 208ab3c3587SDavid Howells } 209ab3c3587SDavid Howells return 0; 210ab3c3587SDavid Howells 211ab3c3587SDavid Howells err_fput: 212ab3c3587SDavid Howells fput(file); 21313100a72SKirill Marinushkin err_enckey: 214*91080180SJason A. Donenfeld kzfree(enckey); 215ab3c3587SDavid Howells error: 216*91080180SJason A. Donenfeld kzfree(data); 217ab3c3587SDavid Howells return ret; 218ab3c3587SDavid Howells } 219ab3c3587SDavid Howells 220ab3c3587SDavid Howells /* 221002edaf7SDavid Howells * Clear preparsement. 222002edaf7SDavid Howells */ 223002edaf7SDavid Howells void big_key_free_preparse(struct key_preparsed_payload *prep) 224002edaf7SDavid Howells { 225002edaf7SDavid Howells if (prep->datalen > BIG_KEY_FILE_THRESHOLD) { 226146aa8b1SDavid Howells struct path *path = (struct path *)&prep->payload.data[big_key_path]; 22713100a72SKirill Marinushkin 228002edaf7SDavid Howells path_put(path); 229002edaf7SDavid Howells } 230*91080180SJason A. Donenfeld kzfree(prep->payload.data[big_key_data]); 231002edaf7SDavid Howells } 232002edaf7SDavid Howells 233002edaf7SDavid Howells /* 234ab3c3587SDavid Howells * dispose of the links from a revoked keyring 235ab3c3587SDavid Howells * - called with the key sem write-locked 236ab3c3587SDavid Howells */ 237ab3c3587SDavid Howells void big_key_revoke(struct key *key) 238ab3c3587SDavid Howells { 239146aa8b1SDavid Howells struct path *path = (struct path *)&key->payload.data[big_key_path]; 240ab3c3587SDavid Howells 241ab3c3587SDavid Howells /* clear the quota */ 242ab3c3587SDavid Howells key_payload_reserve(key, 0); 243146aa8b1SDavid Howells if (key_is_instantiated(key) && 244146aa8b1SDavid Howells (size_t)key->payload.data[big_key_len] > BIG_KEY_FILE_THRESHOLD) 245ab3c3587SDavid Howells vfs_truncate(path, 0); 246ab3c3587SDavid Howells } 247ab3c3587SDavid Howells 248ab3c3587SDavid Howells /* 249ab3c3587SDavid Howells * dispose of the data dangling from the corpse of a big_key key 250ab3c3587SDavid Howells */ 251ab3c3587SDavid Howells void big_key_destroy(struct key *key) 252ab3c3587SDavid Howells { 253146aa8b1SDavid Howells size_t datalen = (size_t)key->payload.data[big_key_len]; 254146aa8b1SDavid Howells 25513100a72SKirill Marinushkin if (datalen > BIG_KEY_FILE_THRESHOLD) { 256146aa8b1SDavid Howells struct path *path = (struct path *)&key->payload.data[big_key_path]; 25713100a72SKirill Marinushkin 258ab3c3587SDavid Howells path_put(path); 259ab3c3587SDavid Howells path->mnt = NULL; 260ab3c3587SDavid Howells path->dentry = NULL; 26113100a72SKirill Marinushkin } 262*91080180SJason A. Donenfeld kzfree(key->payload.data[big_key_data]); 263146aa8b1SDavid Howells key->payload.data[big_key_data] = NULL; 264ab3c3587SDavid Howells } 265ab3c3587SDavid Howells 266ab3c3587SDavid Howells /* 267ab3c3587SDavid Howells * describe the big_key key 268ab3c3587SDavid Howells */ 269ab3c3587SDavid Howells void big_key_describe(const struct key *key, struct seq_file *m) 270ab3c3587SDavid Howells { 271146aa8b1SDavid Howells size_t datalen = (size_t)key->payload.data[big_key_len]; 272ab3c3587SDavid Howells 273ab3c3587SDavid Howells seq_puts(m, key->description); 274ab3c3587SDavid Howells 275ab3c3587SDavid Howells if (key_is_instantiated(key)) 276146aa8b1SDavid Howells seq_printf(m, ": %zu [%s]", 277ab3c3587SDavid Howells datalen, 278ab3c3587SDavid Howells datalen > BIG_KEY_FILE_THRESHOLD ? "file" : "buff"); 279ab3c3587SDavid Howells } 280ab3c3587SDavid Howells 281ab3c3587SDavid Howells /* 282ab3c3587SDavid Howells * read the key data 283ab3c3587SDavid Howells * - the key's semaphore is read-locked 284ab3c3587SDavid Howells */ 285ab3c3587SDavid Howells long big_key_read(const struct key *key, char __user *buffer, size_t buflen) 286ab3c3587SDavid Howells { 287146aa8b1SDavid Howells size_t datalen = (size_t)key->payload.data[big_key_len]; 288ab3c3587SDavid Howells long ret; 289ab3c3587SDavid Howells 290ab3c3587SDavid Howells if (!buffer || buflen < datalen) 291ab3c3587SDavid Howells return datalen; 292ab3c3587SDavid Howells 293ab3c3587SDavid Howells if (datalen > BIG_KEY_FILE_THRESHOLD) { 294146aa8b1SDavid Howells struct path *path = (struct path *)&key->payload.data[big_key_path]; 295ab3c3587SDavid Howells struct file *file; 29613100a72SKirill Marinushkin u8 *data; 29713100a72SKirill Marinushkin u8 *enckey = (u8 *)key->payload.data[big_key_data]; 298d56d72c6SHerbert Xu size_t enclen = ALIGN(datalen, crypto_skcipher_blocksize(big_key_skcipher)); 299bdd1d2d3SChristoph Hellwig loff_t pos = 0; 30013100a72SKirill Marinushkin 30113100a72SKirill Marinushkin data = kmalloc(enclen, GFP_KERNEL); 30213100a72SKirill Marinushkin if (!data) 30313100a72SKirill Marinushkin return -ENOMEM; 304ab3c3587SDavid Howells 305ab3c3587SDavid Howells file = dentry_open(path, O_RDONLY, current_cred()); 30613100a72SKirill Marinushkin if (IS_ERR(file)) { 30713100a72SKirill Marinushkin ret = PTR_ERR(file); 30813100a72SKirill Marinushkin goto error; 30913100a72SKirill Marinushkin } 310ab3c3587SDavid Howells 31113100a72SKirill Marinushkin /* read file to kernel and decrypt */ 312bdd1d2d3SChristoph Hellwig ret = kernel_read(file, data, enclen, &pos); 31313100a72SKirill Marinushkin if (ret >= 0 && ret != enclen) { 314ab3c3587SDavid Howells ret = -EIO; 31513100a72SKirill Marinushkin goto err_fput; 31613100a72SKirill Marinushkin } 31713100a72SKirill Marinushkin 31813100a72SKirill Marinushkin ret = big_key_crypt(BIG_KEY_DEC, data, enclen, enckey); 31913100a72SKirill Marinushkin if (ret) 32013100a72SKirill Marinushkin goto err_fput; 32113100a72SKirill Marinushkin 32213100a72SKirill Marinushkin ret = datalen; 32313100a72SKirill Marinushkin 32413100a72SKirill Marinushkin /* copy decrypted data to user */ 32513100a72SKirill Marinushkin if (copy_to_user(buffer, data, datalen) != 0) 32613100a72SKirill Marinushkin ret = -EFAULT; 32713100a72SKirill Marinushkin 32813100a72SKirill Marinushkin err_fput: 32913100a72SKirill Marinushkin fput(file); 33013100a72SKirill Marinushkin error: 331*91080180SJason A. Donenfeld kzfree(data); 332ab3c3587SDavid Howells } else { 333ab3c3587SDavid Howells ret = datalen; 334146aa8b1SDavid Howells if (copy_to_user(buffer, key->payload.data[big_key_data], 335146aa8b1SDavid Howells datalen) != 0) 336ab3c3587SDavid Howells ret = -EFAULT; 337ab3c3587SDavid Howells } 338ab3c3587SDavid Howells 339ab3c3587SDavid Howells return ret; 340ab3c3587SDavid Howells } 341ab3c3587SDavid Howells 34213100a72SKirill Marinushkin /* 34313100a72SKirill Marinushkin * Register key type 34413100a72SKirill Marinushkin */ 345ab3c3587SDavid Howells static int __init big_key_init(void) 346ab3c3587SDavid Howells { 3477df3e59cSDavid Howells struct crypto_skcipher *cipher; 3487df3e59cSDavid Howells struct crypto_rng *rng; 3497df3e59cSDavid Howells int ret; 3507df3e59cSDavid Howells 3517df3e59cSDavid Howells rng = crypto_alloc_rng(big_key_rng_name, 0, 0); 3527df3e59cSDavid Howells if (IS_ERR(rng)) { 3537df3e59cSDavid Howells pr_err("Can't alloc rng: %ld\n", PTR_ERR(rng)); 3547df3e59cSDavid Howells return PTR_ERR(rng); 355ab3c3587SDavid Howells } 35613100a72SKirill Marinushkin 3577df3e59cSDavid Howells big_key_rng = rng; 35813100a72SKirill Marinushkin 35913100a72SKirill Marinushkin /* seed RNG */ 3607df3e59cSDavid Howells ret = crypto_rng_reset(rng, NULL, crypto_rng_seedsize(rng)); 3617df3e59cSDavid Howells if (ret) { 3627df3e59cSDavid Howells pr_err("Can't reset rng: %d\n", ret); 3637df3e59cSDavid Howells goto error_rng; 3647df3e59cSDavid Howells } 36513100a72SKirill Marinushkin 36613100a72SKirill Marinushkin /* init block cipher */ 3677df3e59cSDavid Howells cipher = crypto_alloc_skcipher(big_key_alg_name, 0, CRYPTO_ALG_ASYNC); 3687df3e59cSDavid Howells if (IS_ERR(cipher)) { 3697df3e59cSDavid Howells ret = PTR_ERR(cipher); 3707df3e59cSDavid Howells pr_err("Can't alloc crypto: %d\n", ret); 3717df3e59cSDavid Howells goto error_rng; 3727df3e59cSDavid Howells } 3737df3e59cSDavid Howells 3747df3e59cSDavid Howells big_key_skcipher = cipher; 3757df3e59cSDavid Howells 3767df3e59cSDavid Howells ret = register_key_type(&key_type_big_key); 3777df3e59cSDavid Howells if (ret < 0) { 3787df3e59cSDavid Howells pr_err("Can't register type: %d\n", ret); 3797df3e59cSDavid Howells goto error_cipher; 38013100a72SKirill Marinushkin } 38113100a72SKirill Marinushkin 38213100a72SKirill Marinushkin return 0; 38313100a72SKirill Marinushkin 3847df3e59cSDavid Howells error_cipher: 3857df3e59cSDavid Howells crypto_free_skcipher(big_key_skcipher); 3867df3e59cSDavid Howells error_rng: 38713100a72SKirill Marinushkin crypto_free_rng(big_key_rng); 38813100a72SKirill Marinushkin return ret; 38913100a72SKirill Marinushkin } 39013100a72SKirill Marinushkin 3917df3e59cSDavid Howells late_initcall(big_key_init); 392