1a59d1b8e05ea (Thomas Gleixner 2019-05-27 08:55:05 +0200 1) // SPDX-License-Identifier: GPL-2.0-or-later
5da877eadffb8 (Lee Jones 2021-03-30 17:44:55 +0100 2) /*
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 3) * eCryptfs: Linux filesystem encryption layer
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 4) *
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 5) * Copyright (C) 1997-2004 Erez Zadok
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 6) * Copyright (C) 2001-2004 Stony Brook University
dd2a3b7ad98f8 (Michael Halcrow 2007-02-12 00:53:46 -0800 7) * Copyright (C) 2004-2007 International Business Machines Corp.
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 8) * Author(s): Michael A. Halcrow <mahalcro@us.ibm.com>
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 9) * Michael C. Thompson <mcthomps@us.ibm.com>
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 10) */
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 11)
3095e8e366b47 (Herbert Xu 2016-01-25 10:29:33 +0800 12) #include <crypto/hash.h>
3095e8e366b47 (Herbert Xu 2016-01-25 10:29:33 +0800 13) #include <crypto/skcipher.h>
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 14) #include <linux/fs.h>
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 15) #include <linux/mount.h>
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 16) #include <linux/pagemap.h>
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 17) #include <linux/random.h>
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 18) #include <linux/compiler.h>
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 19) #include <linux/key.h>
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 20) #include <linux/namei.h>
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 21) #include <linux/file.h>
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 22) #include <linux/scatterlist.h>
5a0e3ad6af866 (Tejun Heo 2010-03-24 17:04:11 +0900 23) #include <linux/slab.h>
29335c6a41568 (Harvey Harrison 2008-07-23 21:30:06 -0700 24) #include <asm/unaligned.h>
02f9876ebb5e9 (Jérémy Lefaure 2017-10-01 15:30:46 -0400 25) #include <linux/kernel.h>
d43388dea04b1 (Robbie Ko 2018-08-21 16:17:40 +0800 26) #include <linux/xattr.h>
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 27) #include "ecryptfs_kernel.h"
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 28)
00a699400a707 (Tyler Hicks 2013-04-05 23:26:22 -0700 29) #define DECRYPT 0
00a699400a707 (Tyler Hicks 2013-04-05 23:26:22 -0700 30) #define ENCRYPT 1
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 31)
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 32) /**
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 33) * ecryptfs_from_hex
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 34) * @dst: Buffer to take the bytes from src hex; must be at least of
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 35) * size (src_size / 2)
5f9f2c2abd16f (Wei Yuan 2016-02-17 14:50:10 +0800 36) * @src: Buffer to be converted from a hex string representation to raw value
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 37) * @dst_size: size of dst buffer, or number of hex characters pairs to convert
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 38) */
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 39) void ecryptfs_from_hex(char *dst, char *src, int dst_size)
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 40) {
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 41) int x;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 42) char tmp[3] = { 0, };
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 43)
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 44) for (x = 0; x < dst_size; x++) {
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 45) tmp[0] = src[x * 2];
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 46) tmp[1] = src[x * 2 + 1];
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 47) dst[x] = (unsigned char)simple_strtol(tmp, NULL, 16);
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 48) }
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 49) }
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 50)
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 51) /**
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 52) * ecryptfs_calculate_md5 - calculates the md5 of @src
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 53) * @dst: Pointer to 16 bytes of allocated memory
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 54) * @crypt_stat: Pointer to crypt_stat struct for the current inode
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 55) * @src: Data to be md5'd
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 56) * @len: Length of @src
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 57) *
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 58) * Uses the allocated crypto context that crypt_stat references to
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 59) * generate the MD5 sum of the contents of src.
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 60) */
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 61) static int ecryptfs_calculate_md5(char *dst,
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 62) struct ecryptfs_crypt_stat *crypt_stat,
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 63) char *src, int len)
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 64) {
1979811388050 (Eric Biggers 2020-05-01 22:31:16 -0700 65) int rc = crypto_shash_tfm_digest(crypt_stat->hash_tfm, src, len, dst);
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 66)
8a29f2b0288ba (Michael Halcrow 2007-11-05 14:51:04 -0800 67) if (rc) {
8a29f2b0288ba (Michael Halcrow 2007-11-05 14:51:04 -0800 68) printk(KERN_ERR
3095e8e366b47 (Herbert Xu 2016-01-25 10:29:33 +0800 69) "%s: Error computing crypto hash; rc = [%d]\n",
18d1dbf1d401e (Harvey Harrison 2008-04-29 00:59:48 -0700 70) __func__, rc);
8a29f2b0288ba (Michael Halcrow 2007-11-05 14:51:04 -0800 71) goto out;
8a29f2b0288ba (Michael Halcrow 2007-11-05 14:51:04 -0800 72) }
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 73) out:
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 74) return rc;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 75) }
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 76)
cd9d67dfd2b4d (Michael Halcrow 2007-10-16 01:28:04 -0700 77) static int ecryptfs_crypto_api_algify_cipher_name(char **algified_name,
cd9d67dfd2b4d (Michael Halcrow 2007-10-16 01:28:04 -0700 78) char *cipher_name,
cd9d67dfd2b4d (Michael Halcrow 2007-10-16 01:28:04 -0700 79) char *chaining_modifier)
8bba066f4e385 (Michael Halcrow 2006-10-30 22:07:18 -0800 80) {
8bba066f4e385 (Michael Halcrow 2006-10-30 22:07:18 -0800 81) int cipher_name_len = strlen(cipher_name);
8bba066f4e385 (Michael Halcrow 2006-10-30 22:07:18 -0800 82) int chaining_modifier_len = strlen(chaining_modifier);
8bba066f4e385 (Michael Halcrow 2006-10-30 22:07:18 -0800 83) int algified_name_len;
8bba066f4e385 (Michael Halcrow 2006-10-30 22:07:18 -0800 84) int rc;
8bba066f4e385 (Michael Halcrow 2006-10-30 22:07:18 -0800 85)
8bba066f4e385 (Michael Halcrow 2006-10-30 22:07:18 -0800 86) algified_name_len = (chaining_modifier_len + cipher_name_len + 3);
8bba066f4e385 (Michael Halcrow 2006-10-30 22:07:18 -0800 87) (*algified_name) = kmalloc(algified_name_len, GFP_KERNEL);
7bd473fcc217a (Michael Halcrow 2006-11-02 22:06:56 -0800 88) if (!(*algified_name)) {
8bba066f4e385 (Michael Halcrow 2006-10-30 22:07:18 -0800 89) rc = -ENOMEM;
8bba066f4e385 (Michael Halcrow 2006-10-30 22:07:18 -0800 90) goto out;
8bba066f4e385 (Michael Halcrow 2006-10-30 22:07:18 -0800 91) }
8bba066f4e385 (Michael Halcrow 2006-10-30 22:07:18 -0800 92) snprintf((*algified_name), algified_name_len, "%s(%s)",
8bba066f4e385 (Michael Halcrow 2006-10-30 22:07:18 -0800 93) chaining_modifier, cipher_name);
8bba066f4e385 (Michael Halcrow 2006-10-30 22:07:18 -0800 94) rc = 0;
8bba066f4e385 (Michael Halcrow 2006-10-30 22:07:18 -0800 95) out:
8bba066f4e385 (Michael Halcrow 2006-10-30 22:07:18 -0800 96) return rc;
8bba066f4e385 (Michael Halcrow 2006-10-30 22:07:18 -0800 97) }
8bba066f4e385 (Michael Halcrow 2006-10-30 22:07:18 -0800 98)
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 99) /**
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 100) * ecryptfs_derive_iv
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 101) * @iv: destination for the derived iv vale
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 102) * @crypt_stat: Pointer to crypt_stat struct for the current inode
d6a13c17164fc (Michael Halcrow 2007-10-16 01:28:12 -0700 103) * @offset: Offset of the extent whose IV we are to derive
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 104) *
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 105) * Generate the initialization vector from the given root IV and page
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 106) * offset.
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 107) *
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 108) * Returns zero on success; non-zero on error.
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 109) */
a34f60f748c6f (Michael Halcrow 2009-01-06 14:41:58 -0800 110) int ecryptfs_derive_iv(char *iv, struct ecryptfs_crypt_stat *crypt_stat,
a34f60f748c6f (Michael Halcrow 2009-01-06 14:41:58 -0800 111) loff_t offset)
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 112) {
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 113) int rc = 0;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 114) char dst[MD5_DIGEST_SIZE];
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 115) char src[ECRYPTFS_MAX_IV_BYTES + 16];
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 116)
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 117) if (unlikely(ecryptfs_verbosity > 0)) {
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 118) ecryptfs_printk(KERN_DEBUG, "root iv:\n");
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 119) ecryptfs_dump_hex(crypt_stat->root_iv, crypt_stat->iv_bytes);
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 120) }
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 121) /* TODO: It is probably secure to just cast the least
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 122) * significant bits of the root IV into an unsigned long and
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 123) * add the offset to that rather than go through all this
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 124) * hashing business. -Halcrow */
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 125) memcpy(src, crypt_stat->root_iv, crypt_stat->iv_bytes);
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 126) memset((src + crypt_stat->iv_bytes), 0, 16);
d6a13c17164fc (Michael Halcrow 2007-10-16 01:28:12 -0700 127) snprintf((src + crypt_stat->iv_bytes), 16, "%lld", offset);
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 128) if (unlikely(ecryptfs_verbosity > 0)) {
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 129) ecryptfs_printk(KERN_DEBUG, "source:\n");
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 130) ecryptfs_dump_hex(src, (crypt_stat->iv_bytes + 16));
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 131) }
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 132) rc = ecryptfs_calculate_md5(dst, crypt_stat, src,
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 133) (crypt_stat->iv_bytes + 16));
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 134) if (rc) {
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 135) ecryptfs_printk(KERN_WARNING, "Error attempting to compute "
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 136) "MD5 while generating IV for a page\n");
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 137) goto out;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 138) }
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 139) memcpy(iv, dst, crypt_stat->iv_bytes);
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 140) if (unlikely(ecryptfs_verbosity > 0)) {
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 141) ecryptfs_printk(KERN_DEBUG, "derived iv:\n");
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 142) ecryptfs_dump_hex(iv, crypt_stat->iv_bytes);
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 143) }
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 144) out:
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 145) return rc;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 146) }
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 147)
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 148) /**
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 149) * ecryptfs_init_crypt_stat
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 150) * @crypt_stat: Pointer to the crypt_stat struct to initialize.
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 151) *
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 152) * Initialize the crypt_stat structure.
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 153) */
e81f3340bba2b (Herbert Xu 2016-04-16 15:01:09 +0800 154) int ecryptfs_init_crypt_stat(struct ecryptfs_crypt_stat *crypt_stat)
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 155) {
e81f3340bba2b (Herbert Xu 2016-04-16 15:01:09 +0800 156) struct crypto_shash *tfm;
e81f3340bba2b (Herbert Xu 2016-04-16 15:01:09 +0800 157) int rc;
e81f3340bba2b (Herbert Xu 2016-04-16 15:01:09 +0800 158)
e81f3340bba2b (Herbert Xu 2016-04-16 15:01:09 +0800 159) tfm = crypto_alloc_shash(ECRYPTFS_DEFAULT_HASH, 0, 0);
e81f3340bba2b (Herbert Xu 2016-04-16 15:01:09 +0800 160) if (IS_ERR(tfm)) {
e81f3340bba2b (Herbert Xu 2016-04-16 15:01:09 +0800 161) rc = PTR_ERR(tfm);
e81f3340bba2b (Herbert Xu 2016-04-16 15:01:09 +0800 162) ecryptfs_printk(KERN_ERR, "Error attempting to "
e81f3340bba2b (Herbert Xu 2016-04-16 15:01:09 +0800 163) "allocate crypto context; rc = [%d]\n",
e81f3340bba2b (Herbert Xu 2016-04-16 15:01:09 +0800 164) rc);
e81f3340bba2b (Herbert Xu 2016-04-16 15:01:09 +0800 165) return rc;
e81f3340bba2b (Herbert Xu 2016-04-16 15:01:09 +0800 166) }
e81f3340bba2b (Herbert Xu 2016-04-16 15:01:09 +0800 167)
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 168) memset((void *)crypt_stat, 0, sizeof(struct ecryptfs_crypt_stat));
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 169) INIT_LIST_HEAD(&crypt_stat->keysig_list);
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 170) mutex_init(&crypt_stat->keysig_list_mutex);
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 171) mutex_init(&crypt_stat->cs_mutex);
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 172) mutex_init(&crypt_stat->cs_tfm_mutex);
e81f3340bba2b (Herbert Xu 2016-04-16 15:01:09 +0800 173) crypt_stat->hash_tfm = tfm;
e2bd99ec5c0e2 (Michael Halcrow 2007-02-12 00:53:49 -0800 174) crypt_stat->flags |= ECRYPTFS_STRUCT_INITIALIZED;
e81f3340bba2b (Herbert Xu 2016-04-16 15:01:09 +0800 175)
e81f3340bba2b (Herbert Xu 2016-04-16 15:01:09 +0800 176) return 0;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 177) }
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 178)
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 179) /**
fcd12835666b0 (Michael Halcrow 2007-10-16 01:28:01 -0700 180) * ecryptfs_destroy_crypt_stat
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 181) * @crypt_stat: Pointer to the crypt_stat struct to initialize.
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 182) *
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 183) * Releases all memory associated with a crypt_stat struct.
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 184) */
fcd12835666b0 (Michael Halcrow 2007-10-16 01:28:01 -0700 185) void ecryptfs_destroy_crypt_stat(struct ecryptfs_crypt_stat *crypt_stat)
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 186) {
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 187) struct ecryptfs_key_sig *key_sig, *key_sig_tmp;
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 188)
3095e8e366b47 (Herbert Xu 2016-01-25 10:29:33 +0800 189) crypto_free_skcipher(crypt_stat->tfm);
3095e8e366b47 (Herbert Xu 2016-01-25 10:29:33 +0800 190) crypto_free_shash(crypt_stat->hash_tfm);
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 191) list_for_each_entry_safe(key_sig, key_sig_tmp,
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 192) &crypt_stat->keysig_list, crypt_stat_list) {
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 193) list_del(&key_sig->crypt_stat_list);
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 194) kmem_cache_free(ecryptfs_key_sig_cache, key_sig);
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 195) }
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 196) memset(crypt_stat, 0, sizeof(struct ecryptfs_crypt_stat));
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 197) }
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 198)
fcd12835666b0 (Michael Halcrow 2007-10-16 01:28:01 -0700 199) void ecryptfs_destroy_mount_crypt_stat(
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 200) struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 201) {
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 202) struct ecryptfs_global_auth_tok *auth_tok, *auth_tok_tmp;
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 203)
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 204) if (!(mount_crypt_stat->flags & ECRYPTFS_MOUNT_CRYPT_STAT_INITIALIZED))
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 205) return;
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 206) mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex);
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 207) list_for_each_entry_safe(auth_tok, auth_tok_tmp,
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 208) &mount_crypt_stat->global_auth_tok_list,
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 209) mount_crypt_stat_list) {
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 210) list_del(&auth_tok->mount_crypt_stat_list);
0dad87fcb7326 (Markus Elfring 2015-06-26 18:18:54 +0200 211) if (!(auth_tok->flags & ECRYPTFS_AUTH_TOK_INVALID))
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 212) key_put(auth_tok->global_auth_tok_key);
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 213) kmem_cache_free(ecryptfs_global_auth_tok_cache, auth_tok);
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 214) }
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 215) mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex);
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 216) memset(mount_crypt_stat, 0, sizeof(struct ecryptfs_mount_crypt_stat));
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 217) }
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 218)
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 219) /**
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 220) * virt_to_scatterlist
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 221) * @addr: Virtual address
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 222) * @size: Size of data; should be an even multiple of the block size
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 223) * @sg: Pointer to scatterlist array; set to NULL to obtain only
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 224) * the number of scatterlist structs required in array
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 225) * @sg_size: Max array size
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 226) *
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 227) * Fills in a scatterlist array with page references for a passed
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 228) * virtual address.
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 229) *
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 230) * Returns the number of scatterlist structs in array used
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 231) */
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 232) int virt_to_scatterlist(const void *addr, int size, struct scatterlist *sg,
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 233) int sg_size)
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 234) {
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 235) int i = 0;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 236) struct page *pg;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 237) int offset;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 238) int remainder_of_page;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 239)
68e3f5dd4db62 (Herbert Xu 2007-10-27 00:52:07 -0700 240) sg_init_table(sg, sg_size);
68e3f5dd4db62 (Herbert Xu 2007-10-27 00:52:07 -0700 241)
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 242) while (size > 0 && i < sg_size) {
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 243) pg = virt_to_page(addr);
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 244) offset = offset_in_page(addr);
a07c48ad5be5c (Dan Carpenter 2013-01-26 10:48:42 +0300 245) sg_set_page(&sg[i], pg, 0, offset);
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 246) remainder_of_page = PAGE_SIZE - offset;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 247) if (size >= remainder_of_page) {
a07c48ad5be5c (Dan Carpenter 2013-01-26 10:48:42 +0300 248) sg[i].length = remainder_of_page;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 249) addr += remainder_of_page;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 250) size -= remainder_of_page;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 251) } else {
a07c48ad5be5c (Dan Carpenter 2013-01-26 10:48:42 +0300 252) sg[i].length = size;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 253) addr += size;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 254) size = 0;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 255) }
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 256) i++;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 257) }
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 258) if (size > 0)
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 259) return -ENOMEM;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 260) return i;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 261) }
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 262)
4dfea4f0d7f13 (Tyler Hicks 2013-05-09 16:55:07 -0700 263) struct extent_crypt_result {
4dfea4f0d7f13 (Tyler Hicks 2013-05-09 16:55:07 -0700 264) struct completion completion;
4dfea4f0d7f13 (Tyler Hicks 2013-05-09 16:55:07 -0700 265) int rc;
4dfea4f0d7f13 (Tyler Hicks 2013-05-09 16:55:07 -0700 266) };
4dfea4f0d7f13 (Tyler Hicks 2013-05-09 16:55:07 -0700 267)
4dfea4f0d7f13 (Tyler Hicks 2013-05-09 16:55:07 -0700 268) static void extent_crypt_complete(struct crypto_async_request *req, int rc)
4dfea4f0d7f13 (Tyler Hicks 2013-05-09 16:55:07 -0700 269) {
4dfea4f0d7f13 (Tyler Hicks 2013-05-09 16:55:07 -0700 270) struct extent_crypt_result *ecr = req->data;
4dfea4f0d7f13 (Tyler Hicks 2013-05-09 16:55:07 -0700 271)
4dfea4f0d7f13 (Tyler Hicks 2013-05-09 16:55:07 -0700 272) if (rc == -EINPROGRESS)
4dfea4f0d7f13 (Tyler Hicks 2013-05-09 16:55:07 -0700 273) return;
4dfea4f0d7f13 (Tyler Hicks 2013-05-09 16:55:07 -0700 274)
4dfea4f0d7f13 (Tyler Hicks 2013-05-09 16:55:07 -0700 275) ecr->rc = rc;
4dfea4f0d7f13 (Tyler Hicks 2013-05-09 16:55:07 -0700 276) complete(&ecr->completion);
4dfea4f0d7f13 (Tyler Hicks 2013-05-09 16:55:07 -0700 277) }
4dfea4f0d7f13 (Tyler Hicks 2013-05-09 16:55:07 -0700 278)
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 279) /**
00a699400a707 (Tyler Hicks 2013-04-05 23:26:22 -0700 280) * crypt_scatterlist
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 281) * @crypt_stat: Pointer to the crypt_stat struct to initialize.
0df5ed65c14e2 (Tyler Hicks 2013-05-23 12:08:40 -0700 282) * @dst_sg: Destination of the data after performing the crypto operation
00a699400a707 (Tyler Hicks 2013-04-05 23:26:22 -0700 283) * @src_sg: Data to be encrypted or decrypted
00a699400a707 (Tyler Hicks 2013-04-05 23:26:22 -0700 284) * @size: Length of data
00a699400a707 (Tyler Hicks 2013-04-05 23:26:22 -0700 285) * @iv: IV to use
00a699400a707 (Tyler Hicks 2013-04-05 23:26:22 -0700 286) * @op: ENCRYPT or DECRYPT to indicate the desired operation
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 287) *
00a699400a707 (Tyler Hicks 2013-04-05 23:26:22 -0700 288) * Returns the number of bytes encrypted or decrypted; negative value on error
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 289) */
00a699400a707 (Tyler Hicks 2013-04-05 23:26:22 -0700 290) static int crypt_scatterlist(struct ecryptfs_crypt_stat *crypt_stat,
0df5ed65c14e2 (Tyler Hicks 2013-05-23 12:08:40 -0700 291) struct scatterlist *dst_sg,
00a699400a707 (Tyler Hicks 2013-04-05 23:26:22 -0700 292) struct scatterlist *src_sg, int size,
00a699400a707 (Tyler Hicks 2013-04-05 23:26:22 -0700 293) unsigned char *iv, int op)
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 294) {
3095e8e366b47 (Herbert Xu 2016-01-25 10:29:33 +0800 295) struct skcipher_request *req = NULL;
4dfea4f0d7f13 (Tyler Hicks 2013-05-09 16:55:07 -0700 296) struct extent_crypt_result ecr;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 297) int rc = 0;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 298)
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 299) if (unlikely(ecryptfs_verbosity > 0)) {
f24b38874e1e3 (Tyler Hicks 2010-11-15 17:36:38 -0600 300) ecryptfs_printk(KERN_DEBUG, "Key size [%zd]; key:\n",
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 301) crypt_stat->key_size);
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 302) ecryptfs_dump_hex(crypt_stat->key,
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 303) crypt_stat->key_size);
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 304) }
4dfea4f0d7f13 (Tyler Hicks 2013-05-09 16:55:07 -0700 305)
4dfea4f0d7f13 (Tyler Hicks 2013-05-09 16:55:07 -0700 306) init_completion(&ecr.completion);
4dfea4f0d7f13 (Tyler Hicks 2013-05-09 16:55:07 -0700 307)
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 308) mutex_lock(&crypt_stat->cs_tfm_mutex);
3095e8e366b47 (Herbert Xu 2016-01-25 10:29:33 +0800 309) req = skcipher_request_alloc(crypt_stat->tfm, GFP_NOFS);
4dfea4f0d7f13 (Tyler Hicks 2013-05-09 16:55:07 -0700 310) if (!req) {
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 311) mutex_unlock(&crypt_stat->cs_tfm_mutex);
4dfea4f0d7f13 (Tyler Hicks 2013-05-09 16:55:07 -0700 312) rc = -ENOMEM;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 313) goto out;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 314) }
4dfea4f0d7f13 (Tyler Hicks 2013-05-09 16:55:07 -0700 315)
3095e8e366b47 (Herbert Xu 2016-01-25 10:29:33 +0800 316) skcipher_request_set_callback(req,
4dfea4f0d7f13 (Tyler Hicks 2013-05-09 16:55:07 -0700 317) CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
4dfea4f0d7f13 (Tyler Hicks 2013-05-09 16:55:07 -0700 318) extent_crypt_complete, &ecr);
4dfea4f0d7f13 (Tyler Hicks 2013-05-09 16:55:07 -0700 319) /* Consider doing this once, when the file is opened */
4dfea4f0d7f13 (Tyler Hicks 2013-05-09 16:55:07 -0700 320) if (!(crypt_stat->flags & ECRYPTFS_KEY_SET)) {
3095e8e366b47 (Herbert Xu 2016-01-25 10:29:33 +0800 321) rc = crypto_skcipher_setkey(crypt_stat->tfm, crypt_stat->key,
3095e8e366b47 (Herbert Xu 2016-01-25 10:29:33 +0800 322) crypt_stat->key_size);
4dfea4f0d7f13 (Tyler Hicks 2013-05-09 16:55:07 -0700 323) if (rc) {
4dfea4f0d7f13 (Tyler Hicks 2013-05-09 16:55:07 -0700 324) ecryptfs_printk(KERN_ERR,
4dfea4f0d7f13 (Tyler Hicks 2013-05-09 16:55:07 -0700 325) "Error setting key; rc = [%d]\n",
4dfea4f0d7f13 (Tyler Hicks 2013-05-09 16:55:07 -0700 326) rc);
4dfea4f0d7f13 (Tyler Hicks 2013-05-09 16:55:07 -0700 327) mutex_unlock(&crypt_stat->cs_tfm_mutex);
4dfea4f0d7f13 (Tyler Hicks 2013-05-09 16:55:07 -0700 328) rc = -EINVAL;
4dfea4f0d7f13 (Tyler Hicks 2013-05-09 16:55:07 -0700 329) goto out;
4dfea4f0d7f13 (Tyler Hicks 2013-05-09 16:55:07 -0700 330) }
4dfea4f0d7f13 (Tyler Hicks 2013-05-09 16:55:07 -0700 331) crypt_stat->flags |= ECRYPTFS_KEY_SET;
4dfea4f0d7f13 (Tyler Hicks 2013-05-09 16:55:07 -0700 332) }
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 333) mutex_unlock(&crypt_stat->cs_tfm_mutex);
3095e8e366b47 (Herbert Xu 2016-01-25 10:29:33 +0800 334) skcipher_request_set_crypt(req, src_sg, dst_sg, size, iv);
3095e8e366b47 (Herbert Xu 2016-01-25 10:29:33 +0800 335) rc = op == ENCRYPT ? crypto_skcipher_encrypt(req) :
3095e8e366b47 (Herbert Xu 2016-01-25 10:29:33 +0800 336) crypto_skcipher_decrypt(req);
4dfea4f0d7f13 (Tyler Hicks 2013-05-09 16:55:07 -0700 337) if (rc == -EINPROGRESS || rc == -EBUSY) {
4dfea4f0d7f13 (Tyler Hicks 2013-05-09 16:55:07 -0700 338) struct extent_crypt_result *ecr = req->base.data;
4dfea4f0d7f13 (Tyler Hicks 2013-05-09 16:55:07 -0700 339)
4dfea4f0d7f13 (Tyler Hicks 2013-05-09 16:55:07 -0700 340) wait_for_completion(&ecr->completion);
4dfea4f0d7f13 (Tyler Hicks 2013-05-09 16:55:07 -0700 341) rc = ecr->rc;
16735d022f72b (Wolfram Sang 2013-11-14 14:32:02 -0800 342) reinit_completion(&ecr->completion);
4dfea4f0d7f13 (Tyler Hicks 2013-05-09 16:55:07 -0700 343) }
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 344) out:
3095e8e366b47 (Herbert Xu 2016-01-25 10:29:33 +0800 345) skcipher_request_free(req);
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 346) return rc;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 347) }
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 348)
5da877eadffb8 (Lee Jones 2021-03-30 17:44:55 +0100 349) /*
24d15266bd86b (Tyler Hicks 2013-04-15 17:28:09 -0700 350) * lower_offset_for_page
0216f7f792175 (Michael Halcrow 2007-10-16 01:28:08 -0700 351) *
0216f7f792175 (Michael Halcrow 2007-10-16 01:28:08 -0700 352) * Convert an eCryptfs page index into a lower byte offset
0216f7f792175 (Michael Halcrow 2007-10-16 01:28:08 -0700 353) */
24d15266bd86b (Tyler Hicks 2013-04-15 17:28:09 -0700 354) static loff_t lower_offset_for_page(struct ecryptfs_crypt_stat *crypt_stat,
24d15266bd86b (Tyler Hicks 2013-04-15 17:28:09 -0700 355) struct page *page)
0216f7f792175 (Michael Halcrow 2007-10-16 01:28:08 -0700 356) {
24d15266bd86b (Tyler Hicks 2013-04-15 17:28:09 -0700 357) return ecryptfs_lower_header_size(crypt_stat) +
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 358) ((loff_t)page->index << PAGE_SHIFT);
0216f7f792175 (Michael Halcrow 2007-10-16 01:28:08 -0700 359) }
0216f7f792175 (Michael Halcrow 2007-10-16 01:28:08 -0700 360)
0216f7f792175 (Michael Halcrow 2007-10-16 01:28:08 -0700 361) /**
d78de618962d1 (Tyler Hicks 2013-04-06 00:08:48 -0700 362) * crypt_extent
0216f7f792175 (Michael Halcrow 2007-10-16 01:28:08 -0700 363) * @crypt_stat: crypt_stat containing cryptographic context for the
0216f7f792175 (Michael Halcrow 2007-10-16 01:28:08 -0700 364) * encryption operation
0df5ed65c14e2 (Tyler Hicks 2013-05-23 12:08:40 -0700 365) * @dst_page: The page to write the result into
d78de618962d1 (Tyler Hicks 2013-04-06 00:08:48 -0700 366) * @src_page: The page to read from
0216f7f792175 (Michael Halcrow 2007-10-16 01:28:08 -0700 367) * @extent_offset: Page extent offset for use in generating IV
d78de618962d1 (Tyler Hicks 2013-04-06 00:08:48 -0700 368) * @op: ENCRYPT or DECRYPT to indicate the desired operation
0216f7f792175 (Michael Halcrow 2007-10-16 01:28:08 -0700 369) *
d78de618962d1 (Tyler Hicks 2013-04-06 00:08:48 -0700 370) * Encrypts or decrypts one extent of data.
0216f7f792175 (Michael Halcrow 2007-10-16 01:28:08 -0700 371) *
0216f7f792175 (Michael Halcrow 2007-10-16 01:28:08 -0700 372) * Return zero on success; non-zero otherwise
0216f7f792175 (Michael Halcrow 2007-10-16 01:28:08 -0700 373) */
0df5ed65c14e2 (Tyler Hicks 2013-05-23 12:08:40 -0700 374) static int crypt_extent(struct ecryptfs_crypt_stat *crypt_stat,
0df5ed65c14e2 (Tyler Hicks 2013-05-23 12:08:40 -0700 375) struct page *dst_page,
d78de618962d1 (Tyler Hicks 2013-04-06 00:08:48 -0700 376) struct page *src_page,
d78de618962d1 (Tyler Hicks 2013-04-06 00:08:48 -0700 377) unsigned long extent_offset, int op)
0216f7f792175 (Michael Halcrow 2007-10-16 01:28:08 -0700 378) {
d78de618962d1 (Tyler Hicks 2013-04-06 00:08:48 -0700 379) pgoff_t page_index = op == ENCRYPT ? src_page->index : dst_page->index;
d6a13c17164fc (Michael Halcrow 2007-10-16 01:28:12 -0700 380) loff_t extent_base;
0216f7f792175 (Michael Halcrow 2007-10-16 01:28:08 -0700 381) char extent_iv[ECRYPTFS_MAX_IV_BYTES];
406c93df09ae7 (Tyler Hicks 2013-04-15 17:49:31 -0700 382) struct scatterlist src_sg, dst_sg;
406c93df09ae7 (Tyler Hicks 2013-04-15 17:49:31 -0700 383) size_t extent_size = crypt_stat->extent_size;
0216f7f792175 (Michael Halcrow 2007-10-16 01:28:08 -0700 384) int rc;
0216f7f792175 (Michael Halcrow 2007-10-16 01:28:08 -0700 385)
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 386) extent_base = (((loff_t)page_index) * (PAGE_SIZE / extent_size));
0216f7f792175 (Michael Halcrow 2007-10-16 01:28:08 -0700 387) rc = ecryptfs_derive_iv(extent_iv, crypt_stat,
0216f7f792175 (Michael Halcrow 2007-10-16 01:28:08 -0700 388) (extent_base + extent_offset));
0216f7f792175 (Michael Halcrow 2007-10-16 01:28:08 -0700 389) if (rc) {
888d57bbc91eb (Joe Perches 2010-11-10 15:46:16 -0800 390) ecryptfs_printk(KERN_ERR, "Error attempting to derive IV for "
888d57bbc91eb (Joe Perches 2010-11-10 15:46:16 -0800 391) "extent [0x%.16llx]; rc = [%d]\n",
888d57bbc91eb (Joe Perches 2010-11-10 15:46:16 -0800 392) (unsigned long long)(extent_base + extent_offset), rc);
0216f7f792175 (Michael Halcrow 2007-10-16 01:28:08 -0700 393) goto out;
0216f7f792175 (Michael Halcrow 2007-10-16 01:28:08 -0700 394) }
406c93df09ae7 (Tyler Hicks 2013-04-15 17:49:31 -0700 395)
406c93df09ae7 (Tyler Hicks 2013-04-15 17:49:31 -0700 396) sg_init_table(&src_sg, 1);
406c93df09ae7 (Tyler Hicks 2013-04-15 17:49:31 -0700 397) sg_init_table(&dst_sg, 1);
406c93df09ae7 (Tyler Hicks 2013-04-15 17:49:31 -0700 398)
406c93df09ae7 (Tyler Hicks 2013-04-15 17:49:31 -0700 399) sg_set_page(&src_sg, src_page, extent_size,
406c93df09ae7 (Tyler Hicks 2013-04-15 17:49:31 -0700 400) extent_offset * extent_size);
406c93df09ae7 (Tyler Hicks 2013-04-15 17:49:31 -0700 401) sg_set_page(&dst_sg, dst_page, extent_size,
406c93df09ae7 (Tyler Hicks 2013-04-15 17:49:31 -0700 402) extent_offset * extent_size);
406c93df09ae7 (Tyler Hicks 2013-04-15 17:49:31 -0700 403)
406c93df09ae7 (Tyler Hicks 2013-04-15 17:49:31 -0700 404) rc = crypt_scatterlist(crypt_stat, &dst_sg, &src_sg, extent_size,
406c93df09ae7 (Tyler Hicks 2013-04-15 17:49:31 -0700 405) extent_iv, op);
0216f7f792175 (Michael Halcrow 2007-10-16 01:28:08 -0700 406) if (rc < 0) {
d78de618962d1 (Tyler Hicks 2013-04-06 00:08:48 -0700 407) printk(KERN_ERR "%s: Error attempting to crypt page with "
d78de618962d1 (Tyler Hicks 2013-04-06 00:08:48 -0700 408) "page_index = [%ld], extent_offset = [%ld]; "
d78de618962d1 (Tyler Hicks 2013-04-06 00:08:48 -0700 409) "rc = [%d]\n", __func__, page_index, extent_offset, rc);
0216f7f792175 (Michael Halcrow 2007-10-16 01:28:08 -0700 410) goto out;
0216f7f792175 (Michael Halcrow 2007-10-16 01:28:08 -0700 411) }
0216f7f792175 (Michael Halcrow 2007-10-16 01:28:08 -0700 412) rc = 0;
0216f7f792175 (Michael Halcrow 2007-10-16 01:28:08 -0700 413) out:
0216f7f792175 (Michael Halcrow 2007-10-16 01:28:08 -0700 414) return rc;
0216f7f792175 (Michael Halcrow 2007-10-16 01:28:08 -0700 415) }
0216f7f792175 (Michael Halcrow 2007-10-16 01:28:08 -0700 416)
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 417) /**
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 418) * ecryptfs_encrypt_page
0216f7f792175 (Michael Halcrow 2007-10-16 01:28:08 -0700 419) * @page: Page mapped from the eCryptfs inode for the file; contains
0216f7f792175 (Michael Halcrow 2007-10-16 01:28:08 -0700 420) * decrypted content that needs to be encrypted (to a temporary
0216f7f792175 (Michael Halcrow 2007-10-16 01:28:08 -0700 421) * page; not in place) and written out to the lower file
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 422) *
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 423) * Encrypt an eCryptfs page. This is done on a per-extent basis. Note
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 424) * that eCryptfs pages may straddle the lower pages -- for instance,
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 425) * if the file was created on a machine with an 8K page size
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 426) * (resulting in an 8K header), and then the file is copied onto a
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 427) * host with a 32K page size, then when reading page 0 of the eCryptfs
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 428) * file, 24K of page 0 of the lower file will be read and decrypted,
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 429) * and then 8K of page 1 of the lower file will be read and decrypted.
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 430) *
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 431) * Returns zero on success; negative on error
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 432) */
0216f7f792175 (Michael Halcrow 2007-10-16 01:28:08 -0700 433) int ecryptfs_encrypt_page(struct page *page)
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 434) {
0216f7f792175 (Michael Halcrow 2007-10-16 01:28:08 -0700 435) struct inode *ecryptfs_inode;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 436) struct ecryptfs_crypt_stat *crypt_stat;
7fcba054373d5 (Eric Sandeen 2008-07-28 15:46:39 -0700 437) char *enc_extent_virt;
7fcba054373d5 (Eric Sandeen 2008-07-28 15:46:39 -0700 438) struct page *enc_extent_page = NULL;
0216f7f792175 (Michael Halcrow 2007-10-16 01:28:08 -0700 439) loff_t extent_offset;
0f89617623fed (Tyler Hicks 2013-04-15 16:16:24 -0700 440) loff_t lower_offset;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 441) int rc = 0;
0216f7f792175 (Michael Halcrow 2007-10-16 01:28:08 -0700 442)
0216f7f792175 (Michael Halcrow 2007-10-16 01:28:08 -0700 443) ecryptfs_inode = page->mapping->host;
0216f7f792175 (Michael Halcrow 2007-10-16 01:28:08 -0700 444) crypt_stat =
0216f7f792175 (Michael Halcrow 2007-10-16 01:28:08 -0700 445) &(ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat);
13a791b4e63eb (Tyler Hicks 2009-04-13 15:29:27 -0500 446) BUG_ON(!(crypt_stat->flags & ECRYPTFS_ENCRYPTED));
7fcba054373d5 (Eric Sandeen 2008-07-28 15:46:39 -0700 447) enc_extent_page = alloc_page(GFP_USER);
7fcba054373d5 (Eric Sandeen 2008-07-28 15:46:39 -0700 448) if (!enc_extent_page) {
0216f7f792175 (Michael Halcrow 2007-10-16 01:28:08 -0700 449) rc = -ENOMEM;
0216f7f792175 (Michael Halcrow 2007-10-16 01:28:08 -0700 450) ecryptfs_printk(KERN_ERR, "Error allocating memory for "
0216f7f792175 (Michael Halcrow 2007-10-16 01:28:08 -0700 451) "encrypted extent\n");
0216f7f792175 (Michael Halcrow 2007-10-16 01:28:08 -0700 452) goto out;
0216f7f792175 (Michael Halcrow 2007-10-16 01:28:08 -0700 453) }
0f89617623fed (Tyler Hicks 2013-04-15 16:16:24 -0700 454)
0216f7f792175 (Michael Halcrow 2007-10-16 01:28:08 -0700 455) for (extent_offset = 0;
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 456) extent_offset < (PAGE_SIZE / crypt_stat->extent_size);
0216f7f792175 (Michael Halcrow 2007-10-16 01:28:08 -0700 457) extent_offset++) {
0df5ed65c14e2 (Tyler Hicks 2013-05-23 12:08:40 -0700 458) rc = crypt_extent(crypt_stat, enc_extent_page, page,
d78de618962d1 (Tyler Hicks 2013-04-06 00:08:48 -0700 459) extent_offset, ENCRYPT);
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 460) if (rc) {
0216f7f792175 (Michael Halcrow 2007-10-16 01:28:08 -0700 461) printk(KERN_ERR "%s: Error encrypting extent; "
18d1dbf1d401e (Harvey Harrison 2008-04-29 00:59:48 -0700 462) "rc = [%d]\n", __func__, rc);
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 463) goto out;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 464) }
7fcba054373d5 (Eric Sandeen 2008-07-28 15:46:39 -0700 465) }
0216f7f792175 (Michael Halcrow 2007-10-16 01:28:08 -0700 466)
24d15266bd86b (Tyler Hicks 2013-04-15 17:28:09 -0700 467) lower_offset = lower_offset_for_page(crypt_stat, page);
0f89617623fed (Tyler Hicks 2013-04-15 16:16:24 -0700 468) enc_extent_virt = kmap(enc_extent_page);
0f89617623fed (Tyler Hicks 2013-04-15 16:16:24 -0700 469) rc = ecryptfs_write_lower(ecryptfs_inode, enc_extent_virt, lower_offset,
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 470) PAGE_SIZE);
0f89617623fed (Tyler Hicks 2013-04-15 16:16:24 -0700 471) kunmap(enc_extent_page);
0216f7f792175 (Michael Halcrow 2007-10-16 01:28:08 -0700 472) if (rc < 0) {
0f89617623fed (Tyler Hicks 2013-04-15 16:16:24 -0700 473) ecryptfs_printk(KERN_ERR,
0f89617623fed (Tyler Hicks 2013-04-15 16:16:24 -0700 474) "Error attempting to write lower page; rc = [%d]\n",
0f89617623fed (Tyler Hicks 2013-04-15 16:16:24 -0700 475) rc);
0216f7f792175 (Michael Halcrow 2007-10-16 01:28:08 -0700 476) goto out;
0216f7f792175 (Michael Halcrow 2007-10-16 01:28:08 -0700 477) }
0216f7f792175 (Michael Halcrow 2007-10-16 01:28:08 -0700 478) rc = 0;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 479) out:
7fcba054373d5 (Eric Sandeen 2008-07-28 15:46:39 -0700 480) if (enc_extent_page) {
7fcba054373d5 (Eric Sandeen 2008-07-28 15:46:39 -0700 481) __free_page(enc_extent_page);
7fcba054373d5 (Eric Sandeen 2008-07-28 15:46:39 -0700 482) }
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 483) return rc;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 484) }
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 485)
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 486) /**
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 487) * ecryptfs_decrypt_page
0216f7f792175 (Michael Halcrow 2007-10-16 01:28:08 -0700 488) * @page: Page mapped from the eCryptfs inode for the file; data read
0216f7f792175 (Michael Halcrow 2007-10-16 01:28:08 -0700 489) * and decrypted from the lower file will be written into this
0216f7f792175 (Michael Halcrow 2007-10-16 01:28:08 -0700 490) * page
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 491) *
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 492) * Decrypt an eCryptfs page. This is done on a per-extent basis. Note
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 493) * that eCryptfs pages may straddle the lower pages -- for instance,
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 494) * if the file was created on a machine with an 8K page size
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 495) * (resulting in an 8K header), and then the file is copied onto a
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 496) * host with a 32K page size, then when reading page 0 of the eCryptfs
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 497) * file, 24K of page 0 of the lower file will be read and decrypted,
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 498) * and then 8K of page 1 of the lower file will be read and decrypted.
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 499) *
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 500) * Returns zero on success; negative on error
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 501) */
0216f7f792175 (Michael Halcrow 2007-10-16 01:28:08 -0700 502) int ecryptfs_decrypt_page(struct page *page)
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 503) {
0216f7f792175 (Michael Halcrow 2007-10-16 01:28:08 -0700 504) struct inode *ecryptfs_inode;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 505) struct ecryptfs_crypt_stat *crypt_stat;
9c6043f41222b (Tyler Hicks 2013-04-06 00:41:48 -0700 506) char *page_virt;
0216f7f792175 (Michael Halcrow 2007-10-16 01:28:08 -0700 507) unsigned long extent_offset;
0f89617623fed (Tyler Hicks 2013-04-15 16:16:24 -0700 508) loff_t lower_offset;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 509) int rc = 0;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 510)
0216f7f792175 (Michael Halcrow 2007-10-16 01:28:08 -0700 511) ecryptfs_inode = page->mapping->host;
0216f7f792175 (Michael Halcrow 2007-10-16 01:28:08 -0700 512) crypt_stat =
0216f7f792175 (Michael Halcrow 2007-10-16 01:28:08 -0700 513) &(ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat);
13a791b4e63eb (Tyler Hicks 2009-04-13 15:29:27 -0500 514) BUG_ON(!(crypt_stat->flags & ECRYPTFS_ENCRYPTED));
0f89617623fed (Tyler Hicks 2013-04-15 16:16:24 -0700 515)
24d15266bd86b (Tyler Hicks 2013-04-15 17:28:09 -0700 516) lower_offset = lower_offset_for_page(crypt_stat, page);
9c6043f41222b (Tyler Hicks 2013-04-06 00:41:48 -0700 517) page_virt = kmap(page);
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 518) rc = ecryptfs_read_lower(page_virt, lower_offset, PAGE_SIZE,
0f89617623fed (Tyler Hicks 2013-04-15 16:16:24 -0700 519) ecryptfs_inode);
9c6043f41222b (Tyler Hicks 2013-04-06 00:41:48 -0700 520) kunmap(page);
0f89617623fed (Tyler Hicks 2013-04-15 16:16:24 -0700 521) if (rc < 0) {
0f89617623fed (Tyler Hicks 2013-04-15 16:16:24 -0700 522) ecryptfs_printk(KERN_ERR,
0f89617623fed (Tyler Hicks 2013-04-15 16:16:24 -0700 523) "Error attempting to read lower page; rc = [%d]\n",
0f89617623fed (Tyler Hicks 2013-04-15 16:16:24 -0700 524) rc);
16a72c455a67b (Michael Halcrow 2007-10-16 01:28:14 -0700 525) goto out;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 526) }
0f89617623fed (Tyler Hicks 2013-04-15 16:16:24 -0700 527)
0216f7f792175 (Michael Halcrow 2007-10-16 01:28:08 -0700 528) for (extent_offset = 0;
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 529) extent_offset < (PAGE_SIZE / crypt_stat->extent_size);
0216f7f792175 (Michael Halcrow 2007-10-16 01:28:08 -0700 530) extent_offset++) {
0df5ed65c14e2 (Tyler Hicks 2013-05-23 12:08:40 -0700 531) rc = crypt_extent(crypt_stat, page, page,
d78de618962d1 (Tyler Hicks 2013-04-06 00:08:48 -0700 532) extent_offset, DECRYPT);
0216f7f792175 (Michael Halcrow 2007-10-16 01:28:08 -0700 533) if (rc) {
1abbe1106d48a (Sascha Hauer 2021-02-24 12:30:59 +0100 534) printk(KERN_ERR "%s: Error decrypting extent; "
18d1dbf1d401e (Harvey Harrison 2008-04-29 00:59:48 -0700 535) "rc = [%d]\n", __func__, rc);
16a72c455a67b (Michael Halcrow 2007-10-16 01:28:14 -0700 536) goto out;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 537) }
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 538) }
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 539) out:
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 540) return rc;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 541) }
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 542)
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 543) #define ECRYPTFS_MAX_SCATTERLIST_LEN 4
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 544)
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 545) /**
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 546) * ecryptfs_init_crypt_ctx
421f91d21ad6f (Uwe Kleine-König 2010-06-11 12:17:00 +0200 547) * @crypt_stat: Uninitialized crypt stats structure
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 548) *
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 549) * Initialize the crypto context.
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 550) *
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 551) * TODO: Performance: Keep a cache of initialized cipher contexts;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 552) * only init if needed
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 553) */
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 554) int ecryptfs_init_crypt_ctx(struct ecryptfs_crypt_stat *crypt_stat)
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 555) {
8bba066f4e385 (Michael Halcrow 2006-10-30 22:07:18 -0800 556) char *full_alg_name;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 557) int rc = -EINVAL;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 558)
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 559) ecryptfs_printk(KERN_DEBUG,
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 560) "Initializing cipher [%s]; strlen = [%d]; "
f24b38874e1e3 (Tyler Hicks 2010-11-15 17:36:38 -0600 561) "key_size_bits = [%zd]\n",
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 562) crypt_stat->cipher, (int)strlen(crypt_stat->cipher),
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 563) crypt_stat->key_size << 3);
cb69f36ba1f8d (Kees Cook 2013-08-13 15:02:27 -0700 564) mutex_lock(&crypt_stat->cs_tfm_mutex);
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 565) if (crypt_stat->tfm) {
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 566) rc = 0;
cb69f36ba1f8d (Kees Cook 2013-08-13 15:02:27 -0700 567) goto out_unlock;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 568) }
8bba066f4e385 (Michael Halcrow 2006-10-30 22:07:18 -0800 569) rc = ecryptfs_crypto_api_algify_cipher_name(&full_alg_name,
8bba066f4e385 (Michael Halcrow 2006-10-30 22:07:18 -0800 570) crypt_stat->cipher, "cbc");
8bba066f4e385 (Michael Halcrow 2006-10-30 22:07:18 -0800 571) if (rc)
c8161f64ccdcc (Eric Sandeen 2007-12-22 14:03:26 -0800 572) goto out_unlock;
3095e8e366b47 (Herbert Xu 2016-01-25 10:29:33 +0800 573) crypt_stat->tfm = crypto_alloc_skcipher(full_alg_name, 0, 0);
de88777e6942d (Akinobu Mita 2006-11-28 12:29:49 -0800 574) if (IS_ERR(crypt_stat->tfm)) {
de88777e6942d (Akinobu Mita 2006-11-28 12:29:49 -0800 575) rc = PTR_ERR(crypt_stat->tfm);
b0105eaefa7cc (Tyler Hicks 2009-08-11 00:36:32 -0500 576) crypt_stat->tfm = NULL;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 577) ecryptfs_printk(KERN_ERR, "cryptfs: init_crypt_ctx(): "
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 578) "Error initializing cipher [%s]\n",
cb69f36ba1f8d (Kees Cook 2013-08-13 15:02:27 -0700 579) full_alg_name);
cb69f36ba1f8d (Kees Cook 2013-08-13 15:02:27 -0700 580) goto out_free;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 581) }
231baecdef7a9 (Eric Biggers 2019-01-18 22:48:00 -0800 582) crypto_skcipher_set_flags(crypt_stat->tfm,
231baecdef7a9 (Eric Biggers 2019-01-18 22:48:00 -0800 583) CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 584) rc = 0;
cb69f36ba1f8d (Kees Cook 2013-08-13 15:02:27 -0700 585) out_free:
cb69f36ba1f8d (Kees Cook 2013-08-13 15:02:27 -0700 586) kfree(full_alg_name);
c8161f64ccdcc (Eric Sandeen 2007-12-22 14:03:26 -0800 587) out_unlock:
c8161f64ccdcc (Eric Sandeen 2007-12-22 14:03:26 -0800 588) mutex_unlock(&crypt_stat->cs_tfm_mutex);
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 589) return rc;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 590) }
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 591)
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 592) static void set_extent_mask_and_shift(struct ecryptfs_crypt_stat *crypt_stat)
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 593) {
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 594) int extent_size_tmp;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 595)
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 596) crypt_stat->extent_mask = 0xFFFFFFFF;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 597) crypt_stat->extent_shift = 0;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 598) if (crypt_stat->extent_size == 0)
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 599) return;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 600) extent_size_tmp = crypt_stat->extent_size;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 601) while ((extent_size_tmp & 0x01) == 0) {
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 602) extent_size_tmp >>= 1;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 603) crypt_stat->extent_mask <<= 1;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 604) crypt_stat->extent_shift++;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 605) }
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 606) }
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 607)
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 608) void ecryptfs_set_default_sizes(struct ecryptfs_crypt_stat *crypt_stat)
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 609) {
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 610) /* Default values; may be overwritten as we are parsing the
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 611) * packets. */
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 612) crypt_stat->extent_size = ECRYPTFS_DEFAULT_EXTENT_SIZE;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 613) set_extent_mask_and_shift(crypt_stat);
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 614) crypt_stat->iv_bytes = ECRYPTFS_DEFAULT_IV_BYTES;
dd2a3b7ad98f8 (Michael Halcrow 2007-02-12 00:53:46 -0800 615) if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
fa3ef1cb4e6e9 (Tyler Hicks 2010-02-11 05:09:14 -0600 616) crypt_stat->metadata_size = ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE;
45eaab79678b9 (Michael Halcrow 2007-10-16 01:28:05 -0700 617) else {
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 618) if (PAGE_SIZE <= ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE)
fa3ef1cb4e6e9 (Tyler Hicks 2010-02-11 05:09:14 -0600 619) crypt_stat->metadata_size =
cc11beffdf80c (Michael Halcrow 2008-02-06 01:38:32 -0800 620) ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE;
45eaab79678b9 (Michael Halcrow 2007-10-16 01:28:05 -0700 621) else
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 622) crypt_stat->metadata_size = PAGE_SIZE;
45eaab79678b9 (Michael Halcrow 2007-10-16 01:28:05 -0700 623) }
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 624) }
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 625)
5da877eadffb8 (Lee Jones 2021-03-30 17:44:55 +0100 626) /*
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 627) * ecryptfs_compute_root_iv
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 628) *
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 629) * On error, sets the root IV to all 0's.
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 630) */
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 631) int ecryptfs_compute_root_iv(struct ecryptfs_crypt_stat *crypt_stat)
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 632) {
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 633) int rc = 0;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 634) char dst[MD5_DIGEST_SIZE];
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 635)
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 636) BUG_ON(crypt_stat->iv_bytes > MD5_DIGEST_SIZE);
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 637) BUG_ON(crypt_stat->iv_bytes <= 0);
e2bd99ec5c0e2 (Michael Halcrow 2007-02-12 00:53:49 -0800 638) if (!(crypt_stat->flags & ECRYPTFS_KEY_VALID)) {
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 639) rc = -EINVAL;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 640) ecryptfs_printk(KERN_WARNING, "Session key not valid; "
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 641) "cannot generate root IV\n");
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 642) goto out;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 643) }
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 644) rc = ecryptfs_calculate_md5(dst, crypt_stat, crypt_stat->key,
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 645) crypt_stat->key_size);
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 646) if (rc) {
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 647) ecryptfs_printk(KERN_WARNING, "Error attempting to compute "
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 648) "MD5 while generating root IV\n");
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 649) goto out;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 650) }
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 651) memcpy(crypt_stat->root_iv, dst, crypt_stat->iv_bytes);
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 652) out:
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 653) if (rc) {
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 654) memset(crypt_stat->root_iv, 0, crypt_stat->iv_bytes);
e2bd99ec5c0e2 (Michael Halcrow 2007-02-12 00:53:49 -0800 655) crypt_stat->flags |= ECRYPTFS_SECURITY_WARNING;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 656) }
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 657) return rc;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 658) }
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 659)
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 660) static void ecryptfs_generate_new_key(struct ecryptfs_crypt_stat *crypt_stat)
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 661) {
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 662) get_random_bytes(crypt_stat->key, crypt_stat->key_size);
e2bd99ec5c0e2 (Michael Halcrow 2007-02-12 00:53:49 -0800 663) crypt_stat->flags |= ECRYPTFS_KEY_VALID;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 664) ecryptfs_compute_root_iv(crypt_stat);
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 665) if (unlikely(ecryptfs_verbosity > 0)) {
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 666) ecryptfs_printk(KERN_DEBUG, "Generated new session key:\n");
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 667) ecryptfs_dump_hex(crypt_stat->key,
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 668) crypt_stat->key_size);
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 669) }
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 670) }
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 671)
17398957aa0a0 (Michael Halcrow 2007-02-12 00:53:45 -0800 672) /**
17398957aa0a0 (Michael Halcrow 2007-02-12 00:53:45 -0800 673) * ecryptfs_copy_mount_wide_flags_to_inode_flags
22e78fafbdf84 (Michael Halcrow 2007-10-16 01:28:02 -0700 674) * @crypt_stat: The inode's cryptographic context
22e78fafbdf84 (Michael Halcrow 2007-10-16 01:28:02 -0700 675) * @mount_crypt_stat: The mount point's cryptographic context
17398957aa0a0 (Michael Halcrow 2007-02-12 00:53:45 -0800 676) *
17398957aa0a0 (Michael Halcrow 2007-02-12 00:53:45 -0800 677) * This function propagates the mount-wide flags to individual inode
17398957aa0a0 (Michael Halcrow 2007-02-12 00:53:45 -0800 678) * flags.
17398957aa0a0 (Michael Halcrow 2007-02-12 00:53:45 -0800 679) */
17398957aa0a0 (Michael Halcrow 2007-02-12 00:53:45 -0800 680) static void ecryptfs_copy_mount_wide_flags_to_inode_flags(
17398957aa0a0 (Michael Halcrow 2007-02-12 00:53:45 -0800 681) struct ecryptfs_crypt_stat *crypt_stat,
17398957aa0a0 (Michael Halcrow 2007-02-12 00:53:45 -0800 682) struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
17398957aa0a0 (Michael Halcrow 2007-02-12 00:53:45 -0800 683) {
17398957aa0a0 (Michael Halcrow 2007-02-12 00:53:45 -0800 684) if (mount_crypt_stat->flags & ECRYPTFS_XATTR_METADATA_ENABLED)
17398957aa0a0 (Michael Halcrow 2007-02-12 00:53:45 -0800 685) crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR;
17398957aa0a0 (Michael Halcrow 2007-02-12 00:53:45 -0800 686) if (mount_crypt_stat->flags & ECRYPTFS_ENCRYPTED_VIEW_ENABLED)
17398957aa0a0 (Michael Halcrow 2007-02-12 00:53:45 -0800 687) crypt_stat->flags |= ECRYPTFS_VIEW_AS_ENCRYPTED;
addd65ad8d19a (Michael Halcrow 2009-01-06 14:42:00 -0800 688) if (mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES) {
addd65ad8d19a (Michael Halcrow 2009-01-06 14:42:00 -0800 689) crypt_stat->flags |= ECRYPTFS_ENCRYPT_FILENAMES;
addd65ad8d19a (Michael Halcrow 2009-01-06 14:42:00 -0800 690) if (mount_crypt_stat->flags
addd65ad8d19a (Michael Halcrow 2009-01-06 14:42:00 -0800 691) & ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK)
addd65ad8d19a (Michael Halcrow 2009-01-06 14:42:00 -0800 692) crypt_stat->flags |= ECRYPTFS_ENCFN_USE_MOUNT_FNEK;
addd65ad8d19a (Michael Halcrow 2009-01-06 14:42:00 -0800 693) else if (mount_crypt_stat->flags
addd65ad8d19a (Michael Halcrow 2009-01-06 14:42:00 -0800 694) & ECRYPTFS_GLOBAL_ENCFN_USE_FEK)
addd65ad8d19a (Michael Halcrow 2009-01-06 14:42:00 -0800 695) crypt_stat->flags |= ECRYPTFS_ENCFN_USE_FEK;
addd65ad8d19a (Michael Halcrow 2009-01-06 14:42:00 -0800 696) }
17398957aa0a0 (Michael Halcrow 2007-02-12 00:53:45 -0800 697) }
17398957aa0a0 (Michael Halcrow 2007-02-12 00:53:45 -0800 698)
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 699) static int ecryptfs_copy_mount_wide_sigs_to_inode_sigs(
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 700) struct ecryptfs_crypt_stat *crypt_stat,
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 701) struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 702) {
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 703) struct ecryptfs_global_auth_tok *global_auth_tok;
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 704) int rc = 0;
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 705)
aa06117f19944 (Roland Dreier 2009-07-01 15:48:18 -0700 706) mutex_lock(&crypt_stat->keysig_list_mutex);
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 707) mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex);
aa06117f19944 (Roland Dreier 2009-07-01 15:48:18 -0700 708)
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 709) list_for_each_entry(global_auth_tok,
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 710) &mount_crypt_stat->global_auth_tok_list,
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 711) mount_crypt_stat_list) {
84814d642a4f1 (Tyler Hicks 2009-03-13 13:51:59 -0700 712) if (global_auth_tok->flags & ECRYPTFS_AUTH_TOK_FNEK)
84814d642a4f1 (Tyler Hicks 2009-03-13 13:51:59 -0700 713) continue;
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 714) rc = ecryptfs_add_keysig(crypt_stat, global_auth_tok->sig);
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 715) if (rc) {
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 716) printk(KERN_ERR "Error adding keysig; rc = [%d]\n", rc);
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 717) goto out;
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 718) }
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 719) }
aa06117f19944 (Roland Dreier 2009-07-01 15:48:18 -0700 720)
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 721) out:
aa06117f19944 (Roland Dreier 2009-07-01 15:48:18 -0700 722) mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex);
aa06117f19944 (Roland Dreier 2009-07-01 15:48:18 -0700 723) mutex_unlock(&crypt_stat->keysig_list_mutex);
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 724) return rc;
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 725) }
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 726)
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 727) /**
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 728) * ecryptfs_set_default_crypt_stat_vals
22e78fafbdf84 (Michael Halcrow 2007-10-16 01:28:02 -0700 729) * @crypt_stat: The inode's cryptographic context
22e78fafbdf84 (Michael Halcrow 2007-10-16 01:28:02 -0700 730) * @mount_crypt_stat: The mount point's cryptographic context
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 731) *
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 732) * Default values in the event that policy does not override them.
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 733) */
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 734) static void ecryptfs_set_default_crypt_stat_vals(
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 735) struct ecryptfs_crypt_stat *crypt_stat,
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 736) struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 737) {
17398957aa0a0 (Michael Halcrow 2007-02-12 00:53:45 -0800 738) ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat,
17398957aa0a0 (Michael Halcrow 2007-02-12 00:53:45 -0800 739) mount_crypt_stat);
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 740) ecryptfs_set_default_sizes(crypt_stat);
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 741) strcpy(crypt_stat->cipher, ECRYPTFS_DEFAULT_CIPHER);
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 742) crypt_stat->key_size = ECRYPTFS_DEFAULT_KEY_BYTES;
e2bd99ec5c0e2 (Michael Halcrow 2007-02-12 00:53:49 -0800 743) crypt_stat->flags &= ~(ECRYPTFS_KEY_VALID);
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 744) crypt_stat->file_version = ECRYPTFS_FILE_VERSION;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 745) crypt_stat->mount_crypt_stat = mount_crypt_stat;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 746) }
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 747)
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 748) /**
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 749) * ecryptfs_new_file_context
b59db43ad4434 (Tyler Hicks 2011-11-21 17:31:02 -0600 750) * @ecryptfs_inode: The eCryptfs inode
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 751) *
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 752) * If the crypto context for the file has not yet been established,
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 753) * this is where we do that. Establishing a new crypto context
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 754) * involves the following decisions:
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 755) * - What cipher to use?
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 756) * - What set of authentication tokens to use?
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 757) * Here we just worry about getting enough information into the
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 758) * authentication tokens so that we know that they are available.
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 759) * We associate the available authentication tokens with the new file
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 760) * via the set of signatures in the crypt_stat struct. Later, when
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 761) * the headers are actually written out, we may again defer to
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 762) * userspace to perform the encryption of the session key; for the
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 763) * foreseeable future, this will be the case with public key packets.
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 764) *
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 765) * Returns zero on success; non-zero otherwise
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 766) */
b59db43ad4434 (Tyler Hicks 2011-11-21 17:31:02 -0600 767) int ecryptfs_new_file_context(struct inode *ecryptfs_inode)
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 768) {
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 769) struct ecryptfs_crypt_stat *crypt_stat =
b59db43ad4434 (Tyler Hicks 2011-11-21 17:31:02 -0600 770) &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 771) struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 772) &ecryptfs_superblock_to_private(
b59db43ad4434 (Tyler Hicks 2011-11-21 17:31:02 -0600 773) ecryptfs_inode->i_sb)->mount_crypt_stat;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 774) int cipher_name_len;
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 775) int rc = 0;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 776)
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 777) ecryptfs_set_default_crypt_stat_vals(crypt_stat, mount_crypt_stat);
af655dc6a9d50 (Michael Halcrow 2007-10-16 01:28:00 -0700 778) crypt_stat->flags |= (ECRYPTFS_ENCRYPTED | ECRYPTFS_KEY_VALID);
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 779) ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat,
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 780) mount_crypt_stat);
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 781) rc = ecryptfs_copy_mount_wide_sigs_to_inode_sigs(crypt_stat,
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 782) mount_crypt_stat);
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 783) if (rc) {
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 784) printk(KERN_ERR "Error attempting to copy mount-wide key sigs "
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 785) "to the inode key sigs; rc = [%d]\n", rc);
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 786) goto out;
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 787) }
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 788) cipher_name_len =
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 789) strlen(mount_crypt_stat->global_default_cipher_name);
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 790) memcpy(crypt_stat->cipher,
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 791) mount_crypt_stat->global_default_cipher_name,
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 792) cipher_name_len);
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 793) crypt_stat->cipher[cipher_name_len] = '\0';
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 794) crypt_stat->key_size =
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 795) mount_crypt_stat->global_default_cipher_key_size;
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 796) ecryptfs_generate_new_key(crypt_stat);
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 797) rc = ecryptfs_init_crypt_ctx(crypt_stat);
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 798) if (rc)
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 799) ecryptfs_printk(KERN_ERR, "Error initializing cryptographic "
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 800) "context for cipher [%s]: rc = [%d]\n",
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 801) crypt_stat->cipher, rc);
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 802) out:
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 803) return rc;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 804) }
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 805)
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 806) /**
7a86617e553f4 (Tyler Hicks 2011-05-02 00:39:54 -0500 807) * ecryptfs_validate_marker - check for the ecryptfs marker
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 808) * @data: The data block in which to check
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 809) *
7a86617e553f4 (Tyler Hicks 2011-05-02 00:39:54 -0500 810) * Returns zero if marker found; -EINVAL if not found
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 811) */
7a86617e553f4 (Tyler Hicks 2011-05-02 00:39:54 -0500 812) static int ecryptfs_validate_marker(char *data)
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 813) {
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 814) u32 m_1, m_2;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 815)
29335c6a41568 (Harvey Harrison 2008-07-23 21:30:06 -0700 816) m_1 = get_unaligned_be32(data);
29335c6a41568 (Harvey Harrison 2008-07-23 21:30:06 -0700 817) m_2 = get_unaligned_be32(data + 4);
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 818) if ((m_1 ^ MAGIC_ECRYPTFS_MARKER) == m_2)
7a86617e553f4 (Tyler Hicks 2011-05-02 00:39:54 -0500 819) return 0;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 820) ecryptfs_printk(KERN_DEBUG, "m_1 = [0x%.8x]; m_2 = [0x%.8x]; "
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 821) "MAGIC_ECRYPTFS_MARKER = [0x%.8x]\n", m_1, m_2,
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 822) MAGIC_ECRYPTFS_MARKER);
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 823) ecryptfs_printk(KERN_DEBUG, "(m_1 ^ MAGIC_ECRYPTFS_MARKER) = "
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 824) "[0x%.8x]\n", (m_1 ^ MAGIC_ECRYPTFS_MARKER));
7a86617e553f4 (Tyler Hicks 2011-05-02 00:39:54 -0500 825) return -EINVAL;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 826) }
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 827)
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 828) struct ecryptfs_flag_map_elem {
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 829) u32 file_flag;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 830) u32 local_flag;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 831) };
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 832)
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 833) /* Add support for additional flags by adding elements here. */
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 834) static struct ecryptfs_flag_map_elem ecryptfs_flag_map[] = {
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 835) {0x00000001, ECRYPTFS_ENABLE_HMAC},
dd2a3b7ad98f8 (Michael Halcrow 2007-02-12 00:53:46 -0800 836) {0x00000002, ECRYPTFS_ENCRYPTED},
addd65ad8d19a (Michael Halcrow 2009-01-06 14:42:00 -0800 837) {0x00000004, ECRYPTFS_METADATA_IN_XATTR},
addd65ad8d19a (Michael Halcrow 2009-01-06 14:42:00 -0800 838) {0x00000008, ECRYPTFS_ENCRYPT_FILENAMES}
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 839) };
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 840)
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 841) /**
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 842) * ecryptfs_process_flags
22e78fafbdf84 (Michael Halcrow 2007-10-16 01:28:02 -0700 843) * @crypt_stat: The cryptographic context
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 844) * @page_virt: Source data to be parsed
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 845) * @bytes_read: Updated with the number of bytes read
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 846) */
7451c54abc913 (Hariprasad Kelam 2019-07-02 23:17:24 +0530 847) static void ecryptfs_process_flags(struct ecryptfs_crypt_stat *crypt_stat,
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 848) char *page_virt, int *bytes_read)
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 849) {
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 850) int i;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 851) u32 flags;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 852)
29335c6a41568 (Harvey Harrison 2008-07-23 21:30:06 -0700 853) flags = get_unaligned_be32(page_virt);
02f9876ebb5e9 (Jérémy Lefaure 2017-10-01 15:30:46 -0400 854) for (i = 0; i < ARRAY_SIZE(ecryptfs_flag_map); i++)
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 855) if (flags & ecryptfs_flag_map[i].file_flag) {
e2bd99ec5c0e2 (Michael Halcrow 2007-02-12 00:53:49 -0800 856) crypt_stat->flags |= ecryptfs_flag_map[i].local_flag;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 857) } else
e2bd99ec5c0e2 (Michael Halcrow 2007-02-12 00:53:49 -0800 858) crypt_stat->flags &= ~(ecryptfs_flag_map[i].local_flag);
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 859) /* Version is in top 8 bits of the 32-bit flag vector */
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 860) crypt_stat->file_version = ((flags >> 24) & 0xFF);
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 861) (*bytes_read) = 4;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 862) }
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 863)
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 864) /**
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 865) * write_ecryptfs_marker
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 866) * @page_virt: The pointer to in a page to begin writing the marker
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 867) * @written: Number of bytes written
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 868) *
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 869) * Marker = 0x3c81b7f5
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 870) */
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 871) static void write_ecryptfs_marker(char *page_virt, size_t *written)
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 872) {
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 873) u32 m_1, m_2;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 874)
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 875) get_random_bytes(&m_1, (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2));
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 876) m_2 = (m_1 ^ MAGIC_ECRYPTFS_MARKER);
29335c6a41568 (Harvey Harrison 2008-07-23 21:30:06 -0700 877) put_unaligned_be32(m_1, page_virt);
29335c6a41568 (Harvey Harrison 2008-07-23 21:30:06 -0700 878) page_virt += (MAGIC_ECRYPTFS_MARKER_SIZE_BYTES / 2);
29335c6a41568 (Harvey Harrison 2008-07-23 21:30:06 -0700 879) put_unaligned_be32(m_2, page_virt);
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 880) (*written) = MAGIC_ECRYPTFS_MARKER_SIZE_BYTES;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 881) }
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 882)
f4e60e6b303bc (Tyler Hicks 2010-02-11 00:02:32 -0600 883) void ecryptfs_write_crypt_stat_flags(char *page_virt,
f4e60e6b303bc (Tyler Hicks 2010-02-11 00:02:32 -0600 884) struct ecryptfs_crypt_stat *crypt_stat,
f4e60e6b303bc (Tyler Hicks 2010-02-11 00:02:32 -0600 885) size_t *written)
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 886) {
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 887) u32 flags = 0;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 888) int i;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 889)
02f9876ebb5e9 (Jérémy Lefaure 2017-10-01 15:30:46 -0400 890) for (i = 0; i < ARRAY_SIZE(ecryptfs_flag_map); i++)
e2bd99ec5c0e2 (Michael Halcrow 2007-02-12 00:53:49 -0800 891) if (crypt_stat->flags & ecryptfs_flag_map[i].local_flag)
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 892) flags |= ecryptfs_flag_map[i].file_flag;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 893) /* Version is in top 8 bits of the 32-bit flag vector */
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 894) flags |= ((((u8)crypt_stat->file_version) << 24) & 0xFF000000);
29335c6a41568 (Harvey Harrison 2008-07-23 21:30:06 -0700 895) put_unaligned_be32(flags, page_virt);
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 896) (*written) = 4;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 897) }
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 898)
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 899) struct ecryptfs_cipher_code_str_map_elem {
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 900) char cipher_str[16];
19e66a67e9b25 (Trevor Highland 2008-02-06 01:38:36 -0800 901) u8 cipher_code;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 902) };
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 903)
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 904) /* Add support for additional ciphers by adding elements here. The
40f0fd372a623 (Chris J Arges 2016-06-09 15:31:29 -0500 905) * cipher_code is whatever OpenPGP applications use to identify the
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 906) * ciphers. List in order of probability. */
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 907) static struct ecryptfs_cipher_code_str_map_elem
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 908) ecryptfs_cipher_code_str_map[] = {
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 909) {"aes",RFC2440_CIPHER_AES_128 },
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 910) {"blowfish", RFC2440_CIPHER_BLOWFISH},
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 911) {"des3_ede", RFC2440_CIPHER_DES3_EDE},
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 912) {"cast5", RFC2440_CIPHER_CAST_5},
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 913) {"twofish", RFC2440_CIPHER_TWOFISH},
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 914) {"cast6", RFC2440_CIPHER_CAST_6},
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 915) {"aes", RFC2440_CIPHER_AES_192},
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 916) {"aes", RFC2440_CIPHER_AES_256}
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 917) };
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 918)
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 919) /**
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 920) * ecryptfs_code_for_cipher_string
9c79f34f7ee71 (Michael Halcrow 2009-01-06 14:41:57 -0800 921) * @cipher_name: The string alias for the cipher
9c79f34f7ee71 (Michael Halcrow 2009-01-06 14:41:57 -0800 922) * @key_bytes: Length of key in bytes; used for AES code selection
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 923) *
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 924) * Returns zero on no match, or the cipher code on match
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 925) */
9c79f34f7ee71 (Michael Halcrow 2009-01-06 14:41:57 -0800 926) u8 ecryptfs_code_for_cipher_string(char *cipher_name, size_t key_bytes)
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 927) {
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 928) int i;
19e66a67e9b25 (Trevor Highland 2008-02-06 01:38:36 -0800 929) u8 code = 0;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 930) struct ecryptfs_cipher_code_str_map_elem *map =
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 931) ecryptfs_cipher_code_str_map;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 932)
9c79f34f7ee71 (Michael Halcrow 2009-01-06 14:41:57 -0800 933) if (strcmp(cipher_name, "aes") == 0) {
9c79f34f7ee71 (Michael Halcrow 2009-01-06 14:41:57 -0800 934) switch (key_bytes) {
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 935) case 16:
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 936) code = RFC2440_CIPHER_AES_128;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 937) break;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 938) case 24:
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 939) code = RFC2440_CIPHER_AES_192;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 940) break;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 941) case 32:
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 942) code = RFC2440_CIPHER_AES_256;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 943) }
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 944) } else {
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 945) for (i = 0; i < ARRAY_SIZE(ecryptfs_cipher_code_str_map); i++)
9c79f34f7ee71 (Michael Halcrow 2009-01-06 14:41:57 -0800 946) if (strcmp(cipher_name, map[i].cipher_str) == 0) {
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 947) code = map[i].cipher_code;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 948) break;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 949) }
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 950) }
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 951) return code;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 952) }
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 953)
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 954) /**
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 955) * ecryptfs_cipher_code_to_string
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 956) * @str: Destination to write out the cipher name
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 957) * @cipher_code: The code to convert to cipher name string
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 958) *
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 959) * Returns zero on success
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 960) */
19e66a67e9b25 (Trevor Highland 2008-02-06 01:38:36 -0800 961) int ecryptfs_cipher_code_to_string(char *str, u8 cipher_code)
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 962) {
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 963) int rc = 0;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 964) int i;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 965)
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 966) str[0] = '\0';
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 967) for (i = 0; i < ARRAY_SIZE(ecryptfs_cipher_code_str_map); i++)
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 968) if (cipher_code == ecryptfs_cipher_code_str_map[i].cipher_code)
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 969) strcpy(str, ecryptfs_cipher_code_str_map[i].cipher_str);
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 970) if (str[0] == '\0') {
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 971) ecryptfs_printk(KERN_WARNING, "Cipher code not recognized: "
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 972) "[%d]\n", cipher_code);
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 973) rc = -EINVAL;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 974) }
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 975) return rc;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 976) }
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 977)
778aeb42a708d (Tyler Hicks 2011-05-24 04:56:23 -0500 978) int ecryptfs_read_and_validate_header_region(struct inode *inode)
dd2a3b7ad98f8 (Michael Halcrow 2007-02-12 00:53:46 -0800 979) {
778aeb42a708d (Tyler Hicks 2011-05-24 04:56:23 -0500 980) u8 file_size[ECRYPTFS_SIZE_AND_MARKER_BYTES];
778aeb42a708d (Tyler Hicks 2011-05-24 04:56:23 -0500 981) u8 *marker = file_size + ECRYPTFS_FILE_SIZE_BYTES;
dd2a3b7ad98f8 (Michael Halcrow 2007-02-12 00:53:46 -0800 982) int rc;
dd2a3b7ad98f8 (Michael Halcrow 2007-02-12 00:53:46 -0800 983)
778aeb42a708d (Tyler Hicks 2011-05-24 04:56:23 -0500 984) rc = ecryptfs_read_lower(file_size, 0, ECRYPTFS_SIZE_AND_MARKER_BYTES,
778aeb42a708d (Tyler Hicks 2011-05-24 04:56:23 -0500 985) inode);
0bdf8a8245fde (Dan Carpenter 2018-07-04 12:35:56 +0300 986) if (rc < 0)
0bdf8a8245fde (Dan Carpenter 2018-07-04 12:35:56 +0300 987) return rc;
0bdf8a8245fde (Dan Carpenter 2018-07-04 12:35:56 +0300 988) else if (rc < ECRYPTFS_SIZE_AND_MARKER_BYTES)
0bdf8a8245fde (Dan Carpenter 2018-07-04 12:35:56 +0300 989) return -EINVAL;
778aeb42a708d (Tyler Hicks 2011-05-24 04:56:23 -0500 990) rc = ecryptfs_validate_marker(marker);
778aeb42a708d (Tyler Hicks 2011-05-24 04:56:23 -0500 991) if (!rc)
778aeb42a708d (Tyler Hicks 2011-05-24 04:56:23 -0500 992) ecryptfs_i_size_init(file_size, inode);
dd2a3b7ad98f8 (Michael Halcrow 2007-02-12 00:53:46 -0800 993) return rc;
dd2a3b7ad98f8 (Michael Halcrow 2007-02-12 00:53:46 -0800 994) }
dd2a3b7ad98f8 (Michael Halcrow 2007-02-12 00:53:46 -0800 995)
e77a56ddceeec (Michael Halcrow 2007-02-12 00:53:47 -0800 996) void
e77a56ddceeec (Michael Halcrow 2007-02-12 00:53:47 -0800 997) ecryptfs_write_header_metadata(char *virt,
e77a56ddceeec (Michael Halcrow 2007-02-12 00:53:47 -0800 998) struct ecryptfs_crypt_stat *crypt_stat,
e77a56ddceeec (Michael Halcrow 2007-02-12 00:53:47 -0800 999) size_t *written)
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1000) {
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1001) u32 header_extent_size;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1002) u16 num_header_extents_at_front;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1003)
45eaab79678b9 (Michael Halcrow 2007-10-16 01:28:05 -0700 1004) header_extent_size = (u32)crypt_stat->extent_size;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1005) num_header_extents_at_front =
fa3ef1cb4e6e9 (Tyler Hicks 2010-02-11 05:09:14 -0600 1006) (u16)(crypt_stat->metadata_size / crypt_stat->extent_size);
29335c6a41568 (Harvey Harrison 2008-07-23 21:30:06 -0700 1007) put_unaligned_be32(header_extent_size, virt);
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1008) virt += 4;
29335c6a41568 (Harvey Harrison 2008-07-23 21:30:06 -0700 1009) put_unaligned_be16(num_header_extents_at_front, virt);
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1010) (*written) = 6;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1011) }
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1012)
3063287053bca (Tyler Hicks 2011-05-24 05:11:12 -0500 1013) struct kmem_cache *ecryptfs_header_cache;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1014)
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1015) /**
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1016) * ecryptfs_write_headers_virt
22e78fafbdf84 (Michael Halcrow 2007-10-16 01:28:02 -0700 1017) * @page_virt: The virtual address to write the headers to
87b811c3f9655 (Eric Sandeen 2008-10-29 14:01:08 -0700 1018) * @max: The size of memory allocated at page_virt
22e78fafbdf84 (Michael Halcrow 2007-10-16 01:28:02 -0700 1019) * @size: Set to the number of bytes written by this function
22e78fafbdf84 (Michael Halcrow 2007-10-16 01:28:02 -0700 1020) * @crypt_stat: The cryptographic context
22e78fafbdf84 (Michael Halcrow 2007-10-16 01:28:02 -0700 1021) * @ecryptfs_dentry: The eCryptfs dentry
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1022) *
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1023) * Format version: 1
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1024) *
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1025) * Header Extent:
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1026) * Octets 0-7: Unencrypted file size (big-endian)
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1027) * Octets 8-15: eCryptfs special marker
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1028) * Octets 16-19: Flags
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1029) * Octet 16: File format version number (between 0 and 255)
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1030) * Octets 17-18: Reserved
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1031) * Octet 19: Bit 1 (lsb): Reserved
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1032) * Bit 2: Encrypted?
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1033) * Bits 3-8: Reserved
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1034) * Octets 20-23: Header extent size (big-endian)
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1035) * Octets 24-25: Number of header extents at front of file
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1036) * (big-endian)
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1037) * Octet 26: Begin RFC 2440 authentication token packet set
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1038) * Data Extent 0:
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1039) * Lower data (CBC encrypted)
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1040) * Data Extent 1:
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1041) * Lower data (CBC encrypted)
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1042) * ...
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1043) *
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1044) * Returns zero on success
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1045) */
87b811c3f9655 (Eric Sandeen 2008-10-29 14:01:08 -0700 1046) static int ecryptfs_write_headers_virt(char *page_virt, size_t max,
87b811c3f9655 (Eric Sandeen 2008-10-29 14:01:08 -0700 1047) size_t *size,
dd2a3b7ad98f8 (Michael Halcrow 2007-02-12 00:53:46 -0800 1048) struct ecryptfs_crypt_stat *crypt_stat,
dd2a3b7ad98f8 (Michael Halcrow 2007-02-12 00:53:46 -0800 1049) struct dentry *ecryptfs_dentry)
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1050) {
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1051) int rc;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1052) size_t written;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1053) size_t offset;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1054)
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1055) offset = ECRYPTFS_FILE_SIZE_BYTES;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1056) write_ecryptfs_marker((page_virt + offset), &written);
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1057) offset += written;
f4e60e6b303bc (Tyler Hicks 2010-02-11 00:02:32 -0600 1058) ecryptfs_write_crypt_stat_flags((page_virt + offset), crypt_stat,
f4e60e6b303bc (Tyler Hicks 2010-02-11 00:02:32 -0600 1059) &written);
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1060) offset += written;
e77a56ddceeec (Michael Halcrow 2007-02-12 00:53:47 -0800 1061) ecryptfs_write_header_metadata((page_virt + offset), crypt_stat,
e77a56ddceeec (Michael Halcrow 2007-02-12 00:53:47 -0800 1062) &written);
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1063) offset += written;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1064) rc = ecryptfs_generate_key_packet_set((page_virt + offset), crypt_stat,
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1065) ecryptfs_dentry, &written,
87b811c3f9655 (Eric Sandeen 2008-10-29 14:01:08 -0700 1066) max - offset);
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1067) if (rc)
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1068) ecryptfs_printk(KERN_WARNING, "Error generating key packet "
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1069) "set; rc = [%d]\n", rc);
dd2a3b7ad98f8 (Michael Halcrow 2007-02-12 00:53:46 -0800 1070) if (size) {
dd2a3b7ad98f8 (Michael Halcrow 2007-02-12 00:53:46 -0800 1071) offset += written;
dd2a3b7ad98f8 (Michael Halcrow 2007-02-12 00:53:46 -0800 1072) *size = offset;
dd2a3b7ad98f8 (Michael Halcrow 2007-02-12 00:53:46 -0800 1073) }
dd2a3b7ad98f8 (Michael Halcrow 2007-02-12 00:53:46 -0800 1074) return rc;
dd2a3b7ad98f8 (Michael Halcrow 2007-02-12 00:53:46 -0800 1075) }
dd2a3b7ad98f8 (Michael Halcrow 2007-02-12 00:53:46 -0800 1076)
22e78fafbdf84 (Michael Halcrow 2007-10-16 01:28:02 -0700 1077) static int
b59db43ad4434 (Tyler Hicks 2011-11-21 17:31:02 -0600 1078) ecryptfs_write_metadata_to_contents(struct inode *ecryptfs_inode,
8faece5f90672 (Tyler Hicks 2009-03-20 01:25:09 -0500 1079) char *virt, size_t virt_len)
dd2a3b7ad98f8 (Michael Halcrow 2007-02-12 00:53:46 -0800 1080) {
d7cdc5febf9f2 (Michael Halcrow 2007-10-16 01:28:10 -0700 1081) int rc;
dd2a3b7ad98f8 (Michael Halcrow 2007-02-12 00:53:46 -0800 1082)
b59db43ad4434 (Tyler Hicks 2011-11-21 17:31:02 -0600 1083) rc = ecryptfs_write_lower(ecryptfs_inode, virt,
8faece5f90672 (Tyler Hicks 2009-03-20 01:25:09 -0500 1084) 0, virt_len);
96a7b9c2f5df8 (Tyler Hicks 2009-09-16 19:04:20 -0500 1085) if (rc < 0)
d7cdc5febf9f2 (Michael Halcrow 2007-10-16 01:28:10 -0700 1086) printk(KERN_ERR "%s: Error attempting to write header "
96a7b9c2f5df8 (Tyler Hicks 2009-09-16 19:04:20 -0500 1087) "information to lower file; rc = [%d]\n", __func__, rc);
96a7b9c2f5df8 (Tyler Hicks 2009-09-16 19:04:20 -0500 1088) else
96a7b9c2f5df8 (Tyler Hicks 2009-09-16 19:04:20 -0500 1089) rc = 0;
70456600f42f8 (Michael Halcrow 2007-02-12 00:53:48 -0800 1090) return rc;
dd2a3b7ad98f8 (Michael Halcrow 2007-02-12 00:53:46 -0800 1091) }
dd2a3b7ad98f8 (Michael Halcrow 2007-02-12 00:53:46 -0800 1092)
22e78fafbdf84 (Michael Halcrow 2007-10-16 01:28:02 -0700 1093) static int
22e78fafbdf84 (Michael Halcrow 2007-10-16 01:28:02 -0700 1094) ecryptfs_write_metadata_to_xattr(struct dentry *ecryptfs_dentry,
3767e255b390d (Al Viro 2016-05-27 11:06:05 -0400 1095) struct inode *ecryptfs_inode,
22e78fafbdf84 (Michael Halcrow 2007-10-16 01:28:02 -0700 1096) char *page_virt, size_t size)
dd2a3b7ad98f8 (Michael Halcrow 2007-02-12 00:53:46 -0800 1097) {
dd2a3b7ad98f8 (Michael Halcrow 2007-02-12 00:53:46 -0800 1098) int rc;
d43388dea04b1 (Robbie Ko 2018-08-21 16:17:40 +0800 1099) struct dentry *lower_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry);
d43388dea04b1 (Robbie Ko 2018-08-21 16:17:40 +0800 1100) struct inode *lower_inode = d_inode(lower_dentry);
dd2a3b7ad98f8 (Michael Halcrow 2007-02-12 00:53:46 -0800 1101)
d43388dea04b1 (Robbie Ko 2018-08-21 16:17:40 +0800 1102) if (!(lower_inode->i_opflags & IOP_XATTR)) {
d43388dea04b1 (Robbie Ko 2018-08-21 16:17:40 +0800 1103) rc = -EOPNOTSUPP;
d43388dea04b1 (Robbie Ko 2018-08-21 16:17:40 +0800 1104) goto out;
d43388dea04b1 (Robbie Ko 2018-08-21 16:17:40 +0800 1105) }
dd2a3b7ad98f8 (Michael Halcrow 2007-02-12 00:53:46 -0800 1106)
d43388dea04b1 (Robbie Ko 2018-08-21 16:17:40 +0800 1107) inode_lock(lower_inode);
c7c7a1a18af4c (Tycho Andersen 2021-01-21 14:19:28 +0100 1108) rc = __vfs_setxattr(&init_user_ns, lower_dentry, lower_inode,
c7c7a1a18af4c (Tycho Andersen 2021-01-21 14:19:28 +0100 1109) ECRYPTFS_XATTR_NAME, page_virt, size, 0);
d43388dea04b1 (Robbie Ko 2018-08-21 16:17:40 +0800 1110) if (!rc && ecryptfs_inode)
d43388dea04b1 (Robbie Ko 2018-08-21 16:17:40 +0800 1111) fsstack_copy_attr_all(ecryptfs_inode, lower_inode);
d43388dea04b1 (Robbie Ko 2018-08-21 16:17:40 +0800 1112) inode_unlock(lower_inode);
d43388dea04b1 (Robbie Ko 2018-08-21 16:17:40 +0800 1113) out:
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1114) return rc;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1115) }
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1116)
8faece5f90672 (Tyler Hicks 2009-03-20 01:25:09 -0500 1117) static unsigned long ecryptfs_get_zeroed_pages(gfp_t gfp_mask,
8faece5f90672 (Tyler Hicks 2009-03-20 01:25:09 -0500 1118) unsigned int order)
8faece5f90672 (Tyler Hicks 2009-03-20 01:25:09 -0500 1119) {
8faece5f90672 (Tyler Hicks 2009-03-20 01:25:09 -0500 1120) struct page *page;
8faece5f90672 (Tyler Hicks 2009-03-20 01:25:09 -0500 1121)
8faece5f90672 (Tyler Hicks 2009-03-20 01:25:09 -0500 1122) page = alloc_pages(gfp_mask | __GFP_ZERO, order);
8faece5f90672 (Tyler Hicks 2009-03-20 01:25:09 -0500 1123) if (page)
8faece5f90672 (Tyler Hicks 2009-03-20 01:25:09 -0500 1124) return (unsigned long) page_address(page);
8faece5f90672 (Tyler Hicks 2009-03-20 01:25:09 -0500 1125) return 0;
8faece5f90672 (Tyler Hicks 2009-03-20 01:25:09 -0500 1126) }
8faece5f90672 (Tyler Hicks 2009-03-20 01:25:09 -0500 1127)
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1128) /**
dd2a3b7ad98f8 (Michael Halcrow 2007-02-12 00:53:46 -0800 1129) * ecryptfs_write_metadata
b59db43ad4434 (Tyler Hicks 2011-11-21 17:31:02 -0600 1130) * @ecryptfs_dentry: The eCryptfs dentry, which should be negative
b59db43ad4434 (Tyler Hicks 2011-11-21 17:31:02 -0600 1131) * @ecryptfs_inode: The newly created eCryptfs inode
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1132) *
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1133) * Write the file headers out. This will likely involve a userspace
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1134) * callout, in which the session key is encrypted with one or more
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1135) * public keys and/or the passphrase necessary to do the encryption is
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1136) * retrieved via a prompt. Exactly what happens at this point should
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1137) * be policy-dependent.
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1138) *
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1139) * Returns zero on success; non-zero on error
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1140) */
b59db43ad4434 (Tyler Hicks 2011-11-21 17:31:02 -0600 1141) int ecryptfs_write_metadata(struct dentry *ecryptfs_dentry,
b59db43ad4434 (Tyler Hicks 2011-11-21 17:31:02 -0600 1142) struct inode *ecryptfs_inode)
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1143) {
d7cdc5febf9f2 (Michael Halcrow 2007-10-16 01:28:10 -0700 1144) struct ecryptfs_crypt_stat *crypt_stat =
b59db43ad4434 (Tyler Hicks 2011-11-21 17:31:02 -0600 1145) &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
8faece5f90672 (Tyler Hicks 2009-03-20 01:25:09 -0500 1146) unsigned int order;
cc11beffdf80c (Michael Halcrow 2008-02-06 01:38:32 -0800 1147) char *virt;
8faece5f90672 (Tyler Hicks 2009-03-20 01:25:09 -0500 1148) size_t virt_len;
d7cdc5febf9f2 (Michael Halcrow 2007-10-16 01:28:10 -0700 1149) size_t size = 0;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1150) int rc = 0;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1151)
e2bd99ec5c0e2 (Michael Halcrow 2007-02-12 00:53:49 -0800 1152) if (likely(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) {
e2bd99ec5c0e2 (Michael Halcrow 2007-02-12 00:53:49 -0800 1153) if (!(crypt_stat->flags & ECRYPTFS_KEY_VALID)) {
d7cdc5febf9f2 (Michael Halcrow 2007-10-16 01:28:10 -0700 1154) printk(KERN_ERR "Key is invalid; bailing out\n");
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1155) rc = -EINVAL;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1156) goto out;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1157) }
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1158) } else {
cc11beffdf80c (Michael Halcrow 2008-02-06 01:38:32 -0800 1159) printk(KERN_WARNING "%s: Encrypted flag not set\n",
18d1dbf1d401e (Harvey Harrison 2008-04-29 00:59:48 -0700 1160) __func__);
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1161) rc = -EINVAL;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1162) goto out;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1163) }
fa3ef1cb4e6e9 (Tyler Hicks 2010-02-11 05:09:14 -0600 1164) virt_len = crypt_stat->metadata_size;
8faece5f90672 (Tyler Hicks 2009-03-20 01:25:09 -0500 1165) order = get_order(virt_len);
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1166) /* Released in this function */
8faece5f90672 (Tyler Hicks 2009-03-20 01:25:09 -0500 1167) virt = (char *)ecryptfs_get_zeroed_pages(GFP_KERNEL, order);
cc11beffdf80c (Michael Halcrow 2008-02-06 01:38:32 -0800 1168) if (!virt) {
18d1dbf1d401e (Harvey Harrison 2008-04-29 00:59:48 -0700 1169) printk(KERN_ERR "%s: Out of memory\n", __func__);
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1170) rc = -ENOMEM;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1171) goto out;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1172) }
bd4f0fe8bb7c7 (Tyler Hicks 2011-02-23 00:14:19 -0600 1173) /* Zeroed page ensures the in-header unencrypted i_size is set to 0 */
8faece5f90672 (Tyler Hicks 2009-03-20 01:25:09 -0500 1174) rc = ecryptfs_write_headers_virt(virt, virt_len, &size, crypt_stat,
8faece5f90672 (Tyler Hicks 2009-03-20 01:25:09 -0500 1175) ecryptfs_dentry);
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1176) if (unlikely(rc)) {
cc11beffdf80c (Michael Halcrow 2008-02-06 01:38:32 -0800 1177) printk(KERN_ERR "%s: Error whilst writing headers; rc = [%d]\n",
18d1dbf1d401e (Harvey Harrison 2008-04-29 00:59:48 -0700 1178) __func__, rc);
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1179) goto out_free;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1180) }
dd2a3b7ad98f8 (Michael Halcrow 2007-02-12 00:53:46 -0800 1181) if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
3767e255b390d (Al Viro 2016-05-27 11:06:05 -0400 1182) rc = ecryptfs_write_metadata_to_xattr(ecryptfs_dentry, ecryptfs_inode,
3767e255b390d (Al Viro 2016-05-27 11:06:05 -0400 1183) virt, size);
dd2a3b7ad98f8 (Michael Halcrow 2007-02-12 00:53:46 -0800 1184) else
b59db43ad4434 (Tyler Hicks 2011-11-21 17:31:02 -0600 1185) rc = ecryptfs_write_metadata_to_contents(ecryptfs_inode, virt,
8faece5f90672 (Tyler Hicks 2009-03-20 01:25:09 -0500 1186) virt_len);
dd2a3b7ad98f8 (Michael Halcrow 2007-02-12 00:53:46 -0800 1187) if (rc) {
cc11beffdf80c (Michael Halcrow 2008-02-06 01:38:32 -0800 1188) printk(KERN_ERR "%s: Error writing metadata out to lower file; "
18d1dbf1d401e (Harvey Harrison 2008-04-29 00:59:48 -0700 1189) "rc = [%d]\n", __func__, rc);
dd2a3b7ad98f8 (Michael Halcrow 2007-02-12 00:53:46 -0800 1190) goto out_free;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1191) }
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1192) out_free:
8faece5f90672 (Tyler Hicks 2009-03-20 01:25:09 -0500 1193) free_pages((unsigned long)virt, order);
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1194) out:
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1195) return rc;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1196) }
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1197)
dd2a3b7ad98f8 (Michael Halcrow 2007-02-12 00:53:46 -0800 1198) #define ECRYPTFS_DONT_VALIDATE_HEADER_SIZE 0
dd2a3b7ad98f8 (Michael Halcrow 2007-02-12 00:53:46 -0800 1199) #define ECRYPTFS_VALIDATE_HEADER_SIZE 1
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1200) static int parse_header_metadata(struct ecryptfs_crypt_stat *crypt_stat,
dd2a3b7ad98f8 (Michael Halcrow 2007-02-12 00:53:46 -0800 1201) char *virt, int *bytes_read,
dd2a3b7ad98f8 (Michael Halcrow 2007-02-12 00:53:46 -0800 1202) int validate_header_size)
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1203) {
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1204) int rc = 0;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1205) u32 header_extent_size;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1206) u16 num_header_extents_at_front;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1207)
29335c6a41568 (Harvey Harrison 2008-07-23 21:30:06 -0700 1208) header_extent_size = get_unaligned_be32(virt);
29335c6a41568 (Harvey Harrison 2008-07-23 21:30:06 -0700 1209) virt += sizeof(__be32);
29335c6a41568 (Harvey Harrison 2008-07-23 21:30:06 -0700 1210) num_header_extents_at_front = get_unaligned_be16(virt);
fa3ef1cb4e6e9 (Tyler Hicks 2010-02-11 05:09:14 -0600 1211) crypt_stat->metadata_size = (((size_t)num_header_extents_at_front
fa3ef1cb4e6e9 (Tyler Hicks 2010-02-11 05:09:14 -0600 1212) * (size_t)header_extent_size));
29335c6a41568 (Harvey Harrison 2008-07-23 21:30:06 -0700 1213) (*bytes_read) = (sizeof(__be32) + sizeof(__be16));
dd2a3b7ad98f8 (Michael Halcrow 2007-02-12 00:53:46 -0800 1214) if ((validate_header_size == ECRYPTFS_VALIDATE_HEADER_SIZE)
fa3ef1cb4e6e9 (Tyler Hicks 2010-02-11 05:09:14 -0600 1215) && (crypt_stat->metadata_size
dd2a3b7ad98f8 (Michael Halcrow 2007-02-12 00:53:46 -0800 1216) < ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE)) {
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1217) rc = -EINVAL;
cc11beffdf80c (Michael Halcrow 2008-02-06 01:38:32 -0800 1218) printk(KERN_WARNING "Invalid header size: [%zd]\n",
fa3ef1cb4e6e9 (Tyler Hicks 2010-02-11 05:09:14 -0600 1219) crypt_stat->metadata_size);
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1220) }
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1221) return rc;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1222) }
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1223)
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1224) /**
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1225) * set_default_header_data
22e78fafbdf84 (Michael Halcrow 2007-10-16 01:28:02 -0700 1226) * @crypt_stat: The cryptographic context
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1227) *
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1228) * For version 0 file format; this function is only for backwards
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1229) * compatibility for files created with the prior versions of
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1230) * eCryptfs.
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1231) */
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1232) static void set_default_header_data(struct ecryptfs_crypt_stat *crypt_stat)
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1233) {
fa3ef1cb4e6e9 (Tyler Hicks 2010-02-11 05:09:14 -0600 1234) crypt_stat->metadata_size = ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1235) }
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1236)
3aeb86ea4cd15 (Tyler Hicks 2011-03-15 14:54:00 -0500 1237) void ecryptfs_i_size_init(const char *page_virt, struct inode *inode)
3aeb86ea4cd15 (Tyler Hicks 2011-03-15 14:54:00 -0500 1238) {
3aeb86ea4cd15 (Tyler Hicks 2011-03-15 14:54:00 -0500 1239) struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
3aeb86ea4cd15 (Tyler Hicks 2011-03-15 14:54:00 -0500 1240) struct ecryptfs_crypt_stat *crypt_stat;
3aeb86ea4cd15 (Tyler Hicks 2011-03-15 14:54:00 -0500 1241) u64 file_size;
3aeb86ea4cd15 (Tyler Hicks 2011-03-15 14:54:00 -0500 1242)
3aeb86ea4cd15 (Tyler Hicks 2011-03-15 14:54:00 -0500 1243) crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat;
3aeb86ea4cd15 (Tyler Hicks 2011-03-15 14:54:00 -0500 1244) mount_crypt_stat =
3aeb86ea4cd15 (Tyler Hicks 2011-03-15 14:54:00 -0500 1245) &ecryptfs_superblock_to_private(inode->i_sb)->mount_crypt_stat;
3aeb86ea4cd15 (Tyler Hicks 2011-03-15 14:54:00 -0500 1246) if (mount_crypt_stat->flags & ECRYPTFS_ENCRYPTED_VIEW_ENABLED) {
3aeb86ea4cd15 (Tyler Hicks 2011-03-15 14:54:00 -0500 1247) file_size = i_size_read(ecryptfs_inode_to_lower(inode));
3aeb86ea4cd15 (Tyler Hicks 2011-03-15 14:54:00 -0500 1248) if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
3aeb86ea4cd15 (Tyler Hicks 2011-03-15 14:54:00 -0500 1249) file_size += crypt_stat->metadata_size;
3aeb86ea4cd15 (Tyler Hicks 2011-03-15 14:54:00 -0500 1250) } else
3aeb86ea4cd15 (Tyler Hicks 2011-03-15 14:54:00 -0500 1251) file_size = get_unaligned_be64(page_virt);
3aeb86ea4cd15 (Tyler Hicks 2011-03-15 14:54:00 -0500 1252) i_size_write(inode, (loff_t)file_size);
3aeb86ea4cd15 (Tyler Hicks 2011-03-15 14:54:00 -0500 1253) crypt_stat->flags |= ECRYPTFS_I_SIZE_INITIALIZED;
3aeb86ea4cd15 (Tyler Hicks 2011-03-15 14:54:00 -0500 1254) }
3aeb86ea4cd15 (Tyler Hicks 2011-03-15 14:54:00 -0500 1255)
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1256) /**
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1257) * ecryptfs_read_headers_virt
22e78fafbdf84 (Michael Halcrow 2007-10-16 01:28:02 -0700 1258) * @page_virt: The virtual address into which to read the headers
22e78fafbdf84 (Michael Halcrow 2007-10-16 01:28:02 -0700 1259) * @crypt_stat: The cryptographic context
22e78fafbdf84 (Michael Halcrow 2007-10-16 01:28:02 -0700 1260) * @ecryptfs_dentry: The eCryptfs dentry
22e78fafbdf84 (Michael Halcrow 2007-10-16 01:28:02 -0700 1261) * @validate_header_size: Whether to validate the header size while reading
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1262) *
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1263) * Read/parse the header data. The header format is detailed in the
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1264) * comment block for the ecryptfs_write_headers_virt() function.
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1265) *
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1266) * Returns zero on success
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1267) */
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1268) static int ecryptfs_read_headers_virt(char *page_virt,
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1269) struct ecryptfs_crypt_stat *crypt_stat,
dd2a3b7ad98f8 (Michael Halcrow 2007-02-12 00:53:46 -0800 1270) struct dentry *ecryptfs_dentry,
dd2a3b7ad98f8 (Michael Halcrow 2007-02-12 00:53:46 -0800 1271) int validate_header_size)
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1272) {
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1273) int rc = 0;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1274) int offset;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1275) int bytes_read;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1276)
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1277) ecryptfs_set_default_sizes(crypt_stat);
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1278) crypt_stat->mount_crypt_stat = &ecryptfs_superblock_to_private(
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1279) ecryptfs_dentry->d_sb)->mount_crypt_stat;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1280) offset = ECRYPTFS_FILE_SIZE_BYTES;
7a86617e553f4 (Tyler Hicks 2011-05-02 00:39:54 -0500 1281) rc = ecryptfs_validate_marker(page_virt + offset);
7a86617e553f4 (Tyler Hicks 2011-05-02 00:39:54 -0500 1282) if (rc)
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1283) goto out;
3aeb86ea4cd15 (Tyler Hicks 2011-03-15 14:54:00 -0500 1284) if (!(crypt_stat->flags & ECRYPTFS_I_SIZE_INITIALIZED))
2b0143b5c986b (David Howells 2015-03-17 22:25:59 +0000 1285) ecryptfs_i_size_init(page_virt, d_inode(ecryptfs_dentry));
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1286) offset += MAGIC_ECRYPTFS_MARKER_SIZE_BYTES;
7451c54abc913 (Hariprasad Kelam 2019-07-02 23:17:24 +0530 1287) ecryptfs_process_flags(crypt_stat, (page_virt + offset), &bytes_read);
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1288) if (crypt_stat->file_version > ECRYPTFS_SUPPORTED_FILE_VERSION) {
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1289) ecryptfs_printk(KERN_WARNING, "File version is [%d]; only "
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1290) "file version [%d] is supported by this "
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1291) "version of eCryptfs\n",
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1292) crypt_stat->file_version,
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1293) ECRYPTFS_SUPPORTED_FILE_VERSION);
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1294) rc = -EINVAL;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1295) goto out;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1296) }
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1297) offset += bytes_read;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1298) if (crypt_stat->file_version >= 1) {
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1299) rc = parse_header_metadata(crypt_stat, (page_virt + offset),
dd2a3b7ad98f8 (Michael Halcrow 2007-02-12 00:53:46 -0800 1300) &bytes_read, validate_header_size);
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1301) if (rc) {
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1302) ecryptfs_printk(KERN_WARNING, "Error reading header "
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1303) "metadata; rc = [%d]\n", rc);
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1304) }
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1305) offset += bytes_read;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1306) } else
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1307) set_default_header_data(crypt_stat);
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1308) rc = ecryptfs_parse_packet_set(crypt_stat, (page_virt + offset),
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1309) ecryptfs_dentry);
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1310) out:
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1311) return rc;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1312) }
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1313)
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1314) /**
dd2a3b7ad98f8 (Michael Halcrow 2007-02-12 00:53:46 -0800 1315) * ecryptfs_read_xattr_region
22e78fafbdf84 (Michael Halcrow 2007-10-16 01:28:02 -0700 1316) * @page_virt: The vitual address into which to read the xattr data
2ed92554abc5c (Michael Halcrow 2007-10-16 01:28:10 -0700 1317) * @ecryptfs_inode: The eCryptfs inode
dd2a3b7ad98f8 (Michael Halcrow 2007-02-12 00:53:46 -0800 1318) *
dd2a3b7ad98f8 (Michael Halcrow 2007-02-12 00:53:46 -0800 1319) * Attempts to read the crypto metadata from the extended attribute
dd2a3b7ad98f8 (Michael Halcrow 2007-02-12 00:53:46 -0800 1320) * region of the lower file.
22e78fafbdf84 (Michael Halcrow 2007-10-16 01:28:02 -0700 1321) *
22e78fafbdf84 (Michael Halcrow 2007-10-16 01:28:02 -0700 1322) * Returns zero on success; non-zero on error
dd2a3b7ad98f8 (Michael Halcrow 2007-02-12 00:53:46 -0800 1323) */
d7cdc5febf9f2 (Michael Halcrow 2007-10-16 01:28:10 -0700 1324) int ecryptfs_read_xattr_region(char *page_virt, struct inode *ecryptfs_inode)
dd2a3b7ad98f8 (Michael Halcrow 2007-02-12 00:53:46 -0800 1325) {
d7cdc5febf9f2 (Michael Halcrow 2007-10-16 01:28:10 -0700 1326) struct dentry *lower_dentry =
b583043e99bc6 (Al Viro 2014-10-31 01:22:04 -0400 1327) ecryptfs_inode_to_private(ecryptfs_inode)->lower_file->f_path.dentry;
dd2a3b7ad98f8 (Michael Halcrow 2007-02-12 00:53:46 -0800 1328) ssize_t size;
dd2a3b7ad98f8 (Michael Halcrow 2007-02-12 00:53:46 -0800 1329) int rc = 0;
dd2a3b7ad98f8 (Michael Halcrow 2007-02-12 00:53:46 -0800 1330)
ce23e64013348 (Al Viro 2016-04-11 00:48:00 -0400 1331) size = ecryptfs_getxattr_lower(lower_dentry,
ce23e64013348 (Al Viro 2016-04-11 00:48:00 -0400 1332) ecryptfs_inode_to_lower(ecryptfs_inode),
ce23e64013348 (Al Viro 2016-04-11 00:48:00 -0400 1333) ECRYPTFS_XATTR_NAME,
d7cdc5febf9f2 (Michael Halcrow 2007-10-16 01:28:10 -0700 1334) page_virt, ECRYPTFS_DEFAULT_EXTENT_SIZE);
dd2a3b7ad98f8 (Michael Halcrow 2007-02-12 00:53:46 -0800 1335) if (size < 0) {
25bd817403603 (Michael Halcrow 2008-02-06 01:38:35 -0800 1336) if (unlikely(ecryptfs_verbosity > 0))
25bd817403603 (Michael Halcrow 2008-02-06 01:38:35 -0800 1337) printk(KERN_INFO "Error attempting to read the [%s] "
25bd817403603 (Michael Halcrow 2008-02-06 01:38:35 -0800 1338) "xattr from the lower file; return value = "
25bd817403603 (Michael Halcrow 2008-02-06 01:38:35 -0800 1339) "[%zd]\n", ECRYPTFS_XATTR_NAME, size);
dd2a3b7ad98f8 (Michael Halcrow 2007-02-12 00:53:46 -0800 1340) rc = -EINVAL;
dd2a3b7ad98f8 (Michael Halcrow 2007-02-12 00:53:46 -0800 1341) goto out;
dd2a3b7ad98f8 (Michael Halcrow 2007-02-12 00:53:46 -0800 1342) }
dd2a3b7ad98f8 (Michael Halcrow 2007-02-12 00:53:46 -0800 1343) out:
dd2a3b7ad98f8 (Michael Halcrow 2007-02-12 00:53:46 -0800 1344) return rc;
dd2a3b7ad98f8 (Michael Halcrow 2007-02-12 00:53:46 -0800 1345) }
dd2a3b7ad98f8 (Michael Halcrow 2007-02-12 00:53:46 -0800 1346)
778aeb42a708d (Tyler Hicks 2011-05-24 04:56:23 -0500 1347) int ecryptfs_read_and_validate_xattr_region(struct dentry *dentry,
3b06b3ebf4417 (Tyler Hicks 2011-05-24 03:49:02 -0500 1348) struct inode *inode)
dd2a3b7ad98f8 (Michael Halcrow 2007-02-12 00:53:46 -0800 1349) {
778aeb42a708d (Tyler Hicks 2011-05-24 04:56:23 -0500 1350) u8 file_size[ECRYPTFS_SIZE_AND_MARKER_BYTES];
778aeb42a708d (Tyler Hicks 2011-05-24 04:56:23 -0500 1351) u8 *marker = file_size + ECRYPTFS_FILE_SIZE_BYTES;
dd2a3b7ad98f8 (Michael Halcrow 2007-02-12 00:53:46 -0800 1352) int rc;
dd2a3b7ad98f8 (Michael Halcrow 2007-02-12 00:53:46 -0800 1353)
778aeb42a708d (Tyler Hicks 2011-05-24 04:56:23 -0500 1354) rc = ecryptfs_getxattr_lower(ecryptfs_dentry_to_lower(dentry),
ce23e64013348 (Al Viro 2016-04-11 00:48:00 -0400 1355) ecryptfs_inode_to_lower(inode),
778aeb42a708d (Tyler Hicks 2011-05-24 04:56:23 -0500 1356) ECRYPTFS_XATTR_NAME, file_size,
778aeb42a708d (Tyler Hicks 2011-05-24 04:56:23 -0500 1357) ECRYPTFS_SIZE_AND_MARKER_BYTES);
0bdf8a8245fde (Dan Carpenter 2018-07-04 12:35:56 +0300 1358) if (rc < 0)
0bdf8a8245fde (Dan Carpenter 2018-07-04 12:35:56 +0300 1359) return rc;
0bdf8a8245fde (Dan Carpenter 2018-07-04 12:35:56 +0300 1360) else if (rc < ECRYPTFS_SIZE_AND_MARKER_BYTES)
0bdf8a8245fde (Dan Carpenter 2018-07-04 12:35:56 +0300 1361) return -EINVAL;
778aeb42a708d (Tyler Hicks 2011-05-24 04:56:23 -0500 1362) rc = ecryptfs_validate_marker(marker);
778aeb42a708d (Tyler Hicks 2011-05-24 04:56:23 -0500 1363) if (!rc)
778aeb42a708d (Tyler Hicks 2011-05-24 04:56:23 -0500 1364) ecryptfs_i_size_init(file_size, inode);
dd2a3b7ad98f8 (Michael Halcrow 2007-02-12 00:53:46 -0800 1365) return rc;
dd2a3b7ad98f8 (Michael Halcrow 2007-02-12 00:53:46 -0800 1366) }
dd2a3b7ad98f8 (Michael Halcrow 2007-02-12 00:53:46 -0800 1367)
5da877eadffb8 (Lee Jones 2021-03-30 17:44:55 +0100 1368) /*
dd2a3b7ad98f8 (Michael Halcrow 2007-02-12 00:53:46 -0800 1369) * ecryptfs_read_metadata
dd2a3b7ad98f8 (Michael Halcrow 2007-02-12 00:53:46 -0800 1370) *
dd2a3b7ad98f8 (Michael Halcrow 2007-02-12 00:53:46 -0800 1371) * Common entry point for reading file metadata. From here, we could
dd2a3b7ad98f8 (Michael Halcrow 2007-02-12 00:53:46 -0800 1372) * retrieve the header information from the header region of the file,
40f0fd372a623 (Chris J Arges 2016-06-09 15:31:29 -0500 1373) * the xattr region of the file, or some other repository that is
dd2a3b7ad98f8 (Michael Halcrow 2007-02-12 00:53:46 -0800 1374) * stored separately from the file itself. The current implementation
dd2a3b7ad98f8 (Michael Halcrow 2007-02-12 00:53:46 -0800 1375) * supports retrieving the metadata information from the file contents
dd2a3b7ad98f8 (Michael Halcrow 2007-02-12 00:53:46 -0800 1376) * and from the xattr region.
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1377) *
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1378) * Returns zero if valid headers found and parsed; non-zero otherwise
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1379) */
d7cdc5febf9f2 (Michael Halcrow 2007-10-16 01:28:10 -0700 1380) int ecryptfs_read_metadata(struct dentry *ecryptfs_dentry)
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1381) {
bb4503615d95d (Tim Gardner 2012-01-12 16:31:55 +0100 1382) int rc;
bb4503615d95d (Tim Gardner 2012-01-12 16:31:55 +0100 1383) char *page_virt;
2b0143b5c986b (David Howells 2015-03-17 22:25:59 +0000 1384) struct inode *ecryptfs_inode = d_inode(ecryptfs_dentry);
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1385) struct ecryptfs_crypt_stat *crypt_stat =
d7cdc5febf9f2 (Michael Halcrow 2007-10-16 01:28:10 -0700 1386) &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
e77a56ddceeec (Michael Halcrow 2007-02-12 00:53:47 -0800 1387) struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
e77a56ddceeec (Michael Halcrow 2007-02-12 00:53:47 -0800 1388) &ecryptfs_superblock_to_private(
e77a56ddceeec (Michael Halcrow 2007-02-12 00:53:47 -0800 1389) ecryptfs_dentry->d_sb)->mount_crypt_stat;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1390)
e77a56ddceeec (Michael Halcrow 2007-02-12 00:53:47 -0800 1391) ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat,
e77a56ddceeec (Michael Halcrow 2007-02-12 00:53:47 -0800 1392) mount_crypt_stat);
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1393) /* Read the first page from the underlying file */
3063287053bca (Tyler Hicks 2011-05-24 05:11:12 -0500 1394) page_virt = kmem_cache_alloc(ecryptfs_header_cache, GFP_USER);
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1395) if (!page_virt) {
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1396) rc = -ENOMEM;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1397) goto out;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1398) }
d7cdc5febf9f2 (Michael Halcrow 2007-10-16 01:28:10 -0700 1399) rc = ecryptfs_read_lower(page_virt, 0, crypt_stat->extent_size,
d7cdc5febf9f2 (Michael Halcrow 2007-10-16 01:28:10 -0700 1400) ecryptfs_inode);
96a7b9c2f5df8 (Tyler Hicks 2009-09-16 19:04:20 -0500 1401) if (rc >= 0)
d7cdc5febf9f2 (Michael Halcrow 2007-10-16 01:28:10 -0700 1402) rc = ecryptfs_read_headers_virt(page_virt, crypt_stat,
d7cdc5febf9f2 (Michael Halcrow 2007-10-16 01:28:10 -0700 1403) ecryptfs_dentry,
d7cdc5febf9f2 (Michael Halcrow 2007-10-16 01:28:10 -0700 1404) ECRYPTFS_VALIDATE_HEADER_SIZE);
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1405) if (rc) {
bb4503615d95d (Tim Gardner 2012-01-12 16:31:55 +0100 1406) /* metadata is not in the file header, so try xattrs */
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 1407) memset(page_virt, 0, PAGE_SIZE);
d7cdc5febf9f2 (Michael Halcrow 2007-10-16 01:28:10 -0700 1408) rc = ecryptfs_read_xattr_region(page_virt, ecryptfs_inode);
dd2a3b7ad98f8 (Michael Halcrow 2007-02-12 00:53:46 -0800 1409) if (rc) {
dd2a3b7ad98f8 (Michael Halcrow 2007-02-12 00:53:46 -0800 1410) printk(KERN_DEBUG "Valid eCryptfs headers not found in "
30373dc0c87ff (Tim Gardner 2012-01-12 16:31:55 +0100 1411) "file header region or xattr region, inode %lu\n",
30373dc0c87ff (Tim Gardner 2012-01-12 16:31:55 +0100 1412) ecryptfs_inode->i_ino);
dd2a3b7ad98f8 (Michael Halcrow 2007-02-12 00:53:46 -0800 1413) rc = -EINVAL;
dd2a3b7ad98f8 (Michael Halcrow 2007-02-12 00:53:46 -0800 1414) goto out;
dd2a3b7ad98f8 (Michael Halcrow 2007-02-12 00:53:46 -0800 1415) }
dd2a3b7ad98f8 (Michael Halcrow 2007-02-12 00:53:46 -0800 1416) rc = ecryptfs_read_headers_virt(page_virt, crypt_stat,
dd2a3b7ad98f8 (Michael Halcrow 2007-02-12 00:53:46 -0800 1417) ecryptfs_dentry,
dd2a3b7ad98f8 (Michael Halcrow 2007-02-12 00:53:46 -0800 1418) ECRYPTFS_DONT_VALIDATE_HEADER_SIZE);
dd2a3b7ad98f8 (Michael Halcrow 2007-02-12 00:53:46 -0800 1419) if (rc) {
dd2a3b7ad98f8 (Michael Halcrow 2007-02-12 00:53:46 -0800 1420) printk(KERN_DEBUG "Valid eCryptfs headers not found in "
30373dc0c87ff (Tim Gardner 2012-01-12 16:31:55 +0100 1421) "file xattr region either, inode %lu\n",
30373dc0c87ff (Tim Gardner 2012-01-12 16:31:55 +0100 1422) ecryptfs_inode->i_ino);
dd2a3b7ad98f8 (Michael Halcrow 2007-02-12 00:53:46 -0800 1423) rc = -EINVAL;
dd2a3b7ad98f8 (Michael Halcrow 2007-02-12 00:53:46 -0800 1424) }
dd2a3b7ad98f8 (Michael Halcrow 2007-02-12 00:53:46 -0800 1425) if (crypt_stat->mount_crypt_stat->flags
dd2a3b7ad98f8 (Michael Halcrow 2007-02-12 00:53:46 -0800 1426) & ECRYPTFS_XATTR_METADATA_ENABLED) {
dd2a3b7ad98f8 (Michael Halcrow 2007-02-12 00:53:46 -0800 1427) crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR;
dd2a3b7ad98f8 (Michael Halcrow 2007-02-12 00:53:46 -0800 1428) } else {
dd2a3b7ad98f8 (Michael Halcrow 2007-02-12 00:53:46 -0800 1429) printk(KERN_WARNING "Attempt to access file with "
dd2a3b7ad98f8 (Michael Halcrow 2007-02-12 00:53:46 -0800 1430) "crypto metadata only in the extended attribute "
dd2a3b7ad98f8 (Michael Halcrow 2007-02-12 00:53:46 -0800 1431) "region, but eCryptfs was mounted without "
dd2a3b7ad98f8 (Michael Halcrow 2007-02-12 00:53:46 -0800 1432) "xattr support enabled. eCryptfs will not treat "
30373dc0c87ff (Tim Gardner 2012-01-12 16:31:55 +0100 1433) "this like an encrypted file, inode %lu\n",
30373dc0c87ff (Tim Gardner 2012-01-12 16:31:55 +0100 1434) ecryptfs_inode->i_ino);
dd2a3b7ad98f8 (Michael Halcrow 2007-02-12 00:53:46 -0800 1435) rc = -EINVAL;
dd2a3b7ad98f8 (Michael Halcrow 2007-02-12 00:53:46 -0800 1436) }
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1437) }
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1438) out:
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1439) if (page_virt) {
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 1440) memset(page_virt, 0, PAGE_SIZE);
3063287053bca (Tyler Hicks 2011-05-24 05:11:12 -0500 1441) kmem_cache_free(ecryptfs_header_cache, page_virt);
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1442) }
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1443) return rc;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1444) }
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1445)
5da877eadffb8 (Lee Jones 2021-03-30 17:44:55 +0100 1446) /*
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1447) * ecryptfs_encrypt_filename - encrypt filename
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1448) *
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1449) * CBC-encrypts the filename. We do not want to encrypt the same
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1450) * filename with the same key and IV, which may happen with hard
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1451) * links, so we prepend random bits to each filename.
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1452) *
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1453) * Returns zero on success; non-zero otherwise
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1454) */
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1455) static int
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1456) ecryptfs_encrypt_filename(struct ecryptfs_filename *filename,
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1457) struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1458) {
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1459) int rc = 0;
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1460)
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1461) filename->encrypted_filename = NULL;
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1462) filename->encrypted_filename_size = 0;
97c31606075b3 (Al Viro 2016-02-22 18:14:25 -0500 1463) if (mount_crypt_stat && (mount_crypt_stat->flags
97c31606075b3 (Al Viro 2016-02-22 18:14:25 -0500 1464) & ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK)) {
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1465) size_t packet_size;
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1466) size_t remaining_bytes;
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1467)
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1468) rc = ecryptfs_write_tag_70_packet(
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1469) NULL, NULL,
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1470) &filename->encrypted_filename_size,
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1471) mount_crypt_stat, NULL,
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1472) filename->filename_size);
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1473) if (rc) {
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1474) printk(KERN_ERR "%s: Error attempting to get packet "
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1475) "size for tag 72; rc = [%d]\n", __func__,
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1476) rc);
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1477) filename->encrypted_filename_size = 0;
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1478) goto out;
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1479) }
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1480) filename->encrypted_filename =
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1481) kmalloc(filename->encrypted_filename_size, GFP_KERNEL);
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1482) if (!filename->encrypted_filename) {
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1483) rc = -ENOMEM;
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1484) goto out;
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1485) }
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1486) remaining_bytes = filename->encrypted_filename_size;
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1487) rc = ecryptfs_write_tag_70_packet(filename->encrypted_filename,
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1488) &remaining_bytes,
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1489) &packet_size,
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1490) mount_crypt_stat,
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1491) filename->filename,
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1492) filename->filename_size);
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1493) if (rc) {
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1494) printk(KERN_ERR "%s: Error attempting to generate "
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1495) "tag 70 packet; rc = [%d]\n", __func__,
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1496) rc);
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1497) kfree(filename->encrypted_filename);
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1498) filename->encrypted_filename = NULL;
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1499) filename->encrypted_filename_size = 0;
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1500) goto out;
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1501) }
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1502) filename->encrypted_filename_size = packet_size;
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1503) } else {
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1504) printk(KERN_ERR "%s: No support for requested filename "
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1505) "encryption method in this release\n", __func__);
df6ad33ba1b98 (Tyler Hicks 2009-08-21 04:27:46 -0500 1506) rc = -EOPNOTSUPP;
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1507) goto out;
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1508) }
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1509) out:
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1510) return rc;
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1511) }
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1512)
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1513) static int ecryptfs_copy_filename(char **copied_name, size_t *copied_name_size,
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1514) const char *name, size_t name_size)
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1515) {
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1516) int rc = 0;
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1517)
fd9fc842bbab0 (Tyler Hicks 2009-02-06 18:06:51 -0600 1518) (*copied_name) = kmalloc((name_size + 1), GFP_KERNEL);
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1519) if (!(*copied_name)) {
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1520) rc = -ENOMEM;
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1521) goto out;
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1522) }
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1523) memcpy((void *)(*copied_name), (void *)name, name_size);
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1524) (*copied_name)[(name_size)] = '\0'; /* Only for convenience
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1525) * in printing out the
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1526) * string in debug
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1527) * messages */
fd9fc842bbab0 (Tyler Hicks 2009-02-06 18:06:51 -0600 1528) (*copied_name_size) = name_size;
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1529) out:
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1530) return rc;
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1531) }
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1532)
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1533) /**
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 1534) * ecryptfs_process_key_cipher - Perform key cipher initialization.
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1535) * @key_tfm: Crypto context for key material, set by this function
e5d9cbde6ce00 (Michael Halcrow 2006-10-30 22:07:16 -0800 1536) * @cipher_name: Name of the cipher
e5d9cbde6ce00 (Michael Halcrow 2006-10-30 22:07:16 -0800 1537) * @key_size: Size of the key in bytes
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1538) *
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1539) * Returns zero on success. Any crypto_tfm structs allocated here
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1540) * should be released by other functions, such as on a superblock put
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1541) * event, regardless of whether this function succeeds for fails.
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1542) */
cd9d67dfd2b4d (Michael Halcrow 2007-10-16 01:28:04 -0700 1543) static int
3095e8e366b47 (Herbert Xu 2016-01-25 10:29:33 +0800 1544) ecryptfs_process_key_cipher(struct crypto_skcipher **key_tfm,
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 1545) char *cipher_name, size_t *key_size)
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1546) {
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1547) char dummy_key[ECRYPTFS_MAX_KEY_BYTES];
ece550f51ba17 (Dan Carpenter 2010-01-19 12:34:32 +0300 1548) char *full_alg_name = NULL;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1549) int rc;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1550)
e5d9cbde6ce00 (Michael Halcrow 2006-10-30 22:07:16 -0800 1551) *key_tfm = NULL;
e5d9cbde6ce00 (Michael Halcrow 2006-10-30 22:07:16 -0800 1552) if (*key_size > ECRYPTFS_MAX_KEY_BYTES) {
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1553) rc = -EINVAL;
df261c52abdef (Michael Halcrow 2009-01-06 14:42:02 -0800 1554) printk(KERN_ERR "Requested key size is [%zd] bytes; maximum "
e5d9cbde6ce00 (Michael Halcrow 2006-10-30 22:07:16 -0800 1555) "allowable is [%d]\n", *key_size, ECRYPTFS_MAX_KEY_BYTES);
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1556) goto out;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1557) }
8bba066f4e385 (Michael Halcrow 2006-10-30 22:07:18 -0800 1558) rc = ecryptfs_crypto_api_algify_cipher_name(&full_alg_name, cipher_name,
8bba066f4e385 (Michael Halcrow 2006-10-30 22:07:18 -0800 1559) "ecb");
8bba066f4e385 (Michael Halcrow 2006-10-30 22:07:18 -0800 1560) if (rc)
8bba066f4e385 (Michael Halcrow 2006-10-30 22:07:18 -0800 1561) goto out;
3095e8e366b47 (Herbert Xu 2016-01-25 10:29:33 +0800 1562) *key_tfm = crypto_alloc_skcipher(full_alg_name, 0, CRYPTO_ALG_ASYNC);
8bba066f4e385 (Michael Halcrow 2006-10-30 22:07:18 -0800 1563) if (IS_ERR(*key_tfm)) {
8bba066f4e385 (Michael Halcrow 2006-10-30 22:07:18 -0800 1564) rc = PTR_ERR(*key_tfm);
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1565) printk(KERN_ERR "Unable to allocate crypto cipher with name "
382684984e930 (Dave Hansen 2009-08-27 09:47:07 -0700 1566) "[%s]; rc = [%d]\n", full_alg_name, rc);
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1567) goto out;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1568) }
231baecdef7a9 (Eric Biggers 2019-01-18 22:48:00 -0800 1569) crypto_skcipher_set_flags(*key_tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
3095e8e366b47 (Herbert Xu 2016-01-25 10:29:33 +0800 1570) if (*key_size == 0)
9ac0d136938ad (Eric Biggers 2019-11-29 10:23:04 -0800 1571) *key_size = crypto_skcipher_max_keysize(*key_tfm);
e5d9cbde6ce00 (Michael Halcrow 2006-10-30 22:07:16 -0800 1572) get_random_bytes(dummy_key, *key_size);
3095e8e366b47 (Herbert Xu 2016-01-25 10:29:33 +0800 1573) rc = crypto_skcipher_setkey(*key_tfm, dummy_key, *key_size);
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1574) if (rc) {
df261c52abdef (Michael Halcrow 2009-01-06 14:42:02 -0800 1575) printk(KERN_ERR "Error attempting to set key of size [%zd] for "
382684984e930 (Dave Hansen 2009-08-27 09:47:07 -0700 1576) "cipher [%s]; rc = [%d]\n", *key_size, full_alg_name,
382684984e930 (Dave Hansen 2009-08-27 09:47:07 -0700 1577) rc);
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1578) rc = -EINVAL;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1579) goto out;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1580) }
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1581) out:
ece550f51ba17 (Dan Carpenter 2010-01-19 12:34:32 +0300 1582) kfree(full_alg_name);
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1583) return rc;
237fead619984 (Michael Halcrow 2006-10-04 02:16:22 -0700 1584) }
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 1585)
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 1586) struct kmem_cache *ecryptfs_key_tfm_cache;
7896b631823c6 (Adrian Bunk 2008-02-06 01:38:32 -0800 1587) static struct list_head key_tfm_list;
902af369942f8 (Zheng Yongjun 2020-12-24 21:22:33 +0800 1588) DEFINE_MUTEX(key_tfm_list_mutex);
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 1589)
7371a38201d04 (Jerome Marchand 2010-08-17 17:24:05 +0200 1590) int __init ecryptfs_init_crypto(void)
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 1591) {
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 1592) INIT_LIST_HEAD(&key_tfm_list);
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 1593) return 0;
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 1594) }
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 1595)
af440f52927e4 (Eric Sandeen 2008-02-06 01:38:37 -0800 1596) /**
af440f52927e4 (Eric Sandeen 2008-02-06 01:38:37 -0800 1597) * ecryptfs_destroy_crypto - free all cached key_tfms on key_tfm_list
af440f52927e4 (Eric Sandeen 2008-02-06 01:38:37 -0800 1598) *
af440f52927e4 (Eric Sandeen 2008-02-06 01:38:37 -0800 1599) * Called only at module unload time
af440f52927e4 (Eric Sandeen 2008-02-06 01:38:37 -0800 1600) */
fcd12835666b0 (Michael Halcrow 2007-10-16 01:28:01 -0700 1601) int ecryptfs_destroy_crypto(void)
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 1602) {
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 1603) struct ecryptfs_key_tfm *key_tfm, *key_tfm_tmp;
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 1604)
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 1605) mutex_lock(&key_tfm_list_mutex);
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 1606) list_for_each_entry_safe(key_tfm, key_tfm_tmp, &key_tfm_list,
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 1607) key_tfm_list) {
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 1608) list_del(&key_tfm->key_tfm_list);
3095e8e366b47 (Herbert Xu 2016-01-25 10:29:33 +0800 1609) crypto_free_skcipher(key_tfm->key_tfm);
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 1610) kmem_cache_free(ecryptfs_key_tfm_cache, key_tfm);
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 1611) }
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 1612) mutex_unlock(&key_tfm_list_mutex);
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 1613) return 0;
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 1614) }
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 1615)
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 1616) int
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 1617) ecryptfs_add_new_key_tfm(struct ecryptfs_key_tfm **key_tfm, char *cipher_name,
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 1618) size_t key_size)
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 1619) {
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 1620) struct ecryptfs_key_tfm *tmp_tfm;
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 1621) int rc = 0;
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 1622)
af440f52927e4 (Eric Sandeen 2008-02-06 01:38:37 -0800 1623) BUG_ON(!mutex_is_locked(&key_tfm_list_mutex));
af440f52927e4 (Eric Sandeen 2008-02-06 01:38:37 -0800 1624)
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 1625) tmp_tfm = kmem_cache_alloc(ecryptfs_key_tfm_cache, GFP_KERNEL);
5032f360dd31e (Markus Elfring 2017-08-19 18:00:22 +0200 1626) if (key_tfm)
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 1627) (*key_tfm) = tmp_tfm;
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 1628) if (!tmp_tfm) {
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 1629) rc = -ENOMEM;
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 1630) goto out;
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 1631) }
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 1632) mutex_init(&tmp_tfm->key_tfm_mutex);
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 1633) strncpy(tmp_tfm->cipher_name, cipher_name,
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 1634) ECRYPTFS_MAX_CIPHER_NAME_SIZE);
b88629060b03a (Eric Sandeen 2007-12-22 14:03:24 -0800 1635) tmp_tfm->cipher_name[ECRYPTFS_MAX_CIPHER_NAME_SIZE] = '\0';
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 1636) tmp_tfm->key_size = key_size;
5dda6992a3138 (Michael Halcrow 2007-10-16 01:28:06 -0700 1637) rc = ecryptfs_process_key_cipher(&tmp_tfm->key_tfm,
5dda6992a3138 (Michael Halcrow 2007-10-16 01:28:06 -0700 1638) tmp_tfm->cipher_name,
5dda6992a3138 (Michael Halcrow 2007-10-16 01:28:06 -0700 1639) &tmp_tfm->key_size);
5dda6992a3138 (Michael Halcrow 2007-10-16 01:28:06 -0700 1640) if (rc) {
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 1641) printk(KERN_ERR "Error attempting to initialize key TFM "
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 1642) "cipher with name = [%s]; rc = [%d]\n",
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 1643) tmp_tfm->cipher_name, rc);
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 1644) kmem_cache_free(ecryptfs_key_tfm_cache, tmp_tfm);
5032f360dd31e (Markus Elfring 2017-08-19 18:00:22 +0200 1645) if (key_tfm)
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 1646) (*key_tfm) = NULL;
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 1647) goto out;
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 1648) }
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 1649) list_add(&tmp_tfm->key_tfm_list, &key_tfm_list);
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 1650) out:
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 1651) return rc;
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 1652) }
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 1653)
af440f52927e4 (Eric Sandeen 2008-02-06 01:38:37 -0800 1654) /**
af440f52927e4 (Eric Sandeen 2008-02-06 01:38:37 -0800 1655) * ecryptfs_tfm_exists - Search for existing tfm for cipher_name.
af440f52927e4 (Eric Sandeen 2008-02-06 01:38:37 -0800 1656) * @cipher_name: the name of the cipher to search for
af440f52927e4 (Eric Sandeen 2008-02-06 01:38:37 -0800 1657) * @key_tfm: set to corresponding tfm if found
af440f52927e4 (Eric Sandeen 2008-02-06 01:38:37 -0800 1658) *
af440f52927e4 (Eric Sandeen 2008-02-06 01:38:37 -0800 1659) * Searches for cached key_tfm matching @cipher_name
af440f52927e4 (Eric Sandeen 2008-02-06 01:38:37 -0800 1660) * Must be called with &key_tfm_list_mutex held
af440f52927e4 (Eric Sandeen 2008-02-06 01:38:37 -0800 1661) * Returns 1 if found, with @key_tfm set
af440f52927e4 (Eric Sandeen 2008-02-06 01:38:37 -0800 1662) * Returns 0 if not found, with @key_tfm set to NULL
af440f52927e4 (Eric Sandeen 2008-02-06 01:38:37 -0800 1663) */
af440f52927e4 (Eric Sandeen 2008-02-06 01:38:37 -0800 1664) int ecryptfs_tfm_exists(char *cipher_name, struct ecryptfs_key_tfm **key_tfm)
af440f52927e4 (Eric Sandeen 2008-02-06 01:38:37 -0800 1665) {
af440f52927e4 (Eric Sandeen 2008-02-06 01:38:37 -0800 1666) struct ecryptfs_key_tfm *tmp_key_tfm;
af440f52927e4 (Eric Sandeen 2008-02-06 01:38:37 -0800 1667)
af440f52927e4 (Eric Sandeen 2008-02-06 01:38:37 -0800 1668) BUG_ON(!mutex_is_locked(&key_tfm_list_mutex));
af440f52927e4 (Eric Sandeen 2008-02-06 01:38:37 -0800 1669)
af440f52927e4 (Eric Sandeen 2008-02-06 01:38:37 -0800 1670) list_for_each_entry(tmp_key_tfm, &key_tfm_list, key_tfm_list) {
af440f52927e4 (Eric Sandeen 2008-02-06 01:38:37 -0800 1671) if (strcmp(tmp_key_tfm->cipher_name, cipher_name) == 0) {
af440f52927e4 (Eric Sandeen 2008-02-06 01:38:37 -0800 1672) if (key_tfm)
af440f52927e4 (Eric Sandeen 2008-02-06 01:38:37 -0800 1673) (*key_tfm) = tmp_key_tfm;
af440f52927e4 (Eric Sandeen 2008-02-06 01:38:37 -0800 1674) return 1;
af440f52927e4 (Eric Sandeen 2008-02-06 01:38:37 -0800 1675) }
af440f52927e4 (Eric Sandeen 2008-02-06 01:38:37 -0800 1676) }
af440f52927e4 (Eric Sandeen 2008-02-06 01:38:37 -0800 1677) if (key_tfm)
af440f52927e4 (Eric Sandeen 2008-02-06 01:38:37 -0800 1678) (*key_tfm) = NULL;
af440f52927e4 (Eric Sandeen 2008-02-06 01:38:37 -0800 1679) return 0;
af440f52927e4 (Eric Sandeen 2008-02-06 01:38:37 -0800 1680) }
af440f52927e4 (Eric Sandeen 2008-02-06 01:38:37 -0800 1681)
af440f52927e4 (Eric Sandeen 2008-02-06 01:38:37 -0800 1682) /**
af440f52927e4 (Eric Sandeen 2008-02-06 01:38:37 -0800 1683) * ecryptfs_get_tfm_and_mutex_for_cipher_name
af440f52927e4 (Eric Sandeen 2008-02-06 01:38:37 -0800 1684) *
af440f52927e4 (Eric Sandeen 2008-02-06 01:38:37 -0800 1685) * @tfm: set to cached tfm found, or new tfm created
af440f52927e4 (Eric Sandeen 2008-02-06 01:38:37 -0800 1686) * @tfm_mutex: set to mutex for cached tfm found, or new tfm created
af440f52927e4 (Eric Sandeen 2008-02-06 01:38:37 -0800 1687) * @cipher_name: the name of the cipher to search for and/or add
af440f52927e4 (Eric Sandeen 2008-02-06 01:38:37 -0800 1688) *
af440f52927e4 (Eric Sandeen 2008-02-06 01:38:37 -0800 1689) * Sets pointers to @tfm & @tfm_mutex matching @cipher_name.
af440f52927e4 (Eric Sandeen 2008-02-06 01:38:37 -0800 1690) * Searches for cached item first, and creates new if not found.
af440f52927e4 (Eric Sandeen 2008-02-06 01:38:37 -0800 1691) * Returns 0 on success, non-zero if adding new cipher failed
af440f52927e4 (Eric Sandeen 2008-02-06 01:38:37 -0800 1692) */
3095e8e366b47 (Herbert Xu 2016-01-25 10:29:33 +0800 1693) int ecryptfs_get_tfm_and_mutex_for_cipher_name(struct crypto_skcipher **tfm,
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 1694) struct mutex **tfm_mutex,
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 1695) char *cipher_name)
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 1696) {
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 1697) struct ecryptfs_key_tfm *key_tfm;
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 1698) int rc = 0;
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 1699)
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 1700) (*tfm) = NULL;
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 1701) (*tfm_mutex) = NULL;
af440f52927e4 (Eric Sandeen 2008-02-06 01:38:37 -0800 1702)
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 1703) mutex_lock(&key_tfm_list_mutex);
af440f52927e4 (Eric Sandeen 2008-02-06 01:38:37 -0800 1704) if (!ecryptfs_tfm_exists(cipher_name, &key_tfm)) {
af440f52927e4 (Eric Sandeen 2008-02-06 01:38:37 -0800 1705) rc = ecryptfs_add_new_key_tfm(&key_tfm, cipher_name, 0);
af440f52927e4 (Eric Sandeen 2008-02-06 01:38:37 -0800 1706) if (rc) {
af440f52927e4 (Eric Sandeen 2008-02-06 01:38:37 -0800 1707) printk(KERN_ERR "Error adding new key_tfm to list; "
af440f52927e4 (Eric Sandeen 2008-02-06 01:38:37 -0800 1708) "rc = [%d]\n", rc);
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 1709) goto out;
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 1710) }
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 1711) }
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 1712) (*tfm) = key_tfm->key_tfm;
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 1713) (*tfm_mutex) = &key_tfm->key_tfm_mutex;
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 1714) out:
71fd5179e8d1d (Cyrill Gorcunov 2008-05-23 13:04:20 -0700 1715) mutex_unlock(&key_tfm_list_mutex);
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 1716) return rc;
f4aad16adfb8f (Michael Halcrow 2007-10-16 01:27:53 -0700 1717) }
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1718)
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1719) /* 64 characters forming a 6-bit target field */
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1720) static unsigned char *portable_filename_chars = ("-.0123456789ABCD"
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1721) "EFGHIJKLMNOPQRST"
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1722) "UVWXYZabcdefghij"
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1723) "klmnopqrstuvwxyz");
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1724)
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1725) /* We could either offset on every reverse map or just pad some 0x00's
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1726) * at the front here */
0f751e641a711 (Tyler Hicks 2011-11-23 11:31:24 -0600 1727) static const unsigned char filename_rev_map[256] = {
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1728) 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 7 */
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1729) 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 15 */
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1730) 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 23 */
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1731) 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 31 */
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1732) 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 39 */
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1733) 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, /* 47 */
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1734) 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, /* 55 */
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1735) 0x0A, 0x0B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 63 */
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1736) 0x00, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, /* 71 */
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1737) 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, /* 79 */
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1738) 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, 0x21, 0x22, /* 87 */
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1739) 0x23, 0x24, 0x25, 0x00, 0x00, 0x00, 0x00, 0x00, /* 95 */
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1740) 0x00, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, /* 103 */
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1741) 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, /* 111 */
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1742) 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, /* 119 */
0f751e641a711 (Tyler Hicks 2011-11-23 11:31:24 -0600 1743) 0x3D, 0x3E, 0x3F /* 123 - 255 initialized to 0x00 */
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1744) };
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1745)
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1746) /**
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1747) * ecryptfs_encode_for_filename
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1748) * @dst: Destination location for encoded filename
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1749) * @dst_size: Size of the encoded filename in bytes
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1750) * @src: Source location for the filename to encode
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1751) * @src_size: Size of the source in bytes
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1752) */
37028758f92d0 (Cong Ding 2012-12-07 22:21:56 +0000 1753) static void ecryptfs_encode_for_filename(unsigned char *dst, size_t *dst_size,
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1754) unsigned char *src, size_t src_size)
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1755) {
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1756) size_t num_blocks;
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1757) size_t block_num = 0;
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1758) size_t dst_offset = 0;
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1759) unsigned char last_block[3];
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1760)
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1761) if (src_size == 0) {
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1762) (*dst_size) = 0;
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1763) goto out;
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1764) }
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1765) num_blocks = (src_size / 3);
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1766) if ((src_size % 3) == 0) {
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1767) memcpy(last_block, (&src[src_size - 3]), 3);
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1768) } else {
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1769) num_blocks++;
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1770) last_block[2] = 0x00;
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1771) switch (src_size % 3) {
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1772) case 1:
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1773) last_block[0] = src[src_size - 1];
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1774) last_block[1] = 0x00;
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1775) break;
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1776) case 2:
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1777) last_block[0] = src[src_size - 2];
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1778) last_block[1] = src[src_size - 1];
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1779) }
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1780) }
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1781) (*dst_size) = (num_blocks * 4);
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1782) if (!dst)
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1783) goto out;
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1784) while (block_num < num_blocks) {
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1785) unsigned char *src_block;
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1786) unsigned char dst_block[4];
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1787)
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1788) if (block_num == (num_blocks - 1))
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1789) src_block = last_block;
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1790) else
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1791) src_block = &src[block_num * 3];
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1792) dst_block[0] = ((src_block[0] >> 2) & 0x3F);
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1793) dst_block[1] = (((src_block[0] << 4) & 0x30)
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1794) | ((src_block[1] >> 4) & 0x0F));
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1795) dst_block[2] = (((src_block[1] << 2) & 0x3C)
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1796) | ((src_block[2] >> 6) & 0x03));
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1797) dst_block[3] = (src_block[2] & 0x3F);
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1798) dst[dst_offset++] = portable_filename_chars[dst_block[0]];
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1799) dst[dst_offset++] = portable_filename_chars[dst_block[1]];
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1800) dst[dst_offset++] = portable_filename_chars[dst_block[2]];
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1801) dst[dst_offset++] = portable_filename_chars[dst_block[3]];
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1802) block_num++;
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1803) }
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1804) out:
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1805) return;
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1806) }
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1807)
4a26620df451a (Tyler Hicks 2011-11-05 13:45:08 -0400 1808) static size_t ecryptfs_max_decoded_size(size_t encoded_size)
4a26620df451a (Tyler Hicks 2011-11-05 13:45:08 -0400 1809) {
4a26620df451a (Tyler Hicks 2011-11-05 13:45:08 -0400 1810) /* Not exact; conservatively long. Every block of 4
4a26620df451a (Tyler Hicks 2011-11-05 13:45:08 -0400 1811) * encoded characters decodes into a block of 3
4a26620df451a (Tyler Hicks 2011-11-05 13:45:08 -0400 1812) * decoded characters. This segment of code provides
4a26620df451a (Tyler Hicks 2011-11-05 13:45:08 -0400 1813) * the caller with the maximum amount of allocated
4a26620df451a (Tyler Hicks 2011-11-05 13:45:08 -0400 1814) * space that @dst will need to point to in a
4a26620df451a (Tyler Hicks 2011-11-05 13:45:08 -0400 1815) * subsequent call. */
4a26620df451a (Tyler Hicks 2011-11-05 13:45:08 -0400 1816) return ((encoded_size + 1) * 3) / 4;
4a26620df451a (Tyler Hicks 2011-11-05 13:45:08 -0400 1817) }
4a26620df451a (Tyler Hicks 2011-11-05 13:45:08 -0400 1818)
71c11c378f46e (Michael Halcrow 2009-01-06 14:42:05 -0800 1819) /**
71c11c378f46e (Michael Halcrow 2009-01-06 14:42:05 -0800 1820) * ecryptfs_decode_from_filename
71c11c378f46e (Michael Halcrow 2009-01-06 14:42:05 -0800 1821) * @dst: If NULL, this function only sets @dst_size and returns. If
71c11c378f46e (Michael Halcrow 2009-01-06 14:42:05 -0800 1822) * non-NULL, this function decodes the encoded octets in @src
71c11c378f46e (Michael Halcrow 2009-01-06 14:42:05 -0800 1823) * into the memory that @dst points to.
71c11c378f46e (Michael Halcrow 2009-01-06 14:42:05 -0800 1824) * @dst_size: Set to the size of the decoded string.
71c11c378f46e (Michael Halcrow 2009-01-06 14:42:05 -0800 1825) * @src: The encoded set of octets to decode.
71c11c378f46e (Michael Halcrow 2009-01-06 14:42:05 -0800 1826) * @src_size: The size of the encoded set of octets to decode.
71c11c378f46e (Michael Halcrow 2009-01-06 14:42:05 -0800 1827) */
71c11c378f46e (Michael Halcrow 2009-01-06 14:42:05 -0800 1828) static void
71c11c378f46e (Michael Halcrow 2009-01-06 14:42:05 -0800 1829) ecryptfs_decode_from_filename(unsigned char *dst, size_t *dst_size,
71c11c378f46e (Michael Halcrow 2009-01-06 14:42:05 -0800 1830) const unsigned char *src, size_t src_size)
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1831) {
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1832) u8 current_bit_offset = 0;
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1833) size_t src_byte_offset = 0;
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1834) size_t dst_byte_offset = 0;
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1835)
5032f360dd31e (Markus Elfring 2017-08-19 18:00:22 +0200 1836) if (!dst) {
4a26620df451a (Tyler Hicks 2011-11-05 13:45:08 -0400 1837) (*dst_size) = ecryptfs_max_decoded_size(src_size);
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1838) goto out;
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1839) }
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1840) while (src_byte_offset < src_size) {
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1841) unsigned char src_byte =
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1842) filename_rev_map[(int)src[src_byte_offset]];
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1843)
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1844) switch (current_bit_offset) {
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1845) case 0:
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1846) dst[dst_byte_offset] = (src_byte << 2);
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1847) current_bit_offset = 6;
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1848) break;
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1849) case 6:
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1850) dst[dst_byte_offset++] |= (src_byte >> 4);
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1851) dst[dst_byte_offset] = ((src_byte & 0xF)
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1852) << 4);
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1853) current_bit_offset = 4;
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1854) break;
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1855) case 4:
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1856) dst[dst_byte_offset++] |= (src_byte >> 2);
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1857) dst[dst_byte_offset] = (src_byte << 6);
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1858) current_bit_offset = 2;
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1859) break;
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1860) case 2:
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1861) dst[dst_byte_offset++] |= (src_byte);
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1862) current_bit_offset = 0;
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1863) break;
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1864) }
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1865) src_byte_offset++;
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1866) }
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1867) (*dst_size) = dst_byte_offset;
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1868) out:
71c11c378f46e (Michael Halcrow 2009-01-06 14:42:05 -0800 1869) return;
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1870) }
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1871)
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1872) /**
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1873) * ecryptfs_encrypt_and_encode_filename - converts a plaintext file name to cipher text
5da877eadffb8 (Lee Jones 2021-03-30 17:44:55 +0100 1874) * @encoded_name: The encrypted name
5da877eadffb8 (Lee Jones 2021-03-30 17:44:55 +0100 1875) * @encoded_name_size: Length of the encrypted name
5da877eadffb8 (Lee Jones 2021-03-30 17:44:55 +0100 1876) * @mount_crypt_stat: The crypt_stat struct associated with the file name to encode
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1877) * @name: The plaintext name
5da877eadffb8 (Lee Jones 2021-03-30 17:44:55 +0100 1878) * @name_size: The length of the plaintext name
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1879) *
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1880) * Encrypts and encodes a filename into something that constitutes a
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1881) * valid filename for a filesystem, with printable characters.
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1882) *
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1883) * We assume that we have a properly initialized crypto context,
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1884) * pointed to by crypt_stat->tfm.
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1885) *
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1886) * Returns zero on success; non-zero on otherwise
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1887) */
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1888) int ecryptfs_encrypt_and_encode_filename(
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1889) char **encoded_name,
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1890) size_t *encoded_name_size,
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1891) struct ecryptfs_mount_crypt_stat *mount_crypt_stat,
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1892) const char *name, size_t name_size)
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1893) {
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1894) size_t encoded_name_no_prefix_size;
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1895) int rc = 0;
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1896)
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1897) (*encoded_name) = NULL;
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1898) (*encoded_name_size) = 0;
97c31606075b3 (Al Viro 2016-02-22 18:14:25 -0500 1899) if (mount_crypt_stat && (mount_crypt_stat->flags
97c31606075b3 (Al Viro 2016-02-22 18:14:25 -0500 1900) & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)) {
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1901) struct ecryptfs_filename *filename;
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1902)
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1903) filename = kzalloc(sizeof(*filename), GFP_KERNEL);
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1904) if (!filename) {
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1905) rc = -ENOMEM;
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1906) goto out;
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1907) }
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1908) filename->filename = (char *)name;
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1909) filename->filename_size = name_size;
97c31606075b3 (Al Viro 2016-02-22 18:14:25 -0500 1910) rc = ecryptfs_encrypt_filename(filename, mount_crypt_stat);
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1911) if (rc) {
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1912) printk(KERN_ERR "%s: Error attempting to encrypt "
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1913) "filename; rc = [%d]\n", __func__, rc);
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1914) kfree(filename);
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1915) goto out;
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1916) }
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1917) ecryptfs_encode_for_filename(
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1918) NULL, &encoded_name_no_prefix_size,
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1919) filename->encrypted_filename,
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1920) filename->encrypted_filename_size);
97c31606075b3 (Al Viro 2016-02-22 18:14:25 -0500 1921) if (mount_crypt_stat
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1922) && (mount_crypt_stat->flags
97c31606075b3 (Al Viro 2016-02-22 18:14:25 -0500 1923) & ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK))
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1924) (*encoded_name_size) =
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1925) (ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1926) + encoded_name_no_prefix_size);
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1927) else
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1928) (*encoded_name_size) =
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1929) (ECRYPTFS_FEK_ENCRYPTED_FILENAME_PREFIX_SIZE
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1930) + encoded_name_no_prefix_size);
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1931) (*encoded_name) = kmalloc((*encoded_name_size) + 1, GFP_KERNEL);
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1932) if (!(*encoded_name)) {
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1933) rc = -ENOMEM;
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1934) kfree(filename->encrypted_filename);
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1935) kfree(filename);
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1936) goto out;
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1937) }
97c31606075b3 (Al Viro 2016-02-22 18:14:25 -0500 1938) if (mount_crypt_stat
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1939) && (mount_crypt_stat->flags
97c31606075b3 (Al Viro 2016-02-22 18:14:25 -0500 1940) & ECRYPTFS_GLOBAL_ENCFN_USE_MOUNT_FNEK)) {
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1941) memcpy((*encoded_name),
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1942) ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX,
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1943) ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE);
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1944) ecryptfs_encode_for_filename(
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1945) ((*encoded_name)
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1946) + ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE),
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1947) &encoded_name_no_prefix_size,
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1948) filename->encrypted_filename,
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1949) filename->encrypted_filename_size);
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1950) (*encoded_name_size) =
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1951) (ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1952) + encoded_name_no_prefix_size);
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1953) (*encoded_name)[(*encoded_name_size)] = '\0';
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1954) } else {
df6ad33ba1b98 (Tyler Hicks 2009-08-21 04:27:46 -0500 1955) rc = -EOPNOTSUPP;
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1956) }
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1957) if (rc) {
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1958) printk(KERN_ERR "%s: Error attempting to encode "
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1959) "encrypted filename; rc = [%d]\n", __func__,
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1960) rc);
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1961) kfree((*encoded_name));
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1962) (*encoded_name) = NULL;
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1963) (*encoded_name_size) = 0;
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1964) }
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1965) kfree(filename->encrypted_filename);
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1966) kfree(filename);
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1967) } else {
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1968) rc = ecryptfs_copy_filename(encoded_name,
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1969) encoded_name_size,
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1970) name, name_size);
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1971) }
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1972) out:
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1973) return rc;
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1974) }
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1975)
e86281e700cca (Tyler Hicks 2018-03-28 23:41:52 +0000 1976) static bool is_dot_dotdot(const char *name, size_t name_size)
e86281e700cca (Tyler Hicks 2018-03-28 23:41:52 +0000 1977) {
e86281e700cca (Tyler Hicks 2018-03-28 23:41:52 +0000 1978) if (name_size == 1 && name[0] == '.')
e86281e700cca (Tyler Hicks 2018-03-28 23:41:52 +0000 1979) return true;
e86281e700cca (Tyler Hicks 2018-03-28 23:41:52 +0000 1980) else if (name_size == 2 && name[0] == '.' && name[1] == '.')
e86281e700cca (Tyler Hicks 2018-03-28 23:41:52 +0000 1981) return true;
e86281e700cca (Tyler Hicks 2018-03-28 23:41:52 +0000 1982)
e86281e700cca (Tyler Hicks 2018-03-28 23:41:52 +0000 1983) return false;
e86281e700cca (Tyler Hicks 2018-03-28 23:41:52 +0000 1984) }
e86281e700cca (Tyler Hicks 2018-03-28 23:41:52 +0000 1985)
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1986) /**
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1987) * ecryptfs_decode_and_decrypt_filename - converts the encoded cipher text name to decoded plaintext
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1988) * @plaintext_name: The plaintext name
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1989) * @plaintext_name_size: The plaintext name size
5da877eadffb8 (Lee Jones 2021-03-30 17:44:55 +0100 1990) * @sb: Ecryptfs's super_block
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1991) * @name: The filename in cipher text
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1992) * @name_size: The cipher text name size
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1993) *
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1994) * Decrypts and decodes the filename.
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1995) *
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1996) * Returns zero on error; non-zero otherwise
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1997) */
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1998) int ecryptfs_decode_and_decrypt_filename(char **plaintext_name,
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 1999) size_t *plaintext_name_size,
0747fdb2bd59d (Al Viro 2013-06-16 20:05:38 +0400 2000) struct super_block *sb,
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 2001) const char *name, size_t name_size)
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 2002) {
2aac0cf88681b (Tyler Hicks 2009-03-20 02:23:57 -0500 2003) struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
0747fdb2bd59d (Al Viro 2013-06-16 20:05:38 +0400 2004) &ecryptfs_superblock_to_private(sb)->mount_crypt_stat;
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 2005) char *decoded_name;
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 2006) size_t decoded_name_size;
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 2007) size_t packet_size;
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 2008) int rc = 0;
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 2009)
e86281e700cca (Tyler Hicks 2018-03-28 23:41:52 +0000 2010) if ((mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES) &&
e86281e700cca (Tyler Hicks 2018-03-28 23:41:52 +0000 2011) !(mount_crypt_stat->flags & ECRYPTFS_ENCRYPTED_VIEW_ENABLED)) {
e86281e700cca (Tyler Hicks 2018-03-28 23:41:52 +0000 2012) if (is_dot_dotdot(name, name_size)) {
e86281e700cca (Tyler Hicks 2018-03-28 23:41:52 +0000 2013) rc = ecryptfs_copy_filename(plaintext_name,
e86281e700cca (Tyler Hicks 2018-03-28 23:41:52 +0000 2014) plaintext_name_size,
e86281e700cca (Tyler Hicks 2018-03-28 23:41:52 +0000 2015) name, name_size);
e86281e700cca (Tyler Hicks 2018-03-28 23:41:52 +0000 2016) goto out;
e86281e700cca (Tyler Hicks 2018-03-28 23:41:52 +0000 2017) }
e86281e700cca (Tyler Hicks 2018-03-28 23:41:52 +0000 2018)
e86281e700cca (Tyler Hicks 2018-03-28 23:41:52 +0000 2019) if (name_size <= ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE ||
e86281e700cca (Tyler Hicks 2018-03-28 23:41:52 +0000 2020) strncmp(name, ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX,
e86281e700cca (Tyler Hicks 2018-03-28 23:41:52 +0000 2021) ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE)) {
e86281e700cca (Tyler Hicks 2018-03-28 23:41:52 +0000 2022) rc = -EINVAL;
e86281e700cca (Tyler Hicks 2018-03-28 23:41:52 +0000 2023) goto out;
e86281e700cca (Tyler Hicks 2018-03-28 23:41:52 +0000 2024) }
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 2025)
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 2026) name += ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE;
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 2027) name_size -= ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE;
71c11c378f46e (Michael Halcrow 2009-01-06 14:42:05 -0800 2028) ecryptfs_decode_from_filename(NULL, &decoded_name_size,
71c11c378f46e (Michael Halcrow 2009-01-06 14:42:05 -0800 2029) name, name_size);
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 2030) decoded_name = kmalloc(decoded_name_size, GFP_KERNEL);
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 2031) if (!decoded_name) {
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 2032) rc = -ENOMEM;
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 2033) goto out;
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 2034) }
71c11c378f46e (Michael Halcrow 2009-01-06 14:42:05 -0800 2035) ecryptfs_decode_from_filename(decoded_name, &decoded_name_size,
71c11c378f46e (Michael Halcrow 2009-01-06 14:42:05 -0800 2036) name, name_size);
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 2037) rc = ecryptfs_parse_tag_70_packet(plaintext_name,
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 2038) plaintext_name_size,
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 2039) &packet_size,
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 2040) mount_crypt_stat,
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 2041) decoded_name,
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 2042) decoded_name_size);
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 2043) if (rc) {
e86281e700cca (Tyler Hicks 2018-03-28 23:41:52 +0000 2044) ecryptfs_printk(KERN_DEBUG,
e86281e700cca (Tyler Hicks 2018-03-28 23:41:52 +0000 2045) "%s: Could not parse tag 70 packet from filename\n",
e86281e700cca (Tyler Hicks 2018-03-28 23:41:52 +0000 2046) __func__);
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 2047) goto out_free;
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 2048) }
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 2049) } else {
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 2050) rc = ecryptfs_copy_filename(plaintext_name,
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 2051) plaintext_name_size,
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 2052) name, name_size);
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 2053) goto out;
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 2054) }
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 2055) out_free:
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 2056) kfree(decoded_name);
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 2057) out:
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 2058) return rc;
51ca58dcc9f0d (Michael Halcrow 2009-01-06 14:41:59 -0800 2059) }
4a26620df451a (Tyler Hicks 2011-11-05 13:45:08 -0400 2060)
4a26620df451a (Tyler Hicks 2011-11-05 13:45:08 -0400 2061) #define ENC_NAME_MAX_BLOCKLEN_8_OR_16 143
4a26620df451a (Tyler Hicks 2011-11-05 13:45:08 -0400 2062)
4a26620df451a (Tyler Hicks 2011-11-05 13:45:08 -0400 2063) int ecryptfs_set_f_namelen(long *namelen, long lower_namelen,
4a26620df451a (Tyler Hicks 2011-11-05 13:45:08 -0400 2064) struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
4a26620df451a (Tyler Hicks 2011-11-05 13:45:08 -0400 2065) {
3095e8e366b47 (Herbert Xu 2016-01-25 10:29:33 +0800 2066) struct crypto_skcipher *tfm;
4a26620df451a (Tyler Hicks 2011-11-05 13:45:08 -0400 2067) struct mutex *tfm_mutex;
4a26620df451a (Tyler Hicks 2011-11-05 13:45:08 -0400 2068) size_t cipher_blocksize;
4a26620df451a (Tyler Hicks 2011-11-05 13:45:08 -0400 2069) int rc;
4a26620df451a (Tyler Hicks 2011-11-05 13:45:08 -0400 2070)
4a26620df451a (Tyler Hicks 2011-11-05 13:45:08 -0400 2071) if (!(mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)) {
4a26620df451a (Tyler Hicks 2011-11-05 13:45:08 -0400 2072) (*namelen) = lower_namelen;
4a26620df451a (Tyler Hicks 2011-11-05 13:45:08 -0400 2073) return 0;
4a26620df451a (Tyler Hicks 2011-11-05 13:45:08 -0400 2074) }
4a26620df451a (Tyler Hicks 2011-11-05 13:45:08 -0400 2075)
3095e8e366b47 (Herbert Xu 2016-01-25 10:29:33 +0800 2076) rc = ecryptfs_get_tfm_and_mutex_for_cipher_name(&tfm, &tfm_mutex,
4a26620df451a (Tyler Hicks 2011-11-05 13:45:08 -0400 2077) mount_crypt_stat->global_default_fn_cipher_name);
4a26620df451a (Tyler Hicks 2011-11-05 13:45:08 -0400 2078) if (unlikely(rc)) {
4a26620df451a (Tyler Hicks 2011-11-05 13:45:08 -0400 2079) (*namelen) = 0;
4a26620df451a (Tyler Hicks 2011-11-05 13:45:08 -0400 2080) return rc;
4a26620df451a (Tyler Hicks 2011-11-05 13:45:08 -0400 2081) }
4a26620df451a (Tyler Hicks 2011-11-05 13:45:08 -0400 2082)
4a26620df451a (Tyler Hicks 2011-11-05 13:45:08 -0400 2083) mutex_lock(tfm_mutex);
3095e8e366b47 (Herbert Xu 2016-01-25 10:29:33 +0800 2084) cipher_blocksize = crypto_skcipher_blocksize(tfm);
4a26620df451a (Tyler Hicks 2011-11-05 13:45:08 -0400 2085) mutex_unlock(tfm_mutex);
4a26620df451a (Tyler Hicks 2011-11-05 13:45:08 -0400 2086)
4a26620df451a (Tyler Hicks 2011-11-05 13:45:08 -0400 2087) /* Return an exact amount for the common cases */
4a26620df451a (Tyler Hicks 2011-11-05 13:45:08 -0400 2088) if (lower_namelen == NAME_MAX
4a26620df451a (Tyler Hicks 2011-11-05 13:45:08 -0400 2089) && (cipher_blocksize == 8 || cipher_blocksize == 16)) {
4a26620df451a (Tyler Hicks 2011-11-05 13:45:08 -0400 2090) (*namelen) = ENC_NAME_MAX_BLOCKLEN_8_OR_16;
4a26620df451a (Tyler Hicks 2011-11-05 13:45:08 -0400 2091) return 0;
4a26620df451a (Tyler Hicks 2011-11-05 13:45:08 -0400 2092) }
4a26620df451a (Tyler Hicks 2011-11-05 13:45:08 -0400 2093)
4a26620df451a (Tyler Hicks 2011-11-05 13:45:08 -0400 2094) /* Return a safe estimate for the uncommon cases */
4a26620df451a (Tyler Hicks 2011-11-05 13:45:08 -0400 2095) (*namelen) = lower_namelen;
4a26620df451a (Tyler Hicks 2011-11-05 13:45:08 -0400 2096) (*namelen) -= ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE;
4a26620df451a (Tyler Hicks 2011-11-05 13:45:08 -0400 2097) /* Since this is the max decoded size, subtract 1 "decoded block" len */
4a26620df451a (Tyler Hicks 2011-11-05 13:45:08 -0400 2098) (*namelen) = ecryptfs_max_decoded_size(*namelen) - 3;
4a26620df451a (Tyler Hicks 2011-11-05 13:45:08 -0400 2099) (*namelen) -= ECRYPTFS_TAG_70_MAX_METADATA_SIZE;
4a26620df451a (Tyler Hicks 2011-11-05 13:45:08 -0400 2100) (*namelen) -= ECRYPTFS_FILENAME_MIN_RANDOM_PREPEND_BYTES;
4a26620df451a (Tyler Hicks 2011-11-05 13:45:08 -0400 2101) /* Worst case is that the filename is padded nearly a full block size */
4a26620df451a (Tyler Hicks 2011-11-05 13:45:08 -0400 2102) (*namelen) -= cipher_blocksize - 1;
4a26620df451a (Tyler Hicks 2011-11-05 13:45:08 -0400 2103)
4a26620df451a (Tyler Hicks 2011-11-05 13:45:08 -0400 2104) if ((*namelen) < 0)
4a26620df451a (Tyler Hicks 2011-11-05 13:45:08 -0400 2105) (*namelen) = 0;
4a26620df451a (Tyler Hicks 2011-11-05 13:45:08 -0400 2106)
4a26620df451a (Tyler Hicks 2011-11-05 13:45:08 -0400 2107) return 0;
4a26620df451a (Tyler Hicks 2011-11-05 13:45:08 -0400 2108) }