File: jail.go

package info (click to toggle)
golang-github-containers-buildah 1.39.3%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 7,724 kB
  • sloc: sh: 2,398; makefile: 236; perl: 187; asm: 16; awk: 12; ansic: 1
file content (246 lines) | stat: -rw-r--r-- 6,001 bytes parent folder | download
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
//go:build freebsd
// +build freebsd

package jail

import (
	"fmt"
	"strconv"
	"strings"
	"sync"
	"syscall"
	"unsafe"

	"github.com/containers/buildah/pkg/util"
	"github.com/sirupsen/logrus"
	"golang.org/x/sys/unix"
)

type NS int32

const (
	DISABLED NS = 0
	NEW      NS = 1
	INHERIT  NS = 2

	JAIL_CREATE = 0x01
	JAIL_UPDATE = 0x02
	JAIL_ATTACH = 0x04
)

type config struct {
	params map[string]interface{}
}

var (
	needVnetJailOnce sync.Once
	needVnetJail     bool
)

func NewConfig() *config {
	return &config{
		params: make(map[string]interface{}),
	}
}

func handleBoolSetting(key string, val bool) (string, interface{}) {
	// jail doesn't deal with booleans - it uses paired parameter
	// names, e.g. "persist"/"nopersist". If the key contains '.',
	// the "no" prefix is applied to the last element.
	if val == false {
		parts := strings.Split(key, ".")
		parts[len(parts)-1] = "no" + parts[len(parts)-1]
		key = strings.Join(parts, ".")
	}
	return key, nil
}

func (c *config) Set(key string, value interface{}) {
	// Normalise integer types to int32
	switch v := value.(type) {
	case int:
		value = int32(v)
	case uint32:
		value = int32(v)
	}

	switch key {
	case "jid", "devfs_ruleset", "enforce_statfs", "children.max", "securelevel":
		if _, ok := value.(int32); !ok {
			logrus.Fatalf("value for parameter %s must be an int32", key)
		}
	case "ip4", "ip6", "host", "vnet":
		nsval, ok := value.(NS)
		if !ok {
			logrus.Fatalf("value for parameter %s must be a jail.NS", key)
		}
		if (key == "host" || key == "vnet") && nsval == DISABLED {
			logrus.Fatalf("value for parameter %s cannot be DISABLED", key)
		}
	case "persist", "sysvmsg", "sysvsem", "sysvshm":
		bval, ok := value.(bool)
		if !ok {
			logrus.Fatalf("value for parameter %s must be bool", key)
		}
		key, value = handleBoolSetting(key, bval)
	default:
		if strings.HasPrefix(key, "allow.") {
			bval, ok := value.(bool)
			if !ok {
				logrus.Fatalf("value for parameter %s must be bool", key)
			}
			key, value = handleBoolSetting(key, bval)
		} else {
			if _, ok := value.(string); !ok {
				logrus.Fatalf("value for parameter %s must be a string", key)
			}
		}
	}
	c.params[key] = value
}

func (c *config) getIovec() ([]syscall.Iovec, error) {
	jiov := make([]syscall.Iovec, 0)
	for key, value := range c.params {
		iov, err := stringToIovec(key)
		if err != nil {
			return nil, err
		}
		jiov = append(jiov, iov)
		switch v := value.(type) {
		case string:
			iov, err := stringToIovec(v)
			if err != nil {
				return nil, err
			}
			jiov = append(jiov, iov)
		case int32:
			jiov = append(jiov, syscall.Iovec{
				Base: (*byte)(unsafe.Pointer(&v)),
				Len:  4,
			})
		case NS:
			jiov = append(jiov, syscall.Iovec{
				Base: (*byte)(unsafe.Pointer(&v)),
				Len:  4,
			})
		default:
			jiov = append(jiov, syscall.Iovec{
				Base: nil,
				Len:  0,
			})
		}
	}
	return jiov, nil
}

type jail struct {
	jid int32
}

func jailSet(jconf *config, flags int) (*jail, error) {
	jiov, err := jconf.getIovec()
	if err != nil {
		return nil, err
	}

	jid, _, errno := syscall.Syscall(unix.SYS_JAIL_SET, uintptr(unsafe.Pointer(&jiov[0])), uintptr(len(jiov)), uintptr(flags))
	if errno != 0 {
		return nil, errno
	}
	return &jail{
		jid: int32(jid),
	}, nil
}

func jailGet(jconf *config, flags int) (*jail, error) {
	jiov, err := jconf.getIovec()
	if err != nil {
		return nil, err
	}

	jid, _, errno := syscall.Syscall(unix.SYS_JAIL_GET, uintptr(unsafe.Pointer(&jiov[0])), uintptr(len(jiov)), uintptr(flags))
	if errno != 0 {
		return nil, errno
	}
	return &jail{
		jid: int32(jid),
	}, nil
}

func Create(jconf *config) (*jail, error) {
	return jailSet(jconf, JAIL_CREATE)
}

func CreateAndAttach(jconf *config) (*jail, error) {
	return jailSet(jconf, JAIL_CREATE|JAIL_ATTACH)
}

func FindByName(name string) (*jail, error) {
	jconf := NewConfig()
	jconf.Set("name", name)
	return jailGet(jconf, 0)
}

func (j *jail) Set(jconf *config) error {
	jconf.Set("jid", j.jid)
	_, err := jailSet(jconf, JAIL_UPDATE)
	return err
}

func parseVersion(version string) (string, int, int, int, error) {
	// Expected formats:
	//	<major>.<minor>-RELEASE optionally followed by -p<patchlevel>
	//	<major>-STABLE
	//	<major>-CURRENT
	parts := strings.Split(string(version), "-")
	if len(parts) < 2 || len(parts) > 3 {
		return "", -1, -1, -1, fmt.Errorf("unexpected OS version: %s", version)
	}
	ver := strings.Split(parts[0], ".")

	if len(ver) != 2 {
		return "", -1, -1, -1, fmt.Errorf("unexpected OS version: %s", version)
	}
	major, err := strconv.Atoi(ver[0])
	if err != nil {
		return "", -1, -1, -1, fmt.Errorf("unexpected OS version: %s", version)
	}
	minor, err := strconv.Atoi(ver[1])
	if err != nil {
		return "", -1, -1, -1, fmt.Errorf("unexpected OS version: %s", version)
	}
	patchlevel := 0
	if len(parts) == 3 {
		if parts[1] != "RELEASE" || !strings.HasPrefix(parts[2], "p") {
			return "", -1, -1, -1, fmt.Errorf("unexpected OS version: %s", version)
		}
		patchlevel, err = strconv.Atoi(strings.TrimPrefix(parts[2], "p"))
		if err != nil {
			return "", -1, -1, -1, fmt.Errorf("unexpected OS version: %s", version)
		}
	}
	return parts[1], major, minor, patchlevel, nil
}

// Return true if its necessary to have a separate jail to own the vnet.  For
// FreeBSD 13.3 and later, we don't need a separate vnet jail since it is
// possible to configure the network without either attaching to the container's
// jail or trusting the ifconfig and route utilities in the container. If for
// any reason, we fail to parse the OS version, we default to returning true.
func NeedVnetJail() bool {
	needVnetJailOnce.Do(func() {
		// FreeBSD 13.3 and later have support for 'ifconfig -j' and 'route -j'
		needVnetJail = true
		version, err := util.ReadKernelVersion()
		if err != nil {
			logrus.Errorf("failed to determine OS version: %v", err)
			return
		}
		_, major, minor, _, err := parseVersion(version)
		if major > 13 || (major == 13 && minor > 2) {
			needVnetJail = false
		}
	})
	return needVnetJail
}