Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-ID: <20261119135600.GY5716@brightrain.aerifal.cx>
Date: Thu, 19 Nov 2026 08:56:00 -0500
From: Rich Felker <dalias@...c.org>
To: zhaotq@....edu.cn
Cc: musl@...ts.openwall.com
Subject: Re: musl libc 1.2.5: putenv() self-overwrite leaves a
 dangling environ entry

On Thu, Nov 19, 2026 at 01:21:01PM +0800, zhaotq@....edu.cn wrote:
> Hello,
> 
> We are developing a software verification tool. During an audit of musl libc
> 1.2.5 we found a heap use-after-free in the environment implementation. The
> local test used musl 1.2.5 and the same source is still present in current
> musl master at the time of writing.
> 
> The issue occurs when an application obtains a name=value entry allocated by
> setenv(), and passes that same pointer back to putenv():
> 
>     setenv("MUSL_PUTENV_SELF_UAF", "initial-value", 1);
>     entry = find_entry_in_environ("MUSL_PUTENV_SELF_UAF");
>     putenv(entry);
> 
> musl's __putenv() replaces the pointer in environ, then passes the old pointer
> to __env_rm_add(). For this self-overwrite, the old and new pointers are the
> same, but __env_rm_add() still frees the allocation. environ continues to point
> at the freed buffer, and a later getenv() reads freed heap memory.
> 
> This is a memory-safety bug, and it also conflicts with POSIX.1-2024 XSH
> putenv(): its DESCRIPTION requires that, on success, the string passed to
> putenv() shall become part of the environment, and its RETURN VALUE section
> requires successful completion to return 0. A successful call cannot leave
> environ holding invalid storage that getenv() then consumes.

Part of the contract of putenv is that the string passed becomes part
of the environment; this implies a requirement to ensure that its
lifetime persists as long as it's in the environment.

If the string was created by setenv and read from environ[], its
lifetime ends when putenv is called to replace it. This means the
program has not satisfied the requirement and has UB.

Rich

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.