Skip to content

Commit 7df3e59

Browse files
dhowellsJames Morris
authored andcommitted
KEYS: Sort out big_key initialisation
big_key has two separate initialisation functions, one that registers the key type and one that registers the crypto. If the key type fails to register, there's no problem if the crypto registers successfully because there's no way to reach the crypto except through the key type. However, if the key type registers successfully but the crypto does not, big_key_rng and big_key_blkcipher may end up set to NULL - but the code neither checks for this nor unregisters the big key key type. Furthermore, since the key type is registered before the crypto, it is theoretically possible for the kernel to try adding a big_key before the crypto is set up, leading to the same effect. Fix this by merging big_key_crypto_init() and big_key_init() and calling the resulting function late. If they're going to be encrypted, we shouldn't be creating big_keys before we have the facilities to do the encryption available. The key type registration is also moved after the crypto initialisation. The fix also includes message printing on failure. If the big_key type isn't correctly set up, simply doing: dd if=/dev/zero bs=4096 count=1 | keyctl padd big_key a @s ought to cause an oops. Fixes: 13100a7 ('Security: Keys: Big keys stored encrypted') Signed-off-by: David Howells <dhowells@redhat.com> cc: Peter Hlavaty <zer0mem@yahoo.com> cc: Kirill Marinushkin <k.marinushkin@gmail.com> cc: Artem Savkov <asavkov@redhat.com> cc: stable@vger.kernel.org Signed-off-by: James Morris <james.l.morris@oracle.com>
1 parent 03dab86 commit 7df3e59

1 file changed

Lines changed: 32 additions & 27 deletions

File tree

‎security/keys/big_key.c‎

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* 2 of the Licence, or (at your option) any later version.
1010
*/
1111

12+
#define pr_fmt(fmt) "big_key: "fmt
1213
#include <linux/init.h>
1314
#include <linux/seq_file.h>
1415
#include <linux/file.h>
@@ -341,44 +342,48 @@ long big_key_read(const struct key *key, char __user *buffer, size_t buflen)
341342
*/
342343
static int __init big_key_init(void)
343344
{
344-
return register_key_type(&key_type_big_key);
345-
}
346-
347-
/*
348-
* Initialize big_key crypto and RNG algorithms
349-
*/
350-
static int __init big_key_crypto_init(void)
351-
{
352-
int ret = -EINVAL;
345+
struct crypto_skcipher *cipher;
346+
struct crypto_rng *rng;
347+
int ret;
353348

354-
/* init RNG */
355-
big_key_rng = crypto_alloc_rng(big_key_rng_name, 0, 0);
356-
if (IS_ERR(big_key_rng)) {
357-
big_key_rng = NULL;
358-
return -EFAULT;
349+
rng = crypto_alloc_rng(big_key_rng_name, 0, 0);
350+
if (IS_ERR(rng)) {
351+
pr_err("Can't alloc rng: %ld\n", PTR_ERR(rng));
352+
return PTR_ERR(rng);
359353
}
360354

355+
big_key_rng = rng;
356+
361357
/* seed RNG */
362-
ret = crypto_rng_reset(big_key_rng, NULL, crypto_rng_seedsize(big_key_rng));
363-
if (ret)
364-
goto error;
358+
ret = crypto_rng_reset(rng, NULL, crypto_rng_seedsize(rng));
359+
if (ret) {
360+
pr_err("Can't reset rng: %d\n", ret);
361+
goto error_rng;
362+
}
365363

366364
/* init block cipher */
367-
big_key_skcipher = crypto_alloc_skcipher(big_key_alg_name,
368-
0, CRYPTO_ALG_ASYNC);
369-
if (IS_ERR(big_key_skcipher)) {
370-
big_key_skcipher = NULL;
371-
ret = -EFAULT;
372-
goto error;
365+
cipher = crypto_alloc_skcipher(big_key_alg_name, 0, CRYPTO_ALG_ASYNC);
366+
if (IS_ERR(cipher)) {
367+
ret = PTR_ERR(cipher);
368+
pr_err("Can't alloc crypto: %d\n", ret);
369+
goto error_rng;
370+
}
371+
372+
big_key_skcipher = cipher;
373+
374+
ret = register_key_type(&key_type_big_key);
375+
if (ret < 0) {
376+
pr_err("Can't register type: %d\n", ret);
377+
goto error_cipher;
373378
}
374379

375380
return 0;
376381

377-
error:
382+
error_cipher:
383+
crypto_free_skcipher(big_key_skcipher);
384+
error_rng:
378385
crypto_free_rng(big_key_rng);
379-
big_key_rng = NULL;
380386
return ret;
381387
}
382388

383-
device_initcall(big_key_init);
384-
late_initcall(big_key_crypto_init);
389+
late_initcall(big_key_init);

0 commit comments

Comments
 (0)