security/keys: rewrite big_key crypto to use Zinc
Subsystems (2)
PATCH 1/1 security/keys: rewrite big_key crypto to use Zinc
A while back, I noticed that the crypto and crypto API usage in big_keys were entirely broken in multiple ways, so I rewrote it. Now, I'm rewriting it again, but this time using Zinc's ChaCha20Poly1305 function. This makes the file considerably more simple; the diffstat alone should justify this commit. It also should be faster, since it no longer requires a mutex around the "aead api object" (nor allocations), allowing us to encrypt multiple items in parallel. We also benefit from being able to pass any type of pointer, so we can get rid of the ridiculously complex custom page allocator that big_key really doesn't need.
---
I finally got around to updating this patch from the mailing list posts back in 2017-2018, using the library interface that we eventually merged in 2019. I haven't retested this for functionality, but nothing much has changed, so I suspect things should still be good to go.
security/keys/Kconfig | 4 +- security/keys/big_key.c | 230 +++++----------------------------------- 2 files changed, 28 insertions(+), 206 deletions(-)
security/keys/Kconfig +1 -3
@@ -60,9 +60,7 @@ config BIG_KEYS bool "Large payload keys" depends on KEYS depends on TMPFS - select CRYPTO - select CRYPTO_AES - select CRYPTO_GCM + select CRYPTO_LIB_CHACHA20POLY1305 help This option provides support for holding large keys within the kernel (for example Kerberos ticket caches). The data may be stored out to
security/keys/big_key.c +27 -203
347 lines not on this page · raw message
Signature · 1 line
2.26.2
Quoted 32 lines · Jason A. Donenfeld
A while back, I noticed that the crypto and crypto API usage in
big_keyswere entirely broken in multiple ways, so I rewrote it. Now, I'm rewriting it again, but this time using Zinc's ChaCha20Poly1305 function. This makes the file considerably more simple; the diffstat alone should justify this commit. It also should be faster, since it no longer requires a mutex around the "aead api object" (nor allocations), allowing us to encrypt multiple items in parallel. We also benefit from being able to pass any type of pointer, so we can get rid of the ridiculously complex custom page allocator thatbig_keyreally doesn't need.Cc David Howells <[email protected]>Cc Andy Lutomirski <[email protected]>Cc Greg KH <[email protected]>Cc Linus Torvalds <[email protected]>Signed-off-by Jason A. Donenfeld <[email protected]>---
I finally got around to updating this patch from the mailing list posts back in 2017-2018, using the library interface that we eventually merged in 2019. I haven't retested this for functionality, but nothing much has changed, so I suspect things should still be good to go.security/keys/Kconfig | 4 +- security/keys/big_key.c | 230 +++++----------------------------------- 2 files changed, 28 insertions(+), 206 deletions(-)security/keys/Kconfig +0
-0
security/keys/Kconfig config BIG_KEYS · line 66
Quoted 10 lines
security/keys/Kconfig +1
-3@@ -60,9 +60,7 @@ config BIG_KEYS bool "Large payload keys" depends on KEYS depends on TMPFS - select CRYPTO - select CRYPTO_AES - select CRYPTO_GCM + select CRYPTO_LIB_CHACHA20POLY1305 help This option provides support for holding large keys within the kernel (for example Kerberos ticket caches). The data may be stored out to
The 'select CRYPTO' is actually still needed because CRYPTO_LIB_CHACHA20POLY1305 is under the CRYPTO menuconfig:
make allnoconfig
cat >> .config << EOFCONFIG_SHMEM=yCONFIG_TMPFS=yCONFIG_KEYS=yCONFIG_BIG_KEYS=y
EOF
make olddefconfig
WARNING: unmet direct dependencies detected for CRYPTO_LIB_CHACHA20POLY1305
Depends on [n]: CRYPTO [=n] && (CRYPTO_ARCH_HAVE_LIB_CHACHA [=n] || !CRYPTO_ARCH_HAVE_LIB_CHACHA [=n]) && (CRYPTO_ARCH_HAVE_LIB_POLY1305 [=n] || !CRYPTO_ARCH_HAVE_LIB_POLY1305 [=n]) Selected by [y]:
- BIG_KEYS [=y] && KEYS [=y] && TMPFS [=y]
Maybe the 'source "lib/crypto/Kconfig"' in crypto/Kconfig should be moved to lib/Kconfig so that it's under "Library routines" and the crypto library options can be selected without the full CRYPTO framework?
But lib/crypto/libchacha.c uses crypto_xor_cpy(), and lib/crypto/chacha20poly1305.c uses crypto_memneq(). So those functions would first need to be pulled into lib/crypto/ too.
Quoted 10 lines
security/keys/Kconfig +1
-1@@ -265,7 +121,7 @@ int big_key_preparse(struct key_preparsed_payload *prep) *path = file->f_path; path_get(path); fput(file); - big_key_free_buffer(buf); + kvfree(buf); } else { /* Just store the data in a buffer */ void *data = kmalloc(datalen, GFP_KERNEL);
security/keys/Kconfig int big_key_preparse(struct key_preparsed_payload *prep) · line 144
Quoted 7 lines
security/keys/Kconfig +1
-1@@ -283,7 +139,7 @@ int big_key_preparse(struct key_preparsed_payload *prep) err_enckey: kzfree(enckey); error: - big_key_free_buffer(buf); + kvfree(buf); return ret; }
There should be a 'memzero_explicit(buf, enclen);' before the above two calls to kvfree().
Or even better these should use kvfree_sensitive(), but that hasn't been merged yet. It was under discussion last month:
https://lkml.kernel.org/lkml/[email protected]/
security/keys/Kconfig int big_key_preparse(struct key_preparsed_payload *prep)
Quoted 7 lines
security/keys/Kconfig +3
-2- ret = big_key_crypt(BIG_KEY_DEC, buf, enclen, enckey); - if (ret) + ret = chacha20poly1305_decrypt(buf, buf, enclen, NULL, 0, 0, + enckey) ? 0 : -EINVAL; + if (unlikely(ret)) goto err_fput;
-EINVAL is often unclear, since everyone uses it for everything. How about using -EBADMSG, which is what was used before via crypto_aead_decrypt()?
security/keys/Kconfig
Quoted 12 lines
security/keys/Kconfig +2
-2ret = datalen; /* copy out decrypted data */ - memcpy(buffer, buf->virt, datalen); + memcpy(buffer, buf, datalen); err_fput: fput(file); error: - big_key_free_buffer(buf); + kvfree(buf);
Likewise, the buffer should be zeroed before freeing here.
- Eric
Hey Eric,
Thanks for the review.
I'll add select CONFIG as you suggested. I agree about trying to move as much as possible out of crypto and into lib/crypto. Breaking those dependency cycles won't be easy but perhaps it'll be possible to chip away at that gradually. (I'd also lib a lib/crypto/arch/{arch}/..., but I guess that's a separate discussion.)
I'll also change -EINVAL to -EBADMSG. Nice catch.
Regarding the buffer zeroing... are you sure? These buffers are already being copied into filesystem caches and all sorts of places over which we have zero control. At that point, does it matter? Or do you argue that because it's still technically key material, we should zero out both the plaintext and ciphertext everywhere we can, and hopefully at some point the places where we can't will go away? IOW, I'm fine doing that, but would like to learn your explicit reasoning before.
Jason
Quoted 12 lines · Jason A. Donenfeld
Hey Eric,
Thanks for the review.
I'll add
select CONFIGas you suggested. I agree about trying to move as much as possible out of crypto and into lib/crypto. Breaking those dependency cycles won't be easy but perhaps it'll be possible to chip away at that gradually. (I'd also lib alib/crypto/arch/{arch}/..., but I guess that's a separate discussion.)I'll also change
-EINVALto-EBADMSG. Nice catch.
Quoted 8 lines
Regarding the buffer zeroing... are you sure? These buffers are already being copied into filesystem caches and all sorts of places over which we have zero control. At that point, does it matter? Or do you argue that because it's still technically key material, we should zero out both the plaintext and ciphertext everywhere we can, and hopefully at some point the places where we can't will go away? IOW, I'm fine doing that, but would like to learn your explicit reasoning before.
It's true that the buffer zeroing doesn't matter in big_key_preparse() because the buffer only holds the encrypted key (which is what the shmem file will contain). But in big_key_read(), the buffer holds the decrypted key. So it's at least needed there. Having it in both places for consistency might be a good idea.
- Eric
Quoted 23 lines · Eric Biggers
Quoted 12 lines · Jason A. Donenfeld
Hey Eric,
Thanks for the review.
I'll add
select CONFIGas you suggested. I agree about trying to move as much as possible out of crypto and into lib/crypto. Breaking those dependency cycles won't be easy but perhaps it'll be possible to chip away at that gradually. (I'd also lib alib/crypto/arch/{arch}/..., but I guess that's a separate discussion.)I'll also change
-EINVALto-EBADMSG. Nice catch.Quoted 8 lines
Regarding the buffer zeroing... are you sure? These buffers are already being copied into filesystem caches and all sorts of places over which we have zero control. At that point, does it matter? Or do you argue that because it's still technically key material, we should zero out both the plaintext and ciphertext everywhere we can, and hopefully at some point the places where we can't will go away? IOW, I'm fine doing that, but would like to learn your explicit reasoning before.
Quoted 5 lines
It's true that the buffer zeroing doesn't matter in
big_key_preparse()because the buffer only holds the encrypted key (which is what the shmem file will contain). But inbig_key_read(), the buffer holds the decrypted key. So it's at least needed there. Having it in both places for consistency might be a good idea.
Alright. v2 coming your way shortly.
Jason
PATCH v2 1/1 security/keys: rewrite big_key crypto to use Zinc
A while back, I noticed that the crypto and crypto API usage in big_keys were entirely broken in multiple ways, so I rewrote it. Now, I'm rewriting it again, but this time using Zinc's ChaCha20Poly1305 function. This makes the file considerably more simple; the diffstat alone should justify this commit. It also should be faster, since it no longer requires a mutex around the "aead api object" (nor allocations), allowing us to encrypt multiple items in parallel. We also benefit from being able to pass any type of pointer, so we can get rid of the ridiculously complex custom page allocator that big_key really doesn't need.
---
Changes v1->v2:
- [Eric] Return -EBADMSG instead of -EINVAL if the authtag fails.
- [Eric] Select CONFIG_CRYPTO, since it's required by the LIB selection.
- [Eric] Zero out buffers that formerly contained either plaintext or ciphertext keys.
- [Jason] If kernel_read() fails, return that error value, instead of relying on the subsequent decryption to fail.
Note v1:
I finally got around to updating this patch from the mailing list posts back in 2017-2018, using the library interface that we eventually merged in 2019. I haven't retested this for functionality, but nothing much has changed, so I suspect things should still be good to go.
security/keys/Kconfig | 3 +- security/keys/big_key.c | 235 ++++++---------------------------------- 2 files changed, 33 insertions(+), 205 deletions(-)
security/keys/Kconfig +1 -2
@@ -61,8 +61,7 @@ config BIG_KEYS depends on KEYS depends on TMPFS select CRYPTO - select CRYPTO_AES - select CRYPTO_GCM + select CRYPTO_LIB_CHACHA20POLY1305 help This option provides support for holding large keys within the kernel (for example Kerberos ticket caches). The data may be stored out to
security/keys/big_key.c +32 -203
352 lines not on this page · raw message
Signature · 1 line
2.26.2
Quoted 11 lines · Jason A. Donenfeld
A while back, I noticed that the crypto and crypto API usage in
big_keyswere entirely broken in multiple ways, so I rewrote it. Now, I'm rewriting it again, but this time using Zinc's ChaCha20Poly1305 function. This makes the file considerably more simple; the diffstat alone should justify this commit. It also should be faster, since it no longer requires a mutex around the "aead api object" (nor allocations), allowing us to encrypt multiple items in parallel. We also benefit from being able to pass any type of pointer, so we can get rid of the ridiculously complex custom page allocator thatbig_keyreally doesn't need.
Quoted 7 lines
Cc David Howells <[email protected]>Cc Andy Lutomirski <[email protected]>Cc Greg KH <[email protected]>Cc Linus Torvalds <[email protected]>Cc Eric Biggers <[email protected]>Signed-off-by Jason A. Donenfeld <[email protected]>
You can add:
But, a couple minor suggestions:
The commit message should say "lib/crypto", not Zinc. Nothing in the source tree actually says Zinc, so it will confuse people who haven't read all the previous discussions.
security/keys/big_key.c long big_key_read(const struct key *key, char *buffer, size_t buflen) · line 245
Quoted 9 lines
security/keys/big_key.c +3
-1/* read file to kernel and decrypt */ - ret =kernel_read (file, buf->virt, enclen, &pos); + ret =kernel_read (file, buf, enclen, &pos);if (ret >=0 && ret != enclen) { ret = -EIO ; goto err_fput; + }else if (ret <0 ) { + goto err_fput; }
It would make sense to write this as the following, to make it consistent with how the return value of kernel_write() is checked in big_key_preparse():
ret = kernel_read(file, buf, enclen, &pos);
if (ret != enclen) {
if (ret >= 0)
ret = -ENOMEM;
goto err_fput;
}- Eric
Quoted 3 lines · Eric Biggers
The commit message should say "lib/crypto", not Zinc. Nothing in the source tree actually says Zinc, so it will confuse people who haven't read all the previous discussions.
Old commit message from a few years ago. Will adjust.
Quoted 14 lines
security/keys/big_key.c long big_key_read(const struct key *key, char *buffer, size_t buflen) · line 245
Quoted 10 lines
/* read file to kernel and decrypt */security/keys/big_key.c +3
-1- ret =kernel_read (file, buf->virt, enclen, &pos); + ret =kernel_read (file, buf, enclen, &pos);if (ret >=0 && ret != enclen) { ret = -EIO ; goto err_fput; + }else if (ret <0 ) { + goto err_fput; }It would make sense to write this as the following, to make it consistent with how the return value of
kernel_write()is checked inbig_key_preparse():
Quoted 6 lines
ret = kernel_read(file, buf, enclen, &pos);
if (ret != enclen) {
if (ret >= 0)
ret = -ENOMEM;
goto err_fput;
}Will do, and will send a v3 with your reviewed-by.
Jason
PATCH v3 1/1 security/keys: rewrite big_key crypto to use library interface
A while back, I noticed that the crypto and crypto API usage in big_keys were entirely broken in multiple ways, so I rewrote it. Now, I'm rewriting it again, but this time using the simpler ChaCha20Poly1305 library function. This makes the file considerably more simple; the diffstat alone should justify this commit. It also should be faster, since it no longer requires a mutex around the "aead api object" (nor allocations), allowing us to encrypt multiple items in parallel. We also benefit from being able to pass any type of pointer, so we can get rid of the ridiculously complex custom page allocator that big_key really doesn't need.
---
Changes v2->v3:
- [Eric] Unify kernel_read/write handling in big_key_preparse and big_key_read.
- [Eric] Update commit message.
Changes v1->v2:
- [Eric] Return -EBADMSG instead of -EINVAL if the authtag fails.
- [Eric] Select CONFIG_CRYPTO, since it's required by the LIB selection.
- [Eric] Zero out buffers that formerly contained either plaintext or ciphertext keys.
- [Jason] If kernel_read() fails, return that error value, instead of relying on the subsequent decryption to fail.
Note v1:
I finally got around to updating this patch from the mailing list posts back in 2017-2018, using the library interface that we eventually merged in 2019. I haven't retested this for functionality, but nothing much has changed, so I suspect things should still be good to go.
security/keys/Kconfig | 3 +- security/keys/big_key.c | 240 ++++++---------------------------------- 2 files changed, 35 insertions(+), 208 deletions(-)
security/keys/Kconfig +1 -2
@@ -61,8 +61,7 @@ config BIG_KEYS depends on KEYS depends on TMPFS select CRYPTO - select CRYPTO_AES - select CRYPTO_GCM + select CRYPTO_LIB_CHACHA20POLY1305 help This option provides support for holding large keys within the kernel (for example Kerberos ticket caches). The data may be stored out to
security/keys/big_key.c +34 -206
358 lines not on this page · raw message
Signature · 1 line
2.26.2
David Howells
maintainerRe: [PATCH v3] security/keys: rewrite big_key crypto to use library interface
Review
Quoted 2 lines · Jason A. Donenfeld
(quoted diff) +1
-1- /* no ->update(); don't add it without changing big_key_crypt() nonce */ + /* no ->update(); don't add it without changing chacha20poly1305's nonce
Note that ->update() doesn't have to modify the contents of the key, but can just rather replace them entirely. See attached. The actual work is done in big_key_preparse(); all big_key_update() has to do is apply it and dispose of the old payload.
David
---
commit 724e76c185d517f35ead4f72f9958850c9218f4d
Author: David Howells <[email protected]> Date: Tue May 12 14:03:53 2020 +0100
keys: Implement update for the big_key type
Implement the ->update op for the big_key type.
include/keys/big_key-type.h +1 -0
@@ -18,5 +18,6 @@ extern void big_key_revoke(struct key *key);extern void big_key_destroy (struct key *key);extern void big_key_describe (const struct key *big_key,struct seq_file *m);extern long big_key_read (const struct key *key,char *buffer,size_t buflen); +extern int big_key_update (struct key *key,struct key_preparsed_payload *prep);#endif /* _KEYS_BIG_KEY_TYPE_H */
security/keys/big_key.c +18 -1
@@ -47,7 +47,7 @@ struct key_type key_type_big_key = { .destroy = big_key_destroy, .describe = big_key_describe, .read = big_key_read, -/* no ->update(); don't add it without changing chacha20poly1305's nonce */ + .update = big_key_update, };/*
@@ -191,6 +191,23 @@ void big_key_destroy(struct key *key) key->payload.data[big_key_data] =NULL ; } +/* +* 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); +} +/* * describe the big_key key */
Re: [PATCH v3] security/keys: rewrite big_key crypto to use library interface
Hi David,
So long as that ->update function:
1. Deletes the old on-disk data.
2. Deletes the old key from the inode.
3. Generates a new key using get_random_bytes.
4. Stores that new key in the inode.
5. Encrypts the updated data afresh with the new key.
6. Puts the updated data onto disk,
then this is fine with me, and feel free to have my Acked-by if you want. But if it doesn't do that -- i.e. if it tries to reuse the old key or similar -- then this isn't fine. But it sounds like from what you've described that things are actually fine, in which case, I guess it makes sense to apply your patch ontop of mine and commit these.
Jason
David Howells
maintainerRe: [PATCH v3] security/keys: rewrite big_key crypto to use library interface
Quoted 8 lines · Jason A. Donenfeld
So long as that ->update function:
1. Deletes the old on-disk data.
2. Deletes the old key from the inode.
3. Generates a new key usingget_random_bytes.
4. Stores that new key in the inode.
5. Encrypts the updated data afresh with the new key.
6. Puts the updated data onto disk,
Quoted 5 lines
then this is fine with me, and feel free to have my Acked-by if you want. But if it doesn't do that -- i.e. if it tries to reuse the old key or similar -- then this isn't fine. But it sounds like from what you've described that things are actually fine, in which case, I guess it makes sense to apply your patch ontop of mine and commit these.
Yep. It calls big_key_destroy(), which clears away the old stuff just as when a key is being destroyed, then generic_key_instantiate() just as when a key is being set up.
The key ID and the key metadata (ownership, perms, expiry) are maintained, but the payload is just completely replaced.
David
Jason A. Donenfeld
Acked-byRe: [PATCH v3] security/keys: rewrite big_key crypto to use library interface
Quoted 17 lines · David Howells
Quoted 8 lines · Jason A. Donenfeld
So long as that ->update function:
1. Deletes the old on-disk data.
2. Deletes the old key from the inode.
3. Generates a new key usingget_random_bytes.
4. Stores that new key in the inode.
5. Encrypts the updated data afresh with the new key.
6. Puts the updated data onto disk,Quoted 5 lines
then this is fine with me, and feel free to have my Acked-by if you want. But if it doesn't do that -- i.e. if it tries to reuse the old key or similar -- then this isn't fine. But it sounds like from what you've described that things are actually fine, in which case, I guess it makes sense to apply your patch ontop of mine and commit these.
Quoted 6 lines
Yep. It calls
big_key_destroy(), which clears away the old stuff just as when a key is being destroyed, thengeneric_key_instantiate()just as when a key is being set up.The key ID and the key metadata (ownership, perms, expiry) are maintained, but the payload is just completely replaced.
Okay, in that case, take my:
And then perhaps you can take both my patch and your addendum into keys-next.
Jason