Skip to content

Commit e709e9d

Browse files
committed
ext4 crypto: encrypt tmpfile located in encryption protected directory
Factor out calls to ext4_inherit_context() and move them to __ext4_new_inode(); this fixes a problem where ext4_tmpfile() wasn't calling calling ext4_inherit_context(), so the temporary file wasn't getting protected. Since the blocks for the tmpfile could end up on disk, they really should be protected if the tmpfile is created within the context of an encrypted directory. Signed-off-by: Theodore Ts'o <tytso@mit.edu>
1 parent 6bc445e commit e709e9d

3 files changed

Lines changed: 30 additions & 34 deletions

File tree

‎fs/ext4/ext4.h‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2149,6 +2149,11 @@ static inline int ext4_get_encryption_info(struct inode *inode)
21492149
return 0;
21502150
}
21512151

2152+
static inline struct ext4_crypt_info *ext4_encryption_info(struct inode *inode)
2153+
{
2154+
return EXT4_I(inode)->i_crypt_info;
2155+
}
2156+
21522157
#else
21532158
static inline int ext4_has_encryption_key(struct inode *inode)
21542159
{
@@ -2158,6 +2163,10 @@ static inline int ext4_get_encryption_info(struct inode *inode)
21582163
{
21592164
return 0;
21602165
}
2166+
static inline struct ext4_crypt_info *ext4_encryption_info(struct inode *inode)
2167+
{
2168+
return NULL;
2169+
}
21612170
#endif
21622171

21632172

‎fs/ext4/ialloc.c‎

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -726,11 +726,25 @@ struct inode *__ext4_new_inode(handle_t *handle, struct inode *dir,
726726
ext4_group_t i;
727727
ext4_group_t flex_group;
728728
struct ext4_group_info *grp;
729+
int encrypt = 0;
729730

730731
/* Cannot create files in a deleted directory */
731732
if (!dir || !dir->i_nlink)
732733
return ERR_PTR(-EPERM);
733734

735+
if ((ext4_encrypted_inode(dir) ||
736+
DUMMY_ENCRYPTION_ENABLED(EXT4_SB(dir->i_sb))) &&
737+
(S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
738+
err = ext4_get_encryption_info(dir);
739+
if (err)
740+
return ERR_PTR(err);
741+
if (ext4_encryption_info(dir) == NULL)
742+
return ERR_PTR(-EPERM);
743+
if (!handle)
744+
nblocks += EXT4_DATA_TRANS_BLOCKS(dir->i_sb);
745+
encrypt = 1;
746+
}
747+
734748
sb = dir->i_sb;
735749
ngroups = ext4_get_groups_count(sb);
736750
trace_ext4_request_inode(dir, mode);
@@ -996,12 +1010,6 @@ struct inode *__ext4_new_inode(handle_t *handle, struct inode *dir,
9961010
ei->i_block_group = group;
9971011
ei->i_last_alloc_group = ~0;
9981012

999-
/* If the directory encrypted, then we should encrypt the inode. */
1000-
if ((S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode)) &&
1001-
(ext4_encrypted_inode(dir) ||
1002-
DUMMY_ENCRYPTION_ENABLED(sbi)))
1003-
ext4_set_inode_flag(inode, EXT4_INODE_ENCRYPT);
1004-
10051013
ext4_set_inode_flags(inode);
10061014
if (IS_DIRSYNC(inode))
10071015
ext4_handle_sync(handle);
@@ -1063,6 +1071,12 @@ struct inode *__ext4_new_inode(handle_t *handle, struct inode *dir,
10631071
ei->i_datasync_tid = handle->h_transaction->t_tid;
10641072
}
10651073

1074+
if (encrypt) {
1075+
err = ext4_inherit_context(dir, inode);
1076+
if (err)
1077+
goto fail_free_drop;
1078+
}
1079+
10661080
err = ext4_mark_inode_dirty(handle, inode);
10671081
if (err) {
10681082
ext4_std_error(sb, err);

‎fs/ext4/namei.c‎

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2437,20 +2437,7 @@ static int ext4_create(struct inode *dir, struct dentry *dentry, umode_t mode,
24372437
inode->i_op = &ext4_file_inode_operations;
24382438
inode->i_fop = &ext4_file_operations;
24392439
ext4_set_aops(inode);
2440-
err = 0;
2441-
#ifdef CONFIG_EXT4_FS_ENCRYPTION
2442-
if (!err && (ext4_encrypted_inode(dir) ||
2443-
DUMMY_ENCRYPTION_ENABLED(EXT4_SB(dir->i_sb)))) {
2444-
err = ext4_inherit_context(dir, inode);
2445-
if (err) {
2446-
clear_nlink(inode);
2447-
unlock_new_inode(inode);
2448-
iput(inode);
2449-
}
2450-
}
2451-
#endif
2452-
if (!err)
2453-
err = ext4_add_nondir(handle, dentry, inode);
2440+
err = ext4_add_nondir(handle, dentry, inode);
24542441
if (!err && IS_DIRSYNC(dir))
24552442
ext4_handle_sync(handle);
24562443
}
@@ -2631,14 +2618,6 @@ static int ext4_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
26312618
err = ext4_init_new_dir(handle, dir, inode);
26322619
if (err)
26332620
goto out_clear_inode;
2634-
#ifdef CONFIG_EXT4_FS_ENCRYPTION
2635-
if (ext4_encrypted_inode(dir) ||
2636-
DUMMY_ENCRYPTION_ENABLED(EXT4_SB(dir->i_sb))) {
2637-
err = ext4_inherit_context(dir, inode);
2638-
if (err)
2639-
goto out_clear_inode;
2640-
}
2641-
#endif
26422621
err = ext4_mark_inode_dirty(handle, inode);
26432622
if (!err)
26442623
err = ext4_add_entry(handle, dentry, inode);
@@ -3106,12 +3085,6 @@ static int ext4_symlink(struct inode *dir,
31063085
err = -ENOMEM;
31073086
goto err_drop_inode;
31083087
}
3109-
err = ext4_inherit_context(dir, inode);
3110-
if (err)
3111-
goto err_drop_inode;
3112-
err = ext4_get_encryption_info(inode);
3113-
if (err)
3114-
goto err_drop_inode;
31153088
istr.name = (const unsigned char *) symname;
31163089
istr.len = len;
31173090
ostr.name = sd->encrypted_path;

0 commit comments

Comments
 (0)