Skip to content

libckteec: fix NULL dereferences, overflow, and thread-safety in PKCS#11 serialization - #419

Merged
jenswikl merged 4 commits into
OP-TEE:masterfrom
manoj23:upstream/pkcs11-fixes
Jun 2, 2026
Merged

libckteec: fix NULL dereferences, overflow, and thread-safety in PKCS#11 serialization#419
jenswikl merged 4 commits into
OP-TEE:masterfrom
manoj23:upstream/pkcs11-fixes

Conversation

@manoj23

@manoj23 manoj23 commented May 27, 2026

Copy link
Copy Markdown
Contributor

This series fixes several issues in libckteec found by fuzzing (AFL++ with AddressSanitizer) and static analysis (cppcheck, clang-tidy):

  1. NULL pointer dereferences in mechanism serialization functions -- serialize_mecha_aes_ctr(), serialize_mecha_aes_gcm(), serialize_mecha_aes_cbc_encrypt_data(), serialize_mecha_eddsa(), and serialize_ck_mecha_params() dereference mecha->pParameter without NULL checks. (CWE-476)

  2. Missing data pointer validation in serialize() -- memcpy(buf + *blen, data, len) is called without validating data, causing invalid memory access when a caller passes a NULL or wild pointer with non-zero len.

  3. Integer overflow in serialize_mecha_aes_cbc_encrypt_data() -- param->length (CK_ULONG, 64-bit on LP64) is added into uint32_t size without bounds checking. Values above UINT32_MAX silently truncate, producing an undersized buffer allocation. (CWE-190 leading to CWE-122)

  4. Redundant return value in serialize_mecha_aes_ctr() -- the final return rv is reached only after early-exit guards guarantee rv == CKR_OK. Return the constant directly.

  5. Thread-unsafe strerror() in invoke_ta.c -- replaced with strerror_r() since libckteec uses pthread_mutex for session management.

Tested with xtest -t pkcs11: 31 test cases, 7682 subtests, 0 failures.

Comment thread libckteec/src/serialize_ck.c Outdated
Comment thread libckteec/src/serializer.c
Comment thread libckteec/src/serialize_ck.c Outdated
return rv;

return rv;
return CKR_OK;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ack, I'll discard this change.

@manoj23
manoj23 force-pushed the upstream/pkcs11-fixes branch from 0f93141 to e39cf64 Compare May 28, 2026 13:39
@jenswikl
jenswikl marked this pull request as ready for review May 29, 2026 07:16
@jenswikl

Copy link
Copy Markdown
Contributor

Looks good to me. Please apply:
Reviewed-by: Jens Wiklander <jens.wiklander@linaro.org>

manoj23 added 4 commits May 29, 2026 09:23
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
manoj23 force-pushed the upstream/pkcs11-fixes branch from e39cf64 to 877d082 Compare May 29, 2026 14:41
@manoj23

manoj23 commented Jun 1, 2026

Copy link
Copy Markdown
Contributor Author

Hello, I added Reviewed-by: Jens Wiklander <jens.wiklander@linaro.org>.

@jenswikl
jenswikl merged commit 7101df5 into OP-TEE:master Jun 2, 2026
2 checks passed
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.

2 participants