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

New generate reset key subcommand and minor fixes #1

Merged
merged 9 commits into from
Jan 20, 2021

Conversation

nick-child-ibm
Copy link
Collaborator

This PR is to update minor bugs and minor enhancements. These include:
-Fixing a typo of the default path to secure variables
-Edited compiler optimization from -Os to -O2 because -Os was causing issues on ppc (opened a gcc bug report here)
-Allow for custom man page destination during install with make install MANDIR=<path>

I also created an easy way to generate key reset files with secvarctl generate reset -n <varName> -k <signerKey> -c <signerCrt>
This update has been reflected in documentation.

secvarctl.c Outdated
@@ -123,7 +123,7 @@ static struct backend *getBackend()
prlog(PR_WARNING, "WARNING!! Could not extract data from %s , assuming platform does not support secure variables\n", secVarFormatLocation);
goto out;
}
//loop through all known backends
// loop through all known backends
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't include Jenkins-related commits in the history here. If you want to keep this change, have the commit message reflect what is actually changing in the commit.

@@ -191,6 +196,8 @@ For any questions regarding secvarctl, feel free to reach out: [Nick Child](nick
A PKCS7 and Auth file can be signed with several signers by adding more ' -k <privKey> -c <cert>' pairs.
Additionaly, when generating an Auth file the secure variable name must be given as -n <keyName> because it is included in the message digest.
When using the input type '[f]ile' it will be assumed to be a text file and if output file is '[e]sl', '[p]kcs7' or '[a]uth' it will be hashed according to <hashAlg> (default SHA256).
To create a variable reset file (one that will remove the current contents of a variable), replace '<inputFormat>:<outputFormat>' with 'reset' and
supply a variable name, public and private signer files and an output file with '-n <keyName> -k <privKey> -c <crtFile> -o <outFile>'
GENERATION OF PKCS7 AND AUTH FILES ARE IN EXPERIMENTAL DEVELEPOMENT PHASE. THEY HAVE NOT BEEN THOROUGHLY TESTED YET.


Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

General commit message comment: Specify what updated about the readme. Looks like the reset command was added? Something else changed about the whitespace i guess in that other list?

Comment on lines -49 to -50
# OBJDIR=obj
# OBJ=$(patsubst %, $(OBJDIR)/%,$(_OBJ))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, please try not to include Jenkins commits in the final commit history.

@@ -13,7 +13,7 @@
#define CERT_BUFFER_SIZE 2048

#ifndef SECVARPATH
#define SECVARPATH "/sys/firmware/secvar/vars"
#define SECVARPATH "/sys/firmware/secvar/vars/"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: this short commit message is fine, since it's literally a 1 character typo fix that isn't largely consequential.

_CFLAGS = -Os -s -g -std=gnu99
_CFLAGS = -O2 -s -g -std=gnu99
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one-character change however, is very consequential. This should be its own commit, with a longer detailed message explaining why this change is mad. Consider someone else trying to debug an issue, and running bisect -- this may be important information to someone in the future using an old GCC wondering why they are seeing the issue again, etc.

Copy link
Collaborator

@erichte-ibm erichte-ibm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aside from the Jenkins commits, the rest largely looks fine.

The major things I would like changed before merge:

  • Remove the Jenkins commits (or merge them and call it a "formatting fix" instead)
  • Split out the optimization change into its own commit

In the future, I'd prefer some of the larger commits separated out -- try not to merge fixes with features. Fixes should be their own commits, even if they are small, as they provide documentation in the git history on when a bug was addressed.

With `make MEMCHECK=1` valgrind will run on all tests and report any memory leaks.
I also patched some reported memory leaks and patched static analysis warnings.
Rather than using 'generate e:a -f' to generate a variable reset file, the user can now do 'generate reset -k <key> -c <crt> -n <var> -o <out>'. I also added test cases for this and updated the documentation.
Updated readme to include documentation for `secvarctl generate reset`, also fixed formatting issues in lists
I commented out code in the Makefile a long time ago for a different OBJDIR, I now know that it will not be used again and can be deleted.
TS was not printing correctly, fixed that with a new funciton readTS
-Os was causing issues with valgrind on ppc so reverting to -O2 until issues can be addressed. A bug on ppc has been opened and is currently under review
different distro's store man pages in different paths, this allows for a user defined destination with `make install MANDIR=<path>` default is `usr/local/share`
@erichte-ibm erichte-ibm merged commit a2d3f9a into open-power:main Jan 20, 2021
erichte-ibm pushed 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
    #2 0x445b4a in get_hash_data backends/guest/common/generate.c:477
    #3 0x43234a in generate_esl backends/guest/guest_svc_generate.c:73
    #4 0x43505e in generate_data backends/guest/guest_svc_generate.c:365
    #5 0x439382 in guest_generate_command backends/guest/guest_svc_generate.c:690
    #6 0x4069b4 in main /home/nchild/IBM/secvarctl-stuff/erichte-guest-devel/secvarctl/secvarctl.c:249
    #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 Feb 6, 2024
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
    #2 0x445b4a in get_hash_data backends/guest/common/generate.c:477
    #3 0x43234a in generate_esl backends/guest/guest_svc_generate.c:73
    #4 0x43505e in generate_data backends/guest/guest_svc_generate.c:365
    #5 0x439382 in guest_generate_command backends/guest/guest_svc_generate.c:690
    #6 0x4069b4 in main /home/nchild/IBM/secvarctl-stuff/erichte-guest-devel/secvarctl/secvarctl.c:249
    #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
Development

Successfully merging this pull request may close these issues.

None yet

2 participants