Closed Bug 2027368 Opened 7 months ago Closed 7 months ago

Intra-struct buffer overflow in [@ ssl_DecodeResumptionToken] via oversized sessionID field

Categories

(NSS :: Libraries, defect, P1)

Tracking

(Not tracked)

RESOLVED DUPLICATE of bug 2020596

People

(Reporter: bugmon, Assigned: djackson)

Details

(5 keywords)

Attachments

(3 files)

Summary

Buffer overflow in security/nss/lib/ssl/sslnonce.c at line 628. ssl_DecodeResumptionToken reads the sessionID field from a serialized resumption token using a 1-byte length prefix (permitting 0–255 bytes) and copies it into sid->u.ssl3.sessionID, a fixed 32-byte array, without validating that the length fits. The 223-byte excess overwrites adjacent members of the sslSessionIDStr.u.ssl3 struct — notably the alpnSelection SECItem — giving the attacker control of a pointer and length that are subsequently used in PORT_Memcpy inside SSLExp_GetResumptionTokenInfo.

Affected Code

File: security/nss/lib/ssl/sslnonce.c, lines 622–629

if (sslRead_ReadVariable(&reader, 1, &readerBuffer) != SECSuccess) {
    PORT_SetError(SEC_ERROR_INVALID_ARGS);
    return SECFailure;
}
if (readerBuffer.len) {
    PORT_Assert(readerBuffer.buf);
    PORT_Memcpy(sid->u.ssl3.sessionID, readerBuffer.buf, readerBuffer.len);
}

The destination sid->u.ssl3.sessionID is declared at sslimpl.h:460 as PRUint8 sessionID[SSL3_SESSIONID_BYTES] where SSL3_SESSIONID_BYTES == 32 (sslimpl.h:108). Because sslRead_ReadVariable was called with lenSize = 1, readerBuffer.len can be up to 255. No comparison against SSL3_SESSIONID_BYTES is performed before the copy.

Immediately following sessionID in the struct are cipherSuite, policy, keys, masterWrapMech, slot IDs, srvName, signedCertTimestamps, and alpnSelection — all corrupted by the overflow. Notably the wrapped_master_secret field just below does have a bounds check (lines 644–648), showing the sessionID omission is an oversight rather than a deliberate trust assumption.

Correct Pattern

if (sslRead_ReadVariable(&reader, 1, &readerBuffer) != SECSuccess) {
    PORT_SetError(SEC_ERROR_INVALID_ARGS);
    return SECFailure;
}
if (readerBuffer.len > SSL3_SESSIONID_BYTES) {
    PORT_SetError(SEC_ERROR_INVALID_ARGS);
    return SECFailure;
}
if (readerBuffer.len) {
    PORT_Assert(readerBuffer.buf);
    PORT_Memcpy(sid->u.ssl3.sessionID, readerBuffer.buf, readerBuffer.len);
}

Exploit Chain

  1. Application retrieves a stored TLS resumption token from persistent storage (disk cache, database, or shared store) that has been tampered with by an attacker.
  2. Application calls the public API SSL_GetResumptionTokenInfo(tokenData, tokenLen, &info, sizeof(info)) — exported via sslexp.h:434 — or SSL_SetResumptionToken to resume the session.
  3. SSLExp_GetResumptionTokenInfo allocates sslSessionID sid = {0} on the stack and calls ssl_DecodeResumptionToken(&sid, tokenData, tokenLen).
  4. The malformed token encodes a sessionID field with length byte 0xFF followed by 255 attacker-controlled bytes.
  5. At sslnonce.c:628, PORT_Memcpy writes 255 bytes into the 32-byte sid.u.ssl3.sessionID[], overflowing into cipherSuite, policy, keys, masterWrapMech, slot IDs, srvName, signedCertTimestamps, and alpnSelection.
  6. Back in SSLExp_GetResumptionTokenInfo at line 4693–4701, sid.u.ssl3.alpnSelection.len (now attacker-controlled, e.g. 0x41414141) is passed to PORT_ZAlloc, and sid.u.ssl3.alpnSelection.data (now an attacker-controlled pointer, e.g. 0x4141414141414141) is used as the source of PORT_Memcpy.
  7. ASAN reports a wild-pointer read. With a valid mapped address, this becomes an arbitrary-address read primitive whose output is returned to the caller in tokenOut->alpnSelection, leaking process memory.
  8. Via the SSL_SetResumptionToken path, the sid is heap-allocated (ssl3_NewSessionID), and the corrupted SECItem pointers (srvName.data, signedCertTimestamps.data, alpnSelection.data) are later passed to SECITEM_FreeItem in ssl_DestroySID, yielding an arbitrary-free primitive.

Security Impact

Severity: Moderate–High.

Attacker capability:

  • Intra-struct overflow of up to 223 bytes with fully controlled content.
  • Control of SECItem.data and SECItem.len for srvName, signedCertTimestamps, and alpnSelection.
  • Arbitrary-read info leak via SSLExp_GetResumptionTokenInfo (corrupted pointer + length copied into caller-visible buffer).
  • Arbitrary-free via ssl_DestroySID when reached through SSL_SetResumptionToken, which can be escalated to use-after-free / heap corruption with grooming.

Preconditions: Attacker must be able to supply or modify a resumption token before the application passes it to NSS. This typically requires local file-system access to the application's session cache, a shared multi-tenant cache, or an application bug that forwards untrusted bytes to these APIs. The token format is not network-exposed by NSS itself, so this is not directly remote-triggerable by a TLS peer.

ASAN Report

==54534==ERROR: AddressSanitizer: unknown-crash on address 0x4141414141414141 at pc 0x7e6aa3f0e42e bp 0x7ffefd25e1b0 sp 0x7ffefd25d958
READ of size 1094795585 at 0x4141414141414141 thread T0
    #0 0x7e6aa3f0e42d in memcpy ../../../../src/libsanitizer/sanitizer_common/sanitizer_common_interceptors_memintrinsics.inc:115
    #1 0x576871ff47cf in SSLExp_GetResumptionTokenInfo ../../lib/ssl/sslsock.c:4700
    #2 0x576871b48cee in nss_test::ResumptionTokenOverflow_SessionIDOverflow_Test::TestBody() ../../gtests/ssl_gtest/ssl_token_overflow_unittest.cc:155
    #3 0x576871f1ac6c in void testing::internal::HandleSehExceptionsInMethodIfSupported<testing::Test, void>(testing::Test*, void (testing::Test::*)(), char const*) ../../gtests/google_test/gtest/src/gtest.cc:2607
    #4 0x576871f0815f in void testing::internal::HandleExceptionsInMethodIfSupported<testing::Test, void>(testing::Test*, void (testing::Test::*)(), char const*) ../../gtests/google_test/gtest/src/gtest.cc:2643
    #5 0x576871eb0f61 in testing::Test::Run() ../../gtests/google_test/gtest/src/gtest.cc:2682
    #6 0x576871eb2589 in testing::TestInfo::Run() ../../gtests/google_test/gtest/src/gtest.cc:2861
    #7 0x576871eb39ec in testing::TestSuite::Run() ../../gtests/google_test/gtest/src/gtest.cc:3015
    #8 0x576871eda9e4 in testing::internal::UnitTestImpl::RunAllTests() ../../gtests/google_test/gtest/src/gtest.cc:5855
    #9 0x576871f1e1d4 in bool testing::internal::HandleSehExceptionsInMethodIfSupported<testing::internal::UnitTestImpl, bool>(testing::internal::UnitTestImpl*, bool (testing::internal::UnitTestImpl::*)(), char const*) ../../gtests/google_test/gtest/src/gtest.cc:2607
    #10 0x576871f0acb6 in bool testing::internal::HandleExceptionsInMethodIfSupported<testing::internal::UnitTestImpl, bool>(testing::internal::UnitTestImpl*, bool (testing::internal::UnitTestImpl::*)(), char const*) ../../gtests/google_test/gtest/src/gtest.cc:2643
    #11 0x576871ed6f01 in testing::UnitTest::Run() ../../gtests/google_test/gtest/src/gtest.cc:5438
    #12 0x5768718ffaf1 in RUN_ALL_TESTS() ../../gtests/google_test/gtest/include/gtest/gtest.h:2490
    #13 0x5768718ffdcb in main ../../gtests/ssl_gtest/ssl_gtest.cc:47
    #14 0x7e6aa34c71c9  (/lib/x86_64-linux-gnu/libc.so.6+0x2a1c9)
    #15 0x7e6aa34c728a in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2a28a)
    #16 0x5768713f5e14 in _start (/firefox/security/dist/Debug/bin/ssl_gtest+0x225e14)

Address 0x4141414141414141 is a wild pointer inside of access range of size 0x000041414141.
SUMMARY: AddressSanitizer: unknown-crash ../../../../src/libsanitizer/sanitizer_common/sanitizer_common_interceptors_memintrinsics.inc:115 in memcpy
==54534==ABORTING

Suggested Fix

Add a bounds check identical in style to the wrapped_master_secret check a few lines below:

--- a/security/nss/lib/ssl/sslnonce.c
+++ b/security/nss/lib/ssl/sslnonce.c
@@ -622,6 +622,10 @@ ssl_DecodeResumptionToken(sslSessionID *sid, ...)
     if (sslRead_ReadVariable(&reader, 1, &readerBuffer) != SECSuccess) {
         PORT_SetError(SEC_ERROR_INVALID_ARGS);
         return SECFailure;
     }
+    if (readerBuffer.len > SSL3_SESSIONID_BYTES) {
+        PORT_SetError(SEC_ERROR_INVALID_ARGS);
+        return SECFailure;
+    }
     if (readerBuffer.len) {
         PORT_Assert(readerBuffer.buf);
         PORT_Memcpy(sid->u.ssl3.sessionID, readerBuffer.buf, readerBuffer.len);
     }

Optionally also validate that the separately-encoded sessionIDLength field (read at line 617–620) matches readerBuffer.len.

Attached file crash_stack.txt —
Group: core-security → crypto-core-security

Marking sec-low because compromising the local session cache is a bit strange threat model.

Assignee: nobody → djackson
Severity: -- → S4
Keywords: sec-moderate → sec-low
Priority: -- → P1
Status: UNCONFIRMED → RESOLVED
Closed: 7 months ago
Duplicate of bug: 2020596
Resolution: --- → DUPLICATE
Group: crypto-core-security
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: