12 Jan, 2017

1 commit

  • commit 42d97eb0ade31e1bc537d086842f5d6e766d9d51 upstream.

    Attempting to link a device node, named pipe, or socket file into an
    encrypted directory through rename(2) or link(2) always failed with
    EPERM. This happened because fscrypt_has_permitted_context() saw that
    the file was unencrypted and forbid creating the link. This behavior
    was unexpected because such files are never encrypted; only regular
    files, directories, and symlinks can be encrypted.

    To fix this, make fscrypt_has_permitted_context() always return true on
    special files.

    This will be covered by a test in my encryption xfstests patchset.

    Fixes: 9bd8212f981e ("ext4 crypto: add encryption policy and password salt support")
    Signed-off-by: Eric Biggers
    Reviewed-by: Richard Weinberger
    Signed-off-by: Theodore Ts'o
    Signed-off-by: Greg Kroah-Hartman

    Eric Biggers
     

20 Nov, 2016

2 commits

  • With the new (in 4.9) option to use a virtually-mapped stack
    (CONFIG_VMAP_STACK), stack buffers cannot be used as input/output for
    the scatterlist crypto API because they may not be directly mappable to
    struct page. get_crypt_info() was using a stack buffer to hold the
    output from the encryption operation used to derive the per-file key.
    Fix it by using a heap buffer.

    This bug could most easily be observed in a CONFIG_DEBUG_SG kernel
    because this allowed the BUG in sg_set_buf() to be triggered.

    Cc: stable@vger.kernel.org
    Signed-off-by: Eric Biggers
    Signed-off-by: Theodore Ts'o

    Eric Biggers
     
  • With the new (in 4.9) option to use a virtually-mapped stack
    (CONFIG_VMAP_STACK), stack buffers cannot be used as input/output for
    the scatterlist crypto API because they may not be directly mappable to
    struct page. For short filenames, fname_encrypt() was encrypting a
    stack buffer holding the padded filename. Fix it by encrypting the
    filename in-place in the output buffer, thereby making the temporary
    buffer unnecessary.

    This bug could most easily be observed in a CONFIG_DEBUG_SG kernel
    because this allowed the BUG in sg_set_buf() to be triggered.

    Cc: stable@vger.kernel.org
    Signed-off-by: Eric Biggers
    Signed-off-by: Theodore Ts'o

    Eric Biggers
     

15 Oct, 2016

1 commit

  • i_rwsem needs to be acquired while setting an encryption policy so that
    concurrent calls to FS_IOC_SET_ENCRYPTION_POLICY are correctly
    serialized (especially the ->get_context() + ->set_context() pair), and
    so that new files cannot be created in the directory during or after the
    ->empty_dir() check.

    Signed-off-by: Eric Biggers
    Signed-off-by: Theodore Ts'o
    Reviewed-by: Richard Weinberger
    Cc: stable@vger.kernel.org

    Eric Biggers
     

13 Oct, 2016

1 commit

  • The XTS tweak (or IV) was initialized differently on little endian and
    big endian systems. Because the ciphertext depends on the XTS tweak, it
    was not possible to use an encrypted filesystem created by a little
    endian system on a big endian system and vice versa, even if they shared
    the same PAGE_SIZE. Fix this by always using little endian.

    This will break hypothetical big endian users of ext4 or f2fs
    encryption. However, all users we are aware of are little endian, and
    it's believed that "real" big endian users are unlikely to exist yet.
    So this might as well be fixed now before it's too late.

    Signed-off-by: Eric Biggers
    Signed-off-by: Theodore Ts'o
    Cc: stable@vger.kernel.org

    Eric Biggers
     

08 Oct, 2016

1 commit

  • Pull ext4 updates from Ted Ts'o:
    "Lots of bug fixes and cleanups"

    * tag 'ext4_for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tytso/ext4: (40 commits)
    ext4: remove unused variable
    ext4: use journal inode to determine journal overhead
    ext4: create function to read journal inode
    ext4: unmap metadata when zeroing blocks
    ext4: remove plugging from ext4_file_write_iter()
    ext4: allow unlocked direct IO when pages are cached
    ext4: require encryption feature for EXT4_IOC_SET_ENCRYPTION_POLICY
    fscrypto: use standard macros to compute length of fname ciphertext
    ext4: do not unnecessarily null-terminate encrypted symlink data
    ext4: release bh in make_indexed_dir
    ext4: Allow parallel DIO reads
    ext4: allow DAX writeback for hole punch
    jbd2: fix lockdep annotation in add_transaction_credits()
    blockgroup_lock.h: simplify definition of NR_BG_LOCKS
    blockgroup_lock.h: remove debris from bgl_lock_ptr() conversion
    fscrypto: make filename crypto functions return 0 on success
    fscrypto: rename completion callbacks to reflect usage
    fscrypto: remove unnecessary includes
    fscrypto: improved validation when loading inode encryption metadata
    ext4: fix memory leak when symlink decryption fails
    ...

    Linus Torvalds
     

30 Sep, 2016

1 commit


16 Sep, 2016

4 commits

  • Several filename crypto functions: fname_decrypt(),
    fscrypt_fname_disk_to_usr(), and fscrypt_fname_usr_to_disk(), returned
    the output length on success or -errno on failure. However, the output
    length was redundant with the value written to 'oname->len'. It is also
    potentially error-prone to make callers have to check for '< 0' instead
    of '!= 0'.

    Therefore, make these functions return 0 instead of a length, and make
    the callers who cared about the return value being a length use
    'oname->len' instead. For consistency also make other callers check for
    a nonzero result rather than a negative result.

    This change also fixes the inconsistency of fname_encrypt() actually
    already returning 0 on success, not a length like the other filename
    crypto functions and as documented in its function comment.

    Signed-off-by: Eric Biggers
    Signed-off-by: Theodore Ts'o
    Reviewed-by: Andreas Dilger
    Acked-by: Jaegeuk Kim

    Eric Biggers
     
  • fscrypt_complete() was used only for data pages, not for all
    encryption/decryption. Rename it to page_crypt_complete().

    dir_crypt_complete() was used for filename encryption/decryption for
    both directory entries and symbolic links. Rename it to
    fname_crypt_complete().

    Signed-off-by: Eric Biggers
    Signed-off-by: Theodore Ts'o

    Eric Biggers
     
  • This patch removes some #includes that are clearly not needed, such as a
    reference to ecryptfs, which is unrelated to the new filesystem
    encryption code.

    Signed-off-by: Eric Biggers
    Signed-off-by: Theodore Ts'o

    Eric Biggers
     
  • - Validate fscrypt_context.format and fscrypt_context.flags. If
    unrecognized values are set, then the kernel may not know how to
    interpret the encrypted file, so it should fail the operation.

    - Validate that AES_256_XTS is used for contents and that AES_256_CTS is
    used for filenames. It was previously possible for the kernel to
    accept these reversed, though it would have taken manual editing of
    the block device. This was not intended.

    - Fail cleanly rather than BUG()-ing if a file has an unexpected type.

    Signed-off-by: Eric Biggers
    Signed-off-by: Theodore Ts'o

    Eric Biggers
     

10 Sep, 2016

3 commits

  • Since setting an encryption policy requires writing metadata to the
    filesystem, it should be guarded by mnt_want_write/mnt_drop_write.
    Otherwise, a user could cause a write to a frozen or readonly
    filesystem. This was handled correctly by f2fs but not by ext4. Make
    fscrypt_process_policy() handle it rather than relying on the filesystem
    to get it right.

    Signed-off-by: Eric Biggers
    Cc: stable@vger.kernel.org # 4.1+; check fs/{ext4,f2fs}
    Signed-off-by: Theodore Ts'o
    Acked-by: Jaegeuk Kim

    Eric Biggers
     
  • The FS_IOC_SET_ENCRYPTION_POLICY ioctl allowed setting an encryption
    policy on nondirectory files. This was unintentional, and in the case
    of nonempty regular files did not behave as expected because existing
    data was not actually encrypted by the ioctl.

    In the case of ext4, the user could also trigger filesystem errors in
    ->empty_dir(), e.g. due to mismatched "directory" checksums when the
    kernel incorrectly tried to interpret a regular file as a directory.

    This bug affected ext4 with kernels v4.8-rc1 or later and f2fs with
    kernels v4.6 and later. It appears that older kernels only permitted
    directories and that the check was accidentally lost during the
    refactoring to share the file encryption code between ext4 and f2fs.

    This patch restores the !S_ISDIR() check that was present in older
    kernels.

    Signed-off-by: Eric Biggers
    Cc: stable@vger.kernel.org
    Signed-off-by: Theodore Ts'o

    Eric Biggers
     
  • On an ext4 or f2fs filesystem with file encryption supported, a user
    could set an encryption policy on any empty directory(*) to which they
    had readonly access. This is obviously problematic, since such a
    directory might be owned by another user and the new encryption policy
    would prevent that other user from creating files in their own directory
    (for example).

    Fix this by requiring inode_owner_or_capable() permission to set an
    encryption policy. This means that either the caller must own the file,
    or the caller must have the capability CAP_FOWNER.

    (*) Or also on any regular file, for f2fs v4.6 and later and ext4
    v4.8-rc1 and later; a separate bug fix is coming for that.

    Signed-off-by: Eric Biggers
    Cc: stable@vger.kernel.org # 4.1+; check fs/{ext4,f2fs}
    Signed-off-by: Theodore Ts'o

    Eric Biggers
     

08 Jun, 2016

2 commits

  • This patch converts the simple bi_rw use cases in the block,
    drivers, mm and fs code to set/get the bio operation using
    bio_set_op_attrs/bio_op

    These should be simple one or two liner cases, so I just did them
    in one patch. The next patches handle the more complicated
    cases in a module per patch.

    Signed-off-by: Mike Christie
    Reviewed-by: Hannes Reinecke
    Signed-off-by: Jens Axboe

    Mike Christie
     
  • This has callers of submit_bio/submit_bio_wait set the bio->bi_rw
    instead of passing it in. This makes that use the same as
    generic_make_request and how we set the other bio fields.

    Signed-off-by: Mike Christie

    Fixed up fs/ext4/crypto.c

    Signed-off-by: Jens Axboe

    Mike Christie
     

08 May, 2016

1 commit

  • This patch allows fscrypto to handle a second key prefix given by filesystem.
    The main reason is to provide backward compatibility, since previously f2fs
    used "f2fs:" as a crypto prefix instead of "fscrypt:".
    Later, ext4 should also provide key_prefix() to give "ext4:".

    One concern decribed by Ted would be kinda double check overhead of prefixes.
    In x86, for example, validate_user_key consumes 8 ms after boot-up, which turns
    out derive_key_aes() consumed most of the time to load specific crypto module.
    After such the cold miss, it shows almost zero latencies, which treats as a
    negligible overhead.
    Note that request_key() detects wrong prefix in prior to derive_key_aes() even.

    Cc: Ted Tso
    Cc: stable@vger.kernel.org # v4.6
    Signed-off-by: Jaegeuk Kim

    Jaegeuk Kim
     

13 Apr, 2016

3 commits


05 Apr, 2016

1 commit

  • PAGE_CACHE_{SIZE,SHIFT,MASK,ALIGN} macros were introduced *long* time
    ago with promise that one day it will be possible to implement page
    cache with bigger chunks than PAGE_SIZE.

    This promise never materialized. And unlikely will.

    We have many places where PAGE_CACHE_SIZE assumed to be equal to
    PAGE_SIZE. And it's constant source of confusion on whether
    PAGE_CACHE_* or PAGE_* constant should be used in a particular case,
    especially on the border between fs and mm.

    Global switching to PAGE_CACHE_SIZE != PAGE_SIZE would cause to much
    breakage to be doable.

    Let's stop pretending that pages in page cache are special. They are
    not.

    The changes are pretty straight-forward:

    - << (PAGE_CACHE_SHIFT - PAGE_SHIFT) -> ;

    - >> (PAGE_CACHE_SHIFT - PAGE_SHIFT) -> ;

    - PAGE_CACHE_{SIZE,SHIFT,MASK,ALIGN} -> PAGE_{SIZE,SHIFT,MASK,ALIGN};

    - page_cache_get() -> get_page();

    - page_cache_release() -> put_page();

    This patch contains automated changes generated with coccinelle using
    script below. For some reason, coccinelle doesn't patch header files.
    I've called spatch for them manually.

    The only adjustment after coccinelle is revert of changes to
    PAGE_CAHCE_ALIGN definition: we are going to drop it later.

    There are few places in the code where coccinelle didn't reach. I'll
    fix them manually in a separate patch. Comments and documentation also
    will be addressed with the separate patch.

    virtual patch

    @@
    expression E;
    @@
    - E << (PAGE_CACHE_SHIFT - PAGE_SHIFT)
    + E

    @@
    expression E;
    @@
    - E >> (PAGE_CACHE_SHIFT - PAGE_SHIFT)
    + E

    @@
    @@
    - PAGE_CACHE_SHIFT
    + PAGE_SHIFT

    @@
    @@
    - PAGE_CACHE_SIZE
    + PAGE_SIZE

    @@
    @@
    - PAGE_CACHE_MASK
    + PAGE_MASK

    @@
    expression E;
    @@
    - PAGE_CACHE_ALIGN(E)
    + PAGE_ALIGN(E)

    @@
    expression E;
    @@
    - page_cache_get(E)
    + get_page(E)

    @@
    expression E;
    @@
    - page_cache_release(E)
    + put_page(E)

    Signed-off-by: Kirill A. Shutemov
    Acked-by: Michal Hocko
    Signed-off-by: Linus Torvalds

    Kirill A. Shutemov
     

27 Mar, 2016

1 commit

  • Commit 0b81d07790726 ("fs crypto: move per-file encryption from f2fs
    tree to fs/crypto") moved the f2fs crypto files to fs/crypto/ and
    renamed the symbol prefixes from "f2fs_" to "fscrypt_" (and from "F2FS_"
    to just "FS" for preprocessor symbols).

    Because of the symbol renaming, it's a bit hard to see it as a file
    move: use

    git show -M30 0b81d07790726

    to lower the rename detection to just 30% similarity and make git show
    the files as renamed (the header file won't be shown as a rename even
    then - since all it contains is symbol definitions, it looks almost
    completely different).

    Even with the renames showing as renames, the diffs are not all that
    easy to read, since so much is just the renames. But Eric Biggers
    noticed that it's not just all renames: the initialization of the
    xts_tweak had been broken too, using the inode number rather than the
    page offset.

    That's not right - it makes the xfs_tweak the same for all pages of each
    inode. It _might_ make sense to make the xfs_tweak contain both the
    offset _and_ the inode number, but not just the inode number.

    Reported-by: Eric Biggers
    Cc: Jaegeuk Kim
    Signed-off-by: Linus Torvalds

    Linus Torvalds
     

22 Mar, 2016

1 commit

  • Pull f2fs updates from Jaegeuk Kim:
    "New Features:
    - uplift filesystem encryption into fs/crypto/
    - give sysfs entries to control memroy consumption

    Enhancements:
    - aio performance by preallocating blocks in ->write_iter
    - use writepages lock for only WB_SYNC_ALL
    - avoid redundant inline_data conversion
    - enhance forground GC
    - use wait_for_stable_page as possible
    - speed up SEEK_DATA and fiiemap

    Bug Fixes:
    - corner case in terms of -ENOSPC for inline_data
    - hung task caused by long latency in shrinker
    - corruption between atomic write and f2fs_trace_pid
    - avoid garbage lengths in dentries
    - revoke atomicly written pages if an error occurs

    In addition, there are various minor bug fixes and clean-ups"

    * tag 'for-f2fs-4.6' of git://git.kernel.org/pub/scm/linux/kernel/git/jaegeuk/f2fs: (81 commits)
    f2fs: submit node page write bios when really required
    f2fs: add missing argument to f2fs_setxattr stub
    f2fs: fix to avoid unneeded unlock_new_inode
    f2fs: clean up opened code with f2fs_update_dentry
    f2fs: declare static functions
    f2fs: use cryptoapi crc32 functions
    f2fs: modify the readahead method in ra_node_page()
    f2fs crypto: sync ext4_lookup and ext4_file_open
    fs crypto: move per-file encryption from f2fs tree to fs/crypto
    f2fs: mutex can't be used by down_write_nest_lock()
    f2fs: recovery missing dot dentries in root directory
    f2fs: fix to avoid deadlock when merging inline data
    f2fs: introduce f2fs_flush_merged_bios for cleanup
    f2fs: introduce f2fs_update_data_blkaddr for cleanup
    f2fs crypto: fix incorrect positioning for GCing encrypted data page
    f2fs: fix incorrect upper bound when iterating inode mapping tree
    f2fs: avoid hungtask problem caused by losing wake_up
    f2fs: trace old block address for CoWed page
    f2fs: try to flush inode after merging inline data
    f2fs: show more info about superblock recovery
    ...

    Linus Torvalds
     

18 Mar, 2016

1 commit

  • This patch adds the renamed functions moved from the f2fs crypto files.

    1. definitions for per-file encryption used by ext4 and f2fs.

    2. crypto.c for encrypt/decrypt functions
    a. IO preparation:
    - fscrypt_get_ctx / fscrypt_release_ctx
    b. before IOs:
    - fscrypt_encrypt_page
    - fscrypt_decrypt_page
    - fscrypt_zeroout_range
    c. after IOs:
    - fscrypt_decrypt_bio_pages
    - fscrypt_pullback_bio_page
    - fscrypt_restore_control_page

    3. policy.c supporting context management.
    a. For ioctls:
    - fscrypt_process_policy
    - fscrypt_get_policy
    b. For context permission
    - fscrypt_has_permitted_context
    - fscrypt_inherit_context

    4. keyinfo.c to handle permissions
    - fscrypt_get_encryption_info
    - fscrypt_free_encryption_info

    5. fname.c to support filename encryption
    a. general wrapper functions
    - fscrypt_fname_disk_to_usr
    - fscrypt_fname_usr_to_disk
    - fscrypt_setup_filename
    - fscrypt_free_filename

    b. specific filename handling functions
    - fscrypt_fname_alloc_buffer
    - fscrypt_fname_free_buffer

    6. Makefile and Kconfig

    Cc: Al Viro
    Signed-off-by: Michael Halcrow
    Signed-off-by: Ildar Muslukhov
    Signed-off-by: Uday Savagaonkar
    Signed-off-by: Theodore Ts'o
    Signed-off-by: Arnd Bergmann
    Signed-off-by: Jaegeuk Kim

    Jaegeuk Kim