fix(libc): keep guest environment strings alive across setenv and uns… #12054
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/12054"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by Aspct3434 on 2026-10-27T17:48:46Z.
…etenv
setenv replaced a variable's string with a new allocation and freed the old one, and unsetenv freed it as well, so a pointer that getenv had returned for that variable dangled. A guest that reads such a pointer after setenv(name, value, 1) reads freed host memory: AddressSanitizer reports a heap-use-after-free on that read.
FreeBSD 11.4's getenv.c, which holds the setenv, unsetenv and putenv the console's libc exports next to getenv, never frees a variable's string ("No previous variables are ever freed here to avoid causing a segmentation fault in a user's code", lib/libc/stdlib/getenv.c lines 449-520 at 8c639945a4). setenv and unsetenv mark the old string inactive; setenv writes the new value over the latest string of that name when the value fits and allocates a new string otherwise; putenv strings stay the caller's and are only forgotten.
The guest environment now keeps every string it allocates with its value capacity and an active flag and follows that code: getenv and the time zone lookup read the name's latest active string, setenv reuses the latest string of the name when it fits, an unset one included, and otherwise appends a new one and deactivates the old, putenv replaces an earlier putenv string or deactivates the setenv one, and unsetenv deactivates them all. Built on the host next to this library, FreeBSD's getenv.c gave the same results, errno, pointer reuse and contents of every earlier pointer over 160,000 random calls once the putenv flag it leaves uninitialized in new setenv strings was cleared; the debt entry records that the console may allocate where this reuses.
guest_environment checks the reuse, that earlier values stay readable after a longer setenv and after unsetenv, and that putenv and setenv leave each other's strings alone.
What
Keeps guest environment strings alive across
setenvandunsetenvas described above, following FreeBSD 11.4getenv.c. Debt entry for the libc environment updated.Tested
ctest -R '^guest_environment$' --repeat until-fail:10passes; withEnvironment.cpprestored from main and the PR's test kept it aborts.guest_environmentpasses.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)Claude-Session: https://claude.ai/code/session_01DXPnhogDW4zpeNCBro4Ctt
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-27T18:36:18Z.
Review: CHANGES_REQUESTED
Reviewed exact head
dbce646e7bbdcbd8fbb3f4e35c5921c346f5bdab. The retained environment strings appear to address the reported pointer lifetime behavior, and the new tests cover shortening, growth, unset/re-add, andputenvownership. I did not independently run the runtime tests in this pass.\n\nThis is currently blocked by the repository contribution template: theWhatandTestedsections are blank, most checklist items remain unchecked, andDepends onis unanswered. The conventions check confirms 16 template errors, including missingAI-assisted: yes/noand requiredDepends on: #N or none. Please complete the PR description and rerun conventions. The overlap bot also could not compare several shared libc/debt files because older PRs conflict with base; manually confirm any applicable overlap and dependency ordering before merge.