libkernel_sys: readlink checks the path before the buffer size, where FreeBSD checks the size first; fix branch ready #8602

Closed
opened 2026-10-20 18:05:15 +00:00 by xXPBSXx · 0 comments
xXPBSXx commented 2026-10-20 18:05:15 +00:00 (Migrated from github.com)
Contributor

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-20T18:05:15Z.


On Windows, readlink and readlinkat fail a NULL path with EFAULT and an empty path with ENOENT before they look at the buffer size. FreeBSD 11.4 returns EINVAL for a size above IOSIZE_MAX first (kern_readlinkat, before namei at :2238). So readlink(NULL, buf, SIZE_MAX), readlink("", buf, SIZE_MAX) and readlink(<missing path>, buf, SIZE_MAX) return EINVAL there; we return EFAULT, ENOENT (the missing path already returned EINVAL, because its lookup comes after our size check). The same order applies to readlinkat.

Cause. readlink_nid_postfix and readlinkat_nid_postfix test the path pointer and the empty string before the size.

Observed on main 892a4f1328, Windows 10 (10.0.19045), WinLibs GCC 15.2.0 r7, Release: the new assertions in guest_kernel_sys fail without the change at the first one (readlink(NULL, buffer, SIZE_MAX): guest errno 14 instead of 22) and pass with it. I did not run the other calls one by one against main; the other results above come from reading the source. I have not found a title that stops on this.

Fix, ready to cherry-pick. xXPBSXx:fix/libkernel-sys-readlink-size-first (994fb42929, one commit on main 892a4f1328, two files, +9/-2): the size check moves to the top of both functions; a NULL or empty path with a valid size still returns EFAULT or ENOENT.

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5946929442 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/8602). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-20T18:05:15Z. --- On Windows, `readlink` and `readlinkat` fail a NULL path with `EFAULT` and an empty path with `ENOENT` before they look at the buffer size. FreeBSD 11.4 returns `EINVAL` for a size above `IOSIZE_MAX` first ([`kern_readlinkat`](https://github.com/freebsd/freebsd-src/blob/releng/11.4/sys/kern/vfs_syscalls.c#L2232-L2233), before `namei` at [:2238](https://github.com/freebsd/freebsd-src/blob/releng/11.4/sys/kern/vfs_syscalls.c#L2238)). So `readlink(NULL, buf, SIZE_MAX)`, `readlink("", buf, SIZE_MAX)` and `readlink(<missing path>, buf, SIZE_MAX)` return `EINVAL` there; we return `EFAULT`, `ENOENT` (the missing path already returned `EINVAL`, because its lookup comes after our size check). The same order applies to `readlinkat`. **Cause.** [`readlink_nid_postfix` and `readlinkat_nid_postfix`](https://github.com/boykopovar/AnyPS5/blob/892a4f1328/core/libs/prx/libkernel_sys/Export.cpp#L275-L288) test the path pointer and the empty string before the size. Observed on `main` 892a4f1328, Windows 10 (10.0.19045), WinLibs GCC 15.2.0 r7, Release: the new assertions in `guest_kernel_sys` fail without the change at the first one (`readlink(NULL, buffer, SIZE_MAX)`: guest errno 14 instead of 22) and pass with it. I did not run the other calls one by one against `main`; the other results above come from reading the source. I have not found a title that stops on this. **Fix, ready to cherry-pick.** [`xXPBSXx:fix/libkernel-sys-readlink-size-first`](https://github.com/boykopovar/AnyPS5/compare/main...xXPBSXx:AnyPS5:fix/libkernel-sys-readlink-size-first) (994fb42929, one commit on `main` 892a4f1328, two files, +9/-2): the size check moves to the top of both functions; a NULL or empty path with a valid size still returns `EFAULT` or `ENOENT`. AI-assisted (Claude Code).
Sign in to join this conversation.
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5#8602
No description provided.