2019-05-21 01:08:01 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2013-09-24 17:35:18 +08:00
|
|
|
/* Large capacity key type
|
|
|
|
*
|
2020-05-12 05:51:01 +08:00
|
|
|
* Copyright (C) 2017-2020 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
|
2013-09-24 17:35:18 +08:00
|
|
|
* Copyright (C) 2013 Red Hat, Inc. All Rights Reserved.
|
|
|
|
* Written by David Howells (dhowells@redhat.com)
|
|
|
|
*/
|
|
|
|
|
2016-10-26 22:02:01 +08:00
|
|
|
#define pr_fmt(fmt) "big_key: "fmt
|
2013-09-24 17:35:18 +08: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 22:58:39 +08:00
|
|
|
#include <linux/random.h>
|
2013-09-24 17:35:18 +08:00
|
|
|
#include <keys/user-type.h>
|
|
|
|
#include <keys/big_key-type.h>
|
2020-05-12 05:51:01 +08:00
|
|
|
#include <crypto/chacha20poly1305.h>
|
2018-02-22 22:38:34 +08:00
|
|
|
|
2015-10-21 21:04:48 +08:00
|
|
|
/*
|
|
|
|
* Layout of key payload words.
|
|
|
|
*/
|
|
|
|
enum {
|
|
|
|
big_key_data,
|
|
|
|
big_key_path,
|
|
|
|
big_key_path_2nd_part,
|
|
|
|
big_key_len,
|
|
|
|
};
|
|
|
|
|
2013-09-24 17:35:18 +08: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-19 01:56:36 +08:00
|
|
|
.preparse = big_key_preparse,
|
|
|
|
.free_preparse = big_key_free_preparse,
|
|
|
|
.instantiate = generic_key_instantiate,
|
2013-09-24 17:35:18 +08:00
|
|
|
.revoke = big_key_revoke,
|
|
|
|
.destroy = big_key_destroy,
|
|
|
|
.describe = big_key_describe,
|
|
|
|
.read = big_key_read,
|
2020-05-12 05:51:01 +08:00
|
|
|
/* no ->update(); don't add it without changing chacha20poly1305's nonce */
|
2013-09-24 17:35:18 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
2014-07-19 01:56:36 +08:00
|
|
|
* Preparse a big key
|
2013-09-24 17:35:18 +08:00
|
|
|
*/
|
2014-07-19 01:56:36 +08:00
|
|
|
int big_key_preparse(struct key_preparsed_payload *prep)
|
2013-09-24 17:35:18 +08:00
|
|
|
{
|
2015-10-21 21:04:48 +08:00
|
|
|
struct path *path = (struct path *)&prep->payload.data[big_key_path];
|
2013-09-24 17:35:18 +08:00
|
|
|
struct file *file;
|
2020-05-12 05:51:01 +08:00
|
|
|
u8 *buf, *enckey;
|
2013-09-24 17:35:18 +08:00
|
|
|
ssize_t written;
|
2020-05-12 05:51:01 +08:00
|
|
|
size_t datalen = prep->datalen;
|
|
|
|
size_t enclen = datalen + CHACHA20POLY1305_AUTHTAG_SIZE;
|
2013-09-24 17:35:18 +08:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (datalen <= 0 || datalen > 1024 * 1024 || !prep->data)
|
2018-02-22 22:38:34 +08:00
|
|
|
return -EINVAL;
|
2013-09-24 17:35:18 +08:00
|
|
|
|
|
|
|
/* Set an arbitrary quota */
|
2014-07-19 01:56:36 +08:00
|
|
|
prep->quotalen = 16;
|
2013-09-24 17:35:18 +08:00
|
|
|
|
2015-10-21 21:04:48 +08:00
|
|
|
prep->payload.data[big_key_len] = (void *)(unsigned long)datalen;
|
2013-09-24 17:35:18 +08: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-13 02:54:58 +08:00
|
|
|
* File content is stored encrypted with randomly generated key.
|
2020-05-12 05:51:01 +08: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 17:35:18 +08:00
|
|
|
*/
|
2017-09-01 23:39:14 +08:00
|
|
|
loff_t pos = 0;
|
2016-04-13 02:54:58 +08:00
|
|
|
|
2020-05-12 05:51:01 +08:00
|
|
|
buf = kvmalloc(enclen, GFP_KERNEL);
|
2018-02-22 22:38:34 +08:00
|
|
|
if (!buf)
|
2016-04-13 02:54:58 +08:00
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
/* generate random key */
|
2020-05-12 05:51:01 +08:00
|
|
|
enckey = kmalloc(CHACHA20POLY1305_KEY_SIZE, GFP_KERNEL);
|
2016-04-13 02:54:58 +08:00
|
|
|
if (!enckey) {
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto error;
|
|
|
|
}
|
2020-05-12 05:51:01 +08: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 22:58:39 +08:00
|
|
|
if (unlikely(ret))
|
2016-04-13 02:54:58 +08:00
|
|
|
goto err_enckey;
|
|
|
|
|
2020-05-12 05:51:01 +08:00
|
|
|
/* encrypt data */
|
|
|
|
chacha20poly1305_encrypt(buf, prep->data, datalen, NULL, 0,
|
|
|
|
0, enckey);
|
2016-04-13 02:54:58 +08:00
|
|
|
|
|
|
|
/* save aligned data to file */
|
|
|
|
file = shmem_kernel_file_setup("", enclen, 0);
|
2013-10-30 11:23:02 +08:00
|
|
|
if (IS_ERR(file)) {
|
|
|
|
ret = PTR_ERR(file);
|
2016-04-13 02:54:58 +08:00
|
|
|
goto err_enckey;
|
2013-10-30 11:23:02 +08:00
|
|
|
}
|
2013-09-24 17:35:18 +08:00
|
|
|
|
2020-05-12 05:51:01 +08:00
|
|
|
written = kernel_write(file, buf, enclen, &pos);
|
2016-04-13 02:54:58 +08:00
|
|
|
if (written != enclen) {
|
2013-11-14 00:51:06 +08:00
|
|
|
ret = written;
|
2013-09-24 17:35:18 +08:00
|
|
|
if (written >= 0)
|
2020-05-12 05:51:01 +08:00
|
|
|
ret = -EIO;
|
2013-09-24 17:35:18 +08:00
|
|
|
goto err_fput;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Pin the mount and dentry to the key so that we can open it again
|
|
|
|
* later
|
|
|
|
*/
|
2016-04-13 02:54:58 +08:00
|
|
|
prep->payload.data[big_key_data] = enckey;
|
2013-09-24 17:35:18 +08:00
|
|
|
*path = file->f_path;
|
|
|
|
path_get(path);
|
|
|
|
fput(file);
|
2020-05-12 05:51:01 +08:00
|
|
|
memzero_explicit(buf, enclen);
|
|
|
|
kvfree(buf);
|
2013-09-24 17:35:18 +08:00
|
|
|
} else {
|
|
|
|
/* Just store the data in a buffer */
|
|
|
|
void *data = kmalloc(datalen, GFP_KERNEL);
|
2016-04-13 02:54:58 +08:00
|
|
|
|
2014-07-19 01:56:36 +08:00
|
|
|
if (!data)
|
|
|
|
return -ENOMEM;
|
2013-09-24 17:35:18 +08:00
|
|
|
|
2015-10-21 21:04:48 +08:00
|
|
|
prep->payload.data[big_key_data] = data;
|
|
|
|
memcpy(data, prep->data, prep->datalen);
|
2013-09-24 17:35:18 +08:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
err_fput:
|
|
|
|
fput(file);
|
2016-04-13 02:54:58 +08:00
|
|
|
err_enckey:
|
2017-09-20 22:58:38 +08:00
|
|
|
kzfree(enckey);
|
2013-09-24 17:35:18 +08:00
|
|
|
error:
|
2020-05-12 05:51:01 +08:00
|
|
|
memzero_explicit(buf, enclen);
|
|
|
|
kvfree(buf);
|
2013-09-24 17:35:18 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-07-19 01:56:36 +08:00
|
|
|
/*
|
|
|
|
* Clear preparsement.
|
|
|
|
*/
|
|
|
|
void big_key_free_preparse(struct key_preparsed_payload *prep)
|
|
|
|
{
|
|
|
|
if (prep->datalen > BIG_KEY_FILE_THRESHOLD) {
|
2015-10-21 21:04:48 +08:00
|
|
|
struct path *path = (struct path *)&prep->payload.data[big_key_path];
|
2016-04-13 02:54:58 +08:00
|
|
|
|
2014-07-19 01:56:36 +08:00
|
|
|
path_put(path);
|
|
|
|
}
|
2017-09-20 22:58:38 +08:00
|
|
|
kzfree(prep->payload.data[big_key_data]);
|
2014-07-19 01:56:36 +08:00
|
|
|
}
|
|
|
|
|
2013-09-24 17:35:18 +08:00
|
|
|
/*
|
|
|
|
* dispose of the links from a revoked keyring
|
|
|
|
* - called with the key sem write-locked
|
|
|
|
*/
|
|
|
|
void big_key_revoke(struct key *key)
|
|
|
|
{
|
2015-10-21 21:04:48 +08:00
|
|
|
struct path *path = (struct path *)&key->payload.data[big_key_path];
|
2013-09-24 17:35:18 +08:00
|
|
|
|
|
|
|
/* clear the quota */
|
|
|
|
key_payload_reserve(key, 0);
|
2017-10-04 23:43:25 +08:00
|
|
|
if (key_is_positive(key) &&
|
2015-10-21 21:04:48 +08:00
|
|
|
(size_t)key->payload.data[big_key_len] > BIG_KEY_FILE_THRESHOLD)
|
2013-09-24 17:35:18 +08:00
|
|
|
vfs_truncate(path, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* dispose of the data dangling from the corpse of a big_key key
|
|
|
|
*/
|
|
|
|
void big_key_destroy(struct key *key)
|
|
|
|
{
|
2015-10-21 21:04:48 +08:00
|
|
|
size_t datalen = (size_t)key->payload.data[big_key_len];
|
|
|
|
|
2016-04-13 02:54:58 +08:00
|
|
|
if (datalen > BIG_KEY_FILE_THRESHOLD) {
|
2015-10-21 21:04:48 +08:00
|
|
|
struct path *path = (struct path *)&key->payload.data[big_key_path];
|
2016-04-13 02:54:58 +08:00
|
|
|
|
2013-09-24 17:35:18 +08:00
|
|
|
path_put(path);
|
|
|
|
path->mnt = NULL;
|
|
|
|
path->dentry = NULL;
|
|
|
|
}
|
2017-09-20 22:58:38 +08:00
|
|
|
kzfree(key->payload.data[big_key_data]);
|
2016-04-13 02:54:58 +08:00
|
|
|
key->payload.data[big_key_data] = NULL;
|
2013-09-24 17:35:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* describe the big_key key
|
|
|
|
*/
|
|
|
|
void big_key_describe(const struct key *key, struct seq_file *m)
|
|
|
|
{
|
2015-10-21 21:04:48 +08:00
|
|
|
size_t datalen = (size_t)key->payload.data[big_key_len];
|
2013-09-24 17:35:18 +08:00
|
|
|
|
|
|
|
seq_puts(m, key->description);
|
|
|
|
|
2017-10-04 23:43:25 +08:00
|
|
|
if (key_is_positive(key))
|
2015-10-21 21:04:48 +08:00
|
|
|
seq_printf(m, ": %zu [%s]",
|
2013-09-24 17:35:18 +08:00
|
|
|
datalen,
|
|
|
|
datalen > BIG_KEY_FILE_THRESHOLD ? "file" : "buff");
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* read the key data
|
|
|
|
* - the key's semaphore is read-locked
|
|
|
|
*/
|
2020-03-22 09:11:24 +08:00
|
|
|
long big_key_read(const struct key *key, char *buffer, size_t buflen)
|
2013-09-24 17:35:18 +08:00
|
|
|
{
|
2015-10-21 21:04:48 +08:00
|
|
|
size_t datalen = (size_t)key->payload.data[big_key_len];
|
2013-09-24 17:35:18 +08:00
|
|
|
long ret;
|
|
|
|
|
|
|
|
if (!buffer || buflen < datalen)
|
|
|
|
return datalen;
|
|
|
|
|
|
|
|
if (datalen > BIG_KEY_FILE_THRESHOLD) {
|
2015-10-21 21:04:48 +08:00
|
|
|
struct path *path = (struct path *)&key->payload.data[big_key_path];
|
2013-09-24 17:35:18 +08:00
|
|
|
struct file *file;
|
2020-05-12 05:51:01 +08:00
|
|
|
u8 *buf, *enckey = (u8 *)key->payload.data[big_key_data];
|
|
|
|
size_t enclen = datalen + CHACHA20POLY1305_AUTHTAG_SIZE;
|
2017-09-01 23:39:13 +08:00
|
|
|
loff_t pos = 0;
|
2016-04-13 02:54:58 +08:00
|
|
|
|
2020-05-12 05:51:01 +08:00
|
|
|
buf = kvmalloc(enclen, GFP_KERNEL);
|
2018-02-22 22:38:34 +08:00
|
|
|
if (!buf)
|
2016-04-13 02:54:58 +08:00
|
|
|
return -ENOMEM;
|
2013-09-24 17:35:18 +08:00
|
|
|
|
|
|
|
file = dentry_open(path, O_RDONLY, current_cred());
|
2016-04-13 02:54:58 +08:00
|
|
|
if (IS_ERR(file)) {
|
|
|
|
ret = PTR_ERR(file);
|
|
|
|
goto error;
|
|
|
|
}
|
2013-09-24 17:35:18 +08:00
|
|
|
|
2016-04-13 02:54:58 +08:00
|
|
|
/* read file to kernel and decrypt */
|
2020-05-12 05:51:01 +08:00
|
|
|
ret = kernel_read(file, buf, enclen, &pos);
|
|
|
|
if (ret != enclen) {
|
|
|
|
if (ret >= 0)
|
|
|
|
ret = -EIO;
|
2016-04-13 02:54:58 +08:00
|
|
|
goto err_fput;
|
|
|
|
}
|
|
|
|
|
2020-05-12 05:51:01 +08:00
|
|
|
ret = chacha20poly1305_decrypt(buf, buf, enclen, NULL, 0, 0,
|
|
|
|
enckey) ? 0 : -EBADMSG;
|
|
|
|
if (unlikely(ret))
|
2016-04-13 02:54:58 +08:00
|
|
|
goto err_fput;
|
|
|
|
|
|
|
|
ret = datalen;
|
|
|
|
|
2020-03-22 09:11:24 +08:00
|
|
|
/* copy out decrypted data */
|
2020-05-12 05:51:01 +08:00
|
|
|
memcpy(buffer, buf, datalen);
|
2016-04-13 02:54:58 +08:00
|
|
|
|
|
|
|
err_fput:
|
|
|
|
fput(file);
|
|
|
|
error:
|
2020-05-12 05:51:01 +08:00
|
|
|
memzero_explicit(buf, enclen);
|
|
|
|
kvfree(buf);
|
2013-09-24 17:35:18 +08:00
|
|
|
} else {
|
|
|
|
ret = datalen;
|
2020-03-22 09:11:24 +08:00
|
|
|
memcpy(buffer, key->payload.data[big_key_data], datalen);
|
2013-09-24 17:35:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-04-13 02:54:58 +08:00
|
|
|
/*
|
|
|
|
* Register key type
|
|
|
|
*/
|
2013-09-24 17:35:18 +08:00
|
|
|
static int __init big_key_init(void)
|
|
|
|
{
|
2020-05-12 05:51:01 +08:00
|
|
|
return register_key_type(&key_type_big_key);
|
2016-04-13 02:54:58 +08:00
|
|
|
}
|
|
|
|
|
2016-10-26 22:02:01 +08:00
|
|
|
late_initcall(big_key_init);
|