Skip to content

Commit 86063ae

Browse files
committed
Added an integer overflow protection
The problem was reported by Eric Sesterhenn
1 parent cf29891 commit 86063ae

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ New in 0.4.8; 2018-08-05; Michał Trojnara
1515
* Backward compatibility for new error handling introduced
1616
in libp11 0.4.7 (Michał Trojnara)
1717
* Memory leak fixes (Frank Morgner, Doug Engert)
18+
* Added an integer overflow protection (Eric Sesterhenn, Michał Trojnara)
1819
* Several bugfixes (Michał Trojnara, Emmanuel Deloget, Anderson Sasaki)
1920

2021
New in 0.4.7; 2017-07-03; Michał Trojnara

src/p11_slot.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,19 +43,26 @@ int pkcs11_enumerate_slots(PKCS11_CTX *ctx, PKCS11_SLOT **slotp, unsigned int *c
4343
CK_SLOT_ID *slotid;
4444
CK_ULONG nslots, n;
4545
PKCS11_SLOT *slots;
46+
size_t alloc_size;
4647
int rv;
4748

4849
rv = cpriv->method->C_GetSlotList(FALSE, NULL_PTR, &nslots);
4950
CRYPTOKI_checkerr(CKR_F_PKCS11_ENUMERATE_SLOTS, rv);
5051

51-
slotid = OPENSSL_malloc(nslots * sizeof(CK_SLOT_ID));
52+
alloc_size = nslots * sizeof(CK_SLOT_ID);
53+
if (alloc_size / sizeof(CK_SLOT_ID) != nslots) /* integer overflow */
54+
return -1;
55+
slotid = OPENSSL_malloc(alloc_size);
5256
if (slotid == NULL)
5357
return -1;
5458

5559
rv = cpriv->method->C_GetSlotList(FALSE, slotid, &nslots);
5660
CRYPTOKI_checkerr(CKR_F_PKCS11_ENUMERATE_SLOTS, rv);
5761

58-
slots = OPENSSL_malloc(nslots * sizeof(PKCS11_SLOT));
62+
alloc_size = nslots * sizeof(PKCS11_SLOT);
63+
if (alloc_size / sizeof(PKCS11_SLOT) != nslots) /* integer overflow */
64+
return -1;
65+
slots = OPENSSL_malloc(alloc_size);
5966
if (slots == NULL)
6067
return -1;
6168
memset(slots, 0, nslots * sizeof(PKCS11_SLOT));

0 commit comments

Comments
 (0)