aboutsummaryrefslogtreecommitdiffhomepage
path: root/internal/sandbox/container.go
blob: 6676a2a9b3ac73adf7b0268707e5f67c7998eda2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package sandbox

import (
	"context"
	"encoding/gob"
	"errors"
	"fmt"
	"io"
	"os"
	"os/exec"
	"path"
	"strconv"
	"syscall"
	"time"

	"git.gensokyo.uk/security/fortify/helper/proc"
	"git.gensokyo.uk/security/fortify/internal"
	"git.gensokyo.uk/security/fortify/internal/fmsg"
	"git.gensokyo.uk/security/fortify/seccomp"
)

type HardeningFlags uintptr

const (
	FSyscallCompat HardeningFlags = 1 << iota
	FAllowDevel
	FAllowUserns
	FAllowTTY
	FAllowNet
)

func (flags HardeningFlags) seccomp(opts seccomp.SyscallOpts) seccomp.SyscallOpts {
	if flags&FSyscallCompat == 0 {
		opts |= seccomp.FlagExt
	}
	if flags&FAllowDevel == 0 {
		opts |= seccomp.FlagDenyDevel
	}
	if flags&FAllowUserns == 0 {
		opts |= seccomp.FlagDenyNS
	}
	if flags&FAllowTTY == 0 {
		opts |= seccomp.FlagDenyTTY
	}
	return opts
}

type (
	// Container represents a container environment being prepared or run.
	// None of [Container] methods are safe for concurrent use.
	Container struct {
		// Name of initial process in the container.
		name string
		// Cgroup fd, nil to disable.
		Cgroup *int
		// ExtraFiles passed through to initial process in the container,
		// with behaviour identical to its [exec.Cmd] counterpart.
		ExtraFiles []*os.File

		InitParams
		// Custom [exec.Cmd] initialisation function.
		CommandContext func(ctx context.Context) (cmd *exec.Cmd)

		// param encoder for shim and init
		setup *gob.Encoder
		// cancels cmd
		cancel context.CancelFunc

		Stdin  io.Reader
		Stdout io.Writer
		Stderr io.Writer

		Cancel    func() error
		WaitDelay time.Duration

		cmd *exec.Cmd
		ctx context.Context
	}

	InitParams struct {
		// Working directory in the container.
		Dir string
		// Initial process environment.
		Env []string
		// Absolute path of initial process in the container. Overrides name.
		Path string
		// Initial process argv.
		Args []string

		// Mapped Uid in user namespace.
		Uid int
		// Mapped Gid in user namespace.
		Gid int
		// Hostname value in UTS namespace.
		Hostname string
		// Sequential container setup ops.
		*Ops
		// Extra seccomp options.
		Seccomp seccomp.SyscallOpts

		Flags HardeningFlags
	}

	Ops []Op
	Op  interface {
		apply(params *InitParams) error

		Is(op Op) bool
		fmt.Stringer
	}
)

func (p *Container) Start() error {
	if p.cmd != nil {
		return errors.New("sandbox: already started")
	}

	c, cancel := context.WithCancel(p.ctx)
	p.cancel = cancel

	var cloneFlags uintptr = syscall.CLONE_NEWIPC |
		syscall.CLONE_NEWUTS |
		syscall.CLONE_NEWCGROUP
	if p.Flags&FAllowNet == 0 {
		cloneFlags |= syscall.CLONE_NEWNET
	}

	// map to overflow id to work around ownership checks
	if p.Uid < 1 {
		p.Uid = OverflowUid()
	}
	if p.Gid < 1 {
		p.Gid = OverflowGid()
	}

	p.cmd = p.CommandContext(c)
	p.cmd.Stdin, p.cmd.Stdout, p.cmd.Stderr = p.Stdin, p.Stdout, p.Stderr
	p.cmd.Cancel, p.cmd.WaitDelay = p.Cancel, p.WaitDelay
	p.cmd.Dir = "/"
	p.cmd.SysProcAttr = &syscall.SysProcAttr{
		Setsid:    p.Flags&FAllowTTY == 0,
		Pdeathsig: syscall.SIGKILL,

		Cloneflags: cloneFlags |
			syscall.CLONE_NEWUSER |
			syscall.CLONE_NEWPID |
			syscall.CLONE_NEWNS,

		// remain privileged for setup
		AmbientCaps: []uintptr{CAP_SYS_ADMIN},

		UseCgroupFD: p.Cgroup != nil,
	}
	if p.cmd.SysProcAttr.UseCgroupFD {
		p.cmd.SysProcAttr.CgroupFD = *p.Cgroup
	}

	// place setup pipe before user supplied extra files, this is later restored by init
	if fd, e, err := proc.Setup(&p.cmd.ExtraFiles); err != nil {
		return fmsg.WrapErrorSuffix(err,
			"cannot create shim setup pipe:")
	} else {
		p.setup = e
		p.cmd.Env = []string{setupEnv + "=" + strconv.Itoa(fd)}
	}
	p.cmd.ExtraFiles = append(p.cmd.ExtraFiles, p.ExtraFiles...)

	fmsg.Verbose("starting container init")
	if err := p.cmd.Start(); err != nil {
		return fmsg.WrapError(err, err.Error())
	}
	return nil
}

func (p *Container) Serve() error {
	if p.setup == nil {
		panic("invalid serve")
	}

	if p.Path != "" && !path.IsAbs(p.Path) {
		return fmsg.WrapError(syscall.EINVAL,
			fmt.Sprintf("invalid executable path %q", p.Path))
	}

	if p.Path == "" {
		if p.name == "" {
			p.Path = os.Getenv("SHELL")
			if !path.IsAbs(p.Path) {
				return fmsg.WrapError(syscall.EBADE,
					"no command specified and $SHELL is invalid")
			}
			p.name = path.Base(p.Path)
		} else if path.IsAbs(p.name) {
			p.Path = p.name
		} else if v, err := exec.LookPath(p.name); err != nil {
			return fmsg.WrapError(err, err.Error())
		} else {
			p.Path = v
		}
	}

	setup := p.setup
	p.setup = nil
	return setup.Encode(
		&initParams{
			p.InitParams,
			syscall.Getuid(),
			syscall.Getgid(),
			len(p.ExtraFiles),
			fmsg.Load(),
		},
	)
}

func (p *Container) Wait() error { defer p.cancel(); return p.cmd.Wait() }

func (p *Container) String() string {
	return fmt.Sprintf("argv: %q, flags: %#x, seccomp: %#x",
		p.Args, p.Flags, int(p.Flags.seccomp(p.Seccomp)))
}

func New(ctx context.Context, name string, args ...string) *Container {
	return &Container{name: name, ctx: ctx,
		InitParams: InitParams{Args: append([]string{name}, args...), Dir: "/", Ops: new(Ops)},
		CommandContext: func(ctx context.Context) (cmd *exec.Cmd) {
			cmd = exec.CommandContext(ctx, internal.MustExecutable())
			cmd.Args = []string{"init"}
			return
		},
	}
}