fix(libc): keep guest environment strings alive across setenv and uns… #12054

Merged
Aspct3434 merged 1 commit from upstream-pr/12054 into main 2026-10-29 10:08:38 +00:00
Aspct3434 commented 2026-10-27 17:48:46 +00:00 (Migrated from github.com)

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 setenv and unsetenv as described above, following FreeBSD 11.4 getenv.c. Debt entry for the libc environment updated.

Tested

  • Linux (Ubuntu 22.04, WSL2, GCC 11.4, Release, @B-Howell): ctest -R '^guest_environment$' --repeat until-fail:10 passes; with Environment.cpp restored from main and the PR's test kept it aborts.
  • Windows 11 (WinLibs GCC 15.2, Release, @maskym): guest_environment passes.
  • Not tested on a PS5 or with a title.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

Claude-Session: https://claude.ai/code/session_01DXPnhogDW4zpeNCBro4Ctt

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5038583684 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12054). Originally posted by [Aspct3434](https://github.com/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 `setenv` and `unsetenv` as described above, following FreeBSD 11.4 `getenv.c`. Debt entry for the libc environment updated. ### Tested - Linux (Ubuntu 22.04, WSL2, GCC 11.4, Release, @B-Howell): `ctest -R '^guest_environment$' --repeat until-fail:10` passes; with `Environment.cpp` restored from main and the PR's test kept it aborts. - Windows 11 (WinLibs GCC 15.2, Release, @maskym): `guest_environment` passes. - Not tested on a PS5 or with a title. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes Claude-Session: https://claude.ai/code/session_01DXPnhogDW4zpeNCBro4Ctt
MarcZX10 commented 2026-10-27 18:36:18 +00:00 (Migrated from github.com)

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, and putenv ownership. I did not independently run the runtime tests in this pass.\n\nThis is currently blocked by the repository contribution template: the What and Tested sections are blank, most checklist items remain unchecked, and Depends on is unanswered. The conventions check confirms 16 template errors, including missing AI-assisted: yes/no and required Depends 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.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5672426113 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12054#pullrequestreview-5672426113). Originally posted by [MarcZX10](https://github.com/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, and `putenv` ownership. I did not independently run the runtime tests in this pass.\n\nThis is currently blocked by the repository contribution template: the `What` and `Tested` sections are blank, most checklist items remain unchecked, and `Depends on` is unanswered. The conventions check confirms 16 template errors, including missing `AI-assisted: yes/no` and required `Depends 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.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!12054
No description provided.