History

fscrypto: don't use on-stack buffer for filename encryption

With the new (in 4.9) option to use a virtually-mapped stack (CONFIG_VMAP_STACK), stack buffers cannot be used as input/output for the scatterlist crypto API because they may not be directly mappable to struct page. For short filenames, fname_encrypt() was encrypting a stack buffer holding the padded filename. Fix it by encrypting the filename in-place in the output buffer, thereby making the temporary buffer unnecessary. This bug could most easily be observed in a CONFIG_DEBUG_SG kernel because this allowed the BUG in sg_set_buf() to be triggered.

Eric Biggers committed 10 years ago

Commit 3c7018e1 parent bc33b0c1 file · +21 −32Browse files

Unified · Split

fs/crypto/fname.c+21−32
@@ -39,65 +39,54 @@ static void fname_crypt_complete(struct crypto_async_request *req, int res) · 38 unmodified lines
3939 static int fname_encrypt(struct inode *inode,
4040 const struct qstr *iname, struct fscrypt_str *oname)
4141 {
42− u32 ciphertext_len;
4342 struct skcipher_request *req = NULL;
4443 DECLARE_FS_COMPLETION_RESULT(ecr);
4544 struct fscrypt_info *ci = inode->i_crypt_info;
4645 struct crypto_skcipher *tfm = ci->ci_ctfm;
4746 int res = 0;
4847 char iv[FS_CRYPTO_BLOCK_SIZE];
49− struct scatterlist src_sg, dst_sg;
48+ struct scatterlist sg;
5049 int padding = 4 << (ci->ci_flags & FS_POLICY_FLAGS_PAD_MASK);
51− char *workbuf, buf[32], *alloc_buf = NULL;
52− unsigned lim;
50+ unsigned int lim;
51+ unsigned int cryptlen;
5352
5453 lim = inode->i_sb->s_cop->max_namelen(inode);
5554 if (iname->len <= 0 || iname->len > lim)
5655 return -EIO;
5756
58− ciphertext_len = max(iname->len, (u32)FS_CRYPTO_BLOCK_SIZE);
59− ciphertext_len = round_up(ciphertext_len, padding);
60− ciphertext_len = min(ciphertext_len, lim);
57+ /*
58+ * Copy the filename to the output buffer for encrypting in-place and
59+ * pad it with the needed number of NUL bytes.
60+ */
61+ cryptlen = max_t(unsigned int, iname->len, FS_CRYPTO_BLOCK_SIZE);
62+ cryptlen = round_up(cryptlen, padding);
63+ cryptlen = min(cryptlen, lim);
64+ memcpy(oname->name, iname->name, iname->len);
65+ memset(oname->name + iname->len, 0, cryptlen - iname->len);
6166
62− if (ciphertext_len <= sizeof(buf)) {
63− workbuf = buf;
64− } else {
65− alloc_buf = kmalloc(ciphertext_len, GFP_NOFS);
66− if (!alloc_buf)
67− return -ENOMEM;
68− workbuf = alloc_buf;
69− }
67+ /* Initialize the IV */
68+ memset(iv, 0, FS_CRYPTO_BLOCK_SIZE);
7069
71− /* Allocate request */
70+ /* Set up the encryption request */
7271 req = skcipher_request_alloc(tfm, GFP_NOFS);
7372 if (!req) {
7473 printk_ratelimited(KERN_ERR
75− "%s: crypto_request_alloc() failed\n", __func__);
76− kfree(alloc_buf);
74+ "%s: skcipher_request_alloc() failed\n", __func__);
7775 return -ENOMEM;
7876 }
7977 skcipher_request_set_callback(req,
8078 CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
8179 fname_crypt_complete, &ecr);
80+ sg_init_one(&sg, oname->name, cryptlen);
81+ skcipher_request_set_crypt(req, &sg, &sg, cryptlen, iv);
8282
83− /* Copy the input */
84− memcpy(workbuf, iname->name, iname->len);
85− if (iname->len < ciphertext_len)
86− memset(workbuf + iname->len, 0, ciphertext_len - iname->len);
87−
88− /* Initialize IV */
89− memset(iv, 0, FS_CRYPTO_BLOCK_SIZE);
90−
91− /* Create encryption request */
92− sg_init_one(&src_sg, workbuf, ciphertext_len);
93− sg_init_one(&dst_sg, oname->name, ciphertext_len);
94− skcipher_request_set_crypt(req, &src_sg, &dst_sg, ciphertext_len, iv);
83+ /* Do the encryption */
9584 res = crypto_skcipher_encrypt(req);
9685 if (res == -EINPROGRESS || res == -EBUSY) {
86+ /* Request is being completed asynchronously; wait for it */
9787 wait_for_completion(&ecr.completion);
9888 res = ecr.res;
9989 }
100− kfree(alloc_buf);
10190 skcipher_request_free(req);
10291 if (res < 0) {
10392 printk_ratelimited(KERN_ERR
@@ -105,7 +94,7 @@ static int fname_encrypt(struct inode *inode, · 1 unmodified line
10594 return res;
10695 }
10796
108− oname->len = ciphertext_len;
97+ oname->len = cryptlen;
10998 return 0;
11099 }
111100

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t