diff options
-rw-r--r-- | Documentation/filesystems/fscrypt.rst | 758 | ||||
-rw-r--r-- | MAINTAINERS | 1 | ||||
-rw-r--r-- | fs/crypto/Kconfig | 2 | ||||
-rw-r--r-- | fs/crypto/Makefile | 10 | ||||
-rw-r--r-- | fs/crypto/crypto.c | 45 | ||||
-rw-r--r-- | fs/crypto/fname.c | 47 | ||||
-rw-r--r-- | fs/crypto/fscrypt_private.h | 399 | ||||
-rw-r--r-- | fs/crypto/hkdf.c | 181 | ||||
-rw-r--r-- | fs/crypto/hooks.c | 6 | ||||
-rw-r--r-- | fs/crypto/keyinfo.c | 611 | ||||
-rw-r--r-- | fs/crypto/keyring.c | 984 | ||||
-rw-r--r-- | fs/crypto/keysetup.c | 591 | ||||
-rw-r--r-- | fs/crypto/keysetup_v1.c | 340 | ||||
-rw-r--r-- | fs/crypto/policy.c | 434 | ||||
-rw-r--r-- | fs/ext4/ioctl.c | 32 | ||||
-rw-r--r-- | fs/ext4/super.c | 3 | ||||
-rw-r--r-- | fs/f2fs/file.c | 58 | ||||
-rw-r--r-- | fs/f2fs/super.c | 2 | ||||
-rw-r--r-- | fs/super.c | 2 | ||||
-rw-r--r-- | fs/ubifs/ioctl.c | 20 | ||||
-rw-r--r-- | fs/ubifs/super.c | 11 | ||||
-rw-r--r-- | include/linux/fs.h | 1 | ||||
-rw-r--r-- | include/linux/fscrypt.h | 55 | ||||
-rw-r--r-- | include/uapi/linux/fs.h | 54 | ||||
-rw-r--r-- | include/uapi/linux/fscrypt.h | 181 |
25 files changed, 3827 insertions, 1001 deletions
diff --git a/Documentation/filesystems/fscrypt.rst b/Documentation/filesystems/fscrypt.rst index 82efa41b0e6c..8a0700af9596 100644 --- a/Documentation/filesystems/fscrypt.rst +++ b/Documentation/filesystems/fscrypt.rst | |||
@@ -72,6 +72,9 @@ Online attacks | |||
72 | fscrypt (and storage encryption in general) can only provide limited | 72 | fscrypt (and storage encryption in general) can only provide limited |
73 | protection, if any at all, against online attacks. In detail: | 73 | protection, if any at all, against online attacks. In detail: |
74 | 74 | ||
75 | Side-channel attacks | ||
76 | ~~~~~~~~~~~~~~~~~~~~ | ||
77 | |||
75 | fscrypt is only resistant to side-channel attacks, such as timing or | 78 | fscrypt is only resistant to side-channel attacks, such as timing or |
76 | electromagnetic attacks, to the extent that the underlying Linux | 79 | electromagnetic attacks, to the extent that the underlying Linux |
77 | Cryptographic API algorithms are. If a vulnerable algorithm is used, | 80 | Cryptographic API algorithms are. If a vulnerable algorithm is used, |
@@ -80,29 +83,90 @@ attacker to mount a side channel attack against the online system. | |||
80 | Side channel attacks may also be mounted against applications | 83 | Side channel attacks may also be mounted against applications |
81 | consuming decrypted data. | 84 | consuming decrypted data. |
82 | 85 | ||
83 | After an encryption key has been provided, fscrypt is not designed to | 86 | Unauthorized file access |
84 | hide the plaintext file contents or filenames from other users on the | 87 | ~~~~~~~~~~~~~~~~~~~~~~~~ |
85 | same system, regardless of the visibility of the keyring key. | 88 | |
86 | Instead, existing access control mechanisms such as file mode bits, | 89 | After an encryption key has been added, fscrypt does not hide the |
87 | POSIX ACLs, LSMs, or mount namespaces should be used for this purpose. | 90 | plaintext file contents or filenames from other users on the same |
88 | Also note that as long as the encryption keys are *anywhere* in | 91 | system. Instead, existing access control mechanisms such as file mode |
89 | memory, an online attacker can necessarily compromise them by mounting | 92 | bits, POSIX ACLs, LSMs, or namespaces should be used for this purpose. |
90 | a physical attack or by exploiting any kernel security vulnerability | 93 | |
91 | which provides an arbitrary memory read primitive. | 94 | (For the reasoning behind this, understand that while the key is |
92 | 95 | added, the confidentiality of the data, from the perspective of the | |
93 | While it is ostensibly possible to "evict" keys from the system, | 96 | system itself, is *not* protected by the mathematical properties of |
94 | recently accessed encrypted files will remain accessible at least | 97 | encryption but rather only by the correctness of the kernel. |
95 | until the filesystem is unmounted or the VFS caches are dropped, e.g. | 98 | Therefore, any encryption-specific access control checks would merely |
96 | using ``echo 2 > /proc/sys/vm/drop_caches``. Even after that, if the | 99 | be enforced by kernel *code* and therefore would be largely redundant |
97 | RAM is compromised before being powered off, it will likely still be | 100 | with the wide variety of access control mechanisms already available.) |
98 | possible to recover portions of the plaintext file contents, if not | 101 | |
99 | some of the encryption keys as well. (Since Linux v4.12, all | 102 | Kernel memory compromise |
100 | in-kernel keys related to fscrypt are sanitized before being freed. | 103 | ~~~~~~~~~~~~~~~~~~~~~~~~ |
101 | However, userspace would need to do its part as well.) | 104 | |
102 | 105 | An attacker who compromises the system enough to read from arbitrary | |
103 | Currently, fscrypt does not prevent a user from maliciously providing | 106 | memory, e.g. by mounting a physical attack or by exploiting a kernel |
104 | an incorrect key for another user's existing encrypted files. A | 107 | security vulnerability, can compromise all encryption keys that are |
105 | protection against this is planned. | 108 | currently in use. |
109 | |||
110 | However, fscrypt allows encryption keys to be removed from the kernel, | ||
111 | which may protect them from later compromise. | ||
112 | |||
113 | In more detail, the FS_IOC_REMOVE_ENCRYPTION_KEY ioctl (or the | ||
114 | FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS ioctl) can wipe a master | ||
115 | encryption key from kernel memory. If it does so, it will also try to | ||
116 | evict all cached inodes which had been "unlocked" using the key, | ||
117 | thereby wiping their per-file keys and making them once again appear | ||
118 | "locked", i.e. in ciphertext or encrypted form. | ||
119 | |||
120 | However, these ioctls have some limitations: | ||
121 | |||
122 | - Per-file keys for in-use files will *not* be removed or wiped. | ||
123 | Therefore, for maximum effect, userspace should close the relevant | ||
124 | encrypted files and directories before removing a master key, as | ||
125 | well as kill any processes whose working directory is in an affected | ||
126 | encrypted directory. | ||
127 | |||
128 | - The kernel cannot magically wipe copies of the master key(s) that | ||
129 | userspace might have as well. Therefore, userspace must wipe all | ||
130 | copies of the master key(s) it makes as well; normally this should | ||
131 | be done immediately after FS_IOC_ADD_ENCRYPTION_KEY, without waiting | ||
132 | for FS_IOC_REMOVE_ENCRYPTION_KEY. Naturally, the same also applies | ||
133 | to all higher levels in the key hierarchy. Userspace should also | ||
134 | follow other security precautions such as mlock()ing memory | ||
135 | containing keys to prevent it from being swapped out. | ||
136 | |||
137 | - In general, decrypted contents and filenames in the kernel VFS | ||
138 | caches are freed but not wiped. Therefore, portions thereof may be | ||
139 | recoverable from freed memory, even after the corresponding key(s) | ||
140 | were wiped. To partially solve this, you can set | ||
141 | CONFIG_PAGE_POISONING=y in your kernel config and add page_poison=1 | ||
142 | to your kernel command line. However, this has a performance cost. | ||
143 | |||
144 | - Secret keys might still exist in CPU registers, in crypto | ||
145 | accelerator hardware (if used by the crypto API to implement any of | ||
146 | the algorithms), or in other places not explicitly considered here. | ||
147 | |||
148 | Limitations of v1 policies | ||
149 | ~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
150 | |||
151 | v1 encryption policies have some weaknesses with respect to online | ||
152 | attacks: | ||
153 | |||
154 | - There is no verification that the provided master key is correct. | ||
155 | Therefore, a malicious user can temporarily associate the wrong key | ||
156 | with another user's encrypted files to which they have read-only | ||
157 | access. Because of filesystem caching, the wrong key will then be | ||
158 | used by the other user's accesses to those files, even if the other | ||
159 | user has the correct key in their own keyring. This violates the | ||
160 | meaning of "read-only access". | ||
161 | |||
162 | - A compromise of a per-file key also compromises the master key from | ||
163 | which it was derived. | ||
164 | |||
165 | - Non-root users cannot securely remove encryption keys. | ||
166 | |||
167 | All the above problems are fixed with v2 encryption policies. For | ||
168 | this reason among others, it is recommended to use v2 encryption | ||
169 | policies on all new encrypted directories. | ||
106 | 170 | ||
107 | Key hierarchy | 171 | Key hierarchy |
108 | ============= | 172 | ============= |
@@ -123,11 +187,52 @@ appropriate master key. There can be any number of master keys, each | |||
123 | of which protects any number of directory trees on any number of | 187 | of which protects any number of directory trees on any number of |
124 | filesystems. | 188 | filesystems. |
125 | 189 | ||
126 | Userspace should generate master keys either using a cryptographically | 190 | Master keys must be real cryptographic keys, i.e. indistinguishable |
127 | secure random number generator, or by using a KDF (Key Derivation | 191 | from random bytestrings of the same length. This implies that users |
128 | Function). Note that whenever a KDF is used to "stretch" a | 192 | **must not** directly use a password as a master key, zero-pad a |
129 | lower-entropy secret such as a passphrase, it is critical that a KDF | 193 | shorter key, or repeat a shorter key. Security cannot be guaranteed |
130 | designed for this purpose be used, such as scrypt, PBKDF2, or Argon2. | 194 | if userspace makes any such error, as the cryptographic proofs and |
195 | analysis would no longer apply. | ||
196 | |||
197 | Instead, users should generate master keys either using a | ||
198 | cryptographically secure random number generator, or by using a KDF | ||
199 | (Key Derivation Function). The kernel does not do any key stretching; | ||
200 | therefore, if userspace derives the key from a low-entropy secret such | ||
201 | as a passphrase, it is critical that a KDF designed for this purpose | ||
202 | be used, such as scrypt, PBKDF2, or Argon2. | ||
203 | |||
204 | Key derivation function | ||
205 | ----------------------- | ||
206 | |||
207 | With one exception, fscrypt never uses the master key(s) for | ||
208 | encryption directly. Instead, they are only used as input to a KDF | ||
209 | (Key Derivation Function) to derive the actual keys. | ||
210 | |||
211 | The KDF used for a particular master key differs depending on whether | ||
212 | the key is used for v1 encryption policies or for v2 encryption | ||
213 | policies. Users **must not** use the same key for both v1 and v2 | ||
214 | encryption policies. (No real-world attack is currently known on this | ||
215 | specific case of key reuse, but its security cannot be guaranteed | ||
216 | since the cryptographic proofs and analysis would no longer apply.) | ||
217 | |||
218 | For v1 encryption policies, the KDF only supports deriving per-file | ||
219 | encryption keys. It works by encrypting the master key with | ||
220 | AES-128-ECB, using the file's 16-byte nonce as the AES key. The | ||
221 | resulting ciphertext is used as the derived key. If the ciphertext is | ||
222 | longer than needed, then it is truncated to the needed length. | ||
223 | |||
224 | For v2 encryption policies, the KDF is HKDF-SHA512. The master key is | ||
225 | passed as the "input keying material", no salt is used, and a distinct | ||
226 | "application-specific information string" is used for each distinct | ||
227 | key to be derived. For example, when a per-file encryption key is | ||
228 | derived, the application-specific information string is the file's | ||
229 | nonce prefixed with "fscrypt\\0" and a context byte. Different | ||
230 | context bytes are used for other types of derived keys. | ||
231 | |||
232 | HKDF-SHA512 is preferred to the original AES-128-ECB based KDF because | ||
233 | HKDF is more flexible, is nonreversible, and evenly distributes | ||
234 | entropy from the master key. HKDF is also standardized and widely | ||
235 | used by other software, whereas the AES-128-ECB based KDF is ad-hoc. | ||
131 | 236 | ||
132 | Per-file keys | 237 | Per-file keys |
133 | ------------- | 238 | ------------- |
@@ -138,29 +243,9 @@ files doesn't map to the same ciphertext, or vice versa. In most | |||
138 | cases, fscrypt does this by deriving per-file keys. When a new | 243 | cases, fscrypt does this by deriving per-file keys. When a new |
139 | encrypted inode (regular file, directory, or symlink) is created, | 244 | encrypted inode (regular file, directory, or symlink) is created, |
140 | fscrypt randomly generates a 16-byte nonce and stores it in the | 245 | fscrypt randomly generates a 16-byte nonce and stores it in the |
141 | inode's encryption xattr. Then, it uses a KDF (Key Derivation | 246 | inode's encryption xattr. Then, it uses a KDF (as described in `Key |
142 | Function) to derive the file's key from the master key and nonce. | 247 | derivation function`_) to derive the file's key from the master key |
143 | 248 | and nonce. | |
144 | The Adiantum encryption mode (see `Encryption modes and usage`_) is | ||
145 | special, since it accepts longer IVs and is suitable for both contents | ||
146 | and filenames encryption. For it, a "direct key" option is offered | ||
147 | where the file's nonce is included in the IVs and the master key is | ||
148 | used for encryption directly. This improves performance; however, | ||
149 | users must not use the same master key for any other encryption mode. | ||
150 | |||
151 | Below, the KDF and design considerations are described in more detail. | ||
152 | |||
153 | The current KDF works by encrypting the master key with AES-128-ECB, | ||
154 | using the file's nonce as the AES key. The output is used as the | ||
155 | derived key. If the output is longer than needed, then it is | ||
156 | truncated to the needed length. | ||
157 | |||
158 | Note: this KDF meets the primary security requirement, which is to | ||
159 | produce unique derived keys that preserve the entropy of the master | ||
160 | key, assuming that the master key is already a good pseudorandom key. | ||
161 | However, it is nonstandard and has some problems such as being | ||
162 | reversible, so it is generally considered to be a mistake! It may be | ||
163 | replaced with HKDF or another more standard KDF in the future. | ||
164 | 249 | ||
165 | Key derivation was chosen over key wrapping because wrapped keys would | 250 | Key derivation was chosen over key wrapping because wrapped keys would |
166 | require larger xattrs which would be less likely to fit in-line in the | 251 | require larger xattrs which would be less likely to fit in-line in the |
@@ -176,6 +261,37 @@ rejected as it would have prevented ext4 filesystems from being | |||
176 | resized, and by itself still wouldn't have been sufficient to prevent | 261 | resized, and by itself still wouldn't have been sufficient to prevent |
177 | the same key from being directly reused for both XTS and CTS-CBC. | 262 | the same key from being directly reused for both XTS and CTS-CBC. |
178 | 263 | ||
264 | DIRECT_KEY and per-mode keys | ||
265 | ---------------------------- | ||
266 | |||
267 | The Adiantum encryption mode (see `Encryption modes and usage`_) is | ||
268 | suitable for both contents and filenames encryption, and it accepts | ||
269 | long IVs --- long enough to hold both an 8-byte logical block number | ||
270 | and a 16-byte per-file nonce. Also, the overhead of each Adiantum key | ||
271 | is greater than that of an AES-256-XTS key. | ||
272 | |||
273 | Therefore, to improve performance and save memory, for Adiantum a | ||
274 | "direct key" configuration is supported. When the user has enabled | ||
275 | this by setting FSCRYPT_POLICY_FLAG_DIRECT_KEY in the fscrypt policy, | ||
276 | per-file keys are not used. Instead, whenever any data (contents or | ||
277 | filenames) is encrypted, the file's 16-byte nonce is included in the | ||
278 | IV. Moreover: | ||
279 | |||
280 | - For v1 encryption policies, the encryption is done directly with the | ||
281 | master key. Because of this, users **must not** use the same master | ||
282 | key for any other purpose, even for other v1 policies. | ||
283 | |||
284 | - For v2 encryption policies, the encryption is done with a per-mode | ||
285 | key derived using the KDF. Users may use the same master key for | ||
286 | other v2 encryption policies. | ||
287 | |||
288 | Key identifiers | ||
289 | --------------- | ||
290 | |||
291 | For master keys used for v2 encryption policies, a unique 16-byte "key | ||
292 | identifier" is also derived using the KDF. This value is stored in | ||
293 | the clear, since it is needed to reliably identify the key itself. | ||
294 | |||
179 | Encryption modes and usage | 295 | Encryption modes and usage |
180 | ========================== | 296 | ========================== |
181 | 297 | ||
@@ -225,9 +341,10 @@ a little endian number, except that: | |||
225 | is encrypted with AES-256 where the AES-256 key is the SHA-256 hash | 341 | is encrypted with AES-256 where the AES-256 key is the SHA-256 hash |
226 | of the file's data encryption key. | 342 | of the file's data encryption key. |
227 | 343 | ||
228 | - In the "direct key" configuration (FS_POLICY_FLAG_DIRECT_KEY set in | 344 | - In the "direct key" configuration (FSCRYPT_POLICY_FLAG_DIRECT_KEY |
229 | the fscrypt_policy), the file's nonce is also appended to the IV. | 345 | set in the fscrypt_policy), the file's nonce is also appended to the |
230 | Currently this is only allowed with the Adiantum encryption mode. | 346 | IV. Currently this is only allowed with the Adiantum encryption |
347 | mode. | ||
231 | 348 | ||
232 | Filenames encryption | 349 | Filenames encryption |
233 | -------------------- | 350 | -------------------- |
@@ -269,49 +386,77 @@ User API | |||
269 | Setting an encryption policy | 386 | Setting an encryption policy |
270 | ---------------------------- | 387 | ---------------------------- |
271 | 388 | ||
389 | FS_IOC_SET_ENCRYPTION_POLICY | ||
390 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
391 | |||
272 | The FS_IOC_SET_ENCRYPTION_POLICY ioctl sets an encryption policy on an | 392 | The FS_IOC_SET_ENCRYPTION_POLICY ioctl sets an encryption policy on an |
273 | empty directory or verifies that a directory or regular file already | 393 | empty directory or verifies that a directory or regular file already |
274 | has the specified encryption policy. It takes in a pointer to a | 394 | has the specified encryption policy. It takes in a pointer to a |
275 | :c:type:`struct fscrypt_policy`, defined as follows:: | 395 | :c:type:`struct fscrypt_policy_v1` or a :c:type:`struct |
396 | fscrypt_policy_v2`, defined as follows:: | ||
276 | 397 | ||
277 | #define FS_KEY_DESCRIPTOR_SIZE 8 | 398 | #define FSCRYPT_POLICY_V1 0 |
399 | #define FSCRYPT_KEY_DESCRIPTOR_SIZE 8 | ||
400 | struct fscrypt_policy_v1 { | ||
401 | __u8 version; | ||
402 | __u8 contents_encryption_mode; | ||
403 | __u8 filenames_encryption_mode; | ||
404 | __u8 flags; | ||
405 | __u8 master_key_descriptor[FSCRYPT_KEY_DESCRIPTOR_SIZE]; | ||
406 | }; | ||
407 | #define fscrypt_policy fscrypt_policy_v1 | ||
278 | 408 | ||
279 | struct fscrypt_policy { | 409 | #define FSCRYPT_POLICY_V2 2 |
410 | #define FSCRYPT_KEY_IDENTIFIER_SIZE 16 | ||
411 | struct fscrypt_policy_v2 { | ||
280 | __u8 version; | 412 | __u8 version; |
281 | __u8 contents_encryption_mode; | 413 | __u8 contents_encryption_mode; |
282 | __u8 filenames_encryption_mode; | 414 | __u8 filenames_encryption_mode; |
283 | __u8 flags; | 415 | __u8 flags; |
284 | __u8 master_key_descriptor[FS_KEY_DESCRIPTOR_SIZE]; | 416 | __u8 __reserved[4]; |
417 | __u8 master_key_identifier[FSCRYPT_KEY_IDENTIFIER_SIZE]; | ||
285 | }; | 418 | }; |
286 | 419 | ||
287 | This structure must be initialized as follows: | 420 | This structure must be initialized as follows: |
288 | 421 | ||
289 | - ``version`` must be 0. | 422 | - ``version`` must be FSCRYPT_POLICY_V1 (0) if the struct is |
423 | :c:type:`fscrypt_policy_v1` or FSCRYPT_POLICY_V2 (2) if the struct | ||
424 | is :c:type:`fscrypt_policy_v2`. (Note: we refer to the original | ||
425 | policy version as "v1", though its version code is really 0.) For | ||
426 | new encrypted directories, use v2 policies. | ||
290 | 427 | ||
291 | - ``contents_encryption_mode`` and ``filenames_encryption_mode`` must | 428 | - ``contents_encryption_mode`` and ``filenames_encryption_mode`` must |
292 | be set to constants from ``<linux/fs.h>`` which identify the | 429 | be set to constants from ``<linux/fscrypt.h>`` which identify the |
293 | encryption modes to use. If unsure, use | 430 | encryption modes to use. If unsure, use FSCRYPT_MODE_AES_256_XTS |
294 | FS_ENCRYPTION_MODE_AES_256_XTS (1) for ``contents_encryption_mode`` | 431 | (1) for ``contents_encryption_mode`` and FSCRYPT_MODE_AES_256_CTS |
295 | and FS_ENCRYPTION_MODE_AES_256_CTS (4) for | 432 | (4) for ``filenames_encryption_mode``. |
296 | ``filenames_encryption_mode``. | ||
297 | 433 | ||
298 | - ``flags`` must contain a value from ``<linux/fs.h>`` which | 434 | - ``flags`` must contain a value from ``<linux/fscrypt.h>`` which |
299 | identifies the amount of NUL-padding to use when encrypting | 435 | identifies the amount of NUL-padding to use when encrypting |
300 | filenames. If unsure, use FS_POLICY_FLAGS_PAD_32 (0x3). | 436 | filenames. If unsure, use FSCRYPT_POLICY_FLAGS_PAD_32 (0x3). |
301 | In addition, if the chosen encryption modes are both | 437 | Additionally, if the encryption modes are both |
302 | FS_ENCRYPTION_MODE_ADIANTUM, this can contain | 438 | FSCRYPT_MODE_ADIANTUM, this can contain |
303 | FS_POLICY_FLAG_DIRECT_KEY to specify that the master key should be | 439 | FSCRYPT_POLICY_FLAG_DIRECT_KEY; see `DIRECT_KEY and per-mode keys`_. |
304 | used directly, without key derivation. | 440 | |
305 | 441 | - For v2 encryption policies, ``__reserved`` must be zeroed. | |
306 | - ``master_key_descriptor`` specifies how to find the master key in | 442 | |
307 | the keyring; see `Adding keys`_. It is up to userspace to choose a | 443 | - For v1 encryption policies, ``master_key_descriptor`` specifies how |
308 | unique ``master_key_descriptor`` for each master key. The e4crypt | 444 | to find the master key in a keyring; see `Adding keys`_. It is up |
309 | and fscrypt tools use the first 8 bytes of | 445 | to userspace to choose a unique ``master_key_descriptor`` for each |
446 | master key. The e4crypt and fscrypt tools use the first 8 bytes of | ||
310 | ``SHA-512(SHA-512(master_key))``, but this particular scheme is not | 447 | ``SHA-512(SHA-512(master_key))``, but this particular scheme is not |
311 | required. Also, the master key need not be in the keyring yet when | 448 | required. Also, the master key need not be in the keyring yet when |
312 | FS_IOC_SET_ENCRYPTION_POLICY is executed. However, it must be added | 449 | FS_IOC_SET_ENCRYPTION_POLICY is executed. However, it must be added |
313 | before any files can be created in the encrypted directory. | 450 | before any files can be created in the encrypted directory. |
314 | 451 | ||
452 | For v2 encryption policies, ``master_key_descriptor`` has been | ||
453 | replaced with ``master_key_identifier``, which is longer and cannot | ||
454 | be arbitrarily chosen. Instead, the key must first be added using | ||
455 | `FS_IOC_ADD_ENCRYPTION_KEY`_. Then, the ``key_spec.u.identifier`` | ||
456 | the kernel returned in the :c:type:`struct fscrypt_add_key_arg` must | ||
457 | be used as the ``master_key_identifier`` in the :c:type:`struct | ||
458 | fscrypt_policy_v2`. | ||
459 | |||
315 | If the file is not yet encrypted, then FS_IOC_SET_ENCRYPTION_POLICY | 460 | If the file is not yet encrypted, then FS_IOC_SET_ENCRYPTION_POLICY |
316 | verifies that the file is an empty directory. If so, the specified | 461 | verifies that the file is an empty directory. If so, the specified |
317 | encryption policy is assigned to the directory, turning it into an | 462 | encryption policy is assigned to the directory, turning it into an |
@@ -327,6 +472,15 @@ policy exactly matches the actual one. If they match, then the ioctl | |||
327 | returns 0. Otherwise, it fails with EEXIST. This works on both | 472 | returns 0. Otherwise, it fails with EEXIST. This works on both |
328 | regular files and directories, including nonempty directories. | 473 | regular files and directories, including nonempty directories. |
329 | 474 | ||
475 | When a v2 encryption policy is assigned to a directory, it is also | ||
476 | required that either the specified key has been added by the current | ||
477 | user or that the caller has CAP_FOWNER in the initial user namespace. | ||
478 | (This is needed to prevent a user from encrypting their data with | ||
479 | another user's key.) The key must remain added while | ||
480 | FS_IOC_SET_ENCRYPTION_POLICY is executing. However, if the new | ||
481 | encrypted directory does not need to be accessed immediately, then the | ||
482 | key can be removed right away afterwards. | ||
483 | |||
330 | Note that the ext4 filesystem does not allow the root directory to be | 484 | Note that the ext4 filesystem does not allow the root directory to be |
331 | encrypted, even if it is empty. Users who want to encrypt an entire | 485 | encrypted, even if it is empty. Users who want to encrypt an entire |
332 | filesystem with one key should consider using dm-crypt instead. | 486 | filesystem with one key should consider using dm-crypt instead. |
@@ -339,7 +493,11 @@ FS_IOC_SET_ENCRYPTION_POLICY can fail with the following errors: | |||
339 | - ``EEXIST``: the file is already encrypted with an encryption policy | 493 | - ``EEXIST``: the file is already encrypted with an encryption policy |
340 | different from the one specified | 494 | different from the one specified |
341 | - ``EINVAL``: an invalid encryption policy was specified (invalid | 495 | - ``EINVAL``: an invalid encryption policy was specified (invalid |
342 | version, mode(s), or flags) | 496 | version, mode(s), or flags; or reserved bits were set) |
497 | - ``ENOKEY``: a v2 encryption policy was specified, but the key with | ||
498 | the specified ``master_key_identifier`` has not been added, nor does | ||
499 | the process have the CAP_FOWNER capability in the initial user | ||
500 | namespace | ||
343 | - ``ENOTDIR``: the file is unencrypted and is a regular file, not a | 501 | - ``ENOTDIR``: the file is unencrypted and is a regular file, not a |
344 | directory | 502 | directory |
345 | - ``ENOTEMPTY``: the file is unencrypted and is a nonempty directory | 503 | - ``ENOTEMPTY``: the file is unencrypted and is a nonempty directory |
@@ -358,25 +516,79 @@ FS_IOC_SET_ENCRYPTION_POLICY can fail with the following errors: | |||
358 | Getting an encryption policy | 516 | Getting an encryption policy |
359 | ---------------------------- | 517 | ---------------------------- |
360 | 518 | ||
361 | The FS_IOC_GET_ENCRYPTION_POLICY ioctl retrieves the :c:type:`struct | 519 | Two ioctls are available to get a file's encryption policy: |
362 | fscrypt_policy`, if any, for a directory or regular file. See above | 520 | |
363 | for the struct definition. No additional permissions are required | 521 | - `FS_IOC_GET_ENCRYPTION_POLICY_EX`_ |
364 | beyond the ability to open the file. | 522 | - `FS_IOC_GET_ENCRYPTION_POLICY`_ |
523 | |||
524 | The extended (_EX) version of the ioctl is more general and is | ||
525 | recommended to use when possible. However, on older kernels only the | ||
526 | original ioctl is available. Applications should try the extended | ||
527 | version, and if it fails with ENOTTY fall back to the original | ||
528 | version. | ||
529 | |||
530 | FS_IOC_GET_ENCRYPTION_POLICY_EX | ||
531 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
532 | |||
533 | The FS_IOC_GET_ENCRYPTION_POLICY_EX ioctl retrieves the encryption | ||
534 | policy, if any, for a directory or regular file. No additional | ||
535 | permissions are required beyond the ability to open the file. It | ||
536 | takes in a pointer to a :c:type:`struct fscrypt_get_policy_ex_arg`, | ||
537 | defined as follows:: | ||
538 | |||
539 | struct fscrypt_get_policy_ex_arg { | ||
540 | __u64 policy_size; /* input/output */ | ||
541 | union { | ||
542 | __u8 version; | ||
543 | struct fscrypt_policy_v1 v1; | ||
544 | struct fscrypt_policy_v2 v2; | ||
545 | } policy; /* output */ | ||
546 | }; | ||
547 | |||
548 | The caller must initialize ``policy_size`` to the size available for | ||
549 | the policy struct, i.e. ``sizeof(arg.policy)``. | ||
550 | |||
551 | On success, the policy struct is returned in ``policy``, and its | ||
552 | actual size is returned in ``policy_size``. ``policy.version`` should | ||
553 | be checked to determine the version of policy returned. Note that the | ||
554 | version code for the "v1" policy is actually 0 (FSCRYPT_POLICY_V1). | ||
365 | 555 | ||
366 | FS_IOC_GET_ENCRYPTION_POLICY can fail with the following errors: | 556 | FS_IOC_GET_ENCRYPTION_POLICY_EX can fail with the following errors: |
367 | 557 | ||
368 | - ``EINVAL``: the file is encrypted, but it uses an unrecognized | 558 | - ``EINVAL``: the file is encrypted, but it uses an unrecognized |
369 | encryption context format | 559 | encryption policy version |
370 | - ``ENODATA``: the file is not encrypted | 560 | - ``ENODATA``: the file is not encrypted |
371 | - ``ENOTTY``: this type of filesystem does not implement encryption | 561 | - ``ENOTTY``: this type of filesystem does not implement encryption, |
562 | or this kernel is too old to support FS_IOC_GET_ENCRYPTION_POLICY_EX | ||
563 | (try FS_IOC_GET_ENCRYPTION_POLICY instead) | ||
372 | - ``EOPNOTSUPP``: the kernel was not configured with encryption | 564 | - ``EOPNOTSUPP``: the kernel was not configured with encryption |
373 | support for this filesystem | 565 | support for this filesystem, or the filesystem superblock has not |
566 | had encryption enabled on it | ||
567 | - ``EOVERFLOW``: the file is encrypted and uses a recognized | ||
568 | encryption policy version, but the policy struct does not fit into | ||
569 | the provided buffer | ||
374 | 570 | ||
375 | Note: if you only need to know whether a file is encrypted or not, on | 571 | Note: if you only need to know whether a file is encrypted or not, on |
376 | most filesystems it is also possible to use the FS_IOC_GETFLAGS ioctl | 572 | most filesystems it is also possible to use the FS_IOC_GETFLAGS ioctl |
377 | and check for FS_ENCRYPT_FL, or to use the statx() system call and | 573 | and check for FS_ENCRYPT_FL, or to use the statx() system call and |
378 | check for STATX_ATTR_ENCRYPTED in stx_attributes. | 574 | check for STATX_ATTR_ENCRYPTED in stx_attributes. |
379 | 575 | ||
576 | FS_IOC_GET_ENCRYPTION_POLICY | ||
577 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
578 | |||
579 | The FS_IOC_GET_ENCRYPTION_POLICY ioctl can also retrieve the | ||
580 | encryption policy, if any, for a directory or regular file. However, | ||
581 | unlike `FS_IOC_GET_ENCRYPTION_POLICY_EX`_, | ||
582 | FS_IOC_GET_ENCRYPTION_POLICY only supports the original policy | ||
583 | version. It takes in a pointer directly to a :c:type:`struct | ||
584 | fscrypt_policy_v1` rather than a :c:type:`struct | ||
585 | fscrypt_get_policy_ex_arg`. | ||
586 | |||
587 | The error codes for FS_IOC_GET_ENCRYPTION_POLICY are the same as those | ||
588 | for FS_IOC_GET_ENCRYPTION_POLICY_EX, except that | ||
589 | FS_IOC_GET_ENCRYPTION_POLICY also returns ``EINVAL`` if the file is | ||
590 | encrypted using a newer encryption policy version. | ||
591 | |||
380 | Getting the per-filesystem salt | 592 | Getting the per-filesystem salt |
381 | ------------------------------- | 593 | ------------------------------- |
382 | 594 | ||
@@ -392,8 +604,115 @@ generate and manage any needed salt(s) in userspace. | |||
392 | Adding keys | 604 | Adding keys |
393 | ----------- | 605 | ----------- |
394 | 606 | ||
395 | To provide a master key, userspace must add it to an appropriate | 607 | FS_IOC_ADD_ENCRYPTION_KEY |
396 | keyring using the add_key() system call (see: | 608 | ~~~~~~~~~~~~~~~~~~~~~~~~~ |
609 | |||
610 | The FS_IOC_ADD_ENCRYPTION_KEY ioctl adds a master encryption key to | ||
611 | the filesystem, making all files on the filesystem which were | ||
612 | encrypted using that key appear "unlocked", i.e. in plaintext form. | ||
613 | It can be executed on any file or directory on the target filesystem, | ||
614 | but using the filesystem's root directory is recommended. It takes in | ||
615 | a pointer to a :c:type:`struct fscrypt_add_key_arg`, defined as | ||
616 | follows:: | ||
617 | |||
618 | struct fscrypt_add_key_arg { | ||
619 | struct fscrypt_key_specifier key_spec; | ||
620 | __u32 raw_size; | ||
621 | __u32 __reserved[9]; | ||
622 | __u8 raw[]; | ||
623 | }; | ||
624 | |||
625 | #define FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR 1 | ||
626 | #define FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER 2 | ||
627 | |||
628 | struct fscrypt_key_specifier { | ||
629 | __u32 type; /* one of FSCRYPT_KEY_SPEC_TYPE_* */ | ||
630 | __u32 __reserved; | ||
631 | union { | ||
632 | __u8 __reserved[32]; /* reserve some extra space */ | ||
633 | __u8 descriptor[FSCRYPT_KEY_DESCRIPTOR_SIZE]; | ||
634 | __u8 identifier[FSCRYPT_KEY_IDENTIFIER_SIZE]; | ||
635 | } u; | ||
636 | }; | ||
637 | |||
638 | :c:type:`struct fscrypt_add_key_arg` must be zeroed, then initialized | ||
639 | as follows: | ||
640 | |||
641 | - If the key is being added for use by v1 encryption policies, then | ||
642 | ``key_spec.type`` must contain FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR, and | ||
643 | ``key_spec.u.descriptor`` must contain the descriptor of the key | ||
644 | being added, corresponding to the value in the | ||
645 | ``master_key_descriptor`` field of :c:type:`struct | ||
646 | fscrypt_policy_v1`. To add this type of key, the calling process | ||
647 | must have the CAP_SYS_ADMIN capability in the initial user | ||
648 | namespace. | ||
649 | |||
650 | Alternatively, if the key is being added for use by v2 encryption | ||
651 | policies, then ``key_spec.type`` must contain | ||
652 | FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER, and ``key_spec.u.identifier`` is | ||
653 | an *output* field which the kernel fills in with a cryptographic | ||
654 | hash of the key. To add this type of key, the calling process does | ||
655 | not need any privileges. However, the number of keys that can be | ||
656 | added is limited by the user's quota for the keyrings service (see | ||
657 | ``Documentation/security/keys/core.rst``). | ||
658 | |||
659 | - ``raw_size`` must be the size of the ``raw`` key provided, in bytes. | ||
660 | |||
661 | - ``raw`` is a variable-length field which must contain the actual | ||
662 | key, ``raw_size`` bytes long. | ||
663 | |||
664 | For v2 policy keys, the kernel keeps track of which user (identified | ||
665 | by effective user ID) added the key, and only allows the key to be | ||
666 | removed by that user --- or by "root", if they use | ||
667 | `FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS`_. | ||
668 | |||
669 | However, if another user has added the key, it may be desirable to | ||
670 | prevent that other user from unexpectedly removing it. Therefore, | ||
671 | FS_IOC_ADD_ENCRYPTION_KEY may also be used to add a v2 policy key | ||
672 | *again*, even if it's already added by other user(s). In this case, | ||
673 | FS_IOC_ADD_ENCRYPTION_KEY will just install a claim to the key for the | ||
674 | current user, rather than actually add the key again (but the raw key | ||
675 | must still be provided, as a proof of knowledge). | ||
676 | |||
677 | FS_IOC_ADD_ENCRYPTION_KEY returns 0 if either the key or a claim to | ||
678 | the key was either added or already exists. | ||
679 | |||
680 | FS_IOC_ADD_ENCRYPTION_KEY can fail with the following errors: | ||
681 | |||
682 | - ``EACCES``: FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR was specified, but the | ||
683 | caller does not have the CAP_SYS_ADMIN capability in the initial | ||
684 | user namespace | ||
685 | - ``EDQUOT``: the key quota for this user would be exceeded by adding | ||
686 | the key | ||
687 | - ``EINVAL``: invalid key size or key specifier type, or reserved bits | ||
688 | were set | ||
689 | - ``ENOTTY``: this type of filesystem does not implement encryption | ||
690 | - ``EOPNOTSUPP``: the kernel was not configured with encryption | ||
691 | support for this filesystem, or the filesystem superblock has not | ||
692 | had encryption enabled on it | ||
693 | |||
694 | Legacy method | ||
695 | ~~~~~~~~~~~~~ | ||
696 | |||
697 | For v1 encryption policies, a master encryption key can also be | ||
698 | provided by adding it to a process-subscribed keyring, e.g. to a | ||
699 | session keyring, or to a user keyring if the user keyring is linked | ||
700 | into the session keyring. | ||
701 | |||
702 | This method is deprecated (and not supported for v2 encryption | ||
703 | policies) for several reasons. First, it cannot be used in | ||
704 | combination with FS_IOC_REMOVE_ENCRYPTION_KEY (see `Removing keys`_), | ||
705 | so for removing a key a workaround such as keyctl_unlink() in | ||
706 | combination with ``sync; echo 2 > /proc/sys/vm/drop_caches`` would | ||
707 | have to be used. Second, it doesn't match the fact that the | ||
708 | locked/unlocked status of encrypted files (i.e. whether they appear to | ||
709 | be in plaintext form or in ciphertext form) is global. This mismatch | ||
710 | has caused much confusion as well as real problems when processes | ||
711 | running under different UIDs, such as a ``sudo`` command, need to | ||
712 | access encrypted files. | ||
713 | |||
714 | Nevertheless, to add a key to one of the process-subscribed keyrings, | ||
715 | the add_key() system call can be used (see: | ||
397 | ``Documentation/security/keys/core.rst``). The key type must be | 716 | ``Documentation/security/keys/core.rst``). The key type must be |
398 | "logon"; keys of this type are kept in kernel memory and cannot be | 717 | "logon"; keys of this type are kept in kernel memory and cannot be |
399 | read back by userspace. The key description must be "fscrypt:" | 718 | read back by userspace. The key description must be "fscrypt:" |
@@ -401,12 +720,12 @@ followed by the 16-character lower case hex representation of the | |||
401 | ``master_key_descriptor`` that was set in the encryption policy. The | 720 | ``master_key_descriptor`` that was set in the encryption policy. The |
402 | key payload must conform to the following structure:: | 721 | key payload must conform to the following structure:: |
403 | 722 | ||
404 | #define FS_MAX_KEY_SIZE 64 | 723 | #define FSCRYPT_MAX_KEY_SIZE 64 |
405 | 724 | ||
406 | struct fscrypt_key { | 725 | struct fscrypt_key { |
407 | u32 mode; | 726 | __u32 mode; |
408 | u8 raw[FS_MAX_KEY_SIZE]; | 727 | __u8 raw[FSCRYPT_MAX_KEY_SIZE]; |
409 | u32 size; | 728 | __u32 size; |
410 | }; | 729 | }; |
411 | 730 | ||
412 | ``mode`` is ignored; just set it to 0. The actual key is provided in | 731 | ``mode`` is ignored; just set it to 0. The actual key is provided in |
@@ -418,26 +737,194 @@ with a filesystem-specific prefix such as "ext4:". However, the | |||
418 | filesystem-specific prefixes are deprecated and should not be used in | 737 | filesystem-specific prefixes are deprecated and should not be used in |
419 | new programs. | 738 | new programs. |
420 | 739 | ||
421 | There are several different types of keyrings in which encryption keys | 740 | Removing keys |
422 | may be placed, such as a session keyring, a user session keyring, or a | 741 | ------------- |
423 | user keyring. Each key must be placed in a keyring that is "attached" | 742 | |
424 | to all processes that might need to access files encrypted with it, in | 743 | Two ioctls are available for removing a key that was added by |
425 | the sense that request_key() will find the key. Generally, if only | 744 | `FS_IOC_ADD_ENCRYPTION_KEY`_: |
426 | processes belonging to a specific user need to access a given | 745 | |
427 | encrypted directory and no session keyring has been installed, then | 746 | - `FS_IOC_REMOVE_ENCRYPTION_KEY`_ |
428 | that directory's key should be placed in that user's user session | 747 | - `FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS`_ |
429 | keyring or user keyring. Otherwise, a session keyring should be | 748 | |
430 | installed if needed, and the key should be linked into that session | 749 | These two ioctls differ only in cases where v2 policy keys are added |
431 | keyring, or in a keyring linked into that session keyring. | 750 | or removed by non-root users. |
432 | 751 | ||
433 | Note: introducing the complex visibility semantics of keyrings here | 752 | These ioctls don't work on keys that were added via the legacy |
434 | was arguably a mistake --- especially given that by design, after any | 753 | process-subscribed keyrings mechanism. |
435 | process successfully opens an encrypted file (thereby setting up the | 754 | |
436 | per-file key), possessing the keyring key is not actually required for | 755 | Before using these ioctls, read the `Kernel memory compromise`_ |
437 | any process to read/write the file until its in-memory inode is | 756 | section for a discussion of the security goals and limitations of |
438 | evicted. In the future there probably should be a way to provide keys | 757 | these ioctls. |
439 | directly to the filesystem instead, which would make the intended | 758 | |
440 | semantics clearer. | 759 | FS_IOC_REMOVE_ENCRYPTION_KEY |
760 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
761 | |||
762 | The FS_IOC_REMOVE_ENCRYPTION_KEY ioctl removes a claim to a master | ||
763 | encryption key from the filesystem, and possibly removes the key | ||
764 | itself. It can be executed on any file or directory on the target | ||
765 | filesystem, but using the filesystem's root directory is recommended. | ||
766 | It takes in a pointer to a :c:type:`struct fscrypt_remove_key_arg`, | ||
767 | defined as follows:: | ||
768 | |||
769 | struct fscrypt_remove_key_arg { | ||
770 | struct fscrypt_key_specifier key_spec; | ||
771 | #define FSCRYPT_KEY_REMOVAL_STATUS_FLAG_FILES_BUSY 0x00000001 | ||
772 | #define FSCRYPT_KEY_REMOVAL_STATUS_FLAG_OTHER_USERS 0x00000002 | ||
773 | __u32 removal_status_flags; /* output */ | ||
774 | __u32 __reserved[5]; | ||
775 | }; | ||
776 | |||
777 | This structure must be zeroed, then initialized as follows: | ||
778 | |||
779 | - The key to remove is specified by ``key_spec``: | ||
780 | |||
781 | - To remove a key used by v1 encryption policies, set | ||
782 | ``key_spec.type`` to FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR and fill | ||
783 | in ``key_spec.u.descriptor``. To remove this type of key, the | ||
784 | calling process must have the CAP_SYS_ADMIN capability in the | ||
785 | initial user namespace. | ||
786 | |||
787 | - To remove a key used by v2 encryption policies, set | ||
788 | ``key_spec.type`` to FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER and fill | ||
789 | in ``key_spec.u.identifier``. | ||
790 | |||
791 | For v2 policy keys, this ioctl is usable by non-root users. However, | ||
792 | to make this possible, it actually just removes the current user's | ||
793 | claim to the key, undoing a single call to FS_IOC_ADD_ENCRYPTION_KEY. | ||
794 | Only after all claims are removed is the key really removed. | ||
795 | |||
796 | For example, if FS_IOC_ADD_ENCRYPTION_KEY was called with uid 1000, | ||
797 | then the key will be "claimed" by uid 1000, and | ||
798 | FS_IOC_REMOVE_ENCRYPTION_KEY will only succeed as uid 1000. Or, if | ||
799 | both uids 1000 and 2000 added the key, then for each uid | ||
800 | FS_IOC_REMOVE_ENCRYPTION_KEY will only remove their own claim. Only | ||
801 | once *both* are removed is the key really removed. (Think of it like | ||
802 | unlinking a file that may have hard links.) | ||
803 | |||
804 | If FS_IOC_REMOVE_ENCRYPTION_KEY really removes the key, it will also | ||
805 | try to "lock" all files that had been unlocked with the key. It won't | ||
806 | lock files that are still in-use, so this ioctl is expected to be used | ||
807 | in cooperation with userspace ensuring that none of the files are | ||
808 | still open. However, if necessary, this ioctl can be executed again | ||
809 | later to retry locking any remaining files. | ||
810 | |||
811 | FS_IOC_REMOVE_ENCRYPTION_KEY returns 0 if either the key was removed | ||
812 | (but may still have files remaining to be locked), the user's claim to | ||
813 | the key was removed, or the key was already removed but had files | ||
814 | remaining to be the locked so the ioctl retried locking them. In any | ||
815 | of these cases, ``removal_status_flags`` is filled in with the | ||
816 | following informational status flags: | ||
817 | |||
818 | - ``FSCRYPT_KEY_REMOVAL_STATUS_FLAG_FILES_BUSY``: set if some file(s) | ||
819 | are still in-use. Not guaranteed to be set in the case where only | ||
820 | the user's claim to the key was removed. | ||
821 | - ``FSCRYPT_KEY_REMOVAL_STATUS_FLAG_OTHER_USERS``: set if only the | ||
822 | user's claim to the key was removed, not the key itself | ||
823 | |||
824 | FS_IOC_REMOVE_ENCRYPTION_KEY can fail with the following errors: | ||
825 | |||
826 | - ``EACCES``: The FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR key specifier type | ||
827 | was specified, but the caller does not have the CAP_SYS_ADMIN | ||
828 | capability in the initial user namespace | ||
829 | - ``EINVAL``: invalid key specifier type, or reserved bits were set | ||
830 | - ``ENOKEY``: the key object was not found at all, i.e. it was never | ||
831 | added in the first place or was already fully removed including all | ||
832 | files locked; or, the user does not have a claim to the key (but | ||
833 | someone else does). | ||
834 | - ``ENOTTY``: this type of filesystem does not implement encryption | ||
835 | - ``EOPNOTSUPP``: the kernel was not configured with encryption | ||
836 | support for this filesystem, or the filesystem superblock has not | ||
837 | had encryption enabled on it | ||
838 | |||
839 | FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS | ||
840 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
841 | |||
842 | FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS is exactly the same as | ||
843 | `FS_IOC_REMOVE_ENCRYPTION_KEY`_, except that for v2 policy keys, the | ||
844 | ALL_USERS version of the ioctl will remove all users' claims to the | ||
845 | key, not just the current user's. I.e., the key itself will always be | ||
846 | removed, no matter how many users have added it. This difference is | ||
847 | only meaningful if non-root users are adding and removing keys. | ||
848 | |||
849 | Because of this, FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS also requires | ||
850 | "root", namely the CAP_SYS_ADMIN capability in the initial user | ||
851 | namespace. Otherwise it will fail with EACCES. | ||
852 | |||
853 | Getting key status | ||
854 | ------------------ | ||
855 | |||
856 | FS_IOC_GET_ENCRYPTION_KEY_STATUS | ||
857 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
858 | |||
859 | The FS_IOC_GET_ENCRYPTION_KEY_STATUS ioctl retrieves the status of a | ||
860 | master encryption key. It can be executed on any file or directory on | ||
861 | the target filesystem, but using the filesystem's root directory is | ||
862 | recommended. It takes in a pointer to a :c:type:`struct | ||
863 | fscrypt_get_key_status_arg`, defined as follows:: | ||
864 | |||
865 | struct fscrypt_get_key_status_arg { | ||
866 | /* input */ | ||
867 | struct fscrypt_key_specifier key_spec; | ||
868 | __u32 __reserved[6]; | ||
869 | |||
870 | /* output */ | ||
871 | #define FSCRYPT_KEY_STATUS_ABSENT 1 | ||
872 | #define FSCRYPT_KEY_STATUS_PRESENT 2 | ||
873 | #define FSCRYPT_KEY_STATUS_INCOMPLETELY_REMOVED 3 | ||
874 | __u32 status; | ||
875 | #define FSCRYPT_KEY_STATUS_FLAG_ADDED_BY_SELF 0x00000001 | ||
876 | __u32 status_flags; | ||
877 | __u32 user_count; | ||
878 | __u32 __out_reserved[13]; | ||
879 | }; | ||
880 | |||
881 | The caller must zero all input fields, then fill in ``key_spec``: | ||
882 | |||
883 | - To get the status of a key for v1 encryption policies, set | ||
884 | ``key_spec.type`` to FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR and fill | ||
885 | in ``key_spec.u.descriptor``. | ||
886 | |||
887 | - To get the status of a key for v2 encryption policies, set | ||
888 | ``key_spec.type`` to FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER and fill | ||
889 | in ``key_spec.u.identifier``. | ||
890 | |||
891 | On success, 0 is returned and the kernel fills in the output fields: | ||
892 | |||
893 | - ``status`` indicates whether the key is absent, present, or | ||
894 | incompletely removed. Incompletely removed means that the master | ||
895 | secret has been removed, but some files are still in use; i.e., | ||
896 | `FS_IOC_REMOVE_ENCRYPTION_KEY`_ returned 0 but set the informational | ||
897 | status flag FSCRYPT_KEY_REMOVAL_STATUS_FLAG_FILES_BUSY. | ||
898 | |||
899 | - ``status_flags`` can contain the following flags: | ||
900 | |||
901 | - ``FSCRYPT_KEY_STATUS_FLAG_ADDED_BY_SELF`` indicates that the key | ||
902 | has added by the current user. This is only set for keys | ||
903 | identified by ``identifier`` rather than by ``descriptor``. | ||
904 | |||
905 | - ``user_count`` specifies the number of users who have added the key. | ||
906 | This is only set for keys identified by ``identifier`` rather than | ||
907 | by ``descriptor``. | ||
908 | |||
909 | FS_IOC_GET_ENCRYPTION_KEY_STATUS can fail with the following errors: | ||
910 | |||
911 | - ``EINVAL``: invalid key specifier type, or reserved bits were set | ||
912 | - ``ENOTTY``: this type of filesystem does not implement encryption | ||
913 | - ``EOPNOTSUPP``: the kernel was not configured with encryption | ||
914 | support for this filesystem, or the filesystem superblock has not | ||
915 | had encryption enabled on it | ||
916 | |||
917 | Among other use cases, FS_IOC_GET_ENCRYPTION_KEY_STATUS can be useful | ||
918 | for determining whether the key for a given encrypted directory needs | ||
919 | to be added before prompting the user for the passphrase needed to | ||
920 | derive the key. | ||
921 | |||
922 | FS_IOC_GET_ENCRYPTION_KEY_STATUS can only get the status of keys in | ||
923 | the filesystem-level keyring, i.e. the keyring managed by | ||
924 | `FS_IOC_ADD_ENCRYPTION_KEY`_ and `FS_IOC_REMOVE_ENCRYPTION_KEY`_. It | ||
925 | cannot get the status of a key that has only been added for use by v1 | ||
926 | encryption policies using the legacy mechanism involving | ||
927 | process-subscribed keyrings. | ||
441 | 928 | ||
442 | Access semantics | 929 | Access semantics |
443 | ================ | 930 | ================ |
@@ -500,7 +987,7 @@ Without the key | |||
500 | 987 | ||
501 | Some filesystem operations may be performed on encrypted regular | 988 | Some filesystem operations may be performed on encrypted regular |
502 | files, directories, and symlinks even before their encryption key has | 989 | files, directories, and symlinks even before their encryption key has |
503 | been provided: | 990 | been added, or after their encryption key has been removed: |
504 | 991 | ||
505 | - File metadata may be read, e.g. using stat(). | 992 | - File metadata may be read, e.g. using stat(). |
506 | 993 | ||
@@ -565,33 +1052,44 @@ Encryption context | |||
565 | ------------------ | 1052 | ------------------ |
566 | 1053 | ||
567 | An encryption policy is represented on-disk by a :c:type:`struct | 1054 | An encryption policy is represented on-disk by a :c:type:`struct |
568 | fscrypt_context`. It is up to individual filesystems to decide where | 1055 | fscrypt_context_v1` or a :c:type:`struct fscrypt_context_v2`. It is |
569 | to store it, but normally it would be stored in a hidden extended | 1056 | up to individual filesystems to decide where to store it, but normally |
570 | attribute. It should *not* be exposed by the xattr-related system | 1057 | it would be stored in a hidden extended attribute. It should *not* be |
571 | calls such as getxattr() and setxattr() because of the special | 1058 | exposed by the xattr-related system calls such as getxattr() and |
572 | semantics of the encryption xattr. (In particular, there would be | 1059 | setxattr() because of the special semantics of the encryption xattr. |
573 | much confusion if an encryption policy were to be added to or removed | 1060 | (In particular, there would be much confusion if an encryption policy |
574 | from anything other than an empty directory.) The struct is defined | 1061 | were to be added to or removed from anything other than an empty |
575 | as follows:: | 1062 | directory.) These structs are defined as follows:: |
576 | 1063 | ||
577 | #define FS_KEY_DESCRIPTOR_SIZE 8 | ||
578 | #define FS_KEY_DERIVATION_NONCE_SIZE 16 | 1064 | #define FS_KEY_DERIVATION_NONCE_SIZE 16 |
579 | 1065 | ||
580 | struct fscrypt_context { | 1066 | #define FSCRYPT_KEY_DESCRIPTOR_SIZE 8 |
581 | u8 format; | 1067 | struct fscrypt_context_v1 { |
1068 | u8 version; | ||
1069 | u8 contents_encryption_mode; | ||
1070 | u8 filenames_encryption_mode; | ||
1071 | u8 flags; | ||
1072 | u8 master_key_descriptor[FSCRYPT_KEY_DESCRIPTOR_SIZE]; | ||
1073 | u8 nonce[FS_KEY_DERIVATION_NONCE_SIZE]; | ||
1074 | }; | ||
1075 | |||
1076 | #define FSCRYPT_KEY_IDENTIFIER_SIZE 16 | ||
1077 | struct fscrypt_context_v2 { | ||
1078 | u8 version; | ||
582 | u8 contents_encryption_mode; | 1079 | u8 contents_encryption_mode; |
583 | u8 filenames_encryption_mode; | 1080 | u8 filenames_encryption_mode; |
584 | u8 flags; | 1081 | u8 flags; |
585 | u8 master_key_descriptor[FS_KEY_DESCRIPTOR_SIZE]; | 1082 | u8 __reserved[4]; |
1083 | u8 master_key_identifier[FSCRYPT_KEY_IDENTIFIER_SIZE]; | ||
586 | u8 nonce[FS_KEY_DERIVATION_NONCE_SIZE]; | 1084 | u8 nonce[FS_KEY_DERIVATION_NONCE_SIZE]; |
587 | }; | 1085 | }; |
588 | 1086 | ||
589 | Note that :c:type:`struct fscrypt_context` contains the same | 1087 | The context structs contain the same information as the corresponding |
590 | information as :c:type:`struct fscrypt_policy` (see `Setting an | 1088 | policy structs (see `Setting an encryption policy`_), except that the |
591 | encryption policy`_), except that :c:type:`struct fscrypt_context` | 1089 | context structs also contain a nonce. The nonce is randomly generated |
592 | also contains a nonce. The nonce is randomly generated by the kernel | 1090 | by the kernel and is used as KDF input or as a tweak to cause |
593 | and is used to derive the inode's encryption key as described in | 1091 | different files to be encrypted differently; see `Per-file keys`_ and |
594 | `Per-file keys`_. | 1092 | `DIRECT_KEY and per-mode keys`_. |
595 | 1093 | ||
596 | Data path changes | 1094 | Data path changes |
597 | ----------------- | 1095 | ----------------- |
diff --git a/MAINTAINERS b/MAINTAINERS index 2b6f10ea1573..ce6113999cf8 100644 --- a/MAINTAINERS +++ b/MAINTAINERS | |||
@@ -6662,6 +6662,7 @@ T: git git://git.kernel.org/pub/scm/fs/fscrypt/fscrypt.git | |||
6662 | S: Supported | 6662 | S: Supported |
6663 | F: fs/crypto/ | 6663 | F: fs/crypto/ |
6664 | F: include/linux/fscrypt*.h | 6664 | F: include/linux/fscrypt*.h |
6665 | F: include/uapi/linux/fscrypt.h | ||
6665 | F: Documentation/filesystems/fscrypt.rst | 6666 | F: Documentation/filesystems/fscrypt.rst |
6666 | 6667 | ||
6667 | FSI SUBSYSTEM | 6668 | FSI SUBSYSTEM |
diff --git a/fs/crypto/Kconfig b/fs/crypto/Kconfig index 5fdf24877c17..ff5a1746cbae 100644 --- a/fs/crypto/Kconfig +++ b/fs/crypto/Kconfig | |||
@@ -7,6 +7,8 @@ config FS_ENCRYPTION | |||
7 | select CRYPTO_ECB | 7 | select CRYPTO_ECB |
8 | select CRYPTO_XTS | 8 | select CRYPTO_XTS |
9 | select CRYPTO_CTS | 9 | select CRYPTO_CTS |
10 | select CRYPTO_SHA512 | ||
11 | select CRYPTO_HMAC | ||
10 | select KEYS | 12 | select KEYS |
11 | help | 13 | help |
12 | Enable encryption of files and directories. This | 14 | Enable encryption of files and directories. This |
diff --git a/fs/crypto/Makefile b/fs/crypto/Makefile index 4f0df5e682e4..232e2bb5a337 100644 --- a/fs/crypto/Makefile +++ b/fs/crypto/Makefile | |||
@@ -1,5 +1,13 @@ | |||
1 | # SPDX-License-Identifier: GPL-2.0-only | 1 | # SPDX-License-Identifier: GPL-2.0-only |
2 | obj-$(CONFIG_FS_ENCRYPTION) += fscrypto.o | 2 | obj-$(CONFIG_FS_ENCRYPTION) += fscrypto.o |
3 | 3 | ||
4 | fscrypto-y := crypto.o fname.o hooks.o keyinfo.o policy.o | 4 | fscrypto-y := crypto.o \ |
5 | fname.o \ | ||
6 | hkdf.o \ | ||
7 | hooks.o \ | ||
8 | keyring.o \ | ||
9 | keysetup.o \ | ||
10 | keysetup_v1.o \ | ||
11 | policy.o | ||
12 | |||
5 | fscrypto-$(CONFIG_BLOCK) += bio.o | 13 | fscrypto-$(CONFIG_BLOCK) += bio.o |
diff --git a/fs/crypto/crypto.c b/fs/crypto/crypto.c index 45c3d0427fb2..32a7ad0098cc 100644 --- a/fs/crypto/crypto.c +++ b/fs/crypto/crypto.c | |||
@@ -141,7 +141,7 @@ void fscrypt_generate_iv(union fscrypt_iv *iv, u64 lblk_num, | |||
141 | memset(iv, 0, ci->ci_mode->ivsize); | 141 | memset(iv, 0, ci->ci_mode->ivsize); |
142 | iv->lblk_num = cpu_to_le64(lblk_num); | 142 | iv->lblk_num = cpu_to_le64(lblk_num); |
143 | 143 | ||
144 | if (ci->ci_flags & FS_POLICY_FLAG_DIRECT_KEY) | 144 | if (fscrypt_is_direct_key_policy(&ci->ci_policy)) |
145 | memcpy(iv->nonce, ci->ci_nonce, FS_KEY_DERIVATION_NONCE_SIZE); | 145 | memcpy(iv->nonce, ci->ci_nonce, FS_KEY_DERIVATION_NONCE_SIZE); |
146 | 146 | ||
147 | if (ci->ci_essiv_tfm != NULL) | 147 | if (ci->ci_essiv_tfm != NULL) |
@@ -188,10 +188,8 @@ int fscrypt_crypt_block(const struct inode *inode, fscrypt_direction_t rw, | |||
188 | res = crypto_wait_req(crypto_skcipher_encrypt(req), &wait); | 188 | res = crypto_wait_req(crypto_skcipher_encrypt(req), &wait); |
189 | skcipher_request_free(req); | 189 | skcipher_request_free(req); |
190 | if (res) { | 190 | if (res) { |
191 | fscrypt_err(inode->i_sb, | 191 | fscrypt_err(inode, "%scryption failed for block %llu: %d", |
192 | "%scryption failed for inode %lu, block %llu: %d", | 192 | (rw == FS_DECRYPT ? "De" : "En"), lblk_num, res); |
193 | (rw == FS_DECRYPT ? "de" : "en"), | ||
194 | inode->i_ino, lblk_num, res); | ||
195 | return res; | 193 | return res; |
196 | } | 194 | } |
197 | return 0; | 195 | return 0; |
@@ -453,7 +451,7 @@ fail: | |||
453 | return res; | 451 | return res; |
454 | } | 452 | } |
455 | 453 | ||
456 | void fscrypt_msg(struct super_block *sb, const char *level, | 454 | void fscrypt_msg(const struct inode *inode, const char *level, |
457 | const char *fmt, ...) | 455 | const char *fmt, ...) |
458 | { | 456 | { |
459 | static DEFINE_RATELIMIT_STATE(rs, DEFAULT_RATELIMIT_INTERVAL, | 457 | static DEFINE_RATELIMIT_STATE(rs, DEFAULT_RATELIMIT_INTERVAL, |
@@ -467,8 +465,9 @@ void fscrypt_msg(struct super_block *sb, const char *level, | |||
467 | va_start(args, fmt); | 465 | va_start(args, fmt); |
468 | vaf.fmt = fmt; | 466 | vaf.fmt = fmt; |
469 | vaf.va = &args; | 467 | vaf.va = &args; |
470 | if (sb) | 468 | if (inode) |
471 | printk("%sfscrypt (%s): %pV\n", level, sb->s_id, &vaf); | 469 | printk("%sfscrypt (%s, inode %lu): %pV\n", |
470 | level, inode->i_sb->s_id, inode->i_ino, &vaf); | ||
472 | else | 471 | else |
473 | printk("%sfscrypt: %pV\n", level, &vaf); | 472 | printk("%sfscrypt: %pV\n", level, &vaf); |
474 | va_end(args); | 473 | va_end(args); |
@@ -479,6 +478,8 @@ void fscrypt_msg(struct super_block *sb, const char *level, | |||
479 | */ | 478 | */ |
480 | static int __init fscrypt_init(void) | 479 | static int __init fscrypt_init(void) |
481 | { | 480 | { |
481 | int err = -ENOMEM; | ||
482 | |||
482 | /* | 483 | /* |
483 | * Use an unbound workqueue to allow bios to be decrypted in parallel | 484 | * Use an unbound workqueue to allow bios to be decrypted in parallel |
484 | * even when they happen to complete on the same CPU. This sacrifices | 485 | * even when they happen to complete on the same CPU. This sacrifices |
@@ -501,31 +502,19 @@ static int __init fscrypt_init(void) | |||
501 | if (!fscrypt_info_cachep) | 502 | if (!fscrypt_info_cachep) |
502 | goto fail_free_ctx; | 503 | goto fail_free_ctx; |
503 | 504 | ||
505 | err = fscrypt_init_keyring(); | ||
506 | if (err) | ||
507 | goto fail_free_info; | ||
508 | |||
504 | return 0; | 509 | return 0; |
505 | 510 | ||
511 | fail_free_info: | ||
512 | kmem_cache_destroy(fscrypt_info_cachep); | ||
506 | fail_free_ctx: | 513 | fail_free_ctx: |
507 | kmem_cache_destroy(fscrypt_ctx_cachep); | 514 | kmem_cache_destroy(fscrypt_ctx_cachep); |
508 | fail_free_queue: | 515 | fail_free_queue: |
509 | destroy_workqueue(fscrypt_read_workqueue); | 516 | destroy_workqueue(fscrypt_read_workqueue); |
510 | fail: | 517 | fail: |
511 | return -ENOMEM; | 518 | return err; |
512 | } | ||
513 | module_init(fscrypt_init) | ||
514 | |||
515 | /** | ||
516 | * fscrypt_exit() - Shutdown the fs encryption system | ||
517 | */ | ||
518 | static void __exit fscrypt_exit(void) | ||
519 | { | ||
520 | fscrypt_destroy(); | ||
521 | |||
522 | if (fscrypt_read_workqueue) | ||
523 | destroy_workqueue(fscrypt_read_workqueue); | ||
524 | kmem_cache_destroy(fscrypt_ctx_cachep); | ||
525 | kmem_cache_destroy(fscrypt_info_cachep); | ||
526 | |||
527 | fscrypt_essiv_cleanup(); | ||
528 | } | 519 | } |
529 | module_exit(fscrypt_exit); | 520 | late_initcall(fscrypt_init) |
530 | |||
531 | MODULE_LICENSE("GPL"); | ||
diff --git a/fs/crypto/fname.c b/fs/crypto/fname.c index 00d150ff3033..3da3707c10e3 100644 --- a/fs/crypto/fname.c +++ b/fs/crypto/fname.c | |||
@@ -71,9 +71,7 @@ int fname_encrypt(struct inode *inode, const struct qstr *iname, | |||
71 | res = crypto_wait_req(crypto_skcipher_encrypt(req), &wait); | 71 | res = crypto_wait_req(crypto_skcipher_encrypt(req), &wait); |
72 | skcipher_request_free(req); | 72 | skcipher_request_free(req); |
73 | if (res < 0) { | 73 | if (res < 0) { |
74 | fscrypt_err(inode->i_sb, | 74 | fscrypt_err(inode, "Filename encryption failed: %d", res); |
75 | "Filename encryption failed for inode %lu: %d", | ||
76 | inode->i_ino, res); | ||
77 | return res; | 75 | return res; |
78 | } | 76 | } |
79 | 77 | ||
@@ -117,9 +115,7 @@ static int fname_decrypt(struct inode *inode, | |||
117 | res = crypto_wait_req(crypto_skcipher_decrypt(req), &wait); | 115 | res = crypto_wait_req(crypto_skcipher_decrypt(req), &wait); |
118 | skcipher_request_free(req); | 116 | skcipher_request_free(req); |
119 | if (res < 0) { | 117 | if (res < 0) { |
120 | fscrypt_err(inode->i_sb, | 118 | fscrypt_err(inode, "Filename decryption failed: %d", res); |
121 | "Filename decryption failed for inode %lu: %d", | ||
122 | inode->i_ino, res); | ||
123 | return res; | 119 | return res; |
124 | } | 120 | } |
125 | 121 | ||
@@ -127,44 +123,45 @@ static int fname_decrypt(struct inode *inode, | |||
127 | return 0; | 123 | return 0; |
128 | } | 124 | } |
129 | 125 | ||
130 | static const char *lookup_table = | 126 | static const char lookup_table[65] = |
131 | "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+,"; | 127 | "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+,"; |
132 | 128 | ||
133 | #define BASE64_CHARS(nbytes) DIV_ROUND_UP((nbytes) * 4, 3) | 129 | #define BASE64_CHARS(nbytes) DIV_ROUND_UP((nbytes) * 4, 3) |
134 | 130 | ||
135 | /** | 131 | /** |
136 | * digest_encode() - | 132 | * base64_encode() - |
137 | * | 133 | * |
138 | * Encodes the input digest using characters from the set [a-zA-Z0-9_+]. | 134 | * Encodes the input string using characters from the set [A-Za-z0-9+,]. |
139 | * The encoded string is roughly 4/3 times the size of the input string. | 135 | * The encoded string is roughly 4/3 times the size of the input string. |
136 | * | ||
137 | * Return: length of the encoded string | ||
140 | */ | 138 | */ |
141 | static int digest_encode(const char *src, int len, char *dst) | 139 | static int base64_encode(const u8 *src, int len, char *dst) |
142 | { | 140 | { |
143 | int i = 0, bits = 0, ac = 0; | 141 | int i, bits = 0, ac = 0; |
144 | char *cp = dst; | 142 | char *cp = dst; |
145 | 143 | ||
146 | while (i < len) { | 144 | for (i = 0; i < len; i++) { |
147 | ac += (((unsigned char) src[i]) << bits); | 145 | ac += src[i] << bits; |
148 | bits += 8; | 146 | bits += 8; |
149 | do { | 147 | do { |
150 | *cp++ = lookup_table[ac & 0x3f]; | 148 | *cp++ = lookup_table[ac & 0x3f]; |
151 | ac >>= 6; | 149 | ac >>= 6; |
152 | bits -= 6; | 150 | bits -= 6; |
153 | } while (bits >= 6); | 151 | } while (bits >= 6); |
154 | i++; | ||
155 | } | 152 | } |
156 | if (bits) | 153 | if (bits) |
157 | *cp++ = lookup_table[ac & 0x3f]; | 154 | *cp++ = lookup_table[ac & 0x3f]; |
158 | return cp - dst; | 155 | return cp - dst; |
159 | } | 156 | } |
160 | 157 | ||
161 | static int digest_decode(const char *src, int len, char *dst) | 158 | static int base64_decode(const char *src, int len, u8 *dst) |
162 | { | 159 | { |
163 | int i = 0, bits = 0, ac = 0; | 160 | int i, bits = 0, ac = 0; |
164 | const char *p; | 161 | const char *p; |
165 | char *cp = dst; | 162 | u8 *cp = dst; |
166 | 163 | ||
167 | while (i < len) { | 164 | for (i = 0; i < len; i++) { |
168 | p = strchr(lookup_table, src[i]); | 165 | p = strchr(lookup_table, src[i]); |
169 | if (p == NULL || src[i] == 0) | 166 | if (p == NULL || src[i] == 0) |
170 | return -2; | 167 | return -2; |
@@ -175,7 +172,6 @@ static int digest_decode(const char *src, int len, char *dst) | |||
175 | ac >>= 8; | 172 | ac >>= 8; |
176 | bits -= 8; | 173 | bits -= 8; |
177 | } | 174 | } |
178 | i++; | ||
179 | } | 175 | } |
180 | if (ac) | 176 | if (ac) |
181 | return -1; | 177 | return -1; |
@@ -185,8 +181,9 @@ static int digest_decode(const char *src, int len, char *dst) | |||
185 | bool fscrypt_fname_encrypted_size(const struct inode *inode, u32 orig_len, | 181 | bool fscrypt_fname_encrypted_size(const struct inode *inode, u32 orig_len, |
186 | u32 max_len, u32 *encrypted_len_ret) | 182 | u32 max_len, u32 *encrypted_len_ret) |
187 | { | 183 | { |
188 | int padding = 4 << (inode->i_crypt_info->ci_flags & | 184 | const struct fscrypt_info *ci = inode->i_crypt_info; |
189 | FS_POLICY_FLAGS_PAD_MASK); | 185 | int padding = 4 << (fscrypt_policy_flags(&ci->ci_policy) & |
186 | FSCRYPT_POLICY_FLAGS_PAD_MASK); | ||
190 | u32 encrypted_len; | 187 | u32 encrypted_len; |
191 | 188 | ||
192 | if (orig_len > max_len) | 189 | if (orig_len > max_len) |
@@ -272,7 +269,7 @@ int fscrypt_fname_disk_to_usr(struct inode *inode, | |||
272 | return fname_decrypt(inode, iname, oname); | 269 | return fname_decrypt(inode, iname, oname); |
273 | 270 | ||
274 | if (iname->len <= FSCRYPT_FNAME_MAX_UNDIGESTED_SIZE) { | 271 | if (iname->len <= FSCRYPT_FNAME_MAX_UNDIGESTED_SIZE) { |
275 | oname->len = digest_encode(iname->name, iname->len, | 272 | oname->len = base64_encode(iname->name, iname->len, |
276 | oname->name); | 273 | oname->name); |
277 | return 0; | 274 | return 0; |
278 | } | 275 | } |
@@ -287,7 +284,7 @@ int fscrypt_fname_disk_to_usr(struct inode *inode, | |||
287 | FSCRYPT_FNAME_DIGEST(iname->name, iname->len), | 284 | FSCRYPT_FNAME_DIGEST(iname->name, iname->len), |
288 | FSCRYPT_FNAME_DIGEST_SIZE); | 285 | FSCRYPT_FNAME_DIGEST_SIZE); |
289 | oname->name[0] = '_'; | 286 | oname->name[0] = '_'; |
290 | oname->len = 1 + digest_encode((const char *)&digested_name, | 287 | oname->len = 1 + base64_encode((const u8 *)&digested_name, |
291 | sizeof(digested_name), oname->name + 1); | 288 | sizeof(digested_name), oname->name + 1); |
292 | return 0; | 289 | return 0; |
293 | } | 290 | } |
@@ -380,8 +377,8 @@ int fscrypt_setup_filename(struct inode *dir, const struct qstr *iname, | |||
380 | if (fname->crypto_buf.name == NULL) | 377 | if (fname->crypto_buf.name == NULL) |
381 | return -ENOMEM; | 378 | return -ENOMEM; |
382 | 379 | ||
383 | ret = digest_decode(iname->name + digested, iname->len - digested, | 380 | ret = base64_decode(iname->name + digested, iname->len - digested, |
384 | fname->crypto_buf.name); | 381 | fname->crypto_buf.name); |
385 | if (ret < 0) { | 382 | if (ret < 0) { |
386 | ret = -ENOENT; | 383 | ret = -ENOENT; |
387 | goto errout; | 384 | goto errout; |
diff --git a/fs/crypto/fscrypt_private.h b/fs/crypto/fscrypt_private.h index 8978eec9d766..e84efc01512e 100644 --- a/fs/crypto/fscrypt_private.h +++ b/fs/crypto/fscrypt_private.h | |||
@@ -4,9 +4,8 @@ | |||
4 | * | 4 | * |
5 | * Copyright (C) 2015, Google, Inc. | 5 | * Copyright (C) 2015, Google, Inc. |
6 | * | 6 | * |
7 | * This contains encryption key functions. | 7 | * Originally written by Michael Halcrow, Ildar Muslukhov, and Uday Savagaonkar. |
8 | * | 8 | * Heavily modified since then. |
9 | * Written by Michael Halcrow, Ildar Muslukhov, and Uday Savagaonkar, 2015. | ||
10 | */ | 9 | */ |
11 | 10 | ||
12 | #ifndef _FSCRYPT_PRIVATE_H | 11 | #ifndef _FSCRYPT_PRIVATE_H |
@@ -15,30 +14,133 @@ | |||
15 | #include <linux/fscrypt.h> | 14 | #include <linux/fscrypt.h> |
16 | #include <crypto/hash.h> | 15 | #include <crypto/hash.h> |
17 | 16 | ||
18 | /* Encryption parameters */ | 17 | #define CONST_STRLEN(str) (sizeof(str) - 1) |
18 | |||
19 | #define FS_KEY_DERIVATION_NONCE_SIZE 16 | 19 | #define FS_KEY_DERIVATION_NONCE_SIZE 16 |
20 | 20 | ||
21 | /** | 21 | #define FSCRYPT_MIN_KEY_SIZE 16 |
22 | * Encryption context for inode | 22 | |
23 | * | 23 | #define FSCRYPT_CONTEXT_V1 1 |
24 | * Protector format: | 24 | #define FSCRYPT_CONTEXT_V2 2 |
25 | * 1 byte: Protector format (1 = this version) | 25 | |
26 | * 1 byte: File contents encryption mode | 26 | struct fscrypt_context_v1 { |
27 | * 1 byte: File names encryption mode | 27 | u8 version; /* FSCRYPT_CONTEXT_V1 */ |
28 | * 1 byte: Flags | ||
29 | * 8 bytes: Master Key descriptor | ||
30 | * 16 bytes: Encryption Key derivation nonce | ||
31 | */ | ||
32 | struct fscrypt_context { | ||
33 | u8 format; | ||
34 | u8 contents_encryption_mode; | 28 | u8 contents_encryption_mode; |
35 | u8 filenames_encryption_mode; | 29 | u8 filenames_encryption_mode; |
36 | u8 flags; | 30 | u8 flags; |
37 | u8 master_key_descriptor[FS_KEY_DESCRIPTOR_SIZE]; | 31 | u8 master_key_descriptor[FSCRYPT_KEY_DESCRIPTOR_SIZE]; |
38 | u8 nonce[FS_KEY_DERIVATION_NONCE_SIZE]; | 32 | u8 nonce[FS_KEY_DERIVATION_NONCE_SIZE]; |
39 | } __packed; | 33 | }; |
40 | 34 | ||
41 | #define FS_ENCRYPTION_CONTEXT_FORMAT_V1 1 | 35 | struct fscrypt_context_v2 { |
36 | u8 version; /* FSCRYPT_CONTEXT_V2 */ | ||
37 | u8 contents_encryption_mode; | ||
38 | u8 filenames_encryption_mode; | ||
39 | u8 flags; | ||
40 | u8 __reserved[4]; | ||
41 | u8 master_key_identifier[FSCRYPT_KEY_IDENTIFIER_SIZE]; | ||
42 | u8 nonce[FS_KEY_DERIVATION_NONCE_SIZE]; | ||
43 | }; | ||
44 | |||
45 | /** | ||
46 | * fscrypt_context - the encryption context of an inode | ||
47 | * | ||
48 | * This is the on-disk equivalent of an fscrypt_policy, stored alongside each | ||
49 | * encrypted file usually in a hidden extended attribute. It contains the | ||
50 | * fields from the fscrypt_policy, in order to identify the encryption algorithm | ||
51 | * and key with which the file is encrypted. It also contains a nonce that was | ||
52 | * randomly generated by fscrypt itself; this is used as KDF input or as a tweak | ||
53 | * to cause different files to be encrypted differently. | ||
54 | */ | ||
55 | union fscrypt_context { | ||
56 | u8 version; | ||
57 | struct fscrypt_context_v1 v1; | ||
58 | struct fscrypt_context_v2 v2; | ||
59 | }; | ||
60 | |||
61 | /* | ||
62 | * Return the size expected for the given fscrypt_context based on its version | ||
63 | * number, or 0 if the context version is unrecognized. | ||
64 | */ | ||
65 | static inline int fscrypt_context_size(const union fscrypt_context *ctx) | ||
66 | { | ||
67 | switch (ctx->version) { | ||
68 | case FSCRYPT_CONTEXT_V1: | ||
69 | BUILD_BUG_ON(sizeof(ctx->v1) != 28); | ||
70 | return sizeof(ctx->v1); | ||
71 | case FSCRYPT_CONTEXT_V2: | ||
72 | BUILD_BUG_ON(sizeof(ctx->v2) != 40); | ||
73 | return sizeof(ctx->v2); | ||
74 | } | ||
75 | return 0; | ||
76 | } | ||
77 | |||
78 | #undef fscrypt_policy | ||
79 | union fscrypt_policy { | ||
80 | u8 version; | ||
81 | struct fscrypt_policy_v1 v1; | ||
82 | struct fscrypt_policy_v2 v2; | ||
83 | }; | ||
84 | |||
85 | /* | ||
86 | * Return the size expected for the given fscrypt_policy based on its version | ||
87 | * number, or 0 if the policy version is unrecognized. | ||
88 | */ | ||
89 | static inline int fscrypt_policy_size(const union fscrypt_policy *policy) | ||
90 | { | ||
91 | switch (policy->version) { | ||
92 | case FSCRYPT_POLICY_V1: | ||
93 | return sizeof(policy->v1); | ||
94 | case FSCRYPT_POLICY_V2: | ||
95 | return sizeof(policy->v2); | ||
96 | } | ||
97 | return 0; | ||
98 | } | ||
99 | |||
100 | /* Return the contents encryption mode of a valid encryption policy */ | ||
101 | static inline u8 | ||
102 | fscrypt_policy_contents_mode(const union fscrypt_policy *policy) | ||
103 | { | ||
104 | switch (policy->version) { | ||
105 | case FSCRYPT_POLICY_V1: | ||
106 | return policy->v1.contents_encryption_mode; | ||
107 | case FSCRYPT_POLICY_V2: | ||
108 | return policy->v2.contents_encryption_mode; | ||
109 | } | ||
110 | BUG(); | ||
111 | } | ||
112 | |||
113 | /* Return the filenames encryption mode of a valid encryption policy */ | ||
114 | static inline u8 | ||
115 | fscrypt_policy_fnames_mode(const union fscrypt_policy *policy) | ||
116 | { | ||
117 | switch (policy->version) { | ||
118 | case FSCRYPT_POLICY_V1: | ||
119 | return policy->v1.filenames_encryption_mode; | ||
120 | case FSCRYPT_POLICY_V2: | ||
121 | return policy->v2.filenames_encryption_mode; | ||
122 | } | ||
123 | BUG(); | ||
124 | } | ||
125 | |||
126 | /* Return the flags (FSCRYPT_POLICY_FLAG*) of a valid encryption policy */ | ||
127 | static inline u8 | ||
128 | fscrypt_policy_flags(const union fscrypt_policy *policy) | ||
129 | { | ||
130 | switch (policy->version) { | ||
131 | case FSCRYPT_POLICY_V1: | ||
132 | return policy->v1.flags; | ||
133 | case FSCRYPT_POLICY_V2: | ||
134 | return policy->v2.flags; | ||
135 | } | ||
136 | BUG(); | ||
137 | } | ||
138 | |||
139 | static inline bool | ||
140 | fscrypt_is_direct_key_policy(const union fscrypt_policy *policy) | ||
141 | { | ||
142 | return fscrypt_policy_flags(policy) & FSCRYPT_POLICY_FLAG_DIRECT_KEY; | ||
143 | } | ||
42 | 144 | ||
43 | /** | 145 | /** |
44 | * For encrypted symlinks, the ciphertext length is stored at the beginning | 146 | * For encrypted symlinks, the ciphertext length is stored at the beginning |
@@ -68,23 +170,37 @@ struct fscrypt_info { | |||
68 | struct crypto_cipher *ci_essiv_tfm; | 170 | struct crypto_cipher *ci_essiv_tfm; |
69 | 171 | ||
70 | /* | 172 | /* |
71 | * Encryption mode used for this inode. It corresponds to either | 173 | * Encryption mode used for this inode. It corresponds to either the |
72 | * ci_data_mode or ci_filename_mode, depending on the inode type. | 174 | * contents or filenames encryption mode, depending on the inode type. |
73 | */ | 175 | */ |
74 | struct fscrypt_mode *ci_mode; | 176 | struct fscrypt_mode *ci_mode; |
75 | 177 | ||
178 | /* Back-pointer to the inode */ | ||
179 | struct inode *ci_inode; | ||
180 | |||
181 | /* | ||
182 | * The master key with which this inode was unlocked (decrypted). This | ||
183 | * will be NULL if the master key was found in a process-subscribed | ||
184 | * keyring rather than in the filesystem-level keyring. | ||
185 | */ | ||
186 | struct key *ci_master_key; | ||
187 | |||
188 | /* | ||
189 | * Link in list of inodes that were unlocked with the master key. | ||
190 | * Only used when ->ci_master_key is set. | ||
191 | */ | ||
192 | struct list_head ci_master_key_link; | ||
193 | |||
76 | /* | 194 | /* |
77 | * If non-NULL, then this inode uses a master key directly rather than a | 195 | * If non-NULL, then encryption is done using the master key directly |
78 | * derived key, and ci_ctfm will equal ci_master_key->mk_ctfm. | 196 | * and ci_ctfm will equal ci_direct_key->dk_ctfm. |
79 | * Otherwise, this inode uses a derived key. | ||
80 | */ | 197 | */ |
81 | struct fscrypt_master_key *ci_master_key; | 198 | struct fscrypt_direct_key *ci_direct_key; |
82 | 199 | ||
83 | /* fields from the fscrypt_context */ | 200 | /* The encryption policy used by this inode */ |
84 | u8 ci_data_mode; | 201 | union fscrypt_policy ci_policy; |
85 | u8 ci_filename_mode; | 202 | |
86 | u8 ci_flags; | 203 | /* This inode's nonce, copied from the fscrypt_context */ |
87 | u8 ci_master_key_descriptor[FS_KEY_DESCRIPTOR_SIZE]; | ||
88 | u8 ci_nonce[FS_KEY_DERIVATION_NONCE_SIZE]; | 204 | u8 ci_nonce[FS_KEY_DERIVATION_NONCE_SIZE]; |
89 | }; | 205 | }; |
90 | 206 | ||
@@ -98,16 +214,16 @@ typedef enum { | |||
98 | static inline bool fscrypt_valid_enc_modes(u32 contents_mode, | 214 | static inline bool fscrypt_valid_enc_modes(u32 contents_mode, |
99 | u32 filenames_mode) | 215 | u32 filenames_mode) |
100 | { | 216 | { |
101 | if (contents_mode == FS_ENCRYPTION_MODE_AES_128_CBC && | 217 | if (contents_mode == FSCRYPT_MODE_AES_128_CBC && |
102 | filenames_mode == FS_ENCRYPTION_MODE_AES_128_CTS) | 218 | filenames_mode == FSCRYPT_MODE_AES_128_CTS) |
103 | return true; | 219 | return true; |
104 | 220 | ||
105 | if (contents_mode == FS_ENCRYPTION_MODE_AES_256_XTS && | 221 | if (contents_mode == FSCRYPT_MODE_AES_256_XTS && |
106 | filenames_mode == FS_ENCRYPTION_MODE_AES_256_CTS) | 222 | filenames_mode == FSCRYPT_MODE_AES_256_CTS) |
107 | return true; | 223 | return true; |
108 | 224 | ||
109 | if (contents_mode == FS_ENCRYPTION_MODE_ADIANTUM && | 225 | if (contents_mode == FSCRYPT_MODE_ADIANTUM && |
110 | filenames_mode == FS_ENCRYPTION_MODE_ADIANTUM) | 226 | filenames_mode == FSCRYPT_MODE_ADIANTUM) |
111 | return true; | 227 | return true; |
112 | 228 | ||
113 | return false; | 229 | return false; |
@@ -125,12 +241,12 @@ extern struct page *fscrypt_alloc_bounce_page(gfp_t gfp_flags); | |||
125 | extern const struct dentry_operations fscrypt_d_ops; | 241 | extern const struct dentry_operations fscrypt_d_ops; |
126 | 242 | ||
127 | extern void __printf(3, 4) __cold | 243 | extern void __printf(3, 4) __cold |
128 | fscrypt_msg(struct super_block *sb, const char *level, const char *fmt, ...); | 244 | fscrypt_msg(const struct inode *inode, const char *level, const char *fmt, ...); |
129 | 245 | ||
130 | #define fscrypt_warn(sb, fmt, ...) \ | 246 | #define fscrypt_warn(inode, fmt, ...) \ |
131 | fscrypt_msg(sb, KERN_WARNING, fmt, ##__VA_ARGS__) | 247 | fscrypt_msg((inode), KERN_WARNING, fmt, ##__VA_ARGS__) |
132 | #define fscrypt_err(sb, fmt, ...) \ | 248 | #define fscrypt_err(inode, fmt, ...) \ |
133 | fscrypt_msg(sb, KERN_ERR, fmt, ##__VA_ARGS__) | 249 | fscrypt_msg((inode), KERN_ERR, fmt, ##__VA_ARGS__) |
134 | 250 | ||
135 | #define FSCRYPT_MAX_IV_SIZE 32 | 251 | #define FSCRYPT_MAX_IV_SIZE 32 |
136 | 252 | ||
@@ -155,7 +271,172 @@ extern bool fscrypt_fname_encrypted_size(const struct inode *inode, | |||
155 | u32 orig_len, u32 max_len, | 271 | u32 orig_len, u32 max_len, |
156 | u32 *encrypted_len_ret); | 272 | u32 *encrypted_len_ret); |
157 | 273 | ||
158 | /* keyinfo.c */ | 274 | /* hkdf.c */ |
275 | |||
276 | struct fscrypt_hkdf { | ||
277 | struct crypto_shash *hmac_tfm; | ||
278 | }; | ||
279 | |||
280 | extern int fscrypt_init_hkdf(struct fscrypt_hkdf *hkdf, const u8 *master_key, | ||
281 | unsigned int master_key_size); | ||
282 | |||
283 | /* | ||
284 | * The list of contexts in which fscrypt uses HKDF. These values are used as | ||
285 | * the first byte of the HKDF application-specific info string to guarantee that | ||
286 | * info strings are never repeated between contexts. This ensures that all HKDF | ||
287 | * outputs are unique and cryptographically isolated, i.e. knowledge of one | ||
288 | * output doesn't reveal another. | ||
289 | */ | ||
290 | #define HKDF_CONTEXT_KEY_IDENTIFIER 1 | ||
291 | #define HKDF_CONTEXT_PER_FILE_KEY 2 | ||
292 | #define HKDF_CONTEXT_PER_MODE_KEY 3 | ||
293 | |||
294 | extern int fscrypt_hkdf_expand(struct fscrypt_hkdf *hkdf, u8 context, | ||
295 | const u8 *info, unsigned int infolen, | ||
296 | u8 *okm, unsigned int okmlen); | ||
297 | |||
298 | extern void fscrypt_destroy_hkdf(struct fscrypt_hkdf *hkdf); | ||
299 | |||
300 | /* keyring.c */ | ||
301 | |||
302 | /* | ||
303 | * fscrypt_master_key_secret - secret key material of an in-use master key | ||
304 | */ | ||
305 | struct fscrypt_master_key_secret { | ||
306 | |||
307 | /* | ||
308 | * For v2 policy keys: HKDF context keyed by this master key. | ||
309 | * For v1 policy keys: not set (hkdf.hmac_tfm == NULL). | ||
310 | */ | ||
311 | struct fscrypt_hkdf hkdf; | ||
312 | |||
313 | /* Size of the raw key in bytes. Set even if ->raw isn't set. */ | ||
314 | u32 size; | ||
315 | |||
316 | /* For v1 policy keys: the raw key. Wiped for v2 policy keys. */ | ||
317 | u8 raw[FSCRYPT_MAX_KEY_SIZE]; | ||
318 | |||
319 | } __randomize_layout; | ||
320 | |||
321 | /* | ||
322 | * fscrypt_master_key - an in-use master key | ||
323 | * | ||
324 | * This represents a master encryption key which has been added to the | ||
325 | * filesystem and can be used to "unlock" the encrypted files which were | ||
326 | * encrypted with it. | ||
327 | */ | ||
328 | struct fscrypt_master_key { | ||
329 | |||
330 | /* | ||
331 | * The secret key material. After FS_IOC_REMOVE_ENCRYPTION_KEY is | ||
332 | * executed, this is wiped and no new inodes can be unlocked with this | ||
333 | * key; however, there may still be inodes in ->mk_decrypted_inodes | ||
334 | * which could not be evicted. As long as some inodes still remain, | ||
335 | * FS_IOC_REMOVE_ENCRYPTION_KEY can be retried, or | ||
336 | * FS_IOC_ADD_ENCRYPTION_KEY can add the secret again. | ||
337 | * | ||
338 | * Locking: protected by key->sem (outer) and mk_secret_sem (inner). | ||
339 | * The reason for two locks is that key->sem also protects modifying | ||
340 | * mk_users, which ranks it above the semaphore for the keyring key | ||
341 | * type, which is in turn above page faults (via keyring_read). But | ||
342 | * sometimes filesystems call fscrypt_get_encryption_info() from within | ||
343 | * a transaction, which ranks it below page faults. So we need a | ||
344 | * separate lock which protects mk_secret but not also mk_users. | ||
345 | */ | ||
346 | struct fscrypt_master_key_secret mk_secret; | ||
347 | struct rw_semaphore mk_secret_sem; | ||
348 | |||
349 | /* | ||
350 | * For v1 policy keys: an arbitrary key descriptor which was assigned by | ||
351 | * userspace (->descriptor). | ||
352 | * | ||
353 | * For v2 policy keys: a cryptographic hash of this key (->identifier). | ||
354 | */ | ||
355 | struct fscrypt_key_specifier mk_spec; | ||
356 | |||
357 | /* | ||
358 | * Keyring which contains a key of type 'key_type_fscrypt_user' for each | ||
359 | * user who has added this key. Normally each key will be added by just | ||
360 | * one user, but it's possible that multiple users share a key, and in | ||
361 | * that case we need to keep track of those users so that one user can't | ||
362 | * remove the key before the others want it removed too. | ||
363 | * | ||
364 | * This is NULL for v1 policy keys; those can only be added by root. | ||
365 | * | ||
366 | * Locking: in addition to this keyrings own semaphore, this is | ||
367 | * protected by the master key's key->sem, so we can do atomic | ||
368 | * search+insert. It can also be searched without taking any locks, but | ||
369 | * in that case the returned key may have already been removed. | ||
370 | */ | ||
371 | struct key *mk_users; | ||
372 | |||
373 | /* | ||
374 | * Length of ->mk_decrypted_inodes, plus one if mk_secret is present. | ||
375 | * Once this goes to 0, the master key is removed from ->s_master_keys. | ||
376 | * The 'struct fscrypt_master_key' will continue to live as long as the | ||
377 | * 'struct key' whose payload it is, but we won't let this reference | ||
378 | * count rise again. | ||
379 | */ | ||
380 | refcount_t mk_refcount; | ||
381 | |||
382 | /* | ||
383 | * List of inodes that were unlocked using this key. This allows the | ||
384 | * inodes to be evicted efficiently if the key is removed. | ||
385 | */ | ||
386 | struct list_head mk_decrypted_inodes; | ||
387 | spinlock_t mk_decrypted_inodes_lock; | ||
388 | |||
389 | /* Per-mode tfms for DIRECT_KEY policies, allocated on-demand */ | ||
390 | struct crypto_skcipher *mk_mode_keys[__FSCRYPT_MODE_MAX + 1]; | ||
391 | |||
392 | } __randomize_layout; | ||
393 | |||
394 | static inline bool | ||
395 | is_master_key_secret_present(const struct fscrypt_master_key_secret *secret) | ||
396 | { | ||
397 | /* | ||
398 | * The READ_ONCE() is only necessary for fscrypt_drop_inode() and | ||
399 | * fscrypt_key_describe(). These run in atomic context, so they can't | ||
400 | * take ->mk_secret_sem and thus 'secret' can change concurrently which | ||
401 | * would be a data race. But they only need to know whether the secret | ||
402 | * *was* present at the time of check, so READ_ONCE() suffices. | ||
403 | */ | ||
404 | return READ_ONCE(secret->size) != 0; | ||
405 | } | ||
406 | |||
407 | static inline const char *master_key_spec_type( | ||
408 | const struct fscrypt_key_specifier *spec) | ||
409 | { | ||
410 | switch (spec->type) { | ||
411 | case FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR: | ||
412 | return "descriptor"; | ||
413 | case FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER: | ||
414 | return "identifier"; | ||
415 | } | ||
416 | return "[unknown]"; | ||
417 | } | ||
418 | |||
419 | static inline int master_key_spec_len(const struct fscrypt_key_specifier *spec) | ||
420 | { | ||
421 | switch (spec->type) { | ||
422 | case FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR: | ||
423 | return FSCRYPT_KEY_DESCRIPTOR_SIZE; | ||
424 | case FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER: | ||
425 | return FSCRYPT_KEY_IDENTIFIER_SIZE; | ||
426 | } | ||
427 | return 0; | ||
428 | } | ||
429 | |||
430 | extern struct key * | ||
431 | fscrypt_find_master_key(struct super_block *sb, | ||
432 | const struct fscrypt_key_specifier *mk_spec); | ||
433 | |||
434 | extern int fscrypt_verify_key_added(struct super_block *sb, | ||
435 | const u8 identifier[FSCRYPT_KEY_IDENTIFIER_SIZE]); | ||
436 | |||
437 | extern int __init fscrypt_init_keyring(void); | ||
438 | |||
439 | /* keysetup.c */ | ||
159 | 440 | ||
160 | struct fscrypt_mode { | 441 | struct fscrypt_mode { |
161 | const char *friendly_name; | 442 | const char *friendly_name; |
@@ -166,6 +447,36 @@ struct fscrypt_mode { | |||
166 | bool needs_essiv; | 447 | bool needs_essiv; |
167 | }; | 448 | }; |
168 | 449 | ||
169 | extern void __exit fscrypt_essiv_cleanup(void); | 450 | static inline bool |
451 | fscrypt_mode_supports_direct_key(const struct fscrypt_mode *mode) | ||
452 | { | ||
453 | return mode->ivsize >= offsetofend(union fscrypt_iv, nonce); | ||
454 | } | ||
455 | |||
456 | extern struct crypto_skcipher * | ||
457 | fscrypt_allocate_skcipher(struct fscrypt_mode *mode, const u8 *raw_key, | ||
458 | const struct inode *inode); | ||
459 | |||
460 | extern int fscrypt_set_derived_key(struct fscrypt_info *ci, | ||
461 | const u8 *derived_key); | ||
462 | |||
463 | /* keysetup_v1.c */ | ||
464 | |||
465 | extern void fscrypt_put_direct_key(struct fscrypt_direct_key *dk); | ||
466 | |||
467 | extern int fscrypt_setup_v1_file_key(struct fscrypt_info *ci, | ||
468 | const u8 *raw_master_key); | ||
469 | |||
470 | extern int fscrypt_setup_v1_file_key_via_subscribed_keyrings( | ||
471 | struct fscrypt_info *ci); | ||
472 | /* policy.c */ | ||
473 | |||
474 | extern bool fscrypt_policies_equal(const union fscrypt_policy *policy1, | ||
475 | const union fscrypt_policy *policy2); | ||
476 | extern bool fscrypt_supported_policy(const union fscrypt_policy *policy_u, | ||
477 | const struct inode *inode); | ||
478 | extern int fscrypt_policy_from_context(union fscrypt_policy *policy_u, | ||
479 | const union fscrypt_context *ctx_u, | ||
480 | int ctx_size); | ||
170 | 481 | ||
171 | #endif /* _FSCRYPT_PRIVATE_H */ | 482 | #endif /* _FSCRYPT_PRIVATE_H */ |
diff --git a/fs/crypto/hkdf.c b/fs/crypto/hkdf.c new file mode 100644 index 000000000000..f21873e1b467 --- /dev/null +++ b/fs/crypto/hkdf.c | |||
@@ -0,0 +1,181 @@ | |||
1 | // SPDX-License-Identifier: GPL-2.0 | ||
2 | /* | ||
3 | * Implementation of HKDF ("HMAC-based Extract-and-Expand Key Derivation | ||
4 | * Function"), aka RFC 5869. See also the original paper (Krawczyk 2010): | ||
5 | * "Cryptographic Extraction and Key Derivation: The HKDF Scheme". | ||
6 | * | ||
7 | * This is used to derive keys from the fscrypt master keys. | ||
8 | * | ||
9 | * Copyright 2019 Google LLC | ||
10 | */ | ||
11 | |||
12 | #include <crypto/hash.h> | ||
13 | #include <crypto/sha.h> | ||
14 | |||
15 | #include "fscrypt_private.h" | ||
16 | |||
17 | /* | ||
18 | * HKDF supports any unkeyed cryptographic hash algorithm, but fscrypt uses | ||
19 | * SHA-512 because it is reasonably secure and efficient; and since it produces | ||
20 | * a 64-byte digest, deriving an AES-256-XTS key preserves all 64 bytes of | ||
21 | * entropy from the master key and requires only one iteration of HKDF-Expand. | ||
22 | */ | ||
23 | #define HKDF_HMAC_ALG "hmac(sha512)" | ||
24 | #define HKDF_HASHLEN SHA512_DIGEST_SIZE | ||
25 | |||
26 | /* | ||
27 | * HKDF consists of two steps: | ||
28 | * | ||
29 | * 1. HKDF-Extract: extract a pseudorandom key of length HKDF_HASHLEN bytes from | ||
30 | * the input keying material and optional salt. | ||
31 | * 2. HKDF-Expand: expand the pseudorandom key into output keying material of | ||
32 | * any length, parameterized by an application-specific info string. | ||
33 | * | ||
34 | * HKDF-Extract can be skipped if the input is already a pseudorandom key of | ||
35 | * length HKDF_HASHLEN bytes. However, cipher modes other than AES-256-XTS take | ||
36 | * shorter keys, and we don't want to force users of those modes to provide | ||
37 | * unnecessarily long master keys. Thus fscrypt still does HKDF-Extract. No | ||
38 | * salt is used, since fscrypt master keys should already be pseudorandom and | ||
39 | * there's no way to persist a random salt per master key from kernel mode. | ||
40 | */ | ||
41 | |||
42 | /* HKDF-Extract (RFC 5869 section 2.2), unsalted */ | ||
43 | static int hkdf_extract(struct crypto_shash *hmac_tfm, const u8 *ikm, | ||
44 | unsigned int ikmlen, u8 prk[HKDF_HASHLEN]) | ||
45 | { | ||
46 | static const u8 default_salt[HKDF_HASHLEN]; | ||
47 | SHASH_DESC_ON_STACK(desc, hmac_tfm); | ||
48 | int err; | ||
49 | |||
50 | err = crypto_shash_setkey(hmac_tfm, default_salt, HKDF_HASHLEN); | ||
51 | if (err) | ||
52 | return err; | ||
53 | |||
54 | desc->tfm = hmac_tfm; | ||
55 | err = crypto_shash_digest(desc, ikm, ikmlen, prk); | ||
56 | shash_desc_zero(desc); | ||
57 | return err; | ||
58 | } | ||
59 | |||
60 | /* | ||
61 | * Compute HKDF-Extract using the given master key as the input keying material, | ||
62 | * and prepare an HMAC transform object keyed by the resulting pseudorandom key. | ||
63 | * | ||
64 | * Afterwards, the keyed HMAC transform object can be used for HKDF-Expand many | ||
65 | * times without having to recompute HKDF-Extract each time. | ||
66 | */ | ||
67 | int fscrypt_init_hkdf(struct fscrypt_hkdf *hkdf, const u8 *master_key, | ||
68 | unsigned int master_key_size) | ||
69 | { | ||
70 | struct crypto_shash *hmac_tfm; | ||
71 | u8 prk[HKDF_HASHLEN]; | ||
72 | int err; | ||
73 | |||
74 | hmac_tfm = crypto_alloc_shash(HKDF_HMAC_ALG, 0, 0); | ||
75 | if (IS_ERR(hmac_tfm)) { | ||
76 | fscrypt_err(NULL, "Error allocating " HKDF_HMAC_ALG ": %ld", | ||
77 | PTR_ERR(hmac_tfm)); | ||
78 | return PTR_ERR(hmac_tfm); | ||
79 | } | ||
80 | |||
81 | if (WARN_ON(crypto_shash_digestsize(hmac_tfm) != sizeof(prk))) { | ||
82 | err = -EINVAL; | ||
83 | goto err_free_tfm; | ||
84 | } | ||
85 | |||
86 | err = hkdf_extract(hmac_tfm, master_key, master_key_size, prk); | ||
87 | if (err) | ||
88 | goto err_free_tfm; | ||
89 | |||
90 | err = crypto_shash_setkey(hmac_tfm, prk, sizeof(prk)); | ||
91 | if (err) | ||
92 | goto err_free_tfm; | ||
93 | |||
94 | hkdf->hmac_tfm = hmac_tfm; | ||
95 | goto out; | ||
96 | |||
97 | err_free_tfm: | ||
98 | crypto_free_shash(hmac_tfm); | ||
99 | out: | ||
100 | memzero_explicit(prk, sizeof(prk)); | ||
101 | return err; | ||
102 | } | ||
103 | |||
104 | /* | ||
105 | * HKDF-Expand (RFC 5869 section 2.3). This expands the pseudorandom key, which | ||
106 | * was already keyed into 'hkdf->hmac_tfm' by fscrypt_init_hkdf(), into 'okmlen' | ||
107 | * bytes of output keying material parameterized by the application-specific | ||
108 | * 'info' of length 'infolen' bytes, prefixed by "fscrypt\0" and the 'context' | ||
109 | * byte. This is thread-safe and may be called by multiple threads in parallel. | ||
110 | * | ||
111 | * ('context' isn't part of the HKDF specification; it's just a prefix fscrypt | ||
112 | * adds to its application-specific info strings to guarantee that it doesn't | ||
113 | * accidentally repeat an info string when using HKDF for different purposes.) | ||
114 | */ | ||
115 | int fscrypt_hkdf_expand(struct fscrypt_hkdf *hkdf, u8 context, | ||
116 | const u8 *info, unsigned int infolen, | ||
117 | u8 *okm, unsigned int okmlen) | ||
118 | { | ||
119 | SHASH_DESC_ON_STACK(desc, hkdf->hmac_tfm); | ||
120 | u8 prefix[9]; | ||
121 | unsigned int i; | ||
122 | int err; | ||
123 | const u8 *prev = NULL; | ||
124 | u8 counter = 1; | ||
125 | u8 tmp[HKDF_HASHLEN]; | ||
126 | |||
127 | if (WARN_ON(okmlen > 255 * HKDF_HASHLEN)) | ||
128 | return -EINVAL; | ||
129 | |||
130 | desc->tfm = hkdf->hmac_tfm; | ||
131 | |||
132 | memcpy(prefix, "fscrypt\0", 8); | ||
133 | prefix[8] = context; | ||
134 | |||
135 | for (i = 0; i < okmlen; i += HKDF_HASHLEN) { | ||
136 | |||
137 | err = crypto_shash_init(desc); | ||
138 | if (err) | ||
139 | goto out; | ||
140 | |||
141 | if (prev) { | ||
142 | err = crypto_shash_update(desc, prev, HKDF_HASHLEN); | ||
143 | if (err) | ||
144 | goto out; | ||
145 | } | ||
146 | |||
147 | err = crypto_shash_update(desc, prefix, sizeof(prefix)); | ||
148 | if (err) | ||
149 | goto out; | ||
150 | |||
151 | err = crypto_shash_update(desc, info, infolen); | ||
152 | if (err) | ||
153 | goto out; | ||
154 | |||
155 | BUILD_BUG_ON(sizeof(counter) != 1); | ||
156 | if (okmlen - i < HKDF_HASHLEN) { | ||
157 | err = crypto_shash_finup(desc, &counter, 1, tmp); | ||
158 | if (err) | ||
159 | goto out; | ||
160 | memcpy(&okm[i], tmp, okmlen - i); | ||
161 | memzero_explicit(tmp, sizeof(tmp)); | ||
162 | } else { | ||
163 | err = crypto_shash_finup(desc, &counter, 1, &okm[i]); | ||
164 | if (err) | ||
165 | goto out; | ||
166 | } | ||
167 | counter++; | ||
168 | prev = &okm[i]; | ||
169 | } | ||
170 | err = 0; | ||
171 | out: | ||
172 | if (unlikely(err)) | ||
173 | memzero_explicit(okm, okmlen); /* so caller doesn't need to */ | ||
174 | shash_desc_zero(desc); | ||
175 | return err; | ||
176 | } | ||
177 | |||
178 | void fscrypt_destroy_hkdf(struct fscrypt_hkdf *hkdf) | ||
179 | { | ||
180 | crypto_free_shash(hkdf->hmac_tfm); | ||
181 | } | ||
diff --git a/fs/crypto/hooks.c b/fs/crypto/hooks.c index c1d6715d88e9..bb3b7fcfdd48 100644 --- a/fs/crypto/hooks.c +++ b/fs/crypto/hooks.c | |||
@@ -39,9 +39,9 @@ int fscrypt_file_open(struct inode *inode, struct file *filp) | |||
39 | dir = dget_parent(file_dentry(filp)); | 39 | dir = dget_parent(file_dentry(filp)); |
40 | if (IS_ENCRYPTED(d_inode(dir)) && | 40 | if (IS_ENCRYPTED(d_inode(dir)) && |
41 | !fscrypt_has_permitted_context(d_inode(dir), inode)) { | 41 | !fscrypt_has_permitted_context(d_inode(dir), inode)) { |
42 | fscrypt_warn(inode->i_sb, | 42 | fscrypt_warn(inode, |
43 | "inconsistent encryption contexts: %lu/%lu", | 43 | "Inconsistent encryption context (parent directory: %lu)", |
44 | d_inode(dir)->i_ino, inode->i_ino); | 44 | d_inode(dir)->i_ino); |
45 | err = -EPERM; | 45 | err = -EPERM; |
46 | } | 46 | } |
47 | dput(dir); | 47 | dput(dir); |
diff --git a/fs/crypto/keyinfo.c b/fs/crypto/keyinfo.c deleted file mode 100644 index 207ebed918c1..000000000000 --- a/fs/crypto/keyinfo.c +++ /dev/null | |||
@@ -1,611 +0,0 @@ | |||
1 | // SPDX-License-Identifier: GPL-2.0 | ||
2 | /* | ||
3 | * key management facility for FS encryption support. | ||
4 | * | ||
5 | * Copyright (C) 2015, Google, Inc. | ||
6 | * | ||
7 | * This contains encryption key functions. | ||
8 | * | ||
9 | * Written by Michael Halcrow, Ildar Muslukhov, and Uday Savagaonkar, 2015. | ||
10 | */ | ||
11 | |||
12 | #include <keys/user-type.h> | ||
13 | #include <linux/hashtable.h> | ||
14 | #include <linux/scatterlist.h> | ||
15 | #include <crypto/aes.h> | ||
16 | #include <crypto/algapi.h> | ||
17 | #include <crypto/sha.h> | ||
18 | #include <crypto/skcipher.h> | ||
19 | #include "fscrypt_private.h" | ||
20 | |||
21 | static struct crypto_shash *essiv_hash_tfm; | ||
22 | |||
23 | /* Table of keys referenced by FS_POLICY_FLAG_DIRECT_KEY policies */ | ||
24 | static DEFINE_HASHTABLE(fscrypt_master_keys, 6); /* 6 bits = 64 buckets */ | ||
25 | static DEFINE_SPINLOCK(fscrypt_master_keys_lock); | ||
26 | |||
27 | /* | ||
28 | * Key derivation function. This generates the derived key by encrypting the | ||
29 | * master key with AES-128-ECB using the inode's nonce as the AES key. | ||
30 | * | ||
31 | * The master key must be at least as long as the derived key. If the master | ||
32 | * key is longer, then only the first 'derived_keysize' bytes are used. | ||
33 | */ | ||
34 | static int derive_key_aes(const u8 *master_key, | ||
35 | const struct fscrypt_context *ctx, | ||
36 | u8 *derived_key, unsigned int derived_keysize) | ||
37 | { | ||
38 | int res = 0; | ||
39 | struct skcipher_request *req = NULL; | ||
40 | DECLARE_CRYPTO_WAIT(wait); | ||
41 | struct scatterlist src_sg, dst_sg; | ||
42 | struct crypto_skcipher *tfm = crypto_alloc_skcipher("ecb(aes)", 0, 0); | ||
43 | |||
44 | if (IS_ERR(tfm)) { | ||
45 | res = PTR_ERR(tfm); | ||
46 | tfm = NULL; | ||
47 | goto out; | ||
48 | } | ||
49 | crypto_skcipher_set_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS); | ||
50 | req = skcipher_request_alloc(tfm, GFP_NOFS); | ||
51 | if (!req) { | ||
52 | res = -ENOMEM; | ||
53 | goto out; | ||
54 | } | ||
55 | skcipher_request_set_callback(req, | ||
56 | CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP, | ||
57 | crypto_req_done, &wait); | ||
58 | res = crypto_skcipher_setkey(tfm, ctx->nonce, sizeof(ctx->nonce)); | ||
59 | if (res < 0) | ||
60 | goto out; | ||
61 | |||
62 | sg_init_one(&src_sg, master_key, derived_keysize); | ||
63 | sg_init_one(&dst_sg, derived_key, derived_keysize); | ||
64 | skcipher_request_set_crypt(req, &src_sg, &dst_sg, derived_keysize, | ||
65 | NULL); | ||
66 | res = crypto_wait_req(crypto_skcipher_encrypt(req), &wait); | ||
67 | out: | ||
68 | skcipher_request_free(req); | ||
69 | crypto_free_skcipher(tfm); | ||
70 | return res; | ||
71 | } | ||
72 | |||
73 | /* | ||
74 | * Search the current task's subscribed keyrings for a "logon" key with | ||
75 | * description prefix:descriptor, and if found acquire a read lock on it and | ||
76 | * return a pointer to its validated payload in *payload_ret. | ||
77 | */ | ||
78 | static struct key * | ||
79 | find_and_lock_process_key(const char *prefix, | ||
80 | const u8 descriptor[FS_KEY_DESCRIPTOR_SIZE], | ||
81 | unsigned int min_keysize, | ||
82 | const struct fscrypt_key **payload_ret) | ||
83 | { | ||
84 | char *description; | ||
85 | struct key *key; | ||
86 | const struct user_key_payload *ukp; | ||
87 | const struct fscrypt_key *payload; | ||
88 | |||
89 | description = kasprintf(GFP_NOFS, "%s%*phN", prefix, | ||
90 | FS_KEY_DESCRIPTOR_SIZE, descriptor); | ||
91 | if (!description) | ||
92 | return ERR_PTR(-ENOMEM); | ||
93 | |||
94 | key = request_key(&key_type_logon, description, NULL); | ||
95 | kfree(description); | ||
96 | if (IS_ERR(key)) | ||
97 | return key; | ||
98 | |||
99 | down_read(&key->sem); | ||
100 | ukp = user_key_payload_locked(key); | ||
101 | |||
102 | if (!ukp) /* was the key revoked before we acquired its semaphore? */ | ||
103 | goto invalid; | ||
104 | |||
105 | payload = (const struct fscrypt_key *)ukp->data; | ||
106 | |||
107 | if (ukp->datalen != sizeof(struct fscrypt_key) || | ||
108 | payload->size < 1 || payload->size > FS_MAX_KEY_SIZE) { | ||
109 | fscrypt_warn(NULL, | ||
110 | "key with description '%s' has invalid payload", | ||
111 | key->description); | ||
112 | goto invalid; | ||
113 | } | ||
114 | |||
115 | if (payload->size < min_keysize) { | ||
116 | fscrypt_warn(NULL, | ||
117 | "key with description '%s' is too short (got %u bytes, need %u+ bytes)", | ||
118 | key->description, payload->size, min_keysize); | ||
119 | goto invalid; | ||
120 | } | ||
121 | |||
122 | *payload_ret = payload; | ||
123 | return key; | ||
124 | |||
125 | invalid: | ||
126 | up_read(&key->sem); | ||
127 | key_put(key); | ||
128 | return ERR_PTR(-ENOKEY); | ||
129 | } | ||
130 | |||
131 | static struct fscrypt_mode available_modes[] = { | ||
132 | [FS_ENCRYPTION_MODE_AES_256_XTS] = { | ||
133 | .friendly_name = "AES-256-XTS", | ||
134 | .cipher_str = "xts(aes)", | ||
135 | .keysize = 64, | ||
136 | .ivsize = 16, | ||
137 | }, | ||
138 | [FS_ENCRYPTION_MODE_AES_256_CTS] = { | ||
139 | .friendly_name = "AES-256-CTS-CBC", | ||
140 | .cipher_str = "cts(cbc(aes))", | ||
141 | .keysize = 32, | ||
142 | .ivsize = 16, | ||
143 | }, | ||
144 | [FS_ENCRYPTION_MODE_AES_128_CBC] = { | ||
145 | .friendly_name = "AES-128-CBC", | ||
146 | .cipher_str = "cbc(aes)", | ||
147 | .keysize = 16, | ||
148 | .ivsize = 16, | ||
149 | .needs_essiv = true, | ||
150 | }, | ||
151 | [FS_ENCRYPTION_MODE_AES_128_CTS] = { | ||
152 | .friendly_name = "AES-128-CTS-CBC", | ||
153 | .cipher_str = "cts(cbc(aes))", | ||
154 | .keysize = 16, | ||
155 | .ivsize = 16, | ||
156 | }, | ||
157 | [FS_ENCRYPTION_MODE_ADIANTUM] = { | ||
158 | .friendly_name = "Adiantum", | ||
159 | .cipher_str = "adiantum(xchacha12,aes)", | ||
160 | .keysize = 32, | ||
161 | .ivsize = 32, | ||
162 | }, | ||
163 | }; | ||
164 | |||
165 | static struct fscrypt_mode * | ||
166 | select_encryption_mode(const struct fscrypt_info *ci, const struct inode *inode) | ||
167 | { | ||
168 | if (!fscrypt_valid_enc_modes(ci->ci_data_mode, ci->ci_filename_mode)) { | ||
169 | fscrypt_warn(inode->i_sb, | ||
170 | "inode %lu uses unsupported encryption modes (contents mode %d, filenames mode %d)", | ||
171 | inode->i_ino, ci->ci_data_mode, | ||
172 | ci->ci_filename_mode); | ||
173 | return ERR_PTR(-EINVAL); | ||
174 | } | ||
175 | |||
176 | if (S_ISREG(inode->i_mode)) | ||
177 | return &available_modes[ci->ci_data_mode]; | ||
178 | |||
179 | if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) | ||
180 | return &available_modes[ci->ci_filename_mode]; | ||
181 | |||
182 | WARN_ONCE(1, "fscrypt: filesystem tried to load encryption info for inode %lu, which is not encryptable (file type %d)\n", | ||
183 | inode->i_ino, (inode->i_mode & S_IFMT)); | ||
184 | return ERR_PTR(-EINVAL); | ||
185 | } | ||
186 | |||
187 | /* Find the master key, then derive the inode's actual encryption key */ | ||
188 | static int find_and_derive_key(const struct inode *inode, | ||
189 | const struct fscrypt_context *ctx, | ||
190 | u8 *derived_key, const struct fscrypt_mode *mode) | ||
191 | { | ||
192 | struct key *key; | ||
193 | const struct fscrypt_key *payload; | ||
194 | int err; | ||
195 | |||
196 | key = find_and_lock_process_key(FS_KEY_DESC_PREFIX, | ||
197 | ctx->master_key_descriptor, | ||
198 | mode->keysize, &payload); | ||
199 | if (key == ERR_PTR(-ENOKEY) && inode->i_sb->s_cop->key_prefix) { | ||
200 | key = find_and_lock_process_key(inode->i_sb->s_cop->key_prefix, | ||
201 | ctx->master_key_descriptor, | ||
202 | mode->keysize, &payload); | ||
203 | } | ||
204 | if (IS_ERR(key)) | ||
205 | return PTR_ERR(key); | ||
206 | |||
207 | if (ctx->flags & FS_POLICY_FLAG_DIRECT_KEY) { | ||
208 | if (mode->ivsize < offsetofend(union fscrypt_iv, nonce)) { | ||
209 | fscrypt_warn(inode->i_sb, | ||
210 | "direct key mode not allowed with %s", | ||
211 | mode->friendly_name); | ||
212 | err = -EINVAL; | ||
213 | } else if (ctx->contents_encryption_mode != | ||
214 | ctx->filenames_encryption_mode) { | ||
215 | fscrypt_warn(inode->i_sb, | ||
216 | "direct key mode not allowed with different contents and filenames modes"); | ||
217 | err = -EINVAL; | ||
218 | } else { | ||
219 | memcpy(derived_key, payload->raw, mode->keysize); | ||
220 | err = 0; | ||
221 | } | ||
222 | } else { | ||
223 | err = derive_key_aes(payload->raw, ctx, derived_key, | ||
224 | mode->keysize); | ||
225 | } | ||
226 | up_read(&key->sem); | ||
227 | key_put(key); | ||
228 | return err; | ||
229 | } | ||
230 | |||
231 | /* Allocate and key a symmetric cipher object for the given encryption mode */ | ||
232 | static struct crypto_skcipher * | ||
233 | allocate_skcipher_for_mode(struct fscrypt_mode *mode, const u8 *raw_key, | ||
234 | const struct inode *inode) | ||
235 | { | ||
236 | struct crypto_skcipher *tfm; | ||
237 | int err; | ||
238 | |||
239 | tfm = crypto_alloc_skcipher(mode->cipher_str, 0, 0); | ||
240 | if (IS_ERR(tfm)) { | ||
241 | fscrypt_warn(inode->i_sb, | ||
242 | "error allocating '%s' transform for inode %lu: %ld", | ||
243 | mode->cipher_str, inode->i_ino, PTR_ERR(tfm)); | ||
244 | return tfm; | ||
245 | } | ||
246 | if (unlikely(!mode->logged_impl_name)) { | ||
247 | /* | ||
248 | * fscrypt performance can vary greatly depending on which | ||
249 | * crypto algorithm implementation is used. Help people debug | ||
250 | * performance problems by logging the ->cra_driver_name the | ||
251 | * first time a mode is used. Note that multiple threads can | ||
252 | * race here, but it doesn't really matter. | ||
253 | */ | ||
254 | mode->logged_impl_name = true; | ||
255 | pr_info("fscrypt: %s using implementation \"%s\"\n", | ||
256 | mode->friendly_name, | ||
257 | crypto_skcipher_alg(tfm)->base.cra_driver_name); | ||
258 | } | ||
259 | crypto_skcipher_set_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS); | ||
260 | err = crypto_skcipher_setkey(tfm, raw_key, mode->keysize); | ||
261 | if (err) | ||
262 | goto err_free_tfm; | ||
263 | |||
264 | return tfm; | ||
265 | |||
266 | err_free_tfm: | ||
267 | crypto_free_skcipher(tfm); | ||
268 | return ERR_PTR(err); | ||
269 | } | ||
270 | |||
271 | /* Master key referenced by FS_POLICY_FLAG_DIRECT_KEY policy */ | ||
272 | struct fscrypt_master_key { | ||
273 | struct hlist_node mk_node; | ||
274 | refcount_t mk_refcount; | ||
275 | const struct fscrypt_mode *mk_mode; | ||
276 | struct crypto_skcipher *mk_ctfm; | ||
277 | u8 mk_descriptor[FS_KEY_DESCRIPTOR_SIZE]; | ||
278 | u8 mk_raw[FS_MAX_KEY_SIZE]; | ||
279 | }; | ||
280 | |||
281 | static void free_master_key(struct fscrypt_master_key *mk) | ||
282 | { | ||
283 | if (mk) { | ||
284 | crypto_free_skcipher(mk->mk_ctfm); | ||
285 | kzfree(mk); | ||
286 | } | ||
287 | } | ||
288 | |||
289 | static void put_master_key(struct fscrypt_master_key *mk) | ||
290 | { | ||
291 | if (!refcount_dec_and_lock(&mk->mk_refcount, &fscrypt_master_keys_lock)) | ||
292 | return; | ||
293 | hash_del(&mk->mk_node); | ||
294 | spin_unlock(&fscrypt_master_keys_lock); | ||
295 | |||
296 | free_master_key(mk); | ||
297 | } | ||
298 | |||
299 | /* | ||
300 | * Find/insert the given master key into the fscrypt_master_keys table. If | ||
301 | * found, it is returned with elevated refcount, and 'to_insert' is freed if | ||
302 | * non-NULL. If not found, 'to_insert' is inserted and returned if it's | ||
303 | * non-NULL; otherwise NULL is returned. | ||
304 | */ | ||
305 | static struct fscrypt_master_key * | ||
306 | find_or_insert_master_key(struct fscrypt_master_key *to_insert, | ||
307 | const u8 *raw_key, const struct fscrypt_mode *mode, | ||
308 | const struct fscrypt_info *ci) | ||
309 | { | ||
310 | unsigned long hash_key; | ||
311 | struct fscrypt_master_key *mk; | ||
312 | |||
313 | /* | ||
314 | * Careful: to avoid potentially leaking secret key bytes via timing | ||
315 | * information, we must key the hash table by descriptor rather than by | ||
316 | * raw key, and use crypto_memneq() when comparing raw keys. | ||
317 | */ | ||
318 | |||
319 | BUILD_BUG_ON(sizeof(hash_key) > FS_KEY_DESCRIPTOR_SIZE); | ||
320 | memcpy(&hash_key, ci->ci_master_key_descriptor, sizeof(hash_key)); | ||
321 | |||
322 | spin_lock(&fscrypt_master_keys_lock); | ||
323 | hash_for_each_possible(fscrypt_master_keys, mk, mk_node, hash_key) { | ||
324 | if (memcmp(ci->ci_master_key_descriptor, mk->mk_descriptor, | ||
325 | FS_KEY_DESCRIPTOR_SIZE) != 0) | ||
326 | continue; | ||
327 | if (mode != mk->mk_mode) | ||
328 | continue; | ||
329 | if (crypto_memneq(raw_key, mk->mk_raw, mode->keysize)) | ||
330 | continue; | ||
331 | /* using existing tfm with same (descriptor, mode, raw_key) */ | ||
332 | refcount_inc(&mk->mk_refcount); | ||
333 | spin_unlock(&fscrypt_master_keys_lock); | ||
334 | free_master_key(to_insert); | ||
335 | return mk; | ||
336 | } | ||
337 | if (to_insert) | ||
338 | hash_add(fscrypt_master_keys, &to_insert->mk_node, hash_key); | ||
339 | spin_unlock(&fscrypt_master_keys_lock); | ||
340 | return to_insert; | ||
341 | } | ||
342 | |||
343 | /* Prepare to encrypt directly using the master key in the given mode */ | ||
344 | static struct fscrypt_master_key * | ||
345 | fscrypt_get_master_key(const struct fscrypt_info *ci, struct fscrypt_mode *mode, | ||
346 | const u8 *raw_key, const struct inode *inode) | ||
347 | { | ||
348 | struct fscrypt_master_key *mk; | ||
349 | int err; | ||
350 | |||
351 | /* Is there already a tfm for this key? */ | ||
352 | mk = find_or_insert_master_key(NULL, raw_key, mode, ci); | ||
353 | if (mk) | ||
354 | return mk; | ||
355 | |||
356 | /* Nope, allocate one. */ | ||
357 | mk = kzalloc(sizeof(*mk), GFP_NOFS); | ||
358 | if (!mk) | ||
359 | return ERR_PTR(-ENOMEM); | ||
360 | refcount_set(&mk->mk_refcount, 1); | ||
361 | mk->mk_mode = mode; | ||
362 | mk->mk_ctfm = allocate_skcipher_for_mode(mode, raw_key, inode); | ||
363 | if (IS_ERR(mk->mk_ctfm)) { | ||
364 | err = PTR_ERR(mk->mk_ctfm); | ||
365 | mk->mk_ctfm = NULL; | ||
366 | goto err_free_mk; | ||
367 | } | ||
368 | memcpy(mk->mk_descriptor, ci->ci_master_key_descriptor, | ||
369 | FS_KEY_DESCRIPTOR_SIZE); | ||
370 | memcpy(mk->mk_raw, raw_key, mode->keysize); | ||
371 | |||
372 | return find_or_insert_master_key(mk, raw_key, mode, ci); | ||
373 | |||
374 | err_free_mk: | ||
375 | free_master_key(mk); | ||
376 | return ERR_PTR(err); | ||
377 | } | ||
378 | |||
379 | static int derive_essiv_salt(const u8 *key, int keysize, u8 *salt) | ||
380 | { | ||
381 | struct crypto_shash *tfm = READ_ONCE(essiv_hash_tfm); | ||
382 | |||
383 | /* init hash transform on demand */ | ||
384 | if (unlikely(!tfm)) { | ||
385 | struct crypto_shash *prev_tfm; | ||
386 | |||
387 | tfm = crypto_alloc_shash("sha256", 0, 0); | ||
388 | if (IS_ERR(tfm)) { | ||
389 | fscrypt_warn(NULL, | ||
390 | "error allocating SHA-256 transform: %ld", | ||
391 | PTR_ERR(tfm)); | ||
392 | return PTR_ERR(tfm); | ||
393 | } | ||
394 | prev_tfm = cmpxchg(&essiv_hash_tfm, NULL, tfm); | ||
395 | if (prev_tfm) { | ||
396 | crypto_free_shash(tfm); | ||
397 | tfm = prev_tfm; | ||
398 | } | ||
399 | } | ||
400 | |||
401 | { | ||
402 | SHASH_DESC_ON_STACK(desc, tfm); | ||
403 | desc->tfm = tfm; | ||
404 | |||
405 | return crypto_shash_digest(desc, key, keysize, salt); | ||
406 | } | ||
407 | } | ||
408 | |||
409 | static int init_essiv_generator(struct fscrypt_info *ci, const u8 *raw_key, | ||
410 | int keysize) | ||
411 | { | ||
412 | int err; | ||
413 | struct crypto_cipher *essiv_tfm; | ||
414 | u8 salt[SHA256_DIGEST_SIZE]; | ||
415 | |||
416 | essiv_tfm = crypto_alloc_cipher("aes", 0, 0); | ||
417 | if (IS_ERR(essiv_tfm)) | ||
418 | return PTR_ERR(essiv_tfm); | ||
419 | |||
420 | ci->ci_essiv_tfm = essiv_tfm; | ||
421 | |||
422 | err = derive_essiv_salt(raw_key, keysize, salt); | ||
423 | if (err) | ||
424 | goto out; | ||
425 | |||
426 | /* | ||
427 | * Using SHA256 to derive the salt/key will result in AES-256 being | ||
428 | * used for IV generation. File contents encryption will still use the | ||
429 | * configured keysize (AES-128) nevertheless. | ||
430 | */ | ||
431 | err = crypto_cipher_setkey(essiv_tfm, salt, sizeof(salt)); | ||
432 | if (err) | ||
433 | goto out; | ||
434 | |||
435 | out: | ||
436 | memzero_explicit(salt, sizeof(salt)); | ||
437 | return err; | ||
438 | } | ||
439 | |||
440 | void __exit fscrypt_essiv_cleanup(void) | ||
441 | { | ||
442 | crypto_free_shash(essiv_hash_tfm); | ||
443 | } | ||
444 | |||
445 | /* | ||
446 | * Given the encryption mode and key (normally the derived key, but for | ||
447 | * FS_POLICY_FLAG_DIRECT_KEY mode it's the master key), set up the inode's | ||
448 | * symmetric cipher transform object(s). | ||
449 | */ | ||
450 | static int setup_crypto_transform(struct fscrypt_info *ci, | ||
451 | struct fscrypt_mode *mode, | ||
452 | const u8 *raw_key, const struct inode *inode) | ||
453 | { | ||
454 | struct fscrypt_master_key *mk; | ||
455 | struct crypto_skcipher *ctfm; | ||
456 | int err; | ||
457 | |||
458 | if (ci->ci_flags & FS_POLICY_FLAG_DIRECT_KEY) { | ||
459 | mk = fscrypt_get_master_key(ci, mode, raw_key, inode); | ||
460 | if (IS_ERR(mk)) | ||
461 | return PTR_ERR(mk); | ||
462 | ctfm = mk->mk_ctfm; | ||
463 | } else { | ||
464 | mk = NULL; | ||
465 | ctfm = allocate_skcipher_for_mode(mode, raw_key, inode); | ||
466 | if (IS_ERR(ctfm)) | ||
467 | return PTR_ERR(ctfm); | ||
468 | } | ||
469 | ci->ci_master_key = mk; | ||
470 | ci->ci_ctfm = ctfm; | ||
471 | |||
472 | if (mode->needs_essiv) { | ||
473 | /* ESSIV implies 16-byte IVs which implies !DIRECT_KEY */ | ||
474 | WARN_ON(mode->ivsize != AES_BLOCK_SIZE); | ||
475 | WARN_ON(ci->ci_flags & FS_POLICY_FLAG_DIRECT_KEY); | ||
476 | |||
477 | err = init_essiv_generator(ci, raw_key, mode->keysize); | ||
478 | if (err) { | ||
479 | fscrypt_warn(inode->i_sb, | ||
480 | "error initializing ESSIV generator for inode %lu: %d", | ||
481 | inode->i_ino, err); | ||
482 | return err; | ||
483 | } | ||
484 | } | ||
485 | return 0; | ||
486 | } | ||
487 | |||
488 | static void put_crypt_info(struct fscrypt_info *ci) | ||
489 | { | ||
490 | if (!ci) | ||
491 | return; | ||
492 | |||
493 | if (ci->ci_master_key) { | ||
494 | put_master_key(ci->ci_master_key); | ||
495 | } else { | ||
496 | crypto_free_skcipher(ci->ci_ctfm); | ||
497 | crypto_free_cipher(ci->ci_essiv_tfm); | ||
498 | } | ||
499 | kmem_cache_free(fscrypt_info_cachep, ci); | ||
500 | } | ||
501 | |||
502 | int fscrypt_get_encryption_info(struct inode *inode) | ||
503 | { | ||
504 | struct fscrypt_info *crypt_info; | ||
505 | struct fscrypt_context ctx; | ||
506 | struct fscrypt_mode *mode; | ||
507 | u8 *raw_key = NULL; | ||
508 | int res; | ||
509 | |||
510 | if (fscrypt_has_encryption_key(inode)) | ||
511 | return 0; | ||
512 | |||
513 | res = fscrypt_initialize(inode->i_sb->s_cop->flags); | ||
514 | if (res) | ||
515 | return res; | ||
516 | |||
517 | res = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx)); | ||
518 | if (res < 0) { | ||
519 | if (!fscrypt_dummy_context_enabled(inode) || | ||
520 | IS_ENCRYPTED(inode)) | ||
521 | return res; | ||
522 | /* Fake up a context for an unencrypted directory */ | ||
523 | memset(&ctx, 0, sizeof(ctx)); | ||
524 | ctx.format = FS_ENCRYPTION_CONTEXT_FORMAT_V1; | ||
525 | ctx.contents_encryption_mode = FS_ENCRYPTION_MODE_AES_256_XTS; | ||
526 | ctx.filenames_encryption_mode = FS_ENCRYPTION_MODE_AES_256_CTS; | ||
527 | memset(ctx.master_key_descriptor, 0x42, FS_KEY_DESCRIPTOR_SIZE); | ||
528 | } else if (res != sizeof(ctx)) { | ||
529 | return -EINVAL; | ||
530 | } | ||
531 | |||
532 | if (ctx.format != FS_ENCRYPTION_CONTEXT_FORMAT_V1) | ||
533 | return -EINVAL; | ||
534 | |||
535 | if (ctx.flags & ~FS_POLICY_FLAGS_VALID) | ||
536 | return -EINVAL; | ||
537 | |||
538 | crypt_info = kmem_cache_zalloc(fscrypt_info_cachep, GFP_NOFS); | ||
539 | if (!crypt_info) | ||
540 | return -ENOMEM; | ||
541 | |||
542 | crypt_info->ci_flags = ctx.flags; | ||
543 | crypt_info->ci_data_mode = ctx.contents_encryption_mode; | ||
544 | crypt_info->ci_filename_mode = ctx.filenames_encryption_mode; | ||
545 | memcpy(crypt_info->ci_master_key_descriptor, ctx.master_key_descriptor, | ||
546 | FS_KEY_DESCRIPTOR_SIZE); | ||
547 | memcpy(crypt_info->ci_nonce, ctx.nonce, FS_KEY_DERIVATION_NONCE_SIZE); | ||
548 | |||
549 | mode = select_encryption_mode(crypt_info, inode); | ||
550 | if (IS_ERR(mode)) { | ||
551 | res = PTR_ERR(mode); | ||
552 | goto out; | ||
553 | } | ||
554 | WARN_ON(mode->ivsize > FSCRYPT_MAX_IV_SIZE); | ||
555 | crypt_info->ci_mode = mode; | ||
556 | |||
557 | /* | ||
558 | * This cannot be a stack buffer because it may be passed to the | ||
559 | * scatterlist crypto API as part of key derivation. | ||
560 | */ | ||
561 | res = -ENOMEM; | ||
562 | raw_key = kmalloc(mode->keysize, GFP_NOFS); | ||
563 | if (!raw_key) | ||
564 | goto out; | ||
565 | |||
566 | res = find_and_derive_key(inode, &ctx, raw_key, mode); | ||
567 | if (res) | ||
568 | goto out; | ||
569 | |||
570 | res = setup_crypto_transform(crypt_info, mode, raw_key, inode); | ||
571 | if (res) | ||
572 | goto out; | ||
573 | |||
574 | if (cmpxchg_release(&inode->i_crypt_info, NULL, crypt_info) == NULL) | ||
575 | crypt_info = NULL; | ||
576 | out: | ||
577 | if (res == -ENOKEY) | ||
578 | res = 0; | ||
579 | put_crypt_info(crypt_info); | ||
580 | kzfree(raw_key); | ||
581 | return res; | ||
582 | } | ||
583 | EXPORT_SYMBOL(fscrypt_get_encryption_info); | ||
584 | |||
585 | /** | ||
586 | * fscrypt_put_encryption_info - free most of an inode's fscrypt data | ||
587 | * | ||
588 | * Free the inode's fscrypt_info. Filesystems must call this when the inode is | ||
589 | * being evicted. An RCU grace period need not have elapsed yet. | ||
590 | */ | ||
591 | void fscrypt_put_encryption_info(struct inode *inode) | ||
592 | { | ||
593 | put_crypt_info(inode->i_crypt_info); | ||
594 | inode->i_crypt_info = NULL; | ||
595 | } | ||
596 | EXPORT_SYMBOL(fscrypt_put_encryption_info); | ||
597 | |||
598 | /** | ||
599 | * fscrypt_free_inode - free an inode's fscrypt data requiring RCU delay | ||
600 | * | ||
601 | * Free the inode's cached decrypted symlink target, if any. Filesystems must | ||
602 | * call this after an RCU grace period, just before they free the inode. | ||
603 | */ | ||
604 | void fscrypt_free_inode(struct inode *inode) | ||
605 | { | ||
606 | if (IS_ENCRYPTED(inode) && S_ISLNK(inode->i_mode)) { | ||
607 | kfree(inode->i_link); | ||
608 | inode->i_link = NULL; | ||
609 | } | ||
610 | } | ||
611 | EXPORT_SYMBOL(fscrypt_free_inode); | ||
diff --git a/fs/crypto/keyring.c b/fs/crypto/keyring.c new file mode 100644 index 000000000000..c34fa7c61b43 --- /dev/null +++ b/fs/crypto/keyring.c | |||
@@ -0,0 +1,984 @@ | |||
1 | // SPDX-License-Identifier: GPL-2.0 | ||
2 | /* | ||
3 | * Filesystem-level keyring for fscrypt | ||
4 | * | ||
5 | * Copyright 2019 Google LLC | ||
6 | */ | ||
7 | |||
8 | /* | ||
9 | * This file implements management of fscrypt master keys in the | ||
10 | * filesystem-level keyring, including the ioctls: | ||
11 | * | ||
12 | * - FS_IOC_ADD_ENCRYPTION_KEY | ||
13 | * - FS_IOC_REMOVE_ENCRYPTION_KEY | ||
14 | * - FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS | ||
15 | * - FS_IOC_GET_ENCRYPTION_KEY_STATUS | ||
16 | * | ||
17 | * See the "User API" section of Documentation/filesystems/fscrypt.rst for more | ||
18 | * information about these ioctls. | ||
19 | */ | ||
20 | |||
21 | #include <crypto/skcipher.h> | ||
22 | #include <linux/key-type.h> | ||
23 | #include <linux/seq_file.h> | ||
24 | |||
25 | #include "fscrypt_private.h" | ||
26 | |||
27 | static void wipe_master_key_secret(struct fscrypt_master_key_secret *secret) | ||
28 | { | ||
29 | fscrypt_destroy_hkdf(&secret->hkdf); | ||
30 | memzero_explicit(secret, sizeof(*secret)); | ||
31 | } | ||
32 | |||
33 | static void move_master_key_secret(struct fscrypt_master_key_secret *dst, | ||
34 | struct fscrypt_master_key_secret *src) | ||
35 | { | ||
36 | memcpy(dst, src, sizeof(*dst)); | ||
37 | memzero_explicit(src, sizeof(*src)); | ||
38 | } | ||
39 | |||
40 | static void free_master_key(struct fscrypt_master_key *mk) | ||
41 | { | ||
42 | size_t i; | ||
43 | |||
44 | wipe_master_key_secret(&mk->mk_secret); | ||
45 | |||
46 | for (i = 0; i < ARRAY_SIZE(mk->mk_mode_keys); i++) | ||
47 | crypto_free_skcipher(mk->mk_mode_keys[i]); | ||
48 | |||
49 | key_put(mk->mk_users); | ||
50 | kzfree(mk); | ||
51 | } | ||
52 | |||
53 | static inline bool valid_key_spec(const struct fscrypt_key_specifier *spec) | ||
54 | { | ||
55 | if (spec->__reserved) | ||
56 | return false; | ||
57 | return master_key_spec_len(spec) != 0; | ||
58 | } | ||
59 | |||
60 | static int fscrypt_key_instantiate(struct key *key, | ||
61 | struct key_preparsed_payload *prep) | ||
62 | { | ||
63 | key->payload.data[0] = (struct fscrypt_master_key *)prep->data; | ||
64 | return 0; | ||
65 | } | ||
66 | |||
67 | static void fscrypt_key_destroy(struct key *key) | ||
68 | { | ||
69 | free_master_key(key->payload.data[0]); | ||
70 | } | ||
71 | |||
72 | static void fscrypt_key_describe(const struct key *key, struct seq_file *m) | ||
73 | { | ||
74 | seq_puts(m, key->description); | ||
75 | |||
76 | if (key_is_positive(key)) { | ||
77 | const struct fscrypt_master_key *mk = key->payload.data[0]; | ||
78 | |||
79 | if (!is_master_key_secret_present(&mk->mk_secret)) | ||
80 | seq_puts(m, ": secret removed"); | ||
81 | } | ||
82 | } | ||
83 | |||
84 | /* | ||
85 | * Type of key in ->s_master_keys. Each key of this type represents a master | ||
86 | * key which has been added to the filesystem. Its payload is a | ||
87 | * 'struct fscrypt_master_key'. The "." prefix in the key type name prevents | ||
88 | * users from adding keys of this type via the keyrings syscalls rather than via | ||
89 | * the intended method of FS_IOC_ADD_ENCRYPTION_KEY. | ||
90 | */ | ||
91 | static struct key_type key_type_fscrypt = { | ||
92 | .name = "._fscrypt", | ||
93 | .instantiate = fscrypt_key_instantiate, | ||
94 | .destroy = fscrypt_key_destroy, | ||
95 | .describe = fscrypt_key_describe, | ||
96 | }; | ||
97 | |||
98 | static int fscrypt_user_key_instantiate(struct key *key, | ||
99 | struct key_preparsed_payload *prep) | ||
100 | { | ||
101 | /* | ||
102 | * We just charge FSCRYPT_MAX_KEY_SIZE bytes to the user's key quota for | ||
103 | * each key, regardless of the exact key size. The amount of memory | ||
104 | * actually used is greater than the size of the raw key anyway. | ||
105 | */ | ||
106 | return key_payload_reserve(key, FSCRYPT_MAX_KEY_SIZE); | ||
107 | } | ||
108 | |||
109 | static void fscrypt_user_key_describe(const struct key *key, struct seq_file *m) | ||
110 | { | ||
111 | seq_puts(m, key->description); | ||
112 | } | ||
113 | |||
114 | /* | ||
115 | * Type of key in ->mk_users. Each key of this type represents a particular | ||
116 | * user who has added a particular master key. | ||
117 | * | ||
118 | * Note that the name of this key type really should be something like | ||
119 | * ".fscrypt-user" instead of simply ".fscrypt". But the shorter name is chosen | ||
120 | * mainly for simplicity of presentation in /proc/keys when read by a non-root | ||
121 | * user. And it is expected to be rare that a key is actually added by multiple | ||
122 | * users, since users should keep their encryption keys confidential. | ||
123 | */ | ||
124 | static struct key_type key_type_fscrypt_user = { | ||
125 | .name = ".fscrypt", | ||
126 | .instantiate = fscrypt_user_key_instantiate, | ||
127 | .describe = fscrypt_user_key_describe, | ||
128 | }; | ||
129 | |||
130 | /* Search ->s_master_keys or ->mk_users */ | ||
131 | static struct key *search_fscrypt_keyring(struct key *keyring, | ||
132 | struct key_type *type, | ||
133 | const char *description) | ||
134 | { | ||
135 | /* | ||
136 | * We need to mark the keyring reference as "possessed" so that we | ||
137 | * acquire permission to search it, via the KEY_POS_SEARCH permission. | ||
138 | */ | ||
139 | key_ref_t keyref = make_key_ref(keyring, true /* possessed */); | ||
140 | |||
141 | keyref = keyring_search(keyref, type, description, false); | ||
142 | if (IS_ERR(keyref)) { | ||
143 | if (PTR_ERR(keyref) == -EAGAIN || /* not found */ | ||
144 | PTR_ERR(keyref) == -EKEYREVOKED) /* recently invalidated */ | ||
145 | keyref = ERR_PTR(-ENOKEY); | ||
146 | return ERR_CAST(keyref); | ||
147 | } | ||
148 | return key_ref_to_ptr(keyref); | ||
149 | } | ||
150 | |||
151 | #define FSCRYPT_FS_KEYRING_DESCRIPTION_SIZE \ | ||
152 | (CONST_STRLEN("fscrypt-") + FIELD_SIZEOF(struct super_block, s_id)) | ||
153 | |||
154 | #define FSCRYPT_MK_DESCRIPTION_SIZE (2 * FSCRYPT_KEY_IDENTIFIER_SIZE + 1) | ||
155 | |||
156 | #define FSCRYPT_MK_USERS_DESCRIPTION_SIZE \ | ||
157 | (CONST_STRLEN("fscrypt-") + 2 * FSCRYPT_KEY_IDENTIFIER_SIZE + \ | ||
158 | CONST_STRLEN("-users") + 1) | ||
159 | |||
160 | #define FSCRYPT_MK_USER_DESCRIPTION_SIZE \ | ||
161 | (2 * FSCRYPT_KEY_IDENTIFIER_SIZE + CONST_STRLEN(".uid.") + 10 + 1) | ||
162 | |||
163 | static void format_fs_keyring_description( | ||
164 | char description[FSCRYPT_FS_KEYRING_DESCRIPTION_SIZE], | ||
165 | const struct super_block *sb) | ||
166 | { | ||
167 | sprintf(description, "fscrypt-%s", sb->s_id); | ||
168 | } | ||
169 | |||
170 | static void format_mk_description( | ||
171 | char description[FSCRYPT_MK_DESCRIPTION_SIZE], | ||
172 | const struct fscrypt_key_specifier *mk_spec) | ||
173 | { | ||
174 | sprintf(description, "%*phN", | ||
175 | master_key_spec_len(mk_spec), (u8 *)&mk_spec->u); | ||
176 | } | ||
177 | |||
178 | static void format_mk_users_keyring_description( | ||
179 | char description[FSCRYPT_MK_USERS_DESCRIPTION_SIZE], | ||
180 | const u8 mk_identifier[FSCRYPT_KEY_IDENTIFIER_SIZE]) | ||
181 | { | ||
182 | sprintf(description, "fscrypt-%*phN-users", | ||
183 | FSCRYPT_KEY_IDENTIFIER_SIZE, mk_identifier); | ||
184 | } | ||
185 | |||
186 | static void format_mk_user_description( | ||
187 | char description[FSCRYPT_MK_USER_DESCRIPTION_SIZE], | ||
188 | const u8 mk_identifier[FSCRYPT_KEY_IDENTIFIER_SIZE]) | ||
189 | { | ||
190 | |||
191 | sprintf(description, "%*phN.uid.%u", FSCRYPT_KEY_IDENTIFIER_SIZE, | ||
192 | mk_identifier, __kuid_val(current_fsuid())); | ||
193 | } | ||
194 | |||
195 | /* Create ->s_master_keys if needed. Synchronized by fscrypt_add_key_mutex. */ | ||
196 | static int allocate_filesystem_keyring(struct super_block *sb) | ||
197 | { | ||
198 | char description[FSCRYPT_FS_KEYRING_DESCRIPTION_SIZE]; | ||
199 | struct key *keyring; | ||
200 | |||
201 | if (sb->s_master_keys) | ||
202 | return 0; | ||
203 | |||
204 | format_fs_keyring_description(description, sb); | ||
205 | keyring = keyring_alloc(description, GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, | ||
206 | current_cred(), KEY_POS_SEARCH | | ||
207 | KEY_USR_SEARCH | KEY_USR_READ | KEY_USR_VIEW, | ||
208 | KEY_ALLOC_NOT_IN_QUOTA, NULL, NULL); | ||
209 | if (IS_ERR(keyring)) | ||
210 | return PTR_ERR(keyring); | ||
211 | |||
212 | /* Pairs with READ_ONCE() in fscrypt_find_master_key() */ | ||
213 | smp_store_release(&sb->s_master_keys, keyring); | ||
214 | return 0; | ||
215 | } | ||
216 | |||
217 | void fscrypt_sb_free(struct super_block *sb) | ||
218 | { | ||
219 | key_put(sb->s_master_keys); | ||
220 | sb->s_master_keys = NULL; | ||
221 | } | ||
222 | |||
223 | /* | ||
224 | * Find the specified master key in ->s_master_keys. | ||
225 | * Returns ERR_PTR(-ENOKEY) if not found. | ||
226 | */ | ||
227 | struct key *fscrypt_find_master_key(struct super_block *sb, | ||
228 | const struct fscrypt_key_specifier *mk_spec) | ||
229 | { | ||
230 | struct key *keyring; | ||
231 | char description[FSCRYPT_MK_DESCRIPTION_SIZE]; | ||
232 | |||
233 | /* pairs with smp_store_release() in allocate_filesystem_keyring() */ | ||
234 | keyring = READ_ONCE(sb->s_master_keys); | ||
235 | if (keyring == NULL) | ||
236 | return ERR_PTR(-ENOKEY); /* No keyring yet, so no keys yet. */ | ||
237 | |||
238 | format_mk_description(description, mk_spec); | ||
239 | return search_fscrypt_keyring(keyring, &key_type_fscrypt, description); | ||
240 | } | ||
241 | |||
242 | static int allocate_master_key_users_keyring(struct fscrypt_master_key *mk) | ||
243 | { | ||
244 | char description[FSCRYPT_MK_USERS_DESCRIPTION_SIZE]; | ||
245 | struct key *keyring; | ||
246 | |||
247 | format_mk_users_keyring_description(description, | ||
248 | mk->mk_spec.u.identifier); | ||
249 | keyring = keyring_alloc(description, GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, | ||
250 | current_cred(), KEY_POS_SEARCH | | ||
251 | KEY_USR_SEARCH | KEY_USR_READ | KEY_USR_VIEW, | ||
252 | KEY_ALLOC_NOT_IN_QUOTA, NULL, NULL); | ||
253 | if (IS_ERR(keyring)) | ||
254 | return PTR_ERR(keyring); | ||
255 | |||
256 | mk->mk_users = keyring; | ||
257 | return 0; | ||
258 | } | ||
259 | |||
260 | /* | ||
261 | * Find the current user's "key" in the master key's ->mk_users. | ||
262 | * Returns ERR_PTR(-ENOKEY) if not found. | ||
263 | */ | ||
264 | static struct key *find_master_key_user(struct fscrypt_master_key *mk) | ||
265 | { | ||
266 | char description[FSCRYPT_MK_USER_DESCRIPTION_SIZE]; | ||
267 | |||
268 | format_mk_user_description(description, mk->mk_spec.u.identifier); | ||
269 | return search_fscrypt_keyring(mk->mk_users, &key_type_fscrypt_user, | ||
270 | description); | ||
271 | } | ||
272 | |||
273 | /* | ||
274 | * Give the current user a "key" in ->mk_users. This charges the user's quota | ||
275 | * and marks the master key as added by the current user, so that it cannot be | ||
276 | * removed by another user with the key. Either the master key's key->sem must | ||
277 | * be held for write, or the master key must be still undergoing initialization. | ||
278 | */ | ||
279 | static int add_master_key_user(struct fscrypt_master_key *mk) | ||
280 | { | ||
281 | char description[FSCRYPT_MK_USER_DESCRIPTION_SIZE]; | ||
282 | struct key *mk_user; | ||
283 | int err; | ||
284 | |||
285 | format_mk_user_description(description, mk->mk_spec.u.identifier); | ||
286 | mk_user = key_alloc(&key_type_fscrypt_user, description, | ||
287 | current_fsuid(), current_gid(), current_cred(), | ||
288 | KEY_POS_SEARCH | KEY_USR_VIEW, 0, NULL); | ||
289 | if (IS_ERR(mk_user)) | ||
290 | return PTR_ERR(mk_user); | ||
291 | |||
292 | err = key_instantiate_and_link(mk_user, NULL, 0, mk->mk_users, NULL); | ||
293 | key_put(mk_user); | ||
294 | return err; | ||
295 | } | ||
296 | |||
297 | /* | ||
298 | * Remove the current user's "key" from ->mk_users. | ||
299 | * The master key's key->sem must be held for write. | ||
300 | * | ||
301 | * Returns 0 if removed, -ENOKEY if not found, or another -errno code. | ||
302 | */ | ||
303 | static int remove_master_key_user(struct fscrypt_master_key *mk) | ||
304 | { | ||
305 | struct key *mk_user; | ||
306 | int err; | ||
307 | |||
308 | mk_user = find_master_key_user(mk); | ||
309 | if (IS_ERR(mk_user)) | ||
310 | return PTR_ERR(mk_user); | ||
311 | err = key_unlink(mk->mk_users, mk_user); | ||
312 | key_put(mk_user); | ||
313 | return err; | ||
314 | } | ||
315 | |||
316 | /* | ||
317 | * Allocate a new fscrypt_master_key which contains the given secret, set it as | ||
318 | * the payload of a new 'struct key' of type fscrypt, and link the 'struct key' | ||
319 | * into the given keyring. Synchronized by fscrypt_add_key_mutex. | ||
320 | */ | ||
321 | static int add_new_master_key(struct fscrypt_master_key_secret *secret, | ||
322 | const struct fscrypt_key_specifier *mk_spec, | ||
323 | struct key *keyring) | ||
324 | { | ||
325 | struct fscrypt_master_key *mk; | ||
326 | char description[FSCRYPT_MK_DESCRIPTION_SIZE]; | ||
327 | struct key *key; | ||
328 | int err; | ||
329 | |||
330 | mk = kzalloc(sizeof(*mk), GFP_KERNEL); | ||
331 | if (!mk) | ||
332 | return -ENOMEM; | ||
333 | |||
334 | mk->mk_spec = *mk_spec; | ||
335 | |||
336 | move_master_key_secret(&mk->mk_secret, secret); | ||
337 | init_rwsem(&mk->mk_secret_sem); | ||
338 | |||
339 | refcount_set(&mk->mk_refcount, 1); /* secret is present */ | ||
340 | INIT_LIST_HEAD(&mk->mk_decrypted_inodes); | ||
341 | spin_lock_init(&mk->mk_decrypted_inodes_lock); | ||
342 | |||
343 | if (mk_spec->type == FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER) { | ||
344 | err = allocate_master_key_users_keyring(mk); | ||
345 | if (err) | ||
346 | goto out_free_mk; | ||
347 | err = add_master_key_user(mk); | ||
348 | if (err) | ||
349 | goto out_free_mk; | ||
350 | } | ||
351 | |||
352 | /* | ||
353 | * Note that we don't charge this key to anyone's quota, since when | ||
354 | * ->mk_users is in use those keys are charged instead, and otherwise | ||
355 | * (when ->mk_users isn't in use) only root can add these keys. | ||
356 | */ | ||
357 | format_mk_description(description, mk_spec); | ||
358 | key = key_alloc(&key_type_fscrypt, description, | ||
359 | GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, current_cred(), | ||
360 | KEY_POS_SEARCH | KEY_USR_SEARCH | KEY_USR_VIEW, | ||
361 | KEY_ALLOC_NOT_IN_QUOTA, NULL); | ||
362 | if (IS_ERR(key)) { | ||
363 | err = PTR_ERR(key); | ||
364 | goto out_free_mk; | ||
365 | } | ||
366 | err = key_instantiate_and_link(key, mk, sizeof(*mk), keyring, NULL); | ||
367 | key_put(key); | ||
368 | if (err) | ||
369 | goto out_free_mk; | ||
370 | |||
371 | return 0; | ||
372 | |||
373 | out_free_mk: | ||
374 | free_master_key(mk); | ||
375 | return err; | ||
376 | } | ||
377 | |||
378 | #define KEY_DEAD 1 | ||
379 | |||
380 | static int add_existing_master_key(struct fscrypt_master_key *mk, | ||
381 | struct fscrypt_master_key_secret *secret) | ||
382 | { | ||
383 | struct key *mk_user; | ||
384 | bool rekey; | ||
385 | int err; | ||
386 | |||
387 | /* | ||
388 | * If the current user is already in ->mk_users, then there's nothing to | ||
389 | * do. (Not applicable for v1 policy keys, which have NULL ->mk_users.) | ||
390 | */ | ||
391 | if (mk->mk_users) { | ||
392 | mk_user = find_master_key_user(mk); | ||
393 | if (mk_user != ERR_PTR(-ENOKEY)) { | ||
394 | if (IS_ERR(mk_user)) | ||
395 | return PTR_ERR(mk_user); | ||
396 | key_put(mk_user); | ||
397 | return 0; | ||
398 | } | ||
399 | } | ||
400 | |||
401 | /* If we'll be re-adding ->mk_secret, try to take the reference. */ | ||
402 | rekey = !is_master_key_secret_present(&mk->mk_secret); | ||
403 | if (rekey && !refcount_inc_not_zero(&mk->mk_refcount)) | ||
404 | return KEY_DEAD; | ||
405 | |||
406 | /* Add the current user to ->mk_users, if applicable. */ | ||
407 | if (mk->mk_users) { | ||
408 | err = add_master_key_user(mk); | ||
409 | if (err) { | ||
410 | if (rekey && refcount_dec_and_test(&mk->mk_refcount)) | ||
411 | return KEY_DEAD; | ||
412 | return err; | ||
413 | } | ||
414 | } | ||
415 | |||
416 | /* Re-add the secret if needed. */ | ||
417 | if (rekey) { | ||
418 | down_write(&mk->mk_secret_sem); | ||
419 | move_master_key_secret(&mk->mk_secret, secret); | ||
420 | up_write(&mk->mk_secret_sem); | ||
421 | } | ||
422 | return 0; | ||
423 | } | ||
424 | |||
425 | static int add_master_key(struct super_block *sb, | ||
426 | struct fscrypt_master_key_secret *secret, | ||
427 | const struct fscrypt_key_specifier *mk_spec) | ||
428 | { | ||
429 | static DEFINE_MUTEX(fscrypt_add_key_mutex); | ||
430 | struct key *key; | ||
431 | int err; | ||
432 | |||
433 | mutex_lock(&fscrypt_add_key_mutex); /* serialize find + link */ | ||
434 | retry: | ||
435 | key = fscrypt_find_master_key(sb, mk_spec); | ||
436 | if (IS_ERR(key)) { | ||
437 | err = PTR_ERR(key); | ||
438 | if (err != -ENOKEY) | ||
439 | goto out_unlock; | ||
440 | /* Didn't find the key in ->s_master_keys. Add it. */ | ||
441 | err = allocate_filesystem_keyring(sb); | ||
442 | if (err) | ||
443 | goto out_unlock; | ||
444 | err = add_new_master_key(secret, mk_spec, sb->s_master_keys); | ||
445 | } else { | ||
446 | /* | ||
447 | * Found the key in ->s_master_keys. Re-add the secret if | ||
448 | * needed, and add the user to ->mk_users if needed. | ||
449 | */ | ||
450 | down_write(&key->sem); | ||
451 | err = add_existing_master_key(key->payload.data[0], secret); | ||
452 | up_write(&key->sem); | ||
453 | if (err == KEY_DEAD) { | ||
454 | /* Key being removed or needs to be removed */ | ||
455 | key_invalidate(key); | ||
456 | key_put(key); | ||
457 | goto retry; | ||
458 | } | ||
459 | key_put(key); | ||
460 | } | ||
461 | out_unlock: | ||
462 | mutex_unlock(&fscrypt_add_key_mutex); | ||
463 | return err; | ||
464 | } | ||
465 | |||
466 | /* | ||
467 | * Add a master encryption key to the filesystem, causing all files which were | ||
468 | * encrypted with it to appear "unlocked" (decrypted) when accessed. | ||
469 | * | ||
470 | * When adding a key for use by v1 encryption policies, this ioctl is | ||
471 | * privileged, and userspace must provide the 'key_descriptor'. | ||
472 | * | ||
473 | * When adding a key for use by v2+ encryption policies, this ioctl is | ||
474 | * unprivileged. This is needed, in general, to allow non-root users to use | ||
475 | * encryption without encountering the visibility problems of process-subscribed | ||
476 | * keyrings and the inability to properly remove keys. This works by having | ||
477 | * each key identified by its cryptographically secure hash --- the | ||
478 | * 'key_identifier'. The cryptographic hash ensures that a malicious user | ||
479 | * cannot add the wrong key for a given identifier. Furthermore, each added key | ||
480 | * is charged to the appropriate user's quota for the keyrings service, which | ||
481 | * prevents a malicious user from adding too many keys. Finally, we forbid a | ||
482 | * user from removing a key while other users have added it too, which prevents | ||
483 | * a user who knows another user's key from causing a denial-of-service by | ||
484 | * removing it at an inopportune time. (We tolerate that a user who knows a key | ||
485 | * can prevent other users from removing it.) | ||
486 | * | ||
487 | * For more details, see the "FS_IOC_ADD_ENCRYPTION_KEY" section of | ||
488 | * Documentation/filesystems/fscrypt.rst. | ||
489 | */ | ||
490 | int fscrypt_ioctl_add_key(struct file *filp, void __user *_uarg) | ||
491 | { | ||
492 | struct super_block *sb = file_inode(filp)->i_sb; | ||
493 | struct fscrypt_add_key_arg __user *uarg = _uarg; | ||
494 | struct fscrypt_add_key_arg arg; | ||
495 | struct fscrypt_master_key_secret secret; | ||
496 | int err; | ||
497 | |||
498 | if (copy_from_user(&arg, uarg, sizeof(arg))) | ||
499 | return -EFAULT; | ||
500 | |||
501 | if (!valid_key_spec(&arg.key_spec)) | ||
502 | return -EINVAL; | ||
503 | |||
504 | if (arg.raw_size < FSCRYPT_MIN_KEY_SIZE || | ||
505 | arg.raw_size > FSCRYPT_MAX_KEY_SIZE) | ||
506 | return -EINVAL; | ||
507 | |||
508 | if (memchr_inv(arg.__reserved, 0, sizeof(arg.__reserved))) | ||
509 | return -EINVAL; | ||
510 | |||
511 | memset(&secret, 0, sizeof(secret)); | ||
512 | secret.size = arg.raw_size; | ||
513 | err = -EFAULT; | ||
514 | if (copy_from_user(secret.raw, uarg->raw, secret.size)) | ||
515 | goto out_wipe_secret; | ||
516 | |||
517 | switch (arg.key_spec.type) { | ||
518 | case FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR: | ||
519 | /* | ||
520 | * Only root can add keys that are identified by an arbitrary | ||
521 | * descriptor rather than by a cryptographic hash --- since | ||
522 | * otherwise a malicious user could add the wrong key. | ||
523 | */ | ||
524 | err = -EACCES; | ||
525 | if (!capable(CAP_SYS_ADMIN)) | ||
526 | goto out_wipe_secret; | ||
527 | break; | ||
528 | case FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER: | ||
529 | err = fscrypt_init_hkdf(&secret.hkdf, secret.raw, secret.size); | ||
530 | if (err) | ||
531 | goto out_wipe_secret; | ||
532 | |||
533 | /* | ||
534 | * Now that the HKDF context is initialized, the raw key is no | ||
535 | * longer needed. | ||
536 | */ | ||
537 | memzero_explicit(secret.raw, secret.size); | ||
538 | |||
539 | /* Calculate the key identifier and return it to userspace. */ | ||
540 | err = fscrypt_hkdf_expand(&secret.hkdf, | ||
541 | HKDF_CONTEXT_KEY_IDENTIFIER, | ||
542 | NULL, 0, arg.key_spec.u.identifier, | ||
543 | FSCRYPT_KEY_IDENTIFIER_SIZE); | ||
544 | if (err) | ||
545 | goto out_wipe_secret; | ||
546 | err = -EFAULT; | ||
547 | if (copy_to_user(uarg->key_spec.u.identifier, | ||
548 | arg.key_spec.u.identifier, | ||
549 | FSCRYPT_KEY_IDENTIFIER_SIZE)) | ||
550 | goto out_wipe_secret; | ||
551 | break; | ||
552 | default: | ||
553 | WARN_ON(1); | ||
554 | err = -EINVAL; | ||
555 | goto out_wipe_secret; | ||
556 | } | ||
557 | |||
558 | err = add_master_key(sb, &secret, &arg.key_spec); | ||
559 | out_wipe_secret: | ||
560 | wipe_master_key_secret(&secret); | ||
561 | return err; | ||
562 | } | ||
563 | EXPORT_SYMBOL_GPL(fscrypt_ioctl_add_key); | ||
564 | |||
565 | /* | ||
566 | * Verify that the current user has added a master key with the given identifier | ||
567 | * (returns -ENOKEY if not). This is needed to prevent a user from encrypting | ||
568 | * their files using some other user's key which they don't actually know. | ||
569 | * Cryptographically this isn't much of a problem, but the semantics of this | ||
570 | * would be a bit weird, so it's best to just forbid it. | ||
571 | * | ||
572 | * The system administrator (CAP_FOWNER) can override this, which should be | ||
573 | * enough for any use cases where encryption policies are being set using keys | ||
574 | * that were chosen ahead of time but aren't available at the moment. | ||
575 | * | ||
576 | * Note that the key may have already removed by the time this returns, but | ||
577 | * that's okay; we just care whether the key was there at some point. | ||
578 | * | ||
579 | * Return: 0 if the key is added, -ENOKEY if it isn't, or another -errno code | ||
580 | */ | ||
581 | int fscrypt_verify_key_added(struct super_block *sb, | ||
582 | const u8 identifier[FSCRYPT_KEY_IDENTIFIER_SIZE]) | ||
583 | { | ||
584 | struct fscrypt_key_specifier mk_spec; | ||
585 | struct key *key, *mk_user; | ||
586 | struct fscrypt_master_key *mk; | ||
587 | int err; | ||
588 | |||
589 | mk_spec.type = FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER; | ||
590 | memcpy(mk_spec.u.identifier, identifier, FSCRYPT_KEY_IDENTIFIER_SIZE); | ||
591 | |||
592 | key = fscrypt_find_master_key(sb, &mk_spec); | ||
593 | if (IS_ERR(key)) { | ||
594 | err = PTR_ERR(key); | ||
595 | goto out; | ||
596 | } | ||
597 | mk = key->payload.data[0]; | ||
598 | mk_user = find_master_key_user(mk); | ||
599 | if (IS_ERR(mk_user)) { | ||
600 | err = PTR_ERR(mk_user); | ||
601 | } else { | ||
602 | key_put(mk_user); | ||
603 | err = 0; | ||
604 | } | ||
605 | key_put(key); | ||
606 | out: | ||
607 | if (err == -ENOKEY && capable(CAP_FOWNER)) | ||
608 | err = 0; | ||
609 | return err; | ||
610 | } | ||
611 | |||
612 | /* | ||
613 | * Try to evict the inode's dentries from the dentry cache. If the inode is a | ||
614 | * directory, then it can have at most one dentry; however, that dentry may be | ||
615 | * pinned by child dentries, so first try to evict the children too. | ||
616 | */ | ||
617 | static void shrink_dcache_inode(struct inode *inode) | ||
618 | { | ||
619 | struct dentry *dentry; | ||
620 | |||
621 | if (S_ISDIR(inode->i_mode)) { | ||
622 | dentry = d_find_any_alias(inode); | ||
623 | if (dentry) { | ||
624 | shrink_dcache_parent(dentry); | ||
625 | dput(dentry); | ||
626 | } | ||
627 | } | ||
628 | d_prune_aliases(inode); | ||
629 | } | ||
630 | |||
631 | static void evict_dentries_for_decrypted_inodes(struct fscrypt_master_key *mk) | ||
632 | { | ||
633 | struct fscrypt_info *ci; | ||
634 | struct inode *inode; | ||
635 | struct inode *toput_inode = NULL; | ||
636 | |||
637 | spin_lock(&mk->mk_decrypted_inodes_lock); | ||
638 | |||
639 | list_for_each_entry(ci, &mk->mk_decrypted_inodes, ci_master_key_link) { | ||
640 | inode = ci->ci_inode; | ||
641 | spin_lock(&inode->i_lock); | ||
642 | if (inode->i_state & (I_FREEING | I_WILL_FREE | I_NEW)) { | ||
643 | spin_unlock(&inode->i_lock); | ||
644 | continue; | ||
645 | } | ||
646 | __iget(inode); | ||
647 | spin_unlock(&inode->i_lock); | ||
648 | spin_unlock(&mk->mk_decrypted_inodes_lock); | ||
649 | |||
650 | shrink_dcache_inode(inode); | ||
651 | iput(toput_inode); | ||
652 | toput_inode = inode; | ||
653 | |||
654 | spin_lock(&mk->mk_decrypted_inodes_lock); | ||
655 | } | ||
656 | |||
657 | spin_unlock(&mk->mk_decrypted_inodes_lock); | ||
658 | iput(toput_inode); | ||
659 | } | ||
660 | |||
661 | static int check_for_busy_inodes(struct super_block *sb, | ||
662 | struct fscrypt_master_key *mk) | ||
663 | { | ||
664 | struct list_head *pos; | ||
665 | size_t busy_count = 0; | ||
666 | unsigned long ino; | ||
667 | struct dentry *dentry; | ||
668 | char _path[256]; | ||
669 | char *path = NULL; | ||
670 | |||
671 | spin_lock(&mk->mk_decrypted_inodes_lock); | ||
672 | |||
673 | list_for_each(pos, &mk->mk_decrypted_inodes) | ||
674 | busy_count++; | ||
675 | |||
676 | if (busy_count == 0) { | ||
677 | spin_unlock(&mk->mk_decrypted_inodes_lock); | ||
678 | return 0; | ||
679 | } | ||
680 | |||
681 | { | ||
682 | /* select an example file to show for debugging purposes */ | ||
683 | struct inode *inode = | ||
684 | list_first_entry(&mk->mk_decrypted_inodes, | ||
685 | struct fscrypt_info, | ||
686 | ci_master_key_link)->ci_inode; | ||
687 | ino = inode->i_ino; | ||
688 | dentry = d_find_alias(inode); | ||
689 | } | ||
690 | spin_unlock(&mk->mk_decrypted_inodes_lock); | ||
691 | |||
692 | if (dentry) { | ||
693 | path = dentry_path(dentry, _path, sizeof(_path)); | ||
694 | dput(dentry); | ||
695 | } | ||
696 | if (IS_ERR_OR_NULL(path)) | ||
697 | path = "(unknown)"; | ||
698 | |||
699 | fscrypt_warn(NULL, | ||
700 | "%s: %zu inode(s) still busy after removing key with %s %*phN, including ino %lu (%s)", | ||
701 | sb->s_id, busy_count, master_key_spec_type(&mk->mk_spec), | ||
702 | master_key_spec_len(&mk->mk_spec), (u8 *)&mk->mk_spec.u, | ||
703 | ino, path); | ||
704 | return -EBUSY; | ||
705 | } | ||
706 | |||
707 | static int try_to_lock_encrypted_files(struct super_block *sb, | ||
708 | struct fscrypt_master_key *mk) | ||
709 | { | ||
710 | int err1; | ||
711 | int err2; | ||
712 | |||
713 | /* | ||
714 | * An inode can't be evicted while it is dirty or has dirty pages. | ||
715 | * Thus, we first have to clean the inodes in ->mk_decrypted_inodes. | ||
716 | * | ||
717 | * Just do it the easy way: call sync_filesystem(). It's overkill, but | ||
718 | * it works, and it's more important to minimize the amount of caches we | ||
719 | * drop than the amount of data we sync. Also, unprivileged users can | ||
720 | * already call sync_filesystem() via sys_syncfs() or sys_sync(). | ||
721 | */ | ||
722 | down_read(&sb->s_umount); | ||
723 | err1 = sync_filesystem(sb); | ||
724 | up_read(&sb->s_umount); | ||
725 | /* If a sync error occurs, still try to evict as much as possible. */ | ||
726 | |||
727 | /* | ||
728 | * Inodes are pinned by their dentries, so we have to evict their | ||
729 | * dentries. shrink_dcache_sb() would suffice, but would be overkill | ||
730 | * and inappropriate for use by unprivileged users. So instead go | ||
731 | * through the inodes' alias lists and try to evict each dentry. | ||
732 | */ | ||
733 | evict_dentries_for_decrypted_inodes(mk); | ||
734 | |||
735 | /* | ||
736 | * evict_dentries_for_decrypted_inodes() already iput() each inode in | ||
737 | * the list; any inodes for which that dropped the last reference will | ||
738 | * have been evicted due to fscrypt_drop_inode() detecting the key | ||
739 | * removal and telling the VFS to evict the inode. So to finish, we | ||
740 | * just need to check whether any inodes couldn't be evicted. | ||
741 | */ | ||
742 | err2 = check_for_busy_inodes(sb, mk); | ||
743 | |||
744 | return err1 ?: err2; | ||
745 | } | ||
746 | |||
747 | /* | ||
748 | * Try to remove an fscrypt master encryption key. | ||
749 | * | ||
750 | * FS_IOC_REMOVE_ENCRYPTION_KEY (all_users=false) removes the current user's | ||
751 | * claim to the key, then removes the key itself if no other users have claims. | ||
752 | * FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS (all_users=true) always removes the | ||
753 | * key itself. | ||
754 | * | ||
755 | * To "remove the key itself", first we wipe the actual master key secret, so | ||
756 | * that no more inodes can be unlocked with it. Then we try to evict all cached | ||
757 | * inodes that had been unlocked with the key. | ||
758 | * | ||
759 | * If all inodes were evicted, then we unlink the fscrypt_master_key from the | ||
760 | * keyring. Otherwise it remains in the keyring in the "incompletely removed" | ||
761 | * state (without the actual secret key) where it tracks the list of remaining | ||
762 | * inodes. Userspace can execute the ioctl again later to retry eviction, or | ||
763 | * alternatively can re-add the secret key again. | ||
764 | * | ||
765 | * For more details, see the "Removing keys" section of | ||
766 | * Documentation/filesystems/fscrypt.rst. | ||
767 | */ | ||
768 | static int do_remove_key(struct file *filp, void __user *_uarg, bool all_users) | ||
769 | { | ||
770 | struct super_block *sb = file_inode(filp)->i_sb; | ||
771 | struct fscrypt_remove_key_arg __user *uarg = _uarg; | ||
772 | struct fscrypt_remove_key_arg arg; | ||
773 | struct key *key; | ||
774 | struct fscrypt_master_key *mk; | ||
775 | u32 status_flags = 0; | ||
776 | int err; | ||
777 | bool dead; | ||
778 | |||
779 | if (copy_from_user(&arg, uarg, sizeof(arg))) | ||
780 | return -EFAULT; | ||
781 | |||
782 | if (!valid_key_spec(&arg.key_spec)) | ||
783 | return -EINVAL; | ||
784 | |||
785 | if (memchr_inv(arg.__reserved, 0, sizeof(arg.__reserved))) | ||
786 | return -EINVAL; | ||
787 | |||
788 | /* | ||
789 | * Only root can add and remove keys that are identified by an arbitrary | ||
790 | * descriptor rather than by a cryptographic hash. | ||
791 | */ | ||
792 | if (arg.key_spec.type == FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR && | ||
793 | !capable(CAP_SYS_ADMIN)) | ||
794 | return -EACCES; | ||
795 | |||
796 | /* Find the key being removed. */ | ||
797 | key = fscrypt_find_master_key(sb, &arg.key_spec); | ||
798 | if (IS_ERR(key)) | ||
799 | return PTR_ERR(key); | ||
800 | mk = key->payload.data[0]; | ||
801 | |||
802 | down_write(&key->sem); | ||
803 | |||
804 | /* If relevant, remove current user's (or all users) claim to the key */ | ||
805 | if (mk->mk_users && mk->mk_users->keys.nr_leaves_on_tree != 0) { | ||
806 | if (all_users) | ||
807 | err = keyring_clear(mk->mk_users); | ||
808 | else | ||
809 | err = remove_master_key_user(mk); | ||
810 | if (err) { | ||
811 | up_write(&key->sem); | ||
812 | goto out_put_key; | ||
813 | } | ||
814 | if (mk->mk_users->keys.nr_leaves_on_tree != 0) { | ||
815 | /* | ||
816 | * Other users have still added the key too. We removed | ||
817 | * the current user's claim to the key, but we still | ||
818 | * can't remove the key itself. | ||
819 | */ | ||
820 | status_flags |= | ||
821 | FSCRYPT_KEY_REMOVAL_STATUS_FLAG_OTHER_USERS; | ||
822 | err = 0; | ||
823 | up_write(&key->sem); | ||
824 | goto out_put_key; | ||
825 | } | ||
826 | } | ||
827 | |||
828 | /* No user claims remaining. Go ahead and wipe the secret. */ | ||
829 | dead = false; | ||
830 | if (is_master_key_secret_present(&mk->mk_secret)) { | ||
831 | down_write(&mk->mk_secret_sem); | ||
832 | wipe_master_key_secret(&mk->mk_secret); | ||
833 | dead = refcount_dec_and_test(&mk->mk_refcount); | ||
834 | up_write(&mk->mk_secret_sem); | ||
835 | } | ||
836 | up_write(&key->sem); | ||
837 | if (dead) { | ||
838 | /* | ||
839 | * No inodes reference the key, and we wiped the secret, so the | ||
840 | * key object is free to be removed from the keyring. | ||
841 | */ | ||
842 | key_invalidate(key); | ||
843 | err = 0; | ||
844 | } else { | ||
845 | /* Some inodes still reference this key; try to evict them. */ | ||
846 | err = try_to_lock_encrypted_files(sb, mk); | ||
847 | if (err == -EBUSY) { | ||
848 | status_flags |= | ||
849 | FSCRYPT_KEY_REMOVAL_STATUS_FLAG_FILES_BUSY; | ||
850 | err = 0; | ||
851 | } | ||
852 | } | ||
853 | /* | ||
854 | * We return 0 if we successfully did something: removed a claim to the | ||
855 | * key, wiped the secret, or tried locking the files again. Users need | ||
856 | * to check the informational status flags if they care whether the key | ||
857 | * has been fully removed including all files locked. | ||
858 | */ | ||
859 | out_put_key: | ||
860 | key_put(key); | ||
861 | if (err == 0) | ||
862 | err = put_user(status_flags, &uarg->removal_status_flags); | ||
863 | return err; | ||
864 | } | ||
865 | |||
866 | int fscrypt_ioctl_remove_key(struct file *filp, void __user *uarg) | ||
867 | { | ||
868 | return do_remove_key(filp, uarg, false); | ||
869 | } | ||
870 | EXPORT_SYMBOL_GPL(fscrypt_ioctl_remove_key); | ||
871 | |||
872 | int fscrypt_ioctl_remove_key_all_users(struct file *filp, void __user *uarg) | ||
873 | { | ||
874 | if (!capable(CAP_SYS_ADMIN)) | ||
875 | return -EACCES; | ||
876 | return do_remove_key(filp, uarg, true); | ||
877 | } | ||
878 | EXPORT_SYMBOL_GPL(fscrypt_ioctl_remove_key_all_users); | ||
879 | |||
880 | /* | ||
881 | * Retrieve the status of an fscrypt master encryption key. | ||
882 | * | ||
883 | * We set ->status to indicate whether the key is absent, present, or | ||
884 | * incompletely removed. "Incompletely removed" means that the master key | ||
885 | * secret has been removed, but some files which had been unlocked with it are | ||
886 | * still in use. This field allows applications to easily determine the state | ||
887 | * of an encrypted directory without using a hack such as trying to open a | ||
888 | * regular file in it (which can confuse the "incompletely removed" state with | ||
889 | * absent or present). | ||
890 | * | ||
891 | * In addition, for v2 policy keys we allow applications to determine, via | ||
892 | * ->status_flags and ->user_count, whether the key has been added by the | ||
893 | * current user, by other users, or by both. Most applications should not need | ||
894 | * this, since ordinarily only one user should know a given key. However, if a | ||
895 | * secret key is shared by multiple users, applications may wish to add an | ||
896 | * already-present key to prevent other users from removing it. This ioctl can | ||
897 | * be used to check whether that really is the case before the work is done to | ||
898 | * add the key --- which might e.g. require prompting the user for a passphrase. | ||
899 | * | ||
900 | * For more details, see the "FS_IOC_GET_ENCRYPTION_KEY_STATUS" section of | ||
901 | * Documentation/filesystems/fscrypt.rst. | ||
902 | */ | ||
903 | int fscrypt_ioctl_get_key_status(struct file *filp, void __user *uarg) | ||
904 | { | ||
905 | struct super_block *sb = file_inode(filp)->i_sb; | ||
906 | struct fscrypt_get_key_status_arg arg; | ||
907 | struct key *key; | ||
908 | struct fscrypt_master_key *mk; | ||
909 | int err; | ||
910 | |||
911 | if (copy_from_user(&arg, uarg, sizeof(arg))) | ||
912 | return -EFAULT; | ||
913 | |||
914 | if (!valid_key_spec(&arg.key_spec)) | ||
915 | return -EINVAL; | ||
916 | |||
917 | if (memchr_inv(arg.__reserved, 0, sizeof(arg.__reserved))) | ||
918 | return -EINVAL; | ||
919 | |||
920 | arg.status_flags = 0; | ||
921 | arg.user_count = 0; | ||
922 | memset(arg.__out_reserved, 0, sizeof(arg.__out_reserved)); | ||
923 | |||
924 | key = fscrypt_find_master_key(sb, &arg.key_spec); | ||
925 | if (IS_ERR(key)) { | ||
926 | if (key != ERR_PTR(-ENOKEY)) | ||
927 | return PTR_ERR(key); | ||
928 | arg.status = FSCRYPT_KEY_STATUS_ABSENT; | ||
929 | err = 0; | ||
930 | goto out; | ||
931 | } | ||
932 | mk = key->payload.data[0]; | ||
933 | down_read(&key->sem); | ||
934 | |||
935 | if (!is_master_key_secret_present(&mk->mk_secret)) { | ||
936 | arg.status = FSCRYPT_KEY_STATUS_INCOMPLETELY_REMOVED; | ||
937 | err = 0; | ||
938 | goto out_release_key; | ||
939 | } | ||
940 | |||
941 | arg.status = FSCRYPT_KEY_STATUS_PRESENT; | ||
942 | if (mk->mk_users) { | ||
943 | struct key *mk_user; | ||
944 | |||
945 | arg.user_count = mk->mk_users->keys.nr_leaves_on_tree; | ||
946 | mk_user = find_master_key_user(mk); | ||
947 | if (!IS_ERR(mk_user)) { | ||
948 | arg.status_flags |= | ||
949 | FSCRYPT_KEY_STATUS_FLAG_ADDED_BY_SELF; | ||
950 | key_put(mk_user); | ||
951 | } else if (mk_user != ERR_PTR(-ENOKEY)) { | ||
952 | err = PTR_ERR(mk_user); | ||
953 | goto out_release_key; | ||
954 | } | ||
955 | } | ||
956 | err = 0; | ||
957 | out_release_key: | ||
958 | up_read(&key->sem); | ||
959 | key_put(key); | ||
960 | out: | ||
961 | if (!err && copy_to_user(uarg, &arg, sizeof(arg))) | ||
962 | err = -EFAULT; | ||
963 | return err; | ||
964 | } | ||
965 | EXPORT_SYMBOL_GPL(fscrypt_ioctl_get_key_status); | ||
966 | |||
967 | int __init fscrypt_init_keyring(void) | ||
968 | { | ||
969 | int err; | ||
970 | |||
971 | err = register_key_type(&key_type_fscrypt); | ||
972 | if (err) | ||
973 | return err; | ||
974 | |||
975 | err = register_key_type(&key_type_fscrypt_user); | ||
976 | if (err) | ||
977 | goto err_unregister_fscrypt; | ||
978 | |||
979 | return 0; | ||
980 | |||
981 | err_unregister_fscrypt: | ||
982 | unregister_key_type(&key_type_fscrypt); | ||
983 | return err; | ||
984 | } | ||
diff --git a/fs/crypto/keysetup.c b/fs/crypto/keysetup.c new file mode 100644 index 000000000000..d71c2d6dd162 --- /dev/null +++ b/fs/crypto/keysetup.c | |||
@@ -0,0 +1,591 @@ | |||
1 | // SPDX-License-Identifier: GPL-2.0 | ||
2 | /* | ||
3 | * Key setup facility for FS encryption support. | ||
4 | * | ||
5 | * Copyright (C) 2015, Google, Inc. | ||
6 | * | ||
7 | * Originally written by Michael Halcrow, Ildar Muslukhov, and Uday Savagaonkar. | ||
8 | * Heavily modified since then. | ||
9 | */ | ||
10 | |||
11 | #include <crypto/aes.h> | ||
12 | #include <crypto/sha.h> | ||
13 | #include <crypto/skcipher.h> | ||
14 | #include <linux/key.h> | ||
15 | |||
16 | #include "fscrypt_private.h" | ||
17 | |||
18 | static struct crypto_shash *essiv_hash_tfm; | ||
19 | |||
20 | static struct fscrypt_mode available_modes[] = { | ||
21 | [FSCRYPT_MODE_AES_256_XTS] = { | ||
22 | .friendly_name = "AES-256-XTS", | ||
23 | .cipher_str = "xts(aes)", | ||
24 | .keysize = 64, | ||
25 | .ivsize = 16, | ||
26 | }, | ||
27 | [FSCRYPT_MODE_AES_256_CTS] = { | ||
28 | .friendly_name = "AES-256-CTS-CBC", | ||
29 | .cipher_str = "cts(cbc(aes))", | ||
30 | .keysize = 32, | ||
31 | .ivsize = 16, | ||
32 | }, | ||
33 | [FSCRYPT_MODE_AES_128_CBC] = { | ||
34 | .friendly_name = "AES-128-CBC", | ||
35 | .cipher_str = "cbc(aes)", | ||
36 | .keysize = 16, | ||
37 | .ivsize = 16, | ||
38 | .needs_essiv = true, | ||
39 | }, | ||
40 | [FSCRYPT_MODE_AES_128_CTS] = { | ||
41 | .friendly_name = "AES-128-CTS-CBC", | ||
42 | .cipher_str = "cts(cbc(aes))", | ||
43 | .keysize = 16, | ||
44 | .ivsize = 16, | ||
45 | }, | ||
46 | [FSCRYPT_MODE_ADIANTUM] = { | ||
47 | .friendly_name = "Adiantum", | ||
48 | .cipher_str = "adiantum(xchacha12,aes)", | ||
49 | .keysize = 32, | ||
50 | .ivsize = 32, | ||
51 | }, | ||
52 | }; | ||
53 | |||
54 | static struct fscrypt_mode * | ||
55 | select_encryption_mode(const union fscrypt_policy *policy, | ||
56 | const struct inode *inode) | ||
57 | { | ||
58 | if (S_ISREG(inode->i_mode)) | ||
59 | return &available_modes[fscrypt_policy_contents_mode(policy)]; | ||
60 | |||
61 | if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) | ||
62 | return &available_modes[fscrypt_policy_fnames_mode(policy)]; | ||
63 | |||
64 | WARN_ONCE(1, "fscrypt: filesystem tried to load encryption info for inode %lu, which is not encryptable (file type %d)\n", | ||
65 | inode->i_ino, (inode->i_mode & S_IFMT)); | ||
66 | return ERR_PTR(-EINVAL); | ||
67 | } | ||
68 | |||
69 | /* Create a symmetric cipher object for the given encryption mode and key */ | ||
70 | struct crypto_skcipher *fscrypt_allocate_skcipher(struct fscrypt_mode *mode, | ||
71 | const u8 *raw_key, | ||
72 | const struct inode *inode) | ||
73 | { | ||
74 | struct crypto_skcipher *tfm; | ||
75 | int err; | ||
76 | |||
77 | tfm = crypto_alloc_skcipher(mode->cipher_str, 0, 0); | ||
78 | if (IS_ERR(tfm)) { | ||
79 | if (PTR_ERR(tfm) == -ENOENT) { | ||
80 | fscrypt_warn(inode, | ||
81 | "Missing crypto API support for %s (API name: \"%s\")", | ||
82 | mode->friendly_name, mode->cipher_str); | ||
83 | return ERR_PTR(-ENOPKG); | ||
84 | } | ||
85 | fscrypt_err(inode, "Error allocating '%s' transform: %ld", | ||
86 | mode->cipher_str, PTR_ERR(tfm)); | ||
87 | return tfm; | ||
88 | } | ||
89 | if (unlikely(!mode->logged_impl_name)) { | ||
90 | /* | ||
91 | * fscrypt performance can vary greatly depending on which | ||
92 | * crypto algorithm implementation is used. Help people debug | ||
93 | * performance problems by logging the ->cra_driver_name the | ||
94 | * first time a mode is used. Note that multiple threads can | ||
95 | * race here, but it doesn't really matter. | ||
96 | */ | ||
97 | mode->logged_impl_name = true; | ||
98 | pr_info("fscrypt: %s using implementation \"%s\"\n", | ||
99 | mode->friendly_name, | ||
100 | crypto_skcipher_alg(tfm)->base.cra_driver_name); | ||
101 | } | ||
102 | crypto_skcipher_set_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS); | ||
103 | err = crypto_skcipher_setkey(tfm, raw_key, mode->keysize); | ||
104 | if (err) | ||
105 | goto err_free_tfm; | ||
106 | |||
107 | return tfm; | ||
108 | |||
109 | err_free_tfm: | ||
110 | crypto_free_skcipher(tfm); | ||
111 | return ERR_PTR(err); | ||
112 | } | ||
113 | |||
114 | static int derive_essiv_salt(const u8 *key, int keysize, u8 *salt) | ||
115 | { | ||
116 | struct crypto_shash *tfm = READ_ONCE(essiv_hash_tfm); | ||
117 | |||
118 | /* init hash transform on demand */ | ||
119 | if (unlikely(!tfm)) { | ||
120 | struct crypto_shash *prev_tfm; | ||
121 | |||
122 | tfm = crypto_alloc_shash("sha256", 0, 0); | ||
123 | if (IS_ERR(tfm)) { | ||
124 | if (PTR_ERR(tfm) == -ENOENT) { | ||
125 | fscrypt_warn(NULL, | ||
126 | "Missing crypto API support for SHA-256"); | ||
127 | return -ENOPKG; | ||
128 | } | ||
129 | fscrypt_err(NULL, | ||
130 | "Error allocating SHA-256 transform: %ld", | ||
131 | PTR_ERR(tfm)); | ||
132 | return PTR_ERR(tfm); | ||
133 | } | ||
134 | prev_tfm = cmpxchg(&essiv_hash_tfm, NULL, tfm); | ||
135 | if (prev_tfm) { | ||
136 | crypto_free_shash(tfm); | ||
137 | tfm = prev_tfm; | ||
138 | } | ||
139 | } | ||
140 | |||
141 | { | ||
142 | SHASH_DESC_ON_STACK(desc, tfm); | ||
143 | desc->tfm = tfm; | ||
144 | |||
145 | return crypto_shash_digest(desc, key, keysize, salt); | ||
146 | } | ||
147 | } | ||
148 | |||
149 | static int init_essiv_generator(struct fscrypt_info *ci, const u8 *raw_key, | ||
150 | int keysize) | ||
151 | { | ||
152 | int err; | ||
153 | struct crypto_cipher *essiv_tfm; | ||
154 | u8 salt[SHA256_DIGEST_SIZE]; | ||
155 | |||
156 | if (WARN_ON(ci->ci_mode->ivsize != AES_BLOCK_SIZE)) | ||
157 | return -EINVAL; | ||
158 | |||
159 | essiv_tfm = crypto_alloc_cipher("aes", 0, 0); | ||
160 | if (IS_ERR(essiv_tfm)) | ||
161 | return PTR_ERR(essiv_tfm); | ||
162 | |||
163 | ci->ci_essiv_tfm = essiv_tfm; | ||
164 | |||
165 | err = derive_essiv_salt(raw_key, keysize, salt); | ||
166 | if (err) | ||
167 | goto out; | ||
168 | |||
169 | /* | ||
170 | * Using SHA256 to derive the salt/key will result in AES-256 being | ||
171 | * used for IV generation. File contents encryption will still use the | ||
172 | * configured keysize (AES-128) nevertheless. | ||
173 | */ | ||
174 | err = crypto_cipher_setkey(essiv_tfm, salt, sizeof(salt)); | ||
175 | if (err) | ||
176 | goto out; | ||
177 | |||
178 | out: | ||
179 | memzero_explicit(salt, sizeof(salt)); | ||
180 | return err; | ||
181 | } | ||
182 | |||
183 | /* Given the per-file key, set up the file's crypto transform object(s) */ | ||
184 | int fscrypt_set_derived_key(struct fscrypt_info *ci, const u8 *derived_key) | ||
185 | { | ||
186 | struct fscrypt_mode *mode = ci->ci_mode; | ||
187 | struct crypto_skcipher *ctfm; | ||
188 | int err; | ||
189 | |||
190 | ctfm = fscrypt_allocate_skcipher(mode, derived_key, ci->ci_inode); | ||
191 | if (IS_ERR(ctfm)) | ||
192 | return PTR_ERR(ctfm); | ||
193 | |||
194 | ci->ci_ctfm = ctfm; | ||
195 | |||
196 | if (mode->needs_essiv) { | ||
197 | err = init_essiv_generator(ci, derived_key, mode->keysize); | ||
198 | if (err) { | ||
199 | fscrypt_warn(ci->ci_inode, | ||
200 | "Error initializing ESSIV generator: %d", | ||
201 | err); | ||
202 | return err; | ||
203 | } | ||
204 | } | ||
205 | return 0; | ||
206 | } | ||
207 | |||
208 | static int setup_per_mode_key(struct fscrypt_info *ci, | ||
209 | struct fscrypt_master_key *mk) | ||
210 | { | ||
211 | struct fscrypt_mode *mode = ci->ci_mode; | ||
212 | u8 mode_num = mode - available_modes; | ||
213 | struct crypto_skcipher *tfm, *prev_tfm; | ||
214 | u8 mode_key[FSCRYPT_MAX_KEY_SIZE]; | ||
215 | int err; | ||
216 | |||
217 | if (WARN_ON(mode_num >= ARRAY_SIZE(mk->mk_mode_keys))) | ||
218 | return -EINVAL; | ||
219 | |||
220 | /* pairs with cmpxchg() below */ | ||
221 | tfm = READ_ONCE(mk->mk_mode_keys[mode_num]); | ||
222 | if (likely(tfm != NULL)) | ||
223 | goto done; | ||
224 | |||
225 | BUILD_BUG_ON(sizeof(mode_num) != 1); | ||
226 | err = fscrypt_hkdf_expand(&mk->mk_secret.hkdf, | ||
227 | HKDF_CONTEXT_PER_MODE_KEY, | ||
228 | &mode_num, sizeof(mode_num), | ||
229 | mode_key, mode->keysize); | ||
230 | if (err) | ||
231 | return err; | ||
232 | tfm = fscrypt_allocate_skcipher(mode, mode_key, ci->ci_inode); | ||
233 | memzero_explicit(mode_key, mode->keysize); | ||
234 | if (IS_ERR(tfm)) | ||
235 | return PTR_ERR(tfm); | ||
236 | |||
237 | /* pairs with READ_ONCE() above */ | ||
238 | prev_tfm = cmpxchg(&mk->mk_mode_keys[mode_num], NULL, tfm); | ||
239 | if (prev_tfm != NULL) { | ||
240 | crypto_free_skcipher(tfm); | ||
241 | tfm = prev_tfm; | ||
242 | } | ||
243 | done: | ||
244 | ci->ci_ctfm = tfm; | ||
245 | return 0; | ||
246 | } | ||
247 | |||
248 | static int fscrypt_setup_v2_file_key(struct fscrypt_info *ci, | ||
249 | struct fscrypt_master_key *mk) | ||
250 | { | ||
251 | u8 derived_key[FSCRYPT_MAX_KEY_SIZE]; | ||
252 | int err; | ||
253 | |||
254 | if (ci->ci_policy.v2.flags & FSCRYPT_POLICY_FLAG_DIRECT_KEY) { | ||
255 | /* | ||
256 | * DIRECT_KEY: instead of deriving per-file keys, the per-file | ||
257 | * nonce will be included in all the IVs. But unlike v1 | ||
258 | * policies, for v2 policies in this case we don't encrypt with | ||
259 | * the master key directly but rather derive a per-mode key. | ||
260 | * This ensures that the master key is consistently used only | ||
261 | * for HKDF, avoiding key reuse issues. | ||
262 | */ | ||
263 | if (!fscrypt_mode_supports_direct_key(ci->ci_mode)) { | ||
264 | fscrypt_warn(ci->ci_inode, | ||
265 | "Direct key flag not allowed with %s", | ||
266 | ci->ci_mode->friendly_name); | ||
267 | return -EINVAL; | ||
268 | } | ||
269 | return setup_per_mode_key(ci, mk); | ||
270 | } | ||
271 | |||
272 | err = fscrypt_hkdf_expand(&mk->mk_secret.hkdf, | ||
273 | HKDF_CONTEXT_PER_FILE_KEY, | ||
274 | ci->ci_nonce, FS_KEY_DERIVATION_NONCE_SIZE, | ||
275 | derived_key, ci->ci_mode->keysize); | ||
276 | if (err) | ||
277 | return err; | ||
278 | |||
279 | err = fscrypt_set_derived_key(ci, derived_key); | ||
280 | memzero_explicit(derived_key, ci->ci_mode->keysize); | ||
281 | return err; | ||
282 | } | ||
283 | |||
284 | /* | ||
285 | * Find the master key, then set up the inode's actual encryption key. | ||
286 | * | ||
287 | * If the master key is found in the filesystem-level keyring, then the | ||
288 | * corresponding 'struct key' is returned in *master_key_ret with | ||
289 | * ->mk_secret_sem read-locked. This is needed to ensure that only one task | ||
290 | * links the fscrypt_info into ->mk_decrypted_inodes (as multiple tasks may race | ||
291 | * to create an fscrypt_info for the same inode), and to synchronize the master | ||
292 | * key being removed with a new inode starting to use it. | ||
293 | */ | ||
294 | static int setup_file_encryption_key(struct fscrypt_info *ci, | ||
295 | struct key **master_key_ret) | ||
296 | { | ||
297 | struct key *key; | ||
298 | struct fscrypt_master_key *mk = NULL; | ||
299 | struct fscrypt_key_specifier mk_spec; | ||
300 | int err; | ||
301 | |||
302 | switch (ci->ci_policy.version) { | ||
303 | case FSCRYPT_POLICY_V1: | ||
304 | mk_spec.type = FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR; | ||
305 | memcpy(mk_spec.u.descriptor, | ||
306 | ci->ci_policy.v1.master_key_descriptor, | ||
307 | FSCRYPT_KEY_DESCRIPTOR_SIZE); | ||
308 | break; | ||
309 | case FSCRYPT_POLICY_V2: | ||
310 | mk_spec.type = FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER; | ||
311 | memcpy(mk_spec.u.identifier, | ||
312 | ci->ci_policy.v2.master_key_identifier, | ||
313 | FSCRYPT_KEY_IDENTIFIER_SIZE); | ||
314 | break; | ||
315 | default: | ||
316 | WARN_ON(1); | ||
317 | return -EINVAL; | ||
318 | } | ||
319 | |||
320 | key = fscrypt_find_master_key(ci->ci_inode->i_sb, &mk_spec); | ||
321 | if (IS_ERR(key)) { | ||
322 | if (key != ERR_PTR(-ENOKEY) || | ||
323 | ci->ci_policy.version != FSCRYPT_POLICY_V1) | ||
324 | return PTR_ERR(key); | ||
325 | |||
326 | /* | ||
327 | * As a legacy fallback for v1 policies, search for the key in | ||
328 | * the current task's subscribed keyrings too. Don't move this | ||
329 | * to before the search of ->s_master_keys, since users | ||
330 | * shouldn't be able to override filesystem-level keys. | ||
331 | */ | ||
332 | return fscrypt_setup_v1_file_key_via_subscribed_keyrings(ci); | ||
333 | } | ||
334 | |||
335 | mk = key->payload.data[0]; | ||
336 | down_read(&mk->mk_secret_sem); | ||
337 | |||
338 | /* Has the secret been removed (via FS_IOC_REMOVE_ENCRYPTION_KEY)? */ | ||
339 | if (!is_master_key_secret_present(&mk->mk_secret)) { | ||
340 | err = -ENOKEY; | ||
341 | goto out_release_key; | ||
342 | } | ||
343 | |||
344 | /* | ||
345 | * Require that the master key be at least as long as the derived key. | ||
346 | * Otherwise, the derived key cannot possibly contain as much entropy as | ||
347 | * that required by the encryption mode it will be used for. For v1 | ||
348 | * policies it's also required for the KDF to work at all. | ||
349 | */ | ||
350 | if (mk->mk_secret.size < ci->ci_mode->keysize) { | ||
351 | fscrypt_warn(NULL, | ||
352 | "key with %s %*phN is too short (got %u bytes, need %u+ bytes)", | ||
353 | master_key_spec_type(&mk_spec), | ||
354 | master_key_spec_len(&mk_spec), (u8 *)&mk_spec.u, | ||
355 | mk->mk_secret.size, ci->ci_mode->keysize); | ||
356 | err = -ENOKEY; | ||
357 | goto out_release_key; | ||
358 | } | ||
359 | |||
360 | switch (ci->ci_policy.version) { | ||
361 | case FSCRYPT_POLICY_V1: | ||
362 | err = fscrypt_setup_v1_file_key(ci, mk->mk_secret.raw); | ||
363 | break; | ||
364 | case FSCRYPT_POLICY_V2: | ||
365 | err = fscrypt_setup_v2_file_key(ci, mk); | ||
366 | break; | ||
367 | default: | ||
368 | WARN_ON(1); | ||
369 | err = -EINVAL; | ||
370 | break; | ||
371 | } | ||
372 | if (err) | ||
373 | goto out_release_key; | ||
374 | |||
375 | *master_key_ret = key; | ||
376 | return 0; | ||
377 | |||
378 | out_release_key: | ||
379 | up_read(&mk->mk_secret_sem); | ||
380 | key_put(key); | ||
381 | return err; | ||
382 | } | ||
383 | |||
384 | static void put_crypt_info(struct fscrypt_info *ci) | ||
385 | { | ||
386 | struct key *key; | ||
387 | |||
388 | if (!ci) | ||
389 | return; | ||
390 | |||
391 | if (ci->ci_direct_key) { | ||
392 | fscrypt_put_direct_key(ci->ci_direct_key); | ||
393 | } else if ((ci->ci_ctfm != NULL || ci->ci_essiv_tfm != NULL) && | ||
394 | !fscrypt_is_direct_key_policy(&ci->ci_policy)) { | ||
395 | crypto_free_skcipher(ci->ci_ctfm); | ||
396 | crypto_free_cipher(ci->ci_essiv_tfm); | ||
397 | } | ||
398 | |||
399 | key = ci->ci_master_key; | ||
400 | if (key) { | ||
401 | struct fscrypt_master_key *mk = key->payload.data[0]; | ||
402 | |||
403 | /* | ||
404 | * Remove this inode from the list of inodes that were unlocked | ||
405 | * with the master key. | ||
406 | * | ||
407 | * In addition, if we're removing the last inode from a key that | ||
408 | * already had its secret removed, invalidate the key so that it | ||
409 | * gets removed from ->s_master_keys. | ||
410 | */ | ||
411 | spin_lock(&mk->mk_decrypted_inodes_lock); | ||
412 | list_del(&ci->ci_master_key_link); | ||
413 | spin_unlock(&mk->mk_decrypted_inodes_lock); | ||
414 | if (refcount_dec_and_test(&mk->mk_refcount)) | ||
415 | key_invalidate(key); | ||
416 | key_put(key); | ||
417 | } | ||
418 | kmem_cache_free(fscrypt_info_cachep, ci); | ||
419 | } | ||
420 | |||
421 | int fscrypt_get_encryption_info(struct inode *inode) | ||
422 | { | ||
423 | struct fscrypt_info *crypt_info; | ||
424 | union fscrypt_context ctx; | ||
425 | struct fscrypt_mode *mode; | ||
426 | struct key *master_key = NULL; | ||
427 | int res; | ||
428 | |||
429 | if (fscrypt_has_encryption_key(inode)) | ||
430 | return 0; | ||
431 | |||
432 | res = fscrypt_initialize(inode->i_sb->s_cop->flags); | ||
433 | if (res) | ||
434 | return res; | ||
435 | |||
436 | res = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx)); | ||
437 | if (res < 0) { | ||
438 | if (!fscrypt_dummy_context_enabled(inode) || | ||
439 | IS_ENCRYPTED(inode)) { | ||
440 | fscrypt_warn(inode, | ||
441 | "Error %d getting encryption context", | ||
442 | res); | ||
443 | return res; | ||
444 | } | ||
445 | /* Fake up a context for an unencrypted directory */ | ||
446 | memset(&ctx, 0, sizeof(ctx)); | ||
447 | ctx.version = FSCRYPT_CONTEXT_V1; | ||
448 | ctx.v1.contents_encryption_mode = FSCRYPT_MODE_AES_256_XTS; | ||
449 | ctx.v1.filenames_encryption_mode = FSCRYPT_MODE_AES_256_CTS; | ||
450 | memset(ctx.v1.master_key_descriptor, 0x42, | ||
451 | FSCRYPT_KEY_DESCRIPTOR_SIZE); | ||
452 | res = sizeof(ctx.v1); | ||
453 | } | ||
454 | |||
455 | crypt_info = kmem_cache_zalloc(fscrypt_info_cachep, GFP_NOFS); | ||
456 | if (!crypt_info) | ||
457 | return -ENOMEM; | ||
458 | |||
459 | crypt_info->ci_inode = inode; | ||
460 | |||
461 | res = fscrypt_policy_from_context(&crypt_info->ci_policy, &ctx, res); | ||
462 | if (res) { | ||
463 | fscrypt_warn(inode, | ||
464 | "Unrecognized or corrupt encryption context"); | ||
465 | goto out; | ||
466 | } | ||
467 | |||
468 | switch (ctx.version) { | ||
469 | case FSCRYPT_CONTEXT_V1: | ||
470 | memcpy(crypt_info->ci_nonce, ctx.v1.nonce, | ||
471 | FS_KEY_DERIVATION_NONCE_SIZE); | ||
472 | break; | ||
473 | case FSCRYPT_CONTEXT_V2: | ||
474 | memcpy(crypt_info->ci_nonce, ctx.v2.nonce, | ||
475 | FS_KEY_DERIVATION_NONCE_SIZE); | ||
476 | break; | ||
477 | default: | ||
478 | WARN_ON(1); | ||
479 | res = -EINVAL; | ||
480 | goto out; | ||
481 | } | ||
482 | |||
483 | if (!fscrypt_supported_policy(&crypt_info->ci_policy, inode)) { | ||
484 | res = -EINVAL; | ||
485 | goto out; | ||
486 | } | ||
487 | |||
488 | mode = select_encryption_mode(&crypt_info->ci_policy, inode); | ||
489 | if (IS_ERR(mode)) { | ||
490 | res = PTR_ERR(mode); | ||
491 | goto out; | ||
492 | } | ||
493 | WARN_ON(mode->ivsize > FSCRYPT_MAX_IV_SIZE); | ||
494 | crypt_info->ci_mode = mode; | ||
495 | |||
496 | res = setup_file_encryption_key(crypt_info, &master_key); | ||
497 | if (res) | ||
498 | goto out; | ||
499 | |||
500 | if (cmpxchg_release(&inode->i_crypt_info, NULL, crypt_info) == NULL) { | ||
501 | if (master_key) { | ||
502 | struct fscrypt_master_key *mk = | ||
503 | master_key->payload.data[0]; | ||
504 | |||
505 | refcount_inc(&mk->mk_refcount); | ||
506 | crypt_info->ci_master_key = key_get(master_key); | ||
507 | spin_lock(&mk->mk_decrypted_inodes_lock); | ||
508 | list_add(&crypt_info->ci_master_key_link, | ||
509 | &mk->mk_decrypted_inodes); | ||
510 | spin_unlock(&mk->mk_decrypted_inodes_lock); | ||
511 | } | ||
512 | crypt_info = NULL; | ||
513 | } | ||
514 | res = 0; | ||
515 | out: | ||
516 | if (master_key) { | ||
517 | struct fscrypt_master_key *mk = master_key->payload.data[0]; | ||
518 | |||
519 | up_read(&mk->mk_secret_sem); | ||
520 | key_put(master_key); | ||
521 | } | ||
522 | if (res == -ENOKEY) | ||
523 | res = 0; | ||
524 | put_crypt_info(crypt_info); | ||
525 | return res; | ||
526 | } | ||
527 | EXPORT_SYMBOL(fscrypt_get_encryption_info); | ||
528 | |||
529 | /** | ||
530 | * fscrypt_put_encryption_info - free most of an inode's fscrypt data | ||
531 | * | ||
532 | * Free the inode's fscrypt_info. Filesystems must call this when the inode is | ||
533 | * being evicted. An RCU grace period need not have elapsed yet. | ||
534 | */ | ||
535 | void fscrypt_put_encryption_info(struct inode *inode) | ||
536 | { | ||
537 | put_crypt_info(inode->i_crypt_info); | ||
538 | inode->i_crypt_info = NULL; | ||
539 | } | ||
540 | EXPORT_SYMBOL(fscrypt_put_encryption_info); | ||
541 | |||
542 | /** | ||
543 | * fscrypt_free_inode - free an inode's fscrypt data requiring RCU delay | ||
544 | * | ||
545 | * Free the inode's cached decrypted symlink target, if any. Filesystems must | ||
546 | * call this after an RCU grace period, just before they free the inode. | ||
547 | */ | ||
548 | void fscrypt_free_inode(struct inode *inode) | ||
549 | { | ||
550 | if (IS_ENCRYPTED(inode) && S_ISLNK(inode->i_mode)) { | ||
551 | kfree(inode->i_link); | ||
552 | inode->i_link = NULL; | ||
553 | } | ||
554 | } | ||
555 | EXPORT_SYMBOL(fscrypt_free_inode); | ||
556 | |||
557 | /** | ||
558 | * fscrypt_drop_inode - check whether the inode's master key has been removed | ||
559 | * | ||
560 | * Filesystems supporting fscrypt must call this from their ->drop_inode() | ||
561 | * method so that encrypted inodes are evicted as soon as they're no longer in | ||
562 | * use and their master key has been removed. | ||
563 | * | ||
564 | * Return: 1 if fscrypt wants the inode to be evicted now, otherwise 0 | ||
565 | */ | ||
566 | int fscrypt_drop_inode(struct inode *inode) | ||
567 | { | ||
568 | const struct fscrypt_info *ci = READ_ONCE(inode->i_crypt_info); | ||
569 | const struct fscrypt_master_key *mk; | ||
570 | |||
571 | /* | ||
572 | * If ci is NULL, then the inode doesn't have an encryption key set up | ||
573 | * so it's irrelevant. If ci_master_key is NULL, then the master key | ||
574 | * was provided via the legacy mechanism of the process-subscribed | ||
575 | * keyrings, so we don't know whether it's been removed or not. | ||
576 | */ | ||
577 | if (!ci || !ci->ci_master_key) | ||
578 | return 0; | ||
579 | mk = ci->ci_master_key->payload.data[0]; | ||
580 | |||
581 | /* | ||
582 | * Note: since we aren't holding ->mk_secret_sem, the result here can | ||
583 | * immediately become outdated. But there's no correctness problem with | ||
584 | * unnecessarily evicting. Nor is there a correctness problem with not | ||
585 | * evicting while iput() is racing with the key being removed, since | ||
586 | * then the thread removing the key will either evict the inode itself | ||
587 | * or will correctly detect that it wasn't evicted due to the race. | ||
588 | */ | ||
589 | return !is_master_key_secret_present(&mk->mk_secret); | ||
590 | } | ||
591 | EXPORT_SYMBOL_GPL(fscrypt_drop_inode); | ||
diff --git a/fs/crypto/keysetup_v1.c b/fs/crypto/keysetup_v1.c new file mode 100644 index 000000000000..ad1a36c370c3 --- /dev/null +++ b/fs/crypto/keysetup_v1.c | |||
@@ -0,0 +1,340 @@ | |||
1 | // SPDX-License-Identifier: GPL-2.0 | ||
2 | /* | ||
3 | * Key setup for v1 encryption policies | ||
4 | * | ||
5 | * Copyright 2015, 2019 Google LLC | ||
6 | */ | ||
7 | |||
8 | /* | ||
9 | * This file implements compatibility functions for the original encryption | ||
10 | * policy version ("v1"), including: | ||
11 | * | ||
12 | * - Deriving per-file keys using the AES-128-ECB based KDF | ||
13 | * (rather than the new method of using HKDF-SHA512) | ||
14 | * | ||
15 | * - Retrieving fscrypt master keys from process-subscribed keyrings | ||
16 | * (rather than the new method of using a filesystem-level keyring) | ||
17 | * | ||
18 | * - Handling policies with the DIRECT_KEY flag set using a master key table | ||
19 | * (rather than the new method of implementing DIRECT_KEY with per-mode keys | ||
20 | * managed alongside the master keys in the filesystem-level keyring) | ||
21 | */ | ||
22 | |||
23 | #include <crypto/algapi.h> | ||
24 | #include <crypto/skcipher.h> | ||
25 | #include <keys/user-type.h> | ||
26 | #include <linux/hashtable.h> | ||
27 | #include <linux/scatterlist.h> | ||
28 | |||
29 | #include "fscrypt_private.h" | ||
30 | |||
31 | /* Table of keys referenced by DIRECT_KEY policies */ | ||
32 | static DEFINE_HASHTABLE(fscrypt_direct_keys, 6); /* 6 bits = 64 buckets */ | ||
33 | static DEFINE_SPINLOCK(fscrypt_direct_keys_lock); | ||
34 | |||
35 | /* | ||
36 | * v1 key derivation function. This generates the derived key by encrypting the | ||
37 | * master key with AES-128-ECB using the nonce as the AES key. This provides a | ||
38 | * unique derived key with sufficient entropy for each inode. However, it's | ||
39 | * nonstandard, non-extensible, doesn't evenly distribute the entropy from the | ||
40 | * master key, and is trivially reversible: an attacker who compromises a | ||
41 | * derived key can "decrypt" it to get back to the master key, then derive any | ||
42 | * other key. For all new code, use HKDF instead. | ||
43 | * | ||
44 | * The master key must be at least as long as the derived key. If the master | ||
45 | * key is longer, then only the first 'derived_keysize' bytes are used. | ||
46 | */ | ||
47 | static int derive_key_aes(const u8 *master_key, | ||
48 | const u8 nonce[FS_KEY_DERIVATION_NONCE_SIZE], | ||
49 | u8 *derived_key, unsigned int derived_keysize) | ||
50 | { | ||
51 | int res = 0; | ||
52 | struct skcipher_request *req = NULL; | ||
53 | DECLARE_CRYPTO_WAIT(wait); | ||
54 | struct scatterlist src_sg, dst_sg; | ||
55 | struct crypto_skcipher *tfm = crypto_alloc_skcipher("ecb(aes)", 0, 0); | ||
56 | |||
57 | if (IS_ERR(tfm)) { | ||
58 | res = PTR_ERR(tfm); | ||
59 | tfm = NULL; | ||
60 | goto out; | ||
61 | } | ||
62 | crypto_skcipher_set_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS); | ||
63 | req = skcipher_request_alloc(tfm, GFP_NOFS); | ||
64 | if (!req) { | ||
65 | res = -ENOMEM; | ||
66 | goto out; | ||
67 | } | ||
68 | skcipher_request_set_callback(req, | ||
69 | CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP, | ||
70 | crypto_req_done, &wait); | ||
71 | res = crypto_skcipher_setkey(tfm, nonce, FS_KEY_DERIVATION_NONCE_SIZE); | ||
72 | if (res < 0) | ||
73 | goto out; | ||
74 | |||
75 | sg_init_one(&src_sg, master_key, derived_keysize); | ||
76 | sg_init_one(&dst_sg, derived_key, derived_keysize); | ||
77 | skcipher_request_set_crypt(req, &src_sg, &dst_sg, derived_keysize, | ||
78 | NULL); | ||
79 | res = crypto_wait_req(crypto_skcipher_encrypt(req), &wait); | ||
80 | out: | ||
81 | skcipher_request_free(req); | ||
82 | crypto_free_skcipher(tfm); | ||
83 | return res; | ||
84 | } | ||
85 | |||
86 | /* | ||
87 | * Search the current task's subscribed keyrings for a "logon" key with | ||
88 | * description prefix:descriptor, and if found acquire a read lock on it and | ||
89 | * return a pointer to its validated payload in *payload_ret. | ||
90 | */ | ||
91 | static struct key * | ||
92 | find_and_lock_process_key(const char *prefix, | ||
93 | const u8 descriptor[FSCRYPT_KEY_DESCRIPTOR_SIZE], | ||
94 | unsigned int min_keysize, | ||
95 | const struct fscrypt_key **payload_ret) | ||
96 | { | ||
97 | char *description; | ||
98 | struct key *key; | ||
99 | const struct user_key_payload *ukp; | ||
100 | const struct fscrypt_key *payload; | ||
101 | |||
102 | description = kasprintf(GFP_NOFS, "%s%*phN", prefix, | ||
103 | FSCRYPT_KEY_DESCRIPTOR_SIZE, descriptor); | ||
104 | if (!description) | ||
105 | return ERR_PTR(-ENOMEM); | ||
106 | |||
107 | key = request_key(&key_type_logon, description, NULL); | ||
108 | kfree(description); | ||
109 | if (IS_ERR(key)) | ||
110 | return key; | ||
111 | |||
112 | down_read(&key->sem); | ||
113 | ukp = user_key_payload_locked(key); | ||
114 | |||
115 | if (!ukp) /* was the key revoked before we acquired its semaphore? */ | ||
116 | goto invalid; | ||
117 | |||
118 | payload = (const struct fscrypt_key *)ukp->data; | ||
119 | |||
120 | if (ukp->datalen != sizeof(struct fscrypt_key) || | ||
121 | payload->size < 1 || payload->size > FSCRYPT_MAX_KEY_SIZE) { | ||
122 | fscrypt_warn(NULL, | ||
123 | "key with description '%s' has invalid payload", | ||
124 | key->description); | ||
125 | goto invalid; | ||
126 | } | ||
127 | |||
128 | if (payload->size < min_keysize) { | ||
129 | fscrypt_warn(NULL, | ||
130 | "key with description '%s' is too short (got %u bytes, need %u+ bytes)", | ||
131 | key->description, payload->size, min_keysize); | ||
132 | goto invalid; | ||
133 | } | ||
134 | |||
135 | *payload_ret = payload; | ||
136 | return key; | ||
137 | |||
138 | invalid: | ||
139 | up_read(&key->sem); | ||
140 | key_put(key); | ||
141 | return ERR_PTR(-ENOKEY); | ||
142 | } | ||
143 | |||
144 | /* Master key referenced by DIRECT_KEY policy */ | ||
145 | struct fscrypt_direct_key { | ||
146 | struct hlist_node dk_node; | ||
147 | refcount_t dk_refcount; | ||
148 | const struct fscrypt_mode *dk_mode; | ||
149 | struct crypto_skcipher *dk_ctfm; | ||
150 | u8 dk_descriptor[FSCRYPT_KEY_DESCRIPTOR_SIZE]; | ||
151 | u8 dk_raw[FSCRYPT_MAX_KEY_SIZE]; | ||
152 | }; | ||
153 | |||
154 | static void free_direct_key(struct fscrypt_direct_key *dk) | ||
155 | { | ||
156 | if (dk) { | ||
157 | crypto_free_skcipher(dk->dk_ctfm); | ||
158 | kzfree(dk); | ||
159 | } | ||
160 | } | ||
161 | |||
162 | void fscrypt_put_direct_key(struct fscrypt_direct_key *dk) | ||
163 | { | ||
164 | if (!refcount_dec_and_lock(&dk->dk_refcount, &fscrypt_direct_keys_lock)) | ||
165 | return; | ||
166 | hash_del(&dk->dk_node); | ||
167 | spin_unlock(&fscrypt_direct_keys_lock); | ||
168 | |||
169 | free_direct_key(dk); | ||
170 | } | ||
171 | |||
172 | /* | ||
173 | * Find/insert the given key into the fscrypt_direct_keys table. If found, it | ||
174 | * is returned with elevated refcount, and 'to_insert' is freed if non-NULL. If | ||
175 | * not found, 'to_insert' is inserted and returned if it's non-NULL; otherwise | ||
176 | * NULL is returned. | ||
177 | */ | ||
178 | static struct fscrypt_direct_key * | ||
179 | find_or_insert_direct_key(struct fscrypt_direct_key *to_insert, | ||
180 | const u8 *raw_key, const struct fscrypt_info *ci) | ||
181 | { | ||
182 | unsigned long hash_key; | ||
183 | struct fscrypt_direct_key *dk; | ||
184 | |||
185 | /* | ||
186 | * Careful: to avoid potentially leaking secret key bytes via timing | ||
187 | * information, we must key the hash table by descriptor rather than by | ||
188 | * raw key, and use crypto_memneq() when comparing raw keys. | ||
189 | */ | ||
190 | |||
191 | BUILD_BUG_ON(sizeof(hash_key) > FSCRYPT_KEY_DESCRIPTOR_SIZE); | ||
192 | memcpy(&hash_key, ci->ci_policy.v1.master_key_descriptor, | ||
193 | sizeof(hash_key)); | ||
194 | |||
195 | spin_lock(&fscrypt_direct_keys_lock); | ||
196 | hash_for_each_possible(fscrypt_direct_keys, dk, dk_node, hash_key) { | ||
197 | if (memcmp(ci->ci_policy.v1.master_key_descriptor, | ||
198 | dk->dk_descriptor, FSCRYPT_KEY_DESCRIPTOR_SIZE) != 0) | ||
199 | continue; | ||
200 | if (ci->ci_mode != dk->dk_mode) | ||
201 | continue; | ||
202 | if (crypto_memneq(raw_key, dk->dk_raw, ci->ci_mode->keysize)) | ||
203 | continue; | ||
204 | /* using existing tfm with same (descriptor, mode, raw_key) */ | ||
205 | refcount_inc(&dk->dk_refcount); | ||
206 | spin_unlock(&fscrypt_direct_keys_lock); | ||
207 | free_direct_key(to_insert); | ||
208 | return dk; | ||
209 | } | ||
210 | if (to_insert) | ||
211 | hash_add(fscrypt_direct_keys, &to_insert->dk_node, hash_key); | ||
212 | spin_unlock(&fscrypt_direct_keys_lock); | ||
213 | return to_insert; | ||
214 | } | ||
215 | |||
216 | /* Prepare to encrypt directly using the master key in the given mode */ | ||
217 | static struct fscrypt_direct_key * | ||
218 | fscrypt_get_direct_key(const struct fscrypt_info *ci, const u8 *raw_key) | ||
219 | { | ||
220 | struct fscrypt_direct_key *dk; | ||
221 | int err; | ||
222 | |||
223 | /* Is there already a tfm for this key? */ | ||
224 | dk = find_or_insert_direct_key(NULL, raw_key, ci); | ||
225 | if (dk) | ||
226 | return dk; | ||
227 | |||
228 | /* Nope, allocate one. */ | ||
229 | dk = kzalloc(sizeof(*dk), GFP_NOFS); | ||
230 | if (!dk) | ||
231 | return ERR_PTR(-ENOMEM); | ||
232 | refcount_set(&dk->dk_refcount, 1); | ||
233 | dk->dk_mode = ci->ci_mode; | ||
234 | dk->dk_ctfm = fscrypt_allocate_skcipher(ci->ci_mode, raw_key, | ||
235 | ci->ci_inode); | ||
236 | if (IS_ERR(dk->dk_ctfm)) { | ||
237 | err = PTR_ERR(dk->dk_ctfm); | ||
238 | dk->dk_ctfm = NULL; | ||
239 | goto err_free_dk; | ||
240 | } | ||
241 | memcpy(dk->dk_descriptor, ci->ci_policy.v1.master_key_descriptor, | ||
242 | FSCRYPT_KEY_DESCRIPTOR_SIZE); | ||
243 | memcpy(dk->dk_raw, raw_key, ci->ci_mode->keysize); | ||
244 | |||
245 | return find_or_insert_direct_key(dk, raw_key, ci); | ||
246 | |||
247 | err_free_dk: | ||
248 | free_direct_key(dk); | ||
249 | return ERR_PTR(err); | ||
250 | } | ||
251 | |||
252 | /* v1 policy, DIRECT_KEY: use the master key directly */ | ||
253 | static int setup_v1_file_key_direct(struct fscrypt_info *ci, | ||
254 | const u8 *raw_master_key) | ||
255 | { | ||
256 | const struct fscrypt_mode *mode = ci->ci_mode; | ||
257 | struct fscrypt_direct_key *dk; | ||
258 | |||
259 | if (!fscrypt_mode_supports_direct_key(mode)) { | ||
260 | fscrypt_warn(ci->ci_inode, | ||
261 | "Direct key mode not allowed with %s", | ||
262 | mode->friendly_name); | ||
263 | return -EINVAL; | ||
264 | } | ||
265 | |||
266 | if (ci->ci_policy.v1.contents_encryption_mode != | ||
267 | ci->ci_policy.v1.filenames_encryption_mode) { | ||
268 | fscrypt_warn(ci->ci_inode, | ||
269 | "Direct key mode not allowed with different contents and filenames modes"); | ||
270 | return -EINVAL; | ||
271 | } | ||
272 | |||
273 | /* ESSIV implies 16-byte IVs which implies !DIRECT_KEY */ | ||
274 | if (WARN_ON(mode->needs_essiv)) | ||
275 | return -EINVAL; | ||
276 | |||
277 | dk = fscrypt_get_direct_key(ci, raw_master_key); | ||
278 | if (IS_ERR(dk)) | ||
279 | return PTR_ERR(dk); | ||
280 | ci->ci_direct_key = dk; | ||
281 | ci->ci_ctfm = dk->dk_ctfm; | ||
282 | return 0; | ||
283 | } | ||
284 | |||
285 | /* v1 policy, !DIRECT_KEY: derive the file's encryption key */ | ||
286 | static int setup_v1_file_key_derived(struct fscrypt_info *ci, | ||
287 | const u8 *raw_master_key) | ||
288 | { | ||
289 | u8 *derived_key; | ||
290 | int err; | ||
291 | |||
292 | /* | ||
293 | * This cannot be a stack buffer because it will be passed to the | ||
294 | * scatterlist crypto API during derive_key_aes(). | ||
295 | */ | ||
296 | derived_key = kmalloc(ci->ci_mode->keysize, GFP_NOFS); | ||
297 | if (!derived_key) | ||
298 | return -ENOMEM; | ||
299 | |||
300 | err = derive_key_aes(raw_master_key, ci->ci_nonce, | ||
301 | derived_key, ci->ci_mode->keysize); | ||
302 | if (err) | ||
303 | goto out; | ||
304 | |||
305 | err = fscrypt_set_derived_key(ci, derived_key); | ||
306 | out: | ||
307 | kzfree(derived_key); | ||
308 | return err; | ||
309 | } | ||
310 | |||
311 | int fscrypt_setup_v1_file_key(struct fscrypt_info *ci, const u8 *raw_master_key) | ||
312 | { | ||
313 | if (ci->ci_policy.v1.flags & FSCRYPT_POLICY_FLAG_DIRECT_KEY) | ||
314 | return setup_v1_file_key_direct(ci, raw_master_key); | ||
315 | else | ||
316 | return setup_v1_file_key_derived(ci, raw_master_key); | ||
317 | } | ||
318 | |||
319 | int fscrypt_setup_v1_file_key_via_subscribed_keyrings(struct fscrypt_info *ci) | ||
320 | { | ||
321 | struct key *key; | ||
322 | const struct fscrypt_key *payload; | ||
323 | int err; | ||
324 | |||
325 | key = find_and_lock_process_key(FSCRYPT_KEY_DESC_PREFIX, | ||
326 | ci->ci_policy.v1.master_key_descriptor, | ||
327 | ci->ci_mode->keysize, &payload); | ||
328 | if (key == ERR_PTR(-ENOKEY) && ci->ci_inode->i_sb->s_cop->key_prefix) { | ||
329 | key = find_and_lock_process_key(ci->ci_inode->i_sb->s_cop->key_prefix, | ||
330 | ci->ci_policy.v1.master_key_descriptor, | ||
331 | ci->ci_mode->keysize, &payload); | ||
332 | } | ||
333 | if (IS_ERR(key)) | ||
334 | return PTR_ERR(key); | ||
335 | |||
336 | err = fscrypt_setup_v1_file_key(ci, payload->raw); | ||
337 | up_read(&key->sem); | ||
338 | key_put(key); | ||
339 | return err; | ||
340 | } | ||
diff --git a/fs/crypto/policy.c b/fs/crypto/policy.c index 4941fe8471ce..4072ba644595 100644 --- a/fs/crypto/policy.c +++ b/fs/crypto/policy.c | |||
@@ -5,8 +5,9 @@ | |||
5 | * Copyright (C) 2015, Google, Inc. | 5 | * Copyright (C) 2015, Google, Inc. |
6 | * Copyright (C) 2015, Motorola Mobility. | 6 | * Copyright (C) 2015, Motorola Mobility. |
7 | * | 7 | * |
8 | * Written by Michael Halcrow, 2015. | 8 | * Originally written by Michael Halcrow, 2015. |
9 | * Modified by Jaegeuk Kim, 2015. | 9 | * Modified by Jaegeuk Kim, 2015. |
10 | * Modified by Eric Biggers, 2019 for v2 policy support. | ||
10 | */ | 11 | */ |
11 | 12 | ||
12 | #include <linux/random.h> | 13 | #include <linux/random.h> |
@@ -14,70 +15,303 @@ | |||
14 | #include <linux/mount.h> | 15 | #include <linux/mount.h> |
15 | #include "fscrypt_private.h" | 16 | #include "fscrypt_private.h" |
16 | 17 | ||
17 | /* | 18 | /** |
18 | * check whether an encryption policy is consistent with an encryption context | 19 | * fscrypt_policies_equal - check whether two encryption policies are the same |
20 | * | ||
21 | * Return: %true if equal, else %false | ||
22 | */ | ||
23 | bool fscrypt_policies_equal(const union fscrypt_policy *policy1, | ||
24 | const union fscrypt_policy *policy2) | ||
25 | { | ||
26 | if (policy1->version != policy2->version) | ||
27 | return false; | ||
28 | |||
29 | return !memcmp(policy1, policy2, fscrypt_policy_size(policy1)); | ||
30 | } | ||
31 | |||
32 | /** | ||
33 | * fscrypt_supported_policy - check whether an encryption policy is supported | ||
34 | * | ||
35 | * Given an encryption policy, check whether all its encryption modes and other | ||
36 | * settings are supported by this kernel. (But we don't currently don't check | ||
37 | * for crypto API support here, so attempting to use an algorithm not configured | ||
38 | * into the crypto API will still fail later.) | ||
39 | * | ||
40 | * Return: %true if supported, else %false | ||
41 | */ | ||
42 | bool fscrypt_supported_policy(const union fscrypt_policy *policy_u, | ||
43 | const struct inode *inode) | ||
44 | { | ||
45 | switch (policy_u->version) { | ||
46 | case FSCRYPT_POLICY_V1: { | ||
47 | const struct fscrypt_policy_v1 *policy = &policy_u->v1; | ||
48 | |||
49 | if (!fscrypt_valid_enc_modes(policy->contents_encryption_mode, | ||
50 | policy->filenames_encryption_mode)) { | ||
51 | fscrypt_warn(inode, | ||
52 | "Unsupported encryption modes (contents %d, filenames %d)", | ||
53 | policy->contents_encryption_mode, | ||
54 | policy->filenames_encryption_mode); | ||
55 | return false; | ||
56 | } | ||
57 | |||
58 | if (policy->flags & ~FSCRYPT_POLICY_FLAGS_VALID) { | ||
59 | fscrypt_warn(inode, | ||
60 | "Unsupported encryption flags (0x%02x)", | ||
61 | policy->flags); | ||
62 | return false; | ||
63 | } | ||
64 | |||
65 | return true; | ||
66 | } | ||
67 | case FSCRYPT_POLICY_V2: { | ||
68 | const struct fscrypt_policy_v2 *policy = &policy_u->v2; | ||
69 | |||
70 | if (!fscrypt_valid_enc_modes(policy->contents_encryption_mode, | ||
71 | policy->filenames_encryption_mode)) { | ||
72 | fscrypt_warn(inode, | ||
73 | "Unsupported encryption modes (contents %d, filenames %d)", | ||
74 | policy->contents_encryption_mode, | ||
75 | policy->filenames_encryption_mode); | ||
76 | return false; | ||
77 | } | ||
78 | |||
79 | if (policy->flags & ~FSCRYPT_POLICY_FLAGS_VALID) { | ||
80 | fscrypt_warn(inode, | ||
81 | "Unsupported encryption flags (0x%02x)", | ||
82 | policy->flags); | ||
83 | return false; | ||
84 | } | ||
85 | |||
86 | if (memchr_inv(policy->__reserved, 0, | ||
87 | sizeof(policy->__reserved))) { | ||
88 | fscrypt_warn(inode, | ||
89 | "Reserved bits set in encryption policy"); | ||
90 | return false; | ||
91 | } | ||
92 | |||
93 | return true; | ||
94 | } | ||
95 | } | ||
96 | return false; | ||
97 | } | ||
98 | |||
99 | /** | ||
100 | * fscrypt_new_context_from_policy - create a new fscrypt_context from a policy | ||
101 | * | ||
102 | * Create an fscrypt_context for an inode that is being assigned the given | ||
103 | * encryption policy. A new nonce is randomly generated. | ||
104 | * | ||
105 | * Return: the size of the new context in bytes. | ||
19 | */ | 106 | */ |
20 | static bool is_encryption_context_consistent_with_policy( | 107 | static int fscrypt_new_context_from_policy(union fscrypt_context *ctx_u, |
21 | const struct fscrypt_context *ctx, | 108 | const union fscrypt_policy *policy_u) |
22 | const struct fscrypt_policy *policy) | ||
23 | { | 109 | { |
24 | return memcmp(ctx->master_key_descriptor, policy->master_key_descriptor, | 110 | memset(ctx_u, 0, sizeof(*ctx_u)); |
25 | FS_KEY_DESCRIPTOR_SIZE) == 0 && | 111 | |
26 | (ctx->flags == policy->flags) && | 112 | switch (policy_u->version) { |
27 | (ctx->contents_encryption_mode == | 113 | case FSCRYPT_POLICY_V1: { |
28 | policy->contents_encryption_mode) && | 114 | const struct fscrypt_policy_v1 *policy = &policy_u->v1; |
29 | (ctx->filenames_encryption_mode == | 115 | struct fscrypt_context_v1 *ctx = &ctx_u->v1; |
30 | policy->filenames_encryption_mode); | 116 | |
117 | ctx->version = FSCRYPT_CONTEXT_V1; | ||
118 | ctx->contents_encryption_mode = | ||
119 | policy->contents_encryption_mode; | ||
120 | ctx->filenames_encryption_mode = | ||
121 | policy->filenames_encryption_mode; | ||
122 | ctx->flags = policy->flags; | ||
123 | memcpy(ctx->master_key_descriptor, | ||
124 | policy->master_key_descriptor, | ||
125 | sizeof(ctx->master_key_descriptor)); | ||
126 | get_random_bytes(ctx->nonce, sizeof(ctx->nonce)); | ||
127 | return sizeof(*ctx); | ||
128 | } | ||
129 | case FSCRYPT_POLICY_V2: { | ||
130 | const struct fscrypt_policy_v2 *policy = &policy_u->v2; | ||
131 | struct fscrypt_context_v2 *ctx = &ctx_u->v2; | ||
132 | |||
133 | ctx->version = FSCRYPT_CONTEXT_V2; | ||
134 | ctx->contents_encryption_mode = | ||
135 | policy->contents_encryption_mode; | ||
136 | ctx->filenames_encryption_mode = | ||
137 | policy->filenames_encryption_mode; | ||
138 | ctx->flags = policy->flags; | ||
139 | memcpy(ctx->master_key_identifier, | ||
140 | policy->master_key_identifier, | ||
141 | sizeof(ctx->master_key_identifier)); | ||
142 | get_random_bytes(ctx->nonce, sizeof(ctx->nonce)); | ||
143 | return sizeof(*ctx); | ||
144 | } | ||
145 | } | ||
146 | BUG(); | ||
31 | } | 147 | } |
32 | 148 | ||
33 | static int create_encryption_context_from_policy(struct inode *inode, | 149 | /** |
34 | const struct fscrypt_policy *policy) | 150 | * fscrypt_policy_from_context - convert an fscrypt_context to an fscrypt_policy |
151 | * | ||
152 | * Given an fscrypt_context, build the corresponding fscrypt_policy. | ||
153 | * | ||
154 | * Return: 0 on success, or -EINVAL if the fscrypt_context has an unrecognized | ||
155 | * version number or size. | ||
156 | * | ||
157 | * This does *not* validate the settings within the policy itself, e.g. the | ||
158 | * modes, flags, and reserved bits. Use fscrypt_supported_policy() for that. | ||
159 | */ | ||
160 | int fscrypt_policy_from_context(union fscrypt_policy *policy_u, | ||
161 | const union fscrypt_context *ctx_u, | ||
162 | int ctx_size) | ||
35 | { | 163 | { |
36 | struct fscrypt_context ctx; | 164 | memset(policy_u, 0, sizeof(*policy_u)); |
165 | |||
166 | if (ctx_size <= 0 || ctx_size != fscrypt_context_size(ctx_u)) | ||
167 | return -EINVAL; | ||
168 | |||
169 | switch (ctx_u->version) { | ||
170 | case FSCRYPT_CONTEXT_V1: { | ||
171 | const struct fscrypt_context_v1 *ctx = &ctx_u->v1; | ||
172 | struct fscrypt_policy_v1 *policy = &policy_u->v1; | ||
173 | |||
174 | policy->version = FSCRYPT_POLICY_V1; | ||
175 | policy->contents_encryption_mode = | ||
176 | ctx->contents_encryption_mode; | ||
177 | policy->filenames_encryption_mode = | ||
178 | ctx->filenames_encryption_mode; | ||
179 | policy->flags = ctx->flags; | ||
180 | memcpy(policy->master_key_descriptor, | ||
181 | ctx->master_key_descriptor, | ||
182 | sizeof(policy->master_key_descriptor)); | ||
183 | return 0; | ||
184 | } | ||
185 | case FSCRYPT_CONTEXT_V2: { | ||
186 | const struct fscrypt_context_v2 *ctx = &ctx_u->v2; | ||
187 | struct fscrypt_policy_v2 *policy = &policy_u->v2; | ||
188 | |||
189 | policy->version = FSCRYPT_POLICY_V2; | ||
190 | policy->contents_encryption_mode = | ||
191 | ctx->contents_encryption_mode; | ||
192 | policy->filenames_encryption_mode = | ||
193 | ctx->filenames_encryption_mode; | ||
194 | policy->flags = ctx->flags; | ||
195 | memcpy(policy->__reserved, ctx->__reserved, | ||
196 | sizeof(policy->__reserved)); | ||
197 | memcpy(policy->master_key_identifier, | ||
198 | ctx->master_key_identifier, | ||
199 | sizeof(policy->master_key_identifier)); | ||
200 | return 0; | ||
201 | } | ||
202 | } | ||
203 | /* unreachable */ | ||
204 | return -EINVAL; | ||
205 | } | ||
206 | |||
207 | /* Retrieve an inode's encryption policy */ | ||
208 | static int fscrypt_get_policy(struct inode *inode, union fscrypt_policy *policy) | ||
209 | { | ||
210 | const struct fscrypt_info *ci; | ||
211 | union fscrypt_context ctx; | ||
212 | int ret; | ||
213 | |||
214 | ci = READ_ONCE(inode->i_crypt_info); | ||
215 | if (ci) { | ||
216 | /* key available, use the cached policy */ | ||
217 | *policy = ci->ci_policy; | ||
218 | return 0; | ||
219 | } | ||
220 | |||
221 | if (!IS_ENCRYPTED(inode)) | ||
222 | return -ENODATA; | ||
37 | 223 | ||
38 | ctx.format = FS_ENCRYPTION_CONTEXT_FORMAT_V1; | 224 | ret = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx)); |
39 | memcpy(ctx.master_key_descriptor, policy->master_key_descriptor, | 225 | if (ret < 0) |
40 | FS_KEY_DESCRIPTOR_SIZE); | 226 | return (ret == -ERANGE) ? -EINVAL : ret; |
41 | 227 | ||
42 | if (!fscrypt_valid_enc_modes(policy->contents_encryption_mode, | 228 | return fscrypt_policy_from_context(policy, &ctx, ret); |
43 | policy->filenames_encryption_mode)) | 229 | } |
230 | |||
231 | static int set_encryption_policy(struct inode *inode, | ||
232 | const union fscrypt_policy *policy) | ||
233 | { | ||
234 | union fscrypt_context ctx; | ||
235 | int ctxsize; | ||
236 | int err; | ||
237 | |||
238 | if (!fscrypt_supported_policy(policy, inode)) | ||
44 | return -EINVAL; | 239 | return -EINVAL; |
45 | 240 | ||
46 | if (policy->flags & ~FS_POLICY_FLAGS_VALID) | 241 | switch (policy->version) { |
242 | case FSCRYPT_POLICY_V1: | ||
243 | /* | ||
244 | * The original encryption policy version provided no way of | ||
245 | * verifying that the correct master key was supplied, which was | ||
246 | * insecure in scenarios where multiple users have access to the | ||
247 | * same encrypted files (even just read-only access). The new | ||
248 | * encryption policy version fixes this and also implies use of | ||
249 | * an improved key derivation function and allows non-root users | ||
250 | * to securely remove keys. So as long as compatibility with | ||
251 | * old kernels isn't required, it is recommended to use the new | ||
252 | * policy version for all new encrypted directories. | ||
253 | */ | ||
254 | pr_warn_once("%s (pid %d) is setting deprecated v1 encryption policy; recommend upgrading to v2.\n", | ||
255 | current->comm, current->pid); | ||
256 | break; | ||
257 | case FSCRYPT_POLICY_V2: | ||
258 | err = fscrypt_verify_key_added(inode->i_sb, | ||
259 | policy->v2.master_key_identifier); | ||
260 | if (err) | ||
261 | return err; | ||
262 | break; | ||
263 | default: | ||
264 | WARN_ON(1); | ||
47 | return -EINVAL; | 265 | return -EINVAL; |
266 | } | ||
48 | 267 | ||
49 | ctx.contents_encryption_mode = policy->contents_encryption_mode; | 268 | ctxsize = fscrypt_new_context_from_policy(&ctx, policy); |
50 | ctx.filenames_encryption_mode = policy->filenames_encryption_mode; | ||
51 | ctx.flags = policy->flags; | ||
52 | BUILD_BUG_ON(sizeof(ctx.nonce) != FS_KEY_DERIVATION_NONCE_SIZE); | ||
53 | get_random_bytes(ctx.nonce, FS_KEY_DERIVATION_NONCE_SIZE); | ||
54 | 269 | ||
55 | return inode->i_sb->s_cop->set_context(inode, &ctx, sizeof(ctx), NULL); | 270 | return inode->i_sb->s_cop->set_context(inode, &ctx, ctxsize, NULL); |
56 | } | 271 | } |
57 | 272 | ||
58 | int fscrypt_ioctl_set_policy(struct file *filp, const void __user *arg) | 273 | int fscrypt_ioctl_set_policy(struct file *filp, const void __user *arg) |
59 | { | 274 | { |
60 | struct fscrypt_policy policy; | 275 | union fscrypt_policy policy; |
276 | union fscrypt_policy existing_policy; | ||
61 | struct inode *inode = file_inode(filp); | 277 | struct inode *inode = file_inode(filp); |
278 | u8 version; | ||
279 | int size; | ||
62 | int ret; | 280 | int ret; |
63 | struct fscrypt_context ctx; | ||
64 | 281 | ||
65 | if (copy_from_user(&policy, arg, sizeof(policy))) | 282 | if (get_user(policy.version, (const u8 __user *)arg)) |
66 | return -EFAULT; | 283 | return -EFAULT; |
67 | 284 | ||
285 | size = fscrypt_policy_size(&policy); | ||
286 | if (size <= 0) | ||
287 | return -EINVAL; | ||
288 | |||
289 | /* | ||
290 | * We should just copy the remaining 'size - 1' bytes here, but a | ||
291 | * bizarre bug in gcc 7 and earlier (fixed by gcc r255731) causes gcc to | ||
292 | * think that size can be 0 here (despite the check above!) *and* that | ||
293 | * it's a compile-time constant. Thus it would think copy_from_user() | ||
294 | * is passed compile-time constant ULONG_MAX, causing the compile-time | ||
295 | * buffer overflow check to fail, breaking the build. This only occurred | ||
296 | * when building an i386 kernel with -Os and branch profiling enabled. | ||
297 | * | ||
298 | * Work around it by just copying the first byte again... | ||
299 | */ | ||
300 | version = policy.version; | ||
301 | if (copy_from_user(&policy, arg, size)) | ||
302 | return -EFAULT; | ||
303 | policy.version = version; | ||
304 | |||
68 | if (!inode_owner_or_capable(inode)) | 305 | if (!inode_owner_or_capable(inode)) |
69 | return -EACCES; | 306 | return -EACCES; |
70 | 307 | ||
71 | if (policy.version != 0) | ||
72 | return -EINVAL; | ||
73 | |||
74 | ret = mnt_want_write_file(filp); | 308 | ret = mnt_want_write_file(filp); |
75 | if (ret) | 309 | if (ret) |
76 | return ret; | 310 | return ret; |
77 | 311 | ||
78 | inode_lock(inode); | 312 | inode_lock(inode); |
79 | 313 | ||
80 | ret = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx)); | 314 | ret = fscrypt_get_policy(inode, &existing_policy); |
81 | if (ret == -ENODATA) { | 315 | if (ret == -ENODATA) { |
82 | if (!S_ISDIR(inode->i_mode)) | 316 | if (!S_ISDIR(inode->i_mode)) |
83 | ret = -ENOTDIR; | 317 | ret = -ENOTDIR; |
@@ -86,14 +320,10 @@ int fscrypt_ioctl_set_policy(struct file *filp, const void __user *arg) | |||
86 | else if (!inode->i_sb->s_cop->empty_dir(inode)) | 320 | else if (!inode->i_sb->s_cop->empty_dir(inode)) |
87 | ret = -ENOTEMPTY; | 321 | ret = -ENOTEMPTY; |
88 | else | 322 | else |
89 | ret = create_encryption_context_from_policy(inode, | 323 | ret = set_encryption_policy(inode, &policy); |
90 | &policy); | 324 | } else if (ret == -EINVAL || |
91 | } else if (ret == sizeof(ctx) && | 325 | (ret == 0 && !fscrypt_policies_equal(&policy, |
92 | is_encryption_context_consistent_with_policy(&ctx, | 326 | &existing_policy))) { |
93 | &policy)) { | ||
94 | /* The file already uses the same encryption policy. */ | ||
95 | ret = 0; | ||
96 | } else if (ret >= 0 || ret == -ERANGE) { | ||
97 | /* The file already uses a different encryption policy. */ | 327 | /* The file already uses a different encryption policy. */ |
98 | ret = -EEXIST; | 328 | ret = -EEXIST; |
99 | } | 329 | } |
@@ -105,37 +335,57 @@ int fscrypt_ioctl_set_policy(struct file *filp, const void __user *arg) | |||
105 | } | 335 | } |
106 | EXPORT_SYMBOL(fscrypt_ioctl_set_policy); | 336 | EXPORT_SYMBOL(fscrypt_ioctl_set_policy); |
107 | 337 | ||
338 | /* Original ioctl version; can only get the original policy version */ | ||
108 | int fscrypt_ioctl_get_policy(struct file *filp, void __user *arg) | 339 | int fscrypt_ioctl_get_policy(struct file *filp, void __user *arg) |
109 | { | 340 | { |
110 | struct inode *inode = file_inode(filp); | 341 | union fscrypt_policy policy; |
111 | struct fscrypt_context ctx; | 342 | int err; |
112 | struct fscrypt_policy policy; | ||
113 | int res; | ||
114 | 343 | ||
115 | if (!IS_ENCRYPTED(inode)) | 344 | err = fscrypt_get_policy(file_inode(filp), &policy); |
116 | return -ENODATA; | 345 | if (err) |
346 | return err; | ||
117 | 347 | ||
118 | res = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx)); | 348 | if (policy.version != FSCRYPT_POLICY_V1) |
119 | if (res < 0 && res != -ERANGE) | ||
120 | return res; | ||
121 | if (res != sizeof(ctx)) | ||
122 | return -EINVAL; | ||
123 | if (ctx.format != FS_ENCRYPTION_CONTEXT_FORMAT_V1) | ||
124 | return -EINVAL; | 349 | return -EINVAL; |
125 | 350 | ||
126 | policy.version = 0; | 351 | if (copy_to_user(arg, &policy, sizeof(policy.v1))) |
127 | policy.contents_encryption_mode = ctx.contents_encryption_mode; | ||
128 | policy.filenames_encryption_mode = ctx.filenames_encryption_mode; | ||
129 | policy.flags = ctx.flags; | ||
130 | memcpy(policy.master_key_descriptor, ctx.master_key_descriptor, | ||
131 | FS_KEY_DESCRIPTOR_SIZE); | ||
132 | |||
133 | if (copy_to_user(arg, &policy, sizeof(policy))) | ||
134 | return -EFAULT; | 352 | return -EFAULT; |
135 | return 0; | 353 | return 0; |
136 | } | 354 | } |
137 | EXPORT_SYMBOL(fscrypt_ioctl_get_policy); | 355 | EXPORT_SYMBOL(fscrypt_ioctl_get_policy); |
138 | 356 | ||
357 | /* Extended ioctl version; can get policies of any version */ | ||
358 | int fscrypt_ioctl_get_policy_ex(struct file *filp, void __user *uarg) | ||
359 | { | ||
360 | struct fscrypt_get_policy_ex_arg arg; | ||
361 | union fscrypt_policy *policy = (union fscrypt_policy *)&arg.policy; | ||
362 | size_t policy_size; | ||
363 | int err; | ||
364 | |||
365 | /* arg is policy_size, then policy */ | ||
366 | BUILD_BUG_ON(offsetof(typeof(arg), policy_size) != 0); | ||
367 | BUILD_BUG_ON(offsetofend(typeof(arg), policy_size) != | ||
368 | offsetof(typeof(arg), policy)); | ||
369 | BUILD_BUG_ON(sizeof(arg.policy) != sizeof(*policy)); | ||
370 | |||
371 | err = fscrypt_get_policy(file_inode(filp), policy); | ||
372 | if (err) | ||
373 | return err; | ||
374 | policy_size = fscrypt_policy_size(policy); | ||
375 | |||
376 | if (copy_from_user(&arg, uarg, sizeof(arg.policy_size))) | ||
377 | return -EFAULT; | ||
378 | |||
379 | if (policy_size > arg.policy_size) | ||
380 | return -EOVERFLOW; | ||
381 | arg.policy_size = policy_size; | ||
382 | |||
383 | if (copy_to_user(uarg, &arg, sizeof(arg.policy_size) + policy_size)) | ||
384 | return -EFAULT; | ||
385 | return 0; | ||
386 | } | ||
387 | EXPORT_SYMBOL_GPL(fscrypt_ioctl_get_policy_ex); | ||
388 | |||
139 | /** | 389 | /** |
140 | * fscrypt_has_permitted_context() - is a file's encryption policy permitted | 390 | * fscrypt_has_permitted_context() - is a file's encryption policy permitted |
141 | * within its directory? | 391 | * within its directory? |
@@ -157,10 +407,8 @@ EXPORT_SYMBOL(fscrypt_ioctl_get_policy); | |||
157 | */ | 407 | */ |
158 | int fscrypt_has_permitted_context(struct inode *parent, struct inode *child) | 408 | int fscrypt_has_permitted_context(struct inode *parent, struct inode *child) |
159 | { | 409 | { |
160 | const struct fscrypt_operations *cops = parent->i_sb->s_cop; | 410 | union fscrypt_policy parent_policy, child_policy; |
161 | const struct fscrypt_info *parent_ci, *child_ci; | 411 | int err; |
162 | struct fscrypt_context parent_ctx, child_ctx; | ||
163 | int res; | ||
164 | 412 | ||
165 | /* No restrictions on file types which are never encrypted */ | 413 | /* No restrictions on file types which are never encrypted */ |
166 | if (!S_ISREG(child->i_mode) && !S_ISDIR(child->i_mode) && | 414 | if (!S_ISREG(child->i_mode) && !S_ISDIR(child->i_mode) && |
@@ -190,41 +438,22 @@ int fscrypt_has_permitted_context(struct inode *parent, struct inode *child) | |||
190 | * In any case, if an unexpected error occurs, fall back to "forbidden". | 438 | * In any case, if an unexpected error occurs, fall back to "forbidden". |
191 | */ | 439 | */ |
192 | 440 | ||
193 | res = fscrypt_get_encryption_info(parent); | 441 | err = fscrypt_get_encryption_info(parent); |
194 | if (res) | 442 | if (err) |
195 | return 0; | 443 | return 0; |
196 | res = fscrypt_get_encryption_info(child); | 444 | err = fscrypt_get_encryption_info(child); |
197 | if (res) | 445 | if (err) |
198 | return 0; | 446 | return 0; |
199 | parent_ci = READ_ONCE(parent->i_crypt_info); | ||
200 | child_ci = READ_ONCE(child->i_crypt_info); | ||
201 | |||
202 | if (parent_ci && child_ci) { | ||
203 | return memcmp(parent_ci->ci_master_key_descriptor, | ||
204 | child_ci->ci_master_key_descriptor, | ||
205 | FS_KEY_DESCRIPTOR_SIZE) == 0 && | ||
206 | (parent_ci->ci_data_mode == child_ci->ci_data_mode) && | ||
207 | (parent_ci->ci_filename_mode == | ||
208 | child_ci->ci_filename_mode) && | ||
209 | (parent_ci->ci_flags == child_ci->ci_flags); | ||
210 | } | ||
211 | 447 | ||
212 | res = cops->get_context(parent, &parent_ctx, sizeof(parent_ctx)); | 448 | err = fscrypt_get_policy(parent, &parent_policy); |
213 | if (res != sizeof(parent_ctx)) | 449 | if (err) |
214 | return 0; | 450 | return 0; |
215 | 451 | ||
216 | res = cops->get_context(child, &child_ctx, sizeof(child_ctx)); | 452 | err = fscrypt_get_policy(child, &child_policy); |
217 | if (res != sizeof(child_ctx)) | 453 | if (err) |
218 | return 0; | 454 | return 0; |
219 | 455 | ||
220 | return memcmp(parent_ctx.master_key_descriptor, | 456 | return fscrypt_policies_equal(&parent_policy, &child_policy); |
221 | child_ctx.master_key_descriptor, | ||
222 | FS_KEY_DESCRIPTOR_SIZE) == 0 && | ||
223 | (parent_ctx.contents_encryption_mode == | ||
224 | child_ctx.contents_encryption_mode) && | ||
225 | (parent_ctx.filenames_encryption_mode == | ||
226 | child_ctx.filenames_encryption_mode) && | ||
227 | (parent_ctx.flags == child_ctx.flags); | ||
228 | } | 457 | } |
229 | EXPORT_SYMBOL(fscrypt_has_permitted_context); | 458 | EXPORT_SYMBOL(fscrypt_has_permitted_context); |
230 | 459 | ||
@@ -240,7 +469,8 @@ EXPORT_SYMBOL(fscrypt_has_permitted_context); | |||
240 | int fscrypt_inherit_context(struct inode *parent, struct inode *child, | 469 | int fscrypt_inherit_context(struct inode *parent, struct inode *child, |
241 | void *fs_data, bool preload) | 470 | void *fs_data, bool preload) |
242 | { | 471 | { |
243 | struct fscrypt_context ctx; | 472 | union fscrypt_context ctx; |
473 | int ctxsize; | ||
244 | struct fscrypt_info *ci; | 474 | struct fscrypt_info *ci; |
245 | int res; | 475 | int res; |
246 | 476 | ||
@@ -252,16 +482,10 @@ int fscrypt_inherit_context(struct inode *parent, struct inode *child, | |||
252 | if (ci == NULL) | 482 | if (ci == NULL) |
253 | return -ENOKEY; | 483 | return -ENOKEY; |
254 | 484 | ||
255 | ctx.format = FS_ENCRYPTION_CONTEXT_FORMAT_V1; | 485 | ctxsize = fscrypt_new_context_from_policy(&ctx, &ci->ci_policy); |
256 | ctx.contents_encryption_mode = ci->ci_data_mode; | 486 | |
257 | ctx.filenames_encryption_mode = ci->ci_filename_mode; | ||
258 | ctx.flags = ci->ci_flags; | ||
259 | memcpy(ctx.master_key_descriptor, ci->ci_master_key_descriptor, | ||
260 | FS_KEY_DESCRIPTOR_SIZE); | ||
261 | get_random_bytes(ctx.nonce, FS_KEY_DERIVATION_NONCE_SIZE); | ||
262 | BUILD_BUG_ON(sizeof(ctx) != FSCRYPT_SET_CONTEXT_MAX_SIZE); | 487 | BUILD_BUG_ON(sizeof(ctx) != FSCRYPT_SET_CONTEXT_MAX_SIZE); |
263 | res = parent->i_sb->s_cop->set_context(child, &ctx, | 488 | res = parent->i_sb->s_cop->set_context(child, &ctx, ctxsize, fs_data); |
264 | sizeof(ctx), fs_data); | ||
265 | if (res) | 489 | if (res) |
266 | return res; | 490 | return res; |
267 | return preload ? fscrypt_get_encryption_info(child): 0; | 491 | return preload ? fscrypt_get_encryption_info(child): 0; |
diff --git a/fs/ext4/ioctl.c b/fs/ext4/ioctl.c index 442f7ef873fc..5703d607f5af 100644 --- a/fs/ext4/ioctl.c +++ b/fs/ext4/ioctl.c | |||
@@ -1113,8 +1113,35 @@ resizefs_out: | |||
1113 | #endif | 1113 | #endif |
1114 | } | 1114 | } |
1115 | case EXT4_IOC_GET_ENCRYPTION_POLICY: | 1115 | case EXT4_IOC_GET_ENCRYPTION_POLICY: |
1116 | if (!ext4_has_feature_encrypt(sb)) | ||
1117 | return -EOPNOTSUPP; | ||
1116 | return fscrypt_ioctl_get_policy(filp, (void __user *)arg); | 1118 | return fscrypt_ioctl_get_policy(filp, (void __user *)arg); |
1117 | 1119 | ||
1120 | case FS_IOC_GET_ENCRYPTION_POLICY_EX: | ||
1121 | if (!ext4_has_feature_encrypt(sb)) | ||
1122 | return -EOPNOTSUPP; | ||
1123 | return fscrypt_ioctl_get_policy_ex(filp, (void __user *)arg); | ||
1124 | |||
1125 | case FS_IOC_ADD_ENCRYPTION_KEY: | ||
1126 | if (!ext4_has_feature_encrypt(sb)) | ||
1127 | return -EOPNOTSUPP; | ||
1128 | return fscrypt_ioctl_add_key(filp, (void __user *)arg); | ||
1129 | |||
1130 | case FS_IOC_REMOVE_ENCRYPTION_KEY: | ||
1131 | if (!ext4_has_feature_encrypt(sb)) | ||
1132 | return -EOPNOTSUPP; | ||
1133 | return fscrypt_ioctl_remove_key(filp, (void __user *)arg); | ||
1134 | |||
1135 | case FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS: | ||
1136 | if (!ext4_has_feature_encrypt(sb)) | ||
1137 | return -EOPNOTSUPP; | ||
1138 | return fscrypt_ioctl_remove_key_all_users(filp, | ||
1139 | (void __user *)arg); | ||
1140 | case FS_IOC_GET_ENCRYPTION_KEY_STATUS: | ||
1141 | if (!ext4_has_feature_encrypt(sb)) | ||
1142 | return -EOPNOTSUPP; | ||
1143 | return fscrypt_ioctl_get_key_status(filp, (void __user *)arg); | ||
1144 | |||
1118 | case EXT4_IOC_FSGETXATTR: | 1145 | case EXT4_IOC_FSGETXATTR: |
1119 | { | 1146 | { |
1120 | struct fsxattr fa; | 1147 | struct fsxattr fa; |
@@ -1231,6 +1258,11 @@ long ext4_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg) | |||
1231 | case EXT4_IOC_SET_ENCRYPTION_POLICY: | 1258 | case EXT4_IOC_SET_ENCRYPTION_POLICY: |
1232 | case EXT4_IOC_GET_ENCRYPTION_PWSALT: | 1259 | case EXT4_IOC_GET_ENCRYPTION_PWSALT: |
1233 | case EXT4_IOC_GET_ENCRYPTION_POLICY: | 1260 | case EXT4_IOC_GET_ENCRYPTION_POLICY: |
1261 | case FS_IOC_GET_ENCRYPTION_POLICY_EX: | ||
1262 | case FS_IOC_ADD_ENCRYPTION_KEY: | ||
1263 | case FS_IOC_REMOVE_ENCRYPTION_KEY: | ||
1264 | case FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS: | ||
1265 | case FS_IOC_GET_ENCRYPTION_KEY_STATUS: | ||
1234 | case EXT4_IOC_SHUTDOWN: | 1266 | case EXT4_IOC_SHUTDOWN: |
1235 | case FS_IOC_GETFSMAP: | 1267 | case FS_IOC_GETFSMAP: |
1236 | break; | 1268 | break; |
diff --git a/fs/ext4/super.c b/fs/ext4/super.c index 4079605d437a..757819139b8f 100644 --- a/fs/ext4/super.c +++ b/fs/ext4/super.c | |||
@@ -1107,6 +1107,9 @@ static int ext4_drop_inode(struct inode *inode) | |||
1107 | { | 1107 | { |
1108 | int drop = generic_drop_inode(inode); | 1108 | int drop = generic_drop_inode(inode); |
1109 | 1109 | ||
1110 | if (!drop) | ||
1111 | drop = fscrypt_drop_inode(inode); | ||
1112 | |||
1110 | trace_ext4_drop_inode(inode, drop); | 1113 | trace_ext4_drop_inode(inode, drop); |
1111 | return drop; | 1114 | return drop; |
1112 | } | 1115 | } |
diff --git a/fs/f2fs/file.c b/fs/f2fs/file.c index 3e58a6f697dd..6a7349f9ac15 100644 --- a/fs/f2fs/file.c +++ b/fs/f2fs/file.c | |||
@@ -2184,6 +2184,49 @@ out_err: | |||
2184 | return err; | 2184 | return err; |
2185 | } | 2185 | } |
2186 | 2186 | ||
2187 | static int f2fs_ioc_get_encryption_policy_ex(struct file *filp, | ||
2188 | unsigned long arg) | ||
2189 | { | ||
2190 | if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp)))) | ||
2191 | return -EOPNOTSUPP; | ||
2192 | |||
2193 | return fscrypt_ioctl_get_policy_ex(filp, (void __user *)arg); | ||
2194 | } | ||
2195 | |||
2196 | static int f2fs_ioc_add_encryption_key(struct file *filp, unsigned long arg) | ||
2197 | { | ||
2198 | if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp)))) | ||
2199 | return -EOPNOTSUPP; | ||
2200 | |||
2201 | return fscrypt_ioctl_add_key(filp, (void __user *)arg); | ||
2202 | } | ||
2203 | |||
2204 | static int f2fs_ioc_remove_encryption_key(struct file *filp, unsigned long arg) | ||
2205 | { | ||
2206 | if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp)))) | ||
2207 | return -EOPNOTSUPP; | ||
2208 | |||
2209 | return fscrypt_ioctl_remove_key(filp, (void __user *)arg); | ||
2210 | } | ||
2211 | |||
2212 | static int f2fs_ioc_remove_encryption_key_all_users(struct file *filp, | ||
2213 | unsigned long arg) | ||
2214 | { | ||
2215 | if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp)))) | ||
2216 | return -EOPNOTSUPP; | ||
2217 | |||
2218 | return fscrypt_ioctl_remove_key_all_users(filp, (void __user *)arg); | ||
2219 | } | ||
2220 | |||
2221 | static int f2fs_ioc_get_encryption_key_status(struct file *filp, | ||
2222 | unsigned long arg) | ||
2223 | { | ||
2224 | if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp)))) | ||
2225 | return -EOPNOTSUPP; | ||
2226 | |||
2227 | return fscrypt_ioctl_get_key_status(filp, (void __user *)arg); | ||
2228 | } | ||
2229 | |||
2187 | static int f2fs_ioc_gc(struct file *filp, unsigned long arg) | 2230 | static int f2fs_ioc_gc(struct file *filp, unsigned long arg) |
2188 | { | 2231 | { |
2189 | struct inode *inode = file_inode(filp); | 2232 | struct inode *inode = file_inode(filp); |
@@ -3092,6 +3135,16 @@ long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) | |||
3092 | return f2fs_ioc_get_encryption_policy(filp, arg); | 3135 | return f2fs_ioc_get_encryption_policy(filp, arg); |
3093 | case F2FS_IOC_GET_ENCRYPTION_PWSALT: | 3136 | case F2FS_IOC_GET_ENCRYPTION_PWSALT: |
3094 | return f2fs_ioc_get_encryption_pwsalt(filp, arg); | 3137 | return f2fs_ioc_get_encryption_pwsalt(filp, arg); |
3138 | case FS_IOC_GET_ENCRYPTION_POLICY_EX: | ||
3139 | return f2fs_ioc_get_encryption_policy_ex(filp, arg); | ||
3140 | case FS_IOC_ADD_ENCRYPTION_KEY: | ||
3141 | return f2fs_ioc_add_encryption_key(filp, arg); | ||
3142 | case FS_IOC_REMOVE_ENCRYPTION_KEY: | ||
3143 | return f2fs_ioc_remove_encryption_key(filp, arg); | ||
3144 | case FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS: | ||
3145 | return f2fs_ioc_remove_encryption_key_all_users(filp, arg); | ||
3146 | case FS_IOC_GET_ENCRYPTION_KEY_STATUS: | ||
3147 | return f2fs_ioc_get_encryption_key_status(filp, arg); | ||
3095 | case F2FS_IOC_GARBAGE_COLLECT: | 3148 | case F2FS_IOC_GARBAGE_COLLECT: |
3096 | return f2fs_ioc_gc(filp, arg); | 3149 | return f2fs_ioc_gc(filp, arg); |
3097 | case F2FS_IOC_GARBAGE_COLLECT_RANGE: | 3150 | case F2FS_IOC_GARBAGE_COLLECT_RANGE: |
@@ -3219,6 +3272,11 @@ long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg) | |||
3219 | case F2FS_IOC_SET_ENCRYPTION_POLICY: | 3272 | case F2FS_IOC_SET_ENCRYPTION_POLICY: |
3220 | case F2FS_IOC_GET_ENCRYPTION_PWSALT: | 3273 | case F2FS_IOC_GET_ENCRYPTION_PWSALT: |
3221 | case F2FS_IOC_GET_ENCRYPTION_POLICY: | 3274 | case F2FS_IOC_GET_ENCRYPTION_POLICY: |
3275 | case FS_IOC_GET_ENCRYPTION_POLICY_EX: | ||
3276 | case FS_IOC_ADD_ENCRYPTION_KEY: | ||
3277 | case FS_IOC_REMOVE_ENCRYPTION_KEY: | ||
3278 | case FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS: | ||
3279 | case FS_IOC_GET_ENCRYPTION_KEY_STATUS: | ||
3222 | case F2FS_IOC_GARBAGE_COLLECT: | 3280 | case F2FS_IOC_GARBAGE_COLLECT: |
3223 | case F2FS_IOC_GARBAGE_COLLECT_RANGE: | 3281 | case F2FS_IOC_GARBAGE_COLLECT_RANGE: |
3224 | case F2FS_IOC_WRITE_CHECKPOINT: | 3282 | case F2FS_IOC_WRITE_CHECKPOINT: |
diff --git a/fs/f2fs/super.c b/fs/f2fs/super.c index 78a1b873e48a..e15bd29bd453 100644 --- a/fs/f2fs/super.c +++ b/fs/f2fs/super.c | |||
@@ -913,6 +913,8 @@ static int f2fs_drop_inode(struct inode *inode) | |||
913 | return 0; | 913 | return 0; |
914 | } | 914 | } |
915 | ret = generic_drop_inode(inode); | 915 | ret = generic_drop_inode(inode); |
916 | if (!ret) | ||
917 | ret = fscrypt_drop_inode(inode); | ||
916 | trace_f2fs_drop_inode(inode, ret); | 918 | trace_f2fs_drop_inode(inode, ret); |
917 | return ret; | 919 | return ret; |
918 | } | 920 | } |
diff --git a/fs/super.c b/fs/super.c index da223b4cfbca..9459ba75a32e 100644 --- a/fs/super.c +++ b/fs/super.c | |||
@@ -32,6 +32,7 @@ | |||
32 | #include <linux/backing-dev.h> | 32 | #include <linux/backing-dev.h> |
33 | #include <linux/rculist_bl.h> | 33 | #include <linux/rculist_bl.h> |
34 | #include <linux/cleancache.h> | 34 | #include <linux/cleancache.h> |
35 | #include <linux/fscrypt.h> | ||
35 | #include <linux/fsnotify.h> | 36 | #include <linux/fsnotify.h> |
36 | #include <linux/lockdep.h> | 37 | #include <linux/lockdep.h> |
37 | #include <linux/user_namespace.h> | 38 | #include <linux/user_namespace.h> |
@@ -290,6 +291,7 @@ static void __put_super(struct super_block *s) | |||
290 | WARN_ON(s->s_inode_lru.node); | 291 | WARN_ON(s->s_inode_lru.node); |
291 | WARN_ON(!list_empty(&s->s_mounts)); | 292 | WARN_ON(!list_empty(&s->s_mounts)); |
292 | security_sb_free(s); | 293 | security_sb_free(s); |
294 | fscrypt_sb_free(s); | ||
293 | put_user_ns(s->s_user_ns); | 295 | put_user_ns(s->s_user_ns); |
294 | kfree(s->s_subtype); | 296 | kfree(s->s_subtype); |
295 | call_rcu(&s->rcu, destroy_super_rcu); | 297 | call_rcu(&s->rcu, destroy_super_rcu); |
diff --git a/fs/ubifs/ioctl.c b/fs/ubifs/ioctl.c index 034ad14710d1..5dc5abca11c7 100644 --- a/fs/ubifs/ioctl.c +++ b/fs/ubifs/ioctl.c | |||
@@ -185,6 +185,21 @@ long ubifs_ioctl(struct file *file, unsigned int cmd, unsigned long arg) | |||
185 | case FS_IOC_GET_ENCRYPTION_POLICY: | 185 | case FS_IOC_GET_ENCRYPTION_POLICY: |
186 | return fscrypt_ioctl_get_policy(file, (void __user *)arg); | 186 | return fscrypt_ioctl_get_policy(file, (void __user *)arg); |
187 | 187 | ||
188 | case FS_IOC_GET_ENCRYPTION_POLICY_EX: | ||
189 | return fscrypt_ioctl_get_policy_ex(file, (void __user *)arg); | ||
190 | |||
191 | case FS_IOC_ADD_ENCRYPTION_KEY: | ||
192 | return fscrypt_ioctl_add_key(file, (void __user *)arg); | ||
193 | |||
194 | case FS_IOC_REMOVE_ENCRYPTION_KEY: | ||
195 | return fscrypt_ioctl_remove_key(file, (void __user *)arg); | ||
196 | |||
197 | case FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS: | ||
198 | return fscrypt_ioctl_remove_key_all_users(file, | ||
199 | (void __user *)arg); | ||
200 | case FS_IOC_GET_ENCRYPTION_KEY_STATUS: | ||
201 | return fscrypt_ioctl_get_key_status(file, (void __user *)arg); | ||
202 | |||
188 | default: | 203 | default: |
189 | return -ENOTTY; | 204 | return -ENOTTY; |
190 | } | 205 | } |
@@ -202,6 +217,11 @@ long ubifs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg) | |||
202 | break; | 217 | break; |
203 | case FS_IOC_SET_ENCRYPTION_POLICY: | 218 | case FS_IOC_SET_ENCRYPTION_POLICY: |
204 | case FS_IOC_GET_ENCRYPTION_POLICY: | 219 | case FS_IOC_GET_ENCRYPTION_POLICY: |
220 | case FS_IOC_GET_ENCRYPTION_POLICY_EX: | ||
221 | case FS_IOC_ADD_ENCRYPTION_KEY: | ||
222 | case FS_IOC_REMOVE_ENCRYPTION_KEY: | ||
223 | case FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS: | ||
224 | case FS_IOC_GET_ENCRYPTION_KEY_STATUS: | ||
205 | break; | 225 | break; |
206 | default: | 226 | default: |
207 | return -ENOIOCTLCMD; | 227 | return -ENOIOCTLCMD; |
diff --git a/fs/ubifs/super.c b/fs/ubifs/super.c index 8c1d571334bc..5e1e8ec0589e 100644 --- a/fs/ubifs/super.c +++ b/fs/ubifs/super.c | |||
@@ -318,6 +318,16 @@ static int ubifs_write_inode(struct inode *inode, struct writeback_control *wbc) | |||
318 | return err; | 318 | return err; |
319 | } | 319 | } |
320 | 320 | ||
321 | static int ubifs_drop_inode(struct inode *inode) | ||
322 | { | ||
323 | int drop = generic_drop_inode(inode); | ||
324 | |||
325 | if (!drop) | ||
326 | drop = fscrypt_drop_inode(inode); | ||
327 | |||
328 | return drop; | ||
329 | } | ||
330 | |||
321 | static void ubifs_evict_inode(struct inode *inode) | 331 | static void ubifs_evict_inode(struct inode *inode) |
322 | { | 332 | { |
323 | int err; | 333 | int err; |
@@ -1994,6 +2004,7 @@ const struct super_operations ubifs_super_operations = { | |||
1994 | .free_inode = ubifs_free_inode, | 2004 | .free_inode = ubifs_free_inode, |
1995 | .put_super = ubifs_put_super, | 2005 | .put_super = ubifs_put_super, |
1996 | .write_inode = ubifs_write_inode, | 2006 | .write_inode = ubifs_write_inode, |
2007 | .drop_inode = ubifs_drop_inode, | ||
1997 | .evict_inode = ubifs_evict_inode, | 2008 | .evict_inode = ubifs_evict_inode, |
1998 | .statfs = ubifs_statfs, | 2009 | .statfs = ubifs_statfs, |
1999 | .dirty_inode = ubifs_dirty_inode, | 2010 | .dirty_inode = ubifs_dirty_inode, |
diff --git a/include/linux/fs.h b/include/linux/fs.h index 997a530ff4e9..5dff77326cec 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h | |||
@@ -1427,6 +1427,7 @@ struct super_block { | |||
1427 | const struct xattr_handler **s_xattr; | 1427 | const struct xattr_handler **s_xattr; |
1428 | #ifdef CONFIG_FS_ENCRYPTION | 1428 | #ifdef CONFIG_FS_ENCRYPTION |
1429 | const struct fscrypt_operations *s_cop; | 1429 | const struct fscrypt_operations *s_cop; |
1430 | struct key *s_master_keys; /* master crypto keys in use */ | ||
1430 | #endif | 1431 | #endif |
1431 | struct hlist_bl_head s_roots; /* alternate root dentries for NFS */ | 1432 | struct hlist_bl_head s_roots; /* alternate root dentries for NFS */ |
1432 | struct list_head s_mounts; /* list of mounts; _not_ for fs use */ | 1433 | struct list_head s_mounts; /* list of mounts; _not_ for fs use */ |
diff --git a/include/linux/fscrypt.h b/include/linux/fscrypt.h index bd8f207a2fb6..f622f7460ed8 100644 --- a/include/linux/fscrypt.h +++ b/include/linux/fscrypt.h | |||
@@ -16,6 +16,7 @@ | |||
16 | #include <linux/fs.h> | 16 | #include <linux/fs.h> |
17 | #include <linux/mm.h> | 17 | #include <linux/mm.h> |
18 | #include <linux/slab.h> | 18 | #include <linux/slab.h> |
19 | #include <uapi/linux/fscrypt.h> | ||
19 | 20 | ||
20 | #define FS_CRYPTO_BLOCK_SIZE 16 | 21 | #define FS_CRYPTO_BLOCK_SIZE 16 |
21 | 22 | ||
@@ -42,7 +43,7 @@ struct fscrypt_name { | |||
42 | #define fname_len(p) ((p)->disk_name.len) | 43 | #define fname_len(p) ((p)->disk_name.len) |
43 | 44 | ||
44 | /* Maximum value for the third parameter of fscrypt_operations.set_context(). */ | 45 | /* Maximum value for the third parameter of fscrypt_operations.set_context(). */ |
45 | #define FSCRYPT_SET_CONTEXT_MAX_SIZE 28 | 46 | #define FSCRYPT_SET_CONTEXT_MAX_SIZE 40 |
46 | 47 | ||
47 | #ifdef CONFIG_FS_ENCRYPTION | 48 | #ifdef CONFIG_FS_ENCRYPTION |
48 | /* | 49 | /* |
@@ -134,13 +135,23 @@ extern void fscrypt_free_bounce_page(struct page *bounce_page); | |||
134 | /* policy.c */ | 135 | /* policy.c */ |
135 | extern int fscrypt_ioctl_set_policy(struct file *, const void __user *); | 136 | extern int fscrypt_ioctl_set_policy(struct file *, const void __user *); |
136 | extern int fscrypt_ioctl_get_policy(struct file *, void __user *); | 137 | extern int fscrypt_ioctl_get_policy(struct file *, void __user *); |
138 | extern int fscrypt_ioctl_get_policy_ex(struct file *, void __user *); | ||
137 | extern int fscrypt_has_permitted_context(struct inode *, struct inode *); | 139 | extern int fscrypt_has_permitted_context(struct inode *, struct inode *); |
138 | extern int fscrypt_inherit_context(struct inode *, struct inode *, | 140 | extern int fscrypt_inherit_context(struct inode *, struct inode *, |
139 | void *, bool); | 141 | void *, bool); |
140 | /* keyinfo.c */ | 142 | /* keyring.c */ |
143 | extern void fscrypt_sb_free(struct super_block *sb); | ||
144 | extern int fscrypt_ioctl_add_key(struct file *filp, void __user *arg); | ||
145 | extern int fscrypt_ioctl_remove_key(struct file *filp, void __user *arg); | ||
146 | extern int fscrypt_ioctl_remove_key_all_users(struct file *filp, | ||
147 | void __user *arg); | ||
148 | extern int fscrypt_ioctl_get_key_status(struct file *filp, void __user *arg); | ||
149 | |||
150 | /* keysetup.c */ | ||
141 | extern int fscrypt_get_encryption_info(struct inode *); | 151 | extern int fscrypt_get_encryption_info(struct inode *); |
142 | extern void fscrypt_put_encryption_info(struct inode *); | 152 | extern void fscrypt_put_encryption_info(struct inode *); |
143 | extern void fscrypt_free_inode(struct inode *); | 153 | extern void fscrypt_free_inode(struct inode *); |
154 | extern int fscrypt_drop_inode(struct inode *inode); | ||
144 | 155 | ||
145 | /* fname.c */ | 156 | /* fname.c */ |
146 | extern int fscrypt_setup_filename(struct inode *, const struct qstr *, | 157 | extern int fscrypt_setup_filename(struct inode *, const struct qstr *, |
@@ -353,6 +364,12 @@ static inline int fscrypt_ioctl_get_policy(struct file *filp, void __user *arg) | |||
353 | return -EOPNOTSUPP; | 364 | return -EOPNOTSUPP; |
354 | } | 365 | } |
355 | 366 | ||
367 | static inline int fscrypt_ioctl_get_policy_ex(struct file *filp, | ||
368 | void __user *arg) | ||
369 | { | ||
370 | return -EOPNOTSUPP; | ||
371 | } | ||
372 | |||
356 | static inline int fscrypt_has_permitted_context(struct inode *parent, | 373 | static inline int fscrypt_has_permitted_context(struct inode *parent, |
357 | struct inode *child) | 374 | struct inode *child) |
358 | { | 375 | { |
@@ -366,7 +383,34 @@ static inline int fscrypt_inherit_context(struct inode *parent, | |||
366 | return -EOPNOTSUPP; | 383 | return -EOPNOTSUPP; |
367 | } | 384 | } |
368 | 385 | ||
369 | /* keyinfo.c */ | 386 | /* keyring.c */ |
387 | static inline void fscrypt_sb_free(struct super_block *sb) | ||
388 | { | ||
389 | } | ||
390 | |||
391 | static inline int fscrypt_ioctl_add_key(struct file *filp, void __user *arg) | ||
392 | { | ||
393 | return -EOPNOTSUPP; | ||
394 | } | ||
395 | |||
396 | static inline int fscrypt_ioctl_remove_key(struct file *filp, void __user *arg) | ||
397 | { | ||
398 | return -EOPNOTSUPP; | ||
399 | } | ||
400 | |||
401 | static inline int fscrypt_ioctl_remove_key_all_users(struct file *filp, | ||
402 | void __user *arg) | ||
403 | { | ||
404 | return -EOPNOTSUPP; | ||
405 | } | ||
406 | |||
407 | static inline int fscrypt_ioctl_get_key_status(struct file *filp, | ||
408 | void __user *arg) | ||
409 | { | ||
410 | return -EOPNOTSUPP; | ||
411 | } | ||
412 | |||
413 | /* keysetup.c */ | ||
370 | static inline int fscrypt_get_encryption_info(struct inode *inode) | 414 | static inline int fscrypt_get_encryption_info(struct inode *inode) |
371 | { | 415 | { |
372 | return -EOPNOTSUPP; | 416 | return -EOPNOTSUPP; |
@@ -381,6 +425,11 @@ static inline void fscrypt_free_inode(struct inode *inode) | |||
381 | { | 425 | { |
382 | } | 426 | } |
383 | 427 | ||
428 | static inline int fscrypt_drop_inode(struct inode *inode) | ||
429 | { | ||
430 | return 0; | ||
431 | } | ||
432 | |||
384 | /* fname.c */ | 433 | /* fname.c */ |
385 | static inline int fscrypt_setup_filename(struct inode *dir, | 434 | static inline int fscrypt_setup_filename(struct inode *dir, |
386 | const struct qstr *iname, | 435 | const struct qstr *iname, |
diff --git a/include/uapi/linux/fs.h b/include/uapi/linux/fs.h index 59c71fa8c553..41bd84d25a98 100644 --- a/include/uapi/linux/fs.h +++ b/include/uapi/linux/fs.h | |||
@@ -13,6 +13,9 @@ | |||
13 | #include <linux/limits.h> | 13 | #include <linux/limits.h> |
14 | #include <linux/ioctl.h> | 14 | #include <linux/ioctl.h> |
15 | #include <linux/types.h> | 15 | #include <linux/types.h> |
16 | #ifndef __KERNEL__ | ||
17 | #include <linux/fscrypt.h> | ||
18 | #endif | ||
16 | 19 | ||
17 | /* Use of MS_* flags within the kernel is restricted to core mount(2) code. */ | 20 | /* Use of MS_* flags within the kernel is restricted to core mount(2) code. */ |
18 | #if !defined(__KERNEL__) | 21 | #if !defined(__KERNEL__) |
@@ -213,57 +216,6 @@ struct fsxattr { | |||
213 | #define FS_IOC_SETFSLABEL _IOW(0x94, 50, char[FSLABEL_MAX]) | 216 | #define FS_IOC_SETFSLABEL _IOW(0x94, 50, char[FSLABEL_MAX]) |
214 | 217 | ||
215 | /* | 218 | /* |
216 | * File system encryption support | ||
217 | */ | ||
218 | /* Policy provided via an ioctl on the topmost directory */ | ||
219 | #define FS_KEY_DESCRIPTOR_SIZE 8 | ||
220 | |||
221 | #define FS_POLICY_FLAGS_PAD_4 0x00 | ||
222 | #define FS_POLICY_FLAGS_PAD_8 0x01 | ||
223 | #define FS_POLICY_FLAGS_PAD_16 0x02 | ||
224 | #define FS_POLICY_FLAGS_PAD_32 0x03 | ||
225 | #define FS_POLICY_FLAGS_PAD_MASK 0x03 | ||
226 | #define FS_POLICY_FLAG_DIRECT_KEY 0x04 /* use master key directly */ | ||
227 | #define FS_POLICY_FLAGS_VALID 0x07 | ||
228 | |||
229 | /* Encryption algorithms */ | ||
230 | #define FS_ENCRYPTION_MODE_INVALID 0 | ||
231 | #define FS_ENCRYPTION_MODE_AES_256_XTS 1 | ||
232 | #define FS_ENCRYPTION_MODE_AES_256_GCM 2 | ||
233 | #define FS_ENCRYPTION_MODE_AES_256_CBC 3 | ||
234 | #define FS_ENCRYPTION_MODE_AES_256_CTS 4 | ||
235 | #define FS_ENCRYPTION_MODE_AES_128_CBC 5 | ||
236 | #define FS_ENCRYPTION_MODE_AES_128_CTS 6 | ||
237 | #define FS_ENCRYPTION_MODE_SPECK128_256_XTS 7 /* Removed, do not use. */ | ||
238 | #define FS_ENCRYPTION_MODE_SPECK128_256_CTS 8 /* Removed, do not use. */ | ||
239 | #define FS_ENCRYPTION_MODE_ADIANTUM 9 | ||
240 | |||
241 | struct fscrypt_policy { | ||
242 | __u8 version; | ||
243 | __u8 contents_encryption_mode; | ||
244 | __u8 filenames_encryption_mode; | ||
245 | __u8 flags; | ||
246 | __u8 master_key_descriptor[FS_KEY_DESCRIPTOR_SIZE]; | ||
247 | }; | ||
248 | |||
249 | #define FS_IOC_SET_ENCRYPTION_POLICY _IOR('f', 19, struct fscrypt_policy) | ||
250 | #define FS_IOC_GET_ENCRYPTION_PWSALT _IOW('f', 20, __u8[16]) | ||
251 | #define FS_IOC_GET_ENCRYPTION_POLICY _IOW('f', 21, struct fscrypt_policy) | ||
252 | |||
253 | /* Parameters for passing an encryption key into the kernel keyring */ | ||
254 | #define FS_KEY_DESC_PREFIX "fscrypt:" | ||
255 | #define FS_KEY_DESC_PREFIX_SIZE 8 | ||
256 | |||
257 | /* Structure that userspace passes to the kernel keyring */ | ||
258 | #define FS_MAX_KEY_SIZE 64 | ||
259 | |||
260 | struct fscrypt_key { | ||
261 | __u32 mode; | ||
262 | __u8 raw[FS_MAX_KEY_SIZE]; | ||
263 | __u32 size; | ||
264 | }; | ||
265 | |||
266 | /* | ||
267 | * Inode flags (FS_IOC_GETFLAGS / FS_IOC_SETFLAGS) | 219 | * Inode flags (FS_IOC_GETFLAGS / FS_IOC_SETFLAGS) |
268 | * | 220 | * |
269 | * Note: for historical reasons, these flags were originally used and | 221 | * Note: for historical reasons, these flags were originally used and |
diff --git a/include/uapi/linux/fscrypt.h b/include/uapi/linux/fscrypt.h new file mode 100644 index 000000000000..39ccfe9311c3 --- /dev/null +++ b/include/uapi/linux/fscrypt.h | |||
@@ -0,0 +1,181 @@ | |||
1 | /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ | ||
2 | /* | ||
3 | * fscrypt user API | ||
4 | * | ||
5 | * These ioctls can be used on filesystems that support fscrypt. See the | ||
6 | * "User API" section of Documentation/filesystems/fscrypt.rst. | ||
7 | */ | ||
8 | #ifndef _UAPI_LINUX_FSCRYPT_H | ||
9 | #define _UAPI_LINUX_FSCRYPT_H | ||
10 | |||
11 | #include <linux/types.h> | ||
12 | |||
13 | /* Encryption policy flags */ | ||
14 | #define FSCRYPT_POLICY_FLAGS_PAD_4 0x00 | ||
15 | #define FSCRYPT_POLICY_FLAGS_PAD_8 0x01 | ||
16 | #define FSCRYPT_POLICY_FLAGS_PAD_16 0x02 | ||
17 | #define FSCRYPT_POLICY_FLAGS_PAD_32 0x03 | ||
18 | #define FSCRYPT_POLICY_FLAGS_PAD_MASK 0x03 | ||
19 | #define FSCRYPT_POLICY_FLAG_DIRECT_KEY 0x04 | ||
20 | #define FSCRYPT_POLICY_FLAGS_VALID 0x07 | ||
21 | |||
22 | /* Encryption algorithms */ | ||
23 | #define FSCRYPT_MODE_AES_256_XTS 1 | ||
24 | #define FSCRYPT_MODE_AES_256_CTS 4 | ||
25 | #define FSCRYPT_MODE_AES_128_CBC 5 | ||
26 | #define FSCRYPT_MODE_AES_128_CTS 6 | ||
27 | #define FSCRYPT_MODE_ADIANTUM 9 | ||
28 | #define __FSCRYPT_MODE_MAX 9 | ||
29 | |||
30 | /* | ||
31 | * Legacy policy version; ad-hoc KDF and no key verification. | ||
32 | * For new encrypted directories, use fscrypt_policy_v2 instead. | ||
33 | * | ||
34 | * Careful: the .version field for this is actually 0, not 1. | ||
35 | */ | ||
36 | #define FSCRYPT_POLICY_V1 0 | ||
37 | #define FSCRYPT_KEY_DESCRIPTOR_SIZE 8 | ||
38 | struct fscrypt_policy_v1 { | ||
39 | __u8 version; | ||
40 | __u8 contents_encryption_mode; | ||
41 | __u8 filenames_encryption_mode; | ||
42 | __u8 flags; | ||
43 | __u8 master_key_descriptor[FSCRYPT_KEY_DESCRIPTOR_SIZE]; | ||
44 | }; | ||
45 | #define fscrypt_policy fscrypt_policy_v1 | ||
46 | |||
47 | /* | ||
48 | * Process-subscribed "logon" key description prefix and payload format. | ||
49 | * Deprecated; prefer FS_IOC_ADD_ENCRYPTION_KEY instead. | ||
50 | */ | ||
51 | #define FSCRYPT_KEY_DESC_PREFIX "fscrypt:" | ||
52 | #define FSCRYPT_KEY_DESC_PREFIX_SIZE 8 | ||
53 | #define FSCRYPT_MAX_KEY_SIZE 64 | ||
54 | struct fscrypt_key { | ||
55 | __u32 mode; | ||
56 | __u8 raw[FSCRYPT_MAX_KEY_SIZE]; | ||
57 | __u32 size; | ||
58 | }; | ||
59 | |||
60 | /* | ||
61 | * New policy version with HKDF and key verification (recommended). | ||
62 | */ | ||
63 | #define FSCRYPT_POLICY_V2 2 | ||
64 | #define FSCRYPT_KEY_IDENTIFIER_SIZE 16 | ||
65 | struct fscrypt_policy_v2 { | ||
66 | __u8 version; | ||
67 | __u8 contents_encryption_mode; | ||
68 | __u8 filenames_encryption_mode; | ||
69 | __u8 flags; | ||
70 | __u8 __reserved[4]; | ||
71 | __u8 master_key_identifier[FSCRYPT_KEY_IDENTIFIER_SIZE]; | ||
72 | }; | ||
73 | |||
74 | /* Struct passed to FS_IOC_GET_ENCRYPTION_POLICY_EX */ | ||
75 | struct fscrypt_get_policy_ex_arg { | ||
76 | __u64 policy_size; /* input/output */ | ||
77 | union { | ||
78 | __u8 version; | ||
79 | struct fscrypt_policy_v1 v1; | ||
80 | struct fscrypt_policy_v2 v2; | ||
81 | } policy; /* output */ | ||
82 | }; | ||
83 | |||
84 | /* | ||
85 | * v1 policy keys are specified by an arbitrary 8-byte key "descriptor", | ||
86 | * matching fscrypt_policy_v1::master_key_descriptor. | ||
87 | */ | ||
88 | #define FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR 1 | ||
89 | |||
90 | /* | ||
91 | * v2 policy keys are specified by a 16-byte key "identifier" which the kernel | ||
92 | * calculates as a cryptographic hash of the key itself, | ||
93 | * matching fscrypt_policy_v2::master_key_identifier. | ||
94 | */ | ||
95 | #define FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER 2 | ||
96 | |||
97 | /* | ||
98 | * Specifies a key, either for v1 or v2 policies. This doesn't contain the | ||
99 | * actual key itself; this is just the "name" of the key. | ||
100 | */ | ||
101 | struct fscrypt_key_specifier { | ||
102 | __u32 type; /* one of FSCRYPT_KEY_SPEC_TYPE_* */ | ||
103 | __u32 __reserved; | ||
104 | union { | ||
105 | __u8 __reserved[32]; /* reserve some extra space */ | ||
106 | __u8 descriptor[FSCRYPT_KEY_DESCRIPTOR_SIZE]; | ||
107 | __u8 identifier[FSCRYPT_KEY_IDENTIFIER_SIZE]; | ||
108 | } u; | ||
109 | }; | ||
110 | |||
111 | /* Struct passed to FS_IOC_ADD_ENCRYPTION_KEY */ | ||
112 | struct fscrypt_add_key_arg { | ||
113 | struct fscrypt_key_specifier key_spec; | ||
114 | __u32 raw_size; | ||
115 | __u32 __reserved[9]; | ||
116 | __u8 raw[]; | ||
117 | }; | ||
118 | |||
119 | /* Struct passed to FS_IOC_REMOVE_ENCRYPTION_KEY */ | ||
120 | struct fscrypt_remove_key_arg { | ||
121 | struct fscrypt_key_specifier key_spec; | ||
122 | #define FSCRYPT_KEY_REMOVAL_STATUS_FLAG_FILES_BUSY 0x00000001 | ||
123 | #define FSCRYPT_KEY_REMOVAL_STATUS_FLAG_OTHER_USERS 0x00000002 | ||
124 | __u32 removal_status_flags; /* output */ | ||
125 | __u32 __reserved[5]; | ||
126 | }; | ||
127 | |||
128 | /* Struct passed to FS_IOC_GET_ENCRYPTION_KEY_STATUS */ | ||
129 | struct fscrypt_get_key_status_arg { | ||
130 | /* input */ | ||
131 | struct fscrypt_key_specifier key_spec; | ||
132 | __u32 __reserved[6]; | ||
133 | |||
134 | /* output */ | ||
135 | #define FSCRYPT_KEY_STATUS_ABSENT 1 | ||
136 | #define FSCRYPT_KEY_STATUS_PRESENT 2 | ||
137 | #define FSCRYPT_KEY_STATUS_INCOMPLETELY_REMOVED 3 | ||
138 | __u32 status; | ||
139 | #define FSCRYPT_KEY_STATUS_FLAG_ADDED_BY_SELF 0x00000001 | ||
140 | __u32 status_flags; | ||
141 | __u32 user_count; | ||
142 | __u32 __out_reserved[13]; | ||
143 | }; | ||
144 | |||
145 | #define FS_IOC_SET_ENCRYPTION_POLICY _IOR('f', 19, struct fscrypt_policy) | ||
146 | #define FS_IOC_GET_ENCRYPTION_PWSALT _IOW('f', 20, __u8[16]) | ||
147 | #define FS_IOC_GET_ENCRYPTION_POLICY _IOW('f', 21, struct fscrypt_policy) | ||
148 | #define FS_IOC_GET_ENCRYPTION_POLICY_EX _IOWR('f', 22, __u8[9]) /* size + version */ | ||
149 | #define FS_IOC_ADD_ENCRYPTION_KEY _IOWR('f', 23, struct fscrypt_add_key_arg) | ||
150 | #define FS_IOC_REMOVE_ENCRYPTION_KEY _IOWR('f', 24, struct fscrypt_remove_key_arg) | ||
151 | #define FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS _IOWR('f', 25, struct fscrypt_remove_key_arg) | ||
152 | #define FS_IOC_GET_ENCRYPTION_KEY_STATUS _IOWR('f', 26, struct fscrypt_get_key_status_arg) | ||
153 | |||
154 | /**********************************************************************/ | ||
155 | |||
156 | /* old names; don't add anything new here! */ | ||
157 | #ifndef __KERNEL__ | ||
158 | #define FS_KEY_DESCRIPTOR_SIZE FSCRYPT_KEY_DESCRIPTOR_SIZE | ||
159 | #define FS_POLICY_FLAGS_PAD_4 FSCRYPT_POLICY_FLAGS_PAD_4 | ||
160 | #define FS_POLICY_FLAGS_PAD_8 FSCRYPT_POLICY_FLAGS_PAD_8 | ||
161 | #define FS_POLICY_FLAGS_PAD_16 FSCRYPT_POLICY_FLAGS_PAD_16 | ||
162 | #define FS_POLICY_FLAGS_PAD_32 FSCRYPT_POLICY_FLAGS_PAD_32 | ||
163 | #define FS_POLICY_FLAGS_PAD_MASK FSCRYPT_POLICY_FLAGS_PAD_MASK | ||
164 | #define FS_POLICY_FLAG_DIRECT_KEY FSCRYPT_POLICY_FLAG_DIRECT_KEY | ||
165 | #define FS_POLICY_FLAGS_VALID FSCRYPT_POLICY_FLAGS_VALID | ||
166 | #define FS_ENCRYPTION_MODE_INVALID 0 /* never used */ | ||
167 | #define FS_ENCRYPTION_MODE_AES_256_XTS FSCRYPT_MODE_AES_256_XTS | ||
168 | #define FS_ENCRYPTION_MODE_AES_256_GCM 2 /* never used */ | ||
169 | #define FS_ENCRYPTION_MODE_AES_256_CBC 3 /* never used */ | ||
170 | #define FS_ENCRYPTION_MODE_AES_256_CTS FSCRYPT_MODE_AES_256_CTS | ||
171 | #define FS_ENCRYPTION_MODE_AES_128_CBC FSCRYPT_MODE_AES_128_CBC | ||
172 | #define FS_ENCRYPTION_MODE_AES_128_CTS FSCRYPT_MODE_AES_128_CTS | ||
173 | #define FS_ENCRYPTION_MODE_SPECK128_256_XTS 7 /* removed */ | ||
174 | #define FS_ENCRYPTION_MODE_SPECK128_256_CTS 8 /* removed */ | ||
175 | #define FS_ENCRYPTION_MODE_ADIANTUM FSCRYPT_MODE_ADIANTUM | ||
176 | #define FS_KEY_DESC_PREFIX FSCRYPT_KEY_DESC_PREFIX | ||
177 | #define FS_KEY_DESC_PREFIX_SIZE FSCRYPT_KEY_DESC_PREFIX_SIZE | ||
178 | #define FS_MAX_KEY_SIZE FSCRYPT_MAX_KEY_SIZE | ||
179 | #endif /* !__KERNEL__ */ | ||
180 | |||
181 | #endif /* _UAPI_LINUX_FSCRYPT_H */ | ||