fscrypt.rst 67 KB

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