xref: /OK3568_Linux_fs/kernel/Documentation/filesystems/fscrypt.rst (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun=====================================
2*4882a593SmuzhiyunFilesystem-level encryption (fscrypt)
3*4882a593Smuzhiyun=====================================
4*4882a593Smuzhiyun
5*4882a593SmuzhiyunIntroduction
6*4882a593Smuzhiyun============
7*4882a593Smuzhiyun
8*4882a593Smuzhiyunfscrypt is a library which filesystems can hook into to support
9*4882a593Smuzhiyuntransparent encryption of files and directories.
10*4882a593Smuzhiyun
11*4882a593SmuzhiyunNote: "fscrypt" in this document refers to the kernel-level portion,
12*4882a593Smuzhiyunimplemented in ``fs/crypto/``, as opposed to the userspace tool
13*4882a593Smuzhiyun`fscrypt <https://github.com/google/fscrypt>`_.  This document only
14*4882a593Smuzhiyuncovers the kernel-level portion.  For command-line examples of how to
15*4882a593Smuzhiyunuse encryption, see the documentation for the userspace tool `fscrypt
16*4882a593Smuzhiyun<https://github.com/google/fscrypt>`_.  Also, it is recommended to use
17*4882a593Smuzhiyunthe fscrypt userspace tool, or other existing userspace tools such as
18*4882a593Smuzhiyun`fscryptctl <https://github.com/google/fscryptctl>`_ or `Android's key
19*4882a593Smuzhiyunmanagement system
20*4882a593Smuzhiyun<https://source.android.com/security/encryption/file-based>`_, over
21*4882a593Smuzhiyunusing the kernel's API directly.  Using existing tools reduces the
22*4882a593Smuzhiyunchance of introducing your own security bugs.  (Nevertheless, for
23*4882a593Smuzhiyuncompleteness this documentation covers the kernel's API anyway.)
24*4882a593Smuzhiyun
25*4882a593SmuzhiyunUnlike dm-crypt, fscrypt operates at the filesystem level rather than
26*4882a593Smuzhiyunat the block device level.  This allows it to encrypt different files
27*4882a593Smuzhiyunwith different keys and to have unencrypted files on the same
28*4882a593Smuzhiyunfilesystem.  This is useful for multi-user systems where each user's
29*4882a593Smuzhiyundata-at-rest needs to be cryptographically isolated from the others.
30*4882a593SmuzhiyunHowever, except for filenames, fscrypt does not encrypt filesystem
31*4882a593Smuzhiyunmetadata.
32*4882a593Smuzhiyun
33*4882a593SmuzhiyunUnlike eCryptfs, which is a stacked filesystem, fscrypt is integrated
34*4882a593Smuzhiyundirectly into supported filesystems --- currently ext4, F2FS, and
35*4882a593SmuzhiyunUBIFS.  This allows encrypted files to be read and written without
36*4882a593Smuzhiyuncaching both the decrypted and encrypted pages in the pagecache,
37*4882a593Smuzhiyunthereby nearly halving the memory used and bringing it in line with
38*4882a593Smuzhiyununencrypted files.  Similarly, half as many dentries and inodes are
39*4882a593Smuzhiyunneeded.  eCryptfs also limits encrypted filenames to 143 bytes,
40*4882a593Smuzhiyuncausing application compatibility issues; fscrypt allows the full 255
41*4882a593Smuzhiyunbytes (NAME_MAX).  Finally, unlike eCryptfs, the fscrypt API can be
42*4882a593Smuzhiyunused by unprivileged users, with no need to mount anything.
43*4882a593Smuzhiyun
44*4882a593Smuzhiyunfscrypt does not support encrypting files in-place.  Instead, it
45*4882a593Smuzhiyunsupports marking an empty directory as encrypted.  Then, after
46*4882a593Smuzhiyunuserspace provides the key, all regular files, directories, and
47*4882a593Smuzhiyunsymbolic links created in that directory tree are transparently
48*4882a593Smuzhiyunencrypted.
49*4882a593Smuzhiyun
50*4882a593SmuzhiyunThreat model
51*4882a593Smuzhiyun============
52*4882a593Smuzhiyun
53*4882a593SmuzhiyunOffline attacks
54*4882a593Smuzhiyun---------------
55*4882a593Smuzhiyun
56*4882a593SmuzhiyunProvided that userspace chooses a strong encryption key, fscrypt
57*4882a593Smuzhiyunprotects the confidentiality of file contents and filenames in the
58*4882a593Smuzhiyunevent of a single point-in-time permanent offline compromise of the
59*4882a593Smuzhiyunblock device content.  fscrypt does not protect the confidentiality of
60*4882a593Smuzhiyunnon-filename metadata, e.g. file sizes, file permissions, file
61*4882a593Smuzhiyuntimestamps, and extended attributes.  Also, the existence and location
62*4882a593Smuzhiyunof holes (unallocated blocks which logically contain all zeroes) in
63*4882a593Smuzhiyunfiles is not protected.
64*4882a593Smuzhiyun
65*4882a593Smuzhiyunfscrypt is not guaranteed to protect confidentiality or authenticity
66*4882a593Smuzhiyunif an attacker is able to manipulate the filesystem offline prior to
67*4882a593Smuzhiyunan authorized user later accessing the filesystem.
68*4882a593Smuzhiyun
69*4882a593SmuzhiyunOnline attacks
70*4882a593Smuzhiyun--------------
71*4882a593Smuzhiyun
72*4882a593Smuzhiyunfscrypt (and storage encryption in general) can only provide limited
73*4882a593Smuzhiyunprotection, if any at all, against online attacks.  In detail:
74*4882a593Smuzhiyun
75*4882a593SmuzhiyunSide-channel attacks
76*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~
77*4882a593Smuzhiyun
78*4882a593Smuzhiyunfscrypt is only resistant to side-channel attacks, such as timing or
79*4882a593Smuzhiyunelectromagnetic attacks, to the extent that the underlying Linux
80*4882a593SmuzhiyunCryptographic API algorithms are.  If a vulnerable algorithm is used,
81*4882a593Smuzhiyunsuch as a table-based implementation of AES, it may be possible for an
82*4882a593Smuzhiyunattacker to mount a side channel attack against the online system.
83*4882a593SmuzhiyunSide channel attacks may also be mounted against applications
84*4882a593Smuzhiyunconsuming decrypted data.
85*4882a593Smuzhiyun
86*4882a593SmuzhiyunUnauthorized file access
87*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~
88*4882a593Smuzhiyun
89*4882a593SmuzhiyunAfter an encryption key has been added, fscrypt does not hide the
90*4882a593Smuzhiyunplaintext file contents or filenames from other users on the same
91*4882a593Smuzhiyunsystem.  Instead, existing access control mechanisms such as file mode
92*4882a593Smuzhiyunbits, POSIX ACLs, LSMs, or namespaces should be used for this purpose.
93*4882a593Smuzhiyun
94*4882a593Smuzhiyun(For the reasoning behind this, understand that while the key is
95*4882a593Smuzhiyunadded, the confidentiality of the data, from the perspective of the
96*4882a593Smuzhiyunsystem itself, is *not* protected by the mathematical properties of
97*4882a593Smuzhiyunencryption but rather only by the correctness of the kernel.
98*4882a593SmuzhiyunTherefore, any encryption-specific access control checks would merely
99*4882a593Smuzhiyunbe enforced by kernel *code* and therefore would be largely redundant
100*4882a593Smuzhiyunwith the wide variety of access control mechanisms already available.)
101*4882a593Smuzhiyun
102*4882a593SmuzhiyunKernel memory compromise
103*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~
104*4882a593Smuzhiyun
105*4882a593SmuzhiyunAn attacker who compromises the system enough to read from arbitrary
106*4882a593Smuzhiyunmemory, e.g. by mounting a physical attack or by exploiting a kernel
107*4882a593Smuzhiyunsecurity vulnerability, can compromise all encryption keys that are
108*4882a593Smuzhiyuncurrently in use.
109*4882a593Smuzhiyun
110*4882a593SmuzhiyunHowever, fscrypt allows encryption keys to be removed from the kernel,
111*4882a593Smuzhiyunwhich may protect them from later compromise.
112*4882a593Smuzhiyun
113*4882a593SmuzhiyunIn more detail, the FS_IOC_REMOVE_ENCRYPTION_KEY ioctl (or the
114*4882a593SmuzhiyunFS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS ioctl) can wipe a master
115*4882a593Smuzhiyunencryption key from kernel memory.  If it does so, it will also try to
116*4882a593Smuzhiyunevict all cached inodes which had been "unlocked" using the key,
117*4882a593Smuzhiyunthereby wiping their per-file keys and making them once again appear
118*4882a593Smuzhiyun"locked", i.e. in ciphertext or encrypted form.
119*4882a593Smuzhiyun
120*4882a593SmuzhiyunHowever, these ioctls have some limitations:
121*4882a593Smuzhiyun
122*4882a593Smuzhiyun- Per-file keys for in-use files will *not* be removed or wiped.
123*4882a593Smuzhiyun  Therefore, for maximum effect, userspace should close the relevant
124*4882a593Smuzhiyun  encrypted files and directories before removing a master key, as
125*4882a593Smuzhiyun  well as kill any processes whose working directory is in an affected
126*4882a593Smuzhiyun  encrypted directory.
127*4882a593Smuzhiyun
128*4882a593Smuzhiyun- The kernel cannot magically wipe copies of the master key(s) that
129*4882a593Smuzhiyun  userspace might have as well.  Therefore, userspace must wipe all
130*4882a593Smuzhiyun  copies of the master key(s) it makes as well; normally this should
131*4882a593Smuzhiyun  be done immediately after FS_IOC_ADD_ENCRYPTION_KEY, without waiting
132*4882a593Smuzhiyun  for FS_IOC_REMOVE_ENCRYPTION_KEY.  Naturally, the same also applies
133*4882a593Smuzhiyun  to all higher levels in the key hierarchy.  Userspace should also
134*4882a593Smuzhiyun  follow other security precautions such as mlock()ing memory
135*4882a593Smuzhiyun  containing keys to prevent it from being swapped out.
136*4882a593Smuzhiyun
137*4882a593Smuzhiyun- In general, decrypted contents and filenames in the kernel VFS
138*4882a593Smuzhiyun  caches are freed but not wiped.  Therefore, portions thereof may be
139*4882a593Smuzhiyun  recoverable from freed memory, even after the corresponding key(s)
140*4882a593Smuzhiyun  were wiped.  To partially solve this, you can set
141*4882a593Smuzhiyun  CONFIG_PAGE_POISONING=y in your kernel config and add page_poison=1
142*4882a593Smuzhiyun  to your kernel command line.  However, this has a performance cost.
143*4882a593Smuzhiyun
144*4882a593Smuzhiyun- Secret keys might still exist in CPU registers, in crypto
145*4882a593Smuzhiyun  accelerator hardware (if used by the crypto API to implement any of
146*4882a593Smuzhiyun  the algorithms), or in other places not explicitly considered here.
147*4882a593Smuzhiyun
148*4882a593SmuzhiyunLimitations of v1 policies
149*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~
150*4882a593Smuzhiyun
151*4882a593Smuzhiyunv1 encryption policies have some weaknesses with respect to online
152*4882a593Smuzhiyunattacks:
153*4882a593Smuzhiyun
154*4882a593Smuzhiyun- There is no verification that the provided master key is correct.
155*4882a593Smuzhiyun  Therefore, a malicious user can temporarily associate the wrong key
156*4882a593Smuzhiyun  with another user's encrypted files to which they have read-only
157*4882a593Smuzhiyun  access.  Because of filesystem caching, the wrong key will then be
158*4882a593Smuzhiyun  used by the other user's accesses to those files, even if the other
159*4882a593Smuzhiyun  user has the correct key in their own keyring.  This violates the
160*4882a593Smuzhiyun  meaning of "read-only access".
161*4882a593Smuzhiyun
162*4882a593Smuzhiyun- A compromise of a per-file key also compromises the master key from
163*4882a593Smuzhiyun  which it was derived.
164*4882a593Smuzhiyun
165*4882a593Smuzhiyun- Non-root users cannot securely remove encryption keys.
166*4882a593Smuzhiyun
167*4882a593SmuzhiyunAll the above problems are fixed with v2 encryption policies.  For
168*4882a593Smuzhiyunthis reason among others, it is recommended to use v2 encryption
169*4882a593Smuzhiyunpolicies on all new encrypted directories.
170*4882a593Smuzhiyun
171*4882a593SmuzhiyunKey hierarchy
172*4882a593Smuzhiyun=============
173*4882a593Smuzhiyun
174*4882a593SmuzhiyunMaster Keys
175*4882a593Smuzhiyun-----------
176*4882a593Smuzhiyun
177*4882a593SmuzhiyunEach encrypted directory tree is protected by a *master key*.  Master
178*4882a593Smuzhiyunkeys can be up to 64 bytes long, and must be at least as long as the
179*4882a593Smuzhiyungreater of the security strength of the contents and filenames
180*4882a593Smuzhiyunencryption modes being used.  For example, if any AES-256 mode is
181*4882a593Smuzhiyunused, the master key must be at least 256 bits, i.e. 32 bytes.  A
182*4882a593Smuzhiyunstricter requirement applies if the key is used by a v1 encryption
183*4882a593Smuzhiyunpolicy and AES-256-XTS is used; such keys must be 64 bytes.
184*4882a593Smuzhiyun
185*4882a593SmuzhiyunTo "unlock" an encrypted directory tree, userspace must provide the
186*4882a593Smuzhiyunappropriate master key.  There can be any number of master keys, each
187*4882a593Smuzhiyunof which protects any number of directory trees on any number of
188*4882a593Smuzhiyunfilesystems.
189*4882a593Smuzhiyun
190*4882a593SmuzhiyunMaster keys must be real cryptographic keys, i.e. indistinguishable
191*4882a593Smuzhiyunfrom random bytestrings of the same length.  This implies that users
192*4882a593Smuzhiyun**must not** directly use a password as a master key, zero-pad a
193*4882a593Smuzhiyunshorter key, or repeat a shorter key.  Security cannot be guaranteed
194*4882a593Smuzhiyunif userspace makes any such error, as the cryptographic proofs and
195*4882a593Smuzhiyunanalysis would no longer apply.
196*4882a593Smuzhiyun
197*4882a593SmuzhiyunInstead, users should generate master keys either using a
198*4882a593Smuzhiyuncryptographically secure random number generator, or by using a KDF
199*4882a593Smuzhiyun(Key Derivation Function).  The kernel does not do any key stretching;
200*4882a593Smuzhiyuntherefore, if userspace derives the key from a low-entropy secret such
201*4882a593Smuzhiyunas a passphrase, it is critical that a KDF designed for this purpose
202*4882a593Smuzhiyunbe used, such as scrypt, PBKDF2, or Argon2.
203*4882a593Smuzhiyun
204*4882a593SmuzhiyunKey derivation function
205*4882a593Smuzhiyun-----------------------
206*4882a593Smuzhiyun
207*4882a593SmuzhiyunWith one exception, fscrypt never uses the master key(s) for
208*4882a593Smuzhiyunencryption directly.  Instead, they are only used as input to a KDF
209*4882a593Smuzhiyun(Key Derivation Function) to derive the actual keys.
210*4882a593Smuzhiyun
211*4882a593SmuzhiyunThe KDF used for a particular master key differs depending on whether
212*4882a593Smuzhiyunthe key is used for v1 encryption policies or for v2 encryption
213*4882a593Smuzhiyunpolicies.  Users **must not** use the same key for both v1 and v2
214*4882a593Smuzhiyunencryption policies.  (No real-world attack is currently known on this
215*4882a593Smuzhiyunspecific case of key reuse, but its security cannot be guaranteed
216*4882a593Smuzhiyunsince the cryptographic proofs and analysis would no longer apply.)
217*4882a593Smuzhiyun
218*4882a593SmuzhiyunFor v1 encryption policies, the KDF only supports deriving per-file
219*4882a593Smuzhiyunencryption keys.  It works by encrypting the master key with
220*4882a593SmuzhiyunAES-128-ECB, using the file's 16-byte nonce as the AES key.  The
221*4882a593Smuzhiyunresulting ciphertext is used as the derived key.  If the ciphertext is
222*4882a593Smuzhiyunlonger than needed, then it is truncated to the needed length.
223*4882a593Smuzhiyun
224*4882a593SmuzhiyunFor v2 encryption policies, the KDF is HKDF-SHA512.  The master key is
225*4882a593Smuzhiyunpassed as the "input keying material", no salt is used, and a distinct
226*4882a593Smuzhiyun"application-specific information string" is used for each distinct
227*4882a593Smuzhiyunkey to be derived.  For example, when a per-file encryption key is
228*4882a593Smuzhiyunderived, the application-specific information string is the file's
229*4882a593Smuzhiyunnonce prefixed with "fscrypt\\0" and a context byte.  Different
230*4882a593Smuzhiyuncontext bytes are used for other types of derived keys.
231*4882a593Smuzhiyun
232*4882a593SmuzhiyunHKDF-SHA512 is preferred to the original AES-128-ECB based KDF because
233*4882a593SmuzhiyunHKDF is more flexible, is nonreversible, and evenly distributes
234*4882a593Smuzhiyunentropy from the master key.  HKDF is also standardized and widely
235*4882a593Smuzhiyunused by other software, whereas the AES-128-ECB based KDF is ad-hoc.
236*4882a593Smuzhiyun
237*4882a593SmuzhiyunPer-file encryption keys
238*4882a593Smuzhiyun------------------------
239*4882a593Smuzhiyun
240*4882a593SmuzhiyunSince each master key can protect many files, it is necessary to
241*4882a593Smuzhiyun"tweak" the encryption of each file so that the same plaintext in two
242*4882a593Smuzhiyunfiles doesn't map to the same ciphertext, or vice versa.  In most
243*4882a593Smuzhiyuncases, fscrypt does this by deriving per-file keys.  When a new
244*4882a593Smuzhiyunencrypted inode (regular file, directory, or symlink) is created,
245*4882a593Smuzhiyunfscrypt randomly generates a 16-byte nonce and stores it in the
246*4882a593Smuzhiyuninode's encryption xattr.  Then, it uses a KDF (as described in `Key
247*4882a593Smuzhiyunderivation function`_) to derive the file's key from the master key
248*4882a593Smuzhiyunand nonce.
249*4882a593Smuzhiyun
250*4882a593SmuzhiyunKey derivation was chosen over key wrapping because wrapped keys would
251*4882a593Smuzhiyunrequire larger xattrs which would be less likely to fit in-line in the
252*4882a593Smuzhiyunfilesystem's inode table, and there didn't appear to be any
253*4882a593Smuzhiyunsignificant advantages to key wrapping.  In particular, currently
254*4882a593Smuzhiyunthere is no requirement to support unlocking a file with multiple
255*4882a593Smuzhiyunalternative master keys or to support rotating master keys.  Instead,
256*4882a593Smuzhiyunthe master keys may be wrapped in userspace, e.g. as is done by the
257*4882a593Smuzhiyun`fscrypt <https://github.com/google/fscrypt>`_ tool.
258*4882a593Smuzhiyun
259*4882a593SmuzhiyunDIRECT_KEY policies
260*4882a593Smuzhiyun-------------------
261*4882a593Smuzhiyun
262*4882a593SmuzhiyunThe Adiantum encryption mode (see `Encryption modes and usage`_) is
263*4882a593Smuzhiyunsuitable for both contents and filenames encryption, and it accepts
264*4882a593Smuzhiyunlong IVs --- long enough to hold both an 8-byte logical block number
265*4882a593Smuzhiyunand a 16-byte per-file nonce.  Also, the overhead of each Adiantum key
266*4882a593Smuzhiyunis greater than that of an AES-256-XTS key.
267*4882a593Smuzhiyun
268*4882a593SmuzhiyunTherefore, to improve performance and save memory, for Adiantum a
269*4882a593Smuzhiyun"direct key" configuration is supported.  When the user has enabled
270*4882a593Smuzhiyunthis by setting FSCRYPT_POLICY_FLAG_DIRECT_KEY in the fscrypt policy,
271*4882a593Smuzhiyunper-file encryption keys are not used.  Instead, whenever any data
272*4882a593Smuzhiyun(contents or filenames) is encrypted, the file's 16-byte nonce is
273*4882a593Smuzhiyunincluded in the IV.  Moreover:
274*4882a593Smuzhiyun
275*4882a593Smuzhiyun- For v1 encryption policies, the encryption is done directly with the
276*4882a593Smuzhiyun  master key.  Because of this, users **must not** use the same master
277*4882a593Smuzhiyun  key for any other purpose, even for other v1 policies.
278*4882a593Smuzhiyun
279*4882a593Smuzhiyun- For v2 encryption policies, the encryption is done with a per-mode
280*4882a593Smuzhiyun  key derived using the KDF.  Users may use the same master key for
281*4882a593Smuzhiyun  other v2 encryption policies.
282*4882a593Smuzhiyun
283*4882a593SmuzhiyunIV_INO_LBLK_64 policies
284*4882a593Smuzhiyun-----------------------
285*4882a593Smuzhiyun
286*4882a593SmuzhiyunWhen FSCRYPT_POLICY_FLAG_IV_INO_LBLK_64 is set in the fscrypt policy,
287*4882a593Smuzhiyunthe encryption keys are derived from the master key, encryption mode
288*4882a593Smuzhiyunnumber, and filesystem UUID.  This normally results in all files
289*4882a593Smuzhiyunprotected by the same master key sharing a single contents encryption
290*4882a593Smuzhiyunkey and a single filenames encryption key.  To still encrypt different
291*4882a593Smuzhiyunfiles' data differently, inode numbers are included in the IVs.
292*4882a593SmuzhiyunConsequently, shrinking the filesystem may not be allowed.
293*4882a593Smuzhiyun
294*4882a593SmuzhiyunThis format is optimized for use with inline encryption hardware
295*4882a593Smuzhiyuncompliant with the UFS standard, which supports only 64 IV bits per
296*4882a593SmuzhiyunI/O request and may have only a small number of keyslots.
297*4882a593Smuzhiyun
298*4882a593SmuzhiyunIV_INO_LBLK_32 policies
299*4882a593Smuzhiyun-----------------------
300*4882a593Smuzhiyun
301*4882a593SmuzhiyunIV_INO_LBLK_32 policies work like IV_INO_LBLK_64, except that for
302*4882a593SmuzhiyunIV_INO_LBLK_32, the inode number is hashed with SipHash-2-4 (where the
303*4882a593SmuzhiyunSipHash key is derived from the master key) and added to the file
304*4882a593Smuzhiyunlogical block number mod 2^32 to produce a 32-bit IV.
305*4882a593Smuzhiyun
306*4882a593SmuzhiyunThis format is optimized for use with inline encryption hardware
307*4882a593Smuzhiyuncompliant with the eMMC v5.2 standard, which supports only 32 IV bits
308*4882a593Smuzhiyunper I/O request and may have only a small number of keyslots.  This
309*4882a593Smuzhiyunformat results in some level of IV reuse, so it should only be used
310*4882a593Smuzhiyunwhen necessary due to hardware limitations.
311*4882a593Smuzhiyun
312*4882a593SmuzhiyunKey identifiers
313*4882a593Smuzhiyun---------------
314*4882a593Smuzhiyun
315*4882a593SmuzhiyunFor master keys used for v2 encryption policies, a unique 16-byte "key
316*4882a593Smuzhiyunidentifier" is also derived using the KDF.  This value is stored in
317*4882a593Smuzhiyunthe clear, since it is needed to reliably identify the key itself.
318*4882a593Smuzhiyun
319*4882a593SmuzhiyunDirhash keys
320*4882a593Smuzhiyun------------
321*4882a593Smuzhiyun
322*4882a593SmuzhiyunFor directories that are indexed using a secret-keyed dirhash over the
323*4882a593Smuzhiyunplaintext filenames, the KDF is also used to derive a 128-bit
324*4882a593SmuzhiyunSipHash-2-4 key per directory in order to hash filenames.  This works
325*4882a593Smuzhiyunjust like deriving a per-file encryption key, except that a different
326*4882a593SmuzhiyunKDF context is used.  Currently, only casefolded ("case-insensitive")
327*4882a593Smuzhiyunencrypted directories use this style of hashing.
328*4882a593Smuzhiyun
329*4882a593SmuzhiyunEncryption modes and usage
330*4882a593Smuzhiyun==========================
331*4882a593Smuzhiyun
332*4882a593Smuzhiyunfscrypt allows one encryption mode to be specified for file contents
333*4882a593Smuzhiyunand one encryption mode to be specified for filenames.  Different
334*4882a593Smuzhiyundirectory trees are permitted to use different encryption modes.
335*4882a593SmuzhiyunCurrently, the following pairs of encryption modes are supported:
336*4882a593Smuzhiyun
337*4882a593Smuzhiyun- AES-256-XTS for contents and AES-256-CTS-CBC for filenames
338*4882a593Smuzhiyun- AES-128-CBC for contents and AES-128-CTS-CBC for filenames
339*4882a593Smuzhiyun- Adiantum for both contents and filenames
340*4882a593Smuzhiyun
341*4882a593SmuzhiyunIf unsure, you should use the (AES-256-XTS, AES-256-CTS-CBC) pair.
342*4882a593Smuzhiyun
343*4882a593SmuzhiyunAES-128-CBC was added only for low-powered embedded devices with
344*4882a593Smuzhiyuncrypto accelerators such as CAAM or CESA that do not support XTS.  To
345*4882a593Smuzhiyunuse AES-128-CBC, CONFIG_CRYPTO_ESSIV and CONFIG_CRYPTO_SHA256 (or
346*4882a593Smuzhiyunanother SHA-256 implementation) must be enabled so that ESSIV can be
347*4882a593Smuzhiyunused.
348*4882a593Smuzhiyun
349*4882a593SmuzhiyunAdiantum is a (primarily) stream cipher-based mode that is fast even
350*4882a593Smuzhiyunon CPUs without dedicated crypto instructions.  It's also a true
351*4882a593Smuzhiyunwide-block mode, unlike XTS.  It can also eliminate the need to derive
352*4882a593Smuzhiyunper-file encryption keys.  However, it depends on the security of two
353*4882a593Smuzhiyunprimitives, XChaCha12 and AES-256, rather than just one.  See the
354*4882a593Smuzhiyunpaper "Adiantum: length-preserving encryption for entry-level
355*4882a593Smuzhiyunprocessors" (https://eprint.iacr.org/2018/720.pdf) for more details.
356*4882a593SmuzhiyunTo use Adiantum, CONFIG_CRYPTO_ADIANTUM must be enabled.  Also, fast
357*4882a593Smuzhiyunimplementations of ChaCha and NHPoly1305 should be enabled, e.g.
358*4882a593SmuzhiyunCONFIG_CRYPTO_CHACHA20_NEON and CONFIG_CRYPTO_NHPOLY1305_NEON for ARM.
359*4882a593Smuzhiyun
360*4882a593SmuzhiyunNew encryption modes can be added relatively easily, without changes
361*4882a593Smuzhiyunto individual filesystems.  However, authenticated encryption (AE)
362*4882a593Smuzhiyunmodes are not currently supported because of the difficulty of dealing
363*4882a593Smuzhiyunwith ciphertext expansion.
364*4882a593Smuzhiyun
365*4882a593SmuzhiyunContents encryption
366*4882a593Smuzhiyun-------------------
367*4882a593Smuzhiyun
368*4882a593SmuzhiyunFor file contents, each filesystem block is encrypted independently.
369*4882a593SmuzhiyunStarting from Linux kernel 5.5, encryption of filesystems with block
370*4882a593Smuzhiyunsize less than system's page size is supported.
371*4882a593Smuzhiyun
372*4882a593SmuzhiyunEach block's IV is set to the logical block number within the file as
373*4882a593Smuzhiyuna little endian number, except that:
374*4882a593Smuzhiyun
375*4882a593Smuzhiyun- With CBC mode encryption, ESSIV is also used.  Specifically, each IV
376*4882a593Smuzhiyun  is encrypted with AES-256 where the AES-256 key is the SHA-256 hash
377*4882a593Smuzhiyun  of the file's data encryption key.
378*4882a593Smuzhiyun
379*4882a593Smuzhiyun- With `DIRECT_KEY policies`_, the file's nonce is appended to the IV.
380*4882a593Smuzhiyun  Currently this is only allowed with the Adiantum encryption mode.
381*4882a593Smuzhiyun
382*4882a593Smuzhiyun- With `IV_INO_LBLK_64 policies`_, the logical block number is limited
383*4882a593Smuzhiyun  to 32 bits and is placed in bits 0-31 of the IV.  The inode number
384*4882a593Smuzhiyun  (which is also limited to 32 bits) is placed in bits 32-63.
385*4882a593Smuzhiyun
386*4882a593Smuzhiyun- With `IV_INO_LBLK_32 policies`_, the logical block number is limited
387*4882a593Smuzhiyun  to 32 bits and is placed in bits 0-31 of the IV.  The inode number
388*4882a593Smuzhiyun  is then hashed and added mod 2^32.
389*4882a593Smuzhiyun
390*4882a593SmuzhiyunNote that because file logical block numbers are included in the IVs,
391*4882a593Smuzhiyunfilesystems must enforce that blocks are never shifted around within
392*4882a593Smuzhiyunencrypted files, e.g. via "collapse range" or "insert range".
393*4882a593Smuzhiyun
394*4882a593SmuzhiyunFilenames encryption
395*4882a593Smuzhiyun--------------------
396*4882a593Smuzhiyun
397*4882a593SmuzhiyunFor filenames, each full filename is encrypted at once.  Because of
398*4882a593Smuzhiyunthe requirements to retain support for efficient directory lookups and
399*4882a593Smuzhiyunfilenames of up to 255 bytes, the same IV is used for every filename
400*4882a593Smuzhiyunin a directory.
401*4882a593Smuzhiyun
402*4882a593SmuzhiyunHowever, each encrypted directory still uses a unique key, or
403*4882a593Smuzhiyunalternatively has the file's nonce (for `DIRECT_KEY policies`_) or
404*4882a593Smuzhiyuninode number (for `IV_INO_LBLK_64 policies`_) included in the IVs.
405*4882a593SmuzhiyunThus, IV reuse is limited to within a single directory.
406*4882a593Smuzhiyun
407*4882a593SmuzhiyunWith CTS-CBC, the IV reuse means that when the plaintext filenames
408*4882a593Smuzhiyunshare a common prefix at least as long as the cipher block size (16
409*4882a593Smuzhiyunbytes for AES), the corresponding encrypted filenames will also share
410*4882a593Smuzhiyuna common prefix.  This is undesirable.  Adiantum does not have this
411*4882a593Smuzhiyunweakness, as it is a wide-block encryption mode.
412*4882a593Smuzhiyun
413*4882a593SmuzhiyunAll supported filenames encryption modes accept any plaintext length
414*4882a593Smuzhiyun>= 16 bytes; cipher block alignment is not required.  However,
415*4882a593Smuzhiyunfilenames shorter than 16 bytes are NUL-padded to 16 bytes before
416*4882a593Smuzhiyunbeing encrypted.  In addition, to reduce leakage of filename lengths
417*4882a593Smuzhiyunvia their ciphertexts, all filenames are NUL-padded to the next 4, 8,
418*4882a593Smuzhiyun16, or 32-byte boundary (configurable).  32 is recommended since this
419*4882a593Smuzhiyunprovides the best confidentiality, at the cost of making directory
420*4882a593Smuzhiyunentries consume slightly more space.  Note that since NUL (``\0``) is
421*4882a593Smuzhiyunnot otherwise a valid character in filenames, the padding will never
422*4882a593Smuzhiyunproduce duplicate plaintexts.
423*4882a593Smuzhiyun
424*4882a593SmuzhiyunSymbolic link targets are considered a type of filename and are
425*4882a593Smuzhiyunencrypted in the same way as filenames in directory entries, except
426*4882a593Smuzhiyunthat IV reuse is not a problem as each symlink has its own inode.
427*4882a593Smuzhiyun
428*4882a593SmuzhiyunUser API
429*4882a593Smuzhiyun========
430*4882a593Smuzhiyun
431*4882a593SmuzhiyunSetting an encryption policy
432*4882a593Smuzhiyun----------------------------
433*4882a593Smuzhiyun
434*4882a593SmuzhiyunFS_IOC_SET_ENCRYPTION_POLICY
435*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~~
436*4882a593Smuzhiyun
437*4882a593SmuzhiyunThe FS_IOC_SET_ENCRYPTION_POLICY ioctl sets an encryption policy on an
438*4882a593Smuzhiyunempty directory or verifies that a directory or regular file already
439*4882a593Smuzhiyunhas the specified encryption policy.  It takes in a pointer to
440*4882a593Smuzhiyunstruct fscrypt_policy_v1 or struct fscrypt_policy_v2, defined as
441*4882a593Smuzhiyunfollows::
442*4882a593Smuzhiyun
443*4882a593Smuzhiyun    #define FSCRYPT_POLICY_V1               0
444*4882a593Smuzhiyun    #define FSCRYPT_KEY_DESCRIPTOR_SIZE     8
445*4882a593Smuzhiyun    struct fscrypt_policy_v1 {
446*4882a593Smuzhiyun            __u8 version;
447*4882a593Smuzhiyun            __u8 contents_encryption_mode;
448*4882a593Smuzhiyun            __u8 filenames_encryption_mode;
449*4882a593Smuzhiyun            __u8 flags;
450*4882a593Smuzhiyun            __u8 master_key_descriptor[FSCRYPT_KEY_DESCRIPTOR_SIZE];
451*4882a593Smuzhiyun    };
452*4882a593Smuzhiyun    #define fscrypt_policy  fscrypt_policy_v1
453*4882a593Smuzhiyun
454*4882a593Smuzhiyun    #define FSCRYPT_POLICY_V2               2
455*4882a593Smuzhiyun    #define FSCRYPT_KEY_IDENTIFIER_SIZE     16
456*4882a593Smuzhiyun    struct fscrypt_policy_v2 {
457*4882a593Smuzhiyun            __u8 version;
458*4882a593Smuzhiyun            __u8 contents_encryption_mode;
459*4882a593Smuzhiyun            __u8 filenames_encryption_mode;
460*4882a593Smuzhiyun            __u8 flags;
461*4882a593Smuzhiyun            __u8 __reserved[4];
462*4882a593Smuzhiyun            __u8 master_key_identifier[FSCRYPT_KEY_IDENTIFIER_SIZE];
463*4882a593Smuzhiyun    };
464*4882a593Smuzhiyun
465*4882a593SmuzhiyunThis structure must be initialized as follows:
466*4882a593Smuzhiyun
467*4882a593Smuzhiyun- ``version`` must be FSCRYPT_POLICY_V1 (0) if
468*4882a593Smuzhiyun  struct fscrypt_policy_v1 is used or FSCRYPT_POLICY_V2 (2) if
469*4882a593Smuzhiyun  struct fscrypt_policy_v2 is used. (Note: we refer to the original
470*4882a593Smuzhiyun  policy version as "v1", though its version code is really 0.)
471*4882a593Smuzhiyun  For new encrypted directories, use v2 policies.
472*4882a593Smuzhiyun
473*4882a593Smuzhiyun- ``contents_encryption_mode`` and ``filenames_encryption_mode`` must
474*4882a593Smuzhiyun  be set to constants from ``<linux/fscrypt.h>`` which identify the
475*4882a593Smuzhiyun  encryption modes to use.  If unsure, use FSCRYPT_MODE_AES_256_XTS
476*4882a593Smuzhiyun  (1) for ``contents_encryption_mode`` and FSCRYPT_MODE_AES_256_CTS
477*4882a593Smuzhiyun  (4) for ``filenames_encryption_mode``.
478*4882a593Smuzhiyun
479*4882a593Smuzhiyun- ``flags`` contains optional flags from ``<linux/fscrypt.h>``:
480*4882a593Smuzhiyun
481*4882a593Smuzhiyun  - FSCRYPT_POLICY_FLAGS_PAD_*: The amount of NUL padding to use when
482*4882a593Smuzhiyun    encrypting filenames.  If unsure, use FSCRYPT_POLICY_FLAGS_PAD_32
483*4882a593Smuzhiyun    (0x3).
484*4882a593Smuzhiyun  - FSCRYPT_POLICY_FLAG_DIRECT_KEY: See `DIRECT_KEY policies`_.
485*4882a593Smuzhiyun  - FSCRYPT_POLICY_FLAG_IV_INO_LBLK_64: See `IV_INO_LBLK_64
486*4882a593Smuzhiyun    policies`_.
487*4882a593Smuzhiyun  - FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32: See `IV_INO_LBLK_32
488*4882a593Smuzhiyun    policies`_.
489*4882a593Smuzhiyun
490*4882a593Smuzhiyun  v1 encryption policies only support the PAD_* and DIRECT_KEY flags.
491*4882a593Smuzhiyun  The other flags are only supported by v2 encryption policies.
492*4882a593Smuzhiyun
493*4882a593Smuzhiyun  The DIRECT_KEY, IV_INO_LBLK_64, and IV_INO_LBLK_32 flags are
494*4882a593Smuzhiyun  mutually exclusive.
495*4882a593Smuzhiyun
496*4882a593Smuzhiyun- For v2 encryption policies, ``__reserved`` must be zeroed.
497*4882a593Smuzhiyun
498*4882a593Smuzhiyun- For v1 encryption policies, ``master_key_descriptor`` specifies how
499*4882a593Smuzhiyun  to find the master key in a keyring; see `Adding keys`_.  It is up
500*4882a593Smuzhiyun  to userspace to choose a unique ``master_key_descriptor`` for each
501*4882a593Smuzhiyun  master key.  The e4crypt and fscrypt tools use the first 8 bytes of
502*4882a593Smuzhiyun  ``SHA-512(SHA-512(master_key))``, but this particular scheme is not
503*4882a593Smuzhiyun  required.  Also, the master key need not be in the keyring yet when
504*4882a593Smuzhiyun  FS_IOC_SET_ENCRYPTION_POLICY is executed.  However, it must be added
505*4882a593Smuzhiyun  before any files can be created in the encrypted directory.
506*4882a593Smuzhiyun
507*4882a593Smuzhiyun  For v2 encryption policies, ``master_key_descriptor`` has been
508*4882a593Smuzhiyun  replaced with ``master_key_identifier``, which is longer and cannot
509*4882a593Smuzhiyun  be arbitrarily chosen.  Instead, the key must first be added using
510*4882a593Smuzhiyun  `FS_IOC_ADD_ENCRYPTION_KEY`_.  Then, the ``key_spec.u.identifier``
511*4882a593Smuzhiyun  the kernel returned in the struct fscrypt_add_key_arg must
512*4882a593Smuzhiyun  be used as the ``master_key_identifier`` in
513*4882a593Smuzhiyun  struct fscrypt_policy_v2.
514*4882a593Smuzhiyun
515*4882a593SmuzhiyunIf the file is not yet encrypted, then FS_IOC_SET_ENCRYPTION_POLICY
516*4882a593Smuzhiyunverifies that the file is an empty directory.  If so, the specified
517*4882a593Smuzhiyunencryption policy is assigned to the directory, turning it into an
518*4882a593Smuzhiyunencrypted directory.  After that, and after providing the
519*4882a593Smuzhiyuncorresponding master key as described in `Adding keys`_, all regular
520*4882a593Smuzhiyunfiles, directories (recursively), and symlinks created in the
521*4882a593Smuzhiyundirectory will be encrypted, inheriting the same encryption policy.
522*4882a593SmuzhiyunThe filenames in the directory's entries will be encrypted as well.
523*4882a593Smuzhiyun
524*4882a593SmuzhiyunAlternatively, if the file is already encrypted, then
525*4882a593SmuzhiyunFS_IOC_SET_ENCRYPTION_POLICY validates that the specified encryption
526*4882a593Smuzhiyunpolicy exactly matches the actual one.  If they match, then the ioctl
527*4882a593Smuzhiyunreturns 0.  Otherwise, it fails with EEXIST.  This works on both
528*4882a593Smuzhiyunregular files and directories, including nonempty directories.
529*4882a593Smuzhiyun
530*4882a593SmuzhiyunWhen a v2 encryption policy is assigned to a directory, it is also
531*4882a593Smuzhiyunrequired that either the specified key has been added by the current
532*4882a593Smuzhiyunuser or that the caller has CAP_FOWNER in the initial user namespace.
533*4882a593Smuzhiyun(This is needed to prevent a user from encrypting their data with
534*4882a593Smuzhiyunanother user's key.)  The key must remain added while
535*4882a593SmuzhiyunFS_IOC_SET_ENCRYPTION_POLICY is executing.  However, if the new
536*4882a593Smuzhiyunencrypted directory does not need to be accessed immediately, then the
537*4882a593Smuzhiyunkey can be removed right away afterwards.
538*4882a593Smuzhiyun
539*4882a593SmuzhiyunNote that the ext4 filesystem does not allow the root directory to be
540*4882a593Smuzhiyunencrypted, even if it is empty.  Users who want to encrypt an entire
541*4882a593Smuzhiyunfilesystem with one key should consider using dm-crypt instead.
542*4882a593Smuzhiyun
543*4882a593SmuzhiyunFS_IOC_SET_ENCRYPTION_POLICY can fail with the following errors:
544*4882a593Smuzhiyun
545*4882a593Smuzhiyun- ``EACCES``: the file is not owned by the process's uid, nor does the
546*4882a593Smuzhiyun  process have the CAP_FOWNER capability in a namespace with the file
547*4882a593Smuzhiyun  owner's uid mapped
548*4882a593Smuzhiyun- ``EEXIST``: the file is already encrypted with an encryption policy
549*4882a593Smuzhiyun  different from the one specified
550*4882a593Smuzhiyun- ``EINVAL``: an invalid encryption policy was specified (invalid
551*4882a593Smuzhiyun  version, mode(s), or flags; or reserved bits were set); or a v1
552*4882a593Smuzhiyun  encryption policy was specified but the directory has the casefold
553*4882a593Smuzhiyun  flag enabled (casefolding is incompatible with v1 policies).
554*4882a593Smuzhiyun- ``ENOKEY``: a v2 encryption policy was specified, but the key with
555*4882a593Smuzhiyun  the specified ``master_key_identifier`` has not been added, nor does
556*4882a593Smuzhiyun  the process have the CAP_FOWNER capability in the initial user
557*4882a593Smuzhiyun  namespace
558*4882a593Smuzhiyun- ``ENOTDIR``: the file is unencrypted and is a regular file, not a
559*4882a593Smuzhiyun  directory
560*4882a593Smuzhiyun- ``ENOTEMPTY``: the file is unencrypted and is a nonempty directory
561*4882a593Smuzhiyun- ``ENOTTY``: this type of filesystem does not implement encryption
562*4882a593Smuzhiyun- ``EOPNOTSUPP``: the kernel was not configured with encryption
563*4882a593Smuzhiyun  support for filesystems, or the filesystem superblock has not
564*4882a593Smuzhiyun  had encryption enabled on it.  (For example, to use encryption on an
565*4882a593Smuzhiyun  ext4 filesystem, CONFIG_FS_ENCRYPTION must be enabled in the
566*4882a593Smuzhiyun  kernel config, and the superblock must have had the "encrypt"
567*4882a593Smuzhiyun  feature flag enabled using ``tune2fs -O encrypt`` or ``mkfs.ext4 -O
568*4882a593Smuzhiyun  encrypt``.)
569*4882a593Smuzhiyun- ``EPERM``: this directory may not be encrypted, e.g. because it is
570*4882a593Smuzhiyun  the root directory of an ext4 filesystem
571*4882a593Smuzhiyun- ``EROFS``: the filesystem is readonly
572*4882a593Smuzhiyun
573*4882a593SmuzhiyunGetting an encryption policy
574*4882a593Smuzhiyun----------------------------
575*4882a593Smuzhiyun
576*4882a593SmuzhiyunTwo ioctls are available to get a file's encryption policy:
577*4882a593Smuzhiyun
578*4882a593Smuzhiyun- `FS_IOC_GET_ENCRYPTION_POLICY_EX`_
579*4882a593Smuzhiyun- `FS_IOC_GET_ENCRYPTION_POLICY`_
580*4882a593Smuzhiyun
581*4882a593SmuzhiyunThe extended (_EX) version of the ioctl is more general and is
582*4882a593Smuzhiyunrecommended to use when possible.  However, on older kernels only the
583*4882a593Smuzhiyunoriginal ioctl is available.  Applications should try the extended
584*4882a593Smuzhiyunversion, and if it fails with ENOTTY fall back to the original
585*4882a593Smuzhiyunversion.
586*4882a593Smuzhiyun
587*4882a593SmuzhiyunFS_IOC_GET_ENCRYPTION_POLICY_EX
588*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
589*4882a593Smuzhiyun
590*4882a593SmuzhiyunThe FS_IOC_GET_ENCRYPTION_POLICY_EX ioctl retrieves the encryption
591*4882a593Smuzhiyunpolicy, if any, for a directory or regular file.  No additional
592*4882a593Smuzhiyunpermissions are required beyond the ability to open the file.  It
593*4882a593Smuzhiyuntakes in a pointer to struct fscrypt_get_policy_ex_arg,
594*4882a593Smuzhiyundefined as follows::
595*4882a593Smuzhiyun
596*4882a593Smuzhiyun    struct fscrypt_get_policy_ex_arg {
597*4882a593Smuzhiyun            __u64 policy_size; /* input/output */
598*4882a593Smuzhiyun            union {
599*4882a593Smuzhiyun                    __u8 version;
600*4882a593Smuzhiyun                    struct fscrypt_policy_v1 v1;
601*4882a593Smuzhiyun                    struct fscrypt_policy_v2 v2;
602*4882a593Smuzhiyun            } policy; /* output */
603*4882a593Smuzhiyun    };
604*4882a593Smuzhiyun
605*4882a593SmuzhiyunThe caller must initialize ``policy_size`` to the size available for
606*4882a593Smuzhiyunthe policy struct, i.e. ``sizeof(arg.policy)``.
607*4882a593Smuzhiyun
608*4882a593SmuzhiyunOn success, the policy struct is returned in ``policy``, and its
609*4882a593Smuzhiyunactual size is returned in ``policy_size``.  ``policy.version`` should
610*4882a593Smuzhiyunbe checked to determine the version of policy returned.  Note that the
611*4882a593Smuzhiyunversion code for the "v1" policy is actually 0 (FSCRYPT_POLICY_V1).
612*4882a593Smuzhiyun
613*4882a593SmuzhiyunFS_IOC_GET_ENCRYPTION_POLICY_EX can fail with the following errors:
614*4882a593Smuzhiyun
615*4882a593Smuzhiyun- ``EINVAL``: the file is encrypted, but it uses an unrecognized
616*4882a593Smuzhiyun  encryption policy version
617*4882a593Smuzhiyun- ``ENODATA``: the file is not encrypted
618*4882a593Smuzhiyun- ``ENOTTY``: this type of filesystem does not implement encryption,
619*4882a593Smuzhiyun  or this kernel is too old to support FS_IOC_GET_ENCRYPTION_POLICY_EX
620*4882a593Smuzhiyun  (try FS_IOC_GET_ENCRYPTION_POLICY instead)
621*4882a593Smuzhiyun- ``EOPNOTSUPP``: the kernel was not configured with encryption
622*4882a593Smuzhiyun  support for this filesystem, or the filesystem superblock has not
623*4882a593Smuzhiyun  had encryption enabled on it
624*4882a593Smuzhiyun- ``EOVERFLOW``: the file is encrypted and uses a recognized
625*4882a593Smuzhiyun  encryption policy version, but the policy struct does not fit into
626*4882a593Smuzhiyun  the provided buffer
627*4882a593Smuzhiyun
628*4882a593SmuzhiyunNote: if you only need to know whether a file is encrypted or not, on
629*4882a593Smuzhiyunmost filesystems it is also possible to use the FS_IOC_GETFLAGS ioctl
630*4882a593Smuzhiyunand check for FS_ENCRYPT_FL, or to use the statx() system call and
631*4882a593Smuzhiyuncheck for STATX_ATTR_ENCRYPTED in stx_attributes.
632*4882a593Smuzhiyun
633*4882a593SmuzhiyunFS_IOC_GET_ENCRYPTION_POLICY
634*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~~
635*4882a593Smuzhiyun
636*4882a593SmuzhiyunThe FS_IOC_GET_ENCRYPTION_POLICY ioctl can also retrieve the
637*4882a593Smuzhiyunencryption policy, if any, for a directory or regular file.  However,
638*4882a593Smuzhiyununlike `FS_IOC_GET_ENCRYPTION_POLICY_EX`_,
639*4882a593SmuzhiyunFS_IOC_GET_ENCRYPTION_POLICY only supports the original policy
640*4882a593Smuzhiyunversion.  It takes in a pointer directly to struct fscrypt_policy_v1
641*4882a593Smuzhiyunrather than struct fscrypt_get_policy_ex_arg.
642*4882a593Smuzhiyun
643*4882a593SmuzhiyunThe error codes for FS_IOC_GET_ENCRYPTION_POLICY are the same as those
644*4882a593Smuzhiyunfor FS_IOC_GET_ENCRYPTION_POLICY_EX, except that
645*4882a593SmuzhiyunFS_IOC_GET_ENCRYPTION_POLICY also returns ``EINVAL`` if the file is
646*4882a593Smuzhiyunencrypted using a newer encryption policy version.
647*4882a593Smuzhiyun
648*4882a593SmuzhiyunGetting the per-filesystem salt
649*4882a593Smuzhiyun-------------------------------
650*4882a593Smuzhiyun
651*4882a593SmuzhiyunSome filesystems, such as ext4 and F2FS, also support the deprecated
652*4882a593Smuzhiyunioctl FS_IOC_GET_ENCRYPTION_PWSALT.  This ioctl retrieves a randomly
653*4882a593Smuzhiyungenerated 16-byte value stored in the filesystem superblock.  This
654*4882a593Smuzhiyunvalue is intended to used as a salt when deriving an encryption key
655*4882a593Smuzhiyunfrom a passphrase or other low-entropy user credential.
656*4882a593Smuzhiyun
657*4882a593SmuzhiyunFS_IOC_GET_ENCRYPTION_PWSALT is deprecated.  Instead, prefer to
658*4882a593Smuzhiyungenerate and manage any needed salt(s) in userspace.
659*4882a593Smuzhiyun
660*4882a593SmuzhiyunGetting a file's encryption nonce
661*4882a593Smuzhiyun---------------------------------
662*4882a593Smuzhiyun
663*4882a593SmuzhiyunSince Linux v5.7, the ioctl FS_IOC_GET_ENCRYPTION_NONCE is supported.
664*4882a593SmuzhiyunOn encrypted files and directories it gets the inode's 16-byte nonce.
665*4882a593SmuzhiyunOn unencrypted files and directories, it fails with ENODATA.
666*4882a593Smuzhiyun
667*4882a593SmuzhiyunThis ioctl can be useful for automated tests which verify that the
668*4882a593Smuzhiyunencryption is being done correctly.  It is not needed for normal use
669*4882a593Smuzhiyunof fscrypt.
670*4882a593Smuzhiyun
671*4882a593SmuzhiyunAdding keys
672*4882a593Smuzhiyun-----------
673*4882a593Smuzhiyun
674*4882a593SmuzhiyunFS_IOC_ADD_ENCRYPTION_KEY
675*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~
676*4882a593Smuzhiyun
677*4882a593SmuzhiyunThe FS_IOC_ADD_ENCRYPTION_KEY ioctl adds a master encryption key to
678*4882a593Smuzhiyunthe filesystem, making all files on the filesystem which were
679*4882a593Smuzhiyunencrypted using that key appear "unlocked", i.e. in plaintext form.
680*4882a593SmuzhiyunIt can be executed on any file or directory on the target filesystem,
681*4882a593Smuzhiyunbut using the filesystem's root directory is recommended.  It takes in
682*4882a593Smuzhiyuna pointer to struct fscrypt_add_key_arg, defined as follows::
683*4882a593Smuzhiyun
684*4882a593Smuzhiyun    struct fscrypt_add_key_arg {
685*4882a593Smuzhiyun            struct fscrypt_key_specifier key_spec;
686*4882a593Smuzhiyun            __u32 raw_size;
687*4882a593Smuzhiyun            __u32 key_id;
688*4882a593Smuzhiyun            __u32 __reserved[8];
689*4882a593Smuzhiyun            __u8 raw[];
690*4882a593Smuzhiyun    };
691*4882a593Smuzhiyun
692*4882a593Smuzhiyun    #define FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR        1
693*4882a593Smuzhiyun    #define FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER        2
694*4882a593Smuzhiyun
695*4882a593Smuzhiyun    struct fscrypt_key_specifier {
696*4882a593Smuzhiyun            __u32 type;     /* one of FSCRYPT_KEY_SPEC_TYPE_* */
697*4882a593Smuzhiyun            __u32 __reserved;
698*4882a593Smuzhiyun            union {
699*4882a593Smuzhiyun                    __u8 __reserved[32]; /* reserve some extra space */
700*4882a593Smuzhiyun                    __u8 descriptor[FSCRYPT_KEY_DESCRIPTOR_SIZE];
701*4882a593Smuzhiyun                    __u8 identifier[FSCRYPT_KEY_IDENTIFIER_SIZE];
702*4882a593Smuzhiyun            } u;
703*4882a593Smuzhiyun    };
704*4882a593Smuzhiyun
705*4882a593Smuzhiyun    struct fscrypt_provisioning_key_payload {
706*4882a593Smuzhiyun            __u32 type;
707*4882a593Smuzhiyun            __u32 __reserved;
708*4882a593Smuzhiyun            __u8 raw[];
709*4882a593Smuzhiyun    };
710*4882a593Smuzhiyun
711*4882a593Smuzhiyunstruct fscrypt_add_key_arg must be zeroed, then initialized
712*4882a593Smuzhiyunas follows:
713*4882a593Smuzhiyun
714*4882a593Smuzhiyun- If the key is being added for use by v1 encryption policies, then
715*4882a593Smuzhiyun  ``key_spec.type`` must contain FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR, and
716*4882a593Smuzhiyun  ``key_spec.u.descriptor`` must contain the descriptor of the key
717*4882a593Smuzhiyun  being added, corresponding to the value in the
718*4882a593Smuzhiyun  ``master_key_descriptor`` field of struct fscrypt_policy_v1.
719*4882a593Smuzhiyun  To add this type of key, the calling process must have the
720*4882a593Smuzhiyun  CAP_SYS_ADMIN capability in the initial user namespace.
721*4882a593Smuzhiyun
722*4882a593Smuzhiyun  Alternatively, if the key is being added for use by v2 encryption
723*4882a593Smuzhiyun  policies, then ``key_spec.type`` must contain
724*4882a593Smuzhiyun  FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER, and ``key_spec.u.identifier`` is
725*4882a593Smuzhiyun  an *output* field which the kernel fills in with a cryptographic
726*4882a593Smuzhiyun  hash of the key.  To add this type of key, the calling process does
727*4882a593Smuzhiyun  not need any privileges.  However, the number of keys that can be
728*4882a593Smuzhiyun  added is limited by the user's quota for the keyrings service (see
729*4882a593Smuzhiyun  ``Documentation/security/keys/core.rst``).
730*4882a593Smuzhiyun
731*4882a593Smuzhiyun- ``raw_size`` must be the size of the ``raw`` key provided, in bytes.
732*4882a593Smuzhiyun  Alternatively, if ``key_id`` is nonzero, this field must be 0, since
733*4882a593Smuzhiyun  in that case the size is implied by the specified Linux keyring key.
734*4882a593Smuzhiyun
735*4882a593Smuzhiyun- ``key_id`` is 0 if the raw key is given directly in the ``raw``
736*4882a593Smuzhiyun  field.  Otherwise ``key_id`` is the ID of a Linux keyring key of
737*4882a593Smuzhiyun  type "fscrypt-provisioning" whose payload is
738*4882a593Smuzhiyun  struct fscrypt_provisioning_key_payload whose ``raw`` field contains
739*4882a593Smuzhiyun  the raw key and whose ``type`` field matches ``key_spec.type``.
740*4882a593Smuzhiyun  Since ``raw`` is variable-length, the total size of this key's
741*4882a593Smuzhiyun  payload must be ``sizeof(struct fscrypt_provisioning_key_payload)``
742*4882a593Smuzhiyun  plus the raw key size.  The process must have Search permission on
743*4882a593Smuzhiyun  this key.
744*4882a593Smuzhiyun
745*4882a593Smuzhiyun  Most users should leave this 0 and specify the raw key directly.
746*4882a593Smuzhiyun  The support for specifying a Linux keyring key is intended mainly to
747*4882a593Smuzhiyun  allow re-adding keys after a filesystem is unmounted and re-mounted,
748*4882a593Smuzhiyun  without having to store the raw keys in userspace memory.
749*4882a593Smuzhiyun
750*4882a593Smuzhiyun- ``raw`` is a variable-length field which must contain the actual
751*4882a593Smuzhiyun  key, ``raw_size`` bytes long.  Alternatively, if ``key_id`` is
752*4882a593Smuzhiyun  nonzero, then this field is unused.
753*4882a593Smuzhiyun
754*4882a593SmuzhiyunFor v2 policy keys, the kernel keeps track of which user (identified
755*4882a593Smuzhiyunby effective user ID) added the key, and only allows the key to be
756*4882a593Smuzhiyunremoved by that user --- or by "root", if they use
757*4882a593Smuzhiyun`FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS`_.
758*4882a593Smuzhiyun
759*4882a593SmuzhiyunHowever, if another user has added the key, it may be desirable to
760*4882a593Smuzhiyunprevent that other user from unexpectedly removing it.  Therefore,
761*4882a593SmuzhiyunFS_IOC_ADD_ENCRYPTION_KEY may also be used to add a v2 policy key
762*4882a593Smuzhiyun*again*, even if it's already added by other user(s).  In this case,
763*4882a593SmuzhiyunFS_IOC_ADD_ENCRYPTION_KEY will just install a claim to the key for the
764*4882a593Smuzhiyuncurrent user, rather than actually add the key again (but the raw key
765*4882a593Smuzhiyunmust still be provided, as a proof of knowledge).
766*4882a593Smuzhiyun
767*4882a593SmuzhiyunFS_IOC_ADD_ENCRYPTION_KEY returns 0 if either the key or a claim to
768*4882a593Smuzhiyunthe key was either added or already exists.
769*4882a593Smuzhiyun
770*4882a593SmuzhiyunFS_IOC_ADD_ENCRYPTION_KEY can fail with the following errors:
771*4882a593Smuzhiyun
772*4882a593Smuzhiyun- ``EACCES``: FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR was specified, but the
773*4882a593Smuzhiyun  caller does not have the CAP_SYS_ADMIN capability in the initial
774*4882a593Smuzhiyun  user namespace; or the raw key was specified by Linux key ID but the
775*4882a593Smuzhiyun  process lacks Search permission on the key.
776*4882a593Smuzhiyun- ``EDQUOT``: the key quota for this user would be exceeded by adding
777*4882a593Smuzhiyun  the key
778*4882a593Smuzhiyun- ``EINVAL``: invalid key size or key specifier type, or reserved bits
779*4882a593Smuzhiyun  were set
780*4882a593Smuzhiyun- ``EKEYREJECTED``: the raw key was specified by Linux key ID, but the
781*4882a593Smuzhiyun  key has the wrong type
782*4882a593Smuzhiyun- ``ENOKEY``: the raw key was specified by Linux key ID, but no key
783*4882a593Smuzhiyun  exists with that ID
784*4882a593Smuzhiyun- ``ENOTTY``: this type of filesystem does not implement encryption
785*4882a593Smuzhiyun- ``EOPNOTSUPP``: the kernel was not configured with encryption
786*4882a593Smuzhiyun  support for this filesystem, or the filesystem superblock has not
787*4882a593Smuzhiyun  had encryption enabled on it
788*4882a593Smuzhiyun
789*4882a593SmuzhiyunLegacy method
790*4882a593Smuzhiyun~~~~~~~~~~~~~
791*4882a593Smuzhiyun
792*4882a593SmuzhiyunFor v1 encryption policies, a master encryption key can also be
793*4882a593Smuzhiyunprovided by adding it to a process-subscribed keyring, e.g. to a
794*4882a593Smuzhiyunsession keyring, or to a user keyring if the user keyring is linked
795*4882a593Smuzhiyuninto the session keyring.
796*4882a593Smuzhiyun
797*4882a593SmuzhiyunThis method is deprecated (and not supported for v2 encryption
798*4882a593Smuzhiyunpolicies) for several reasons.  First, it cannot be used in
799*4882a593Smuzhiyuncombination with FS_IOC_REMOVE_ENCRYPTION_KEY (see `Removing keys`_),
800*4882a593Smuzhiyunso for removing a key a workaround such as keyctl_unlink() in
801*4882a593Smuzhiyuncombination with ``sync; echo 2 > /proc/sys/vm/drop_caches`` would
802*4882a593Smuzhiyunhave to be used.  Second, it doesn't match the fact that the
803*4882a593Smuzhiyunlocked/unlocked status of encrypted files (i.e. whether they appear to
804*4882a593Smuzhiyunbe in plaintext form or in ciphertext form) is global.  This mismatch
805*4882a593Smuzhiyunhas caused much confusion as well as real problems when processes
806*4882a593Smuzhiyunrunning under different UIDs, such as a ``sudo`` command, need to
807*4882a593Smuzhiyunaccess encrypted files.
808*4882a593Smuzhiyun
809*4882a593SmuzhiyunNevertheless, to add a key to one of the process-subscribed keyrings,
810*4882a593Smuzhiyunthe add_key() system call can be used (see:
811*4882a593Smuzhiyun``Documentation/security/keys/core.rst``).  The key type must be
812*4882a593Smuzhiyun"logon"; keys of this type are kept in kernel memory and cannot be
813*4882a593Smuzhiyunread back by userspace.  The key description must be "fscrypt:"
814*4882a593Smuzhiyunfollowed by the 16-character lower case hex representation of the
815*4882a593Smuzhiyun``master_key_descriptor`` that was set in the encryption policy.  The
816*4882a593Smuzhiyunkey payload must conform to the following structure::
817*4882a593Smuzhiyun
818*4882a593Smuzhiyun    #define FSCRYPT_MAX_KEY_SIZE            64
819*4882a593Smuzhiyun
820*4882a593Smuzhiyun    struct fscrypt_key {
821*4882a593Smuzhiyun            __u32 mode;
822*4882a593Smuzhiyun            __u8 raw[FSCRYPT_MAX_KEY_SIZE];
823*4882a593Smuzhiyun            __u32 size;
824*4882a593Smuzhiyun    };
825*4882a593Smuzhiyun
826*4882a593Smuzhiyun``mode`` is ignored; just set it to 0.  The actual key is provided in
827*4882a593Smuzhiyun``raw`` with ``size`` indicating its size in bytes.  That is, the
828*4882a593Smuzhiyunbytes ``raw[0..size-1]`` (inclusive) are the actual key.
829*4882a593Smuzhiyun
830*4882a593SmuzhiyunThe key description prefix "fscrypt:" may alternatively be replaced
831*4882a593Smuzhiyunwith a filesystem-specific prefix such as "ext4:".  However, the
832*4882a593Smuzhiyunfilesystem-specific prefixes are deprecated and should not be used in
833*4882a593Smuzhiyunnew programs.
834*4882a593Smuzhiyun
835*4882a593SmuzhiyunRemoving keys
836*4882a593Smuzhiyun-------------
837*4882a593Smuzhiyun
838*4882a593SmuzhiyunTwo ioctls are available for removing a key that was added by
839*4882a593Smuzhiyun`FS_IOC_ADD_ENCRYPTION_KEY`_:
840*4882a593Smuzhiyun
841*4882a593Smuzhiyun- `FS_IOC_REMOVE_ENCRYPTION_KEY`_
842*4882a593Smuzhiyun- `FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS`_
843*4882a593Smuzhiyun
844*4882a593SmuzhiyunThese two ioctls differ only in cases where v2 policy keys are added
845*4882a593Smuzhiyunor removed by non-root users.
846*4882a593Smuzhiyun
847*4882a593SmuzhiyunThese ioctls don't work on keys that were added via the legacy
848*4882a593Smuzhiyunprocess-subscribed keyrings mechanism.
849*4882a593Smuzhiyun
850*4882a593SmuzhiyunBefore using these ioctls, read the `Kernel memory compromise`_
851*4882a593Smuzhiyunsection for a discussion of the security goals and limitations of
852*4882a593Smuzhiyunthese ioctls.
853*4882a593Smuzhiyun
854*4882a593SmuzhiyunFS_IOC_REMOVE_ENCRYPTION_KEY
855*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~~
856*4882a593Smuzhiyun
857*4882a593SmuzhiyunThe FS_IOC_REMOVE_ENCRYPTION_KEY ioctl removes a claim to a master
858*4882a593Smuzhiyunencryption key from the filesystem, and possibly removes the key
859*4882a593Smuzhiyunitself.  It can be executed on any file or directory on the target
860*4882a593Smuzhiyunfilesystem, but using the filesystem's root directory is recommended.
861*4882a593SmuzhiyunIt takes in a pointer to struct fscrypt_remove_key_arg, defined
862*4882a593Smuzhiyunas follows::
863*4882a593Smuzhiyun
864*4882a593Smuzhiyun    struct fscrypt_remove_key_arg {
865*4882a593Smuzhiyun            struct fscrypt_key_specifier key_spec;
866*4882a593Smuzhiyun    #define FSCRYPT_KEY_REMOVAL_STATUS_FLAG_FILES_BUSY      0x00000001
867*4882a593Smuzhiyun    #define FSCRYPT_KEY_REMOVAL_STATUS_FLAG_OTHER_USERS     0x00000002
868*4882a593Smuzhiyun            __u32 removal_status_flags;     /* output */
869*4882a593Smuzhiyun            __u32 __reserved[5];
870*4882a593Smuzhiyun    };
871*4882a593Smuzhiyun
872*4882a593SmuzhiyunThis structure must be zeroed, then initialized as follows:
873*4882a593Smuzhiyun
874*4882a593Smuzhiyun- The key to remove is specified by ``key_spec``:
875*4882a593Smuzhiyun
876*4882a593Smuzhiyun    - To remove a key used by v1 encryption policies, set
877*4882a593Smuzhiyun      ``key_spec.type`` to FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR and fill
878*4882a593Smuzhiyun      in ``key_spec.u.descriptor``.  To remove this type of key, the
879*4882a593Smuzhiyun      calling process must have the CAP_SYS_ADMIN capability in the
880*4882a593Smuzhiyun      initial user namespace.
881*4882a593Smuzhiyun
882*4882a593Smuzhiyun    - To remove a key used by v2 encryption policies, set
883*4882a593Smuzhiyun      ``key_spec.type`` to FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER and fill
884*4882a593Smuzhiyun      in ``key_spec.u.identifier``.
885*4882a593Smuzhiyun
886*4882a593SmuzhiyunFor v2 policy keys, this ioctl is usable by non-root users.  However,
887*4882a593Smuzhiyunto make this possible, it actually just removes the current user's
888*4882a593Smuzhiyunclaim to the key, undoing a single call to FS_IOC_ADD_ENCRYPTION_KEY.
889*4882a593SmuzhiyunOnly after all claims are removed is the key really removed.
890*4882a593Smuzhiyun
891*4882a593SmuzhiyunFor example, if FS_IOC_ADD_ENCRYPTION_KEY was called with uid 1000,
892*4882a593Smuzhiyunthen the key will be "claimed" by uid 1000, and
893*4882a593SmuzhiyunFS_IOC_REMOVE_ENCRYPTION_KEY will only succeed as uid 1000.  Or, if
894*4882a593Smuzhiyunboth uids 1000 and 2000 added the key, then for each uid
895*4882a593SmuzhiyunFS_IOC_REMOVE_ENCRYPTION_KEY will only remove their own claim.  Only
896*4882a593Smuzhiyunonce *both* are removed is the key really removed.  (Think of it like
897*4882a593Smuzhiyununlinking a file that may have hard links.)
898*4882a593Smuzhiyun
899*4882a593SmuzhiyunIf FS_IOC_REMOVE_ENCRYPTION_KEY really removes the key, it will also
900*4882a593Smuzhiyuntry to "lock" all files that had been unlocked with the key.  It won't
901*4882a593Smuzhiyunlock files that are still in-use, so this ioctl is expected to be used
902*4882a593Smuzhiyunin cooperation with userspace ensuring that none of the files are
903*4882a593Smuzhiyunstill open.  However, if necessary, this ioctl can be executed again
904*4882a593Smuzhiyunlater to retry locking any remaining files.
905*4882a593Smuzhiyun
906*4882a593SmuzhiyunFS_IOC_REMOVE_ENCRYPTION_KEY returns 0 if either the key was removed
907*4882a593Smuzhiyun(but may still have files remaining to be locked), the user's claim to
908*4882a593Smuzhiyunthe key was removed, or the key was already removed but had files
909*4882a593Smuzhiyunremaining to be the locked so the ioctl retried locking them.  In any
910*4882a593Smuzhiyunof these cases, ``removal_status_flags`` is filled in with the
911*4882a593Smuzhiyunfollowing informational status flags:
912*4882a593Smuzhiyun
913*4882a593Smuzhiyun- ``FSCRYPT_KEY_REMOVAL_STATUS_FLAG_FILES_BUSY``: set if some file(s)
914*4882a593Smuzhiyun  are still in-use.  Not guaranteed to be set in the case where only
915*4882a593Smuzhiyun  the user's claim to the key was removed.
916*4882a593Smuzhiyun- ``FSCRYPT_KEY_REMOVAL_STATUS_FLAG_OTHER_USERS``: set if only the
917*4882a593Smuzhiyun  user's claim to the key was removed, not the key itself
918*4882a593Smuzhiyun
919*4882a593SmuzhiyunFS_IOC_REMOVE_ENCRYPTION_KEY can fail with the following errors:
920*4882a593Smuzhiyun
921*4882a593Smuzhiyun- ``EACCES``: The FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR key specifier type
922*4882a593Smuzhiyun  was specified, but the caller does not have the CAP_SYS_ADMIN
923*4882a593Smuzhiyun  capability in the initial user namespace
924*4882a593Smuzhiyun- ``EINVAL``: invalid key specifier type, or reserved bits were set
925*4882a593Smuzhiyun- ``ENOKEY``: the key object was not found at all, i.e. it was never
926*4882a593Smuzhiyun  added in the first place or was already fully removed including all
927*4882a593Smuzhiyun  files locked; or, the user does not have a claim to the key (but
928*4882a593Smuzhiyun  someone else does).
929*4882a593Smuzhiyun- ``ENOTTY``: this type of filesystem does not implement encryption
930*4882a593Smuzhiyun- ``EOPNOTSUPP``: the kernel was not configured with encryption
931*4882a593Smuzhiyun  support for this filesystem, or the filesystem superblock has not
932*4882a593Smuzhiyun  had encryption enabled on it
933*4882a593Smuzhiyun
934*4882a593SmuzhiyunFS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS
935*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
936*4882a593Smuzhiyun
937*4882a593SmuzhiyunFS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS is exactly the same as
938*4882a593Smuzhiyun`FS_IOC_REMOVE_ENCRYPTION_KEY`_, except that for v2 policy keys, the
939*4882a593SmuzhiyunALL_USERS version of the ioctl will remove all users' claims to the
940*4882a593Smuzhiyunkey, not just the current user's.  I.e., the key itself will always be
941*4882a593Smuzhiyunremoved, no matter how many users have added it.  This difference is
942*4882a593Smuzhiyunonly meaningful if non-root users are adding and removing keys.
943*4882a593Smuzhiyun
944*4882a593SmuzhiyunBecause of this, FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS also requires
945*4882a593Smuzhiyun"root", namely the CAP_SYS_ADMIN capability in the initial user
946*4882a593Smuzhiyunnamespace.  Otherwise it will fail with EACCES.
947*4882a593Smuzhiyun
948*4882a593SmuzhiyunGetting key status
949*4882a593Smuzhiyun------------------
950*4882a593Smuzhiyun
951*4882a593SmuzhiyunFS_IOC_GET_ENCRYPTION_KEY_STATUS
952*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
953*4882a593Smuzhiyun
954*4882a593SmuzhiyunThe FS_IOC_GET_ENCRYPTION_KEY_STATUS ioctl retrieves the status of a
955*4882a593Smuzhiyunmaster encryption key.  It can be executed on any file or directory on
956*4882a593Smuzhiyunthe target filesystem, but using the filesystem's root directory is
957*4882a593Smuzhiyunrecommended.  It takes in a pointer to
958*4882a593Smuzhiyunstruct fscrypt_get_key_status_arg, defined as follows::
959*4882a593Smuzhiyun
960*4882a593Smuzhiyun    struct fscrypt_get_key_status_arg {
961*4882a593Smuzhiyun            /* input */
962*4882a593Smuzhiyun            struct fscrypt_key_specifier key_spec;
963*4882a593Smuzhiyun            __u32 __reserved[6];
964*4882a593Smuzhiyun
965*4882a593Smuzhiyun            /* output */
966*4882a593Smuzhiyun    #define FSCRYPT_KEY_STATUS_ABSENT               1
967*4882a593Smuzhiyun    #define FSCRYPT_KEY_STATUS_PRESENT              2
968*4882a593Smuzhiyun    #define FSCRYPT_KEY_STATUS_INCOMPLETELY_REMOVED 3
969*4882a593Smuzhiyun            __u32 status;
970*4882a593Smuzhiyun    #define FSCRYPT_KEY_STATUS_FLAG_ADDED_BY_SELF   0x00000001
971*4882a593Smuzhiyun            __u32 status_flags;
972*4882a593Smuzhiyun            __u32 user_count;
973*4882a593Smuzhiyun            __u32 __out_reserved[13];
974*4882a593Smuzhiyun    };
975*4882a593Smuzhiyun
976*4882a593SmuzhiyunThe caller must zero all input fields, then fill in ``key_spec``:
977*4882a593Smuzhiyun
978*4882a593Smuzhiyun    - To get the status of a key for v1 encryption policies, set
979*4882a593Smuzhiyun      ``key_spec.type`` to FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR and fill
980*4882a593Smuzhiyun      in ``key_spec.u.descriptor``.
981*4882a593Smuzhiyun
982*4882a593Smuzhiyun    - To get the status of a key for v2 encryption policies, set
983*4882a593Smuzhiyun      ``key_spec.type`` to FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER and fill
984*4882a593Smuzhiyun      in ``key_spec.u.identifier``.
985*4882a593Smuzhiyun
986*4882a593SmuzhiyunOn success, 0 is returned and the kernel fills in the output fields:
987*4882a593Smuzhiyun
988*4882a593Smuzhiyun- ``status`` indicates whether the key is absent, present, or
989*4882a593Smuzhiyun  incompletely removed.  Incompletely removed means that the master
990*4882a593Smuzhiyun  secret has been removed, but some files are still in use; i.e.,
991*4882a593Smuzhiyun  `FS_IOC_REMOVE_ENCRYPTION_KEY`_ returned 0 but set the informational
992*4882a593Smuzhiyun  status flag FSCRYPT_KEY_REMOVAL_STATUS_FLAG_FILES_BUSY.
993*4882a593Smuzhiyun
994*4882a593Smuzhiyun- ``status_flags`` can contain the following flags:
995*4882a593Smuzhiyun
996*4882a593Smuzhiyun    - ``FSCRYPT_KEY_STATUS_FLAG_ADDED_BY_SELF`` indicates that the key
997*4882a593Smuzhiyun      has added by the current user.  This is only set for keys
998*4882a593Smuzhiyun      identified by ``identifier`` rather than by ``descriptor``.
999*4882a593Smuzhiyun
1000*4882a593Smuzhiyun- ``user_count`` specifies the number of users who have added the key.
1001*4882a593Smuzhiyun  This is only set for keys identified by ``identifier`` rather than
1002*4882a593Smuzhiyun  by ``descriptor``.
1003*4882a593Smuzhiyun
1004*4882a593SmuzhiyunFS_IOC_GET_ENCRYPTION_KEY_STATUS can fail with the following errors:
1005*4882a593Smuzhiyun
1006*4882a593Smuzhiyun- ``EINVAL``: invalid key specifier type, or reserved bits were set
1007*4882a593Smuzhiyun- ``ENOTTY``: this type of filesystem does not implement encryption
1008*4882a593Smuzhiyun- ``EOPNOTSUPP``: the kernel was not configured with encryption
1009*4882a593Smuzhiyun  support for this filesystem, or the filesystem superblock has not
1010*4882a593Smuzhiyun  had encryption enabled on it
1011*4882a593Smuzhiyun
1012*4882a593SmuzhiyunAmong other use cases, FS_IOC_GET_ENCRYPTION_KEY_STATUS can be useful
1013*4882a593Smuzhiyunfor determining whether the key for a given encrypted directory needs
1014*4882a593Smuzhiyunto be added before prompting the user for the passphrase needed to
1015*4882a593Smuzhiyunderive the key.
1016*4882a593Smuzhiyun
1017*4882a593SmuzhiyunFS_IOC_GET_ENCRYPTION_KEY_STATUS can only get the status of keys in
1018*4882a593Smuzhiyunthe filesystem-level keyring, i.e. the keyring managed by
1019*4882a593Smuzhiyun`FS_IOC_ADD_ENCRYPTION_KEY`_ and `FS_IOC_REMOVE_ENCRYPTION_KEY`_.  It
1020*4882a593Smuzhiyuncannot get the status of a key that has only been added for use by v1
1021*4882a593Smuzhiyunencryption policies using the legacy mechanism involving
1022*4882a593Smuzhiyunprocess-subscribed keyrings.
1023*4882a593Smuzhiyun
1024*4882a593SmuzhiyunAccess semantics
1025*4882a593Smuzhiyun================
1026*4882a593Smuzhiyun
1027*4882a593SmuzhiyunWith the key
1028*4882a593Smuzhiyun------------
1029*4882a593Smuzhiyun
1030*4882a593SmuzhiyunWith the encryption key, encrypted regular files, directories, and
1031*4882a593Smuzhiyunsymlinks behave very similarly to their unencrypted counterparts ---
1032*4882a593Smuzhiyunafter all, the encryption is intended to be transparent.  However,
1033*4882a593Smuzhiyunastute users may notice some differences in behavior:
1034*4882a593Smuzhiyun
1035*4882a593Smuzhiyun- Unencrypted files, or files encrypted with a different encryption
1036*4882a593Smuzhiyun  policy (i.e. different key, modes, or flags), cannot be renamed or
1037*4882a593Smuzhiyun  linked into an encrypted directory; see `Encryption policy
1038*4882a593Smuzhiyun  enforcement`_.  Attempts to do so will fail with EXDEV.  However,
1039*4882a593Smuzhiyun  encrypted files can be renamed within an encrypted directory, or
1040*4882a593Smuzhiyun  into an unencrypted directory.
1041*4882a593Smuzhiyun
1042*4882a593Smuzhiyun  Note: "moving" an unencrypted file into an encrypted directory, e.g.
1043*4882a593Smuzhiyun  with the `mv` program, is implemented in userspace by a copy
1044*4882a593Smuzhiyun  followed by a delete.  Be aware that the original unencrypted data
1045*4882a593Smuzhiyun  may remain recoverable from free space on the disk; prefer to keep
1046*4882a593Smuzhiyun  all files encrypted from the very beginning.  The `shred` program
1047*4882a593Smuzhiyun  may be used to overwrite the source files but isn't guaranteed to be
1048*4882a593Smuzhiyun  effective on all filesystems and storage devices.
1049*4882a593Smuzhiyun
1050*4882a593Smuzhiyun- Direct I/O is not supported on encrypted files.  Attempts to use
1051*4882a593Smuzhiyun  direct I/O on such files will fall back to buffered I/O.
1052*4882a593Smuzhiyun
1053*4882a593Smuzhiyun- The fallocate operations FALLOC_FL_COLLAPSE_RANGE and
1054*4882a593Smuzhiyun  FALLOC_FL_INSERT_RANGE are not supported on encrypted files and will
1055*4882a593Smuzhiyun  fail with EOPNOTSUPP.
1056*4882a593Smuzhiyun
1057*4882a593Smuzhiyun- Online defragmentation of encrypted files is not supported.  The
1058*4882a593Smuzhiyun  EXT4_IOC_MOVE_EXT and F2FS_IOC_MOVE_RANGE ioctls will fail with
1059*4882a593Smuzhiyun  EOPNOTSUPP.
1060*4882a593Smuzhiyun
1061*4882a593Smuzhiyun- The ext4 filesystem does not support data journaling with encrypted
1062*4882a593Smuzhiyun  regular files.  It will fall back to ordered data mode instead.
1063*4882a593Smuzhiyun
1064*4882a593Smuzhiyun- DAX (Direct Access) is not supported on encrypted files.
1065*4882a593Smuzhiyun
1066*4882a593Smuzhiyun- The st_size of an encrypted symlink will not necessarily give the
1067*4882a593Smuzhiyun  length of the symlink target as required by POSIX.  It will actually
1068*4882a593Smuzhiyun  give the length of the ciphertext, which will be slightly longer
1069*4882a593Smuzhiyun  than the plaintext due to NUL-padding and an extra 2-byte overhead.
1070*4882a593Smuzhiyun
1071*4882a593Smuzhiyun- The maximum length of an encrypted symlink is 2 bytes shorter than
1072*4882a593Smuzhiyun  the maximum length of an unencrypted symlink.  For example, on an
1073*4882a593Smuzhiyun  EXT4 filesystem with a 4K block size, unencrypted symlinks can be up
1074*4882a593Smuzhiyun  to 4095 bytes long, while encrypted symlinks can only be up to 4093
1075*4882a593Smuzhiyun  bytes long (both lengths excluding the terminating null).
1076*4882a593Smuzhiyun
1077*4882a593SmuzhiyunNote that mmap *is* supported.  This is possible because the pagecache
1078*4882a593Smuzhiyunfor an encrypted file contains the plaintext, not the ciphertext.
1079*4882a593Smuzhiyun
1080*4882a593SmuzhiyunWithout the key
1081*4882a593Smuzhiyun---------------
1082*4882a593Smuzhiyun
1083*4882a593SmuzhiyunSome filesystem operations may be performed on encrypted regular
1084*4882a593Smuzhiyunfiles, directories, and symlinks even before their encryption key has
1085*4882a593Smuzhiyunbeen added, or after their encryption key has been removed:
1086*4882a593Smuzhiyun
1087*4882a593Smuzhiyun- File metadata may be read, e.g. using stat().
1088*4882a593Smuzhiyun
1089*4882a593Smuzhiyun- Directories may be listed, in which case the filenames will be
1090*4882a593Smuzhiyun  listed in an encoded form derived from their ciphertext.  The
1091*4882a593Smuzhiyun  current encoding algorithm is described in `Filename hashing and
1092*4882a593Smuzhiyun  encoding`_.  The algorithm is subject to change, but it is
1093*4882a593Smuzhiyun  guaranteed that the presented filenames will be no longer than
1094*4882a593Smuzhiyun  NAME_MAX bytes, will not contain the ``/`` or ``\0`` characters, and
1095*4882a593Smuzhiyun  will uniquely identify directory entries.
1096*4882a593Smuzhiyun
1097*4882a593Smuzhiyun  The ``.`` and ``..`` directory entries are special.  They are always
1098*4882a593Smuzhiyun  present and are not encrypted or encoded.
1099*4882a593Smuzhiyun
1100*4882a593Smuzhiyun- Files may be deleted.  That is, nondirectory files may be deleted
1101*4882a593Smuzhiyun  with unlink() as usual, and empty directories may be deleted with
1102*4882a593Smuzhiyun  rmdir() as usual.  Therefore, ``rm`` and ``rm -r`` will work as
1103*4882a593Smuzhiyun  expected.
1104*4882a593Smuzhiyun
1105*4882a593Smuzhiyun- Symlink targets may be read and followed, but they will be presented
1106*4882a593Smuzhiyun  in encrypted form, similar to filenames in directories.  Hence, they
1107*4882a593Smuzhiyun  are unlikely to point to anywhere useful.
1108*4882a593Smuzhiyun
1109*4882a593SmuzhiyunWithout the key, regular files cannot be opened or truncated.
1110*4882a593SmuzhiyunAttempts to do so will fail with ENOKEY.  This implies that any
1111*4882a593Smuzhiyunregular file operations that require a file descriptor, such as
1112*4882a593Smuzhiyunread(), write(), mmap(), fallocate(), and ioctl(), are also forbidden.
1113*4882a593Smuzhiyun
1114*4882a593SmuzhiyunAlso without the key, files of any type (including directories) cannot
1115*4882a593Smuzhiyunbe created or linked into an encrypted directory, nor can a name in an
1116*4882a593Smuzhiyunencrypted directory be the source or target of a rename, nor can an
1117*4882a593SmuzhiyunO_TMPFILE temporary file be created in an encrypted directory.  All
1118*4882a593Smuzhiyunsuch operations will fail with ENOKEY.
1119*4882a593Smuzhiyun
1120*4882a593SmuzhiyunIt is not currently possible to backup and restore encrypted files
1121*4882a593Smuzhiyunwithout the encryption key.  This would require special APIs which
1122*4882a593Smuzhiyunhave not yet been implemented.
1123*4882a593Smuzhiyun
1124*4882a593SmuzhiyunEncryption policy enforcement
1125*4882a593Smuzhiyun=============================
1126*4882a593Smuzhiyun
1127*4882a593SmuzhiyunAfter an encryption policy has been set on a directory, all regular
1128*4882a593Smuzhiyunfiles, directories, and symbolic links created in that directory
1129*4882a593Smuzhiyun(recursively) will inherit that encryption policy.  Special files ---
1130*4882a593Smuzhiyunthat is, named pipes, device nodes, and UNIX domain sockets --- will
1131*4882a593Smuzhiyunnot be encrypted.
1132*4882a593Smuzhiyun
1133*4882a593SmuzhiyunExcept for those special files, it is forbidden to have unencrypted
1134*4882a593Smuzhiyunfiles, or files encrypted with a different encryption policy, in an
1135*4882a593Smuzhiyunencrypted directory tree.  Attempts to link or rename such a file into
1136*4882a593Smuzhiyunan encrypted directory will fail with EXDEV.  This is also enforced
1137*4882a593Smuzhiyunduring ->lookup() to provide limited protection against offline
1138*4882a593Smuzhiyunattacks that try to disable or downgrade encryption in known locations
1139*4882a593Smuzhiyunwhere applications may later write sensitive data.  It is recommended
1140*4882a593Smuzhiyunthat systems implementing a form of "verified boot" take advantage of
1141*4882a593Smuzhiyunthis by validating all top-level encryption policies prior to access.
1142*4882a593Smuzhiyun
1143*4882a593SmuzhiyunImplementation details
1144*4882a593Smuzhiyun======================
1145*4882a593Smuzhiyun
1146*4882a593SmuzhiyunEncryption context
1147*4882a593Smuzhiyun------------------
1148*4882a593Smuzhiyun
1149*4882a593SmuzhiyunAn encryption policy is represented on-disk by
1150*4882a593Smuzhiyunstruct fscrypt_context_v1 or struct fscrypt_context_v2.  It is up to
1151*4882a593Smuzhiyunindividual filesystems to decide where to store it, but normally it
1152*4882a593Smuzhiyunwould be stored in a hidden extended attribute.  It should *not* be
1153*4882a593Smuzhiyunexposed by the xattr-related system calls such as getxattr() and
1154*4882a593Smuzhiyunsetxattr() because of the special semantics of the encryption xattr.
1155*4882a593Smuzhiyun(In particular, there would be much confusion if an encryption policy
1156*4882a593Smuzhiyunwere to be added to or removed from anything other than an empty
1157*4882a593Smuzhiyundirectory.)  These structs are defined as follows::
1158*4882a593Smuzhiyun
1159*4882a593Smuzhiyun    #define FSCRYPT_FILE_NONCE_SIZE 16
1160*4882a593Smuzhiyun
1161*4882a593Smuzhiyun    #define FSCRYPT_KEY_DESCRIPTOR_SIZE  8
1162*4882a593Smuzhiyun    struct fscrypt_context_v1 {
1163*4882a593Smuzhiyun            u8 version;
1164*4882a593Smuzhiyun            u8 contents_encryption_mode;
1165*4882a593Smuzhiyun            u8 filenames_encryption_mode;
1166*4882a593Smuzhiyun            u8 flags;
1167*4882a593Smuzhiyun            u8 master_key_descriptor[FSCRYPT_KEY_DESCRIPTOR_SIZE];
1168*4882a593Smuzhiyun            u8 nonce[FSCRYPT_FILE_NONCE_SIZE];
1169*4882a593Smuzhiyun    };
1170*4882a593Smuzhiyun
1171*4882a593Smuzhiyun    #define FSCRYPT_KEY_IDENTIFIER_SIZE  16
1172*4882a593Smuzhiyun    struct fscrypt_context_v2 {
1173*4882a593Smuzhiyun            u8 version;
1174*4882a593Smuzhiyun            u8 contents_encryption_mode;
1175*4882a593Smuzhiyun            u8 filenames_encryption_mode;
1176*4882a593Smuzhiyun            u8 flags;
1177*4882a593Smuzhiyun            u8 __reserved[4];
1178*4882a593Smuzhiyun            u8 master_key_identifier[FSCRYPT_KEY_IDENTIFIER_SIZE];
1179*4882a593Smuzhiyun            u8 nonce[FSCRYPT_FILE_NONCE_SIZE];
1180*4882a593Smuzhiyun    };
1181*4882a593Smuzhiyun
1182*4882a593SmuzhiyunThe context structs contain the same information as the corresponding
1183*4882a593Smuzhiyunpolicy structs (see `Setting an encryption policy`_), except that the
1184*4882a593Smuzhiyuncontext structs also contain a nonce.  The nonce is randomly generated
1185*4882a593Smuzhiyunby the kernel and is used as KDF input or as a tweak to cause
1186*4882a593Smuzhiyundifferent files to be encrypted differently; see `Per-file encryption
1187*4882a593Smuzhiyunkeys`_ and `DIRECT_KEY policies`_.
1188*4882a593Smuzhiyun
1189*4882a593SmuzhiyunData path changes
1190*4882a593Smuzhiyun-----------------
1191*4882a593Smuzhiyun
1192*4882a593SmuzhiyunFor the read path (->readpage()) of regular files, filesystems can
1193*4882a593Smuzhiyunread the ciphertext into the page cache and decrypt it in-place.  The
1194*4882a593Smuzhiyunpage lock must be held until decryption has finished, to prevent the
1195*4882a593Smuzhiyunpage from becoming visible to userspace prematurely.
1196*4882a593Smuzhiyun
1197*4882a593SmuzhiyunFor the write path (->writepage()) of regular files, filesystems
1198*4882a593Smuzhiyuncannot encrypt data in-place in the page cache, since the cached
1199*4882a593Smuzhiyunplaintext must be preserved.  Instead, filesystems must encrypt into a
1200*4882a593Smuzhiyuntemporary buffer or "bounce page", then write out the temporary
1201*4882a593Smuzhiyunbuffer.  Some filesystems, such as UBIFS, already use temporary
1202*4882a593Smuzhiyunbuffers regardless of encryption.  Other filesystems, such as ext4 and
1203*4882a593SmuzhiyunF2FS, have to allocate bounce pages specially for encryption.
1204*4882a593Smuzhiyun
1205*4882a593SmuzhiyunFscrypt is also able to use inline encryption hardware instead of the
1206*4882a593Smuzhiyunkernel crypto API for en/decryption of file contents.  When possible,
1207*4882a593Smuzhiyunand if directed to do so (by specifying the 'inlinecrypt' mount option
1208*4882a593Smuzhiyunfor an ext4/F2FS filesystem), it adds encryption contexts to bios and
1209*4882a593Smuzhiyunuses blk-crypto to perform the en/decryption instead of making use of
1210*4882a593Smuzhiyunthe above read/write path changes.  Of course, even if directed to
1211*4882a593Smuzhiyunmake use of inline encryption, fscrypt will only be able to do so if
1212*4882a593Smuzhiyuneither hardware inline encryption support is available for the
1213*4882a593Smuzhiyunselected encryption algorithm or CONFIG_BLK_INLINE_ENCRYPTION_FALLBACK
1214*4882a593Smuzhiyunis selected.  If neither is the case, fscrypt will fall back to using
1215*4882a593Smuzhiyunthe above mentioned read/write path changes for en/decryption.
1216*4882a593Smuzhiyun
1217*4882a593SmuzhiyunFilename hashing and encoding
1218*4882a593Smuzhiyun-----------------------------
1219*4882a593Smuzhiyun
1220*4882a593SmuzhiyunModern filesystems accelerate directory lookups by using indexed
1221*4882a593Smuzhiyundirectories.  An indexed directory is organized as a tree keyed by
1222*4882a593Smuzhiyunfilename hashes.  When a ->lookup() is requested, the filesystem
1223*4882a593Smuzhiyunnormally hashes the filename being looked up so that it can quickly
1224*4882a593Smuzhiyunfind the corresponding directory entry, if any.
1225*4882a593Smuzhiyun
1226*4882a593SmuzhiyunWith encryption, lookups must be supported and efficient both with and
1227*4882a593Smuzhiyunwithout the encryption key.  Clearly, it would not work to hash the
1228*4882a593Smuzhiyunplaintext filenames, since the plaintext filenames are unavailable
1229*4882a593Smuzhiyunwithout the key.  (Hashing the plaintext filenames would also make it
1230*4882a593Smuzhiyunimpossible for the filesystem's fsck tool to optimize encrypted
1231*4882a593Smuzhiyundirectories.)  Instead, filesystems hash the ciphertext filenames,
1232*4882a593Smuzhiyuni.e. the bytes actually stored on-disk in the directory entries.  When
1233*4882a593Smuzhiyunasked to do a ->lookup() with the key, the filesystem just encrypts
1234*4882a593Smuzhiyunthe user-supplied name to get the ciphertext.
1235*4882a593Smuzhiyun
1236*4882a593SmuzhiyunLookups without the key are more complicated.  The raw ciphertext may
1237*4882a593Smuzhiyuncontain the ``\0`` and ``/`` characters, which are illegal in
1238*4882a593Smuzhiyunfilenames.  Therefore, readdir() must base64-encode the ciphertext for
1239*4882a593Smuzhiyunpresentation.  For most filenames, this works fine; on ->lookup(), the
1240*4882a593Smuzhiyunfilesystem just base64-decodes the user-supplied name to get back to
1241*4882a593Smuzhiyunthe raw ciphertext.
1242*4882a593Smuzhiyun
1243*4882a593SmuzhiyunHowever, for very long filenames, base64 encoding would cause the
1244*4882a593Smuzhiyunfilename length to exceed NAME_MAX.  To prevent this, readdir()
1245*4882a593Smuzhiyunactually presents long filenames in an abbreviated form which encodes
1246*4882a593Smuzhiyuna strong "hash" of the ciphertext filename, along with the optional
1247*4882a593Smuzhiyunfilesystem-specific hash(es) needed for directory lookups.  This
1248*4882a593Smuzhiyunallows the filesystem to still, with a high degree of confidence, map
1249*4882a593Smuzhiyunthe filename given in ->lookup() back to a particular directory entry
1250*4882a593Smuzhiyunthat was previously listed by readdir().  See
1251*4882a593Smuzhiyunstruct fscrypt_nokey_name in the source for more details.
1252*4882a593Smuzhiyun
1253*4882a593SmuzhiyunNote that the precise way that filenames are presented to userspace
1254*4882a593Smuzhiyunwithout the key is subject to change in the future.  It is only meant
1255*4882a593Smuzhiyunas a way to temporarily present valid filenames so that commands like
1256*4882a593Smuzhiyun``rm -r`` work as expected on encrypted directories.
1257*4882a593Smuzhiyun
1258*4882a593SmuzhiyunTests
1259*4882a593Smuzhiyun=====
1260*4882a593Smuzhiyun
1261*4882a593SmuzhiyunTo test fscrypt, use xfstests, which is Linux's de facto standard
1262*4882a593Smuzhiyunfilesystem test suite.  First, run all the tests in the "encrypt"
1263*4882a593Smuzhiyungroup on the relevant filesystem(s).  One can also run the tests
1264*4882a593Smuzhiyunwith the 'inlinecrypt' mount option to test the implementation for
1265*4882a593Smuzhiyuninline encryption support.  For example, to test ext4 and
1266*4882a593Smuzhiyunf2fs encryption using `kvm-xfstests
1267*4882a593Smuzhiyun<https://github.com/tytso/xfstests-bld/blob/master/Documentation/kvm-quickstart.md>`_::
1268*4882a593Smuzhiyun
1269*4882a593Smuzhiyun    kvm-xfstests -c ext4,f2fs -g encrypt
1270*4882a593Smuzhiyun    kvm-xfstests -c ext4,f2fs -g encrypt -m inlinecrypt
1271*4882a593Smuzhiyun
1272*4882a593SmuzhiyunUBIFS encryption can also be tested this way, but it should be done in
1273*4882a593Smuzhiyuna separate command, and it takes some time for kvm-xfstests to set up
1274*4882a593Smuzhiyunemulated UBI volumes::
1275*4882a593Smuzhiyun
1276*4882a593Smuzhiyun    kvm-xfstests -c ubifs -g encrypt
1277*4882a593Smuzhiyun
1278*4882a593SmuzhiyunNo tests should fail.  However, tests that use non-default encryption
1279*4882a593Smuzhiyunmodes (e.g. generic/549 and generic/550) will be skipped if the needed
1280*4882a593Smuzhiyunalgorithms were not built into the kernel's crypto API.  Also, tests
1281*4882a593Smuzhiyunthat access the raw block device (e.g. generic/399, generic/548,
1282*4882a593Smuzhiyungeneric/549, generic/550) will be skipped on UBIFS.
1283*4882a593Smuzhiyun
1284*4882a593SmuzhiyunBesides running the "encrypt" group tests, for ext4 and f2fs it's also
1285*4882a593Smuzhiyunpossible to run most xfstests with the "test_dummy_encryption" mount
1286*4882a593Smuzhiyunoption.  This option causes all new files to be automatically
1287*4882a593Smuzhiyunencrypted with a dummy key, without having to make any API calls.
1288*4882a593SmuzhiyunThis tests the encrypted I/O paths more thoroughly.  To do this with
1289*4882a593Smuzhiyunkvm-xfstests, use the "encrypt" filesystem configuration::
1290*4882a593Smuzhiyun
1291*4882a593Smuzhiyun    kvm-xfstests -c ext4/encrypt,f2fs/encrypt -g auto
1292*4882a593Smuzhiyun    kvm-xfstests -c ext4/encrypt,f2fs/encrypt -g auto -m inlinecrypt
1293*4882a593Smuzhiyun
1294*4882a593SmuzhiyunBecause this runs many more tests than "-g encrypt" does, it takes
1295*4882a593Smuzhiyunmuch longer to run; so also consider using `gce-xfstests
1296*4882a593Smuzhiyun<https://github.com/tytso/xfstests-bld/blob/master/Documentation/gce-xfstests.md>`_
1297*4882a593Smuzhiyuninstead of kvm-xfstests::
1298*4882a593Smuzhiyun
1299*4882a593Smuzhiyun    gce-xfstests -c ext4/encrypt,f2fs/encrypt -g auto
1300*4882a593Smuzhiyun    gce-xfstests -c ext4/encrypt,f2fs/encrypt -g auto -m inlinecrypt
1301