diff options
Diffstat (limited to 'fs/ecryptfs/crypto.c')
-rw-r--r-- | fs/ecryptfs/crypto.c | 74 |
1 files changed, 32 insertions, 42 deletions
diff --git a/fs/ecryptfs/crypto.c b/fs/ecryptfs/crypto.c index b8d5c8091024..58609bde3b9f 100644 --- a/fs/ecryptfs/crypto.c +++ b/fs/ecryptfs/crypto.c | |||
@@ -1024,25 +1024,25 @@ out: | |||
1024 | } | 1024 | } |
1025 | 1025 | ||
1026 | /** | 1026 | /** |
1027 | * contains_ecryptfs_marker - check for the ecryptfs marker | 1027 | * ecryptfs_validate_marker - check for the ecryptfs marker |
1028 | * @data: The data block in which to check | 1028 | * @data: The data block in which to check |
1029 | * | 1029 | * |
1030 | * Returns one if marker found; zero if not found | 1030 | * Returns zero if marker found; -EINVAL if not found |
1031 | */ | 1031 | */ |
1032 | static int contains_ecryptfs_marker(char *data) | 1032 | static int ecryptfs_validate_marker(char *data) |
1033 | { | 1033 | { |
1034 | u32 m_1, m_2; | 1034 | u32 m_1, m_2; |
1035 | 1035 | ||
1036 | m_1 = get_unaligned_be32(data); | 1036 | m_1 = get_unaligned_be32(data); |
1037 | m_2 = get_unaligned_be32(data + 4); | 1037 | m_2 = get_unaligned_be32(data + 4); |
1038 | if ((m_1 ^ MAGIC_ECRYPTFS_MARKER) == m_2) | 1038 | if ((m_1 ^ MAGIC_ECRYPTFS_MARKER) == m_2) |
1039 | return 1; | 1039 | return 0; |
1040 | ecryptfs_printk(KERN_DEBUG, "m_1 = [0x%.8x]; m_2 = [0x%.8x]; " | 1040 | ecryptfs_printk(KERN_DEBUG, "m_1 = [0x%.8x]; m_2 = [0x%.8x]; " |
1041 | "MAGIC_ECRYPTFS_MARKER = [0x%.8x]\n", m_1, m_2, | 1041 | "MAGIC_ECRYPTFS_MARKER = [0x%.8x]\n", m_1, m_2, |
1042 | MAGIC_ECRYPTFS_MARKER); | 1042 | MAGIC_ECRYPTFS_MARKER); |
1043 | ecryptfs_printk(KERN_DEBUG, "(m_1 ^ MAGIC_ECRYPTFS_MARKER) = " | 1043 | ecryptfs_printk(KERN_DEBUG, "(m_1 ^ MAGIC_ECRYPTFS_MARKER) = " |
1044 | "[0x%.8x]\n", (m_1 ^ MAGIC_ECRYPTFS_MARKER)); | 1044 | "[0x%.8x]\n", (m_1 ^ MAGIC_ECRYPTFS_MARKER)); |
1045 | return 0; | 1045 | return -EINVAL; |
1046 | } | 1046 | } |
1047 | 1047 | ||
1048 | struct ecryptfs_flag_map_elem { | 1048 | struct ecryptfs_flag_map_elem { |
@@ -1201,27 +1201,19 @@ int ecryptfs_cipher_code_to_string(char *str, u8 cipher_code) | |||
1201 | return rc; | 1201 | return rc; |
1202 | } | 1202 | } |
1203 | 1203 | ||
1204 | int ecryptfs_read_and_validate_header_region(char *data, | 1204 | int ecryptfs_read_and_validate_header_region(struct inode *inode) |
1205 | struct inode *ecryptfs_inode) | ||
1206 | { | 1205 | { |
1207 | struct ecryptfs_crypt_stat *crypt_stat = | 1206 | u8 file_size[ECRYPTFS_SIZE_AND_MARKER_BYTES]; |
1208 | &(ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat); | 1207 | u8 *marker = file_size + ECRYPTFS_FILE_SIZE_BYTES; |
1209 | int rc; | 1208 | int rc; |
1210 | 1209 | ||
1211 | if (crypt_stat->extent_size == 0) | 1210 | rc = ecryptfs_read_lower(file_size, 0, ECRYPTFS_SIZE_AND_MARKER_BYTES, |
1212 | crypt_stat->extent_size = ECRYPTFS_DEFAULT_EXTENT_SIZE; | 1211 | inode); |
1213 | rc = ecryptfs_read_lower(data, 0, crypt_stat->extent_size, | 1212 | if (rc < ECRYPTFS_SIZE_AND_MARKER_BYTES) |
1214 | ecryptfs_inode); | 1213 | return rc >= 0 ? -EINVAL : rc; |
1215 | if (rc < 0) { | 1214 | rc = ecryptfs_validate_marker(marker); |
1216 | printk(KERN_ERR "%s: Error reading header region; rc = [%d]\n", | 1215 | if (!rc) |
1217 | __func__, rc); | 1216 | ecryptfs_i_size_init(file_size, inode); |
1218 | goto out; | ||
1219 | } | ||
1220 | if (!contains_ecryptfs_marker(data + ECRYPTFS_FILE_SIZE_BYTES)) { | ||
1221 | rc = -EINVAL; | ||
1222 | } else | ||
1223 | rc = 0; | ||
1224 | out: | ||
1225 | return rc; | 1217 | return rc; |
1226 | } | 1218 | } |
1227 | 1219 | ||
@@ -1242,8 +1234,7 @@ ecryptfs_write_header_metadata(char *virt, | |||
1242 | (*written) = 6; | 1234 | (*written) = 6; |
1243 | } | 1235 | } |
1244 | 1236 | ||
1245 | struct kmem_cache *ecryptfs_header_cache_1; | 1237 | struct kmem_cache *ecryptfs_header_cache; |
1246 | struct kmem_cache *ecryptfs_header_cache_2; | ||
1247 | 1238 | ||
1248 | /** | 1239 | /** |
1249 | * ecryptfs_write_headers_virt | 1240 | * ecryptfs_write_headers_virt |
@@ -1496,11 +1487,9 @@ static int ecryptfs_read_headers_virt(char *page_virt, | |||
1496 | crypt_stat->mount_crypt_stat = &ecryptfs_superblock_to_private( | 1487 | crypt_stat->mount_crypt_stat = &ecryptfs_superblock_to_private( |
1497 | ecryptfs_dentry->d_sb)->mount_crypt_stat; | 1488 | ecryptfs_dentry->d_sb)->mount_crypt_stat; |
1498 | offset = ECRYPTFS_FILE_SIZE_BYTES; | 1489 | offset = ECRYPTFS_FILE_SIZE_BYTES; |
1499 | rc = contains_ecryptfs_marker(page_virt + offset); | 1490 | rc = ecryptfs_validate_marker(page_virt + offset); |
1500 | if (rc == 0) { | 1491 | if (rc) |
1501 | rc = -EINVAL; | ||
1502 | goto out; | 1492 | goto out; |
1503 | } | ||
1504 | if (!(crypt_stat->flags & ECRYPTFS_I_SIZE_INITIALIZED)) | 1493 | if (!(crypt_stat->flags & ECRYPTFS_I_SIZE_INITIALIZED)) |
1505 | ecryptfs_i_size_init(page_virt, ecryptfs_dentry->d_inode); | 1494 | ecryptfs_i_size_init(page_virt, ecryptfs_dentry->d_inode); |
1506 | offset += MAGIC_ECRYPTFS_MARKER_SIZE_BYTES; | 1495 | offset += MAGIC_ECRYPTFS_MARKER_SIZE_BYTES; |
@@ -1567,20 +1556,21 @@ out: | |||
1567 | return rc; | 1556 | return rc; |
1568 | } | 1557 | } |
1569 | 1558 | ||
1570 | int ecryptfs_read_and_validate_xattr_region(char *page_virt, | 1559 | int ecryptfs_read_and_validate_xattr_region(struct dentry *dentry, |
1571 | struct dentry *ecryptfs_dentry) | 1560 | struct inode *inode) |
1572 | { | 1561 | { |
1562 | u8 file_size[ECRYPTFS_SIZE_AND_MARKER_BYTES]; | ||
1563 | u8 *marker = file_size + ECRYPTFS_FILE_SIZE_BYTES; | ||
1573 | int rc; | 1564 | int rc; |
1574 | 1565 | ||
1575 | rc = ecryptfs_read_xattr_region(page_virt, ecryptfs_dentry->d_inode); | 1566 | rc = ecryptfs_getxattr_lower(ecryptfs_dentry_to_lower(dentry), |
1576 | if (rc) | 1567 | ECRYPTFS_XATTR_NAME, file_size, |
1577 | goto out; | 1568 | ECRYPTFS_SIZE_AND_MARKER_BYTES); |
1578 | if (!contains_ecryptfs_marker(page_virt + ECRYPTFS_FILE_SIZE_BYTES)) { | 1569 | if (rc < ECRYPTFS_SIZE_AND_MARKER_BYTES) |
1579 | printk(KERN_WARNING "Valid data found in [%s] xattr, but " | 1570 | return rc >= 0 ? -EINVAL : rc; |
1580 | "the marker is invalid\n", ECRYPTFS_XATTR_NAME); | 1571 | rc = ecryptfs_validate_marker(marker); |
1581 | rc = -EINVAL; | 1572 | if (!rc) |
1582 | } | 1573 | ecryptfs_i_size_init(file_size, inode); |
1583 | out: | ||
1584 | return rc; | 1574 | return rc; |
1585 | } | 1575 | } |
1586 | 1576 | ||
@@ -1610,7 +1600,7 @@ int ecryptfs_read_metadata(struct dentry *ecryptfs_dentry) | |||
1610 | ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat, | 1600 | ecryptfs_copy_mount_wide_flags_to_inode_flags(crypt_stat, |
1611 | mount_crypt_stat); | 1601 | mount_crypt_stat); |
1612 | /* Read the first page from the underlying file */ | 1602 | /* Read the first page from the underlying file */ |
1613 | page_virt = kmem_cache_alloc(ecryptfs_header_cache_1, GFP_USER); | 1603 | page_virt = kmem_cache_alloc(ecryptfs_header_cache, GFP_USER); |
1614 | if (!page_virt) { | 1604 | if (!page_virt) { |
1615 | rc = -ENOMEM; | 1605 | rc = -ENOMEM; |
1616 | printk(KERN_ERR "%s: Unable to allocate page_virt\n", | 1606 | printk(KERN_ERR "%s: Unable to allocate page_virt\n", |
@@ -1655,7 +1645,7 @@ int ecryptfs_read_metadata(struct dentry *ecryptfs_dentry) | |||
1655 | out: | 1645 | out: |
1656 | if (page_virt) { | 1646 | if (page_virt) { |
1657 | memset(page_virt, 0, PAGE_CACHE_SIZE); | 1647 | memset(page_virt, 0, PAGE_CACHE_SIZE); |
1658 | kmem_cache_free(ecryptfs_header_cache_1, page_virt); | 1648 | kmem_cache_free(ecryptfs_header_cache, page_virt); |
1659 | } | 1649 | } |
1660 | return rc; | 1650 | return rc; |
1661 | } | 1651 | } |