fsverity.rst 42 KB

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