-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspec.go
More file actions
190 lines (175 loc) · 5.19 KB
/
Copy pathspec.go
File metadata and controls
190 lines (175 loc) · 5.19 KB
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
package gvisorexec
import (
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
)
// buildSpec generates the OCI runtime-spec JSON matching c.
func buildSpec(c Config) ([]byte, error) {
if err := validate(c); err != nil {
return nil, err
}
spec := ociSpec{
OCIVersion: "1.0.0",
Process: ociProcess{
Terminal: false,
User: ociUser{UID: c.UID, GID: c.GID},
Args: c.Args,
Env: c.Env,
Cwd: c.Cwd,
Capabilities: ociCapabilities{
Bounding: []string{},
Effective: []string{},
Permitted: []string{},
Inheritable: []string{},
Ambient: []string{},
},
Rlimits: []ociRlimit{{Type: "RLIMIT_NOFILE", Hard: 65536, Soft: 65536}},
NoNewPrivileges: true,
},
Root: ociRoot{Path: c.Rootfs, Readonly: true},
Hostname: c.Hostname,
Mounts: []ociMount{
{Destination: "/proc", Type: "proc", Source: "proc"},
{Destination: "/dev", Type: "tmpfs", Source: "tmpfs"},
{Destination: "/sys", Type: "sysfs", Source: "sysfs", Options: []string{"nosuid", "noexec", "nodev", "ro"}},
{Destination: "/tmp", Type: "tmpfs", Source: "tmpfs", Options: []string{"nosuid", "nodev", "mode=1777"}},
},
Linux: ociLinux{
Namespaces: []ociNamespace{
{Type: "pid"},
{Type: "network"},
{Type: "ipc"},
{Type: "uts"},
{Type: "mount"},
},
},
}
for _, m := range c.ROBindMounts {
spec.Mounts = append(spec.Mounts, ociMount{
Destination: m.Destination,
Type: "bind",
Source: m.Source,
Options: []string{"bind", "ro"},
})
}
for _, m := range c.BindMounts {
spec.Mounts = append(spec.Mounts, ociMount{
Destination: m.Destination,
Type: "bind",
Source: m.Source,
Options: []string{"bind", "rw"},
})
}
for _, dst := range c.Tmpfs {
if dst == "/tmp" {
continue
}
spec.Mounts = append(spec.Mounts, ociMount{
Destination: dst,
Type: "tmpfs",
Source: "tmpfs",
Options: []string{"nosuid", "nodev", "mode=1777"},
})
}
return json.MarshalIndent(spec, "", " ")
}
func validate(c Config) error {
if len(c.Args) == 0 {
return errors.New("gvisorexec: config: Args is empty")
}
switch c.Platform {
case "systrap", "ptrace", "kvm":
default:
return fmt.Errorf("gvisorexec: config: invalid platform %q", c.Platform)
}
switch c.Network {
case "none", "host", "sandbox":
default:
return fmt.Errorf("gvisorexec: config: invalid network %q", c.Network)
}
switch c.Overlay {
case "self", "memory", "none":
default:
return fmt.Errorf("gvisorexec: config: invalid overlay %q", c.Overlay)
}
if !filepath.IsAbs(c.Rootfs) {
return fmt.Errorf("gvisorexec: config: rootfs must be absolute, got %q", c.Rootfs)
}
if c.Cwd != "" && !filepath.IsAbs(c.Cwd) {
return fmt.Errorf("gvisorexec: config: cwd must be absolute, got %q", c.Cwd)
}
for _, m := range append(append([]Mount{}, c.BindMounts...), c.ROBindMounts...) {
if !filepath.IsAbs(m.Source) {
return fmt.Errorf("gvisorexec: config: bind source must be absolute, got %q", m.Source)
}
if !filepath.IsAbs(m.Destination) {
return fmt.Errorf("gvisorexec: config: bind destination must be absolute, got %q", m.Destination)
}
if _, err := os.Stat(m.Source); err != nil {
return fmt.Errorf("gvisorexec: config: bind source %q: %w", m.Source, err)
}
// The gofer does not have permission to mkdir into a read-only
// host rootfs, so the destination must already exist when the
// sandbox rootfs is the host root.
if c.Rootfs == "/" {
destOnHost := m.Destination
if _, err := os.Stat(destOnHost); err != nil {
return fmt.Errorf("gvisorexec: bind destination %q does not exist on host; the gofer cannot create mount points in a read-only host rootfs", m.Destination)
}
}
}
return nil
}
type ociSpec struct {
OCIVersion string `json:"ociVersion"`
Process ociProcess `json:"process"`
Root ociRoot `json:"root"`
Hostname string `json:"hostname"`
Mounts []ociMount `json:"mounts"`
Linux ociLinux `json:"linux"`
}
type ociProcess struct {
Terminal bool `json:"terminal"`
User ociUser `json:"user"`
Args []string `json:"args"`
Env []string `json:"env"`
Cwd string `json:"cwd"`
Capabilities ociCapabilities `json:"capabilities"`
Rlimits []ociRlimit `json:"rlimits"`
NoNewPrivileges bool `json:"noNewPrivileges"`
}
type ociUser struct {
UID uint32 `json:"uid"`
GID uint32 `json:"gid"`
}
type ociCapabilities struct {
Bounding []string `json:"bounding"`
Effective []string `json:"effective"`
Permitted []string `json:"permitted"`
Inheritable []string `json:"inheritable"`
Ambient []string `json:"ambient"`
}
type ociRlimit struct {
Type string `json:"type"`
Hard uint64 `json:"hard"`
Soft uint64 `json:"soft"`
}
type ociRoot struct {
Path string `json:"path"`
Readonly bool `json:"readonly"`
}
type ociMount struct {
Destination string `json:"destination"`
Type string `json:"type"`
Source string `json:"source"`
Options []string `json:"options,omitempty"`
}
type ociLinux struct {
Namespaces []ociNamespace `json:"namespaces"`
}
type ociNamespace struct {
Type string `json:"type"`
}