Skip to content

dockerfile: don't nil-deref llbCaps on read-write bind mounts - #15206

Open
santhiprakash wants to merge 1 commit into
dagger:mainfrom
santhiprakash:fix-dockerbuild-rw-bind-mount-nil-caps
Open

santhiprakash wants to merge 1 commit into
dagger:mainfrom
santhiprakash:fix-dockerbuild-rw-bind-mount-nil-caps

Conversation

@santhiprakash

Copy link
Copy Markdown

Fixes #15198

Problem

Directory.dockerBuild on a Dockerfile with a read-write bind mount
(RUN --mount=type=bind,from=<stage>,...,rw) panics the engine:

panic: runtime error: invalid memory address or nil pointer dereference
apicaps.(*CapSet).Supports(0x0, ...)               caps.go:109
dockerfile2llb.dispatchRunMounts(...)             convert_runmount.go:104
dockerfile2llb.dispatchRun / dispatch / toDispatchState / Dockerfile2LLB

The client just sees unexpected EOF, and on restart the engine reports
dagql persistence store marked unclean; wiping and cold-starting, losing
its cache. Read-only bind mounts are unaffected — only rw reaches the
buggy call.

Root cause

dockerBuild in core/container.go builds dockerfile2llb.ConvertOpt
with neither Client nor LLBCaps set. toDispatchState only fills
opt.LLBCaps from opt.Client.BuildOpts().LLBCaps when opt.Client != nil, so on the in-engine path dispatchOpt.llbCaps is nil — and
dispatchRunMounts called opt.llbCaps.Supports(...) unguarded for any
non-readonly bind mount.

Nil llbCaps is an expected state on this path: toDispatchState already
skips llb.WithCaps when opt.LLBCaps == nil, and every other caps check
in the package is nil-guarded (convert.go — including one that comments
// cfg.opt.llbCaps can be nil in unit tests). This was the only
unguarded site.

Fix

Nil-guard the Supports call like its siblings: when caps are unknown,
skip llb.ForceNoOutput and let the mount keep its output. The mount
still works; ForceNoOutput is only the no-output optimization for
writable bind mounts.

Callers that do advertise caps (buildkit's dockerui path) keep the exact
same behavior as before.

Verification

New internal/buildkit/frontend/dockerfile/dockerfile2llb/convert_runmount_test.go:

  • TestDockerfile2LLBBindMountRWWithoutLLBCaps drives Dockerfile2LLB
    with ConvertOpt{} (no Client/LLBCaps — the engine's shape) on a
    Dockerfile with RUN --mount=type=bind,from=a,source=/d,target=/y,rw.
    Before the fix it panics at the exact stack above
    (caps.go:109 <- convert_runmount.go:104 <- dispatchRun <- Dockerfile2LLB);
    after, it produces a BIND mount at /y that is not readonly and keeps
    its output (Output != pb.SkipOutput).
  • TestDockerfile2LLBBindMountRWWithLLBCaps advertises
    CapExecMountBindReadWriteNoOutput and asserts ForceNoOutput still
    applies (Output == pb.SkipOutput), so the guard does not disable the
    optimization when caps are known.

Run on this host:

go test ./internal/buildkit/frontend/dockerfile/dockerfile2llb/ -run 'TestDockerfile2LLBBindMountRW' -count=1 -v
--- PASS: TestDockerfile2LLBBindMountRWWithoutLLBCaps (0.00s)
--- PASS: TestDockerfile2LLBBindMountRWWithLLBCaps (0.00s)

plus gofmt, go vet, and go build on the touched package. The
repo-native dagger api call engine-dev test integration path needs a
Docker engine, which this host doesn't have; the reporter's engine-level
repro should be covered by the new test's direct Dockerfile2LLB call.

Risks

The only behavioral delta is in the nil-caps path (in-engine
dockerBuild, or any caller invoking Dockerfile2LLB without a
dockerui.Client): a read-write bind mount there keeps its output rather
than crashing. With caps advertised, output is unchanged.

The engine's dockerBuild path calls Dockerfile2LLB without a dockerui
Client, so opt.LLBCaps is never populated and dispatchOpt.llbCaps stays
nil. dispatchRunMounts then called llbCaps.Supports() unconditionally for
any non-readonly bind mount, panicking on a nil receiver. Guard the check
like the sibling llbCaps checks in convert.go: with unknown caps the
mount keeps its output instead of applying ForceNoOutput.

Signed-off-by: Santhi Prakash <[email protected]>
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.

Engine panics (nil CapSet) on RUN --mount=type=bind,…,rw in Directory.dockerBuild, then wipes its cache on restart

1 participant