1. 21 9月, 2021 1 次提交
  2. 29 7月, 2021 1 次提交
  3. 26 7月, 2021 1 次提交
    • E
      fscrypt: add fscrypt_symlink_getattr() for computing st_size · d1876056
      Eric Biggers 提交于
      Add a helper function fscrypt_symlink_getattr() which will be called
      from the various filesystems' ->getattr() methods to read and decrypt
      the target of encrypted symlinks in order to report the correct st_size.
      
      Detailed explanation:
      
      As required by POSIX and as documented in various man pages, st_size for
      a symlink is supposed to be the length of the symlink target.
      Unfortunately, st_size has always been wrong for encrypted symlinks
      because st_size is populated from i_size from disk, which intentionally
      contains the length of the encrypted symlink target.  That's slightly
      greater than the length of the decrypted symlink target (which is the
      symlink target that userspace usually sees), and usually won't match the
      length of the no-key encoded symlink target either.
      
      This hadn't been fixed yet because reporting the correct st_size would
      require reading the symlink target from disk and decrypting or encoding
      it, which historically has been considered too heavyweight to do in
      ->getattr().  Also historically, the wrong st_size had only broken a
      test (LTP lstat03) and there were no known complaints from real users.
      (This is probably because the st_size of symlinks isn't used too often,
      and when it is, typically it's for a hint for what buffer size to pass
      to readlink() -- which a slightly-too-large size still works for.)
      
      However, a couple things have changed now.  First, there have recently
      been complaints about the current behavior from real users:
      
      - Breakage in rpmbuild:
        https://github.com/rpm-software-management/rpm/issues/1682
        https://github.com/google/fscrypt/issues/305
      
      - Breakage in toybox cpio:
        https://www.mail-archive.com/toybox@lists.landley.net/msg07193.html
      
      - Breakage in libgit2: https://issuetracker.google.com/issues/189629152
        (on Android public issue tracker, requires login)
      
      Second, we now cache decrypted symlink targets in ->i_link.  Therefore,
      taking the performance hit of reading and decrypting the symlink target
      in ->getattr() wouldn't be as big a deal as it used to be, since usually
      it will just save having to do the same thing later.
      
      Also note that eCryptfs ended up having to read and decrypt symlink
      targets in ->getattr() as well, to fix this same issue; see
      commit 3a60a168 ("eCryptfs: Decrypt symlink target for stat size").
      
      So, let's just bite the bullet, and read and decrypt the symlink target
      in ->getattr() in order to report the correct st_size.  Add a function
      fscrypt_symlink_getattr() which the filesystems will call to do this.
      
      (Alternatively, we could store the decrypted size of symlinks on-disk.
      But there isn't a great place to do so, and encryption is meant to hide
      the original size to some extent; that property would be lost.)
      
      Cc: stable@vger.kernel.org
      Link: https://lore.kernel.org/r/20210702065350.209646-2-ebiggers@kernel.orgSigned-off-by: NEric Biggers <ebiggers@google.com>
      d1876056
  4. 03 12月, 2020 6 次提交
  5. 25 11月, 2020 2 次提交
    • E
      fscrypt: remove unnecessary calls to fscrypt_require_key() · 234f1b7f
      Eric Biggers 提交于
      In an encrypted directory, a regular dentry (one that doesn't have the
      no-key name flag) can only be created if the directory's encryption key
      is available.
      
      Therefore the calls to fscrypt_require_key() in __fscrypt_prepare_link()
      and __fscrypt_prepare_rename() are unnecessary, as these functions
      already check that the dentries they're given aren't no-key names.
      
      Remove these unnecessary calls to fscrypt_require_key().
      
      Link: https://lore.kernel.org/r/20201118075609.120337-6-ebiggers@kernel.orgSigned-off-by: NEric Biggers <ebiggers@google.com>
      234f1b7f
    • E
      fscrypt: add fscrypt_is_nokey_name() · 159e1de2
      Eric Biggers 提交于
      It's possible to create a duplicate filename in an encrypted directory
      by creating a file concurrently with adding the encryption key.
      
      Specifically, sys_open(O_CREAT) (or sys_mkdir(), sys_mknod(), or
      sys_symlink()) can lookup the target filename while the directory's
      encryption key hasn't been added yet, resulting in a negative no-key
      dentry.  The VFS then calls ->create() (or ->mkdir(), ->mknod(), or
      ->symlink()) because the dentry is negative.  Normally, ->create() would
      return -ENOKEY due to the directory's key being unavailable.  However,
      if the key was added between the dentry lookup and ->create(), then the
      filesystem will go ahead and try to create the file.
      
      If the target filename happens to already exist as a normal name (not a
      no-key name), a duplicate filename may be added to the directory.
      
      In order to fix this, we need to fix the filesystems to prevent
      ->create(), ->mkdir(), ->mknod(), and ->symlink() on no-key names.
      (->rename() and ->link() need it too, but those are already handled
      correctly by fscrypt_prepare_rename() and fscrypt_prepare_link().)
      
      In preparation for this, add a helper function fscrypt_is_nokey_name()
      that filesystems can use to do this check.  Use this helper function for
      the existing checks that fs/crypto/ does for rename and link.
      
      Cc: stable@vger.kernel.org
      Link: https://lore.kernel.org/r/20201118075609.120337-2-ebiggers@kernel.orgSigned-off-by: NEric Biggers <ebiggers@google.com>
      159e1de2
  6. 29 9月, 2020 1 次提交
  7. 24 9月, 2020 2 次提交
  8. 22 9月, 2020 5 次提交
    • 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: move fscrypt_prepare_symlink() out-of-line · 31114726
      Eric Biggers 提交于
      In preparation for moving the logic for "get the encryption policy
      inherited by new files in this directory" to a single place, make
      fscrypt_prepare_symlink() a regular function rather than an inline
      function that wraps __fscrypt_prepare_symlink().
      
      This way, the new function fscrypt_policy_to_inherit() won't need to be
      exported to filesystems.
      Acked-by: NJeff Layton <jlayton@kernel.org>
      Link: https://lore.kernel.org/r/20200917041136.178600-12-ebiggers@kernel.orgSigned-off-by: NEric Biggers <ebiggers@google.com>
      31114726
    • 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
  9. 08 9月, 2020 1 次提交
  10. 22 7月, 2020 1 次提交
    • 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
  11. 09 7月, 2020 1 次提交
    • S
      fscrypt: add inline encryption support · 5fee3609
      Satya Tangirala 提交于
      Add support for inline encryption to fs/crypto/.  With "inline
      encryption", the block layer handles the decryption/encryption as part
      of the bio, instead of the filesystem doing the crypto itself via
      Linux's crypto API. This model is needed in order to take advantage of
      the inline encryption hardware present on most modern mobile SoCs.
      
      To use inline encryption, the filesystem needs to be mounted with
      '-o inlinecrypt'. Blk-crypto will then be used instead of the traditional
      filesystem-layer crypto whenever possible to encrypt the contents
      of any encrypted files in that filesystem. Fscrypt still provides the key
      and IV to use, and the actual ciphertext on-disk is still the same;
      therefore it's testable using the existing fscrypt ciphertext verification
      tests.
      
      Note that since blk-crypto has a fallback to Linux's crypto API, and
      also supports all the encryption modes currently supported by fscrypt,
      this feature is usable and testable even without actual inline
      encryption hardware.
      
      Per-filesystem changes will be needed to set encryption contexts when
      submitting bios and to implement the 'inlinecrypt' mount option.  This
      patch just adds the common code.
      Signed-off-by: NSatya Tangirala <satyat@google.com>
      Reviewed-by: NJaegeuk Kim <jaegeuk@kernel.org>
      Reviewed-by: NEric Biggers <ebiggers@google.com>
      Reviewed-by: NTheodore Ts'o <tytso@mit.edu>
      Link: https://lore.kernel.org/r/20200702015607.1215430-3-satyat@google.comCo-developed-by: NEric Biggers <ebiggers@google.com>
      Signed-off-by: NEric Biggers <ebiggers@google.com>
      5fee3609
  12. 19 5月, 2020 1 次提交
    • 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
  13. 13 5月, 2020 3 次提交
  14. 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
  15. 23 1月, 2020 3 次提交
    • D
      fscrypt: improve format of no-key names · edc440e3
      Daniel Rosenberg 提交于
      When an encrypted directory is listed without the key, the filesystem
      must show "no-key names" that uniquely identify directory entries, are
      at most 255 (NAME_MAX) bytes long, and don't contain '/' or '\0'.
      Currently, for short names the no-key name is the base64 encoding of the
      ciphertext filename, while for long names it's the base64 encoding of
      the ciphertext filename's dirhash and second-to-last 16-byte block.
      
      This format has the following problems:
      
      - Since it doesn't always include the dirhash, it's incompatible with
        directories that will use a secret-keyed dirhash over the plaintext
        filenames.  In this case, the dirhash won't be computable from the
        ciphertext name without the key, so it instead must be retrieved from
        the directory entry and always included in the no-key name.
        Casefolded encrypted directories will use this type of dirhash.
      
      - It's ambiguous: it's possible to craft two filenames that map to the
        same no-key name, since the method used to abbreviate long filenames
        doesn't use a proper cryptographic hash function.
      
      Solve both these problems by switching to a new no-key name format that
      is the base64 encoding of a variable-length structure that contains the
      dirhash, up to 149 bytes of the ciphertext filename, and (if any bytes
      remain) the SHA-256 of the remaining bytes of the ciphertext filename.
      
      This ensures that each no-key name contains everything needed to find
      the directory entry again, contains only legal characters, doesn't
      exceed NAME_MAX, is unambiguous unless there's a SHA-256 collision, and
      that we only take the performance hit of SHA-256 on very long filenames.
      
      Note: this change does *not* address the existing issue where users can
      modify the 'dirhash' part of a no-key name and the filesystem may still
      accept the name.
      Signed-off-by: NDaniel Rosenberg <drosen@google.com>
      [EB: improved comments and commit message, fixed checking return value
       of base64_decode(), check for SHA-256 error, continue to set disk_name
       for short names to keep matching simpler, and many other cleanups]
      Link: https://lore.kernel.org/r/20200120223201.241390-7-ebiggers@kernel.orgSigned-off-by: NEric Biggers <ebiggers@google.com>
      edc440e3
    • D
      fscrypt: derive dirhash key for casefolded directories · aa408f83
      Daniel Rosenberg 提交于
      When we allow indexed directories to use both encryption and
      casefolding, for the dirhash we can't just hash the ciphertext filenames
      that are stored on-disk (as is done currently) because the dirhash must
      be case insensitive, but the stored names are case-preserving.  Nor can
      we hash the plaintext names with an unkeyed hash (or a hash keyed with a
      value stored on-disk like ext4's s_hash_seed), since that would leak
      information about the names that encryption is meant to protect.
      
      Instead, if we can accept a dirhash that's only computable when the
      fscrypt key is available, we can hash the plaintext names with a keyed
      hash using a secret key derived from the directory's fscrypt master key.
      We'll use SipHash-2-4 for this purpose.
      
      Prepare for this by deriving a SipHash key for each casefolded encrypted
      directory.  Make sure to handle deriving the key not only when setting
      up the directory's fscrypt_info, but also in the case where the casefold
      flag is enabled after the fscrypt_info was already set up.  (We could
      just always derive the key regardless of casefolding, but that would
      introduce unnecessary overhead for people not using casefolding.)
      Signed-off-by: NDaniel Rosenberg <drosen@google.com>
      [EB: improved commit message, updated fscrypt.rst, squashed with change
       that avoids unnecessarily deriving the key, and many other cleanups]
      Link: https://lore.kernel.org/r/20200120223201.241390-3-ebiggers@kernel.orgSigned-off-by: NEric Biggers <ebiggers@google.com>
      aa408f83
    • D
      fscrypt: don't allow v1 policies with casefolding · 6e1918cf
      Daniel Rosenberg 提交于
      Casefolded encrypted directories will use a new dirhash method that
      requires a secret key.  If the directory uses a v2 encryption policy,
      it's easy to derive this key from the master key using HKDF.  However,
      v1 encryption policies don't provide a way to derive additional keys.
      
      Therefore, don't allow casefolding on directories that use a v1 policy.
      Specifically, make it so that trying to enable casefolding on a
      directory that has a v1 policy fails, trying to set a v1 policy on a
      casefolded directory fails, and trying to open a casefolded directory
      that has a v1 policy (if one somehow exists on-disk) fails.
      Signed-off-by: NDaniel Rosenberg <drosen@google.com>
      [EB: improved commit message, updated fscrypt.rst, and other cleanups]
      Link: https://lore.kernel.org/r/20200120223201.241390-2-ebiggers@kernel.orgSigned-off-by: NEric Biggers <ebiggers@google.com>
      6e1918cf
  16. 01 1月, 2020 2 次提交
  17. 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
  18. 22 10月, 2019 1 次提交
    • E
      fscrypt: remove struct fscrypt_ctx · 1565bdad
      Eric Biggers 提交于
      Now that ext4 and f2fs implement their own post-read workflow that
      supports both fscrypt and fsverity, the fscrypt-only workflow based
      around struct fscrypt_ctx is no longer used.  So remove the unused code.
      
      This is based on a patch from Chandan Rajendra's "Consolidate FS read
      I/O callbacks code" patchset, but rebased onto the latest kernel, folded
      __fscrypt_decrypt_bio() into fscrypt_decrypt_bio(), cleaned up
      fscrypt_initialize(), and updated the commit message.
      
      Originally-from: Chandan Rajendra <chandan@linux.ibm.com>
      Signed-off-by: NEric Biggers <ebiggers@google.com>
      1565bdad
  19. 13 8月, 2019 6 次提交
    • E
      fscrypt: add FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS ioctl · 78a1b96b
      Eric Biggers 提交于
      Add a root-only variant of the FS_IOC_REMOVE_ENCRYPTION_KEY ioctl which
      removes all users' claims of the key, not just the current user's claim.
      I.e., it always removes the key itself, no matter how many users have
      added it.
      
      This is useful for forcing a directory to be locked, without having to
      figure out which user ID(s) the key was added under.  This is planned to
      be used by a command like 'sudo fscrypt lock DIR --all-users' in the
      fscrypt userspace tool (http://github.com/google/fscrypt).
      Reviewed-by: NTheodore Ts'o <tytso@mit.edu>
      Signed-off-by: NEric Biggers <ebiggers@google.com>
      78a1b96b
    • 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: add FS_IOC_GET_ENCRYPTION_KEY_STATUS ioctl · 5a7e2992
      Eric Biggers 提交于
      Add a new fscrypt ioctl, FS_IOC_GET_ENCRYPTION_KEY_STATUS.  Given a key
      specified by 'struct fscrypt_key_specifier' (the same way a key is
      specified for the other fscrypt key management ioctls), it returns
      status information in a 'struct fscrypt_get_key_status_arg'.
      
      The main motivation for this is that applications need to be able to
      check whether an encrypted directory is "unlocked" or not, so that they
      can add the key if it is not, and avoid adding the key (which may
      involve prompting the user for a passphrase) if it already is.
      
      It's possible to use some workarounds such as checking whether opening a
      regular file fails with ENOKEY, or checking whether the filenames "look
      like gibberish" or not.  However, no workaround is usable in all cases.
      
      Like the other key management ioctls, the keyrings syscalls may seem at
      first to be a good fit for this.  Unfortunately, they are not.  Even if
      we exposed the keyring ID of the ->s_master_keys keyring and gave
      everyone Search permission on it (note: currently the keyrings
      permission system would also allow everyone to "invalidate" the keyring
      too), the fscrypt keys have an additional state that doesn't map cleanly
      to the keyrings API: the secret can be removed, but we can be still
      tracking the files that were using the key, and the removal can be
      re-attempted or the secret added again.
      
      After later patches, some applications will also need a way to determine
      whether a key was added by the current user vs. by some other user.
      Reserved fields are included in fscrypt_get_key_status_arg for this and
      other future extensions.
      Reviewed-by: NTheodore Ts'o <tytso@mit.edu>
      Signed-off-by: NEric Biggers <ebiggers@google.com>
      5a7e2992
    • E
      fscrypt: add FS_IOC_REMOVE_ENCRYPTION_KEY ioctl · b1c0ec35
      Eric Biggers 提交于
      Add a new fscrypt ioctl, FS_IOC_REMOVE_ENCRYPTION_KEY.  This ioctl
      removes an encryption key that was added by FS_IOC_ADD_ENCRYPTION_KEY.
      It wipes the secret key itself, then "locks" the encrypted files and
      directories that had been unlocked using that key -- implemented by
      evicting the relevant dentries and inodes from the VFS caches.
      
      The problem this solves is that many fscrypt users want the ability to
      remove encryption keys, causing the corresponding encrypted directories
      to appear "locked" (presented in ciphertext form) again.  Moreover,
      users want removing an encryption key to *really* remove it, in the
      sense that the removed keys cannot be recovered even if kernel memory is
      compromised, e.g. by the exploit of a kernel security vulnerability or
      by a physical attack.  This is desirable after a user logs out of the
      system, for example.  In many cases users even already assume this to be
      the case and are surprised to hear when it's not.
      
      It is not sufficient to simply unlink the master key from the keyring
      (or to revoke or invalidate it), since the actual encryption transform
      objects are still pinned in memory by their inodes.  Therefore, to
      really remove a key we must also evict the relevant inodes.
      
      Currently one workaround is to run 'sync && echo 2 >
      /proc/sys/vm/drop_caches'.  But, that evicts all unused inodes in the
      system rather than just the inodes associated with the key being
      removed, causing severe performance problems.  Moreover, it requires
      root privileges, so regular users can't "lock" their encrypted files.
      
      Another workaround, used in Chromium OS kernels, is to add a new
      VFS-level ioctl FS_IOC_DROP_CACHE which is a more restricted version of
      drop_caches that operates on a single super_block.  It does:
      
              shrink_dcache_sb(sb);
              invalidate_inodes(sb, false);
      
      But it's still a hack.  Yet, the major users of filesystem encryption
      want this feature badly enough that they are actually using these hacks.
      
      To properly solve the problem, start maintaining a list of the inodes
      which have been "unlocked" using each master key.  Originally this
      wasn't possible because the kernel didn't keep track of in-use master
      keys at all.  But, with the ->s_master_keys keyring it is now possible.
      
      Then, add an ioctl FS_IOC_REMOVE_ENCRYPTION_KEY.  It finds the specified
      master key in ->s_master_keys, then wipes the secret key itself, which
      prevents any additional inodes from being unlocked with the key.  Then,
      it syncs the filesystem and evicts the inodes in the key's list.  The
      normal inode eviction code will free and wipe the per-file keys (in
      ->i_crypt_info).  Note that freeing ->i_crypt_info without evicting the
      inodes was also considered, but would have been racy.
      
      Some inodes may still be in use when a master key is removed, and we
      can't simply revoke random file descriptors, mmap's, etc.  Thus, the
      ioctl simply skips in-use inodes, and returns -EBUSY to indicate that
      some inodes weren't evicted.  The master key *secret* is still removed,
      but the fscrypt_master_key struct remains to keep track of the remaining
      inodes.  Userspace can then retry the ioctl to evict the remaining
      inodes.  Alternatively, if userspace adds the key again, the refreshed
      secret will be associated with the existing list of inodes so they
      remain correctly tracked for future key removals.
      
      The ioctl doesn't wipe pagecache pages.  Thus, we tolerate that after a
      kernel compromise some portions of plaintext file contents may still be
      recoverable from memory.  This can be solved by enabling page poisoning
      system-wide, which security conscious users may choose to do.  But it's
      very difficult to solve otherwise, e.g. note that plaintext file
      contents may have been read in other places than pagecache pages.
      
      Like FS_IOC_ADD_ENCRYPTION_KEY, FS_IOC_REMOVE_ENCRYPTION_KEY is
      initially restricted to privileged users only.  This is sufficient for
      some use cases, but not all.  A later patch will relax this restriction,
      but it will require introducing key hashes, among other changes.
      Reviewed-by: NTheodore Ts'o <tytso@mit.edu>
      Signed-off-by: NEric Biggers <ebiggers@google.com>
      b1c0ec35
    • E
      fscrypt: add FS_IOC_ADD_ENCRYPTION_KEY ioctl · 22d94f49
      Eric Biggers 提交于
      Add a new fscrypt ioctl, FS_IOC_ADD_ENCRYPTION_KEY.  This ioctl adds an
      encryption key to the filesystem's fscrypt keyring ->s_master_keys,
      making any files encrypted with that key appear "unlocked".
      
      Why we need this
      ~~~~~~~~~~~~~~~~
      
      The main problem is that the "locked/unlocked" (ciphertext/plaintext)
      status of encrypted files is global, but the fscrypt keys are not.
      fscrypt only looks for keys in the keyring(s) the process accessing the
      filesystem is subscribed to: the thread keyring, process keyring, and
      session keyring, where the session keyring may contain the user keyring.
      
      Therefore, userspace has to put fscrypt keys in the keyrings for
      individual users or sessions.  But this means that when a process with a
      different keyring tries to access encrypted files, whether they appear
      "unlocked" or not is nondeterministic.  This is because it depends on
      whether the files are currently present in the inode cache.
      
      Fixing this by consistently providing each process its own view of the
      filesystem depending on whether it has the key or not isn't feasible due
      to how the VFS caches work.  Furthermore, while sometimes users expect
      this behavior, it is misguided for two reasons.  First, it would be an
      OS-level access control mechanism largely redundant with existing access
      control mechanisms such as UNIX file permissions, ACLs, LSMs, etc.
      Encryption is actually for protecting the data at rest.
      
      Second, almost all users of fscrypt actually do need the keys to be
      global.  The largest users of fscrypt, Android and Chromium OS, achieve
      this by having PID 1 create a "session keyring" that is inherited by
      every process.  This works, but it isn't scalable because it prevents
      session keyrings from being used for any other purpose.
      
      On general-purpose Linux distros, the 'fscrypt' userspace tool [1] can't
      similarly abuse the session keyring, so to make 'sudo' work on all
      systems it has to link all the user keyrings into root's user keyring
      [2].  This is ugly and raises security concerns.  Moreover it can't make
      the keys available to system services, such as sshd trying to access the
      user's '~/.ssh' directory (see [3], [4]) or NetworkManager trying to
      read certificates from the user's home directory (see [5]); or to Docker
      containers (see [6], [7]).
      
      By having an API to add a key to the *filesystem* we'll be able to fix
      the above bugs, remove userspace workarounds, and clearly express the
      intended semantics: the locked/unlocked status of an encrypted directory
      is global, and encryption is orthogonal to OS-level access control.
      
      Why not use the add_key() syscall
      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      
      We use an ioctl for this API rather than the existing add_key() system
      call because the ioctl gives us the flexibility needed to implement
      fscrypt-specific semantics that will be introduced in later patches:
      
      - Supporting key removal with the semantics such that the secret is
        removed immediately and any unused inodes using the key are evicted;
        also, the eviction of any in-use inodes can be retried.
      
      - Calculating a key-dependent cryptographic identifier and returning it
        to userspace.
      
      - Allowing keys to be added and removed by non-root users, but only keys
        for v2 encryption policies; and to prevent denial-of-service attacks,
        users can only remove keys they themselves have added, and a key is
        only really removed after all users who added it have removed it.
      
      Trying to shoehorn these semantics into the keyrings syscalls would be
      very difficult, whereas the ioctls make things much easier.
      
      However, to reuse code the implementation still uses the keyrings
      service internally.  Thus we get lockless RCU-mode key lookups without
      having to re-implement it, and the keys automatically show up in
      /proc/keys for debugging purposes.
      
      References:
      
          [1] https://github.com/google/fscrypt
          [2] https://goo.gl/55cCrI#heading=h.vf09isp98isb
          [3] https://github.com/google/fscrypt/issues/111#issuecomment-444347939
          [4] https://github.com/google/fscrypt/issues/116
          [5] https://bugs.launchpad.net/ubuntu/+source/fscrypt/+bug/1770715
          [6] https://github.com/google/fscrypt/issues/128
          [7] https://askubuntu.com/questions/1130306/cannot-run-docker-on-an-encrypted-filesystemReviewed-by: NTheodore Ts'o <tytso@mit.edu>
      Signed-off-by: NEric Biggers <ebiggers@google.com>
      22d94f49
    • E
      fscrypt: rename keyinfo.c to keysetup.c · feed8258
      Eric Biggers 提交于
      Rename keyinfo.c to keysetup.c since this better describes what the file
      does (sets up the key), and it matches the new file keysetup_v1.c.
      Reviewed-by: NTheodore Ts'o <tytso@mit.edu>
      Signed-off-by: NEric Biggers <ebiggers@google.com>
      feed8258