|
| 1 | +/** |
| 2 | +# SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 3 | +# SPDX-License-Identifier: Apache-2.0 |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | +**/ |
| 17 | + |
| 18 | +package disabledevicenodemodification |
| 19 | + |
| 20 | +import ( |
| 21 | + "bufio" |
| 22 | + "bytes" |
| 23 | + "errors" |
| 24 | + "fmt" |
| 25 | + "io" |
| 26 | + "os" |
| 27 | + "path/filepath" |
| 28 | + "strings" |
| 29 | + |
| 30 | + "github.com/urfave/cli/v2" |
| 31 | + |
| 32 | + "github.com/NVIDIA/nvidia-container-toolkit/internal/logger" |
| 33 | + "github.com/NVIDIA/nvidia-container-toolkit/internal/oci" |
| 34 | +) |
| 35 | + |
| 36 | +const ( |
| 37 | + nvidiaDriverParamsPath = "/proc/driver/nvidia/params" |
| 38 | +) |
| 39 | + |
| 40 | +type command struct { |
| 41 | + logger logger.Interface |
| 42 | +} |
| 43 | + |
| 44 | +type options struct { |
| 45 | + containerSpec string |
| 46 | +} |
| 47 | + |
| 48 | +// NewCommand constructs an disable-device-node-modification subcommand with the specified logger |
| 49 | +func NewCommand(logger logger.Interface) *cli.Command { |
| 50 | + c := command{ |
| 51 | + logger: logger, |
| 52 | + } |
| 53 | + return c.build() |
| 54 | +} |
| 55 | + |
| 56 | +func (m command) build() *cli.Command { |
| 57 | + cfg := options{} |
| 58 | + |
| 59 | + c := cli.Command{ |
| 60 | + Name: "disable-device-node-modification", |
| 61 | + Usage: "Ensure that the /proc/driver/nvidia/params file present in the container does not allow device node modifications.", |
| 62 | + Before: func(c *cli.Context) error { |
| 63 | + return m.validateFlags(c, &cfg) |
| 64 | + }, |
| 65 | + Action: func(c *cli.Context) error { |
| 66 | + return m.run(c, &cfg) |
| 67 | + }, |
| 68 | + } |
| 69 | + |
| 70 | + c.Flags = []cli.Flag{ |
| 71 | + &cli.StringFlag{ |
| 72 | + Name: "container-spec", |
| 73 | + Hidden: true, |
| 74 | + Usage: "Specify the path to the OCI container spec. If empty or '-' the spec will be read from STDIN", |
| 75 | + Destination: &cfg.containerSpec, |
| 76 | + }, |
| 77 | + } |
| 78 | + |
| 79 | + return &c |
| 80 | +} |
| 81 | + |
| 82 | +func (m command) validateFlags(c *cli.Context, cfg *options) error { |
| 83 | + return nil |
| 84 | +} |
| 85 | + |
| 86 | +func (m command) run(c *cli.Context, cfg *options) error { |
| 87 | + // TODO: Do we need to prefix the driver root? |
| 88 | + hostNvidiaParamsFile, err := os.Open(nvidiaDriverParamsPath) |
| 89 | + if errors.Is(err, os.ErrNotExist) { |
| 90 | + return nil |
| 91 | + } |
| 92 | + if err != nil { |
| 93 | + return fmt.Errorf("failed to load params file: %w", err) |
| 94 | + } |
| 95 | + defer hostNvidiaParamsFile.Close() |
| 96 | + |
| 97 | + s, err := oci.LoadContainerState(cfg.containerSpec) |
| 98 | + if err != nil { |
| 99 | + return fmt.Errorf("failed to load container state: %w", err) |
| 100 | + } |
| 101 | + |
| 102 | + containerRoot, err := s.GetContainerRoot() |
| 103 | + if err != nil { |
| 104 | + return fmt.Errorf("failed to determined container root: %w", err) |
| 105 | + } |
| 106 | + |
| 107 | + return m.updateNvidiaParamsFromReader(hostNvidiaParamsFile, containerRoot) |
| 108 | +} |
| 109 | + |
| 110 | +func (m command) updateNvidiaParamsFromReader(r io.Reader, containerRoot string) error { |
| 111 | + modifiedContents, err := m.getModifiedParamsFileContentsFromReader(r) |
| 112 | + if err != nil { |
| 113 | + return fmt.Errorf("failed to generate modified contents: %w", err) |
| 114 | + } |
| 115 | + if len(modifiedContents) == 0 { |
| 116 | + m.logger.Debugf("No modification required") |
| 117 | + return nil |
| 118 | + } |
| 119 | + return createParamsFileInContainer(containerRoot, modifiedContents) |
| 120 | +} |
| 121 | + |
| 122 | +// getModifiedParamsFileContentsFromReader returns the contents of a modified params file from the specified reader. |
| 123 | +func (m command) getModifiedParamsFileContentsFromReader(r io.Reader) ([]byte, error) { |
| 124 | + var modified bytes.Buffer |
| 125 | + scanner := bufio.NewScanner(r) |
| 126 | + |
| 127 | + var requiresModification bool |
| 128 | + for scanner.Scan() { |
| 129 | + line := scanner.Text() |
| 130 | + if strings.HasPrefix(line, "ModifyDeviceFiles: ") { |
| 131 | + if line == "ModifyDeviceFiles: 0" { |
| 132 | + m.logger.Debugf("Device node modification is already disabled") |
| 133 | + return nil, nil |
| 134 | + } |
| 135 | + if line == "ModifyDeviceFiles: 1" { |
| 136 | + line = "ModifyDeviceFiles: 0" |
| 137 | + requiresModification = true |
| 138 | + } |
| 139 | + } |
| 140 | + if _, err := modified.WriteString(line + "\n"); err != nil { |
| 141 | + return nil, fmt.Errorf("failed to create output buffer: %w", err) |
| 142 | + } |
| 143 | + } |
| 144 | + if err := scanner.Err(); err != nil { |
| 145 | + return nil, fmt.Errorf("failed to read params file: %w", err) |
| 146 | + } |
| 147 | + |
| 148 | + if !requiresModification { |
| 149 | + return nil, nil |
| 150 | + } |
| 151 | + |
| 152 | + return modified.Bytes(), nil |
| 153 | +} |
| 154 | + |
| 155 | +func createParamsFileInContainer(containerRoot string, contents []byte) error { |
| 156 | + if len(contents) == 0 { |
| 157 | + return nil |
| 158 | + } |
| 159 | + |
| 160 | + tempParamsFileName, err := createFileInTempfs("nvct-params", contents, 0o444) |
| 161 | + if err != nil { |
| 162 | + return fmt.Errorf("failed to create temporary file: %w", err) |
| 163 | + } |
| 164 | + |
| 165 | + if err := bindMountReadonly(tempParamsFileName, filepath.Join(containerRoot, nvidiaDriverParamsPath)); err != nil { |
| 166 | + return fmt.Errorf("failed to create temporary params file mount: %w", err) |
| 167 | + } |
| 168 | + |
| 169 | + return nil |
| 170 | +} |
| 171 | + |
| 172 | +// createFileInTempfs creates a file with the specified name, contents, and mode in a tmpfs. |
| 173 | +// A tmpfs is created at /tmp/nvct-emtpy-dir* with a size sufficient for the specified contents. |
| 174 | +func createFileInTempfs(name string, contents []byte, mode os.FileMode) (string, error) { |
| 175 | + tmpRoot, err := os.MkdirTemp("", "nvct-empty-dir*") |
| 176 | + if err != nil { |
| 177 | + return "", fmt.Errorf("failed to create temporary folder: %w", err) |
| 178 | + } |
| 179 | + if err := createTmpFs(tmpRoot, len(contents)); err != nil { |
| 180 | + return "", fmt.Errorf("failed to create tmpfs mount for params file: %w", err) |
| 181 | + } |
| 182 | + |
| 183 | + filename := filepath.Join(tmpRoot, name) |
| 184 | + fileInTempfs, err := os.Create(filename) |
| 185 | + if err != nil { |
| 186 | + return "", fmt.Errorf("failed to create temporary params file: %w", err) |
| 187 | + } |
| 188 | + defer fileInTempfs.Close() |
| 189 | + |
| 190 | + if _, err := fileInTempfs.Write(contents); err != nil { |
| 191 | + return "", fmt.Errorf("failed to write temporary params file: %w", err) |
| 192 | + } |
| 193 | + |
| 194 | + if err := fileInTempfs.Chmod(mode); err != nil { |
| 195 | + return "", fmt.Errorf("failed to set permissions on temporary params file: %w", err) |
| 196 | + } |
| 197 | + return filename, nil |
| 198 | +} |
0 commit comments