Skip to content

Commit d7cdc5f

Browse files
Michael HalcrowLinus Torvalds
authored andcommitted
eCryptfs: update metadata read/write functions
Update the metadata read/write functions and grow_file() to use the read_write.c routines. Do not open another lower file; use the persistent lower file instead. Provide a separate function for crypto.c::ecryptfs_read_xattr_region() to get to the lower xattr without having to go through the eCryptfs getxattr. Signed-off-by: Michael Halcrow <mhalcrow@us.ibm.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 4981e08 commit d7cdc5f

5 files changed

Lines changed: 114 additions & 133 deletions

File tree

‎fs/ecryptfs/crypto.c‎

Lines changed: 59 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1342,21 +1342,28 @@ static int ecryptfs_read_header_region(char *data, struct dentry *dentry,
13421342
return rc;
13431343
}
13441344

1345-
int ecryptfs_read_and_validate_header_region(char *data, struct dentry *dentry,
1346-
struct vfsmount *mnt)
1345+
int ecryptfs_read_and_validate_header_region(char *data,
1346+
struct inode *ecryptfs_inode)
13471347
{
1348+
struct ecryptfs_crypt_stat *crypt_stat =
1349+
&(ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat);
13481350
int rc;
13491351

1350-
rc = ecryptfs_read_header_region(data, dentry, mnt);
1351-
if (rc)
1352+
rc = ecryptfs_read_lower(data, 0, crypt_stat->extent_size,
1353+
ecryptfs_inode);
1354+
if (rc) {
1355+
printk(KERN_ERR "%s: Error reading header region; rc = [%d]\n",
1356+
__FUNCTION__, rc);
13521357
goto out;
1353-
if (!contains_ecryptfs_marker(data + ECRYPTFS_FILE_SIZE_BYTES))
1358+
}
1359+
if (!contains_ecryptfs_marker(data + ECRYPTFS_FILE_SIZE_BYTES)) {
13541360
rc = -EINVAL;
1361+
ecryptfs_printk(KERN_DEBUG, "Valid marker not found\n");
1362+
}
13551363
out:
13561364
return rc;
13571365
}
13581366

1359-
13601367
void
13611368
ecryptfs_write_header_metadata(char *virt,
13621369
struct ecryptfs_crypt_stat *crypt_stat,
@@ -1441,24 +1448,19 @@ static int ecryptfs_write_headers_virt(char *page_virt, size_t *size,
14411448

14421449
static int
14431450
ecryptfs_write_metadata_to_contents(struct ecryptfs_crypt_stat *crypt_stat,
1444-
struct file *lower_file, char *page_virt)
1451+
struct dentry *ecryptfs_dentry,
1452+
char *page_virt)
14451453
{
1446-
mm_segment_t oldfs;
14471454
int current_header_page;
14481455
int header_pages;
1449-
ssize_t size;
1450-
int rc = 0;
1456+
int rc;
14511457

1452-
lower_file->f_pos = 0;
1453-
oldfs = get_fs();
1454-
set_fs(get_ds());
1455-
size = vfs_write(lower_file, (char __user *)page_virt, PAGE_CACHE_SIZE,
1456-
&lower_file->f_pos);
1457-
if (size < 0) {
1458-
rc = (int)size;
1459-
printk(KERN_ERR "Error attempting to write lower page; "
1460-
"rc = [%d]\n", rc);
1461-
set_fs(oldfs);
1458+
rc = ecryptfs_write_lower(ecryptfs_dentry->d_inode, page_virt,
1459+
0, PAGE_CACHE_SIZE);
1460+
if (rc) {
1461+
printk(KERN_ERR "%s: Error attempting to write header "
1462+
"information to lower file; rc = [%d]\n", __FUNCTION__,
1463+
rc);
14621464
goto out;
14631465
}
14641466
header_pages = ((crypt_stat->extent_size
@@ -1467,18 +1469,19 @@ ecryptfs_write_metadata_to_contents(struct ecryptfs_crypt_stat *crypt_stat,
14671469
memset(page_virt, 0, PAGE_CACHE_SIZE);
14681470
current_header_page = 1;
14691471
while (current_header_page < header_pages) {
1470-
size = vfs_write(lower_file, (char __user *)page_virt,
1471-
PAGE_CACHE_SIZE, &lower_file->f_pos);
1472-
if (size < 0) {
1473-
rc = (int)size;
1474-
printk(KERN_ERR "Error attempting to write lower page; "
1475-
"rc = [%d]\n", rc);
1476-
set_fs(oldfs);
1472+
loff_t offset;
1473+
1474+
offset = (current_header_page << PAGE_CACHE_SHIFT);
1475+
if ((rc = ecryptfs_write_lower(ecryptfs_dentry->d_inode,
1476+
page_virt, offset,
1477+
PAGE_CACHE_SIZE))) {
1478+
printk(KERN_ERR "%s: Error attempting to write header "
1479+
"information to lower file; rc = [%d]\n",
1480+
__FUNCTION__, rc);
14771481
goto out;
14781482
}
14791483
current_header_page++;
14801484
}
1481-
set_fs(oldfs);
14821485
out:
14831486
return rc;
14841487
}
@@ -1498,30 +1501,28 @@ ecryptfs_write_metadata_to_xattr(struct dentry *ecryptfs_dentry,
14981501
/**
14991502
* ecryptfs_write_metadata
15001503
* @ecryptfs_dentry: The eCryptfs dentry
1501-
* @lower_file: The lower file struct, which was returned from dentry_open
15021504
*
15031505
* Write the file headers out. This will likely involve a userspace
15041506
* callout, in which the session key is encrypted with one or more
15051507
* public keys and/or the passphrase necessary to do the encryption is
15061508
* retrieved via a prompt. Exactly what happens at this point should
15071509
* be policy-dependent.
15081510
*
1511+
* TODO: Support header information spanning multiple pages
1512+
*
15091513
* Returns zero on success; non-zero on error
15101514
*/
1511-
int ecryptfs_write_metadata(struct dentry *ecryptfs_dentry,
1512-
struct file *lower_file)
1515+
int ecryptfs_write_metadata(struct dentry *ecryptfs_dentry)
15131516
{
1514-
struct ecryptfs_crypt_stat *crypt_stat;
1517+
struct ecryptfs_crypt_stat *crypt_stat =
1518+
&ecryptfs_inode_to_private(ecryptfs_dentry->d_inode)->crypt_stat;
15151519
char *page_virt;
1516-
size_t size;
1520+
size_t size = 0;
15171521
int rc = 0;
15181522

1519-
crypt_stat = &ecryptfs_inode_to_private(
1520-
ecryptfs_dentry->d_inode)->crypt_stat;
15211523
if (likely(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) {
15221524
if (!(crypt_stat->flags & ECRYPTFS_KEY_VALID)) {
1523-
ecryptfs_printk(KERN_DEBUG, "Key is "
1524-
"invalid; bailing out\n");
1525+
printk(KERN_ERR "Key is invalid; bailing out\n");
15251526
rc = -EINVAL;
15261527
goto out;
15271528
}
@@ -1550,7 +1551,8 @@ int ecryptfs_write_metadata(struct dentry *ecryptfs_dentry,
15501551
crypt_stat, page_virt,
15511552
size);
15521553
else
1553-
rc = ecryptfs_write_metadata_to_contents(crypt_stat, lower_file,
1554+
rc = ecryptfs_write_metadata_to_contents(crypt_stat,
1555+
ecryptfs_dentry,
15541556
page_virt);
15551557
if (rc) {
15561558
printk(KERN_ERR "Error writing metadata out to lower file; "
@@ -1678,15 +1680,17 @@ static int ecryptfs_read_headers_virt(char *page_virt,
16781680
*
16791681
* Returns zero on success; non-zero on error
16801682
*/
1681-
int ecryptfs_read_xattr_region(char *page_virt, struct dentry *ecryptfs_dentry)
1683+
int ecryptfs_read_xattr_region(char *page_virt, struct inode *ecryptfs_inode)
16821684
{
1685+
struct dentry *lower_dentry =
1686+
ecryptfs_inode_to_private(ecryptfs_inode)->lower_file->f_dentry;
16831687
ssize_t size;
16841688
int rc = 0;
16851689

1686-
size = ecryptfs_getxattr(ecryptfs_dentry, ECRYPTFS_XATTR_NAME,
1687-
page_virt, ECRYPTFS_DEFAULT_EXTENT_SIZE);
1690+
size = ecryptfs_getxattr_lower(lower_dentry, ECRYPTFS_XATTR_NAME,
1691+
page_virt, ECRYPTFS_DEFAULT_EXTENT_SIZE);
16881692
if (size < 0) {
1689-
printk(KERN_DEBUG "Error attempting to read the [%s] "
1693+
printk(KERN_ERR "Error attempting to read the [%s] "
16901694
"xattr from the lower file; return value = [%zd]\n",
16911695
ECRYPTFS_XATTR_NAME, size);
16921696
rc = -EINVAL;
@@ -1701,7 +1705,7 @@ int ecryptfs_read_and_validate_xattr_region(char *page_virt,
17011705
{
17021706
int rc;
17031707

1704-
rc = ecryptfs_read_xattr_region(page_virt, ecryptfs_dentry);
1708+
rc = ecryptfs_read_xattr_region(page_virt, ecryptfs_dentry->d_inode);
17051709
if (rc)
17061710
goto out;
17071711
if (!contains_ecryptfs_marker(page_virt + ECRYPTFS_FILE_SIZE_BYTES)) {
@@ -1715,8 +1719,6 @@ int ecryptfs_read_and_validate_xattr_region(char *page_virt,
17151719

17161720
/**
17171721
* ecryptfs_read_metadata
1718-
* @ecryptfs_dentry: The eCryptfs dentry
1719-
* @lower_file: The lower file from which to read the metadata
17201722
*
17211723
* Common entry point for reading file metadata. From here, we could
17221724
* retrieve the header information from the header region of the file,
@@ -1727,15 +1729,13 @@ int ecryptfs_read_and_validate_xattr_region(char *page_virt,
17271729
*
17281730
* Returns zero if valid headers found and parsed; non-zero otherwise
17291731
*/
1730-
int ecryptfs_read_metadata(struct dentry *ecryptfs_dentry,
1731-
struct file *lower_file)
1732+
int ecryptfs_read_metadata(struct dentry *ecryptfs_dentry)
17321733
{
17331734
int rc = 0;
17341735
char *page_virt = NULL;
1735-
mm_segment_t oldfs;
1736-
ssize_t bytes_read;
1736+
struct inode *ecryptfs_inode = ecryptfs_dentry->d_inode;
17371737
struct ecryptfs_crypt_stat *crypt_stat =
1738-
&ecryptfs_inode_to_private(ecryptfs_dentry->d_inode)->crypt_stat;
1738+
&ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
17391739
struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
17401740
&ecryptfs_superblock_to_private(
17411741
ecryptfs_dentry->d_sb)->mount_crypt_stat;
@@ -1746,27 +1746,18 @@ int ecryptfs_read_metadata(struct dentry *ecryptfs_dentry,
17461746
page_virt = kmem_cache_alloc(ecryptfs_header_cache_1, GFP_USER);
17471747
if (!page_virt) {
17481748
rc = -ENOMEM;
1749-
ecryptfs_printk(KERN_ERR, "Unable to allocate page_virt\n");
1749+
printk(KERN_ERR "%s: Unable to allocate page_virt\n",
1750+
__FUNCTION__);
17501751
goto out;
17511752
}
1752-
lower_file->f_pos = 0;
1753-
oldfs = get_fs();
1754-
set_fs(get_ds());
1755-
bytes_read = lower_file->f_op->read(lower_file,
1756-
(char __user *)page_virt,
1757-
ECRYPTFS_DEFAULT_EXTENT_SIZE,
1758-
&lower_file->f_pos);
1759-
set_fs(oldfs);
1760-
if (bytes_read != ECRYPTFS_DEFAULT_EXTENT_SIZE) {
1761-
rc = -EINVAL;
1762-
goto out;
1763-
}
1764-
rc = ecryptfs_read_headers_virt(page_virt, crypt_stat,
1765-
ecryptfs_dentry,
1766-
ECRYPTFS_VALIDATE_HEADER_SIZE);
1753+
rc = ecryptfs_read_lower(page_virt, 0, crypt_stat->extent_size,
1754+
ecryptfs_inode);
1755+
if (!rc)
1756+
rc = ecryptfs_read_headers_virt(page_virt, crypt_stat,
1757+
ecryptfs_dentry,
1758+
ECRYPTFS_VALIDATE_HEADER_SIZE);
17671759
if (rc) {
1768-
rc = ecryptfs_read_xattr_region(page_virt,
1769-
ecryptfs_dentry);
1760+
rc = ecryptfs_read_xattr_region(page_virt, ecryptfs_inode);
17701761
if (rc) {
17711762
printk(KERN_DEBUG "Valid eCryptfs headers not found in "
17721763
"file header region or xattr region\n");

‎fs/ecryptfs/ecryptfs_kernel.h‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -570,13 +570,11 @@ int ecryptfs_writepage_and_release_lower_page(struct page *lower_page,
570570
struct writeback_control *wbc);
571571
int ecryptfs_encrypt_page(struct page *page);
572572
int ecryptfs_decrypt_page(struct page *page);
573-
int ecryptfs_write_metadata(struct dentry *ecryptfs_dentry,
574-
struct file *lower_file);
575-
int ecryptfs_read_metadata(struct dentry *ecryptfs_dentry,
576-
struct file *lower_file);
573+
int ecryptfs_write_metadata(struct dentry *ecryptfs_dentry);
574+
int ecryptfs_read_metadata(struct dentry *ecryptfs_dentry);
577575
int ecryptfs_new_file_context(struct dentry *ecryptfs_dentry);
578-
int ecryptfs_read_and_validate_header_region(char *data, struct dentry *dentry,
579-
struct vfsmount *mnt);
576+
int ecryptfs_read_and_validate_header_region(char *data,
577+
struct inode *ecryptfs_inode);
580578
int ecryptfs_read_and_validate_xattr_region(char *page_virt,
581579
struct dentry *ecryptfs_dentry);
582580
u16 ecryptfs_code_for_cipher_string(struct ecryptfs_crypt_stat *crypt_stat);
@@ -599,10 +597,13 @@ int ecryptfs_open_lower_file(struct file **lower_file,
599597
int ecryptfs_close_lower_file(struct file *lower_file);
600598
ssize_t ecryptfs_getxattr(struct dentry *dentry, const char *name, void *value,
601599
size_t size);
600+
ssize_t
601+
ecryptfs_getxattr_lower(struct dentry *lower_dentry, const char *name,
602+
void *value, size_t size);
602603
int
603604
ecryptfs_setxattr(struct dentry *dentry, const char *name, const void *value,
604605
size_t size, int flags);
605-
int ecryptfs_read_xattr_region(char *page_virt, struct dentry *ecryptfs_dentry);
606+
int ecryptfs_read_xattr_region(char *page_virt, struct inode *ecryptfs_inode);
606607
int ecryptfs_process_helo(unsigned int transport, uid_t uid, pid_t pid);
607608
int ecryptfs_process_quit(uid_t uid, pid_t pid);
608609
int ecryptfs_process_response(struct ecryptfs_message *msg, uid_t uid,

‎fs/ecryptfs/file.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ static int ecryptfs_open(struct inode *inode, struct file *file)
248248
mutex_lock(&crypt_stat->cs_mutex);
249249
if (!(crypt_stat->flags & ECRYPTFS_POLICY_APPLIED)
250250
|| !(crypt_stat->flags & ECRYPTFS_KEY_VALID)) {
251-
rc = ecryptfs_read_metadata(ecryptfs_dentry, lower_file);
251+
rc = ecryptfs_read_metadata(ecryptfs_dentry);
252252
if (rc) {
253253
ecryptfs_printk(KERN_DEBUG,
254254
"Valid headers not found\n");

0 commit comments

Comments
 (0)