Skip to content

fix(libkernel_sys): stop link at 32 symbolic links as FreeBSD does - #14548

Merged
oneandonlydean merged 2 commits into
boykopovar:mainfrom
Ragbie:fix/link-symbolic-link-limit
Nov 2, 2026
Merged

oneandonlydean merged 2 commits into
boykopovar:mainfrom
Ragbie:fix/link-symbolic-link-limit

Conversation

@Ragbie

@Ragbie Ragbie commented Nov 1, 2026

Copy link
Copy Markdown
Contributor

What

link and linkat now stop at FreeBSD 11.4's limit of 32 symbolic links per lookup. The libkernel_sys debt entry recorded the gap for Windows: there the host resolved the source, so a chain of 40 links was linked. Linux had the same gap through its own limit of 40.

namei counts every symbolic link it meets in one lookup, if (ndp->ni_loopcnt++ >= MAXSYMLINKS) error = ELOOP;, with MAXSYMLINKS 32 (param.h):

  • 32 links resolve, and the 33rd returns ELOOP.
  • The count covers links in intermediate components and links inside the targets of other links.
  • lookup follows the last component only with FOLLOW or a trailing slash.

kern_linkat looks up the source with FOLLOW for link and for linkat with AT_SYMLINK_FOLLOW, and the new name without it. Each of the two lookups has its own count.

SymbolicLinkLimitError walks the host path from its root one component at a time and counts the links it follows:

  • A relative target's components go in front of the rest of the path. An absolute target restarts at its root.
  • .. goes to the parent of the resolved directory, as FreeBSD's physical lookup does.
  • A .. after a missing or non-directory component stops the count, so the existing checks still report ENOENT or ENOTDIR as FreeBSD does.
  • On Windows only IO_REPARSE_TAG_SYMLINK reparse points count, read with the existing WindowsSymbolicLink::Read. Junctions are walked through without being counted.

link checks the source (last component followed) and the new name (last component not followed). linkat does the same and follows the source's last component only with AT_SYMLINK_FOLLOW. The order of errors follows kern_linkat:

  1. source ELOOP/ENOENT/ENOTDIR
  2. EPERM for a directory
  3. the new name's ELOOP/ENOENT/ENOTDIR
  4. EEXIST, then EXDEV

The debt entry now states what is left:

  • The count starts at the host root, so links in the host directories a guest path maps to count too. The default guest root is canonical, so this only adds to the count for an alias mount whose host path holds links.
  • A guest path, and on Windows a descriptor-relative one, reaches the count with .. already dropped lexically.

Tested

guest_kernel_sys gets CheckLinkSymbolicLinkLimit. It builds chains of 33 file links and 33 directory links, made with the guest's symlink, and checks:

  • File chain: link through 32 links makes the hard link. Through 33, link and linkat with AT_SYMLINK_FOLLOW return ELOOP, and linkat without the flag links the 33rd link itself.
  • Directory chain: a file below 32 directory links is linked. Below 33, link and linkat return ELOOP.
  • Count across components: 31 directory links plus a file link inside the directory are linked; 32 plus that link return ELOOP.
  • New name: a new name below 32 directory links is created; below 33, link and linkat return ELOOP.
  • Linux and macOS: a descriptor-relative source missing/../file33 returns ENOENT and file.txt/../file33 returns ENOTDIR, as FreeBSD stops at the missing or non-directory component. Win32 drops missing\.. lexically, so these cases are not run on Windows.
  • The test skips itself, as the existing symbolic link cases do, when the user cannot create symbolic links.

Linux (Ubuntu 24.04 container, GCC 13.3.0, non-root, seccomp off):

  • Without the fix: guest_kernel_sys fails at the first 33-link link, which succeeds.
  • With the fix: the 26 guest_kernel_sys* tests pass with --repeat until-fail:3. The 128 tests matching kernel_sys|filesystem|link|access|directory|rename|realpath|stat|open pass.
  • The .. case: with the count but without the stop at a missing component, missing/../file33 returned ELOOP. With the stop it returns ENOENT.

Windows 11 Pro, Intel Core i5-14600KF, WinLibs GCC 15.2.0, full Release build with libs:

  • This machine cannot create symbolic links (no Developer Mode), so the chain checks skip here. GitHub's Windows runners run as an administrator, so the hosted Windows job runs them.
  • The 131 tests matching the same pattern pass.
  • Full ctest: 99% tests passed, 2 tests failed out of 3282. Both fail on main on this machine too:
    • guest_wide_formatting: %Z gives the host's non-ASCII zone name.
    • agc_driver_image_plane_address_on_line.
  • check_skips.py reports one skip, guest_fopen_errors. It skips itself here for the same symbolic link reason.

macOS: not tested. The walk shares the Linux code; macOS's own limit is 32, so the 32-link success cases hold there.

Overlaps: #11298 (dangling Windows link prefixes) and #14101 (fchmodat on a link) also change Export.cpp. Both merge with this branch without conflicts (git merge-tree).

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
  • AI-assisted: yes (Claude Code)

@github-actions

github-actions Bot commented Nov 1, 2026 •

Copy link
Copy Markdown

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 5021 core/libs/prx/libkernel_sys/Export.cpp
PR 9019 core/libs/prx/libkernel_sys/Export.cpp
docs/dev/debt/libkernel_sys.md
PR 9097 core/libs/prx/libkernel_sys/Export.cpp
core/libs/tests/GuestKernelSys.cpp
PR 9162 docs/dev/debt/libkernel_sys.md
PR 9297 docs/dev/debt/libkernel_sys.md
PR 9302 docs/dev/debt/libkernel_sys.md
PR 9389 core/libs/prx/libkernel_sys/Export.cpp
PR 12130 docs/dev/debt/libkernel_sys.md
PR 12642 docs/dev/debt/libkernel_sys.md
PR 13129 docs/dev/debt/libkernel_sys.md
PR 13253 docs/dev/debt/libkernel_sys.md
PR 13256 docs/dev/debt/libkernel_sys.md
PR 14101 core/libs/prx/libkernel_sys/Export.cpp
docs/dev/debt/libkernel_sys.md
PR 14212 docs/dev/debt/libkernel_sys.md
PR 14299 docs/dev/debt/libkernel_sys.md
PR 14403 docs/dev/debt/libkernel_sys.md

@CnRJay CnRJay 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.

Correction (2026-11-01): the title-ID/log demand below is not a CONTRIBUTING requirement; Unblocks: none is allowed. The technical points below are unaffected.

Two questions on SymbolicLinkLimitError in libkernel_sys/Export.cpp, and one on evidence.

  1. Absolute targets. if (target.has_root_path()) current = target.root_path(); restarts the walk at the host root. When a guest created the link with an absolute guest path (/app0/..., /data/...), what does symlink store on disk, the guest string or a translated host path? If it stores the guest string, this walk resolves it against the host filesystem, not the guest mount table, so the count for that chain is wrong (and a link the host cannot read is not counted at all). The test only uses relative targets, so it cannot show which case applies. Please add an absolute-target chain, or point at where the translation happens.

  2. Walk and the real link are separate operations. The count is taken first by walking the path with per-component symlink_status/GetFileAttributesW, and then the real link runs. That is two lookups of the same path, so a concurrent rename can make the answer differ from what the call acts on. For ELOOP only that is probably acceptable, but it should be stated as the reason this is not done by letting the host report its own loop error. Is there a reason the host ELOOP/ERROR_CANT_RESOLVE_FILENAME result is not mapped instead of reimplementing the walk? (I can see the Windows host does not report it, but on Linux the limit is 40, so the code is needed on both; a note on that would help.)

  3. Evidence. No title ID with a log line showing a title hitting link/linkat on a chain of symlinks on main. Without that this is a debt-entry fix with a 113-line diff and the walk costs several stat-class syscalls per component on every link call.

@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 — no blocking findings

The change counts symbolic links while resolving the source and the new name in link and linkat, and returns ELOOP at the 33rd, as the debt entry describes for FreeBSD 11.4's MAXSYMLINKS (32). I read the whole production hunk: relative link targets are resolved against the directory that holds the link, absolute targets reset the walk, and .. is handled. The new test's counts (32 allowed, 33 refused, on the file chain, the folder chain, and the destination side) agree with that logic. The guest test did not run in the hosted Windows job, and its symlink-unavailable branch returns early and passes.

What I ran

  • Head de2b3c010fe4e546d505366d1a198d7fb08e0553; base f2d874c1fdccf781c0fa5d86619bb5dd8894b958. 3 files, +113/-1: core/libs/prx/libkernel_sys/Export.cpp (+54), core/libs/tests/GuestKernelSys.cpp (+58) and docs/dev/debt/libkernel_sys.md.
  • Read the full production hunk (ReadSymbolicLinkTarget and SymbolicLinkLimitError, and their call sites in link_nid_postfix and linkat_nid_postfix) and the test's CheckLinkSymbolicLinkLimit.
  • Read-only checks from the trusted main checkout (precheck not run: no PR-head code is run here): diff check, conventions and the stack check passed; the ABI audit is not applicable.
  • Hosted: the windows CTest run has one case, a tool check. guest_kernel_sys was not in the run.
  • No MarcZX10 review at this head.

Results

Check Result Note
Diff check passed
Conventions and PR template passed conventions check from the main checkout
Mergeability and dependencies passed depends on none; the stack check passed
Link counting and ELOOP at the 33rd link passed read in SymbolicLinkLimitError
Relative and absolute targets passed read; a relative target keeps the directory of the link
Test counts (32 allowed, 33 refused) passed read; agree with the logic on each chain
guest_kernel_sys (hosted) not run not in the Windows CTest run
Symlink-unavailable branch noted returns early and passes vacuously (see Findings)
ABI audit not applicable no exported symbol changed
Build and runtime tests not run no local build
Negative controls not run no local build
Hosted CI passed windows passed (one tool case); linux and relinkers skipped
Read-only check output

Precheck was not run: no PR-head code is run here. Conventions: passed. Hosted: gh api repos/boykopovar/AnyPS5/commits/de2b3c010fe4e546d505366d1a198d7fb08e0553/check-runs.

Negative control

Not run here. A negative control needs a build, and code from this head is not run here.

Findings

Blocking

None.

Non-blocking

  • CheckLinkSymbolicLinkLimit returns early when symlink_nid_postfix fails on the first link, after printing "the link chain limit not tested". The test then passes, so on a host without symlink support the limit is not checked and the run is green. Please make that case a skip (as the other tests do) or a failure, not a pass.

Sources

Not verified

  • The guest test did not run hosted.
  • The FreeBSD 11.4 namei reference in the debt entry was not checked.

To get this to a good state

  • Nothing blocking. Make the symlink-unavailable branch report a skip or a failure, and run the guest test once.

@MarcZX10

MarcZX10 commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

Correction to my review 5732251799 (COMMENTED, head de2b3c0).

The ABI row says "not applicable". The audit was not run when the review was posted. I have now run tools/pr_abi_audit.py from the main checkout at this head: status passed, 64 symbols, 0 findings, 2 limitations. The diff changes only the bodies of link_nid_postfix and linkat_nid_postfix in core/libs/prx/libkernel_sys/Export.cpp, and adds two internal helpers (ReadSymbolicLinkTarget and SymbolicLinkLimitError) with no APS5_VABI export. No export signature changed, so the statement "no exported symbol changed" still holds. The rest of the review is unchanged.

@Ragbie

Ragbie commented Nov 2, 2026

Copy link
Copy Markdown
Contributor Author

Thanks, @CnRJay.

  1. Absolute targets. A guest cannot create a link with an absolute target: symlink_nid_postfix rejects absolute and .. targets with NotImplemented_nid_no_patch and stores the relative target unchanged. Every guest-made link in a chain is therefore relative and resolves against the host directory it sits in. The absolute branch is reached only by links the host made (in host directories a guest path maps to); their targets are host paths, so resolving them from the host root is the right reading. A guest test cannot build such a link, which is why the test uses relative targets only. A link the host cannot read as a symbolic link is not counted, as the debt entry states for junctions.

  2. Host error vs. walk. Neither host stops at 32: Linux returns ELOOP after 40 links and Windows follows up to 63 reparse points before ERROR_CANT_RESOLVE_FILENAME, so mapping the host error would still link chains of 33 to 40 (or 63). The debt entry records the Linux limit. The walk and the real call are separate lookups, as you say; a concurrent rename between them can change the count, the same window LinkSourceError already has before LinkNative. FreeBSD counts inside its single namei, which would need the host lookup reimplemented.

  3. Cost. The walk runs only in link and linkat: one lstat (Linux) or GetFileAttributesW (Windows) per path component, plus one target read per link met.

CI note: the hosted Windows job built guest_kernel_sys_tests but ran conventions_function_bodies_shard_7 instead, the dropped-last-index case that #14413 fixes. The Windows chain test has therefore not run in CI yet, and locally it skips because this account cannot create symbolic links. I will rebase once #14413 is on main so the hosted runner runs it.

@oneandonlydean oneandonlydean self-assigned this Nov 2, 2026
@oneandonlydean
oneandonlydean merged commit 5c24846 into boykopovar:main Nov 2, 2026
12 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.

4 participants