summaryrefslogtreecommitdiffstats
path: root/jail.go
blob: fc6104aa038e387bb4cd60b543435892f87823ba (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
package jail

// #include <stdio.h>
// #include <stdlib.h>
// #include <string.h>
// #include <errno.h>
// #include <sys/param.h>
// #include <sys/jail.h>
// #include <sys/uio.h>
// #include <sys/types.h>
// #include <sys/socket.h>
// #include <netinet/in.h>
// #include <arpa/inet.h>
//
// void set_iov_field(struct iovec *iov, int field, void *val, size_t len) {
//     iov[field].iov_base = val;
//     iov[field].iov_len  = len;
// }
//
// void *get_iov_field(struct iovec *iov, int field, size_t *len) {
//     *len = iov[field].iov_len;
//     return iov[field].iov_base;
// }
//
// void print_iov(struct iovec *iov, int niov) {
//     for (int i = 0; i < niov; i++) {
//         if (iov[i].iov_base == NULL)
//             fprintf(stderr, "(NULL), %lu\n", iov[i].iov_len);
//         else
//             fprintf(stderr, "%s, %lu\n", iov[i].iov_base, iov[i].iov_len);
//     }
// }
//
// void set_int_val(int *dst, int src) {
//     *dst = src;
// }
//
// int get_errno() {
//     return errno;
// }
//
// struct in_addr *get_in_addr_ptr(struct in_addr *a, int idx) {
//     return &a[idx];
// }
//
// struct in6_addr *get_in6_addr_ptr(struct in6_addr *a, int idx) {
//     return &a[idx];
// }
import "C"
import "errors"
import "fmt"
import "unsafe"

const (
	JAIL_CREATE = C.JAIL_CREATE
	JAIL_UPDATE = C.JAIL_UPDATE
	JAIL_ATTACH = C.JAIL_ATTACH
	JAIL_DYING  = C.JAIL_DYING

	JAIL_SYS_DISABLE = C.JAIL_SYS_DISABLE
	JAIL_SYS_NEW     = C.JAIL_SYS_NEW
	JAIL_SYS_INHERIT = C.JAIL_SYS_INHERIT
)

var EPerm  = errors.New(C.GoString(C.strerror(C.EPERM)))
var EFault = errors.New(C.GoString(C.strerror(C.EFAULT)))
var EInval = errors.New(C.GoString(C.strerror(C.EINVAL)))
var EAgain = errors.New(C.GoString(C.strerror(C.EAGAIN)))
var ENoent = errors.New(C.GoString(C.strerror(C.ENOENT)))
var EExist = errors.New(C.GoString(C.strerror(C.EEXIST)))
var ENameTooLong = errors.New(C.GoString(C.strerror(C.ENAMETOOLONG)))

func mapToIov(params map[string]interface{}) (unsafe.Pointer, int, []unsafe.Pointer) {
	var freeList []unsafe.Pointer
	var i = 0

	iov := C.malloc(C.ulong(C.sizeof_struct_iovec * C.int(len(params)*2)))
	freeList = append(freeList, iov)

	for k, v := range params {
		c_key := C.CString(k)
		freeList = append(freeList, unsafe.Pointer(c_key))

		C.set_iov_field((*C.struct_iovec)(iov), C.int(i), unsafe.Pointer(c_key), C.strlen(c_key)+1)
		i++

		if k == "ip4.addr" {
			if v_ip, ok := v.(string); ok {
				ip := C.malloc(C.sizeof_struct_in_addr)
				freeList = append(freeList, ip)

				c_str := C.CString(v_ip)

				if C.inet_pton(C.AF_INET, c_str, ip) == -1 {
					panic("Invalid IPv4 address")
				}

				C.free(unsafe.Pointer(c_str))

				C.set_iov_field((*C.struct_iovec)(iov), C.int(i), ip, C.sizeof_struct_in_addr)
			} else if v_a, ok := v.([]string); ok {
				ips := C.malloc(C.ulong(C.sizeof_struct_in_addr * len(v_a)))
				freeList = append(freeList, ips)

				for i, ip := range v_a {
					c_str := C.CString(ip)
					ptr := C.get_in_addr_ptr((*C.struct_in_addr)(ips), C.int(i))

					if C.inet_pton(C.AF_INET, c_str, unsafe.Pointer(ptr)) == -1 {
						panic("Invalid IPv4 address")
					}

					C.free(unsafe.Pointer(c_str))
				}

				C.set_iov_field((*C.struct_iovec)(iov), C.int(i), ips, C.ulong(C.sizeof_struct_in_addr * len(v_a)))
			} else {
				panic("Unknown IPv4 type")
			}
		} else if k == "ip6.addr" {
			if v_ip, ok := v.(string); ok {
				ip := C.malloc(C.sizeof_struct_in6_addr)
				freeList = append(freeList, ip)

				c_str := C.CString(v_ip)

				if C.inet_pton(C.AF_INET6, c_str, ip) == -1 {
					panic("Invalid IPv6 address")
				}

				C.free(unsafe.Pointer(c_str))

				C.set_iov_field((*C.struct_iovec)(iov), C.int(i), ip, C.sizeof_struct_in6_addr)
			} else if v_a, ok := v.([]string); ok {
				ips := C.malloc(C.ulong(C.sizeof_struct_in6_addr * len(v_a)))
				freeList = append(freeList, ips)

				for i, ip := range v_a {
					c_str := C.CString(ip)
					ptr := C.get_in6_addr_ptr((*C.struct_in6_addr)(ips), C.int(i))

					if C.inet_pton(C.AF_INET6, c_str, unsafe.Pointer(ptr)) == -1 {
						panic("Invalid IPv6 address")
					}

					C.free(unsafe.Pointer(c_str))
				}

				C.set_iov_field((*C.struct_iovec)(iov), C.int(i), ips, C.ulong(C.sizeof_struct_in6_addr * len(v_a)))
			} else {
				panic("Unknown IPv6 type")
			}
		} else if v_i, ok := v.(int); ok {
			c_val := C.malloc(C.sizeof_int)
			freeList = append(freeList, unsafe.Pointer(c_val))

			C.set_int_val((*C.int)(c_val), C.int(v_i))
			C.set_iov_field((*C.struct_iovec)(iov), C.int(i), c_val, C.sizeof_int)
		} else if v_s, ok := v.(string); ok {
			c_val := C.CString(v_s)
			freeList = append(freeList, unsafe.Pointer(c_val))

			C.set_iov_field((*C.struct_iovec)(iov), C.int(i), unsafe.Pointer(c_val), C.strlen(c_val)+1)
		} else if _, ok := v.(bool); ok {
			C.set_iov_field((*C.struct_iovec)(iov), C.int(i), unsafe.Pointer(nil), C.ulong(0))
		} else {
			panic("Unknown type")
		}

		i++
	}

	return iov, len(params)*2, freeList
}

func iovToMap(iov unsafe.Pointer, niov int, params map[string]interface{}) {
	for i := 0; i < len(params)*2; i+=2 {
		var key *C.char
		var key_len C.ulong
		var val unsafe.Pointer
		var val_len C.ulong

		key = (*C.char)(C.get_iov_field((*C.struct_iovec)(iov), C.int(i+0), &key_len))
		val = C.get_iov_field((*C.struct_iovec)(iov), C.int(i+1), &val_len)

		go_key := C.GoString(key)
		if v, ok := params[go_key]; ok {
			if _, ok := v.(int); ok {
				params[go_key] = int(*(*C.int)(val))
			} else if _, ok := v.(string); ok {
				params[go_key] = C.GoString((*C.char)(val))
			} else if _, ok := v.(bool); ok {
				// XXX: noop for now
			} else {
				panic("Got unknown type from kernel")
			}
		} else {
			panic(fmt.Sprintf("Got unknown key from kernel: %v", go_key))
		}
	}
}

func freeIov(freeList []unsafe.Pointer) {
	for _, e := range freeList {
		C.free(e)
	}
}

func errnoToError() error {
	errno := C.get_errno()

	if errno == C.EPERM {
		return EPerm
	} else if errno == C.EFAULT {
		return EFault
	} else if errno == C.EINVAL {
		return EInval
	} else if errno == C.EAGAIN {
		return EAgain
	} else if errno == C.ENOENT {
		return ENoent
	} else if errno == C.EEXIST {
		return EExist
	} else if errno == C.ENAMETOOLONG {
		return ENameTooLong
	} else {
		return errors.New("Unknown error")
	}
}

func Set(params map[string]interface{}, flags int) (int, error) {
	iov, niov, freeList := mapToIov(params)

	defer freeIov(freeList)

	ret := C.jail_set((*C.struct_iovec)(iov), C.uint(niov), C.int(flags))
	if ret == -1 {
		return 0, errnoToError()
	}

	return int(ret), nil
}

func Get(params map[string]interface{}, flags int) (int, error) {
	iov, niov, freeList := mapToIov(params)

	defer freeIov(freeList)

	ret := C.jail_get((*C.struct_iovec)(iov), C.uint(niov), C.int(flags))
	if ret == -1 {
		return 0, errnoToError()
	}

	iovToMap(iov, niov, params)

	return int(ret), nil
}

func Attach(jid int) error {
	ret := C.jail_attach(C.int(jid))
	if ret == -1 {
		return errnoToError()
	}

	return nil
}

func Remove(jid int) error {
	ret := C.jail_remove(C.int(jid))
	if ret == -1 {
		return errnoToError()
	}

	return nil
}