1. 22 9月, 2020 4 次提交
    • E
      fscrypt: make fscrypt_set_test_dummy_encryption() take a 'const char *' · c8c868ab
      Eric Biggers 提交于
      fscrypt_set_test_dummy_encryption() requires that the optional argument
      to the test_dummy_encryption mount option be specified as a substring_t.
      That doesn't work well with filesystems that use the new mount API,
      since the new way of parsing mount options doesn't use substring_t.
      
      Make it take the argument as a 'const char *' instead.
      
      Instead of moving the match_strdup() into the callers in ext4 and f2fs,
      make them just use arg->from directly.  Since the pattern is
      "test_dummy_encryption=%s", the argument will be null-terminated.
      Acked-by: NJeff Layton <jlayton@kernel.org>
      Link: https://lore.kernel.org/r/20200917041136.178600-14-ebiggers@kernel.orgSigned-off-by: NEric Biggers <ebiggers@google.com>
      c8c868ab
    • E
      fscrypt: handle test_dummy_encryption in more logical way · ac4acb1f
      Eric Biggers 提交于
      The behavior of the test_dummy_encryption mount option is that when a
      new file (or directory or symlink) is created in an unencrypted
      directory, it's automatically encrypted using a dummy encryption policy.
      That's it; in particular, the encryption (or lack thereof) of existing
      files (or directories or symlinks) doesn't change.
      
      Unfortunately the implementation of test_dummy_encryption is a bit weird
      and confusing.  When test_dummy_encryption is enabled and a file is
      being created in an unencrypted directory, we set up an encryption key
      (->i_crypt_info) for the directory.  This isn't actually used to do any
      encryption, however, since the directory is still unencrypted!  Instead,
      ->i_crypt_info is only used for inheriting the encryption policy.
      
      One consequence of this is that the filesystem ends up providing a
      "dummy context" (policy + nonce) instead of a "dummy policy".  In
      commit ed318a6c ("fscrypt: support test_dummy_encryption=v2"), I
      mistakenly thought this was required.  However, actually the nonce only
      ends up being used to derive a key that is never used.
      
      Another consequence of this implementation is that it allows for
      'inode->i_crypt_info != NULL && !IS_ENCRYPTED(inode)', which is an edge
      case that can be forgotten about.  For example, currently
      FS_IOC_GET_ENCRYPTION_POLICY on an unencrypted directory may return the
      dummy encryption policy when the filesystem is mounted with
      test_dummy_encryption.  That seems like the wrong thing to do, since
      again, the directory itself is not actually encrypted.
      
      Therefore, switch to a more logical and maintainable implementation
      where the dummy encryption policy inheritance is done without setting up
      keys for unencrypted directories.  This involves:
      
      - Adding a function fscrypt_policy_to_inherit() which returns the
        encryption policy to inherit from a directory.  This can be a real
        policy, a dummy policy, or no policy.
      
      - Replacing struct fscrypt_dummy_context, ->get_dummy_context(), etc.
        with struct fscrypt_dummy_policy, ->get_dummy_policy(), etc.
      
      - Making fscrypt_fname_encrypted_size() take an fscrypt_policy instead
        of an inode.
      Acked-by: NJaegeuk Kim <jaegeuk@kernel.org>
      Acked-by: NJeff Layton <jlayton@kernel.org>
      Link: https://lore.kernel.org/r/20200917041136.178600-13-ebiggers@kernel.orgSigned-off-by: NEric Biggers <ebiggers@google.com>
      ac4acb1f
    • E
      fscrypt: remove fscrypt_inherit_context() · e9d5e31d
      Eric Biggers 提交于
      Now that all filesystems have been converted to use
      fscrypt_prepare_new_inode() and fscrypt_set_context(),
      fscrypt_inherit_context() is no longer used.  Remove it.
      Acked-by: NJeff Layton <jlayton@kernel.org>
      Link: https://lore.kernel.org/r/20200917041136.178600-8-ebiggers@kernel.orgSigned-off-by: NEric Biggers <ebiggers@google.com>
      e9d5e31d
    • E
      fscrypt: add fscrypt_prepare_new_inode() and fscrypt_set_context() · a992b20c
      Eric Biggers 提交于
      fscrypt_get_encryption_info() is intended to be GFP_NOFS-safe.  But
      actually it isn't, since it uses functions like crypto_alloc_skcipher()
      which aren't GFP_NOFS-safe, even when called under memalloc_nofs_save().
      Therefore it can deadlock when called from a context that needs
      GFP_NOFS, e.g. during an ext4 transaction or between f2fs_lock_op() and
      f2fs_unlock_op().  This happens when creating a new encrypted file.
      
      We can't fix this by just not setting up the key for new inodes right
      away, since new symlinks need their key to encrypt the symlink target.
      
      So we need to set up the new inode's key before starting the
      transaction.  But just calling fscrypt_get_encryption_info() earlier
      doesn't work, since it assumes the encryption context is already set,
      and the encryption context can't be set until the transaction.
      
      The recently proposed fscrypt support for the ceph filesystem
      (https://lkml.kernel.org/linux-fscrypt/20200821182813.52570-1-jlayton@kernel.org/T/#u)
      will have this same ordering problem too, since ceph will need to
      encrypt new symlinks before setting their encryption context.
      
      Finally, f2fs can deadlock when the filesystem is mounted with
      '-o test_dummy_encryption' and a new file is created in an existing
      unencrypted directory.  Similarly, this is caused by holding too many
      locks when calling fscrypt_get_encryption_info().
      
      To solve all these problems, add new helper functions:
      
      - fscrypt_prepare_new_inode() sets up a new inode's encryption key
        (fscrypt_info), using the parent directory's encryption policy and a
        new random nonce.  It neither reads nor writes the encryption context.
      
      - fscrypt_set_context() persists the encryption context of a new inode,
        using the information from the fscrypt_info already in memory.  This
        replaces fscrypt_inherit_context().
      
      Temporarily keep fscrypt_inherit_context() around until all filesystems
      have been converted to use fscrypt_set_context().
      Acked-by: NJeff Layton <jlayton@kernel.org>
      Link: https://lore.kernel.org/r/20200917041136.178600-2-ebiggers@kernel.orgSigned-off-by: NEric Biggers <ebiggers@google.com>
      a992b20c
  2. 08 9月, 2020 1 次提交
    • E
      fscrypt: restrict IV_INO_LBLK_32 to ino_bits <= 32 · 5e895bd4
      Eric Biggers 提交于
      When an encryption policy has the IV_INO_LBLK_32 flag set, the IV
      generation method involves hashing the inode number.  This is different
      from fscrypt's other IV generation methods, where the inode number is
      either not used at all or is included directly in the IVs.
      
      Therefore, in principle IV_INO_LBLK_32 can work with any length inode
      number.  However, currently fscrypt gets the inode number from
      inode::i_ino, which is 'unsigned long'.  So currently the implementation
      limit is actually 32 bits (like IV_INO_LBLK_64), since longer inode
      numbers will have been truncated by the VFS on 32-bit platforms.
      
      Fix fscrypt_supported_v2_policy() to enforce the correct limit.
      
      This doesn't actually matter currently, since only ext4 and f2fs support
      IV_INO_LBLK_32, and they both only support 32-bit inode numbers.  But we
      might as well fix it in case it matters in the future.
      
      Ideally inode::i_ino would instead be made 64-bit, but for now it's not
      needed.  (Note, this limit does *not* prevent filesystems with 64-bit
      inode numbers from adding fscrypt support, since IV_INO_LBLK_* support
      is optional and is useful only on certain hardware.)
      
      Fixes: e3b1078b ("fscrypt: add support for IV_INO_LBLK_32 policies")
      Reported-by: NJeff Layton <jlayton@kernel.org>
      Link: https://lore.kernel.org/r/20200824203841.1707847-1-ebiggers@kernel.orgSigned-off-by: NEric Biggers <ebiggers@google.com>
      5e895bd4
  3. 22 7月, 2020 2 次提交
    • E
      fscrypt: use smp_load_acquire() for ->i_crypt_info · ab673b98
      Eric Biggers 提交于
      Normally smp_store_release() or cmpxchg_release() is paired with
      smp_load_acquire().  Sometimes smp_load_acquire() can be replaced with
      the more lightweight READ_ONCE().  However, for this to be safe, all the
      published memory must only be accessed in a way that involves the
      pointer itself.  This may not be the case if allocating the object also
      involves initializing a static or global variable, for example.
      
      fscrypt_info includes various sub-objects which are internal to and are
      allocated by other kernel subsystems such as keyrings and crypto.  So by
      using READ_ONCE() for ->i_crypt_info, we're relying on internal
      implementation details of these other kernel subsystems.
      
      Remove this fragile assumption by using smp_load_acquire() instead.
      
      (Note: I haven't seen any real-world problems here.  This change is just
      fixing the code to be guaranteed correct and less fragile.)
      
      Fixes: e37a784d ("fscrypt: use READ_ONCE() to access ->i_crypt_info")
      Link: https://lore.kernel.org/r/20200721225920.114347-5-ebiggers@kernel.orgSigned-off-by: NEric Biggers <ebiggers@google.com>
      ab673b98
    • E
      fscrypt: restrict IV_INO_LBLK_* to AES-256-XTS · f000223c
      Eric Biggers 提交于
      IV_INO_LBLK_* exist only because of hardware limitations, and currently
      the only known use case for them involves AES-256-XTS.  Therefore, for
      now only allow them in combination with AES-256-XTS.  This way we don't
      have to worry about them being combined with other encryption modes.
      
      (To be clear, combining IV_INO_LBLK_* with other encryption modes
      *should* work just fine.  It's just not being tested, so we can't be
      100% sure it works.  So with no known use case, it's best to disallow it
      for now, just like we don't allow other weird combinations like
      AES-256-XTS contents encryption with Adiantum filenames encryption.)
      
      This can be relaxed later if a use case for other combinations arises.
      
      Fixes: b103fb76 ("fscrypt: add support for IV_INO_LBLK_64 policies")
      Fixes: e3b1078b ("fscrypt: add support for IV_INO_LBLK_32 policies")
      Link: https://lore.kernel.org/r/20200721181012.39308-1-ebiggers@kernel.orgSigned-off-by: NEric Biggers <ebiggers@google.com>
      f000223c
  4. 21 7月, 2020 1 次提交
  5. 20 5月, 2020 1 次提交
    • E
      fscrypt: add support for IV_INO_LBLK_32 policies · e3b1078b
      Eric Biggers 提交于
      The eMMC inline crypto standard will only specify 32 DUN bits (a.k.a. IV
      bits), unlike UFS's 64.  IV_INO_LBLK_64 is therefore not applicable, but
      an encryption format which uses one key per policy and permits the
      moving of encrypted file contents (as f2fs's garbage collector requires)
      is still desirable.
      
      To support such hardware, add a new encryption format IV_INO_LBLK_32
      that makes the best use of the 32 bits: the IV is set to
      'SipHash-2-4(inode_number) + file_logical_block_number mod 2^32', where
      the SipHash key is derived from the fscrypt master key.  We hash only
      the inode number and not also the block number, because we need to
      maintain contiguity of DUNs to merge bios.
      
      Unlike with IV_INO_LBLK_64, with this format IV reuse is possible; this
      is unavoidable given the size of the DUN.  This means this format should
      only be used where the requirements of the first paragraph apply.
      However, the hash spreads out the IVs in the whole usable range, and the
      use of a keyed hash makes it difficult for an attacker to determine
      which files use which IVs.
      
      Besides the above differences, this flag works like IV_INO_LBLK_64 in
      that on ext4 it is only allowed if the stable_inodes feature has been
      enabled to prevent inode numbers and the filesystem UUID from changing.
      
      Link: https://lore.kernel.org/r/20200515204141.251098-1-ebiggers@kernel.orgReviewed-by: NTheodore Ts'o <tytso@mit.edu>
      Reviewed-by: NPaul Crowley <paulcrowley@google.com>
      Signed-off-by: NEric Biggers <ebiggers@google.com>
      e3b1078b
  6. 19 5月, 2020 2 次提交
    • E
      fscrypt: make test_dummy_encryption use v2 by default · 0ca2ddb0
      Eric Biggers 提交于
      Since v1 encryption policies are deprecated, make test_dummy_encryption
      test v2 policies by default.
      
      Note that this causes ext4/023 and ext4/028 to start failing due to
      known bugs in those tests (see previous commit).
      
      Link: https://lore.kernel.org/r/20200512233251.118314-5-ebiggers@kernel.orgReviewed-by: NJaegeuk Kim <jaegeuk@kernel.org>
      Reviewed-by: NTheodore Ts'o <tytso@mit.edu>
      Signed-off-by: NEric Biggers <ebiggers@google.com>
      0ca2ddb0
    • E
      fscrypt: support test_dummy_encryption=v2 · ed318a6c
      Eric Biggers 提交于
      v1 encryption policies are deprecated in favor of v2, and some new
      features (e.g. encryption+casefolding) are only being added for v2.
      
      Therefore, the "test_dummy_encryption" mount option (which is used for
      encryption I/O testing with xfstests) needs to support v2 policies.
      
      To do this, extend its syntax to be "test_dummy_encryption=v1" or
      "test_dummy_encryption=v2".  The existing "test_dummy_encryption" (no
      argument) also continues to be accepted, to specify the default setting
      -- currently v1, but the next patch changes it to v2.
      
      To cleanly support both v1 and v2 while also making it easy to support
      specifying other encryption settings in the future (say, accepting
      "$contents_mode:$filenames_mode:v2"), make ext4 and f2fs maintain a
      pointer to the dummy fscrypt_context rather than using mount flags.
      
      To avoid concurrency issues, don't allow test_dummy_encryption to be set
      or changed during a remount.  (The former restriction is new, but
      xfstests doesn't run into it, so no one should notice.)
      
      Tested with 'gce-xfstests -c {ext4,f2fs}/encrypt -g auto'.  On ext4,
      there are two regressions, both of which are test bugs: ext4/023 and
      ext4/028 fail because they set an xattr and expect it to be stored
      inline, but the increase in size of the fscrypt_context from
      24 to 40 bytes causes this xattr to be spilled into an external block.
      
      Link: https://lore.kernel.org/r/20200512233251.118314-4-ebiggers@kernel.orgAcked-by: NJaegeuk Kim <jaegeuk@kernel.org>
      Reviewed-by: NTheodore Ts'o <tytso@mit.edu>
      Signed-off-by: NEric Biggers <ebiggers@google.com>
      ed318a6c
  7. 13 5月, 2020 1 次提交
    • E
      fscrypt: fix all kerneldoc warnings · d2fe9754
      Eric Biggers 提交于
      Fix all kerneldoc warnings in fs/crypto/ and include/linux/fscrypt.h.
      Most of these were due to missing documentation for function parameters.
      
      Detected with:
      
          scripts/kernel-doc -v -none fs/crypto/*.{c,h} include/linux/fscrypt.h
      
      This cleanup makes it possible to check new patches for kerneldoc
      warnings without having to filter out all the existing ones.
      
      For consistency, also adjust some function "brief descriptions" to
      include the parentheses and to wrap at 80 characters.  (The latter
      matches the checkpatch expectation.)
      
      Link: https://lore.kernel.org/r/20200511191358.53096-2-ebiggers@kernel.orgSigned-off-by: NEric Biggers <ebiggers@google.com>
      d2fe9754
  8. 20 3月, 2020 1 次提交
    • E
      fscrypt: add FS_IOC_GET_ENCRYPTION_NONCE ioctl · e98ad464
      Eric Biggers 提交于
      Add an ioctl FS_IOC_GET_ENCRYPTION_NONCE which retrieves the nonce from
      an encrypted file or directory.  The nonce is the 16-byte random value
      stored in the inode's encryption xattr.  It is normally used together
      with the master key to derive the inode's actual encryption key.
      
      The nonces are needed by automated tests that verify the correctness of
      the ciphertext on-disk.  Except for the IV_INO_LBLK_64 case, there's no
      way to replicate a file's ciphertext without knowing that file's nonce.
      
      The nonces aren't secret, and the existing ciphertext verification tests
      in xfstests retrieve them from disk using debugfs or dump.f2fs.  But in
      environments that lack these debugging tools, getting the nonces by
      manually parsing the filesystem structure would be very hard.
      
      To make this important type of testing much easier, let's just add an
      ioctl that retrieves the nonce.
      
      Link: https://lore.kernel.org/r/20200314205052.93294-2-ebiggers@kernel.orgReviewed-by: NTheodore Ts'o <tytso@mit.edu>
      Signed-off-by: NEric Biggers <ebiggers@google.com>
      e98ad464
  9. 23 1月, 2020 1 次提交
  10. 01 1月, 2020 3 次提交
  11. 07 11月, 2019 1 次提交
    • E
      fscrypt: add support for IV_INO_LBLK_64 policies · b103fb76
      Eric Biggers 提交于
      Inline encryption hardware compliant with the UFS v2.1 standard or with
      the upcoming version of the eMMC standard has the following properties:
      
      (1) Per I/O request, the encryption key is specified by a previously
          loaded keyslot.  There might be only a small number of keyslots.
      
      (2) Per I/O request, the starting IV is specified by a 64-bit "data unit
          number" (DUN).  IV bits 64-127 are assumed to be 0.  The hardware
          automatically increments the DUN for each "data unit" of
          configurable size in the request, e.g. for each filesystem block.
      
      Property (1) makes it inefficient to use the traditional fscrypt
      per-file keys.  Property (2) precludes the use of the existing
      DIRECT_KEY fscrypt policy flag, which needs at least 192 IV bits.
      
      Therefore, add a new fscrypt policy flag IV_INO_LBLK_64 which causes the
      encryption to modified as follows:
      
      - The encryption keys are derived from the master key, encryption mode
        number, and filesystem UUID.
      
      - The IVs are chosen as (inode_number << 32) | file_logical_block_num.
        For filenames encryption, file_logical_block_num is 0.
      
      Since the file nonces aren't used in the key derivation, many files may
      share the same encryption key.  This is much more efficient on the
      target hardware.  Including the inode number in the IVs and mixing the
      filesystem UUID into the keys ensures that data in different files is
      nevertheless still encrypted differently.
      
      Additionally, limiting the inode and block numbers to 32 bits and
      placing the block number in the low bits maintains compatibility with
      the 64-bit DUN convention (property (2) above).
      
      Since this scheme assumes that inode numbers are stable (which may
      preclude filesystem shrinking) and that inode and file logical block
      numbers are at most 32-bit, IV_INO_LBLK_64 will only be allowed on
      filesystems that meet these constraints.  These are acceptable
      limitations for the cases where this format would actually be used.
      
      Note that IV_INO_LBLK_64 is an on-disk format, not an implementation.
      This patch just adds support for it using the existing filesystem layer
      encryption.  A later patch will add support for inline encryption.
      Reviewed-by: NPaul Crowley <paulcrowley@google.com>
      Co-developed-by: NSatya Tangirala <satyat@google.com>
      Signed-off-by: NSatya Tangirala <satyat@google.com>
      Signed-off-by: NEric Biggers <ebiggers@google.com>
      b103fb76
  12. 13 8月, 2019 3 次提交
    • E
      fscrypt: require that key be added when setting a v2 encryption policy · 5ab7189a
      Eric Biggers 提交于
      By looking up the master keys in a filesystem-level keyring rather than
      in the calling processes' key hierarchy, it becomes possible for a user
      to set an encryption policy which refers to some key they don't actually
      know, then encrypt their files using that key.  Cryptographically this
      isn't much of a problem, but the semantics of this would be a bit weird.
      Thus, enforce that a v2 encryption policy can only be set if the user
      has previously added the key, or has capable(CAP_FOWNER).
      
      We tolerate that this problem will continue to exist for v1 encryption
      policies, however; there is no way around that.
      Reviewed-by: NTheodore Ts'o <tytso@mit.edu>
      Signed-off-by: NEric Biggers <ebiggers@google.com>
      5ab7189a
    • E
      fscrypt: v2 encryption policy support · 5dae460c
      Eric Biggers 提交于
      Add a new fscrypt policy version, "v2".  It has the following changes
      from the original policy version, which we call "v1" (*):
      
      - Master keys (the user-provided encryption keys) are only ever used as
        input to HKDF-SHA512.  This is more flexible and less error-prone, and
        it avoids the quirks and limitations of the AES-128-ECB based KDF.
        Three classes of cryptographically isolated subkeys are defined:
      
          - Per-file keys, like used in v1 policies except for the new KDF.
      
          - Per-mode keys.  These implement the semantics of the DIRECT_KEY
            flag, which for v1 policies made the master key be used directly.
            These are also planned to be used for inline encryption when
            support for it is added.
      
          - Key identifiers (see below).
      
      - Each master key is identified by a 16-byte master_key_identifier,
        which is derived from the key itself using HKDF-SHA512.  This prevents
        users from associating the wrong key with an encrypted file or
        directory.  This was easily possible with v1 policies, which
        identified the key by an arbitrary 8-byte master_key_descriptor.
      
      - The key must be provided in the filesystem-level keyring, not in a
        process-subscribed keyring.
      
      The following UAPI additions are made:
      
      - The existing ioctl FS_IOC_SET_ENCRYPTION_POLICY can now be passed a
        fscrypt_policy_v2 to set a v2 encryption policy.  It's disambiguated
        from fscrypt_policy/fscrypt_policy_v1 by the version code prefix.
      
      - A new ioctl FS_IOC_GET_ENCRYPTION_POLICY_EX is added.  It allows
        getting the v1 or v2 encryption policy of an encrypted file or
        directory.  The existing FS_IOC_GET_ENCRYPTION_POLICY ioctl could not
        be used because it did not have a way for userspace to indicate which
        policy structure is expected.  The new ioctl includes a size field, so
        it is extensible to future fscrypt policy versions.
      
      - The ioctls FS_IOC_ADD_ENCRYPTION_KEY, FS_IOC_REMOVE_ENCRYPTION_KEY,
        and FS_IOC_GET_ENCRYPTION_KEY_STATUS now support managing keys for v2
        encryption policies.  Such keys are kept logically separate from keys
        for v1 encryption policies, and are identified by 'identifier' rather
        than by 'descriptor'.  The 'identifier' need not be provided when
        adding a key, since the kernel will calculate it anyway.
      
      This patch temporarily keeps adding/removing v2 policy keys behind the
      same permission check done for adding/removing v1 policy keys:
      capable(CAP_SYS_ADMIN).  However, the next patch will carefully take
      advantage of the cryptographically secure master_key_identifier to allow
      non-root users to add/remove v2 policy keys, thus providing a full
      replacement for v1 policies.
      
      (*) Actually, in the API fscrypt_policy::version is 0 while on-disk
          fscrypt_context::format is 1.  But I believe it makes the most sense
          to advance both to '2' to have them be in sync, and to consider the
          numbering to start at 1 except for the API quirk.
      Reviewed-by: NPaul Crowley <paulcrowley@google.com>
      Reviewed-by: NTheodore Ts'o <tytso@mit.edu>
      Signed-off-by: NEric Biggers <ebiggers@google.com>
      5dae460c
    • E
      fscrypt: use FSCRYPT_* definitions, not FS_* · 3b6df59b
      Eric Biggers 提交于
      Update fs/crypto/ to use the new names for the UAPI constants rather
      than the old names, then make the old definitions conditional on
      !__KERNEL__.
      Reviewed-by: NTheodore Ts'o <tytso@mit.edu>
      Signed-off-by: NEric Biggers <ebiggers@google.com>
      3b6df59b
  13. 29 5月, 2019 1 次提交
    • H
      fscrypt: don't set policy for a dead directory · 5858bdad
      Hongjie Fang 提交于
      The directory may have been removed when entering
      fscrypt_ioctl_set_policy().  If so, the empty_dir() check will return
      error for ext4 file system.
      
      ext4_rmdir() sets i_size = 0, then ext4_empty_dir() reports an error
      because 'inode->i_size < EXT4_DIR_REC_LEN(1) + EXT4_DIR_REC_LEN(2)'.  If
      the fs is mounted with errors=panic, it will trigger a panic issue.
      
      Add the check IS_DEADDIR() to fix this problem.
      
      Fixes: 9bd8212f ("ext4 crypto: add encryption policy and password salt support")
      Cc: <stable@vger.kernel.org> # v4.1+
      Signed-off-by: NHongjie Fang <hongjiefang@asrmicro.com>
      Signed-off-by: NEric Biggers <ebiggers@google.com>
      5858bdad
  14. 17 4月, 2019 1 次提交
    • E
      fscrypt: use READ_ONCE() to access ->i_crypt_info · e37a784d
      Eric Biggers 提交于
      ->i_crypt_info starts out NULL and may later be locklessly set to a
      non-NULL value by the cmpxchg() in fscrypt_get_encryption_info().
      
      But ->i_crypt_info is used directly, which technically is incorrect.
      It's a data race, and it doesn't include the data dependency barrier
      needed to safely dereference the pointer on at least one architecture.
      
      Fix this by using READ_ONCE() instead.  Note: we don't need to use
      smp_load_acquire(), since dereferencing the pointer only requires a data
      dependency barrier, which is already included in READ_ONCE().  We also
      don't need READ_ONCE() in places where ->i_crypt_info is unconditionally
      dereferenced, since it must have already been checked.
      
      Also downgrade the cmpxchg() to cmpxchg_release(), since RELEASE
      semantics are sufficient on the write side.
      Signed-off-by: NEric Biggers <ebiggers@google.com>
      Signed-off-by: NTheodore Ts'o <tytso@mit.edu>
      e37a784d
  15. 24 1月, 2019 1 次提交
    • E
      fscrypt: return -EXDEV for incompatible rename or link into encrypted dir · f5e55e77
      Eric Biggers 提交于
      Currently, trying to rename or link a regular file, directory, or
      symlink into an encrypted directory fails with EPERM when the source
      file is unencrypted or is encrypted with a different encryption policy,
      and is on the same mountpoint.  It is correct for the operation to fail,
      but the choice of EPERM breaks tools like 'mv' that know to copy rather
      than rename if they see EXDEV, but don't know what to do with EPERM.
      
      Our original motivation for EPERM was to encourage users to securely
      handle their data.  Encrypting files by "moving" them into an encrypted
      directory can be insecure because the unencrypted data may remain in
      free space on disk, where it can later be recovered by an attacker.
      It's much better to encrypt the data from the start, or at least try to
      securely delete the source data e.g. using the 'shred' program.
      
      However, the current behavior hasn't been effective at achieving its
      goal because users tend to be confused, hack around it, and complain;
      see e.g. https://github.com/google/fscrypt/issues/76.  And in some cases
      it's actually inconsistent or unnecessary.  For example, 'mv'-ing files
      between differently encrypted directories doesn't work even in cases
      where it can be secure, such as when in userspace the same passphrase
      protects both directories.  Yet, you *can* already 'mv' unencrypted
      files into an encrypted directory if the source files are on a different
      mountpoint, even though doing so is often insecure.
      
      There are probably better ways to teach users to securely handle their
      files.  For example, the 'fscrypt' userspace tool could provide a
      command that migrates unencrypted files into an encrypted directory,
      acting like 'shred' on the source files and providing appropriate
      warnings depending on the type of the source filesystem and disk.
      
      Receiving errors on unimportant files might also force some users to
      disable encryption, thus making the behavior counterproductive.  It's
      desirable to make encryption as unobtrusive as possible.
      
      Therefore, change the error code from EPERM to EXDEV so that tools
      looking for EXDEV will fall back to a copy.
      
      This, of course, doesn't prevent users from still doing the right things
      to securely manage their files.  Note that this also matches the
      behavior when a file is renamed between two project quota hierarchies;
      so there's precedent for using EXDEV for things other than mountpoints.
      
      xfstests generic/398 will require an update with this change.
      
      [Rewritten from an earlier patch series by Michael Halcrow.]
      
      Cc: Michael Halcrow <mhalcrow@google.com>
      Cc: Joe Richey <joerichey@google.com>
      Signed-off-by: NEric Biggers <ebiggers@google.com>
      f5e55e77
  16. 06 1月, 2019 1 次提交
    • E
      fscrypt: add Adiantum support · 8094c3ce
      Eric Biggers 提交于
      Add support for the Adiantum encryption mode to fscrypt.  Adiantum is a
      tweakable, length-preserving encryption mode with security provably
      reducible to that of XChaCha12 and AES-256, subject to a security bound.
      It's also a true wide-block mode, unlike XTS.  See the paper
      "Adiantum: length-preserving encryption for entry-level processors"
      (https://eprint.iacr.org/2018/720.pdf) for more details.  Also see
      commit 059c2a4d ("crypto: adiantum - add Adiantum support").
      
      On sufficiently long messages, Adiantum's bottlenecks are XChaCha12 and
      the NH hash function.  These algorithms are fast even on processors
      without dedicated crypto instructions.  Adiantum makes it feasible to
      enable storage encryption on low-end mobile devices that lack AES
      instructions; currently such devices are unencrypted.  On ARM Cortex-A7,
      on 4096-byte messages Adiantum encryption is about 4 times faster than
      AES-256-XTS encryption; decryption is about 5 times faster.
      
      In fscrypt, Adiantum is suitable for encrypting both file contents and
      names.  With filenames, it fixes a known weakness: when two filenames in
      a directory share a common prefix of >= 16 bytes, with CTS-CBC their
      encrypted filenames share a common prefix too, leaking information.
      Adiantum does not have this problem.
      
      Since Adiantum also accepts long tweaks (IVs), it's also safe to use the
      master key directly for Adiantum encryption rather than deriving
      per-file keys, provided that the per-file nonce is included in the IVs
      and the master key isn't used for any other encryption mode.  This
      configuration saves memory and improves performance.  A new fscrypt
      policy flag is added to allow users to opt-in to this configuration.
      Signed-off-by: NEric Biggers <ebiggers@google.com>
      Signed-off-by: NTheodore Ts'o <tytso@mit.edu>
      8094c3ce
  17. 02 11月, 2017 1 次提交
    • G
      License cleanup: add SPDX GPL-2.0 license identifier to files with no license · b2441318
      Greg Kroah-Hartman 提交于
      Many source files in the tree are missing licensing information, which
      makes it harder for compliance tools to determine the correct license.
      
      By default all files without license information are under the default
      license of the kernel, which is GPL version 2.
      
      Update the files which contain no license information with the 'GPL-2.0'
      SPDX license identifier.  The SPDX identifier is a legally binding
      shorthand, which can be used instead of the full boiler plate text.
      
      This patch is based on work done by Thomas Gleixner and Kate Stewart and
      Philippe Ombredanne.
      
      How this work was done:
      
      Patches were generated and checked against linux-4.14-rc6 for a subset of
      the use cases:
       - file had no licensing information it it.
       - file was a */uapi/* one with no licensing information in it,
       - file was a */uapi/* one with existing licensing information,
      
      Further patches will be generated in subsequent months to fix up cases
      where non-standard license headers were used, and references to license
      had to be inferred by heuristics based on keywords.
      
      The analysis to determine which SPDX License Identifier to be applied to
      a file was done in a spreadsheet of side by side results from of the
      output of two independent scanners (ScanCode & Windriver) producing SPDX
      tag:value files created by Philippe Ombredanne.  Philippe prepared the
      base worksheet, and did an initial spot review of a few 1000 files.
      
      The 4.13 kernel was the starting point of the analysis with 60,537 files
      assessed.  Kate Stewart did a file by file comparison of the scanner
      results in the spreadsheet to determine which SPDX license identifier(s)
      to be applied to the file. She confirmed any determination that was not
      immediately clear with lawyers working with the Linux Foundation.
      
      Criteria used to select files for SPDX license identifier tagging was:
       - Files considered eligible had to be source code files.
       - Make and config files were included as candidates if they contained >5
         lines of source
       - File already had some variant of a license header in it (even if <5
         lines).
      
      All documentation files were explicitly excluded.
      
      The following heuristics were used to determine which SPDX license
      identifiers to apply.
      
       - when both scanners couldn't find any license traces, file was
         considered to have no license information in it, and the top level
         COPYING file license applied.
      
         For non */uapi/* files that summary was:
      
         SPDX license identifier                            # files
         ---------------------------------------------------|-------
         GPL-2.0                                              11139
      
         and resulted in the first patch in this series.
      
         If that file was a */uapi/* path one, it was "GPL-2.0 WITH
         Linux-syscall-note" otherwise it was "GPL-2.0".  Results of that was:
      
         SPDX license identifier                            # files
         ---------------------------------------------------|-------
         GPL-2.0 WITH Linux-syscall-note                        930
      
         and resulted in the second patch in this series.
      
       - if a file had some form of licensing information in it, and was one
         of the */uapi/* ones, it was denoted with the Linux-syscall-note if
         any GPL family license was found in the file or had no licensing in
         it (per prior point).  Results summary:
      
         SPDX license identifier                            # files
         ---------------------------------------------------|------
         GPL-2.0 WITH Linux-syscall-note                       270
         GPL-2.0+ WITH Linux-syscall-note                      169
         ((GPL-2.0 WITH Linux-syscall-note) OR BSD-2-Clause)    21
         ((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Clause)    17
         LGPL-2.1+ WITH Linux-syscall-note                      15
         GPL-1.0+ WITH Linux-syscall-note                       14
         ((GPL-2.0+ WITH Linux-syscall-note) OR BSD-3-Clause)    5
         LGPL-2.0+ WITH Linux-syscall-note                       4
         LGPL-2.1 WITH Linux-syscall-note                        3
         ((GPL-2.0 WITH Linux-syscall-note) OR MIT)              3
         ((GPL-2.0 WITH Linux-syscall-note) AND MIT)             1
      
         and that resulted in the third patch in this series.
      
       - when the two scanners agreed on the detected license(s), that became
         the concluded license(s).
      
       - when there was disagreement between the two scanners (one detected a
         license but the other didn't, or they both detected different
         licenses) a manual inspection of the file occurred.
      
       - In most cases a manual inspection of the information in the file
         resulted in a clear resolution of the license that should apply (and
         which scanner probably needed to revisit its heuristics).
      
       - When it was not immediately clear, the license identifier was
         confirmed with lawyers working with the Linux Foundation.
      
       - If there was any question as to the appropriate license identifier,
         the file was flagged for further research and to be revisited later
         in time.
      
      In total, over 70 hours of logged manual review was done on the
      spreadsheet to determine the SPDX license identifiers to apply to the
      source files by Kate, Philippe, Thomas and, in some cases, confirmation
      by lawyers working with the Linux Foundation.
      
      Kate also obtained a third independent scan of the 4.13 code base from
      FOSSology, and compared selected files where the other two scanners
      disagreed against that SPDX file, to see if there was new insights.  The
      Windriver scanner is based on an older version of FOSSology in part, so
      they are related.
      
      Thomas did random spot checks in about 500 files from the spreadsheets
      for the uapi headers and agreed with SPDX license identifier in the
      files he inspected. For the non-uapi files Thomas did random spot checks
      in about 15000 files.
      
      In initial set of patches against 4.14-rc6, 3 files were found to have
      copy/paste license identifier errors, and have been fixed to reflect the
      correct identifier.
      
      Additionally Philippe spent 10 hours this week doing a detailed manual
      inspection and review of the 12,461 patched files from the initial patch
      version early this week with:
       - a full scancode scan run, collecting the matched texts, detected
         license ids and scores
       - reviewing anything where there was a license detected (about 500+
         files) to ensure that the applied SPDX license was correct
       - reviewing anything where there was no detection but the patch license
         was not GPL-2.0 WITH Linux-syscall-note to ensure that the applied
         SPDX license was correct
      
      This produced a worksheet with 20 files needing minor correction.  This
      worksheet was then exported into 3 different .csv files for the
      different types of files to be modified.
      
      These .csv files were then reviewed by Greg.  Thomas wrote a script to
      parse the csv files and add the proper SPDX tag to the file, in the
      format that the file expected.  This script was further refined by Greg
      based on the output to detect more types of files automatically and to
      distinguish between header and source .c files (which need different
      comment types.)  Finally Greg ran the script using the .csv files to
      generate the patches.
      Reviewed-by: NKate Stewart <kstewart@linuxfoundation.org>
      Reviewed-by: NPhilippe Ombredanne <pombredanne@nexb.com>
      Reviewed-by: NThomas Gleixner <tglx@linutronix.de>
      Signed-off-by: NGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      b2441318
  18. 19 10月, 2017 1 次提交
  19. 06 7月, 2017 1 次提交
    • T
      ext4: fix __ext4_new_inode() journal credits calculation · af65207c
      Tahsin Erdogan 提交于
      ea_inode feature allows creating extended attributes that are up to
      64k in size. Update __ext4_new_inode() to pick increased credit limits.
      
      To avoid overallocating too many journal credits, update
      __ext4_xattr_set_credits() to make a distinction between xattr create
      vs update. This helps __ext4_new_inode() because all attributes are
      known to be new, so we can save credits that are normally needed to
      delete old values.
      
      Also, have fscrypt specify its maximum context size so that we don't
      end up allocating credits for 64k size.
      Signed-off-by: NTahsin Erdogan <tahsin@google.com>
      Signed-off-by: NTheodore Ts'o <tytso@mit.edu>
      af65207c
  20. 24 6月, 2017 1 次提交
    • D
      fscrypt: add support for AES-128-CBC · b7e7cf7a
      Daniel Walter 提交于
      fscrypt provides facilities to use different encryption algorithms which
      are selectable by userspace when setting the encryption policy. Currently,
      only AES-256-XTS for file contents and AES-256-CBC-CTS for file names are
      implemented. This is a clear case of kernel offers the mechanism and
      userspace selects a policy. Similar to what dm-crypt and ecryptfs have.
      
      This patch adds support for using AES-128-CBC for file contents and
      AES-128-CBC-CTS for file name encryption. To mitigate watermarking
      attacks, IVs are generated using the ESSIV algorithm. While AES-CBC is
      actually slightly less secure than AES-XTS from a security point of view,
      there is more widespread hardware support. Using AES-CBC gives us the
      acceptable performance while still providing a moderate level of security
      for persistent storage.
      
      Especially low-powered embedded devices with crypto accelerators such as
      CAAM or CESA often only support AES-CBC. Since using AES-CBC over AES-XTS
      is basically thought of a last resort, we use AES-128-CBC over AES-256-CBC
      since it has less encryption rounds and yields noticeable better
      performance starting from a file size of just a few kB.
      Signed-off-by: NDaniel Walter <dwalter@sigma-star.at>
      [david@sigma-star.at: addressed review comments]
      Signed-off-by: NDavid Gstir <david@sigma-star.at>
      Reviewed-by: NEric Biggers <ebiggers@google.com>
      Signed-off-by: NTheodore Ts'o <tytso@mit.edu>
      b7e7cf7a
  21. 04 5月, 2017 1 次提交
    • E
      fscrypt: fix context consistency check when key(s) unavailable · 272f98f6
      Eric Biggers 提交于
      To mitigate some types of offline attacks, filesystem encryption is
      designed to enforce that all files in an encrypted directory tree use
      the same encryption policy (i.e. the same encryption context excluding
      the nonce).  However, the fscrypt_has_permitted_context() function which
      enforces this relies on comparing struct fscrypt_info's, which are only
      available when we have the encryption keys.  This can cause two
      incorrect behaviors:
      
      1. If we have the parent directory's key but not the child's key, or
         vice versa, then fscrypt_has_permitted_context() returned false,
         causing applications to see EPERM or ENOKEY.  This is incorrect if
         the encryption contexts are in fact consistent.  Although we'd
         normally have either both keys or neither key in that case since the
         master_key_descriptors would be the same, this is not guaranteed
         because keys can be added or removed from keyrings at any time.
      
      2. If we have neither the parent's key nor the child's key, then
         fscrypt_has_permitted_context() returned true, causing applications
         to see no error (or else an error for some other reason).  This is
         incorrect if the encryption contexts are in fact inconsistent, since
         in that case we should deny access.
      
      To fix this, retrieve and compare the fscrypt_contexts if we are unable
      to set up both fscrypt_infos.
      
      While this slightly hurts performance when accessing an encrypted
      directory tree without the key, this isn't a case we really need to be
      optimizing for; access *with* the key is much more important.
      Furthermore, the performance hit is barely noticeable given that we are
      already retrieving the fscrypt_context and doing two keyring searches in
      fscrypt_get_encryption_info().  If we ever actually wanted to optimize
      this case we might start by caching the fscrypt_contexts.
      
      Cc: stable@vger.kernel.org # 4.0+
      Signed-off-by: NEric Biggers <ebiggers@google.com>
      Signed-off-by: NTheodore Ts'o <tytso@mit.edu>
      272f98f6
  22. 30 4月, 2017 1 次提交
  23. 16 3月, 2017 1 次提交
    • E
      fscrypt: eliminate ->prepare_context() operation · 94840e3c
      Eric Biggers 提交于
      The only use of the ->prepare_context() fscrypt operation was to allow
      ext4 to evict inline data from the inode before ->set_context().
      However, there is no reason why this cannot be done as simply the first
      step in ->set_context(), and in fact it makes more sense to do it that
      way because then the policy modes and flags get validated before any
      real work is done.  Therefore, merge ext4_prepare_context() into
      ext4_set_context(), and remove ->prepare_context().
      Signed-off-by: NEric Biggers <ebiggers@google.com>
      Signed-off-by: NTheodore Ts'o <tytso@mit.edu>
      94840e3c
  24. 03 1月, 2017 1 次提交
    • T
      fscrypt: make test_dummy_encryption require a keyring key · 5bbdcbbb
      Theodore Ts'o 提交于
      Currently, the test_dummy_encryption ext4 mount option, which exists
      only to test encrypted I/O paths with xfstests, overrides all
      per-inode encryption keys with a fixed key.
      
      This change minimizes test_dummy_encryption-specific code path changes
      by supplying a fake context for directories which are not encrypted
      for use when creating new directories, files, or symlinks.  This
      allows us to properly exercise the keyring lookup, derivation, and
      context inheritance code paths.
      
      Before mounting a file system using test_dummy_encryption, userspace
      must execute the following shell commands:
      
          mode='\x00\x00\x00\x00'
          raw="$(printf ""\\\\x%02x"" $(seq 0 63))"
          if lscpu | grep "Byte Order" | grep -q Little ; then
              size='\x40\x00\x00\x00'
          else
              size='\x00\x00\x00\x40'
          fi
          key="${mode}${raw}${size}"
          keyctl new_session
          echo -n -e "${key}" | keyctl padd logon fscrypt:4242424242424242 @s
      Signed-off-by: NTheodore Ts'o <tytso@mit.edu>
      5bbdcbbb
  25. 01 1月, 2017 4 次提交
  26. 31 12月, 2016 1 次提交
    • E
      fscrypt: fix renaming and linking special files · 42d97eb0
      Eric Biggers 提交于
      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: 9bd8212f ("ext4 crypto: add encryption policy and password salt support")
      Signed-off-by: NEric Biggers <ebiggers@google.com>
      Reviewed-by: NRichard Weinberger <richard@nod.at>
      Cc: stable@vger.kernel.org
      Signed-off-by: NTheodore Ts'o <tytso@mit.edu>
      42d97eb0
  27. 12 12月, 2016 2 次提交