feat(libkernel_sys): implement chown, lchown, fchownat and linkat #9103
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/9103"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by HQasper on 2026-10-21T12:22:47Z.
What
chown,lchown,fchownatandlinkatin libkernel_sys were throwing stubs. This implements them on top of the calls libkernel_sys already has.chown,lchownandfchownatfollowfchown: ownership is not modelled, so owner and group 0 and the unchanged value (-1) are accepted, and any other owner or group throws throughNotImplemented_nid_no_patch, naming the call.fchownis unchanged.stat(chown),lstat(lchown) orfstatat(fchownat), and the owner is checked afterwards, as in FreeBSD 11'skern_fchownat. So a null, empty, missing or not-a-directory path or a bad descriptor fails as those calls do, also with a foreign owner, and nothing is changed on the host.fchownatflags other thanAT_SYMLINK_NOFOLLOW(0x200) returnEINVAL, as in FreeBSD 11'skern_fchownat.linkatpassesAT_FDCWD(-100) and absolute paths tolink, and flags other thanAT_SYMLINK_FOLLOW(0x400) returnEINVAL, as in FreeBSD 11'ssys_linkat. A relative path with a directory descriptor throws. A symbolic link as the source withoutAT_SYMLINK_FOLLOWalso throws: FreeBSD then links the symbolic link itself, whilelinkfollows it. On Windows the source is checked forFILE_ATTRIBUTE_REPARSE_POINT, asfchmodatdoes, since libstdc++ there reports a symbolic link as a regular file. WithAT_SYMLINK_FOLLOWa symbolic link source goes tolink, which follows it on every host, on Windows since #9336.libkernel_sys.prxexports the four NIDs and importsstatandlstatfromlibkernel.prx, which defines them.Tested
Linux (Ubuntu 24.04.5 container, GCC 13.3, Release,
BUILD_TESTING=ON):guest_kernel_syswith a new ownership and link case: passed. It checks owner and group 0 and -1 for all three ownership calls,fchownatthroughAT_FDCWDand a directory descriptor, with and withoutAT_SYMLINK_NOFOLLOW,ENOENT,ENOTDIR,EFAULTandEINVAL, and the throws with their messages. A foreign owner with a missing path, a null path, a not-a-directory path or a bad descriptor returns the lookup error, andfchownatwith a bad flag and a foreign owner returnsEINVAL. A dangling symbolic link failschownwithENOENTand passeslchown. Forlinkatit checks that a hard link is made (link count 2),EEXIST,ENOENT,EINVALforAT_SYMLINK_NOFOLLOW,EFAULT, and the two throws. WithAT_SYMLINK_FOLLOWon a symbolic link it checks that the new name is a hard link to the target (link count 3), and that it is not a symbolic link (on Windows: no reparse point attribute). The symbolic link part was checked to run by breaking one of its expectations once.patched_libraries: passed, and the patched exports and imports were checked withnm -D.python3 tools/check_conventions.py --base upstream/main: passed.705f6e158(main0a0ed7216):guest_kernel_sys,guest_kernel_sys_thread_defaults,guest_kernel_sys_sdk_version,guest_kernel_sys_barrier,guest_kernel_sys_module_internalandpatched_librariespassed.-fsyntax-onlyonExport.cppandGuestKernelSys.cppat705f6e158: passed.Windows 11 Pro 10.0.26300 (MinGW-w64 GCC 15.2.0 WinLibs
x86_64-ucrt-posix-sehr7, Release,BUILD_TESTING=ON, symbolic links allowed through Developer Mode, so the symbolic-link block runs):705f6e158(2026-10-23):guest_kernel_sys,guest_kernel_sys_thread_defaults,guest_kernel_sys_sdk_version,guest_kernel_sys_barrierandguest_kernel_sys_module_internalpassed, with the symbolic-link block running. This covers the changed Windows case ofAT_SYMLINK_FOLLOWon a symbolic link, which now goes throughlinkas #9336 does.guest_kernel_syspassed on 2c5d55055. On 3c7427dd7 it fails at line 136 (guest errno 2), as in a reviewer's Windows run; that run found the two issues fixed in 2c5d55055.Note: #9008 and #9200 have merged and their neighbouring-stub conflicts are resolved in
705f6e158. Since #9336 merged, linkat on Windows passes a symbolic link source with AT_SYMLINK_FOLLOW to link, which now follows it, instead of throwing.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)Duplicate and overlap check
Checked on 2026-10-23 against upstream main
0a0ed7216and the heads of all 1240 open PRs.chown,lchown,fchownatorlinkat.705f6e158merges cleanly with main. For each open PR that changesExport.cpp,Export.Discovery.cpp,GuestKernelSys.cppor the libkernel_sys debt file, merging it after this PR gives no conflict that it does not already have with main.lchflags) now conflicts with main itself inExport.Discovery.cpp, after #9200 removed the stub next to it. Itslchflagsstub is next to thelchownstub this PR removes, so the same file stays the only conflict there.fchownso a foreign owner returnsEPERMinstead of throwing. This PR keeps the throw thatfchownhas on main forchown,lchownandfchownat. If #8457 merges first, the same change can follow here.Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-21T16:14:45Z.
Independent Windows run of this PR (data only).
Environment: Windows 10 Pro 10.0.19045, Ryzen 7 5800X3D, WinLibs GCC 15.2.0 r7 (MinGW-w64 UCRT), Release, BUILD_TESTING=ON. Merge-tree of the PR head against
mainis clean. I built onlyguest_kernel_sys_tests;ctest -N -R guest_kernel_syslists exactly one test.PR head tested:
3c7427dd7128ce09e59a03e1fd3453f80943dca5.Results (3 runs each):
guest_kernel_syspasses 3/3.core/libs/prx/libkernel_sys/Export.Discovery.cpp,core/libs/prx/libkernel_sys/Export.cppwith the version from the PR's merge base (158d8ad34c), then rebuilt. The test fails 3/3 (exit 0xc0000409). I restored the PR's files afterwards.GPU: the run output has no
Selected GPUline; no GPU claim is made.Not run: other platforms, the other tests in the same family, the full repository CTest, and any title or console run.
AI-assisted (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by ycbuzi on 2026-10-21T17:51:56Z.
Windows run of 3c7427dd71 merged onto
main745b394629(a2f1954c38, clean), the platform the description lists as not tested. Windows 11 Home 10.0.26200, MinGW-w64 GCC 15.2.0 (ucrt-posix-seh r7), Release,BUILD_TESTING=ON. The session can create symbolic links, so the new symbolic-link block runs. Without that privilegesymlink_nid_postfixfails and the block is skipped, which fits the 3/3 pass in the Windows run above.guest_kernel_sysfails, whilemain's passes twice on the same host:Both causes are the same MinGW limitation: libstdc++ in MinGW-w64 GCC 15.2 does not see Windows symbolic links. A probe built with the same compiler shows a
CreateSymbolicLinkWlink withFILE_ATTRIBUTE_REPARSE_POINTset, butstd::filesystem::symlink_statusreports it as a regular file (is_symlink0,is_regular_file1).std::filesystem::create_symlinkthere fails with "Function not implemented".linkat_nid_postfixusessymlink_status, so it never sees the link and does not throw. With the check done the wayfchmodat_nid_postfixin the same file does it on Windows, line 136 passes:linkatwithAT_SYMLINK_FOLLOWreturns 0, butowned.txtkeeps one link. That part ismain'slink_nid_postfix, not this PR's code.7cba4ce60adecides whether to follow withstd::filesystem::is_symlink, which has the same blind spot, and its test is under#ifndef _WIN32. A probe onmain80565c5942: afterlink("owned.link", "other.txt")returns 0,other.txtis a reparse point,owned.linkhas 2 links andowned.txt1. So on Windowslinkmakes a hard link to the symbolic link itself, and this PR's new case is the first test that reaches that path there.Not run: a fix for
link_nid_postfixon Windows, Linux, a title.Imported from boykopovar/AnyPS5. Originally posted by HQasper on 2026-10-21T22:06:58Z.
Thanks for the Windows run and the two probes. Both are fixed in 2c5d55055:
linkatnow detects a symbolic link source on Windows withGetFileAttributesWandFILE_ATTRIBUTE_REPARSE_POINT, asfchmodatdoes, so the symbolic-link check in the test now throws as intended. Sincelinkon Windows links the reparse point itself instead of its target (your second finding, inmain'slink),linkatwithAT_SYMLINK_FOLLOWon such a source now throws on Windows instead of quietly making the wrong link, and the test expects that throw there and checks that nothing was created. Makinglinkfollow symbolic links on Windows is left for a separate PR; the debt entry records the gap.chown,lchownandfchownatnow look the path up first and check the owner afterwards, with thefchownatflag check still first. The test now covers your table:chown(missing, 1000, 0)givesENOENT,chown(nullptr, 1000, 0)EFAULT,lchown("owned.txt/inner", 0, 1000)ENOTDIRandfchownat(4321, ...)EBADF, andfchownatwithAT_SYMLINK_FOLLOWand a foreign owner givesEINVAL.Tested on Windows 11 Pro 10.0.26300 with MinGW-w64 GCC 15.2.0 (WinLibs ucrt-posix-seh r7), Release,
BUILD_TESTING=ON, in a session that can create symbolic links (Developer Mode), so the symbolic-link block runs: on 3c7427dd7guest_kernel_sysfails at line 136 (guest errno 2), as in your run, and on 2c5d55055 it passes. On Linux (Ubuntu 24.04, GCC 13.3, Release)guest_kernel_sysandpatched_librariespass, and so doescheck_conventions.Imported from boykopovar/AnyPS5. Originally posted by ycbuzi on 2026-10-21T22:08:38Z.
Thanks for both fixes. The separate PR for
linkexists: #9336 makeslinkfollow a symbolic link on Windows (aGetFileAttributesWreparse check, thenCreateFileWandGetFinalPathNameByHandleW; a dangling link givesENOENT, a loopELOOP, a directoryEPERM). Sincelinkatpasses absolute paths tolink, whichever of the two lands second can drop the WindowsAT_SYMLINK_FOLLOWthrow and its debt entry. Both also changeExport.cppandGuestKernelSys.cpparoundlink, so the second one needs a rebase there.Imported from boykopovar/AnyPS5. Originally posted by HQasper on 2026-10-21T22:12:48Z.
Thanks for clarifying. I'll wait to see which PR lands first and adjust mine accordingly to avoid redundant changes.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T01:36:11Z.
The current overlap scan flags the #9442
libkernel_sys/Export.cppinsertion aroundfstatat/unlink. Yourchown/lchown/fchownatadditions share that insertion area with itslchflagswrapper. Please coordinate with #9442’s author on rebase/order so both sets of exports remain.Imported from boykopovar/AnyPS5. Originally posted by Gruzzberry on 2026-10-22T06:49:13Z.
#9442 adds
lchflagsinlibkernel_sys/Export.cppnext to theunlinkdeclaration where this PR addschown/lchown, and new cases inGuestKernelSys.cpp; text only, whichever lands second rebases.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-23T18:41:15Z.
Review: APPROVED
Reviewed the implementation, test changes, debt notes, and current checks. The ownership calls perform path or descriptor lookup before validating unsupported owner values, preserving the intended lookup-error precedence;
fchownatalso validates its supported flag mask before forwarding. Thelinkatimplementation clearly bounds its supported cases and throws for the two unsupported cases instead of silently claiming success. The added cases cover null and empty paths, lookup errors, flag errors, unchanged and guest ownership, symlinks, descriptor-relative paths, and hard-link results on both reported platforms. The PR reports focused Linux and Windows test runs; current hosted product test jobs are skipped, while conventions, intake, and labels pass. I found no blocking issue in the reviewed changes.