Skip to content

# FileManager: take a mount off the list before releasing it (use-after-free), and run the filemanager suite under ASan in CI - #1062

Merged
chrisgleissner merged 3 commits into
GideonZ:masterfrom
JC-000:fix/filemanager-mount-release-uaf
Nov 10, 2026
Merged

chrisgleissner merged 3 commits into
GideonZ:masterfrom
JC-000:fix/filemanager-mount-release-uaf

Conversation

@JC-000

@JC-000 JC-000 commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

Fixes #1050

Problem

On #1041 a maintainer reported that make host_tests stops at software/filemanager/tests/temp_auto_cleanup_test.cpp with SIGSEGV, so the suites listed after filemanager never run in CI. It does not crash on every machine (it passes here on macOS and on Ubuntu 24.04 / gcc 13.3, aarch64), because it reads freed memory and only faults depending on the allocator. Under AddressSanitizer it is a deterministic heap-use-after-free.

The maintainer's bisect on #1041 points at #931 (94a5f62). It is not only a test problem. The same code runs on the device (by code inspection; not reproduced on a device): detaching a root entry (remove_root_entry) or mounting a ninth image (cache eviction) can read a freed File when the mounted image is a managed temp file whose deletion was deferred.

Cause, on current master:

Two places release a mount while it is still in mount_points:

  • FileManager::invalidate (software/filemanager/filemanager.cc:415) calls release_mount_point(m) and only then mark_for_removal(i) (:416).
  • FileManager::evict_mount_points (:1110) calls release_mount_point(mount_points[victim]) and only then mark_for_removal(victim) (:1111).

release_mount_point (:298) calls fclose on the mount's backing file. fclose closes and frees the file (:1006), and when the temp had a pending delete-on-last-close it then calls delete_file (:1013). That reaches in_use_for_writing (:1165) and discard_mounts_of_file, which walks mount_points and calls mp->get_file()->get_path() (:1154) on the mount being released, whose file was just freed.

What changed and why

Remove the mount from mount_points before releasing it, in both places, and rescan from the start since the release can drop other mounts. The branch also adds a make -C software/filemanager/tests asan target (ASan, env -u LD_PRELOAD ASAN_OPTIONS=detect_leaks=0, as the existing image mount step) and a CI step that runs it — that CI step has only been run locally (make asan), not yet in GitHub Actions, so it guards nothing until it has run there. The default make host_tests build is unchanged.

Products. the code is shared by the Ultimate 64, Ultimate 64 Elite II, C64 Ultimate and the Ultimate-II cartridges (U2+, U2+L). Host only.

How I tested it

Host: macOS, with the device-free tests run in an Ubuntu 24.04 container (g++ 13.3). The hardware runs used the test harness from the same machine.

Host tests (device-free, run before and after the change):

  • filemanager suite under AddressSanitizer: make -C software/filemanager/tests asan
    • with the change: passes (both temp-cleanup tests)
    • without it: heap-use-after-free in discard_mounts_of_file; each of the two fixes alone leaves the other test failing

Hardware.

Device Firmware Ultimate FPGA C64 core
Ultimate 64 Elite ac0304316 126 1.50
C64 Ultimate b0f40a17b 126 1.50

None: host only. The AddressSanitizer report is in the issue.

Standard profile (./run-tests --profile standard), red = master, green = a build carrying this landing set of fixes (this change included), report from python3 tools/e2e_report.py:

  • U64 Elite: 71 of 72 suites pass on master and 71 of 72 with the fixes. The one failure is the same on both, iec-partition-config: it stops at a key-send step because the upstream test's REST backend has no F2 key alias (rest_backend.py:556), a test-side limitation, not an app difference.
  • C64 Ultimate: 71 of 72 on master and 71 of 72 with the fixes. The one failure is again iec-partition-config, here at a "menu screen unavailable" step that also fails on master, so it is not from these changes.

Soaks pass on both devices: mount-cache-leak — the soak that exercises exactly this mount-after-mount release path — plus listener-soak and network-connection.

Untested

  • Ultimate-II cartridge (U2+ / U2+L): not tested. I do not have one. Not built either for the cartridges, because the build environment I use cannot build those targets; CI builds them.
  • I could not reproduce the plain SIGSEGV, only the ASan report (macOS and aarch64 Ubuntu); not run on x86_64.
  • The CI step has only run locally (make asan), not in GitHub Actions; it assumes my_docker_image does not force an LD_PRELOAD.
  • make host_tests still stops later at recoverymanifest in my container (no ESP-IDF there); unrelated.
  • Not run on any device; exposure on devices is by code reading.

Written with Claude Opus 5.5 in Claude Code.

JC-000 and others added 3 commits November 7, 2026 16:30
FileManager::invalidate() released a mount while it was still listed in mount_points. Closing its backing file can delete that file (delete_on_last_close), and the delete walks mount_points through discard_mounts_of_file(), reading the freed file of the mount being released. Unlist the mount before releasing it and rescan.

The filemanager host test is built with AddressSanitizer so the existing CountCleanupDefersOpenMountedTempUntilBackingHandleCloses case fails on the old code instead of depending on allocator behaviour.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…test target

evict_mount_points() had the same release-before-unlist order as invalidate(). A cached mount of a temp file whose deletion was deferred is deleted while it is released, and the delete walked a mount_points list that still held it. New test covers it.

Move AddressSanitizer out of the default filemanager test build into 'make -C software/filemanager/tests asan' (env -u LD_PRELOAD, detect_leaks=0, as the image mount step in build.yml).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@chrisgleissner chrisgleissner added bug Existing behavior is incorrect, broken, or regressed; includes supported documentation defects. storage Disk/tape emulation, images, mounting, copying, filenames, filesystems, and storage media. test Test suites, harnesses, coverage, and infrastructure; not tests supplied to reproduce firmware bugs. labels Nov 10, 2026
@chrisgleissner
chrisgleissner self-requested a review November 10, 2026 12:30
@chrisgleissner
chrisgleissner merged commit c3a41d5 into GideonZ:master Nov 10, 2026
1 check passed
@chrisgleissner

chrisgleissner commented Nov 10, 2026 •

Copy link
Copy Markdown
Collaborator

@JC-000 thanks for the fix, now merged.

Review: unlisting a mount before releasing it matches the existing remove-then-release pattern in discard_mount_tree, and rescanning from the start in invalidate is right because a release can drop other mounts. I ran the new tests at the PR head, then with each fix reverted separately. Plain make segfaults and make asan reports a heap-use-after-free through evict_mount_points and invalidate respectively, so each fix is covered. CI on the merged head was green, including the new ASan step. I did not run it on hardware; the defect does not reliably fault on a device.

One open point, not blocking: ~FileManager releases mounts while they are still listed, which has the same shape.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Existing behavior is incorrect, broken, or regressed; includes supported documentation defects. storage Disk/tape emulation, images, mounting, copying, filenames, filesystems, and storage media. test Test suites, harnesses, coverage, and infrastructure; not tests supplied to reproduce firmware bugs.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

make host_tests can SIGSEGV in the filemanager suite: use-after-free when a mounted temp file is deleted as its mount is released

2 participants