fix(libSceLibcInternal): take a getopt_long argument only inside argc #15126

Merged
MarcZX10 merged 3 commits from upstream-pr/15126 into main 2026-11-10 21:13:23 +00:00
MarcZX10 commented 2026-11-02 17:43:50 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-02T17:43:50Z.


What

Three libSceLibcInternal fixes:

  • inet_aton accepted a single number above 0xffffffff and wrapped the 64-bit accumulation. It now rejects it.
  • getopt_long read arguments[optind] for a required argument without checking argc. It now takes the argument only inside argc.
  • _Lstat followed symbolic links, so it reported the target's type. It now reports the link itself.

Commits:

  • 135aa6c65a fix(libSceLibcInternal): reject inet_aton numbers above 32 bits
  • a7dc079e3b test(libSceLibcInternal): cover inet_aton single numbers above 32 bits
  • 2b21473909 fix(libSceLibcInternal): getopt_long takes a required argument only inside argc
  • 8912f19113 test(libSceLibcInternal): cover getopt_long required argument beyond argc
  • 95d524c277 fix(libSceLibcInternal): _Lstat reports a symbolic link itself
  • 10fed0815a test(libSceLibcInternal): cover _Lstat on symbolic links

Tested

  • Host: Linux, archive build, lane 3, on the final head.
  • ctest -R '^(guest_getopt|guest_inet_address|guest_lstat_link)$': 3 of 3 passed.
  • Negative control: with all three fixes reverted in a verify copy, the matching cases fail (single number above 32 bits, required argument beyond argc, _Lstat reports a file link as a symbolic link).
  • tools/check_conventions.py --base upstream/main and git diff --check upstream/main: clean.

Not verified

  • The same single-number bug may exist in __inet_aton in StringExtensions.cpp; it is out of scope here.
  • Windows paths in Spawn.cpp and PathStatistics.cpp were not compiled.
  • Possible textual conflict with #12924 in Inet.cpp; the functions it adds are different.

Checklist

  • Based on current main; no open PR implements these three fixes
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5124762127 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/15126). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-02T17:43:50Z. --- ### What Three libSceLibcInternal fixes: - `inet_aton` accepted a single number above `0xffffffff` and wrapped the 64-bit accumulation. It now rejects it. - `getopt_long` read `arguments[optind]` for a required argument without checking `argc`. It now takes the argument only inside `argc`. - `_Lstat` followed symbolic links, so it reported the target's type. It now reports the link itself. Commits: - 135aa6c65a fix(libSceLibcInternal): reject inet_aton numbers above 32 bits - a7dc079e3b test(libSceLibcInternal): cover inet_aton single numbers above 32 bits - 2b21473909 fix(libSceLibcInternal): getopt_long takes a required argument only inside argc - 8912f19113 test(libSceLibcInternal): cover getopt_long required argument beyond argc - 95d524c277 fix(libSceLibcInternal): _Lstat reports a symbolic link itself - 10fed0815a test(libSceLibcInternal): cover _Lstat on symbolic links ### Tested - Host: Linux, archive build, lane 3, on the final head. - `ctest -R '^(guest_getopt|guest_inet_address|guest_lstat_link)$'`: 3 of 3 passed. - Negative control: with all three fixes reverted in a verify copy, the matching cases fail (`single number above 32 bits`, `required argument beyond argc`, `_Lstat reports a file link as a symbolic link`). - `tools/check_conventions.py --base upstream/main` and `git diff --check upstream/main`: clean. ### Not verified - The same single-number bug may exist in `__inet_aton` in `StringExtensions.cpp`; it is out of scope here. - Windows paths in `Spawn.cpp` and `PathStatistics.cpp` were not compiled. - Possible textual conflict with #12924 in `Inet.cpp`; the functions it adds are different. ### Checklist - [x] Based on current `main`; no open PR implements these three fixes - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except technical debt - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
BunnyFl commented 2026-11-02 19:07:11 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by BunnyFl on 2026-11-02T19:07:11Z.

Review: DISMISSED


Two of the three fixes move away from the console, and the third is unclear, so I'd hold all three. Firmware 13.42's system/common/lib/libSceLibcInternal.sprx (sha256 a01795ab49df639dd40abd6194956ada062244826824ce486c9acab227bf5285), read from disassembly only, not measured on a console:

_Lstat follows links on the console. _ZNSt10filesystem6_LstatEPKcPNS_5permsEPi (tizCFt051h8, 0x902d0) and _ZNSt10filesystem5_StatEPKcPNS_5permsEPi (8KeeeFcEdls, 0x901d0) are both 0xe9 bytes. Apart from position-relative offsets their instructions are identical, and they make the same calls in the same order (0x124330, 0x124950, 0x124330 five times, 0x124310). So in 13.42 _Lstat gives the same result as _Stat for any path, which is what main does now by sharing StatType. Reporting the link itself would differ from the console.

inet_aton doesn't range-check a single number. At ISTLytNGT0c (0x8ae20), the parts switch checks the last part only for 2, 3 and 4 parts:

8b0ac: cmp $0x1,%edx ; jne 0x8b125        one dot   -> 8b0b8: cmp $0xffffff,%r11 ; ja (return 0)
8b0a7: cmp $0x2,%edx ; je 0x8b0ca         two dots  -> 8b0d1: cmp $0xffff,%r11 ; ja (return 0)
8b09e: cmp $0x3,%edx ; je 0x8b0ef         three dots -> 8b0fa: cmp $0xff,%r11 ; ja (return 0)
8b125: (no dots, no check)
8b12c: mov $0x1,%eax
8b13a: movbe %r11d,(%rsi)                  low 32 bits of the 64-bit value

So "4294967296" returns 1 and stores 0.0.0.0. That matches FreeBSD 11.4's inet_aton: u_long val, and case 1: has no check. The new Rejects("4294967296") and Rejects("0x100000000") rows pin a result neither gives. I didn't check the digit loop for an overflow test, so I can't say what a value past 64 bits does.

getopt_long with the argument at argv[argc]. FreeBSD 11.4's parse_long_options takes optarg = nargv[optind++] without comparing against nargc, then reports a missing argument only if that is NULL and steps optind back. With a NULL-terminated argv the two agree, but for the new case (getopt_long(2, …) over "foo --required bar"), FreeBSD takes "bar" as the argument rather than returning '?'. I haven't traced the 13.42 getopt_long (8VVXJxB5nlk, 0x8bc00) far enough to say which the console does, so this one needs evidence either way. If the intent is a host-safety bound (an argv that isn't NULL-terminated), it should be listed as a divergence in the debt file.

Tested on Windows 11, WinLibs GCC 15.2.0, with this head merged onto main: guest_getopt and guest_inet_address pass. With main's Inet.cpp, Getopt.cpp and Filesystem.cpp, they fail on the new rows. guest_lstat_link aborts at "create file link" here, because this account can't create symlinks, so it aborts instead of skipping where links are unavailable.

The three changes are also unrelated (network parsing, option parsing, filesystem). The template asks for one topic per PR, so splitting them would let the parts that hold up go in on their own.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5741880472 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/15126#pullrequestreview-5741880472). Originally posted by [BunnyFl](https://github.com/BunnyFl) on 2026-11-02T19:07:11Z. **Review:** DISMISSED --- Two of the three fixes move away from the console, and the third is unclear, so I'd hold all three. Firmware 13.42's `system/common/lib/libSceLibcInternal.sprx` (sha256 `a01795ab49df639dd40abd6194956ada062244826824ce486c9acab227bf5285`), read from disassembly only, not measured on a console: **`_Lstat` follows links on the console.** `_ZNSt10filesystem6_LstatEPKcPNS_5permsEPi` (`tizCFt051h8`, `0x902d0`) and `_ZNSt10filesystem5_StatEPKcPNS_5permsEPi` (`8KeeeFcEdls`, `0x901d0`) are both 0xe9 bytes. Apart from position-relative offsets their instructions are identical, and they make the same calls in the same order (`0x124330`, `0x124950`, `0x124330` five times, `0x124310`). So in 13.42 `_Lstat` gives the same result as `_Stat` for any path, which is what `main` does now by sharing `StatType`. Reporting the link itself would differ from the console. **`inet_aton` doesn't range-check a single number.** At `ISTLytNGT0c` (`0x8ae20`), the parts switch checks the last part only for 2, 3 and 4 parts: ``` 8b0ac: cmp $0x1,%edx ; jne 0x8b125 one dot -> 8b0b8: cmp $0xffffff,%r11 ; ja (return 0) 8b0a7: cmp $0x2,%edx ; je 0x8b0ca two dots -> 8b0d1: cmp $0xffff,%r11 ; ja (return 0) 8b09e: cmp $0x3,%edx ; je 0x8b0ef three dots -> 8b0fa: cmp $0xff,%r11 ; ja (return 0) 8b125: (no dots, no check) 8b12c: mov $0x1,%eax 8b13a: movbe %r11d,(%rsi) low 32 bits of the 64-bit value ``` So `"4294967296"` returns 1 and stores 0.0.0.0. That matches FreeBSD 11.4's [`inet_aton`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/lib/libc/inet/inet_addr.c#L176-L200): `u_long val`, and `case 1:` has no check. The new `Rejects("4294967296")` and `Rejects("0x100000000")` rows pin a result neither gives. I didn't check the digit loop for an overflow test, so I can't say what a value past 64 bits does. **`getopt_long` with the argument at `argv[argc]`.** FreeBSD 11.4's [`parse_long_options`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/lib/libc/stdlib/getopt_long.c#L296-L320) takes `optarg = nargv[optind++]` without comparing against `nargc`, then reports a missing argument only if that is NULL and steps `optind` back. With a NULL-terminated `argv` the two agree, but for the new case (`getopt_long(2, …)` over `"foo --required bar"`), FreeBSD takes `"bar"` as the argument rather than returning `'?'`. I haven't traced the 13.42 `getopt_long` (`8VVXJxB5nlk`, `0x8bc00`) far enough to say which the console does, so this one needs evidence either way. If the intent is a host-safety bound (an `argv` that isn't NULL-terminated), it should be listed as a divergence in the debt file. Tested on Windows 11, WinLibs GCC 15.2.0, with this head merged onto `main`: `guest_getopt` and `guest_inet_address` pass. With `main`'s `Inet.cpp`, `Getopt.cpp` and `Filesystem.cpp`, they fail on the new rows. `guest_lstat_link` aborts at "create file link" here, because this account can't create symlinks, so it aborts instead of skipping where links are unavailable. The three changes are also unrelated (network parsing, option parsing, filesystem). The template asks for one topic per PR, so splitting them would let the parts that hold up go in on their own.
adriadam10 commented 2026-11-06 23:36:01 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-11-06T23:36:01Z.

Review: CHANGES_REQUESTED


Thanks for the fixes. Going through the existing review points against the head, none of the three is shown to match the console, so I can't merge them as they are:

  • inet_aton (Inet.cpp:77, :82): FreeBSD 11.4's case 1 has no range check, and the 13.42 libSceLibcInternal.sprx disassembly @BunnyFl posted (ISTLytNGT0c, 0x8ae20) checks only the 2, 3 and 4 part forms, then stores the low 32 bits. So "4294967296" parses as 0.0.0.0 on the console, and the new Rejects(...) rows in GuestInetAddress.cpp:48 pin a result it doesn't give. Please drop these two commits, or send console evidence for the rejection.
  • _Lstat (Filesystem.cpp:6-21): in the same disassembly _Lstat (tizCFt051h8) and _Stat (8KeeeFcEdls) are identical apart from relative offsets, so the console follows links in both, which is what main does. Please drop the change and GuestLstatLink.cpp, or show the console reports the link itself.
  • getopt_long (Getopt.cpp:165): FreeBSD's parse_long_options takes nargv[optind++] with no nargc comparison. If this is a host-safety bound for a non-NULL-terminated argv, say so and list it as a divergence in docs/dev/debt/libSceLibcInternal.md; otherwise show what the console does at argv[argc] (8VVXJxB5nlk, 0x8bc00). The removed --optind on the missing-argument path also needs the same evidence.
  • GuestLstatLink.cpp:31: it aborts when the account can't create symbolic links (Windows without the privilege). It should skip with the exit code the CI skip rules accept.
  • Three unrelated topics (network parsing, option parsing, filesystem): one topic per PR, so please split whichever parts you keep.
  • The windows job is red on this head; please check its log.

Feel free to push the parts that come with console evidence.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5808476800 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/15126#pullrequestreview-5808476800). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-11-06T23:36:01Z. **Review:** CHANGES_REQUESTED --- Thanks for the fixes. Going through the existing review points against the head, none of the three is shown to match the console, so I can't merge them as they are: - `inet_aton` (`Inet.cpp:77`, `:82`): FreeBSD 11.4's `case 1` has no range check, and the 13.42 `libSceLibcInternal.sprx` disassembly @BunnyFl posted (`ISTLytNGT0c`, `0x8ae20`) checks only the 2, 3 and 4 part forms, then stores the low 32 bits. So `"4294967296"` parses as `0.0.0.0` on the console, and the new `Rejects(...)` rows in `GuestInetAddress.cpp:48` pin a result it doesn't give. Please drop these two commits, or send console evidence for the rejection. - `_Lstat` (`Filesystem.cpp:6-21`): in the same disassembly `_Lstat` (`tizCFt051h8`) and `_Stat` (`8KeeeFcEdls`) are identical apart from relative offsets, so the console follows links in both, which is what `main` does. Please drop the change and `GuestLstatLink.cpp`, or show the console reports the link itself. - `getopt_long` (`Getopt.cpp:165`): FreeBSD's `parse_long_options` takes `nargv[optind++]` with no `nargc` comparison. If this is a host-safety bound for a non-NULL-terminated `argv`, say so and list it as a divergence in `docs/dev/debt/libSceLibcInternal.md`; otherwise show what the console does at `argv[argc]` (`8VVXJxB5nlk`, `0x8bc00`). The removed `--optind` on the missing-argument path also needs the same evidence. - `GuestLstatLink.cpp:31`: it aborts when the account can't create symbolic links (Windows without the privilege). It should skip with the exit code the CI skip rules accept. - Three unrelated topics (network parsing, option parsing, filesystem): one topic per PR, so please split whichever parts you keep. - The `windows` job is red on this head; please check its log. Feel free to push the parts that come with console evidence.
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!15126
No description provided.