Skip to content

Commit 646b0c4

Browse files
committed
fix(nvidia-validator): don't leak PATH from container to chroot env
Signed-off-by: sinanmohd <[email protected]>
1 parent 0dd238e commit 646b0c4

File tree

3 files changed

+40
-11
lines changed

3 files changed

+40
-11
lines changed

Dockerfile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
FROM golang:1.24.3
2+
COPY ./ /app
3+
WORKDIR /app
4+
RUN go build ./cmd/nvidia-validator/
5+
6+
7+
FROM nvcr.io/nvidia/cloud-native/gpu-operator-validator:v25.3.0
8+
COPY --from=0 /app/nvidia-validator /usr/bin/nvidia-validator

cmd/nvidia-validator/driver.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ func getDriverInfo(isHostDriver bool, hostRoot string, driverInstallDir string,
4444
isHostDriver: true,
4545
hostRoot: hostRoot,
4646
driverRoot: hostRoot,
47-
driverRootCtrPath: "/host",
47+
driverRootCtrPath: hostMountPath,
4848
devRoot: hostRoot,
49-
devRootCtrPath: "/host",
49+
devRootCtrPath: hostMountPath,
5050
}
5151
}
5252

@@ -56,7 +56,7 @@ func getDriverInfo(isHostDriver bool, hostRoot string, driverInstallDir string,
5656
devRoot = root(driverInstallDirCtrPath).getDevRoot()
5757
if devRoot == "/" {
5858
devRoot = hostRoot
59-
devRootCtrPath = "/host"
59+
devRootCtrPath = hostMountPath
6060
} else {
6161
devRoot = driverInstallDir
6262
devRootCtrPath = "/driver-root"

cmd/nvidia-validator/main.go

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,8 @@ const (
219219
wslNvidiaSMIPath = "/usr/lib/wsl/lib/nvidia-smi"
220220
// shell indicates what shell to use when invoking commands in a subprocess
221221
shell = "sh"
222+
// path where host is mounted
223+
hostMountPath = "/host"
222224
)
223225

224226
func main() {
@@ -608,6 +610,18 @@ func runCommand(command string, args []string, silent bool) error {
608610
return cmd.Run()
609611
}
610612

613+
func getHostCommandPath(command string) (string, error) {
614+
args := []string{hostMountPath, "/bin/sh", "-l", "-c", fmt.Sprintf("realpath $(which %s)", command)}
615+
cmd := exec.Command("chroot", args...)
616+
617+
path, err := cmd.Output()
618+
if err != nil {
619+
return "", err
620+
}
621+
622+
return strings.TrimSpace(string(path)), nil
623+
}
624+
611625
func runCommandWithWait(command string, args []string, sleepSeconds int, silent bool) error {
612626
for {
613627
cmd := exec.Command(command, args...)
@@ -698,20 +712,26 @@ func isDriverManagedByOperator(ctx context.Context) (bool, error) {
698712

699713
func validateHostDriver(silent bool) error {
700714
log.Info("Attempting to validate a pre-installed driver on the host")
701-
if fileInfo, err := os.Lstat(filepath.Join("/host", wslNvidiaSMIPath)); err == nil && fileInfo.Size() != 0 {
715+
if fileInfo, err := os.Lstat(filepath.Join(hostMountPath, wslNvidiaSMIPath)); err == nil && fileInfo.Size() != 0 {
702716
log.Infof("WSL2 system detected, assuming driver is pre-installed")
703717
disableDevCharSymlinkCreation = true
704718
return nil
705719
}
706-
fileInfo, err := os.Lstat("/host/usr/bin/nvidia-smi")
720+
721+
nvidiaSMIPath, err := getHostCommandPath("nvidia-smi")
707722
if err != nil {
708-
return fmt.Errorf("no 'nvidia-smi' file present on the host: %w", err)
723+
return fmt.Errorf("no 'nvidia-smi' executable present on the host $PATH: %w", err)
724+
}
725+
726+
fileInfo, err := os.Lstat(filepath.Join(hostMountPath, nvidiaSMIPath))
727+
if err != nil {
728+
return fmt.Errorf("failed to stat 'nvidia-smi' path on the host: %w", err)
709729
}
710730
if fileInfo.Size() == 0 {
711731
return fmt.Errorf("empty 'nvidia-smi' file found on the host")
712732
}
713733
command := "chroot"
714-
args := []string{"/host", "nvidia-smi"}
734+
args := []string{hostMountPath, nvidiaSMIPath}
715735

716736
return runCommand(command, args, silent)
717737
}
@@ -770,7 +790,7 @@ func (d *Driver) runValidation(silent bool) (driverInfo, error) {
770790
err := validateHostDriver(silent)
771791
if err == nil {
772792
log.Info("Detected a pre-installed driver on the host")
773-
return getDriverInfo(true, hostRootFlag, hostRootFlag, "/host"), nil
793+
return getDriverInfo(true, hostRootFlag, hostRootFlag, hostMountPath), nil
774794
}
775795

776796
err = validateDriverContainer(silent, d.ctx)
@@ -848,7 +868,7 @@ func createDevCharSymlinks(driverInfo driverInfo, disableDevCharSymlinkCreation
848868
// either '/host' or '/driver-root', both paths would exist in the validation container.
849869
driverRootCtrPath := driverInstallDirCtrPathFlag
850870
if driverInfo.isHostDriver {
851-
driverRootCtrPath = "/host"
871+
driverRootCtrPath = hostMountPath
852872
}
853873

854874
// We now create the symlinks in /dev/char.
@@ -1560,8 +1580,9 @@ func (v *VGPUManager) runValidation(silent bool) (hostDriver bool, err error) {
15601580
args := []string{"/run/nvidia/driver", "nvidia-smi"}
15611581

15621582
// check if driver is pre-installed on the host and use host path for validation
1563-
if _, err := os.Lstat("/host/usr/bin/nvidia-smi"); err == nil {
1564-
args = []string{"/host", "nvidia-smi"}
1583+
nvidiaSMIPath, err := getHostCommandPath("nvidia-smi")
1584+
if err == nil {
1585+
args = []string{hostMountPath, nvidiaSMIPath}
15651586
hostDriver = true
15661587
}
15671588

0 commit comments

Comments
 (0)