Skip to content

Segfault when SSL_CTX_get_cert_store returns NULL #3306

@dqp10515

Description

@dqp10515

There appears to be a bug in Mongoose’s OpenSSL TLS integration.

  • My goal is: Use mongoose's TLS functionality with openssl.
  • My actions were:
    Examined the add_ca_certs() function and its usage of SSL_CTX_get_cert_store()
    and reviewed OpenSSL source code.
  • My expectation was: Mongoose handles the case where SSL_CTX_get_cert_store() returns NULL.
  • The result I saw:
    Code Analysis Issue:
    static bool add_ca_certs(SSL_CTX *ctx, STACK_OF(X509_INFO) * certs) {
      int i;
      X509_STORE *cert_store = SSL_CTX_get_cert_store(ctx);  // No NULL check
      for (i = 0; i < sk_X509_INFO_num(certs); i++) {
        X509_INFO *cert_info = sk_X509_INFO_value(certs, i);
        if (cert_info->x509 && !X509_STORE_add_cert(cert_store, cert_info->x509))  // Potential NULL pointer dereference
          return false;
      }
      return true;
    }
    
    Crash Scenario:
    SSL_CTX_get_cert_store() can return NULL if the SSL_CTX’s certificate store was not successfully created during context initialization—for example, when X509_STORE_new() fails due to memory allocation failure or other internal initialization errors, leading to a segmentation fault.
    AddressSanitizer:DEADLYSIGNAL
    =================================================================
    ==296176==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000090 (pc 0x7f55c6506df4 bp 0x602000002190 sp 0x7ffddd145338 T0)
    ==296176==The signal is caused by a READ memory access.
    ==296176==Hint: address points to the zero page.
        #0 0x7f55c6506df4 in X509_STORE_lock (openssl/libcrypto.so.1.1+0x209df4)
        #1 0x7f55c6507baf in X509_STORE_add_cert (openssl/libcrypto.so.1.1+0x20abaf)
        #2 0x5a1b00 in add_ca_certs mongoose/tutorials/mqtt/mqtt-client/src/tls_openssl.c:45:29
        #3 0x5a002e in mg_tls_init mongoose/tutorials/mqtt/mqtt-client/src/tls_openssl.c:178:10
        #4 0x4cbb67 in mqtt_ev_handler mongoose/tutorials/mqtt/mqtt-client/main.c:49:7
        #5 0x4dc0c2 in mg_call mongoose/tutorials/mqtt/mqtt-client/src/event.c:22:22
        #6 0x59b7b4 in connect_conn mongoose/tutorials/mqtt/mqtt-client/src/sock.c:368:5
        #7 0x53dda5 in mg_mgr_poll mongoose/tutorials/mqtt/mqtt-client/src/sock.c:752:45
        #8 0x4caf73 in main mongoose/tutorials/mqtt/mqtt-client/main.c:117:5
        #9 0x7f55c5eea0b2 in __libc_start_main /build/glibc-eX1tMB/glibc-2.31/csu/../csu/libc-start.c:308:16
        #10 0x41d6bd in _start (mongoose/tutorials/mqtt/mqtt-client/client+0x41d6bd)
    
    AddressSanitizer can not provide additional info.
    SUMMARY: AddressSanitizer: SEGV (openssl/libcrypto.so.1.1+0x209df4) in X509_STORE_lock
    ==296176==ABORTING
    

Environment

  • mongoose version: 235dafe
  • Compiler/IDE and SDK: gcc
  • Target RTOS/OS (if applicable): Ubuntu 20.04

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions