summaryrefslogtreecommitdiffstats
path: root/go-selinux/selinux_linux.go
blob: f1e95977d3b0418c09e840871c1e11a6a7f31b05 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
package selinux

import (
	"bufio"
	"bytes"
	"crypto/rand"
	"encoding/binary"
	"errors"
	"fmt"
	"io"
	"io/fs"
	"math/big"
	"os"
	"os/user"
	"path/filepath"
	"strconv"
	"strings"
	"sync"

	"github.com/opencontainers/selinux/pkg/pwalkdir"
	"golang.org/x/sys/unix"
)

const (
	minSensLen       = 2
	contextFile      = "/usr/share/containers/selinux/contexts"
	selinuxDir       = "/etc/selinux/"
	selinuxUsersDir  = "contexts/users"
	defaultContexts  = "contexts/default_contexts"
	selinuxConfig    = selinuxDir + "config"
	selinuxfsMount   = "/sys/fs/selinux"
	selinuxTypeTag   = "SELINUXTYPE"
	selinuxTag       = "SELINUX"
	xattrNameSelinux = "security.selinux"
)

type selinuxState struct {
	mcsList       map[string]bool
	selinuxfs     string
	selinuxfsOnce sync.Once
	enabledSet    bool
	enabled       bool
	sync.Mutex
}

type level struct {
	cats *big.Int
	sens uint
}

type mlsRange struct {
	low  *level
	high *level
}

type defaultSECtx struct {
	userRdr           io.Reader
	verifier          func(string) error
	defaultRdr        io.Reader
	user, level, scon string
}

type levelItem byte

const (
	sensitivity levelItem = 's'
	category    levelItem = 'c'
)

var (
	readOnlyFileLabel string
	state             = selinuxState{
		mcsList: make(map[string]bool),
	}

	// for attrPath()
	attrPathOnce   sync.Once
	haveThreadSelf bool

	// for policyRoot()
	policyRootOnce sync.Once
	policyRootVal  string

	// for label()
	loadLabelsOnce sync.Once
	labels         map[string]string
)

func policyRoot() string {
	policyRootOnce.Do(func() {
		policyRootVal = filepath.Join(selinuxDir, readConfig(selinuxTypeTag))
	})

	return policyRootVal
}

func (s *selinuxState) setEnable(enabled bool) bool {
	s.Lock()
	defer s.Unlock()
	s.enabledSet = true
	s.enabled = enabled
	return s.enabled
}

func (s *selinuxState) getEnabled() bool {
	s.Lock()
	enabled := s.enabled
	enabledSet := s.enabledSet
	s.Unlock()
	if enabledSet {
		return enabled
	}

	enabled = false
	if fs := getSelinuxMountPoint(); fs != "" {
		if con, _ := CurrentLabel(); con != "kernel" {
			enabled = true
		}
	}
	return s.setEnable(enabled)
}

// setDisabled disables SELinux support for the package
func setDisabled() {
	state.setEnable(false)
}

func verifySELinuxfsMount(mnt string) bool {
	var buf unix.Statfs_t
	for {
		err := unix.Statfs(mnt, &buf)
		if err == nil {
			break
		}
		if err == unix.EAGAIN || err == unix.EINTR { //nolint:errorlint // unix errors are bare
			continue
		}
		return false
	}

	if uint32(buf.Type) != uint32(unix.SELINUX_MAGIC) {
		return false
	}
	if (buf.Flags & unix.ST_RDONLY) != 0 {
		return false
	}

	return true
}

func findSELinuxfs() string {
	// fast path: check the default mount first
	if verifySELinuxfsMount(selinuxfsMount) {
		return selinuxfsMount
	}

	// check if selinuxfs is available before going the slow path
	fs, err := os.ReadFile("/proc/filesystems")
	if err != nil {
		return ""
	}
	if !bytes.Contains(fs, []byte("\tselinuxfs\n")) {
		return ""
	}

	// slow path: try to find among the mounts
	f, err := os.Open("/proc/self/mountinfo")
	if err != nil {
		return ""
	}
	defer f.Close()

	scanner := bufio.NewScanner(f)
	for {
		mnt := findSELinuxfsMount(scanner)
		if mnt == "" { // error or not found
			return ""
		}
		if verifySELinuxfsMount(mnt) {
			return mnt
		}
	}
}

// findSELinuxfsMount returns a next selinuxfs mount point found,
// if there is one, or an empty string in case of EOF or error.
func findSELinuxfsMount(s *bufio.Scanner) string {
	for s.Scan() {
		txt := s.Bytes()
		// The first field after - is fs type.
		// Safe as spaces in mountpoints are encoded as \040
		if !bytes.Contains(txt, []byte(" - selinuxfs ")) {
			continue
		}
		const mPos = 5 // mount point is 5th field
		fields := bytes.SplitN(txt, []byte(" "), mPos+1)
		if len(fields) < mPos+1 {
			continue
		}
		return string(fields[mPos-1])
	}

	return ""
}

func (s *selinuxState) getSELinuxfs() string {
	s.selinuxfsOnce.Do(func() {
		s.selinuxfs = findSELinuxfs()
	})

	return s.selinuxfs
}

// getSelinuxMountPoint returns the path to the mountpoint of an selinuxfs
// filesystem or an empty string if no mountpoint is found.  Selinuxfs is
// a proc-like pseudo-filesystem that exposes the SELinux policy API to
// processes.  The existence of an selinuxfs mount is used to determine
// whether SELinux is currently enabled or not.
func getSelinuxMountPoint() string {
	return state.getSELinuxfs()
}

// getEnabled returns whether SELinux is currently enabled.
func getEnabled() bool {
	return state.getEnabled()
}

func readConfig(target string) string {
	in, err := os.Open(selinuxConfig)
	if err != nil {
		return ""
	}
	defer in.Close()

	scanner := bufio.NewScanner(in)

	for scanner.Scan() {
		line := bytes.TrimSpace(scanner.Bytes())
		if len(line) == 0 {
			// Skip blank lines
			continue
		}
		if line[0] == ';' || line[0] == '#' {
			// Skip comments
			continue
		}
		fields := bytes.SplitN(line, []byte{'='}, 2)
		if len(fields) != 2 {
			continue
		}
		if bytes.Equal(fields[0], []byte(target)) {
			return string(bytes.Trim(fields[1], `"`))
		}
	}
	return ""
}

func isProcHandle(fh *os.File) error {
	var buf unix.Statfs_t

	for {
		err := unix.Fstatfs(int(fh.Fd()), &buf)
		if err == nil {
			break
		}
		if err != unix.EINTR { //nolint:errorlint // unix errors are bare
			return &os.PathError{Op: "fstatfs", Path: fh.Name(), Err: err}
		}
	}
	if buf.Type != unix.PROC_SUPER_MAGIC {
		return fmt.Errorf("file %q is not on procfs", fh.Name())
	}

	return nil
}

func readCon(fpath string) (string, error) {
	if fpath == "" {
		return "", ErrEmptyPath
	}

	in, err := os.Open(fpath)
	if err != nil {
		return "", err
	}
	defer in.Close()

	if err := isProcHandle(in); err != nil {
		return "", err
	}
	return readConFd(in)
}

func readConFd(in *os.File) (string, error) {
	data, err := io.ReadAll(in)
	if err != nil {
		return "", err
	}
	return string(bytes.TrimSuffix(data, []byte{0})), nil
}

// classIndex returns the int index for an object class in the loaded policy,
// or -1 and an error
func classIndex(class string) (int, error) {
	permpath := fmt.Sprintf("class/%s/index", class)
	indexpath := filepath.Join(getSelinuxMountPoint(), permpath)

	indexB, err := os.ReadFile(indexpath)
	if err != nil {
		return -1, err
	}
	index, err := strconv.Atoi(string(indexB))
	if err != nil {
		return -1, err
	}

	return index, nil
}

// lSetFileLabel sets the SELinux label for this path, not following symlinks,
// or returns an error.
func lSetFileLabel(fpath string, label string) error {
	if fpath == "" {
		return ErrEmptyPath
	}
	for {
		err := unix.Lsetxattr(fpath, xattrNameSelinux, []byte(label), 0)
		if err == nil {
			break
		}
		if err != unix.EINTR { //nolint:errorlint // unix errors are bare
			return &os.PathError{Op: "lsetxattr", Path: fpath, Err: err}
		}
	}

	return nil
}

// setFileLabel sets the SELinux label for this path, following symlinks,
// or returns an error.
func setFileLabel(fpath string, label string) error {
	if fpath == "" {
		return ErrEmptyPath
	}
	for {
		err := unix.Setxattr(fpath, xattrNameSelinux, []byte(label), 0)
		if err == nil {
			break
		}
		if err != unix.EINTR { //nolint:errorlint // unix errors are bare
			return &os.PathError{Op: "setxattr", Path: fpath, Err: err}
		}
	}

	return nil
}

// fileLabel returns the SELinux label for this path, following symlinks,
// or returns an error.
func fileLabel(fpath string) (string, error) {
	if fpath == "" {
		return "", ErrEmptyPath
	}

	label, err := getxattr(fpath, xattrNameSelinux)
	if err != nil {
		return "", &os.PathError{Op: "getxattr", Path: fpath, Err: err}
	}
	// Trim the NUL byte at the end of the byte buffer, if present.
	if len(label) > 0 && label[len(label)-1] == '\x00' {
		label = label[:len(label)-1]
	}
	return string(label), nil
}

// lFileLabel returns the SELinux label for this path, not following symlinks,
// or returns an error.
func lFileLabel(fpath string) (string, error) {
	if fpath == "" {
		return "", ErrEmptyPath
	}

	label, err := lgetxattr(fpath, xattrNameSelinux)
	if err != nil {
		return "", &os.PathError{Op: "lgetxattr", Path: fpath, Err: err}
	}
	// Trim the NUL byte at the end of the byte buffer, if present.
	if len(label) > 0 && label[len(label)-1] == '\x00' {
		label = label[:len(label)-1]
	}
	return string(label), nil
}

func setFSCreateLabel(label string) error {
	return writeCon(attrPath("fscreate"), label)
}

// fsCreateLabel returns the default label the kernel which the kernel is using
// for file system objects created by this task. "" indicates default.
func fsCreateLabel() (string, error) {
	return readCon(attrPath("fscreate"))
}

// currentLabel returns the SELinux label of the current process thread, or an error.
func currentLabel() (string, error) {
	return readCon(attrPath("current"))
}

// pidLabel returns the SELinux label of the given pid, or an error.
func pidLabel(pid int) (string, error) {
	return readCon(fmt.Sprintf("/proc/%d/attr/current", pid))
}

// ExecLabel returns the SELinux label that the kernel will use for any programs
// that are executed by the current process thread, or an error.
func execLabel() (string, error) {
	return readCon(attrPath("exec"))
}

func writeCon(fpath, val string) error {
	if fpath == "" {
		return ErrEmptyPath
	}
	if val == "" {
		if !getEnabled() {
			return nil
		}
	}

	out, err := os.OpenFile(fpath, os.O_WRONLY, 0)
	if err != nil {
		return err
	}
	defer out.Close()

	if err := isProcHandle(out); err != nil {
		return err
	}

	if val != "" {
		_, err = out.Write([]byte(val))
	} else {
		_, err = out.Write(nil)
	}
	if err != nil {
		return err
	}
	return nil
}

func attrPath(attr string) string {
	// Linux >= 3.17 provides this
	const threadSelfPrefix = "/proc/thread-self/attr"

	attrPathOnce.Do(func() {
		st, err := os.Stat(threadSelfPrefix)
		if err == nil && st.Mode().IsDir() {
			haveThreadSelf = true
		}
	})

	if haveThreadSelf {
		return filepath.Join(threadSelfPrefix, attr)
	}

	return filepath.Join("/proc/self/task", strconv.Itoa(unix.Gettid()), "attr", attr)
}

// canonicalizeContext takes a context string and writes it to the kernel
// the function then returns the context that the kernel will use. Use this
// function to check if two contexts are equivalent
func canonicalizeContext(val string) (string, error) {
	return readWriteCon(filepath.Join(getSelinuxMountPoint(), "context"), val)
}

// computeCreateContext requests the type transition from source to target for
// class from the kernel.
func computeCreateContext(source string, target string, class string) (string, error) {
	classidx, err := classIndex(class)
	if err != nil {
		return "", err
	}

	return readWriteCon(filepath.Join(getSelinuxMountPoint(), "create"), fmt.Sprintf("%s %s %d", source, target, classidx))
}

// catsToBitset stores categories in a bitset.
func catsToBitset(cats string) (*big.Int, error) {
	bitset := new(big.Int)

	catlist := strings.Split(cats, ",")
	for _, r := range catlist {
		ranges := strings.SplitN(r, ".", 2)
		if len(ranges) > 1 {
			catstart, err := parseLevelItem(ranges[0], category)
			if err != nil {
				return nil, err
			}
			catend, err := parseLevelItem(ranges[1], category)
			if err != nil {
				return nil, err
			}
			for i := catstart; i <= catend; i++ {
				bitset.SetBit(bitset, int(i), 1)
			}
		} else {
			cat, err := parseLevelItem(ranges[0], category)
			if err != nil {
				return nil, err
			}
			bitset.SetBit(bitset, int(cat), 1)
		}
	}

	return bitset, nil
}

// parseLevelItem parses and verifies that a sensitivity or category are valid
func parseLevelItem(s string, sep levelItem) (uint, error) {
	if len(s) < minSensLen || levelItem(s[0]) != sep {
		return 0, ErrLevelSyntax
	}
	val, err := strconv.ParseUint(s[1:], 10, 32)
	if err != nil {
		return 0, err
	}

	return uint(val), nil
}

// parseLevel fills a level from a string that contains
// a sensitivity and categories
func (l *level) parseLevel(levelStr string) error {
	lvl := strings.SplitN(levelStr, ":", 2)
	sens, err := parseLevelItem(lvl[0], sensitivity)
	if err != nil {
		return fmt.Errorf("failed to parse sensitivity: %w", err)
	}
	l.sens = sens
	if len(lvl) > 1 {
		cats, err := catsToBitset(lvl[1])
		if err != nil {
			return fmt.Errorf("failed to parse categories: %w", err)
		}
		l.cats = cats
	}

	return nil
}

// rangeStrToMLSRange marshals a string representation of a range.
func rangeStrToMLSRange(rangeStr string) (*mlsRange, error) {
	r := &mlsRange{}
	l := strings.SplitN(rangeStr, "-", 2)

	switch len(l) {
	// rangeStr that has a low and a high level, e.g. s4:c0.c1023-s6:c0.c1023
	case 2:
		r.high = &level{}
		if err := r.high.parseLevel(l[1]); err != nil {
			return nil, fmt.Errorf("failed to parse high level %q: %w", l[1], err)
		}
		fallthrough
	// rangeStr that is single level, e.g. s6:c0,c3,c5,c30.c1023
	case 1:
		r.low = &level{}
		if err := r.low.parseLevel(l[0]); err != nil {
			return nil, fmt.Errorf("failed to parse low level %q: %w", l[0], err)
		}
	}

	if r.high == nil {
		r.high = r.low
	}

	return r, nil
}

// bitsetToStr takes a category bitset and returns it in the
// canonical selinux syntax
func bitsetToStr(c *big.Int) string {
	var str string

	length := 0
	for i := int(c.TrailingZeroBits()); i < c.BitLen(); i++ {
		if c.Bit(i) == 0 {
			continue
		}
		if length == 0 {
			if str != "" {
				str += ","
			}
			str += "c" + strconv.Itoa(i)
		}
		if c.Bit(i+1) == 1 {
			length++
			continue
		}
		if length == 1 {
			str += ",c" + strconv.Itoa(i)
		} else if length > 1 {
			str += ".c" + strconv.Itoa(i)
		}
		length = 0
	}

	return str
}

func (l *level) equal(l2 *level) bool {
	if l2 == nil || l == nil {
		return l == l2
	}
	if l2.sens != l.sens {
		return false
	}
	if l2.cats == nil || l.cats == nil {
		return l2.cats == l.cats
	}
	return l.cats.Cmp(l2.cats) == 0
}

// String returns an mlsRange as a string.
func (m mlsRange) String() string {
	low := "s" + strconv.Itoa(int(m.low.sens))
	if m.low.cats != nil && m.low.cats.BitLen() > 0 {
		low += ":" + bitsetToStr(m.low.cats)
	}

	if m.low.equal(m.high) {
		return low
	}

	high := "s" + strconv.Itoa(int(m.high.sens))
	if m.high.cats != nil && m.high.cats.BitLen() > 0 {
		high += ":" + bitsetToStr(m.high.cats)
	}

	return low + "-" + high
}

func max(a, b uint) uint {
	if a > b {
		return a
	}
	return b
}

func min(a, b uint) uint {
	if a < b {
		return a
	}
	return b
}

// calculateGlbLub computes the glb (greatest lower bound) and lub (least upper bound)
// of a source and target range.
// The glblub is calculated as the greater of the low sensitivities and
// the lower of the high sensitivities and the and of each category bitset.
func calculateGlbLub(sourceRange, targetRange string) (string, error) {
	s, err := rangeStrToMLSRange(sourceRange)
	if err != nil {
		return "", err
	}
	t, err := rangeStrToMLSRange(targetRange)
	if err != nil {
		return "", err
	}

	if s.high.sens < t.low.sens || t.high.sens < s.low.sens {
		/* these ranges have no common sensitivities */
		return "", ErrIncomparable
	}

	outrange := &mlsRange{low: &level{}, high: &level{}}

	/* take the greatest of the low */
	outrange.low.sens = max(s.low.sens, t.low.sens)

	/* take the least of the high */
	outrange.high.sens = min(s.high.sens, t.high.sens)

	/* find the intersecting categories */
	if s.low.cats != nil && t.low.cats != nil {
		outrange.low.cats = new(big.Int)
		outrange.low.cats.And(s.low.cats, t.low.cats)
	}
	if s.high.cats != nil && t.high.cats != nil {
		outrange.high.cats = new(big.Int)
		outrange.high.cats.And(s.high.cats, t.high.cats)
	}

	return outrange.String(), nil
}

func readWriteCon(fpath string, val string) (string, error) {
	if fpath == "" {
		return "", ErrEmptyPath
	}
	f, err := os.OpenFile(fpath, os.O_RDWR, 0)
	if err != nil {
		return "", err
	}
	defer f.Close()

	_, err = f.Write([]byte(val))
	if err != nil {
		return "", err
	}

	return readConFd(f)
}

// peerLabel retrieves the label of the client on the other side of a socket
func peerLabel(fd uintptr) (string, error) {
	l, err := unix.GetsockoptString(int(fd), unix.SOL_SOCKET, unix.SO_PEERSEC)
	if err != nil {
		return "", &os.PathError{Op: "getsockopt", Path: "fd " + strconv.Itoa(int(fd)), Err: err}
	}
	return l, nil
}

// setKeyLabel takes a process label and tells the kernel to assign the
// label to the next kernel keyring that gets created
func setKeyLabel(label string) error {
	err := writeCon("/proc/self/attr/keycreate", label)
	if errors.Is(err, os.ErrNotExist) {
		return nil
	}
	if label == "" && errors.Is(err, os.ErrPermission) {
		return nil
	}
	return err
}

// get returns the Context as a string
func (c Context) get() string {
	if l := c["level"]; l != "" {
		return c["user"] + ":" + c["role"] + ":" + c["type"] + ":" + l
	}
	return c["user"] + ":" + c["role"] + ":" + c["type"]
}

// newContext creates a new Context struct from the specified label
func newContext(label string) (Context, error) {
	c := make(Context)

	if len(label) != 0 {
		con := strings.SplitN(label, ":", 4)
		if len(con) < 3 {
			return c, ErrInvalidLabel
		}
		c["user"] = con[0]
		c["role"] = con[1]
		c["type"] = con[2]
		if len(con) > 3 {
			c["level"] = con[3]
		}
	}
	return c, nil
}

// clearLabels clears all reserved labels
func clearLabels() {
	state.Lock()
	state.mcsList = make(map[string]bool)
	state.Unlock()
}

// reserveLabel reserves the MLS/MCS level component of the specified label
func reserveLabel(label string) {
	if len(label) != 0 {
		con := strings.SplitN(label, ":", 4)
		if len(con) > 3 {
			_ = mcsAdd(con[3])
		}
	}
}

func selinuxEnforcePath() string {
	return filepath.Join(getSelinuxMountPoint(), "enforce")
}

// isMLSEnabled checks if MLS is enabled.
func isMLSEnabled() bool {
	enabledB, err := os.ReadFile(filepath.Join(getSelinuxMountPoint(), "mls"))
	if err != nil {
		return false
	}
	return bytes.Equal(enabledB, []byte{'1'})
}

// enforceMode returns the current SELinux mode Enforcing, Permissive, Disabled
func enforceMode() int {
	var enforce int

	enforceB, err := os.ReadFile(selinuxEnforcePath())
	if err != nil {
		return -1
	}
	enforce, err = strconv.Atoi(string(enforceB))
	if err != nil {
		return -1
	}
	return enforce
}

// setEnforceMode sets the current SELinux mode Enforcing, Permissive.
// Disabled is not valid, since this needs to be set at boot time.
func setEnforceMode(mode int) error {
	//nolint:gosec // ignore G306: permissions to be 0600 or less.
	return os.WriteFile(selinuxEnforcePath(), []byte(strconv.Itoa(mode)), 0o644)
}

// defaultEnforceMode returns the systems default SELinux mode Enforcing,
// Permissive or Disabled. Note this is just the default at boot time.
// EnforceMode tells you the systems current mode.
func defaultEnforceMode() int {
	switch readConfig(selinuxTag) {
	case "enforcing":
		return Enforcing
	case "permissive":
		return Permissive
	}
	return Disabled
}

func mcsAdd(mcs string) error {
	if mcs == "" {
		return nil
	}
	state.Lock()
	defer state.Unlock()
	if state.mcsList[mcs] {
		return ErrMCSAlreadyExists
	}
	state.mcsList[mcs] = true
	return nil
}

func mcsDelete(mcs string) {
	if mcs == "" {
		return
	}
	state.Lock()
	defer state.Unlock()
	state.mcsList[mcs] = false
}

func intToMcs(id int, catRange uint32) string {
	var (
		SETSIZE = int(catRange)
		TIER    = SETSIZE
		ORD     = id
	)

	if id < 1 || id > 523776 {
		return ""
	}

	for ORD > TIER {
		ORD -= TIER
		TIER--
	}
	TIER = SETSIZE - TIER
	ORD += TIER
	return fmt.Sprintf("s0:c%d,c%d", TIER, ORD)
}

func uniqMcs(catRange uint32) string {
	var (
		n      uint32
		c1, c2 uint32
		mcs    string
	)

	for {
		_ = binary.Read(rand.Reader, binary.LittleEndian, &n)
		c1 = n % catRange
		_ = binary.Read(rand.Reader, binary.LittleEndian, &n)
		c2 = n % catRange
		if c1 == c2 {
			continue
		} else if c1 > c2 {
			c1, c2 = c2, c1
		}
		mcs = fmt.Sprintf("s0:c%d,c%d", c1, c2)
		if err := mcsAdd(mcs); err != nil {
			continue
		}
		break
	}
	return mcs
}

// releaseLabel un-reserves the MLS/MCS Level field of the specified label,
// allowing it to be used by another process.
func releaseLabel(label string) {
	if len(label) != 0 {
		con := strings.SplitN(label, ":", 4)
		if len(con) > 3 {
			mcsDelete(con[3])
		}
	}
}

// roFileLabel returns the specified SELinux readonly file label
func roFileLabel() string {
	return readOnlyFileLabel
}

func openContextFile() (*os.File, error) {
	if f, err := os.Open(contextFile); err == nil {
		return f, nil
	}
	return os.Open(filepath.Join(policyRoot(), "contexts", "lxc_contexts"))
}

func loadLabels() {
	labels = make(map[string]string)
	in, err := openContextFile()
	if err != nil {
		return
	}
	defer in.Close()

	scanner := bufio.NewScanner(in)

	for scanner.Scan() {
		line := bytes.TrimSpace(scanner.Bytes())
		if len(line) == 0 {
			// Skip blank lines
			continue
		}
		if line[0] == ';' || line[0] == '#' {
			// Skip comments
			continue
		}
		fields := bytes.SplitN(line, []byte{'='}, 2)
		if len(fields) != 2 {
			continue
		}
		key, val := bytes.TrimSpace(fields[0]), bytes.TrimSpace(fields[1])
		labels[string(key)] = string(bytes.Trim(val, `"`))
	}

	con, _ := NewContext(labels["file"])
	con["level"] = fmt.Sprintf("s0:c%d,c%d", maxCategory-2, maxCategory-1)
	privContainerMountLabel = con.get()
	reserveLabel(privContainerMountLabel)
}

func label(key string) string {
	loadLabelsOnce.Do(func() {
		loadLabels()
	})
	return labels[key]
}

// kvmContainerLabels returns the default processLabel and mountLabel to be used
// for kvm containers by the calling process.
func kvmContainerLabels() (string, string) {
	processLabel := label("kvm_process")
	if processLabel == "" {
		processLabel = label("process")
	}

	return addMcs(processLabel, label("file"))
}

// initContainerLabels returns the default processLabel and file labels to be
// used for containers running an init system like systemd by the calling process.
func initContainerLabels() (string, string) {
	processLabel := label("init_process")
	if processLabel == "" {
		processLabel = label("process")
	}

	return addMcs(processLabel, label("file"))
}

// containerLabels returns an allocated processLabel and fileLabel to be used for
// container labeling by the calling process.
func containerLabels() (processLabel string, fileLabel string) {
	if !getEnabled() {
		return "", ""
	}

	processLabel = label("process")
	fileLabel = label("file")
	readOnlyFileLabel = label("ro_file")

	if processLabel == "" || fileLabel == "" {
		return "", fileLabel
	}

	if readOnlyFileLabel == "" {
		readOnlyFileLabel = fileLabel
	}

	return addMcs(processLabel, fileLabel)
}

func addMcs(processLabel, fileLabel string) (string, string) {
	scon, _ := NewContext(processLabel)
	if scon["level"] != "" {
		mcs := uniqMcs(CategoryRange)
		scon["level"] = mcs
		processLabel = scon.Get()
		scon, _ = NewContext(fileLabel)
		scon["level"] = mcs
		fileLabel = scon.Get()
	}
	return processLabel, fileLabel
}

// securityCheckContext validates that the SELinux label is understood by the kernel
func securityCheckContext(val string) error {
	//nolint:gosec // ignore G306: permissions to be 0600 or less.
	return os.WriteFile(filepath.Join(getSelinuxMountPoint(), "context"), []byte(val), 0o644)
}

// copyLevel returns a label with the MLS/MCS level from src label replaced on
// the dest label.
func copyLevel(src, dest string) (string, error) {
	if src == "" {
		return "", nil
	}
	if err := SecurityCheckContext(src); err != nil {
		return "", err
	}
	if err := SecurityCheckContext(dest); err != nil {
		return "", err
	}
	scon, err := NewContext(src)
	if err != nil {
		return "", err
	}
	tcon, err := NewContext(dest)
	if err != nil {
		return "", err
	}
	mcsDelete(tcon["level"])
	_ = mcsAdd(scon["level"])
	tcon["level"] = scon["level"]
	return tcon.Get(), nil
}

// chcon changes the fpath file object to the SELinux label.
// If fpath is a directory and recurse is true, then chcon walks the
// directory tree setting the label.
func chcon(fpath string, label string, recurse bool) error {
	if fpath == "" {
		return ErrEmptyPath
	}
	if label == "" {
		return nil
	}

	excludePaths := map[string]bool{
		"/":           true,
		"/bin":        true,
		"/boot":       true,
		"/dev":        true,
		"/etc":        true,
		"/etc/passwd": true,
		"/etc/pki":    true,
		"/etc/shadow": true,
		"/home":       true,
		"/lib":        true,
		"/lib64":      true,
		"/media":      true,
		"/opt":        true,
		"/proc":       true,
		"/root":       true,
		"/run":        true,
		"/sbin":       true,
		"/srv":        true,
		"/sys":        true,
		"/tmp":        true,
		"/usr":        true,
		"/var":        true,
		"/var/lib":    true,
		"/var/log":    true,
	}

	if home := os.Getenv("HOME"); home != "" {
		excludePaths[home] = true
	}

	if sudoUser := os.Getenv("SUDO_USER"); sudoUser != "" {
		if usr, err := user.Lookup(sudoUser); err == nil {
			excludePaths[usr.HomeDir] = true
		}
	}

	if fpath != "/" {
		fpath = strings.TrimSuffix(fpath, "/")
	}
	if excludePaths[fpath] {
		return fmt.Errorf("SELinux relabeling of %s is not allowed", fpath)
	}

	if !recurse {
		err := lSetFileLabel(fpath, label)
		if err != nil {
			// Check if file doesn't exist, must have been removed
			if errors.Is(err, os.ErrNotExist) {
				return nil
			}
			// Check if current label is correct on disk
			flabel, nerr := lFileLabel(fpath)
			if nerr == nil && flabel == label {
				return nil
			}
			// Check if file doesn't exist, must have been removed
			if errors.Is(nerr, os.ErrNotExist) {
				return nil
			}
			return err
		}
		return nil
	}

	return rchcon(fpath, label)
}

func rchcon(fpath, label string) error { //revive:disable:cognitive-complexity
	fastMode := false
	// If the current label matches the new label, assume
	// other labels are correct.
	if cLabel, err := lFileLabel(fpath); err == nil && cLabel == label {
		fastMode = true
	}
	return pwalkdir.Walk(fpath, func(p string, _ fs.DirEntry, _ error) error {
		if fastMode {
			if cLabel, err := lFileLabel(fpath); err == nil && cLabel == label {
				return nil
			}
		}
		err := lSetFileLabel(p, label)
		// Walk a file tree can race with removal, so ignore ENOENT.
		if errors.Is(err, os.ErrNotExist) {
			return nil
		}
		return err
	})
}

// dupSecOpt takes an SELinux process label and returns security options that
// can be used to set the SELinux Type and Level for future container processes.
func dupSecOpt(src string) ([]string, error) {
	if src == "" {
		return nil, nil
	}
	con, err := NewContext(src)
	if err != nil {
		return nil, err
	}
	if con["user"] == "" ||
		con["role"] == "" ||
		con["type"] == "" {
		return nil, nil
	}
	dup := []string{
		"user:" + con["user"],
		"role:" + con["role"],
		"type:" + con["type"],
	}

	if con["level"] != "" {
		dup = append(dup, "level:"+con["level"])
	}

	return dup, nil
}

// findUserInContext scans the reader for a valid SELinux context
// match that is verified with the verifier. Invalid contexts are
// skipped. It returns a matched context or an empty string if no
// match is found. If a scanner error occurs, it is returned.
func findUserInContext(context Context, r io.Reader, verifier func(string) error) (string, error) {
	fromRole := context["role"]
	fromType := context["type"]
	scanner := bufio.NewScanner(r)

	for scanner.Scan() {
		fromConns := strings.Fields(scanner.Text())
		if len(fromConns) == 0 {
			// Skip blank lines
			continue
		}

		line := fromConns[0]

		if line[0] == ';' || line[0] == '#' {
			// Skip comments
			continue
		}

		// user context files contexts are formatted as
		// role_r:type_t:s0 where the user is missing.
		lineArr := strings.SplitN(line, ":", 4)
		// skip context with typo, or role and type do not match
		if len(lineArr) != 3 ||
			lineArr[0] != fromRole ||
			lineArr[1] != fromType {
			continue
		}

		for _, cc := range fromConns[1:] {
			toConns := strings.SplitN(cc, ":", 4)
			if len(toConns) != 3 {
				continue
			}

			context["role"] = toConns[0]
			context["type"] = toConns[1]

			outConn := context.get()
			if err := verifier(outConn); err != nil {
				continue
			}

			return outConn, nil
		}
	}
	if err := scanner.Err(); err != nil {
		return "", fmt.Errorf("failed to scan for context: %w", err)
	}

	return "", nil
}

func getDefaultContextFromReaders(c *defaultSECtx) (string, error) {
	if c.verifier == nil {
		return "", ErrVerifierNil
	}

	context, err := newContext(c.scon)
	if err != nil {
		return "", fmt.Errorf("failed to create label for %s: %w", c.scon, err)
	}

	// set so the verifier validates the matched context with the provided user and level.
	context["user"] = c.user
	context["level"] = c.level

	conn, err := findUserInContext(context, c.userRdr, c.verifier)
	if err != nil {
		return "", err
	}

	if conn != "" {
		return conn, nil
	}

	conn, err = findUserInContext(context, c.defaultRdr, c.verifier)
	if err != nil {
		return "", err
	}

	if conn != "" {
		return conn, nil
	}

	return "", fmt.Errorf("context %q not found: %w", c.scon, ErrContextMissing)
}

func getDefaultContextWithLevel(user, level, scon string) (string, error) {
	userPath := filepath.Join(policyRoot(), selinuxUsersDir, user)
	fu, err := os.Open(userPath)
	if err != nil {
		return "", err
	}
	defer fu.Close()

	defaultPath := filepath.Join(policyRoot(), defaultContexts)
	fd, err := os.Open(defaultPath)
	if err != nil {
		return "", err
	}
	defer fd.Close()

	c := defaultSECtx{
		user:       user,
		level:      level,
		scon:       scon,
		userRdr:    fu,
		defaultRdr: fd,
		verifier:   securityCheckContext,
	}

	return getDefaultContextFromReaders(&c)
}