Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Message-ID: <79987d13.2a60a.1a1f24c3d17.Coremail.zhaotq@pku.edu.cn>
Date: Thu, 19 Nov 2026 13:34:05 +0800 (GMT+08:00)
From: zhaotq@....edu.cn
To: musl@...ts.openwall.com
Subject: musl libc 1.2.5: fmemopen(NULL, 0, "w+") writes past a
 zero-capacity buffer

Hello,

We found a deterministic one-byte heap out-of-bounds write in musl 1.2.5:

    fmemopen(NULL, 0, "w+");

When buf is null and max_size is zero, musl allocates a memory stream object
with no bytes available in its dynamic data area. It then executes the
following write for every mode containing '+':

    else if (plus) *f->c.buf = 0;

That writes one byte at the start of the zero-length data area, immediately
past the allocation. With an ordinary allocator the write may silently
corrupt adjacent memory; a guarded-page allocator turns it into a
deterministic SIGSEGV.

The relevant POSIX.1-2024 XSH fmemopen() rule is that a null buf causes
max_size bytes to be allocated, and the stream must not access outside that
storage. In particular, the null-byte truncation required by POSIX is
conditioned on a nonzero buffer size; a zero-capacity stream has no byte
available for the write. The current musl implementation accepts max_size zero
but still performs the write.

The guarded PoC replaces malloc() so the stream object ends immediately
before a PROT_NONE page. The fault address falls in that page, confirming
the out-of-bounds write at the public API boundary. The key implementation is:

    static int arm_guard;

    void *malloc(size_t n)
    {
        if (arm_guard) {
            unsigned char *pages =
                mmap(0, 8192, PROT_READ|PROT_WRITE,
                     MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
            if (pages == MAP_FAILED) return 0;
            mprotect(pages + 4096, 4096, PROT_NONE);
            return pages + 4096 - n;
        }
        return 0;
    }

    int main(void)
    {
        arm_guard = 1;
        fmemopen(0, 0, "w+");
        return 0;
    }

Reproduction:

    tar xzf 05-fmemopen-zero-wplus.tar.gz
    ./prepare.sh
    cd musl-1.2.5
    ./poc/stdio/run-fmemopen-zero-wplus.sh

Expected marker: FMEMOPEN_ZERO_WPLUS=SEGV.

`prepare.sh` downloads the official musl 1.2.5 source from
https://musl.libc.org/releases/musl-1.2.5.tar.gz and verifies this SHA-256:
a9a118bbe84d8764da0ea0d28b3ab3fae8477fc7e4085d90102b8596fc7c75e4.

Attached are the PoC archive and README.md, which documents the
environment requirements, reproduction commands, and observed results.

Archive SHA-256:
b39503d2ed0abad2ff180be9fbdeefcf4d7fb0243325a4b82814533aab0ec6e2

We would appreciate your assessment of the security impact and whether
a CVE is warranted. 

Thanks,
Tianqi

Download attachment "05-fmemopen-zero-wplus.tar.gz" of type "application/x-gzip" (3003 bytes)

Download attachment "README.md" of type "application/octet-stream" (1936 bytes)

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.