Skip to content

Commit 76c18c8

Browse files
committed
chroot: use $PATH when finding commands
Use the $PATH from the runtime config, if it includes one, so that when the command to run isn't an absolute path and the command isn't being processed by the shell, exec.Command()'s internal call to exec.LookPath() will find it. Signed-off-by: Nalin Dahyabhai <[email protected]>
1 parent f297289 commit 76c18c8

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

chroot/run_common.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"os/signal"
1313
"path/filepath"
1414
"runtime"
15+
"slices"
1516
"strconv"
1617
"strings"
1718
"sync"
@@ -743,6 +744,15 @@ func runUsingChrootExecMain() {
743744
os.Exit(1)
744745
}
745746

747+
// Set $PATH to the value for the container, so that when args[0] is not an absolute path,
748+
// exec.Command() can find it using exec.LookPath().
749+
for _, env := range slices.Backward(options.Spec.Process.Env) {
750+
if val, ok := strings.CutPrefix(env, "PATH="); ok {
751+
os.Setenv("PATH", val)
752+
break
753+
}
754+
}
755+
746756
// Actually run the specified command.
747757
cmd := exec.Command(args[0], args[1:]...)
748758
setPdeathsig(cmd)

tests/bud.bats

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8888,3 +8888,20 @@ _EOF
88888888
run_buildah --root=${TEST_SCRATCH_DIR}/newroot --storage-opt=imagestore=${TEST_SCRATCH_DIR}/root build --pull=never ${contextdir}
88898889
run_buildah --root=${TEST_SCRATCH_DIR}/newroot --storage-opt=imagestore=${TEST_SCRATCH_DIR}/root build --pull=never --squash ${contextdir}
88908890
}
8891+
8892+
@test "bud with exec-form RUN instruction" {
8893+
baseimage=busybox
8894+
_prefetch $baseimage
8895+
local contextdir=${TEST_SCRATCH_DIR}/context
8896+
mkdir -p "${contextdir}"
8897+
cat > "${contextdir}"/Dockerfile <<-EOF
8898+
FROM scratch AS mkdir
8899+
RUN --mount=type=bind,from="${baseimage}",destination=/usr ["busybox", "sh", "-x", "-c", "mkdir /brand-new-subdir"]
8900+
FROM "${baseimage}"
8901+
RUN --mount=type=bind,from=mkdir,destination=/mounted find /mounted -print
8902+
EOF
8903+
run_buildah build --layers=true "${contextdir}"
8904+
expect_output --substring /mounted/brand-new-subdir
8905+
run_buildah build --layers=false "${contextdir}"
8906+
expect_output --substring /mounted/brand-new-subdir
8907+
}

0 commit comments

Comments
 (0)