-
Notifications
You must be signed in to change notification settings - Fork 83
fix unshare infinite loop without CGO_ENABLED #590
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,3 +8,5 @@ package unshare | |
| // _containers_unshare(); | ||
| // } | ||
| import "C" | ||
|
|
||
| func unshareWithNoCGO(c *Cmd) {} | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,3 +23,5 @@ func init() { | |
| C.init() | ||
| } | ||
| } | ||
|
|
||
| func unshareWithNoCGO(c *Cmd) {} | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| //go:build linux && !cgo | ||
|
|
||
| package unshare | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "io" | ||
| "os" | ||
| "strconv" | ||
| "syscall" | ||
| ) | ||
|
|
||
| func unshareWithNoCGO(c *Cmd) { | ||
| if c.Cmd.SysProcAttr == nil { | ||
| c.Cmd.SysProcAttr = &syscall.SysProcAttr{} | ||
| } | ||
| attr := c.Cmd.SysProcAttr | ||
| if c.UnshareFlags&syscall.CLONE_NEWUSER != 0 { | ||
| attr.Cloneflags = uintptr(c.UnshareFlags) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nothing documents why we are not using “unshare” flags for AFAICS the two functions don’t accept exactly the same sets of flags, so even if this were correct for the callers we are currently anticipating, I’d expect the code here to report a failure if any unexpected flag value is set. (And, well, if This sort of applies generally — if we can do something natively using |
||
| attr.GidMappingsEnableSetgroups = c.GidMappingsEnableSetgroups | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This does nothing at all unless … |
||
| if c.Ctty != nil { | ||
| index := len(c.Cmd.ExtraFiles) | ||
| c.Cmd.ExtraFiles = append(c.Cmd.ExtraFiles, c.Ctty) | ||
| attr.Ctty = index | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This does nothing unless … |
||
| } | ||
| } | ||
| } | ||
|
|
||
| func parseIntContainersUnshareEnv(name string) int { | ||
| env := os.Getenv(name) | ||
| if env == "" { | ||
| return -1 | ||
| } | ||
| _ = os.Unsetenv(name) | ||
| v, err := strconv.Atoi(env) | ||
| if err != nil { | ||
| return -1 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The C code differentiates between “missing” and “invalid format” |
||
| } | ||
| return v | ||
| } | ||
|
|
||
| func init() { | ||
| flags := parseIntContainersUnshareEnv("_Containers-unshare") | ||
| if flags == -1 { | ||
| return | ||
| } | ||
|
|
||
| if pidFD := parseIntContainersUnshareEnv("_Containers-pid-pipe"); pidFD > -1 { | ||
| pidFile := os.NewFile(uintptr(pidFD), "") | ||
| _, err := fmt.Fprintf(pidFile, "%d", os.Getpid()) | ||
| if err != nil { | ||
| bailOnError(err, "Write pid failed") | ||
| } | ||
| _ = pidFile.Close() | ||
| } | ||
| if continueFD := parseIntContainersUnshareEnv("_Containers-continue-pipe"); continueFD > -1 { | ||
| continueFile := os.NewFile(uintptr(continueFD), "") | ||
| buf := make([]byte, 2048) | ||
| n, err := continueFile.Read(buf) | ||
| if err != nil && err != io.EOF { | ||
| bailOnError(err, "Read containers continue pipe") | ||
| } | ||
| if n > 0 { | ||
| bailOnError(fmt.Errorf(string(buf)), "Unexpected containers continue pipe read") | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Format string vulnerability. Nothing is all that unexpected about this error reporting path, the data exists only to be reported this way. |
||
| } | ||
| } | ||
|
|
||
| if setSid := parseIntContainersUnshareEnv("_Containers-setsid"); setSid == 1 { | ||
| _, err := syscall.Setsid() | ||
| if err != nil { | ||
| bailOnError(err, "Error during setsid") | ||
| } | ||
| } | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| if flags&syscall.CLONE_NEWUSER != 0 { | ||
| if err := syscall.Setresuid(0, 0, 0); err != nil { | ||
| bailOnError(err, "Setresuid failed") | ||
| } | ||
| if err := syscall.Setresgid(0, 0, 0); err != nil { | ||
| bailOnError(err, "Setresgid failed") | ||
| } | ||
|
Comment on lines
+76
to
+81
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why does the |
||
| } | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The C version does a two-stage |
||
| // Re-invoke the execve system call to obtain capabilities. | ||
| err := syscall.Exec("/proc/self/exe", os.Args, os.Environ()) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| if err != nil { | ||
| bailOnError(err, "syscall.Exec %s", "/proc/self/exe") | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All of this could happen only inside the
ifbelow.