Skip to content

Commit

Permalink
Add AppArmor support to native driver + change pipe/dup logic
Browse files Browse the repository at this point in the history
Docker-DCO-1.1-Signed-off-by: Guillaume J. Charmes <guillaume.charmes@docker.com> (github: creack)
  • Loading branch information
creack committed Mar 5, 2014
1 parent a24a802 commit cb4189a
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 10 deletions.
4 changes: 4 additions & 0 deletions execdriver/native/default_template.go
Expand Up @@ -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
Expand Down Expand Up @@ -78,5 +79,8 @@ func getDefaultTemplate() *libcontainer.Container {
Parent: "docker",
DeviceAccess: false,
},
Context: libcontainer.Context{
"apparmor_profile": "lxc-container-default",
},
}
}
42 changes: 42 additions & 0 deletions pkg/libcontainer/apparmor/apparmor.go
@@ -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
}
3 changes: 2 additions & 1 deletion pkg/libcontainer/container.go
Expand Up @@ -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
Expand Down
22 changes: 13 additions & 9 deletions pkg/libcontainer/nsinit/init.go
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand All @@ -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)
}
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit cb4189a

Please sign in to comment.