-
Notifications
You must be signed in to change notification settings - Fork 766
feature: support selinux #4639
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?
feature: support selinux #4639
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 |
|---|---|---|
|
|
@@ -26,9 +26,15 @@ import ( | |
|
|
||
| "gotest.tools/v3/assert" | ||
|
|
||
| "github.com/containerd/nerdctl/mod/tigron/expect" | ||
| "github.com/containerd/nerdctl/mod/tigron/require" | ||
| "github.com/containerd/nerdctl/mod/tigron/test" | ||
| "github.com/containerd/nerdctl/mod/tigron/tig" | ||
|
|
||
| "github.com/containerd/nerdctl/v2/pkg/apparmorutil" | ||
| "github.com/containerd/nerdctl/v2/pkg/rootlessutil" | ||
| "github.com/containerd/nerdctl/v2/pkg/testutil" | ||
| "github.com/containerd/nerdctl/v2/pkg/testutil/nerdtest" | ||
| ) | ||
|
|
||
| func getCapEff(base *testutil.Base, args ...string) uint64 { | ||
|
|
@@ -186,6 +192,105 @@ func TestRunApparmor(t *testing.T) { | |
| base.Cmd("run", "--rm", "--privileged", testutil.AlpineImage, "cat", attrCurrentPath).AssertOutContains("unconfined") | ||
| } | ||
|
|
||
| func TestRunSelinuxWithSecurityOpt(t *testing.T) { | ||
| testCase := nerdtest.Setup() | ||
| testCase.Require = require.Not(nerdtest.NoSelinux) | ||
|
ningmingxiao marked this conversation as resolved.
|
||
| testContainer := testutil.Identifier(t) | ||
|
|
||
| testCase.SubTests = []*test.Case{ | ||
| { | ||
| Description: "test run with selinux-enabled", | ||
| Command: func(data test.Data, helpers test.Helpers) test.TestableCommand { | ||
| return helpers.Command("--selinux-enabled", "run", "-d", "--security-opt", "label=type:container_t", "--name", testContainer, "sleep", "infinity") | ||
| }, | ||
| Cleanup: func(data test.Data, helpers test.Helpers) { | ||
| helpers.Anyhow("rm", "-f", testContainer) | ||
| }, | ||
| Expected: func(data test.Data, helpers test.Helpers) *test.Expected { | ||
| return &test.Expected{ | ||
| ExitCode: 0, | ||
|
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. can use |
||
| Output: expect.All( | ||
| func(stdout string, t tig.T) { | ||
| inspectOut := helpers.Capture("container", "inspect", "--format", "{{.State.Pid}}", testContainer) | ||
| pid := strings.TrimSpace(inspectOut) | ||
| fileName := fmt.Sprintf("/proc/%s/attr/current", pid) | ||
| data, err := os.ReadFile(fileName) | ||
| assert.NilError(t, err) | ||
| assert.Equal(t, strings.Contains(string(data), "container_t"), true) | ||
| }, | ||
| ), | ||
| } | ||
| }, | ||
| }, | ||
| } | ||
| testCase.Run(t) | ||
| } | ||
| func TestRunSelinux(t *testing.T) { | ||
|
ningmingxiao marked this conversation as resolved.
Member
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. Same too. |
||
| testCase := nerdtest.Setup() | ||
| testCase.Require = require.Not(nerdtest.NoSelinux) | ||
| testContainer := testutil.Identifier(t) | ||
|
|
||
| testCase.SubTests = []*test.Case{ | ||
| { | ||
| Description: "test run with selinux-enabled", | ||
| Command: func(data test.Data, helpers test.Helpers) test.TestableCommand { | ||
| return helpers.Command("--selinux-enabled", "run", "-d", "--name", testContainer, "sleep", "infinity") | ||
| }, | ||
| Cleanup: func(data test.Data, helpers test.Helpers) { | ||
| helpers.Anyhow("rm", "-f", testContainer) | ||
| }, | ||
| Expected: func(data test.Data, helpers test.Helpers) *test.Expected { | ||
| return &test.Expected{ | ||
| ExitCode: 0, | ||
| Output: expect.All( | ||
|
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. same here can use |
||
| func(stdout string, t tig.T) { | ||
| inspectOut := helpers.Capture("container", "inspect", "--format", "{{.State.Pid}}", testContainer) | ||
| pid := strings.TrimSpace(inspectOut) | ||
| fileName := fmt.Sprintf("/proc/%s/attr/current", pid) | ||
| data, err := os.ReadFile(fileName) | ||
| assert.NilError(t, err) | ||
| assert.Equal(t, strings.Contains(string(data), "container_t"), true) | ||
| }, | ||
| ), | ||
| } | ||
| }, | ||
| }, | ||
| } | ||
| testCase.Run(t) | ||
| } | ||
|
|
||
| func TestRunSelinuxWithVolumeLabel(t *testing.T) { | ||
|
ningmingxiao marked this conversation as resolved.
|
||
| testCase := nerdtest.Setup() | ||
| testCase.Require = require.Not(nerdtest.NoSelinux) | ||
| testContainer := testutil.Identifier(t) | ||
|
|
||
| testCase.SubTests = []*test.Case{ | ||
| { | ||
| Description: "test run with selinux-enabled", | ||
| Command: func(data test.Data, helpers test.Helpers) test.TestableCommand { | ||
| return helpers.Command("--selinux-enabled", "run", "-d", "-v", fmt.Sprintf("/%s:/%s:Z", testContainer, testContainer), "--name", testContainer, "sleep", "infinity") | ||
|
Member
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. lower z should be tested too
This should be verified by launching multiple containers |
||
| }, | ||
| Cleanup: func(data test.Data, helpers test.Helpers) { | ||
| helpers.Anyhow("rm", "-f", testContainer) | ||
| }, | ||
|
Comment on lines
+270
to
+275
Member
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 test creates a directory at |
||
| Expected: func(data test.Data, helpers test.Helpers) *test.Expected { | ||
| return &test.Expected{ | ||
| ExitCode: 0, | ||
|
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. can change |
||
| Output: expect.All( | ||
| func(stdout string, t tig.T) { | ||
| cmd := exec.Command("ls", "-Z", fmt.Sprintf("/%s", testContainer)) | ||
| lsStdout, err := cmd.CombinedOutput() | ||
| assert.NilError(t, err) | ||
| assert.Equal(t, strings.Contains(string(lsStdout), "container_t"), true) | ||
| }, | ||
| ), | ||
| } | ||
| }, | ||
| }, | ||
| } | ||
| testCase.Run(t) | ||
| } | ||
|
|
||
| // TestRunSeccompCapSysPtrace tests https://github.com/containerd/nerdctl/issues/976 | ||
| func TestRunSeccompCapSysPtrace(t *testing.T) { | ||
| base := testutil.NewBase(t) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -255,6 +255,7 @@ Security flags: | |
|
|
||
| - :whale: `--security-opt seccomp=<PROFILE_JSON_FILE>`: specify custom seccomp profile | ||
| - :whale: `--security-opt apparmor=<PROFILE>`: specify custom AppArmor profile | ||
| :whale: `--security-opt label=<selinuxbel>`: specify custom selinux label | ||
|
Member
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. Minor formatting issue - this line is missing the list marker |
||
| - :whale: `--security-opt no-new-privileges`: disallow privilege escalation, e.g., setuid and file capabilities | ||
| - :whale: `--security-opt systempaths=unconfined`: Turn off confinement for system paths (masked paths, read-only paths) for the container | ||
| - :whale: `--security-opt writable-cgroups`: making the cgroups writeable | ||
|
|
@@ -1977,6 +1978,7 @@ Flags: | |
| - :nerd_face: `--host-gateway-ip`: IP address that the special 'host-gateway' string in --add-host resolves to. It has no effect without setting --add-host | ||
| - Default: the IP address of the host | ||
| - :nerd_face: `--userns-remap=<username>:<groupname>`: Support idmapping of containers. This options is only supported on rootful linux for container create and run if a user name and optionally group name is passed, it does idmapping based on the uidmap and gidmap ranges specified in /etc/subuid and /etc/subgid respectively. Note: `--userns-remap` is not supported for building containers. Nerdctl Build doesn't support userns-remap feature. (format: <name|uid>[:<group|gid>]) | ||
| - :nerd_face: `--selinux-enabled`: Enable selinux support | ||
|
ningmingxiao marked this conversation as resolved.
|
||
|
|
||
| The global flags can be also specified in `/etc/nerdctl/nerdctl.toml` (rootful) and `~/.config/nerdctl/nerdctl.toml` (rootless). | ||
| See [`./config.md`](./config.md). | ||
|
|
||
|
Member
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. missing |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,6 +30,7 @@ userns_remap = "" | |
| dns = ["8.8.8.8", "1.1.1.1"] | ||
| dns_opts = ["ndots:1", "timeout:2"] | ||
| dns_search = ["example.com", "example.org"] | ||
| selinux_enabled= true | ||
| ``` | ||
|
|
||
| ## Properties | ||
|
Member
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. Needs to be updated
Contributor
Author
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. I don't understand what you meean ? about selinux_enabled= true @AkihiroSuda |
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,14 +17,19 @@ | |
| package container | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
| "strconv" | ||
| "strings" | ||
| "sync" | ||
|
|
||
| "github.com/opencontainers/runtime-spec/specs-go" | ||
| "github.com/opencontainers/selinux/go-selinux/label" | ||
|
|
||
| "github.com/containerd/containerd/v2/contrib/apparmor" | ||
| "github.com/containerd/containerd/v2/contrib/seccomp" | ||
| "github.com/containerd/containerd/v2/core/containers" | ||
| "github.com/containerd/containerd/v2/pkg/cap" | ||
| "github.com/containerd/containerd/v2/pkg/oci" | ||
| "github.com/containerd/log" | ||
|
|
@@ -51,10 +56,10 @@ const ( | |
| systemPathsUnconfined = "unconfined" | ||
| ) | ||
|
|
||
| func generateSecurityOpts(privileged bool, securityOptsMap map[string]string) ([]oci.SpecOpts, error) { | ||
| func generateSecurityOpts(privileged bool, selinuxEnabled bool, securityOptsMap map[string]string) ([]oci.SpecOpts, error) { | ||
| for k := range securityOptsMap { | ||
| switch k { | ||
| case "seccomp", "apparmor", "no-new-privileges", "systempaths", "privileged-without-host-devices", "writable-cgroups": | ||
| case "seccomp", "apparmor", "no-new-privileges", "systempaths", "privileged-without-host-devices", "writable-cgroups", "label": | ||
| default: | ||
| log.L.Warnf("unknown security-opt: %q", k) | ||
| } | ||
|
|
@@ -95,6 +100,27 @@ func generateSecurityOpts(privileged bool, securityOptsMap map[string]string) ([ | |
| opts = append(opts, apparmor.WithProfile(defaults.AppArmorProfileName)) | ||
| } | ||
| } | ||
| var labelOpts []string | ||
| // TODO: should set unique MCS categorie. | ||
| if !privileged && selinuxEnabled { | ||
| if selinuxLabel, ok := securityOptsMap["label"]; ok { | ||
| labelOpts = append(labelOpts, selinuxLabel) | ||
| processLabel, mountLabel, err := label.InitLabels(labelOpts) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| opts = append(opts, WithSelinuxLabel(processLabel, mountLabel)) | ||
| } | ||
|
|
||
| // if selinux-enabled=true and security-opt selinux label is not set. | ||
| if len(labelOpts) == 0 { | ||
| processLabel, mountLabel, err := label.InitLabels(labelOpts) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| opts = append(opts, WithSelinuxLabel(processLabel, mountLabel)) | ||
| } | ||
| } | ||
|
Comment on lines
+103
to
+123
Member
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 logic here seems redundant. Could we simplify this to: |
||
|
|
||
| nnp, err := maputil.MapBoolValueAsOpt(securityOptsMap, "no-new-privileges") | ||
| if err != nil { | ||
|
|
@@ -141,6 +167,21 @@ func generateSecurityOpts(privileged bool, securityOptsMap map[string]string) ([ | |
| return opts, nil | ||
| } | ||
|
|
||
| // WithSelinuxLabels sets the mount and process labels | ||
| func WithSelinuxLabel(process, mount string) oci.SpecOpts { | ||
| return func(_ context.Context, _ oci.Client, _ *containers.Container, s *oci.Spec) error { | ||
| if s.Linux == nil { | ||
| s.Linux = &specs.Linux{} | ||
| } | ||
| if s.Process == nil { | ||
| s.Process = &specs.Process{} | ||
| } | ||
| s.Linux.MountLabel = mount | ||
| s.Process.SelinuxLabel = process | ||
| return nil | ||
| } | ||
| } | ||
|
|
||
| func canonicalizeCapName(s string) string { | ||
| if s == "" { | ||
| return "" | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.