Skip to content

fix(libkernel): fail ftruncate and getdirentries on a file read from app0.zar with EINVAL, as FreeBSD 11.4 does - #22298

Merged
oneandonlydean merged 1 commit into
boykopovar:mainfrom
brajankw:fix/libkernel-archived-file-ftruncate-getdirentries-einval
Nov 17, 2026
Merged

oneandonlydean merged 1 commit into
boykopovar:mainfrom
brajankw:fix/libkernel-archived-file-ftruncate-getdirentries-einval

Conversation

@brajankw

Copy link
Copy Markdown
Contributor

What

ftruncate, sceKernelFtruncate, getdirentries, sceKernelGetdirentries and sceKernelGetdents threw on a descriptor of a file read from app0.zar. In FreeBSD 11.4 these fail with EINVAL whatever the filesystem:

  • kern_ftruncate (sys/kern/sys_generic.c) refuses a descriptor opened without FWRITE, and an archived file can only be opened read-only, since other modes fail with EROFS.
  • kern_getdirentries (sys/kern/vfs_syscalls.c) refuses a vnode that is not VDIR, and archived directories are not opened through this path.

ftruncate now returns EINVAL for an archived file after its existing length and socket checks. getdirentries returns EINVAL for an archived file before it looks at the buffer, as kern_getdirentries rejects the vnode before any copyout. A null buffer or a size of 0 therefore gives EINVAL too. A host regular file still gets EFAULT for a null buffer first; that ordering is unchanged here.

  • GuestKernelAppArchive.cpp: in Refusals, the ftruncate and getdents throw checks become EINVAL checks for ftruncate, sceKernelFtruncate, sceKernelGetdents, sceKernelGetdirentries (with a buffer, with a null buffer and with size 0, each leaving *basep alone) and getdirentries.
  • fsync, fdatasync, fchmod, futimes, flock, fstatfs and fdopen on an archived file still throw, as their answers depend on the console's /app0 filesystem.
  • No debt fragment; no entry lists these two calls.

Tested

Linux (Debian 13, GCC 14.2.0, Release), on main 47b33132f9.

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 and the licence notice of code adapted from upstream (see AGENTS.md)
  • 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

…app0.zar with EINVAL, as FreeBSD 11.4 does

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@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 caf24688e9:

  • Conventions: tools/check_conventions.py passed.
  • Build: guest_kernel_app_archive_tests built cleanly.
  • CTest (guest_kernel_app_archive_archive, guest_kernel_app_archive_plain, guest_kernel_app_archive_mmap):
    1/3 Test #5502: guest_kernel_app_archive_plain .....   Passed    0.05 sec
    2/3 Test #5501: guest_kernel_app_archive_archive ...   Passed    0.07 sec
    3/3 Test #5503: guest_kernel_app_archive_mmap ......   Passed    0.04 sec
    100% tests passed, 0 tests failed out of 3
    
  • Negative control: with core/libs/prx/libkernel/File/src/Stdio.cpp reverted to main and the new tests kept, the suite fails:
    1/3 Test #5502: guest_kernel_app_archive_plain .....   Passed    0.04 sec
    2/3 Test #5503: guest_kernel_app_archive_mmap ......   Passed    0.03 sec
    3/3 Test #5501: guest_kernel_app_archive_archive ...Subprocess aborted***Exception:   0.40 sec
    67% tests passed, 1 tests failed out of 3
    
    Restoring the change makes it pass again.
  • Code reading:
    • Checked Stdio.cpp:1220 ftruncate_nid_postfix correctly returns PosixFailure(GUEST_EINVAL) when File::IsArchivedFile(d) is true, matching FreeBSD 11.4 kern_ftruncate descriptor write-permission requirements.
    • Checked Stdio.cpp:1464, 1511, 1593 sceKernelGetdirentries overloads on Windows, macOS, and Linux to ensure File::IsArchivedFile check precedes buf/nbytes validation and returns SceErrorFromErrno(GUEST_EINVAL), matching FreeBSD kern_getdirentries non-directory vnode check.
    • Verified error code conversions between PosixFailure(GUEST_EINVAL) (-1 with errno=22) and sceKernel error codes (SCE_KERNEL_ERROR_EINVAL = 0x80020016) for both POSIX and sceKernel variants.
    • Audited GuestKernelAppArchive.cpp:432-443 test cases to ensure ftruncate, sceKernelFtruncate, getdirentries, sceKernelGetdirentries (with valid buf, null buf, and zero size), and sceKernelGetdents are verified against archived file descriptors without modifying basep.
    • Verified strict compliance with AGENTS.md conventions: no comments added, no hardcoded title IDs or special cases, and no extraneous files added.

Looks Good To Me

@oneandonlydean
oneandonlydean merged commit f807c7d into boykopovar:main Nov 17, 2026
14 checks passed
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.

3 participants