Skip to content

Commit 72b55ff

Browse files
Michael Halcrowtorvalds
authored andcommitted
eCryptfs: do not try to open device files on mknod
When creating device nodes, eCryptfs needs to delay actually opening the lower persistent file until an application tries to open. Device handles may not be backed by anything when they first come into existence. [Valdis.Kletnieks@vt.edu: build fix] Signed-off-by: Michael Halcrow <mhalcrow@us.ibm.com> Cc: <Valdis.Kletnieks@vt.edu} Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 0a688ad commit 72b55ff

4 files changed

Lines changed: 42 additions & 13 deletions

File tree

‎fs/ecryptfs/ecryptfs_kernel.h‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ struct ecryptfs_crypt_stat {
235235
#define ECRYPTFS_METADATA_IN_XATTR 0x00000100
236236
#define ECRYPTFS_VIEW_AS_ENCRYPTED 0x00000200
237237
#define ECRYPTFS_KEY_SET 0x00000400
238+
#define ECRYPTFS_DELAY_PERSISTENT 0x00000800
238239
u32 flags;
239240
unsigned int file_version;
240241
size_t iv_bytes;
@@ -574,9 +575,11 @@ struct ecryptfs_open_req {
574575
struct list_head kthread_ctl_list;
575576
};
576577

578+
#define ECRYPTFS_INTERPOSE_FLAG_D_ADD 0x00000001
579+
#define ECRYPTFS_INTERPOSE_FLAG_DELAY_PERSISTENT_FILE 0x00000002
577580
int ecryptfs_interpose(struct dentry *hidden_dentry,
578581
struct dentry *this_dentry, struct super_block *sb,
579-
int flag);
582+
u32 flags);
580583
int ecryptfs_fill_zeros(struct file *file, loff_t new_length);
581584
int ecryptfs_decode_filename(struct ecryptfs_crypt_stat *crypt_stat,
582585
const char *name, int length,
@@ -709,5 +712,6 @@ void ecryptfs_destroy_kthread(void);
709712
int ecryptfs_privileged_open(struct file **lower_file,
710713
struct dentry *lower_dentry,
711714
struct vfsmount *lower_mnt);
715+
int ecryptfs_init_persistent_file(struct dentry *ecryptfs_dentry);
712716

713717
#endif /* #ifndef ECRYPTFS_KERNEL_H */

‎fs/ecryptfs/file.c‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,20 @@ static int ecryptfs_open(struct inode *inode, struct file *file)
199199
"file must hence be opened RO\n", __func__);
200200
goto out;
201201
}
202+
if (!ecryptfs_inode_to_private(inode)->lower_file) {
203+
BUG_ON(!(crypt_stat->flags & ECRYPTFS_DELAY_PERSISTENT));
204+
mutex_lock(&crypt_stat->cs_mutex);
205+
crypt_stat->flags &= ~(ECRYPTFS_ENCRYPTED);
206+
mutex_unlock(&crypt_stat->cs_mutex);
207+
rc = ecryptfs_init_persistent_file(ecryptfs_dentry);
208+
if (rc) {
209+
printk(KERN_ERR "%s: Error attempting to initialize "
210+
"the persistent file for the dentry with name "
211+
"[%s]; rc = [%d]\n", __func__,
212+
ecryptfs_dentry->d_name.name, rc);
213+
goto out;
214+
}
215+
}
202216
ecryptfs_set_file_lower(
203217
file, ecryptfs_inode_to_private(inode)->lower_file);
204218
if (S_ISDIR(ecryptfs_dentry->d_inode->i_mode)) {

‎fs/ecryptfs/inode.c‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,8 @@ static struct dentry *ecryptfs_lookup(struct inode *dir, struct dentry *dentry,
308308
d_add(dentry, NULL);
309309
goto out;
310310
}
311-
rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb, 1);
311+
rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb,
312+
ECRYPTFS_INTERPOSE_FLAG_D_ADD);
312313
if (rc) {
313314
ecryptfs_printk(KERN_ERR, "Error interposing\n");
314315
goto out_dput;
@@ -537,7 +538,8 @@ ecryptfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
537538
rc = vfs_mknod(lower_dir_dentry->d_inode, lower_dentry, mode, dev);
538539
if (rc || !lower_dentry->d_inode)
539540
goto out;
540-
rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb, 0);
541+
rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb,
542+
ECRYPTFS_INTERPOSE_FLAG_DELAY_PERSISTENT_FILE);
541543
if (rc)
542544
goto out;
543545
fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);

‎fs/ecryptfs/main.c‎

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ void __ecryptfs_printk(const char *fmt, ...)
117117
*
118118
* Returns zero on success; non-zero otherwise
119119
*/
120-
static int ecryptfs_init_persistent_file(struct dentry *ecryptfs_dentry)
120+
int ecryptfs_init_persistent_file(struct dentry *ecryptfs_dentry)
121121
{
122122
struct ecryptfs_inode_info *inode_info =
123123
ecryptfs_inode_to_private(ecryptfs_dentry->d_inode);
@@ -149,14 +149,14 @@ static int ecryptfs_init_persistent_file(struct dentry *ecryptfs_dentry)
149149
* @lower_dentry: Existing dentry in the lower filesystem
150150
* @dentry: ecryptfs' dentry
151151
* @sb: ecryptfs's super_block
152-
* @flag: If set to true, then d_add is called, else d_instantiate is called
152+
* @flags: flags to govern behavior of interpose procedure
153153
*
154154
* Interposes upper and lower dentries.
155155
*
156156
* Returns zero on success; non-zero otherwise
157157
*/
158158
int ecryptfs_interpose(struct dentry *lower_dentry, struct dentry *dentry,
159-
struct super_block *sb, int flag)
159+
struct super_block *sb, u32 flags)
160160
{
161161
struct inode *lower_inode;
162162
struct inode *inode;
@@ -193,20 +193,29 @@ int ecryptfs_interpose(struct dentry *lower_dentry, struct dentry *dentry,
193193
init_special_inode(inode, lower_inode->i_mode,
194194
lower_inode->i_rdev);
195195
dentry->d_op = &ecryptfs_dops;
196-
if (flag)
196+
if (flags & ECRYPTFS_INTERPOSE_FLAG_D_ADD)
197197
d_add(dentry, inode);
198198
else
199199
d_instantiate(dentry, inode);
200200
fsstack_copy_attr_all(inode, lower_inode, NULL);
201201
/* This size will be overwritten for real files w/ headers and
202202
* other metadata */
203203
fsstack_copy_inode_size(inode, lower_inode);
204-
rc = ecryptfs_init_persistent_file(dentry);
205-
if (rc) {
206-
printk(KERN_ERR "%s: Error attempting to initialize the "
207-
"persistent file for the dentry with name [%s]; "
208-
"rc = [%d]\n", __func__, dentry->d_name.name, rc);
209-
goto out;
204+
if (!(flags & ECRYPTFS_INTERPOSE_FLAG_DELAY_PERSISTENT_FILE)) {
205+
rc = ecryptfs_init_persistent_file(dentry);
206+
if (rc) {
207+
printk(KERN_ERR "%s: Error attempting to initialize "
208+
"the persistent file for the dentry with name "
209+
"[%s]; rc = [%d]\n", __func__,
210+
dentry->d_name.name, rc);
211+
goto out;
212+
}
213+
} else {
214+
struct ecryptfs_inode_info *inode_info =
215+
ecryptfs_inode_to_private(dentry->d_inode);
216+
217+
inode_info->lower_file = NULL;
218+
inode_info->crypt_stat.flags |= ECRYPTFS_DELAY_PERSISTENT;
210219
}
211220
out:
212221
return rc;

0 commit comments

Comments
 (0)