fscrypt.rst 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  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, and
  30. UBIFS. This allows encrypted files to be read and written without
  31. caching both the decrypted and encrypted pages in the pagecache,
  32. thereby nearly halving the memory used and bringing it in line with
  33. unencrypted files. Similarly, half as many dentries and inodes are
  34. needed. eCryptfs also limits encrypted filenames to 143 bytes,
  35. causing application compatibility issues; fscrypt allows the full 255
  36. bytes (NAME_MAX). Finally, unlike eCryptfs, the fscrypt API can be
  37. 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. fscrypt is only resistant to side-channel attacks, such as timing or
  63. electromagnetic attacks, to the extent that the underlying Linux
  64. Cryptographic API algorithms are. If a vulnerable algorithm is used,
  65. such as a table-based implementation of AES, it may be possible for an
  66. attacker to mount a side channel attack against the online system.
  67. Side channel attacks may also be mounted against applications
  68. consuming decrypted data.
  69. After an encryption key has been provided, fscrypt is not designed to
  70. hide the plaintext file contents or filenames from other users on the
  71. same system, regardless of the visibility of the keyring key.
  72. Instead, existing access control mechanisms such as file mode bits,
  73. POSIX ACLs, LSMs, or mount namespaces should be used for this purpose.
  74. Also note that as long as the encryption keys are *anywhere* in
  75. memory, an online attacker can necessarily compromise them by mounting
  76. a physical attack or by exploiting any kernel security vulnerability
  77. which provides an arbitrary memory read primitive.
  78. While it is ostensibly possible to "evict" keys from the system,
  79. recently accessed encrypted files will remain accessible at least
  80. until the filesystem is unmounted or the VFS caches are dropped, e.g.
  81. using ``echo 2 > /proc/sys/vm/drop_caches``. Even after that, if the
  82. RAM is compromised before being powered off, it will likely still be
  83. possible to recover portions of the plaintext file contents, if not
  84. some of the encryption keys as well. (Since Linux v4.12, all
  85. in-kernel keys related to fscrypt are sanitized before being freed.
  86. However, userspace would need to do its part as well.)
  87. Currently, fscrypt does not prevent a user from maliciously providing
  88. an incorrect key for another user's existing encrypted files. A
  89. protection against this is planned.
  90. Key hierarchy
  91. =============
  92. Master Keys
  93. -----------
  94. Each encrypted directory tree is protected by a *master key*. Master
  95. keys can be up to 64 bytes long, and must be at least as long as the
  96. greater of the key length needed by the contents and filenames
  97. encryption modes being used. For example, if AES-256-XTS is used for
  98. contents encryption, the master key must be 64 bytes (512 bits). Note
  99. that the XTS mode is defined to require a key twice as long as that
  100. required by the underlying block cipher.
  101. To "unlock" an encrypted directory tree, userspace must provide the
  102. appropriate master key. There can be any number of master keys, each
  103. of which protects any number of directory trees on any number of
  104. filesystems.
  105. Userspace should generate master keys either using a cryptographically
  106. secure random number generator, or by using a KDF (Key Derivation
  107. Function). Note that whenever a KDF is used to "stretch" a
  108. lower-entropy secret such as a passphrase, it is critical that a KDF
  109. designed for this purpose be used, such as scrypt, PBKDF2, or Argon2.
  110. Per-file keys
  111. -------------
  112. Since each master key can protect many files, it is necessary to
  113. "tweak" the encryption of each file so that the same plaintext in two
  114. files doesn't map to the same ciphertext, or vice versa. In most
  115. cases, fscrypt does this by deriving per-file keys. When a new
  116. encrypted inode (regular file, directory, or symlink) is created,
  117. fscrypt randomly generates a 16-byte nonce and stores it in the
  118. inode's encryption xattr. Then, it uses a KDF (Key Derivation
  119. Function) to derive the file's key from the master key and nonce.
  120. The Adiantum encryption mode (see `Encryption modes and usage`_) is
  121. special, since it accepts longer IVs and is suitable for both contents
  122. and filenames encryption. For it, a "direct key" option is offered
  123. where the file's nonce is included in the IVs and the master key is
  124. used for encryption directly. This improves performance; however,
  125. users must not use the same master key for any other encryption mode.
  126. Below, the KDF and design considerations are described in more detail.
  127. The current KDF works by encrypting the master key with AES-128-ECB,
  128. using the file's nonce as the AES key. The output is used as the
  129. derived key. If the output is longer than needed, then it is
  130. truncated to the needed length.
  131. Note: this KDF meets the primary security requirement, which is to
  132. produce unique derived keys that preserve the entropy of the master
  133. key, assuming that the master key is already a good pseudorandom key.
  134. However, it is nonstandard and has some problems such as being
  135. reversible, so it is generally considered to be a mistake! It may be
  136. replaced with HKDF or another more standard KDF in the future.
  137. Key derivation was chosen over key wrapping because wrapped keys would
  138. require larger xattrs which would be less likely to fit in-line in the
  139. filesystem's inode table, and there didn't appear to be any
  140. significant advantages to key wrapping. In particular, currently
  141. there is no requirement to support unlocking a file with multiple
  142. alternative master keys or to support rotating master keys. Instead,
  143. the master keys may be wrapped in userspace, e.g. as is done by the
  144. `fscrypt <https://github.com/google/fscrypt>`_ tool.
  145. Including the inode number in the IVs was considered. However, it was
  146. rejected as it would have prevented ext4 filesystems from being
  147. resized, and by itself still wouldn't have been sufficient to prevent
  148. the same key from being directly reused for both XTS and CTS-CBC.
  149. Encryption modes and usage
  150. ==========================
  151. fscrypt allows one encryption mode to be specified for file contents
  152. and one encryption mode to be specified for filenames. Different
  153. directory trees are permitted to use different encryption modes.
  154. Currently, the following pairs of encryption modes are supported:
  155. - AES-256-XTS for contents and AES-256-CTS-CBC for filenames
  156. - AES-128-CBC for contents and AES-128-CTS-CBC for filenames
  157. - Adiantum for both contents and filenames
  158. If unsure, you should use the (AES-256-XTS, AES-256-CTS-CBC) pair.
  159. AES-128-CBC was added only for low-powered embedded devices with
  160. crypto accelerators such as CAAM or CESA that do not support XTS.
  161. Adiantum is a (primarily) stream cipher-based mode that is fast even
  162. on CPUs without dedicated crypto instructions. It's also a true
  163. wide-block mode, unlike XTS. It can also eliminate the need to derive
  164. per-file keys. However, it depends on the security of two primitives,
  165. XChaCha12 and AES-256, rather than just one. See the paper
  166. "Adiantum: length-preserving encryption for entry-level processors"
  167. (https://eprint.iacr.org/2018/720.pdf) for more details. To use
  168. Adiantum, CONFIG_CRYPTO_ADIANTUM must be enabled. Also, fast
  169. implementations of ChaCha and NHPoly1305 should be enabled, e.g.
  170. CONFIG_CRYPTO_CHACHA20_NEON and CONFIG_CRYPTO_NHPOLY1305_NEON for ARM.
  171. New encryption modes can be added relatively easily, without changes
  172. to individual filesystems. However, authenticated encryption (AE)
  173. modes are not currently supported because of the difficulty of dealing
  174. with ciphertext expansion.
  175. Contents encryption
  176. -------------------
  177. For file contents, each filesystem block is encrypted independently.
  178. Currently, only the case where the filesystem block size is equal to
  179. the system's page size (usually 4096 bytes) is supported.
  180. Each block's IV is set to the logical block number within the file as
  181. a little endian number, except that:
  182. - With CBC mode encryption, ESSIV is also used. Specifically, each IV
  183. is encrypted with AES-256 where the AES-256 key is the SHA-256 hash
  184. of the file's data encryption key.
  185. - In the "direct key" configuration (FS_POLICY_FLAG_DIRECT_KEY set in
  186. the fscrypt_policy), the file's nonce is also appended to the IV.
  187. Currently this is only allowed with the Adiantum encryption mode.
  188. Filenames encryption
  189. --------------------
  190. For filenames, each full filename is encrypted at once. Because of
  191. the requirements to retain support for efficient directory lookups and
  192. filenames of up to 255 bytes, the same IV is used for every filename
  193. in a directory.
  194. However, each encrypted directory still uses a unique key; or
  195. alternatively (for the "direct key" configuration) has the file's
  196. nonce included in the IVs. Thus, IV reuse is limited to within a
  197. single directory.
  198. With CTS-CBC, the IV reuse means that when the plaintext filenames
  199. share a common prefix at least as long as the cipher block size (16
  200. bytes for AES), the corresponding encrypted filenames will also share
  201. a common prefix. This is undesirable. Adiantum does not have this
  202. weakness, as it is a wide-block encryption mode.
  203. All supported filenames encryption modes accept any plaintext length
  204. >= 16 bytes; cipher block alignment is not required. However,
  205. filenames shorter than 16 bytes are NUL-padded to 16 bytes before
  206. being encrypted. In addition, to reduce leakage of filename lengths
  207. via their ciphertexts, all filenames are NUL-padded to the next 4, 8,
  208. 16, or 32-byte boundary (configurable). 32 is recommended since this
  209. provides the best confidentiality, at the cost of making directory
  210. entries consume slightly more space. Note that since NUL (``\0``) is
  211. not otherwise a valid character in filenames, the padding will never
  212. produce duplicate plaintexts.
  213. Symbolic link targets are considered a type of filename and are
  214. encrypted in the same way as filenames in directory entries, except
  215. that IV reuse is not a problem as each symlink has its own inode.
  216. User API
  217. ========
  218. Setting an encryption policy
  219. ----------------------------
  220. The FS_IOC_SET_ENCRYPTION_POLICY ioctl sets an encryption policy on an
  221. empty directory or verifies that a directory or regular file already
  222. has the specified encryption policy. It takes in a pointer to a
  223. :c:type:`struct fscrypt_policy`, defined as follows::
  224. #define FS_KEY_DESCRIPTOR_SIZE 8
  225. struct fscrypt_policy {
  226. __u8 version;
  227. __u8 contents_encryption_mode;
  228. __u8 filenames_encryption_mode;
  229. __u8 flags;
  230. __u8 master_key_descriptor[FS_KEY_DESCRIPTOR_SIZE];
  231. };
  232. This structure must be initialized as follows:
  233. - ``version`` must be 0.
  234. - ``contents_encryption_mode`` and ``filenames_encryption_mode`` must
  235. be set to constants from ``<linux/fs.h>`` which identify the
  236. encryption modes to use. If unsure, use
  237. FS_ENCRYPTION_MODE_AES_256_XTS (1) for ``contents_encryption_mode``
  238. and FS_ENCRYPTION_MODE_AES_256_CTS (4) for
  239. ``filenames_encryption_mode``.
  240. - ``flags`` must contain a value from ``<linux/fs.h>`` which
  241. identifies the amount of NUL-padding to use when encrypting
  242. filenames. If unsure, use FS_POLICY_FLAGS_PAD_32 (0x3).
  243. In addition, if the chosen encryption modes are both
  244. FS_ENCRYPTION_MODE_ADIANTUM, this can contain
  245. FS_POLICY_FLAG_DIRECT_KEY to specify that the master key should be
  246. used directly, without key derivation.
  247. - ``master_key_descriptor`` specifies how to find the master key in
  248. the keyring; see `Adding keys`_. It is up to userspace to choose a
  249. unique ``master_key_descriptor`` for each master key. The e4crypt
  250. and fscrypt tools use the first 8 bytes of
  251. ``SHA-512(SHA-512(master_key))``, but this particular scheme is not
  252. required. Also, the master key need not be in the keyring yet when
  253. FS_IOC_SET_ENCRYPTION_POLICY is executed. However, it must be added
  254. before any files can be created in the encrypted directory.
  255. If the file is not yet encrypted, then FS_IOC_SET_ENCRYPTION_POLICY
  256. verifies that the file is an empty directory. If so, the specified
  257. encryption policy is assigned to the directory, turning it into an
  258. encrypted directory. After that, and after providing the
  259. corresponding master key as described in `Adding keys`_, all regular
  260. files, directories (recursively), and symlinks created in the
  261. directory will be encrypted, inheriting the same encryption policy.
  262. The filenames in the directory's entries will be encrypted as well.
  263. Alternatively, if the file is already encrypted, then
  264. FS_IOC_SET_ENCRYPTION_POLICY validates that the specified encryption
  265. policy exactly matches the actual one. If they match, then the ioctl
  266. returns 0. Otherwise, it fails with EEXIST. This works on both
  267. regular files and directories, including nonempty directories.
  268. Note that the ext4 filesystem does not allow the root directory to be
  269. encrypted, even if it is empty. Users who want to encrypt an entire
  270. filesystem with one key should consider using dm-crypt instead.
  271. FS_IOC_SET_ENCRYPTION_POLICY can fail with the following errors:
  272. - ``EACCES``: the file is not owned by the process's uid, nor does the
  273. process have the CAP_FOWNER capability in a namespace with the file
  274. owner's uid mapped
  275. - ``EEXIST``: the file is already encrypted with an encryption policy
  276. different from the one specified
  277. - ``EINVAL``: an invalid encryption policy was specified (invalid
  278. version, mode(s), or flags)
  279. - ``ENOTDIR``: the file is unencrypted and is a regular file, not a
  280. directory
  281. - ``ENOTEMPTY``: the file is unencrypted and is a nonempty directory
  282. - ``ENOTTY``: this type of filesystem does not implement encryption
  283. - ``EOPNOTSUPP``: the kernel was not configured with encryption
  284. support for filesystems, or the filesystem superblock has not
  285. had encryption enabled on it. (For example, to use encryption on an
  286. ext4 filesystem, CONFIG_FS_ENCRYPTION must be enabled in the
  287. kernel config, and the superblock must have had the "encrypt"
  288. feature flag enabled using ``tune2fs -O encrypt`` or ``mkfs.ext4 -O
  289. encrypt``.)
  290. - ``EPERM``: this directory may not be encrypted, e.g. because it is
  291. the root directory of an ext4 filesystem
  292. - ``EROFS``: the filesystem is readonly
  293. Getting an encryption policy
  294. ----------------------------
  295. The FS_IOC_GET_ENCRYPTION_POLICY ioctl retrieves the :c:type:`struct
  296. fscrypt_policy`, if any, for a directory or regular file. See above
  297. for the struct definition. No additional permissions are required
  298. beyond the ability to open the file.
  299. FS_IOC_GET_ENCRYPTION_POLICY can fail with the following errors:
  300. - ``EINVAL``: the file is encrypted, but it uses an unrecognized
  301. encryption context format
  302. - ``ENODATA``: the file is not encrypted
  303. - ``ENOTTY``: this type of filesystem does not implement encryption
  304. - ``EOPNOTSUPP``: the kernel was not configured with encryption
  305. support for this filesystem
  306. Note: if you only need to know whether a file is encrypted or not, on
  307. most filesystems it is also possible to use the FS_IOC_GETFLAGS ioctl
  308. and check for FS_ENCRYPT_FL, or to use the statx() system call and
  309. check for STATX_ATTR_ENCRYPTED in stx_attributes.
  310. Getting the per-filesystem salt
  311. -------------------------------
  312. Some filesystems, such as ext4 and F2FS, also support the deprecated
  313. ioctl FS_IOC_GET_ENCRYPTION_PWSALT. This ioctl retrieves a randomly
  314. generated 16-byte value stored in the filesystem superblock. This
  315. value is intended to used as a salt when deriving an encryption key
  316. from a passphrase or other low-entropy user credential.
  317. FS_IOC_GET_ENCRYPTION_PWSALT is deprecated. Instead, prefer to
  318. generate and manage any needed salt(s) in userspace.
  319. Adding keys
  320. -----------
  321. To provide a master key, userspace must add it to an appropriate
  322. keyring using the add_key() system call (see:
  323. ``Documentation/security/keys/core.rst``). The key type must be
  324. "logon"; keys of this type are kept in kernel memory and cannot be
  325. read back by userspace. The key description must be "fscrypt:"
  326. followed by the 16-character lower case hex representation of the
  327. ``master_key_descriptor`` that was set in the encryption policy. The
  328. key payload must conform to the following structure::
  329. #define FS_MAX_KEY_SIZE 64
  330. struct fscrypt_key {
  331. u32 mode;
  332. u8 raw[FS_MAX_KEY_SIZE];
  333. u32 size;
  334. };
  335. ``mode`` is ignored; just set it to 0. The actual key is provided in
  336. ``raw`` with ``size`` indicating its size in bytes. That is, the
  337. bytes ``raw[0..size-1]`` (inclusive) are the actual key.
  338. The key description prefix "fscrypt:" may alternatively be replaced
  339. with a filesystem-specific prefix such as "ext4:". However, the
  340. filesystem-specific prefixes are deprecated and should not be used in
  341. new programs.
  342. There are several different types of keyrings in which encryption keys
  343. may be placed, such as a session keyring, a user session keyring, or a
  344. user keyring. Each key must be placed in a keyring that is "attached"
  345. to all processes that might need to access files encrypted with it, in
  346. the sense that request_key() will find the key. Generally, if only
  347. processes belonging to a specific user need to access a given
  348. encrypted directory and no session keyring has been installed, then
  349. that directory's key should be placed in that user's user session
  350. keyring or user keyring. Otherwise, a session keyring should be
  351. installed if needed, and the key should be linked into that session
  352. keyring, or in a keyring linked into that session keyring.
  353. Note: introducing the complex visibility semantics of keyrings here
  354. was arguably a mistake --- especially given that by design, after any
  355. process successfully opens an encrypted file (thereby setting up the
  356. per-file key), possessing the keyring key is not actually required for
  357. any process to read/write the file until its in-memory inode is
  358. evicted. In the future there probably should be a way to provide keys
  359. directly to the filesystem instead, which would make the intended
  360. semantics clearer.
  361. Access semantics
  362. ================
  363. With the key
  364. ------------
  365. With the encryption key, encrypted regular files, directories, and
  366. symlinks behave very similarly to their unencrypted counterparts ---
  367. after all, the encryption is intended to be transparent. However,
  368. astute users may notice some differences in behavior:
  369. - Unencrypted files, or files encrypted with a different encryption
  370. policy (i.e. different key, modes, or flags), cannot be renamed or
  371. linked into an encrypted directory; see `Encryption policy
  372. enforcement`_. Attempts to do so will fail with EXDEV. However,
  373. encrypted files can be renamed within an encrypted directory, or
  374. into an unencrypted directory.
  375. Note: "moving" an unencrypted file into an encrypted directory, e.g.
  376. with the `mv` program, is implemented in userspace by a copy
  377. followed by a delete. Be aware that the original unencrypted data
  378. may remain recoverable from free space on the disk; prefer to keep
  379. all files encrypted from the very beginning. The `shred` program
  380. may be used to overwrite the source files but isn't guaranteed to be
  381. effective on all filesystems and storage devices.
  382. - Direct I/O is not supported on encrypted files. Attempts to use
  383. direct I/O on such files will fall back to buffered I/O.
  384. - The fallocate operations FALLOC_FL_COLLAPSE_RANGE,
  385. FALLOC_FL_INSERT_RANGE, and FALLOC_FL_ZERO_RANGE are not supported
  386. on encrypted files and will fail with EOPNOTSUPP.
  387. - Online defragmentation of encrypted files is not supported. The
  388. EXT4_IOC_MOVE_EXT and F2FS_IOC_MOVE_RANGE ioctls will fail with
  389. EOPNOTSUPP.
  390. - The ext4 filesystem does not support data journaling with encrypted
  391. regular files. It will fall back to ordered data mode instead.
  392. - DAX (Direct Access) is not supported on encrypted files.
  393. - The st_size of an encrypted symlink will not necessarily give the
  394. length of the symlink target as required by POSIX. It will actually
  395. give the length of the ciphertext, which will be slightly longer
  396. than the plaintext due to NUL-padding and an extra 2-byte overhead.
  397. - The maximum length of an encrypted symlink is 2 bytes shorter than
  398. the maximum length of an unencrypted symlink. For example, on an
  399. EXT4 filesystem with a 4K block size, unencrypted symlinks can be up
  400. to 4095 bytes long, while encrypted symlinks can only be up to 4093
  401. bytes long (both lengths excluding the terminating null).
  402. Note that mmap *is* supported. This is possible because the pagecache
  403. for an encrypted file contains the plaintext, not the ciphertext.
  404. Without the key
  405. ---------------
  406. Some filesystem operations may be performed on encrypted regular
  407. files, directories, and symlinks even before their encryption key has
  408. been provided:
  409. - File metadata may be read, e.g. using stat().
  410. - Directories may be listed, in which case the filenames will be
  411. listed in an encoded form derived from their ciphertext. The
  412. current encoding algorithm is described in `Filename hashing and
  413. encoding`_. The algorithm is subject to change, but it is
  414. guaranteed that the presented filenames will be no longer than
  415. NAME_MAX bytes, will not contain the ``/`` or ``\0`` characters, and
  416. will uniquely identify directory entries.
  417. The ``.`` and ``..`` directory entries are special. They are always
  418. present and are not encrypted or encoded.
  419. - Files may be deleted. That is, nondirectory files may be deleted
  420. with unlink() as usual, and empty directories may be deleted with
  421. rmdir() as usual. Therefore, ``rm`` and ``rm -r`` will work as
  422. expected.
  423. - Symlink targets may be read and followed, but they will be presented
  424. in encrypted form, similar to filenames in directories. Hence, they
  425. are unlikely to point to anywhere useful.
  426. Without the key, regular files cannot be opened or truncated.
  427. Attempts to do so will fail with ENOKEY. This implies that any
  428. regular file operations that require a file descriptor, such as
  429. read(), write(), mmap(), fallocate(), and ioctl(), are also forbidden.
  430. Also without the key, files of any type (including directories) cannot
  431. be created or linked into an encrypted directory, nor can a name in an
  432. encrypted directory be the source or target of a rename, nor can an
  433. O_TMPFILE temporary file be created in an encrypted directory. All
  434. such operations will fail with ENOKEY.
  435. It is not currently possible to backup and restore encrypted files
  436. without the encryption key. This would require special APIs which
  437. have not yet been implemented.
  438. Encryption policy enforcement
  439. =============================
  440. After an encryption policy has been set on a directory, all regular
  441. files, directories, and symbolic links created in that directory
  442. (recursively) will inherit that encryption policy. Special files ---
  443. that is, named pipes, device nodes, and UNIX domain sockets --- will
  444. not be encrypted.
  445. Except for those special files, it is forbidden to have unencrypted
  446. files, or files encrypted with a different encryption policy, in an
  447. encrypted directory tree. Attempts to link or rename such a file into
  448. an encrypted directory will fail with EXDEV. This is also enforced
  449. during ->lookup() to provide limited protection against offline
  450. attacks that try to disable or downgrade encryption in known locations
  451. where applications may later write sensitive data. It is recommended
  452. that systems implementing a form of "verified boot" take advantage of
  453. this by validating all top-level encryption policies prior to access.
  454. Implementation details
  455. ======================
  456. Encryption context
  457. ------------------
  458. An encryption policy is represented on-disk by a :c:type:`struct
  459. fscrypt_context`. It is up to individual filesystems to decide where
  460. to store it, but normally it would be stored in a hidden extended
  461. attribute. It should *not* be exposed by the xattr-related system
  462. calls such as getxattr() and setxattr() because of the special
  463. semantics of the encryption xattr. (In particular, there would be
  464. much confusion if an encryption policy were to be added to or removed
  465. from anything other than an empty directory.) The struct is defined
  466. as follows::
  467. #define FS_KEY_DESCRIPTOR_SIZE 8
  468. #define FS_KEY_DERIVATION_NONCE_SIZE 16
  469. struct fscrypt_context {
  470. u8 format;
  471. u8 contents_encryption_mode;
  472. u8 filenames_encryption_mode;
  473. u8 flags;
  474. u8 master_key_descriptor[FS_KEY_DESCRIPTOR_SIZE];
  475. u8 nonce[FS_KEY_DERIVATION_NONCE_SIZE];
  476. };
  477. Note that :c:type:`struct fscrypt_context` contains the same
  478. information as :c:type:`struct fscrypt_policy` (see `Setting an
  479. encryption policy`_), except that :c:type:`struct fscrypt_context`
  480. also contains a nonce. The nonce is randomly generated by the kernel
  481. and is used to derive the inode's encryption key as described in
  482. `Per-file keys`_.
  483. Data path changes
  484. -----------------
  485. For the read path (->readpage()) of regular files, filesystems can
  486. read the ciphertext into the page cache and decrypt it in-place. The
  487. page lock must be held until decryption has finished, to prevent the
  488. page from becoming visible to userspace prematurely.
  489. For the write path (->writepage()) of regular files, filesystems
  490. cannot encrypt data in-place in the page cache, since the cached
  491. plaintext must be preserved. Instead, filesystems must encrypt into a
  492. temporary buffer or "bounce page", then write out the temporary
  493. buffer. Some filesystems, such as UBIFS, already use temporary
  494. buffers regardless of encryption. Other filesystems, such as ext4 and
  495. F2FS, have to allocate bounce pages specially for encryption.
  496. Filename hashing and encoding
  497. -----------------------------
  498. Modern filesystems accelerate directory lookups by using indexed
  499. directories. An indexed directory is organized as a tree keyed by
  500. filename hashes. When a ->lookup() is requested, the filesystem
  501. normally hashes the filename being looked up so that it can quickly
  502. find the corresponding directory entry, if any.
  503. With encryption, lookups must be supported and efficient both with and
  504. without the encryption key. Clearly, it would not work to hash the
  505. plaintext filenames, since the plaintext filenames are unavailable
  506. without the key. (Hashing the plaintext filenames would also make it
  507. impossible for the filesystem's fsck tool to optimize encrypted
  508. directories.) Instead, filesystems hash the ciphertext filenames,
  509. i.e. the bytes actually stored on-disk in the directory entries. When
  510. asked to do a ->lookup() with the key, the filesystem just encrypts
  511. the user-supplied name to get the ciphertext.
  512. Lookups without the key are more complicated. The raw ciphertext may
  513. contain the ``\0`` and ``/`` characters, which are illegal in
  514. filenames. Therefore, readdir() must base64-encode the ciphertext for
  515. presentation. For most filenames, this works fine; on ->lookup(), the
  516. filesystem just base64-decodes the user-supplied name to get back to
  517. the raw ciphertext.
  518. However, for very long filenames, base64 encoding would cause the
  519. filename length to exceed NAME_MAX. To prevent this, readdir()
  520. actually presents long filenames in an abbreviated form which encodes
  521. a strong "hash" of the ciphertext filename, along with the optional
  522. filesystem-specific hash(es) needed for directory lookups. This
  523. allows the filesystem to still, with a high degree of confidence, map
  524. the filename given in ->lookup() back to a particular directory entry
  525. that was previously listed by readdir(). See :c:type:`struct
  526. fscrypt_digested_name` in the source for more details.
  527. Note that the precise way that filenames are presented to userspace
  528. without the key is subject to change in the future. It is only meant
  529. as a way to temporarily present valid filenames so that commands like
  530. ``rm -r`` work as expected on encrypted directories.