Add support for --no-new-privs

Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>

Closes: #369
Approved by: rhatdan
This commit is contained in:
Daniel J Walsh
2018-02-15 12:23:36 -05:00
committed by Atomic Bot
parent 1d9539337b
commit 831dc48883
5 changed files with 46 additions and 9 deletions

View File

@ -128,7 +128,7 @@ type createConfig struct {
WorkDir string //workdir WorkDir string //workdir
MountLabel string //SecurityOpts MountLabel string //SecurityOpts
ProcessLabel string //SecurityOpts ProcessLabel string //SecurityOpts
NoNewPrivileges bool //SecurityOpts NoNewPrivs bool //SecurityOpts
ApparmorProfile string //SecurityOpts ApparmorProfile string //SecurityOpts
SeccompProfilePath string //SecurityOpts SeccompProfilePath string //SecurityOpts
SecurityOpts []string SecurityOpts []string
@ -252,7 +252,7 @@ func parseSecurityOpt(config *createConfig, securityOpts []string) error {
for _, opt := range securityOpts { for _, opt := range securityOpts {
if opt == "no-new-privileges" { if opt == "no-new-privileges" {
config.NoNewPrivileges = true config.NoNewPrivs = true
} else { } else {
con := strings.SplitN(opt, "=", 2) con := strings.SplitN(opt, "=", 2)
if len(con) != 2 { if len(con) != 2 {

View File

@ -259,7 +259,7 @@ func createConfigToOCISpec(config *createConfig) (*spec.Spec, error) {
} }
// SECURITY OPTS // SECURITY OPTS
g.SetProcessNoNewPrivileges(config.NoNewPrivileges) g.SetProcessNoNewPrivileges(config.NoNewPrivs)
g.SetProcessApparmorProfile(config.ApparmorProfile) g.SetProcessApparmorProfile(config.ApparmorProfile)
g.SetProcessSelinuxLabel(config.ProcessLabel) g.SetProcessSelinuxLabel(config.ProcessLabel)
g.SetLinuxMountLabel(config.MountLabel) g.SetLinuxMountLabel(config.MountLabel)
@ -665,6 +665,7 @@ func (c *createConfig) GetContainerCreateOptions() ([]libpod.CtrCreateOption, er
} }
options = append(options, libpod.WithPrivileged(c.Privileged)) options = append(options, libpod.WithPrivileged(c.Privileged))
options = append(options, libpod.WithNoNewPrivs(c.NoNewPrivs))
return options, nil return options, nil
} }

View File

@ -240,6 +240,7 @@ func (c *Container) Exec(tty, privileged bool, env, cmd []string, user string) e
capAdd: capList, capAdd: capList,
pidFile: filepath.Join(c.state.RunDir, fmt.Sprintf("%s-execpid", stringid.GenerateNonCryptoID()[:12])), pidFile: filepath.Join(c.state.RunDir, fmt.Sprintf("%s-execpid", stringid.GenerateNonCryptoID()[:12])),
env: env, env: env,
noNewPrivs: c.config.NoNewPrivs,
user: user, user: user,
cwd: c.config.Spec.Process.Cwd, cwd: c.config.Spec.Process.Cwd,
tty: tty, tty: tty,

View File

@ -272,6 +272,18 @@ func WithPrivileged(privileged bool) CtrCreateOption {
} }
} }
// WithNoNewPrivs sets the noNewPrivs flag in the container runtime
func WithNoNewPrivs(noNewPrivs bool) CtrCreateOption {
return func(ctr *Container) error {
if ctr.valid {
return ErrCtrFinalized
}
ctr.config.NoNewPrivs = noNewPrivs
return nil
}
}
// WithSELinuxLabels sets the mount label for SELinux // WithSELinuxLabels sets the mount label for SELinux
func WithSELinuxLabels(processLabel, mountLabel string) CtrCreateOption { func WithSELinuxLabels(processLabel, mountLabel string) CtrCreateOption {
return func(ctr *Container) error { return func(ctr *Container) error {

View File

@ -1,6 +1,7 @@
package integration package integration
import ( import (
"fmt"
"os" "os"
. "github.com/onsi/ginkgo" . "github.com/onsi/ginkgo"
@ -81,4 +82,26 @@ var _ = Describe("Podman privileged container tests", func() {
Expect(session.ExitCode()).To(Equal(0)) Expect(session.ExitCode()).To(Equal(0))
Expect(len(session.OutputToStringArray())).To(BeNumerically(">", 20)) Expect(len(session.OutputToStringArray())).To(BeNumerically(">", 20))
}) })
It("run no-new-privileges test", func() {
cap := podmanTest.SystemExec("grep", []string{"NoNewPrivs", "/proc/self/status"})
cap.WaitWithDefaultTimeout()
if cap.ExitCode() != 0 {
fmt.Println("Can't determine NoNewPrivs")
return
}
session := podmanTest.Podman([]string{"run", "busybox", "grep", "NoNewPrivs", "/proc/self/status"})
session.WaitWithDefaultTimeout()
Expect(session.ExitCode()).To(Equal(0))
privs := strings.Split(cap.OutputToString(), ":")
session = podmanTest.Podman([]string{"run", "--security-opt", "no-new-privileges", "busybox", "grep", "NoNewPrivs", "/proc/self/status"})
session.WaitWithDefaultTimeout()
Expect(session.ExitCode()).To(Equal(0))
noprivs := strings.Split(cap.OutputToString(), ":")
Expect(privs[1]).To(Not(Equal(noprivs[1])))
})
}) })