Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Message-ID: <CAHv6NagBhrwbOz1daaqSSaXEwAYVDe37ZPpgjdfX7fBaVzbJ-w@mail.gmail.com>
Date: Mon, 30 Nov 2026 13:50:02 +0800
From: convre <c0nvr3@...il.com>
To: musl@...ts.openwall.com
Subject: wordexp() executes unquoted shell metacharacters that POSIX requires
 it to reject (flags without WRDE_NOCMD); WRDE_UNDEF unimplemented

Hello,

I'd like to report a security-relevant conformance gap in musl's
wordexp() that I hope can be fixed. Everything below was tested locally
on stock builds; nothing was run against third-party systems. If you
consider any part of this a design decision rather than a bug, I'd
genuinely like to reconcile it against the POSIX text quoted below.

Summary
-------

musl's wordexp() runs the words string through /bin/sh with only a thin
pre-scan. The pre-scan that rejects unquoted metacharacters
(; & | < > ( ) { } and newline) with WRDE_BADCHAR is gated inside
"if (flags & WRDE_NOCMD)" (src/misc/wordexp.c:43). In other words, the
BADCHAR check already exists in musl and works -- it is just gated
behind the wrong flag; this is a gating bug, not a missing feature.
POSIX.1-2017 (XSH, wordexp()) does not condition the BADCHAR requirement
on WRDE_NOCMD, and the standard's APPLICATION USAGE states the check
exists precisely to prevent "unwanted side-effects, such as executing a
command or writing a file."

On musl, wordexp("a; : > /tmp/marker", &we, 0) returns 0 and the marker
file is created; on glibc the same call returns WRDE_BADCHAR and nothing
is executed. WRDE_UNDEF/WRDE_BADVAL are also unimplemented (an undefined
variable under WRDE_UNDEF silently expands to zero words instead of
failing).

Scope clarification (to avoid confusion)
----------------------------------------

With WRDE_NOCMD set, musl's scan does work: I verified that $(...),
backticks, and even arithmetic-nested command substitution ($((`...`)) --
the shape glibc missed before CVE-2014-7817) all return WRDE_CMDSUB and
are not executed. The issue is exclusively the flags-without-NOCMD
case, which is the common call shape in the wild (all glibc-based
development and testing makes it look safe), plus the missing WRDE_UNDEF.
(Side portability note observed while testing: musl's
WRDE_CMDSUB/WRDE_SYNTAX numeric values are swapped relative to glibc --
POSIX permits any distinct nonzero values, and code using the macro names
is unaffected, but hardcoded/serialized numeric comparisons are not
portable.)

In-the-wild exposure (flags=0, confirmed by reading the code)
--------------------------------------------------------------

The common call shape in the wild is flags=0, and on glibc it is kept
safe by BADCHAR rejection -- so the same code silently becomes an
execution path on musl. One root-context example, confirmed end to end:

- fsarchiver (backup/restore, runs as root, packaged in Alpine's main
  repository): exec_command() ignores the wordexp return value and
  builds "mkfs.* -f %s %s" command strings where the options come from
  the archive's own filesystem labels/UUIDs (src/common.c:326, all 8
  fs_*.c backends). During restfs as root, metadata from an untrusted
  .fsa reaches a flags=0 wordexp: glibc makes the restore fail safely
  with BADCHAR; musl runs the string through /bin/sh. Data fields
  (labels/UUIDs) escaping into command syntax is exactly the pattern the
  BADCHAR check exists to stop.

The same flags=0 shape appears elsewhere in code I read directly --
not an accusation against any of these projects: the same code is safe
on glibc, which is precisely the problem. Sway/swayfx/scroll expand
config include paths with flags=0; clifm and sc-im expand strings that
include third-party-crafted filenames, so a filename carrying unquoted
; or a backtick becomes execution on musl, BADCHAR on glibc; merecat
expands Redirect targets on the request path with host/request_uri/args
exported from the raw HTTP request; gsocket's filetransfer expands
patterns supplied by the remote peer; and about ten more user-session
tools (i3, Waybar, conky, lxqt-runner, fastfetch, ...) call wordexp
with flags=0 on the user's own config/session. For calibration: every
root/daemon-context caller found in the same sweep (jailkit, ifupdown,
collectd, systemtap, condor, sudo's noexec interceptor, ...) passes
WRDE_NOCMD and is immune on musl -- the exposure is specifically the
flags-without-NOCMD shape, which glibc-based development makes look
safe. I have the full caller-by-caller list ready and can file it as a
follow-up portability advisory if that is useful.

Two ecosystem data points worth noting: a third-party seccomp sandbox
author documented in the wild that "musl actually exec /bin/sh" during
wordexp (klutshnik sbox notes), and sudo ships a noexec interceptor
that force-adds WRDE_NOCMD to all wordexp calls (CVE-2016-7076) -- the
ecosystem already treats wordexp execution as a real attack surface.

The strongest exposure patterns are fsarchiver (root + untrusted
archive content) and the clifm/sc-im filename-injection shape. fsarchiver
ships pre-built in Alpine's main repository, so this is not a
hypothetical build: "apk add fsarchiver" delivers the affected call
shape on musl. I reproduced the full chain against the stock,
unmodified, distro binaries on both sides:

- musl side: stock Alpine Linux 3.22 fsarchiver 0.8.8-r0 (musl 1.2.5),
  running as root. A .fsa archive whose filesystem-UUID metadata field
  (36 chars -- the only validation fsarchiver applies to this field is
  the length) contains ";touch /tmp/fsa_m29_real;" reaches the
  unquoted " -U %s " option of the mke2fs command string. On musl the
  payload is executed inside the wordexp() call itself -- confirmed by
  the marker file being created (two independent runs) -- before or
  without the intended mkfs exec (mke2fs then failed on the mangled
  arguments). fsarchiver's own error log prints the injected command
  line.
- glibc side: stock Ubuntu 24.04 fsarchiver 0.8.7 (glibc 2.39), same
  archive: wordexp returns WRDE_BADCHAR, nothing is executed, and the
  restore fails safely. fsarchiver never initializes the wordexp_t
  before the call and ignores the return value, so the consumer loop
  reads an indeterminate structure -- in my run it saw no usable words,
  argv[0] came out (null) and the command was simply not found. That is
  a latent robustness wart in fsarchiver (it should check the return
  value), but on glibc the observable outcome is a safe failure:
  nothing executes.

Same input, glibc refuses (safe failure), musl executes. The archive
patch is byte-level and format-preserving (same-length field, checksum
recomputed per fsarchiver's own on-disk format), and "fsarchiver
archinfo" accepts the modified archive; the call sites in both 0.8.7
and 0.8.8 are identical (wordexp(command, &p, 0) with the return value
ignored at src/common.c:326). This is the same "context-dependent"
framing under which glibc's own failure to enforce a wordexp flag was
treated as a security issue (CVE-2014-7817). sway's config exposure is
weaker (its config has an exec directive, so config write access is
already equivalent to execution; the delta is include-chains/kiosk
setups).

Evidence
--------

Affected versions: the flag gating has been present since wordexp() was
added to musl, and no commit in its history moves the checks out of the
NOCMD gate; verified on musl 1.2.5 (Alpine 3.22 stock package) and on
master tip 7813e911 (unfixed as of 2026-11-29).

Real static musl @ 7813e911, no sanitizer:

    musl : wordexp("a; : > /tmp/we_nocmd_marker", &we, 0)  -> rc=0, file
created
    glibc: same call                                        -> rc=2
(WRDE_BADCHAR), no file
    musl : wordexp("$(echo hi)", &we, WRDE_NOCMD)           ->
rc=WRDE_CMDSUB, blocked (scan works)

End-to-end against stock distro binaries (dual-channel, logs kept):

    input: 36-char pseudo-UUID "zzzzzzzz;touch /tmp/fsa_m29_real;zzz"
           -> unquoted " -U %s " in the mkfs command (archive metadata
field)

    musl : stock Alpine fsarchiver 0.8.8-r0 (musl 1.2.5), root, restfs
           -> PAYLOAD EXECUTED inside the wordexp() call (marker present,
              two independent runs); mke2fs then failed on mangled args
    glibc: stock Ubuntu fsarchiver 0.8.7 (glibc 2.39), root, same archive
           -> wordexp() returns WRDE_BADCHAR; return ignored by fsarchiver
           -> argv[0]=(null), command not executed, restore fails safely
           -> no marker, no crash (the real binary fails gracefully)

An earlier verbatim-transplant demo of fsarchiver's call site (0.8.9-3)
showed a glibc-side consumer-loop SIGSEGV; that crash is specific to the
transplant's stack layout and does not reproduce on the real 0.8.7
binary, which fails safely -- noted for accuracy; the portability delta
(glibc refuses, musl executes) is unchanged and is the claim.

WRDE_UNDEF: wordexp("$UNDEF", &we, WRDE_UNDEF) returns 0 with zero
words on musl; glibc returns WRDE_BADVAL. A caller expanding "$DIR/*"
with DIR unset gets "/*" silently.

Suggested fix
-------------

Move the BADCHAR cases out of the NOCMD gate (keep the CMDSUB checks
under it -- they are correct as-is), i.e. make the pre-scan unconditional
for the metacharacter set; and implement WRDE_UNDEF either via a
subshell with "set -u" semantics or an environ-based pre-scan of
$NAME/${NAME} references. This matches glibc's observable behavior and
the POSIX text, and removes the portability trap without changing any
documented musl design decision (the sh-delegation stays).

Minimal reproducer (same logic as the probes I ran; expected output in
comments):

--- wordexp-min.c ---
/* build (musl): musl-clang -static wordexp-min.c -o wordexp-min
 * build (glibc): gcc wordexp-min.c -o wordexp-min-glibc
 */
#include <stdio.h>
#include <wordexp.h>

static void t(const char *s, int flags)
{
    wordexp_t we = {0};
    int rc = wordexp(s, &we, flags);
    printf("flags=%-11d rc=%-2d words=%zu  input: %s\n",
           flags, rc, rc ? (size_t)0 : we.we_wordc, s);
    if (!rc) wordfree(&we);
}

static void side_effect(int flags, const char *tag)
{
    const char *mark = "/tmp/wordexp_marker";
    wordexp_t we = {0};
    int rc = wordexp("a; : > /tmp/wordexp_marker", &we, flags);
    FILE *f = fopen(mark, "r");
    printf("%-24s rc=%-2d marker_exists=%s\n", tag, rc, f ? "YES" : "NO");
    if (f) fclose(f);
    if (!rc) wordfree(&we);
    remove(mark);
}

int main(void)
{
    /* 1. BADCHAR gate: unquoted ';' must be rejected unconditionally
     *    (POSIX.1-2017 XSH wordexp()), but on musl the check only runs
     *    under WRDE_NOCMD: */
    side_effect(0,          "flags=0");          /* musl: rc=0, marker YES
                                                    glibc: rc=WRDE_BADCHAR,
NO */
    side_effect(WRDE_NOCMD, "flags=NOCMD");      /* both: WRDE_BADCHAR, NO
    */

    /* 2. NOCMD itself works on musl (not the issue reported here): */
    t("$(echo hi)",     WRDE_NOCMD);            /* musl: WRDE_CMDSUB,
blocked */
    t("`echo hi`",      WRDE_NOCMD);            /* musl: WRDE_CMDSUB,
blocked */
    t("$((`echo hi`))", WRDE_NOCMD);            /* musl: WRDE_CMDSUB,
blocked */

    /* 3. WRDE_UNDEF unimplemented: */
    t("$UNDEF", WRDE_UNDEF);                    /* musl: rc=0, 0 words
                                                    glibc: WRDE_BADVAL
    */
    return 0;
}
--- end wordexp-min.c ---

The fuller probes (83-case grid incl. DOFFS/APPEND/REUSE shapes) and the
fsarchiver archive-patch workflow (a small Python script plus
dual-channel logs) are available on request -- happy to attach or link
them.

Best regards,
Convre

Content of type "text/html" skipped

Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.