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

Fix NULL Checks and Error Handling #527

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions oqsprov/oqs_kem.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,19 +67,23 @@ static void oqs_kem_freectx(void *vpkemctx) {
PROV_OQSKEM_CTX *pkemctx = (PROV_OQSKEM_CTX *)vpkemctx;

OQS_KEM_PRINTF("OQS KEM provider called: freectx\n");
oqsx_key_free(pkemctx->kem);
OPENSSL_free(pkemctx);
if (pkemctx != NULL) {
oqsx_key_free(pkemctx->kem);
OPENSSL_free(pkemctx);
Copy link
Member

Choose a reason for hiding this comment

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

Same comment as below: OPENSSL_free handles NULL gracefully, and imo it's cleaner to not perform the check.

Copy link
Author

Choose a reason for hiding this comment

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

The NULL check is for prevent from NULL pointer for oqsx_key_free(pkemctx->kem), not for OPENSSL_free/free().

Copy link
Member

Choose a reason for hiding this comment

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

Ah yes, I should have been more clear: I meant to write that the OPENSSL_free call could be moved outside the conditional block, not that the check itself could be removed entirely. Certainly it's still necessary before the dereference.

}
}

static int oqs_kem_decapsencaps_init(void *vpkemctx, void *vkem,
int operation) {
PROV_OQSKEM_CTX *pkemctx = (PROV_OQSKEM_CTX *)vpkemctx;

OQS_KEM_PRINTF3("OQS KEM provider called: _init : New: %p; old: %p \n",
vkem, pkemctx->kem);
vkem, pkemctx ? pkemctx->kem : NULL);
if (pkemctx == NULL || vkem == NULL || !oqsx_key_up_ref(vkem))
return 0;
oqsx_key_free(pkemctx->kem);
if (pkemctx->kem != NULL) {
Copy link
Member

Choose a reason for hiding this comment

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

There's nothing technically wrong with putting this guard here, but it's not necessary—oqsx_key_free handles null pointers gracefully, as (in my opinion) any "good" freeing function should. I would vote to remove the guard and add a comment above the oqsx_key_free function stating that it must handle NULL.

Copy link
Author

Choose a reason for hiding this comment

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

Thanks for the comments.
We see quite some crashes due to dereference NULL pointer in our load testing. That is why we aggressively add NULL checks.
I will remove unnecessary NULL checks.

oqsx_key_free(pkemctx->kem);
}
pkemctx->kem = vkem;

return 1;
Expand All @@ -106,13 +110,13 @@ static int oqs_qs_kem_encaps_keyslot(void *vpkemctx, unsigned char *out,
const OQS_KEM *kem_ctx = NULL;

OQS_KEM_PRINTF("OQS KEM provider called: encaps\n");
if (pkemctx->kem == NULL) {
if (pkemctx == NULL || pkemctx->kem == NULL) {
OQS_KEM_PRINTF("OQS Warning: OQS_KEM not initialized\n");
return -1;
}

kem_ctx = pkemctx->kem->oqsx_provider_ctx.oqsx_qs_ctx.kem;
if (pkemctx->kem->comp_pubkey == NULL ||
if (kem_ctx == NULL || pkemctx->kem->comp_pubkey == NULL ||
pkemctx->kem->comp_pubkey[keyslot] == NULL) {
OQS_KEM_PRINTF("OQS Warning: public key is NULL\n");
return -1;
Expand Down Expand Up @@ -156,12 +160,12 @@ static int oqs_qs_kem_decaps_keyslot(void *vpkemctx, unsigned char *out,
const OQS_KEM *kem_ctx = NULL;

OQS_KEM_PRINTF("OQS KEM provider called: decaps\n");
if (pkemctx->kem == NULL) {
if (pkemctx == NULL || pkemctx->kem == NULL) {
OQS_KEM_PRINTF("OQS Warning: OQS_KEM not initialized\n");
return -1;
}
kem_ctx = pkemctx->kem->oqsx_provider_ctx.oqsx_qs_ctx.kem;
if (pkemctx->kem->comp_privkey == NULL ||
if (kem_ctx == NULL || pkemctx->kem->comp_privkey == NULL ||
pkemctx->kem->comp_privkey[keyslot] == NULL) {
OQS_KEM_PRINTF("OQS Warning: private key is NULL\n");
return -1;
Expand Down
Loading
Loading