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

libckteec: fix null pointer dereference on template serialization #392

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

hoyong2007
Copy link
Contributor

Fix potential null-pointer dereference on serialize_mecha_XXX() by checking every pointer value in under CK_MECHANISM_PTR.

Link: #388

libckteec/src/serialize_ck.c Outdated Show resolved Hide resolved
@@ -461,6 +464,9 @@ static CK_RV serialize_mecha_aes_iv(struct serializer *obj,
uint32_t iv_size = mecha->ulParameterLen;
CK_RV rv = CKR_GENERAL_ERROR;

if (!mecha->pParameter)
Copy link
Contributor

Choose a reason for hiding this comment

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

The mechanism currently using serialize_mecha_aes_iv() expect an IV of 16byte. But I think this function would be more flexible if it supports any IV size (for example here we don't enforce iv_size is 16), including 0:

	if (iv_size && !mecha->pParameter)
		return CKR_MECHANISM_PARAM_INVALID;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can we use mecha->ulParameterLen instead of iv_size? Since mecha->ulParameterLen is already used when serialize_buffer() is called.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If you think using iv_size seems more appropriate, feel free to resolve this conversation since it’s already reflected in the commit.

Copy link
Contributor

Choose a reason for hiding this comment

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

Testing iv_size or mecha->ulParameterLen is equivalent. At your will.

libckteec/src/serialize_ck.c Outdated Show resolved Hide resolved
libckteec/src/serialize_ck.c Outdated Show resolved Hide resolved
libckteec/src/serialize_ck.c Outdated Show resolved Hide resolved
libckteec/src/serialize_ck.c Outdated Show resolved Hide resolved
libckteec/src/serialize_ck.c Outdated Show resolved Hide resolved
libckteec/src/serialize_ck.c Outdated Show resolved Hide resolved
Fix potential null-pointer dereference on serialize_mecha_XXX()
by checking every pointer value in under CK_MECHANISM_PTR.

Link: OP-TEE#388
Signed-off-by: Hoyong Jin <[email protected]>
Reviewed-by: Etienne Carriere <[email protected]>
Copy link
Contributor

@etienne-lms etienne-lms left a comment

Choose a reason for hiding this comment

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

Sorry, I think I answered too quickly on these changes. For all cases where a buffer pointer is passed together with its buffer size, we should not have to strictly check the buffer is not NULL when the size is not 0. Note that we cannot verify that the passed pointer is really well sized regarding the passed size. That said, your changes add no regression and will have negligible impact on the API function execution performances so I'm fine we consider them.

By the way, I see that few of the functions in serialize_ck.c already return a error when mecha->ulParameterLen != sizeof(*params) but not all function do. For consistency, I think all these functions should. But that would need dedicated commit since your commit initially addresses potential NULL buffer references. Would you mind adding such a commit to your P-R?

By the way, I've not posted a Review-by explicit comment for this P-R so you should not add my R-b tag in the commit message.

@@ -420,13 +423,13 @@ static CK_RV serialize_mecha_aes_gcm(struct serializer *obj,
CK_RV rv = CKR_GENERAL_ERROR;
CK_ULONG aad_len = 0;

if (!param || !param->pIv)
Copy link
Contributor

Choose a reason for hiding this comment

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

I think test on !param->pIv was not really useful, testing !param should be enough. That said, for consistency, could you test !param || (param->ulIvLen && !param->pIv)

Copy link
Contributor

Choose a reason for hiding this comment

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

Looking again at PKCS#11 specifications, on one hand v2.40-errata01 says both AES-GCM IV can be NULL and must be between 1 and 256 byte. On the other hand v2.30 says AES-GCM IV must be between 1 and 2^32-1 bytes. In all one, I think the legacy implementation here in optee_client is enough, so OK for your proposal if (!param || !param->pIv).

Copy link

This pull request has been marked as a stale pull request because it has been open (more than) 30 days with no activity. Remove the stale label or add a comment, otherwise this pull request will automatically be closed in 5 days. Note, that you can always re-open a closed issue at any time.

@github-actions github-actions bot added the Stale label Nov 14, 2024
Copy link
Contributor

@etienne-lms etienne-lms left a comment

Choose a reason for hiding this comment

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

@hoyong2007, sorry for this very late feedback.
Changes look good to me, just an indentation to fix.

(params->ulPublicDataLen && !params->pPublicData))
return CKR_MECHANISM_PARAM_INVALID;

params_size = 3 * sizeof(uint32_t) + params->ulSharedDataLen +
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you fix indentation at line below?

@@ -420,13 +423,13 @@ static CK_RV serialize_mecha_aes_gcm(struct serializer *obj,
CK_RV rv = CKR_GENERAL_ERROR;
CK_ULONG aad_len = 0;

if (!param || !param->pIv)
Copy link
Contributor

Choose a reason for hiding this comment

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

Looking again at PKCS#11 specifications, on one hand v2.40-errata01 says both AES-GCM IV can be NULL and must be between 1 and 256 byte. On the other hand v2.30 says AES-GCM IV must be between 1 and 2^32-1 bytes. In all one, I think the legacy implementation here in optee_client is enough, so OK for your proposal if (!param || !param->pIv).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants