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