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