Skip to content

Commit 1e7a07a

Browse files
committed
Move SafeExec logic to utils package
Signed-off-by: Evan Lezar <[email protected]>
1 parent af89fb0 commit 1e7a07a

File tree

4 files changed

+44
-9
lines changed

4 files changed

+44
-9
lines changed

cmd/nvidia-cdi-hook/update-ldcache/update-ldcache.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ const (
4040
)
4141

4242
type command struct {
43+
utils.SafeExecer
4344
logger logger.Interface
4445
}
4546

@@ -52,7 +53,8 @@ type options struct {
5253
// NewCommand constructs an update-ldcache command with the specified logger
5354
func NewCommand(logger logger.Interface) *cli.Command {
5455
c := command{
55-
logger: logger,
56+
logger: logger,
57+
SafeExecer: utils.NewSafeExecer(logger),
5658
}
5759
return c.build()
5860
}
@@ -142,7 +144,7 @@ func (m command) run(c *cli.Context, cfg *options) error {
142144
// be configured to use a different config file by default.
143145
args = append(args, "-f", "/etc/ld.so.conf")
144146

145-
return m.SafeExec(ldconfigPath, args, nil)
147+
return m.Exec(ldconfigPath, args, nil)
146148
}
147149

148150
// resolveLDConfigPath determines the LDConfig path to use for the system.
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
# Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
**/
16+
17+
package utils
18+
19+
import "github.com/NVIDIA/nvidia-container-toolkit/internal/logger"
20+
21+
// A SafeExecer is used to Exec an application from a memfd to prevent possible
22+
// tampering.
23+
type SafeExecer struct {
24+
logger logger.Interface
25+
}
26+
27+
// NewSafeExecer creates a SafeExecer with the specified logger.
28+
func NewSafeExecer(logger logger.Interface) SafeExecer {
29+
return SafeExecer{
30+
logger: logger,
31+
}
32+
}

cmd/nvidia-cdi-hook/update-ldcache/safe-exec_linux.go renamed to cmd/nvidia-cdi-hook/utils/safe-exec_linux.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
# limitations under the License.
1515
**/
1616

17-
package ldcache
17+
package utils
1818

1919
import (
2020
"fmt"
@@ -25,11 +25,11 @@ import (
2525
"github.com/opencontainers/runc/libcontainer/dmz"
2626
)
2727

28-
// SafeExec attempts to clone the specified binary (as an memfd, for example) before executing it.
29-
func (m command) SafeExec(path string, args []string, envv []string) error {
28+
// Exec attempts to clone the specified binary (as an memfd, for example) before executing it.
29+
func (s SafeExecer) Exec(path string, args []string, envv []string) error {
3030
safeExe, err := cloneBinary(path)
3131
if err != nil {
32-
m.logger.Warningf("Failed to clone binary %q: %v; falling back to Exec", path, err)
32+
s.logger.Warningf("Failed to clone binary %q: %v; falling back to Exec", path, err)
3333
//nolint:gosec // TODO: Can we harden this so that there is less risk of command injection
3434
return syscall.Exec(path, args, envv)
3535
}

cmd/nvidia-cdi-hook/update-ldcache/safe-exec_other.go renamed to cmd/nvidia-cdi-hook/utils/safe-exec_other.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,14 @@
1717
# limitations under the License.
1818
**/
1919

20-
package ldcache
20+
package utils
2121

2222
import "syscall"
2323

24-
// SafeExec is not implemented on non-linux systems and forwards directly to the
24+
// Exec is not implemented on non-linux systems and forwards directly to the
2525
// Exec syscall.
26-
func (m *command) SafeExec(path string, args []string, envv []string) error {
26+
func (s SafeExecer) Exec(path string, args []string, envv []string) error {
27+
s.logger.Warningf("Cloning binary not implemented for binary %q; falling back to Exec", path)
2728
//nolint:gosec // TODO: Can we harden this so that there is less risk of command injection
2829
return syscall.Exec(path, args, envv)
2930
}

0 commit comments

Comments
 (0)