/
starter.go
399 lines (344 loc) · 10.8 KB
/
starter.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
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
// Copyright (c) 2018-2019, Sylabs Inc. All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the
// LICENSE.md file distributed with the sources of this project regarding your
// rights to use or distribute this software.
package starter
/*
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/types.h>
#include "starter.h"
*/
// #cgo CFLAGS: -I../../../../../../cmd/starter/c/include
import "C"
import (
"encoding/json"
"fmt"
"strings"
"syscall"
"unsafe"
"github.com/opencontainers/runtime-spec/specs-go"
"github.com/sylabs/singularity/internal/pkg/sylog"
"github.com/sylabs/singularity/pkg/util/capabilities"
)
// CConfig is the common type for C.struct_cConfig
type CConfig *C.struct_cConfig
// Config represents structure to manipulate C starter configuration
type Config struct {
config CConfig
nsPath []byte
}
// NewConfig takes a pointer to C starter configuration and returns a
// pointer to a Config
func NewConfig(config CConfig) *Config {
return &Config{config: config, nsPath: make([]byte, 1)}
}
// GetIsSUID returns if SUID workflow is enabled or not
func (c *Config) GetIsSUID() bool {
if c.config.container.isSuid == 1 {
return true
}
return false
}
// GetContainerPid returns container process ID
func (c *Config) GetContainerPid() int {
return int(c.config.container.pid)
}
// SetInstance sets if starter should spawn instance or not
func (c *Config) SetInstance(instance bool) {
if instance {
c.config.container.isInstance = C.uchar(1)
} else {
c.config.container.isInstance = C.uchar(0)
}
}
// GetInstance returns if container run as instance or not
func (c *Config) GetInstance() bool {
if c.config.container.isInstance == 1 {
return true
}
return false
}
// SetNoNewPrivs sets NO_NEW_PRIVS flag
func (c *Config) SetNoNewPrivs(noprivs bool) {
if noprivs {
c.config.container.noNewPrivs = C.uchar(1)
} else {
c.config.container.noNewPrivs = C.uchar(0)
}
}
// GetNoNewPrivs returns if NO_NEW_PRIVS flag is set or not
func (c *Config) GetNoNewPrivs() bool {
if c.config.container.noNewPrivs == 1 {
return true
}
return false
}
// SetSharedMount sets if master/container shares mount point
func (c *Config) SetSharedMount(shared bool) {
if shared {
c.config.container.sharedMount = C.uchar(1)
} else {
c.config.container.sharedMount = C.uchar(0)
}
}
// GetSharedMount returns if master/container shares mount point or not
func (c *Config) GetSharedMount() bool {
if c.config.container.sharedMount == 1 {
return true
}
return false
}
// SetJoinMount sets if container process join a mount namespace
func (c *Config) SetJoinMount(join bool) {
if join {
c.config.container.joinMount = C.uchar(1)
} else {
c.config.container.joinMount = C.uchar(0)
}
}
// GetJoinMount returns if container process join a mount namespace
func (c *Config) GetJoinMount() bool {
if c.config.container.joinMount == 1 {
return true
}
return false
}
// SetBringLoopbackInterface sets if starter bring loopback network interface
func (c *Config) SetBringLoopbackInterface(bring bool) {
if bring {
c.config.container.bringLoopbackInterface = C.uchar(1)
} else {
c.config.container.bringLoopbackInterface = C.uchar(0)
}
}
// GetBringLoopbackInterface returns if starter bring loopback network interface
func (c *Config) GetBringLoopbackInterface() bool {
if c.config.container.bringLoopbackInterface == 1 {
return true
}
return false
}
// SetMountPropagation sets root filesystem mount propagation
func (c *Config) SetMountPropagation(propagation string) {
var flags uintptr
switch propagation {
case "shared", "rshared":
flags = syscall.MS_SHARED
case "slave", "rslave":
flags = syscall.MS_SLAVE
case "private", "rprivate":
flags = syscall.MS_SHARED
case "unbindable", "runbindable":
flags = syscall.MS_UNBINDABLE
}
if strings.HasPrefix(propagation, "r") {
flags |= syscall.MS_REC
}
c.config.container.mountPropagation = C.ulong(flags)
}
// GetJSONConfig returns pointer to JSON configuration
func (c *Config) GetJSONConfig() []byte {
return C.GoBytes(unsafe.Pointer(&c.config.json.config[0]), C.int(c.config.json.size))
}
// WriteConfig writes raw C configuration
func (c *Config) Write(payload interface{}) error {
jsonConf, err := json.Marshal(payload)
if err != nil {
return fmt.Errorf("failed to marshal payload: %s", err)
}
size := len(jsonConf)
maxSize := C.MAX_JSON_SIZE - 1
c.config.json.size = C.size_t(size)
if size >= maxSize {
return fmt.Errorf("json configuration too big %d > %d", size, maxSize)
}
json := C.CBytes(jsonConf)
C.memcpy(unsafe.Pointer(&c.config.json.config[0]), json, c.config.json.size)
C.free(json)
return nil
}
// AddUIDMappings sets user namespace UID mapping.
func (c *Config) AddUIDMappings(uids []specs.LinuxIDMapping) error {
uidMap := ""
for i, uid := range uids {
if i == 0 {
c.SetTargetUID(int(uid.ContainerID))
}
uidMap = uidMap + fmt.Sprintf("%d %d %d\n", uid.ContainerID, uid.HostID, uid.Size)
}
l := len(uidMap)
if l >= C.MAX_MAP_SIZE-1 {
return fmt.Errorf("UID map too big")
}
if l > 0 {
cpath := unsafe.Pointer(C.CString(uidMap))
size := C.size_t(l)
C.memcpy(unsafe.Pointer(&c.config.container.uidMap[0]), cpath, size)
C.free(cpath)
}
return nil
}
// AddGIDMappings sets user namespace GID mapping
func (c *Config) AddGIDMappings(gids []specs.LinuxIDMapping) error {
var targetGids []int
gidMap := ""
for _, gid := range gids {
targetGids = append(targetGids, int(gid.ContainerID))
gidMap = gidMap + fmt.Sprintf("%d %d %d\n", gid.ContainerID, gid.HostID, gid.Size)
}
if len(targetGids) != 0 {
c.SetTargetGID(targetGids)
}
l := len(gidMap)
if l >= C.MAX_MAP_SIZE-1 {
return fmt.Errorf("GID map too big")
}
if l > 0 {
cpath := unsafe.Pointer(C.CString(gidMap))
size := C.size_t(l)
C.memcpy(unsafe.Pointer(&c.config.container.gidMap[0]), cpath, size)
C.free(cpath)
}
return nil
}
// SetNsFlags sets namespaces flag directly from flags argument
func (c *Config) SetNsFlags(flags int) {
c.config.namespace.flags = C.uint(flags)
}
// SetNsFlagsFromSpec sets namespaces flag from OCI spec
func (c *Config) SetNsFlagsFromSpec(namespaces []specs.LinuxNamespace) {
c.config.namespace.flags = 0
for _, namespace := range namespaces {
if namespace.Path == "" {
switch namespace.Type {
case specs.UserNamespace:
c.config.namespace.flags |= syscall.CLONE_NEWUSER
case specs.IPCNamespace:
c.config.namespace.flags |= syscall.CLONE_NEWIPC
case specs.UTSNamespace:
c.config.namespace.flags |= syscall.CLONE_NEWUTS
case specs.PIDNamespace:
c.config.namespace.flags |= syscall.CLONE_NEWPID
case specs.NetworkNamespace:
c.config.namespace.flags |= syscall.CLONE_NEWNET
case specs.MountNamespace:
c.config.namespace.flags |= syscall.CLONE_NEWNS
case specs.CgroupNamespace:
c.config.namespace.flags |= 0x2000000
}
}
}
}
// SetNsPath sets corresponding namespace to be joined
func (c *Config) SetNsPath(nstype specs.LinuxNamespaceType, path string) error {
cpath := unsafe.Pointer(C.CString(path))
l := len(path)
size := C.size_t(l)
if l > C.MAX_NS_PATH_SIZE-1 {
return fmt.Errorf("%s namespace path too big", nstype)
}
switch nstype {
case specs.UserNamespace:
C.memcpy(unsafe.Pointer(&c.config.namespace.user[0]), cpath, size)
case specs.IPCNamespace:
C.memcpy(unsafe.Pointer(&c.config.namespace.ipc[0]), cpath, size)
case specs.UTSNamespace:
C.memcpy(unsafe.Pointer(&c.config.namespace.uts[0]), cpath, size)
case specs.PIDNamespace:
C.memcpy(unsafe.Pointer(&c.config.namespace.pid[0]), cpath, size)
case specs.NetworkNamespace:
C.memcpy(unsafe.Pointer(&c.config.namespace.network[0]), cpath, size)
case specs.MountNamespace:
C.memcpy(unsafe.Pointer(&c.config.namespace.mount[0]), cpath, size)
case specs.CgroupNamespace:
C.memcpy(unsafe.Pointer(&c.config.namespace.cgroup[0]), cpath, size)
}
C.free(cpath)
return nil
}
// SetNsPathFromSpec sets corresponding namespace to be joined from OCI spec
func (c *Config) SetNsPathFromSpec(namespaces []specs.LinuxNamespace) error {
for _, namespace := range namespaces {
if namespace.Path != "" {
cpath := unsafe.Pointer(C.CString(namespace.Path))
l := len(namespace.Path)
size := C.size_t(l)
if l > C.MAX_NS_PATH_SIZE-1 {
return fmt.Errorf("%s namespace path too big", namespace.Type)
}
switch namespace.Type {
case specs.UserNamespace:
C.memcpy(unsafe.Pointer(&c.config.namespace.user[0]), cpath, size)
case specs.IPCNamespace:
C.memcpy(unsafe.Pointer(&c.config.namespace.ipc[0]), cpath, size)
case specs.UTSNamespace:
C.memcpy(unsafe.Pointer(&c.config.namespace.uts[0]), cpath, size)
case specs.PIDNamespace:
C.memcpy(unsafe.Pointer(&c.config.namespace.pid[0]), cpath, size)
case specs.NetworkNamespace:
C.memcpy(unsafe.Pointer(&c.config.namespace.network[0]), cpath, size)
case specs.MountNamespace:
C.memcpy(unsafe.Pointer(&c.config.namespace.mount[0]), cpath, size)
case specs.CgroupNamespace:
C.memcpy(unsafe.Pointer(&c.config.namespace.cgroup[0]), cpath, size)
}
C.free(cpath)
}
}
return nil
}
// SetCapabilities sets corresponding capability set identified by ctype
// from a capability string list identified by ctype
func (c *Config) SetCapabilities(ctype string, caps []string) {
switch ctype {
case capabilities.Permitted:
c.config.capabilities.permitted = 0
for _, v := range caps {
c.config.capabilities.permitted |= C.ulonglong(1 << capabilities.Map[v].Value)
}
case capabilities.Effective:
c.config.capabilities.effective = 0
for _, v := range caps {
c.config.capabilities.effective |= C.ulonglong(1 << capabilities.Map[v].Value)
}
case capabilities.Inheritable:
c.config.capabilities.inheritable = 0
for _, v := range caps {
c.config.capabilities.inheritable |= C.ulonglong(1 << capabilities.Map[v].Value)
}
case capabilities.Bounding:
c.config.capabilities.bounding = 0
for _, v := range caps {
c.config.capabilities.bounding |= C.ulonglong(1 << capabilities.Map[v].Value)
}
case capabilities.Ambient:
c.config.capabilities.ambient = 0
for _, v := range caps {
c.config.capabilities.ambient |= C.ulonglong(1 << capabilities.Map[v].Value)
}
}
}
// SetTargetUID sets target UID to execute the container process as user ID
func (c *Config) SetTargetUID(uid int) {
c.config.container.targetUID = C.uid_t(uid)
}
// SetTargetGID sets target GIDs to execute container process as group IDs
func (c *Config) SetTargetGID(gids []int) {
c.config.container.numGID = C.int(len(gids))
for i, gid := range gids {
if i > C.MAX_GID {
sylog.Warningf("you can't specify more than %d group IDs", C.MAX_GID)
break
}
c.config.container.targetGID[i] = C.gid_t(gid)
}
}
// Release performs a unmap on starter config and release mapped memory
func (c *Config) Release() error {
if C.munmap(unsafe.Pointer(c.config), C.sizeof_struct_cConfig) != 0 {
return fmt.Errorf("failed to release starter memory")
}
return nil
}