sandbox: mount container /dev
Signed-off-by: Ophestra <cat@gensokyo.uk>
This commit is contained in:
parent
2eff470091
commit
f332200ca4
@ -13,6 +13,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"git.gensokyo.uk/security/fortify/fst"
|
"git.gensokyo.uk/security/fortify/fst"
|
||||||
|
"git.gensokyo.uk/security/fortify/helper/seccomp"
|
||||||
"git.gensokyo.uk/security/fortify/internal"
|
"git.gensokyo.uk/security/fortify/internal"
|
||||||
"git.gensokyo.uk/security/fortify/internal/fmsg"
|
"git.gensokyo.uk/security/fortify/internal/fmsg"
|
||||||
"git.gensokyo.uk/security/fortify/internal/sandbox"
|
"git.gensokyo.uk/security/fortify/internal/sandbox"
|
||||||
@ -29,17 +30,33 @@ func TestContainer(t *testing.T) {
|
|||||||
|
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
name string
|
name string
|
||||||
|
flags sandbox.HardeningFlags
|
||||||
ops *sandbox.Ops
|
ops *sandbox.Ops
|
||||||
mnt []*check.Mntent
|
mnt []*check.Mntent
|
||||||
host string
|
host string
|
||||||
}{
|
}{
|
||||||
{"minimal", new(sandbox.Ops), nil, "test-minimal"},
|
{"minimal", 0, new(sandbox.Ops), nil, "test-minimal"},
|
||||||
{"tmpfs",
|
{"allow", sandbox.FAllowUserns | sandbox.FAllowNet | sandbox.FAllowTTY,
|
||||||
|
new(sandbox.Ops), nil, "test-minimal"},
|
||||||
|
{"tmpfs", 0,
|
||||||
new(sandbox.Ops).
|
new(sandbox.Ops).
|
||||||
Tmpfs(fst.Tmp, 0, 0755),
|
Tmpfs(fst.Tmp, 0, 0755),
|
||||||
[]*check.Mntent{
|
[]*check.Mntent{
|
||||||
{FSName: "tmpfs", Dir: fst.Tmp, Type: "tmpfs", Opts: "\x00"},
|
{FSName: "tmpfs", Dir: fst.Tmp, Type: "tmpfs", Opts: "\x00"},
|
||||||
}, "test-tmpfs"},
|
}, "test-tmpfs"},
|
||||||
|
{"dev", sandbox.FAllowTTY, // go test output is not a tty
|
||||||
|
new(sandbox.Ops).
|
||||||
|
Dev("/dev"),
|
||||||
|
[]*check.Mntent{
|
||||||
|
{FSName: "devtmpfs", Dir: "/dev", Type: "tmpfs", Opts: "\x00"},
|
||||||
|
{FSName: "devtmpfs", Dir: "/dev/null", Type: "devtmpfs", Opts: "\x00", Freq: -1, Passno: -1},
|
||||||
|
{FSName: "devtmpfs", Dir: "/dev/zero", Type: "devtmpfs", Opts: "\x00", Freq: -1, Passno: -1},
|
||||||
|
{FSName: "devtmpfs", Dir: "/dev/full", Type: "devtmpfs", Opts: "\x00", Freq: -1, Passno: -1},
|
||||||
|
{FSName: "devtmpfs", Dir: "/dev/random", Type: "devtmpfs", Opts: "\x00", Freq: -1, Passno: -1},
|
||||||
|
{FSName: "devtmpfs", Dir: "/dev/urandom", Type: "devtmpfs", Opts: "\x00", Freq: -1, Passno: -1},
|
||||||
|
{FSName: "devtmpfs", Dir: "/dev/tty", Type: "devtmpfs", Opts: "\x00", Freq: -1, Passno: -1},
|
||||||
|
{FSName: "devpts", Dir: "/dev/pts", Type: "devpts", Opts: "rw,nosuid,noexec,relatime,mode=620,ptmxmode=666", Freq: 0, Passno: 0},
|
||||||
|
}, ""},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tc := range testCases {
|
for _, tc := range testCases {
|
||||||
@ -54,6 +71,8 @@ func TestContainer(t *testing.T) {
|
|||||||
return exec.CommandContext(ctx, os.Args[0], "-test.v",
|
return exec.CommandContext(ctx, os.Args[0], "-test.v",
|
||||||
"-test.run=TestHelperInit", "--", "init")
|
"-test.run=TestHelperInit", "--", "init")
|
||||||
}
|
}
|
||||||
|
container.Seccomp |= seccomp.FlagExt
|
||||||
|
container.Flags |= tc.flags
|
||||||
container.Stdout, container.Stderr = os.Stdout, os.Stderr
|
container.Stdout, container.Stderr = os.Stdout, os.Stderr
|
||||||
container.Ops = tc.ops
|
container.Ops = tc.ops
|
||||||
if container.Args[5] == "" {
|
if container.Args[5] == "" {
|
||||||
|
@ -7,6 +7,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
"unsafe"
|
||||||
|
|
||||||
"git.gensokyo.uk/security/fortify/internal/fmsg"
|
"git.gensokyo.uk/security/fortify/internal/fmsg"
|
||||||
)
|
)
|
||||||
@ -62,6 +63,89 @@ func (p *MountProc) apply(*InitParams) error {
|
|||||||
fmt.Sprintf("cannot mount proc on %q:", p.Path))
|
fmt.Sprintf("cannot mount proc on %q:", p.Path))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func init() { gob.Register(new(MountDev)) }
|
||||||
|
|
||||||
|
// MountDev mounts dev on container Path.
|
||||||
|
type MountDev struct {
|
||||||
|
Path string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *MountDev) apply(params *InitParams) error {
|
||||||
|
if !path.IsAbs(d.Path) {
|
||||||
|
return fmsg.WrapError(syscall.EBADE,
|
||||||
|
fmt.Sprintf("path %q is not absolute", d.Path))
|
||||||
|
}
|
||||||
|
target := toSysroot(d.Path)
|
||||||
|
|
||||||
|
if err := mountTmpfs("devtmpfs", d.Path, 0, 0755); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, name := range []string{"null", "zero", "full", "random", "urandom", "tty"} {
|
||||||
|
if err := bindMount(
|
||||||
|
"/dev/"+name, path.Join(d.Path, name),
|
||||||
|
BindSource|BindDevices,
|
||||||
|
); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for i, name := range []string{"stdin", "stdout", "stderr"} {
|
||||||
|
if err := os.Symlink(
|
||||||
|
"/proc/self/fd/"+string(rune(i+'0')),
|
||||||
|
path.Join(target, name),
|
||||||
|
); err != nil {
|
||||||
|
return fmsg.WrapError(err, err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _, pair := range [][2]string{
|
||||||
|
{"/proc/self/fd", "fd"},
|
||||||
|
{"/proc/kcore", "core"},
|
||||||
|
{"pts/ptmx", "ptmx"},
|
||||||
|
} {
|
||||||
|
if err := os.Symlink(pair[0], path.Join(target, pair[1])); err != nil {
|
||||||
|
return fmsg.WrapError(err, err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
devPtsPath := path.Join(target, "pts")
|
||||||
|
for _, name := range []string{path.Join(target, "shm"), devPtsPath} {
|
||||||
|
if err := os.Mkdir(name, 0755); err != nil {
|
||||||
|
return fmsg.WrapError(err, err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := syscall.Mount("devpts", devPtsPath, "devpts",
|
||||||
|
syscall.MS_NOSUID|syscall.MS_NOEXEC,
|
||||||
|
"newinstance,ptmxmode=0666,mode=620"); err != nil {
|
||||||
|
return fmsg.WrapErrorSuffix(err,
|
||||||
|
fmt.Sprintf("cannot mount devpts on %q:", devPtsPath))
|
||||||
|
}
|
||||||
|
|
||||||
|
if params.Flags&FAllowTTY != 0 {
|
||||||
|
var buf [8]byte
|
||||||
|
if _, _, errno := syscall.Syscall(
|
||||||
|
syscall.SYS_IOCTL, 1, syscall.TIOCGWINSZ,
|
||||||
|
uintptr(unsafe.Pointer(&buf[0])),
|
||||||
|
); errno == 0 {
|
||||||
|
if err := bindMount(
|
||||||
|
"/proc/self/fd/1", path.Join(d.Path, "console"),
|
||||||
|
BindDevices,
|
||||||
|
); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *MountDev) Is(op Op) bool { vd, ok := op.(*MountDev); return ok && *d == *vd }
|
||||||
|
func (d *MountDev) String() string { return fmt.Sprintf("dev on %q", d.Path) }
|
||||||
|
func (f *Ops) Dev(dest string) *Ops {
|
||||||
|
*f = append(*f, &MountDev{dest})
|
||||||
|
return f
|
||||||
|
}
|
||||||
|
|
||||||
func (p *MountProc) Is(op Op) bool { vp, ok := op.(*MountProc); return ok && *p == *vp }
|
func (p *MountProc) Is(op Op) bool { vp, ok := op.(*MountProc); return ok && *p == *vp }
|
||||||
func (p *MountProc) String() string { return fmt.Sprintf("proc on %q", p.Path) }
|
func (p *MountProc) String() string { return fmt.Sprintf("proc on %q", p.Path) }
|
||||||
func (f *Ops) Proc(dest string) *Ops {
|
func (f *Ops) Proc(dest string) *Ops {
|
||||||
|
Loading…
Reference in New Issue
Block a user