Skip to content

Commit 8e14cce

Browse files
committed
libckteec: fix integer overflow in PKCS#11 serializer
Add overflow check in serialize() to prevent size_t wraparound when computing new buffer length. A crafted ulValueLen could cause *blen + len to wrap, leading to a small realloc followed by an out-of-bounds memcpy. Also add a bounds check in serialize_ck_attribute() for the CKA_ALLOWED_MECHANISMS path where n * sizeof(uint32_t) could overflow the uint32_t pkcs11_size, resulting in an undersized malloc. Fixes: 85a7ea7 ("libckteec: introduce helpers for serializing data") Signed-off-by: Minghao Cheng <m@minhal.me>
1 parent 32ae379 commit 8e14cce

2 files changed

Lines changed: 8 additions & 1 deletion

File tree

libckteec/src/serialize_ck.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,8 @@ static CK_RV serialize_ck_attribute(struct serializer *obj, CK_ATTRIBUTE *attr)
173173
return serialize_indirect_attribute(obj, attr);
174174
case CKA_ALLOWED_MECHANISMS:
175175
n = attr->ulValueLen / sizeof(CK_ULONG);
176+
if (n > UINT32_MAX / sizeof(uint32_t))
177+
return CKR_ARGUMENTS_BAD;
176178
pkcs11_size = n * sizeof(uint32_t);
177179
mech_buf = malloc(pkcs11_size);
178180
if (!mech_buf)

libckteec/src/serializer.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,12 @@ void release_serial_object(struct serializer *obj)
4545
static CK_RV serialize(char **bstart, size_t *blen, void *data, size_t len)
4646
{
4747
size_t nlen = *blen + len;
48-
char *buf = realloc(*bstart, nlen);
48+
char *buf = NULL;
49+
50+
if (nlen < *blen)
51+
return CKR_HOST_MEMORY;
52+
53+
buf = realloc(*bstart, nlen);
4954

5055
if (!buf)
5156
return CKR_HOST_MEMORY;

0 commit comments

Comments
 (0)