Skip to content

Commit 6f304d3

Browse files
bestminhaljenswikl
authored andcommitted
libckteec: fix CKA_ALLOWED_MECHANISMS size on 64-bit
On 64-bit systems, CKA_ALLOWED_MECHANISMS deserialization reports the wire format size (count * sizeof(uint32_t)) instead of the CK_ULONG-based size (count * sizeof(CK_ULONG)). This causes the standard PKCS#11 size-query pattern to allocate a buffer that is too small, leading to a heap buffer overflow when the mechanisms are written as CK_ULONG values. Fix the size reporting and buffer guard in deserialize_ck_attribute() to use CK_ULONG-based sizes for CKA_ALLOWED_MECHANISMS, and fix the buffer advancement in deserialize_ck_attributes() to match the wire format size. Signed-off-by: Minghao Cheng <m@minhal.me> Reviewed-by: Jens Wiklander <jens.wiklander@oss.qualcomm.com> Acked-by: Jerome Forissier <jerome.forissier@arm.com>
1 parent ab96380 commit 6f304d3

1 file changed

Lines changed: 18 additions & 0 deletions

File tree

libckteec/src/serialize_ck.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,20 @@ static CK_RV deserialize_ck_attribute(struct pkcs11_attribute_head *in,
288288
return CKR_OK;
289289
}
290290

291+
if (out->type == CKA_ALLOWED_MECHANISMS) {
292+
CK_ULONG ck_size = (in->size / sizeof(uint32_t)) *
293+
sizeof(CK_ULONG);
294+
295+
if (!out->pValue) {
296+
out->ulValueLen = ck_size;
297+
return CKR_OK;
298+
}
299+
if (out->ulValueLen < ck_size) {
300+
out->ulValueLen = ck_size;
301+
return CKR_OK;
302+
}
303+
}
304+
291305
if (out->ulValueLen < in->size) {
292306
out->ulValueLen = in->size;
293307
return CKR_OK;
@@ -363,6 +377,10 @@ CK_RV deserialize_ck_attributes(uint8_t *in, CK_ATTRIBUTE_PTR attributes,
363377
if (cur_attr->pValue) {
364378
if (ck_attr_is_ulong(cur_attr->type))
365379
len += sizeof(uint32_t);
380+
else if (cur_attr->type == CKA_ALLOWED_MECHANISMS)
381+
len += (cur_attr->ulValueLen /
382+
sizeof(CK_ULONG)) *
383+
sizeof(uint32_t);
366384
else
367385
len += cur_attr->ulValueLen;
368386
}

0 commit comments

Comments
 (0)