-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathminibox.go
187 lines (163 loc) · 4.64 KB
/
minibox.go
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
package main
import (
"flag"
"fmt"
"log"
"os"
"path/filepath"
"strings"
"syscall"
"github.com/pkg/errors"
"golang.org/x/sys/unix"
)
/*
#cgo CFLAGS: -std=c99
#define _GNU_SOURCE
#include <linux/loop.h>
#include <sched.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mount.h>
#include <sys/syscall.h>
#include <unistd.h>
void execWrapper(char* path, int argc, char *argstr) {
char** argv = malloc((argc+2) * sizeof(char*));
argv[0] = path;
for (int i = 0; i < argc; i++) {
argv[i+1] = argstr;
argstr += strlen(argstr) + 1;
}
argv[argc+1] = NULL;
execve(path, argv, NULL);
free(argv);
perror("execv");
exit(1);
}
*/
import "C"
var (
CLONE_FS = int(C.CLONE_FS)
CLONE_NEWNS = int(C.CLONE_NEWNS)
LOOP_CTL_GET_FREE = uint(C.LOOP_CTL_GET_FREE)
LOOP_SET_FD = uint(C.LOOP_SET_FD)
LOOP_CLR_FD = uint(C.LOOP_CLR_FD)
MNT_DETACH = int(C.MNT_DETACH)
)
func main() {
log.SetFlags(0)
exitCode, err := run(os.Args)
if err != nil {
log.Print(err)
}
os.Exit(exitCode)
}
func run(args []string) (exitCode int, err error) {
closeFn := func(name string, fd int) {
if closeErr := unix.Close(fd); err == nil && closeErr != nil {
err = errors.Wrapf(closeErr, "close %s", name)
}
}
flags := flag.NewFlagSet(args[0], flag.ExitOnError)
dirFlag := flags.String("dir", "", "the mount point for the container")
imageFlag := flags.String("image", "", "the container image to open")
fstypeFlag := flags.String("fstype", "", "the file system type of the container")
entryFlag := flags.String("entry", "", "name of the binary to execute within the container")
uidFlag := flags.Int("uid", -1, "the uid under which the binary should be run")
gidFlag := flags.Int("gid", -1, "the gid under which the binary should be run")
if err := flags.Parse(args[1:]); err != nil {
return 1, err
}
dir := *dirFlag
image := *imageFlag
fstype := *fstypeFlag
entry := *entryFlag
uid := *uidFlag
gid := *gidFlag
if dir == "" || image == "" || fstype == "" || entry == "" || uid < 0 || gid < 0 {
flags.PrintDefaults()
os.Exit(1)
}
if err := unix.Unshare(CLONE_FS | CLONE_NEWNS); err != nil {
return 1, errors.Wrap(err, "unshare")
}
loopctlFd, err := unix.Open("/dev/loop-control", syscall.O_RDWR, 0)
if err != nil {
return 1, errors.Wrapf(err, "open /dev/loop-control", err)
}
defer closeFn("/dev/loop-control", loopctlFd)
devNum, err := unix.IoctlGetInt(loopctlFd, LOOP_CTL_GET_FREE)
if err != nil {
return 1, errors.Wrap(err, "ioctl LOOP_CTL_GET_FREE")
}
loopDevName := fmt.Sprintf("/dev/loop%d", devNum)
loopFd, err := unix.Open(loopDevName, syscall.O_RDWR, 0)
if err != nil {
return 1, errors.Wrapf(err, "open %s", loopDevName)
}
defer closeFn(loopDevName, loopFd)
imageFd, err := unix.Open(image, syscall.O_RDWR, 0)
if err != nil {
return 1, errors.Wrapf(err, "open %s", image)
}
defer closeFn(image, imageFd)
if err := unix.IoctlSetInt(loopFd, LOOP_SET_FD, imageFd); err != nil {
return 1, errors.Wrap(err, "ioctl LOOP_SET_FD")
}
defer func() {
if _, clearErr := unix.IoctlGetInt(loopFd, LOOP_CLR_FD); clearErr != nil && err == nil {
err = clearErr
}
}()
if err := unix.Mount(loopDevName, dir, fstype, 0, ""); err != nil {
return 1, errors.Wrap(err, "mount")
}
pid, _, _ := unix.RawSyscall(uintptr(C.SYS_fork), 0, 0, 0)
if pid < 0 {
return 1, errors.New("fork")
}
if pid == 0 {
oldRootDir := filepath.Join(dir, ".old_root")
os.Mkdir(oldRootDir, 0700)
if err := unix.PivotRoot(dir, oldRootDir); err != nil {
log.Fatal(errors.Wrap(err, "pivot_root"))
}
if err := os.Chdir("/"); err != nil {
log.Fatal(errors.Wrap(err, "chdir"))
}
if err := unix.Unmount("/.old_root", MNT_DETACH); err != nil {
log.Fatal(errors.Wrap(err, "unmount"))
}
if err := os.Remove("/.old_root"); err != nil {
log.Fatal(errors.Wrap(err, "remove"))
}
ret, _, _ := unix.RawSyscall(uintptr(C.SYS_setgid), uintptr(gid), 0, 0)
if ret < 0 {
log.Fatal("setgid")
}
ret, _, _ = unix.RawSyscall(uintptr(C.SYS_setuid), uintptr(uid), 0, 0)
if ret < 0 {
log.Fatal("setuid")
}
cEntry := C.CString(entry)
cArgc := C.int(len(flag.Args()))
cArgstr := C.CString(strings.Join(flag.Args(), "\x00") + "\x00")
C.execWrapper(cEntry, cArgc, cArgstr)
}
for {
var status unix.WaitStatus
if _, err = unix.Wait4(int(pid), &status, 0, nil); err != nil {
return 1, errors.Wrap(err, "wait4")
}
if status.Signaled() {
return 1, errors.Errorf("process terminated by signal %v", status.Signal())
}
if status.Exited() {
return status.ExitStatus(), nil
}
if status.Stopped() || status.Continued() {
continue
}
return 1, errors.Errorf("unknown return from wait: %x", status)
}
}