diff options
| -rw-r--r-- | Documentation/filesystems/fscrypt.rst | 179 | ||||
| -rw-r--r-- | fs/crypto/crypto.c | 28 | ||||
| -rw-r--r-- | fs/crypto/fname.c | 22 | ||||
| -rw-r--r-- | fs/crypto/fscrypt_private.h | 67 | ||||
| -rw-r--r-- | fs/crypto/keyinfo.c | 351 | ||||
| -rw-r--r-- | fs/crypto/policy.c | 5 | ||||
| -rw-r--r-- | include/uapi/linux/fs.h | 4 |
7 files changed, 468 insertions, 188 deletions
diff --git a/Documentation/filesystems/fscrypt.rst b/Documentation/filesystems/fscrypt.rst index cfbc18f0d9c9..3a7b60521b94 100644 --- a/Documentation/filesystems/fscrypt.rst +++ b/Documentation/filesystems/fscrypt.rst | |||
| @@ -132,47 +132,28 @@ designed for this purpose be used, such as scrypt, PBKDF2, or Argon2. | |||
| 132 | Per-file keys | 132 | Per-file keys |
| 133 | ------------- | 133 | ------------- |
| 134 | 134 | ||
| 135 | Master keys are not used to encrypt file contents or names directly. | 135 | Since each master key can protect many files, it is necessary to |
| 136 | Instead, a unique key is derived for each encrypted file, including | 136 | "tweak" the encryption of each file so that the same plaintext in two |
| 137 | each regular file, directory, and symbolic link. This has several | 137 | files doesn't map to the same ciphertext, or vice versa. In most |
| 138 | advantages: | 138 | cases, fscrypt does this by deriving per-file keys. When a new |
| 139 | 139 | encrypted inode (regular file, directory, or symlink) is created, | |
| 140 | - In cryptosystems, the same key material should never be used for | 140 | fscrypt randomly generates a 16-byte nonce and stores it in the |
| 141 | different purposes. Using the master key as both an XTS key for | 141 | inode's encryption xattr. Then, it uses a KDF (Key Derivation |
| 142 | contents encryption and as a CTS-CBC key for filenames encryption | 142 | Function) to derive the file's key from the master key and nonce. |
| 143 | would violate this rule. | 143 | |
| 144 | - Per-file keys simplify the choice of IVs (Initialization Vectors) | 144 | The Adiantum encryption mode (see `Encryption modes and usage`_) is |
| 145 | for contents encryption. Without per-file keys, to ensure IV | 145 | special, since it accepts longer IVs and is suitable for both contents |
| 146 | uniqueness both the inode and logical block number would need to be | 146 | and filenames encryption. For it, a "direct key" option is offered |
| 147 | encoded in the IVs. This would make it impossible to renumber | 147 | where the file's nonce is included in the IVs and the master key is |
| 148 | inodes, which e.g. ``resize2fs`` can do when resizing an ext4 | 148 | used for encryption directly. This improves performance; however, |
| 149 | filesystem. With per-file keys, it is sufficient to encode just the | 149 | users must not use the same master key for any other encryption mode. |
| 150 | logical block number in the IVs. | 150 | |
| 151 | - Per-file keys strengthen the encryption of filenames, where IVs are | 151 | Below, the KDF and design considerations are described in more detail. |
| 152 | reused out of necessity. With a unique key per directory, IV reuse | 152 | |
| 153 | is limited to within a single directory. | 153 | The current KDF works by encrypting the master key with AES-128-ECB, |
| 154 | - Per-file keys allow individual files to be securely erased simply by | 154 | using the file's nonce as the AES key. The output is used as the |
| 155 | securely erasing their keys. (Not yet implemented.) | 155 | derived key. If the output is longer than needed, then it is |
| 156 | 156 | truncated to the needed length. | |
| 157 | A KDF (Key Derivation Function) is used to derive per-file keys from | ||
| 158 | the master key. This is done instead of wrapping a randomly-generated | ||
| 159 | key for each file because it reduces the size of the encryption xattr, | ||
| 160 | which for some filesystems makes the xattr more likely to fit in-line | ||
| 161 | in the filesystem's inode table. With a KDF, only a 16-byte nonce is | ||
| 162 | required --- long enough to make key reuse extremely unlikely. A | ||
| 163 | wrapped key, on the other hand, would need to be up to 64 bytes --- | ||
| 164 | the length of an AES-256-XTS key. Furthermore, currently there is no | ||
| 165 | requirement to support unlocking a file with multiple alternative | ||
| 166 | master keys or to support rotating master keys. Instead, the master | ||
| 167 | keys may be wrapped in userspace, e.g. as done by the `fscrypt | ||
| 168 | <https://github.com/google/fscrypt>`_ tool. | ||
| 169 | |||
| 170 | The current KDF encrypts the master key using the 16-byte nonce as an | ||
| 171 | AES-128-ECB key. The output is used as the derived key. If the | ||
| 172 | output is longer than needed, then it is truncated to the needed | ||
| 173 | length. Truncation is the norm for directories and symlinks, since | ||
| 174 | those use the CTS-CBC encryption mode which requires a key half as | ||
| 175 | long as that required by the XTS encryption mode. | ||
| 176 | 157 | ||
| 177 | Note: this KDF meets the primary security requirement, which is to | 158 | Note: this KDF meets the primary security requirement, which is to |
| 178 | produce unique derived keys that preserve the entropy of the master | 159 | produce unique derived keys that preserve the entropy of the master |
| @@ -181,6 +162,20 @@ However, it is nonstandard and has some problems such as being | |||
| 181 | reversible, so it is generally considered to be a mistake! It may be | 162 | reversible, so it is generally considered to be a mistake! It may be |
| 182 | replaced with HKDF or another more standard KDF in the future. | 163 | replaced with HKDF or another more standard KDF in the future. |
| 183 | 164 | ||
| 165 | 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 | ||
| 167 | filesystem's inode table, and there didn't appear to be any | ||
| 168 | significant advantages to key wrapping. In particular, currently | ||
| 169 | there is no requirement to support unlocking a file with multiple | ||
| 170 | alternative master keys or to support rotating master keys. Instead, | ||
| 171 | the master keys may be wrapped in userspace, e.g. as is done by the | ||
| 172 | `fscrypt <https://github.com/google/fscrypt>`_ tool. | ||
| 173 | |||
| 174 | Including the inode number in the IVs was considered. However, it was | ||
| 175 | rejected as it would have prevented ext4 filesystems from being | ||
| 176 | 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. | ||
| 178 | |||
| 184 | Encryption modes and usage | 179 | Encryption modes and usage |
| 185 | ========================== | 180 | ========================== |
| 186 | 181 | ||
| @@ -191,54 +186,80 @@ Currently, the following pairs of encryption modes are supported: | |||
| 191 | 186 | ||
| 192 | - AES-256-XTS for contents and AES-256-CTS-CBC for filenames | 187 | - AES-256-XTS for contents and AES-256-CTS-CBC for filenames |
| 193 | - AES-128-CBC for contents and AES-128-CTS-CBC for filenames | 188 | - AES-128-CBC for contents and AES-128-CTS-CBC for filenames |
| 189 | - Adiantum for both contents and filenames | ||
| 190 | |||
| 191 | If unsure, you should use the (AES-256-XTS, AES-256-CTS-CBC) pair. | ||
| 194 | 192 | ||
| 195 | It is strongly recommended to use AES-256-XTS for contents encryption. | ||
| 196 | AES-128-CBC was added only for low-powered embedded devices with | 193 | AES-128-CBC was added only for low-powered embedded devices with |
| 197 | crypto accelerators such as CAAM or CESA that do not support XTS. | 194 | crypto accelerators such as CAAM or CESA that do not support XTS. |
| 198 | 195 | ||
| 196 | Adiantum is a (primarily) stream cipher-based mode that is fast even | ||
| 197 | on CPUs without dedicated crypto instructions. It's also a true | ||
| 198 | wide-block mode, unlike XTS. It can also eliminate the need to derive | ||
| 199 | per-file keys. However, it depends on the security of two primitives, | ||
| 200 | XChaCha12 and AES-256, rather than just one. See the paper | ||
| 201 | "Adiantum: length-preserving encryption for entry-level processors" | ||
| 202 | (https://eprint.iacr.org/2018/720.pdf) for more details. To use | ||
| 203 | Adiantum, CONFIG_CRYPTO_ADIANTUM must be enabled. Also, fast | ||
| 204 | implementations of ChaCha and NHPoly1305 should be enabled, e.g. | ||
| 205 | CONFIG_CRYPTO_CHACHA20_NEON and CONFIG_CRYPTO_NHPOLY1305_NEON for ARM. | ||
| 206 | |||
| 199 | New encryption modes can be added relatively easily, without changes | 207 | New encryption modes can be added relatively easily, without changes |
| 200 | to individual filesystems. However, authenticated encryption (AE) | 208 | to individual filesystems. However, authenticated encryption (AE) |
| 201 | modes are not currently supported because of the difficulty of dealing | 209 | modes are not currently supported because of the difficulty of dealing |
| 202 | with ciphertext expansion. | 210 | with ciphertext expansion. |
| 203 | 211 | ||
| 212 | Contents encryption | ||
| 213 | ------------------- | ||
| 214 | |||
| 204 | For file contents, each filesystem block is encrypted independently. | 215 | For file contents, each filesystem block is encrypted independently. |
| 205 | Currently, only the case where the filesystem block size is equal to | 216 | Currently, only the case where the filesystem block size is equal to |
| 206 | the system's page size (usually 4096 bytes) is supported. With the | 217 | the system's page size (usually 4096 bytes) is supported. |
| 207 | XTS mode of operation (recommended), the logical block number within | 218 | |
| 208 | the file is used as the IV. With the CBC mode of operation (not | 219 | Each block's IV is set to the logical block number within the file as |
| 209 | recommended), ESSIV is used; specifically, the IV for CBC is the | 220 | a little endian number, except that: |
| 210 | logical block number encrypted with AES-256, where the AES-256 key is | 221 | |
| 211 | the SHA-256 hash of the inode's data encryption key. | 222 | - With CBC mode encryption, ESSIV is also used. Specifically, each IV |
| 212 | 223 | is encrypted with AES-256 where the AES-256 key is the SHA-256 hash | |
| 213 | For filenames, the full filename is encrypted at once. Because of the | 224 | of the file's data encryption key. |
| 214 | requirements to retain support for efficient directory lookups and | 225 | |
| 215 | filenames of up to 255 bytes, a constant initialization vector (IV) is | 226 | - In the "direct key" configuration (FS_POLICY_FLAG_DIRECT_KEY set in |
| 216 | used. However, each encrypted directory uses a unique key, which | 227 | the fscrypt_policy), the file's nonce is also appended to the IV. |
| 217 | limits IV reuse to within a single directory. Note that IV reuse in | 228 | Currently this is only allowed with the Adiantum encryption mode. |
| 218 | the context of CTS-CBC encryption means that when the original | 229 | |
| 219 | filenames share a common prefix at least as long as the cipher block | 230 | Filenames encryption |
| 220 | size (16 bytes for AES), the corresponding encrypted filenames will | 231 | -------------------- |
| 221 | also share a common prefix. This is undesirable; it may be fixed in | 232 | |
| 222 | the future by switching to an encryption mode that is a strong | 233 | For filenames, each full filename is encrypted at once. Because of |
| 223 | pseudorandom permutation on arbitrary-length messages, e.g. the HEH | 234 | the requirements to retain support for efficient directory lookups and |
| 224 | (Hash-Encrypt-Hash) mode. | 235 | filenames of up to 255 bytes, the same IV is used for every filename |
| 225 | 236 | in a directory. | |
| 226 | Since filenames are encrypted with the CTS-CBC mode of operation, the | 237 | |
| 227 | plaintext and ciphertext filenames need not be multiples of the AES | 238 | However, each encrypted directory still uses a unique key; or |
| 228 | block size, i.e. 16 bytes. However, the minimum size that can be | 239 | alternatively (for the "direct key" configuration) has the file's |
| 229 | encrypted is 16 bytes, so shorter filenames are NUL-padded to 16 bytes | 240 | nonce included in the IVs. Thus, IV reuse is limited to within a |
| 230 | before being encrypted. In addition, to reduce leakage of filename | 241 | single directory. |
| 231 | lengths via their ciphertexts, all filenames are NUL-padded to the | 242 | |
| 232 | next 4, 8, 16, or 32-byte boundary (configurable). 32 is recommended | 243 | With CTS-CBC, the IV reuse means that when the plaintext filenames |
| 233 | since this provides the best confidentiality, at the cost of making | 244 | share a common prefix at least as long as the cipher block size (16 |
| 234 | directory entries consume slightly more space. Note that since NUL | 245 | bytes for AES), the corresponding encrypted filenames will also share |
| 235 | (``\0``) is not otherwise a valid character in filenames, the padding | 246 | a common prefix. This is undesirable. Adiantum does not have this |
| 236 | will never produce duplicate plaintexts. | 247 | weakness, as it is a wide-block encryption mode. |
| 248 | |||
| 249 | All supported filenames encryption modes accept any plaintext length | ||
| 250 | >= 16 bytes; cipher block alignment is not required. However, | ||
| 251 | filenames shorter than 16 bytes are NUL-padded to 16 bytes before | ||
| 252 | being encrypted. In addition, to reduce leakage of filename lengths | ||
| 253 | via their ciphertexts, all filenames are NUL-padded to the next 4, 8, | ||
| 254 | 16, or 32-byte boundary (configurable). 32 is recommended since this | ||
| 255 | provides the best confidentiality, at the cost of making directory | ||
| 256 | entries consume slightly more space. Note that since NUL (``\0``) is | ||
| 257 | not otherwise a valid character in filenames, the padding will never | ||
| 258 | produce duplicate plaintexts. | ||
| 237 | 259 | ||
| 238 | Symbolic link targets are considered a type of filename and are | 260 | Symbolic link targets are considered a type of filename and are |
| 239 | encrypted in the same way as filenames in directory entries. Each | 261 | encrypted in the same way as filenames in directory entries, except |
| 240 | symlink also uses a unique key; hence, the hardcoded IV is not a | 262 | that IV reuse is not a problem as each symlink has its own inode. |
| 241 | problem for symlinks. | ||
| 242 | 263 | ||
| 243 | User API | 264 | User API |
| 244 | ======== | 265 | ======== |
| @@ -272,9 +293,13 @@ This structure must be initialized as follows: | |||
| 272 | and FS_ENCRYPTION_MODE_AES_256_CTS (4) for | 293 | and FS_ENCRYPTION_MODE_AES_256_CTS (4) for |
| 273 | ``filenames_encryption_mode``. | 294 | ``filenames_encryption_mode``. |
| 274 | 295 | ||
| 275 | - ``flags`` must be set to a value from ``<linux/fs.h>`` which | 296 | - ``flags`` must contain a value from ``<linux/fs.h>`` which |
| 276 | identifies the amount of NUL-padding to use when encrypting | 297 | identifies the amount of NUL-padding to use when encrypting |
| 277 | filenames. If unsure, use FS_POLICY_FLAGS_PAD_32 (0x3). | 298 | filenames. If unsure, use FS_POLICY_FLAGS_PAD_32 (0x3). |
| 299 | In addition, if the chosen encryption modes are both | ||
| 300 | FS_ENCRYPTION_MODE_ADIANTUM, this can contain | ||
| 301 | FS_POLICY_FLAG_DIRECT_KEY to specify that the master key should be | ||
| 302 | used directly, without key derivation. | ||
| 278 | 303 | ||
| 279 | - ``master_key_descriptor`` specifies how to find the master key in | 304 | - ``master_key_descriptor`` specifies how to find the master key in |
| 280 | the keyring; see `Adding keys`_. It is up to userspace to choose a | 305 | the keyring; see `Adding keys`_. It is up to userspace to choose a |
diff --git a/fs/crypto/crypto.c b/fs/crypto/crypto.c index 0f46cf550907..4dc788e3bc96 100644 --- a/fs/crypto/crypto.c +++ b/fs/crypto/crypto.c | |||
| @@ -133,15 +133,25 @@ struct fscrypt_ctx *fscrypt_get_ctx(const struct inode *inode, gfp_t gfp_flags) | |||
| 133 | } | 133 | } |
| 134 | EXPORT_SYMBOL(fscrypt_get_ctx); | 134 | EXPORT_SYMBOL(fscrypt_get_ctx); |
| 135 | 135 | ||
| 136 | void fscrypt_generate_iv(union fscrypt_iv *iv, u64 lblk_num, | ||
| 137 | const struct fscrypt_info *ci) | ||
| 138 | { | ||
| 139 | memset(iv, 0, ci->ci_mode->ivsize); | ||
| 140 | iv->lblk_num = cpu_to_le64(lblk_num); | ||
| 141 | |||
| 142 | if (ci->ci_flags & FS_POLICY_FLAG_DIRECT_KEY) | ||
| 143 | memcpy(iv->nonce, ci->ci_nonce, FS_KEY_DERIVATION_NONCE_SIZE); | ||
| 144 | |||
| 145 | if (ci->ci_essiv_tfm != NULL) | ||
| 146 | crypto_cipher_encrypt_one(ci->ci_essiv_tfm, iv->raw, iv->raw); | ||
| 147 | } | ||
| 148 | |||
| 136 | int fscrypt_do_page_crypto(const struct inode *inode, fscrypt_direction_t rw, | 149 | int fscrypt_do_page_crypto(const struct inode *inode, fscrypt_direction_t rw, |
| 137 | u64 lblk_num, struct page *src_page, | 150 | u64 lblk_num, struct page *src_page, |
| 138 | struct page *dest_page, unsigned int len, | 151 | struct page *dest_page, unsigned int len, |
| 139 | unsigned int offs, gfp_t gfp_flags) | 152 | unsigned int offs, gfp_t gfp_flags) |
| 140 | { | 153 | { |
| 141 | struct { | 154 | union fscrypt_iv iv; |
| 142 | __le64 index; | ||
| 143 | u8 padding[FS_IV_SIZE - sizeof(__le64)]; | ||
| 144 | } iv; | ||
| 145 | struct skcipher_request *req = NULL; | 155 | struct skcipher_request *req = NULL; |
| 146 | DECLARE_CRYPTO_WAIT(wait); | 156 | DECLARE_CRYPTO_WAIT(wait); |
| 147 | struct scatterlist dst, src; | 157 | struct scatterlist dst, src; |
| @@ -151,15 +161,7 @@ int fscrypt_do_page_crypto(const struct inode *inode, fscrypt_direction_t rw, | |||
| 151 | 161 | ||
| 152 | BUG_ON(len == 0); | 162 | BUG_ON(len == 0); |
| 153 | 163 | ||
| 154 | BUILD_BUG_ON(sizeof(iv) != FS_IV_SIZE); | 164 | fscrypt_generate_iv(&iv, lblk_num, ci); |
| 155 | BUILD_BUG_ON(AES_BLOCK_SIZE != FS_IV_SIZE); | ||
| 156 | iv.index = cpu_to_le64(lblk_num); | ||
| 157 | memset(iv.padding, 0, sizeof(iv.padding)); | ||
| 158 | |||
| 159 | if (ci->ci_essiv_tfm != NULL) { | ||
| 160 | crypto_cipher_encrypt_one(ci->ci_essiv_tfm, (u8 *)&iv, | ||
| 161 | (u8 *)&iv); | ||
| 162 | } | ||
| 163 | 165 | ||
| 164 | req = skcipher_request_alloc(tfm, gfp_flags); | 166 | req = skcipher_request_alloc(tfm, gfp_flags); |
| 165 | if (!req) | 167 | if (!req) |
diff --git a/fs/crypto/fname.c b/fs/crypto/fname.c index d7a0f682ca12..7ff40a73dbec 100644 --- a/fs/crypto/fname.c +++ b/fs/crypto/fname.c | |||
| @@ -40,10 +40,11 @@ int fname_encrypt(struct inode *inode, const struct qstr *iname, | |||
| 40 | { | 40 | { |
| 41 | struct skcipher_request *req = NULL; | 41 | struct skcipher_request *req = NULL; |
| 42 | DECLARE_CRYPTO_WAIT(wait); | 42 | DECLARE_CRYPTO_WAIT(wait); |
| 43 | struct crypto_skcipher *tfm = inode->i_crypt_info->ci_ctfm; | 43 | struct fscrypt_info *ci = inode->i_crypt_info; |
| 44 | int res = 0; | 44 | struct crypto_skcipher *tfm = ci->ci_ctfm; |
| 45 | char iv[FS_CRYPTO_BLOCK_SIZE]; | 45 | union fscrypt_iv iv; |
| 46 | struct scatterlist sg; | 46 | struct scatterlist sg; |
| 47 | int res; | ||
| 47 | 48 | ||
| 48 | /* | 49 | /* |
| 49 | * Copy the filename to the output buffer for encrypting in-place and | 50 | * Copy the filename to the output buffer for encrypting in-place and |
| @@ -55,7 +56,7 @@ int fname_encrypt(struct inode *inode, const struct qstr *iname, | |||
| 55 | memset(out + iname->len, 0, olen - iname->len); | 56 | memset(out + iname->len, 0, olen - iname->len); |
| 56 | 57 | ||
| 57 | /* Initialize the IV */ | 58 | /* Initialize the IV */ |
| 58 | memset(iv, 0, FS_CRYPTO_BLOCK_SIZE); | 59 | fscrypt_generate_iv(&iv, 0, ci); |
| 59 | 60 | ||
| 60 | /* Set up the encryption request */ | 61 | /* Set up the encryption request */ |
| 61 | req = skcipher_request_alloc(tfm, GFP_NOFS); | 62 | req = skcipher_request_alloc(tfm, GFP_NOFS); |
| @@ -65,7 +66,7 @@ int fname_encrypt(struct inode *inode, const struct qstr *iname, | |||
| 65 | CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP, | 66 | CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP, |
| 66 | crypto_req_done, &wait); | 67 | crypto_req_done, &wait); |
| 67 | sg_init_one(&sg, out, olen); | 68 | sg_init_one(&sg, out, olen); |
| 68 | skcipher_request_set_crypt(req, &sg, &sg, olen, iv); | 69 | skcipher_request_set_crypt(req, &sg, &sg, olen, &iv); |
| 69 | 70 | ||
| 70 | /* Do the encryption */ | 71 | /* Do the encryption */ |
| 71 | res = crypto_wait_req(crypto_skcipher_encrypt(req), &wait); | 72 | res = crypto_wait_req(crypto_skcipher_encrypt(req), &wait); |
| @@ -94,9 +95,10 @@ static int fname_decrypt(struct inode *inode, | |||
| 94 | struct skcipher_request *req = NULL; | 95 | struct skcipher_request *req = NULL; |
| 95 | DECLARE_CRYPTO_WAIT(wait); | 96 | DECLARE_CRYPTO_WAIT(wait); |
| 96 | struct scatterlist src_sg, dst_sg; | 97 | struct scatterlist src_sg, dst_sg; |
| 97 | struct crypto_skcipher *tfm = inode->i_crypt_info->ci_ctfm; | 98 | struct fscrypt_info *ci = inode->i_crypt_info; |
| 98 | int res = 0; | 99 | struct crypto_skcipher *tfm = ci->ci_ctfm; |
| 99 | char iv[FS_CRYPTO_BLOCK_SIZE]; | 100 | union fscrypt_iv iv; |
| 101 | int res; | ||
| 100 | 102 | ||
| 101 | /* Allocate request */ | 103 | /* Allocate request */ |
| 102 | req = skcipher_request_alloc(tfm, GFP_NOFS); | 104 | req = skcipher_request_alloc(tfm, GFP_NOFS); |
| @@ -107,12 +109,12 @@ static int fname_decrypt(struct inode *inode, | |||
| 107 | crypto_req_done, &wait); | 109 | crypto_req_done, &wait); |
| 108 | 110 | ||
| 109 | /* Initialize IV */ | 111 | /* Initialize IV */ |
| 110 | memset(iv, 0, FS_CRYPTO_BLOCK_SIZE); | 112 | fscrypt_generate_iv(&iv, 0, ci); |
| 111 | 113 | ||
| 112 | /* Create decryption request */ | 114 | /* Create decryption request */ |
| 113 | sg_init_one(&src_sg, iname->name, iname->len); | 115 | sg_init_one(&src_sg, iname->name, iname->len); |
| 114 | sg_init_one(&dst_sg, oname->name, oname->len); | 116 | sg_init_one(&dst_sg, oname->name, oname->len); |
| 115 | skcipher_request_set_crypt(req, &src_sg, &dst_sg, iname->len, iv); | 117 | skcipher_request_set_crypt(req, &src_sg, &dst_sg, iname->len, &iv); |
| 116 | res = crypto_wait_req(crypto_skcipher_decrypt(req), &wait); | 118 | res = crypto_wait_req(crypto_skcipher_decrypt(req), &wait); |
| 117 | skcipher_request_free(req); | 119 | skcipher_request_free(req); |
| 118 | if (res < 0) { | 120 | if (res < 0) { |
diff --git a/fs/crypto/fscrypt_private.h b/fs/crypto/fscrypt_private.h index 79debfc9cef9..7424f851eb5c 100644 --- a/fs/crypto/fscrypt_private.h +++ b/fs/crypto/fscrypt_private.h | |||
| @@ -17,7 +17,6 @@ | |||
| 17 | #include <crypto/hash.h> | 17 | #include <crypto/hash.h> |
| 18 | 18 | ||
| 19 | /* Encryption parameters */ | 19 | /* Encryption parameters */ |
| 20 | #define FS_IV_SIZE 16 | ||
| 21 | #define FS_KEY_DERIVATION_NONCE_SIZE 16 | 20 | #define FS_KEY_DERIVATION_NONCE_SIZE 16 |
| 22 | 21 | ||
| 23 | /** | 22 | /** |
| @@ -52,16 +51,42 @@ struct fscrypt_symlink_data { | |||
| 52 | } __packed; | 51 | } __packed; |
| 53 | 52 | ||
| 54 | /* | 53 | /* |
| 55 | * A pointer to this structure is stored in the file system's in-core | 54 | * fscrypt_info - the "encryption key" for an inode |
| 56 | * representation of an inode. | 55 | * |
| 56 | * When an encrypted file's key is made available, an instance of this struct is | ||
| 57 | * allocated and stored in ->i_crypt_info. Once created, it remains until the | ||
| 58 | * inode is evicted. | ||
| 57 | */ | 59 | */ |
| 58 | struct fscrypt_info { | 60 | struct fscrypt_info { |
| 61 | |||
| 62 | /* The actual crypto transform used for encryption and decryption */ | ||
| 63 | struct crypto_skcipher *ci_ctfm; | ||
| 64 | |||
| 65 | /* | ||
| 66 | * Cipher for ESSIV IV generation. Only set for CBC contents | ||
| 67 | * encryption, otherwise is NULL. | ||
| 68 | */ | ||
| 69 | struct crypto_cipher *ci_essiv_tfm; | ||
| 70 | |||
| 71 | /* | ||
| 72 | * Encryption mode used for this inode. It corresponds to either | ||
| 73 | * ci_data_mode or ci_filename_mode, depending on the inode type. | ||
| 74 | */ | ||
| 75 | struct fscrypt_mode *ci_mode; | ||
| 76 | |||
| 77 | /* | ||
| 78 | * If non-NULL, then this inode uses a master key directly rather than a | ||
| 79 | * derived key, and ci_ctfm will equal ci_master_key->mk_ctfm. | ||
| 80 | * Otherwise, this inode uses a derived key. | ||
| 81 | */ | ||
| 82 | struct fscrypt_master_key *ci_master_key; | ||
| 83 | |||
| 84 | /* fields from the fscrypt_context */ | ||
| 59 | u8 ci_data_mode; | 85 | u8 ci_data_mode; |
| 60 | u8 ci_filename_mode; | 86 | u8 ci_filename_mode; |
| 61 | u8 ci_flags; | 87 | u8 ci_flags; |
| 62 | struct crypto_skcipher *ci_ctfm; | 88 | u8 ci_master_key_descriptor[FS_KEY_DESCRIPTOR_SIZE]; |
| 63 | struct crypto_cipher *ci_essiv_tfm; | 89 | u8 ci_nonce[FS_KEY_DERIVATION_NONCE_SIZE]; |
| 64 | u8 ci_master_key[FS_KEY_DESCRIPTOR_SIZE]; | ||
| 65 | }; | 90 | }; |
| 66 | 91 | ||
| 67 | typedef enum { | 92 | typedef enum { |
| @@ -83,6 +108,10 @@ static inline bool fscrypt_valid_enc_modes(u32 contents_mode, | |||
| 83 | filenames_mode == FS_ENCRYPTION_MODE_AES_256_CTS) | 108 | filenames_mode == FS_ENCRYPTION_MODE_AES_256_CTS) |
| 84 | return true; | 109 | return true; |
| 85 | 110 | ||
| 111 | if (contents_mode == FS_ENCRYPTION_MODE_ADIANTUM && | ||
| 112 | filenames_mode == FS_ENCRYPTION_MODE_ADIANTUM) | ||
| 113 | return true; | ||
| 114 | |||
| 86 | return false; | 115 | return false; |
| 87 | } | 116 | } |
| 88 | 117 | ||
| @@ -107,6 +136,22 @@ fscrypt_msg(struct super_block *sb, const char *level, const char *fmt, ...); | |||
| 107 | #define fscrypt_err(sb, fmt, ...) \ | 136 | #define fscrypt_err(sb, fmt, ...) \ |
| 108 | fscrypt_msg(sb, KERN_ERR, fmt, ##__VA_ARGS__) | 137 | fscrypt_msg(sb, KERN_ERR, fmt, ##__VA_ARGS__) |
| 109 | 138 | ||
| 139 | #define FSCRYPT_MAX_IV_SIZE 32 | ||
| 140 | |||
| 141 | union fscrypt_iv { | ||
| 142 | struct { | ||
| 143 | /* logical block number within the file */ | ||
| 144 | __le64 lblk_num; | ||
| 145 | |||
| 146 | /* per-file nonce; only set in DIRECT_KEY mode */ | ||
| 147 | u8 nonce[FS_KEY_DERIVATION_NONCE_SIZE]; | ||
| 148 | }; | ||
| 149 | u8 raw[FSCRYPT_MAX_IV_SIZE]; | ||
| 150 | }; | ||
| 151 | |||
| 152 | void fscrypt_generate_iv(union fscrypt_iv *iv, u64 lblk_num, | ||
| 153 | const struct fscrypt_info *ci); | ||
| 154 | |||
| 110 | /* fname.c */ | 155 | /* fname.c */ |
| 111 | extern int fname_encrypt(struct inode *inode, const struct qstr *iname, | 156 | extern int fname_encrypt(struct inode *inode, const struct qstr *iname, |
| 112 | u8 *out, unsigned int olen); | 157 | u8 *out, unsigned int olen); |
| @@ -115,6 +160,16 @@ extern bool fscrypt_fname_encrypted_size(const struct inode *inode, | |||
| 115 | u32 *encrypted_len_ret); | 160 | u32 *encrypted_len_ret); |
| 116 | 161 | ||
| 117 | /* keyinfo.c */ | 162 | /* keyinfo.c */ |
| 163 | |||
| 164 | struct fscrypt_mode { | ||
| 165 | const char *friendly_name; | ||
| 166 | const char *cipher_str; | ||
| 167 | int keysize; | ||
| 168 | int ivsize; | ||
| 169 | bool logged_impl_name; | ||
| 170 | bool needs_essiv; | ||
| 171 | }; | ||
| 172 | |||
| 118 | extern void __exit fscrypt_essiv_cleanup(void); | 173 | extern void __exit fscrypt_essiv_cleanup(void); |
| 119 | 174 | ||
| 120 | #endif /* _FSCRYPT_PRIVATE_H */ | 175 | #endif /* _FSCRYPT_PRIVATE_H */ |
diff --git a/fs/crypto/keyinfo.c b/fs/crypto/keyinfo.c index 7874c9bb2fc5..1e11a683f63d 100644 --- a/fs/crypto/keyinfo.c +++ b/fs/crypto/keyinfo.c | |||
| @@ -10,15 +10,21 @@ | |||
| 10 | */ | 10 | */ |
| 11 | 11 | ||
| 12 | #include <keys/user-type.h> | 12 | #include <keys/user-type.h> |
| 13 | #include <linux/hashtable.h> | ||
| 13 | #include <linux/scatterlist.h> | 14 | #include <linux/scatterlist.h> |
| 14 | #include <linux/ratelimit.h> | 15 | #include <linux/ratelimit.h> |
| 15 | #include <crypto/aes.h> | 16 | #include <crypto/aes.h> |
| 17 | #include <crypto/algapi.h> | ||
| 16 | #include <crypto/sha.h> | 18 | #include <crypto/sha.h> |
| 17 | #include <crypto/skcipher.h> | 19 | #include <crypto/skcipher.h> |
| 18 | #include "fscrypt_private.h" | 20 | #include "fscrypt_private.h" |
| 19 | 21 | ||
| 20 | static struct crypto_shash *essiv_hash_tfm; | 22 | static struct crypto_shash *essiv_hash_tfm; |
| 21 | 23 | ||
| 24 | /* Table of keys referenced by FS_POLICY_FLAG_DIRECT_KEY policies */ | ||
| 25 | static DEFINE_HASHTABLE(fscrypt_master_keys, 6); /* 6 bits = 64 buckets */ | ||
| 26 | static DEFINE_SPINLOCK(fscrypt_master_keys_lock); | ||
| 27 | |||
| 22 | /* | 28 | /* |
| 23 | * Key derivation function. This generates the derived key by encrypting the | 29 | * Key derivation function. This generates the derived key by encrypting the |
| 24 | * master key with AES-128-ECB using the inode's nonce as the AES key. | 30 | * master key with AES-128-ECB using the inode's nonce as the AES key. |
| @@ -123,56 +129,37 @@ invalid: | |||
| 123 | return ERR_PTR(-ENOKEY); | 129 | return ERR_PTR(-ENOKEY); |
| 124 | } | 130 | } |
| 125 | 131 | ||
| 126 | /* Find the master key, then derive the inode's actual encryption key */ | 132 | static struct fscrypt_mode available_modes[] = { |
| 127 | static int find_and_derive_key(const struct inode *inode, | ||
| 128 | const struct fscrypt_context *ctx, | ||
| 129 | u8 *derived_key, unsigned int derived_keysize) | ||
| 130 | { | ||
| 131 | struct key *key; | ||
| 132 | const struct fscrypt_key *payload; | ||
| 133 | int err; | ||
| 134 | |||
| 135 | key = find_and_lock_process_key(FS_KEY_DESC_PREFIX, | ||
| 136 | ctx->master_key_descriptor, | ||
| 137 | derived_keysize, &payload); | ||
| 138 | if (key == ERR_PTR(-ENOKEY) && inode->i_sb->s_cop->key_prefix) { | ||
| 139 | key = find_and_lock_process_key(inode->i_sb->s_cop->key_prefix, | ||
| 140 | ctx->master_key_descriptor, | ||
| 141 | derived_keysize, &payload); | ||
| 142 | } | ||
| 143 | if (IS_ERR(key)) | ||
| 144 | return PTR_ERR(key); | ||
| 145 | err = derive_key_aes(payload->raw, ctx, derived_key, derived_keysize); | ||
| 146 | up_read(&key->sem); | ||
| 147 | key_put(key); | ||
| 148 | return err; | ||
| 149 | } | ||
| 150 | |||
| 151 | static struct fscrypt_mode { | ||
| 152 | const char *friendly_name; | ||
| 153 | const char *cipher_str; | ||
| 154 | int keysize; | ||
| 155 | bool logged_impl_name; | ||
| 156 | } available_modes[] = { | ||
| 157 | [FS_ENCRYPTION_MODE_AES_256_XTS] = { | 133 | [FS_ENCRYPTION_MODE_AES_256_XTS] = { |
| 158 | .friendly_name = "AES-256-XTS", | 134 | .friendly_name = "AES-256-XTS", |
| 159 | .cipher_str = "xts(aes)", | 135 | .cipher_str = "xts(aes)", |
| 160 | .keysize = 64, | 136 | .keysize = 64, |
| 137 | .ivsize = 16, | ||
| 161 | }, | 138 | }, |
| 162 | [FS_ENCRYPTION_MODE_AES_256_CTS] = { | 139 | [FS_ENCRYPTION_MODE_AES_256_CTS] = { |
| 163 | .friendly_name = "AES-256-CTS-CBC", | 140 | .friendly_name = "AES-256-CTS-CBC", |
| 164 | .cipher_str = "cts(cbc(aes))", | 141 | .cipher_str = "cts(cbc(aes))", |
| 165 | .keysize = 32, | 142 | .keysize = 32, |
| 143 | .ivsize = 16, | ||
| 166 | }, | 144 | }, |
| 167 | [FS_ENCRYPTION_MODE_AES_128_CBC] = { | 145 | [FS_ENCRYPTION_MODE_AES_128_CBC] = { |
| 168 | .friendly_name = "AES-128-CBC", | 146 | .friendly_name = "AES-128-CBC", |
| 169 | .cipher_str = "cbc(aes)", | 147 | .cipher_str = "cbc(aes)", |
| 170 | .keysize = 16, | 148 | .keysize = 16, |
| 149 | .ivsize = 16, | ||
| 150 | .needs_essiv = true, | ||
| 171 | }, | 151 | }, |
| 172 | [FS_ENCRYPTION_MODE_AES_128_CTS] = { | 152 | [FS_ENCRYPTION_MODE_AES_128_CTS] = { |
| 173 | .friendly_name = "AES-128-CTS-CBC", | 153 | .friendly_name = "AES-128-CTS-CBC", |
| 174 | .cipher_str = "cts(cbc(aes))", | 154 | .cipher_str = "cts(cbc(aes))", |
| 175 | .keysize = 16, | 155 | .keysize = 16, |
| 156 | .ivsize = 16, | ||
| 157 | }, | ||
| 158 | [FS_ENCRYPTION_MODE_ADIANTUM] = { | ||
| 159 | .friendly_name = "Adiantum", | ||
| 160 | .cipher_str = "adiantum(xchacha12,aes)", | ||
| 161 | .keysize = 32, | ||
| 162 | .ivsize = 32, | ||
| 176 | }, | 163 | }, |
| 177 | }; | 164 | }; |
| 178 | 165 | ||
| @@ -198,14 +185,196 @@ select_encryption_mode(const struct fscrypt_info *ci, const struct inode *inode) | |||
| 198 | return ERR_PTR(-EINVAL); | 185 | return ERR_PTR(-EINVAL); |
| 199 | } | 186 | } |
| 200 | 187 | ||
| 201 | static void put_crypt_info(struct fscrypt_info *ci) | 188 | /* Find the master key, then derive the inode's actual encryption key */ |
| 189 | static int find_and_derive_key(const struct inode *inode, | ||
| 190 | const struct fscrypt_context *ctx, | ||
| 191 | u8 *derived_key, const struct fscrypt_mode *mode) | ||
| 202 | { | 192 | { |
| 203 | if (!ci) | 193 | struct key *key; |
| 194 | const struct fscrypt_key *payload; | ||
| 195 | int err; | ||
| 196 | |||
| 197 | key = find_and_lock_process_key(FS_KEY_DESC_PREFIX, | ||
| 198 | ctx->master_key_descriptor, | ||
| 199 | mode->keysize, &payload); | ||
| 200 | if (key == ERR_PTR(-ENOKEY) && inode->i_sb->s_cop->key_prefix) { | ||
| 201 | key = find_and_lock_process_key(inode->i_sb->s_cop->key_prefix, | ||
| 202 | ctx->master_key_descriptor, | ||
| 203 | mode->keysize, &payload); | ||
| 204 | } | ||
| 205 | if (IS_ERR(key)) | ||
| 206 | return PTR_ERR(key); | ||
| 207 | |||
| 208 | if (ctx->flags & FS_POLICY_FLAG_DIRECT_KEY) { | ||
| 209 | if (mode->ivsize < offsetofend(union fscrypt_iv, nonce)) { | ||
| 210 | fscrypt_warn(inode->i_sb, | ||
| 211 | "direct key mode not allowed with %s", | ||
| 212 | mode->friendly_name); | ||
| 213 | err = -EINVAL; | ||
| 214 | } else if (ctx->contents_encryption_mode != | ||
| 215 | ctx->filenames_encryption_mode) { | ||
| 216 | fscrypt_warn(inode->i_sb, | ||
| 217 | "direct key mode not allowed with different contents and filenames modes"); | ||
| 218 | err = -EINVAL; | ||
| 219 | } else { | ||
| 220 | memcpy(derived_key, payload->raw, mode->keysize); | ||
| 221 | err = 0; | ||
| 222 | } | ||
| 223 | } else { | ||
| 224 | err = derive_key_aes(payload->raw, ctx, derived_key, | ||
| 225 | mode->keysize); | ||
| 226 | } | ||
| 227 | up_read(&key->sem); | ||
| 228 | key_put(key); | ||
| 229 | return err; | ||
| 230 | } | ||
| 231 | |||
| 232 | /* Allocate and key a symmetric cipher object for the given encryption mode */ | ||
| 233 | static struct crypto_skcipher * | ||
| 234 | allocate_skcipher_for_mode(struct fscrypt_mode *mode, const u8 *raw_key, | ||
| 235 | const struct inode *inode) | ||
| 236 | { | ||
| 237 | struct crypto_skcipher *tfm; | ||
| 238 | int err; | ||
| 239 | |||
| 240 | tfm = crypto_alloc_skcipher(mode->cipher_str, 0, 0); | ||
| 241 | if (IS_ERR(tfm)) { | ||
| 242 | fscrypt_warn(inode->i_sb, | ||
| 243 | "error allocating '%s' transform for inode %lu: %ld", | ||
| 244 | mode->cipher_str, inode->i_ino, PTR_ERR(tfm)); | ||
| 245 | return tfm; | ||
| 246 | } | ||
| 247 | if (unlikely(!mode->logged_impl_name)) { | ||
| 248 | /* | ||
| 249 | * fscrypt performance can vary greatly depending on which | ||
| 250 | * crypto algorithm implementation is used. Help people debug | ||
| 251 | * performance problems by logging the ->cra_driver_name the | ||
| 252 | * first time a mode is used. Note that multiple threads can | ||
| 253 | * race here, but it doesn't really matter. | ||
| 254 | */ | ||
| 255 | mode->logged_impl_name = true; | ||
| 256 | pr_info("fscrypt: %s using implementation \"%s\"\n", | ||
| 257 | mode->friendly_name, | ||
| 258 | crypto_skcipher_alg(tfm)->base.cra_driver_name); | ||
| 259 | } | ||
| 260 | crypto_skcipher_set_flags(tfm, CRYPTO_TFM_REQ_WEAK_KEY); | ||
| 261 | err = crypto_skcipher_setkey(tfm, raw_key, mode->keysize); | ||
| 262 | if (err) | ||
| 263 | goto err_free_tfm; | ||
| 264 | |||
| 265 | return tfm; | ||
| 266 | |||
| 267 | err_free_tfm: | ||
| 268 | crypto_free_skcipher(tfm); | ||
| 269 | return ERR_PTR(err); | ||
| 270 | } | ||
| 271 | |||
| 272 | /* Master key referenced by FS_POLICY_FLAG_DIRECT_KEY policy */ | ||
| 273 | struct fscrypt_master_key { | ||
| 274 | struct hlist_node mk_node; | ||
| 275 | refcount_t mk_refcount; | ||
| 276 | const struct fscrypt_mode *mk_mode; | ||
| 277 | struct crypto_skcipher *mk_ctfm; | ||
| 278 | u8 mk_descriptor[FS_KEY_DESCRIPTOR_SIZE]; | ||
| 279 | u8 mk_raw[FS_MAX_KEY_SIZE]; | ||
| 280 | }; | ||
| 281 | |||
| 282 | static void free_master_key(struct fscrypt_master_key *mk) | ||
| 283 | { | ||
| 284 | if (mk) { | ||
| 285 | crypto_free_skcipher(mk->mk_ctfm); | ||
| 286 | kzfree(mk); | ||
| 287 | } | ||
| 288 | } | ||
| 289 | |||
| 290 | static void put_master_key(struct fscrypt_master_key *mk) | ||
| 291 | { | ||
| 292 | if (!refcount_dec_and_lock(&mk->mk_refcount, &fscrypt_master_keys_lock)) | ||
| 204 | return; | 293 | return; |
| 294 | hash_del(&mk->mk_node); | ||
| 295 | spin_unlock(&fscrypt_master_keys_lock); | ||
| 205 | 296 | ||
| 206 | crypto_free_skcipher(ci->ci_ctfm); | 297 | free_master_key(mk); |
| 207 | crypto_free_cipher(ci->ci_essiv_tfm); | 298 | } |
| 208 | kmem_cache_free(fscrypt_info_cachep, ci); | 299 | |
| 300 | /* | ||
| 301 | * Find/insert the given master key into the fscrypt_master_keys table. If | ||
| 302 | * found, it is returned with elevated refcount, and 'to_insert' is freed if | ||
| 303 | * non-NULL. If not found, 'to_insert' is inserted and returned if it's | ||
| 304 | * non-NULL; otherwise NULL is returned. | ||
| 305 | */ | ||
| 306 | static struct fscrypt_master_key * | ||
| 307 | find_or_insert_master_key(struct fscrypt_master_key *to_insert, | ||
| 308 | const u8 *raw_key, const struct fscrypt_mode *mode, | ||
| 309 | const struct fscrypt_info *ci) | ||
| 310 | { | ||
| 311 | unsigned long hash_key; | ||
| 312 | struct fscrypt_master_key *mk; | ||
| 313 | |||
| 314 | /* | ||
| 315 | * Careful: to avoid potentially leaking secret key bytes via timing | ||
| 316 | * information, we must key the hash table by descriptor rather than by | ||
| 317 | * raw key, and use crypto_memneq() when comparing raw keys. | ||
| 318 | */ | ||
| 319 | |||
| 320 | BUILD_BUG_ON(sizeof(hash_key) > FS_KEY_DESCRIPTOR_SIZE); | ||
| 321 | memcpy(&hash_key, ci->ci_master_key_descriptor, sizeof(hash_key)); | ||
| 322 | |||
| 323 | spin_lock(&fscrypt_master_keys_lock); | ||
| 324 | hash_for_each_possible(fscrypt_master_keys, mk, mk_node, hash_key) { | ||
| 325 | if (memcmp(ci->ci_master_key_descriptor, mk->mk_descriptor, | ||
| 326 | FS_KEY_DESCRIPTOR_SIZE) != 0) | ||
| 327 | continue; | ||
| 328 | if (mode != mk->mk_mode) | ||
| 329 | continue; | ||
| 330 | if (crypto_memneq(raw_key, mk->mk_raw, mode->keysize)) | ||
| 331 | continue; | ||
| 332 | /* using existing tfm with same (descriptor, mode, raw_key) */ | ||
| 333 | refcount_inc(&mk->mk_refcount); | ||
| 334 | spin_unlock(&fscrypt_master_keys_lock); | ||
| 335 | free_master_key(to_insert); | ||
| 336 | return mk; | ||
| 337 | } | ||
| 338 | if (to_insert) | ||
| 339 | hash_add(fscrypt_master_keys, &to_insert->mk_node, hash_key); | ||
| 340 | spin_unlock(&fscrypt_master_keys_lock); | ||
| 341 | return to_insert; | ||
| 342 | } | ||
| 343 | |||
| 344 | /* Prepare to encrypt directly using the master key in the given mode */ | ||
| 345 | static struct fscrypt_master_key * | ||
| 346 | fscrypt_get_master_key(const struct fscrypt_info *ci, struct fscrypt_mode *mode, | ||
| 347 | const u8 *raw_key, const struct inode *inode) | ||
| 348 | { | ||
| 349 | struct fscrypt_master_key *mk; | ||
| 350 | int err; | ||
| 351 | |||
| 352 | /* Is there already a tfm for this key? */ | ||
| 353 | mk = find_or_insert_master_key(NULL, raw_key, mode, ci); | ||
| 354 | if (mk) | ||
| 355 | return mk; | ||
| 356 | |||
| 357 | /* Nope, allocate one. */ | ||
| 358 | mk = kzalloc(sizeof(*mk), GFP_NOFS); | ||
| 359 | if (!mk) | ||
| 360 | return ERR_PTR(-ENOMEM); | ||
| 361 | refcount_set(&mk->mk_refcount, 1); | ||
| 362 | mk->mk_mode = mode; | ||
| 363 | mk->mk_ctfm = allocate_skcipher_for_mode(mode, raw_key, inode); | ||
| 364 | if (IS_ERR(mk->mk_ctfm)) { | ||
| 365 | err = PTR_ERR(mk->mk_ctfm); | ||
| 366 | mk->mk_ctfm = NULL; | ||
| 367 | goto err_free_mk; | ||
| 368 | } | ||
| 369 | memcpy(mk->mk_descriptor, ci->ci_master_key_descriptor, | ||
| 370 | FS_KEY_DESCRIPTOR_SIZE); | ||
| 371 | memcpy(mk->mk_raw, raw_key, mode->keysize); | ||
| 372 | |||
| 373 | return find_or_insert_master_key(mk, raw_key, mode, ci); | ||
| 374 | |||
| 375 | err_free_mk: | ||
| 376 | free_master_key(mk); | ||
| 377 | return ERR_PTR(err); | ||
| 209 | } | 378 | } |
| 210 | 379 | ||
| 211 | static int derive_essiv_salt(const u8 *key, int keysize, u8 *salt) | 380 | static int derive_essiv_salt(const u8 *key, int keysize, u8 *salt) |
| @@ -275,11 +444,67 @@ void __exit fscrypt_essiv_cleanup(void) | |||
| 275 | crypto_free_shash(essiv_hash_tfm); | 444 | crypto_free_shash(essiv_hash_tfm); |
| 276 | } | 445 | } |
| 277 | 446 | ||
| 447 | /* | ||
| 448 | * Given the encryption mode and key (normally the derived key, but for | ||
| 449 | * FS_POLICY_FLAG_DIRECT_KEY mode it's the master key), set up the inode's | ||
| 450 | * symmetric cipher transform object(s). | ||
| 451 | */ | ||
| 452 | static int setup_crypto_transform(struct fscrypt_info *ci, | ||
| 453 | struct fscrypt_mode *mode, | ||
| 454 | const u8 *raw_key, const struct inode *inode) | ||
| 455 | { | ||
| 456 | struct fscrypt_master_key *mk; | ||
| 457 | struct crypto_skcipher *ctfm; | ||
| 458 | int err; | ||
| 459 | |||
| 460 | if (ci->ci_flags & FS_POLICY_FLAG_DIRECT_KEY) { | ||
| 461 | mk = fscrypt_get_master_key(ci, mode, raw_key, inode); | ||
| 462 | if (IS_ERR(mk)) | ||
| 463 | return PTR_ERR(mk); | ||
| 464 | ctfm = mk->mk_ctfm; | ||
| 465 | } else { | ||
| 466 | mk = NULL; | ||
| 467 | ctfm = allocate_skcipher_for_mode(mode, raw_key, inode); | ||
| 468 | if (IS_ERR(ctfm)) | ||
| 469 | return PTR_ERR(ctfm); | ||
| 470 | } | ||
| 471 | ci->ci_master_key = mk; | ||
| 472 | ci->ci_ctfm = ctfm; | ||
| 473 | |||
| 474 | if (mode->needs_essiv) { | ||
| 475 | /* ESSIV implies 16-byte IVs which implies !DIRECT_KEY */ | ||
| 476 | WARN_ON(mode->ivsize != AES_BLOCK_SIZE); | ||
| 477 | WARN_ON(ci->ci_flags & FS_POLICY_FLAG_DIRECT_KEY); | ||
| 478 | |||
| 479 | err = init_essiv_generator(ci, raw_key, mode->keysize); | ||
| 480 | if (err) { | ||
| 481 | fscrypt_warn(inode->i_sb, | ||
| 482 | "error initializing ESSIV generator for inode %lu: %d", | ||
| 483 | inode->i_ino, err); | ||
| 484 | return err; | ||
| 485 | } | ||
| 486 | } | ||
| 487 | return 0; | ||
| 488 | } | ||
| 489 | |||
| 490 | static void put_crypt_info(struct fscrypt_info *ci) | ||
| 491 | { | ||
| 492 | if (!ci) | ||
| 493 | return; | ||
| 494 | |||
| 495 | if (ci->ci_master_key) { | ||
| 496 | put_master_key(ci->ci_master_key); | ||
| 497 | } else { | ||
| 498 | crypto_free_skcipher(ci->ci_ctfm); | ||
| 499 | crypto_free_cipher(ci->ci_essiv_tfm); | ||
| 500 | } | ||
| 501 | kmem_cache_free(fscrypt_info_cachep, ci); | ||
| 502 | } | ||
| 503 | |||
| 278 | int fscrypt_get_encryption_info(struct inode *inode) | 504 | int fscrypt_get_encryption_info(struct inode *inode) |
| 279 | { | 505 | { |
| 280 | struct fscrypt_info *crypt_info; | 506 | struct fscrypt_info *crypt_info; |
| 281 | struct fscrypt_context ctx; | 507 | struct fscrypt_context ctx; |
| 282 | struct crypto_skcipher *ctfm; | ||
| 283 | struct fscrypt_mode *mode; | 508 | struct fscrypt_mode *mode; |
| 284 | u8 *raw_key = NULL; | 509 | u8 *raw_key = NULL; |
| 285 | int res; | 510 | int res; |
| @@ -312,74 +537,42 @@ int fscrypt_get_encryption_info(struct inode *inode) | |||
| 312 | if (ctx.flags & ~FS_POLICY_FLAGS_VALID) | 537 | if (ctx.flags & ~FS_POLICY_FLAGS_VALID) |
| 313 | return -EINVAL; | 538 | return -EINVAL; |
| 314 | 539 | ||
| 315 | crypt_info = kmem_cache_alloc(fscrypt_info_cachep, GFP_NOFS); | 540 | crypt_info = kmem_cache_zalloc(fscrypt_info_cachep, GFP_NOFS); |
| 316 | if (!crypt_info) | 541 | if (!crypt_info) |
| 317 | return -ENOMEM; | 542 | return -ENOMEM; |
| 318 | 543 | ||
| 319 | crypt_info->ci_flags = ctx.flags; | 544 | crypt_info->ci_flags = ctx.flags; |
| 320 | crypt_info->ci_data_mode = ctx.contents_encryption_mode; | 545 | crypt_info->ci_data_mode = ctx.contents_encryption_mode; |
| 321 | crypt_info->ci_filename_mode = ctx.filenames_encryption_mode; | 546 | crypt_info->ci_filename_mode = ctx.filenames_encryption_mode; |
| 322 | crypt_info->ci_ctfm = NULL; | 547 | memcpy(crypt_info->ci_master_key_descriptor, ctx.master_key_descriptor, |
| 323 | crypt_info->ci_essiv_tfm = NULL; | 548 | FS_KEY_DESCRIPTOR_SIZE); |
| 324 | memcpy(crypt_info->ci_master_key, ctx.master_key_descriptor, | 549 | memcpy(crypt_info->ci_nonce, ctx.nonce, FS_KEY_DERIVATION_NONCE_SIZE); |
| 325 | sizeof(crypt_info->ci_master_key)); | ||
| 326 | 550 | ||
| 327 | mode = select_encryption_mode(crypt_info, inode); | 551 | mode = select_encryption_mode(crypt_info, inode); |
| 328 | if (IS_ERR(mode)) { | 552 | if (IS_ERR(mode)) { |
| 329 | res = PTR_ERR(mode); | 553 | res = PTR_ERR(mode); |
| 330 | goto out; | 554 | goto out; |
| 331 | } | 555 | } |
| 556 | WARN_ON(mode->ivsize > FSCRYPT_MAX_IV_SIZE); | ||
| 557 | crypt_info->ci_mode = mode; | ||
| 332 | 558 | ||
| 333 | /* | 559 | /* |
| 334 | * This cannot be a stack buffer because it is passed to the scatterlist | 560 | * This cannot be a stack buffer because it may be passed to the |
| 335 | * crypto API as part of key derivation. | 561 | * scatterlist crypto API as part of key derivation. |
| 336 | */ | 562 | */ |
| 337 | res = -ENOMEM; | 563 | res = -ENOMEM; |
| 338 | raw_key = kmalloc(mode->keysize, GFP_NOFS); | 564 | raw_key = kmalloc(mode->keysize, GFP_NOFS); |
| 339 | if (!raw_key) | 565 | if (!raw_key) |
| 340 | goto out; | 566 | goto out; |
| 341 | 567 | ||
| 342 | res = find_and_derive_key(inode, &ctx, raw_key, mode->keysize); | 568 | res = find_and_derive_key(inode, &ctx, raw_key, mode); |
| 343 | if (res) | 569 | if (res) |
| 344 | goto out; | 570 | goto out; |
| 345 | 571 | ||
| 346 | ctfm = crypto_alloc_skcipher(mode->cipher_str, 0, 0); | 572 | res = setup_crypto_transform(crypt_info, mode, raw_key, inode); |
| 347 | if (IS_ERR(ctfm)) { | ||
| 348 | res = PTR_ERR(ctfm); | ||
| 349 | fscrypt_warn(inode->i_sb, | ||
| 350 | "error allocating '%s' transform for inode %lu: %d", | ||
| 351 | mode->cipher_str, inode->i_ino, res); | ||
| 352 | goto out; | ||
| 353 | } | ||
| 354 | if (unlikely(!mode->logged_impl_name)) { | ||
| 355 | /* | ||
| 356 | * fscrypt performance can vary greatly depending on which | ||
| 357 | * crypto algorithm implementation is used. Help people debug | ||
| 358 | * performance problems by logging the ->cra_driver_name the | ||
| 359 | * first time a mode is used. Note that multiple threads can | ||
| 360 | * race here, but it doesn't really matter. | ||
| 361 | */ | ||
| 362 | mode->logged_impl_name = true; | ||
| 363 | pr_info("fscrypt: %s using implementation \"%s\"\n", | ||
| 364 | mode->friendly_name, | ||
| 365 | crypto_skcipher_alg(ctfm)->base.cra_driver_name); | ||
| 366 | } | ||
| 367 | crypt_info->ci_ctfm = ctfm; | ||
| 368 | crypto_skcipher_set_flags(ctfm, CRYPTO_TFM_REQ_WEAK_KEY); | ||
| 369 | res = crypto_skcipher_setkey(ctfm, raw_key, mode->keysize); | ||
| 370 | if (res) | 573 | if (res) |
| 371 | goto out; | 574 | goto out; |
| 372 | 575 | ||
| 373 | if (S_ISREG(inode->i_mode) && | ||
| 374 | crypt_info->ci_data_mode == FS_ENCRYPTION_MODE_AES_128_CBC) { | ||
| 375 | res = init_essiv_generator(crypt_info, raw_key, mode->keysize); | ||
| 376 | if (res) { | ||
| 377 | fscrypt_warn(inode->i_sb, | ||
| 378 | "error initializing ESSIV generator for inode %lu: %d", | ||
| 379 | inode->i_ino, res); | ||
| 380 | goto out; | ||
| 381 | } | ||
| 382 | } | ||
| 383 | if (cmpxchg(&inode->i_crypt_info, NULL, crypt_info) == NULL) | 576 | if (cmpxchg(&inode->i_crypt_info, NULL, crypt_info) == NULL) |
| 384 | crypt_info = NULL; | 577 | crypt_info = NULL; |
| 385 | out: | 578 | out: |
diff --git a/fs/crypto/policy.c b/fs/crypto/policy.c index c6d431a5cce9..f490de921ce8 100644 --- a/fs/crypto/policy.c +++ b/fs/crypto/policy.c | |||
| @@ -199,7 +199,8 @@ int fscrypt_has_permitted_context(struct inode *parent, struct inode *child) | |||
| 199 | child_ci = child->i_crypt_info; | 199 | child_ci = child->i_crypt_info; |
| 200 | 200 | ||
| 201 | if (parent_ci && child_ci) { | 201 | if (parent_ci && child_ci) { |
| 202 | return memcmp(parent_ci->ci_master_key, child_ci->ci_master_key, | 202 | return memcmp(parent_ci->ci_master_key_descriptor, |
| 203 | child_ci->ci_master_key_descriptor, | ||
| 203 | FS_KEY_DESCRIPTOR_SIZE) == 0 && | 204 | FS_KEY_DESCRIPTOR_SIZE) == 0 && |
| 204 | (parent_ci->ci_data_mode == child_ci->ci_data_mode) && | 205 | (parent_ci->ci_data_mode == child_ci->ci_data_mode) && |
| 205 | (parent_ci->ci_filename_mode == | 206 | (parent_ci->ci_filename_mode == |
| @@ -254,7 +255,7 @@ int fscrypt_inherit_context(struct inode *parent, struct inode *child, | |||
| 254 | ctx.contents_encryption_mode = ci->ci_data_mode; | 255 | ctx.contents_encryption_mode = ci->ci_data_mode; |
| 255 | ctx.filenames_encryption_mode = ci->ci_filename_mode; | 256 | ctx.filenames_encryption_mode = ci->ci_filename_mode; |
| 256 | ctx.flags = ci->ci_flags; | 257 | ctx.flags = ci->ci_flags; |
| 257 | memcpy(ctx.master_key_descriptor, ci->ci_master_key, | 258 | memcpy(ctx.master_key_descriptor, ci->ci_master_key_descriptor, |
| 258 | FS_KEY_DESCRIPTOR_SIZE); | 259 | FS_KEY_DESCRIPTOR_SIZE); |
| 259 | get_random_bytes(ctx.nonce, FS_KEY_DERIVATION_NONCE_SIZE); | 260 | get_random_bytes(ctx.nonce, FS_KEY_DERIVATION_NONCE_SIZE); |
| 260 | BUILD_BUG_ON(sizeof(ctx) != FSCRYPT_SET_CONTEXT_MAX_SIZE); | 261 | BUILD_BUG_ON(sizeof(ctx) != FSCRYPT_SET_CONTEXT_MAX_SIZE); |
diff --git a/include/uapi/linux/fs.h b/include/uapi/linux/fs.h index 53a22e8e0408..121e82ce296b 100644 --- a/include/uapi/linux/fs.h +++ b/include/uapi/linux/fs.h | |||
| @@ -223,7 +223,8 @@ struct fsxattr { | |||
| 223 | #define FS_POLICY_FLAGS_PAD_16 0x02 | 223 | #define FS_POLICY_FLAGS_PAD_16 0x02 |
| 224 | #define FS_POLICY_FLAGS_PAD_32 0x03 | 224 | #define FS_POLICY_FLAGS_PAD_32 0x03 |
| 225 | #define FS_POLICY_FLAGS_PAD_MASK 0x03 | 225 | #define FS_POLICY_FLAGS_PAD_MASK 0x03 |
| 226 | #define FS_POLICY_FLAGS_VALID 0x03 | 226 | #define FS_POLICY_FLAG_DIRECT_KEY 0x04 /* use master key directly */ |
| 227 | #define FS_POLICY_FLAGS_VALID 0x07 | ||
| 227 | 228 | ||
| 228 | /* Encryption algorithms */ | 229 | /* Encryption algorithms */ |
| 229 | #define FS_ENCRYPTION_MODE_INVALID 0 | 230 | #define FS_ENCRYPTION_MODE_INVALID 0 |
| @@ -235,6 +236,7 @@ struct fsxattr { | |||
| 235 | #define FS_ENCRYPTION_MODE_AES_128_CTS 6 | 236 | #define FS_ENCRYPTION_MODE_AES_128_CTS 6 |
| 236 | #define FS_ENCRYPTION_MODE_SPECK128_256_XTS 7 /* Removed, do not use. */ | 237 | #define FS_ENCRYPTION_MODE_SPECK128_256_XTS 7 /* Removed, do not use. */ |
| 237 | #define FS_ENCRYPTION_MODE_SPECK128_256_CTS 8 /* Removed, do not use. */ | 238 | #define FS_ENCRYPTION_MODE_SPECK128_256_CTS 8 /* Removed, do not use. */ |
| 239 | #define FS_ENCRYPTION_MODE_ADIANTUM 9 | ||
| 238 | 240 | ||
| 239 | struct fscrypt_policy { | 241 | struct fscrypt_policy { |
| 240 | __u8 version; | 242 | __u8 version; |
