Repository navigation
Conversation
…measured on the console Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…tive length from overflowing Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
|
Probe source (built with ps5-payload-sdk v0.45; probe.c/* devname and devname_r, payload process, firmware 11.60 (debt libc.md devname and devname_r entry, NIDs fqLrWSWcGHw and
BIALMFTZ75I: AnyPS5 never queries the kern.devname sysctl, so a character device always gets the formatted #C:<number> name;
neither function was measured on a PS5; otherwise they follow FreeBSD 11.4 devname.c with a 64-byte static buffer: NODEV
gives #NODEV, the number prints as %#jx does, a short buffer truncates as strlcpy does). The probe looks devname and
devname_r up with dlsym in libSceLibcInternal.sprx (and reports where). It reads st_rdev with stat of /dev/null, /dev/zero,
/dev/console and /dev/random (stat only, nothing is opened) and calls devname_r(rdev, S_IFCHR, buf, 64) for each (the text
is printed: it is a device name, never a person's data), then NODEV (0xffffffff) with S_IFCHR, 0x1234 with S_IFCHR and
S_IFBLK, devname_r with the buffer lengths 1, 2, 5 and 0 and -1 on a 16-byte buffer followed by a 0x5a guard area, and devname
twice (is the returned pointer the same static buffer). Nothing is written, nothing is sent. */
#include <dlfcn.h>
#include <errno.h>
#include <pthread.h>
#include <signal.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <ps5/kernel.h> /* only for kernel_get_fw_version() */
#ifndef PROBE_NAME
#define PROBE_NAME "unnamed"
#endif
#ifndef PROBE_BUILD
#define PROBE_BUILD "unbuilt"
#endif
/* Self limit: the runner can only close the connection, it cannot end the process on the console, so the probe ends
itself after PROBE_MAX_S seconds at the latest (set by the build script). */
#ifndef PROBE_MAX_S
#define PROBE_MAX_S 50
#endif
static volatile int g_handle = -1;
static int (*volatile g_setmargins)(int, int, int) = 0; /* unused here */
static void reset_rate(void) {
if (g_setmargins && g_handle >= 0) g_setmargins(g_handle, 0, 0);
}
static void probe_alarm(int sig) {
static const char msg[] = "TIMEOUT\tprobe self limit\n";
(void)sig;
(void)!write(1, msg, sizeof msg - 1);
reset_rate();
_exit(124);
}
/* Watchdog thread: alarm()/SIGALRM does not help when the main thread hangs in a blocking system call or IPC. The
thread sleeps PROBE_MAX_S seconds, writes TIMEOUT with write() and ends the process with _exit(124). alarm() stays
as a second safeguard. */
static void* probe_watchdog(void* arg) {
static const char msg[] = "TIMEOUT\tprobe watchdog thread\n";
unsigned left = PROBE_MAX_S;
(void)arg;
while (left) left = sleep(left);
(void)!write(1, msg, sizeof msg - 1);
reset_rate();
_exit(124);
return 0;
}
static void probe_watchdog_start(void) {
pthread_t t;
if (pthread_create(&t, 0, probe_watchdog, 0) == 0) pthread_detach(t);
}
static int cases = 0;
static void probe_header(void) {
char utc[32];
time_t now = time(0);
probe_watchdog_start();
struct tm tmv;
gmtime_r(&now, &tmv);
strftime(utc, sizeof utc, "%Y-%m-%dT%H:%M:%SZ", &tmv);
printf("PROBE\t%s\tfw=0x%08x\tbuild=%s\tpid=%d\tutc=%s\n", PROBE_NAME, kernel_get_fw_version(), PROBE_BUILD,
getpid(), utc);
fflush(stdout);
}
static void probe_case(const char* id, const char* call, long ret, int err, const char* extra) {
printf("CASE\t%s\t%s\t%ld\t%d\t%s\n", id, call, ret, err, extra ? extra : "-");
fflush(stdout);
cases++;
}
static void probe_end(void) {
printf("END\t%d\n", cases);
fflush(stdout);
}
#include <sys/stat.h>
#include <sys/types.h>
typedef char* (*fn_dr)(unsigned, unsigned, char*, int);
typedef char* (*fn_d)(unsigned, unsigned);
static void fault(int sig) {
static const char msg[] = "FAULT\tsignal in the call\n";
(void)sig;
(void)!write(1, msg, sizeof msg - 1);
_exit(125);
}
static void dn_case(const char* id, const char* label, fn_dr f, unsigned dev, unsigned type, int len) {
unsigned char buf[16 + 16];
memset(buf, 0x5a, sizeof buf);
memset(buf, 0xaa, 16);
char extra[200];
errno = 0;
char* r = f(dev, type, (char*)buf, len);
int e = errno, guard = 0;
for (int i = 16; i < 32; ++i) if (buf[i] != 0x5a) guard++;
char text[40] = "";
if (r) { size_t n = 0; while (n < 20 && r[n]) { text[n] = (r[n] >= 32 && r[n] < 127) ? r[n] : '?'; ++n; } text[n] = 0; }
snprintf(extra, sizeof extra, "returned_null=%d same_as_buf=%d text=%s guard_changed=%d", r == 0, r == (char*)buf, text, guard);
probe_case(id, label, r != 0, e, extra);
}
int main(void) {
signal(SIGALRM, probe_alarm);
signal(SIGSEGV, fault);
signal(SIGBUS, fault);
alarm(PROBE_MAX_S);
probe_header();
char extra[200];
errno = 0;
void* lc = dlopen("libSceLibcInternal.sprx", RTLD_LAZY);
probe_case("0", "load libSceLibcInternal.sprx", lc ? 0 : -1, errno, lc ? "loaded" : dlerror());
if (!lc) { probe_end(); return 0; }
fn_dr dr = (fn_dr)dlsym(lc, "devname_r");
fn_d d = (fn_d)dlsym(lc, "devname");
snprintf(extra, sizeof extra, "devname_r=%d devname=%d", dr != 0, d != 0);
probe_case("0b", "dlsym of the functions", 0, 0, extra);
if (!dr) { probe_end(); return 0; }
static const char* const paths[] = {"/dev/null", "/dev/zero", "/dev/console", "/dev/random"};
for (int i = 0; i < 4; ++i) {
struct stat st;
char id[16], label[64];
snprintf(id, sizeof id, "1.%d", i + 1);
if (stat(paths[i], &st) != 0) {
snprintf(extra, sizeof extra, "stat failed errno=%d", errno);
probe_case(id, paths[i], -1, errno, extra);
continue;
}
snprintf(label, sizeof label, "devname_r(rdev of %s, S_IFCHR, 64)", paths[i]);
unsigned char big[64 + 16];
memset(big, 0xaa, sizeof big);
char* r = dr((unsigned)st.st_rdev, S_IFCHR, (char*)big, 64);
char text[48] = "";
if (r) { size_t n = 0; while (n < 40 && r[n]) { text[n] = (r[n] >= 32 && r[n] < 127) ? r[n] : '?'; ++n; } text[n] = 0; }
snprintf(extra, sizeof extra, "returned_null=%d text=%s", r == 0, text);
probe_case(id, label, r != 0, 0, extra);
}
dn_case("2.1", "devname_r(NODEV 0xffffffff, S_IFCHR, 16)", dr, 0xffffffffu, S_IFCHR, 16);
dn_case("2.2", "devname_r(0x1234, S_IFCHR, 16)", dr, 0x1234, S_IFCHR, 16);
dn_case("2.3", "devname_r(0x1234, S_IFBLK, 16)", dr, 0x1234, S_IFBLK, 16);
dn_case("3.1", "devname_r(0x1234, S_IFCHR, 1)", dr, 0x1234, S_IFCHR, 1);
dn_case("3.2", "devname_r(0x1234, S_IFCHR, 2)", dr, 0x1234, S_IFCHR, 2);
dn_case("3.3", "devname_r(0x1234, S_IFCHR, 5)", dr, 0x1234, S_IFCHR, 5);
dn_case("3.4", "devname_r(0x1234, S_IFCHR, 0)", dr, 0x1234, S_IFCHR, 0);
dn_case("3.5", "devname_r(0x1234, S_IFCHR, -1)", dr, 0x1234, S_IFCHR, -1);
if (d) {
char* p1 = d(0x1234, S_IFCHR);
char* p2 = d(0x1235, S_IFCHR);
snprintf(extra, sizeof extra, "first_null=%d second_null=%d same_pointer=%d", p1 == 0, p2 == 0, p1 == p2);
probe_case("4", "devname(0x1234) and devname(0x1235): same static buffer", p1 != 0, 0, extra);
}
probe_end();
return 0;
}stdoutAI-assisted (Claude Code). |
PrivacyOnion
left a comment
There was a problem hiding this comment.
Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against af7de378cf:
- Conventions:
tools/check_conventions.pypassed. - Build:
guest_device_name_testsbuilt cleanly. - CTest (
guest_device_name):1/1 Test #1225: guest_device_name ................ Passed 0.00 sec 100% tests passed, 0 tests failed out of 1 - Negative control: with
core/libs/prx/libc/src/DeviceName.cppreverted tomainand the new tests kept, the suite fails:Restoring the change makes it pass again.1/1 Test #1225: guest_device_name ................Subprocess aborted***Exception: 0.12 sec 0% tests passed, 1 tests failed out of 1 - Code reading:
- Checked DeviceName.cpp devname_r_nid_postfix buffer size casting and negative length handling (length < 0 casting to std::size_t results in large unsigned value)
- Checked DeviceName.cpp CopyString bounds checking, count calculation std::min(length, size - 1), and std::copy_n with GuestCopyOut_nid_no_patch
- Checked DeviceName.cpp character vs block type check condition (device == NoDevice || !(character || (device & TypeMask) == BlockType)) and comparison against type parameter vs device parameter
- Checked DeviceName.cpp format string '#%c:%d:0x%x' and calculation of major = (device >> 8) & 0xFF and minor = device & 0xFFFF00FFu against probe results and FreeBSD macro conventions
- Checked errno setting logic for character and block devices against probe measurements and debt fragment docs/dev/debt/changes/fix-libc-devname-console-format.md
- Checked devname_nid_postfix static buffer sizing and reentrancy / thread safety behavior matching probe tests
- Concerns:
- core/libs/prx/libc/src/DeviceName.cpp:28: In devname_r_nid_postfix, the block type check evaluates
!(character || (device & TypeMask) == BlockType), maskingdevicewithTypeMaskinstead of maskingtypewithTypeMask. Becausetypeholds theS_IFMTfile mode bits (0020000for character,0060000for block) whiledeviceis the dev_t value (0x1234), passing a block device mode intype(e.g.devname_r_nid_postfix(0x6001, BlockType, buf, len)) tests(0x6001 & 0170000) == 0060000rather than(type & TypeMask) == BlockType. If(type & TypeMask) == BlockTypeis passed with a device number whose high bits don't match0060000, it erroneously treats the valid block device type as#NODEV, and conversely if a device number happens to have bits matchingBlockType, any non-charactertype(like regular file0100000) will incorrectly be treated as a block device. The condition should check(type & TypeMask) == BlockType.
- core/libs/prx/libc/src/DeviceName.cpp:28: In devname_r_nid_postfix, the block type check evaluates
Requesting changes for the points above.
MarcZX10
left a comment
There was a problem hiding this comment.
Reviewed — looks good
Nothing blocks this change; the implementation and tests match the reported console observations for the measured cases.
What I ran
Read the prepared diff and all three changed files at head af7de378cfa34cc628ea2704bfdd46097559d65d. No build or tests were run in this review; the build and test results in the PR description are author-reported.
Results
| Check | Result | Note |
|---|---|---|
| Changed source and tests read | passed | All three changed files were included and read |
| Build and tests in this review | not run | PR reports prior builds and tests |
| Console behavior | not run | Only the author's reported measurements were available |
Findings
Blocking
None.
Non-blocking
None.
Not verified
No files were excluded from review. The build and test results, console measurements, title-process behavior, and unmeasured device numbers described in the PR were not independently verified.
To get this to a good state
Nothing is required.
|
The condition in AI-assisted (Claude Code). |
What
devname_r_nid_postfixinlibc/src/DeviceName.cpp, behinddevnameanddevname_r, formatted the name of a character device that is notNODEVas#C:followed by the whole device number in hexadecimal (#C:0x1234), never seterrno, and answered a negative length withEOVERFLOWand an empty string. A PS5 payload process on firmware 11.60 calleddevname_rinlibSceLibcInternal.sprx: for the device number0x1234with a character type it returned#C:18:0x34witherrno2 (case 2.2, line 9 of the raw output), the major number (bits 8 to 15) in decimal and the minor number in hexadecimal.NODEV(0xffffffff) gave#NODEVwitherrnostill 0 (2.1, line 8), and so did0x1234with a block type (2.3, line 10). With buffer lengths 1, 2 and 5 it wrote an empty string,#and#C:1, witherrno2 each time (3.1 to 3.3, lines 11 to 13); with length 0 the buffer was untouched anderrnowas 2 (3.4, line 14); with length -1 it wrote the whole name#C:18:0x34anderrnowas 14 (3.5, line 15), anddevnamecalled twice returned the same static buffer (case 4, line 16). The fix formats#%c:%d:0x%xwithmajor = (device >> 8) & 0xffandminor = device & 0xffff00ff, setserrnoto 2 for a character type (14 for a negative length) and drops theEOVERFLOWbranch, so a negative length writes the whole name; the copy buffer grows from 16 to 32 bytes.The test commit changes
GuestDeviceName.cpp: the pins for0x1234, the truncation loop, the length 0 and -1 cases and thedevnamepointer now use the measured values, and theNODEVcheck also checks thaterrnostays 0. The pins for numbers the probe did not measure (0,1,0xf,0x10,0xdeadbeef,#B:names,INT_MINand an unmapped buffer with length -1) are removed; the older unchanged checks (a regular file, a directory or no type give#NODEV, and unmapped buffers) stay and are not measured. The entry indocs/dev/debt/changes/now states the console result, that theerrnocause is not observed, and what is unmeasured; it is 942 characters, up from 852.The console also returned the names
null,zero,consoleandrandomfor the device numbers of/dev/null,/dev/zero,/dev/consoleand/dev/random(cases 1.1 to 1.4, lines 4 to 7); AnyPS5 does not model named devices, and the entry says so. The measurement is oflibSceLibcInternal; a title's ownlibc.prxis not measured, and AnyPS5'slibcemulates it.Not measured on the console: other device numbers (the major and minor macros are those of FreeBSD 11.4 and tested only at
0x1234),#B:names for block numbers, negative lengths other than -1, the cause oferrno2, the 64-byte size of the static buffer andS_ISBLK(dev), and a title process.Tested
Measured on a PS5 with firmware 11.60 (
kernel_get_fw_version0x11600005), from a homebrew payload process (ps5-payload-sdk v0.45, elfldr) that loadslibSceLibcInternal.sprxand takesdevname_randdevnamewithdlsym(line 3 shows both were found), on 2026-11-17 at 15:05 UTC. A title process may differ. The cases are 0 to 4, lines 2 to 16 of the raw output in the first comment.mainbefore (test commit only)devname_r(0x1234, S_IFCHR, 16)#C:18:0x34,errno2 (line 9)#C:0x1234,errnounchanged#C:18:0x34,errno2NODEVwithS_IFCHR;0x1234withS_IFBLK#NODEV,errno0 (lines 8, 10)#NODEV#NODEV,errnounchanged#,#C:1,errno2 (lines 11 to 13)#,#C:0#,#C:1,errno2errno2 (line 14)errnounchangederrno2errno14 (line 15)errno84errno14devnametwiceOn
main, with the test commit alone, one test,guest_device_name, fails; it contains the checks of the table; the "before" column is read from the old code and the old test, and only the failure of the test is run. Windows 10 Pro 10.0.19045 and Linux (WSL), one build per commit, on baseupstream/main1adc0e2f4b9, on 2026-11-17, Windows from 15:57 UTC and Linux from 15:57 UTC. Windows: the test commit alone built withninjareturning 0 andctestreturned 8 (the first changed check, line 39 of the test, failed); a build of the documentation commit as it was before its fragment was reworded returnedninja0 andctest0. Linux: on commits whose trees equal the three commits of this pull request as they were at that time,ninjareturned 0 for all three, the test commit alone abortedguest_device_name(1 of 1 tests failed), and the fix commit and the documentation commit passed it in three runs each;tools/check_conventions.pypassed for each commit. The documentation commit was reworded afterwards in one line of the fragment; the trees of the test and fix commits are unchanged.tools/check_conventions.py --base upstream/mainpasses with no error and no warning at the head (the changed entry is 942 characters, up from 852, counted without the leading '- '), anddebt_fold.py --checkreturns 0. The change is 3 files, +34/-54 (DeviceName.cpp+10/-21, the test +19/-33, the fragment +5), with no new comments in code. Not tested: macOS, the CI runner, a title process, other firmware, and the cases listed above as not measured.Overlap: the other open pull requests that change one of the files of this branch (
core/libs/prx/libc/src/DeviceName.cpp,core/libs/tests/GuestDeviceName.cpp) or the debt fragment, found by reading the changed files of all 1765 open pull requests at about 18:08 UTC, by reading what they change indocs/dev/debt/libc.mdand in the fragments underdocs/dev/debt/changes/fordevnameanddevname_r, and by searching their titles and branch names for the same names (no hit), were test-merged on 2026-11-17 at about 18:33 UTC againstmain89b0eef7ab4together with this head; the branch is based on1adc0e2f4b9, an ancestor of it, merges intomainwithout a conflict, andtools/debt_fold.py --checkapplies the fragment to thelibcdebt file ofmainwithout an error (checked without the fragmentlibressl3-20171-inventory.md, which already conflicts withfeat-libressl3-evp-pkey-new-mac-key-v2.mdonmainand is not part of this change), the text the change names occurs in exactly one entry, and no other fragment changes that entry. Two pull requests list one of the files: #12315 (feat(libSceLibreSsl3): implement numeric object identifier text parsing, which already conflicts withmain) and #6918 (a branch with many unrelated files); both change nothing in these two files againstmain, so neither conflicts only with this change. No open pull request changes a line that namesdevnameordevname_ror the debt entry this fragment changes. The merged result was not built or run.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)