-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmount.go
1096 lines (912 loc) · 32 KB
/
mount.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
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// Copyright 2019-2020 Nestybox, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package seccomp
import (
"fmt"
"path/filepath"
"strings"
"syscall"
"github.com/nestybox/sysbox-fs/domain"
"github.com/nestybox/sysbox-fs/fuse"
"github.com/sirupsen/logrus"
"golang.org/x/sys/unix"
)
// MountSyscall information structure.
type mountSyscallInfo struct {
syscallCtx // syscall generic info
*domain.MountSyscallPayload // mount-syscall specific details
}
// Mount syscall processing wrapper instruction.
func (m *mountSyscallInfo) process() (*sysResponse, error) {
mts := m.tracer.service.mts
if mts == nil {
return nil, fmt.Errorf("unexpected mount-service handler")
}
mh := mts.MountHelper()
if mh == nil {
return nil, fmt.Errorf("unexpected mount-service-helper handler")
}
// Adjust mount attributes attending to the process' root path.
m.targetAdjust()
// Ensure that the mountInfoDB corresponding to the sys-container hosting
// this process has been already built. This info is necessary to be able
// to discern between 'initial' and 'regular' mounts, which is required
// for the proper operation of the mount-hardening feature.
if !m.cntr.IsMountInfoInitialized() {
if err := m.cntr.InitializeMountInfo(); err != nil {
return nil, err
}
}
// Handle requests that create a new mountpoint for filesystems managed by
// sysbox-fs.
if mh.IsNewMount(m.Flags) {
mip, err := mts.NewMountInfoParser(m.cntr, m.processInfo, true, true, false)
if err != nil {
return nil, err
}
switch m.FsType {
case "proc":
return m.processProcMount(mip)
case "sysfs":
return m.processSysMount(mip)
case "overlay":
return m.processOverlayMount(mip)
case "nfs":
return m.processNfsMount(mip)
}
}
// Mount moves are handled by the kernel
if mh.IsMove(m.Flags) {
return m.tracer.createContinueResponse(m.reqId), nil
}
// Handle propagation type changes on filesystems managed by sysbox-fs (no
// action required; let the kernel handle mount propagation changes).
if mh.HasPropagationFlag(m.Flags) {
return m.tracer.createContinueResponse(m.reqId), nil
}
// Handle remount requests on filesystems managed by sysbox-fs
if mh.IsRemount(m.Flags) {
mip, err := mts.NewMountInfoParser(m.cntr, m.processInfo, true, true, false)
if err != nil {
return nil, err
}
if ok, resp := m.remountAllowed(mip); !ok {
return resp, nil
}
if mip.IsSysboxfsBaseMount(m.Target) ||
mip.IsSysboxfsSubmount(m.Target) {
return m.processRemount(mip)
}
// No action by sysbox-fs
return m.tracer.createContinueResponse(m.reqId), nil
}
// Handle bind-mount requests on filesystems managed by sysbox-fs.
if mh.IsBind(m.Flags) {
mip, err := mts.NewMountInfoParser(m.cntr, m.processInfo, true, true, false)
if err != nil {
return nil, err
}
// Ignore binds-to-self requests on sysbox-fs managed submounts (these
// are already bind-mounts, so we want to avoid the redundant bind mount
// for cosmetic purposes).
if m.Source == m.Target && mip.IsSysboxfsSubmount(m.Target) {
logrus.Debugf("Ignoring bind-to-self request of sysbox-fs managed submount at %s",
m.Target)
return m.tracer.createSuccessResponse(m.reqId), nil
}
// Ignore /dev/null bind mounts on sysbox-fs managed submounts which are
// already bind-mounted to /dev/null (i.e., masked).
if m.Source == "/dev/null" && mip.IsSysboxfsMaskedSubmount(m.Target) {
logrus.Debugf("Ignoring /dev/null bind request over sysbox-fs masked submount at %s",
m.Target)
return m.tracer.createSuccessResponse(m.reqId), nil
}
// Process bind-mounts whose source is a sysbox-fs base mount (as we
// want the submounts to also be bind-mounted at the target).
if m.Source != m.Target && mip.IsSysboxfsBaseMount(m.Source) {
return m.processBindMount(mip)
}
// No action by sysbox-fs
return m.tracer.createContinueResponse(m.reqId), nil
}
// No action by sysbox-fs otherwise
return m.tracer.createContinueResponse(m.reqId), nil
}
// Method handles procfs mount syscall requests. As part of this function, we
// also create submounts under procfs (to expose, hide, or emulate resources).
func (m *mountSyscallInfo) processProcMount(
mip domain.MountInfoParserIface) (*sysResponse, error) {
logrus.Debugf("Processing new procfs mount: %v", m)
// Create instructions payload.
payload := m.createProcPayload(mip)
if payload == nil {
return nil, fmt.Errorf("Could not construct procMount payload")
}
// Create nsenter-event envelope.
nss := m.tracer.service.nss
event := nss.NewEvent(
m.syscallCtx.pid,
&domain.AllNSs,
0,
&domain.NSenterMessage{
Type: domain.MountSyscallRequest,
Payload: payload,
},
nil,
false,
)
// Launch nsenter-event.
err := nss.SendRequestEvent(event)
if err != nil {
return nil, err
}
// Obtain nsenter-event response.
responseMsg := nss.ReceiveResponseEvent(event)
if responseMsg.Type == domain.ErrorResponse {
resp := m.tracer.createErrorResponse(
m.reqId,
responseMsg.Payload.(fuse.IOerror).Code)
return resp, nil
}
// Chown the proc mount to the requesting process' uid:gid (typically
// root:root) as otherwise it will show up as "nobody:nogroup".
//
// NOTE: for now we skip the chown if the mount is read-only, as otherwise
// the chown will fail. This means that read-only mounts of proc will still
// show up as "nobody:nouser" inside the sys container (e.g., in inner
// containers). Solving this would require that we first mount proc, then
// chown, then remount read-only. This would in turn require 3 nsenter
// events, because the namespaces that we must enter for each are not the
// same (in particular for the chown to succeed, we must not enter the
// user-ns of the container).
if m.Flags&unix.MS_RDONLY == unix.MS_RDONLY {
return m.tracer.createSuccessResponse(m.reqId), nil
}
ci := &chownSyscallInfo{
path: m.Target,
ownerUid: int64(m.uid),
ownerGid: int64(m.gid),
}
ci.syscallCtx.reqId = m.reqId
ci.syscallCtx.pid = m.pid
ci.syscallCtx.tracer = m.tracer
return ci.processChownNSenter(domain.AllNSsButUser)
}
// Build instructions payload required to mount "/proc" subtree.
func (m *mountSyscallInfo) createProcPayload(
mip domain.MountInfoParserIface) *[]*domain.MountSyscallPayload {
var payload []*domain.MountSyscallPayload
// Payload instruction for original "/proc" mount request.
payload = append(payload, m.MountSyscallPayload)
// If procfs has a read-only attribute at super-block level, we must also
// apply this to the new mountpoint (otherwise we will get a permission
// denied from the kernel when doing the mount).
procInfo := mip.GetInfo("/proc")
if procInfo != nil {
if _, ok := procInfo.VfsOptions["ro"]; ok {
payload[0].Flags |= unix.MS_RDONLY
}
}
mh := m.tracer.service.mts.MountHelper()
// Sysbox-fs "/proc" bind-mounts.
procBindMounts := mh.ProcMounts()
for _, v := range procBindMounts {
relPath := strings.TrimPrefix(v, "/proc")
newelem := &domain.MountSyscallPayload{
domain.NSenterMsgHeader{},
domain.Mount{
Source: v,
Target: filepath.Join(m.Target, relPath),
FsType: "",
Flags: unix.MS_BIND,
Data: "",
},
}
payload = append(payload, newelem)
}
// Container-specific read-only paths.
procRoPaths := m.cntr.ProcRoPaths()
for _, v := range procRoPaths {
if !domain.FileExists(v) {
continue
}
relPath := strings.TrimPrefix(v, "/proc")
newelem := &domain.MountSyscallPayload{
domain.NSenterMsgHeader{},
domain.Mount{
Source: v,
Target: filepath.Join(m.Target, relPath),
FsType: "",
Flags: unix.MS_BIND,
Data: "",
},
}
payload = append(payload, newelem)
}
// Container-specific masked paths.
procMaskPaths := m.cntr.ProcMaskPaths()
for _, v := range procMaskPaths {
if !domain.FileExists(v) {
continue
}
relPath := strings.TrimPrefix(v, "/proc")
newelem := &domain.MountSyscallPayload{
domain.NSenterMsgHeader{},
domain.Mount{
Source: v,
Target: filepath.Join(m.Target, relPath),
FsType: "",
Flags: unix.MS_BIND,
Data: "",
},
}
payload = append(payload, newelem)
}
// If "/proc" is to be mounted as read-only, we want this requirement to
// extend to all of its inner bind-mounts.
if m.Flags&unix.MS_RDONLY == unix.MS_RDONLY {
for _, v := range procBindMounts {
relPath := strings.TrimPrefix(v, "/proc")
newelem := &domain.MountSyscallPayload{
domain.NSenterMsgHeader{},
domain.Mount{
Source: "",
Target: filepath.Join(m.Target, relPath),
FsType: "",
// TODO: Avoid hard-coding these flags.
Flags: unix.MS_RDONLY | unix.MS_BIND | unix.MS_REMOUNT | unix.MS_NOSUID | unix.MS_NODEV | unix.MS_NOEXEC,
Data: "",
},
}
payload = append(payload, newelem)
}
}
return &payload
}
// Method handles sysfs mount syscall requests. As part of this function, we
// also create submounts under sysfs (to expose, hide, or emulate resources).
func (m *mountSyscallInfo) processSysMount(
mip domain.MountInfoParserIface) (*sysResponse, error) {
logrus.Debugf("Processing new sysfs mount: %v", m)
// Create instruction's payload.
payload := m.createSysPayload(mip)
if payload == nil {
return nil, fmt.Errorf("Could not construct sysfsMount payload")
}
// Create nsenter-event envelope.
nss := m.tracer.service.nss
event := nss.NewEvent(
m.syscallCtx.pid,
&domain.AllNSs,
0,
&domain.NSenterMessage{
Type: domain.MountSyscallRequest,
Payload: payload,
},
nil,
false,
)
// Launch nsenter-event.
err := nss.SendRequestEvent(event)
if err != nil {
return nil, err
}
// Obtain nsenter-event response.
responseMsg := nss.ReceiveResponseEvent(event)
if responseMsg.Type == domain.ErrorResponse {
resp := m.tracer.createErrorResponse(
m.reqId,
responseMsg.Payload.(fuse.IOerror).Code)
return resp, nil
}
return m.tracer.createSuccessResponse(m.reqId), nil
}
// Build instructions payload required to mount "/sys" subtree.
func (m *mountSyscallInfo) createSysPayload(
mip domain.MountInfoParserIface) *[]*domain.MountSyscallPayload {
var payload []*domain.MountSyscallPayload
// Payload instruction for original "/sys" mount request.
payload = append(payload, m.MountSyscallPayload)
// If sysfs has a read-only attribute at super-block level, we must also
// apply this to the new mountpoint (otherwise we will get a permission
// denied from the kernel when doing the mount).
sysInfo := mip.GetInfo("/sys")
if sysInfo != nil {
if _, ok := sysInfo.VfsOptions["ro"]; ok {
payload[0].Flags |= unix.MS_RDONLY
}
}
mh := m.tracer.service.mts.MountHelper()
// Sysbox-fs "/sys" bind-mounts.
sysBindMounts := mh.SysMounts()
for _, v := range sysBindMounts {
relPath := strings.TrimPrefix(v, "/sys")
newelem := &domain.MountSyscallPayload{
domain.NSenterMsgHeader{},
domain.Mount{
Source: v,
Target: filepath.Join(m.Target, relPath),
FsType: "",
Flags: unix.MS_BIND,
Data: "",
},
}
payload = append(payload, newelem)
}
// If "/sys" is to be mounted as read-only, we want this requirement to
// extend to all of its inner bind-mounts.
if m.Flags&unix.MS_RDONLY == unix.MS_RDONLY {
for _, v := range sysBindMounts {
relPath := strings.TrimPrefix(v, "/sys")
newelem := &domain.MountSyscallPayload{
domain.NSenterMsgHeader{},
domain.Mount{
Source: "",
Target: filepath.Join(m.Target, relPath),
FsType: "",
// TODO: Avoid hard-coding these flags.
Flags: unix.MS_RDONLY | unix.MS_BIND | unix.MS_REMOUNT | unix.MS_NOSUID | unix.MS_NODEV | unix.MS_NOEXEC,
Data: "",
},
}
payload = append(payload, newelem)
}
}
return &payload
}
// Method handles overlayfs mount syscall requests.
func (m *mountSyscallInfo) processOverlayMount(
mip domain.MountInfoParserIface) (*sysResponse, error) {
logrus.Debugf("Processing new overlayfs mount: %v", m)
// Notice that, in chroot scenarios, we are undoing the previous call to
// targetAdjust() to avoid the need to mess around with the paths in the
// 'data' object. Once within the 'nsenter' context, we will adjust all
// path elements by doing a chroot() as part of the personality-adjustment
// logic.
m.targetUnadjust()
// Create instructions payload.
payload := m.createOverlayMountPayload(mip)
if payload == nil {
return nil, fmt.Errorf("Could not construct overlayMount payload")
}
// Create nsenter-event envelope.
nss := m.tracer.service.nss
event := nss.NewEvent(
m.syscallCtx.pid,
&domain.AllNSsButUser,
0,
&domain.NSenterMessage{
Type: domain.MountSyscallRequest,
Payload: payload,
},
nil,
false,
)
// Launch nsenter-event.
err := nss.SendRequestEvent(event)
if err != nil {
return nil, err
}
// Obtain nsenter-event response.
responseMsg := nss.ReceiveResponseEvent(event)
if responseMsg.Type == domain.ErrorResponse {
resp := m.tracer.createErrorResponse(
m.reqId,
responseMsg.Payload.(fuse.IOerror).Code)
return resp, nil
}
return m.tracer.createSuccessResponse(m.reqId), nil
}
// Build instructions payload required for overlay-mount operations.
func (m *mountSyscallInfo) createOverlayMountPayload(
mip domain.MountInfoParserIface) *[]*domain.MountSyscallPayload {
var payload []*domain.MountSyscallPayload
// Create a process struct to represent the process generating the 'mount'
// instruction, and extract its capabilities to hand them out to 'nsenter'
// logic.
process := m.tracer.service.prs.ProcessCreate(m.pid, 0, 0)
// Payload instruction for overlayfs mount request.
payload = append(payload, m.MountSyscallPayload)
// Insert appended fields.
payload[0].Header = domain.NSenterMsgHeader{
Pid: m.pid,
Uid: m.uid,
Gid: m.gid,
Root: m.root,
Cwd: m.cwd,
Capabilities: process.GetEffCaps(),
}
return &payload
}
// Method handles "nfs" mount syscall requests. Sysbox-fs does not manage nfs
// mounts per-se, but only "proxies" the nfs mount syscall. It does this in
// order to enable nfs to be mounted from within a (non init) user-ns.
func (m *mountSyscallInfo) processNfsMount(
mip domain.MountInfoParserIface) (*sysResponse, error) {
logrus.Debugf("Processing new nfs mount: %v", m)
// Create instruction's payload.
payload := m.createNfsMountPayload(mip)
if payload == nil {
return nil, fmt.Errorf("Could not construct nfsMount payload")
}
// Create nsenter-event envelope
nss := m.tracer.service.nss
event := nss.NewEvent(
m.syscallCtx.pid,
&domain.AllNSsButUser,
0,
&domain.NSenterMessage{
Type: domain.MountSyscallRequest,
Payload: payload,
},
nil,
false,
)
// Launch nsenter-event.
err := nss.SendRequestEvent(event)
if err != nil {
return nil, err
}
// Obtain nsenter-event response.
responseMsg := nss.ReceiveResponseEvent(event)
if responseMsg.Type == domain.ErrorResponse {
resp := m.tracer.createErrorResponse(
m.reqId,
responseMsg.Payload.(fuse.IOerror).Code)
return resp, nil
}
return m.tracer.createSuccessResponse(m.reqId), nil
}
// Build instructions payload required for remount operations.
func (m *mountSyscallInfo) createNfsMountPayload(
mip domain.MountInfoParserIface) *[]*domain.MountSyscallPayload {
var payload []*domain.MountSyscallPayload
// Payload instruction for re-mount request.
payload = append(payload, m.MountSyscallPayload)
return &payload
}
// remountAllowed purpose is to prevent certain remount operations from
// succeeding, such as preventing RO mountpoints to be remounted as RW.
//
// Method will return 'true' when the remount operation is deemed legit, and
// will return 'false' otherwise.
func (m *mountSyscallInfo) remountAllowed(
mip domain.MountInfoParserIface) (bool, *sysResponse) {
mh := m.tracer.service.mts.MountHelper()
// Skip verification process if explicitly requested by the user. By default,
// remount operations of RO immutables are not allowed.
if m.tracer.service.allowImmutableRemounts {
return true, nil
}
// Skip instructions targeting file-systems explicitly handled by sysbox-fs.
if m.FsType == "proc" || m.FsType == "sysfs" {
return true, nil
}
// Allow operation if it attempts to remount target as read-only.
if mh.IsReadOnlyMount(m.Flags) {
return true, nil
}
// There must be mountinfo state present for this target. Otherwise, return
// error back to the user.
info := mip.GetInfo(m.Target)
if info == nil {
return false, m.tracer.createErrorResponse(m.reqId, syscall.EINVAL)
}
// Allow operation if the remount target is a read-write mountpoint.
if !mip.IsRoMount(info) {
return true, nil
}
//
// The following scenarios are relevant within the context of this function
// and will be handled separately to ease the logic comprehension and its
// maintenability / debuggability.
//
// The different columns in this table denote the 'context' in which the
// remount process is executing, and thereby, dictates the logic chosen
// to handle each remount request.
//
// +-----------+--------------+--------------+----------+
// | Scenarios | Unshare(mnt) | Pivot-root() | Chroot() |
// +-----------+--------------+--------------+----------+
// | 1 | no | no | no |
// | 2 | no | yes | no |
// | 3 | no | no | yes |
// | 4 | no | yes | yes |
// | 5 | yes | no | no |
// | 6 | yes | yes | no |
// | 7 | yes | no | yes |
// | 8 | yes | yes | yes |
// +-----------+--------------+--------------+----------+
//
// Identify the mount-ns of the process launching the remount to compare it
// with the one of the sys container's initpid. In the unlikely case of an
// error, let the kernel deal with it.
processMountNs, err := m.processInfo.MountNsInode()
if err != nil {
return false, m.tracer.createErrorResponse(m.reqId, syscall.EINVAL)
}
initProcMountNs, err := m.cntr.InitProc().MountNsInode()
if err != nil {
return false, m.tracer.createErrorResponse(m.reqId, syscall.EINVAL)
}
// Obtain the sys-container's root-path inode.
syscntrRootInode := m.cntr.InitProc().RootInode()
// If process' mount-ns matches the sys-container's one, then we can simply
// rely on the target's mountID to discern an immutable target from a
// regular one. Otherwise, we cannot rely on the mountID field, as the values
// allocated by kernel for these very mountpoints will differ in other mount
// namespaces.
if processMountNs == initProcMountNs {
var (
immutable bool
bindmountImmutable bool
)
if ok := m.cntr.IsImmutableRoMountID(info.MountID); ok {
logrus.Infof("Rejected remount operation over read-only immutable target: %s",
m.Target)
immutable = true
}
if !immutable {
if ok := m.cntr.IsImmutableRoBindMount(info); ok {
logrus.Infof("Rejected remount operation over bind-mount to read-only immutable target: %s",
m.Target)
bindmountImmutable = true
}
}
if !immutable && !bindmountImmutable {
return true, nil
}
if logrus.IsLevelEnabled(logrus.DebugLevel) {
if m.processInfo.Root() == "/" {
processRootInode := m.processInfo.RootInode()
// Scenario 1): no-unshare(mnt) & no-privot() & no-chroot()
if processRootInode == syscntrRootInode {
logrus.Debug("Rejected remount operation -- scenario 1")
}
// Scenario 2): no-unshare(mnt) & pivot() & no-chroot()
if processRootInode != syscntrRootInode {
logrus.Debug("Rejected remount operation -- scenario 2")
}
}
if m.processInfo.Root() != "/" {
// We are dealing with a chroot'ed process, so obtain the inode of "/"
// as seen within the process' namespaces, and *not* the one associated
// to the process' root-path.
processRootInode, err := mip.ExtractInode("/")
if err != nil {
return false, m.tracer.createErrorResponse(m.reqId, syscall.EINVAL)
}
// Scenario 3): no-unshare(mnt) & no-pivot() & chroot()
if processRootInode == syscntrRootInode {
logrus.Debug("Rejected remount operation -- scenario 3")
}
// Scenario 4): no-unshare(mnt) & pivot() & chroot()
if processRootInode != syscntrRootInode {
logrus.Debug("Rejected remount operation -- scenario 4")
}
}
}
return false, m.tracer.createErrorResponse(m.reqId, syscall.EPERM)
} else {
if m.processInfo.Root() == "/" {
processRootInode := m.processInfo.RootInode()
// Scenario 5): unshare(mnt) & no-pivot() & no-chroot()
if processRootInode == syscntrRootInode {
// We need to check if we're dealing with an overlapped mount, as
// this is a case that we usually (see exception below) want to
// allow.
if mip.IsOverlapMount(info) {
// The exception mentioned above refer to the scenario where
// the overlapped mountpoint is an immutable itself, hence the
// checkpoint below.
if m.cntr.IsImmutableOverlapMountpoint(info.MountPoint) {
logrus.Infof("Rejected remount operation over immutable overlapped target: %s (scenario 5)",
m.Target)
return false, m.tracer.createErrorResponse(m.reqId, syscall.EPERM)
}
return true, nil
}
// In this scenario we have full access to all the mountpoints
// within the sys-container (different mount-id though), so we
// can safely rely on their mountinfo attributes to determine
// resource's immutability.
if m.cntr.IsImmutableRoMountpoint(info.MountPoint) {
logrus.Infof("Rejected remount operation over read-only immutable target: %s (scenario 5)",
m.Target)
return false, m.tracer.createErrorResponse(m.reqId, syscall.EPERM)
}
if ok := m.cntr.IsImmutableRoBindMount(info); ok {
logrus.Infof("Rejected remount operation over bind-mount to read-only immutable target: %s (scenario 5)",
m.Target)
return false, m.tracer.createErrorResponse(m.reqId, syscall.EPERM)
}
return true, nil
}
// Scenario 6): unshare(mnt) & pivot() & no-chroot()
if processRootInode != syscntrRootInode {
isImmutable, err := m.cntr.IsImmutableRoMount(info)
if err != nil {
return false, m.tracer.createErrorResponse(m.reqId, syscall.EINVAL)
}
if isImmutable {
logrus.Infof("Rejected remount operation over read-only immutable target: %s (scenario 6)",
m.Target)
return false, m.tracer.createErrorResponse(m.reqId, syscall.EPERM)
}
if ok := m.cntr.IsImmutableRoBindMount(info); ok {
logrus.Infof("Rejected remount operation over bind-mount to read-only-immutable target: %s (scenario 6)",
m.Target)
return false, m.tracer.createErrorResponse(m.reqId, syscall.EPERM)
}
return true, nil
}
}
if m.processInfo.Root() != "/" {
// We are dealing with a chroot'ed process, so obtain the inode of "/"
// as seen within the process' namespaces, and *not* the one associated
// to the process' root-path.
processRootInode, err := mip.ExtractInode("/")
if err != nil {
return false, m.tracer.createErrorResponse(m.reqId, syscall.EINVAL)
}
// Scenario 7): unshare(mnt) & no-pivot() & chroot()
if processRootInode == syscntrRootInode {
// We need to check if we're dealing with an overlapped mount, as
// this is a case that we usually (see exception below) want to
// allow.
if mip.IsOverlapMount(info) {
// The exception mentioned above refer to the scenario where
// the overlapped mountpoint is an immutable itself, hence the
// checkpoint below.
if m.cntr.IsImmutableOverlapMountpoint(info.MountPoint) {
logrus.Infof("Rejected remount operation over immutable overlapped target: %s (scenario 7)",
m.Target)
return false, m.tracer.createErrorResponse(m.reqId, syscall.EPERM)
}
return true, nil
}
// In this scenario we have full access to all the mountpoints
// within the sys-container (different mount-id though), so we
// can safely rely on their mountinfo attributes to determine
// resource's immutability.
if m.cntr.IsImmutableRoMountpoint(info.MountPoint) {
logrus.Infof("Rejected remount operation over read-only immutable target: %s (scenario 7)",
m.Target)
return false, m.tracer.createErrorResponse(m.reqId, syscall.EPERM)
}
if ok := m.cntr.IsImmutableRoBindMount(info); ok {
logrus.Infof("Rejected remount operation over bind-mount to read-only immutable target: %s (scenario 7)",
m.Target)
return false, m.tracer.createErrorResponse(m.reqId, syscall.EPERM)
}
return true, nil
}
// Scenario 8): unshare(mnt) & pivot() & chroot()
if processRootInode != syscntrRootInode {
isImmutable, err := m.cntr.IsImmutableRoMount(info)
if err != nil {
return false, m.tracer.createErrorResponse(m.reqId, syscall.EINVAL)
}
if isImmutable {
logrus.Infof("Rejected remount operation over read-only immutable target: %s (scenario 8)",
m.Target)
return false, m.tracer.createErrorResponse(m.reqId, syscall.EPERM)
}
if ok := m.cntr.IsImmutableRoBindMount(info); ok {
logrus.Infof("Rejected remount operation over bind-mount to read-only immutable target: %s (scenario 8)",
m.Target)
return false, m.tracer.createErrorResponse(m.reqId, syscall.EPERM)
}
return true, nil
}
}
}
return true, nil
}
func (m *mountSyscallInfo) processRemount(
mip domain.MountInfoParserIface) (*sysResponse, error) {
logrus.Debugf("Processing re-mount: %v", m)
// Create instruction's payload.
payload := m.createRemountPayload(mip)
if payload == nil {
return nil, fmt.Errorf("Could not construct ReMount payload")
}
// Create nsenter-event envelope.
nss := m.tracer.service.nss
event := nss.NewEvent(
m.syscallCtx.pid,
&domain.AllNSsButUser,
0,
&domain.NSenterMessage{
Type: domain.MountSyscallRequest,
Payload: payload,
},
nil,
false,
)
// Launch nsenter-event.
err := nss.SendRequestEvent(event)
if err != nil {
return nil, err
}
// Obtain nsenter-event response.
responseMsg := nss.ReceiveResponseEvent(event)
if responseMsg.Type == domain.ErrorResponse {
resp := m.tracer.createErrorResponse(
m.reqId,
responseMsg.Payload.(fuse.IOerror).Code)
return resp, nil
}
return m.tracer.createSuccessResponse(m.reqId), nil
}
// Build instructions payload required for remount operations.
func (m *mountSyscallInfo) createRemountPayload(
mip domain.MountInfoParserIface) *[]*domain.MountSyscallPayload {
var payload []*domain.MountSyscallPayload
mh := m.tracer.service.mts.MountHelper()
// A procfs mount inside a sys container is a combination of a base proc
// mount plus sysbox-fs submounts. If the remount is done on the base mount,
// its effect is also applied to the submounts. If the remount is on a
// submount, its effect is limited to that submount.
submounts := []string{}
if mip.IsSysboxfsBaseMount(m.Target) {
submounts = mip.GetSysboxfsSubMounts(m.Target)
} else {
submounts = append(submounts, m.Target)
}
for _, subm := range submounts {
submInfo := mip.GetInfo(subm)
perMountFlags := mh.StringToFlags(submInfo.Options)
perFsFlags := mh.StringToFlags(submInfo.VfsOptions)
submFlags := perMountFlags | perFsFlags
// Pass the remount flags to the submounts
submFlags |= unix.MS_REMOUNT
// The submounts must always be remounted with "MS_BIND" to ensure that
// only the submounts are affected. Otherwise, the remount effect
// applies at the sysbox-fs fuse level, causing weird behavior (e.g.,
// remounting /proc as read-only would cause all sysbox-fs managed
// submounts under /sys to become read-only too!).
submFlags |= unix.MS_BIND
// We only propagate changes to the MS_RDONLY flag to the submounts. In
// the future we could propagate other flags too.
//
// For MS_RDONLY:
//
// When set, we apply the read-only flag on all submounts. When cleared,
// we apply the read-write flag on all submounts which are not mounted
// as read-only in the container's /proc.
if m.Flags&unix.MS_RDONLY == unix.MS_RDONLY {
submFlags |= unix.MS_RDONLY
} else {
if !mip.IsSysboxfsRoSubmount(subm) {
submFlags = submFlags &^ unix.MS_RDONLY
}
}
// Leave the filesystem options (aka data) unchanged; note that since
// mountinfo provides them mixed with flags, we must filter the options
// out.
submOpts := mh.FilterFsFlags(submInfo.VfsOptions)
newelem := &domain.MountSyscallPayload{
domain.NSenterMsgHeader{},
domain.Mount{
Source: "",
Target: subm,
FsType: "",
Flags: submFlags,
Data: submOpts,
},
}
payload = append(payload, newelem)
}
if mip.IsSysboxfsBaseMount(m.Target) {
payload = append(payload, m.MountSyscallPayload)
}
return &payload
}
// Method handles bind-mount requests whose source is a mountpoint managed by
// sysbox-fs.
func (m *mountSyscallInfo) processBindMount(
mip domain.MountInfoParserIface) (*sysResponse, error) {
logrus.Debugf("Processing bind mount: %v", m)
// Create instruction's payload.
payload := m.createBindMountPayload(mip)
if payload == nil {
return nil, fmt.Errorf("Could not construct ReMount payload")
}
// Create nsenter-event envelope.
nss := m.tracer.service.nss
event := nss.NewEvent(
m.syscallCtx.pid,
&domain.AllNSs,
0,
&domain.NSenterMessage{
Type: domain.MountSyscallRequest,
Payload: payload,