Introduction
SELinux or Security Enhanced Linux is a set of access controls developed by the NSA (National Security Administration) of the United States. It’s aim is to provide more fine-grained control over what applications and users are allowed to access on the system and how they are allowed to access it.
Description
Common SELinux troubleshooting techniques, commands and syntax.
Requirements
Explanation of requirements.
- Root or appropriate sudo access to the system in question, preferably a sandbox system.
- Some spare time to read and experiment.
Command Overview
Common SELinux related commands in alphabetical order:
ausearch – SELinux audit log search tool.
audit2allow – Generate SELinux policy allow rules from logs of denied operations.
audit2why – Determine which component of your policy caused a denial.
chcon – Tool for changing the SELinux context of files and directories.
fixfiles – Fixfiles is a shell script that wraps setfiles and restorecon.
getenforce – Tool for getting the SELinux enforcement state.
getsebool – Tool for getting SELinux boolean values.
matchpathcon – This is a simple tool that takes files/directories and prints the default security context of the files.
restorecon – Tool for reverting files back to the default labels.
semanage – Tool for managing SELinux policy mappings.
semodule – Tool for manipulating SELinux modules.
sestatus – Tool for retrieving the current SELinux status.
setenforce – Tool for setting the SELinux enforcement state.
setsebool – Tool for setting/toggling SELinux booleans.
setroubleshoot – GUI troubleshooting tool / daemon.
system-config-securitylevel-tui – Rudimentary tool for enabling/disabling/configuring SELinux and IPtables.
Common SELinux problems and solutions
- Check the current status/state of SELinux on your system:
- Open special port:
- Check that the port is added:
- Fix http proxy connect error: (for a list of other SELinux booleans see: getsebool)
- Fix database connect error: ‘Could not connect to the MySQL server: Can’t connect to MySQL server on ‘myserver.amazonaws.com’ (13)’
- Filesystem relabel: (requires reboot)
- List currently installed SELinux modules:
- Relabel or change SELinux file contexts:
- Search for recent SELinux AVC denials:
- Determine why SELinux has denied an event:
- View SELinux file contexts on a given directory and the files within it:
- Reset SELinux to its initial state:
yum remove selinux-policy
rm -rf /etc/selinux
yum install selinux-policy-targeted
fixfiles -f -F relabel
reboot
- Hand edit SELinux enforcement policy: (requires a reboot)
- Check what SELinux related rpms you have installed:
- Install and configure setroubleshoot to help you isolate and fix SELinux AVC denials:
- Use ausearch to locate denials and make policies.
- http://wiki.centos.org
- http://wiki.centos.org/TipsAndTricks/SelinuxBooleans
- http://wiki.centos.org/HowTos/SELinux
sestatus
semanage port -a -t http_port_t -p tcp 8081
semanage port -l | grep 8081
setsebool -P httpd_can_network_connect true
«- a value of ‘1’ / ‘0’ or ‘true’ / ‘false’ will work here
setsebool -P httpd_can_network_connect_db 1
«- a value of ‘1’ / ‘0’ or ‘true’ / ‘false’ will work here
fixfiles -f -F relabel
or: fixfiles onboot; reboot
semodule -l
chcon -t httpd_sys_content_t -R /var/www
chcon -t httpd_sys_content_t -R /home/john
Note: Apache httpd files are served from /var/www/ on Centos for security reasons.
ausearch -m avc -ts recent
ausearch -m avc -ts today
ausearch -m avc -ts today | audit2why
(You can also use getfattr although, you need to specify -n security.selinux)
ls -Z
getfattr -n security.selinux /var/www
vi /etc/selinux/config
Troubleshooting / How To Test
Explanation troubleshooting basics and expectations.
1 | rpm -qa --qf '%{name}-%{version}-%{release}\n'| grep -E -i '(checkpolicy|libselinux|libselinux-python|libselinux-utils|libsemanage|libsepol|mcstrans|policycoreutils|policycoreutils-gui|selinux-policy|selinux-policy-minimum|selinux-policy-mls selinux-policy-strict|selinux-policy-targeted|setroubleshoot|setroubleshoot-server|setroubleshoot-plugins|setools|setools-gui)' |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | libselinux-devel policycoreutils-python libsemanage-python setools-console setools libselinux-utils setroubleshoot-server setools-libs libsemanage selinux-policy policycoreutils libselinux selinux-policy-targeted libselinux-python3 libsepol libsepol-devel setroubleshoot setools-libs-tcl setools-gui setroubleshoot-plugins checkpolicy libselinux-python |
yum install setroubleshoot*
chkconfig --level 2345 setroubleshoot on
ausearch -m avc -ts recent
ausearch -m avc -ts today