Re: PKCS11 Extension
| From: | Johannes Schlüter | Date: | Mon, 20 Jul 2020 12:55:33 +0000 |
| Subject: | Re: PKCS11 Extension | ||
| References: | 1 | Groups: | php.pecl.dev |
| Request: | Send a blank email to pecl-dev+get-16841@lists.php.net to get a copy of this message | ||
Hi,
On Sun, 2020-07-19 at 22:33 -0400, Guillaume Amringer wrote:
> The extension is currently hosted here:
> https://github.com/gamringer/php-pkcs11
I scrolled over it without much attention (thus probably missed other
issues) a few comments to this line I noticed:
pSlotList = (CK_SLOT_ID_PTR) malloc(ulSlotCount * sizeof(CK_SLOT_ID));
https://github.com/gamringer/php-pkcs11/blob/master/pkcs11.c#L300
1. The return value of malloc() is not checked. If the system runs out
of memory (OOM) or for some other reason can't return a memory block
this will return NULL and then lead to undefined behavior later.
(unlikely on today's systems, but might happen)
2. emalloc should be used instead of malloc. For one it counts towards
PHP's memory_limit, ten it also ensures the memory is freed if
something weird happens by the end of the request, thus reduces risk
of memory leaks and it fixes point 1. by terminating the request in
an OOM situation.
3. ulSlotCount * sizeof(CK_SLOT_ID) could eventually overflow. Better
use
safe_emalloc(ulSlotCount, sizeof(CK_SLOT_ID), 0)
this
calculates
ulSlotCount * sizeof(CK_SLOT_ID) + 0
in a overflow-safe
way and errors out in case of a problem.
johannes