Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

crypto playnice fixes #1

Conversation

erichte-ibm
Copy link

@erichte-ibm erichte-ibm commented Aug 29, 2023

Set of fixes for open-power#51 , feel free to either merge this, re-implement, squash, whatever.

Changes:

  • build system tweaks to remove directly referencing crypto-openssl.c (built into library)
  • fix formatting (monolith commit, perhaps could be split/rebased/squashed)
  • update libstb-secvar since crypto rewrite was merged
  • remove a variable cppcheck didn't like

There is one additional issue I noted in a comment on the original PR here: https://github.com/open-power/secvarctl/pull/51/files/3f33595e6eb183a70dea1027a405fcb36c1474e3#r1309015588 , I have an additional diff I can apply to this branch to fix that issue as well.

…bstb-secvar-openssl.a

Signed-off-by: Eric Richter <erichte@linux.ibm.com>
Signed-off-by: Eric Richter <erichte@linux.ibm.com>
Signed-off-by: Eric Richter <erichte@linux.ibm.com>
Signed-off-by: Eric Richter <erichte@linux.ibm.com>
Signed-off-by: Eric Richter <erichte@linux.ibm.com>
@nick-child-ibm nick-child-ibm merged commit 61e0264 into nick-child-ibm:nick_crypto_playnice Sep 8, 2023
nick-child-ibm added a commit that referenced this pull request Oct 5, 2023
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>
nick-child-ibm added a commit that referenced this pull request Oct 6, 2023
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants