Repository navigation
fix(libkernel): refuse the title workaround bits above 0x78 in IsEnabled and IsEnabled2 as the console does - #22365
Conversation
…on the console Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…nelTitleWorkaroundIsEnabled and IsEnabled2 as the console does Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
|
Probe sources (built with ps5-payload-sdk v0.45; probe 1 (IsEnabled): probe.c/* sceKernelTitleWorkaroundIsEnabled, the bit limit, payload process, firmware 11.60 (follow-up of an earlier run in
which bit 127 returned 0x80020016 although the debt libkernel_sys.md sceKernelTitleWorkaroundIsEnabled entry, read from the
13.42 disassembly, says bits 0 to 0x7F are accepted). The probe looks the function up with dlsym in libkernel.sprx and
calls it once for every bit from 0 to 127 on a block of its own (version 1, align 0, ids = {0, 0}; the output word is
filled with 0x5a5a5a5a before each call), 128 calls in all, no loop beyond that. It prints the first bit that is rejected,
the number of accepted bits, the list of rejected bits below 128 as ranges, and whether an accepted bit ever returned
enabled other than 0 (it must not, the block is zero). Then the same for the block ids = {0xffffffffffffffff,
0xffffffffffffffff}: the number of accepted bits and how many returned 1. Only a block of its own is used. A fault ends
the probe at once with _exit. 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);
}
typedef struct { int32_t version; int32_t align; uint64_t ids[2]; } Workaround;
typedef int (*fn_w)(const Workaround*, int32_t, int32_t*);
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 sweep(const char* id, const char* label, fn_w f, const Workaround* w) {
char ranges[160];
int accepted = 0, ones = 0, first_bad = -1, nonzero_when_zero = 0, n = 0;
int in_range = 0, start = 0;
ranges[0] = 0;
for (int bit = 0; bit < 128; ++bit) {
int32_t en = 0x5a5a5a5a;
int r = f(w, bit, &en);
if (r == 0) {
accepted++;
if (en == 1) ones++;
if (en != 0 && en != 1) nonzero_when_zero++;
if (in_range && n < (int)sizeof ranges - 16) { n += snprintf(ranges + n, sizeof ranges - n, "%d-%d,", start, bit - 1); in_range = 0; }
} else {
if (first_bad < 0) first_bad = bit;
if (!in_range) { in_range = 1; start = bit; }
}
}
if (in_range && n < (int)sizeof ranges - 16) n += snprintf(ranges + n, sizeof ranges - n, "%d-127,", start);
char extra[260];
snprintf(extra, sizeof extra, "accepted=%d first_rejected=%d returned_one=%d odd_values=%d rejected_ranges=%s", accepted, first_bad, ones, nonzero_when_zero, ranges);
probe_case(id, label, accepted, 0, extra);
}
int main(void) {
signal(SIGALRM, probe_alarm);
signal(SIGSEGV, fault);
signal(SIGBUS, fault);
alarm(PROBE_MAX_S);
probe_header();
char extra[96];
errno = 0;
void* lk = dlopen("libkernel.sprx", RTLD_LAZY);
probe_case("0", "load libkernel.sprx", lk ? 0 : -1, errno, lk ? "loaded" : dlerror());
if (!lk) { probe_end(); return 0; }
fn_w f = (fn_w)dlsym(lk, "sceKernelTitleWorkaroundIsEnabled");
snprintf(extra, sizeof extra, "IsEnabled=%d", f != 0);
probe_case("0b", "dlsym of the function", f ? 0 : -1, 0, extra);
if (!f) { probe_end(); return 0; }
Workaround zero = {1, 0, {0, 0}};
Workaround ones = {1, 0, {0xffffffffffffffffULL, 0xffffffffffffffffULL}};
sweep("1", "bits 0..127 on an all-zero block", f, &zero);
sweep("2", "bits 0..127 on an all-ones block", f, &ones);
probe_end();
return 0;
}probe 1 (IsEnabled): stdoutprobe 2 (IsEnabled2): probe.c/* sceKernelTitleWorkaroundIsEnabled2, the bit limit, payload process, firmware 11.60 (follow-up of an earlier run in
which bit 127 returned 0x80020016 although the debt libkernel_sys.md sceKernelTitleWorkaroundIsEnabled entry, read from the
13.42 disassembly, says bits 0 to 0x7F are accepted). The probe looks the function up with dlsym in libkernel.sprx and
calls it once for every bit from 0 to 127 on a block of its own (version 1, align 0, ids = {0, 0} or all ones; the output word is
filled with 0x5a5a5a5a before each call), 128 calls per sweep and four sweeps (selector 0 and 0x28, an all-zero and an all-ones block), 512 calls in all. It prints the first bit that is rejected,
the number of accepted bits, the list of rejected bits below 128 as ranges, and whether an accepted bit ever returned
enabled other than 0 (it must not, the block is zero). Then the same for the block ids = {0xffffffffffffffff,
0xffffffffffffffff}: the number of accepted bits and how many returned 1. Only a block of its own is used. A fault ends
the probe at once with _exit. 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);
}
typedef struct { int32_t version; int32_t align; uint64_t ids[2]; } Workaround;
typedef int (*fn_w)(const Workaround*, uint64_t, uint32_t, int32_t*);
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 sweep(const char* id, const char* label, fn_w f, const Workaround* w, uint64_t sel) {
char ranges[160];
int accepted = 0, ones = 0, first_bad = -1, nonzero_when_zero = 0, n = 0;
int in_range = 0, start = 0;
ranges[0] = 0;
for (int bit = 0; bit < 128; ++bit) {
int32_t en = 0x5a5a5a5a;
int r = f(w, sel, (uint32_t)bit, &en);
if (r == 0) {
accepted++;
if (en == 1) ones++;
if (en != 0 && en != 1) nonzero_when_zero++;
if (in_range && n < (int)sizeof ranges - 16) { n += snprintf(ranges + n, sizeof ranges - n, "%d-%d,", start, bit - 1); in_range = 0; }
} else {
if (first_bad < 0) first_bad = bit;
if (!in_range) { in_range = 1; start = bit; }
}
}
if (in_range && n < (int)sizeof ranges - 16) n += snprintf(ranges + n, sizeof ranges - n, "%d-127,", start);
char extra[260];
snprintf(extra, sizeof extra, "accepted=%d first_rejected=%d returned_one=%d odd_values=%d rejected_ranges=%s", accepted, first_bad, ones, nonzero_when_zero, ranges);
probe_case(id, label, accepted, 0, extra);
}
int main(void) {
signal(SIGALRM, probe_alarm);
signal(SIGSEGV, fault);
signal(SIGBUS, fault);
alarm(PROBE_MAX_S);
probe_header();
char extra[96];
errno = 0;
void* lk = dlopen("libkernel.sprx", RTLD_LAZY);
probe_case("0", "load libkernel.sprx", lk ? 0 : -1, errno, lk ? "loaded" : dlerror());
if (!lk) { probe_end(); return 0; }
fn_w f = (fn_w)dlsym(lk, "sceKernelTitleWorkaroundIsEnabled2");
snprintf(extra, sizeof extra, "IsEnabled2=%d", f != 0);
probe_case("0b", "dlsym of the function", f ? 0 : -1, 0, extra);
if (!f) { probe_end(); return 0; }
Workaround zero = {1, 0, {0, 0}};
Workaround ones = {1, 0, {0xffffffffffffffffULL, 0xffffffffffffffffULL}};
sweep("1.0", "IsEnabled2 selector 0, bits 0..127, all-zero block", f, &zero, 0);
sweep("1.1", "IsEnabled2 selector 0, bits 0..127, all-ones block", f, &ones, 0); sweep("2.0", "IsEnabled2 selector 0x28, bits 0..127, all-zero block", f, &zero, 0x28); sweep("2.1", "IsEnabled2 selector 0x28, bits 0..127, all-ones block", f, &ones, 0x28);
probe_end();
return 0;
}probe 2 (IsEnabled2): 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 cc24ac6624:
- Conventions:
tools/check_conventions.pypassed. - Build:
guest_kernel_sys_testsbuilt cleanly. - CTest (
guest_kernel_sys):1/1 Test #864: guest_kernel_sys ................. Passed 0.22 sec 100% tests passed, 0 tests failed out of 1 - Negative control: with
core/libs/prx/libkernel_sys/Export.cppreverted tomainand the new tests kept, the suite fails:Restoring the change makes it pass again.1/1 Test #864: guest_kernel_sys .................Subprocess aborted***Exception: 0.37 sec 0% tests passed, 1 tests failed out of 1 - Code reading:
- Verified bit limit boundary checks in QueryTitleWorkaround (static_caststd::uint32_t(bit) > 0x78) returning 0x80020016 (EINVAL) matching probe measurements on PS5 firmware 11.60.
- Verified null pointer checks for workaround and enabled pointers in QueryTitleWorkaround and sceKernelTitleWorkaroundIsEnabled2 returning 0x8002000E (EFAULT).
- Verified selector validation in sceKernelTitleWorkaroundIsEnabled2 (selector >= 0x29) returning 0x80020016 (EINVAL) before calling QueryTitleWorkaround.
- Verified GuestCopyIn_nid_no_patch and GuestCopyOut_nid_no_patch error handling and errno restoration on copy failure.
- Verified debt fragment docs/dev/debt/changes/fix-kernel-sys-title-workaround-bit-limit.md conforms to repository conventions for technical debt changes.
- Verified test suite in GuestKernelSys.cpp covers boundary values (0x77, 0x78, 0x79, 0x7E, 0x7F) and selectors for IsEnabled, IsEnabled2, and the typo alias sceKernelTitleWorkdaroundIsEnabled.
Looks Good To Me
What
sceKernelTitleWorkaroundIsEnabledandsceKernelTitleWorkaroundIsEnabled2inlibkernel_sys/Export.cppaccepted the bits 0 to 0x7F of the 128-bit workaround block and returned0x80020016(EINVAL) above that, following a reading of the firmware 13.42 disassembly. A PS5 payload process on firmware 11.60 called both console functions (libkernel.sprx) for every bit from 0 to 127 on a block of zeros and on a block of ones.IsEnabled: in both sweeps 121 bits were accepted (return 0), the first refused bit was 121 (0x79) and the refused range was 121 to 127 (cases 1 and 2, lines 4 and 5 of the first raw output).IsEnabled2with the selectors 0 and 0x28: the same in all four sweeps, 121 bits accepted and 121 to 127 refused (cases 1.0 to 2.1, lines 4 to 7 of the second raw output). On the blocks of ones every accepted bit returned 1, on the blocks of zeros none did, and no value other than 0 or 1 was returned (odd_values=0). The probes count a bit as refused when the return value is not 0 and do not print the code; an earlier probe ofIsEnabledhad seen0x80020016(EINVAL) for the bit 127, with the output unchanged. Both functions now refuse the bits above 0x78 with0x80020016(EINVAL, the code seen for the bit 127) and leave the output unchanged; the misspelled exportsceKernelTitleWorkdaroundIsEnabledcallsIsEnabledand follows the new limit.In the code, a shared helper
QueryTitleWorkaroundholds the old logic (a null workaround or output returns0x8002000Efirst, a bit above 0x78 returns0x80020016, and the host-safety fallback for unreadable memory),IsEnabledcalls it, andIsEnabled2keeps its own check of the selector (0x29 or more returns0x80020016, as the 13.42 reading says and as an earlier probe measured for 0x29 and the largest value) and then calls it.The entries in
docs/dev/debt/libkernel_sys.mdsaid that the layout, the bit test and the limit follow a firmware 13.42 reading and are not measured on a console. The fragment adds what 11.60 did for both functions and keeps the 13.42 reading named; it does not call the reading wrong, since a different firmware or a misreading could explain the difference. ForIsEnabled2it also says that the error code was not printed, soEINVALthere follows the 13.42 reading.The test commit extends the workaround block of
CheckProcessQueriesinGuestKernelSys.cppby the bit 120 and checks, for both spellings ofIsEnabledand forIsEnabled2with the selectors 0 and 0x28, that bit 0x77 reads 0, bit 0x78 reads 1, and the bits 0x79, 0x7E and 0x7F return0x80020016with the output unchanged. The old checks that accepted 0x7E and 0x7F are replaced.Not measured on the console: the code returned for the refused bits of
IsEnabled2and for 0x79 to 0x7E ofIsEnabled, the misspelled exportsceKernelTitleWorkdaroundIsEnabledon 11.60 (the entry says it runs the same code, from the 13.42 reading), selectors other than 0 and 0x28 with high bits, a title process, and which workaround bits a title tests. No title is shown to call either function with a bit between 0x79 and 0x7F.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 loadslibkernel.sprxand takes the function withdlsym:IsEnabledon 2026-11-17 at 09:05 UTC andIsEnabled2at 11:04 UTC, two probes with the same sweep. A title process may differ. The cases are 0 to 2, lines 2 to 5 of the first raw output and 0 to 2.1, lines 2 to 7 of the second, both in the first comment (line 3 of each shows that the function was found).mainbefore (test commit only)IsEnabled, bits 0 to 120IsEnabled, bits 121 to 127EINVALseen for 127 in an earlier run)EINVALIsEnabled2, selectors 0 and 0x28, bits 0 to 120IsEnabled2, selectors 0 and 0x28, bits 121 to 127EINVALOn
main, with the test commit alone, one test,guest_kernel_sys, fails; it contains the checks of the table. Windows 10 Pro 10.0.19045 and Linux (WSL), one build per commit, on baseupstream/maine798a424c71, on 2026-11-17 between 11:25 and 11:29 UTC. Windows: the test commit alone built withninjareturning 0 andctestreturned 8 (the test ended with exit code 0xc0000409); the head of this pull request built withninjareturning 0 andctestreturned 0. Linux: on commits whose trees equal the three commits of this pull request,ninjareturned 0 for all three, the test commit alone abortedguest_kernel_sys(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.tools/check_conventions.py --base upstream/mainpasses with no error and no warning (the changedsceKernelTitleWorkaroundIsEnabledentry is 993 characters, up from 900, and thesceKernelTitleWorkaroundIsEnabled2entry 894, up from 637, both counted without the leading '- '). The change is 3 files, +30/-8, 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/libkernel_sys/Export.cpp,core/libs/tests/GuestKernelSys.cpp) or the debt fragment, found by reading the changed files of all 1679 open pull requests at about 12:07 UTC, by reading what they change indocs/dev/debt/libkernel_sys.mdand in the fragments underdocs/dev/debt/changes/forsceKernelTitleWorkaroundIsEnabledandsceKernelTitleWorkaroundIsEnabled2, and by searching their titles and branch names for the same names (no hit), were test-merged on 2026-11-17 at about 12:13 UTC againstmain0859a63ae9ftogether with this head; the branch is based one798a424c71, an ancestor of it, merges intomainwithout a conflict, andtools/debt_fold.py --checkapplies the fragment to thelibkernel_sysdebt 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 each change names occurs in exactly one entry, and no other fragment changes those entries. Eleven pull requests list one of the files. Three of them conflict only with this change: #17908 (feat(libkernel): implement sceKernelSetProcessProperty), #17897 (feat(libkernel): implement the sceKernel*ToolMemory exports) and #17885 (feat(libkernel): export the kernel functions libkernel_sys provided alone) belong to one series that removes the definition ofsceKernelTitleWorkaroundIsEnabledfromlibkernel_sys/Export.cpp(22 lines) and forwards the old name to a version inlibkernel; they merge cleanly withmainand conflict withmainplus this head inExport.cpp, and #17885 also edits thesceKernelTitleWorkaroundIsEnabledline of thelibkernel_sysdebt file. Whichever lands second has to carry the limit of 0x78 into the moved function, because this change adds it to the definition they delete. #12315 and #9389 already conflict withmainand conflict in the same files with this head merged first, and #22219, #21984, #18190, #17936, #15037 and #6918 merge cleanly withmainand with this head. No open pull request changes the two debt entries this fragment changes in another way. 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)