Add AppArmor support to native driver + change pipe/dup logic

Docker-DCO-1.1-Signed-off-by: Guillaume J. Charmes <guillaume.charmes@docker.com> (github: creack)
This commit is contained in:
Guillaume J. Charmes 2014-03-05 11:59:31 -08:00
parent a24a802193
commit cb4189a292
No known key found for this signature in database
GPG Key ID: B33E4642CB6E3FF3
4 changed files with 61 additions and 10 deletions

View File

@ -37,6 +37,7 @@ func createContainer(c *execdriver.Command) *libcontainer.Container {
if c.Privileged {
container.Capabilities = nil
container.Cgroups.DeviceAccess = true
container.Context["apparmor_profile"] = "unconfined"
}
if c.Resources != nil {
container.Cgroups.CpuShares = c.Resources.CpuShares
@ -78,5 +79,8 @@ func getDefaultTemplate() *libcontainer.Container {
Parent: "docker",
DeviceAccess: false,
},
Context: libcontainer.Context{
"apparmor_profile": "lxc-container-default",
},
}
}

View File

@ -0,0 +1,42 @@
package apparmor
import (
"errors"
"fmt"
"io/ioutil"
"log"
"os"
)
var AppArmorEnabled bool
var (
ErrAppArmorDisabled = errors.New("Error: AppArmor is not enabled on this system")
)
func init() {
buf, err := ioutil.ReadFile("/sys/module/apparmor/parameters/enabled")
AppArmorEnabled = err == nil && len(buf) > 1 && buf[0] == 'Y'
}
func ApplyProfile(pid int, name string) error {
if !AppArmorEnabled {
return ErrAppArmorDisabled
}
f, err := os.OpenFile(fmt.Sprintf("/proc/%d/attr/current", pid), os.O_WRONLY, 0)
if err != nil {
log.Printf("error open: %s\n", err)
return err
}
defer f.Close()
if _, err := fmt.Fprintf(f, "changeprofile %s", name); err != nil {
log.Printf("changeprofile %s", name)
log.Printf("Error write: %s\n", err)
return err
} else {
log.Printf("Write success!")
}
return nil
}

View File

@ -20,7 +20,8 @@ type Container struct {
Namespaces Namespaces `json:"namespaces,omitempty"` // namespaces to apply
Capabilities Capabilities `json:"capabilities,omitempty"` // capabilities to drop
Networks []*Network `json:"networks,omitempty"` // nil for host's network stack
Cgroups *cgroups.Cgroup `json:"cgroups,omitempty"`
Cgroups *cgroups.Cgroup `json:"cgroups,omitempty"` // cgroups
Context Context `json:"context,omitempty"` // generic context for specific options (apparmor, selinux)
}
// Network defines configuration for a container's networking stack

View File

@ -5,6 +5,7 @@ package nsinit
import (
"fmt"
"github.com/dotcloud/docker/pkg/libcontainer"
"github.com/dotcloud/docker/pkg/libcontainer/apparmor"
"github.com/dotcloud/docker/pkg/libcontainer/capabilities"
"github.com/dotcloud/docker/pkg/libcontainer/network"
"github.com/dotcloud/docker/pkg/libcontainer/utils"
@ -32,7 +33,7 @@ func (ns *linuxNs) Init(container *libcontainer.Container, uncleanRootfs, consol
if console != "" {
// close pipes so that we can replace it with the pty
closeStdPipes()
// closeStdPipes()
slave, err := system.OpenTerminal(console, syscall.O_RDWR)
if err != nil {
return fmt.Errorf("open terminal %s", err)
@ -55,9 +56,17 @@ func (ns *linuxNs) Init(container *libcontainer.Container, uncleanRootfs, consol
return fmt.Errorf("parent death signal %s", err)
}
*/
if err := setupNewMountNamespace(rootfs, console, container.ReadonlyFs); err != nil {
return fmt.Errorf("setup mount namespace %s", err)
}
if err := apparmor.ApplyProfile(os.Getpid(), container.Context["apparmor_profile"]); err != nil {
if err != apparmor.ErrAppArmorDisabled {
return err
}
}
if err := setupNetwork(container, context); err != nil {
return fmt.Errorf("setup networking %s", err)
}
@ -67,13 +76,8 @@ func (ns *linuxNs) Init(container *libcontainer.Container, uncleanRootfs, consol
if err := finalizeNamespace(container); err != nil {
return fmt.Errorf("finalize namespace %s", err)
}
return system.Execv(args[0], args[0:], container.Env)
}
func closeStdPipes() {
os.Stdin.Close()
os.Stdout.Close()
os.Stderr.Close()
return system.Execv(args[0], args[0:], container.Env)
}
func setupUser(container *libcontainer.Container) error {
@ -109,8 +113,8 @@ func setupUser(container *libcontainer.Container) error {
// dupSlave dup2 the pty slave's fd into stdout and stdin and ensures that
// the slave's fd is 0, or stdin
func dupSlave(slave *os.File) error {
if slave.Fd() != 0 {
return fmt.Errorf("slave fd not 0 %d", slave.Fd())
if err := system.Dup2(slave.Fd(), 0); err != nil {
return err
}
if err := system.Dup2(slave.Fd(), 1); err != nil {
return err