1 .. SPDX-License-Identifier: GPL-2.0 1 .. SPDX-License-Identifier: GPL-2.0 2 2 3 .. _fsverity: 3 .. _fsverity: 4 4 5 ============================================== 5 ======================================================= 6 fs-verity: read-only file-based authenticity p 6 fs-verity: read-only file-based authenticity protection 7 ============================================== 7 ======================================================= 8 8 9 Introduction 9 Introduction 10 ============ 10 ============ 11 11 12 fs-verity (``fs/verity/``) is a support layer 12 fs-verity (``fs/verity/``) is a support layer that filesystems can 13 hook into to support transparent integrity and 13 hook into to support transparent integrity and authenticity protection 14 of read-only files. Currently, it is supporte 14 of read-only files. Currently, it is supported by the ext4, f2fs, and 15 btrfs filesystems. Like fscrypt, not too much 15 btrfs filesystems. Like fscrypt, not too much filesystem-specific 16 code is needed to support fs-verity. 16 code is needed to support fs-verity. 17 17 18 fs-verity is similar to `dm-verity 18 fs-verity is similar to `dm-verity 19 <https://www.kernel.org/doc/Documentation/devi 19 <https://www.kernel.org/doc/Documentation/device-mapper/verity.txt>`_ 20 but works on files rather than block devices. 20 but works on files rather than block devices. On regular files on 21 filesystems supporting fs-verity, userspace ca 21 filesystems supporting fs-verity, userspace can execute an ioctl that 22 causes the filesystem to build a Merkle tree f 22 causes the filesystem to build a Merkle tree for the file and persist 23 it to a filesystem-specific location associate 23 it to a filesystem-specific location associated with the file. 24 24 25 After this, the file is made readonly, and all 25 After this, the file is made readonly, and all reads from the file are 26 automatically verified against the file's Merk 26 automatically verified against the file's Merkle tree. Reads of any 27 corrupted data, including mmap reads, will fai 27 corrupted data, including mmap reads, will fail. 28 28 29 Userspace can use another ioctl to retrieve th 29 Userspace can use another ioctl to retrieve the root hash (actually 30 the "fs-verity file digest", which is a hash t 30 the "fs-verity file digest", which is a hash that includes the Merkle 31 tree root hash) that fs-verity is enforcing fo 31 tree root hash) that fs-verity is enforcing for the file. This ioctl 32 executes in constant time, regardless of the f 32 executes in constant time, regardless of the file size. 33 33 34 fs-verity is essentially a way to hash a file 34 fs-verity is essentially a way to hash a file in constant time, 35 subject to the caveat that reads which would v 35 subject to the caveat that reads which would violate the hash will 36 fail at runtime. 36 fail at runtime. 37 37 38 Use cases 38 Use cases 39 ========= 39 ========= 40 40 41 By itself, fs-verity only provides integrity p !! 41 By itself, the base fs-verity feature only provides integrity 42 detection of accidental (non-malicious) corrup !! 42 protection, i.e. detection of accidental (non-malicious) corruption. 43 43 44 However, because fs-verity makes retrieving th 44 However, because fs-verity makes retrieving the file hash extremely 45 efficient, it's primarily meant to be used as 45 efficient, it's primarily meant to be used as a tool to support 46 authentication (detection of malicious modific 46 authentication (detection of malicious modifications) or auditing 47 (logging file hashes before use). 47 (logging file hashes before use). 48 48 >> 49 Trusted userspace code (e.g. operating system code running on a >> 50 read-only partition that is itself authenticated by dm-verity) can >> 51 authenticate the contents of an fs-verity file by using the >> 52 `FS_IOC_MEASURE_VERITY`_ ioctl to retrieve its hash, then verifying a >> 53 digital signature of it. >> 54 49 A standard file hash could be used instead of 55 A standard file hash could be used instead of fs-verity. However, 50 this is inefficient if the file is large and o 56 this is inefficient if the file is large and only a small portion may 51 be accessed. This is often the case for Andro 57 be accessed. This is often the case for Android application package 52 (APK) files, for example. These typically con 58 (APK) files, for example. These typically contain many translations, 53 classes, and other resources that are infreque 59 classes, and other resources that are infrequently or even never 54 accessed on a particular device. It would be 60 accessed on a particular device. It would be slow and wasteful to 55 read and hash the entire file before starting 61 read and hash the entire file before starting the application. 56 62 57 Unlike an ahead-of-time hash, fs-verity also r 63 Unlike an ahead-of-time hash, fs-verity also re-verifies data each 58 time it's paged in. This ensures that malicio 64 time it's paged in. This ensures that malicious disk firmware can't 59 undetectably change the contents of the file a 65 undetectably change the contents of the file at runtime. 60 66 61 fs-verity does not replace or obsolete dm-veri 67 fs-verity does not replace or obsolete dm-verity. dm-verity should 62 still be used on read-only filesystems. fs-ve 68 still be used on read-only filesystems. fs-verity is for files that 63 must live on a read-write filesystem because t 69 must live on a read-write filesystem because they are independently 64 updated and potentially user-installed, so dm- 70 updated and potentially user-installed, so dm-verity cannot be used. 65 71 66 fs-verity does not mandate a particular scheme !! 72 The base fs-verity feature is a hashing mechanism only; actually 67 file hashes. (Similarly, dm-verity does not m !! 73 authenticating the files may be done by: 68 scheme for authenticating its block device roo !! 74 69 authenticating fs-verity file hashes include: !! 75 * Userspace-only 70 !! 76 71 - Trusted userspace code. Often, the userspac !! 77 * Builtin signature verification + userspace policy 72 files can be trusted to authenticate them. !! 78 73 application that wants to authenticate data !! 79 fs-verity optionally supports a simple signature verification 74 or an application loader that is part of the !! 80 mechanism where users can configure the kernel to require that 75 is already authenticated in a different way, !! 81 all fs-verity files be signed by a key loaded into a keyring; 76 from a read-only partition that uses dm-veri !! 82 see `Built-in signature verification`_. 77 authenticate applications before loading the !! 83 78 trusted userspace code can authenticate a fi !! 84 * Integrity Measurement Architecture (IMA) 79 retrieving its fs-verity digest using `FS_IO !! 85 80 verifying a signature of it using any usersp !! 86 IMA supports including fs-verity file digests and signatures in the 81 library that supports digital signatures. !! 87 IMA measurement list and verifying fs-verity based file signatures 82 !! 88 stored as security.ima xattrs, based on policy. 83 - Integrity Measurement Architecture (IMA). I << 84 file digests as an alternative to its tradit << 85 "IMA appraisal" enforces that files contain << 86 signature in their "security.ima" extended a << 87 by the IMA policy. For more information, se << 88 89 89 - Trusted userspace code in combination with ` << 90 verification`_. This approach should be use << 91 90 92 User API 91 User API 93 ======== 92 ======== 94 93 95 FS_IOC_ENABLE_VERITY 94 FS_IOC_ENABLE_VERITY 96 -------------------- 95 -------------------- 97 96 98 The FS_IOC_ENABLE_VERITY ioctl enables fs-veri 97 The FS_IOC_ENABLE_VERITY ioctl enables fs-verity on a file. It takes 99 in a pointer to a struct fsverity_enable_arg, 98 in a pointer to a struct fsverity_enable_arg, defined as 100 follows:: 99 follows:: 101 100 102 struct fsverity_enable_arg { 101 struct fsverity_enable_arg { 103 __u32 version; 102 __u32 version; 104 __u32 hash_algorithm; 103 __u32 hash_algorithm; 105 __u32 block_size; 104 __u32 block_size; 106 __u32 salt_size; 105 __u32 salt_size; 107 __u64 salt_ptr; 106 __u64 salt_ptr; 108 __u32 sig_size; 107 __u32 sig_size; 109 __u32 __reserved1; 108 __u32 __reserved1; 110 __u64 sig_ptr; 109 __u64 sig_ptr; 111 __u64 __reserved2[11]; 110 __u64 __reserved2[11]; 112 }; 111 }; 113 112 114 This structure contains the parameters of the 113 This structure contains the parameters of the Merkle tree to build for 115 the file. It must be initialized as follows: !! 114 the file, and optionally contains a signature. It must be initialized >> 115 as follows: 116 116 117 - ``version`` must be 1. 117 - ``version`` must be 1. 118 - ``hash_algorithm`` must be the identifier fo 118 - ``hash_algorithm`` must be the identifier for the hash algorithm to 119 use for the Merkle tree, such as FS_VERITY_H 119 use for the Merkle tree, such as FS_VERITY_HASH_ALG_SHA256. See 120 ``include/uapi/linux/fsverity.h`` for the li 120 ``include/uapi/linux/fsverity.h`` for the list of possible values. 121 - ``block_size`` is the Merkle tree block size !! 121 - ``block_size`` must be the Merkle tree block size. Currently, this 122 v6.3 and later, this can be any power of 2 b !! 122 must be equal to the system page size, which is usually 4096 bytes. 123 1024 and the minimum of the system page size !! 123 Other sizes may be supported in the future. This value is not 124 block size. In earlier versions, the page s !! 124 necessarily the same as the filesystem block size. 125 value. << 126 - ``salt_size`` is the size of the salt in byt 125 - ``salt_size`` is the size of the salt in bytes, or 0 if no salt is 127 provided. The salt is a value that is prepe 126 provided. The salt is a value that is prepended to every hashed 128 block; it can be used to personalize the has 127 block; it can be used to personalize the hashing for a particular 129 file or device. Currently the maximum salt 128 file or device. Currently the maximum salt size is 32 bytes. 130 - ``salt_ptr`` is the pointer to the salt, or 129 - ``salt_ptr`` is the pointer to the salt, or NULL if no salt is 131 provided. 130 provided. 132 - ``sig_size`` is the size of the builtin sign !! 131 - ``sig_size`` is the size of the signature in bytes, or 0 if no 133 builtin signature is provided. Currently th !! 132 signature is provided. Currently the signature is (somewhat 134 (somewhat arbitrarily) limited to 16128 byte !! 133 arbitrarily) limited to 16128 bytes. See `Built-in signature 135 - ``sig_ptr`` is the pointer to the builtin s !! 134 verification`_ for more information. 136 builtin signature is provided. A builtin si !! 135 - ``sig_ptr`` is the pointer to the signature, or NULL if no 137 if the `Built-in signature verification`_ fe !! 136 signature is provided. 138 is not needed for IMA appraisal, and it is n << 139 signature is being handled entirely in users << 140 - All reserved fields must be zeroed. 137 - All reserved fields must be zeroed. 141 138 142 FS_IOC_ENABLE_VERITY causes the filesystem to 139 FS_IOC_ENABLE_VERITY causes the filesystem to build a Merkle tree for 143 the file and persist it to a filesystem-specif 140 the file and persist it to a filesystem-specific location associated 144 with the file, then mark the file as a verity 141 with the file, then mark the file as a verity file. This ioctl may 145 take a long time to execute on large files, an 142 take a long time to execute on large files, and it is interruptible by 146 fatal signals. 143 fatal signals. 147 144 148 FS_IOC_ENABLE_VERITY checks for write access t 145 FS_IOC_ENABLE_VERITY checks for write access to the inode. However, 149 it must be executed on an O_RDONLY file descri 146 it must be executed on an O_RDONLY file descriptor and no processes 150 can have the file open for writing. Attempts 147 can have the file open for writing. Attempts to open the file for 151 writing while this ioctl is executing will fai 148 writing while this ioctl is executing will fail with ETXTBSY. (This 152 is necessary to guarantee that no writable fil 149 is necessary to guarantee that no writable file descriptors will exist 153 after verity is enabled, and to guarantee that 150 after verity is enabled, and to guarantee that the file's contents are 154 stable while the Merkle tree is being built ov 151 stable while the Merkle tree is being built over it.) 155 152 156 On success, FS_IOC_ENABLE_VERITY returns 0, an 153 On success, FS_IOC_ENABLE_VERITY returns 0, and the file becomes a 157 verity file. On failure (including the case o 154 verity file. On failure (including the case of interruption by a 158 fatal signal), no changes are made to the file 155 fatal signal), no changes are made to the file. 159 156 160 FS_IOC_ENABLE_VERITY can fail with the followi 157 FS_IOC_ENABLE_VERITY can fail with the following errors: 161 158 162 - ``EACCES``: the process does not have write 159 - ``EACCES``: the process does not have write access to the file 163 - ``EBADMSG``: the builtin signature is malfor !! 160 - ``EBADMSG``: the signature is malformed 164 - ``EBUSY``: this ioctl is already running on 161 - ``EBUSY``: this ioctl is already running on the file 165 - ``EEXIST``: the file already has verity enab 162 - ``EEXIST``: the file already has verity enabled 166 - ``EFAULT``: the caller provided inaccessible 163 - ``EFAULT``: the caller provided inaccessible memory 167 - ``EFBIG``: the file is too large to enable v << 168 - ``EINTR``: the operation was interrupted by 164 - ``EINTR``: the operation was interrupted by a fatal signal 169 - ``EINVAL``: unsupported version, hash algori 165 - ``EINVAL``: unsupported version, hash algorithm, or block size; or 170 reserved bits are set; or the file descripto 166 reserved bits are set; or the file descriptor refers to neither a 171 regular file nor a directory. 167 regular file nor a directory. 172 - ``EISDIR``: the file descriptor refers to a 168 - ``EISDIR``: the file descriptor refers to a directory 173 - ``EKEYREJECTED``: the builtin signature does !! 169 - ``EKEYREJECTED``: the signature doesn't match the file 174 - ``EMSGSIZE``: the salt or builtin signature !! 170 - ``EMSGSIZE``: the salt or signature is too long 175 - ``ENOKEY``: the ".fs-verity" keyring doesn't !! 171 - ``ENOKEY``: the fs-verity keyring doesn't contain the certificate 176 needed to verify the builtin signature !! 172 needed to verify the signature 177 - ``ENOPKG``: fs-verity recognizes the hash al 173 - ``ENOPKG``: fs-verity recognizes the hash algorithm, but it's not 178 available in the kernel's crypto API as curr 174 available in the kernel's crypto API as currently configured (e.g. 179 for SHA-512, missing CONFIG_CRYPTO_SHA512). 175 for SHA-512, missing CONFIG_CRYPTO_SHA512). 180 - ``ENOTTY``: this type of filesystem does not 176 - ``ENOTTY``: this type of filesystem does not implement fs-verity 181 - ``EOPNOTSUPP``: the kernel was not configure 177 - ``EOPNOTSUPP``: the kernel was not configured with fs-verity 182 support; or the filesystem superblock has no 178 support; or the filesystem superblock has not had the 'verity' 183 feature enabled on it; or the filesystem doe 179 feature enabled on it; or the filesystem does not support fs-verity 184 on this file. (See `Filesystem support`_.) 180 on this file. (See `Filesystem support`_.) 185 - ``EPERM``: the file is append-only; or, a bu !! 181 - ``EPERM``: the file is append-only; or, a signature is required and 186 required and one was not provided. !! 182 one was not provided. 187 - ``EROFS``: the filesystem is read-only 183 - ``EROFS``: the filesystem is read-only 188 - ``ETXTBSY``: someone has the file open for w 184 - ``ETXTBSY``: someone has the file open for writing. This can be the 189 caller's file descriptor, another open file 185 caller's file descriptor, another open file descriptor, or the file 190 reference held by a writable memory map. 186 reference held by a writable memory map. 191 187 192 FS_IOC_MEASURE_VERITY 188 FS_IOC_MEASURE_VERITY 193 --------------------- 189 --------------------- 194 190 195 The FS_IOC_MEASURE_VERITY ioctl retrieves the 191 The FS_IOC_MEASURE_VERITY ioctl retrieves the digest of a verity file. 196 The fs-verity file digest is a cryptographic d 192 The fs-verity file digest is a cryptographic digest that identifies 197 the file contents that are being enforced on r 193 the file contents that are being enforced on reads; it is computed via 198 a Merkle tree and is different from a traditio 194 a Merkle tree and is different from a traditional full-file digest. 199 195 200 This ioctl takes in a pointer to a variable-le 196 This ioctl takes in a pointer to a variable-length structure:: 201 197 202 struct fsverity_digest { 198 struct fsverity_digest { 203 __u16 digest_algorithm; 199 __u16 digest_algorithm; 204 __u16 digest_size; /* input/output 200 __u16 digest_size; /* input/output */ 205 __u8 digest[]; 201 __u8 digest[]; 206 }; 202 }; 207 203 208 ``digest_size`` is an input/output field. On 204 ``digest_size`` is an input/output field. On input, it must be 209 initialized to the number of bytes allocated f 205 initialized to the number of bytes allocated for the variable-length 210 ``digest`` field. 206 ``digest`` field. 211 207 212 On success, 0 is returned and the kernel fills 208 On success, 0 is returned and the kernel fills in the structure as 213 follows: 209 follows: 214 210 215 - ``digest_algorithm`` will be the hash algori 211 - ``digest_algorithm`` will be the hash algorithm used for the file 216 digest. It will match ``fsverity_enable_arg 212 digest. It will match ``fsverity_enable_arg::hash_algorithm``. 217 - ``digest_size`` will be the size of the dige 213 - ``digest_size`` will be the size of the digest in bytes, e.g. 32 218 for SHA-256. (This can be redundant with `` 214 for SHA-256. (This can be redundant with ``digest_algorithm``.) 219 - ``digest`` will be the actual bytes of the d 215 - ``digest`` will be the actual bytes of the digest. 220 216 221 FS_IOC_MEASURE_VERITY is guaranteed to execute 217 FS_IOC_MEASURE_VERITY is guaranteed to execute in constant time, 222 regardless of the size of the file. 218 regardless of the size of the file. 223 219 224 FS_IOC_MEASURE_VERITY can fail with the follow 220 FS_IOC_MEASURE_VERITY can fail with the following errors: 225 221 226 - ``EFAULT``: the caller provided inaccessible 222 - ``EFAULT``: the caller provided inaccessible memory 227 - ``ENODATA``: the file is not a verity file 223 - ``ENODATA``: the file is not a verity file 228 - ``ENOTTY``: this type of filesystem does not 224 - ``ENOTTY``: this type of filesystem does not implement fs-verity 229 - ``EOPNOTSUPP``: the kernel was not configure 225 - ``EOPNOTSUPP``: the kernel was not configured with fs-verity 230 support, or the filesystem superblock has no 226 support, or the filesystem superblock has not had the 'verity' 231 feature enabled on it. (See `Filesystem sup 227 feature enabled on it. (See `Filesystem support`_.) 232 - ``EOVERFLOW``: the digest is longer than the 228 - ``EOVERFLOW``: the digest is longer than the specified 233 ``digest_size`` bytes. Try providing a larg 229 ``digest_size`` bytes. Try providing a larger buffer. 234 230 235 FS_IOC_READ_VERITY_METADATA 231 FS_IOC_READ_VERITY_METADATA 236 --------------------------- 232 --------------------------- 237 233 238 The FS_IOC_READ_VERITY_METADATA ioctl reads ve 234 The FS_IOC_READ_VERITY_METADATA ioctl reads verity metadata from a 239 verity file. This ioctl is available since Li 235 verity file. This ioctl is available since Linux v5.12. 240 236 241 This ioctl allows writing a server program tha 237 This ioctl allows writing a server program that takes a verity file 242 and serves it to a client program, such that t 238 and serves it to a client program, such that the client can do its own 243 fs-verity compatible verification of the file. 239 fs-verity compatible verification of the file. This only makes sense 244 if the client doesn't trust the server and if 240 if the client doesn't trust the server and if the server needs to 245 provide the storage for the client. 241 provide the storage for the client. 246 242 247 This is a fairly specialized use case, and mos 243 This is a fairly specialized use case, and most fs-verity users won't 248 need this ioctl. 244 need this ioctl. 249 245 250 This ioctl takes in a pointer to the following 246 This ioctl takes in a pointer to the following structure:: 251 247 252 #define FS_VERITY_METADATA_TYPE_MERKLE_TREE 248 #define FS_VERITY_METADATA_TYPE_MERKLE_TREE 1 253 #define FS_VERITY_METADATA_TYPE_DESCRIPTOR 249 #define FS_VERITY_METADATA_TYPE_DESCRIPTOR 2 254 #define FS_VERITY_METADATA_TYPE_SIGNATURE 250 #define FS_VERITY_METADATA_TYPE_SIGNATURE 3 255 251 256 struct fsverity_read_metadata_arg { 252 struct fsverity_read_metadata_arg { 257 __u64 metadata_type; 253 __u64 metadata_type; 258 __u64 offset; 254 __u64 offset; 259 __u64 length; 255 __u64 length; 260 __u64 buf_ptr; 256 __u64 buf_ptr; 261 __u64 __reserved; 257 __u64 __reserved; 262 }; 258 }; 263 259 264 ``metadata_type`` specifies the type of metada 260 ``metadata_type`` specifies the type of metadata to read: 265 261 266 - ``FS_VERITY_METADATA_TYPE_MERKLE_TREE`` read 262 - ``FS_VERITY_METADATA_TYPE_MERKLE_TREE`` reads the blocks of the 267 Merkle tree. The blocks are returned in ord 263 Merkle tree. The blocks are returned in order from the root level 268 to the leaf level. Within each level, the b 264 to the leaf level. Within each level, the blocks are returned in 269 the same order that their hashes are themsel 265 the same order that their hashes are themselves hashed. 270 See `Merkle tree`_ for more information. 266 See `Merkle tree`_ for more information. 271 267 272 - ``FS_VERITY_METADATA_TYPE_DESCRIPTOR`` reads 268 - ``FS_VERITY_METADATA_TYPE_DESCRIPTOR`` reads the fs-verity 273 descriptor. See `fs-verity descriptor`_. 269 descriptor. See `fs-verity descriptor`_. 274 270 275 - ``FS_VERITY_METADATA_TYPE_SIGNATURE`` reads !! 271 - ``FS_VERITY_METADATA_TYPE_SIGNATURE`` reads the signature which was 276 which was passed to FS_IOC_ENABLE_VERITY, if !! 272 passed to FS_IOC_ENABLE_VERITY, if any. See `Built-in signature 277 signature verification`_. !! 273 verification`_. 278 274 279 The semantics are similar to those of ``pread( 275 The semantics are similar to those of ``pread()``. ``offset`` 280 specifies the offset in bytes into the metadat 276 specifies the offset in bytes into the metadata item to read from, and 281 ``length`` specifies the maximum number of byt 277 ``length`` specifies the maximum number of bytes to read from the 282 metadata item. ``buf_ptr`` is the pointer to 278 metadata item. ``buf_ptr`` is the pointer to the buffer to read into, 283 cast to a 64-bit integer. ``__reserved`` must 279 cast to a 64-bit integer. ``__reserved`` must be 0. On success, the 284 number of bytes read is returned. 0 is return 280 number of bytes read is returned. 0 is returned at the end of the 285 metadata item. The returned length may be les 281 metadata item. The returned length may be less than ``length``, for 286 example if the ioctl is interrupted. 282 example if the ioctl is interrupted. 287 283 288 The metadata returned by FS_IOC_READ_VERITY_ME 284 The metadata returned by FS_IOC_READ_VERITY_METADATA isn't guaranteed 289 to be authenticated against the file digest th 285 to be authenticated against the file digest that would be returned by 290 `FS_IOC_MEASURE_VERITY`_, as the metadata is e 286 `FS_IOC_MEASURE_VERITY`_, as the metadata is expected to be used to 291 implement fs-verity compatible verification an 287 implement fs-verity compatible verification anyway (though absent a 292 malicious disk, the metadata will indeed match 288 malicious disk, the metadata will indeed match). E.g. to implement 293 this ioctl, the filesystem is allowed to just 289 this ioctl, the filesystem is allowed to just read the Merkle tree 294 blocks from disk without actually verifying th 290 blocks from disk without actually verifying the path to the root node. 295 291 296 FS_IOC_READ_VERITY_METADATA can fail with the 292 FS_IOC_READ_VERITY_METADATA can fail with the following errors: 297 293 298 - ``EFAULT``: the caller provided inaccessible 294 - ``EFAULT``: the caller provided inaccessible memory 299 - ``EINTR``: the ioctl was interrupted before 295 - ``EINTR``: the ioctl was interrupted before any data was read 300 - ``EINVAL``: reserved fields were set, or ``o 296 - ``EINVAL``: reserved fields were set, or ``offset + length`` 301 overflowed 297 overflowed 302 - ``ENODATA``: the file is not a verity file, 298 - ``ENODATA``: the file is not a verity file, or 303 FS_VERITY_METADATA_TYPE_SIGNATURE was reques 299 FS_VERITY_METADATA_TYPE_SIGNATURE was requested but the file doesn't 304 have a builtin signature !! 300 have a built-in signature 305 - ``ENOTTY``: this type of filesystem does not 301 - ``ENOTTY``: this type of filesystem does not implement fs-verity, or 306 this ioctl is not yet implemented on it 302 this ioctl is not yet implemented on it 307 - ``EOPNOTSUPP``: the kernel was not configure 303 - ``EOPNOTSUPP``: the kernel was not configured with fs-verity 308 support, or the filesystem superblock has no 304 support, or the filesystem superblock has not had the 'verity' 309 feature enabled on it. (See `Filesystem sup 305 feature enabled on it. (See `Filesystem support`_.) 310 306 311 FS_IOC_GETFLAGS 307 FS_IOC_GETFLAGS 312 --------------- 308 --------------- 313 309 314 The existing ioctl FS_IOC_GETFLAGS (which isn' 310 The existing ioctl FS_IOC_GETFLAGS (which isn't specific to fs-verity) 315 can also be used to check whether a file has f 311 can also be used to check whether a file has fs-verity enabled or not. 316 To do so, check for FS_VERITY_FL (0x00100000) 312 To do so, check for FS_VERITY_FL (0x00100000) in the returned flags. 317 313 318 The verity flag is not settable via FS_IOC_SET 314 The verity flag is not settable via FS_IOC_SETFLAGS. You must use 319 FS_IOC_ENABLE_VERITY instead, since parameters 315 FS_IOC_ENABLE_VERITY instead, since parameters must be provided. 320 316 321 statx 317 statx 322 ----- 318 ----- 323 319 324 Since Linux v5.5, the statx() system call sets 320 Since Linux v5.5, the statx() system call sets STATX_ATTR_VERITY if 325 the file has fs-verity enabled. This can perf 321 the file has fs-verity enabled. This can perform better than 326 FS_IOC_GETFLAGS and FS_IOC_MEASURE_VERITY beca 322 FS_IOC_GETFLAGS and FS_IOC_MEASURE_VERITY because it doesn't require 327 opening the file, and opening verity files can 323 opening the file, and opening verity files can be expensive. 328 324 329 .. _accessing_verity_files: << 330 << 331 Accessing verity files 325 Accessing verity files 332 ====================== 326 ====================== 333 327 334 Applications can transparently access a verity 328 Applications can transparently access a verity file just like a 335 non-verity one, with the following exceptions: 329 non-verity one, with the following exceptions: 336 330 337 - Verity files are readonly. They cannot be o 331 - Verity files are readonly. They cannot be opened for writing or 338 truncate()d, even if the file mode bits allo 332 truncate()d, even if the file mode bits allow it. Attempts to do 339 one of these things will fail with EPERM. H 333 one of these things will fail with EPERM. However, changes to 340 metadata such as owner, mode, timestamps, an 334 metadata such as owner, mode, timestamps, and xattrs are still 341 allowed, since these are not measured by fs- 335 allowed, since these are not measured by fs-verity. Verity files 342 can also still be renamed, deleted, and link 336 can also still be renamed, deleted, and linked to. 343 337 344 - Direct I/O is not supported on verity files. 338 - Direct I/O is not supported on verity files. Attempts to use direct 345 I/O on such files will fall back to buffered 339 I/O on such files will fall back to buffered I/O. 346 340 347 - DAX (Direct Access) is not supported on veri 341 - DAX (Direct Access) is not supported on verity files, because this 348 would circumvent the data verification. 342 would circumvent the data verification. 349 343 350 - Reads of data that doesn't match the verity 344 - Reads of data that doesn't match the verity Merkle tree will fail 351 with EIO (for read()) or SIGBUS (for mmap() 345 with EIO (for read()) or SIGBUS (for mmap() reads). 352 346 353 - If the sysctl "fs.verity.require_signatures" 347 - If the sysctl "fs.verity.require_signatures" is set to 1 and the 354 file is not signed by a key in the ".fs-veri !! 348 file is not signed by a key in the fs-verity keyring, then opening 355 opening the file will fail. See `Built-in s !! 349 the file will fail. See `Built-in signature verification`_. 356 350 357 Direct access to the Merkle tree is not suppor 351 Direct access to the Merkle tree is not supported. Therefore, if a 358 verity file is copied, or is backed up and res 352 verity file is copied, or is backed up and restored, then it will lose 359 its "verity"-ness. fs-verity is primarily mea 353 its "verity"-ness. fs-verity is primarily meant for files like 360 executables that are managed by a package mana 354 executables that are managed by a package manager. 361 355 362 File digest computation 356 File digest computation 363 ======================= 357 ======================= 364 358 365 This section describes how fs-verity hashes th 359 This section describes how fs-verity hashes the file contents using a 366 Merkle tree to produce the digest which crypto 360 Merkle tree to produce the digest which cryptographically identifies 367 the file contents. This algorithm is the same 361 the file contents. This algorithm is the same for all filesystems 368 that support fs-verity. 362 that support fs-verity. 369 363 370 Userspace only needs to be aware of this algor 364 Userspace only needs to be aware of this algorithm if it needs to 371 compute fs-verity file digests itself, e.g. in 365 compute fs-verity file digests itself, e.g. in order to sign files. 372 366 373 .. _fsverity_merkle_tree: 367 .. _fsverity_merkle_tree: 374 368 375 Merkle tree 369 Merkle tree 376 ----------- 370 ----------- 377 371 378 The file contents is divided into blocks, wher 372 The file contents is divided into blocks, where the block size is 379 configurable but is usually 4096 bytes. The e 373 configurable but is usually 4096 bytes. The end of the last block is 380 zero-padded if needed. Each block is then has 374 zero-padded if needed. Each block is then hashed, producing the first 381 level of hashes. Then, the hashes in this fir 375 level of hashes. Then, the hashes in this first level are grouped 382 into 'blocksize'-byte blocks (zero-padding the 376 into 'blocksize'-byte blocks (zero-padding the ends as needed) and 383 these blocks are hashed, producing the second 377 these blocks are hashed, producing the second level of hashes. This 384 proceeds up the tree until only a single block 378 proceeds up the tree until only a single block remains. The hash of 385 this block is the "Merkle tree root hash". 379 this block is the "Merkle tree root hash". 386 380 387 If the file fits in one block and is nonempty, 381 If the file fits in one block and is nonempty, then the "Merkle tree 388 root hash" is simply the hash of the single da 382 root hash" is simply the hash of the single data block. If the file 389 is empty, then the "Merkle tree root hash" is 383 is empty, then the "Merkle tree root hash" is all zeroes. 390 384 391 The "blocks" here are not necessarily the same 385 The "blocks" here are not necessarily the same as "filesystem blocks". 392 386 393 If a salt was specified, then it's zero-padded 387 If a salt was specified, then it's zero-padded to the closest multiple 394 of the input size of the hash algorithm's comp 388 of the input size of the hash algorithm's compression function, e.g. 395 64 bytes for SHA-256 or 128 bytes for SHA-512. 389 64 bytes for SHA-256 or 128 bytes for SHA-512. The padded salt is 396 prepended to every data or Merkle tree block t 390 prepended to every data or Merkle tree block that is hashed. 397 391 398 The purpose of the block padding is to cause e 392 The purpose of the block padding is to cause every hash to be taken 399 over the same amount of data, which simplifies 393 over the same amount of data, which simplifies the implementation and 400 keeps open more possibilities for hardware acc 394 keeps open more possibilities for hardware acceleration. The purpose 401 of the salt padding is to make the salting "fr 395 of the salt padding is to make the salting "free" when the salted hash 402 state is precomputed, then imported for each h 396 state is precomputed, then imported for each hash. 403 397 404 Example: in the recommended configuration of S 398 Example: in the recommended configuration of SHA-256 and 4K blocks, 405 128 hash values fit in each block. Thus, each 399 128 hash values fit in each block. Thus, each level of the Merkle 406 tree is approximately 128 times smaller than t 400 tree is approximately 128 times smaller than the previous, and for 407 large files the Merkle tree's size converges t 401 large files the Merkle tree's size converges to approximately 1/127 of 408 the original file size. However, for small fi 402 the original file size. However, for small files, the padding is 409 significant, making the space overhead proport 403 significant, making the space overhead proportionally more. 410 404 411 .. _fsverity_descriptor: 405 .. _fsverity_descriptor: 412 406 413 fs-verity descriptor 407 fs-verity descriptor 414 -------------------- 408 -------------------- 415 409 416 By itself, the Merkle tree root hash is ambigu 410 By itself, the Merkle tree root hash is ambiguous. For example, it 417 can't a distinguish a large file from a small 411 can't a distinguish a large file from a small second file whose data 418 is exactly the top-level hash block of the fir 412 is exactly the top-level hash block of the first file. Ambiguities 419 also arise from the convention of padding to t 413 also arise from the convention of padding to the next block boundary. 420 414 421 To solve this problem, the fs-verity file dige 415 To solve this problem, the fs-verity file digest is actually computed 422 as a hash of the following structure, which co 416 as a hash of the following structure, which contains the Merkle tree 423 root hash as well as other fields such as the 417 root hash as well as other fields such as the file size:: 424 418 425 struct fsverity_descriptor { 419 struct fsverity_descriptor { 426 __u8 version; /* must be 420 __u8 version; /* must be 1 */ 427 __u8 hash_algorithm; /* Merkle 421 __u8 hash_algorithm; /* Merkle tree hash algorithm */ 428 __u8 log_blocksize; /* log2 of 422 __u8 log_blocksize; /* log2 of size of data and tree blocks */ 429 __u8 salt_size; /* size of 423 __u8 salt_size; /* size of salt in bytes; 0 if none */ 430 __le32 __reserved_0x04; /* must be 424 __le32 __reserved_0x04; /* must be 0 */ 431 __le64 data_size; /* size of 425 __le64 data_size; /* size of file the Merkle tree is built over */ 432 __u8 root_hash[64]; /* Merkle 426 __u8 root_hash[64]; /* Merkle tree root hash */ 433 __u8 salt[32]; /* salt pr 427 __u8 salt[32]; /* salt prepended to each hashed block */ 434 __u8 __reserved[144]; /* must be 428 __u8 __reserved[144]; /* must be 0's */ 435 }; 429 }; 436 430 437 Built-in signature verification 431 Built-in signature verification 438 =============================== 432 =============================== 439 433 440 CONFIG_FS_VERITY_BUILTIN_SIGNATURES=y adds sup !! 434 With CONFIG_FS_VERITY_BUILTIN_SIGNATURES=y, fs-verity supports putting 441 verification of fs-verity builtin signatures. !! 435 a portion of an authentication policy (see `Use cases`_) in the 442 !! 436 kernel. Specifically, it adds support for: 443 **IMPORTANT**! Please take great care before !! 437 444 It is not the only way to do signatures with f !! 438 1. At fs-verity module initialization time, a keyring ".fs-verity" is 445 alternatives (such as userspace signature veri !! 439 created. The root user can add trusted X.509 certificates to this 446 appraisal) can be much better. It's also easy !! 440 keyring using the add_key() system call, then (when done) 447 of thinking this feature solves more problems !! 441 optionally use keyctl_restrict_keyring() to prevent additional 448 !! 442 certificates from being added. 449 Enabling this option adds the following: << 450 << 451 1. At boot time, the kernel creates a keyring << 452 root user can add trusted X.509 certificate << 453 the add_key() system call. << 454 443 455 2. `FS_IOC_ENABLE_VERITY`_ accepts a pointer t 444 2. `FS_IOC_ENABLE_VERITY`_ accepts a pointer to a PKCS#7 formatted 456 detached signature in DER format of the fil 445 detached signature in DER format of the file's fs-verity digest. 457 On success, the ioctl persists the signatur !! 446 On success, this signature is persisted alongside the Merkle tree. 458 tree. Then, any time the file is opened, t !! 447 Then, any time the file is opened, the kernel will verify the 459 file's actual digest against this signature 448 file's actual digest against this signature, using the certificates 460 in the ".fs-verity" keyring. 449 in the ".fs-verity" keyring. 461 450 462 3. A new sysctl "fs.verity.require_signatures" 451 3. A new sysctl "fs.verity.require_signatures" is made available. 463 When set to 1, the kernel requires that all 452 When set to 1, the kernel requires that all verity files have a 464 correctly signed digest as described in (2) 453 correctly signed digest as described in (2). 465 454 466 The data that the signature as described in (2 !! 455 fs-verity file digests must be signed in the following format, which 467 is the fs-verity file digest in the following !! 456 is similar to the structure used by `FS_IOC_MEASURE_VERITY`_:: 468 457 469 struct fsverity_formatted_digest { 458 struct fsverity_formatted_digest { 470 char magic[8]; /* 459 char magic[8]; /* must be "FSVerity" */ 471 __le16 digest_algorithm; 460 __le16 digest_algorithm; 472 __le16 digest_size; 461 __le16 digest_size; 473 __u8 digest[]; 462 __u8 digest[]; 474 }; 463 }; 475 464 476 That's it. It should be emphasized again that !! 465 fs-verity's built-in signature verification support is meant as a 477 signatures are not the only way to do signatur !! 466 relatively simple mechanism that can be used to provide some level of 478 `Use cases`_ for an overview of ways in which !! 467 authenticity protection for verity files, as an alternative to doing 479 fs-verity builtin signatures have some major l !! 468 the signature verification in userspace or using IMA-appraisal. 480 be carefully considered before using them: !! 469 However, with this mechanism, userspace programs still need to check 481 !! 470 that the verity bit is set, and there is no protection against verity 482 - Builtin signature verification does *not* ma !! 471 files being swapped around. 483 that any files actually have fs-verity enabl << 484 complete authentication policy. Currently, << 485 way to complete the authentication policy is << 486 code to explicitly check whether files have << 487 signature before they are accessed. (With << 488 fs.verity.require_signatures=1, just checkin << 489 enabled suffices.) But, in this case the tr << 490 could just store the signature alongside the << 491 itself using a cryptographic library, instea << 492 << 493 - A file's builtin signature can only be set a << 494 fs-verity is being enabled on the file. Cha << 495 builtin signature later requires re-creating << 496 << 497 - Builtin signature verification uses the same << 498 all fs-verity enabled files on the system. << 499 trusted for different files; each key is all << 500 << 501 - The sysctl fs.verity.require_signatures appl << 502 Setting it to 1 only works when all users of << 503 agree that it should be set to 1. This limi << 504 fs-verity from being used in cases where it << 505 << 506 - Builtin signature verification can only use << 507 that are supported by the kernel. For examp << 508 yet support Ed25519, even though this is oft << 509 algorithm that is recommended for new crypto << 510 << 511 - fs-verity builtin signatures are in PKCS#7 f << 512 keys are in X.509 format. These formats are << 513 including by some other kernel features (whi << 514 builtin signatures use them), and are very f << 515 Unfortunately, history has shown that code t << 516 these formats (which are from the 1990s and << 517 often has vulnerabilities as a result of the << 518 complexity is not inherent to the cryptograp << 519 << 520 fs-verity users who do not need advanced fea << 521 PKCS#7 should strongly consider using simple << 522 Ed25519 keys and signatures, and verifying s << 523 << 524 fs-verity users who choose to use X.509 and << 525 still consider that verifying those signatur << 526 flexible (for other reasons mentioned earlie << 527 eliminates the need to enable CONFIG_FS_VERI << 528 and its associated increase in kernel attack << 529 it can even be necessary, since advanced X.5 << 530 do not always work as intended with the kern << 531 kernel does not check X.509 certificate vali << 532 << 533 Note: IMA appraisal, which supports fs-verit << 534 for its signatures, so it partially avoids t << 535 here. IMA appraisal does use X.509. << 536 472 537 Filesystem support 473 Filesystem support 538 ================== 474 ================== 539 475 540 fs-verity is supported by several filesystems, 476 fs-verity is supported by several filesystems, described below. The 541 CONFIG_FS_VERITY kconfig option must be enable 477 CONFIG_FS_VERITY kconfig option must be enabled to use fs-verity on 542 any of these filesystems. 478 any of these filesystems. 543 479 544 ``include/linux/fsverity.h`` declares the inte 480 ``include/linux/fsverity.h`` declares the interface between the 545 ``fs/verity/`` support layer and filesystems. 481 ``fs/verity/`` support layer and filesystems. Briefly, filesystems 546 must provide an ``fsverity_operations`` struct 482 must provide an ``fsverity_operations`` structure that provides 547 methods to read and write the verity metadata 483 methods to read and write the verity metadata to a filesystem-specific 548 location, including the Merkle tree blocks and 484 location, including the Merkle tree blocks and 549 ``fsverity_descriptor``. Filesystems must als 485 ``fsverity_descriptor``. Filesystems must also call functions in 550 ``fs/verity/`` at certain times, such as when 486 ``fs/verity/`` at certain times, such as when a file is opened or when 551 pages have been read into the pagecache. (See 487 pages have been read into the pagecache. (See `Verifying data`_.) 552 488 553 ext4 489 ext4 554 ---- 490 ---- 555 491 556 ext4 supports fs-verity since Linux v5.4 and e 492 ext4 supports fs-verity since Linux v5.4 and e2fsprogs v1.45.2. 557 493 558 To create verity files on an ext4 filesystem, 494 To create verity files on an ext4 filesystem, the filesystem must have 559 been formatted with ``-O verity`` or had ``tun 495 been formatted with ``-O verity`` or had ``tune2fs -O verity`` run on 560 it. "verity" is an RO_COMPAT filesystem featu 496 it. "verity" is an RO_COMPAT filesystem feature, so once set, old 561 kernels will only be able to mount the filesys 497 kernels will only be able to mount the filesystem readonly, and old 562 versions of e2fsck will be unable to check the !! 498 versions of e2fsck will be unable to check the filesystem. Moreover, 563 !! 499 currently ext4 only supports mounting a filesystem with the "verity" 564 Originally, an ext4 filesystem with the "verit !! 500 feature when its block size is equal to PAGE_SIZE (often 4096 bytes). 565 mounted when its block size was equal to the s << 566 (typically 4096 bytes). In Linux v6.3, this l << 567 501 568 ext4 sets the EXT4_VERITY_FL on-disk inode fla 502 ext4 sets the EXT4_VERITY_FL on-disk inode flag on verity files. It 569 can only be set by `FS_IOC_ENABLE_VERITY`_, an 503 can only be set by `FS_IOC_ENABLE_VERITY`_, and it cannot be cleared. 570 504 571 ext4 also supports encryption, which can be us 505 ext4 also supports encryption, which can be used simultaneously with 572 fs-verity. In this case, the plaintext data i 506 fs-verity. In this case, the plaintext data is verified rather than 573 the ciphertext. This is necessary in order to 507 the ciphertext. This is necessary in order to make the fs-verity file 574 digest meaningful, since every file is encrypt 508 digest meaningful, since every file is encrypted differently. 575 509 576 ext4 stores the verity metadata (Merkle tree a 510 ext4 stores the verity metadata (Merkle tree and fsverity_descriptor) 577 past the end of the file, starting at the firs 511 past the end of the file, starting at the first 64K boundary beyond 578 i_size. This approach works because (a) verit 512 i_size. This approach works because (a) verity files are readonly, 579 and (b) pages fully beyond i_size aren't visib 513 and (b) pages fully beyond i_size aren't visible to userspace but can 580 be read/written internally by ext4 with only s 514 be read/written internally by ext4 with only some relatively small 581 changes to ext4. This approach avoids having 515 changes to ext4. This approach avoids having to depend on the 582 EA_INODE feature and on rearchitecturing ext4' 516 EA_INODE feature and on rearchitecturing ext4's xattr support to 583 support paging multi-gigabyte xattrs into memo 517 support paging multi-gigabyte xattrs into memory, and to support 584 encrypting xattrs. Note that the verity metad 518 encrypting xattrs. Note that the verity metadata *must* be encrypted 585 when the file is, since it contains hashes of 519 when the file is, since it contains hashes of the plaintext data. 586 520 587 ext4 only allows verity on extent-based files. !! 521 Currently, ext4 verity only supports the case where the Merkle tree >> 522 block size, filesystem block size, and page size are all the same. It >> 523 also only supports extent-based files. 588 524 589 f2fs 525 f2fs 590 ---- 526 ---- 591 527 592 f2fs supports fs-verity since Linux v5.4 and f 528 f2fs supports fs-verity since Linux v5.4 and f2fs-tools v1.11.0. 593 529 594 To create verity files on an f2fs filesystem, 530 To create verity files on an f2fs filesystem, the filesystem must have 595 been formatted with ``-O verity``. 531 been formatted with ``-O verity``. 596 532 597 f2fs sets the FADVISE_VERITY_BIT on-disk inode 533 f2fs sets the FADVISE_VERITY_BIT on-disk inode flag on verity files. 598 It can only be set by `FS_IOC_ENABLE_VERITY`_, 534 It can only be set by `FS_IOC_ENABLE_VERITY`_, and it cannot be 599 cleared. 535 cleared. 600 536 601 Like ext4, f2fs stores the verity metadata (Me 537 Like ext4, f2fs stores the verity metadata (Merkle tree and 602 fsverity_descriptor) past the end of the file, 538 fsverity_descriptor) past the end of the file, starting at the first 603 64K boundary beyond i_size. See explanation f 539 64K boundary beyond i_size. See explanation for ext4 above. 604 Moreover, f2fs supports at most 4096 bytes of 540 Moreover, f2fs supports at most 4096 bytes of xattr entries per inode 605 which usually wouldn't be enough for even a si !! 541 which wouldn't be enough for even a single Merkle tree block. 606 542 607 f2fs doesn't support enabling verity on files !! 543 Currently, f2fs verity only supports a Merkle tree block size of 4096. 608 atomic or volatile writes pending. !! 544 Also, f2fs doesn't support enabling verity on files that currently >> 545 have atomic or volatile writes pending. 609 546 610 btrfs 547 btrfs 611 ----- 548 ----- 612 549 613 btrfs supports fs-verity since Linux v5.15. V 550 btrfs supports fs-verity since Linux v5.15. Verity-enabled inodes are 614 marked with a RO_COMPAT inode flag, and the ve 551 marked with a RO_COMPAT inode flag, and the verity metadata is stored 615 in separate btree items. 552 in separate btree items. 616 553 617 Implementation details 554 Implementation details 618 ====================== 555 ====================== 619 556 620 Verifying data 557 Verifying data 621 -------------- 558 -------------- 622 559 623 fs-verity ensures that all reads of a verity f 560 fs-verity ensures that all reads of a verity file's data are verified, 624 regardless of which syscall is used to do the 561 regardless of which syscall is used to do the read (e.g. mmap(), 625 read(), pread()) and regardless of whether it' 562 read(), pread()) and regardless of whether it's the first read or a 626 later read (unless the later read can return c 563 later read (unless the later read can return cached data that was 627 already verified). Below, we describe how fil 564 already verified). Below, we describe how filesystems implement this. 628 565 629 Pagecache 566 Pagecache 630 ~~~~~~~~~ 567 ~~~~~~~~~ 631 568 632 For filesystems using Linux's pagecache, the ` 569 For filesystems using Linux's pagecache, the ``->read_folio()`` and 633 ``->readahead()`` methods must be modified to !! 570 ``->readahead()`` methods must be modified to verify pages before they 634 they are marked Uptodate. Merely hooking ``-> !! 571 are marked Uptodate. Merely hooking ``->read_iter()`` would be 635 insufficient, since ``->read_iter()`` is not u 572 insufficient, since ``->read_iter()`` is not used for memory maps. 636 573 637 Therefore, fs/verity/ provides the function fs !! 574 Therefore, fs/verity/ provides a function fsverity_verify_page() which 638 which verifies data that has been read into th !! 575 verifies a page that has been read into the pagecache of a verity 639 inode. The containing folio must still be loc !! 576 inode, but is still locked and not Uptodate, so it's not yet readable 640 it's not yet readable by userspace. As needed !! 577 by userspace. As needed to do the verification, 641 fsverity_verify_blocks() will call back into t !! 578 fsverity_verify_page() will call back into the filesystem to read 642 hash blocks via fsverity_operations::read_merk !! 579 Merkle tree pages via fsverity_operations::read_merkle_tree_page(). 643 580 644 fsverity_verify_blocks() returns false if veri !! 581 fsverity_verify_page() returns false if verification failed; in this 645 case, the filesystem must not set the folio Up !! 582 case, the filesystem must not set the page Uptodate. Following this, 646 as per the usual Linux pagecache behavior, att 583 as per the usual Linux pagecache behavior, attempts by userspace to 647 read() from the part of the file containing th !! 584 read() from the part of the file containing the page will fail with 648 EIO, and accesses to the folio within a memory !! 585 EIO, and accesses to the page within a memory map will raise SIGBUS. >> 586 >> 587 fsverity_verify_page() currently only supports the case where the >> 588 Merkle tree block size is equal to PAGE_SIZE (often 4096 bytes). 649 589 650 In principle, verifying a data block requires !! 590 In principle, fsverity_verify_page() verifies the entire path in the 651 path in the Merkle tree from the data block to !! 591 Merkle tree from the data page to the root hash. However, for 652 However, for efficiency the filesystem may cac !! 592 efficiency the filesystem may cache the hash pages. Therefore, 653 Therefore, fsverity_verify_blocks() only ascen !! 593 fsverity_verify_page() only ascends the tree reading hash pages until 654 blocks until an already-verified hash block is !! 594 an already-verified hash page is seen, as indicated by the PageChecked 655 the path to that block. !! 595 bit being set. It then verifies the path to that page. 656 596 657 This optimization, which is also used by dm-ve 597 This optimization, which is also used by dm-verity, results in 658 excellent sequential read performance. This i 598 excellent sequential read performance. This is because usually (e.g. 659 127 in 128 times for 4K blocks and SHA-256) th !! 599 127 in 128 times for 4K blocks and SHA-256) the hash page from the 660 bottom level of the tree will already be cache 600 bottom level of the tree will already be cached and checked from 661 reading a previous data block. However, rando !! 601 reading a previous data page. However, random reads perform worse. 662 602 663 Block device based filesystems 603 Block device based filesystems 664 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 604 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 665 605 666 Block device based filesystems (e.g. ext4 and 606 Block device based filesystems (e.g. ext4 and f2fs) in Linux also use 667 the pagecache, so the above subsection applies 607 the pagecache, so the above subsection applies too. However, they 668 also usually read many data blocks from a file !! 608 also usually read many pages from a file at once, grouped into a 669 structure called a "bio". To make it easier f 609 structure called a "bio". To make it easier for these types of 670 filesystems to support fs-verity, fs/verity/ a 610 filesystems to support fs-verity, fs/verity/ also provides a function 671 fsverity_verify_bio() which verifies all data !! 611 fsverity_verify_bio() which verifies all pages in a bio. 672 612 673 ext4 and f2fs also support encryption. If a v 613 ext4 and f2fs also support encryption. If a verity file is also 674 encrypted, the data must be decrypted before b !! 614 encrypted, the pages must be decrypted before being verified. To 675 support this, these filesystems allocate a "po 615 support this, these filesystems allocate a "post-read context" for 676 each bio and store it in ``->bi_private``:: 616 each bio and store it in ``->bi_private``:: 677 617 678 struct bio_post_read_ctx { 618 struct bio_post_read_ctx { 679 struct bio *bio; 619 struct bio *bio; 680 struct work_struct work; 620 struct work_struct work; 681 unsigned int cur_step; 621 unsigned int cur_step; 682 unsigned int enabled_steps; 622 unsigned int enabled_steps; 683 }; 623 }; 684 624 685 ``enabled_steps`` is a bitmask that specifies 625 ``enabled_steps`` is a bitmask that specifies whether decryption, 686 verity, or both is enabled. After the bio com 626 verity, or both is enabled. After the bio completes, for each needed 687 postprocessing step the filesystem enqueues th 627 postprocessing step the filesystem enqueues the bio_post_read_ctx on a 688 workqueue, and then the workqueue work does th 628 workqueue, and then the workqueue work does the decryption or 689 verification. Finally, folios where no decryp !! 629 verification. Finally, pages where no decryption or verity error 690 occurred are marked Uptodate, and the folios a !! 630 occurred are marked Uptodate, and the pages are unlocked. 691 631 692 On many filesystems, files can contain holes. 632 On many filesystems, files can contain holes. Normally, 693 ``->readahead()`` simply zeroes hole blocks an !! 633 ``->readahead()`` simply zeroes holes and sets the corresponding pages 694 corresponding data to be up-to-date; no bios a !! 634 Uptodate; no bios are issued. To prevent this case from bypassing 695 this case from bypassing fs-verity, filesystem !! 635 fs-verity, these filesystems use fsverity_verify_page() to verify hole 696 fsverity_verify_blocks() to verify hole blocks !! 636 pages. 697 637 698 Filesystems also disable direct I/O on verity 638 Filesystems also disable direct I/O on verity files, since otherwise 699 direct I/O would bypass fs-verity. 639 direct I/O would bypass fs-verity. 700 640 701 Userspace utility 641 Userspace utility 702 ================= 642 ================= 703 643 704 This document focuses on the kernel, but a use 644 This document focuses on the kernel, but a userspace utility for 705 fs-verity can be found at: 645 fs-verity can be found at: 706 646 707 https://git.kernel.org/pub/scm/fs/fsve !! 647 https://git.kernel.org/pub/scm/linux/kernel/git/ebiggers/fsverity-utils.git 708 648 709 See the README.md file in the fsverity-utils s 649 See the README.md file in the fsverity-utils source tree for details, 710 including examples of setting up fs-verity pro 650 including examples of setting up fs-verity protected files. 711 651 712 Tests 652 Tests 713 ===== 653 ===== 714 654 715 To test fs-verity, use xfstests. For example, 655 To test fs-verity, use xfstests. For example, using `kvm-xfstests 716 <https://github.com/tytso/xfstests-bld/blob/ma 656 <https://github.com/tytso/xfstests-bld/blob/master/Documentation/kvm-quickstart.md>`_:: 717 657 718 kvm-xfstests -c ext4,f2fs,btrfs -g verity 658 kvm-xfstests -c ext4,f2fs,btrfs -g verity 719 659 720 FAQ 660 FAQ 721 === 661 === 722 662 723 This section answers frequently asked question 663 This section answers frequently asked questions about fs-verity that 724 weren't already directly answered in other par 664 weren't already directly answered in other parts of this document. 725 665 726 :Q: Why isn't fs-verity part of IMA? 666 :Q: Why isn't fs-verity part of IMA? 727 :A: fs-verity and IMA (Integrity Measurement A 667 :A: fs-verity and IMA (Integrity Measurement Architecture) have 728 different focuses. fs-verity is a filesys 668 different focuses. fs-verity is a filesystem-level mechanism for 729 hashing individual files using a Merkle tr 669 hashing individual files using a Merkle tree. In contrast, IMA 730 specifies a system-wide policy that specif 670 specifies a system-wide policy that specifies which files are 731 hashed and what to do with those hashes, s 671 hashed and what to do with those hashes, such as log them, 732 authenticate them, or add them to a measur 672 authenticate them, or add them to a measurement list. 733 673 734 IMA supports the fs-verity hashing mechani 674 IMA supports the fs-verity hashing mechanism as an alternative 735 to full file hashes, for those who want th 675 to full file hashes, for those who want the performance and 736 security benefits of the Merkle tree based 676 security benefits of the Merkle tree based hash. However, it 737 doesn't make sense to force all uses of fs 677 doesn't make sense to force all uses of fs-verity to be through 738 IMA. fs-verity already meets many users' 678 IMA. fs-verity already meets many users' needs even as a 739 standalone filesystem feature, and it's te 679 standalone filesystem feature, and it's testable like other 740 filesystem features e.g. with xfstests. 680 filesystem features e.g. with xfstests. 741 681 742 :Q: Isn't fs-verity useless because the attack 682 :Q: Isn't fs-verity useless because the attacker can just modify the 743 hashes in the Merkle tree, which is stored 683 hashes in the Merkle tree, which is stored on-disk? 744 :A: To verify the authenticity of an fs-verity 684 :A: To verify the authenticity of an fs-verity file you must verify 745 the authenticity of the "fs-verity file di 685 the authenticity of the "fs-verity file digest", which 746 incorporates the root hash of the Merkle t 686 incorporates the root hash of the Merkle tree. See `Use cases`_. 747 687 748 :Q: Isn't fs-verity useless because the attack 688 :Q: Isn't fs-verity useless because the attacker can just replace a 749 verity file with a non-verity one? 689 verity file with a non-verity one? 750 :A: See `Use cases`_. In the initial use case 690 :A: See `Use cases`_. In the initial use case, it's really trusted 751 userspace code that authenticates the file 691 userspace code that authenticates the files; fs-verity is just a 752 tool to do this job efficiently and secure 692 tool to do this job efficiently and securely. The trusted 753 userspace code will consider non-verity fi 693 userspace code will consider non-verity files to be inauthentic. 754 694 755 :Q: Why does the Merkle tree need to be stored 695 :Q: Why does the Merkle tree need to be stored on-disk? Couldn't you 756 store just the root hash? 696 store just the root hash? 757 :A: If the Merkle tree wasn't stored on-disk, 697 :A: If the Merkle tree wasn't stored on-disk, then you'd have to 758 compute the entire tree when the file is f 698 compute the entire tree when the file is first accessed, even if 759 just one byte is being read. This is a fu 699 just one byte is being read. This is a fundamental consequence of 760 how Merkle tree hashing works. To verify 700 how Merkle tree hashing works. To verify a leaf node, you need to 761 verify the whole path to the root hash, in 701 verify the whole path to the root hash, including the root node 762 (the thing which the root hash is a hash o 702 (the thing which the root hash is a hash of). But if the root 763 node isn't stored on-disk, you have to com 703 node isn't stored on-disk, you have to compute it by hashing its 764 children, and so on until you've actually 704 children, and so on until you've actually hashed the entire file. 765 705 766 That defeats most of the point of doing a 706 That defeats most of the point of doing a Merkle tree-based hash, 767 since if you have to hash the whole file a 707 since if you have to hash the whole file ahead of time anyway, 768 then you could simply do sha256(file) inst 708 then you could simply do sha256(file) instead. That would be much 769 simpler, and a bit faster too. 709 simpler, and a bit faster too. 770 710 771 It's true that an in-memory Merkle tree co 711 It's true that an in-memory Merkle tree could still provide the 772 advantage of verification on every read ra 712 advantage of verification on every read rather than just on the 773 first read. However, it would be ineffici 713 first read. However, it would be inefficient because every time a 774 hash page gets evicted (you can't pin the 714 hash page gets evicted (you can't pin the entire Merkle tree into 775 memory, since it may be very large), in or 715 memory, since it may be very large), in order to restore it you 776 again need to hash everything below it in 716 again need to hash everything below it in the tree. This again 777 defeats most of the point of doing a Merkl 717 defeats most of the point of doing a Merkle tree-based hash, since 778 a single block read could trigger re-hashi 718 a single block read could trigger re-hashing gigabytes of data. 779 719 780 :Q: But couldn't you store just the leaf nodes 720 :Q: But couldn't you store just the leaf nodes and compute the rest? 781 :A: See previous answer; this really just move 721 :A: See previous answer; this really just moves up one level, since 782 one could alternatively interpret the data 722 one could alternatively interpret the data blocks as being the 783 leaf nodes of the Merkle tree. It's true 723 leaf nodes of the Merkle tree. It's true that the tree can be 784 computed much faster if the leaf level is 724 computed much faster if the leaf level is stored rather than just 785 the data, but that's only because each lev 725 the data, but that's only because each level is less than 1% the 786 size of the level below (assuming the reco 726 size of the level below (assuming the recommended settings of 787 SHA-256 and 4K blocks). For the exact sam 727 SHA-256 and 4K blocks). For the exact same reason, by storing 788 "just the leaf nodes" you'd already be sto 728 "just the leaf nodes" you'd already be storing over 99% of the 789 tree, so you might as well simply store th 729 tree, so you might as well simply store the whole tree. 790 730 791 :Q: Can the Merkle tree be built ahead of time 731 :Q: Can the Merkle tree be built ahead of time, e.g. distributed as 792 part of a package that is installed to man 732 part of a package that is installed to many computers? 793 :A: This isn't currently supported. It was pa 733 :A: This isn't currently supported. It was part of the original 794 design, but was removed to simplify the ke 734 design, but was removed to simplify the kernel UAPI and because it 795 wasn't a critical use case. Files are usu 735 wasn't a critical use case. Files are usually installed once and 796 used many times, and cryptographic hashing 736 used many times, and cryptographic hashing is somewhat fast on 797 most modern processors. 737 most modern processors. 798 738 799 :Q: Why doesn't fs-verity support writes? 739 :Q: Why doesn't fs-verity support writes? 800 :A: Write support would be very difficult and 740 :A: Write support would be very difficult and would require a 801 completely different design, so it's well 741 completely different design, so it's well outside the scope of 802 fs-verity. Write support would require: 742 fs-verity. Write support would require: 803 743 804 - A way to maintain consistency between th 744 - A way to maintain consistency between the data and hashes, 805 including all levels of hashes, since co 745 including all levels of hashes, since corruption after a crash 806 (especially of potentially the entire fi 746 (especially of potentially the entire file!) is unacceptable. 807 The main options for solving this are da 747 The main options for solving this are data journalling, 808 copy-on-write, and log-structured volume 748 copy-on-write, and log-structured volume. But it's very hard to 809 retrofit existing filesystems with new c 749 retrofit existing filesystems with new consistency mechanisms. 810 Data journalling is available on ext4, b 750 Data journalling is available on ext4, but is very slow. 811 751 812 - Rebuilding the Merkle tree after every w 752 - Rebuilding the Merkle tree after every write, which would be 813 extremely inefficient. Alternatively, a 753 extremely inefficient. Alternatively, a different authenticated 814 dictionary structure such as an "authent 754 dictionary structure such as an "authenticated skiplist" could 815 be used. However, this would be far mor 755 be used. However, this would be far more complex. 816 756 817 Compare it to dm-verity vs. dm-integrity. 757 Compare it to dm-verity vs. dm-integrity. dm-verity is very 818 simple: the kernel just verifies read-only 758 simple: the kernel just verifies read-only data against a 819 read-only Merkle tree. In contrast, dm-in 759 read-only Merkle tree. In contrast, dm-integrity supports writes 820 but is slow, is much more complex, and doe 760 but is slow, is much more complex, and doesn't actually support 821 full-device authentication since it authen 761 full-device authentication since it authenticates each sector 822 independently, i.e. there is no "root hash 762 independently, i.e. there is no "root hash". It doesn't really 823 make sense for the same device-mapper targ 763 make sense for the same device-mapper target to support these two 824 very different cases; the same applies to 764 very different cases; the same applies to fs-verity. 825 765 826 :Q: Since verity files are immutable, why isn' 766 :Q: Since verity files are immutable, why isn't the immutable bit set? 827 :A: The existing "immutable" bit (FS_IMMUTABLE 767 :A: The existing "immutable" bit (FS_IMMUTABLE_FL) already has a 828 specific set of semantics which not only m 768 specific set of semantics which not only make the file contents 829 read-only, but also prevent the file from 769 read-only, but also prevent the file from being deleted, renamed, 830 linked to, or having its owner or mode cha 770 linked to, or having its owner or mode changed. These extra 831 properties are unwanted for fs-verity, so 771 properties are unwanted for fs-verity, so reusing the immutable 832 bit isn't appropriate. 772 bit isn't appropriate. 833 773 834 :Q: Why does the API use ioctls instead of set 774 :Q: Why does the API use ioctls instead of setxattr() and getxattr()? 835 :A: Abusing the xattr interface for basically 775 :A: Abusing the xattr interface for basically arbitrary syscalls is 836 heavily frowned upon by most of the Linux 776 heavily frowned upon by most of the Linux filesystem developers. 837 An xattr should really just be an xattr on 777 An xattr should really just be an xattr on-disk, not an API to 838 e.g. magically trigger construction of a M 778 e.g. magically trigger construction of a Merkle tree. 839 779 840 :Q: Does fs-verity support remote filesystems? 780 :Q: Does fs-verity support remote filesystems? 841 :A: So far all filesystems that have implement 781 :A: So far all filesystems that have implemented fs-verity support are 842 local filesystems, but in principle any fi 782 local filesystems, but in principle any filesystem that can store 843 per-file verity metadata can support fs-ve 783 per-file verity metadata can support fs-verity, regardless of 844 whether it's local or remote. Some filesy 784 whether it's local or remote. Some filesystems may have fewer 845 options of where to store the verity metad 785 options of where to store the verity metadata; one possibility is 846 to store it past the end of the file and " 786 to store it past the end of the file and "hide" it from userspace 847 by manipulating i_size. The data verifica 787 by manipulating i_size. The data verification functions provided 848 by ``fs/verity/`` also assume that the fil 788 by ``fs/verity/`` also assume that the filesystem uses the Linux 849 pagecache, but both local and remote files 789 pagecache, but both local and remote filesystems normally do so. 850 790 851 :Q: Why is anything filesystem-specific at all 791 :Q: Why is anything filesystem-specific at all? Shouldn't fs-verity 852 be implemented entirely at the VFS level? 792 be implemented entirely at the VFS level? 853 :A: There are many reasons why this is not pos 793 :A: There are many reasons why this is not possible or would be very 854 difficult, including the following: 794 difficult, including the following: 855 795 856 - To prevent bypassing verification, folio !! 796 - To prevent bypassing verification, pages must not be marked 857 Uptodate until they've been verified. C 797 Uptodate until they've been verified. Currently, each 858 filesystem is responsible for marking fo !! 798 filesystem is responsible for marking pages Uptodate via 859 ``->readahead()``. Therefore, currently 799 ``->readahead()``. Therefore, currently it's not possible for 860 the VFS to do the verification on its ow 800 the VFS to do the verification on its own. Changing this would 861 require significant changes to the VFS a 801 require significant changes to the VFS and all filesystems. 862 802 863 - It would require defining a filesystem-i 803 - It would require defining a filesystem-independent way to store 864 the verity metadata. Extended attribute 804 the verity metadata. Extended attributes don't work for this 865 because (a) the Merkle tree may be gigab 805 because (a) the Merkle tree may be gigabytes, but many 866 filesystems assume that all xattrs fit i 806 filesystems assume that all xattrs fit into a single 4K 867 filesystem block, and (b) ext4 and f2fs 807 filesystem block, and (b) ext4 and f2fs encryption doesn't 868 encrypt xattrs, yet the Merkle tree *mus 808 encrypt xattrs, yet the Merkle tree *must* be encrypted when the 869 file contents are, because it stores has 809 file contents are, because it stores hashes of the plaintext 870 file contents. 810 file contents. 871 811 872 So the verity metadata would have to be 812 So the verity metadata would have to be stored in an actual 873 file. Using a separate file would be ve 813 file. Using a separate file would be very ugly, since the 874 metadata is fundamentally part of the fi 814 metadata is fundamentally part of the file to be protected, and 875 it could cause problems where users coul 815 it could cause problems where users could delete the real file 876 but not the metadata file or vice versa. 816 but not the metadata file or vice versa. On the other hand, 877 having it be in the same file would brea 817 having it be in the same file would break applications unless 878 filesystems' notion of i_size were divor 818 filesystems' notion of i_size were divorced from the VFS's, 879 which would be complex and require chang 819 which would be complex and require changes to all filesystems. 880 820 881 - It's desirable that FS_IOC_ENABLE_VERITY 821 - It's desirable that FS_IOC_ENABLE_VERITY uses the filesystem's 882 transaction mechanism so that either the 822 transaction mechanism so that either the file ends up with 883 verity enabled, or no changes were made. 823 verity enabled, or no changes were made. Allowing intermediate 884 states to occur after a crash may cause 824 states to occur after a crash may cause problems.
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.