libckteec: fix NULL dereferences, overflow, and thread-safety in PKCS#11 serialization - #419
Merged
Merged
Conversation
jenswikl
reviewed
May 28, 2026
| return rv; | ||
|
|
||
| return rv; | ||
| return CKR_OK; |
Contributor
Author
There was a problem hiding this comment.
Ack, I'll discard this change.
manoj23
force-pushed
the
upstream/pkcs11-fixes
branch
from
May 28, 2026 13:39
0f93141 to
e39cf64
Compare
jenswikl
marked this pull request as ready for review
May 29, 2026 07:16
Contributor
|
Looks good to me. Please apply: |
Multiple functions in serialize_ck.c dereference mechanism parameters and attribute values without NULL checks, causing crashes when processing malformed input: - serialize_mecha_aes_ctr / aes_gcm / aes_cbc_encrypt_data / aes_iv / key_deriv_str / ecdh1_derive_param / rsa_pss_param / rsa_oaep_param / rsa_aes_key_wrap / eddsa / mac_general_param: NULL-check pParameter (and, in the OAEP/ECDH1 cases, defer the size computation until after the check). - serialize_mecha_rsa_aes_key_wrap also NULL-checks params->pOAEPParams, which was previously dereferenced unconditionally on the variable initialiser line. All handlers return CKR_MECHANISM_PARAM_INVALID for NULL pParameter, matching the existing return code used elsewhere in the file. CWE-476: NULL Pointer Dereference Found by fuzzing with AFL++/AddressSanitizer. Fixes: e7a4706 ("libckteec: add support for symmetric ciphers") Fixes: 0698b42 ("libckteec: serialize_ck: Add helper functions for mechanisms") Fixes: 98dc4f4 ("libckteec: serialize_ck.c: serialize AES-GCM mechanism parameters") Fixes: 8b3f7fe ("libckteec: add support for ECDH derive") Fixes: 6e94ff5 ("libckteec: Add PKCS OP-TEE#1 RSA PSS signing support") Fixes: cd66d40 ("libckteec: Add PKCS OP-TEE#1 RSA OAEP encryption support") Fixes: d26d885 ("libckteec: Add RSA AES key wrap serialization") Fixes: 7197806 ("libckteec: Add support for more HMAC mechanisms") Fixes: 140bf46 ("libckteec: Add EDDSA attribute serialization") Reported-by: Tobi Gaertner <tggaertn@amazon.com> Signed-off-by: Georges Savoundararadj <savoundg@amazon.com> Reviewed-by: Jens Wiklander <jens.wiklander@linaro.org>
serialize() calls memcpy(buf + *blen, data, len) without validating the data pointer. When a caller passes a wild or NULL pointer with a non-zero len, this causes an invalid memory access (CWE-476). Add input validation: - Reject len > 0 with NULL data, returning CKR_ARGUMENTS_BAD - Short-circuit len == 0 as a no-op, returning CKR_OK - Move variable declarations to top for C89 compliance Found by fuzzing with AFL++/AddressSanitizer. Fixes: 85a7ea7 ("libckteec: introduce helpers for serializing data") Reported-by: Tobi Gaertner <tggaertn@amazon.com> Signed-off-by: Georges Savoundararadj <savoundg@amazon.com> Reviewed-by: Jens Wiklander <jens.wiklander@linaro.org>
…ypt_data param->length is CK_ULONG (64-bit on LP64) but is added into a uint32_t size variable without bounds checking. Values above UINT32_MAX silently truncate, producing a small size value. The subsequent serialize_buffer() then writes param->length bytes into a buffer sized by the truncated value — heap buffer overflow. Add an overflow check before the size calculation to reject param->length values that would exceed the uint32_t budget. CWE-190 (Integer Overflow) leading to CWE-122 (Heap Buffer Overflow). Found by code review during fuzzing triage. Fixes: 0698b42 ("libckteec: serialize_ck: Add helper functions for mechanisms") Reported-by: Tobi Gaertner <tggaertn@amazon.com> Signed-off-by: Georges Savoundararadj <savoundg@amazon.com> Reviewed-by: Jens Wiklander <jens.wiklander@linaro.org>
strerror() returns a pointer to a shared static buffer that is not thread-safe. Since libckteec uses pthread_mutex for session management, concurrent threads hitting mutex errors could observe corrupted error strings. Replace with strerror_r() which writes to a caller-provided buffer. Check the return value and null-terminate on failure. Fixes: 2c812c5 ("libckteec: return CKR_CANT_LOCK if mutex fails at lib initialization") Reported-by: Tobi Gaertner <tggaertn@amazon.com> Signed-off-by: Georges Savoundararadj <savoundg@amazon.com> Reviewed-by: Jens Wiklander <jens.wiklander@linaro.org>
manoj23
force-pushed
the
upstream/pkcs11-fixes
branch
from
May 29, 2026 14:41
e39cf64 to
877d082
Compare
Contributor
Author
|
Hello, I added |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This series fixes several issues in libckteec found by fuzzing (AFL++ with AddressSanitizer) and static analysis (cppcheck, clang-tidy):
NULL pointer dereferences in mechanism serialization functions --
serialize_mecha_aes_ctr(),serialize_mecha_aes_gcm(),serialize_mecha_aes_cbc_encrypt_data(),serialize_mecha_eddsa(), andserialize_ck_mecha_params()dereferencemecha->pParameterwithout NULL checks. (CWE-476)Missing data pointer validation in
serialize()--memcpy(buf + *blen, data, len)is called without validatingdata, causing invalid memory access when a caller passes a NULL or wild pointer with non-zerolen.Integer overflow in
serialize_mecha_aes_cbc_encrypt_data()--param->length(CK_ULONG, 64-bit on LP64) is added intouint32_t sizewithout bounds checking. Values above UINT32_MAX silently truncate, producing an undersized buffer allocation. (CWE-190 leading to CWE-122)Redundant return value in
serialize_mecha_aes_ctr()-- the finalreturn rvis reached only after early-exit guards guaranteerv == CKR_OK. Return the constant directly.Thread-unsafe
strerror()ininvoke_ta.c-- replaced withstrerror_r()since libckteec uses pthread_mutex for session management.Tested with
xtest -t pkcs11: 31 test cases, 7682 subtests, 0 failures.