aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2018-06-05 18:15:32 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2018-06-05 18:15:32 -0400
commitfd59ccc53062964007beda8787ffd9cd93968d63 (patch)
treea2d2dd5cf5d456f7e1a1427e2a062d1a530e0ca4
parent6567af78acd18a1f9dec99f27f1b7ba9d270074b (diff)
parente1cc40e5d42acb1d99652babb17e6a5ee4247409 (diff)
Merge tag 'fscrypt_for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tytso/fscrypt
Pull fscrypt updates from Ted Ts'o: "Add bunch of cleanups, and add support for the Speck128/256 algorithms. Yes, Speck is contrversial, but the intention is to use them only for the lowest end Android devices, where the alternative *really* is no encryption at all for data stored at rest" * tag 'fscrypt_for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tytso/fscrypt: fscrypt: log the crypto algorithm implementations fscrypt: add Speck128/256 support fscrypt: only derive the needed portion of the key fscrypt: separate key lookup from key derivation fscrypt: use a common logging function fscrypt: remove internal key size constants fscrypt: remove unnecessary check for non-logon key type fscrypt: make fscrypt_operations.max_namelen an integer fscrypt: drop empty name check from fname_decrypt() fscrypt: drop max_namelen check from fname_decrypt() fscrypt: don't special-case EOPNOTSUPP from fscrypt_get_encryption_info() fscrypt: don't clear flags on crypto transform fscrypt: remove stale comment from fscrypt_d_revalidate() fscrypt: remove error messages for skcipher_request_alloc() failure fscrypt: remove unnecessary NULL check when allocating skcipher fscrypt: clean up after fscrypt_prepare_lookup() conversions fs, fscrypt: only define ->s_cop when FS_ENCRYPTION is enabled fscrypt: use unbound workqueue for decryption
-rw-r--r--Documentation/filesystems/fscrypt.rst10
-rw-r--r--fs/crypto/crypto.c47
-rw-r--r--fs/crypto/fname.c32
-rw-r--r--fs/crypto/fscrypt_private.h23
-rw-r--r--fs/crypto/hooks.c5
-rw-r--r--fs/crypto/keyinfo.c286
-rw-r--r--fs/ext4/super.c8
-rw-r--r--fs/f2fs/super.c8
-rw-r--r--fs/ubifs/crypto.c10
-rw-r--r--include/linux/fs.h4
-rw-r--r--include/linux/fscrypt_notsupp.h10
-rw-r--r--include/linux/fscrypt_supp.h16
-rw-r--r--include/uapi/linux/fs.h2
13 files changed, 248 insertions, 213 deletions
diff --git a/Documentation/filesystems/fscrypt.rst b/Documentation/filesystems/fscrypt.rst
index cfbc18f0d9c9..48b424de85bb 100644
--- a/Documentation/filesystems/fscrypt.rst
+++ b/Documentation/filesystems/fscrypt.rst
@@ -191,11 +191,21 @@ Currently, the following pairs of encryption modes are supported:
191 191
192- AES-256-XTS for contents and AES-256-CTS-CBC for filenames 192- 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 193- AES-128-CBC for contents and AES-128-CTS-CBC for filenames
194- Speck128/256-XTS for contents and Speck128/256-CTS-CBC for filenames
194 195
195It is strongly recommended to use AES-256-XTS for contents encryption. 196It is strongly recommended to use AES-256-XTS for contents encryption.
196AES-128-CBC was added only for low-powered embedded devices with 197AES-128-CBC was added only for low-powered embedded devices with
197crypto accelerators such as CAAM or CESA that do not support XTS. 198crypto accelerators such as CAAM or CESA that do not support XTS.
198 199
200Similarly, Speck128/256 support was only added for older or low-end
201CPUs which cannot do AES fast enough -- especially ARM CPUs which have
202NEON instructions but not the Cryptography Extensions -- and for which
203it would not otherwise be feasible to use encryption at all. It is
204not recommended to use Speck on CPUs that have AES instructions.
205Speck support is only available if it has been enabled in the crypto
206API via CONFIG_CRYPTO_SPECK. Also, on ARM platforms, to get
207acceptable performance CONFIG_CRYPTO_SPECK_NEON must be enabled.
208
199New encryption modes can be added relatively easily, without changes 209New encryption modes can be added relatively easily, without changes
200to individual filesystems. However, authenticated encryption (AE) 210to individual filesystems. However, authenticated encryption (AE)
201modes are not currently supported because of the difficulty of dealing 211modes are not currently supported because of the difficulty of dealing
diff --git a/fs/crypto/crypto.c b/fs/crypto/crypto.c
index ce654526c0fb..243a269e6c5f 100644
--- a/fs/crypto/crypto.c
+++ b/fs/crypto/crypto.c
@@ -156,12 +156,8 @@ int fscrypt_do_page_crypto(const struct inode *inode, fscrypt_direction_t rw,
156 } 156 }
157 157
158 req = skcipher_request_alloc(tfm, gfp_flags); 158 req = skcipher_request_alloc(tfm, gfp_flags);
159 if (!req) { 159 if (!req)
160 printk_ratelimited(KERN_ERR
161 "%s: crypto_request_alloc() failed\n",
162 __func__);
163 return -ENOMEM; 160 return -ENOMEM;
164 }
165 161
166 skcipher_request_set_callback( 162 skcipher_request_set_callback(
167 req, CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP, 163 req, CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
@@ -178,9 +174,10 @@ int fscrypt_do_page_crypto(const struct inode *inode, fscrypt_direction_t rw,
178 res = crypto_wait_req(crypto_skcipher_encrypt(req), &wait); 174 res = crypto_wait_req(crypto_skcipher_encrypt(req), &wait);
179 skcipher_request_free(req); 175 skcipher_request_free(req);
180 if (res) { 176 if (res) {
181 printk_ratelimited(KERN_ERR 177 fscrypt_err(inode->i_sb,
182 "%s: crypto_skcipher_encrypt() returned %d\n", 178 "%scryption failed for inode %lu, block %llu: %d",
183 __func__, res); 179 (rw == FS_DECRYPT ? "de" : "en"),
180 inode->i_ino, lblk_num, res);
184 return res; 181 return res;
185 } 182 }
186 return 0; 183 return 0;
@@ -326,7 +323,6 @@ static int fscrypt_d_revalidate(struct dentry *dentry, unsigned int flags)
326 return 0; 323 return 0;
327 } 324 }
328 325
329 /* this should eventually be an flag in d_flags */
330 spin_lock(&dentry->d_lock); 326 spin_lock(&dentry->d_lock);
331 cached_with_key = dentry->d_flags & DCACHE_ENCRYPTED_WITH_KEY; 327 cached_with_key = dentry->d_flags & DCACHE_ENCRYPTED_WITH_KEY;
332 spin_unlock(&dentry->d_lock); 328 spin_unlock(&dentry->d_lock);
@@ -353,7 +349,6 @@ static int fscrypt_d_revalidate(struct dentry *dentry, unsigned int flags)
353const struct dentry_operations fscrypt_d_ops = { 349const struct dentry_operations fscrypt_d_ops = {
354 .d_revalidate = fscrypt_d_revalidate, 350 .d_revalidate = fscrypt_d_revalidate,
355}; 351};
356EXPORT_SYMBOL(fscrypt_d_ops);
357 352
358void fscrypt_restore_control_page(struct page *page) 353void fscrypt_restore_control_page(struct page *page)
359{ 354{
@@ -422,13 +417,43 @@ fail:
422 return res; 417 return res;
423} 418}
424 419
420void fscrypt_msg(struct super_block *sb, const char *level,
421 const char *fmt, ...)
422{
423 static DEFINE_RATELIMIT_STATE(rs, DEFAULT_RATELIMIT_INTERVAL,
424 DEFAULT_RATELIMIT_BURST);
425 struct va_format vaf;
426 va_list args;
427
428 if (!__ratelimit(&rs))
429 return;
430
431 va_start(args, fmt);
432 vaf.fmt = fmt;
433 vaf.va = &args;
434 if (sb)
435 printk("%sfscrypt (%s): %pV\n", level, sb->s_id, &vaf);
436 else
437 printk("%sfscrypt: %pV\n", level, &vaf);
438 va_end(args);
439}
440
425/** 441/**
426 * fscrypt_init() - Set up for fs encryption. 442 * fscrypt_init() - Set up for fs encryption.
427 */ 443 */
428static int __init fscrypt_init(void) 444static int __init fscrypt_init(void)
429{ 445{
446 /*
447 * Use an unbound workqueue to allow bios to be decrypted in parallel
448 * even when they happen to complete on the same CPU. This sacrifices
449 * locality, but it's worthwhile since decryption is CPU-intensive.
450 *
451 * Also use a high-priority workqueue to prioritize decryption work,
452 * which blocks reads from completing, over regular application tasks.
453 */
430 fscrypt_read_workqueue = alloc_workqueue("fscrypt_read_queue", 454 fscrypt_read_workqueue = alloc_workqueue("fscrypt_read_queue",
431 WQ_HIGHPRI, 0); 455 WQ_UNBOUND | WQ_HIGHPRI,
456 num_online_cpus());
432 if (!fscrypt_read_workqueue) 457 if (!fscrypt_read_workqueue)
433 goto fail; 458 goto fail;
434 459
diff --git a/fs/crypto/fname.c b/fs/crypto/fname.c
index e33f3d3c5ade..d7a0f682ca12 100644
--- a/fs/crypto/fname.c
+++ b/fs/crypto/fname.c
@@ -59,11 +59,8 @@ int fname_encrypt(struct inode *inode, const struct qstr *iname,
59 59
60 /* Set up the encryption request */ 60 /* Set up the encryption request */
61 req = skcipher_request_alloc(tfm, GFP_NOFS); 61 req = skcipher_request_alloc(tfm, GFP_NOFS);
62 if (!req) { 62 if (!req)
63 printk_ratelimited(KERN_ERR
64 "%s: skcipher_request_alloc() failed\n", __func__);
65 return -ENOMEM; 63 return -ENOMEM;
66 }
67 skcipher_request_set_callback(req, 64 skcipher_request_set_callback(req,
68 CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP, 65 CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
69 crypto_req_done, &wait); 66 crypto_req_done, &wait);
@@ -74,8 +71,9 @@ int fname_encrypt(struct inode *inode, const struct qstr *iname,
74 res = crypto_wait_req(crypto_skcipher_encrypt(req), &wait); 71 res = crypto_wait_req(crypto_skcipher_encrypt(req), &wait);
75 skcipher_request_free(req); 72 skcipher_request_free(req);
76 if (res < 0) { 73 if (res < 0) {
77 printk_ratelimited(KERN_ERR 74 fscrypt_err(inode->i_sb,
78 "%s: Error (error code %d)\n", __func__, res); 75 "Filename encryption failed for inode %lu: %d",
76 inode->i_ino, res);
79 return res; 77 return res;
80 } 78 }
81 79
@@ -96,23 +94,14 @@ static int fname_decrypt(struct inode *inode,
96 struct skcipher_request *req = NULL; 94 struct skcipher_request *req = NULL;
97 DECLARE_CRYPTO_WAIT(wait); 95 DECLARE_CRYPTO_WAIT(wait);
98 struct scatterlist src_sg, dst_sg; 96 struct scatterlist src_sg, dst_sg;
99 struct fscrypt_info *ci = inode->i_crypt_info; 97 struct crypto_skcipher *tfm = inode->i_crypt_info->ci_ctfm;
100 struct crypto_skcipher *tfm = ci->ci_ctfm;
101 int res = 0; 98 int res = 0;
102 char iv[FS_CRYPTO_BLOCK_SIZE]; 99 char iv[FS_CRYPTO_BLOCK_SIZE];
103 unsigned lim;
104
105 lim = inode->i_sb->s_cop->max_namelen(inode);
106 if (iname->len <= 0 || iname->len > lim)
107 return -EIO;
108 100
109 /* Allocate request */ 101 /* Allocate request */
110 req = skcipher_request_alloc(tfm, GFP_NOFS); 102 req = skcipher_request_alloc(tfm, GFP_NOFS);
111 if (!req) { 103 if (!req)
112 printk_ratelimited(KERN_ERR
113 "%s: crypto_request_alloc() failed\n", __func__);
114 return -ENOMEM; 104 return -ENOMEM;
115 }
116 skcipher_request_set_callback(req, 105 skcipher_request_set_callback(req,
117 CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP, 106 CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
118 crypto_req_done, &wait); 107 crypto_req_done, &wait);
@@ -127,8 +116,9 @@ static int fname_decrypt(struct inode *inode,
127 res = crypto_wait_req(crypto_skcipher_decrypt(req), &wait); 116 res = crypto_wait_req(crypto_skcipher_decrypt(req), &wait);
128 skcipher_request_free(req); 117 skcipher_request_free(req);
129 if (res < 0) { 118 if (res < 0) {
130 printk_ratelimited(KERN_ERR 119 fscrypt_err(inode->i_sb,
131 "%s: Error (error code %d)\n", __func__, res); 120 "Filename decryption failed for inode %lu: %d",
121 inode->i_ino, res);
132 return res; 122 return res;
133 } 123 }
134 124
@@ -341,12 +331,12 @@ int fscrypt_setup_filename(struct inode *dir, const struct qstr *iname,
341 return 0; 331 return 0;
342 } 332 }
343 ret = fscrypt_get_encryption_info(dir); 333 ret = fscrypt_get_encryption_info(dir);
344 if (ret && ret != -EOPNOTSUPP) 334 if (ret)
345 return ret; 335 return ret;
346 336
347 if (dir->i_crypt_info) { 337 if (dir->i_crypt_info) {
348 if (!fscrypt_fname_encrypted_size(dir, iname->len, 338 if (!fscrypt_fname_encrypted_size(dir, iname->len,
349 dir->i_sb->s_cop->max_namelen(dir), 339 dir->i_sb->s_cop->max_namelen,
350 &fname->crypto_buf.len)) 340 &fname->crypto_buf.len))
351 return -ENAMETOOLONG; 341 return -ENAMETOOLONG;
352 fname->crypto_buf.name = kmalloc(fname->crypto_buf.len, 342 fname->crypto_buf.name = kmalloc(fname->crypto_buf.len,
diff --git a/fs/crypto/fscrypt_private.h b/fs/crypto/fscrypt_private.h
index ad6722bae8b7..37562394c5de 100644
--- a/fs/crypto/fscrypt_private.h
+++ b/fs/crypto/fscrypt_private.h
@@ -18,15 +18,7 @@
18 18
19/* Encryption parameters */ 19/* Encryption parameters */
20#define FS_IV_SIZE 16 20#define FS_IV_SIZE 16
21#define FS_AES_128_ECB_KEY_SIZE 16 21#define FS_KEY_DERIVATION_NONCE_SIZE 16
22#define FS_AES_128_CBC_KEY_SIZE 16
23#define FS_AES_128_CTS_KEY_SIZE 16
24#define FS_AES_256_GCM_KEY_SIZE 32
25#define FS_AES_256_CBC_KEY_SIZE 32
26#define FS_AES_256_CTS_KEY_SIZE 32
27#define FS_AES_256_XTS_KEY_SIZE 64
28
29#define FS_KEY_DERIVATION_NONCE_SIZE 16
30 22
31/** 23/**
32 * Encryption context for inode 24 * Encryption context for inode
@@ -91,6 +83,10 @@ static inline bool fscrypt_valid_enc_modes(u32 contents_mode,
91 filenames_mode == FS_ENCRYPTION_MODE_AES_256_CTS) 83 filenames_mode == FS_ENCRYPTION_MODE_AES_256_CTS)
92 return true; 84 return true;
93 85
86 if (contents_mode == FS_ENCRYPTION_MODE_SPECK128_256_XTS &&
87 filenames_mode == FS_ENCRYPTION_MODE_SPECK128_256_CTS)
88 return true;
89
94 return false; 90 return false;
95} 91}
96 92
@@ -106,6 +102,15 @@ extern int fscrypt_do_page_crypto(const struct inode *inode,
106 gfp_t gfp_flags); 102 gfp_t gfp_flags);
107extern struct page *fscrypt_alloc_bounce_page(struct fscrypt_ctx *ctx, 103extern struct page *fscrypt_alloc_bounce_page(struct fscrypt_ctx *ctx,
108 gfp_t gfp_flags); 104 gfp_t gfp_flags);
105extern const struct dentry_operations fscrypt_d_ops;
106
107extern void __printf(3, 4) __cold
108fscrypt_msg(struct super_block *sb, const char *level, const char *fmt, ...);
109
110#define fscrypt_warn(sb, fmt, ...) \
111 fscrypt_msg(sb, KERN_WARNING, fmt, ##__VA_ARGS__)
112#define fscrypt_err(sb, fmt, ...) \
113 fscrypt_msg(sb, KERN_ERR, fmt, ##__VA_ARGS__)
109 114
110/* fname.c */ 115/* fname.c */
111extern int fname_encrypt(struct inode *inode, const struct qstr *iname, 116extern int fname_encrypt(struct inode *inode, const struct qstr *iname,
diff --git a/fs/crypto/hooks.c b/fs/crypto/hooks.c
index bec06490fb13..926e5df20ec3 100644
--- a/fs/crypto/hooks.c
+++ b/fs/crypto/hooks.c
@@ -39,8 +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 pr_warn_ratelimited("fscrypt: inconsistent encryption contexts: %lu/%lu", 42 fscrypt_warn(inode->i_sb,
43 d_inode(dir)->i_ino, inode->i_ino); 43 "inconsistent encryption contexts: %lu/%lu",
44 d_inode(dir)->i_ino, inode->i_ino);
44 err = -EPERM; 45 err = -EPERM;
45 } 46 }
46 dput(dir); 47 dput(dir);
diff --git a/fs/crypto/keyinfo.c b/fs/crypto/keyinfo.c
index 05f5ee1f0705..e997ca51192f 100644
--- a/fs/crypto/keyinfo.c
+++ b/fs/crypto/keyinfo.c
@@ -19,17 +19,16 @@
19 19
20static struct crypto_shash *essiv_hash_tfm; 20static struct crypto_shash *essiv_hash_tfm;
21 21
22/** 22/*
23 * derive_key_aes() - Derive a key using AES-128-ECB 23 * Key derivation function. This generates the derived key by encrypting the
24 * @deriving_key: Encryption key used for derivation. 24 * master key with AES-128-ECB using the inode's nonce as the AES key.
25 * @source_key: Source key to which to apply derivation.
26 * @derived_raw_key: Derived raw key.
27 * 25 *
28 * Return: Zero on success; non-zero otherwise. 26 * The master key must be at least as long as the derived key. If the master
27 * key is longer, then only the first 'derived_keysize' bytes are used.
29 */ 28 */
30static int derive_key_aes(u8 deriving_key[FS_AES_128_ECB_KEY_SIZE], 29static int derive_key_aes(const u8 *master_key,
31 const struct fscrypt_key *source_key, 30 const struct fscrypt_context *ctx,
32 u8 derived_raw_key[FS_MAX_KEY_SIZE]) 31 u8 *derived_key, unsigned int derived_keysize)
33{ 32{
34 int res = 0; 33 int res = 0;
35 struct skcipher_request *req = NULL; 34 struct skcipher_request *req = NULL;
@@ -51,14 +50,13 @@ static int derive_key_aes(u8 deriving_key[FS_AES_128_ECB_KEY_SIZE],
51 skcipher_request_set_callback(req, 50 skcipher_request_set_callback(req,
52 CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP, 51 CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
53 crypto_req_done, &wait); 52 crypto_req_done, &wait);
54 res = crypto_skcipher_setkey(tfm, deriving_key, 53 res = crypto_skcipher_setkey(tfm, ctx->nonce, sizeof(ctx->nonce));
55 FS_AES_128_ECB_KEY_SIZE);
56 if (res < 0) 54 if (res < 0)
57 goto out; 55 goto out;
58 56
59 sg_init_one(&src_sg, source_key->raw, source_key->size); 57 sg_init_one(&src_sg, master_key, derived_keysize);
60 sg_init_one(&dst_sg, derived_raw_key, source_key->size); 58 sg_init_one(&dst_sg, derived_key, derived_keysize);
61 skcipher_request_set_crypt(req, &src_sg, &dst_sg, source_key->size, 59 skcipher_request_set_crypt(req, &src_sg, &dst_sg, derived_keysize,
62 NULL); 60 NULL);
63 res = crypto_wait_req(crypto_skcipher_encrypt(req), &wait); 61 res = crypto_wait_req(crypto_skcipher_encrypt(req), &wait);
64out: 62out:
@@ -67,101 +65,147 @@ out:
67 return res; 65 return res;
68} 66}
69 67
70static int validate_user_key(struct fscrypt_info *crypt_info, 68/*
71 struct fscrypt_context *ctx, u8 *raw_key, 69 * Search the current task's subscribed keyrings for a "logon" key with
72 const char *prefix, int min_keysize) 70 * description prefix:descriptor, and if found acquire a read lock on it and
71 * return a pointer to its validated payload in *payload_ret.
72 */
73static struct key *
74find_and_lock_process_key(const char *prefix,
75 const u8 descriptor[FS_KEY_DESCRIPTOR_SIZE],
76 unsigned int min_keysize,
77 const struct fscrypt_key **payload_ret)
73{ 78{
74 char *description; 79 char *description;
75 struct key *keyring_key; 80 struct key *key;
76 struct fscrypt_key *master_key;
77 const struct user_key_payload *ukp; 81 const struct user_key_payload *ukp;
78 int res; 82 const struct fscrypt_key *payload;
79 83
80 description = kasprintf(GFP_NOFS, "%s%*phN", prefix, 84 description = kasprintf(GFP_NOFS, "%s%*phN", prefix,
81 FS_KEY_DESCRIPTOR_SIZE, 85 FS_KEY_DESCRIPTOR_SIZE, descriptor);
82 ctx->master_key_descriptor);
83 if (!description) 86 if (!description)
84 return -ENOMEM; 87 return ERR_PTR(-ENOMEM);
85 88
86 keyring_key = request_key(&key_type_logon, description, NULL); 89 key = request_key(&key_type_logon, description, NULL);
87 kfree(description); 90 kfree(description);
88 if (IS_ERR(keyring_key)) 91 if (IS_ERR(key))
89 return PTR_ERR(keyring_key); 92 return key;
90 down_read(&keyring_key->sem); 93
91 94 down_read(&key->sem);
92 if (keyring_key->type != &key_type_logon) { 95 ukp = user_key_payload_locked(key);
93 printk_once(KERN_WARNING 96
94 "%s: key type must be logon\n", __func__); 97 if (!ukp) /* was the key revoked before we acquired its semaphore? */
95 res = -ENOKEY; 98 goto invalid;
96 goto out; 99
97 } 100 payload = (const struct fscrypt_key *)ukp->data;
98 ukp = user_key_payload_locked(keyring_key); 101
99 if (!ukp) { 102 if (ukp->datalen != sizeof(struct fscrypt_key) ||
100 /* key was revoked before we acquired its semaphore */ 103 payload->size < 1 || payload->size > FS_MAX_KEY_SIZE) {
101 res = -EKEYREVOKED; 104 fscrypt_warn(NULL,
102 goto out; 105 "key with description '%s' has invalid payload",
106 key->description);
107 goto invalid;
103 } 108 }
104 if (ukp->datalen != sizeof(struct fscrypt_key)) { 109
105 res = -EINVAL; 110 if (payload->size < min_keysize) {
106 goto out; 111 fscrypt_warn(NULL,
112 "key with description '%s' is too short (got %u bytes, need %u+ bytes)",
113 key->description, payload->size, min_keysize);
114 goto invalid;
107 } 115 }
108 master_key = (struct fscrypt_key *)ukp->data; 116
109 BUILD_BUG_ON(FS_AES_128_ECB_KEY_SIZE != FS_KEY_DERIVATION_NONCE_SIZE); 117 *payload_ret = payload;
110 118 return key;
111 if (master_key->size < min_keysize || master_key->size > FS_MAX_KEY_SIZE 119
112 || master_key->size % AES_BLOCK_SIZE != 0) { 120invalid:
113 printk_once(KERN_WARNING 121 up_read(&key->sem);
114 "%s: key size incorrect: %d\n", 122 key_put(key);
115 __func__, master_key->size); 123 return ERR_PTR(-ENOKEY);
116 res = -ENOKEY; 124}
117 goto out; 125
126/* Find the master key, then derive the inode's actual encryption key */
127static 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);
118 } 142 }
119 res = derive_key_aes(ctx->nonce, master_key, raw_key); 143 if (IS_ERR(key))
120out: 144 return PTR_ERR(key);
121 up_read(&keyring_key->sem); 145 err = derive_key_aes(payload->raw, ctx, derived_key, derived_keysize);
122 key_put(keyring_key); 146 up_read(&key->sem);
123 return res; 147 key_put(key);
148 return err;
124} 149}
125 150
126static const struct { 151static struct fscrypt_mode {
152 const char *friendly_name;
127 const char *cipher_str; 153 const char *cipher_str;
128 int keysize; 154 int keysize;
155 bool logged_impl_name;
129} available_modes[] = { 156} available_modes[] = {
130 [FS_ENCRYPTION_MODE_AES_256_XTS] = { "xts(aes)", 157 [FS_ENCRYPTION_MODE_AES_256_XTS] = {
131 FS_AES_256_XTS_KEY_SIZE }, 158 .friendly_name = "AES-256-XTS",
132 [FS_ENCRYPTION_MODE_AES_256_CTS] = { "cts(cbc(aes))", 159 .cipher_str = "xts(aes)",
133 FS_AES_256_CTS_KEY_SIZE }, 160 .keysize = 64,
134 [FS_ENCRYPTION_MODE_AES_128_CBC] = { "cbc(aes)", 161 },
135 FS_AES_128_CBC_KEY_SIZE }, 162 [FS_ENCRYPTION_MODE_AES_256_CTS] = {
136 [FS_ENCRYPTION_MODE_AES_128_CTS] = { "cts(cbc(aes))", 163 .friendly_name = "AES-256-CTS-CBC",
137 FS_AES_128_CTS_KEY_SIZE }, 164 .cipher_str = "cts(cbc(aes))",
165 .keysize = 32,
166 },
167 [FS_ENCRYPTION_MODE_AES_128_CBC] = {
168 .friendly_name = "AES-128-CBC",
169 .cipher_str = "cbc(aes)",
170 .keysize = 16,
171 },
172 [FS_ENCRYPTION_MODE_AES_128_CTS] = {
173 .friendly_name = "AES-128-CTS-CBC",
174 .cipher_str = "cts(cbc(aes))",
175 .keysize = 16,
176 },
177 [FS_ENCRYPTION_MODE_SPECK128_256_XTS] = {
178 .friendly_name = "Speck128/256-XTS",
179 .cipher_str = "xts(speck128)",
180 .keysize = 64,
181 },
182 [FS_ENCRYPTION_MODE_SPECK128_256_CTS] = {
183 .friendly_name = "Speck128/256-CTS-CBC",
184 .cipher_str = "cts(cbc(speck128))",
185 .keysize = 32,
186 },
138}; 187};
139 188
140static int determine_cipher_type(struct fscrypt_info *ci, struct inode *inode, 189static struct fscrypt_mode *
141 const char **cipher_str_ret, int *keysize_ret) 190select_encryption_mode(const struct fscrypt_info *ci, const struct inode *inode)
142{ 191{
143 u32 mode;
144
145 if (!fscrypt_valid_enc_modes(ci->ci_data_mode, ci->ci_filename_mode)) { 192 if (!fscrypt_valid_enc_modes(ci->ci_data_mode, ci->ci_filename_mode)) {
146 pr_warn_ratelimited("fscrypt: inode %lu uses unsupported encryption modes (contents mode %d, filenames mode %d)\n", 193 fscrypt_warn(inode->i_sb,
147 inode->i_ino, 194 "inode %lu uses unsupported encryption modes (contents mode %d, filenames mode %d)",
148 ci->ci_data_mode, ci->ci_filename_mode); 195 inode->i_ino, ci->ci_data_mode,
149 return -EINVAL; 196 ci->ci_filename_mode);
197 return ERR_PTR(-EINVAL);
150 } 198 }
151 199
152 if (S_ISREG(inode->i_mode)) { 200 if (S_ISREG(inode->i_mode))
153 mode = ci->ci_data_mode; 201 return &available_modes[ci->ci_data_mode];
154 } else if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) { 202
155 mode = ci->ci_filename_mode; 203 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
156 } else { 204 return &available_modes[ci->ci_filename_mode];
157 WARN_ONCE(1, "fscrypt: filesystem tried to load encryption info for inode %lu, which is not encryptable (file type %d)\n",
158 inode->i_ino, (inode->i_mode & S_IFMT));
159 return -EINVAL;
160 }
161 205
162 *cipher_str_ret = available_modes[mode].cipher_str; 206 WARN_ONCE(1, "fscrypt: filesystem tried to load encryption info for inode %lu, which is not encryptable (file type %d)\n",
163 *keysize_ret = available_modes[mode].keysize; 207 inode->i_ino, (inode->i_mode & S_IFMT));
164 return 0; 208 return ERR_PTR(-EINVAL);
165} 209}
166 210
167static void put_crypt_info(struct fscrypt_info *ci) 211static void put_crypt_info(struct fscrypt_info *ci)
@@ -184,8 +228,9 @@ static int derive_essiv_salt(const u8 *key, int keysize, u8 *salt)
184 228
185 tfm = crypto_alloc_shash("sha256", 0, 0); 229 tfm = crypto_alloc_shash("sha256", 0, 0);
186 if (IS_ERR(tfm)) { 230 if (IS_ERR(tfm)) {
187 pr_warn_ratelimited("fscrypt: error allocating SHA-256 transform: %ld\n", 231 fscrypt_warn(NULL,
188 PTR_ERR(tfm)); 232 "error allocating SHA-256 transform: %ld",
233 PTR_ERR(tfm));
189 return PTR_ERR(tfm); 234 return PTR_ERR(tfm);
190 } 235 }
191 prev_tfm = cmpxchg(&essiv_hash_tfm, NULL, tfm); 236 prev_tfm = cmpxchg(&essiv_hash_tfm, NULL, tfm);
@@ -245,8 +290,7 @@ int fscrypt_get_encryption_info(struct inode *inode)
245 struct fscrypt_info *crypt_info; 290 struct fscrypt_info *crypt_info;
246 struct fscrypt_context ctx; 291 struct fscrypt_context ctx;
247 struct crypto_skcipher *ctfm; 292 struct crypto_skcipher *ctfm;
248 const char *cipher_str; 293 struct fscrypt_mode *mode;
249 int keysize;
250 u8 *raw_key = NULL; 294 u8 *raw_key = NULL;
251 int res; 295 int res;
252 296
@@ -290,57 +334,59 @@ int fscrypt_get_encryption_info(struct inode *inode)
290 memcpy(crypt_info->ci_master_key, ctx.master_key_descriptor, 334 memcpy(crypt_info->ci_master_key, ctx.master_key_descriptor,
291 sizeof(crypt_info->ci_master_key)); 335 sizeof(crypt_info->ci_master_key));
292 336
293 res = determine_cipher_type(crypt_info, inode, &cipher_str, &keysize); 337 mode = select_encryption_mode(crypt_info, inode);
294 if (res) 338 if (IS_ERR(mode)) {
339 res = PTR_ERR(mode);
295 goto out; 340 goto out;
341 }
296 342
297 /* 343 /*
298 * This cannot be a stack buffer because it is passed to the scatterlist 344 * This cannot be a stack buffer because it is passed to the scatterlist
299 * crypto API as part of key derivation. 345 * crypto API as part of key derivation.
300 */ 346 */
301 res = -ENOMEM; 347 res = -ENOMEM;
302 raw_key = kmalloc(FS_MAX_KEY_SIZE, GFP_NOFS); 348 raw_key = kmalloc(mode->keysize, GFP_NOFS);
303 if (!raw_key) 349 if (!raw_key)
304 goto out; 350 goto out;
305 351
306 res = validate_user_key(crypt_info, &ctx, raw_key, FS_KEY_DESC_PREFIX, 352 res = find_and_derive_key(inode, &ctx, raw_key, mode->keysize);
307 keysize); 353 if (res)
308 if (res && inode->i_sb->s_cop->key_prefix) {
309 int res2 = validate_user_key(crypt_info, &ctx, raw_key,
310 inode->i_sb->s_cop->key_prefix,
311 keysize);
312 if (res2) {
313 if (res2 == -ENOKEY)
314 res = -ENOKEY;
315 goto out;
316 }
317 } else if (res) {
318 goto out; 354 goto out;
319 } 355
320 ctfm = crypto_alloc_skcipher(cipher_str, 0, 0); 356 ctfm = crypto_alloc_skcipher(mode->cipher_str, 0, 0);
321 if (!ctfm || IS_ERR(ctfm)) { 357 if (IS_ERR(ctfm)) {
322 res = ctfm ? PTR_ERR(ctfm) : -ENOMEM; 358 res = PTR_ERR(ctfm);
323 pr_debug("%s: error %d (inode %lu) allocating crypto tfm\n", 359 fscrypt_warn(inode->i_sb,
324 __func__, res, inode->i_ino); 360 "error allocating '%s' transform for inode %lu: %d",
361 mode->cipher_str, inode->i_ino, res);
325 goto out; 362 goto out;
326 } 363 }
364 if (unlikely(!mode->logged_impl_name)) {
365 /*
366 * fscrypt performance can vary greatly depending on which
367 * crypto algorithm implementation is used. Help people debug
368 * performance problems by logging the ->cra_driver_name the
369 * first time a mode is used. Note that multiple threads can
370 * race here, but it doesn't really matter.
371 */
372 mode->logged_impl_name = true;
373 pr_info("fscrypt: %s using implementation \"%s\"\n",
374 mode->friendly_name,
375 crypto_skcipher_alg(ctfm)->base.cra_driver_name);
376 }
327 crypt_info->ci_ctfm = ctfm; 377 crypt_info->ci_ctfm = ctfm;
328 crypto_skcipher_clear_flags(ctfm, ~0);
329 crypto_skcipher_set_flags(ctfm, CRYPTO_TFM_REQ_WEAK_KEY); 378 crypto_skcipher_set_flags(ctfm, CRYPTO_TFM_REQ_WEAK_KEY);
330 /* 379 res = crypto_skcipher_setkey(ctfm, raw_key, mode->keysize);
331 * if the provided key is longer than keysize, we use the first
332 * keysize bytes of the derived key only
333 */
334 res = crypto_skcipher_setkey(ctfm, raw_key, keysize);
335 if (res) 380 if (res)
336 goto out; 381 goto out;
337 382
338 if (S_ISREG(inode->i_mode) && 383 if (S_ISREG(inode->i_mode) &&
339 crypt_info->ci_data_mode == FS_ENCRYPTION_MODE_AES_128_CBC) { 384 crypt_info->ci_data_mode == FS_ENCRYPTION_MODE_AES_128_CBC) {
340 res = init_essiv_generator(crypt_info, raw_key, keysize); 385 res = init_essiv_generator(crypt_info, raw_key, mode->keysize);
341 if (res) { 386 if (res) {
342 pr_debug("%s: error %d (inode %lu) allocating essiv tfm\n", 387 fscrypt_warn(inode->i_sb,
343 __func__, res, inode->i_ino); 388 "error initializing ESSIV generator for inode %lu: %d",
389 inode->i_ino, res);
344 goto out; 390 goto out;
345 } 391 }
346 } 392 }
diff --git a/fs/ext4/super.c b/fs/ext4/super.c
index 7022d843c667..00fe75a71c4b 100644
--- a/fs/ext4/super.c
+++ b/fs/ext4/super.c
@@ -1267,19 +1267,13 @@ static bool ext4_dummy_context(struct inode *inode)
1267 return DUMMY_ENCRYPTION_ENABLED(EXT4_SB(inode->i_sb)); 1267 return DUMMY_ENCRYPTION_ENABLED(EXT4_SB(inode->i_sb));
1268} 1268}
1269 1269
1270static unsigned ext4_max_namelen(struct inode *inode)
1271{
1272 return S_ISLNK(inode->i_mode) ? inode->i_sb->s_blocksize :
1273 EXT4_NAME_LEN;
1274}
1275
1276static const struct fscrypt_operations ext4_cryptops = { 1270static const struct fscrypt_operations ext4_cryptops = {
1277 .key_prefix = "ext4:", 1271 .key_prefix = "ext4:",
1278 .get_context = ext4_get_context, 1272 .get_context = ext4_get_context,
1279 .set_context = ext4_set_context, 1273 .set_context = ext4_set_context,
1280 .dummy_context = ext4_dummy_context, 1274 .dummy_context = ext4_dummy_context,
1281 .empty_dir = ext4_empty_dir, 1275 .empty_dir = ext4_empty_dir,
1282 .max_namelen = ext4_max_namelen, 1276 .max_namelen = EXT4_NAME_LEN,
1283}; 1277};
1284#endif 1278#endif
1285 1279
diff --git a/fs/f2fs/super.c b/fs/f2fs/super.c
index 42d564c5ccd0..970ae27f401c 100644
--- a/fs/f2fs/super.c
+++ b/fs/f2fs/super.c
@@ -1930,19 +1930,13 @@ static bool f2fs_dummy_context(struct inode *inode)
1930 return DUMMY_ENCRYPTION_ENABLED(F2FS_I_SB(inode)); 1930 return DUMMY_ENCRYPTION_ENABLED(F2FS_I_SB(inode));
1931} 1931}
1932 1932
1933static unsigned f2fs_max_namelen(struct inode *inode)
1934{
1935 return S_ISLNK(inode->i_mode) ?
1936 inode->i_sb->s_blocksize : F2FS_NAME_LEN;
1937}
1938
1939static const struct fscrypt_operations f2fs_cryptops = { 1933static const struct fscrypt_operations f2fs_cryptops = {
1940 .key_prefix = "f2fs:", 1934 .key_prefix = "f2fs:",
1941 .get_context = f2fs_get_context, 1935 .get_context = f2fs_get_context,
1942 .set_context = f2fs_set_context, 1936 .set_context = f2fs_set_context,
1943 .dummy_context = f2fs_dummy_context, 1937 .dummy_context = f2fs_dummy_context,
1944 .empty_dir = f2fs_empty_dir, 1938 .empty_dir = f2fs_empty_dir,
1945 .max_namelen = f2fs_max_namelen, 1939 .max_namelen = F2FS_NAME_LEN,
1946}; 1940};
1947#endif 1941#endif
1948 1942
diff --git a/fs/ubifs/crypto.c b/fs/ubifs/crypto.c
index 616a688f5d8f..55c508fe8131 100644
--- a/fs/ubifs/crypto.c
+++ b/fs/ubifs/crypto.c
@@ -24,14 +24,6 @@ static bool ubifs_crypt_empty_dir(struct inode *inode)
24 return ubifs_check_dir_empty(inode) == 0; 24 return ubifs_check_dir_empty(inode) == 0;
25} 25}
26 26
27static unsigned int ubifs_crypt_max_namelen(struct inode *inode)
28{
29 if (S_ISLNK(inode->i_mode))
30 return UBIFS_MAX_INO_DATA;
31 else
32 return UBIFS_MAX_NLEN;
33}
34
35int ubifs_encrypt(const struct inode *inode, struct ubifs_data_node *dn, 27int ubifs_encrypt(const struct inode *inode, struct ubifs_data_node *dn,
36 unsigned int in_len, unsigned int *out_len, int block) 28 unsigned int in_len, unsigned int *out_len, int block)
37{ 29{
@@ -89,5 +81,5 @@ const struct fscrypt_operations ubifs_crypt_operations = {
89 .get_context = ubifs_crypt_get_context, 81 .get_context = ubifs_crypt_get_context,
90 .set_context = ubifs_crypt_set_context, 82 .set_context = ubifs_crypt_set_context,
91 .empty_dir = ubifs_crypt_empty_dir, 83 .empty_dir = ubifs_crypt_empty_dir,
92 .max_namelen = ubifs_crypt_max_namelen, 84 .max_namelen = UBIFS_MAX_NLEN,
93}; 85};
diff --git a/include/linux/fs.h b/include/linux/fs.h
index f4b8c8dc67e3..7ef7193488b8 100644
--- a/include/linux/fs.h
+++ b/include/linux/fs.h
@@ -1364,9 +1364,9 @@ struct super_block {
1364 void *s_security; 1364 void *s_security;
1365#endif 1365#endif
1366 const struct xattr_handler **s_xattr; 1366 const struct xattr_handler **s_xattr;
1367 1367#if IS_ENABLED(CONFIG_FS_ENCRYPTION)
1368 const struct fscrypt_operations *s_cop; 1368 const struct fscrypt_operations *s_cop;
1369 1369#endif
1370 struct hlist_bl_head s_roots; /* alternate root dentries for NFS */ 1370 struct hlist_bl_head s_roots; /* alternate root dentries for NFS */
1371 struct list_head s_mounts; /* list of mounts; _not_ for fs use */ 1371 struct list_head s_mounts; /* list of mounts; _not_ for fs use */
1372 struct block_device *s_bdev; 1372 struct block_device *s_bdev;
diff --git a/include/linux/fscrypt_notsupp.h b/include/linux/fscrypt_notsupp.h
index 44b50c04bae9..25b6492de6e5 100644
--- a/include/linux/fscrypt_notsupp.h
+++ b/include/linux/fscrypt_notsupp.h
@@ -64,16 +64,6 @@ static inline void fscrypt_restore_control_page(struct page *page)
64 return; 64 return;
65} 65}
66 66
67static inline void fscrypt_set_d_op(struct dentry *dentry)
68{
69 return;
70}
71
72static inline void fscrypt_set_encrypted_dentry(struct dentry *dentry)
73{
74 return;
75}
76
77/* policy.c */ 67/* policy.c */
78static inline int fscrypt_ioctl_set_policy(struct file *filp, 68static inline int fscrypt_ioctl_set_policy(struct file *filp,
79 const void __user *arg) 69 const void __user *arg)
diff --git a/include/linux/fscrypt_supp.h b/include/linux/fscrypt_supp.h
index 477a7a6504d2..5080cb1bec4c 100644
--- a/include/linux/fscrypt_supp.h
+++ b/include/linux/fscrypt_supp.h
@@ -29,7 +29,7 @@ struct fscrypt_operations {
29 int (*set_context)(struct inode *, const void *, size_t, void *); 29 int (*set_context)(struct inode *, const void *, size_t, void *);
30 bool (*dummy_context)(struct inode *); 30 bool (*dummy_context)(struct inode *);
31 bool (*empty_dir)(struct inode *); 31 bool (*empty_dir)(struct inode *);
32 unsigned (*max_namelen)(struct inode *); 32 unsigned int max_namelen;
33}; 33};
34 34
35struct fscrypt_ctx { 35struct fscrypt_ctx {
@@ -74,20 +74,6 @@ static inline struct page *fscrypt_control_page(struct page *page)
74 74
75extern void fscrypt_restore_control_page(struct page *); 75extern void fscrypt_restore_control_page(struct page *);
76 76
77extern const struct dentry_operations fscrypt_d_ops;
78
79static inline void fscrypt_set_d_op(struct dentry *dentry)
80{
81 d_set_d_op(dentry, &fscrypt_d_ops);
82}
83
84static inline void fscrypt_set_encrypted_dentry(struct dentry *dentry)
85{
86 spin_lock(&dentry->d_lock);
87 dentry->d_flags |= DCACHE_ENCRYPTED_WITH_KEY;
88 spin_unlock(&dentry->d_lock);
89}
90
91/* policy.c */ 77/* policy.c */
92extern int fscrypt_ioctl_set_policy(struct file *, const void __user *); 78extern int fscrypt_ioctl_set_policy(struct file *, const void __user *);
93extern int fscrypt_ioctl_get_policy(struct file *, void __user *); 79extern int fscrypt_ioctl_get_policy(struct file *, void __user *);
diff --git a/include/uapi/linux/fs.h b/include/uapi/linux/fs.h
index 9d132f8f2df8..73e01918f996 100644
--- a/include/uapi/linux/fs.h
+++ b/include/uapi/linux/fs.h
@@ -279,6 +279,8 @@ struct fsxattr {
279#define FS_ENCRYPTION_MODE_AES_256_CTS 4 279#define FS_ENCRYPTION_MODE_AES_256_CTS 4
280#define FS_ENCRYPTION_MODE_AES_128_CBC 5 280#define FS_ENCRYPTION_MODE_AES_128_CBC 5
281#define FS_ENCRYPTION_MODE_AES_128_CTS 6 281#define FS_ENCRYPTION_MODE_AES_128_CTS 6
282#define FS_ENCRYPTION_MODE_SPECK128_256_XTS 7
283#define FS_ENCRYPTION_MODE_SPECK128_256_CTS 8
282 284
283struct fscrypt_policy { 285struct fscrypt_policy {
284 __u8 version; 286 __u8 version;