29 #include <linux/random.h>
30 #include <linux/compiler.h>
36 #include <linux/slab.h>
37 #include <asm/unaligned.h>
42 struct page *dst_page,
int dst_offset,
43 struct page *src_page,
int src_offset,
int size,
47 struct page *dst_page,
int dst_offset,
48 struct page *src_page,
int src_offset,
int size,
62 for (x = 0; x < src_size; x++)
63 sprintf(&dst[x * 2],
"%.2x", (
unsigned char)src[x]);
80 tmp[1] = src[x * 2 + 1];
95 static int ecryptfs_calculate_md5(
char *
dst,
111 if (IS_ERR(desc.
tfm)) {
112 rc = PTR_ERR(desc.
tfm);
114 "allocate crypto context; rc = [%d]\n",
120 rc = crypto_hash_init(&desc);
123 "%s: Error initializing crypto hash; rc = [%d]\n",
127 rc = crypto_hash_update(&desc, &
sg, len);
130 "%s: Error updating crypto hash; rc = [%d]\n",
134 rc = crypto_hash_final(&desc, dst);
137 "%s: Error finalizing crypto hash; rc = [%d]\n",
146 static int ecryptfs_crypto_api_algify_cipher_name(
char **algified_name,
148 char *chaining_modifier)
150 int cipher_name_len =
strlen(cipher_name);
151 int chaining_modifier_len =
strlen(chaining_modifier);
152 int algified_name_len;
155 algified_name_len = (chaining_modifier_len + cipher_name_len + 3);
157 if (!(*algified_name)) {
161 snprintf((*algified_name), algified_name_len,
"%s(%s)",
162 chaining_modifier, cipher_name);
201 rc = ecryptfs_calculate_md5(dst, crypt_stat, src,
205 "MD5 while generating IV for a page\n");
246 crypto_free_blkcipher(crypt_stat->
tfm);
248 crypto_free_hash(crypt_stat->
hash_tfm);
297 int remainder_of_page;
301 while (size > 0 && i < sg_size) {
305 sg_set_page(&sg[i], pg, 0, offset);
307 if (size >= remainder_of_page) {
309 sg[
i].
length = remainder_of_page;
310 addr += remainder_of_page;
311 size -= remainder_of_page;
358 rc = crypto_blkcipher_setkey(crypt_stat->
tfm, crypt_stat->
key,
370 crypto_blkcipher_encrypt_iv(&desc, dest_sg, src_sg, size);
381 static void ecryptfs_lower_offset_for_extent(loff_t *
offset, loff_t extent_num,
384 (*offset) = ecryptfs_lower_header_size(crypt_stat)
401 static int ecryptfs_encrypt_extent(
struct page *enc_extent_page,
404 unsigned long extent_offset)
410 extent_base = (((loff_t)page->
index)
413 (extent_base + extent_offset));
416 "extent [0x%.16llx]; rc = [%d]\n",
417 (
unsigned long long)(extent_base + extent_offset), rc);
420 rc = ecryptfs_encrypt_page_offset(crypt_stat, enc_extent_page, 0,
426 "page->index = [%ld], extent_offset = [%ld]; "
427 "rc = [%d]\n", __func__, page->
index, extent_offset,
454 struct inode *ecryptfs_inode;
456 char *enc_extent_virt;
457 struct page *enc_extent_page =
NULL;
458 loff_t extent_offset;
461 ecryptfs_inode = page->
mapping->host;
463 &(ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat);
466 if (!enc_extent_page) {
469 "encrypted extent\n");
472 enc_extent_virt =
kmap(enc_extent_page);
473 for (extent_offset = 0;
478 rc = ecryptfs_encrypt_extent(enc_extent_page, crypt_stat, page,
482 "rc = [%d]\n", __func__, rc);
485 ecryptfs_lower_offset_for_extent(
486 &offset, ((((loff_t)page->
index)
489 + extent_offset), crypt_stat);
494 "to write lower page; rc = [%d]"
501 if (enc_extent_page) {
508 static int ecryptfs_decrypt_extent(
struct page *page,
510 struct page *enc_extent_page,
511 unsigned long extent_offset)
517 extent_base = (((loff_t)page->
index)
520 (extent_base + extent_offset));
523 "extent [0x%.16llx]; rc = [%d]\n",
524 (
unsigned long long)(extent_base + extent_offset), rc);
527 rc = ecryptfs_decrypt_page_offset(crypt_stat, page,
534 "page->index = [%ld], extent_offset = [%ld]; "
535 "rc = [%d]\n", __func__, page->
index, extent_offset,
562 struct inode *ecryptfs_inode;
564 char *enc_extent_virt;
565 struct page *enc_extent_page =
NULL;
566 unsigned long extent_offset;
569 ecryptfs_inode = page->
mapping->host;
571 &(ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat);
574 if (!enc_extent_page) {
577 "encrypted extent\n");
580 enc_extent_virt =
kmap(enc_extent_page);
581 for (extent_offset = 0;
586 ecryptfs_lower_offset_for_extent(
589 + extent_offset), crypt_stat);
595 "to read lower page; rc = [%d]"
599 rc = ecryptfs_decrypt_extent(page, crypt_stat, enc_extent_page,
603 "rc = [%d]\n", __func__, rc);
608 if (enc_extent_page) {
639 rc = crypto_blkcipher_setkey(crypt_stat->
tfm, crypt_stat->
key,
649 rc = crypto_blkcipher_decrypt_iv(&desc, dest_sg, src_sg, size);
675 struct page *dst_page,
int dst_offset,
676 struct page *src_page,
int src_offset,
int size,
684 sg_set_page(&src_sg, src_page, size, src_offset);
685 sg_set_page(&dst_sg, dst_page, size, dst_offset);
686 return encrypt_scatterlist(crypt_stat, &dst_sg, &src_sg, size, iv);
703 struct page *dst_page,
int dst_offset,
704 struct page *src_page,
int src_offset,
int size,
710 sg_set_page(&src_sg, src_page, size, src_offset);
713 sg_set_page(&dst_sg, dst_page, size, dst_offset);
715 return decrypt_scatterlist(crypt_stat, &dst_sg, &src_sg, size, iv);
718 #define ECRYPTFS_MAX_SCATTERLIST_LEN 4
734 if (!crypt_stat->
cipher) {
739 "Initializing cipher [%s]; strlen = [%d]; "
740 "key_size_bits = [%zd]\n",
743 if (crypt_stat->
tfm) {
748 rc = ecryptfs_crypto_api_algify_cipher_name(&full_alg_name,
749 crypt_stat->
cipher,
"cbc");
752 crypt_stat->
tfm = crypto_alloc_blkcipher(full_alg_name, 0,
754 kfree(full_alg_name);
755 if (IS_ERR(crypt_stat->
tfm)) {
756 rc = PTR_ERR(crypt_stat->
tfm);
759 "Error initializing cipher [%s]\n",
780 while ((extent_size_tmp & 0x01) == 0) {
781 extent_size_tmp >>= 1;
792 set_extent_mask_and_shift(crypt_stat);
821 "cannot generate root IV\n");
824 rc = ecryptfs_calculate_md5(dst, crypt_stat, crypt_stat->
key,
828 "MD5 while generating root IV\n");
860 static void ecryptfs_copy_mount_wide_flags_to_inode_flags(
870 if (mount_crypt_stat->
flags
873 else if (mount_crypt_stat->
flags
879 static int ecryptfs_copy_mount_wide_sigs_to_inode_sigs(
914 static void ecryptfs_set_default_crypt_stat_vals(
918 ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat,
950 &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
952 &ecryptfs_superblock_to_private(
953 ecryptfs_inode->
i_sb)->mount_crypt_stat;
957 ecryptfs_set_default_crypt_stat_vals(crypt_stat, mount_crypt_stat);
959 ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat,
961 rc = ecryptfs_copy_mount_wide_sigs_to_inode_sigs(crypt_stat,
965 "to the inode key sigs; rc = [%d]\n", rc);
973 crypt_stat->
cipher[cipher_name_len] =
'\0';
976 ecryptfs_generate_new_key(crypt_stat);
980 "context for cipher [%s]: rc = [%d]\n",
992 static int ecryptfs_validate_marker(
char *
data)
1001 "MAGIC_ECRYPTFS_MARKER = [0x%.8x]\n", m_1, m_2,
1002 MAGIC_ECRYPTFS_MARKER);
1004 "[0x%.8x]\n", (m_1 ^ MAGIC_ECRYPTFS_MARKER));
1030 char *page_virt,
int *bytes_read)
1037 for (i = 0; i < ((
sizeof(ecryptfs_flag_map)
1039 if (flags & ecryptfs_flag_map[i].
file_flag) {
1056 static void write_ecryptfs_marker(
char *page_virt,
size_t *written)
1075 for (i = 0; i < ((
sizeof(ecryptfs_flag_map)
1094 ecryptfs_cipher_code_str_map[] = {
1117 ecryptfs_cipher_code_str_map;
1119 if (
strcmp(cipher_name,
"aes") == 0) {
1120 switch (key_bytes) {
1131 for (i = 0; i <
ARRAY_SIZE(ecryptfs_cipher_code_str_map); i++)
1153 for (i = 0; i <
ARRAY_SIZE(ecryptfs_cipher_code_str_map); i++)
1154 if (cipher_code == ecryptfs_cipher_code_str_map[i].cipher_code)
1156 if (str[0] ==
'\0') {
1158 "[%d]\n", cipher_code);
1174 rc = ecryptfs_validate_marker(marker);
1185 u32 header_extent_size;
1186 u16 num_header_extents_at_front;
1189 num_header_extents_at_front =
1193 put_unaligned_be16(num_header_extents_at_front, virt);
1230 static int ecryptfs_write_headers_virt(
char *page_virt,
size_t max,
1233 struct dentry *ecryptfs_dentry)
1240 write_ecryptfs_marker((page_virt + offset), &written);
1249 ecryptfs_dentry, &written,
1253 "set; rc = [%d]\n", rc);
1262 ecryptfs_write_metadata_to_contents(
struct inode *ecryptfs_inode,
1263 char *virt,
size_t virt_len)
1271 "information to lower file; rc = [%d]\n", __func__, rc);
1278 ecryptfs_write_metadata_to_xattr(
struct dentry *ecryptfs_dentry,
1279 char *page_virt,
size_t size)
1288 static unsigned long ecryptfs_get_zeroed_pages(
gfp_t gfp_mask,
1313 struct inode *ecryptfs_inode)
1316 &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
1338 virt = (
char *)ecryptfs_get_zeroed_pages(
GFP_KERNEL, order);
1345 rc = ecryptfs_write_headers_virt(virt, virt_len, &size, crypt_stat,
1353 rc = ecryptfs_write_metadata_to_xattr(ecryptfs_dentry, virt,
1356 rc = ecryptfs_write_metadata_to_contents(ecryptfs_inode, virt,
1360 "rc = [%d]\n", __func__, rc);
1369 #define ECRYPTFS_DONT_VALIDATE_HEADER_SIZE 0
1370 #define ECRYPTFS_VALIDATE_HEADER_SIZE 1
1372 char *virt,
int *bytes_read,
1373 int validate_header_size)
1376 u32 header_extent_size;
1377 u16 num_header_extents_at_front;
1381 num_header_extents_at_front = get_unaligned_be16(virt);
1383 * (
size_t)header_extent_size));
1414 crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat;
1416 &ecryptfs_superblock_to_private(inode->
i_sb)->mount_crypt_stat;
1418 file_size = i_size_read(ecryptfs_inode_to_lower(inode));
1423 i_size_write(inode, (loff_t)file_size);
1439 static int ecryptfs_read_headers_virt(
char *page_virt,
1441 struct dentry *ecryptfs_dentry,
1442 int validate_header_size)
1450 ecryptfs_dentry->
d_sb)->mount_crypt_stat;
1452 rc = ecryptfs_validate_marker(page_virt + offset);
1458 rc = ecryptfs_process_flags(crypt_stat, (page_virt + offset),
1466 "file version [%d] is supported by this "
1467 "version of eCryptfs\n",
1473 offset += bytes_read;
1475 rc = parse_header_metadata(crypt_stat, (page_virt + offset),
1476 &bytes_read, validate_header_size);
1479 "metadata; rc = [%d]\n", rc);
1481 offset += bytes_read;
1483 set_default_header_data(crypt_stat);
1502 struct dentry *lower_dentry =
1503 ecryptfs_inode_to_private(ecryptfs_inode)->lower_file->f_dentry;
1512 "xattr from the lower file; return value = "
1533 rc = ecryptfs_validate_marker(marker);
1555 struct inode *ecryptfs_inode = ecryptfs_dentry->
d_inode;
1557 &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
1559 &ecryptfs_superblock_to_private(
1560 ecryptfs_dentry->
d_sb)->mount_crypt_stat;
1562 ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat,
1575 rc = ecryptfs_read_headers_virt(page_virt, crypt_stat,
1584 "file header region or xattr region, inode %lu\n",
1585 ecryptfs_inode->
i_ino);
1589 rc = ecryptfs_read_headers_virt(page_virt, crypt_stat,
1594 "file xattr region either, inode %lu\n",
1595 ecryptfs_inode->
i_ino);
1603 "crypto metadata only in the extended attribute "
1604 "region, but eCryptfs was mounted without "
1605 "xattr support enabled. eCryptfs will not treat "
1606 "this like an encrypted file, inode %lu\n",
1607 ecryptfs_inode->
i_ino);
1638 || (mount_crypt_stat && (mount_crypt_stat->
flags
1641 size_t remaining_bytes;
1646 mount_crypt_stat,
NULL,
1650 "size for tag 72; rc = [%d]\n", __func__,
1659 "to kmalloc [%zd] bytes\n", __func__,
1673 "tag 70 packet; rc = [%d]\n", __func__,
1683 "encryption method in this release\n", __func__);
1691 static int ecryptfs_copy_filename(
char **copied_name,
size_t *copied_name_size,
1692 const char *
name,
size_t name_size)
1697 if (!(*copied_name)) {
1701 memcpy((
void *)(*copied_name), (
void *)name, name_size);
1702 (*copied_name)[(name_size)] =
'\0';
1706 (*copied_name_size) = name_size;
1723 char *cipher_name,
size_t *
key_size)
1726 char *full_alg_name =
NULL;
1736 rc = ecryptfs_crypto_api_algify_cipher_name(&full_alg_name, cipher_name,
1741 if (IS_ERR(*key_tfm)) {
1742 rc = PTR_ERR(*key_tfm);
1744 "[%s]; rc = [%d]\n", full_alg_name, rc);
1748 if (*key_size == 0) {
1754 rc = crypto_blkcipher_setkey(*key_tfm, dummy_key, *key_size);
1757 "cipher [%s]; rc = [%d]\n", *key_size, full_alg_name,
1763 kfree(full_alg_name);
1774 INIT_LIST_HEAD(&key_tfm_list);
1792 crypto_free_blkcipher(key_tfm->
key_tfm);
1809 if (key_tfm !=
NULL)
1810 (*key_tfm) = tmp_tfm;
1814 "ecryptfs_key_tfm_cache\n");
1822 rc = ecryptfs_process_key_cipher(&tmp_tfm->
key_tfm,
1827 "cipher with name = [%s]; rc = [%d]\n",
1830 if (key_tfm !=
NULL)
1858 (*key_tfm) = tmp_key_tfm;
1879 struct mutex **tfm_mutex,
1886 (*tfm_mutex) =
NULL;
1905 static unsigned char *portable_filename_chars = (
"-.0123456789ABCD"
1908 "klmnopqrstuvwxyz");
1912 static const unsigned char filename_rev_map[256] = {
1913 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1914 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1915 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1916 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1917 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1918 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00,
1919 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
1920 0x0A, 0x0B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1921 0x00, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12,
1922 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A,
1923 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, 0x21, 0x22,
1924 0x23, 0x24, 0x25, 0x00, 0x00, 0x00, 0x00, 0x00,
1925 0x00, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C,
1926 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34,
1927 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C,
1939 unsigned char *src,
size_t src_size)
1942 size_t block_num = 0;
1943 size_t dst_offset = 0;
1944 unsigned char last_block[3];
1946 if (src_size == 0) {
1950 num_blocks = (src_size / 3);
1951 if ((src_size % 3) == 0) {
1952 memcpy(last_block, (&src[src_size - 3]), 3);
1955 last_block[2] = 0x00;
1956 switch (src_size % 3) {
1958 last_block[0] = src[src_size - 1];
1959 last_block[1] = 0x00;
1962 last_block[0] = src[src_size - 2];
1963 last_block[1] = src[src_size - 1];
1966 (*dst_size) = (num_blocks * 4);
1969 while (block_num < num_blocks) {
1970 unsigned char *src_block;
1971 unsigned char dst_block[4];
1973 if (block_num == (num_blocks - 1))
1974 src_block = last_block;
1976 src_block = &src[block_num * 3];
1977 dst_block[0] = ((src_block[0] >> 2) & 0x3F);
1978 dst_block[1] = (((src_block[0] << 4) & 0x30)
1979 | ((src_block[1] >> 4) & 0x0F));
1980 dst_block[2] = (((src_block[1] << 2) & 0x3C)
1981 | ((src_block[2] >> 6) & 0x03));
1982 dst_block[3] = (src_block[2] & 0x3F);
1983 dst[dst_offset++] = portable_filename_chars[dst_block[0]];
1984 dst[dst_offset++] = portable_filename_chars[dst_block[1]];
1985 dst[dst_offset++] = portable_filename_chars[dst_block[2]];
1986 dst[dst_offset++] = portable_filename_chars[dst_block[3]];
1993 static size_t ecryptfs_max_decoded_size(
size_t encoded_size)
2001 return ((encoded_size + 1) * 3) / 4;
2014 ecryptfs_decode_from_filename(
unsigned char *dst,
size_t *
dst_size,
2015 const unsigned char *src,
size_t src_size)
2017 u8 current_bit_offset = 0;
2018 size_t src_byte_offset = 0;
2019 size_t dst_byte_offset = 0;
2022 (*dst_size) = ecryptfs_max_decoded_size(src_size);
2025 while (src_byte_offset < src_size) {
2026 unsigned char src_byte =
2027 filename_rev_map[(
int)src[src_byte_offset]];
2029 switch (current_bit_offset) {
2031 dst[dst_byte_offset] = (src_byte << 2);
2032 current_bit_offset = 6;
2035 dst[dst_byte_offset++] |= (src_byte >> 4);
2036 dst[dst_byte_offset] = ((src_byte & 0xF)
2038 current_bit_offset = 4;
2041 dst[dst_byte_offset++] |= (src_byte >> 2);
2042 dst[dst_byte_offset] = (src_byte << 6);
2043 current_bit_offset = 2;
2046 dst[dst_byte_offset++] |= (src_byte);
2047 dst[dst_byte_offset] = 0;
2048 current_bit_offset = 0;
2053 (*dst_size) = dst_byte_offset;
2074 char **encoded_name,
2075 size_t *encoded_name_size,
2078 const char *name,
size_t name_size)
2080 size_t encoded_name_no_prefix_size;
2083 (*encoded_name) =
NULL;
2084 (*encoded_name_size) = 0;
2086 || (mount_crypt_stat && (mount_crypt_stat->
flags
2090 filename = kzalloc(
sizeof(*filename),
GFP_KERNEL);
2093 "to kzalloc [%zd] bytes\n", __func__,
2100 rc = ecryptfs_encrypt_filename(filename, crypt_stat,
2104 "filename; rc = [%d]\n", __func__, rc);
2109 NULL, &encoded_name_no_prefix_size,
2112 if ((crypt_stat && (crypt_stat->
flags
2114 || (mount_crypt_stat
2115 && (mount_crypt_stat->
flags
2117 (*encoded_name_size) =
2119 + encoded_name_no_prefix_size);
2121 (*encoded_name_size) =
2123 + encoded_name_no_prefix_size);
2125 if (!(*encoded_name)) {
2127 "to kzalloc [%zd] bytes\n", __func__,
2128 (*encoded_name_size));
2134 if ((crypt_stat && (crypt_stat->
flags
2136 || (mount_crypt_stat
2137 && (mount_crypt_stat->
flags
2145 &encoded_name_no_prefix_size,
2148 (*encoded_name_size) =
2150 + encoded_name_no_prefix_size);
2151 (*encoded_name)[(*encoded_name_size)] =
'\0';
2157 "encrypted filename; rc = [%d]\n", __func__,
2159 kfree((*encoded_name));
2160 (*encoded_name) =
NULL;
2161 (*encoded_name_size) = 0;
2166 rc = ecryptfs_copy_filename(encoded_name,
2187 size_t *plaintext_name_size,
2188 struct dentry *ecryptfs_dir_dentry,
2189 const char *name,
size_t name_size)
2192 &ecryptfs_superblock_to_private(
2193 ecryptfs_dir_dentry->
d_sb)->mount_crypt_stat;
2195 size_t decoded_name_size;
2204 const char *orig_name =
name;
2205 size_t orig_name_size = name_size;
2209 ecryptfs_decode_from_filename(
NULL, &decoded_name_size,
2212 if (!decoded_name) {
2214 "to kmalloc [%zd] bytes\n", __func__,
2219 ecryptfs_decode_from_filename(decoded_name, &decoded_name_size,
2222 plaintext_name_size,
2229 "from filename; copying through filename "
2230 "as-is\n", __func__);
2231 rc = ecryptfs_copy_filename(plaintext_name,
2232 plaintext_name_size,
2233 orig_name, orig_name_size);
2237 rc = ecryptfs_copy_filename(plaintext_name,
2238 plaintext_name_size,
2243 kfree(decoded_name);
2248 #define ENC_NAME_MAX_BLOCKLEN_8_OR_16 143
2254 struct mutex *tfm_mutex;
2255 size_t cipher_blocksize;
2259 (*namelen) = lower_namelen;
2271 cipher_blocksize = crypto_blkcipher_blocksize(desc.
tfm);
2276 && (cipher_blocksize == 8 || cipher_blocksize == 16)) {
2282 (*namelen) = lower_namelen;
2285 (*namelen) = ecryptfs_max_decoded_size(*namelen) - 3;
2289 (*namelen) -= cipher_blocksize - 1;