Skip to content

Commit 49aa5d7

Browse files
committed
Add a hook to disable device node creation in a container
If required, this hook creates a modified params file (with ModifyDeviceFiles: 0) in a tmpfs and mounts this over /proc/driver/nvidia/params. This prevents device node creation when running tools such as nvidia-smi. In general the creation of these devices is cosmetic as a container does not have the required cgroup access for the devices. Signed-off-by: Evan Lezar <[email protected]>
1 parent 7dc93d6 commit 49aa5d7

File tree

5 files changed

+364
-0
lines changed

5 files changed

+364
-0
lines changed

cmd/nvidia-cdi-hook/commands/commands.go

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"github.com/NVIDIA/nvidia-container-toolkit/cmd/nvidia-cdi-hook/chmod"
2323
symlinks "github.com/NVIDIA/nvidia-container-toolkit/cmd/nvidia-cdi-hook/create-symlinks"
2424
"github.com/NVIDIA/nvidia-container-toolkit/cmd/nvidia-cdi-hook/cudacompat"
25+
disabledevicenodemodification "github.com/NVIDIA/nvidia-container-toolkit/cmd/nvidia-cdi-hook/disable-device-node-modification"
2526
ldcache "github.com/NVIDIA/nvidia-container-toolkit/cmd/nvidia-cdi-hook/update-ldcache"
2627
"github.com/NVIDIA/nvidia-container-toolkit/internal/logger"
2728
)
@@ -34,5 +35,6 @@ func New(logger logger.Interface) []*cli.Command {
3435
symlinks.NewCommand(logger),
3536
chmod.NewCommand(logger),
3637
cudacompat.NewCommand(logger),
38+
disabledevicenodemodification.NewCommand(logger),
3739
}
3840
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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+
"bytes"
22+
"testing"
23+
24+
testlog "github.com/sirupsen/logrus/hooks/test"
25+
"github.com/stretchr/testify/require"
26+
)
27+
28+
func TestGetModifiedParamsFileContentsFromReader(t *testing.T) {
29+
logger, _ := testlog.NewNullLogger()
30+
testCases := map[string]struct {
31+
contents []byte
32+
expectedError error
33+
expectedContents []byte
34+
}{
35+
"no contents": {
36+
contents: nil,
37+
expectedError: nil,
38+
expectedContents: nil,
39+
},
40+
"other contents are ignored": {
41+
contents: []byte(`# Some other content
42+
that we don't care about
43+
`),
44+
expectedError: nil,
45+
expectedContents: nil,
46+
},
47+
"already zero requires no modification": {
48+
contents: []byte("ModifyDeviceFiles: 0"),
49+
expectedError: nil,
50+
expectedContents: nil,
51+
},
52+
"leading spaces require no modification": {
53+
contents: []byte(" ModifyDeviceFiles: 1"),
54+
},
55+
"Trailing spaces require no modification": {
56+
contents: []byte("ModifyDeviceFiles: 1 "),
57+
},
58+
"Not 1 require no modification": {
59+
contents: []byte("ModifyDeviceFiles: 11"),
60+
},
61+
"single line requires modification": {
62+
contents: []byte("ModifyDeviceFiles: 1"),
63+
expectedError: nil,
64+
expectedContents: []byte("ModifyDeviceFiles: 0\n"),
65+
},
66+
"single line with trailing newline requires modification": {
67+
contents: []byte("ModifyDeviceFiles: 1\n"),
68+
expectedError: nil,
69+
expectedContents: []byte("ModifyDeviceFiles: 0\n"),
70+
},
71+
"other content is maintained": {
72+
contents: []byte(`ModifyDeviceFiles: 1
73+
other content
74+
that
75+
is maintained`),
76+
expectedError: nil,
77+
expectedContents: []byte(`ModifyDeviceFiles: 0
78+
other content
79+
that
80+
is maintained
81+
`),
82+
},
83+
}
84+
85+
for description, tc := range testCases {
86+
t.Run(description, func(t *testing.T) {
87+
c := command{
88+
logger: logger,
89+
}
90+
contents, err := c.getModifiedParamsFileContentsFromReader(bytes.NewReader(tc.contents))
91+
require.EqualValues(t, tc.expectedError, err)
92+
require.EqualValues(t, string(tc.expectedContents), string(contents))
93+
})
94+
}
95+
96+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//go:build linux
2+
// +build linux
3+
4+
/**
5+
# SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
6+
# SPDX-License-Identifier: Apache-2.0
7+
#
8+
# Licensed under the Apache License, Version 2.0 (the "License");
9+
# you may not use this file except in compliance with the License.
10+
# You may obtain a copy of the License at
11+
#
12+
# http://www.apache.org/licenses/LICENSE-2.0
13+
#
14+
# Unless required by applicable law or agreed to in writing, software
15+
# distributed under the License is distributed on an "AS IS" BASIS,
16+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
# See the License for the specific language governing permissions and
18+
# limitations under the License.
19+
**/
20+
21+
package disabledevicenodemodification
22+
23+
import (
24+
"fmt"
25+
26+
"golang.org/x/sys/unix"
27+
)
28+
29+
func createTmpFs(target string, size int) error {
30+
return unix.Mount("tmpfs", target, "tmpfs", 0, fmt.Sprintf("size=%d", size))
31+
}
32+
33+
func bindMountReadonly(source string, target string) error {
34+
return unix.Mount(source, target, "", unix.MS_BIND|unix.MS_RDONLY|unix.MS_NOSYMFOLLOW, "")
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//go:build !linux
2+
// +build !linux
3+
4+
/**
5+
# SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
6+
# SPDX-License-Identifier: Apache-2.0
7+
#
8+
# Licensed under the Apache License, Version 2.0 (the "License");
9+
# you may not use this file except in compliance with the License.
10+
# You may obtain a copy of the License at
11+
#
12+
# http://www.apache.org/licenses/LICENSE-2.0
13+
#
14+
# Unless required by applicable law or agreed to in writing, software
15+
# distributed under the License is distributed on an "AS IS" BASIS,
16+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
# See the License for the specific language governing permissions and
18+
# limitations under the License.
19+
**/
20+
21+
package disabledevicenodemodification
22+
23+
import (
24+
"fmt"
25+
)
26+
27+
func createTmpFs(target string, size int) error {
28+
return fmt.Errorf("not supported")
29+
}
30+
31+
func bindMountReadonly(source string, target string) error {
32+
return fmt.Errorf("not supported")
33+
}

0 commit comments

Comments
 (0)