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 <nalin@redhat.com>
This commit is contained in:
Nalin Dahyabhai 2025-09-09 12:58:48 -04:00
parent f297289c72
commit 76c18c8970
2 changed files with 27 additions and 0 deletions

View File

@ -12,6 +12,7 @@ import (
"os/signal" "os/signal"
"path/filepath" "path/filepath"
"runtime" "runtime"
"slices"
"strconv" "strconv"
"strings" "strings"
"sync" "sync"
@ -743,6 +744,15 @@ func runUsingChrootExecMain() {
os.Exit(1) os.Exit(1)
} }
// Set $PATH to the value for the container, so that when args[0] is not an absolute path,
// exec.Command() can find it using exec.LookPath().
for _, env := range slices.Backward(options.Spec.Process.Env) {
if val, ok := strings.CutPrefix(env, "PATH="); ok {
os.Setenv("PATH", val)
break
}
}
// Actually run the specified command. // Actually run the specified command.
cmd := exec.Command(args[0], args[1:]...) cmd := exec.Command(args[0], args[1:]...)
setPdeathsig(cmd) setPdeathsig(cmd)

View File

@ -8888,3 +8888,20 @@ _EOF
run_buildah --root=${TEST_SCRATCH_DIR}/newroot --storage-opt=imagestore=${TEST_SCRATCH_DIR}/root build --pull=never ${contextdir} run_buildah --root=${TEST_SCRATCH_DIR}/newroot --storage-opt=imagestore=${TEST_SCRATCH_DIR}/root build --pull=never ${contextdir}
run_buildah --root=${TEST_SCRATCH_DIR}/newroot --storage-opt=imagestore=${TEST_SCRATCH_DIR}/root build --pull=never --squash ${contextdir} run_buildah --root=${TEST_SCRATCH_DIR}/newroot --storage-opt=imagestore=${TEST_SCRATCH_DIR}/root build --pull=never --squash ${contextdir}
} }
@test "bud with exec-form RUN instruction" {
baseimage=busybox
_prefetch $baseimage
local contextdir=${TEST_SCRATCH_DIR}/context
mkdir -p "${contextdir}"
cat > "${contextdir}"/Dockerfile <<-EOF
FROM scratch AS mkdir
RUN --mount=type=bind,from="${baseimage}",destination=/usr ["busybox", "sh", "-x", "-c", "mkdir /brand-new-subdir"]
FROM "${baseimage}"
RUN --mount=type=bind,from=mkdir,destination=/mounted find /mounted -print
EOF
run_buildah build --layers=true "${contextdir}"
expect_output --substring /mounted/brand-new-subdir
run_buildah build --layers=false "${contextdir}"
expect_output --substring /mounted/brand-new-subdir
}