SELinux
Mostly notes from Security-Enhanced Linux for mere mortals by Thomas Cameron | Redhat: SELinux User's and Administrator's Guide | The arch wiki article
SELinux policy:
The policy can be found at /etc/selinux/config,
which is also symlinked to /etc/sysconfig/selinux
SELINUX= can take one of these three values:
enforcing - SELinux security policy is enforced.
permissive - SELinux prints warnings instead of enforcing.
disabled - No SELinux policy is loaded.
SELINUXTYPE= can take one of three values:
targeted - Targeted processes are protected,
minimum - Modification of targeted policy. Only selected processes are protected.
mls - Multi Level Security protection.
It can also be seen via sestatus
The current mode can be viewed using getenforce
Labeling
Files, Ports etc are labelled with an SELinux context. for files and directories, extended attrs are used to store these labels while kernel manages the labels for processes and such.
They are of the format
user:rele:type:level(optional)
The user above is the selinux user and is different from the normal linux
user
just the type can be used for basic usecases
selinux adds the -Z option to several commands to view the labels associated
with the associated resources. A few of them are:
ls -Zid -Zps -Znetstat -Z
Type Enforcement
The types in the labels are usually related for resources that belong together.
Eg. for apache, the following types are observed:
httpd_config_tfor its config directoryhttpd_log_tfor its log directoryhttpd_sys_content_tfor the webroothttpd_initrc_exec_tfor the init scripthttpd_tfor the process
Type enforcement is the part of the policy that says, for instance, “a process running with the label httpd_t can have read access to a file labeled httpd_config_t"
Working with labels
chcon or restorecon may be used to change the context of a file
When a file is created, it inherits the context of its parent (wiht a few exceptions)
The login process sets the default context, which is unconfined for the
targeted policy
-
File transitions (defined by policy) may be set such that if an application foo_t creates a file in a directory labeled bar_t, it ges the baz_t label.
-
getsebool -ashows all booleans available -
use
setsebool <boolean_name> <0|1>to set the boolean temporarily- use the
-Pargument with it to make the change persistent
- use the
Tip: Install
setroubleshootandsetroubleshoot-serveron machines used to develop policy modules. Reboot or restart auditd afterwards. This will make the logs in/var/log/messagesmore human-readable
The SELinux logs tells you what to do in most cases
A good place to check for currently actiove booleans is the booleans.local
file under /etc/selinux/targeted/modules/active/.
Note: editing this file will not change anything.
Modifying policies using setsebool or other commands regenerates the
/etc/selinux/targeted directory. So sont bother changing anything in there.
use man chcon so see how to change the labels.
Usually to change just the type, use chcon -t my_type_t /path/to/file
-
Or even just use
chcon --reference /known/good/file /path/to/target/fileto -
use the refernce file's security context
-
Or maybe you just need to restore the context to its defaults. In this case, use
restorecon. eg:restorecon -vR /path/to/file-vis for verbose-Ris recursive- read man btw
restorecon(8)
-
restoreconuses information from the/etc/selinux/targeted/contexts/files/file_contexts(*)files to determine what a file or directory's context should be -
to add a new default context to be used, we use
semanage fcontext -a -t my_type_t "/foo(/.*)?"fcontext: for file contextman semanage-fcontext(8)-ato add-tfor type
-
or simply
semanage fcontext -a -e /known/good/file/ /foo/"-efor equal
-
dont forget to use
restoreconto apply the changes from the default context that was just set -
When working on creating a polllicy, a good way is to use a
permissivemode for SELinux to simply log the messages. Then after going through the usage of the application, use something likegrep httpd /var/log/audit/audit.log | audit2allow -M mymodulelocalto create a new policy based on the audit. Then usesemodule -i mymodulelocal.ppto install (-i) the module -
audit2whycan be used in a similar fashion toaudit2allowto get a human-readable descriptions of the audits
Enabling SELinux on a new system
Setting selinux to enforcing usually doesnt end so well
So first set it to permissive, touch a file called .autorelabel to the root of
the filesystem and reboot. This will relabel the whole filesystem to work with
selinux
After its done relabelling, set SELinux to enforcing