Skip to content

fix(libc): format devname_r names as the console does - #22522

Open
xXPBSXx wants to merge 3 commits into
boykopovar:mainfrom
xXPBSXx:fix/libc-devname-console-format
Open

xXPBSXx wants to merge 3 commits into
boykopovar:mainfrom
xXPBSXx:fix/libc-devname-console-format

Conversation

@xXPBSXx

@xXPBSXx xXPBSXx commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

What

devname_r_nid_postfix in libc/src/DeviceName.cpp, behind devname and devname_r, formatted the name of a character device that is not NODEV as #C: followed by the whole device number in hexadecimal (#C:0x1234), never set errno, and answered a negative length with EOVERFLOW and an empty string. A PS5 payload process on firmware 11.60 called devname_r in libSceLibcInternal.sprx: for the device number 0x1234 with a character type it returned #C:18:0x34 with errno 2 (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 #NODEV with errno still 0 (2.1, line 8), and so did 0x1234 with a block type (2.3, line 10). With buffer lengths 1, 2 and 5 it wrote an empty string, # and #C:1, with errno 2 each time (3.1 to 3.3, lines 11 to 13); with length 0 the buffer was untouched and errno was 2 (3.4, line 14); with length -1 it wrote the whole name #C:18:0x34 and errno was 14 (3.5, line 15), and devname called twice returned the same static buffer (case 4, line 16). The fix formats #%c:%d:0x%x with major = (device >> 8) & 0xff and minor = device & 0xffff00ff, sets errno to 2 for a character type (14 for a negative length) and drops the EOVERFLOW branch, 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 for 0x1234, the truncation loop, the length 0 and -1 cases and the devname pointer now use the measured values, and the NODEV check also checks that errno stays 0. The pins for numbers the probe did not measure (0, 1, 0xf, 0x10, 0xdeadbeef, #B: names, INT_MIN and 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 in docs/dev/debt/changes/ now states the console result, that the errno cause is not observed, and what is unmeasured; it is 942 characters, up from 852.

The console also returned the names null, zero, console and random for the device numbers of /dev/null, /dev/zero, /dev/console and /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 of libSceLibcInternal; a title's own libc.prx is not measured, and AnyPS5's libc emulates 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 of errno 2, the 64-byte size of the static buffer and S_ISBLK(dev), and a title process.

Tested

Measured on a PS5 with firmware 11.60 (kernel_get_fw_version 0x11600005), from a homebrew payload process (ps5-payload-sdk v0.45, elfldr) that loads libSceLibcInternal.sprx and takes devname_r and devname with dlsym (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.

Call PS5 payload process, firmware 11.60 AnyPS5 main before (test commit only) here (test and fix)
devname_r(0x1234, S_IFCHR, 16) #C:18:0x34, errno 2 (line 9) #C:0x1234, errno unchanged #C:18:0x34, errno 2
NODEV with S_IFCHR; 0x1234 with S_IFBLK #NODEV, errno 0 (lines 8, 10) #NODEV #NODEV, errno unchanged
lengths 1, 2, 5 empty, #, #C:1, errno 2 (lines 11 to 13) empty, #, #C:0 empty, #, #C:1, errno 2
length 0 buffer untouched, errno 2 (line 14) untouched, errno unchanged untouched, errno 2
length -1 whole name, errno 14 (line 15) empty string, errno 84 whole name, errno 14
devname twice same static buffer (line 16) same buffer same buffer

On 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 base upstream/main 1adc0e2f4b9, on 2026-11-17, Windows from 15:57 UTC and Linux from 15:57 UTC. Windows: the test commit alone built with ninja returning 0 and ctest returned 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 returned ninja 0 and ctest 0. Linux: on commits whose trees equal the three commits of this pull request as they were at that time, ninja returned 0 for all three, the test commit alone aborted guest_device_name (1 of 1 tests failed), and the fix commit and the documentation commit passed it in three runs each; tools/check_conventions.py passed 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/main passes with no error and no warning at the head (the changed entry is 942 characters, up from 852, counted without the leading '- '), and debt_fold.py --check returns 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 in docs/dev/debt/libc.md and in the fragments under docs/dev/debt/changes/ for devname and devname_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 against main 89b0eef7ab4 together with this head; the branch is based on 1adc0e2f4b9, an ancestor of it, merges into main without a conflict, and tools/debt_fold.py --check applies the fragment to the libc debt file of main without an error (checked without the fragment libressl3-20171-inventory.md, which already conflicts with feat-libressl3-evp-pkey-new-mac-key-v2.md on main and 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 with main) and #6918 (a branch with many unrelated files); both change nothing in these two files against main, so neither conflicts only with this change. No open pull request changes a line that names devname or devname_r or the debt entry this fragment changes. The merged result was not built or run.

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 (no graphics change)
  • AI-assisted: yes

xXPBSXx and others added 3 commits November 17, 2026 16:53
…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>
@xXPBSXx

xXPBSXx commented Nov 17, 2026

Copy link
Copy Markdown
Contributor Author

Probe source (built with ps5-payload-sdk v0.45; PROBE_BUILD is the first 12 hex digits of the source's SHA-256, b60e7ba4f34b) and its raw output from the run on 2026-11-17 at 15:05 UTC. The clock in the PROBE line is the console's own clock. The probe loads libSceLibcInternal.sprx, takes devname_r and devname with dlsym, reads st_rdev with stat for /dev/null, /dev/zero, /dev/console and /dev/random (nothing is opened) and calls devname_r for each, then for NODEV, for 0x1234 with a character and a block type, with the buffer lengths 1, 2, 5, 0 and -1 on a 16-byte buffer followed by a guard area, and calls devname twice to compare the returned pointers; every call is guarded against signals and by a self limit. An internal probe number in the header comment was removed; the code is unchanged, and the build ID is the hash of the original file.

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;
}
stdout
PROBE	libc_devname	fw=0x11600005	build=b60e7ba4f34b	pid=222	utc=2026-11-17T16:47:01Z
CASE	0	load libSceLibcInternal.sprx	0	2	loaded
CASE	0b	dlsym of the functions	0	0	devname_r=1 devname=1
CASE	1.1	devname_r(rdev of /dev/null, S_IFCHR, 64)	1	0	returned_null=0 text=null
CASE	1.2	devname_r(rdev of /dev/zero, S_IFCHR, 64)	1	0	returned_null=0 text=zero
CASE	1.3	devname_r(rdev of /dev/console, S_IFCHR, 64)	1	0	returned_null=0 text=console
CASE	1.4	devname_r(rdev of /dev/random, S_IFCHR, 64)	1	0	returned_null=0 text=random
CASE	2.1	devname_r(NODEV 0xffffffff, S_IFCHR, 16)	1	0	returned_null=0 same_as_buf=1 text=#NODEV guard_changed=0
CASE	2.2	devname_r(0x1234, S_IFCHR, 16)	1	2	returned_null=0 same_as_buf=1 text=#C:18:0x34 guard_changed=0
CASE	2.3	devname_r(0x1234, S_IFBLK, 16)	1	0	returned_null=0 same_as_buf=1 text=#NODEV guard_changed=0
CASE	3.1	devname_r(0x1234, S_IFCHR, 1)	1	2	returned_null=0 same_as_buf=1 text= guard_changed=0
CASE	3.2	devname_r(0x1234, S_IFCHR, 2)	1	2	returned_null=0 same_as_buf=1 text=# guard_changed=0
CASE	3.3	devname_r(0x1234, S_IFCHR, 5)	1	2	returned_null=0 same_as_buf=1 text=#C:1 guard_changed=0
CASE	3.4	devname_r(0x1234, S_IFCHR, 0)	1	2	returned_null=0 same_as_buf=1 text=????????????????ZZZZ guard_changed=0
CASE	3.5	devname_r(0x1234, S_IFCHR, -1)	1	14	returned_null=0 same_as_buf=1 text=#C:18:0x34 guard_changed=0
CASE	4	devname(0x1234) and devname(0x1235): same static buffer	1	0	first_null=0 second_null=0 same_pointer=1
END	15

AI-assisted (Claude Code).

@PrivacyOnion PrivacyOnion left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against af7de378cf:

  • Conventions: tools/check_conventions.py passed.
  • Build: guest_device_name_tests built 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.cpp reverted to main and the new tests kept, the suite fails:
    1/1 Test #1225: guest_device_name ................Subprocess aborted***Exception:   0.12 sec
    0% tests passed, 1 tests failed out of 1
    
    Restoring the change makes it pass again.
  • 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), masking device with TypeMask instead of masking type with TypeMask. Because type holds the S_IFMT file mode bits (0020000 for character, 0060000 for block) while device is the dev_t value (0x1234), passing a block device mode in type (e.g. devname_r_nid_postfix(0x6001, BlockType, buf, len)) tests (0x6001 & 0170000) == 0060000 rather than (type & TypeMask) == BlockType. If (type & TypeMask) == BlockType is passed with a device number whose high bits don't match 0060000, it erroneously treats the valid block device type as #NODEV, and conversely if a device number happens to have bits matching BlockType, any non-character type (like regular file 0100000) will incorrectly be treated as a block device. The condition should check (type & TypeMask) == BlockType.

Requesting changes for the points above.

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@xXPBSXx

xXPBSXx commented Dec 2, 2026

Copy link
Copy Markdown
Contributor Author

The condition in devname_r_nid_postfix that tests device & TypeMask instead of type & TypeMask is deliberate: the debt entry docs/dev/debt/changes/fix-libc-devname-console-format.md states it as an assumption from FreeBSD 11.4 that was not measured on the console, and the test pins that the device number 0x1234 with a block type gives #NODEV as the PS5 did (case 2.3).

AI-assisted (Claude Code).

@ligmaSec ligmaSec left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Built the submitted DeviceName.cpp and ran the exact GuestDeviceName fixture on Linux. It passed the console name formatting cases, short and zero-size buffers, and same-thread reuse of the devname return buffer.

AI-assisted: yes.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants