Commit 2c3c560
committed
Fix decrypt-context leak on abandoned streaming EnvelopedData decode
wc_PKCS7_Free() released cachedEncryptedContent but never released
pkcs7->decryptKey, the content-cipher context (Aes/Des/Des3) allocated
by wc_PKCS7_DecryptContentInit() on the streaming EnvelopedData decode
path. On WC_PKCS7_WANT_READ_E the decode deliberately keeps that context
to resume on the next input chunk, so a decode abandoned mid-message
(e.g. a truncated or malformed streaming message the caller gives up on)
and then freed leaked the cipher context.
wc_PKCS7_DecodeEnvelopedData()'s own error cleanup only runs for hard
errors (ret < 0 && ret != WC_PKCS7_WANT_READ_E), so it does not cover
the abandoned-WANT_READ case; the release has to happen in
wc_PKCS7_Free(). Before tearing down the stream, and only when a decrypt
context is still pending (decryptKey non-NULL), retrieve the content
cipher OID from the stream's saved var and call
wc_PKCS7_DecryptContentFree(). decryptKey is only ever left allocated by
this flow, where that saved var holds the content cipher OID; gating on
it leaves completed decodes and other streaming flows (whose saved var
is unrelated to any cipher) untouched, and avoids a spurious
"Unsupported content cipher type" debug message on their teardown.
Exposed by the new constructed-definite [0] negative tests, which decode
a malformed message (returning WC_PKCS7_WANT_READ_E) and then free the
wc_PKCS7 -- flagged by LeakSanitizer in CI.1 parent 855f70d commit 2c3c560
1 file changed
Lines changed: 29 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1494 | 1494 | | |
1495 | 1495 | | |
1496 | 1496 | | |
| 1497 | + | |
| 1498 | + | |
| 1499 | + | |
| 1500 | + | |
| 1501 | + | |
| 1502 | + | |
| 1503 | + | |
1497 | 1504 | | |
1498 | 1505 | | |
1499 | 1506 | | |
1500 | 1507 | | |
1501 | 1508 | | |
1502 | 1509 | | |
1503 | 1510 | | |
| 1511 | + | |
| 1512 | + | |
| 1513 | + | |
| 1514 | + | |
| 1515 | + | |
| 1516 | + | |
| 1517 | + | |
| 1518 | + | |
| 1519 | + | |
| 1520 | + | |
| 1521 | + | |
| 1522 | + | |
| 1523 | + | |
| 1524 | + | |
| 1525 | + | |
| 1526 | + | |
| 1527 | + | |
| 1528 | + | |
| 1529 | + | |
| 1530 | + | |
| 1531 | + | |
| 1532 | + | |
1504 | 1533 | | |
1505 | 1534 | | |
1506 | 1535 | | |
| |||
0 commit comments