1b4d0d230SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 2ab3c3587SDavid Howells /* Large capacity key type 3ab3c3587SDavid Howells * 4521fd61cSJason A. Donenfeld * Copyright (C) 2017-2020 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved. 5ab3c3587SDavid Howells * Copyright (C) 2013 Red Hat, Inc. All Rights Reserved. 6ab3c3587SDavid Howells * Written by David Howells (dhowells@redhat.com) 7ab3c3587SDavid Howells */ 8ab3c3587SDavid Howells 97df3e59cSDavid Howells #define pr_fmt(fmt) "big_key: "fmt 10ab3c3587SDavid Howells #include <linux/init.h> 11ab3c3587SDavid Howells #include <linux/seq_file.h> 12ab3c3587SDavid Howells #include <linux/file.h> 13ab3c3587SDavid Howells #include <linux/shmem_fs.h> 14ab3c3587SDavid Howells #include <linux/err.h> 15428490e3SJason A. Donenfeld #include <linux/random.h> 16ab3c3587SDavid Howells #include <keys/user-type.h> 17ab3c3587SDavid Howells #include <keys/big_key-type.h> 18521fd61cSJason A. Donenfeld #include <crypto/chacha20poly1305.h> 19d9f4bb1aSDavid Howells 20ab3c3587SDavid Howells /* 21146aa8b1SDavid Howells * Layout of key payload words. 22146aa8b1SDavid Howells */ 23146aa8b1SDavid Howells enum { 24146aa8b1SDavid Howells big_key_data, 25146aa8b1SDavid Howells big_key_path, 26146aa8b1SDavid Howells big_key_path_2nd_part, 27146aa8b1SDavid Howells big_key_len, 28146aa8b1SDavid Howells }; 29146aa8b1SDavid Howells 30146aa8b1SDavid Howells /* 31ab3c3587SDavid Howells * If the data is under this limit, there's no point creating a shm file to 32ab3c3587SDavid Howells * hold it as the permanently resident metadata for the shmem fs will be at 33ab3c3587SDavid Howells * least as large as the data. 34ab3c3587SDavid Howells */ 35ab3c3587SDavid Howells #define BIG_KEY_FILE_THRESHOLD (sizeof(struct inode) + sizeof(struct dentry)) 36ab3c3587SDavid Howells 37ab3c3587SDavid Howells /* 38ab3c3587SDavid Howells * big_key defined keys take an arbitrary string as the description and an 39ab3c3587SDavid Howells * arbitrary blob of data as the payload 40ab3c3587SDavid Howells */ 41ab3c3587SDavid Howells struct key_type key_type_big_key = { 42ab3c3587SDavid Howells .name = "big_key", 43002edaf7SDavid Howells .preparse = big_key_preparse, 44002edaf7SDavid Howells .free_preparse = big_key_free_preparse, 45002edaf7SDavid Howells .instantiate = generic_key_instantiate, 46ab3c3587SDavid Howells .revoke = big_key_revoke, 47ab3c3587SDavid Howells .destroy = big_key_destroy, 48ab3c3587SDavid Howells .describe = big_key_describe, 49ab3c3587SDavid Howells .read = big_key_read, 50b6f61c31SDavid Howells .update = big_key_update, 51ab3c3587SDavid Howells }; 52ab3c3587SDavid Howells 53ab3c3587SDavid Howells /* 54002edaf7SDavid Howells * Preparse a big key 55ab3c3587SDavid Howells */ 56002edaf7SDavid Howells int big_key_preparse(struct key_preparsed_payload *prep) 57ab3c3587SDavid Howells { 58146aa8b1SDavid Howells struct path *path = (struct path *)&prep->payload.data[big_key_path]; 59ab3c3587SDavid Howells struct file *file; 60521fd61cSJason A. Donenfeld u8 *buf, *enckey; 61ab3c3587SDavid Howells ssize_t written; 62521fd61cSJason A. Donenfeld size_t datalen = prep->datalen; 63521fd61cSJason A. Donenfeld size_t enclen = datalen + CHACHA20POLY1305_AUTHTAG_SIZE; 64ab3c3587SDavid Howells int ret; 65ab3c3587SDavid Howells 66ab3c3587SDavid Howells if (datalen <= 0 || datalen > 1024 * 1024 || !prep->data) 67d9f4bb1aSDavid Howells return -EINVAL; 68ab3c3587SDavid Howells 69ab3c3587SDavid Howells /* Set an arbitrary quota */ 70002edaf7SDavid Howells prep->quotalen = 16; 71ab3c3587SDavid Howells 72146aa8b1SDavid Howells prep->payload.data[big_key_len] = (void *)(unsigned long)datalen; 73ab3c3587SDavid Howells 74ab3c3587SDavid Howells if (datalen > BIG_KEY_FILE_THRESHOLD) { 75ab3c3587SDavid Howells /* Create a shmem file to store the data in. This will permit the data 76ab3c3587SDavid Howells * to be swapped out if needed. 77ab3c3587SDavid Howells * 7813100a72SKirill Marinushkin * File content is stored encrypted with randomly generated key. 79521fd61cSJason A. Donenfeld * Since the key is random for each file, we can set the nonce 80521fd61cSJason A. Donenfeld * to zero, provided we never define a ->update() call. 81ab3c3587SDavid Howells */ 82e13ec939SChristoph Hellwig loff_t pos = 0; 8313100a72SKirill Marinushkin 84521fd61cSJason A. Donenfeld buf = kvmalloc(enclen, GFP_KERNEL); 85d9f4bb1aSDavid Howells if (!buf) 8613100a72SKirill Marinushkin return -ENOMEM; 8713100a72SKirill Marinushkin 8813100a72SKirill Marinushkin /* generate random key */ 89521fd61cSJason A. Donenfeld enckey = kmalloc(CHACHA20POLY1305_KEY_SIZE, GFP_KERNEL); 9013100a72SKirill Marinushkin if (!enckey) { 9113100a72SKirill Marinushkin ret = -ENOMEM; 92002edaf7SDavid Howells goto error; 93d2b86970SWei Yongjun } 94521fd61cSJason A. Donenfeld ret = get_random_bytes_wait(enckey, CHACHA20POLY1305_KEY_SIZE); 95428490e3SJason A. Donenfeld if (unlikely(ret)) 9613100a72SKirill Marinushkin goto err_enckey; 9713100a72SKirill Marinushkin 98521fd61cSJason A. Donenfeld /* encrypt data */ 99521fd61cSJason A. Donenfeld chacha20poly1305_encrypt(buf, prep->data, datalen, NULL, 0, 100521fd61cSJason A. Donenfeld 0, enckey); 10113100a72SKirill Marinushkin 10213100a72SKirill Marinushkin /* save aligned data to file */ 10313100a72SKirill Marinushkin file = shmem_kernel_file_setup("", enclen, 0); 10413100a72SKirill Marinushkin if (IS_ERR(file)) { 10513100a72SKirill Marinushkin ret = PTR_ERR(file); 10613100a72SKirill Marinushkin goto err_enckey; 10713100a72SKirill Marinushkin } 10813100a72SKirill Marinushkin 109521fd61cSJason A. Donenfeld written = kernel_write(file, buf, enclen, &pos); 11013100a72SKirill Marinushkin if (written != enclen) { 11197826c82SDavid Howells ret = written; 112ab3c3587SDavid Howells if (written >= 0) 113521fd61cSJason A. Donenfeld ret = -EIO; 114ab3c3587SDavid Howells goto err_fput; 115ab3c3587SDavid Howells } 116ab3c3587SDavid Howells 117ab3c3587SDavid Howells /* Pin the mount and dentry to the key so that we can open it again 118ab3c3587SDavid Howells * later 119ab3c3587SDavid Howells */ 12013100a72SKirill Marinushkin prep->payload.data[big_key_data] = enckey; 121ab3c3587SDavid Howells *path = file->f_path; 122ab3c3587SDavid Howells path_get(path); 123ab3c3587SDavid Howells fput(file); 124*272a1219SDenis Efremov kvfree_sensitive(buf, enclen); 125ab3c3587SDavid Howells } else { 126ab3c3587SDavid Howells /* Just store the data in a buffer */ 127ab3c3587SDavid Howells void *data = kmalloc(datalen, GFP_KERNEL); 12813100a72SKirill Marinushkin 129002edaf7SDavid Howells if (!data) 130002edaf7SDavid Howells return -ENOMEM; 131ab3c3587SDavid Howells 132146aa8b1SDavid Howells prep->payload.data[big_key_data] = data; 133146aa8b1SDavid Howells memcpy(data, prep->data, prep->datalen); 134ab3c3587SDavid Howells } 135ab3c3587SDavid Howells return 0; 136ab3c3587SDavid Howells 137ab3c3587SDavid Howells err_fput: 138ab3c3587SDavid Howells fput(file); 13913100a72SKirill Marinushkin err_enckey: 140453431a5SWaiman Long kfree_sensitive(enckey); 141ab3c3587SDavid Howells error: 142*272a1219SDenis Efremov kvfree_sensitive(buf, enclen); 143ab3c3587SDavid Howells return ret; 144ab3c3587SDavid Howells } 145ab3c3587SDavid Howells 146ab3c3587SDavid Howells /* 147002edaf7SDavid Howells * Clear preparsement. 148002edaf7SDavid Howells */ 149002edaf7SDavid Howells void big_key_free_preparse(struct key_preparsed_payload *prep) 150002edaf7SDavid Howells { 151002edaf7SDavid Howells if (prep->datalen > BIG_KEY_FILE_THRESHOLD) { 152146aa8b1SDavid Howells struct path *path = (struct path *)&prep->payload.data[big_key_path]; 15313100a72SKirill Marinushkin 154002edaf7SDavid Howells path_put(path); 155002edaf7SDavid Howells } 156453431a5SWaiman Long kfree_sensitive(prep->payload.data[big_key_data]); 157002edaf7SDavid Howells } 158002edaf7SDavid Howells 159002edaf7SDavid Howells /* 160ab3c3587SDavid Howells * dispose of the links from a revoked keyring 161ab3c3587SDavid Howells * - called with the key sem write-locked 162ab3c3587SDavid Howells */ 163ab3c3587SDavid Howells void big_key_revoke(struct key *key) 164ab3c3587SDavid Howells { 165146aa8b1SDavid Howells struct path *path = (struct path *)&key->payload.data[big_key_path]; 166ab3c3587SDavid Howells 167ab3c3587SDavid Howells /* clear the quota */ 168ab3c3587SDavid Howells key_payload_reserve(key, 0); 169363b02daSDavid Howells if (key_is_positive(key) && 170146aa8b1SDavid Howells (size_t)key->payload.data[big_key_len] > BIG_KEY_FILE_THRESHOLD) 171ab3c3587SDavid Howells vfs_truncate(path, 0); 172ab3c3587SDavid Howells } 173ab3c3587SDavid Howells 174ab3c3587SDavid Howells /* 175ab3c3587SDavid Howells * dispose of the data dangling from the corpse of a big_key key 176ab3c3587SDavid Howells */ 177ab3c3587SDavid Howells void big_key_destroy(struct key *key) 178ab3c3587SDavid Howells { 179146aa8b1SDavid Howells size_t datalen = (size_t)key->payload.data[big_key_len]; 180146aa8b1SDavid Howells 18113100a72SKirill Marinushkin if (datalen > BIG_KEY_FILE_THRESHOLD) { 182146aa8b1SDavid Howells struct path *path = (struct path *)&key->payload.data[big_key_path]; 18313100a72SKirill Marinushkin 184ab3c3587SDavid Howells path_put(path); 185ab3c3587SDavid Howells path->mnt = NULL; 186ab3c3587SDavid Howells path->dentry = NULL; 18713100a72SKirill Marinushkin } 188453431a5SWaiman Long kfree_sensitive(key->payload.data[big_key_data]); 189146aa8b1SDavid Howells key->payload.data[big_key_data] = NULL; 190ab3c3587SDavid Howells } 191ab3c3587SDavid Howells 192ab3c3587SDavid Howells /* 193b6f61c31SDavid Howells * Update a big key 194b6f61c31SDavid Howells */ 195b6f61c31SDavid Howells int big_key_update(struct key *key, struct key_preparsed_payload *prep) 196b6f61c31SDavid Howells { 197b6f61c31SDavid Howells int ret; 198b6f61c31SDavid Howells 199b6f61c31SDavid Howells ret = key_payload_reserve(key, prep->datalen); 200b6f61c31SDavid Howells if (ret < 0) 201b6f61c31SDavid Howells return ret; 202b6f61c31SDavid Howells 203b6f61c31SDavid Howells if (key_is_positive(key)) 204b6f61c31SDavid Howells big_key_destroy(key); 205b6f61c31SDavid Howells 206b6f61c31SDavid Howells return generic_key_instantiate(key, prep); 207b6f61c31SDavid Howells } 208b6f61c31SDavid Howells 209b6f61c31SDavid Howells /* 210ab3c3587SDavid Howells * describe the big_key key 211ab3c3587SDavid Howells */ 212ab3c3587SDavid Howells void big_key_describe(const struct key *key, struct seq_file *m) 213ab3c3587SDavid Howells { 214146aa8b1SDavid Howells size_t datalen = (size_t)key->payload.data[big_key_len]; 215ab3c3587SDavid Howells 216ab3c3587SDavid Howells seq_puts(m, key->description); 217ab3c3587SDavid Howells 218363b02daSDavid Howells if (key_is_positive(key)) 219146aa8b1SDavid Howells seq_printf(m, ": %zu [%s]", 220ab3c3587SDavid Howells datalen, 221ab3c3587SDavid Howells datalen > BIG_KEY_FILE_THRESHOLD ? "file" : "buff"); 222ab3c3587SDavid Howells } 223ab3c3587SDavid Howells 224ab3c3587SDavid Howells /* 225ab3c3587SDavid Howells * read the key data 226ab3c3587SDavid Howells * - the key's semaphore is read-locked 227ab3c3587SDavid Howells */ 228d3ec10aaSWaiman Long long big_key_read(const struct key *key, char *buffer, size_t buflen) 229ab3c3587SDavid Howells { 230146aa8b1SDavid Howells size_t datalen = (size_t)key->payload.data[big_key_len]; 231ab3c3587SDavid Howells long ret; 232ab3c3587SDavid Howells 233ab3c3587SDavid Howells if (!buffer || buflen < datalen) 234ab3c3587SDavid Howells return datalen; 235ab3c3587SDavid Howells 236ab3c3587SDavid Howells if (datalen > BIG_KEY_FILE_THRESHOLD) { 237146aa8b1SDavid Howells struct path *path = (struct path *)&key->payload.data[big_key_path]; 238ab3c3587SDavid Howells struct file *file; 239521fd61cSJason A. Donenfeld u8 *buf, *enckey = (u8 *)key->payload.data[big_key_data]; 240521fd61cSJason A. Donenfeld size_t enclen = datalen + CHACHA20POLY1305_AUTHTAG_SIZE; 241bdd1d2d3SChristoph Hellwig loff_t pos = 0; 24213100a72SKirill Marinushkin 243521fd61cSJason A. Donenfeld buf = kvmalloc(enclen, GFP_KERNEL); 244d9f4bb1aSDavid Howells if (!buf) 24513100a72SKirill Marinushkin return -ENOMEM; 246ab3c3587SDavid Howells 247ab3c3587SDavid Howells file = dentry_open(path, O_RDONLY, current_cred()); 24813100a72SKirill Marinushkin if (IS_ERR(file)) { 24913100a72SKirill Marinushkin ret = PTR_ERR(file); 25013100a72SKirill Marinushkin goto error; 25113100a72SKirill Marinushkin } 252ab3c3587SDavid Howells 25313100a72SKirill Marinushkin /* read file to kernel and decrypt */ 254521fd61cSJason A. Donenfeld ret = kernel_read(file, buf, enclen, &pos); 255521fd61cSJason A. Donenfeld if (ret != enclen) { 256521fd61cSJason A. Donenfeld if (ret >= 0) 257ab3c3587SDavid Howells ret = -EIO; 25813100a72SKirill Marinushkin goto err_fput; 25913100a72SKirill Marinushkin } 26013100a72SKirill Marinushkin 261521fd61cSJason A. Donenfeld ret = chacha20poly1305_decrypt(buf, buf, enclen, NULL, 0, 0, 262521fd61cSJason A. Donenfeld enckey) ? 0 : -EBADMSG; 263521fd61cSJason A. Donenfeld if (unlikely(ret)) 26413100a72SKirill Marinushkin goto err_fput; 26513100a72SKirill Marinushkin 26613100a72SKirill Marinushkin ret = datalen; 26713100a72SKirill Marinushkin 268d3ec10aaSWaiman Long /* copy out decrypted data */ 269521fd61cSJason A. Donenfeld memcpy(buffer, buf, datalen); 27013100a72SKirill Marinushkin 27113100a72SKirill Marinushkin err_fput: 27213100a72SKirill Marinushkin fput(file); 27313100a72SKirill Marinushkin error: 274*272a1219SDenis Efremov kvfree_sensitive(buf, enclen); 275ab3c3587SDavid Howells } else { 276ab3c3587SDavid Howells ret = datalen; 277d3ec10aaSWaiman Long memcpy(buffer, key->payload.data[big_key_data], datalen); 278ab3c3587SDavid Howells } 279ab3c3587SDavid Howells 280ab3c3587SDavid Howells return ret; 281ab3c3587SDavid Howells } 282ab3c3587SDavid Howells 28313100a72SKirill Marinushkin /* 28413100a72SKirill Marinushkin * Register key type 28513100a72SKirill Marinushkin */ 286ab3c3587SDavid Howells static int __init big_key_init(void) 287ab3c3587SDavid Howells { 288521fd61cSJason A. Donenfeld return register_key_type(&key_type_big_key); 28913100a72SKirill Marinushkin } 29013100a72SKirill Marinushkin 2917df3e59cSDavid Howells late_initcall(big_key_init); 292