mirror of
https://github.com/torvalds/linux.git
synced 2024-11-11 06:31:49 +00:00
7af3ea189a
This is useless and more importantly not allowed on the writeback path, because crypto_alloc_skcipher() allocates memory with GFP_KERNEL, which can recurse back into the filesystem: kworker/9:3 D ffff92303f318180 0 20732 2 0x00000080 Workqueue: ceph-msgr ceph_con_workfn [libceph] ffff923035dd4480 ffff923038f8a0c0 0000000000000001 000000009eb27318 ffff92269eb28000 ffff92269eb27338 ffff923036b145ac ffff923035dd4480 00000000ffffffff ffff923036b145b0 ffffffff951eb4e1 ffff923036b145a8 Call Trace: [<ffffffff951eb4e1>] ? schedule+0x31/0x80 [<ffffffff951eb77a>] ? schedule_preempt_disabled+0xa/0x10 [<ffffffff951ed1f4>] ? __mutex_lock_slowpath+0xb4/0x130 [<ffffffff951ed28b>] ? mutex_lock+0x1b/0x30 [<ffffffffc0a974b3>] ? xfs_reclaim_inodes_ag+0x233/0x2d0 [xfs] [<ffffffff94d92ba5>] ? move_active_pages_to_lru+0x125/0x270 [<ffffffff94f2b985>] ? radix_tree_gang_lookup_tag+0xc5/0x1c0 [<ffffffff94dad0f3>] ? __list_lru_walk_one.isra.3+0x33/0x120 [<ffffffffc0a98331>] ? xfs_reclaim_inodes_nr+0x31/0x40 [xfs] [<ffffffff94e05bfe>] ? super_cache_scan+0x17e/0x190 [<ffffffff94d919f3>] ? shrink_slab.part.38+0x1e3/0x3d0 [<ffffffff94d9616a>] ? shrink_node+0x10a/0x320 [<ffffffff94d96474>] ? do_try_to_free_pages+0xf4/0x350 [<ffffffff94d967ba>] ? try_to_free_pages+0xea/0x1b0 [<ffffffff94d863bd>] ? __alloc_pages_nodemask+0x61d/0xe60 [<ffffffff94ddf42d>] ? cache_grow_begin+0x9d/0x560 [<ffffffff94ddfb88>] ? fallback_alloc+0x148/0x1c0 [<ffffffff94ed84e7>] ? __crypto_alloc_tfm+0x37/0x130 [<ffffffff94de09db>] ? __kmalloc+0x1eb/0x580 [<ffffffffc09fe2db>] ? crush_choose_firstn+0x3eb/0x470 [libceph] [<ffffffff94ed84e7>] ? __crypto_alloc_tfm+0x37/0x130 [<ffffffff94ed9c19>] ? crypto_spawn_tfm+0x39/0x60 [<ffffffffc08b30a3>] ? crypto_cbc_init_tfm+0x23/0x40 [cbc] [<ffffffff94ed857c>] ? __crypto_alloc_tfm+0xcc/0x130 [<ffffffff94edcc23>] ? crypto_skcipher_init_tfm+0x113/0x180 [<ffffffff94ed7cc3>] ? crypto_create_tfm+0x43/0xb0 [<ffffffff94ed83b0>] ? crypto_larval_lookup+0x150/0x150 [<ffffffff94ed7da2>] ? crypto_alloc_tfm+0x72/0x120 [<ffffffffc0a01dd7>] ? ceph_aes_encrypt2+0x67/0x400 [libceph] [<ffffffffc09fd264>] ? ceph_pg_to_up_acting_osds+0x84/0x5b0 [libceph] [<ffffffff950d40a0>] ? release_sock+0x40/0x90 [<ffffffff95139f94>] ? tcp_recvmsg+0x4b4/0xae0 [<ffffffffc0a02714>] ? ceph_encrypt2+0x54/0xc0 [libceph] [<ffffffffc0a02b4d>] ? ceph_x_encrypt+0x5d/0x90 [libceph] [<ffffffffc0a02bdf>] ? calcu_signature+0x5f/0x90 [libceph] [<ffffffffc0a02ef5>] ? ceph_x_sign_message+0x35/0x50 [libceph] [<ffffffffc09e948c>] ? prepare_write_message_footer+0x5c/0xa0 [libceph] [<ffffffffc09ecd18>] ? ceph_con_workfn+0x2258/0x2dd0 [libceph] [<ffffffffc09e9903>] ? queue_con_delay+0x33/0xd0 [libceph] [<ffffffffc09f68ed>] ? __submit_request+0x20d/0x2f0 [libceph] [<ffffffffc09f6ef8>] ? ceph_osdc_start_request+0x28/0x30 [libceph] [<ffffffffc0b52603>] ? rbd_queue_workfn+0x2f3/0x350 [rbd] [<ffffffff94c94ec0>] ? process_one_work+0x160/0x410 [<ffffffff94c951bd>] ? worker_thread+0x4d/0x480 [<ffffffff94c95170>] ? process_one_work+0x410/0x410 [<ffffffff94c9af8d>] ? kthread+0xcd/0xf0 [<ffffffff951efb2f>] ? ret_from_fork+0x1f/0x40 [<ffffffff94c9aec0>] ? kthread_create_on_node+0x190/0x190 Allocating the cipher along with the key fixes the issue - as long the key doesn't change, a single cipher context can be used concurrently in multiple requests. We still can't take that GFP_KERNEL allocation though. Both ceph_crypto_key_clone() and ceph_crypto_key_decode() are called from GFP_NOFS context, so resort to memalloc_noio_{save,restore}() here. Reported-by: Lucas Stach <l.stach@pengutronix.de> Signed-off-by: Ilya Dryomov <idryomov@gmail.com> Reviewed-by: Sage Weil <sage@redhat.com>
352 lines
7.8 KiB
C
352 lines
7.8 KiB
C
|
|
#include <linux/ceph/ceph_debug.h>
|
|
|
|
#include <linux/err.h>
|
|
#include <linux/scatterlist.h>
|
|
#include <linux/slab.h>
|
|
#include <crypto/aes.h>
|
|
#include <crypto/skcipher.h>
|
|
#include <linux/key-type.h>
|
|
|
|
#include <keys/ceph-type.h>
|
|
#include <keys/user-type.h>
|
|
#include <linux/ceph/decode.h>
|
|
#include "crypto.h"
|
|
|
|
/*
|
|
* Set ->key and ->tfm. The rest of the key should be filled in before
|
|
* this function is called.
|
|
*/
|
|
static int set_secret(struct ceph_crypto_key *key, void *buf)
|
|
{
|
|
unsigned int noio_flag;
|
|
int ret;
|
|
|
|
key->key = NULL;
|
|
key->tfm = NULL;
|
|
|
|
switch (key->type) {
|
|
case CEPH_CRYPTO_NONE:
|
|
return 0; /* nothing to do */
|
|
case CEPH_CRYPTO_AES:
|
|
break;
|
|
default:
|
|
return -ENOTSUPP;
|
|
}
|
|
|
|
WARN_ON(!key->len);
|
|
key->key = kmemdup(buf, key->len, GFP_NOIO);
|
|
if (!key->key) {
|
|
ret = -ENOMEM;
|
|
goto fail;
|
|
}
|
|
|
|
/* crypto_alloc_skcipher() allocates with GFP_KERNEL */
|
|
noio_flag = memalloc_noio_save();
|
|
key->tfm = crypto_alloc_skcipher("cbc(aes)", 0, CRYPTO_ALG_ASYNC);
|
|
memalloc_noio_restore(noio_flag);
|
|
if (IS_ERR(key->tfm)) {
|
|
ret = PTR_ERR(key->tfm);
|
|
key->tfm = NULL;
|
|
goto fail;
|
|
}
|
|
|
|
ret = crypto_skcipher_setkey(key->tfm, key->key, key->len);
|
|
if (ret)
|
|
goto fail;
|
|
|
|
return 0;
|
|
|
|
fail:
|
|
ceph_crypto_key_destroy(key);
|
|
return ret;
|
|
}
|
|
|
|
int ceph_crypto_key_clone(struct ceph_crypto_key *dst,
|
|
const struct ceph_crypto_key *src)
|
|
{
|
|
memcpy(dst, src, sizeof(struct ceph_crypto_key));
|
|
return set_secret(dst, src->key);
|
|
}
|
|
|
|
int ceph_crypto_key_encode(struct ceph_crypto_key *key, void **p, void *end)
|
|
{
|
|
if (*p + sizeof(u16) + sizeof(key->created) +
|
|
sizeof(u16) + key->len > end)
|
|
return -ERANGE;
|
|
ceph_encode_16(p, key->type);
|
|
ceph_encode_copy(p, &key->created, sizeof(key->created));
|
|
ceph_encode_16(p, key->len);
|
|
ceph_encode_copy(p, key->key, key->len);
|
|
return 0;
|
|
}
|
|
|
|
int ceph_crypto_key_decode(struct ceph_crypto_key *key, void **p, void *end)
|
|
{
|
|
int ret;
|
|
|
|
ceph_decode_need(p, end, 2*sizeof(u16) + sizeof(key->created), bad);
|
|
key->type = ceph_decode_16(p);
|
|
ceph_decode_copy(p, &key->created, sizeof(key->created));
|
|
key->len = ceph_decode_16(p);
|
|
ceph_decode_need(p, end, key->len, bad);
|
|
ret = set_secret(key, *p);
|
|
*p += key->len;
|
|
return ret;
|
|
|
|
bad:
|
|
dout("failed to decode crypto key\n");
|
|
return -EINVAL;
|
|
}
|
|
|
|
int ceph_crypto_key_unarmor(struct ceph_crypto_key *key, const char *inkey)
|
|
{
|
|
int inlen = strlen(inkey);
|
|
int blen = inlen * 3 / 4;
|
|
void *buf, *p;
|
|
int ret;
|
|
|
|
dout("crypto_key_unarmor %s\n", inkey);
|
|
buf = kmalloc(blen, GFP_NOFS);
|
|
if (!buf)
|
|
return -ENOMEM;
|
|
blen = ceph_unarmor(buf, inkey, inkey+inlen);
|
|
if (blen < 0) {
|
|
kfree(buf);
|
|
return blen;
|
|
}
|
|
|
|
p = buf;
|
|
ret = ceph_crypto_key_decode(key, &p, p + blen);
|
|
kfree(buf);
|
|
if (ret)
|
|
return ret;
|
|
dout("crypto_key_unarmor key %p type %d len %d\n", key,
|
|
key->type, key->len);
|
|
return 0;
|
|
}
|
|
|
|
void ceph_crypto_key_destroy(struct ceph_crypto_key *key)
|
|
{
|
|
if (key) {
|
|
kfree(key->key);
|
|
key->key = NULL;
|
|
crypto_free_skcipher(key->tfm);
|
|
key->tfm = NULL;
|
|
}
|
|
}
|
|
|
|
static const u8 *aes_iv = (u8 *)CEPH_AES_IV;
|
|
|
|
/*
|
|
* Should be used for buffers allocated with ceph_kvmalloc().
|
|
* Currently these are encrypt out-buffer (ceph_buffer) and decrypt
|
|
* in-buffer (msg front).
|
|
*
|
|
* Dispose of @sgt with teardown_sgtable().
|
|
*
|
|
* @prealloc_sg is to avoid memory allocation inside sg_alloc_table()
|
|
* in cases where a single sg is sufficient. No attempt to reduce the
|
|
* number of sgs by squeezing physically contiguous pages together is
|
|
* made though, for simplicity.
|
|
*/
|
|
static int setup_sgtable(struct sg_table *sgt, struct scatterlist *prealloc_sg,
|
|
const void *buf, unsigned int buf_len)
|
|
{
|
|
struct scatterlist *sg;
|
|
const bool is_vmalloc = is_vmalloc_addr(buf);
|
|
unsigned int off = offset_in_page(buf);
|
|
unsigned int chunk_cnt = 1;
|
|
unsigned int chunk_len = PAGE_ALIGN(off + buf_len);
|
|
int i;
|
|
int ret;
|
|
|
|
if (buf_len == 0) {
|
|
memset(sgt, 0, sizeof(*sgt));
|
|
return -EINVAL;
|
|
}
|
|
|
|
if (is_vmalloc) {
|
|
chunk_cnt = chunk_len >> PAGE_SHIFT;
|
|
chunk_len = PAGE_SIZE;
|
|
}
|
|
|
|
if (chunk_cnt > 1) {
|
|
ret = sg_alloc_table(sgt, chunk_cnt, GFP_NOFS);
|
|
if (ret)
|
|
return ret;
|
|
} else {
|
|
WARN_ON(chunk_cnt != 1);
|
|
sg_init_table(prealloc_sg, 1);
|
|
sgt->sgl = prealloc_sg;
|
|
sgt->nents = sgt->orig_nents = 1;
|
|
}
|
|
|
|
for_each_sg(sgt->sgl, sg, sgt->orig_nents, i) {
|
|
struct page *page;
|
|
unsigned int len = min(chunk_len - off, buf_len);
|
|
|
|
if (is_vmalloc)
|
|
page = vmalloc_to_page(buf);
|
|
else
|
|
page = virt_to_page(buf);
|
|
|
|
sg_set_page(sg, page, len, off);
|
|
|
|
off = 0;
|
|
buf += len;
|
|
buf_len -= len;
|
|
}
|
|
WARN_ON(buf_len != 0);
|
|
|
|
return 0;
|
|
}
|
|
|
|
static void teardown_sgtable(struct sg_table *sgt)
|
|
{
|
|
if (sgt->orig_nents > 1)
|
|
sg_free_table(sgt);
|
|
}
|
|
|
|
static int ceph_aes_crypt(const struct ceph_crypto_key *key, bool encrypt,
|
|
void *buf, int buf_len, int in_len, int *pout_len)
|
|
{
|
|
SKCIPHER_REQUEST_ON_STACK(req, key->tfm);
|
|
struct sg_table sgt;
|
|
struct scatterlist prealloc_sg;
|
|
char iv[AES_BLOCK_SIZE];
|
|
int pad_byte = AES_BLOCK_SIZE - (in_len & (AES_BLOCK_SIZE - 1));
|
|
int crypt_len = encrypt ? in_len + pad_byte : in_len;
|
|
int ret;
|
|
|
|
WARN_ON(crypt_len > buf_len);
|
|
if (encrypt)
|
|
memset(buf + in_len, pad_byte, pad_byte);
|
|
ret = setup_sgtable(&sgt, &prealloc_sg, buf, crypt_len);
|
|
if (ret)
|
|
return ret;
|
|
|
|
memcpy(iv, aes_iv, AES_BLOCK_SIZE);
|
|
skcipher_request_set_tfm(req, key->tfm);
|
|
skcipher_request_set_callback(req, 0, NULL, NULL);
|
|
skcipher_request_set_crypt(req, sgt.sgl, sgt.sgl, crypt_len, iv);
|
|
|
|
/*
|
|
print_hex_dump(KERN_ERR, "key: ", DUMP_PREFIX_NONE, 16, 1,
|
|
key->key, key->len, 1);
|
|
print_hex_dump(KERN_ERR, " in: ", DUMP_PREFIX_NONE, 16, 1,
|
|
buf, crypt_len, 1);
|
|
*/
|
|
if (encrypt)
|
|
ret = crypto_skcipher_encrypt(req);
|
|
else
|
|
ret = crypto_skcipher_decrypt(req);
|
|
skcipher_request_zero(req);
|
|
if (ret) {
|
|
pr_err("%s %scrypt failed: %d\n", __func__,
|
|
encrypt ? "en" : "de", ret);
|
|
goto out_sgt;
|
|
}
|
|
/*
|
|
print_hex_dump(KERN_ERR, "out: ", DUMP_PREFIX_NONE, 16, 1,
|
|
buf, crypt_len, 1);
|
|
*/
|
|
|
|
if (encrypt) {
|
|
*pout_len = crypt_len;
|
|
} else {
|
|
pad_byte = *(char *)(buf + in_len - 1);
|
|
if (pad_byte > 0 && pad_byte <= AES_BLOCK_SIZE &&
|
|
in_len >= pad_byte) {
|
|
*pout_len = in_len - pad_byte;
|
|
} else {
|
|
pr_err("%s got bad padding %d on in_len %d\n",
|
|
__func__, pad_byte, in_len);
|
|
ret = -EPERM;
|
|
goto out_sgt;
|
|
}
|
|
}
|
|
|
|
out_sgt:
|
|
teardown_sgtable(&sgt);
|
|
return ret;
|
|
}
|
|
|
|
int ceph_crypt(const struct ceph_crypto_key *key, bool encrypt,
|
|
void *buf, int buf_len, int in_len, int *pout_len)
|
|
{
|
|
switch (key->type) {
|
|
case CEPH_CRYPTO_NONE:
|
|
*pout_len = in_len;
|
|
return 0;
|
|
case CEPH_CRYPTO_AES:
|
|
return ceph_aes_crypt(key, encrypt, buf, buf_len, in_len,
|
|
pout_len);
|
|
default:
|
|
return -ENOTSUPP;
|
|
}
|
|
}
|
|
|
|
static int ceph_key_preparse(struct key_preparsed_payload *prep)
|
|
{
|
|
struct ceph_crypto_key *ckey;
|
|
size_t datalen = prep->datalen;
|
|
int ret;
|
|
void *p;
|
|
|
|
ret = -EINVAL;
|
|
if (datalen <= 0 || datalen > 32767 || !prep->data)
|
|
goto err;
|
|
|
|
ret = -ENOMEM;
|
|
ckey = kmalloc(sizeof(*ckey), GFP_KERNEL);
|
|
if (!ckey)
|
|
goto err;
|
|
|
|
/* TODO ceph_crypto_key_decode should really take const input */
|
|
p = (void *)prep->data;
|
|
ret = ceph_crypto_key_decode(ckey, &p, (char*)prep->data+datalen);
|
|
if (ret < 0)
|
|
goto err_ckey;
|
|
|
|
prep->payload.data[0] = ckey;
|
|
prep->quotalen = datalen;
|
|
return 0;
|
|
|
|
err_ckey:
|
|
kfree(ckey);
|
|
err:
|
|
return ret;
|
|
}
|
|
|
|
static void ceph_key_free_preparse(struct key_preparsed_payload *prep)
|
|
{
|
|
struct ceph_crypto_key *ckey = prep->payload.data[0];
|
|
ceph_crypto_key_destroy(ckey);
|
|
kfree(ckey);
|
|
}
|
|
|
|
static void ceph_key_destroy(struct key *key)
|
|
{
|
|
struct ceph_crypto_key *ckey = key->payload.data[0];
|
|
|
|
ceph_crypto_key_destroy(ckey);
|
|
kfree(ckey);
|
|
}
|
|
|
|
struct key_type key_type_ceph = {
|
|
.name = "ceph",
|
|
.preparse = ceph_key_preparse,
|
|
.free_preparse = ceph_key_free_preparse,
|
|
.instantiate = generic_key_instantiate,
|
|
.destroy = ceph_key_destroy,
|
|
};
|
|
|
|
int ceph_crypto_init(void) {
|
|
return register_key_type(&key_type_ceph);
|
|
}
|
|
|
|
void ceph_crypto_shutdown(void) {
|
|
unregister_key_type(&key_type_ceph);
|
|
}
|