Skip to content

Commit d50b169

Browse files
Jakujefrankmorgner
authored andcommittedDec 11, 2024
pkcs11-spy: Avoid crash while spying C_GetInterface()
When calling the C_GetInterface(), the spy was trying to change the memory returned by the underlying pkcs11 module. In cases where it was pointing to the readonly/static memory, the application crashed. This changes the C_GetInterface() in a way that it does not return the original memory of underlying pkcs11 module, but local one with our pointers. Signed-off-by: Jakub Jelen <jjelen@redhat.com>
1 parent 3e90fd0 commit d50b169

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed
 

‎src/pkcs11/pkcs11-spy.c

+17-9
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,8 @@ CK_INTERFACE compat_interfaces[NUM_INTERFACES] = {
187187
{"PKCS 11", NULL, 0}
188188
};
189189

190+
CK_INTERFACE spy_interface = {"PKCS 11", NULL, 0};
191+
190192
/* Inits the spy. If successful, po != NULL */
191193
static CK_RV
192194
init_spy(void)
@@ -1610,22 +1612,24 @@ C_WaitForSlotEvent(CK_FLAGS flags, CK_SLOT_ID_PTR pSlot, CK_VOID_PTR pRserved)
16101612
return retne(rv);
16111613
}
16121614

1613-
/* PKCS #11 3.0 functions */
1615+
/* Returns spied PKCS #11 3.0 interface based on the interface version returned from the
1616+
* underlying pkcs11 module, respecting major versions */
16141617
static void
1615-
spy_interface_function_list(CK_INTERFACE_PTR pInterface)
1618+
spy_interface_function_list(CK_INTERFACE_PTR pInterface, CK_INTERFACE_PTR_PTR retInterface)
16161619
{
16171620
CK_VERSION *version;
16181621

16191622
/* Do not touch unknown interfaces. We can not do anything with these */
16201623
if (strcmp(pInterface->pInterfaceName, "PKCS 11") != 0) {
1624+
*retInterface = pInterface;
16211625
return;
16221626
}
16231627

16241628
version = (CK_VERSION *)pInterface->pFunctionList;
16251629
if (version->major == 2) {
1626-
pInterface->pFunctionList = pkcs11_spy;
1630+
(*retInterface)->pFunctionList = pkcs11_spy;
16271631
} else if (version->major == 3 && version->minor == 0) {
1628-
pInterface->pFunctionList = pkcs11_spy_3_0;
1632+
(*retInterface)->pFunctionList = pkcs11_spy_3_0;
16291633
}
16301634
}
16311635

@@ -1686,7 +1690,8 @@ C_GetInterfaceList(CK_INTERFACE_PTR pInterfacesList, CK_ULONG_PTR pulCount)
16861690

16871691
/* Now, replace function lists of known interfaces (PKCS 11, v 2.x and 3.0) */
16881692
for (i = 0; i < *pulCount; i++) {
1689-
spy_interface_function_list(&pInterfacesList[i]);
1693+
CK_INTERFACE_PTR pInterface = &pInterfacesList[i];
1694+
spy_interface_function_list(pInterface, &pInterface);
16901695
}
16911696
}
16921697

@@ -1726,10 +1731,12 @@ C_GetInterface(CK_UTF8CHAR_PTR pInterfaceName, CK_VERSION_PTR pVersion,
17261731
fprintf(spy_output, "[in] flags = %s\n",
17271732
(flags & CKF_INTERFACE_FORK_SAFE ? "CKF_INTERFACE_FORK_SAFE" : ""));
17281733
if (po->version.major >= 3) {
1734+
CK_INTERFACE_PTR rInterface = NULL;
1735+
17291736
/* We can not assume the version we told the caller matches the version in the underlying
17301737
* pkcs11 module so map it back to the known ones */
1731-
CK_VERSION in_version;
17321738
if ((pInterfaceName == NULL || strcmp((char *)pInterfaceName, "PKCS 11") == 0) && pVersion) {
1739+
CK_VERSION in_version;
17331740
for (unsigned long i = 0; i < num_orig_interfaces; i++) {
17341741
CK_VERSION *v = (CK_VERSION *)orig_interfaces[i].pFunctionList;
17351742
/* We found the same major version. Copy the minor and call it a day */
@@ -1745,9 +1752,10 @@ C_GetInterface(CK_UTF8CHAR_PTR pInterfaceName, CK_VERSION_PTR pVersion,
17451752
/* If not found, see what we will get */
17461753
}
17471754

1748-
rv = po->C_GetInterface(pInterfaceName, pVersion, ppInterface, flags);
1749-
if (rv == CKR_OK && ppInterface != NULL) {
1750-
spy_interface_function_list(*ppInterface);
1755+
rv = po->C_GetInterface(pInterfaceName, pVersion, &rInterface, flags);
1756+
if (rv == CKR_OK && rInterface != NULL) {
1757+
*ppInterface = &spy_interface;
1758+
spy_interface_function_list(rInterface, ppInterface);
17511759
}
17521760
} else {
17531761
if ((pInterfaceName == NULL_PTR || strcmp((char *)pInterfaceName, "PKCS 11") == 0) &&

0 commit comments

Comments
 (0)