Skip to content
Snippets Groups Projects
  1. Oct 12, 2023
  2. Apr 06, 2023
  3. Mar 30, 2023
  4. Sep 26, 2022
    • Boris Burkov's avatar
      btrfs: send: add support for fs-verity · 38622010
      Boris Burkov authored
      
      Preserve the fs-verity status of a btrfs file across send/recv.
      
      There is no facility for installing the Merkle tree contents directly on
      the receiving filesystem, so we package up the parameters used to enable
      verity found in the verity descriptor. This gives the receive side
      enough information to properly enable verity again. Note that this means
      that receive will have to re-compute the whole Merkle tree, similar to
      how compression worked before encoded_write.
      
      Since the file becomes read-only after verity is enabled, it is
      important that verity is added to the send stream after any file writes.
      Therefore, when we process a verity item, merely note that it happened,
      then actually create the command in the send stream during
      'finish_inode_if_needed'.
      
      This also creates V3 of the send stream format, without any format
      changes besides adding the new commands and attributes.
      
      Signed-off-by: default avatarBoris Burkov <boris@bur.io>
      Signed-off-by: default avatarDavid Sterba <dsterba@suse.com>
      38622010
  5. Aug 19, 2022
  6. Jul 16, 2022
  7. May 19, 2022
  8. May 18, 2022
  9. May 08, 2022
  10. May 01, 2022
  11. Apr 01, 2022
  12. Sep 22, 2021
  13. Apr 22, 2021
    • Ard Biesheuvel's avatar
      fsverity: relax build time dependency on CRYPTO_SHA256 · e3a606f2
      Ard Biesheuvel authored
      
      CONFIG_CRYPTO_SHA256 denotes the generic C implementation of the SHA-256
      shash algorithm, which is selected as the default crypto shash provider
      for fsverity. However, fsverity has no strict link time dependency, and
      the same shash could be exposed by an optimized implementation, and arm64
      has a number of those (scalar, NEON-based and one based on special crypto
      instructions). In such cases, it makes little sense to require that the
      generic C implementation is incorporated as well, given that it will never
      be called.
      
      To address this, relax the 'select' clause to 'imply' so that the generic
      driver can be omitted from the build if desired.
      
      Acked-by: default avatarEric Biggers <ebiggers@google.com>
      Signed-off-by: default avatarArd Biesheuvel <ardb@kernel.org>
      Signed-off-by: default avatarHerbert Xu <herbert@gondor.apana.org.au>
      e3a606f2
  14. Feb 07, 2021
    • Eric Biggers's avatar
      fs-verity: support reading signature with ioctl · 07c99001
      Eric Biggers authored
      Add support for FS_VERITY_METADATA_TYPE_SIGNATURE to
      FS_IOC_READ_VERITY_METADATA.  This allows a userspace server program to
      retrieve the built-in signature (if present) of a verity file for
      serving to a client which implements fs-verity compatible verification.
      See the patch which introduced FS_IOC_READ_VERITY_METADATA for more
      details.
      
      The ability for userspace to read the built-in signatures is also useful
      because it allows a system that is using the in-kernel signature
      verification to migrate to userspace signature verification.
      
      This has been tested using a new xfstest which calls this ioctl via a
      new subcommand for the 'fsverity' program from fsverity-utils.
      
      Link: https://lore.kernel.org/r/20210115181819.34732-7-ebiggers@kernel.org
      
      
      Reviewed-by: default avatarVictor Hsieh <victorhsieh@google.com>
      Reviewed-by: default avatarJaegeuk Kim <jaegeuk@kernel.org>
      Reviewed-by: default avatarChao Yu <yuchao0@huawei.com>
      Signed-off-by: default avatarEric Biggers <ebiggers@google.com>
      07c99001
    • Eric Biggers's avatar
      fs-verity: support reading descriptor with ioctl · 947191ac
      Eric Biggers authored
      Add support for FS_VERITY_METADATA_TYPE_DESCRIPTOR to
      FS_IOC_READ_VERITY_METADATA.  This allows a userspace server program to
      retrieve the fs-verity descriptor of a file for serving to a client
      which implements fs-verity compatible verification.  See the patch which
      introduced FS_IOC_READ_VERITY_METADATA for more details.
      
      "fs-verity descriptor" here means only the part that userspace cares
      about because it is hashed to produce the file digest.  It doesn't
      include the signature which ext4 and f2fs append to the
      fsverity_descriptor struct when storing it on-disk, since that way of
      storing the signature is an implementation detail.  The next patch adds
      a separate metadata_type value for retrieving the signature separately.
      
      This has been tested using a new xfstest which calls this ioctl via a
      new subcommand for the 'fsverity' program from fsverity-utils.
      
      Link: https://lore.kernel.org/r/20210115181819.34732-6-ebiggers@kernel.org
      
      
      Reviewed-by: default avatarVictor Hsieh <victorhsieh@google.com>
      Reviewed-by: default avatarJaegeuk Kim <jaegeuk@kernel.org>
      Reviewed-by: default avatarChao Yu <yuchao0@huawei.com>
      Signed-off-by: default avatarEric Biggers <ebiggers@google.com>
      947191ac
    • Eric Biggers's avatar
      fs-verity: support reading Merkle tree with ioctl · 622699cf
      Eric Biggers authored
      Add support for FS_VERITY_METADATA_TYPE_MERKLE_TREE to
      FS_IOC_READ_VERITY_METADATA.  This allows a userspace server program to
      retrieve the Merkle tree of a verity file for serving to a client which
      implements fs-verity compatible verification.  See the patch which
      introduced FS_IOC_READ_VERITY_METADATA for more details.
      
      This has been tested using a new xfstest which calls this ioctl via a
      new subcommand for the 'fsverity' program from fsverity-utils.
      
      Link: https://lore.kernel.org/r/20210115181819.34732-5-ebiggers@kernel.org
      
      
      Reviewed-by: default avatarVictor Hsieh <victorhsieh@google.com>
      Reviewed-by: default avatarJaegeuk Kim <jaegeuk@kernel.org>
      Reviewed-by: default avatarChao Yu <yuchao0@huawei.com>
      Signed-off-by: default avatarEric Biggers <ebiggers@google.com>
      622699cf
    • Eric Biggers's avatar
      fs-verity: add FS_IOC_READ_VERITY_METADATA ioctl · e17fe657
      Eric Biggers authored
      Add an ioctl FS_IOC_READ_VERITY_METADATA which will allow reading verity
      metadata from a file that has fs-verity enabled, including:
      
      - The Merkle tree
      - The fsverity_descriptor (not including the signature if present)
      - The built-in signature, if present
      
      This ioctl has similar semantics to pread().  It is passed the type of
      metadata to read (one of the above three), and a buffer, offset, and
      size.  It returns the number of bytes read or an error.
      
      Separate patches will add support for each of the above metadata types.
      This patch just adds the ioctl itself.
      
      This ioctl doesn't make any assumption about where the metadata is
      stored on-disk.  It does assume the metadata is in a stable format, but
      that's basically already the case:
      
      - The Merkle tree and fsverity_descriptor are defined by how fs-verity
        file digests are computed; see the "File digest computation" section
        of Documentation/filesystems/fsverity.rst.  Technically, the way in
        which the levels of the tree are ordered relative to each other wasn't
        previously specified, but it's logical to put the root level first.
      
      - The built-in signature is the value passed to FS_IOC_ENABLE_VERITY.
      
      This ioctl is useful because it allows writing a server program that
      takes a verity file and serves it to a client program, such that the
      client can do its own fs-verity compatible verification of the file.
      This only makes sense if the client doesn't trust the server and if the
      server needs to provide the storage for the client.
      
      More concretely, there is interest in using this ability in Android to
      export APK files (which are protected by fs-verity) to "protected VMs".
      This would use Protected KVM (https://lwn.net/Articles/836693), which
      provides an isolated execution environment without having to trust the
      traditional "host".  A "guest" VM can boot from a signed image and
      perform specific tasks in a minimum trusted environment using files that
      have fs-verity enabled on the host, without trusting the host or
      requiring that the guest has its own trusted storage.
      
      Technically, it would be possible to duplicate the metadata and store it
      in separate files for serving.  However, that would be less efficient
      and would require extra care in userspace to maintain file consistency.
      
      In addition to the above, the ability to read the built-in signatures is
      useful because it allows a system that is using the in-kernel signature
      verification to migrate to userspace signature verification.
      
      Link: https://lore.kernel.org/r/20210115181819.34732-4-ebiggers@kernel.org
      
      
      Reviewed-by: default avatarVictor Hsieh <victorhsieh@google.com>
      Acked-by: default avatarJaegeuk Kim <jaegeuk@kernel.org>
      Reviewed-by: default avatarChao Yu <yuchao0@huawei.com>
      Signed-off-by: default avatarEric Biggers <ebiggers@google.com>
      e17fe657
    • Eric Biggers's avatar
      fs-verity: don't pass whole descriptor to fsverity_verify_signature() · fab634c4
      Eric Biggers authored
      Now that fsverity_get_descriptor() validates the sig_size field,
      fsverity_verify_signature() doesn't need to do it.
      
      Just change the prototype of fsverity_verify_signature() to take the
      signature directly rather than take a fsverity_descriptor.
      
      Link: https://lore.kernel.org/r/20210115181819.34732-3-ebiggers@kernel.org
      
      
      Reviewed-by: default avatarVictor Hsieh <victorhsieh@google.com>
      Reviewed-by: default avatarJaegeuk Kim <jaegeuk@kernel.org>
      Reviewed-by: default avatarAmy Parker <enbyamy@gmail.com>
      Reviewed-by: default avatarChao Yu <yuchao0@huawei.com>
      Signed-off-by: default avatarEric Biggers <ebiggers@google.com>
      fab634c4
    • Eric Biggers's avatar
      fs-verity: factor out fsverity_get_descriptor() · c2c82611
      Eric Biggers authored
      The FS_IOC_READ_VERITY_METADATA ioctl will need to return the fs-verity
      descriptor (and signature) to userspace.
      
      There are a few ways we could implement this:
      
      - Save a copy of the descriptor (and signature) in the fsverity_info
        struct that hangs off of the in-memory inode.  However, this would
        waste memory since most of the time it wouldn't be needed.
      
      - Regenerate the descriptor from the merkle_tree_params in the
        fsverity_info.  However, this wouldn't work for the signature, nor for
        the salt which the merkle_tree_params only contains indirectly as part
        of the 'hashstate'.  It would also be error-prone.
      
      - Just get them from the filesystem again.  The disadvantage is that in
        general we can't trust that they haven't been maliciously changed
        since the file has opened.  However, the use cases for
        FS_IOC_READ_VERITY_METADATA don't require that it verifies the chain
        of trust.  So this is okay as long as we do some basic validation.
      
      In preparation for implementing the third option, factor out a helper
      function fsverity_get_descriptor() which gets the descriptor (and
      appended signature) from the filesystem and does some basic validation.
      
      As part of this, start checking the sig_size field for overflow.
      Currently fsverity_verify_signature() does this.  But the new ioctl will
      need this too, so do it earlier.
      
      Link: https://lore.kernel.org/r/20210115181819.34732-2-ebiggers@kernel.org
      
      
      Reviewed-by: default avatarVictor Hsieh <victorhsieh@google.com>
      Reviewed-by: default avatarJaegeuk Kim <jaegeuk@kernel.org>
      Reviewed-by: default avatarChao Yu <yuchao0@huawei.com>
      Signed-off-by: default avatarEric Biggers <ebiggers@google.com>
      c2c82611
  15. Jan 24, 2021
  16. Nov 24, 2020
  17. Nov 20, 2020
    • Eric Biggers's avatar
      crypto: sha - split sha.h into sha1.h and sha2.h · a24d22b2
      Eric Biggers authored
      
      Currently <crypto/sha.h> contains declarations for both SHA-1 and SHA-2,
      and <crypto/sha3.h> contains declarations for SHA-3.
      
      This organization is inconsistent, but more importantly SHA-1 is no
      longer considered to be cryptographically secure.  So to the extent
      possible, SHA-1 shouldn't be grouped together with any of the other SHA
      versions, and usage of it should be phased out.
      
      Therefore, split <crypto/sha.h> into two headers <crypto/sha1.h> and
      <crypto/sha2.h>, and make everyone explicitly specify whether they want
      the declarations for SHA-1, SHA-2, or both.
      
      This avoids making the SHA-1 declarations visible to files that don't
      want anything to do with SHA-1.  It also prepares for potentially moving
      sha1.h into a new insecure/ or dangerous/ directory.
      
      Signed-off-by: default avatarEric Biggers <ebiggers@google.com>
      Acked-by: default avatarArd Biesheuvel <ardb@kernel.org>
      Acked-by: default avatarJason A. Donenfeld <Jason@zx2c4.com>
      Signed-off-by: default avatarHerbert Xu <herbert@gondor.apana.org.au>
      a24d22b2
  18. Nov 16, 2020
  19. Jul 21, 2020
    • Eric Biggers's avatar
      fs-verity: use smp_load_acquire() for ->i_verity_info · f3db0bed
      Eric Biggers authored
      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.
      
      fsverity_info::tree_params.hash_alg->tfm is a crypto_ahash object that's
      internal to and is allocated by the crypto subsystem.  So by using
      READ_ONCE() for ->i_verity_info, we're relying on internal
      implementation details of the crypto subsystem.
      
      Remove this fragile assumption by using smp_load_acquire() instead.
      
      Also fix the cmpxchg logic to correctly execute an ACQUIRE barrier when
      losing the cmpxchg race, since cmpxchg doesn't guarantee a memory
      barrier on failure.
      
      (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: fd2d1acf ("fs-verity: add the hook for file ->open()")
      Link: https://lore.kernel.org/r/20200721225920.114347-6-ebiggers@kernel.org
      
      
      Signed-off-by: default avatarEric Biggers <ebiggers@google.com>
      f3db0bed
  20. May 12, 2020
  21. Jan 14, 2020
    • Eric Biggers's avatar
      fs-verity: use u64_to_user_ptr() · da3a3da4
      Eric Biggers authored
      <linux/kernel.h> already provides a macro u64_to_user_ptr().
      Use it instead of open-coding the two casts.
      
      No change in behavior.
      
      Link: https://lore.kernel.org/r/20191231175408.20524-1-ebiggers@kernel.org
      
      
      Signed-off-by: default avatarEric Biggers <ebiggers@google.com>
      da3a3da4
    • Eric Biggers's avatar
      fs-verity: use mempool for hash requests · 439bea10
      Eric Biggers authored
      When initializing an fs-verity hash algorithm, also initialize a mempool
      that contains a single preallocated hash request object.  Then replace
      the direct calls to ahash_request_alloc() and ahash_request_free() with
      allocating and freeing from this mempool.
      
      This eliminates the possibility of the allocation failing, which is
      desirable for the I/O path.
      
      This doesn't cause deadlocks because there's no case where multiple hash
      requests are needed at a time to make forward progress.
      
      Link: https://lore.kernel.org/r/20191231175545.20709-1-ebiggers@kernel.org
      
      
      Reviewed-by: default avatarTheodore Ts'o <tytso@mit.edu>
      Signed-off-by: default avatarEric Biggers <ebiggers@google.com>
      439bea10
    • Eric Biggers's avatar
      fs-verity: implement readahead of Merkle tree pages · fd39073d
      Eric Biggers authored
      When fs-verity verifies data pages, currently it reads each Merkle tree
      page synchronously using read_mapping_page().
      
      Therefore, when the Merkle tree pages aren't already cached, fs-verity
      causes an extra 4 KiB I/O request for every 512 KiB of data (assuming
      that the Merkle tree uses SHA-256 and 4 KiB blocks).  This results in
      more I/O requests and performance loss than is strictly necessary.
      
      Therefore, implement readahead of the Merkle tree pages.
      
      For simplicity, we take advantage of the fact that the kernel already
      does readahead of the file's *data*, just like it does for any other
      file.  Due to this, we don't really need a separate readahead state
      (struct file_ra_state) just for the Merkle tree, but rather we just need
      to piggy-back on the existing data readahead requests.
      
      We also only really need to bother with the first level of the Merkle
      tree, since the usual fan-out factor is 128, so normally over 99% of
      Merkle tree I/O requests are for the first level.
      
      Therefore, make fsverity_verify_bio() enable readahead of the first
      Merkle tree level, for up to 1/4 the number of pages in the bio, when it
      sees that the REQ_RAHEAD flag is set on the bio.  The readahead size is
      then passed down to ->read_merkle_tree_page() for the filesystem to
      (optionally) implement if it sees that the requested page is uncached.
      
      While we're at it, also make build_merkle_tree_level() set the Merkle
      tree readahead size, since it's easy to do there.
      
      However, for now don't set the readahead size in fsverity_verify_page(),
      since currently it's only used to verify holes on ext4 and f2fs, and it
      would need parameters added to know how much to read ahead.
      
      This patch significantly improves fs-verity sequential read performance.
      Some quick benchmarks with 'cat'-ing a 250MB file after dropping caches:
      
          On an ARM64 phone (using sha256-ce):
              Before: 217 MB/s
              After: 263 MB/s
              (compare to sha256sum of non-verity file: 357 MB/s)
      
          In an x86_64 VM (using sha256-avx2):
              Before: 173 MB/s
              After: 215 MB/s
              (compare to sha256sum of non-verity file: 223 MB/s)
      
      Link: https://lore.kernel.org/r/20200106205533.137005-1-ebiggers@kernel.org
      
      
      Reviewed-by: default avatarTheodore Ts'o <tytso@mit.edu>
      Signed-off-by: default avatarEric Biggers <ebiggers@google.com>
      fd39073d
    • Eric Biggers's avatar
      fs-verity: implement readahead for FS_IOC_ENABLE_VERITY · c22415d3
      Eric Biggers authored
      When it builds the first level of the Merkle tree, FS_IOC_ENABLE_VERITY
      sequentially reads each page of the file using read_mapping_page().
      This works fine if the file's data is already in pagecache, which should
      normally be the case, since this ioctl is normally used immediately
      after writing out the file.
      
      But in any other case this implementation performs very poorly, since
      only one page is read at a time.
      
      Fix this by implementing readahead using the functions from
      mm/readahead.c.
      
      This improves performance in the uncached case by about 20x, as seen in
      the following benchmarks done on a 250MB file (on x86_64 with SHA-NI):
      
          FS_IOC_ENABLE_VERITY uncached (before) 3.299s
          FS_IOC_ENABLE_VERITY uncached (after)  0.160s
          FS_IOC_ENABLE_VERITY cached            0.147s
          sha256sum uncached                     0.191s
          sha256sum cached                       0.145s
      
      Note: we could instead switch to kernel_read().  But that would mean
      we'd no longer be hashing the data directly from the pagecache, which is
      a nice optimization of its own.  And using kernel_read() would require
      allocating another temporary buffer, hashing the data and tree pages
      separately, and explicitly zero-padding the last page -- so it wouldn't
      really be any simpler than direct pagecache access, at least for now.
      
      Link: https://lore.kernel.org/r/20200106205410.136707-1-ebiggers@kernel.org
      
      
      Reviewed-by: default avatarTheodore Ts'o <tytso@mit.edu>
      Signed-off-by: default avatarEric Biggers <ebiggers@google.com>
      c22415d3
  22. Dec 09, 2019
  23. Aug 13, 2019
  24. Jul 28, 2019
Loading