/
check_snap.go
677 lines (600 loc) · 23.4 KB
/
check_snap.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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2014-2016 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package snapstate
import (
"fmt"
"regexp"
"strconv"
"strings"
"github.com/snapcore/snapd/arch"
"github.com/snapcore/snapd/asserts"
"github.com/snapcore/snapd/logger"
"github.com/snapcore/snapd/osutil"
"github.com/snapcore/snapd/overlord/snapstate/backend"
"github.com/snapcore/snapd/overlord/state"
"github.com/snapcore/snapd/release"
seccomp_compiler "github.com/snapcore/snapd/sandbox/seccomp"
"github.com/snapcore/snapd/snap"
"github.com/snapcore/snapd/snapdtool"
)
// featureSet contains the flag values that can be listed in assumes entries
// that this ubuntu-core actually provides.
var featureSet = map[string]bool{
// Support for common data directory across revisions of a snap.
"common-data-dir": true,
// Support for the "Environment:" feature in snap.yaml
"snap-env": true,
// Support for the "command-chain" feature for apps and hooks in snap.yaml
"command-chain": true,
}
// supportedSystemUsernames for now contains the hardcoded list of system
// users (and implied system group of same name) that snaps may specify. This
// will eventually be moved out of here into the store.
//
// Since the snap is mounted read-only and to avoid problems associated with
// different systems using different uids and gids for the same user name and
// group name, snapd will create system-usernames where 'scope' is not
// 'external' (currently snapd only supports 'scope: shared') with the
// following characteristics:
//
// - uid and gid shall match for the specified system-username
// - a snapd-allocated [ug]id for a user/group name shall never change
// - snapd should avoid [ug]ids that are known to overlap with uid ranges of
// common use cases and user namespace container managers so that DAC and
// AppArmor owner match work as intended.
// - [ug]id shall be < 2^31 to avoid (at least) broken devpts kernel code
// - [ug]id shall be >= 524288 (0x00080000) to give plenty of room for large
// sites, default uid/gid ranges for docker (231072-296608), LXD installs
// that setup a default /etc/sub{uid,gid} (100000-165536) and podman whose
// tutorials reference setting up a specific default user and range
// (100000-165536)
// - [ug]id shall be < 1,000,000 and > 1,001,000,000 (ie, 1,000,000 subordinate
// uid with 1,000,000,000 range) to avoid overlapping with LXD's minimum and
// maximum id ranges. LXD allows for any id range >= 65536 and doesn't
// perform any [ug]id overlap detection with current users
// - [ug]ids assigned by snapd initially will fall within a 65536 (2^16) range
// (see below) where the first [ug]id in the range has the 16 lower bits all
// set to zero. This allows snapd to conveniently be bitwise aligned, follows
// sensible conventions (see https://systemd.io/UIDS-GIDS.html) but also
// potentially discoverable by systemd-nspawn (it assigns a different 65536
// range to each container. Its allocation algorithm is not sequential and
// may choose anything within its range that isn't already allocated. It's
// detection algorithm includes (effectively) performing a getpwent()
// operation on CANDIDATE_UID & 0XFFFF0000 and selecting another range if it
// is assigned).
//
// What [ug]id range(s) should snapd use?
//
// While snapd does not employ user namespaces, it will operate on systems with
// container managers that do and will assign from a range of [ug]ids. It is
// desirable that snapd assigns [ug]ids that minimally conflict with the system
// and other software (potential conflicts with admin-assigned ranges in
// /etc/subuid and /etc/subgid cannot be avoided, but can be documented as well
// as detected/logged). Overlapping with container managers is non-fatal for
// snapd and the container, but introduces the possibility that a uid in the
// container matches a uid a snap is using, which is undesirable in terms of
// security (eg, DoS via ulimit, same ownership of files between container and
// snap (even if the other's files are otherwise inaccessible), etc).
//
// snapd shall assign [ug]ids from range(s) of 65536 where the lowest value in
// the range has the 16 lower bits all set to zero (initially just one range,
// but snapd can add more as needed).
//
// To avoid [ug]id overlaps, snapd shall only assign [ug]ids >= 524288
// (0x00080000) and <= 983040 (0x000F0000, ie the first 65536 range under LXD's
// minimum where the lower 16 bits are all zeroes). While [ug]ids >= 1001062400
// (0x3BAB0000, the first 65536 range above LXD's maximum where the lower 16
// bits are all zeroes) would also avoid overlap, considering nested containers
// (eg, LXD snap runs a container that runs a container that runs snapd),
// choosing >= 1001062400 would mean that the admin would need to increase the
// LXD id range for these containers for snapd to be allowed to create its
// [ug]ids in the deeply nested containers. The requirements would both be an
// administrative burden and artificially limit the number of deeply nested
// containers the host could have.
//
// Looking at the LSB and distribution defaults for login.defs, we can observe
// uids and gids in the system's initial 65536 range (ie, 0-65536):
//
// - 0-99 LSB-suggested statically assigned range (eg, root, daemon,
// etc)
// - 0 mandatory 'root' user
// - 100-499 LSB-suggested dynamically assigned range for system users
// (distributions often prefer a higher range, see below)
// - 500-999 typical distribution default for dynamically assigned range
// for system users (some distributions use a smaller
// SYS_[GU]ID_MIN)
// - 1000-60000 typical distribution default for dynamically assigned range
// for regular users
// - 65535 (-1) should not be assigned since '-1' might be evaluated as this
// with set[ug]id* and chown families of functions
// - 65534 (-2) nobody/nogroup user for NFS/etc [ug]id anonymous squashing
// - 65519-65533 systemd recommended reserved range for site-local anonymous
// additions, etc
//
// To facilitate potential future use cases within the 65536 range snapd will
// assign from, snapd will only assign from the following subset of ranges
// relative to the range minimum (ie, its 'base' which has the lower 16 bits
// all set to zero):
//
// - 60500-60999 'scope: shared' system-usernames
// - 61000-65519 'scope: private' system-usernames
//
// Since the first [ug]id range must be >= 524288 and <= 983040 (see above) and
// following the above guide for system-usernames [ug]ids within this 65536
// range, the lowest 'scope: shared' user in this range is 584788 (0x0008EC54).
//
// Since this number is within systemd-nspawn's range of 524288-1879048191
// (0x00080000-0x6FFFFFFF), the number's lower 16 bits are not all zeroes so
// systemd-nspawn won't detect this allocation and could potentially assign the
// 65536 range starting at 0x00080000 to a container. snapd will therefore also
// create the 'snapd-range-524288-root' user and group with [ug]id 524288 to
// work within systemd-nspawn's collision detection. This user/group will not
// be assigned to snaps at this time.
//
// In short (phew!), use the following:
//
// $ snappy-debug.id-range 524288 # 0x00080000
// Host range: 524288-589823 (00080000-0008ffff; 0-65535)
// LSB static range: 524288-524387 (00080000-00080063; 0-99)
// Useradd system range: 524788-525287 (000801f4-000803e7; 500-999)
// Useradd regular range: 525288-584288 (000803e8-0008ea60; 1000-60000)
// Snapd system range: 584788-585287 (0008ec54-0008ee47; 60500-60999)
// Snapd private range: 585288-589807 (0008ee48-0008ffef; 61000-65519)
//
// Snapd is of course free to add more ranges (eg, 589824 (0x00090000)) with
// new snapd-range-<base>-root users, or to allocate differently within its
// 65536 range in the future (sequentially assigned [ug]ids are not required),
// but for now start very regimented to avoid as many problems as possible.
//
// References:
// https://forum.snapcraft.io/t/multiple-users-and-groups-in-snaps/
// https://systemd.io/UIDS-GIDS.html
// https://docs.docker.com/engine/security/userns-remap/
// https://github.com/lxc/lxd/blob/master/doc/userns-idmap.md
var supportedSystemUsernames = map[string]uint32{
"snap_daemon": 584788,
}
func checkAssumes(si *snap.Info) error {
missing := ([]string)(nil)
for _, flag := range si.Assumes {
if strings.HasPrefix(flag, "snapd") && checkVersion(flag[5:]) {
continue
}
if !featureSet[flag] {
missing = append(missing, flag)
}
}
if len(missing) > 0 {
hint := "try to refresh the core or snapd snaps"
if release.OnClassic {
hint = "try to update snapd and refresh the core snap"
}
return fmt.Errorf("snap %q assumes unsupported features: %s (%s)", si.InstanceName(), strings.Join(missing, ", "), hint)
}
return nil
}
// regular expression which matches a version expressed as groups of digits
// separated with dots, with optional non-numbers afterwards
var versionExp = regexp.MustCompile(`^(?:[1-9][0-9]*)(?:\.(?:[0-9]+))*`)
func checkVersion(version string) bool {
// double check that the input looks like a snapd version
reqVersionNumMatch := versionExp.FindStringSubmatch(version)
if reqVersionNumMatch == nil {
return false
}
// this check ensures that no one can use an assumes like snapd2.48.3~pre2
// or snapd2.48.5+20.10, as modifiers past the version number are not meant
// to be relied on for snaps via assumes, however the check against the real
// snapd version number below allows such non-numeric modifiers since real
// snapds do have versions like that (for example debian pkg of snapd)
if reqVersionNumMatch[0] != version {
return false
}
req := strings.Split(reqVersionNumMatch[0], ".")
if snapdtool.Version == "unknown" {
return true // Development tree.
}
// We could (should?) use strutil.VersionCompare here and simplify
// this code (see PR#7344). However this would change current
// behavior, i.e. "2.41~pre1" would *not* match [snapd2.41] anymore
// (which the code below does).
curVersionNumMatch := versionExp.FindStringSubmatch(snapdtool.Version)
if curVersionNumMatch == nil {
return false
}
cur := strings.Split(curVersionNumMatch[0], ".")
for i := range req {
if i == len(cur) {
// we hit the end of the elements of the current version number and have
// more required version numbers left, so this doesn't match, if the
// previous element was higher we would have broken out already, so the
// only case left here is where we have version requirements that are
// not met
return false
}
reqN, err1 := strconv.Atoi(req[i])
curN, err2 := strconv.Atoi(cur[i])
if err1 != nil || err2 != nil {
panic("internal error: version regexp is broken")
}
if curN != reqN {
return curN > reqN
}
}
return true
}
type SnapNeedsDevModeError struct {
Snap string
}
func (e *SnapNeedsDevModeError) Error() string {
return fmt.Sprintf("snap %q requires devmode or confinement override", e.Snap)
}
type SnapNeedsClassicError struct {
Snap string
}
func (e *SnapNeedsClassicError) Error() string {
return fmt.Sprintf("snap %q requires classic confinement", e.Snap)
}
type SnapNeedsClassicSystemError struct {
Snap string
}
func (e *SnapNeedsClassicSystemError) Error() string {
return fmt.Sprintf("snap %q requires classic confinement which is only available on classic systems", e.Snap)
}
type SnapNotClassicError struct {
Snap string
}
func (e *SnapNotClassicError) Error() string {
return fmt.Sprintf("snap %q is not a classic confined snap", e.Snap)
}
// determine whether the flags (and system overrides thereof) are
// compatible with the given *snap.Info
func validateFlagsForInfo(info *snap.Info, snapst *SnapState, flags Flags) error {
if flags.Classic && !info.NeedsClassic() {
return &SnapNotClassicError{Snap: info.InstanceName()}
}
switch c := info.Confinement; c {
case snap.StrictConfinement, "":
// strict is always fine
return nil
case snap.DevModeConfinement:
// --devmode needs to be specified every time (==> ignore snapst)
if flags.DevModeAllowed() {
return nil
}
return &SnapNeedsDevModeError{
Snap: info.InstanceName(),
}
case snap.ClassicConfinement:
if !release.OnClassic {
return &SnapNeedsClassicSystemError{Snap: info.InstanceName()}
}
if flags.Classic {
return nil
}
if snapst != nil && snapst.Flags.Classic {
return nil
}
return &SnapNeedsClassicError{
Snap: info.InstanceName(),
}
default:
return fmt.Errorf("unknown confinement %q", c)
}
}
// do a reasonably lightweight check that a snap described by Info,
// with the given SnapState and the user-specified Flags should be
// installable on the current system.
func validateInfoAndFlags(info *snap.Info, snapst *SnapState, flags Flags) error {
if err := validateFlagsForInfo(info, snapst, flags); err != nil {
return err
}
// verify we have a valid architecture
if !arch.IsSupportedArchitecture(info.Architectures) {
return fmt.Errorf("snap %q supported architectures (%s) are incompatible with this system (%s)", info.InstanceName(), strings.Join(info.Architectures, ", "), arch.DpkgArchitecture())
}
// check assumes
if err := checkAssumes(info); err != nil {
return err
}
// check and create system-usernames
if err := checkAndCreateSystemUsernames(info); err != nil {
return err
}
return nil
}
var openSnapFile = backend.OpenSnapFile
func validateContainer(c snap.Container, s *snap.Info, logf func(format string, v ...interface{})) error {
err := snap.ValidateContainer(c, s, logf)
if err == nil {
return nil
}
return fmt.Errorf("%v; contact developer", err)
}
// checkSnap ensures that the snap can be installed.
func checkSnap(st *state.State, snapFilePath, instanceName string, si *snap.SideInfo, curInfo *snap.Info, flags Flags, deviceCtx DeviceContext) error {
// This assumes that the snap was already verified or --dangerous was used.
s, c, err := openSnapFile(snapFilePath, si)
if err != nil {
return err
}
if err := validateInfoAndFlags(s, nil, flags); err != nil {
return err
}
if err := validateContainer(c, s, logger.Noticef); err != nil {
return err
}
snapName, instanceKey := snap.SplitInstanceName(instanceName)
// update instance key to what was requested
s.InstanceKey = instanceKey
st.Lock()
defer st.Unlock()
// allow registered checks to run first as they may produce more
// precise errors
for _, check := range checkSnapCallbacks {
err := check(st, s, curInfo, c, flags, deviceCtx)
if err != nil {
return err
}
}
if snapName != s.SnapName() {
return fmt.Errorf("cannot install snap %q using instance name %q", s.SnapName(), instanceName)
}
return nil
}
// CheckSnapCallback defines callbacks for checking a snap for installation or refresh.
type CheckSnapCallback func(st *state.State, snap, curSnap *snap.Info, snapf snap.Container, flags Flags, deviceCtx DeviceContext) error
var checkSnapCallbacks []CheckSnapCallback
// AddCheckSnapCallback installs a callback to check a snap for installation or refresh.
func AddCheckSnapCallback(check CheckSnapCallback) {
checkSnapCallbacks = append(checkSnapCallbacks, check)
}
func MockCheckSnapCallbacks(checks []CheckSnapCallback) (restore func()) {
prev := checkSnapCallbacks
checkSnapCallbacks = checks
return func() {
checkSnapCallbacks = prev
}
}
func checkSnapdName(st *state.State, snapInfo, curInfo *snap.Info, _ snap.Container, flags Flags, deviceCtx DeviceContext) error {
if snapInfo.Type() != snap.TypeSnapd {
// not a relevant check
return nil
}
if snapInfo.InstanceName() != "snapd" {
return fmt.Errorf(`cannot install snap %q of type "snapd" with a name other than "snapd"`, snapInfo.InstanceName())
}
return nil
}
func checkCoreName(st *state.State, snapInfo, curInfo *snap.Info, _ snap.Container, flags Flags, deviceCtx DeviceContext) error {
if snapInfo.Type() != snap.TypeOS {
// not a relevant check
return nil
}
if curInfo != nil {
// already one of these installed
return nil
}
core, err := coreInfo(st)
if err == state.ErrNoState {
return nil
}
if err != nil {
return err
}
// Allow installing "core" even if "ubuntu-core" is already
// installed. Ideally we should only allow this if we know
// this install is part of the ubuntu-core->core transition
// (e.g. via a flag) because if this happens outside of this
// transition we will end up with not connected interface
// connections in the "core" snap. But the transition will
// kick in automatically quickly so an extra flag is overkill.
if snapInfo.InstanceName() == "core" && core.InstanceName() == "ubuntu-core" {
return nil
}
// but generally do not allow to have two cores installed
if core.InstanceName() != snapInfo.InstanceName() {
return fmt.Errorf("cannot install core snap %q when core snap %q is already present", snapInfo.InstanceName(), core.InstanceName())
}
return nil
}
func checkGadgetOrKernel(st *state.State, snapInfo, curInfo *snap.Info, snapf snap.Container, flags Flags, deviceCtx DeviceContext) error {
typ := snapInfo.Type()
kind := ""
var whichName func(*asserts.Model) string
switch typ {
case snap.TypeGadget:
kind = "gadget"
whichName = (*asserts.Model).Gadget
case snap.TypeKernel:
kind = "kernel"
whichName = (*asserts.Model).Kernel
default:
// not a relevant check
return nil
}
ok, err := HasSnapOfType(st, typ)
if err != nil {
return fmt.Errorf("cannot detect original %s snap: %v", kind, err)
}
// in firstboot we have no gadget/kernel yet - that is ok
// first install rules are in devicestate!
if !ok {
return nil
}
currentSnap, err := infoForDeviceSnap(st, deviceCtx, kind, whichName)
if err == state.ErrNoState {
// check if we are in the remodel case
if deviceCtx != nil && deviceCtx.ForRemodeling() {
if whichName(deviceCtx.Model()) == snapInfo.InstanceName() {
return nil
}
}
return fmt.Errorf("internal error: no state for %s snap %q", kind, snapInfo.InstanceName())
}
if err != nil {
return fmt.Errorf("cannot find original %s snap: %v", kind, err)
}
if currentSnap.SnapID != "" && snapInfo.SnapID == "" {
return fmt.Errorf("cannot replace signed %s snap with an unasserted one", kind)
}
if currentSnap.SnapID != "" && snapInfo.SnapID != "" {
if currentSnap.SnapID == snapInfo.SnapID {
// same snap
return nil
}
return fmt.Errorf("cannot replace %s snap with a different one", kind)
}
if currentSnap.InstanceName() != snapInfo.InstanceName() {
return fmt.Errorf("cannot replace %s snap with a different one", kind)
}
return nil
}
func checkBases(st *state.State, snapInfo, curInfo *snap.Info, _ snap.Container, flags Flags, deviceCtx DeviceContext) error {
// check if this is relevant
if snapInfo.Type() != snap.TypeApp && snapInfo.Type() != snap.TypeGadget {
return nil
}
if snapInfo.Base == "" {
return nil
}
if snapInfo.Base == "none" {
return nil
}
snapStates, err := All(st)
if err != nil {
return err
}
for otherSnap, snapst := range snapStates {
typ, err := snapst.Type()
if err != nil {
return err
}
if typ == snap.TypeBase && otherSnap == snapInfo.Base {
return nil
}
// core can be used instead for core16
if snapInfo.Base == "core16" && otherSnap == "core" {
return nil
}
}
return fmt.Errorf("cannot find required base %q", snapInfo.Base)
}
func checkEpochs(_ *state.State, snapInfo, curInfo *snap.Info, _ snap.Container, _ Flags, deviceCtx DeviceContext) error {
if curInfo == nil {
return nil
}
if snapInfo.Epoch.CanRead(curInfo.Epoch) {
return nil
}
desc := "local snap"
if snapInfo.SideInfo.Revision.Store() {
desc = fmt.Sprintf("new revision %s", snapInfo.SideInfo.Revision)
}
return fmt.Errorf("cannot refresh %q to %s with epoch %s, because it can't read the current epoch of %s", snapInfo.InstanceName(), desc, snapInfo.Epoch, curInfo.Epoch)
}
// check that the snap installed in the system (via snapst) can be
// upgraded to info (i.e. that info's epoch can read sanpst's epoch)
func earlyEpochCheck(info *snap.Info, snapst *SnapState) error {
if snapst == nil {
// no snapst, no problem
return nil
}
cur, err := snapst.CurrentInfo()
if err != nil {
if err == ErrNoCurrent {
// refreshing a disabled snap (maybe via InstallPath)
return nil
}
return err
}
return checkEpochs(nil, info, cur, nil, Flags{}, nil)
}
// check that the listed system users are valid
var osutilEnsureUserGroup = osutil.EnsureUserGroup
func validateSystemUsernames(si *snap.Info) error {
for _, user := range si.SystemUsernames {
if _, ok := supportedSystemUsernames[user.Name]; !ok {
return fmt.Errorf(`snap %q requires unsupported system username "%s"`, si.InstanceName(), user.Name)
}
switch user.Scope {
case "shared":
// this is supported
continue
case "private", "external":
// not supported yet
return fmt.Errorf(`snap %q requires unsupported user scope "%s" for this version of snapd`, si.InstanceName(), user.Scope)
default:
return fmt.Errorf(`snap %q requires unsupported user scope "%s"`, si.InstanceName(), user.Scope)
}
}
return nil
}
func checkAndCreateSystemUsernames(si *snap.Info) error {
// No need to check support if no system-usernames
if len(si.SystemUsernames) == 0 {
return nil
}
// Run /.../snap-seccomp version-info
vi, err := seccomp_compiler.CompilerVersionInfo(snapdtool.InternalToolPath)
if err != nil {
return fmt.Errorf("cannot obtain seccomp compiler information: %v", err)
}
// If the system doesn't support robust argument filtering then we
// can't support system-usernames
if err := vi.SupportsRobustArgumentFiltering(); err != nil {
if re, ok := err.(*seccomp_compiler.BuildTimeRequirementError); ok {
return fmt.Errorf("snap %q system usernames require a snapd built against %s", si.InstanceName(), re.RequirementsString())
}
return err
}
// first validate
if err := validateSystemUsernames(si); err != nil {
return err
}
// then create
// TODO: move user creation to a more appropriate place like "link-snap"
extrausers := !release.OnClassic
for _, user := range si.SystemUsernames {
id := supportedSystemUsernames[user.Name]
switch user.Scope {
case "shared":
// Create the snapd-range-<base>-root user and group so
// systemd-nspawn can avoid our range. Our ranges will always
// be in 65536 chunks, so mask off the lower bits to obtain our
// base (see above)
rangeStart := id & 0xFFFF0000
rangeName := fmt.Sprintf("snapd-range-%d-root", rangeStart)
if err := osutilEnsureUserGroup(rangeName, rangeStart, extrausers); err != nil {
return fmt.Errorf(`cannot ensure users for snap %q required system username "%s": %v`, si.InstanceName(), user.Name, err)
}
// Create the requested user and group
if err := osutilEnsureUserGroup(user.Name, id, extrausers); err != nil {
return fmt.Errorf(`cannot ensure users for snap %q required system username "%s": %v`, si.InstanceName(), user.Name, err)
}
}
}
return nil
}
func init() {
AddCheckSnapCallback(checkCoreName)
AddCheckSnapCallback(checkSnapdName)
AddCheckSnapCallback(checkGadgetOrKernel)
AddCheckSnapCallback(checkBases)
AddCheckSnapCallback(checkEpochs)
}