-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinux_SElinux.txt
841 lines (766 loc) · 52.9 KB
/
linux_SElinux.txt
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
[[{security.selinux,security.aaa,kernel.100]]
# SElinux Summary
- Ext.Refs
- <https://github.com/SELinuxProject/selinux/wiki>
- <https://github.com/SELinuxProject/setools/wiki>
- <https://github.com/SELinuxProject/cil/wiki>
- <https://github.com/SELinuxProject/refpolicy>
- (Book) "SElinux by example"
- (Book) <https://www.freetechbooks.com/the-selinux-notebook-the-foundations-t785.html>
- [Stop disabling SELinux](https://www.youtube.com/watch?v=cNoVgDqqJmM&feature=youtu.be)
```
| SE-LINUX GLOBAL SUMMARY ──────────────────────────────────────
|
| KERNEL
| OBJECTS CLASSES
| ↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
|Represent type─of─resources
|handled by kernel(vs app) Each type─of─resource has a set of
|that must be protected HARDCODED list of actions defined
|by Mandatory access Rules for each se-linux kernel object class
| ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓ ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
| File class ←→ [append, create, lock, ... ]
| Process class ←→ [dyntransition, ptrace, fork, ... ]
| INET Socket class ←→ [bind, read, ... ... ]
| login class ←→ [action1, action2, ... ... ]
| user class ←→ [action1, action2, ... ... ]
| ...
|
|─TheG Process objects differ from the rest in the sense that
|they are the "active" kernel object triggering a new system call,
|probably as a result of a user intention to access some other
|resource (kernel object instance in SELinux parlance)
|
|─ File alike objects (Files, directories,...) must be first
| "manually" labelled through extended file─system attributes.
| Other object labelling is done by kernel automatically
|
| KERNEL─OBJECT─INSTANCE CONTEXT
|
| ┌── SELinux context ────┐
| user:role:" type ":level
|
| - All Kernel Object instances are labeled with
| a 4-tuple SELINUX-CONTEXT (user:role:type:level)
|
| The type is the core data used for MACO TYPE─ENFORCEMENT
```
```
┌─────────────────────┬────────────────────────────────────────┐
│ DAC+MAC FLOW SUMMARY│ │
├─────────────────────┘ │
│ USER ┌─────────→ ┌─────────┐ │
│ SPACE │ ┌───────→ │ PROCESS │ ←─────────────────┐ │
│ │ │ ┌─────→ │"SUBJECT"│ │ │
│ │ │ │ │ type1 │ │ │
│ │ │ │ └─────────┘ │ │
│ │ │ │ 1) ↓ │ │
│ ───────────── │ │ │ ───── system─call ───────────────── │ ───│
│ KERNEL │ │ │ │☝request(hardcoded) │ │
│ SPACE │ │ │ │ action1 over targeted │ │
│ │ │ │ │object (labeled with │ │
│ │ │ │ │SEContext type2 ) │ │
│ │ │ │ 2) ↓ │ │
│ │ │ │ Lookup │ │
│ │ │ │ data 8) │Sys.│
│ │ │ │ │ ┌────────┐ │call│
│ │ │ │ KO 3) ↓ │target │ │Res.│
│ │ │ └───────── Error ┌────│object ├──┘ │
│ │ │ Checks │ │ type2 │ │
│ │ │ │ │ └────────┘ │
│ │ │ │ OK │ 7) │
│ │ │ 4) ↓ │ OK :exec gaction1 │
│ │ │ KO DAC │on target object │
│ │ └───────── permission │ │
│ │ checks │ │
│ │ KO │ │ │
│ │ (audit) 7) ↓ │ │
│ └───────────── LSM ────┘ │
│ hooks │
│ 5) │ ^ 6) │
│ type1 is allowed│ │ OK ☜ MAC │
│ action1 over│ │ KO TYPE │
│ type2 ?↓ │ ENFORCEMENT │
│ ┌───────────┐ │
│ │ Security │ │
│ │Enhance(SE)│ │
│ │ Server │ │
│ └───────────┘ │
│ ☝ │
│ The server will first query the AVC(ache) and return │
│ OK/KO if a match is found. │
│ If nothing is found the server will: │
│ 1) work over the AV─hash table for matching rules │
│ 2) calculate the result, │
│ 3) put the result back in AVC(ache) │
│ 4) Return with OK/KO │
└──────────────────────────────────────────────────────────────┘
```
```
┌────────────────────────────┬───────────────────────────────┐
│ INITIAL PRE─SETUP SUMMARY: │ │
├────────────────────────────┘ │
│ Define SE-Linux System policy with a flow similar to: │
│ │
│ create modules → load into → Init bitmap─"matrix" │
│ kernel context1─to─[context2,action] │
│ ↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑ │
│ Access Vector (AV) │
└────────────────────────────────────────────────────────────┘
```
```
┌──────────────────────────┬───────┐
│ SELinux bootup sequence │ │
├──────────────────────────┘ │
│ kernel start → │
│ load policy into memory │
│ (organized in modules) │
│ │
│ semodule command manages the │
│install, remove, reload, upgrade, │
│enable or disable fo modules │
│Ex. lists modules currently loaded│
│~ sudo semodule ─l │
│→abrt 1.2.0 │
│→accountsd 1.0.6 │
│... │
└──────────────────────────────────┘
```
```
┌─────────────────────────────────┐
│QUERY SOURCE TO TARGET RELATIONS:│
├─────────────────────────────────┴─────────────────────────────────────────────────────┐
│#~ sesearch --allow \ ← Show allowed │
│ --source httpd_t │
│ --target httpd_sys_content_t │
│ --class file ← kernel selinux object class │
│ → Found 4 semantic av rules: │
│ → allow httpd_t httpd_sys_content_t : file { ioctl read getattr lock open } ;│
│ → allow httpd_t httpd_content_type : file { ioctl read getattr lock open } ;│
│ → allow httpd_t httpd_content_type : file { ioctl read getattr lock open } ;│
│ → allow httpd_t httpdcontent : file { ioctl read write create .... } ;│
└─────────────────────────────────┴─────────────────────────────────────────────────────┘
```
```
│ DISPLAYING SELINUX CONTEXT ATTRIBUTES ────────
|
| - Use "-Z" flag to shell commands
|
| - FILE SYSTEM OBJECT (files, dirs,..) ───────
| $ ls -ldZ
| /usr/sbin/httpd → httpd_exec_t
| /var/www/html/ → httpd_sys_content_t
| /etc/apache2/ → httpd_config_t
| /var/log/httpd/ → httpd_log_t
| /etc/init.d/httpd → httpd_initrc_exec_t
| - SUBJECTS (running processes) ──────────────
| $ ps axZ │ grep [h]ttpd
| unconfined_u:system_r: httpd_ t:s0 ...
| - SOCKET ────────────────────────────────────
| $ sudo netstat ─tnlpZ │ grep httpd
| ... unconfined_u:system_r: httpd_ t:s0 ..
| - PORT ──────────────────────────────────────
| $ sudo semanage port ─l │ grep http
| http_cache_port_t tcp 3128,8080, ...
| ...
| http_port_t tcp 80, 443
```
```
| sestatus(8) ─────────────────────────────────────
| Return various status info: ex:
| $ sudo sestatus
| → SELinux (enforcing) status: enabled
| → SELinuxfs mount : /sys/fs/selinux
| → SELinux root directory : /etc/selinux
| → Loaded policy name : targeted
| → Current mode : permissive
| → Mode from config file : error (Success)
| → Policy MLS status : enabled
| → Policy deny_unknown status: allowed
| → Max kernel policy version : 28
```
```
| show AVC stats ───────────────────────────────
| $ sudo avcstat
| lookups hits misses allocs reclaims frees
| 6688846 5637360 1051486 051486 968960 1050979
```
```
| SHELL SCRIPTS SUPORT ───────────────────────
|
| getenforce(8) Returns status like:
| "permissive" │ "enforcing"
|
| selinuxenabled exits with
| 0 exit if enabled
| 1 exit if not
```
```
| ┌─────────────────┬────────┬────────────────────────────────────────────────────────────────────────┐
| │ TERMINOLOGY │ACRONYM │ DESCRIPTION │
| │─────────────────┼────────┼────────────────────────────────────────────────────────────────────────│
| │ Access Vector │AV │ - bit map representing a set of permissions such as open, read, ... │
| │ │ │ - Each policy defines a different AV. │
| │ │ │ - Actually is implemented as a hash table where the key is the tuple │
| │ │ │ (source-type, targeted-type, targeted-kernel-object-class) │
| │─────────────────┼────────┼────────────────────────────────────────────────────────────────────────│
| │ Access Vector │AVC │ ─ SELinux Security Server can take a time to calculate │
| │ Cache │ │ access decissions based on SE─rules. │
| │ │ │ ─ The AVC stores such decissions to speed up following │
| │ │ │ access avoiding to recompute. │
| │ │ │ ─ two AVCs exists: │
| │ │ │ ─ 1.kernel AVC caching decisions from Security Server │
| │ │ │ on behalf of kernel based object managers. │
| │ │ │ ─ 2.userspace AVC built into libselinux that caches │
| │ │ │ decisions when SELinux─aware applications use │
| │ │ │ avc_open(3) with avc_has_perm (3) or avc_has_perm_noaudit(3) │
| │ │ │ function calls saving kernel calls after first │
| │ │ │ decision has been made. │
| │─────────────────┼────────┼────────────────────────────────────────────────────────────────────────│
| │ Bell─La Padula │BLP │ │
| │─────────────────┼────────┼────────────────────────────────────────────────────────────────────────│
| │ Common Criteria│CC │ │
| │─────────────────┼────────┼────────────────────────────────────────────────────────────────────────│
| │ Common │CIL │ │
| │ Intermediate │ │ │
| │ Language │ │ │
| │─────────────────┼────────┼────────────────────────────────────────────────────────────────────────│
| │ Discretionary │DAC │ │
| │ Access │ │ │
| │ Control │ │ │
| │─────────────────┼────────┼────────────────────────────────────────────────────────────────────────│
| │ SELinux │ │ ─ consists of one or more processes associated │
| │ Domain │ │ to the type component of a Security Context. │
| │─────────────────┼────────┼────────────────────────────────────────────────────────────────────────│
| │ Flux Advanced │FLASK │ ─ See Flux Research Group (http://www.cs.utah.edu/flux/) │
| │ Security Kernel │ │ μ─kernel Environment (Fluke) │
| │─────────────────┼────────┼────────────────────────────────────────────────────────────────────────│
| │ Linux Security │LSM │ ─ framework providing hooks into kernel components │
| │ Module │ │ (e.g. disk, net─services,...) used by │
| │ │ │ security modules (SELinux, ....) to perform │
| │ │ │ access control checks. │
| │─────────────────┼────────┼────────────────────────────────────────────────────────────────────────│
| │ Mandatory Access│MAC │ ─ access control mechanism enforced by the system, │
| │ Control │ │ e.g. 'hard─wiring' the OS and applications or │
| │ │ │ via policies enforced by the administrator. │
| │─────────────────┼────────┼────────────────────────────────────────────────────────────────────────│
| │ Multi─Category │MCS │ │
| │ Security │ │ │
| │─────────────────┼────────┼────────────────────────────────────────────────────────────────────────│
| │ Multi─Level │MLS │ ─ Based on Bell─La Padula model for │
| │ Security │ │ confidentiality in that (for example) a │
| │ │ │ process running at a 'Confidential' level │
| │ │ │ can read / write at their current level but │
| │ │ │ only read down levels or write up levels. │
| │ │ │ ─ "Today" it is more commonly used for │
| │ │ │ application separation utilising the │
| │ │ │ Multi─Category Security variant. │
| │─────────────────┼────────┼────────────────────────────────────────────────────────────────────────│
| │ SELinux Policy │ │ - Set of (thousands of) rules that define the type-enforcement rules │
| │ │ │ in the AV bitmap matrix/hashtable │
| │─────────────────┼────────┼────────────────────────────────────────────────────────────────────────│
| │ Object Manager │OM │ ─ Userspace&kernel components responsible │
| │ │ │ management (object labeling, creation, access, destruction) │
| │ │ │ of SELinux object under their control. │
| │─────────────────┼────────┼────────────────────────────────────────────────────────────────────────│
| │ Security │SID │ │
| │ Identifier │ │ │
| │─────────────────┼────────┼────────────────────────────────────────────────────────────────────────│
| │ Simplified │SMACK │ │
| │ Mandatory │ │ │
| │ Access Control │ │ │
| │ Kernel │ │ │
| │─────────────────┼────────┼────────────────────────────────────────────────────────────────────────│
| │ Super─user │SUID │ │
| │ Identifier │ │ │
| │─────────────────┼────────┼────────────────────────────────────────────────────────────────────────│
| │ Type Enforcement│TE │ ─ set of rules declared in Policy describe │
| │ │ │ how the domain will interact with objects │
| │ │ │ ─ In practice: the AV bit─map used to check │
| │ │ │ where type1 is allowed actionN over type2 │
| │─────────────────┼────────┼────────────────────────────────────────────────────────────────────────│
| │ User Identifier│UID │ │
| │─────────────────┼────────┼────────────────────────────────────────────────────────────────────────│
| │ X(window) Access│XACE │ │
| │ Control │ │ │
| │ Extension │ │ │
| │─────────────────┼────────┼────────────────────────────────────────────────────────────────────────│
| │ Security Server │ │ A sub─system in the Linux kernel that makes access decisions │
| │ │ │ and computes security contexts based on Policy on behalf of │
| │ │ │ SELinux─aware applications and Object Managers. │
| │ │ │ The Security Server does not enforce a decision, it merely │
| │ │ │ states whether the operation is allowed or not according to the │
| │ │ │ Policy. It is the SELinux─aware application or Object │
| │ │ │ Manager responsibility to enforce the decision. │
| │─────────────────┼────────┼────────────────────────────────────────────────────────────────────────│
| │ Security Context│ │ An SELinux Security Context is a variable length string that │
| │ │ │ consists of the following mandatory components │
| │ │ │ user:role:type and an optional [:range] component. │
| │ │ │ Generally abbreviated to 'context', and sometimes called a │
| │ │ │ 'label'. │
| │─────────────────┼────────┼────────────────────────────────────────────────────────────────────────│
| │ Security │SID │ SIDs are unique opaque integer values mapped by the kernel │
| │ Identifier │ │ Security Server and userspace AVC that represent a Security Context. │
| │ │ │ The SIDs generated by the kernel Security Server are u32 │
| │ │ │ values that are passed via the Linux Security Module │
| │ │ │ hooks to/from the kernel Object Managers. │
| │─────────────────┼────────┼────────────────────────────────────────────────────────────────────────│
| │ Type Enforcement│ │ SELinux makes use of a specific style of type enforcement │
| │ │ │ (TE) to enforce Mandatory Access Control. This is where all │
| │ │ │ subjects and objects have a type identifier associated to them │
| │ │ │ that can then be used to enforce rules laid down by │
| │ │ │ Policy │
| └─────────────────┴────────┴────────────────────────────────────────────────────────────────────────┘
| ```
# SELinux Kernel Object Classes:
Reminder:
- For nearly every linux (kernel) object that must be protected there is a matching SELInux kernel class.
- This class has a defined set of hardcoded actions that will be allowed/denied by a running policy.
- Type enforcement will determine which "source"-context has which list of allowed-actions over
a given class labeled with a "target"-context
- Next follows a summary of classes and actions extracted from
<https://github.com/SELinuxProject/refpolicy/blob/master/policy/fl ask/access_vectors>
```
┌ FILE-ALIKE RELATED OBJECTS ─────────────────────────────────────────────────────┐
│ common file │class dir │class file │class lnk_file │class chr_file │
│ │inherits file │inherits file │inherits file │inherits file │
│ │ │ │ │ │
│ ioctl │add_name │execute_no_trans │open │execute_no_trans │
│ read │remove_name │entrypoint │audit_access │entrypoint │
│ write │reparent │execmod │execmod │execmod │
│ create │search │open │ │open │
│ getattr │rmdir │audit_access │audit_access │
│ setattr │open │ │ │
│ lock │audit_access │
│ relabelfrom │execmod │
│ relabelto │ │
│ append │
│ map │class sock_file │class fifo_file │class fd │class blk_file │
│ unlink │inherits file │inherits file │ │inherits file │
│ link │ │ │use │ │
│ rename │open │open │ │open │
│ execute │audit_access │audit_access │audit_access │
│ swapon │execmod │execmod │execmod │
│ quotaon │ │ │ │
│ mounton │
└─────────────────────────────────────────────────────────────────────────────────┘
┌ NETWORK RELATED OBJECTS ─────────────────────────────────────────────┐
│ │
│ COMMON SOCKET │CLASS SOCKET │class rawip_socket │
│ │(inherits socket) │inherits socket │
│ # file inheri. │class netlink_socket │ │
│ │inherits socket │node_bind │
│ ioctl │class packet_socket │
│ read │inherits socket │class unix_stream_socket │
│ write │class key_socket │inherits socket │
│ create │inherits socket │ │
│ getattr │class unix_dgram_socket │connectto │
│ setattr │inherits socket │newconn │
│ lock │acceptfrom │
│ relabelfrom │class tcp_socket │class node │class netif │
│ relabelto │inherits socket │ │ │
│ append │ │tcp_recv │tcp_recv │
│ map │connectto │tcp_send │tcp_send │
│ │newconn │udp_recv │udp_recv │
│ # socket │acceptfrom │udp_send │udp_send │
│ bind │node_bind │rawip_recv │rawip_recv │
│ connect │name_connect │rawip_send │rawip_send │
│ listen │enforce_dest │dccp_recv │
│ accept │class udp_socket │dccp_recv │dccp_send │
│ getopt │inherits socket │dccp_send │ingress │
│ setopt │ │recvfrom │egress │
│ shutdown │node_bind │sendto │
│ recvfrom │ │
│ sendto │
│ recv_msg │
│ send_msg │
│ name_bind │
└──────────────────────────────────────────────────────────────────────┘
PROCESS RELATED CAPABILITY RELATED SYTEM OPERATIONS COMMON IPC
│ │ │ class system │
│ class process │ common cap │ ipc_info │create
│ │ │ syslog_read │destroy
│ fork │ chown │ syslog_mod │getattr
│ transition │ dac_override │ syslog_console │setattr
│ sigchld │ dac_read_search │ module_request │read
│ sigkill │ fowner │ module_load │write
│ sigstop │ fsetid │ halt │associate
│ signull │ kill │ reboot │unix_read
│ signal │ setgid │ status │unix_write
│ ptrace │ setuid │ start
│ getsched │ setpcap │ stop │COMMON X
│ setsched │ linux_immutable │ enable │ X_DEVICE
│ getsession │ net_bind_service │ disable │pointer,keyb.
│ getpgid │ net_broadcast │ reload │getattr
│ setpgid │ net_admin │setattr
│ getcap │ net_raw CLASS FS │use
│ setcap │ ipc_lock │ mount │read
│ share │ ipc_owner │ remount │write
│ getattr │ sys_module │ unmount │getfocus
│ setexec │ sys_rawio │ getattr │setfocus
│ setfscreate │ sys_chroot │ relabelfrom │bell
│ noatsecure │ sys_ptrace │ relabelto │force_cursor
│ siginh │ sys_pacct │ transition │freeze
│ setrlimit │ sys_admin │ associate │grab
│ rlimitinh │ sys_boot │ quotamod │manage
│ dyntransition │ sys_nice │ quotaget │list_property
│ setcurrent │ sys_resource │get_property
│ execmem │ sys_time │COMMON DDBB │set_property
│ execstack │ sys_tty_config │ │add
│ execheap │ mknod │create │remove
│ setkeycreate │ lease │drop │create
│ setsockcreate │ audit_write │getattr │destroy
│ getrlimit │ audit_control │setattr
│ setfcap │relabelfrom
│relabelto
┌ OTHERS ─────────────────────────────────────────────────────────────┐
│ security, capability, X─Windows, Netlink, D─BUS, nscd, IPSEC, │
│ dccp,memprotect db_database/db_table/db_column/..., (network) peer, │
│ tun_socket, binder infiniband , ... │
└─────────────────────────────────────────────────────────────────────┘
```
# SELINUX FS-LABELING
- When a new file-alike object is created its contexts
is copied (by default) from its parent's directory
by default.
- This behaviour can be modified with a type_transition rule
in the policy.
```
| $ sudo chcon --type var_t index.html ← Changes context *temporarely*
| └───┘ └───┘ (FS relabel will revert changes)
|
| $ sudo restorecon -v index.html
| $ sudo restorecon reset index.html ← similar to fixfiles(8) suited
| unconfined_u:object_r: var_t:s0 -> for individual file or dir. relabeling
| unconfined_u:object_r: httpd_sys_content_t*:s0
```
## OTHERS:
* `fixfiles(8)`: Relabels FS objects. By default relabel all mounted
FSs that support SELinux unless mounted with the context mount
option, automatically detemining the file sec.ctx specs to use for
the labeling.
* `genhomedircon(8)`: Script for generating correct file ctx specs
for user's home directories.
See also: Troubleshooting with restorecon
* NOTE: SELinux context for remote FS can be specified *at mount time*.
*Verify a file context against file_context(.local) ddbb (active policy) *
```
| # matchpathcon -V /www/html/index.html
| ^^
|
| In case of mismatch an error similar to next one will be displayed:
| /www/html/index.html has context unconfined_u:object_r:default_t:s0,
| should be system_u:object_r:httpd_sys_content_t:s0
```
# SELINUX login objects:
- SELinux users are not created with a command,
nor does they have its own login access to the server.
- SELinux users are*defined in the policy*loaded into
memory at boot time, and*there are only a few of these users*
- Standard Linux Users are mapped to SElinux users upon login
according to selinux-login-objects defined by policy and
customizable through semanage login.
```
| │Linux│N<·¹·>1│SELinux│1<·²·>N│roles│·³>│process │
| │User │ │user │ │ │ │domain (*_t)│
|
| ¹ To show N─to─1 relationship:
| # semanage login ─
| (Example CentOS 7 output)
| Login Name SELinux User MLS/MCS Range Service
|
| __default__ unconfined_u s0─s0:c0.c1023 *
| root unconfined_u s0─s0:c0.c1023 *
| system_u system_u s0─s0:c0.c1023 *
| └────┬────┘ └────┬─────┘ └────┬────┘
| linux 1 <········>1 SE user Multi Level/
| user /Category Sec.
|
| ² A user "enters a roll" when policy grants it.
| To show the 1-to-N relationship:
| # semanage user
| (Example CentOS 7 output)
| Labeling ...
| SELinux User Prefix ... SELinux Roles
|
| guest_u user ... guest_r
| root user ... staff_r sysadm_r system_r unconfined_r
| staff_u user ... staff_r sysadm_r system_r unconfined_r
| sysadm_u user ... sysadm_r
| system_u user ... system_r unconfined_r
| unconfined_u user ... system_r unconfined_r
| user_u user ... user_r
| xguest_u user ... xguest_r
|
| ³ A role "access a process domain" when policy grants it.
```
```
| semanage login [opts]
|
| map linux user to selinux user upon login by
| Adding/modifying/deleting(and listing) login object types
|
| semanage login [opts]
| --add -s "SEUSER" "linuxUser" [─r "MLC/MCS range"]
| --modify -s "SEUSER" [─r "MLC/MCS range"]
| --delete "linuxUser"
| --deleteall # (all = local/non─policy─defined)
| --extract # ← (for use within TX)
| --list [--locallist]
| show local
| (non─defined─by─policy)
| customizations
| OTHER OPTIONS:
| --noreload ← Do NOT reload policy after commit
| --store STORE ← Select alternate SELinux Policy Store
```
* Example: Restricting su/sudo
```
|[regularuser@localhost ~]$ id ─
|unconfined_u:unconfined_r:unconfined_t:s0
|
| regularuser@localhost ~]$ su ─ switcheduser
| Password: XXXX
| → [switcheduser@localhost ~]$
|
| #~ semanage login ─a ─s user_u regularuser
| └────────────────┬───────────┘
| Remap linux_user to selinux user
|
| [regularuser@localhost ~]$ id ─
| user_u:user_r:user_t:s0
|
| [regularuser@localhost ~]$ su ─ switcheduser
| Password: XXXX
| → su: Authentication failure
```
* Example: disable script execution
```
| By default, SELinux allows users mapped to the
| guest_t account to exec $HOME/ scripts
|
| #~ getsebool allow_guest_exec_content
| guest_exec_content ─ → hon
|
| [guestuser@localhost ~]$ ~/myscript.sh
| This is a test script
|
| #~ setsebool allow_guest_exec_content off
| [guestuser@localhost ~]$ ~/myscript.sh
| bash: myscript.sh: Permission denied
```
## SELINUX PROCESS CONTEXT-SWITCH
- Kernel takes care of automatic process-context-labeling once
a policy has been loaded.
- Next commands can be used to alter or display the process labeling:
- runcon(1): run command with given ctx (user, role and domain)
- secon(1): See ctx from a file|program|user-input
- newrole(1): creates new shell running with new sec.ctx.
Use must specify new role and/or type.
type is derived from role if not specified.
- run_init(9): Runs initrc script using the sec.ctx.
found in current policy's ctx/initrc_context
file. Ussually used to restart system
services in new intended domain
## PROCESS LABELING FLOW AT LOGIN
- Login process sets a default context (unconfined for targeted policy)
Policy defined Context transitions will change children process context
at runtime.
## PROCESS CONTEXT TRANSITION
- A default context is given upon login defined by the
policy login-objects (see `semanage login` notes )
- Context Transtion Flow
```
| app_1 → Execute /usr/bin/app_2 → app_2
|
| domain/type src_t app_exec_t target_t
| ↑ ↑
| Requirements 1)- policy must define 2)- policy must define
| exec permission to app_exec_t as entrypoint
| app_exec_t entry-point to target_t
| 3)- policy must allow
| transition from src_t
| to target_t
```
```
| Check 1): | Check 2): | Check 3):
| # sesearch | # sesearch | # sesearch
| -s src_t | -s target_t | -s src_t
| -t app_exec_t | -t target_t | -t target_t
| -c file | -c file | -c process ¹
| -p execute | -p entrypoint | -p transition ²
| -Ad | -Ad | -Ad
¹ (selinux-kernel)class
² action requite for class(See selinux object classes)
```
# SELINUX Booleans
* <https://linux.die.net/man/8/booleans>
- Allow to customize a given policy at runtime.
- Common to-many-apps actions that can be allowed/denied are "grouped"
into booleans
Ex:
- """ Do we allow the ftp server access to home directories? """
- """ Can httpd use mod_auth_ntlm_winbind ? """
- ...
```
| List Booleans Set: ---------------------------------------
| Take a look at files:
| $ cat /etc/selinux/targeted/modules/active/booleans.local
| allow_console_login login_console_enabled
| allow_execheap selinuxuser_execheap
| allow_execmod selinuxuser_execmod
| allow_execstack selinuxuser_execstack
| allow_mount_anyfile mount_anyfile
| allow_ssh_keysign ssh_keysign
| allow_staff_exec_content staff_exec_content
| user_ping selinuxuser_ping
| virt_sandbox_use_nfs virt_use_nfs
| ....
|
| List Modified Booleans Set: ------------------------------
| $ cat /etc/selinux/targeted/booleans.subs_dist
| # This file is auto─generated by libsemanage. Do not edit directly.
| httpd_read_user_content=1
| httpd_enable_homedirs=1
| Change Booleans ------------------------------------------
| # getsebool - # <· Show all booleans
|
| # setsebool \ # <· Set boolean
| ─P "mySELinBool" 0│1 -P: (optional) Persist reboot
|
| # togglesebool \ # <· toggle 1<>0 value
| ─P "mySELinBool"
```
- Example. Fix Apache DNS problem:
(REF: <https://stackoverflow.com/questions/23851452/cant-resolve-domain-names-in-php-under-apache/24019910#24019910>)
```
$ sudo setsebool -P nis_enabled 0
$ sudo setsebool -P httpd_can_network_connect 1
```
## SELINUX TROUBLESHOOTING
```
### Q: What an SELinux error means? <!-- { -->
It can means:
1. file-system labeling is wrong
```
| Fix examples:
| Ex 1:
| # sudo chcon --reference /var/www/html ..../index.html
| Ex 2:
| # restorecon -vR /var/www/html
| ^^^^^^^^^^
| uses info from
| /etc/selinux/targeted/contexts/files/file_contexts, ...
| to determine what a file or dir's ctx should be
| Ex 3:
| # semanage fcontext -a -e /var/www/ /my/alternative/www/
| ^^^^^^^^
| WARN: we are just defining what the context is,
| we are not writing what the
| extended attributes are. It just means:
| "On relable it must look like this"
| # restorecon -vR /my/alternative/www
| ^^^^^^^^^^
| must be run to actually proceed with relabeling
```
2. Policy needs to be tweaked
```
- booleans
- policy modules
```
3. A bug in the policy
```
| App vendors must supply policy modules for SELinux systems.
| You must submit a ticket to the app vendor following steps:
|
| STEP 1 ------------------------------------------------------
| # setenforce 0 <· change to "permissive" mode and run
| the application through all its paces
| to log all SELinux denials
| STEP 2 ------------------------------------------------------
| From /var/log/messages Copy and Paste the proposed solution:
| # grep httpd /var/log/audit/audit.log │ \
| audit2allow -MO quirrellocal
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| generate new policy file
| (See also audit2why(1)
| STEP 3 ------------------------------------------------------
| # semodule - squirrellocal.pp ← Import the new module
| STEP 4 ------------------------------------------------------
| # setenforce 1 ← Re─enable enforcement
```
4. You have been, or are being, broken into!!
```
| - HOUSTON: We have a problem
```
<!-- } -->
### SELinux BUG FIXING GENERAL PROCEDURE
```
| STEP 01: Check /var/log/messages for messages like
| ...
| ... setroubleshoot: SELinux is preventing /usr/bin/httpd \
| from getattr access on the directory /home/fred. \
| For complet SELinux messages, run \
| sealert -l 37acc7d8-e955-4333-123a-1d027dbcea72
|
| STEP 02: Run the indicated command
| ~# sealert -l 37acc7d8-e955-4333-123a-1d027dbcea72
| → SELinux is preventing /usr/sbin/httpd from search access \
| on the directory /home/fred
| → ...
| → Do
| → setsebool -P httpd_read_user_content 1
| → ...
| → setsebool -P httpd_enable_homedirs 1
```
### AUDITING SELINUX ERROR MESSAGES
```
| #~ ausearch -m avc -c httpd
| └──────┘ └──┘
| standard filter by
| linux AVC related
| audit messaged
| framework
| tool
| (auditd daemon
| must be running)
| → ...
| → time → Thu Aug 21 16:42:17 2014
| → type=AVC msg=audit(1408603337.115:914): avc: denied { getattr } for \
| pid=10204 comm="httpd" path="/www/html/index.html" dev="dm─0" ino=8445484 \
| scontext=system_u:system_r:httpd_t:s0 \
| tcontext=unconfined_u:object_r:default_t:s0 tclass=file
| └ translates to:
| type=AVC: ...: avc: The message comes from the AVC log and it's an AVC event
| denied { getattr }: The permission that was attempted and the result it got.
| In this case the get attribute operation was denied.
| pid=10204 process id of the process that attempted the access.
| comm="httpd" shows the process command for the pid
| path: resource trying to be accessed.
| dev : device
| ino : inode
| scontext: source security context of the process.
| tcontext: target security context of the resource.
| tclass: target resource class.
|
| sealert tool
| #~ cat /var/log/messages │ grep "SELinux is preventing"
| → ...
| → ... setroubleshoot: SELinux is preventing /usr/bin/su from using the setuid capability \
| For complete SELinux messages. run sealert -l e9e6c6d8-f217-414c-a14e-4bccb70cfbce
| #~ sealert -l e9e6c6d8-f217-414c-a14e-4bccb70cfbce
| → SELinux is preventing /usr/bin/su from using the setuid capability.
| → ...
| → Raw Audit Messages
| → type=AVC msg=audit(1408931985.387:850): avc: denied { setuid } for pid=5855 \
| comm="sudo" capability=7 scontext=user_u:user_r:user_t:s0 \
| tcontext=user_u:user_r:user_t:s0 tclass=capability
| → type=SYSCALL msg=audit(1408931985.387:850): arch=x86_64 syscall=setresuid success=no \
| exit=EPERM a0=ffffffff a1=1 a2=ffffffff a3=7fae591b92e0 items=0 ppid=5739 pid=5855 \
| auid=1008 uid=0 gid=1008 euid=0 suid=0 fsuid=0 egid=0 sgid=1008 fsgid=0 tty=pts2 ses=22 \
| comm=sudo exe=/usr/bin/sudo subj=user_u:user_r:user_t:s0 key=(null)
| →
| → Hash: su,user_t,user_t,capability,setuid
| →
```
[[security.selinux}]]