summaryrefslogtreecommitdiffstats
path: root/plugin/rpcplugin/sandbox/sandbox_linux.go
blob: dad485f6827ae9c2a8bc4095767318863eeadb95 (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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

package sandbox

import (
	"bytes"
	"context"
	"encoding/json"
	"fmt"
	"io"
	"io/ioutil"
	"os"
	"os/exec"
	"path/filepath"
	"syscall"
	"unsafe"

	"github.com/pkg/errors"
	"golang.org/x/sys/unix"

	"github.com/mattermost/mattermost-server/plugin/rpcplugin"
)

func init() {
	if len(os.Args) < 3 || os.Args[0] != "sandbox.runProcess" {
		return
	}

	var config Configuration
	if err := json.Unmarshal([]byte(os.Args[1]), &config); err != nil {
		fmt.Println(err.Error())
		os.Exit(1)
	}
	if err := runProcess(&config, os.Args[2]); err != nil {
		if eerr, ok := err.(*exec.ExitError); ok {
			if status, ok := eerr.Sys().(syscall.WaitStatus); ok {
				os.Exit(status.ExitStatus())
			}
		}
		fmt.Println(err.Error())
		os.Exit(1)
	}
	os.Exit(0)
}

func systemMountPoints() (points []*MountPoint) {
	points = append(points, &MountPoint{
		Source:      "proc",
		Destination: "/proc",
		Type:        "proc",
	}, &MountPoint{
		Source:      "/dev/null",
		Destination: "/dev/null",
	}, &MountPoint{
		Source:      "/dev/zero",
		Destination: "/dev/zero",
	}, &MountPoint{
		Source:      "/dev/full",
		Destination: "/dev/full",
	})

	readOnly := []string{
		"/dev/random",
		"/dev/urandom",
		"/etc/resolv.conf",
		"/lib",
		"/lib32",
		"/lib64",
		"/usr/lib",
		"/usr/lib32",
		"/usr/lib64",
		"/etc/ca-certificates",
		"/etc/ssl/certs",
		"/system/etc/security/cacerts",
		"/usr/local/share/certs",
		"/etc/pki/tls/certs",
		"/etc/openssl/certs",
		"/etc/ssl/ca-bundle.pem",
		"/etc/pki/tls/cacert.pem",
		"/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem",
	}

	for _, v := range []string{"SSL_CERT_FILE", "SSL_CERT_DIR"} {
		if path := os.Getenv(v); path != "" {
			readOnly = append(readOnly, path)
		}
	}

	for _, point := range readOnly {
		points = append(points, &MountPoint{
			Source:      point,
			Destination: point,
			ReadOnly:    true,
		})
	}

	return
}

func runProcess(config *Configuration, path string) error {
	root, err := ioutil.TempDir("", "")
	if err != nil {
		return err
	}
	defer os.RemoveAll(root)

	if err := syscall.Mount("", "/", "", syscall.MS_PRIVATE|syscall.MS_REC, ""); err != nil {
		return errors.Wrapf(err, "unable to make root private")
	}

	if err := mountMountPoints(root, systemMountPoints()); err != nil {
		return errors.Wrapf(err, "unable to mount sandbox system mount points")
	}

	if err := mountMountPoints(root, config.MountPoints); err != nil {
		return errors.Wrapf(err, "unable to mount sandbox config mount points")
	}

	if err := pivotRoot(root); err != nil {
		return errors.Wrapf(err, "unable to pivot sandbox root")
	}

	if err := os.Mkdir("/tmp", 0755); err != nil {
		return errors.Wrapf(err, "unable to create /tmp")
	}

	if config.WorkingDirectory != "" {
		if err := os.Chdir(config.WorkingDirectory); err != nil {
			return errors.Wrapf(err, "unable to set working directory")
		}
	}

	if err := dropInheritableCapabilities(); err != nil {
		return errors.Wrapf(err, "unable to drop inheritable capabilities")
	}

	if err := enableSeccompFilter(); err != nil {
		return errors.Wrapf(err, "unable to enable seccomp filter")
	}

	return runExecutable(path)
}

func mountMountPoint(root string, mountPoint *MountPoint) error {
	isDir := true
	if mountPoint.Type == "" {
		stat, err := os.Lstat(mountPoint.Source)
		if err != nil {
			return nil
		}
		if (stat.Mode() & os.ModeSymlink) != 0 {
			if path, err := filepath.EvalSymlinks(mountPoint.Source); err == nil {
				newMountPoint := *mountPoint
				newMountPoint.Source = path
				if err := mountMountPoint(root, &newMountPoint); err != nil {
					return errors.Wrapf(err, "unable to mount symbolic link target: "+mountPoint.Source)
				}
				return nil
			}
		}
		isDir = stat.IsDir()
	}

	target := filepath.Join(root, mountPoint.Destination)

	if isDir {
		if err := os.MkdirAll(target, 0755); err != nil {
			return errors.Wrapf(err, "unable to create directory: "+target)
		}
	} else {
		if err := os.MkdirAll(filepath.Dir(target), 0755); err != nil {
			return errors.Wrapf(err, "unable to create directory: "+target)
		}
		f, err := os.Create(target)
		if err != nil {
			return errors.Wrapf(err, "unable to create file: "+target)
		}
		f.Close()
	}

	flags := uintptr(syscall.MS_NOSUID | syscall.MS_NODEV)
	if mountPoint.Type == "" {
		flags |= syscall.MS_BIND
	}
	if mountPoint.ReadOnly {
		flags |= syscall.MS_RDONLY
	}

	if err := syscall.Mount(mountPoint.Source, target, mountPoint.Type, flags, ""); err != nil {
		return errors.Wrapf(err, "unable to mount "+mountPoint.Source)
	}

	if (flags & syscall.MS_BIND) != 0 {
		// If this was a bind mount, our other flags actually got silently ignored during the above syscall:
		//
		//     If mountflags includes MS_BIND [...] The remaining bits in the mountflags argument are
		//     also ignored, with the exception of MS_REC.
		//
		// Furthermore, remounting will fail if we attempt to unset a bit that was inherited from
		// the mount's parent:
		//
		//     The mount(2) flags MS_RDONLY, MS_NOSUID, MS_NOEXEC, and the "atime" flags
		//     (MS_NOATIME, MS_NODIRATIME, MS_RELATIME) settings become locked when propagated from
		//     a more privileged to a less privileged mount namespace, and may not be changed in the
		//     less privileged mount namespace.
		//
		// So we need to get the actual flags, add our new ones, then do a remount if needed.
		var stats syscall.Statfs_t
		if err := syscall.Statfs(target, &stats); err != nil {
			return errors.Wrap(err, "unable to get mount flags for target: "+target)
		}
		const lockedFlagsMask = unix.MS_RDONLY | unix.MS_NOSUID | unix.MS_NOEXEC | unix.MS_NOATIME | unix.MS_NODIRATIME | unix.MS_RELATIME
		lockedFlags := uintptr(stats.Flags & lockedFlagsMask)
		if lockedFlags != ((flags | lockedFlags) & lockedFlagsMask) {
			if err := syscall.Mount("", target, "", flags|lockedFlags|syscall.MS_REMOUNT, ""); err != nil {
				return errors.Wrapf(err, "unable to remount "+mountPoint.Source)
			}
		}
	}

	return nil
}

func mountMountPoints(root string, mountPoints []*MountPoint) error {
	for _, mountPoint := range mountPoints {
		if err := mountMountPoint(root, mountPoint); err != nil {
			return err
		}
	}

	return nil
}

func pivotRoot(newRoot string) error {
	if err := syscall.Mount(newRoot, newRoot, "", syscall.MS_BIND|syscall.MS_REC, ""); err != nil {
		return errors.Wrapf(err, "unable to mount new root")
	}

	prevRoot := filepath.Join(newRoot, ".prev_root")

	if err := os.MkdirAll(prevRoot, 0700); err != nil {
		return errors.Wrapf(err, "unable to create directory for previous root")
	}

	if err := syscall.PivotRoot(newRoot, prevRoot); err != nil {
		return errors.Wrapf(err, "syscall error")
	}

	if err := os.Chdir("/"); err != nil {
		return errors.Wrapf(err, "unable to change directory")
	}

	prevRoot = "/.prev_root"

	if err := syscall.Unmount(prevRoot, syscall.MNT_DETACH); err != nil {
		return errors.Wrapf(err, "unable to unmount previous root")
	}

	if err := os.RemoveAll(prevRoot); err != nil {
		return errors.Wrapf(err, "unable to remove previous root directory")
	}

	return nil
}

func dropInheritableCapabilities() error {
	type capHeader struct {
		version uint32
		pid     int
	}

	type capData struct {
		effective   uint32
		permitted   uint32
		inheritable uint32
	}

	var hdr capHeader
	var data [2]capData

	if _, _, errno := syscall.Syscall(syscall.SYS_CAPGET, uintptr(unsafe.Pointer(&hdr)), 0, 0); errno != 0 {
		return errors.Wrapf(syscall.Errno(errno), "unable to get capabilities version")
	}

	if _, _, errno := syscall.Syscall(syscall.SYS_CAPGET, uintptr(unsafe.Pointer(&hdr)), uintptr(unsafe.Pointer(&data[0])), 0); errno != 0 {
		return errors.Wrapf(syscall.Errno(errno), "unable to get capabilities")
	}

	data[0].inheritable = 0
	data[1].inheritable = 0
	if _, _, errno := syscall.Syscall(syscall.SYS_CAPSET, uintptr(unsafe.Pointer(&hdr)), uintptr(unsafe.Pointer(&data[0])), 0); errno != 0 {
		return errors.Wrapf(syscall.Errno(errno), "unable to set inheritable capabilities")
	}

	for i := 0; i < 64; i++ {
		if _, _, errno := syscall.Syscall(syscall.SYS_PRCTL, syscall.PR_CAPBSET_DROP, uintptr(i), 0); errno != 0 && errno != syscall.EINVAL {
			return errors.Wrapf(syscall.Errno(errno), "unable to drop bounding set capability")
		}
	}

	return nil
}

func enableSeccompFilter() error {
	return EnableSeccompFilter(SeccompFilter(NATIVE_AUDIT_ARCH, AllowedSyscalls))
}

func runExecutable(path string) error {
	childFiles := []*os.File{
		os.NewFile(3, ""), os.NewFile(4, ""),
	}
	defer childFiles[0].Close()
	defer childFiles[1].Close()

	cmd := exec.Command(path)
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
	cmd.ExtraFiles = childFiles
	cmd.SysProcAttr = &syscall.SysProcAttr{
		Pdeathsig: syscall.SIGTERM,
	}

	if err := cmd.Run(); err != nil {
		return err
	}

	return nil
}

type process struct {
	command *exec.Cmd
}

func newProcess(ctx context.Context, config *Configuration, path string) (rpcplugin.Process, io.ReadWriteCloser, error) {
	configJSON, err := json.Marshal(config)
	if err != nil {
		return nil, nil, err
	}

	ipc, childFiles, err := rpcplugin.NewIPC()
	if err != nil {
		return nil, nil, err
	}
	defer childFiles[0].Close()
	defer childFiles[1].Close()

	cmd := exec.CommandContext(ctx, "/proc/self/exe")
	cmd.Args = []string{"sandbox.runProcess", string(configJSON), path}
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
	cmd.ExtraFiles = childFiles

	cmd.SysProcAttr = &syscall.SysProcAttr{
		Cloneflags: syscall.CLONE_NEWNS | syscall.CLONE_NEWUTS | syscall.CLONE_NEWIPC | syscall.CLONE_NEWPID | syscall.CLONE_NEWUSER,
		Pdeathsig:  syscall.SIGTERM,
		GidMappings: []syscall.SysProcIDMap{
			{
				ContainerID: 0,
				HostID:      os.Getgid(),
				Size:        1,
			},
		},
		UidMappings: []syscall.SysProcIDMap{
			{
				ContainerID: 0,
				HostID:      os.Getuid(),
				Size:        1,
			},
		},
	}

	err = cmd.Start()
	if err != nil {
		ipc.Close()
		return nil, nil, err
	}

	return &process{
		command: cmd,
	}, ipc, nil
}

func (p *process) Wait() error {
	return p.command.Wait()
}

func init() {
	if len(os.Args) < 1 || os.Args[0] != "sandbox.checkSupportInNamespace" {
		return
	}

	if err := checkSupportInNamespace(); err != nil {
		fmt.Fprintf(os.Stderr, "%v", err.Error())
		os.Exit(1)
	}

	os.Exit(0)
}

func checkSupportInNamespace() error {
	root, err := ioutil.TempDir("", "")
	if err != nil {
		return err
	}
	defer os.RemoveAll(root)

	if err := syscall.Mount("", "/", "", syscall.MS_PRIVATE|syscall.MS_REC, ""); err != nil {
		return errors.Wrapf(err, "unable to make root private")
	}

	if err := mountMountPoints(root, systemMountPoints()); err != nil {
		return errors.Wrapf(err, "unable to mount sandbox system mount points")
	}

	if err := pivotRoot(root); err != nil {
		return errors.Wrapf(err, "unable to pivot sandbox root")
	}

	if err := dropInheritableCapabilities(); err != nil {
		return errors.Wrapf(err, "unable to drop inheritable capabilities")
	}

	if err := enableSeccompFilter(); err != nil {
		return errors.Wrapf(err, "unable to enable seccomp filter")
	}

	return nil
}

func checkSupport() error {
	if AllowedSyscalls == nil {
		return fmt.Errorf("unsupported architecture")
	}

	stderr := &bytes.Buffer{}

	cmd := exec.Command("/proc/self/exe")
	cmd.Args = []string{"sandbox.checkSupportInNamespace"}
	cmd.Stderr = stderr
	cmd.SysProcAttr = &syscall.SysProcAttr{
		Cloneflags: syscall.CLONE_NEWNS | syscall.CLONE_NEWUTS | syscall.CLONE_NEWIPC | syscall.CLONE_NEWPID | syscall.CLONE_NEWUSER,
		Pdeathsig:  syscall.SIGTERM,
		GidMappings: []syscall.SysProcIDMap{
			{
				ContainerID: 0,
				HostID:      os.Getgid(),
				Size:        1,
			},
		},
		UidMappings: []syscall.SysProcIDMap{
			{
				ContainerID: 0,
				HostID:      os.Getuid(),
				Size:        1,
			},
		},
	}

	if err := cmd.Start(); err != nil {
		return errors.Wrapf(err, "unable to create user namespace")
	}

	if err := cmd.Wait(); err != nil {
		if _, ok := err.(*exec.ExitError); ok {
			return errors.Wrapf(fmt.Errorf("%v", stderr.String()), "unable to prepare namespace")
		}
		return errors.Wrapf(err, "unable to prepare namespace")
	}

	return nil
}