C_Sign succeeds after C_Logout (cached privateKey / no resetOp)
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Idoneità per principianti
- 43/100
Direzione di ricerca
Start with SoftHSM::C_Logout in src/lib/SoftHSM.cpp and the session reset logic described in the issue. Trace how logout affects every session on the slot, then check the sign operation behavior from the repro. Done means an in-progress sign cannot succeed after logout; the issue expects CKR_OPERATION_NOT_INITIALIZED after resetting operations.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
Component: SoftHSMv2 PKCS#11 module
Seen on: 2.6.1 and current develop (f59e8f4, 2.7.0-26-gf59e8f4)
Severity: private-key use after logout (session ACL bypass)
Summary
C_SignInit on a private key while logged in as USER, followed by C_Logout, then C_Sign still returns CKR_OK and a valid signature. After logout the session is CKS_RW_PUBLIC_SESSION. Private-object handles are correctly invalidated, but the in-progress sign operation keeps a cached privateKey on the Session object.
Same pattern applies to C_SignUpdate / C_SignFinal.
Spec
PKCS#11 v2.40 §5.6 C_Logout:
- handles to private objects become invalid
- those handles stay invalid after a later re-login
- private session objects are destroyed
access.cpp haveRead also forbids private-object use in CKS_*_PUBLIC_SESSION and CKS_RW_SO_FUNCTIONS (CKR_USER_NOT_LOGGED_IN).
C_Sign never re-checks that table; it uses session->getPrivateKey().
Repro (unpatched tree)
Needs a token with an RSA private key. Distro SoftHSM 2.6.1 or a clean clone of this repo is enough. Do not use a tree that already has sessionManager->resetSlotOperations() in C_Logout.
1. Token
export SOFTHSM2_CONF=/tmp/softhsm-logout-repro.conf
mkdir -p /tmp/softhsm-logout-repro-tokens
cat > "$SOFTHSM2_CONF" <<EOF
directories.tokendir = /tmp/softhsm-logout-repro-tokens
objectstore.backend = file
log.level = ERROR
EOF
softhsm2-util --init-token --free --label logout-repro --so-pin 1234 --pin 1234
pkcs11-tool --module /usr/lib/softhsm/libsofthsm2.so \
--login --pin 1234 --keypairgen --key-type rsa:2048 --id 01 --label repro-rsa
Adjust --module if SoftHSM is installed elsewhere (libsofthsm2.so).
2. Program
Save as repro-sign-after-logout.c and build:
cc -O0 -g repro-sign-after-logout.c -ldl -o repro-sign-after-logout
./repro-sign-after-logout /usr/lib/softhsm/libsofthsm2.so 1234
#define _GNU_SOURCE
#include <dlfcn.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pkcs11.h> /* or SoftHSM's src/lib/pkcs11/pkcs11.h */
static CK_FUNCTION_LIST_PTR p11;
static void die(const char *m, CK_RV rv) {
fprintf(stderr, "%s: 0x%lx\n", m, (unsigned long)rv);
exit(2);
}
int main(int argc, char **argv) {
const char *mod = argc > 1 ? argv[1] : "/usr/lib/softhsm/libsofthsm2.so";
CK_UTF8CHAR_PTR pin = (CK_UTF8CHAR_PTR)(argc > 2 ? argv[2] : "1234");
CK_ULONG pin_len = (CK_ULONG)strlen((char *)pin);
CK_C_GetFunctionList get_fl;
CK_SLOT_ID slot, *slots;
CK_ULONG nslots = 0, count = 0, sig_len;
CK_SESSION_HANDLE sess;
CK_OBJECT_HANDLE key = CK_INVALID_HANDLE;
CK_OBJECT_CLASS cls = CKO_PRIVATE_KEY;
CK_BYTE id = 0x01, msg[] = "logout-repro", sig[512];
CK_ATTRIBUTE tmpl[] = {
{ CKA_CLASS, &cls, sizeof(cls) },
{ CKA_ID, &id, sizeof(id) },
};
CK_MECHANISM mech = { CKM_SHA256_RSA_PKCS, NULL_PTR, 0 };
CK_SESSION_INFO info;
CK_RV rv;
void *dl;
dl = dlopen(mod, RTLD_NOW | RTLD_GLOBAL);
if (!dl) { fprintf(stderr, "dlopen: %s\n", dlerror()); return 2; }
get_fl = (CK_C_GetFunctionList)dlsym(dl, "C_GetFunctionList");
if (!get_fl || get_fl(&p11) != CKR_OK) die("C_GetFunctionList", CKR_GENERAL_ERROR);
if ((rv = p11->C_Initialize(NULL_PTR)) != CKR_OK) die("C_Initialize", rv);
p11->C_GetSlotList(CK_TRUE, NULL_PTR, &nslots);
slots = calloc(nslots, sizeof(*slots));
p11->C_GetSlotList(CK_TRUE, slots, &nslots);
slot = slots[0];
free(slots);
if ((rv = p11->C_OpenSession(slot, CKF_SERIAL_SESSION | CKF_RW_SESSION,
NULL_PTR, NULL_PTR, &sess)) != CKR_OK)
die("C_OpenSession", rv);
if ((rv = p11->C_Login(sess, CKU_USER, pin, pin_len)) != CKR_OK)
die("C_Login", rv);
if ((rv = p11->C_FindObjectsInit(sess, tmpl, 2)) != CKR_OK) die("FindInit", rv);
p11->C_FindObjects(sess, &key, 1, &count);
p11->C_FindObjectsFinal(sess);
if (count != 1) die("no private key id=01", CKR_GENERAL_ERROR);
if ((rv = p11->C_SignInit(sess, &mech, key)) != CKR_OK) die("C_SignInit", rv);
if ((rv = p11->C_Logout(sess)) != CKR_OK) die("C_Logout", rv);
if ((rv = p11->C_GetSessionInfo(sess, &info)) != CKR_OK) die("C_GetSessionInfo", rv);
sig_len = sizeof(sig);
rv = p11->C_Sign(sess, msg, sizeof(msg) - 1, sig, &sig_len);
printf("after logout: state=0x%lx C_Sign=0x%lx sig_len=%lu\n",
(unsigned long)info.state, (unsigned long)rv, (unsigned long)sig_len);
/* Bug: rv == CKR_OK and state is CKS_RW_PUBLIC_SESSION (0x2). */
return (rv == CKR_OK) ? 1 : 0;
}
3. Actual vs expected
| Actual (bug) | Expected | |
|---|---|---|
C_GetSessionInfo after logout |
CKS_RW_PUBLIC_SESSION (0x2) |
same |
C_Sign |
CKR_OK (0x0), valid signature |
CKR_OPERATION_NOT_INITIALIZED or CKR_USER_NOT_LOGGED_IN |
C_SignInit again on the same handle |
CKR_OBJECT_HANDLE_INVALID (handles are invalidated) |
same |
Exit status 1 from the program means the bug is present.
Sanitizers do not fire. This is a PKCS#11 state bug, not memory corruption.
Root cause
C_Logout (src/lib/SoftHSM.cpp) invalidates private handles and destroys private session objects, but does not call Session::resetOp().
AsymSign (C_Sign) then uses session->getPrivateKey() and does not call haveRead().
Login is token-wide, so every session on the slot can still finish a sign that was initialized before logout.
// C_Logout today: token->logout(); handleManager->tokenLoggedOut(); sessionObjectStore->tokenLoggedOut();
// missing: resetOp() on all sessions for that slot
Suggested fix (optional)
Reset crypto state on every session of the slot in C_Logout:
void SessionManager::resetSlotOperations(CK_SLOT_ID slotID)
{
MutexLocker lock(sessionsMutex);
for (std::vector<Session*>::iterator i = sessions.begin(); i != sessions.end(); i++)
{
if (*i == NULL) continue;
if ((*i)->getSlot()->getSlotID() == slotID)
(*i)->resetOp();
}
}
Call it from SoftHSM::C_Logout after tokenLoggedOut. After that, C_Sign returns CKR_OPERATION_NOT_INITIALIZED.
- Lingua principale
- C++
- Stelle
- 1.1k
- Fork
- 434
- Merge medio
- 12g 14h
- PR unite (30g)
- 2
Preparare l'ambiente
Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di softhsm/SoftHSMv2
-
C_DigestKey digests a sensitive EC private key's CKA_VALUE: no check that the object is a secret keyAperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
-
Difficoltà 4/5 3-5 giorni Idoneità per principianti 54/100
-
Difficoltà 4/5 3-5 giorni Idoneità per principianti 56/100
Tutte le issue di softhsm/SoftHSMv2
Issue simili
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
charmplusplus/charm#4210 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 66/100
I maintainer di solito rispondono entro 1 giorno
-
[BUG] VPN auth dialog spawned by the secret agent fails with "TLS support is not available" on NixOSApertabug nixos umbriel
Difficoltà 2/5 1-3 ore Idoneità per principianti 62/100
noctalia-dev/noctalia#5310 ·
I maintainer di solito rispondono entro 1 giorno
-
area/docdb kind/bug priority/medium status/awaiting-triage
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
yugabyte/yugabyte-db#37426 ·
I maintainer di solito rispondono entro 1 giorno
-
[Engine] Two prompt block scorers enabled at once: WMMA silently wins and no log line is printedAperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
Niko1221/Strata#5693 · 2 commenti ·
I maintainer di solito rispondono entro 4 giorni