Skip to content

Commit e6a4c98

Browse files
Nikolaus Vossgregkh
Nikolaus Voss
authored andcommitted
KEYS: encrypted: fix key instantiation with user-provided data
commit 5adedd4 upstream. Commit cd3bc04 ("KEYS: encrypted: Instantiate key with user-provided decrypted data") added key instantiation with user provided decrypted data. The user data is hex-ascii-encoded but was just memcpy'ed to the binary buffer. Fix this to use hex2bin instead. Old keys created from user provided decrypted data saved with "keyctl pipe" are still valid, however if the key is recreated from decrypted data the old key must be converted to the correct format. This can be done with a small shell script, e.g.: BROKENKEY=abcdefABCDEF1234567890aaaaaaaaaa NEWKEY=$(echo -ne $BROKENKEY | xxd -p -c32) keyctl add user masterkey "$(cat masterkey.bin)" @U keyctl add encrypted testkey "new user:masterkey 32 $NEWKEY" @U However, NEWKEY is still broken: If for BROKENKEY 32 bytes were specified, a brute force attacker knowing the key properties would only need to try at most 2^(16*8) keys, as if the key was only 16 bytes long. The security issue is a result of the combination of limiting the input range to hex-ascii and using memcpy() instead of hex2bin(). It could have been fixed either by allowing binary input or using hex2bin() (and doubling the ascii input key length). This patch implements the latter. The corresponding test for the Linux Test Project ltp has also been fixed (see link below). Fixes: cd3bc04 ("KEYS: encrypted: Instantiate key with user-provided decrypted data") Cc: [email protected] Link: https://lore.kernel.org/ltp/[email protected]/ Reviewed-by: Mimi Zohar <[email protected]> Signed-off-by: Nikolaus Voss <[email protected]> Signed-off-by: Mimi Zohar <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent b4270b0 commit e6a4c98

File tree

2 files changed

+5
-4
lines changed

2 files changed

+5
-4
lines changed

Documentation/security/keys/trusted-encrypted.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,8 @@ Load an encrypted key "evm" from saved blob::
350350

351351
Instantiate an encrypted key "evm" using user-provided decrypted data::
352352

353-
$ keyctl add encrypted evm "new default user:kmk 32 `cat evm_decrypted_data.blob`" @u
353+
$ evmkey=$(dd if=/dev/urandom bs=1 count=32 | xxd -c32 -p)
354+
$ keyctl add encrypted evm "new default user:kmk 32 $evmkey" @u
354355
794890253
355356

356357
$ keyctl print 794890253

security/keys/encrypted-keys/encrypted.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,7 @@ static struct encrypted_key_payload *encrypted_key_alloc(struct key *key,
627627
pr_err("encrypted key: instantiation of keys using provided decrypted data is disabled since CONFIG_USER_DECRYPTED_DATA is set to false\n");
628628
return ERR_PTR(-EINVAL);
629629
}
630-
if (strlen(decrypted_data) != decrypted_datalen) {
630+
if (strlen(decrypted_data) != decrypted_datalen * 2) {
631631
pr_err("encrypted key: decrypted data provided does not match decrypted data length provided\n");
632632
return ERR_PTR(-EINVAL);
633633
}
@@ -791,8 +791,8 @@ static int encrypted_init(struct encrypted_key_payload *epayload,
791791
ret = encrypted_key_decrypt(epayload, format, hex_encoded_iv);
792792
} else if (decrypted_data) {
793793
get_random_bytes(epayload->iv, ivsize);
794-
memcpy(epayload->decrypted_data, decrypted_data,
795-
epayload->decrypted_datalen);
794+
ret = hex2bin(epayload->decrypted_data, decrypted_data,
795+
epayload->decrypted_datalen);
796796
} else {
797797
get_random_bytes(epayload->iv, ivsize);
798798
get_random_bytes(epayload->decrypted_data, epayload->decrypted_datalen);

0 commit comments

Comments
 (0)