2019-05-20 17:08:01 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2013-09-24 09:35:18 +00:00
|
|
|
/* Large capacity key type
|
|
|
|
*
|
2020-05-11 21:51:01 +00:00
|
|
|
* Copyright (C) 2017-2020 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
|
2013-09-24 09:35:18 +00:00
|
|
|
* Copyright (C) 2013 Red Hat, Inc. All Rights Reserved.
|
|
|
|
* Written by David Howells (dhowells@redhat.com)
|
|
|
|
*/
|
|
|
|
|
2016-10-26 14:02:01 +00:00
|
|
|
#define pr_fmt(fmt) "big_key: "fmt
|
2013-09-24 09:35:18 +00:00
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/seq_file.h>
|
|
|
|
#include <linux/file.h>
|
|
|
|
#include <linux/shmem_fs.h>
|
|
|
|
#include <linux/err.h>
|
security/keys: rewrite all of big_key crypto
This started out as just replacing the use of crypto/rng with
get_random_bytes_wait, so that we wouldn't use bad randomness at boot
time. But, upon looking further, it appears that there were even deeper
underlying cryptographic problems, and that this seems to have been
committed with very little crypto review. So, I rewrote the whole thing,
trying to keep to the conventions introduced by the previous author, to
fix these cryptographic flaws.
It makes no sense to seed crypto/rng at boot time and then keep
using it like this, when in fact there's already get_random_bytes_wait,
which can ensure there's enough entropy and be a much more standard way
of generating keys. Since this sensitive material is being stored
untrusted, using ECB and no authentication is simply not okay at all. I
find it surprising and a bit horrifying that this code even made it past
basic crypto review, which perhaps points to some larger issues. This
patch moves from using AES-ECB to using AES-GCM. Since keys are uniquely
generated each time, we can set the nonce to zero. There was also a race
condition in which the same key would be reused at the same time in
different threads. A mutex fixes this issue now.
So, to summarize, this commit fixes the following vulnerabilities:
* Low entropy key generation, allowing an attacker to potentially
guess or predict keys.
* Unauthenticated encryption, allowing an attacker to modify the
cipher text in particular ways in order to manipulate the plaintext,
which is is even more frightening considering the next point.
* Use of ECB mode, allowing an attacker to trivially swap blocks or
compare identical plaintext blocks.
* Key re-use.
* Faulty memory zeroing.
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Reviewed-by: Eric Biggers <ebiggers3@gmail.com>
Signed-off-by: David Howells <dhowells@redhat.com>
Cc: Herbert Xu <herbert@gondor.apana.org.au>
Cc: Kirill Marinushkin <k.marinushkin@gmail.com>
Cc: security@kernel.org
Cc: stable@vger.kernel.org
2017-09-20 14:58:39 +00:00
|
|
|
#include <linux/random.h>
|
2013-09-24 09:35:18 +00:00
|
|
|
#include <keys/user-type.h>
|
|
|
|
#include <keys/big_key-type.h>
|
2020-05-11 21:51:01 +00:00
|
|
|
#include <crypto/chacha20poly1305.h>
|
2018-02-22 14:38:34 +00:00
|
|
|
|
2015-10-21 13:04:48 +00:00
|
|
|
/*
|
|
|
|
* Layout of key payload words.
|
|
|
|
*/
|
2022-05-08 16:15:53 +00:00
|
|
|
struct big_key_payload {
|
|
|
|
u8 *data;
|
|
|
|
struct path path;
|
|
|
|
size_t length;
|
2015-10-21 13:04:48 +00:00
|
|
|
};
|
2022-05-08 16:15:53 +00:00
|
|
|
#define to_big_key_payload(payload) \
|
|
|
|
(struct big_key_payload *)((payload).data)
|
2015-10-21 13:04:48 +00:00
|
|
|
|
2013-09-24 09:35:18 +00:00
|
|
|
/*
|
|
|
|
* If the data is under this limit, there's no point creating a shm file to
|
|
|
|
* hold it as the permanently resident metadata for the shmem fs will be at
|
|
|
|
* least as large as the data.
|
|
|
|
*/
|
|
|
|
#define BIG_KEY_FILE_THRESHOLD (sizeof(struct inode) + sizeof(struct dentry))
|
|
|
|
|
|
|
|
/*
|
|
|
|
* big_key defined keys take an arbitrary string as the description and an
|
|
|
|
* arbitrary blob of data as the payload
|
|
|
|
*/
|
|
|
|
struct key_type key_type_big_key = {
|
|
|
|
.name = "big_key",
|
2014-07-18 17:56:36 +00:00
|
|
|
.preparse = big_key_preparse,
|
|
|
|
.free_preparse = big_key_free_preparse,
|
|
|
|
.instantiate = generic_key_instantiate,
|
2013-09-24 09:35:18 +00:00
|
|
|
.revoke = big_key_revoke,
|
|
|
|
.destroy = big_key_destroy,
|
|
|
|
.describe = big_key_describe,
|
|
|
|
.read = big_key_read,
|
2020-05-12 13:03:53 +00:00
|
|
|
.update = big_key_update,
|
2013-09-24 09:35:18 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
2014-07-18 17:56:36 +00:00
|
|
|
* Preparse a big key
|
2013-09-24 09:35:18 +00:00
|
|
|
*/
|
2014-07-18 17:56:36 +00:00
|
|
|
int big_key_preparse(struct key_preparsed_payload *prep)
|
2013-09-24 09:35:18 +00:00
|
|
|
{
|
2022-05-08 16:15:53 +00:00
|
|
|
struct big_key_payload *payload = to_big_key_payload(prep->payload);
|
2013-09-24 09:35:18 +00:00
|
|
|
struct file *file;
|
2020-05-11 21:51:01 +00:00
|
|
|
u8 *buf, *enckey;
|
2013-09-24 09:35:18 +00:00
|
|
|
ssize_t written;
|
2020-05-11 21:51:01 +00:00
|
|
|
size_t datalen = prep->datalen;
|
|
|
|
size_t enclen = datalen + CHACHA20POLY1305_AUTHTAG_SIZE;
|
2013-09-24 09:35:18 +00:00
|
|
|
int ret;
|
|
|
|
|
2022-05-08 16:15:53 +00:00
|
|
|
BUILD_BUG_ON(sizeof(*payload) != sizeof(prep->payload.data));
|
|
|
|
|
2013-09-24 09:35:18 +00:00
|
|
|
if (datalen <= 0 || datalen > 1024 * 1024 || !prep->data)
|
2018-02-22 14:38:34 +00:00
|
|
|
return -EINVAL;
|
2013-09-24 09:35:18 +00:00
|
|
|
|
|
|
|
/* Set an arbitrary quota */
|
2014-07-18 17:56:36 +00:00
|
|
|
prep->quotalen = 16;
|
2013-09-24 09:35:18 +00:00
|
|
|
|
2022-05-08 16:15:53 +00:00
|
|
|
payload->length = datalen;
|
2013-09-24 09:35:18 +00:00
|
|
|
|
|
|
|
if (datalen > BIG_KEY_FILE_THRESHOLD) {
|
|
|
|
/* Create a shmem file to store the data in. This will permit the data
|
|
|
|
* to be swapped out if needed.
|
|
|
|
*
|
2016-04-12 18:54:58 +00:00
|
|
|
* File content is stored encrypted with randomly generated key.
|
2020-05-11 21:51:01 +00:00
|
|
|
* Since the key is random for each file, we can set the nonce
|
|
|
|
* to zero, provided we never define a ->update() call.
|
2013-09-24 09:35:18 +00:00
|
|
|
*/
|
2017-09-01 15:39:14 +00:00
|
|
|
loff_t pos = 0;
|
2016-04-12 18:54:58 +00:00
|
|
|
|
2020-05-11 21:51:01 +00:00
|
|
|
buf = kvmalloc(enclen, GFP_KERNEL);
|
2018-02-22 14:38:34 +00:00
|
|
|
if (!buf)
|
2016-04-12 18:54:58 +00:00
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
/* generate random key */
|
2020-05-11 21:51:01 +00:00
|
|
|
enckey = kmalloc(CHACHA20POLY1305_KEY_SIZE, GFP_KERNEL);
|
2016-04-12 18:54:58 +00:00
|
|
|
if (!enckey) {
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto error;
|
|
|
|
}
|
2020-05-11 21:51:01 +00:00
|
|
|
ret = get_random_bytes_wait(enckey, CHACHA20POLY1305_KEY_SIZE);
|
security/keys: rewrite all of big_key crypto
This started out as just replacing the use of crypto/rng with
get_random_bytes_wait, so that we wouldn't use bad randomness at boot
time. But, upon looking further, it appears that there were even deeper
underlying cryptographic problems, and that this seems to have been
committed with very little crypto review. So, I rewrote the whole thing,
trying to keep to the conventions introduced by the previous author, to
fix these cryptographic flaws.
It makes no sense to seed crypto/rng at boot time and then keep
using it like this, when in fact there's already get_random_bytes_wait,
which can ensure there's enough entropy and be a much more standard way
of generating keys. Since this sensitive material is being stored
untrusted, using ECB and no authentication is simply not okay at all. I
find it surprising and a bit horrifying that this code even made it past
basic crypto review, which perhaps points to some larger issues. This
patch moves from using AES-ECB to using AES-GCM. Since keys are uniquely
generated each time, we can set the nonce to zero. There was also a race
condition in which the same key would be reused at the same time in
different threads. A mutex fixes this issue now.
So, to summarize, this commit fixes the following vulnerabilities:
* Low entropy key generation, allowing an attacker to potentially
guess or predict keys.
* Unauthenticated encryption, allowing an attacker to modify the
cipher text in particular ways in order to manipulate the plaintext,
which is is even more frightening considering the next point.
* Use of ECB mode, allowing an attacker to trivially swap blocks or
compare identical plaintext blocks.
* Key re-use.
* Faulty memory zeroing.
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Reviewed-by: Eric Biggers <ebiggers3@gmail.com>
Signed-off-by: David Howells <dhowells@redhat.com>
Cc: Herbert Xu <herbert@gondor.apana.org.au>
Cc: Kirill Marinushkin <k.marinushkin@gmail.com>
Cc: security@kernel.org
Cc: stable@vger.kernel.org
2017-09-20 14:58:39 +00:00
|
|
|
if (unlikely(ret))
|
2016-04-12 18:54:58 +00:00
|
|
|
goto err_enckey;
|
|
|
|
|
2020-05-11 21:51:01 +00:00
|
|
|
/* encrypt data */
|
|
|
|
chacha20poly1305_encrypt(buf, prep->data, datalen, NULL, 0,
|
|
|
|
0, enckey);
|
2016-04-12 18:54:58 +00:00
|
|
|
|
|
|
|
/* save aligned data to file */
|
|
|
|
file = shmem_kernel_file_setup("", enclen, 0);
|
2013-10-30 03:23:02 +00:00
|
|
|
if (IS_ERR(file)) {
|
|
|
|
ret = PTR_ERR(file);
|
2016-04-12 18:54:58 +00:00
|
|
|
goto err_enckey;
|
2013-10-30 03:23:02 +00:00
|
|
|
}
|
2013-09-24 09:35:18 +00:00
|
|
|
|
2020-05-11 21:51:01 +00:00
|
|
|
written = kernel_write(file, buf, enclen, &pos);
|
2016-04-12 18:54:58 +00:00
|
|
|
if (written != enclen) {
|
2013-11-13 16:51:06 +00:00
|
|
|
ret = written;
|
2013-09-24 09:35:18 +00:00
|
|
|
if (written >= 0)
|
2020-05-11 21:51:01 +00:00
|
|
|
ret = -EIO;
|
2013-09-24 09:35:18 +00:00
|
|
|
goto err_fput;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Pin the mount and dentry to the key so that we can open it again
|
|
|
|
* later
|
|
|
|
*/
|
2022-05-08 16:15:53 +00:00
|
|
|
payload->data = enckey;
|
|
|
|
payload->path = file->f_path;
|
|
|
|
path_get(&payload->path);
|
2013-09-24 09:35:18 +00:00
|
|
|
fput(file);
|
2020-08-27 07:29:23 +00:00
|
|
|
kvfree_sensitive(buf, enclen);
|
2013-09-24 09:35:18 +00:00
|
|
|
} else {
|
|
|
|
/* Just store the data in a buffer */
|
|
|
|
void *data = kmalloc(datalen, GFP_KERNEL);
|
2016-04-12 18:54:58 +00:00
|
|
|
|
2014-07-18 17:56:36 +00:00
|
|
|
if (!data)
|
|
|
|
return -ENOMEM;
|
2013-09-24 09:35:18 +00:00
|
|
|
|
2022-05-08 16:15:53 +00:00
|
|
|
payload->data = data;
|
2015-10-21 13:04:48 +00:00
|
|
|
memcpy(data, prep->data, prep->datalen);
|
2013-09-24 09:35:18 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
err_fput:
|
|
|
|
fput(file);
|
2016-04-12 18:54:58 +00:00
|
|
|
err_enckey:
|
2020-08-07 06:18:13 +00:00
|
|
|
kfree_sensitive(enckey);
|
2013-09-24 09:35:18 +00:00
|
|
|
error:
|
2020-08-27 07:29:23 +00:00
|
|
|
kvfree_sensitive(buf, enclen);
|
2013-09-24 09:35:18 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-07-18 17:56:36 +00:00
|
|
|
/*
|
|
|
|
* Clear preparsement.
|
|
|
|
*/
|
|
|
|
void big_key_free_preparse(struct key_preparsed_payload *prep)
|
|
|
|
{
|
2022-05-08 16:15:53 +00:00
|
|
|
struct big_key_payload *payload = to_big_key_payload(prep->payload);
|
2016-04-12 18:54:58 +00:00
|
|
|
|
2022-05-08 16:15:53 +00:00
|
|
|
if (prep->datalen > BIG_KEY_FILE_THRESHOLD)
|
|
|
|
path_put(&payload->path);
|
|
|
|
kfree_sensitive(payload->data);
|
2014-07-18 17:56:36 +00:00
|
|
|
}
|
|
|
|
|
2013-09-24 09:35:18 +00:00
|
|
|
/*
|
|
|
|
* dispose of the links from a revoked keyring
|
|
|
|
* - called with the key sem write-locked
|
|
|
|
*/
|
|
|
|
void big_key_revoke(struct key *key)
|
|
|
|
{
|
2022-05-08 16:15:53 +00:00
|
|
|
struct big_key_payload *payload = to_big_key_payload(key->payload);
|
2013-09-24 09:35:18 +00:00
|
|
|
|
|
|
|
/* clear the quota */
|
|
|
|
key_payload_reserve(key, 0);
|
2022-05-08 16:15:53 +00:00
|
|
|
if (key_is_positive(key) && payload->length > BIG_KEY_FILE_THRESHOLD)
|
|
|
|
vfs_truncate(&payload->path, 0);
|
2013-09-24 09:35:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* dispose of the data dangling from the corpse of a big_key key
|
|
|
|
*/
|
|
|
|
void big_key_destroy(struct key *key)
|
|
|
|
{
|
2022-05-08 16:15:53 +00:00
|
|
|
struct big_key_payload *payload = to_big_key_payload(key->payload);
|
2016-04-12 18:54:58 +00:00
|
|
|
|
2022-05-08 16:15:53 +00:00
|
|
|
if (payload->length > BIG_KEY_FILE_THRESHOLD) {
|
|
|
|
path_put(&payload->path);
|
|
|
|
payload->path.mnt = NULL;
|
|
|
|
payload->path.dentry = NULL;
|
2013-09-24 09:35:18 +00:00
|
|
|
}
|
2022-05-08 16:15:53 +00:00
|
|
|
kfree_sensitive(payload->data);
|
|
|
|
payload->data = NULL;
|
2013-09-24 09:35:18 +00:00
|
|
|
}
|
|
|
|
|
2020-05-12 13:03:53 +00:00
|
|
|
/*
|
|
|
|
* Update a big key
|
|
|
|
*/
|
|
|
|
int big_key_update(struct key *key, struct key_preparsed_payload *prep)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = key_payload_reserve(key, prep->datalen);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
if (key_is_positive(key))
|
|
|
|
big_key_destroy(key);
|
|
|
|
|
|
|
|
return generic_key_instantiate(key, prep);
|
|
|
|
}
|
|
|
|
|
2013-09-24 09:35:18 +00:00
|
|
|
/*
|
|
|
|
* describe the big_key key
|
|
|
|
*/
|
|
|
|
void big_key_describe(const struct key *key, struct seq_file *m)
|
|
|
|
{
|
2022-05-08 16:15:53 +00:00
|
|
|
struct big_key_payload *payload = to_big_key_payload(key->payload);
|
2013-09-24 09:35:18 +00:00
|
|
|
|
|
|
|
seq_puts(m, key->description);
|
|
|
|
|
2017-10-04 15:43:25 +00:00
|
|
|
if (key_is_positive(key))
|
2015-10-21 13:04:48 +00:00
|
|
|
seq_printf(m, ": %zu [%s]",
|
2022-05-08 16:15:53 +00:00
|
|
|
payload->length,
|
|
|
|
payload->length > BIG_KEY_FILE_THRESHOLD ? "file" : "buff");
|
2013-09-24 09:35:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* read the key data
|
|
|
|
* - the key's semaphore is read-locked
|
|
|
|
*/
|
2020-03-22 01:11:24 +00:00
|
|
|
long big_key_read(const struct key *key, char *buffer, size_t buflen)
|
2013-09-24 09:35:18 +00:00
|
|
|
{
|
2022-05-08 16:15:53 +00:00
|
|
|
struct big_key_payload *payload = to_big_key_payload(key->payload);
|
|
|
|
size_t datalen = payload->length;
|
2013-09-24 09:35:18 +00:00
|
|
|
long ret;
|
|
|
|
|
|
|
|
if (!buffer || buflen < datalen)
|
|
|
|
return datalen;
|
|
|
|
|
|
|
|
if (datalen > BIG_KEY_FILE_THRESHOLD) {
|
|
|
|
struct file *file;
|
2022-05-08 16:15:53 +00:00
|
|
|
u8 *buf, *enckey = payload->data;
|
2020-05-11 21:51:01 +00:00
|
|
|
size_t enclen = datalen + CHACHA20POLY1305_AUTHTAG_SIZE;
|
2017-09-01 15:39:13 +00:00
|
|
|
loff_t pos = 0;
|
2016-04-12 18:54:58 +00:00
|
|
|
|
2020-05-11 21:51:01 +00:00
|
|
|
buf = kvmalloc(enclen, GFP_KERNEL);
|
2018-02-22 14:38:34 +00:00
|
|
|
if (!buf)
|
2016-04-12 18:54:58 +00:00
|
|
|
return -ENOMEM;
|
2013-09-24 09:35:18 +00:00
|
|
|
|
2022-05-08 16:15:53 +00:00
|
|
|
file = dentry_open(&payload->path, O_RDONLY, current_cred());
|
2016-04-12 18:54:58 +00:00
|
|
|
if (IS_ERR(file)) {
|
|
|
|
ret = PTR_ERR(file);
|
|
|
|
goto error;
|
|
|
|
}
|
2013-09-24 09:35:18 +00:00
|
|
|
|
2016-04-12 18:54:58 +00:00
|
|
|
/* read file to kernel and decrypt */
|
2020-05-11 21:51:01 +00:00
|
|
|
ret = kernel_read(file, buf, enclen, &pos);
|
|
|
|
if (ret != enclen) {
|
|
|
|
if (ret >= 0)
|
|
|
|
ret = -EIO;
|
2016-04-12 18:54:58 +00:00
|
|
|
goto err_fput;
|
|
|
|
}
|
|
|
|
|
2020-05-11 21:51:01 +00:00
|
|
|
ret = chacha20poly1305_decrypt(buf, buf, enclen, NULL, 0, 0,
|
|
|
|
enckey) ? 0 : -EBADMSG;
|
|
|
|
if (unlikely(ret))
|
2016-04-12 18:54:58 +00:00
|
|
|
goto err_fput;
|
|
|
|
|
|
|
|
ret = datalen;
|
|
|
|
|
2020-03-22 01:11:24 +00:00
|
|
|
/* copy out decrypted data */
|
2020-05-11 21:51:01 +00:00
|
|
|
memcpy(buffer, buf, datalen);
|
2016-04-12 18:54:58 +00:00
|
|
|
|
|
|
|
err_fput:
|
|
|
|
fput(file);
|
|
|
|
error:
|
2020-08-27 07:29:23 +00:00
|
|
|
kvfree_sensitive(buf, enclen);
|
2013-09-24 09:35:18 +00:00
|
|
|
} else {
|
|
|
|
ret = datalen;
|
2022-05-08 16:15:53 +00:00
|
|
|
memcpy(buffer, payload->data, datalen);
|
2013-09-24 09:35:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-04-12 18:54:58 +00:00
|
|
|
/*
|
|
|
|
* Register key type
|
|
|
|
*/
|
2013-09-24 09:35:18 +00:00
|
|
|
static int __init big_key_init(void)
|
|
|
|
{
|
2020-05-11 21:51:01 +00:00
|
|
|
return register_key_type(&key_type_big_key);
|
2016-04-12 18:54:58 +00:00
|
|
|
}
|
|
|
|
|
2016-10-26 14:02:01 +00:00
|
|
|
late_initcall(big_key_init);
|