CVE-2026-40510
Description
OpenSC before 0.27.0-rc1, fixed in commit 3f24f0b, contains a stack buffer overflow vulnerability in piv_process_history() in src/libopensc/card-piv.c that allows physically present attackers to trigger memory corruption by presenting a crafted PIV smart card or USB device returning a URL field longer than 118 bytes in the Key History Object ASN.1 response.
AI Insight
LLM-synthesized narrative grounded in this CVE's description and references.
Stack buffer overflow in OpenSC's piv_process_history() allows physically present attackers to corrupt memory via crafted PIV cards.
Vulnerability
A stack buffer overflow vulnerability exists in the piv_process_history() function in src/libopensc/card-piv.c of OpenSC before version 0.27.0-rc1 [1][2][3]. The function parses the Key History Object ASN.1 response from a PIV card or USB device. It fails to validate the length of the URL field (tag 0xF3) before copying it into a fixed-size stack buffer. When the URL exceeds 118 bytes, the copy overflows the stack buffer, leading to memory corruption [1][3].
Exploitation
An attacker must be physically present to present a crafted PIV smart card or USB device that returns a Key History Object containing a URL field longer than 118 bytes [3]. The vulnerable code path is reachable when piv_process_history() is called during card initialization, without requiring any additional authentication [1]. The attacker controls the URL content, and the overflow occurs when the function copies the URL into the stack buffer without a prior length check [1][3].
Impact
Successful exploitation results in stack-based buffer overflow, causing memory corruption that can lead to undefined behavior, including potential denial of service or arbitrary code execution [3]. The physical access requirement limits the attack surface to scenarios where an attacker can supply a malicious card or device directly to a system running OpenSC [3].
Mitigation
The vulnerability is fixed in OpenSC 0.27.0-rc1 and commit 3f24f0b [1][2][3]. Users should upgrade to OpenSC 0.27.0-rc1 or later. No effective workaround is available [3].
AI Insight generated on May 29, 2026. Synthesized from this CVE's description and the cited reference URLs; citations are validated against the source bundle.
Affected products
2Patches
13f24f0b48a48Merge pull request #3558 from dengert/piv-history-fix
1 file changed · +16 −1
src/libopensc/card-piv.c+16 −1 modified@@ -5086,6 +5086,10 @@ piv_process_history(sc_card_t *card) url = sc_asn1_find_tag(card->ctx, body, bodylen, 0xF3, &urllen); if (url) { + if (urllen > 118) { + r = SC_ERROR_INVALID_ASN1_OBJECT; + goto err; + } priv->offCardCertURL = calloc(1,urllen+1); if (priv->offCardCertURL == NULL) LOG_FUNC_RETURN(card->ctx, SC_ERROR_OUT_OF_MEMORY); @@ -5112,8 +5116,9 @@ piv_process_history(sc_card_t *card) * the card. some of the certs may be on the card as well. * * Get file name from url. verify that the filename is valid - * The URL ends in a SHA1 string. We will use this as the filename + * The URL ends in a SHA-256 string. We will use this as the filename * in the directory used for the PKCS15 cache + * "http://" <DNS name> "/" <ASCII-HEX encoded SHA-256 hash of OffCardKeyHistoryFile> */ r = 0; @@ -5132,6 +5137,16 @@ piv_process_history(sc_card_t *card) goto err; } fp++; + if (strlen(fp) != 64) { /* ASCII-HEX encoded SHA-256 */ + r = SC_ERROR_INVALID_DATA; + goto err; + } + for (i = 0; i < 64; i++) { + if (isxdigit((unsigned char)fp[i]) == 0) { + r = SC_ERROR_INVALID_DATA; + goto err; + } + } /* Use the same directory as used for other OpenSC cached items */ r = sc_get_cache_dir(card->ctx, filename, sizeof(filename) - strlen(fp) - 2);
Vulnerability mechanics
Root cause
"Missing bounds check on the URL field length in the Key History Object ASN.1 parser allows a stack buffer overflow."
Attack vector
A physically present attacker presents a malicious PIV smart card or USB device that returns a crafted Key History Object ASN.1 response. The URL field (tag `0xF3`) is set to more than 118 bytes, which overflows the stack buffer allocated by `calloc(1, urllen+1)` in `piv_process_history()`. The attacker must have physical access to the reader and the ability to supply a specially crafted card or device [ref_id=1].
Affected code
**`piv_process_history()`** in `src/libopensc/card-piv.c` is the vulnerable function. The patch adds a length check on the URL field (tag `0xF3`) inside the Key History Object ASN.1 response, capping it at 118 bytes, and validates that the trailing filename portion is exactly 64 hex characters (a SHA-256 hash).
What the fix does
The patch inserts a guard that rejects any URL longer than 118 bytes (`if (urllen > 118)`) before the `calloc` allocation, preventing the stack buffer overflow. It also adds a check that the filename portion after the last `/` is exactly 64 ASCII hex characters and that every character is a valid hex digit, blocking malformed or oversized filenames that could cause further memory corruption [ref_id=1].
Preconditions
- networkAttacker must have physical access to the smart card reader
- inputAttacker must present a crafted PIV smart card or USB device that returns a malicious Key History Object
Generated on May 29, 2026. Inputs: CWE entries + fix-commit diffs from this CVE's patches. Citations validated against bundle.
References
3News mentions
0No linked articles in our index yet.