1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

change default docker-engine profile to a template based on apparmor_parser version

Signed-off-by: Jessica Frazelle <acidburn@docker.com>
This commit is contained in:
Jessica Frazelle 2015-10-13 17:37:54 -07:00
parent bece5b82a8
commit 8cf89245f5
No known key found for this signature in database
GPG key ID: 18F3685C0022BFF3
2 changed files with 105 additions and 2 deletions

85
contrib/apparmor/main.go Normal file
View file

@ -0,0 +1,85 @@
package main
import (
"fmt"
"log"
"os"
"os/exec"
"path"
"strconv"
"strings"
"text/template"
)
type profileData struct {
MajorVersion int
MinorVersion int
}
func main() {
if len(os.Args) < 2 {
log.Fatal("pass a filename to save the profile in.")
}
// parse the arg
apparmorProfilePath := os.Args[1]
// get the apparmor_version version
cmd := exec.Command("/sbin/apparmor_parser", "--version")
output, err := cmd.CombinedOutput()
if err != nil {
log.Fatalf("getting apparmor_parser version failed: %s (%s)", err, output)
}
// parse the version from the output
// output is in the form of the following:
// AppArmor parser version 2.9.1
// Copyright (C) 1999-2008 Novell Inc.
// Copyright 2009-2012 Canonical Ltd.
lines := strings.SplitN(string(output), "\n", 2)
words := strings.Split(lines[0], " ")
version := words[len(words)-1]
// split by major minor version
v := strings.Split(version, ".")
if len(v) < 2 {
log.Fatalf("parsing major minor version failed for %q", version)
}
majorVersion, err := strconv.Atoi(v[0])
if err != nil {
log.Fatal(err)
}
minorVersion, err := strconv.Atoi(v[1])
if err != nil {
log.Fatal(err)
}
data := profileData{
MajorVersion: majorVersion,
MinorVersion: minorVersion,
}
fmt.Printf("apparmor_parser is of version %+v\n", data)
// parse the template
compiled, err := template.New("apparmor_profile").Parse(dockerProfileTemplate)
if err != nil {
log.Fatalf("parsing template failed: %v", err)
}
// make sure /etc/apparmor.d exists
if err := os.MkdirAll(path.Dir(apparmorProfilePath), 0755); err != nil {
log.Fatal(err)
}
f, err := os.OpenFile(apparmorProfilePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
log.Fatal(err)
}
defer f.Close()
if err := compiled.Execute(f, data); err != nil {
log.Fatalf("executing template failed: %v", err)
}
fmt.Printf("created apparmor profile for version %+v at %q\n", data, apparmorProfilePath)
}

View file

@ -1,4 +1,6 @@
@{DOCKER_GRAPH_PATH}=/var/lib/docker
package main
const dockerProfileTemplate = `@{DOCKER_GRAPH_PATH}=/var/lib/docker
profile /usr/bin/docker (attach_disconnected, complain) {
# Prevent following links to these files during container setup.
@ -15,9 +17,11 @@ profile /usr/bin/docker (attach_disconnected, complain) {
umount,
pivot_root,
{{if ge .MajorVersion 2}}{{if ge .MinorVersion 9}}
signal (receive) peer=@{profile_name},
signal (receive) peer=unconfined,
signal (send),
{{end}}{{end}}
ipc rw,
network,
capability,
@ -34,10 +38,12 @@ profile /usr/bin/docker (attach_disconnected, complain) {
/etc/localtime r,
/etc/ld.so.cache r,
{{if ge .MajorVersion 2}}{{if ge .MinorVersion 9}}
ptrace peer=@{profile_name},
ptrace (read) peer=docker-default,
deny ptrace (trace) peer=docker-default,
deny ptrace peer=/usr/bin/docker///bin/ps,
{{end}}{{end}}
/usr/lib/** rm,
/lib/** rm,
@ -57,9 +63,11 @@ profile /usr/bin/docker (attach_disconnected, complain) {
/sbin/zfs rCx,
/sbin/apparmor_parser rCx,
{{if ge .MajorVersion 2}}{{if ge .MinorVersion 9}}
# Transitions
change_profile -> docker-*,
change_profile -> unconfined,
{{end}}{{end}}
profile /bin/cat (complain) {
/etc/ld.so.cache r,
@ -81,8 +89,10 @@ profile /usr/bin/docker (attach_disconnected, complain) {
/dev/null rw,
/bin/ps mr,
{{if ge .MajorVersion 2}}{{if ge .MinorVersion 9}}
# We don't need ptrace so we'll deny and ignore the error.
deny ptrace (read, trace),
{{end}}{{end}}
# Quiet dac_override denials
deny capability dac_override,
@ -100,11 +110,15 @@ profile /usr/bin/docker (attach_disconnected, complain) {
/proc/tty/drivers r,
}
profile /sbin/iptables (complain) {
{{if ge .MajorVersion 2}}{{if ge .MinorVersion 9}}
signal (receive) peer=/usr/bin/docker,
{{end}}{{end}}
capability net_admin,
}
profile /sbin/auplink flags=(attach_disconnected, complain) {
{{if ge .MajorVersion 2}}{{if ge .MinorVersion 9}}
signal (receive) peer=/usr/bin/docker,
{{end}}{{end}}
capability sys_admin,
capability dac_override,
@ -123,7 +137,9 @@ profile /usr/bin/docker (attach_disconnected, complain) {
/proc/[0-9]*/mounts rw,
}
profile /sbin/modprobe /bin/kmod (complain) {
{{if ge .MajorVersion 2}}{{if ge .MinorVersion 9}}
signal (receive) peer=/usr/bin/docker,
{{end}}{{end}}
capability sys_module,
/etc/ld.so.cache r,
/lib/** rm,
@ -137,7 +153,9 @@ profile /usr/bin/docker (attach_disconnected, complain) {
}
# xz works via pipes, so we do not need access to the filesystem.
profile /usr/bin/xz (complain) {
{{if ge .MajorVersion 2}}{{if ge .MinorVersion 9}}
signal (receive) peer=/usr/bin/docker,
{{end}}{{end}}
/etc/ld.so.cache r,
/lib/** rm,
/usr/bin/xz rm,
@ -238,4 +256,4 @@ profile /usr/bin/docker (attach_disconnected, complain) {
capability mac_admin,
}
}
}`