-
Notifications
You must be signed in to change notification settings - Fork 160
[WIP] validation: add test for NSProcInPath #613
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
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
| "os/exec" | ||
| "runtime" | ||
| "time" | ||
|
|
||
| "github.com/mndrix/tap-go" | ||
| rspec "github.com/opencontainers/runtime-spec/specs-go" | ||
| "github.com/opencontainers/runtime-tools/validation/util" | ||
| ) | ||
|
|
||
| func getRuntimeToolsNamespace(ns string) string { | ||
| // Deal with exceptional cases of "net" and "mnt", because those strings | ||
| // cannot be recognized by mapStrToNamespace(), which actually expects | ||
| // "network" and "mount" respectively. | ||
| switch ns { | ||
| case "net": | ||
| return "network" | ||
| case "mnt": | ||
| return "mount" | ||
| } | ||
|
|
||
| // In other cases, return just the original string | ||
| return ns | ||
| } | ||
|
|
||
| func testNamespacePath(t *tap.T, ns string, unshareOpt string) error { | ||
| // Calling 'unshare' (part of util-linux) is easier than doing it from | ||
| // Golang: mnt namespaces cannot be unshared from multithreaded | ||
| // programs. | ||
| cmd := exec.Command("unshare", unshareOpt, "--fork", "sleep", "10000") | ||
| err := cmd.Start() | ||
| if err != nil { | ||
| return fmt.Errorf("cannot run unshare: %s", err) | ||
| } | ||
| defer func() { | ||
| if cmd.Process != nil { | ||
| cmd.Process.Kill() | ||
| } | ||
| cmd.Wait() | ||
| }() | ||
| if cmd.Process == nil { | ||
| return fmt.Errorf("process failed to start") | ||
| } | ||
| unsharePid := cmd.Process.Pid | ||
|
|
||
| // Wait until 'unshare' switched its namespace | ||
| // TODO: avoid synchronisation with sleeps. | ||
| time.Sleep(time.Second) | ||
|
|
||
| specialChildren := "" | ||
| if ns == "pid" { | ||
| // Unsharing pidns does not move the process into the new | ||
| // pidns but the next forked process. 'unshare' is called with | ||
| // '--fork' so the pidns will be fully created and populated | ||
| // with a pid 1. | ||
| // | ||
| // However, finding out the pid of the child process is not | ||
| // trivial: it would require to parse | ||
| // /proc/$pid/task/$tid/children but that only works on kernels | ||
| // with CONFIG_PROC_CHILDREN (not all distros have that). | ||
| // | ||
| // It is easier to look at /proc/$pid/ns/pid_for_children on | ||
| // the parent process. Available since Linux 4.12. | ||
| specialChildren = "_for_children" | ||
| } | ||
| unshareNsPath := fmt.Sprintf("/proc/%d/ns/%s", unsharePid, ns+specialChildren) | ||
| unshareNsInode, err := os.Readlink(unshareNsPath) | ||
| if err != nil { | ||
| return fmt.Errorf("cannot read namespace link for the unshare process: %s", err) | ||
| } | ||
|
|
||
| g := util.GetDefaultGenerator() | ||
|
|
||
| rtns := getRuntimeToolsNamespace(ns) | ||
| g.AddOrReplaceLinuxNamespace(rtns, unshareNsPath) | ||
|
|
||
| // The spec is not clear about userns mappings when reusing an | ||
| // existing userns. | ||
| // See https://github.com/opencontainers/runtime-spec/issues/961 | ||
| //if ns == "user" { | ||
| // g.AddLinuxUIDMapping(uint32(1000), uint32(0), uint32(1000)) | ||
| // g.AddLinuxGIDMapping(uint32(1000), uint32(0), uint32(1000)) | ||
| //} | ||
|
|
||
| err = util.RuntimeOutsideValidate(g, func(config *rspec.Spec, state *rspec.State) error { | ||
| containerNsPath := fmt.Sprintf("/proc/%d/ns/%s", state.Pid, ns) | ||
| containerNsInode, err := os.Readlink(containerNsPath) | ||
| if err != nil { | ||
| out, err2 := exec.Command("sh", "-c", fmt.Sprintf("ls -la /proc/%d/ns/", state.Pid)).CombinedOutput() | ||
| return fmt.Errorf("cannot read namespace link for the container process: %s\n%v\n%v", err, err2, out) | ||
| } | ||
| if containerNsInode != unshareNsInode { | ||
| return fmt.Errorf("expected: %q, found: %q", unshareNsInode, containerNsInode) | ||
| } | ||
| return nil | ||
| }) | ||
|
|
||
| return err | ||
| } | ||
|
|
||
| func main() { | ||
| t := tap.New() | ||
| t.Header(0) | ||
|
|
||
| cases := []struct { | ||
| name string | ||
| unshareOpt string | ||
| }{ | ||
| {"cgroup", "--cgroup"}, | ||
| {"ipc", "--ipc"}, | ||
| {"mnt", "--mount"}, | ||
| {"net", "--net"}, | ||
| {"pid", "--pid"}, | ||
| {"user", "--user"}, | ||
| {"uts", "--uts"}, | ||
| } | ||
|
|
||
| for _, c := range cases { | ||
| if "linux" != runtime.GOOS { | ||
| t.Skip(1, fmt.Sprintf("linux-specific namespace test: %s", c)) | ||
| } | ||
|
|
||
| err := testNamespacePath(t, c.name, c.unshareOpt) | ||
| t.Ok(err == nil, fmt.Sprintf("set %s namespace by path", c.name)) | ||
| if err != nil { | ||
| diagnostic := map[string]string{ | ||
| "error": err.Error(), | ||
| } | ||
| t.YAML(diagnostic) | ||
| } | ||
| } | ||
|
|
||
| t.AutoPlan() | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
The sleep processes are surviving the death of the
unshareparent unfortunately.I tried
cmd.Process.Signal(syscall.SIGTERM)but it does not help because: