Closed sgtatham closed 3 weeks ago
Possibly related to #388? But the segfault reported there is in a different location.
Version details: I had built osslsigncode from the current master
, which is commit 185cc0748dd5aeb867a9b5cddcd5ecdef06a04de. I'm on Ubuntu 24.04, with relevant package versions
Can you reproduce this issue with the latest master branch of libp11?
Yes, I just tried with commit https://github.com/OpenSC/libp11/commit/1d23e0cedcd3bd1b579e0b108445292ea29ac8c2. Same behaviour: a segfault in RSA_sign
, which goes away if I comment out that call to ENGINE_finish
in osslsigncode's read_token
.
@olszomal Could you take a look at it?
@sgtatham Could you try again using pkcs11.so
built from the latest master branch of https://github.com/OpenSC/libp11?
Looks good! Having first replicated the previous crash with https://github.com/OpenSC/libp11/commit/1d23e0cedcd3bd1b579e0b108445292ea29ac8c2, I updated to https://github.com/OpenSC/libp11/commit/ba4ae57f05ed708c4e2b88561893aa9addb9ee47 changing nothing else. This time I get no segfault, and see the output
Engine "pkcs11" set.
Workaround for OpenSSL 3.0.13 30 Jan 2024 enabled
Succeeded
and afterwards I have a correctly signed output file.
Thank you for testing.
I'm setting up osslsigncode to work with a SafeNet hardware token, via the PKCS11 driver library
libeTPkcs11.so
provided by SafeNet.Running a command of this form (details specific to my token omitted):
I found osslsigncode segfaulted. I believe this occurs because of this code at the end of
read_token()
inosslsigncode.c
:in which the call to
ENGINE_finish
frees a data structure that is needed by the later signing operation from withinpkcs7_sign_content
.Details of investigation: a gdb backtrace from the segfault pointed to somewhere deep in libengine-pkcs11-openssl:
To get more detail, I recompiled
pkcs11.so
from the Ubuntu package source with debugging enabled, and reproduced the segfault with the rebuilt library. The immediate cause of the segfault was thatpkcs11_getattr_var
was being called with actx
pointer that looked like 64 bits of binary nonsense and not a sensible address. Tracing back up, that pointer had been read out of aPKCS11_OBJECT_private *key
by accessingkey->slot->ctx
.By breakpointing
pkcs11_slot_new
I observed thatslot
structure being created with a sensiblectx
value; by watchpointing that to see when its value changed, I found that it was becoming corrupted by afree()
arising from the call toENGINE_finish()
in osslsigncode'sread_token()
. But apparently a pointer to the sameslot
structure was retained somewhere, and later read by the code called frompkcs7_sign_content
.My workaround is to remove the call to
ENGINE_finish()
completely, on the path leading to a success return fromread_token()
. I'm sure that's not a high-quality patch (probably the memory ought to be freed somewhere else instead), but it's prevented the segfault for me.