selinix:introduction_to_selinux
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
selinix:introduction_to_selinux [2016/07/12 11:31] – peter | selinix:introduction_to_selinux [2019/12/04 20:10] (current) – removed peter | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== SELinux - Introduction to SELinux ====== | ||
- | |||
- | |||
- | Security-Enhanced Linux (SELinux) is a security enhancement to Linux which allows users and administrators more control over access levels. | ||
- | |||
- | SELinux also adds finer granularity to access controls. Instead of only being able to specify who can read, write or execute a file, SELinux lets you specify who can unlink, append only, move a file and so on. Additionally SELinux allows you to specify access to many resources other than files as well, such as network resources and interprocess communication (IPC). | ||
- | |||
- | SELinux enforces the idea that programs should be limited to what files they can access and what actions they can take. | ||
- | |||
- | SELinux is a kernel security extension, which can be used to guard against misconfigured or compromised programs. | ||
- | |||
- | SELinux can be any one of the following state: | ||
- | |||
- | * enforcing – SELinux security policy is enforced. | ||
- | * permissive – SELinux prints warnings instead of enforcing. | ||
- | * disabled – SELinux is fully disabled. | ||
- | |||
- | The type of policies that can be used for the SELinux include: | ||
- | |||
- | * targeted – This policy will protected only specific targeted network daemons (such as DNS, Apache and others). | ||
- | * mls - Multi Level Security (MLS) allows further categorization of data privilege levels, such as “confidential, | ||
- | * strict – This is for maximum SELinux protection. | ||
- | |||
- | |||
- | ===== SELinux ===== | ||
- | |||
- | SELinux is an implementation of a mandatory access control mechanism in the Linux kernel, checking for allowed operations after standard discretionary access controls are checked. | ||
- | |||
- | When using SELinux, files, including directories and devices, are referred to as objects. | ||
- | |||
- | Relying on DAC mechanisms alone is fundamentally inadequate for strong system security. | ||
- | |||
- | SELinux adds Mandatory Access Control (MAC) to the Linux kernel, and is enabled by default in Red Hat Enterprise Linux. | ||
- | |||
- | MAC provides strong separation of applications that permits the safe execution of untrustworthy applications. | ||
- | |||
- | ===== Benefits of SELinux Controls ===== | ||
- | |||
- | * All processes and files are labeled with a type. A type defines a domain for processes, and a type for files. | ||
- | * Fine-grained access control. | ||
- | * SELinux policy is administratively-defined, | ||
- | * Reduced vulnerability to privilege escalation attacks. | ||
- | * SELinux can be used to enforce data confidentiality and integrity, as well as protecting processes from untrusted inputs. | ||
- | |||
- | |||
- | ==== SELinux Architecture ==== | ||
- | |||
- | SELinux is a Linux security module that is built into the Linux kernel. | ||
- | |||
- | |||
- | ==== Impact and Overhead ==== | ||
- | |||
- | It is envisaged that there should be minimal resource overhead should SELinux be deployed, in terms of system performance. | ||
- | |||
- | SELinux decisions, such as allowing or disallowing access, are cached. | ||
- | |||
- | |||
- | ==== Policies ==== | ||
- | |||
- | The SELinux Policy is the set of rules that guide the SELinux security engine. | ||
- | |||
- | A type is a way of grouping items based on their similarity from a security perspective. | ||
- | |||
- | These object types are considered alike because they are accessible in the same way by the same set of subjects. | ||
- | |||
- | For example, the binary executable file object at / | ||
- | |||
- | The SELinux policy defines various rules which determine how each domain may access each type. Only what is specifically allowed by the rules is permitted. | ||
- | |||
- | The policy can be defined either by modifying the existing files or by adding local //Type Enforcement (TE)// and //File Context (FC)// files to the policy tree. These new policies can be loaded into the kernel in real time. Otherwise, the policy is loaded during the boot process by / | ||
- | |||
- | |||
- | ===== Policy Deployment Strategy ===== | ||
- | |||
- | The default SELinux policy should be usable on almost any machine with minimal configuration and a small amount of system administrator training. | ||
- | |||
- | This policy should prevent system services - including most of the common network-visible services such as mail servers, ftp servers, and DNS servers - from accessing files which those services have no valid reason to access. | ||
- | |||
- | Almost every service that listens on a network, such as sshd or httpd, is confined. | ||
- | |||
- | Under the **targeted policy**, every subject and object runs in the **unconfined_t** domain except for the specific targeted daemons. | ||
- | |||
- | Unconfined processes run in unconfined domains, for example, init programs run in the unconfined **initrc_t** domain, unconfined kernel processes run in the **kernel_t** domain, and unconfined Linux users run in the **unconfined_t** domain. | ||
- | |||
- | Processes running in unconfined domains fall back to using DAC rules exclusively. | ||
- | |||
- | In other words, any process that is not specifically covered by a policy is allowed to run unfettered, e.g. third party applications. | ||
- | |||
- | |||
- | ===== Appendix A - Technical Configuration Details ===== | ||
- | |||
- | There are two ways to configure SELinux under Red Hat Enterprise Linux: using the Security Level Configuration Tool (system-config-selinux), | ||
- | |||
- | The / | ||
- | |||
- | **NOTE**: | ||
- | |||
- | The / | ||
- | |||
- | The following explains the full subset of options available for configuration: | ||
- | |||
- | * SELINUX=enforcing|permissive|disabled — Defines the top-level state of SELinux on a system. | ||
- | * enforcing — The SELinux security policy is enforced. | ||
- | * permissive — The SELinux system prints warnings but does not enforce policy. [1] | ||
- | * disabled — SELinux is fully disabled. SELinux hooks are disengaged from the kernel and the pseudo-file system is unregistered. | ||
- | |||
- | [1] This is useful for debugging and troubleshooting purposes. | ||
- | |||
- | Actions made while SELinux is disabled may result in the file system no longer having the correct security context. | ||
- | |||
- | It is possible to use the fixfiles relabel command prior to enabling SELinux to relabel the file system. | ||
- | |||
- | * SELINUXTYPE=targeted|strict — Specifies which policy SELinux should enforce. | ||
- | * targeted — Only targeted network daemons are protected. | ||
- | * strict — Full SELinux protection, for all daemons. Security contexts are defined for all subjects and objects, and every action is processed by the policy enforcement server. | ||
- | |||
- | The opposite of the targeted policy is the strict policy. | ||
- | |||
- | The strict policy is a much more complex environment and Security Engineering would recommend that the SELINUXTYPE variable be set to targeted. | ||
- | |||
- | ==== Directory Structure ==== | ||
- | |||
- | The **/ | ||
- | |||
- | The following example shows sample contents of the / | ||
- | |||
- | < | ||
- | -rw-r--r-- 1 root root 448 Sep 22 17:34 config | ||
- | drwxr-xr-x 5 root root 4096 Sep 22 17:27 strict | ||
- | drwxr-xr-x 5 root root 4096 Sep 22 17:28 targeted | ||
- | </ | ||
- | |||
- | The two subdirectories, | ||
- | |||
- | |||
- | ==== SELinux Utilities ==== | ||
- | |||
- | The following are some of the commonly used SELinux utilities: | ||
- | |||
- | === setenforce === | ||
- | |||
- | / | ||
- | |||
- | For example: | ||
- | |||
- | setenforce 1 — SELinux runs in enforcing mode. | ||
- | setenforce 0 — SELinux runs in permissive mode. | ||
- | |||
- | To disable SELinux, you need to either specify the appropriate setenforce parameter in / | ||
- | |||
- | |||
- | === sestatus === | ||
- | |||
- | / | ||
- | |||
- | < | ||
- | SELinux status: enabled | ||
- | SELinuxfs mount: /selinux | ||
- | Current mode: enforcing | ||
- | Mode from config file: enforcing | ||
- | Policy version: 21 | ||
- | Policy from config file: targeted | ||
- | Process contexts: | ||
- | Current context: user_u: | ||
- | Init context: system_u: | ||
- | / | ||
- | </ | ||
- | |||
- | Refer to the **setools** or **policycoreutils** package contents for more information on all available binary utilities. | ||
- | |||
- | |||
- | ===== Appendix B - Amending / Creating Policies ===== | ||
- | |||
- | Inside the " | ||
- | |||
- | ==== Source Policy Installation ==== | ||
- | |||
- | Requirements to install the source policy include: | ||
- | |||
- | - In order to customize the policy or changing the policy | ||
- | - In order to learn more about the targeted Policy that how it is written. | ||
- | - You may need to define a policy in case of installation of a new program (which moves the newly installed program to the confined domains in SELinux). | ||
- | |||
- | To install the source policy, we first need to determine the currently installed policy. | ||
- | |||
- | <code bash> | ||
- | rpm -qa |grep -i selinux | ||
- | </ | ||
- | |||
- | This will show the selinux-policy-targeted package. | ||
- | |||
- | Install the package | ||
- | |||
- | <code bash> | ||
- | rpm -ivh selinux-policy-targeted.version.rpm # cd / | ||
- | </ | ||
- | |||
- | Beneath the / | ||
- | |||
- | These can then be edited and changed to suit particular goals. | ||
- | |||
- | |||
- | ==== Amending Policies ==== | ||
- | |||
- | There may be circumstances that require amending policies that are in force. | ||
- | |||
- | By design and default, web servers generally run upon two ports, 80(http) and 443(https). | ||
- | |||
- | <file bash / | ||
- | “Listen 80” -> “Listen 12345” | ||
- | </ | ||
- | |||
- | This will force the web server to listen on port 12345, however when we attempt to start the web server an error message is displayed. | ||
- | |||
- | < | ||
- | Starting httpd: | ||
- | (13)Permission denied: make_sock: count not bind to address 0.0.0.0: | ||
- | no listening sockets available, shutting down | ||
- | unable to open logs | ||
- | </ | ||
- | |||
- | Fix this: | ||
- | |||
- | <code bash> | ||
- | semanage port -a -t http_port_t -p tcp 12345 | ||
- | </ | ||
- | |||
- | Result: | ||
- | |||
- | < | ||
- | Starting httpd: | ||
- | </ | ||
- | |||
- | As the above shows, a further command – **semanage** – was run to allow this change to be implemented. | ||
- | |||
- | The **semanage** command is part of the " | ||
- | |||
- | The **sealert** command can be used to parse messages and provide useful information to help in this task. | ||
- | |||
- | For example, when the above command returned an error which we were not expecting, running the command | ||
- | |||
- | <code bash> | ||
- | sealert –a / | ||
- | </ | ||
- | |||
- | displays the output which advises on a possible course of action: | ||
- | |||
- | < | ||
- | SELinux is preventing / | ||
- | ... | ||
- | Suggests | ||
- | ... | ||
- | semanage port -a -t http_port_t -p tcp 12345 | ||
- | </ | ||
- | |||
- | |||
- | ==== Monitoring SELinux Denials ==== | ||
- | |||
- | RHEL6 also comes with utilities to support administrators when investigating issues which may be attributed to SELinux. | ||
- | |||
- | Of particular recommendation is " | ||
- | |||
- | The purpose behind setroubleshoot is to let users know when access has been denied, help them resolve it if necessary, and to reduce overall frustration while working through tight security restrictions in the default SELinux policies. | ||
- | |||
- | The GUI frontend for setroubleshoot is useful for developers and administrators who run Linux desktops for testing, or those who run servers with a display attached. | ||
- | |||
- | To install setroubleshoot, | ||
- | |||
- | <code bash> | ||
- | yum install setroubleshoot{-server, | ||
- | </ | ||
- | |||
- | Once installed, email alerting can provide sight over the estate. | ||
- | |||
- | |||
- | ===== Appendix C - SELinux and User Contexts ===== | ||
- | |||
- | By default, users are not constrained under SELinux by default on Redhat Enterprise Linux (and derivitives), | ||
- | |||
- | As previously mentioned, most of the coreutils have an additonal “-Z” flag which provides additional information about a users' SELinux domain. | ||
- | |||
- | As a standard user, e.g. peter: | ||
- | |||
- | <code bash> | ||
- | id && sudo -i | ||
- | </ | ||
- | |||
- | Result: | ||
- | |||
- | < | ||
- | uid=1234(peter) gid=1234(peter) groups=1234(peter), | ||
- | </ | ||
- | |||
- | Now as root | ||
- | |||
- | <code bash> | ||
- | id | ||
- | </ | ||
- | |||
- | Result: | ||
- | |||
- | < | ||
- | uid=0(root) gid=0(root) groups=0(root) context=unconfined_u: | ||
- | </ | ||
- | |||
- | A number of confined SELinux user roles are available. | ||
- | |||
- | |||
- | ==== Adding and Confining a User ==== | ||
- | |||
- | The following shows a new user being added to the system (note the added "-Z user_u" | ||
- | |||
- | **NOTE**: The " | ||
- | |||
- | Create a new test user named peter_se. | ||
- | |||
- | <code bash> | ||
- | useradd -Z user_u peter_se | ||
- | </ | ||
- | |||
- | Check all SELinux login permissions. | ||
- | |||
- | <code bash> | ||
- | semanage login -l | ||
- | </ | ||
- | |||
- | Result: | ||
- | |||
- | < | ||
- | Login Name SELinux User MLS/MCS Range | ||
- | |||
- | __default__ | ||
- | root unconfined_u | ||
- | peter_se | ||
- | system_u | ||
- | </ | ||
- | |||
- | Set a password for the test user: | ||
- | |||
- | <code bash> | ||
- | passwd peter_se | ||
- | </ | ||
- | |||
- | Run as root with this test user account: | ||
- | |||
- | <code bash> | ||
- | su - peter_se | ||
- | </ | ||
- | |||
- | Check the SELinux permissions for the user. | ||
- | |||
- | <code bash> | ||
- | id -Z | ||
- | </ | ||
- | |||
- | Result: | ||
- | |||
- | < | ||
- | unconfined_u: | ||
- | </ | ||
- | |||
- | Now logout. | ||
- | |||
- | <code bash> | ||
- | logout | ||
- | </ | ||
- | |||
- | Remote into the account as the test user. | ||
- | |||
- | < | ||
- | ssh peter_se@127.1 | ||
- | </ | ||
- | |||
- | Check the SELinux permissions again for the user. | ||
- | |||
- | <code bash> | ||
- | id -Z | ||
- | </ | ||
- | |||
- | Result: | ||
- | |||
- | < | ||
- | user_u: | ||
- | </ | ||
- | |||
- | <code bash> | ||
- | su - | ||
- | </ | ||
- | |||
- | Result: | ||
- | |||
- | Try to run as root again using the test user. This wont work. | ||
- | |||
- | < | ||
- | -bash su: command not found | ||
- | </ | ||
- | |||
- | Try to run the su command directly. | ||
- | |||
- | <code bash> | ||
- | /bin/su | ||
- | </ | ||
- | |||
- | Result: | ||
- | |||
- | < | ||
- | -bash /bin/su: Permission denied | ||
- | </ | ||
- | |||
- | |||
- | Now logout. | ||
- | |||
- | <code bash> | ||
- | logout | ||
- | </ | ||
- | |||
- | Remote into the account as a normal user. | ||
- | |||
- | <code bash> | ||
- | su - peter | ||
- | </ | ||
- | |||
- | and check the SELinux permissions: | ||
- | |||
- | <code bash> | ||
- | id -Z | ||
- | </ | ||
- | |||
- | Result: | ||
- | |||
- | < | ||
- | unconfined_u: | ||
- | </ | ||
- | |||
- | This shows that this user has unconfined access, i.e. all access. | ||
- | |||
- | Delete the test account | ||
- | |||
- | <code bash> | ||
- | userdel -r peter_se | ||
- | </ | ||
- | |||
- | |||
- | Linux users mapped to the SELinux **user_u** user run in the **user_t** domain. | ||
- | |||
- | Note however that the root user was able to " | ||
- | |||
- | Further, as can be seen, the test user cannot execute the " | ||
- | |||
- | As shown above, a new user was created which confined a user to the **user_u** class. | ||
- | |||
- | Check the SELinx permissions: | ||
- | |||
- | <code bash> | ||
- | semanage login -l | ||
- | </ | ||
- | |||
- | Result: | ||
- | |||
- | < | ||
- | Login Name SELinux User MLS/MCS Range | ||
- | |||
- | __default__ | ||
- | root unconfined_u | ||
- | system_u | ||
- | </ | ||
- | |||
- | Now modify the SELinux permissions for the user peter. | ||
- | |||
- | <code bash> | ||
- | login -a -s user_u peter | ||
- | </ | ||
- | |||
- | Check the SELinx permissions again: | ||
- | |||
- | |||
- | < | ||
- | Login Name SELinux User MLS/MCS Range | ||
- | |||
- | __default__ | ||
- | root unconfined_u | ||
- | peter_se | ||
- | system_u | ||
- | </ | ||
- | |||
- | |||
- | ==== SELinux User Capabilities ==== | ||
- | |||
- | In the table below, you can see examples of basic confined domains for Linux users in Red Hat Enterprise Linux 6: | ||
- | |||
- | ^User^Domain^X Window System^su or sudo^Execute in home directory and /tmp/ (default)^Networking^ | ||
- | |sysadm_u|sysadm_t|yes|su and sudo|yes|yes| | ||
- | |staff_u|staff_t|yes|only sudo|yes|yes| | ||
- | |user_u|user_t|yes|no|yes|yes| | ||
- | |guest_u|guest_t|no|no|no|yes| | ||
- | |xguest_u|xguest_t|yes|no|no|Firefox only| | ||
- | |||
- | * Linux users in the **user_t**, **guest_t**, | ||
- | * Linux users in the **sysadm_t**, | ||
- | * By default, Linux users in the **guest_t** and **xguest_t** domains cannot execute applications in their home directories or **/tmp/**, preventing them from executing applications, | ||
- | * By default, Linux users in the **staff_t** and **user_t** domains can execute applications in their home directories and **/ | ||
- | * The only network access Linux users in the **xguest_t** domain have is **Firefox** connecting to web pages. | ||
- | |||
- | |||
- | ==== Disabling User Executable Content ==== | ||
- | |||
- | In certain circumstances it may be useful to control users ability to load and execute programs. | ||
- | |||
- | Linux users in the **guest_t** and **xguest_t** domains can not execute applications in their home directories or **/tmp/**; however, by default, Linux users in the **user_t** and **staff_t** domains can. | ||
- | |||
- | Booleans are available to change this behavior, and are configured with the **setsebool** command. | ||
- | |||
- | The following screenshot shows (in the left pane) the command that is used to disable " | ||
- | |||
- | <code bash> | ||
- | setsebool -P allow_user_exec_content off | ||
- | </ | ||
- | |||
- | Then try to run something from the user#s home directory. | ||
- | |||
selinix/introduction_to_selinux.1468323067.txt.gz · Last modified: 2020/07/15 09:30 (external edit)