mirror of
				https://github.com/NVIDIA/nvidia-container-toolkit
				synced 2025-06-26 18:18:24 +00:00 
			
		
		
		
	Move check for privileged images to config/image/ package
Signed-off-by: Evan Lezar <elezar@nvidia.com>
This commit is contained in:
		
							parent
							
								
									86dd046c7c
								
							
						
					
					
						commit
						936fad1d04
					
				| @ -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,30 +140,33 @@ 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) | ||||
| 		} | ||||
| 		// 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 | ||||
| 	} | ||||
| 
 | ||||
| 		for _, c := range caps { | ||||
| 			if c == capSysAdmin { | ||||
| 				return true | ||||
| 			} | ||||
| 		} | ||||
| 
 | ||||
| 		return false | ||||
| 	} | ||||
| 
 | ||||
| 	// 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, | ||||
| 	} | ||||
| 
 | ||||
| 	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 { | ||||
| 	// We check if the image has at least one of the Swarm resource envvars defined and use this
 | ||||
| 	// if specified.
 | ||||
|  | ||||
							
								
								
									
										43
									
								
								internal/config/image/privileged.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										43
									
								
								internal/config/image/privileged.go
									
									
									
									
									
										Normal file
									
								
							| @ -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 | ||||
| } | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user