diff --git a/cmd/nvidia-container-runtime-hook/container_config.go b/cmd/nvidia-container-runtime-hook/container_config.go index 531b2b42..20a3b09f 100644 --- a/cmd/nvidia-container-runtime-hook/container_config.go +++ b/cmd/nvidia-container-runtime-hook/container_config.go @@ -10,6 +10,7 @@ import ( "strings" "github.com/NVIDIA/nvidia-container-toolkit/internal/config/image" + "github.com/opencontainers/runtime-spec/specs-go" "golang.org/x/mod/semver" ) @@ -130,7 +131,7 @@ func isPrivileged(s *Spec) bool { } var caps []string - // If v1.1.0-rc1 <= OCI version < v1.0.0-rc5 parse s.Process.Capabilities as: + // If v1.0.0-rc1 <= OCI version < v1.0.0-rc5 parse s.Process.Capabilities as: // github.com/opencontainers/runtime-spec/blob/v1.0.0-rc1/specs-go/config.go#L30-L54 rc1cmp := semver.Compare("v"+*s.Version, "v1.0.0-rc1") rc5cmp := semver.Compare("v"+*s.Version, "v1.0.0-rc5") @@ -139,28 +140,31 @@ func isPrivileged(s *Spec) bool { if err != nil { log.Panicln("could not decode Process.Capabilities in OCI spec:", err) } - // Otherwise, parse s.Process.Capabilities as: - // github.com/opencontainers/runtime-spec/blob/v1.0.0/specs-go/config.go#L30-L54 - } else { - var lc LinuxCapabilities - err := json.Unmarshal(*s.Process.Capabilities, &lc) - if err != nil { - log.Panicln("could not decode Process.Capabilities in OCI spec:", err) + for _, c := range caps { + if c == capSysAdmin { + return true + } } - // We only make sure that the bounding capabibility set has - // CAP_SYS_ADMIN. This allows us to make sure that the container was - // actually started as '--privileged', but also allow non-root users to - // access the privileged NVIDIA capabilities. - caps = lc.Bounding + return false } - for _, c := range caps { - if c == capSysAdmin { - return true - } + // Otherwise, parse s.Process.Capabilities as: + // github.com/opencontainers/runtime-spec/blob/v1.0.0/specs-go/config.go#L30-L54 + process := specs.Process{ + Env: s.Process.Env, } - return false + err := json.Unmarshal(*s.Process.Capabilities, &process.Capabilities) + if err != nil { + log.Panicln("could not decode Process.Capabilities in OCI spec:", err) + } + + fullSpec := specs.Spec{ + Version: *s.Version, + Process: &process, + } + + return image.IsPrivileged(&fullSpec) } func getDevicesFromEnvvar(image image.CUDA, swarmResourceEnvvars []string) *string { diff --git a/internal/config/image/privileged.go b/internal/config/image/privileged.go new file mode 100644 index 00000000..a54598d6 --- /dev/null +++ b/internal/config/image/privileged.go @@ -0,0 +1,43 @@ +/** +# Copyright (c) NVIDIA CORPORATION. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +**/ + +package image + +import ( + "github.com/opencontainers/runtime-spec/specs-go" +) + +const ( + capSysAdmin = "CAP_SYS_ADMIN" +) + +// IsPrivileged returns true if the container is a privileged container. +func IsPrivileged(s *specs.Spec) bool { + if s.Process.Capabilities == nil { + return false + } + + // We only make sure that the bounding capabibility set has + // CAP_SYS_ADMIN. This allows us to make sure that the container was + // actually started as '--privileged', but also allow non-root users to + // access the privileged NVIDIA capabilities. + for _, c := range s.Process.Capabilities.Bounding { + if c == capSysAdmin { + return true + } + } + return false +}