android_kernel_oneplus_msm8998/fs/f2fs
Eric Biggers ae3d7b8931 fscrypt: avoid collisions when presenting long encrypted filenames
commit 6b06cdee81d68a8a829ad8e8d0f31d6836744af9 upstream.

When accessing an encrypted directory without the key, userspace must
operate on filenames derived from the ciphertext names, which contain
arbitrary bytes.  Since we must support filenames as long as NAME_MAX,
we can't always just base64-encode the ciphertext, since that may make
it too long.  Currently, this is solved by presenting long names in an
abbreviated form containing any needed filesystem-specific hashes (e.g.
to identify a directory block), then the last 16 bytes of ciphertext.
This needs to be sufficient to identify the actual name on lookup.

However, there is a bug.  It seems to have been assumed that due to the
use of a CBC (ciphertext block chaining)-based encryption mode, the last
16 bytes (i.e. the AES block size) of ciphertext would depend on the
full plaintext, preventing collisions.  However, we actually use CBC
with ciphertext stealing (CTS), which handles the last two blocks
specially, causing them to appear "flipped".  Thus, it's actually the
second-to-last block which depends on the full plaintext.

This caused long filenames that differ only near the end of their
plaintexts to, when observed without the key, point to the wrong inode
and be undeletable.  For example, with ext4:

    # echo pass | e4crypt add_key -p 16 edir/
    # seq -f "edir/abcdefghijklmnopqrstuvwxyz012345%.0f" 100000 | xargs touch
    # find edir/ -type f | xargs stat -c %i | sort | uniq | wc -l
    100000
    # sync
    # echo 3 > /proc/sys/vm/drop_caches
    # keyctl new_session
    # find edir/ -type f | xargs stat -c %i | sort | uniq | wc -l
    2004
    # rm -rf edir/
    rm: cannot remove 'edir/_A7nNFi3rhkEQlJ6P,hdzluhODKOeWx5V': Structure needs cleaning
    ...

To fix this, when presenting long encrypted filenames, encode the
second-to-last block of ciphertext rather than the last 16 bytes.

Although it would be nice to solve this without depending on a specific
encryption mode, that would mean doing a cryptographic hash like SHA-256
which would be much less efficient.  This way is sufficient for now, and
it's still compatible with encryption modes like HEH which are strong
pseudorandom permutations.  Also, changing the presented names is still
allowed at any time because they are only provided to allow applications
to do things like delete encrypted directories.  They're not designed to
be used to persistently identify files --- which would be hard to do
anyway, given that they're encrypted after all.

For ease of backports, this patch only makes the minimal fix to both
ext4 and f2fs.  It leaves ubifs as-is, since ubifs doesn't compare the
ciphertext block yet.  Follow-on patches will clean things up properly
and make the filesystems use a shared helper function.

Fixes: 5de0b4d0cd ("ext4 crypto: simplify and speed up filename encryption")
Reported-by: Gwendal Grignou <gwendal@chromium.org>
Signed-off-by: Eric Biggers <ebiggers@google.com>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2017-05-25 14:30:11 +02:00
..
acl.c posix_acl: Clear SGID bit when setting file permissions 2016-10-31 04:13:58 -06:00
acl.h f2fs: avoid deadlock on init_inode_metadata 2014-11-03 16:07:33 -08:00
checkpoint.c f2fs: support lower priority asynchronous readahead in ra_meta_pages 2015-10-12 14:03:15 -07:00
crypto.c f2fs crypto: add alloc_bounce_page 2015-06-11 15:04:20 -07:00
crypto_fname.c fscrypt: avoid collisions when presenting long encrypted filenames 2017-05-25 14:30:11 +02:00
crypto_key.c fscrypt: remove broken support for detecting keyring key revocation 2017-03-31 09:49:54 +02:00
crypto_policy.c fscrypt: fix context consistency check when key(s) unavailable 2017-05-25 14:30:11 +02:00
data.c f2fs: support fiemap for inline_data 2015-10-20 11:33:21 -07:00
debug.c f2fs: set ->owner for debugfs status file's file_operations 2017-01-06 11:16:13 +01:00
dir.c fscrypt: avoid collisions when presenting long encrypted filenames 2017-05-25 14:30:11 +02:00
extent_cache.c f2fs: fix to skip shrinking extent nodes 2015-10-22 09:39:35 -07:00
f2fs.h f2fs: check entire encrypted bigname when finding a dentry 2017-05-25 14:30:11 +02:00
f2fs_crypto.h fscrypt: remove broken support for detecting keyring key revocation 2017-03-31 09:49:54 +02:00
file.c fscrypto: lock inode while setting encryption policy 2017-03-22 12:04:18 +01:00
gc.c f2fs: relocate the tracepoint for background_gc 2015-10-13 10:02:01 -07:00
gc.h f2fs: support synchronous gc in ioctl 2015-10-09 16:20:56 -07:00
hash.c f2fs: check entire encrypted bigname when finding a dentry 2017-05-25 14:30:11 +02:00
inline.c f2fs: check entire encrypted bigname when finding a dentry 2017-05-25 14:30:11 +02:00
inode.c f2fs: no need to lock for update_inode_page all the time 2015-10-09 16:20:50 -07:00
Kconfig f2fs: fix typo 2015-08-21 22:43:32 -07:00
Makefile f2fs: maintain extent cache in separated file 2015-08-04 14:09:58 -07:00
namei.c fs/f2fs/namei.c: remove unnecessary new_valid_dev() check 2015-11-09 15:11:24 -08:00
node.c f2fs: export ra_nid_pages to sysfs 2015-10-12 14:03:43 -07:00
node.h f2fs: export ra_nid_pages to sysfs 2015-10-12 14:03:43 -07:00
recovery.c f2fs: support lower priority asynchronous readahead in ra_meta_pages 2015-10-12 14:03:15 -07:00
segment.c f2fs: fix to clear GCed flag for atomic written page 2015-10-22 09:37:13 -07:00
segment.h Revert "f2fs: do not skip dentry block writes" 2015-10-12 13:38:02 -07:00
shrinker.c f2fs: shrink free_nids entries 2015-08-20 09:00:06 -07:00
super.c f2fs: sanity check segment count 2017-05-14 13:32:59 +02:00
trace.c f2fs: add sbi and page pointer in f2fs_io_info 2015-05-28 15:41:32 -07:00
trace.h f2fs: add sbi and page pointer in f2fs_io_info 2015-05-28 15:41:32 -07:00
xattr.c f2fs: xattr simplifications 2015-11-13 20:34:34 -05:00
xattr.h f2fs crypto: add encryption xattr support 2015-05-28 15:41:47 -07:00