Skip to content

Commit e3ccaa9

Browse files
committed
eCryptfs: Initialize empty lower files when opening them
Historically, eCryptfs has only initialized lower files in the ecryptfs_create() path. Lower file initialization is the act of writing the cryptographic metadata from the inode's crypt_stat to the header of the file. The ecryptfs_open() path already expects that metadata to be in the header of the file. A number of users have reported empty lower files in beneath their eCryptfs mounts. Most of the causes for those empty files being left around have been addressed, but the presence of empty files causes problems due to the lack of proper cryptographic metadata. To transparently solve this problem, this patch initializes empty lower files in the ecryptfs_open() error path. If the metadata is unreadable due to the lower inode size being 0, plaintext passthrough support is not in use, and the metadata is stored in the header of the file (as opposed to the user.ecryptfs extended attribute), the lower file will be initialized. The number of nested conditionals in ecryptfs_open() was getting out of hand, so a helper function was created. To avoid the same nested conditional problem, the conditional logic was reversed inside of the helper function. https://launchpad.net/bugs/911507 Signed-off-by: Tyler Hicks <tyhicks@canonical.com> Cc: John Johansen <john.johansen@canonical.com> Cc: Colin Ian King <colin.king@canonical.com>
1 parent 8bc2d3c commit e3ccaa9

3 files changed

Lines changed: 49 additions & 28 deletions

File tree

‎fs/ecryptfs/ecryptfs_kernel.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,8 @@ struct ecryptfs_open_req {
563563
struct inode *ecryptfs_get_inode(struct inode *lower_inode,
564564
struct super_block *sb);
565565
void ecryptfs_i_size_init(const char *page_virt, struct inode *inode);
566+
int ecryptfs_initialize_file(struct dentry *ecryptfs_dentry,
567+
struct inode *ecryptfs_inode);
566568
int ecryptfs_decode_and_decrypt_filename(char **decrypted_name,
567569
size_t *decrypted_name_size,
568570
struct dentry *ecryptfs_dentry,

‎fs/ecryptfs/file.c‎

Lines changed: 45 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,48 @@ static int ecryptfs_file_mmap(struct file *file, struct vm_area_struct *vma)
161161

162162
struct kmem_cache *ecryptfs_file_info_cache;
163163

164+
static int read_or_initialize_metadata(struct dentry *dentry)
165+
{
166+
struct inode *inode = dentry->d_inode;
167+
struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
168+
struct ecryptfs_crypt_stat *crypt_stat;
169+
int rc;
170+
171+
crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat;
172+
mount_crypt_stat = &ecryptfs_superblock_to_private(
173+
inode->i_sb)->mount_crypt_stat;
174+
mutex_lock(&crypt_stat->cs_mutex);
175+
176+
if (crypt_stat->flags & ECRYPTFS_POLICY_APPLIED &&
177+
crypt_stat->flags & ECRYPTFS_KEY_VALID) {
178+
rc = 0;
179+
goto out;
180+
}
181+
182+
rc = ecryptfs_read_metadata(dentry);
183+
if (!rc)
184+
goto out;
185+
186+
if (mount_crypt_stat->flags & ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED) {
187+
crypt_stat->flags &= ~(ECRYPTFS_I_SIZE_INITIALIZED
188+
| ECRYPTFS_ENCRYPTED);
189+
rc = 0;
190+
goto out;
191+
}
192+
193+
if (!(mount_crypt_stat->flags & ECRYPTFS_XATTR_METADATA_ENABLED) &&
194+
!i_size_read(ecryptfs_inode_to_lower(inode))) {
195+
rc = ecryptfs_initialize_file(dentry, inode);
196+
if (!rc)
197+
goto out;
198+
}
199+
200+
rc = -EIO;
201+
out:
202+
mutex_unlock(&crypt_stat->cs_mutex);
203+
return rc;
204+
}
205+
164206
/**
165207
* ecryptfs_open
166208
* @inode: inode speciying file to open
@@ -236,32 +278,9 @@ static int ecryptfs_open(struct inode *inode, struct file *file)
236278
rc = 0;
237279
goto out;
238280
}
239-
mutex_lock(&crypt_stat->cs_mutex);
240-
if (!(crypt_stat->flags & ECRYPTFS_POLICY_APPLIED)
241-
|| !(crypt_stat->flags & ECRYPTFS_KEY_VALID)) {
242-
rc = ecryptfs_read_metadata(ecryptfs_dentry);
243-
if (rc) {
244-
ecryptfs_printk(KERN_DEBUG,
245-
"Valid headers not found\n");
246-
if (!(mount_crypt_stat->flags
247-
& ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED)) {
248-
rc = -EIO;
249-
printk(KERN_WARNING "Either the lower file "
250-
"is not in a valid eCryptfs format, "
251-
"or the key could not be retrieved. "
252-
"Plaintext passthrough mode is not "
253-
"enabled; returning -EIO\n");
254-
mutex_unlock(&crypt_stat->cs_mutex);
255-
goto out_put;
256-
}
257-
rc = 0;
258-
crypt_stat->flags &= ~(ECRYPTFS_I_SIZE_INITIALIZED
259-
| ECRYPTFS_ENCRYPTED);
260-
mutex_unlock(&crypt_stat->cs_mutex);
261-
goto out;
262-
}
263-
}
264-
mutex_unlock(&crypt_stat->cs_mutex);
281+
rc = read_or_initialize_metadata(ecryptfs_dentry);
282+
if (rc)
283+
goto out_put;
265284
ecryptfs_printk(KERN_DEBUG, "inode w/ addr = [0x%p], i_ino = "
266285
"[0x%.16lx] size: [0x%.16llx]\n", inode, inode->i_ino,
267286
(unsigned long long)i_size_read(inode));

‎fs/ecryptfs/inode.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,8 +227,8 @@ ecryptfs_do_create(struct inode *directory_inode,
227227
*
228228
* Returns zero on success
229229
*/
230-
static int ecryptfs_initialize_file(struct dentry *ecryptfs_dentry,
231-
struct inode *ecryptfs_inode)
230+
int ecryptfs_initialize_file(struct dentry *ecryptfs_dentry,
231+
struct inode *ecryptfs_inode)
232232
{
233233
struct ecryptfs_crypt_stat *crypt_stat =
234234
&ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;

0 commit comments

Comments
 (0)