-
-
Notifications
You must be signed in to change notification settings - Fork 160
/
set_load.go
339 lines (272 loc) · 7.12 KB
/
set_load.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
package idmap
import (
"bufio"
"encoding/json"
"fmt"
"os"
"os/exec"
"os/user"
"strconv"
"strings"
"github.com/lxc/incus/v6/shared/util"
)
// ErrNoUserMap indicates that no entry could be found for the user.
var ErrNoUserMap = fmt.Errorf("No map found for user")
// ErrSubidUnsupported indicates that the system is lacking support for subuid/subgid.
var ErrSubidUnsupported = fmt.Errorf("System doesn't support subuid/subgid")
// DefaultFullKernelSet is the default Set of uid/gid with no mapping at all.
var DefaultFullKernelSet = &Set{Entries: []Entry{
{true, false, int64(0), int64(0), int64(4294967294)},
{false, true, int64(0), int64(0), int64(4294967294)},
}}
// NewSetFromJSON unpacks an idmap Set from its JSON representation.
func NewSetFromJSON(data string) (*Set, error) {
ret := &Set{}
err := json.Unmarshal([]byte(data), &ret.Entries)
if err != nil {
return nil, err
}
if len(ret.Entries) == 0 {
return nil, nil
}
return ret, nil
}
// NewSetFromIncusIDMap parses an Incus raw.idmap into a new idmap Set.
func NewSetFromIncusIDMap(value string) (*Set, error) {
getRange := func(r string) (int64, int64, error) {
entries := strings.Split(r, "-")
if len(entries) > 2 {
return -1, -1, fmt.Errorf("Invalid ID map range: %s", r)
}
base, err := strconv.ParseInt(entries[0], 10, 64)
if err != nil {
return -1, -1, err
}
size := int64(1)
if len(entries) > 1 {
size, err = strconv.ParseInt(entries[1], 10, 64)
if err != nil {
return -1, -1, err
}
size -= base
size++
}
return base, size, nil
}
ret := &Set{}
for _, line := range strings.Split(value, "\n") {
if line == "" {
continue
}
entries := strings.Split(line, " ")
if len(entries) != 3 {
return nil, fmt.Errorf("Invalid ID map line: %s", line)
}
outsideBase, outsideSize, err := getRange(entries[1])
if err != nil {
return nil, err
}
insideBase, insideSize, err := getRange(entries[2])
if err != nil {
return nil, err
}
if insideSize != outsideSize {
return nil, fmt.Errorf("The ID map ranges are of different sizes: %s", line)
}
entry := Entry{
HostID: outsideBase,
NSID: insideBase,
MapRange: insideSize,
}
switch entries[0] {
case "both":
entry.IsUID = true
entry.IsGID = true
err := ret.AddSafe(entry)
if err != nil {
return nil, err
}
case "uid":
entry.IsUID = true
err := ret.AddSafe(entry)
if err != nil {
return nil, err
}
case "gid":
entry.IsGID = true
err := ret.AddSafe(entry)
if err != nil {
return nil, err
}
default:
return nil, fmt.Errorf("Invalid ID map type: %s", line)
}
}
return ret, nil
}
// NewSetFromCurrentProcess returns a Set from the process' current uid/gid map.
func NewSetFromCurrentProcess() (*Set, error) {
// Check if system doesn't have user namespaces.
if !util.PathExists("/proc/self/uid_map") || !util.PathExists("/proc/self/gid_map") {
// Without a user namespace, just return the full map.
return DefaultFullKernelSet, nil
}
ret := &Set{}
// Parse the uidmap.
entries, err := getFromProc("/proc/self/uid_map")
if err != nil {
return nil, err
}
for _, entry := range entries {
e := Entry{IsUID: true, NSID: entry[0], HostID: entry[1], MapRange: entry[2]}
ret.Entries = append(ret.Entries, e)
}
// Parse the gidmap.
entries, err = getFromProc("/proc/self/gid_map")
if err != nil {
return nil, err
}
for _, entry := range entries {
e := Entry{IsGID: true, NSID: entry[0], HostID: entry[1], MapRange: entry[2]}
ret.Entries = append(ret.Entries, e)
}
return ret, nil
}
// NewSetFromSystem returns a Set for the specified user from the system's subuid/subgid configuration.
func NewSetFromSystem(rootfs string, username string) (*Set, error) {
// Check if the system supports subuid/subgid.
pathNewUIDMap, _ := exec.LookPath("newuidmap")
pathNewGIDMap, _ := exec.LookPath("newgidmap")
if !util.PathExists("/etc/subuid") || !util.PathExists("/etc/subgid") || pathNewUIDMap == "" || pathNewGIDMap == "" {
return nil, ErrSubidUnsupported
}
// If no username specified, use the current user.
if username == "" {
currentUser, err := user.Current()
if err != nil {
return nil, err
}
username = currentUser.Username
}
ret := &Set{}
// Parse the shadow uidmap.
entries, err := getFromShadow("/etc/subuid", username)
if err != nil {
if err != ErrNoUserMap {
return nil, err
}
// Try using the uid instead.
u, err := user.Lookup(username)
if err != nil {
return nil, ErrNoUserMap
}
entries, err = getFromShadow("/etc/subuid", u.Uid)
if err != nil {
return nil, err
}
}
for _, entry := range entries {
e := Entry{IsUID: true, NSID: 0, HostID: entry[0], MapRange: entry[1]}
ret.Entries = append(ret.Entries, e)
}
// Parse the shadow gidmap.
entries, err = getFromShadow("/etc/subgid", username)
if err != nil {
if err != ErrNoUserMap {
return nil, err
}
// Try using the uid instead.
u, err := user.Lookup(username)
if err != nil {
return nil, ErrNoUserMap
}
entries, err = getFromShadow("/etc/subgid", u.Uid)
if err != nil {
return nil, err
}
}
for _, entry := range entries {
e := Entry{IsGID: true, NSID: 0, HostID: entry[0], MapRange: entry[1]}
ret.Entries = append(ret.Entries, e)
}
return ret, nil
}
func getFromShadow(fname string, username string) ([][]int64, error) {
entries := [][]int64{}
f, err := os.Open(fname)
if err != nil {
return nil, err
}
defer func() { _ = f.Close() }()
scanner := bufio.NewScanner(f)
for scanner.Scan() {
// Skip comments.
s := strings.Split(scanner.Text(), "#")
if len(s[0]) == 0 {
continue
}
// Validate format.
s = strings.Split(s[0], ":")
if len(s) < 3 {
return nil, fmt.Errorf("Unexpected values in %q: %q", fname, s)
}
if strings.EqualFold(s[0], username) {
// Get range start.
entryStart, err := strconv.ParseUint(s[1], 10, 32)
if err != nil {
continue
}
// Get range size.
entrySize, err := strconv.ParseUint(s[2], 10, 32)
if err != nil {
continue
}
entries = append(entries, []int64{int64(entryStart), int64(entrySize)})
}
}
if len(entries) == 0 {
return nil, ErrNoUserMap
}
return entries, nil
}
func getFromProc(fname string) ([][]int64, error) {
entries := [][]int64{}
f, err := os.Open(fname)
if err != nil {
return nil, err
}
defer func() { _ = f.Close() }()
scanner := bufio.NewScanner(f)
for scanner.Scan() {
// Skip comments.
s := strings.Split(scanner.Text(), "#")
if len(s[0]) == 0 {
continue
}
// Validate format.
s = strings.Fields(s[0])
if len(s) < 3 {
return nil, fmt.Errorf("Unexpected values in %q: %q", fname, s)
}
// Get range start.
entryStart, err := strconv.ParseUint(s[0], 10, 32)
if err != nil {
continue
}
// Get range size.
entryHost, err := strconv.ParseUint(s[1], 10, 32)
if err != nil {
continue
}
// Get range size.
entrySize, err := strconv.ParseUint(s[2], 10, 32)
if err != nil {
continue
}
entries = append(entries, []int64{int64(entryStart), int64(entryHost), int64(entrySize)})
}
if len(entries) == 0 {
return nil, fmt.Errorf("Namespace doesn't have any map set")
}
return entries, nil
}