Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
There are several things wrong with get_hash_data: 1. it doesn't work, all returned hashes are all 0x00 2. memory leak of hash allocated by crypto_md_generate_hash 3. memory leak of der data buffer from is_x509certificate 4. useless casting of hash_ids to x509_hash_ids Issues 1-3 can be seen below $ bin/secvarctl-dbg -m guest generate f:e -i ./test/testdata/guest/x509certs/dbx.crt -o dbx.esl -n dbx error: invalid crypto alg key 8 error: invalid crypto alg key 11 error: invalid crypto alg key 8 RESULT: SUCCESS ================================================================= ==88342==ERROR: LeakSanitizer: detected memory leaks Direct leak of 813 byte(s) in 1 object(s) allocated from: #0 0x7fa153bdebb8 in __interceptor_malloc (/lib64/libasan.so.5+0xefbb8) #1 0x7fa1537a42da in PEM_read_bio_ex (/lib64/libcrypto.so.1.1+0x19f2da) Direct leak of 32 byte(s) in 1 object(s) allocated from: #0 0x7fa153bdebb8 in __interceptor_malloc (/lib64/libasan.so.5+0xefbb8) #1 0x463e27 in crypto_md_generate_hash src/crypto_openssl.c:768 open-power#2 0x445b4a in get_hash_data backends/guest/common/generate.c:477 open-power#3 0x43234a in generate_esl backends/guest/guest_svc_generate.c:73 open-power#4 0x43505e in generate_data backends/guest/guest_svc_generate.c:365 open-power#5 0x439382 in guest_generate_command backends/guest/guest_svc_generate.c:690 open-power#6 0x4069b4 in main /home/nchild/IBM/secvarctl-stuff/erichte-guest-devel/secvarctl/secvarctl.c:249 open-power#7 0x7fa152558d84 in __libc_start_main (/lib64/libc.so.6+0x3ad84) SUMMARY: AddressSanitizer: 845 byte(s) leaked in 2 allocation(s). $ hexdump dbx.esl 0000000 1626 c1c4 504c 4092 a9ac f941 9336 2843 0000010 004c 0000 0000 0000 0030 0000 0000 0000 0000020 0000 0000 0000 0000 0000 0000 0000 0000 * 0000040 0000 0000 0000 0000 0000 0000 000004c Fixing issue 1 and 2: get_hash_data() acts as a middle function during hash generation. First generate_esl() allocates an empty hash buffer and hands it to get_hash_data(). get_hash_data() then hands the address to that buffer to crypto_md_generate_hash(). crypto_md_generate_hash() allocates its own buffer and overwrites our pointer to our buffer. get_hash_data() has no way of updating generate_esl() of the new address. This results in a successful hash generation but a memoryleaks and an empty hash in the esl: To avoid this, simply copy the returned buffer into the existing buffer. Fixing issue 3: is_x509certificate will allocate and return data if it finds the given buffer contains an x509. To fix this, simply free the der data after generating the hash. Fixing issue 4: This is only a semi-issue, but previously, if the buffer was a certificate then we would cast our hash_function parameter into an x509 hash function ID. Later on this value get passed to get_crypto_alg_id() to get the crypto library specific MD identifier. get_crypto_alg_id() does not keep track of x509 hash function ID (and tbh the difference between them is not clear) and defaults to sha256. The point of converting the given hash id to x509 hash id only to then convert that value into crypto library specific ID is unnecessary. To fix just stick with the hash function passed to the function. Lastly, add a test case for all this. Signed-off-by: Nick Child <nick.child@ibm.com>
- Loading branch information