Skip to content

Commit 9570d20

Browse files
authored
Merge pull request #4099 from ark-j/main
Fix stopping container when setup using setuid bit
2 parents 625b674 + 0bf31d0 commit 9570d20

File tree

4 files changed

+30
-1
lines changed

4 files changed

+30
-1
lines changed

cmd/nerdctl/main.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,6 @@ func initRootCmdFlags(rootCmd *cobra.Command, tomlPath string) (*pflag.FlagSet,
190190
}
191191

192192
func newApp() (*cobra.Command, error) {
193-
194193
tomlPath := ncdefaults.NerdctlTOML()
195194
if v, ok := os.LookupEnv("NERDCTL_TOML"); ok {
196195
tomlPath = v
@@ -217,6 +216,10 @@ Config file ($NERDCTL_TOML): %s
217216
return nil, err
218217
}
219218

219+
if err := resetSavedSETUID(); err != nil {
220+
return nil, err
221+
}
222+
220223
rootCmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error {
221224
globalOptions, err := helpers.ProcessRootCmdFlags(cmd)
222225
if err != nil {

cmd/nerdctl/main_freebsd.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,8 @@ func appNeedsRootlessParentMain(cmd *cobra.Command, args []string) bool {
2727
func addApparmorCommand(rootCmd *cobra.Command) {
2828
// NOP
2929
}
30+
31+
func resetSavedSETUID() error {
32+
// NOP
33+
return nil
34+
}

cmd/nerdctl/main_linux.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package main
1818

1919
import (
2020
"github.com/spf13/cobra"
21+
"golang.org/x/sys/unix"
2122

2223
"github.com/containerd/nerdctl/v2/cmd/nerdctl/apparmor"
2324
"github.com/containerd/nerdctl/v2/pkg/rootlessutil"
@@ -66,3 +67,18 @@ func appNeedsRootlessParentMain(cmd *cobra.Command, args []string) bool {
6667
func addApparmorCommand(rootCmd *cobra.Command) {
6768
rootCmd.AddCommand(apparmor.Command())
6869
}
70+
71+
// resetSavedSETUID drops the saved UID of a setuid-root process to the original real UID.
72+
// This ensures the process cannot regain root privileges later.
73+
// It only performs the operation if the process is currently running with effective UID 0 (root)
74+
// and was started by a non-root user (i.e., real UID != effective UID).
75+
// For more info see issue https://github.com/containerd/nerdctl/issues/4098
76+
func resetSavedSETUID() error {
77+
var err error
78+
uid := unix.Getuid()
79+
euid := unix.Geteuid()
80+
if uid != euid && euid == 0 {
81+
err = unix.Setresuid(0, 0, uid)
82+
}
83+
return err
84+
}

cmd/nerdctl/main_windows.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,8 @@ func appNeedsRootlessParentMain(cmd *cobra.Command, args []string) bool {
2727
func addApparmorCommand(rootCmd *cobra.Command) {
2828
// NOP
2929
}
30+
31+
func resetSavedSETUID() error {
32+
// NOP
33+
return nil
34+
}

0 commit comments

Comments
 (0)