Skip to content

🐞 WithDirectory from a container with delete+recreate exec history resurrects deleted files (layercopy ignores ancestor opaque dirs) #13844

Closed

Description

@jeffreyzant

What is the issue?

What happened

When a Directory is taken from a container whose exec deleted a directory
and recreated it (e.g. a package manager replacing a package directory), and
that Directory is then used engine-side β€” Container.WithDirectory,
Directory.Glob, etc. β€” files that were deleted from subdirectories of the
recreated directory reappear with their old content.

The same Directory is correct when read through a mounted view
(WithMountedDirectory, terminal, host export), which makes the
corruption easy to miss: everything you eyeball looks right, while the copied
tree is wrong.

Root cause analysis (AI)

util/layercopy/source_linux.go walks the source snapshot's overlay layer
directories directly instead of reading a mounted view.

readOverlayDir
merges the layers per directory: for each layer it checks
isOpaqueDir(layerDir) on that directory and stops descending to lower
layers when it is opaque. Whiteout entries in the listing are honored via
isWhiteout.

The bug: opacity is only consulted for the directory currently being listed.
When the walk recurses into a subdirectory, readOverlayDir(child) starts a
fresh merge across all layers with no knowledge that an ancestor was
opaque in an upper layer.

In overlayfs, a directory that is deleted and recreated gets
trusted.overlay.opaque on itself only β€” subdirectories created inside it
carry no marker of their own, because the kernel applies opacity to the whole
subtree. The walker doesn't:

  • listing the recreated dir a/: upper layer is opaque β†’ upper entries only.
    x.txt and any dropped subdir are absent β†’ correctly gone. βœ“
  • recursing into a/b/: upper b/ has no own opaque marker β†’ the walker
    falls through to the lower layer's a/b/ and unions the old y.txt
    back in. βœ—

realPath
has the same ancestor-blindness for single-file resolution: it walks layers
top-down per path, honoring a whiteout of the file itself but not an opaque
ancestor, so it also resolves to lower-layer content that should be hidden.

The code is unchanged on main at the time of writing.

Impact

Silent data corruption, discovered in production: our CI upgrades WordPress
plugins inside a container (the WP upgrader replaces plugin directories),
grafts the upgraded tree into a toolbox container and commits with git. Since
the v0.20 β†’ v0.21 engine upgrade, every upgrade commit silently re-added
files that upstream releases had removed β€” including files removed by a
security release. Six weeks of CMS-upgrade commits across multiple
repositories were affected before a plugin's own version self-check exposed
it. export to the host is unaffected, so local inspection showed a correct
tree while the engine-side copy did not.

Environment

  • Reproduced on: dagger v0.21.7 and v0.21.8 (CLI + engine, linux/amd64,
    rootful engine container, overlayfs)
  • Not reproducible on v0.20.8 (pre-layercopy)

Workaround

Use a kernel-resolved view instead of the layer walker:

  • WithMountedDirectory instead of WithDirectory where the tree is only
    needed inside execs, or
  • round-trip the directory through a tarball created from a mounted view.

Dagger version

v0.21.8 (image://registry.dagger.io/engine:v0.21.8) linux/amd64

Steps to reproduce

Reproduction (v0.21.8)

mkdir -p mini/a/b
echo old > mini/a/x.txt
echo old > mini/a/b/y.txt

# View inside the container that did the delete+recreate: correct.
dagger -c 'container | from alpine:3.20 | with-directory /data mini | with-exec sh,-c,"rm -rf /data/a && mkdir -p /data/a/b && echo new > /data/a/b/new.txt" | with-exec sh,-c,"find /data -type f" | stdout'
# /data/a/b/new.txt

# Same directory grafted into a fresh container: y.txt is resurrected.
dagger -c 'container | from alpine:3.20 | with-directory /graft $(container | from alpine:3.20 | with-directory /data mini | with-exec sh,-c,"rm -rf /data/a && mkdir -p /data/a/b && echo new > /data/a/b/new.txt" | directory /data) | with-exec sh,-c,"find /graft -type f" | stdout'
# /graft/a/b/new.txt
# /graft/a/b/y.txt      <-- deleted before the recreate, back with old content

Note the asymmetry: a/x.txt (direct child of the recreated dir) stays
deleted; a/b/y.txt (one level deeper) is resurrected. That asymmetry is the
fingerprint of the root cause below.

Log output

No response

Activity

  1. sipsma commented on Aug 5, 2026

    @sipsma
    Contributor

    Thanks for finding and reporting it! Fix is in progress, will link the PR for it soon

  2. sipsma commented on Aug 6, 2026

    @sipsma
    Contributor

    fix here #13848

  3. jeffreyzant commented on Aug 6, 2026

    @jeffreyzant
    Author

    Thanks man! That's some quick issue handling :)

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions