Skip to content

fix invalid overlay mounts - #11545

Merged
sipsma merged 3 commits into
dagger:mainfrom
sipsma:fix-invalid-overlay-fresh
Dec 10, 2025
Merged

sipsma merged 3 commits into
dagger:mainfrom
sipsma:fix-invalid-overlay-fresh

Conversation

@sipsma

@sipsma sipsma commented Dec 9, 2025 •

Copy link
Copy Markdown
Contributor

Been working on fixing #8265, which has been an issue since forever but seems to have become much easier to trigger recently (I'd highly suspect due to the boltdb fix making race conditions requiring tight time windows much easier to hit now).

  • I'm hoping the problems w/ overlapping overlays is actually impacting us and the cause of some of the strange missing file errors we flake out on in CI sometimes. But it's also impossible to know whether it's the root cause until we've fixed this problem, so gotta fix it either way 🤷‍♂️

Through eBPF tracing I've eventually figured out that at minimum the primary cause (hopefully the only one, but not confirmed) is a situation like:

  1. Engine creates any overlay mount (for anything) with an upperdir
  2. Completely unrelated runc child process starts initialization (has no relation to the overlay from step 1). It gets to the point of being in its own mount namespace but hasn't yet pivot_root'd
    • Because mount namespaces are forks of the parent, the mount ns of the child process will contain the unrelated overlay mount in step 1 during this time!
  3. Engine unmounts the overlay from step 1.
    • It's gone from the engine's mount ns, but not the mount ns of the child process of step 2!
  4. Engine creates a new overlay mount where the upper dir from step 1 is now a lower dir
    • Because that mount still exists on the system in the mount ns of the process from step 2, it's invalid and, at least according to the kernel warning, can result in undefined behavior (whether it truly does is an open question)
  5. The runc process finishes init, does the pivot_root and releases its hold on that overlay mount that leaked into it

AFAICT this is not a unique problem to dagger. There's a long and storied history of github issues about these invalid overlays and it seems to have become mostly ignored in the container ecosystem because everyone just forces index=off now-a-days so the errors "go away" and it just becomes a kernel warning message.

The idea of the fix is to always spawn runc processes from a "clean" mount ns that was made during engine init and has no leaked unrelated mounts. Some open_tree/move_mount magic allows the mounts to selectively move into that namespace.


Some other commits are for random problems I found along the way, e.g.

  • Some old buildkit code leaks tmpdirs for mounts in many case. Do a docker exec dagger-engine-v0.19.8 ls /tmp after running some tests to see 😄
  • Incorrect ref handling logic in http code

TODOs (some will be spin-off PRs)

  • Get actually working fully
    • While I don't hit the invalid mount in the scenario I was hitting it before, it is still happening. I suspect it's just due to a flaw in my fix, but tbd.
    • Figured it out, there was indeed still a tiny race possible due to the way I was unsharing. Fixed now and can't trigger the invalid overlay anymore (at least in TestModule)
  • Separate PR for the "load bpftrace script at engine start" feature

Fixes #8265

@sipsma
sipsma marked this pull request as draft December 9, 2025 09:46
@sipsma
sipsma force-pushed the fix-invalid-overlay-fresh branch 3 times, most recently from c3aa5dd to 86ab327 Compare December 9, 2025 20:49
Old code we picked up from buildkit had a bug where if a temporary mount
was created and then a subpath was the target of the mount used, the
root tempdir would never get removed. If you run a bunch of tests in an
engine and then exec is and ls tmp you will see hundreds/thousands of
empty dirs. This fixes the removal logic to always remove the actual
root of the temp dir.

Signed-off-by: Erik Sipsma <[email protected]>
This code had a release of a ref, which was then returned and used and
then released again. Surprisingly this hasn't *obviously* caused issues
yet but is incorrect. Fixes the ref to only be released once after its
actually done being used.

Signed-off-by: Erik Sipsma <[email protected]>
@sipsma
sipsma force-pushed the fix-invalid-overlay-fresh branch from 86ab327 to 24e43ae Compare December 10, 2025 01:41
@sipsma sipsma changed the title [wip] fix invalid overlay mounts fix invalid overlay mounts Dec 10, 2025
@sipsma
sipsma marked this pull request as ready for review December 10, 2025 01:41
@sipsma
sipsma requested review from alexcb, tiborvass and vito December 10, 2025 01:45
@sipsma sipsma added this to the v0.19.9 milestone Dec 10, 2025

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

Very nice - explanation makes total sense. These new Linux mount syscalls are so handy, too

We need to avoid the following type of race condition, which can result in invalid overlapping overlay mounts:
1. Engine creates random (unrelated to this exec) overlay mount like upperdir=B,lowerdir=A
2. We hit this code and start runc, which gets to the point where it has unshared its mount namespace but
   not yet pivot_root'd. In this state, since mount namespaces are forks of their parent, the overlay mount
   from (1) is visible in the runc processes mount namespace.
3. Engine unmounts the overlay from (1), but that does NOT unmount it from the runc process's mount namespace
4. Engine creates a new overlay mount like upperdir=C,lowerdir=B:C (i.e. upperdir from (1) is now lowerdir).
   This mount is invalid and technically hitting "undefined behavior" since B is still an upperdir in mounts
   that exist on the system.

We avoid this by starting the runc process in a clean mount namespace that was created during engine init before
any mounts existed, guaranteeing none are leaked into it. We setns to that clean mount namespace and then unshare
again to guarantee the namespace for the runc process is fully isolated. OpenTree+MoveMount are then used to
bind the mounts actually needed by the container into that isolated namespace so that runc can see them.

Signed-off-by: Erik Sipsma <[email protected]>
@sipsma
sipsma force-pushed the fix-invalid-overlay-fresh branch from 49ef185 to f5741cc Compare December 10, 2025 17:11
@sipsma
sipsma merged commit 95c5ad7 into dagger:main Dec 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

kernel warnings about invalid overlay mounts

2 participants