Skip to content

Commit e86281e

Browse files
committed
eCryptfs: don't pass up plaintext names when using filename encryption
Both ecryptfs_filldir() and ecryptfs_readlink_lower() use ecryptfs_decode_and_decrypt_filename() to translate lower filenames to upper filenames. The function correctly passes up lower filenames, unchanged, when filename encryption isn't in use. However, it was also passing up lower filenames when the filename wasn't encrypted or when decryption failed. Since 88ae4ab, eCryptfs refuses to lookup lower plaintext names when filename encryption is enabled so this resulted in a situation where userspace would see lower plaintext filenames in calls to getdents(2) but then not be able to lookup those filenames. An example of this can be seen when enabling filename encryption on an eCryptfs mount at the root directory of an Ext4 filesystem: $ ls -1i /lower 12 ECRYPTFS_FNEK_ENCRYPTED.FWYZD8TcW.5FV-TKTEYOHsheiHX9a-w.NURCCYIMjI8pn5BDB9-h3fXwrE-- 11 lost+found $ ls -1i /upper ls: cannot access '/upper/lost+found': No such file or directory ? lost+found 12 test With this change, the lower lost+found dentry is ignored: $ ls -1i /lower 12 ECRYPTFS_FNEK_ENCRYPTED.FWYZD8TcW.5FV-TKTEYOHsheiHX9a-w.NURCCYIMjI8pn5BDB9-h3fXwrE-- 11 lost+found $ ls -1i /upper 12 test Additionally, some potentially noisy error/info messages in the related code paths are turned into debug messages so that the logs can't be easily filled. Fixes: 88ae4ab ("ecryptfs_lookup(): try either only encrypted or plaintext name") Reported-by: Guenter Roeck <linux@roeck-us.net> Cc: Al Viro <viro@zeniv.linux.org.uk> Signed-off-by: Tyler Hicks <tyhicks@canonical.com>
1 parent f62fd7a commit e86281e

2 files changed

Lines changed: 44 additions & 18 deletions

File tree

‎fs/ecryptfs/crypto.c‎

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1997,6 +1997,16 @@ int ecryptfs_encrypt_and_encode_filename(
19971997
return rc;
19981998
}
19991999

2000+
static bool is_dot_dotdot(const char *name, size_t name_size)
2001+
{
2002+
if (name_size == 1 && name[0] == '.')
2003+
return true;
2004+
else if (name_size == 2 && name[0] == '.' && name[1] == '.')
2005+
return true;
2006+
2007+
return false;
2008+
}
2009+
20002010
/**
20012011
* ecryptfs_decode_and_decrypt_filename - converts the encoded cipher text name to decoded plaintext
20022012
* @plaintext_name: The plaintext name
@@ -2021,13 +2031,21 @@ int ecryptfs_decode_and_decrypt_filename(char **plaintext_name,
20212031
size_t packet_size;
20222032
int rc = 0;
20232033

2024-
if ((mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)
2025-
&& !(mount_crypt_stat->flags & ECRYPTFS_ENCRYPTED_VIEW_ENABLED)
2026-
&& (name_size > ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE)
2027-
&& (strncmp(name, ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX,
2028-
ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE) == 0)) {
2029-
const char *orig_name = name;
2030-
size_t orig_name_size = name_size;
2034+
if ((mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES) &&
2035+
!(mount_crypt_stat->flags & ECRYPTFS_ENCRYPTED_VIEW_ENABLED)) {
2036+
if (is_dot_dotdot(name, name_size)) {
2037+
rc = ecryptfs_copy_filename(plaintext_name,
2038+
plaintext_name_size,
2039+
name, name_size);
2040+
goto out;
2041+
}
2042+
2043+
if (name_size <= ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE ||
2044+
strncmp(name, ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX,
2045+
ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE)) {
2046+
rc = -EINVAL;
2047+
goto out;
2048+
}
20312049

20322050
name += ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE;
20332051
name_size -= ECRYPTFS_FNEK_ENCRYPTED_FILENAME_PREFIX_SIZE;
@@ -2047,12 +2065,9 @@ int ecryptfs_decode_and_decrypt_filename(char **plaintext_name,
20472065
decoded_name,
20482066
decoded_name_size);
20492067
if (rc) {
2050-
printk(KERN_INFO "%s: Could not parse tag 70 packet "
2051-
"from filename; copying through filename "
2052-
"as-is\n", __func__);
2053-
rc = ecryptfs_copy_filename(plaintext_name,
2054-
plaintext_name_size,
2055-
orig_name, orig_name_size);
2068+
ecryptfs_printk(KERN_DEBUG,
2069+
"%s: Could not parse tag 70 packet from filename\n",
2070+
__func__);
20562071
goto out_free;
20572072
}
20582073
} else {

‎fs/ecryptfs/file.c‎

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,17 +82,28 @@ ecryptfs_filldir(struct dir_context *ctx, const char *lower_name,
8282
buf->sb, lower_name,
8383
lower_namelen);
8484
if (rc) {
85-
printk(KERN_ERR "%s: Error attempting to decode and decrypt "
86-
"filename [%s]; rc = [%d]\n", __func__, lower_name,
87-
rc);
88-
goto out;
85+
if (rc != -EINVAL) {
86+
ecryptfs_printk(KERN_DEBUG,
87+
"%s: Error attempting to decode and decrypt filename [%s]; rc = [%d]\n",
88+
__func__, lower_name, rc);
89+
return rc;
90+
}
91+
92+
/* Mask -EINVAL errors as these are most likely due a plaintext
93+
* filename present in the lower filesystem despite filename
94+
* encryption being enabled. One unavoidable example would be
95+
* the "lost+found" dentry in the root directory of an Ext4
96+
* filesystem.
97+
*/
98+
return 0;
8999
}
100+
90101
buf->caller->pos = buf->ctx.pos;
91102
rc = !dir_emit(buf->caller, name, name_size, ino, d_type);
92103
kfree(name);
93104
if (!rc)
94105
buf->entries_written++;
95-
out:
106+
96107
return rc;
97108
}
98109

0 commit comments

Comments
 (0)