fscrypt.rst 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500
  1. =====================================
  2. Filesystem-level encryption (fscrypt)
  3. =====================================
  4. Introduction
  5. ============
  6. fscrypt is a library which filesystems can hook into to support
  7. transparent encryption of files and directories.
  8. Note: "fscrypt" in this document refers to the kernel-level portion,
  9. implemented in ``fs/crypto/``, as opposed to the userspace tool
  10. `fscrypt <https://github.com/google/fscrypt>`_. This document only
  11. covers the kernel-level portion. For command-line examples of how to
  12. use encryption, see the documentation for the userspace tool `fscrypt
  13. <https://github.com/google/fscrypt>`_. Also, it is recommended to use
  14. the fscrypt userspace tool, or other existing userspace tools such as
  15. `fscryptctl <https://github.com/google/fscryptctl>`_ or `Android's key
  16. management system
  17. <https://source.android.com/security/encryption/file-based>`_, over
  18. using the kernel's API directly. Using existing tools reduces the
  19. chance of introducing your own security bugs. (Nevertheless, for
  20. completeness this documentation covers the kernel's API anyway.)
  21. Unlike dm-crypt, fscrypt operates at the filesystem level rather than
  22. at the block device level. This allows it to encrypt different files
  23. with different keys and to have unencrypted files on the same
  24. filesystem. This is useful for multi-user systems where each user's
  25. data-at-rest needs to be cryptographically isolated from the others.
  26. However, except for filenames, fscrypt does not encrypt filesystem
  27. metadata.
  28. Unlike eCryptfs, which is a stacked filesystem, fscrypt is integrated
  29. directly into supported filesystems --- currently ext4, F2FS, UBIFS,
  30. and CephFS. This allows encrypted files to be read and written
  31. without caching both the decrypted and encrypted pages in the
  32. pagecache, thereby nearly halving the memory used and bringing it in
  33. line with unencrypted files. Similarly, half as many dentries and
  34. inodes are needed. eCryptfs also limits encrypted filenames to 143
  35. bytes, causing application compatibility issues; fscrypt allows the
  36. full 255 bytes (NAME_MAX). Finally, unlike eCryptfs, the fscrypt API
  37. can be used by unprivileged users, with no need to mount anything.
  38. fscrypt does not support encrypting files in-place. Instead, it
  39. supports marking an empty directory as encrypted. Then, after
  40. userspace provides the key, all regular files, directories, and
  41. symbolic links created in that directory tree are transparently
  42. encrypted.
  43. Threat model
  44. ============
  45. Offline attacks
  46. ---------------
  47. Provided that userspace chooses a strong encryption key, fscrypt
  48. protects the confidentiality of file contents and filenames in the
  49. event of a single point-in-time permanent offline compromise of the
  50. block device content. fscrypt does not protect the confidentiality of
  51. non-filename metadata, e.g. file sizes, file permissions, file
  52. timestamps, and extended attributes. Also, the existence and location
  53. of holes (unallocated blocks which logically contain all zeroes) in
  54. files is not protected.
  55. fscrypt is not guaranteed to protect confidentiality or authenticity
  56. if an attacker is able to manipulate the filesystem offline prior to
  57. an authorized user later accessing the filesystem.
  58. Online attacks
  59. --------------
  60. fscrypt (and storage encryption in general) can only provide limited
  61. protection, if any at all, against online attacks. In detail:
  62. Side-channel attacks
  63. ~~~~~~~~~~~~~~~~~~~~
  64. fscrypt is only resistant to side-channel attacks, such as timing or
  65. electromagnetic attacks, to the extent that the underlying Linux
  66. Cryptographic API algorithms or inline encryption hardware are. If a
  67. vulnerable algorithm is used, such as a table-based implementation of
  68. AES, it may be possible for an attacker to mount a side channel attack
  69. against the online system. Side channel attacks may also be mounted
  70. against applications consuming decrypted data.
  71. Unauthorized file access
  72. ~~~~~~~~~~~~~~~~~~~~~~~~
  73. After an encryption key has been added, fscrypt does not hide the
  74. plaintext file contents or filenames from other users on the same
  75. system. Instead, existing access control mechanisms such as file mode
  76. bits, POSIX ACLs, LSMs, or namespaces should be used for this purpose.
  77. (For the reasoning behind this, understand that while the key is
  78. added, the confidentiality of the data, from the perspective of the
  79. system itself, is *not* protected by the mathematical properties of
  80. encryption but rather only by the correctness of the kernel.
  81. Therefore, any encryption-specific access control checks would merely
  82. be enforced by kernel *code* and therefore would be largely redundant
  83. with the wide variety of access control mechanisms already available.)
  84. Kernel memory compromise
  85. ~~~~~~~~~~~~~~~~~~~~~~~~
  86. An attacker who compromises the system enough to read from arbitrary
  87. memory, e.g. by mounting a physical attack or by exploiting a kernel
  88. security vulnerability, can compromise all encryption keys that are
  89. currently in use.
  90. However, fscrypt allows encryption keys to be removed from the kernel,
  91. which may protect them from later compromise.
  92. In more detail, the FS_IOC_REMOVE_ENCRYPTION_KEY ioctl (or the
  93. FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS ioctl) can wipe a master
  94. encryption key from kernel memory. If it does so, it will also try to
  95. evict all cached inodes which had been "unlocked" using the key,
  96. thereby wiping their per-file keys and making them once again appear
  97. "locked", i.e. in ciphertext or encrypted form.
  98. However, these ioctls have some limitations:
  99. - Per-file keys for in-use files will *not* be removed or wiped.
  100. Therefore, for maximum effect, userspace should close the relevant
  101. encrypted files and directories before removing a master key, as
  102. well as kill any processes whose working directory is in an affected
  103. encrypted directory.
  104. - The kernel cannot magically wipe copies of the master key(s) that
  105. userspace might have as well. Therefore, userspace must wipe all
  106. copies of the master key(s) it makes as well; normally this should
  107. be done immediately after FS_IOC_ADD_ENCRYPTION_KEY, without waiting
  108. for FS_IOC_REMOVE_ENCRYPTION_KEY. Naturally, the same also applies
  109. to all higher levels in the key hierarchy. Userspace should also
  110. follow other security precautions such as mlock()ing memory
  111. containing keys to prevent it from being swapped out.
  112. - In general, decrypted contents and filenames in the kernel VFS
  113. caches are freed but not wiped. Therefore, portions thereof may be
  114. recoverable from freed memory, even after the corresponding key(s)
  115. were wiped. To partially solve this, you can set
  116. CONFIG_PAGE_POISONING=y in your kernel config and add page_poison=1
  117. to your kernel command line. However, this has a performance cost.
  118. - Secret keys might still exist in CPU registers or in other places
  119. not explicitly considered here.
  120. Limitations of v1 policies
  121. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  122. v1 encryption policies have some weaknesses with respect to online
  123. attacks:
  124. - There is no verification that the provided master key is correct.
  125. Therefore, a malicious user can temporarily associate the wrong key
  126. with another user's encrypted files to which they have read-only
  127. access. Because of filesystem caching, the wrong key will then be
  128. used by the other user's accesses to those files, even if the other
  129. user has the correct key in their own keyring. This violates the
  130. meaning of "read-only access".
  131. - A compromise of a per-file key also compromises the master key from
  132. which it was derived.
  133. - Non-root users cannot securely remove encryption keys.
  134. All the above problems are fixed with v2 encryption policies. For
  135. this reason among others, it is recommended to use v2 encryption
  136. policies on all new encrypted directories.
  137. Key hierarchy
  138. =============
  139. Master Keys
  140. -----------
  141. Each encrypted directory tree is protected by a *master key*. Master
  142. keys can be up to 64 bytes long, and must be at least as long as the
  143. greater of the security strength of the contents and filenames
  144. encryption modes being used. For example, if any AES-256 mode is
  145. used, the master key must be at least 256 bits, i.e. 32 bytes. A
  146. stricter requirement applies if the key is used by a v1 encryption
  147. policy and AES-256-XTS is used; such keys must be 64 bytes.
  148. To "unlock" an encrypted directory tree, userspace must provide the
  149. appropriate master key. There can be any number of master keys, each
  150. of which protects any number of directory trees on any number of
  151. filesystems.
  152. Master keys must be real cryptographic keys, i.e. indistinguishable
  153. from random bytestrings of the same length. This implies that users
  154. **must not** directly use a password as a master key, zero-pad a
  155. shorter key, or repeat a shorter key. Security cannot be guaranteed
  156. if userspace makes any such error, as the cryptographic proofs and
  157. analysis would no longer apply.
  158. Instead, users should generate master keys either using a
  159. cryptographically secure random number generator, or by using a KDF
  160. (Key Derivation Function). The kernel does not do any key stretching;
  161. therefore, if userspace derives the key from a low-entropy secret such
  162. as a passphrase, it is critical that a KDF designed for this purpose
  163. be used, such as scrypt, PBKDF2, or Argon2.
  164. Key derivation function
  165. -----------------------
  166. With one exception, fscrypt never uses the master key(s) for
  167. encryption directly. Instead, they are only used as input to a KDF
  168. (Key Derivation Function) to derive the actual keys.
  169. The KDF used for a particular master key differs depending on whether
  170. the key is used for v1 encryption policies or for v2 encryption
  171. policies. Users **must not** use the same key for both v1 and v2
  172. encryption policies. (No real-world attack is currently known on this
  173. specific case of key reuse, but its security cannot be guaranteed
  174. since the cryptographic proofs and analysis would no longer apply.)
  175. For v1 encryption policies, the KDF only supports deriving per-file
  176. encryption keys. It works by encrypting the master key with
  177. AES-128-ECB, using the file's 16-byte nonce as the AES key. The
  178. resulting ciphertext is used as the derived key. If the ciphertext is
  179. longer than needed, then it is truncated to the needed length.
  180. For v2 encryption policies, the KDF is HKDF-SHA512. The master key is
  181. passed as the "input keying material", no salt is used, and a distinct
  182. "application-specific information string" is used for each distinct
  183. key to be derived. For example, when a per-file encryption key is
  184. derived, the application-specific information string is the file's
  185. nonce prefixed with "fscrypt\\0" and a context byte. Different
  186. context bytes are used for other types of derived keys.
  187. HKDF-SHA512 is preferred to the original AES-128-ECB based KDF because
  188. HKDF is more flexible, is nonreversible, and evenly distributes
  189. entropy from the master key. HKDF is also standardized and widely
  190. used by other software, whereas the AES-128-ECB based KDF is ad-hoc.
  191. Per-file encryption keys
  192. ------------------------
  193. Since each master key can protect many files, it is necessary to
  194. "tweak" the encryption of each file so that the same plaintext in two
  195. files doesn't map to the same ciphertext, or vice versa. In most
  196. cases, fscrypt does this by deriving per-file keys. When a new
  197. encrypted inode (regular file, directory, or symlink) is created,
  198. fscrypt randomly generates a 16-byte nonce and stores it in the
  199. inode's encryption xattr. Then, it uses a KDF (as described in `Key
  200. derivation function`_) to derive the file's key from the master key
  201. and nonce.
  202. Key derivation was chosen over key wrapping because wrapped keys would
  203. require larger xattrs which would be less likely to fit in-line in the
  204. filesystem's inode table, and there didn't appear to be any
  205. significant advantages to key wrapping. In particular, currently
  206. there is no requirement to support unlocking a file with multiple
  207. alternative master keys or to support rotating master keys. Instead,
  208. the master keys may be wrapped in userspace, e.g. as is done by the
  209. `fscrypt <https://github.com/google/fscrypt>`_ tool.
  210. DIRECT_KEY policies
  211. -------------------
  212. The Adiantum encryption mode (see `Encryption modes and usage`_) is
  213. suitable for both contents and filenames encryption, and it accepts
  214. long IVs --- long enough to hold both an 8-byte data unit index and a
  215. 16-byte per-file nonce. Also, the overhead of each Adiantum key is
  216. greater than that of an AES-256-XTS key.
  217. Therefore, to improve performance and save memory, for Adiantum a
  218. "direct key" configuration is supported. When the user has enabled
  219. this by setting FSCRYPT_POLICY_FLAG_DIRECT_KEY in the fscrypt policy,
  220. per-file encryption keys are not used. Instead, whenever any data
  221. (contents or filenames) is encrypted, the file's 16-byte nonce is
  222. included in the IV. Moreover:
  223. - For v1 encryption policies, the encryption is done directly with the
  224. master key. Because of this, users **must not** use the same master
  225. key for any other purpose, even for other v1 policies.
  226. - For v2 encryption policies, the encryption is done with a per-mode
  227. key derived using the KDF. Users may use the same master key for
  228. other v2 encryption policies.
  229. IV_INO_LBLK_64 policies
  230. -----------------------
  231. When FSCRYPT_POLICY_FLAG_IV_INO_LBLK_64 is set in the fscrypt policy,
  232. the encryption keys are derived from the master key, encryption mode
  233. number, and filesystem UUID. This normally results in all files
  234. protected by the same master key sharing a single contents encryption
  235. key and a single filenames encryption key. To still encrypt different
  236. files' data differently, inode numbers are included in the IVs.
  237. Consequently, shrinking the filesystem may not be allowed.
  238. This format is optimized for use with inline encryption hardware
  239. compliant with the UFS standard, which supports only 64 IV bits per
  240. I/O request and may have only a small number of keyslots.
  241. IV_INO_LBLK_32 policies
  242. -----------------------
  243. IV_INO_LBLK_32 policies work like IV_INO_LBLK_64, except that for
  244. IV_INO_LBLK_32, the inode number is hashed with SipHash-2-4 (where the
  245. SipHash key is derived from the master key) and added to the file data
  246. unit index mod 2^32 to produce a 32-bit IV.
  247. This format is optimized for use with inline encryption hardware
  248. compliant with the eMMC v5.2 standard, which supports only 32 IV bits
  249. per I/O request and may have only a small number of keyslots. This
  250. format results in some level of IV reuse, so it should only be used
  251. when necessary due to hardware limitations.
  252. Key identifiers
  253. ---------------
  254. For master keys used for v2 encryption policies, a unique 16-byte "key
  255. identifier" is also derived using the KDF. This value is stored in
  256. the clear, since it is needed to reliably identify the key itself.
  257. Dirhash keys
  258. ------------
  259. For directories that are indexed using a secret-keyed dirhash over the
  260. plaintext filenames, the KDF is also used to derive a 128-bit
  261. SipHash-2-4 key per directory in order to hash filenames. This works
  262. just like deriving a per-file encryption key, except that a different
  263. KDF context is used. Currently, only casefolded ("case-insensitive")
  264. encrypted directories use this style of hashing.
  265. Encryption modes and usage
  266. ==========================
  267. fscrypt allows one encryption mode to be specified for file contents
  268. and one encryption mode to be specified for filenames. Different
  269. directory trees are permitted to use different encryption modes.
  270. Supported modes
  271. ---------------
  272. Currently, the following pairs of encryption modes are supported:
  273. - AES-256-XTS for contents and AES-256-CBC-CTS for filenames
  274. - AES-256-XTS for contents and AES-256-HCTR2 for filenames
  275. - Adiantum for both contents and filenames
  276. - AES-128-CBC-ESSIV for contents and AES-128-CBC-CTS for filenames
  277. - SM4-XTS for contents and SM4-CBC-CTS for filenames
  278. Note: in the API, "CBC" means CBC-ESSIV, and "CTS" means CBC-CTS.
  279. So, for example, FSCRYPT_MODE_AES_256_CTS means AES-256-CBC-CTS.
  280. Authenticated encryption modes are not currently supported because of
  281. the difficulty of dealing with ciphertext expansion. Therefore,
  282. contents encryption uses a block cipher in `XTS mode
  283. <https://en.wikipedia.org/wiki/Disk_encryption_theory#XTS>`_ or
  284. `CBC-ESSIV mode
  285. <https://en.wikipedia.org/wiki/Disk_encryption_theory#Encrypted_salt-sector_initialization_vector_(ESSIV)>`_,
  286. or a wide-block cipher. Filenames encryption uses a
  287. block cipher in `CBC-CTS mode
  288. <https://en.wikipedia.org/wiki/Ciphertext_stealing>`_ or a wide-block
  289. cipher.
  290. The (AES-256-XTS, AES-256-CBC-CTS) pair is the recommended default.
  291. It is also the only option that is *guaranteed* to always be supported
  292. if the kernel supports fscrypt at all; see `Kernel config options`_.
  293. The (AES-256-XTS, AES-256-HCTR2) pair is also a good choice that
  294. upgrades the filenames encryption to use a wide-block cipher. (A
  295. *wide-block cipher*, also called a tweakable super-pseudorandom
  296. permutation, has the property that changing one bit scrambles the
  297. entire result.) As described in `Filenames encryption`_, a wide-block
  298. cipher is the ideal mode for the problem domain, though CBC-CTS is the
  299. "least bad" choice among the alternatives. For more information about
  300. HCTR2, see `the HCTR2 paper <https://eprint.iacr.org/2021/1441.pdf>`_.
  301. Adiantum is recommended on systems where AES is too slow due to lack
  302. of hardware acceleration for AES. Adiantum is a wide-block cipher
  303. that uses XChaCha12 and AES-256 as its underlying components. Most of
  304. the work is done by XChaCha12, which is much faster than AES when AES
  305. acceleration is unavailable. For more information about Adiantum, see
  306. `the Adiantum paper <https://eprint.iacr.org/2018/720.pdf>`_.
  307. The (AES-128-CBC-ESSIV, AES-128-CBC-CTS) pair was added to try to
  308. provide a more efficient option for systems that lack AES instructions
  309. in the CPU but do have a non-inline crypto engine such as CAAM or CESA
  310. that supports AES-CBC (and not AES-XTS). This is deprecated. It has
  311. been shown that just doing AES on the CPU is actually faster.
  312. Moreover, Adiantum is faster still and is recommended on such systems.
  313. The remaining mode pairs are the "national pride ciphers":
  314. - (SM4-XTS, SM4-CBC-CTS)
  315. Generally speaking, these ciphers aren't "bad" per se, but they
  316. receive limited security review compared to the usual choices such as
  317. AES and ChaCha. They also don't bring much new to the table. It is
  318. suggested to only use these ciphers where their use is mandated.
  319. Kernel config options
  320. ---------------------
  321. Enabling fscrypt support (CONFIG_FS_ENCRYPTION) automatically pulls in
  322. only the basic support from the crypto API needed to use AES-256-XTS
  323. and AES-256-CBC-CTS encryption. For optimal performance, it is
  324. strongly recommended to also enable any available platform-specific
  325. kconfig options that provide acceleration for the algorithm(s) you
  326. wish to use. Support for any "non-default" encryption modes typically
  327. requires extra kconfig options as well.
  328. Below, some relevant options are listed by encryption mode. Note,
  329. acceleration options not listed below may be available for your
  330. platform; refer to the kconfig menus. File contents encryption can
  331. also be configured to use inline encryption hardware instead of the
  332. kernel crypto API (see `Inline encryption support`_); in that case,
  333. the file contents mode doesn't need to supported in the kernel crypto
  334. API, but the filenames mode still does.
  335. - AES-256-XTS and AES-256-CBC-CTS
  336. - Recommended:
  337. - arm64: CONFIG_CRYPTO_AES_ARM64_CE_BLK
  338. - x86: CONFIG_CRYPTO_AES_NI_INTEL
  339. - AES-256-HCTR2
  340. - Mandatory:
  341. - CONFIG_CRYPTO_HCTR2
  342. - Recommended:
  343. - arm64: CONFIG_CRYPTO_AES_ARM64_CE_BLK
  344. - arm64: CONFIG_CRYPTO_POLYVAL_ARM64_CE
  345. - x86: CONFIG_CRYPTO_AES_NI_INTEL
  346. - x86: CONFIG_CRYPTO_POLYVAL_CLMUL_NI
  347. - Adiantum
  348. - Mandatory:
  349. - CONFIG_CRYPTO_ADIANTUM
  350. - Recommended:
  351. - arm32: CONFIG_CRYPTO_CHACHA20_NEON
  352. - arm32: CONFIG_CRYPTO_NHPOLY1305_NEON
  353. - arm64: CONFIG_CRYPTO_CHACHA20_NEON
  354. - arm64: CONFIG_CRYPTO_NHPOLY1305_NEON
  355. - x86: CONFIG_CRYPTO_CHACHA20_X86_64
  356. - x86: CONFIG_CRYPTO_NHPOLY1305_SSE2
  357. - x86: CONFIG_CRYPTO_NHPOLY1305_AVX2
  358. - AES-128-CBC-ESSIV and AES-128-CBC-CTS:
  359. - Mandatory:
  360. - CONFIG_CRYPTO_ESSIV
  361. - CONFIG_CRYPTO_SHA256 or another SHA-256 implementation
  362. - Recommended:
  363. - AES-CBC acceleration
  364. fscrypt also uses HMAC-SHA512 for key derivation, so enabling SHA-512
  365. acceleration is recommended:
  366. - SHA-512
  367. - Recommended:
  368. - arm64: CONFIG_CRYPTO_SHA512_ARM64_CE
  369. - x86: CONFIG_CRYPTO_SHA512_SSSE3
  370. Contents encryption
  371. -------------------
  372. For contents encryption, each file's contents is divided into "data
  373. units". Each data unit is encrypted independently. The IV for each
  374. data unit incorporates the zero-based index of the data unit within
  375. the file. This ensures that each data unit within a file is encrypted
  376. differently, which is essential to prevent leaking information.
  377. Note: the encryption depending on the offset into the file means that
  378. operations like "collapse range" and "insert range" that rearrange the
  379. extent mapping of files are not supported on encrypted files.
  380. There are two cases for the sizes of the data units:
  381. * Fixed-size data units. This is how all filesystems other than UBIFS
  382. work. A file's data units are all the same size; the last data unit
  383. is zero-padded if needed. By default, the data unit size is equal
  384. to the filesystem block size. On some filesystems, users can select
  385. a sub-block data unit size via the ``log2_data_unit_size`` field of
  386. the encryption policy; see `FS_IOC_SET_ENCRYPTION_POLICY`_.
  387. * Variable-size data units. This is what UBIFS does. Each "UBIFS
  388. data node" is treated as a crypto data unit. Each contains variable
  389. length, possibly compressed data, zero-padded to the next 16-byte
  390. boundary. Users cannot select a sub-block data unit size on UBIFS.
  391. In the case of compression + encryption, the compressed data is
  392. encrypted. UBIFS compression works as described above. f2fs
  393. compression works a bit differently; it compresses a number of
  394. filesystem blocks into a smaller number of filesystem blocks.
  395. Therefore a f2fs-compressed file still uses fixed-size data units, and
  396. it is encrypted in a similar way to a file containing holes.
  397. As mentioned in `Key hierarchy`_, the default encryption setting uses
  398. per-file keys. In this case, the IV for each data unit is simply the
  399. index of the data unit in the file. However, users can select an
  400. encryption setting that does not use per-file keys. For these, some
  401. kind of file identifier is incorporated into the IVs as follows:
  402. - With `DIRECT_KEY policies`_, the data unit index is placed in bits
  403. 0-63 of the IV, and the file's nonce is placed in bits 64-191.
  404. - With `IV_INO_LBLK_64 policies`_, the data unit index is placed in
  405. bits 0-31 of the IV, and the file's inode number is placed in bits
  406. 32-63. This setting is only allowed when data unit indices and
  407. inode numbers fit in 32 bits.
  408. - With `IV_INO_LBLK_32 policies`_, the file's inode number is hashed
  409. and added to the data unit index. The resulting value is truncated
  410. to 32 bits and placed in bits 0-31 of the IV. This setting is only
  411. allowed when data unit indices and inode numbers fit in 32 bits.
  412. The byte order of the IV is always little endian.
  413. If the user selects FSCRYPT_MODE_AES_128_CBC for the contents mode, an
  414. ESSIV layer is automatically included. In this case, before the IV is
  415. passed to AES-128-CBC, it is encrypted with AES-256 where the AES-256
  416. key is the SHA-256 hash of the file's contents encryption key.
  417. Filenames encryption
  418. --------------------
  419. For filenames, each full filename is encrypted at once. Because of
  420. the requirements to retain support for efficient directory lookups and
  421. filenames of up to 255 bytes, the same IV is used for every filename
  422. in a directory.
  423. However, each encrypted directory still uses a unique key, or
  424. alternatively has the file's nonce (for `DIRECT_KEY policies`_) or
  425. inode number (for `IV_INO_LBLK_64 policies`_) included in the IVs.
  426. Thus, IV reuse is limited to within a single directory.
  427. With CBC-CTS, the IV reuse means that when the plaintext filenames share a
  428. common prefix at least as long as the cipher block size (16 bytes for AES), the
  429. corresponding encrypted filenames will also share a common prefix. This is
  430. undesirable. Adiantum and HCTR2 do not have this weakness, as they are
  431. wide-block encryption modes.
  432. All supported filenames encryption modes accept any plaintext length
  433. >= 16 bytes; cipher block alignment is not required. However,
  434. filenames shorter than 16 bytes are NUL-padded to 16 bytes before
  435. being encrypted. In addition, to reduce leakage of filename lengths
  436. via their ciphertexts, all filenames are NUL-padded to the next 4, 8,
  437. 16, or 32-byte boundary (configurable). 32 is recommended since this
  438. provides the best confidentiality, at the cost of making directory
  439. entries consume slightly more space. Note that since NUL (``\0``) is
  440. not otherwise a valid character in filenames, the padding will never
  441. produce duplicate plaintexts.
  442. Symbolic link targets are considered a type of filename and are
  443. encrypted in the same way as filenames in directory entries, except
  444. that IV reuse is not a problem as each symlink has its own inode.
  445. User API
  446. ========
  447. Setting an encryption policy
  448. ----------------------------
  449. FS_IOC_SET_ENCRYPTION_POLICY
  450. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  451. The FS_IOC_SET_ENCRYPTION_POLICY ioctl sets an encryption policy on an
  452. empty directory or verifies that a directory or regular file already
  453. has the specified encryption policy. It takes in a pointer to
  454. struct fscrypt_policy_v1 or struct fscrypt_policy_v2, defined as
  455. follows::
  456. #define FSCRYPT_POLICY_V1 0
  457. #define FSCRYPT_KEY_DESCRIPTOR_SIZE 8
  458. struct fscrypt_policy_v1 {
  459. __u8 version;
  460. __u8 contents_encryption_mode;
  461. __u8 filenames_encryption_mode;
  462. __u8 flags;
  463. __u8 master_key_descriptor[FSCRYPT_KEY_DESCRIPTOR_SIZE];
  464. };
  465. #define fscrypt_policy fscrypt_policy_v1
  466. #define FSCRYPT_POLICY_V2 2
  467. #define FSCRYPT_KEY_IDENTIFIER_SIZE 16
  468. struct fscrypt_policy_v2 {
  469. __u8 version;
  470. __u8 contents_encryption_mode;
  471. __u8 filenames_encryption_mode;
  472. __u8 flags;
  473. __u8 log2_data_unit_size;
  474. __u8 __reserved[3];
  475. __u8 master_key_identifier[FSCRYPT_KEY_IDENTIFIER_SIZE];
  476. };
  477. This structure must be initialized as follows:
  478. - ``version`` must be FSCRYPT_POLICY_V1 (0) if
  479. struct fscrypt_policy_v1 is used or FSCRYPT_POLICY_V2 (2) if
  480. struct fscrypt_policy_v2 is used. (Note: we refer to the original
  481. policy version as "v1", though its version code is really 0.)
  482. For new encrypted directories, use v2 policies.
  483. - ``contents_encryption_mode`` and ``filenames_encryption_mode`` must
  484. be set to constants from ``<linux/fscrypt.h>`` which identify the
  485. encryption modes to use. If unsure, use FSCRYPT_MODE_AES_256_XTS
  486. (1) for ``contents_encryption_mode`` and FSCRYPT_MODE_AES_256_CTS
  487. (4) for ``filenames_encryption_mode``. For details, see `Encryption
  488. modes and usage`_.
  489. v1 encryption policies only support three combinations of modes:
  490. (FSCRYPT_MODE_AES_256_XTS, FSCRYPT_MODE_AES_256_CTS),
  491. (FSCRYPT_MODE_AES_128_CBC, FSCRYPT_MODE_AES_128_CTS), and
  492. (FSCRYPT_MODE_ADIANTUM, FSCRYPT_MODE_ADIANTUM). v2 policies support
  493. all combinations documented in `Supported modes`_.
  494. - ``flags`` contains optional flags from ``<linux/fscrypt.h>``:
  495. - FSCRYPT_POLICY_FLAGS_PAD_*: The amount of NUL padding to use when
  496. encrypting filenames. If unsure, use FSCRYPT_POLICY_FLAGS_PAD_32
  497. (0x3).
  498. - FSCRYPT_POLICY_FLAG_DIRECT_KEY: See `DIRECT_KEY policies`_.
  499. - FSCRYPT_POLICY_FLAG_IV_INO_LBLK_64: See `IV_INO_LBLK_64
  500. policies`_.
  501. - FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32: See `IV_INO_LBLK_32
  502. policies`_.
  503. v1 encryption policies only support the PAD_* and DIRECT_KEY flags.
  504. The other flags are only supported by v2 encryption policies.
  505. The DIRECT_KEY, IV_INO_LBLK_64, and IV_INO_LBLK_32 flags are
  506. mutually exclusive.
  507. - ``log2_data_unit_size`` is the log2 of the data unit size in bytes,
  508. or 0 to select the default data unit size. The data unit size is
  509. the granularity of file contents encryption. For example, setting
  510. ``log2_data_unit_size`` to 12 causes file contents be passed to the
  511. underlying encryption algorithm (such as AES-256-XTS) in 4096-byte
  512. data units, each with its own IV.
  513. Not all filesystems support setting ``log2_data_unit_size``. ext4
  514. and f2fs support it since Linux v6.7. On filesystems that support
  515. it, the supported nonzero values are 9 through the log2 of the
  516. filesystem block size, inclusively. The default value of 0 selects
  517. the filesystem block size.
  518. The main use case for ``log2_data_unit_size`` is for selecting a
  519. data unit size smaller than the filesystem block size for
  520. compatibility with inline encryption hardware that only supports
  521. smaller data unit sizes. ``/sys/block/$disk/queue/crypto/`` may be
  522. useful for checking which data unit sizes are supported by a
  523. particular system's inline encryption hardware.
  524. Leave this field zeroed unless you are certain you need it. Using
  525. an unnecessarily small data unit size reduces performance.
  526. - For v2 encryption policies, ``__reserved`` must be zeroed.
  527. - For v1 encryption policies, ``master_key_descriptor`` specifies how
  528. to find the master key in a keyring; see `Adding keys`_. It is up
  529. to userspace to choose a unique ``master_key_descriptor`` for each
  530. master key. The e4crypt and fscrypt tools use the first 8 bytes of
  531. ``SHA-512(SHA-512(master_key))``, but this particular scheme is not
  532. required. Also, the master key need not be in the keyring yet when
  533. FS_IOC_SET_ENCRYPTION_POLICY is executed. However, it must be added
  534. before any files can be created in the encrypted directory.
  535. For v2 encryption policies, ``master_key_descriptor`` has been
  536. replaced with ``master_key_identifier``, which is longer and cannot
  537. be arbitrarily chosen. Instead, the key must first be added using
  538. `FS_IOC_ADD_ENCRYPTION_KEY`_. Then, the ``key_spec.u.identifier``
  539. the kernel returned in the struct fscrypt_add_key_arg must
  540. be used as the ``master_key_identifier`` in
  541. struct fscrypt_policy_v2.
  542. If the file is not yet encrypted, then FS_IOC_SET_ENCRYPTION_POLICY
  543. verifies that the file is an empty directory. If so, the specified
  544. encryption policy is assigned to the directory, turning it into an
  545. encrypted directory. After that, and after providing the
  546. corresponding master key as described in `Adding keys`_, all regular
  547. files, directories (recursively), and symlinks created in the
  548. directory will be encrypted, inheriting the same encryption policy.
  549. The filenames in the directory's entries will be encrypted as well.
  550. Alternatively, if the file is already encrypted, then
  551. FS_IOC_SET_ENCRYPTION_POLICY validates that the specified encryption
  552. policy exactly matches the actual one. If they match, then the ioctl
  553. returns 0. Otherwise, it fails with EEXIST. This works on both
  554. regular files and directories, including nonempty directories.
  555. When a v2 encryption policy is assigned to a directory, it is also
  556. required that either the specified key has been added by the current
  557. user or that the caller has CAP_FOWNER in the initial user namespace.
  558. (This is needed to prevent a user from encrypting their data with
  559. another user's key.) The key must remain added while
  560. FS_IOC_SET_ENCRYPTION_POLICY is executing. However, if the new
  561. encrypted directory does not need to be accessed immediately, then the
  562. key can be removed right away afterwards.
  563. Note that the ext4 filesystem does not allow the root directory to be
  564. encrypted, even if it is empty. Users who want to encrypt an entire
  565. filesystem with one key should consider using dm-crypt instead.
  566. FS_IOC_SET_ENCRYPTION_POLICY can fail with the following errors:
  567. - ``EACCES``: the file is not owned by the process's uid, nor does the
  568. process have the CAP_FOWNER capability in a namespace with the file
  569. owner's uid mapped
  570. - ``EEXIST``: the file is already encrypted with an encryption policy
  571. different from the one specified
  572. - ``EINVAL``: an invalid encryption policy was specified (invalid
  573. version, mode(s), or flags; or reserved bits were set); or a v1
  574. encryption policy was specified but the directory has the casefold
  575. flag enabled (casefolding is incompatible with v1 policies).
  576. - ``ENOKEY``: a v2 encryption policy was specified, but the key with
  577. the specified ``master_key_identifier`` has not been added, nor does
  578. the process have the CAP_FOWNER capability in the initial user
  579. namespace
  580. - ``ENOTDIR``: the file is unencrypted and is a regular file, not a
  581. directory
  582. - ``ENOTEMPTY``: the file is unencrypted and is a nonempty directory
  583. - ``ENOTTY``: this type of filesystem does not implement encryption
  584. - ``EOPNOTSUPP``: the kernel was not configured with encryption
  585. support for filesystems, or the filesystem superblock has not
  586. had encryption enabled on it. (For example, to use encryption on an
  587. ext4 filesystem, CONFIG_FS_ENCRYPTION must be enabled in the
  588. kernel config, and the superblock must have had the "encrypt"
  589. feature flag enabled using ``tune2fs -O encrypt`` or ``mkfs.ext4 -O
  590. encrypt``.)
  591. - ``EPERM``: this directory may not be encrypted, e.g. because it is
  592. the root directory of an ext4 filesystem
  593. - ``EROFS``: the filesystem is readonly
  594. Getting an encryption policy
  595. ----------------------------
  596. Two ioctls are available to get a file's encryption policy:
  597. - `FS_IOC_GET_ENCRYPTION_POLICY_EX`_
  598. - `FS_IOC_GET_ENCRYPTION_POLICY`_
  599. The extended (_EX) version of the ioctl is more general and is
  600. recommended to use when possible. However, on older kernels only the
  601. original ioctl is available. Applications should try the extended
  602. version, and if it fails with ENOTTY fall back to the original
  603. version.
  604. FS_IOC_GET_ENCRYPTION_POLICY_EX
  605. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  606. The FS_IOC_GET_ENCRYPTION_POLICY_EX ioctl retrieves the encryption
  607. policy, if any, for a directory or regular file. No additional
  608. permissions are required beyond the ability to open the file. It
  609. takes in a pointer to struct fscrypt_get_policy_ex_arg,
  610. defined as follows::
  611. struct fscrypt_get_policy_ex_arg {
  612. __u64 policy_size; /* input/output */
  613. union {
  614. __u8 version;
  615. struct fscrypt_policy_v1 v1;
  616. struct fscrypt_policy_v2 v2;
  617. } policy; /* output */
  618. };
  619. The caller must initialize ``policy_size`` to the size available for
  620. the policy struct, i.e. ``sizeof(arg.policy)``.
  621. On success, the policy struct is returned in ``policy``, and its
  622. actual size is returned in ``policy_size``. ``policy.version`` should
  623. be checked to determine the version of policy returned. Note that the
  624. version code for the "v1" policy is actually 0 (FSCRYPT_POLICY_V1).
  625. FS_IOC_GET_ENCRYPTION_POLICY_EX can fail with the following errors:
  626. - ``EINVAL``: the file is encrypted, but it uses an unrecognized
  627. encryption policy version
  628. - ``ENODATA``: the file is not encrypted
  629. - ``ENOTTY``: this type of filesystem does not implement encryption,
  630. or this kernel is too old to support FS_IOC_GET_ENCRYPTION_POLICY_EX
  631. (try FS_IOC_GET_ENCRYPTION_POLICY instead)
  632. - ``EOPNOTSUPP``: the kernel was not configured with encryption
  633. support for this filesystem, or the filesystem superblock has not
  634. had encryption enabled on it
  635. - ``EOVERFLOW``: the file is encrypted and uses a recognized
  636. encryption policy version, but the policy struct does not fit into
  637. the provided buffer
  638. Note: if you only need to know whether a file is encrypted or not, on
  639. most filesystems it is also possible to use the FS_IOC_GETFLAGS ioctl
  640. and check for FS_ENCRYPT_FL, or to use the statx() system call and
  641. check for STATX_ATTR_ENCRYPTED in stx_attributes.
  642. FS_IOC_GET_ENCRYPTION_POLICY
  643. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  644. The FS_IOC_GET_ENCRYPTION_POLICY ioctl can also retrieve the
  645. encryption policy, if any, for a directory or regular file. However,
  646. unlike `FS_IOC_GET_ENCRYPTION_POLICY_EX`_,
  647. FS_IOC_GET_ENCRYPTION_POLICY only supports the original policy
  648. version. It takes in a pointer directly to struct fscrypt_policy_v1
  649. rather than struct fscrypt_get_policy_ex_arg.
  650. The error codes for FS_IOC_GET_ENCRYPTION_POLICY are the same as those
  651. for FS_IOC_GET_ENCRYPTION_POLICY_EX, except that
  652. FS_IOC_GET_ENCRYPTION_POLICY also returns ``EINVAL`` if the file is
  653. encrypted using a newer encryption policy version.
  654. Getting the per-filesystem salt
  655. -------------------------------
  656. Some filesystems, such as ext4 and F2FS, also support the deprecated
  657. ioctl FS_IOC_GET_ENCRYPTION_PWSALT. This ioctl retrieves a randomly
  658. generated 16-byte value stored in the filesystem superblock. This
  659. value is intended to used as a salt when deriving an encryption key
  660. from a passphrase or other low-entropy user credential.
  661. FS_IOC_GET_ENCRYPTION_PWSALT is deprecated. Instead, prefer to
  662. generate and manage any needed salt(s) in userspace.
  663. Getting a file's encryption nonce
  664. ---------------------------------
  665. Since Linux v5.7, the ioctl FS_IOC_GET_ENCRYPTION_NONCE is supported.
  666. On encrypted files and directories it gets the inode's 16-byte nonce.
  667. On unencrypted files and directories, it fails with ENODATA.
  668. This ioctl can be useful for automated tests which verify that the
  669. encryption is being done correctly. It is not needed for normal use
  670. of fscrypt.
  671. Adding keys
  672. -----------
  673. FS_IOC_ADD_ENCRYPTION_KEY
  674. ~~~~~~~~~~~~~~~~~~~~~~~~~
  675. The FS_IOC_ADD_ENCRYPTION_KEY ioctl adds a master encryption key to
  676. the filesystem, making all files on the filesystem which were
  677. encrypted using that key appear "unlocked", i.e. in plaintext form.
  678. It can be executed on any file or directory on the target filesystem,
  679. but using the filesystem's root directory is recommended. It takes in
  680. a pointer to struct fscrypt_add_key_arg, defined as follows::
  681. struct fscrypt_add_key_arg {
  682. struct fscrypt_key_specifier key_spec;
  683. __u32 raw_size;
  684. __u32 key_id;
  685. __u32 __reserved[8];
  686. __u8 raw[];
  687. };
  688. #define FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR 1
  689. #define FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER 2
  690. struct fscrypt_key_specifier {
  691. __u32 type; /* one of FSCRYPT_KEY_SPEC_TYPE_* */
  692. __u32 __reserved;
  693. union {
  694. __u8 __reserved[32]; /* reserve some extra space */
  695. __u8 descriptor[FSCRYPT_KEY_DESCRIPTOR_SIZE];
  696. __u8 identifier[FSCRYPT_KEY_IDENTIFIER_SIZE];
  697. } u;
  698. };
  699. struct fscrypt_provisioning_key_payload {
  700. __u32 type;
  701. __u32 __reserved;
  702. __u8 raw[];
  703. };
  704. struct fscrypt_add_key_arg must be zeroed, then initialized
  705. as follows:
  706. - If the key is being added for use by v1 encryption policies, then
  707. ``key_spec.type`` must contain FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR, and
  708. ``key_spec.u.descriptor`` must contain the descriptor of the key
  709. being added, corresponding to the value in the
  710. ``master_key_descriptor`` field of struct fscrypt_policy_v1.
  711. To add this type of key, the calling process must have the
  712. CAP_SYS_ADMIN capability in the initial user namespace.
  713. Alternatively, if the key is being added for use by v2 encryption
  714. policies, then ``key_spec.type`` must contain
  715. FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER, and ``key_spec.u.identifier`` is
  716. an *output* field which the kernel fills in with a cryptographic
  717. hash of the key. To add this type of key, the calling process does
  718. not need any privileges. However, the number of keys that can be
  719. added is limited by the user's quota for the keyrings service (see
  720. ``Documentation/security/keys/core.rst``).
  721. - ``raw_size`` must be the size of the ``raw`` key provided, in bytes.
  722. Alternatively, if ``key_id`` is nonzero, this field must be 0, since
  723. in that case the size is implied by the specified Linux keyring key.
  724. - ``key_id`` is 0 if the raw key is given directly in the ``raw``
  725. field. Otherwise ``key_id`` is the ID of a Linux keyring key of
  726. type "fscrypt-provisioning" whose payload is
  727. struct fscrypt_provisioning_key_payload whose ``raw`` field contains
  728. the raw key and whose ``type`` field matches ``key_spec.type``.
  729. Since ``raw`` is variable-length, the total size of this key's
  730. payload must be ``sizeof(struct fscrypt_provisioning_key_payload)``
  731. plus the raw key size. The process must have Search permission on
  732. this key.
  733. Most users should leave this 0 and specify the raw key directly.
  734. The support for specifying a Linux keyring key is intended mainly to
  735. allow re-adding keys after a filesystem is unmounted and re-mounted,
  736. without having to store the raw keys in userspace memory.
  737. - ``raw`` is a variable-length field which must contain the actual
  738. key, ``raw_size`` bytes long. Alternatively, if ``key_id`` is
  739. nonzero, then this field is unused.
  740. For v2 policy keys, the kernel keeps track of which user (identified
  741. by effective user ID) added the key, and only allows the key to be
  742. removed by that user --- or by "root", if they use
  743. `FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS`_.
  744. However, if another user has added the key, it may be desirable to
  745. prevent that other user from unexpectedly removing it. Therefore,
  746. FS_IOC_ADD_ENCRYPTION_KEY may also be used to add a v2 policy key
  747. *again*, even if it's already added by other user(s). In this case,
  748. FS_IOC_ADD_ENCRYPTION_KEY will just install a claim to the key for the
  749. current user, rather than actually add the key again (but the raw key
  750. must still be provided, as a proof of knowledge).
  751. FS_IOC_ADD_ENCRYPTION_KEY returns 0 if either the key or a claim to
  752. the key was either added or already exists.
  753. FS_IOC_ADD_ENCRYPTION_KEY can fail with the following errors:
  754. - ``EACCES``: FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR was specified, but the
  755. caller does not have the CAP_SYS_ADMIN capability in the initial
  756. user namespace; or the raw key was specified by Linux key ID but the
  757. process lacks Search permission on the key.
  758. - ``EDQUOT``: the key quota for this user would be exceeded by adding
  759. the key
  760. - ``EINVAL``: invalid key size or key specifier type, or reserved bits
  761. were set
  762. - ``EKEYREJECTED``: the raw key was specified by Linux key ID, but the
  763. key has the wrong type
  764. - ``ENOKEY``: the raw key was specified by Linux key ID, but no key
  765. exists with that ID
  766. - ``ENOTTY``: this type of filesystem does not implement encryption
  767. - ``EOPNOTSUPP``: the kernel was not configured with encryption
  768. support for this filesystem, or the filesystem superblock has not
  769. had encryption enabled on it
  770. Legacy method
  771. ~~~~~~~~~~~~~
  772. For v1 encryption policies, a master encryption key can also be
  773. provided by adding it to a process-subscribed keyring, e.g. to a
  774. session keyring, or to a user keyring if the user keyring is linked
  775. into the session keyring.
  776. This method is deprecated (and not supported for v2 encryption
  777. policies) for several reasons. First, it cannot be used in
  778. combination with FS_IOC_REMOVE_ENCRYPTION_KEY (see `Removing keys`_),
  779. so for removing a key a workaround such as keyctl_unlink() in
  780. combination with ``sync; echo 2 > /proc/sys/vm/drop_caches`` would
  781. have to be used. Second, it doesn't match the fact that the
  782. locked/unlocked status of encrypted files (i.e. whether they appear to
  783. be in plaintext form or in ciphertext form) is global. This mismatch
  784. has caused much confusion as well as real problems when processes
  785. running under different UIDs, such as a ``sudo`` command, need to
  786. access encrypted files.
  787. Nevertheless, to add a key to one of the process-subscribed keyrings,
  788. the add_key() system call can be used (see:
  789. ``Documentation/security/keys/core.rst``). The key type must be
  790. "logon"; keys of this type are kept in kernel memory and cannot be
  791. read back by userspace. The key description must be "fscrypt:"
  792. followed by the 16-character lower case hex representation of the
  793. ``master_key_descriptor`` that was set in the encryption policy. The
  794. key payload must conform to the following structure::
  795. #define FSCRYPT_MAX_KEY_SIZE 64
  796. struct fscrypt_key {
  797. __u32 mode;
  798. __u8 raw[FSCRYPT_MAX_KEY_SIZE];
  799. __u32 size;
  800. };
  801. ``mode`` is ignored; just set it to 0. The actual key is provided in
  802. ``raw`` with ``size`` indicating its size in bytes. That is, the
  803. bytes ``raw[0..size-1]`` (inclusive) are the actual key.
  804. The key description prefix "fscrypt:" may alternatively be replaced
  805. with a filesystem-specific prefix such as "ext4:". However, the
  806. filesystem-specific prefixes are deprecated and should not be used in
  807. new programs.
  808. Removing keys
  809. -------------
  810. Two ioctls are available for removing a key that was added by
  811. `FS_IOC_ADD_ENCRYPTION_KEY`_:
  812. - `FS_IOC_REMOVE_ENCRYPTION_KEY`_
  813. - `FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS`_
  814. These two ioctls differ only in cases where v2 policy keys are added
  815. or removed by non-root users.
  816. These ioctls don't work on keys that were added via the legacy
  817. process-subscribed keyrings mechanism.
  818. Before using these ioctls, read the `Kernel memory compromise`_
  819. section for a discussion of the security goals and limitations of
  820. these ioctls.
  821. FS_IOC_REMOVE_ENCRYPTION_KEY
  822. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  823. The FS_IOC_REMOVE_ENCRYPTION_KEY ioctl removes a claim to a master
  824. encryption key from the filesystem, and possibly removes the key
  825. itself. It can be executed on any file or directory on the target
  826. filesystem, but using the filesystem's root directory is recommended.
  827. It takes in a pointer to struct fscrypt_remove_key_arg, defined
  828. as follows::
  829. struct fscrypt_remove_key_arg {
  830. struct fscrypt_key_specifier key_spec;
  831. #define FSCRYPT_KEY_REMOVAL_STATUS_FLAG_FILES_BUSY 0x00000001
  832. #define FSCRYPT_KEY_REMOVAL_STATUS_FLAG_OTHER_USERS 0x00000002
  833. __u32 removal_status_flags; /* output */
  834. __u32 __reserved[5];
  835. };
  836. This structure must be zeroed, then initialized as follows:
  837. - The key to remove is specified by ``key_spec``:
  838. - To remove a key used by v1 encryption policies, set
  839. ``key_spec.type`` to FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR and fill
  840. in ``key_spec.u.descriptor``. To remove this type of key, the
  841. calling process must have the CAP_SYS_ADMIN capability in the
  842. initial user namespace.
  843. - To remove a key used by v2 encryption policies, set
  844. ``key_spec.type`` to FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER and fill
  845. in ``key_spec.u.identifier``.
  846. For v2 policy keys, this ioctl is usable by non-root users. However,
  847. to make this possible, it actually just removes the current user's
  848. claim to the key, undoing a single call to FS_IOC_ADD_ENCRYPTION_KEY.
  849. Only after all claims are removed is the key really removed.
  850. For example, if FS_IOC_ADD_ENCRYPTION_KEY was called with uid 1000,
  851. then the key will be "claimed" by uid 1000, and
  852. FS_IOC_REMOVE_ENCRYPTION_KEY will only succeed as uid 1000. Or, if
  853. both uids 1000 and 2000 added the key, then for each uid
  854. FS_IOC_REMOVE_ENCRYPTION_KEY will only remove their own claim. Only
  855. once *both* are removed is the key really removed. (Think of it like
  856. unlinking a file that may have hard links.)
  857. If FS_IOC_REMOVE_ENCRYPTION_KEY really removes the key, it will also
  858. try to "lock" all files that had been unlocked with the key. It won't
  859. lock files that are still in-use, so this ioctl is expected to be used
  860. in cooperation with userspace ensuring that none of the files are
  861. still open. However, if necessary, this ioctl can be executed again
  862. later to retry locking any remaining files.
  863. FS_IOC_REMOVE_ENCRYPTION_KEY returns 0 if either the key was removed
  864. (but may still have files remaining to be locked), the user's claim to
  865. the key was removed, or the key was already removed but had files
  866. remaining to be the locked so the ioctl retried locking them. In any
  867. of these cases, ``removal_status_flags`` is filled in with the
  868. following informational status flags:
  869. - ``FSCRYPT_KEY_REMOVAL_STATUS_FLAG_FILES_BUSY``: set if some file(s)
  870. are still in-use. Not guaranteed to be set in the case where only
  871. the user's claim to the key was removed.
  872. - ``FSCRYPT_KEY_REMOVAL_STATUS_FLAG_OTHER_USERS``: set if only the
  873. user's claim to the key was removed, not the key itself
  874. FS_IOC_REMOVE_ENCRYPTION_KEY can fail with the following errors:
  875. - ``EACCES``: The FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR key specifier type
  876. was specified, but the caller does not have the CAP_SYS_ADMIN
  877. capability in the initial user namespace
  878. - ``EINVAL``: invalid key specifier type, or reserved bits were set
  879. - ``ENOKEY``: the key object was not found at all, i.e. it was never
  880. added in the first place or was already fully removed including all
  881. files locked; or, the user does not have a claim to the key (but
  882. someone else does).
  883. - ``ENOTTY``: this type of filesystem does not implement encryption
  884. - ``EOPNOTSUPP``: the kernel was not configured with encryption
  885. support for this filesystem, or the filesystem superblock has not
  886. had encryption enabled on it
  887. FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS
  888. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  889. FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS is exactly the same as
  890. `FS_IOC_REMOVE_ENCRYPTION_KEY`_, except that for v2 policy keys, the
  891. ALL_USERS version of the ioctl will remove all users' claims to the
  892. key, not just the current user's. I.e., the key itself will always be
  893. removed, no matter how many users have added it. This difference is
  894. only meaningful if non-root users are adding and removing keys.
  895. Because of this, FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS also requires
  896. "root", namely the CAP_SYS_ADMIN capability in the initial user
  897. namespace. Otherwise it will fail with EACCES.
  898. Getting key status
  899. ------------------
  900. FS_IOC_GET_ENCRYPTION_KEY_STATUS
  901. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  902. The FS_IOC_GET_ENCRYPTION_KEY_STATUS ioctl retrieves the status of a
  903. master encryption key. It can be executed on any file or directory on
  904. the target filesystem, but using the filesystem's root directory is
  905. recommended. It takes in a pointer to
  906. struct fscrypt_get_key_status_arg, defined as follows::
  907. struct fscrypt_get_key_status_arg {
  908. /* input */
  909. struct fscrypt_key_specifier key_spec;
  910. __u32 __reserved[6];
  911. /* output */
  912. #define FSCRYPT_KEY_STATUS_ABSENT 1
  913. #define FSCRYPT_KEY_STATUS_PRESENT 2
  914. #define FSCRYPT_KEY_STATUS_INCOMPLETELY_REMOVED 3
  915. __u32 status;
  916. #define FSCRYPT_KEY_STATUS_FLAG_ADDED_BY_SELF 0x00000001
  917. __u32 status_flags;
  918. __u32 user_count;
  919. __u32 __out_reserved[13];
  920. };
  921. The caller must zero all input fields, then fill in ``key_spec``:
  922. - To get the status of a key for v1 encryption policies, set
  923. ``key_spec.type`` to FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR and fill
  924. in ``key_spec.u.descriptor``.
  925. - To get the status of a key for v2 encryption policies, set
  926. ``key_spec.type`` to FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER and fill
  927. in ``key_spec.u.identifier``.
  928. On success, 0 is returned and the kernel fills in the output fields:
  929. - ``status`` indicates whether the key is absent, present, or
  930. incompletely removed. Incompletely removed means that removal has
  931. been initiated, but some files are still in use; i.e.,
  932. `FS_IOC_REMOVE_ENCRYPTION_KEY`_ returned 0 but set the informational
  933. status flag FSCRYPT_KEY_REMOVAL_STATUS_FLAG_FILES_BUSY.
  934. - ``status_flags`` can contain the following flags:
  935. - ``FSCRYPT_KEY_STATUS_FLAG_ADDED_BY_SELF`` indicates that the key
  936. has added by the current user. This is only set for keys
  937. identified by ``identifier`` rather than by ``descriptor``.
  938. - ``user_count`` specifies the number of users who have added the key.
  939. This is only set for keys identified by ``identifier`` rather than
  940. by ``descriptor``.
  941. FS_IOC_GET_ENCRYPTION_KEY_STATUS can fail with the following errors:
  942. - ``EINVAL``: invalid key specifier type, or reserved bits were set
  943. - ``ENOTTY``: this type of filesystem does not implement encryption
  944. - ``EOPNOTSUPP``: the kernel was not configured with encryption
  945. support for this filesystem, or the filesystem superblock has not
  946. had encryption enabled on it
  947. Among other use cases, FS_IOC_GET_ENCRYPTION_KEY_STATUS can be useful
  948. for determining whether the key for a given encrypted directory needs
  949. to be added before prompting the user for the passphrase needed to
  950. derive the key.
  951. FS_IOC_GET_ENCRYPTION_KEY_STATUS can only get the status of keys in
  952. the filesystem-level keyring, i.e. the keyring managed by
  953. `FS_IOC_ADD_ENCRYPTION_KEY`_ and `FS_IOC_REMOVE_ENCRYPTION_KEY`_. It
  954. cannot get the status of a key that has only been added for use by v1
  955. encryption policies using the legacy mechanism involving
  956. process-subscribed keyrings.
  957. Access semantics
  958. ================
  959. With the key
  960. ------------
  961. With the encryption key, encrypted regular files, directories, and
  962. symlinks behave very similarly to their unencrypted counterparts ---
  963. after all, the encryption is intended to be transparent. However,
  964. astute users may notice some differences in behavior:
  965. - Unencrypted files, or files encrypted with a different encryption
  966. policy (i.e. different key, modes, or flags), cannot be renamed or
  967. linked into an encrypted directory; see `Encryption policy
  968. enforcement`_. Attempts to do so will fail with EXDEV. However,
  969. encrypted files can be renamed within an encrypted directory, or
  970. into an unencrypted directory.
  971. Note: "moving" an unencrypted file into an encrypted directory, e.g.
  972. with the `mv` program, is implemented in userspace by a copy
  973. followed by a delete. Be aware that the original unencrypted data
  974. may remain recoverable from free space on the disk; prefer to keep
  975. all files encrypted from the very beginning. The `shred` program
  976. may be used to overwrite the source files but isn't guaranteed to be
  977. effective on all filesystems and storage devices.
  978. - Direct I/O is supported on encrypted files only under some
  979. circumstances. For details, see `Direct I/O support`_.
  980. - The fallocate operations FALLOC_FL_COLLAPSE_RANGE and
  981. FALLOC_FL_INSERT_RANGE are not supported on encrypted files and will
  982. fail with EOPNOTSUPP.
  983. - Online defragmentation of encrypted files is not supported. The
  984. EXT4_IOC_MOVE_EXT and F2FS_IOC_MOVE_RANGE ioctls will fail with
  985. EOPNOTSUPP.
  986. - The ext4 filesystem does not support data journaling with encrypted
  987. regular files. It will fall back to ordered data mode instead.
  988. - DAX (Direct Access) is not supported on encrypted files.
  989. - The maximum length of an encrypted symlink is 2 bytes shorter than
  990. the maximum length of an unencrypted symlink. For example, on an
  991. EXT4 filesystem with a 4K block size, unencrypted symlinks can be up
  992. to 4095 bytes long, while encrypted symlinks can only be up to 4093
  993. bytes long (both lengths excluding the terminating null).
  994. Note that mmap *is* supported. This is possible because the pagecache
  995. for an encrypted file contains the plaintext, not the ciphertext.
  996. Without the key
  997. ---------------
  998. Some filesystem operations may be performed on encrypted regular
  999. files, directories, and symlinks even before their encryption key has
  1000. been added, or after their encryption key has been removed:
  1001. - File metadata may be read, e.g. using stat().
  1002. - Directories may be listed, in which case the filenames will be
  1003. listed in an encoded form derived from their ciphertext. The
  1004. current encoding algorithm is described in `Filename hashing and
  1005. encoding`_. The algorithm is subject to change, but it is
  1006. guaranteed that the presented filenames will be no longer than
  1007. NAME_MAX bytes, will not contain the ``/`` or ``\0`` characters, and
  1008. will uniquely identify directory entries.
  1009. The ``.`` and ``..`` directory entries are special. They are always
  1010. present and are not encrypted or encoded.
  1011. - Files may be deleted. That is, nondirectory files may be deleted
  1012. with unlink() as usual, and empty directories may be deleted with
  1013. rmdir() as usual. Therefore, ``rm`` and ``rm -r`` will work as
  1014. expected.
  1015. - Symlink targets may be read and followed, but they will be presented
  1016. in encrypted form, similar to filenames in directories. Hence, they
  1017. are unlikely to point to anywhere useful.
  1018. Without the key, regular files cannot be opened or truncated.
  1019. Attempts to do so will fail with ENOKEY. This implies that any
  1020. regular file operations that require a file descriptor, such as
  1021. read(), write(), mmap(), fallocate(), and ioctl(), are also forbidden.
  1022. Also without the key, files of any type (including directories) cannot
  1023. be created or linked into an encrypted directory, nor can a name in an
  1024. encrypted directory be the source or target of a rename, nor can an
  1025. O_TMPFILE temporary file be created in an encrypted directory. All
  1026. such operations will fail with ENOKEY.
  1027. It is not currently possible to backup and restore encrypted files
  1028. without the encryption key. This would require special APIs which
  1029. have not yet been implemented.
  1030. Encryption policy enforcement
  1031. =============================
  1032. After an encryption policy has been set on a directory, all regular
  1033. files, directories, and symbolic links created in that directory
  1034. (recursively) will inherit that encryption policy. Special files ---
  1035. that is, named pipes, device nodes, and UNIX domain sockets --- will
  1036. not be encrypted.
  1037. Except for those special files, it is forbidden to have unencrypted
  1038. files, or files encrypted with a different encryption policy, in an
  1039. encrypted directory tree. Attempts to link or rename such a file into
  1040. an encrypted directory will fail with EXDEV. This is also enforced
  1041. during ->lookup() to provide limited protection against offline
  1042. attacks that try to disable or downgrade encryption in known locations
  1043. where applications may later write sensitive data. It is recommended
  1044. that systems implementing a form of "verified boot" take advantage of
  1045. this by validating all top-level encryption policies prior to access.
  1046. Inline encryption support
  1047. =========================
  1048. Many newer systems (especially mobile SoCs) have *inline encryption
  1049. hardware* that can encrypt/decrypt data while it is on its way to/from
  1050. the storage device. Linux supports inline encryption through a set of
  1051. extensions to the block layer called *blk-crypto*. blk-crypto allows
  1052. filesystems to attach encryption contexts to bios (I/O requests) to
  1053. specify how the data will be encrypted or decrypted in-line. For more
  1054. information about blk-crypto, see
  1055. :ref:`Documentation/block/inline-encryption.rst <inline_encryption>`.
  1056. On supported filesystems (currently ext4 and f2fs), fscrypt can use
  1057. blk-crypto instead of the kernel crypto API to encrypt/decrypt file
  1058. contents. To enable this, set CONFIG_FS_ENCRYPTION_INLINE_CRYPT=y in
  1059. the kernel configuration, and specify the "inlinecrypt" mount option
  1060. when mounting the filesystem.
  1061. Note that the "inlinecrypt" mount option just specifies to use inline
  1062. encryption when possible; it doesn't force its use. fscrypt will
  1063. still fall back to using the kernel crypto API on files where the
  1064. inline encryption hardware doesn't have the needed crypto capabilities
  1065. (e.g. support for the needed encryption algorithm and data unit size)
  1066. and where blk-crypto-fallback is unusable. (For blk-crypto-fallback
  1067. to be usable, it must be enabled in the kernel configuration with
  1068. CONFIG_BLK_INLINE_ENCRYPTION_FALLBACK=y.)
  1069. Currently fscrypt always uses the filesystem block size (which is
  1070. usually 4096 bytes) as the data unit size. Therefore, it can only use
  1071. inline encryption hardware that supports that data unit size.
  1072. Inline encryption doesn't affect the ciphertext or other aspects of
  1073. the on-disk format, so users may freely switch back and forth between
  1074. using "inlinecrypt" and not using "inlinecrypt".
  1075. Direct I/O support
  1076. ==================
  1077. For direct I/O on an encrypted file to work, the following conditions
  1078. must be met (in addition to the conditions for direct I/O on an
  1079. unencrypted file):
  1080. * The file must be using inline encryption. Usually this means that
  1081. the filesystem must be mounted with ``-o inlinecrypt`` and inline
  1082. encryption hardware must be present. However, a software fallback
  1083. is also available. For details, see `Inline encryption support`_.
  1084. * The I/O request must be fully aligned to the filesystem block size.
  1085. This means that the file position the I/O is targeting, the lengths
  1086. of all I/O segments, and the memory addresses of all I/O buffers
  1087. must be multiples of this value. Note that the filesystem block
  1088. size may be greater than the logical block size of the block device.
  1089. If either of the above conditions is not met, then direct I/O on the
  1090. encrypted file will fall back to buffered I/O.
  1091. Implementation details
  1092. ======================
  1093. Encryption context
  1094. ------------------
  1095. An encryption policy is represented on-disk by
  1096. struct fscrypt_context_v1 or struct fscrypt_context_v2. It is up to
  1097. individual filesystems to decide where to store it, but normally it
  1098. would be stored in a hidden extended attribute. It should *not* be
  1099. exposed by the xattr-related system calls such as getxattr() and
  1100. setxattr() because of the special semantics of the encryption xattr.
  1101. (In particular, there would be much confusion if an encryption policy
  1102. were to be added to or removed from anything other than an empty
  1103. directory.) These structs are defined as follows::
  1104. #define FSCRYPT_FILE_NONCE_SIZE 16
  1105. #define FSCRYPT_KEY_DESCRIPTOR_SIZE 8
  1106. struct fscrypt_context_v1 {
  1107. u8 version;
  1108. u8 contents_encryption_mode;
  1109. u8 filenames_encryption_mode;
  1110. u8 flags;
  1111. u8 master_key_descriptor[FSCRYPT_KEY_DESCRIPTOR_SIZE];
  1112. u8 nonce[FSCRYPT_FILE_NONCE_SIZE];
  1113. };
  1114. #define FSCRYPT_KEY_IDENTIFIER_SIZE 16
  1115. struct fscrypt_context_v2 {
  1116. u8 version;
  1117. u8 contents_encryption_mode;
  1118. u8 filenames_encryption_mode;
  1119. u8 flags;
  1120. u8 log2_data_unit_size;
  1121. u8 __reserved[3];
  1122. u8 master_key_identifier[FSCRYPT_KEY_IDENTIFIER_SIZE];
  1123. u8 nonce[FSCRYPT_FILE_NONCE_SIZE];
  1124. };
  1125. The context structs contain the same information as the corresponding
  1126. policy structs (see `Setting an encryption policy`_), except that the
  1127. context structs also contain a nonce. The nonce is randomly generated
  1128. by the kernel and is used as KDF input or as a tweak to cause
  1129. different files to be encrypted differently; see `Per-file encryption
  1130. keys`_ and `DIRECT_KEY policies`_.
  1131. Data path changes
  1132. -----------------
  1133. When inline encryption is used, filesystems just need to associate
  1134. encryption contexts with bios to specify how the block layer or the
  1135. inline encryption hardware will encrypt/decrypt the file contents.
  1136. When inline encryption isn't used, filesystems must encrypt/decrypt
  1137. the file contents themselves, as described below:
  1138. For the read path (->read_folio()) of regular files, filesystems can
  1139. read the ciphertext into the page cache and decrypt it in-place. The
  1140. folio lock must be held until decryption has finished, to prevent the
  1141. folio from becoming visible to userspace prematurely.
  1142. For the write path (->writepage()) of regular files, filesystems
  1143. cannot encrypt data in-place in the page cache, since the cached
  1144. plaintext must be preserved. Instead, filesystems must encrypt into a
  1145. temporary buffer or "bounce page", then write out the temporary
  1146. buffer. Some filesystems, such as UBIFS, already use temporary
  1147. buffers regardless of encryption. Other filesystems, such as ext4 and
  1148. F2FS, have to allocate bounce pages specially for encryption.
  1149. Filename hashing and encoding
  1150. -----------------------------
  1151. Modern filesystems accelerate directory lookups by using indexed
  1152. directories. An indexed directory is organized as a tree keyed by
  1153. filename hashes. When a ->lookup() is requested, the filesystem
  1154. normally hashes the filename being looked up so that it can quickly
  1155. find the corresponding directory entry, if any.
  1156. With encryption, lookups must be supported and efficient both with and
  1157. without the encryption key. Clearly, it would not work to hash the
  1158. plaintext filenames, since the plaintext filenames are unavailable
  1159. without the key. (Hashing the plaintext filenames would also make it
  1160. impossible for the filesystem's fsck tool to optimize encrypted
  1161. directories.) Instead, filesystems hash the ciphertext filenames,
  1162. i.e. the bytes actually stored on-disk in the directory entries. When
  1163. asked to do a ->lookup() with the key, the filesystem just encrypts
  1164. the user-supplied name to get the ciphertext.
  1165. Lookups without the key are more complicated. The raw ciphertext may
  1166. contain the ``\0`` and ``/`` characters, which are illegal in
  1167. filenames. Therefore, readdir() must base64url-encode the ciphertext
  1168. for presentation. For most filenames, this works fine; on ->lookup(),
  1169. the filesystem just base64url-decodes the user-supplied name to get
  1170. back to the raw ciphertext.
  1171. However, for very long filenames, base64url encoding would cause the
  1172. filename length to exceed NAME_MAX. To prevent this, readdir()
  1173. actually presents long filenames in an abbreviated form which encodes
  1174. a strong "hash" of the ciphertext filename, along with the optional
  1175. filesystem-specific hash(es) needed for directory lookups. This
  1176. allows the filesystem to still, with a high degree of confidence, map
  1177. the filename given in ->lookup() back to a particular directory entry
  1178. that was previously listed by readdir(). See
  1179. struct fscrypt_nokey_name in the source for more details.
  1180. Note that the precise way that filenames are presented to userspace
  1181. without the key is subject to change in the future. It is only meant
  1182. as a way to temporarily present valid filenames so that commands like
  1183. ``rm -r`` work as expected on encrypted directories.
  1184. Tests
  1185. =====
  1186. To test fscrypt, use xfstests, which is Linux's de facto standard
  1187. filesystem test suite. First, run all the tests in the "encrypt"
  1188. group on the relevant filesystem(s). One can also run the tests
  1189. with the 'inlinecrypt' mount option to test the implementation for
  1190. inline encryption support. For example, to test ext4 and
  1191. f2fs encryption using `kvm-xfstests
  1192. <https://github.com/tytso/xfstests-bld/blob/master/Documentation/kvm-quickstart.md>`_::
  1193. kvm-xfstests -c ext4,f2fs -g encrypt
  1194. kvm-xfstests -c ext4,f2fs -g encrypt -m inlinecrypt
  1195. UBIFS encryption can also be tested this way, but it should be done in
  1196. a separate command, and it takes some time for kvm-xfstests to set up
  1197. emulated UBI volumes::
  1198. kvm-xfstests -c ubifs -g encrypt
  1199. No tests should fail. However, tests that use non-default encryption
  1200. modes (e.g. generic/549 and generic/550) will be skipped if the needed
  1201. algorithms were not built into the kernel's crypto API. Also, tests
  1202. that access the raw block device (e.g. generic/399, generic/548,
  1203. generic/549, generic/550) will be skipped on UBIFS.
  1204. Besides running the "encrypt" group tests, for ext4 and f2fs it's also
  1205. possible to run most xfstests with the "test_dummy_encryption" mount
  1206. option. This option causes all new files to be automatically
  1207. encrypted with a dummy key, without having to make any API calls.
  1208. This tests the encrypted I/O paths more thoroughly. To do this with
  1209. kvm-xfstests, use the "encrypt" filesystem configuration::
  1210. kvm-xfstests -c ext4/encrypt,f2fs/encrypt -g auto
  1211. kvm-xfstests -c ext4/encrypt,f2fs/encrypt -g auto -m inlinecrypt
  1212. Because this runs many more tests than "-g encrypt" does, it takes
  1213. much longer to run; so also consider using `gce-xfstests
  1214. <https://github.com/tytso/xfstests-bld/blob/master/Documentation/gce-xfstests.md>`_
  1215. instead of kvm-xfstests::
  1216. gce-xfstests -c ext4/encrypt,f2fs/encrypt -g auto
  1217. gce-xfstests -c ext4/encrypt,f2fs/encrypt -g auto -m inlinecrypt