Cari di RHE Linux 
    Red Hat Enterprise Linux Manual
Daftar Isi
(Sebelumnya) 8 : Security-Enhanced Linux8 : Chapter 6. Confining Users ... (Berikutnya)

Security-Enhanced Linux

Chapter 5. Working with SELinux

The following sections give a brief overview of the main SELinux packages in Red Hat Enterprise Linux; installing and updating packages; which log files are used; the main SELinux configuration file; enabling and disabling SELinux; SELinux modes; configuring Booleans; temporarily and persistently changing file and directory labels; overriding file system labels with the mount command; mounting NFS file systems; and how to preserve SELinux contexts when copying and archiving files and directories.

5.1. SELinux Packages

In Red Hat Enterprise Linux, the SELinux packages are installed by default, in a full installation, unless they are manually excluded during installation. If performing a minimal installation in text mode, the policycoreutils-python and the policycoreutils-gui package are not installed by default. Also, by default, SELinux targeted policy is used, and SELinux runs in enforcing mode. The following is a brief description of the SELinux packages that are installed on your system by default:
  • policycoreutils - provides utilities such as restorecon, secon, setfiles, semodule, load_policy, and setsebool, for operating and managing SELinux.
  • selinux-policy - provides the SELinux Reference Policy. The SELinux Reference Policy is a complete SELinux policy, and is used as a basis for other policies, such as the SELinux targeted policy; refer to the Tresys Technology SELinux Reference Policy page for further information. This package also provides the /usr/share/selinux/devel/policygentool development utility, as well as example policy files.
  • selinux-policy-targeted - provides the SELinux targeted policy.
  • libselinux - provides an API for SELinux applications.
  • libselinux-utils - provides the avcstat, getenforce, getsebool, matchpathcon, selinuxconlist, selinuxdefcon, selinuxenabled, setenforce, togglesebool tools.
  • libselinux-python - provides Python bindings for developing SELinux applications.
The following is a brief description of the main optional packages which have to be installed via the yum install <package-name> command:
  • selinux-policy-mls - provides the MLS SELinux policy.
  • setroubleshoot-server - translates denial messages, produced when access is denied by SELinux, into detailed descriptions that are viewed with sealert (which is provided by this package).
  • setools-console - this package provides the Tresys Technology SETools distribution, a number of tools and libraries for analyzing and querying policy, audit log monitoring and reporting, and file context management[8]. The setools package is a meta-package for SETools. The setools-gui package provides the apol, seaudit, and sediffx tools. The setools-console package provides the seaudit-report, sechecker, sediff, seinfo, sesearch, findcon, replcon, and indexcon command line tools. Refer to the Tresys Technology SETools page for information about these tools.
  • mcstrans - translates levels, such as s0-s0:c0.c1023, to an easier to read form, such as SystemLow-SystemHigh. This package is not installed by default.
  • policycoreutils-python - provides utilities such as semanage, audit2allow, audit2why and chcat, for operating and managing SELinux.
  • policycoreutils-gui - provides system-config-selinux, a graphical tool for managing SELinux.

5.2. Which Log File is Used

In Red Hat Enterprise Linux 6, the dbus and audit packages are installed by default, unless they are removed from the default package selection. The setroubleshoot-server must be installed via Yum (yum install setroubleshoot).
SELinux denial messages, such as the following, are written to /var/log/audit/audit.log by default:
type=AVC msg=audit(1223024155.684:49): avc:  denied  { getattr } for  pid=2000 comm="httpd" path="/var/www/html/file1" dev=dm-0 ino=399185 scontext=unconfined_u:system_r:httpd_t:s0 tcontext=system_u:object_r:samba_share_t:s0 tclass=file
May  7 18:55:56 localhost setroubleshoot: SELinux is preventing httpd (httpd_t) "getattr" to /var/www/html/file1 (samba_share_t). For complete SELinux messages. run sealert -l de7e30d6-5488-466d-a606-92c9f40d316d
In Red Hat Enterprise Linux 6, setroubleshootd no longer constantly runs as a service, however it is still used to analyze the AVC messages. Two new programs act as a method to start setroubleshoot when needed: sedispatch and seapplet. sedispatch runs as part of the audit subsystem, and via dbus, sends a message when an AVC denial occurs, which will go straight to setroubleshootd if it is already running, or it will start setroubleshootd if it is not running. seapplet is a tool which runs in the system's toolbar, waiting for dbus messages in setroubleshootd, and will launch the notification bubble, allowing the user to review the denial.
Starting Daemons Automatically
To configure the auditd and rsyslogd daemons to automatically start at boot, run the following commands as the Linux root user:
~]# chkconfig --levels 2345 auditd on~]# chkconfig --levels 2345 rsyslog on
Use the service service-name status command to check if these services are running, for example:
~]# service auditd statusauditd (pid  1318) is running...
If the above services are not running (service-name is stopped), use the service service-name start command as the Linux root user to start them. For example:
~]# service auditd startStarting auditd:  [  OK  ]

5.3. Main Configuration File

The /etc/selinux/config file is the main SELinux configuration file. It controls the SELinux mode and the SELinux policy to use:
# This file controls the state of SELinux on the system.# 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.SELINUX=enforcing# SELINUXTYPE= can take one of these two values:#   targeted - Targeted processes are protected,#   mls - Multi Level Security protection.SELINUXTYPE=targeted
SELINUX=enforcing
The SELINUX option sets the mode SELinux runs in. SELinux has three modes: enforcing, permissive, and disabled. When using enforcing mode, SELinux policy is enforced, and SELinux denies access based on SELinux policy rules. Denial messages are logged. When using permissive mode, SELinux policy is not enforced. SELinux does not deny access, but denials are logged for actions that would have been denied if running SELinux in enforcing mode. When using disabled mode, SELinux is disabled (the SELinux module is not registered with the Linux kernel), and only DAC rules are used.
SELINUXTYPE=targeted
The SELINUXTYPE option sets the SELinux policy to use. Targeted policy is the default policy. Only change this option if you want to use the MLS policy. For information on how to enable the MLS policy, refer to Section 5.12.2, "Enabling MLS in SELinux".

Important

When systems run with SELinux in permissive or disabled mode, users have permission to label files incorrectly. Also, files created while SELinux is disabled are not labeled. This causes problems when changing to enforcing mode. To prevent incorrectly labeled and unlabeled files from causing problems, file systems are automatically relabeled when changing from disabled mode to permissive or enforcing mode.

5.4. Enabling and Disabling SELinux

Use the getenforce or sestatus commands to check the status of SELinux. The getenforce command returns Enforcing, Permissive, or Disabled. The getenforce command returns Enforcing when SELinux is enabled (SELinux policy rules are enforced):
~]$ getenforceEnforcing
The getenforce command returns Permissive when SELinux is enabled, but SELinux policy rules are not enforced, and only DAC rules are used. The getenforce command returns Disabled if SELinux is disabled.
The sestatus command returns the SELinux status and the SELinux policy being used:
~]$ sestatusSELinux status: enabledSELinuxfs mount: /selinuxCurrent mode:   enforcingMode from config file:  enforcingPolicy version: 24Policy from config file: targeted
SELinux status: enabled is returned when SELinux is enabled. Current mode: enforcing is returned when SELinux is running in enforcing mode. Policy from config file: targeted is returned when the SELinux targeted policy is used.

5.4.1. Enabling SELinux

Important

If the system was initially installed without SELinux, particularly the selinux-policy package, which was added to the system later, one additional step is necessary to enable SELinux. To make sure SELinux is initialized during system startup, the dracut utility has to be run to put SELinux awareness into the initramfs file system. Failing to do so causes SELinux not to start during system startup.
On systems with SELinux disabled, the SELINUX=disabled option is configured in /etc/selinux/config:
# This file controls the state of SELinux on the system.# 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.SELINUX=disabled# SELINUXTYPE= can take one of these two values:#   targeted - Targeted processes are protected,#   mls - Multi Level Security protection.SELINUXTYPE=targeted
Also, the getenforce command returns Disabled:
~]$ getenforceDisabled
To enable SELinux:
  1. Use the rpm -qa | grep selinux, rpm -q policycoreutils, and rpm -qa | grep setroubleshoot commands to confirm that the SELinux packages are installed. This guide assumes the following packages are installed: selinux-policy-targeted, selinux-policy, libselinux, libselinux-python, libselinux-utils, policycoreutils, policycoreutils-python, setroubleshoot, setroubleshoot-server, setroubleshoot-plugins. If these packages are not installed, as the Linux root user, install them via the yum install package-name command. The following packages are optional: policycoreutils-gui, setroubleshoot, and mcstrans.
  2. Before SELinux is enabled, each file on the file system must be labeled with an SELinux context. Before this happens, confined domains may be denied access, preventing your system from booting correctly. To prevent this, configure SELINUX=permissive in /etc/selinux/config:
    # This file controls the state of SELinux on the system.# 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.SELINUX=permissive# SELINUXTYPE= can take one of these two values:#   targeted - Targeted processes are protected,#   mls - Multi Level Security protection.SELINUXTYPE=targeted
  3. As the Linux root user, run the reboot command to restart the system. During the next boot, file systems are labeled. The label process labels all files with an SELinux context:
    *** Warning -- SELinux targeted policy relabel is required.*** Relabeling could take a very long time, depending on file*** system size and speed of hard drives.****
    Each * (asterisk) character on the bottom line represents 1000 files that have been labeled. In the above example, four * characters represent 4000 files have been labeled. The time it takes to label all files depends upon the number of files on the system, and the speed of the hard disk drives. On modern systems, this process can take as little as 10 minutes.
  4. In permissive mode, SELinux policy is not enforced, but denials are still logged for actions that would have been denied if running in enforcing mode. Before changing to enforcing mode, as the Linux root user, run the grep "SELinux is preventing" /var/log/messages command to confirm that SELinux did not deny actions during the last boot. If SELinux did not deny actions during the last boot, this command does not return any output. Refer to Chapter 8, Troubleshooting for troubleshooting information if SELinux denied access during boot.
  5. If there were no denial messages in /var/log/messages, configure SELINUX=enforcing in /etc/selinux/config:
    # This file controls the state of SELinux on the system.# 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.SELINUX=enforcing# SELINUXTYPE= can take one of these two values:#   targeted - Targeted processes are protected,#   mls - Multi Level Security protection.SELINUXTYPE=targeted
  6. Reboot your system. After reboot, confirm that the getenforce command returns Enforcing:
    ~]$ getenforceEnforcing
  7. As the Linux root user, run the semanage login -l command to view the mapping between SELinux and Linux users. The output should be as follows:
    Login Name SELinux User  MLS/MCS Range__default__   unconfined_u  s0-s0:c0.c1023root  unconfined_u  s0-s0:c0.c1023system_u  system_u  s0-s0:c0.c1023
If this is not the case, run the following commands as the Linux root user to fix the user mappings. It is safe to ignore the SELinux-user username is already defined warnings if they occur, where username can be unconfined_u, guest_u, or xguest_u:
  1. semanage user -a -S targeted -P user -R "unconfined_r system_r" -r s0-s0:c0.c1023 unconfined_u
  2. semanage login -m -S targeted -s "unconfined_u" -r s0-s0:c0.c1023 __default__
  3. semanage login -m -S targeted -s "unconfined_u" -r s0-s0:c0.c1023 root
  4. semanage user -a -S targeted -P user -R guest_r guest_u
  5. semanage user -a -S targeted -P user -R xguest_r xguest_u

Important

When systems run with SELinux in permissive or disabled mode, users have permission to label files incorrectly. Also, files created while SELinux is disabled are not labeled. This causes problems when changing to enforcing mode. To prevent incorrectly labeled and unlabeled files from causing problems, file systems are automatically relabeled when changing from disabled mode to permissive or enforcing mode.

5.4.2. Disabling SELinux

To disable SELinux, configure SELINUX=disabled in /etc/selinux/config:
# This file controls the state of SELinux on the system.# 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.SELINUX=disabled# SELINUXTYPE= can take one of these two values:#   targeted - Targeted processes are protected,#   mls - Multi Level Security protection.SELINUXTYPE=targeted
Reboot your system. After reboot, confirm that the getenforce command returns Disabled:
~]$ getenforceDisabled

5.5. SELinux Modes

SELinux has three modes:
  • Enforcing: SELinux policy is enforced. SELinux denies access based on SELinux policy rules.
  • Permissive: SELinux policy is not enforced. SELinux does not deny access, but denials are logged for actions that would have been denied if running in enforcing mode.
  • Disabled: SELinux is disabled. Only DAC rules are used.
Use the setenforce command to change between enforcing and permissive mode. Changes made with setenforce do not persist across reboots. To change to enforcing mode, as the Linux root user, run the setenforce 1 command. To change to permissive mode, run the setenforce 0 command. Use the getenforce command to view the current SELinux mode.
Persistent mode changes are covered in Section 5.4, "Enabling and Disabling SELinux".

5.6. Booleans

Booleans allow parts of SELinux policy to be changed at runtime, without any knowledge of SELinux policy writing. This allows changes, such as allowing services access to NFS file systems, without reloading or recompiling SELinux policy.

5.6.1. Listing Booleans

For a list of Booleans, an explanation of what each one is, and whether they are on or off, run the semanage boolean -l command as the Linux root user. The following example does not list all Booleans:
~]# semanage boolean -lSELinux boolean  Descriptionftp_home_dir   -> off   Allow ftp to read and write files in the user home directoriesxen_use_nfs -> off   Allow xen to manage nfs filesxguest_connect_network -> on Allow xguest to configure Network Manager
The SELinux boolean column lists Boolean names. The Description column lists whether the Booleans are on or off, and what they do.
In the following example, the ftp_home_dir Boolean is off, preventing the FTP daemon (vsftpd) from reading and writing to files in user home directories:
ftp_home_dir   -> off   Allow ftp to read and write files in the user home directories
The getsebool -a command lists Booleans, whether they are on or off, but does not give a description of each one. The following example does not list all Booleans:
~]$ getsebool -aallow_console_login --> offallow_cvs_read_shadow --> offallow_daemons_dump_core --> on
Run the getsebool boolean-name command to only list the status of the boolean-name Boolean:
~]$ getsebool allow_console_loginallow_console_login --> off
Use a space-separated list to list multiple Booleans:
~]$ getsebool allow_console_login allow_cvs_read_shadow allow_daemons_dump_coreallow_console_login --> offallow_cvs_read_shadow --> offallow_daemons_dump_core --> on

5.6.2. Configuring Booleans

The setsebool boolean-name x command turns Booleans on or off, where boolean-name is a Boolean name, and x is either on to turn the Boolean on, or off to turn it off.
The following example demonstrates configuring the httpd_can_network_connect_db Boolean:
  1. By default, the httpd_can_network_connect_db Boolean is off, preventing Apache HTTP Server scripts and modules from connecting to database servers:
    ~]$ getsebool httpd_can_network_connect_dbhttpd_can_network_connect_db --> off
  2. To temporarily enable Apache HTTP Server scripts and modules to connect to database servers, run the setsebool httpd_can_network_connect_db on command as the Linux root user.
  3. Use the getsebool httpd_can_network_connect_db command to verify the Boolean is turned on:
    ~]$ getsebool httpd_can_network_connect_dbhttpd_can_network_connect_db --> on
    This allows Apache HTTP Server scripts and modules to connect to database servers.
  4. This change is not persistent across reboots. To make changes persistent across reboots, run the setsebool -P boolean-name on command as the Linux root user:
    ~]# setsebool -P httpd_can_network_connect_db on
  5. To temporarily revert to the default behavior, as the Linux root user, run the setsebool httpd_can_network_connect_db off command. For changes that persist across reboots, run the setsebool -P httpd_can_network_connect_db off command.

5.6.3. Booleans for NFS and CIFS

By default, NFS mounts on the client side are labeled with a default context defined by policy for NFS file systems. In common policies, this default context uses the nfs_t type. Also, by default, Samba shares mounted on the client side are labeled with a default context defined by policy. In common policies, this default context uses the cifs_t type.
Depending on policy configuration, services may not be able to read files labeled with the nfs_t or cifs_t types. This may prevent file systems labeled with these types from being mounted and then read or exported by other services. Booleans can be turned on or off to control which services are allowed to access the nfs_t and cifs_t types.
The setsebool and semanage commands must be run as the Linux root user. The setsebool -P command makes persistent changes. Do not use the -P option if you do not want changes to persist across reboots.
Apache HTTP Server
To allow access to NFS file systems (files labeled with the nfs_t type):
~]# setsebool -P httpd_use_nfs on
To allow access to Samba file systems (files labeled with the cifs_t type):
~]# setsebool -P httpd_use_cifs on
Samba
To export NFS file systems:
~]# setsebool -P samba_share_nfs on
FTP (vsftpd)
To allow access to NFS file systems:
~]# setsebool -P allow_ftpd_use_nfs on
To allow access to Samba file systems:
~]# setsebool -P allow_ftpd_use_cifs on
Other Services
For a list of NFS related Booleans for other services:
~]# semanage boolean -l | grep nfs
For a list of Samba related Booleans for other services:
~]# semanage boolean -l | grep cifs

Note

These Booleans exist in SELinux policy as shipped with Red Hat Enterprise Linux 6. They may not exist in policy shipped with other versions of Red Hat Enterprise Linux or other operating systems.

5.7. SELinux Contexts - Labeling Files

On systems running SELinux, all processes and files are labeled in a way that represents security-relevant information. This information is called the SELinux context. For files, this is viewed using the ls -Z command:
~]$ ls -Z file1-rw-rw-r--  user1 group1 unconfined_u:object_r:user_home_t:s0 file1
In this example, SELinux provides a user (unconfined_u), a role (object_r), a type (user_home_t), and a level (s0). This information is used to make access control decisions. On DAC systems, access is controlled based on Linux user and group IDs. SELinux policy rules are checked after DAC rules. SELinux policy rules are not used if DAC rules deny access first.
There are multiple commands for managing the SELinux context for files, such as chcon, semanage fcontext, and restorecon.

5.7.1. Temporary Changes: chcon

The chcon command changes the SELinux context for files. However, changes made with the chcon command do not survive a file system relabel, or the execution of the restorecon command. SELinux policy controls whether users are able to modify the SELinux context for any given file. When using chcon, users provide all or part of the SELinux context to change. An incorrect file type is a common cause of SELinux denying access.
Quick Reference
  • Run the chcon -t type file-name command to change the file type, where type is a type, such as httpd_sys_content_t, and file-name is a file or directory name.
  • Run the chcon -R -t type directory-name command to change the type of the directory and its contents, where type is a type, such as httpd_sys_content_t, and directory-name is a directory name.
Changing a File's or Directory's Type
The following example demonstrates changing the type, and no other attributes of the SELinux context:
  1. Run the cd command without arguments to change into your home directory.
  2. Run the touch file1 command to create a new file. Use the ls -Z file1 command to view the SELinux context for file1:
    ~]$ ls -Z file1-rw-rw-r--  user1 group1 unconfined_u:object_r:user_home_t:s0 file1
    In this example, the SELinux context for file1 includes the SELinux unconfined_u user, object_r role, user_home_t type, and the s0 level. For a description of each part of the SELinux context, refer to Chapter 3, SELinux Contexts.
  3. Run the chcon -t samba_share_t file1 command to change the type to samba_share_t. The -t option only changes the type. View the change with ls -Z file1:
    ~]$ ls -Z file1 -rw-rw-r--  user1 group1 unconfined_u:object_r:samba_share_t:s0 file1
  4. Use the restorecon -v file1 command to restore the SELinux context for the file1 file. Use the -v option to view what changes:
    ~]$ restorecon -v file1restorecon reset file1 context unconfined_u:object_r:samba_share_t:s0->system_u:object_r:user_home_t:s0
    In this example, the previous type, samba_share_t, is restored to the correct, user_home_t type. When using targeted policy (the default SELinux policy in Red Hat Enterprise Linux 6), the restorecon command reads the files in the /etc/selinux/targeted/contexts/files/ directory, to see which SELinux context files should have.
The example in this section works the same for directories, for example, if file1 was a directory.
Changing a Directory and its Contents Types
The following example demonstrates creating a new directory, and changing the directory's file type (along with its contents) to a type used by the Apache HTTP Server. The configuration in this example is used if you want Apache HTTP Server to use a different document root (instead of /var/www/html/):
  1. As the Linux root user, run the mkdir /web command to create a new directory, and then the touch /web/file{1,2,3} command to create 3 empty files (file1, file2, and file3). The /web/ directory and files in it are labeled with the default_t type:
    ~]# ls -dZ /webdrwxr-xr-x  root root unconfined_u:object_r:default_t:s0 /web~]# ls -lZ /web-rw-r--r--  root root unconfined_u:object_r:default_t:s0 file1-rw-r--r--  root root unconfined_u:object_r:default_t:s0 file2-rw-r--r--  root root unconfined_u:object_r:default_t:s0 file3
  2. As the Linux root user, run the chcon -R -t httpd_sys_content_t /web/ command to change the type of the /web/ directory (and its contents) to httpd_sys_content_t:
    ~]# chcon -R -t httpd_sys_content_t /web/~]# ls -dZ /web/drwxr-xr-x  root root unconfined_u:object_r:httpd_sys_content_t:s0 /web/~]# ls -lZ /web/-rw-r--r--  root root unconfined_u:object_r:httpd_sys_content_t:s0 file1-rw-r--r--  root root unconfined_u:object_r:httpd_sys_content_t:s0 file2-rw-r--r--  root root unconfined_u:object_r:httpd_sys_content_t:s0 file3
  3. As the Linux root user, run the restorecon -R -v /web/ command to restore the default SELinux contexts:
    ~]# restorecon -R -v /web/restorecon reset /web context unconfined_u:object_r:httpd_sys_content_t:s0->system_u:object_r:default_t:s0restorecon reset /web/file2 context unconfined_u:object_r:httpd_sys_content_t:s0->system_u:object_r:default_t:s0restorecon reset /web/file3 context unconfined_u:object_r:httpd_sys_content_t:s0->system_u:object_r:default_t:s0restorecon reset /web/file1 context unconfined_u:object_r:httpd_sys_content_t:s0->system_u:object_r:default_t:s0
Refer to the chcon(1) manual page for further information about chcon.

Note

Type Enforcement is the main permission control used in SELinux targeted policy. For the most part, SELinux users and roles can be ignored.

5.7.2. Persistent Changes: semanage fcontext

The semanage fcontext command changes the SELinux context for files. When using targeted policy, changes made with this command are added to the /etc/selinux/targeted/contexts/files/file_contexts file if the changes are to files that exists in file_contexts, or are added to file_contexts.local for new files and directories, such as creating a /web/ directory. The setfiles command, which is used when a file system is relabeled, and the restorecon command, which restores the default SELinux contexts, read these files. This means that changes made by semanage fcontext are persistent, even if the file system is relabeled. SELinux policy controls whether users are able to modify the SELinux context for any given file.
Quick Reference
To make SELinux context changes that survive a file system relabel:
  1. Run the semanage fcontext -a options file-name|directory-name command, remembering to use the full path to the file or directory.
  2. Run the restorecon -v file-name|directory-name command to apply the context changes.
Changing a File's Type
The following example demonstrates changing a file's type, and no other attributes of the SELinux context:
  1. As the Linux root user, run the touch /etc/file1 command to create a new file. By default, newly-created files in the /etc/ directory are labeled with the etc_t type:
    ~]# ls -Z /etc/file1-rw-r--r--  root root unconfined_u:object_r:etc_t:s0   /etc/file1
  2. As the Linux root user, run the semanage fcontext -a -t samba_share_t /etc/file1 command to change the file1 type to samba_share_t. The -a option adds a new record, and the -t option defines a type (samba_share_t). Note: running this command does not directly change the type-file1 is still labeled with the etc_t type:
    ~]# semanage fcontext -a -t samba_share_t /etc/file1~]# ls -Z /etc/file1 -rw-r--r--  root root unconfined_u:object_r:etc_t:s0   /etc/file1
    The semanage fcontext -a -t samba_share_t /etc/file1 command adds the following entry to /etc/selinux/targeted/contexts/files/file_contexts.local:
    /etc/file1 unconfined_u:object_r:samba_share_t:s0
  3. As the Linux root user, run the restorecon -v /etc/file1 command to change the type. Because the semanage command added an entry to file.contexts.local for /etc/file1, the restorecon command changes the type to samba_share_t:
    ~]# restorecon -v /etc/file1restorecon reset /etc/file1 context unconfined_u:object_r:etc_t:s0->system_u:object_r:samba_share_t:s0
  4. As the Linux root user, run the rm -i /etc/file1 command to remove file1.
  5. As the Linux root user, run the semanage fcontext -d /etc/file1 command to remove the context added for /etc/file1.
Changing a Directory's Type
The following example demonstrates creating a new directory and changing that directory's file type, to a type used by Apache HTTP Server:
  1. As the Linux root user, run the mkdir /web command to create a new directory. This directory is labeled with the default_t type:
    ~]# ls -dZ /webdrwxr-xr-x  root root unconfined_u:object_r:default_t:s0 /web
    The ls -d option makes ls list information about a directory, rather than its contents, and the -Z option makes ls display the SELinux context (in this example, unconfined_u:object_r:default_t:s0).
  2. As the Linux root user, run the semanage fcontext -a -t httpd_sys_content_t /web command to change the /web/ type to httpd_sys_content_t. The -a option adds a new record, and the -t option defines a type (httpd_sys_content_t). Note: running this command does not directly change the type - /web/ is still labeled with the default_t type:
    ~]# semanage fcontext -a -t httpd_sys_content_t /web~]# ls -dZ /web drwxr-xr-x  root root unconfined_u:object_r:default_t:s0   /web
    The semanage fcontext -a -t httpd_sys_content_t /web command adds the following entry to /etc/selinux/targeted/contexts/files/file_contexts.local:
    /web unconfined_u:object_r:httpd_sys_content_t:s0
  3. As the Linux root user, run the restorecon -v /web command to change the type. Since the semanage command added an entry to file.contexts.local for /web, the restorecon command changes the type to httpd_sys_content_t:
    ~]# restorecon -v /webrestorecon reset /web context unconfined_u:object_r:default_t:s0->system_u:object_r:httpd_sys_content_t:s0
    By default, newly-created files and directories inherit the SELinux type of their parent folders. When using this example, and before removing the SELinux context added for /web/, files and directories created in the /web/ directory are labeled with the httpd_sys_content_t type.
  4. As the Linux root user, run the semanage fcontext -d /web command to remove the context added for /web/.
  5. As the Linux root user, run the restorecon -v /web command to restore the default SELinux context.
Changing a Directory and its Contents Types
The following example demonstrates creating a new directory, and changing the directory's file type (along with its contents) to a type used by Apache HTTP Server. The configuration in this example is used if you want Apache HTTP Server to use a different document root (instead of /var/www/html/):
  1. As the Linux root user, run the mkdir /web command to create a new directory, and then the touch /web/file{1,2,3} command to create 3 empty files (file1, file2, and file3). The /web/ directory and files in it are labeled with the default_t type:
    ~]# ls -dZ /webdrwxr-xr-x  root root unconfined_u:object_r:default_t:s0 /web~]# ls -lZ /web -rw-r--r--  root root unconfined_u:object_r:default_t:s0 file1-rw-r--r--  root root unconfined_u:object_r:default_t:s0 file2-rw-r--r--  root root unconfined_u:object_r:default_t:s0 file3
  2. As the Linux root user, run the semanage fcontext -a -t httpd_sys_content_t "/web(/.*)?" command to change the type of the /web/ directory and the files in it, to httpd_sys_content_t. The -a option adds a new record, and the -t option defines a type (httpd_sys_content_t). The "/web(/.*)?" regular expression causes the semanage command to apply changes to the /web/ directory, as well as the files in it. Note: running this command does not directly change the type - /web/ and files in it are still labeled with the default_t type:
    ~]# ls -dZ /webdrwxr-xr-x  root root unconfined_u:object_r:default_t:s0 /web~]# ls -lZ /web -rw-r--r--  root root unconfined_u:object_r:default_t:s0 file1-rw-r--r--  root root unconfined_u:object_r:default_t:s0 file2-rw-r--r--  root root unconfined_u:object_r:default_t:s0 file3
    The semanage fcontext -a -t httpd_sys_content_t "/web(/.*)?" command adds the following entry to /etc/selinux/targeted/contexts/files/file_contexts.local:
    /web(/.*)? system_u:object_r:httpd_sys_content_t:s0
  3. As the Linux root user, run the restorecon -R -v /web command to change the type of the /web/ directory, as well as all files in it. The -R is for recursive, which means all files and directories under the /web/ directory are labeled with the httpd_sys_content_t type. Since the semanage command added an entry to file.contexts.local for /web(/.*)?, the restorecon command changes the types to httpd_sys_content_t:
    ~]# restorecon -R -v /webrestorecon reset /web context unconfined_u:object_r:default_t:s0->system_u:object_r:httpd_sys_content_t:s0restorecon reset /web/file2 context unconfined_u:object_r:default_t:s0->system_u:object_r:httpd_sys_content_t:s0restorecon reset /web/file3 context unconfined_u:object_r:default_t:s0->system_u:object_r:httpd_sys_content_t:s0restorecon reset /web/file1 context unconfined_u:object_r:default_t:s0->system_u:object_r:httpd_sys_content_t:s0
    By default, newly-created files and directories inherit the SELinux type of their parents. In this example, files and directories created in the /web/ directory will be labeled with the httpd_sys_content_t type.
  4. As the Linux root user, run the semanage fcontext -d "/web(/.*)?" command to remove the context added for "/web(/.*)?".
  5. As the Linux root user, run the restorecon -R -v /web command to restore the default SELinux contexts.
Deleting an added Context
The following example demonstrates adding and removing an SELinux context:
  1. As the Linux root user, run the semanage fcontext -a -t httpd_sys_content_t /test command. The /test/ directory does not have to exist. This command adds the following context to /etc/selinux/targeted/contexts/files/file_contexts.local:
    /test system_u:object_r:httpd_sys_content_t:s0
  2. To remove the context, as the Linux root user, run the semanage fcontext -d file-name|directory-name command, where file-name|directory-name is the first part in file_contexts.local. The following is an example of a context in file_contexts.local:
    /test system_u:object_r:httpd_sys_content_t:s0
    With the first part being /test. To prevent the /test/ directory from being labeled with the httpd_sys_content_t after running restorecon, or after a file system relabel, run the following command as the Linux root user to delete the context from file_contexts.local:
    ~]# semanage fcontext -d /test
If the context is part of a regular expression, for example, /web(/.*)?, use quotation marks around the regular expression:
~]# semanage fcontext -d "/web(/.*)?"
Refer to the semanage(8) manual page for further information about semanage.

Important

When changing the SELinux context with semanage fcontext -a, use the full path to the file or directory to avoid files being mislabeled after a file system relabel, or after the restorecon command is run.

5.8. The file_t and default_t Types

For file systems that support extended attributes, when a file that lacks an SELinux context on disk is accessed, it is treated as if it had a default context as defined by SELinux policy. In common policies, this default context uses the file_t type. This should be the only use of this type, so that files without a context on disk can be distinguished in policy, and generally kept inaccessible to confined domains. The file_t type should not exist on correctly-labeled file systems, because all files on a system running SELinux should have an SELinux context, and the file_t type is never used in file-context configuration[9].
The default_t type is used on files that do not match any other pattern in file-context configuration, so that such files can be distinguished from files that do not have a context on disk, and generally kept inaccessible to confined domains. If you create a new top-level directory, such as /mydirectory/, this directory may be labeled with the default_t type. If services need access to such a directory, update the file-contexts configuration for this location. Refer to Section 5.7.2, "Persistent Changes: semanage fcontext" for details on adding a context to the file-context configuration.

5.9. Mounting File Systems

By default, when a file system that supports extended attributes is mounted, the security context for each file is obtained from the security.selinux extended attribute of the file. Files in file systems that do not support extended attributes are assigned a single, default security context from the policy configuration, based on file system type.
Use the mount -o context command to override existing extended attributes, or to specify a different, default context for file systems that do not support extended attributes. This is useful if you do not trust a file system to supply the correct attributes, for example, removable media used in multiple systems. The mount -o context command can also be used to support labeling for file systems that do not support extended attributes, such as File Allocation Table (FAT) or NFS file systems. The context specified with the context is not written to disk: the original contexts are preserved, and are seen when mounting without a context option (if the file system had extended attributes in the first place).
For further information about file system labeling, refer to James Morris's "Filesystem Labeling in SELinux" article: http://www.linuxjournal.com/article/7426.

5.9.1. Context Mounts

To mount a file system with the specified context, overriding existing contexts if they exist, or to specify a different, default context for a file system that does not support extended attributes, as the Linux root user, use the mount -o context=SELinux_user:role:type:level command when mounting the desired file system. Context changes are not written to disk. By default, NFS mounts on the client side are labeled with a default context defined by policy for NFS file systems. In common policies, this default context uses the nfs_t type. Without additional mount options, this may prevent sharing NFS file systems via other services, such as the Apache HTTP Server. The following example mounts an NFS file system so that it can be shared via the Apache HTTP Server:
~]# mount server:/export /local/mount/point -o \ context="system_u:object_r:httpd_sys_content_t:s0"
Newly-created files and directories on this file system appear to have the SELinux context specified with -o context; however, since context changes are not written to disk for these situations, the context specified with the context option is only retained if the context option is used on the next mount, and if the same context is specified.
Type Enforcement is the main permission control used in SELinux targeted policy. For the most part, SELinux users and roles can be ignored, so, when overriding the SELinux context with -o context, use the SELinux system_u user and object_r role, and concentrate on the type. If you are not using the MLS policy or multi-category security, use the s0 level.

Note

When a file system is mounted with a context option, context changes (by users and processes) are prohibited. For example, running chcon on a file system mounted with a context option results in a Operation not supported error.

5.9.2. Changing the Default Context

As mentioned in Section 5.8, "The file_t and default_t Types", on file systems that support extended attributes, when a file that lacks an SELinux context on disk is accessed, it is treated as if it had a default context as defined by SELinux policy. In common policies, this default context uses the file_t type. If it is desirable to use a different default context, mount the file system with the defcontext option.
The following example mounts a newly-created file system (on /dev/sda2) to the newly-created /test/ directory. It assumes that there are no rules in /etc/selinux/targeted/contexts/files/ that define a context for the /test/ directory:
~]# mount /dev/sda2 /test/ -o defcontext="system_u:object_r:samba_share_t:s0"
In this example:
  • the defcontext option defines that system_u:object_r:samba_share_t:s0 is "the default security context for unlabeled files"[10].
  • when mounted, the root directory (/test/) of the file system is treated as if it is labeled with the context specified by defcontext (this label is not stored on disk). This affects the labeling for files created under /test/: new files inherit the samba_share_t type, and these labels are stored on disk.
  • files created under /test/ while the file system was mounted with a defcontext option retain their labels.

5.9.3. Mounting an NFS File System

By default, NFS mounts on the client side are labeled with a default context defined by policy for NFS file systems. In common policies, this default context uses the nfs_t type. Depending on policy configuration, services, such as Apache HTTP Server and MySQL, may not be able to read files labeled with the nfs_t type. This may prevent file systems labeled with this type from being mounted and then read or exported by other services.
If you would like to mount an NFS file system and read or export that file system with another service, use the context option when mounting to override the nfs_t type. Use the following context option to mount NFS file systems so that they can be shared via the Apache HTTP Server:
~]# mount server:/export /local/mount/point -o context="system_u:object_r:httpd_sys_content_t:s0"
Since context changes are not written to disk for these situations, the context specified with the context option is only retained if the context option is used on the next mount, and if the same context is specified.
As an alternative to mounting file systems with context options, Booleans can be turned on to allow services access to file systems labeled with the nfs_t type. Refer to Section 5.6.3, "Booleans for NFS and CIFS" for instructions on configuring Booleans to allow services access to the nfs_t type.

5.9.4. Multiple NFS Mounts

When mounting multiple mounts from the same NFS export, attempting to override the SELinux context of each mount with a different context, results in subsequent mount commands failing. In the following example, the NFS server has a single export, /export, which has two subdirectories, web/ and database/. The following commands attempt two mounts from a single NFS export, and try to override the context for each one:
~]# mount server:/export/web /local/web -o context="system_u:object_r:httpd_sys_content_t:s0"~]# mount server:/export/database /local/database -o context="system_u:object_r:mysqld_db_t:s0"
The second mount command fails, and the following is logged to /var/log/messages:
kernel: SELinux: mount invalid.  Same superblock, different security settings for (dev 0:15, type nfs)
To mount multiple mounts from a single NFS export, with each mount having a different context, use the -o nosharecache,context options. The following example mounts multiple mounts from a single NFS export, with a different context for each mount (allowing a single service access to each one):
~]# mount server:/export/web /local/web -o nosharecache,context="system_u:object_r:httpd_sys_content_t:s0"~]# mount server:/export/database /local/database -o \ nosharecache,context="system_u:object_r:mysqld_db_t:s0"
In this example, server:/export/web is mounted locally to /local/web/, with all files being labeled with the httpd_sys_content_t type, allowing Apache HTTP Server access. server:/export/database is mounted locally to /local/database, with all files being labeled with the mysqld_db_t type, allowing MySQL access. These type changes are not written to disk.

Important

The nosharecache options allows you to mount the same subdirectory of an export multiple times with different contexts (for example, mounting /export/web multiple times). Do not mount the same subdirectory from an export multiple times with different contexts, as this creates an overlapping mount, where files are accessible under two different contexts.

5.9.5. Making Context Mounts Persistent

To make context mounts persistent across remounting and reboots, add entries for the file systems in /etc/fstab or an automounter map, and use the desired context as a mount option. The following example adds an entry to /etc/fstab for an NFS context mount:
server:/export /local/mount/ nfs context="system_u:object_r:httpd_sys_content_t:s0" 0 0

5.10. Maintaining SELinux Labels

These sections describe what happens to SELinux contexts when copying, moving, and archiving files and directories. Also, it explains how to preserve contexts when copying and archiving.

5.10.1. Copying Files and Directories

When a file or directory is copied, a new file or directory is created if it does not exist. That new file or directory's context is based on default-labeling rules, not the original file or directory's context (unless options were used to preserve the original context). For example, files created in user home directories are labeled with the user_home_t type:
~]$ touch file1~]$ ls -Z file1-rw-rw-r--  user1 group1 unconfined_u:object_r:user_home_t:s0 file1
If such a file is copied to another directory, such as /etc/, the new file is created in accordance to default-labeling rules for the /etc/ directory. Copying a file (without additional options) may not preserve the original context:
~]$ ls -Z file1-rw-rw-r--  user1 group1 unconfined_u:object_r:user_home_t:s0 file1~]# cp file1 /etc/~]$ ls -Z /etc/file1 -rw-r--r--  root root unconfined_u:object_r:etc_t:s0   /etc/file1
When file1 is copied to /etc/, if /etc/file1 does not exist, /etc/file1 is created as a new file. As shown in the example above, /etc/file1 is labeled with the etc_t type, in accordance to default-labeling rules.
When a file is copied over an existing file, the existing file's context is preserved, unless the user specified cp options to preserve the context of the original file, such as --preserve=context. SELinux policy may prevent contexts from being preserved during copies.
Copying Without Preserving SELinux Contexts
When copying a file with the cp command, if no options are given, the type is inherited from the targeted, parent directory:
~]$ touch file1~]$ ls -Z file1-rw-rw-r--  user1 group1 unconfined_u:object_r:user_home_t:s0 file1~]$ ls -dZ /var/www/html/drwxr-xr-x  root root system_u:object_r:httpd_sys_content_t:s0 /var/www/html/~]# cp file1 /var/www/html/~]$ ls -Z /var/www/html/file1-rw-r--r--  root root unconfined_u:object_r:httpd_sys_content_t:s0 /var/www/html/file1
In this example, file1 is created in a user's home directory, and is labeled with the user_home_t type. The /var/www/html/ directory is labeled with the httpd_sys_content_t type, as shown with the ls -dZ /var/www/html/ command. When file1 is copied to /var/www/html/, it inherits the httpd_sys_content_t type, as shown with the ls -Z /var/www/html/file1 command.
Preserving SELinux Contexts When Copying
Use the cp --preserve=context command to preserve contexts when copying:
~]$ touch file1~]$ ls -Z file1-rw-rw-r--  user1 group1 unconfined_u:object_r:user_home_t:s0 file1~]$ ls -dZ /var/www/html/drwxr-xr-x  root root system_u:object_r:httpd_sys_content_t:s0 /var/www/html/~]# cp --preserve=context file1 /var/www/html/~]$ ls -Z /var/www/html/file1-rw-r--r--  root root unconfined_u:object_r:user_home_t:s0 /var/www/html/file1
In this example, file1 is created in a user's home directory, and is labeled with the user_home_t type. The /var/www/html/ directory is labeled with the httpd_sys_content_t type, as shown with the ls -dZ /var/www/html/ command. Using the --preserve=context option preserves SELinux contexts during copy operations. As shown with the ls -Z /var/www/html/file1 command, the file1 user_home_t type was preserved when the file was copied to /var/www/html/.
Copying and Changing the Context
Use the cp -Z command to change the destination copy's context. The following example was performed in the user's home directory:
~]$ touch file1~]$ cp -Z system_u:object_r:samba_share_t:s0 file1 file2~]$ ls -Z file1 file2-rw-rw-r--  user1 group1 unconfined_u:object_r:user_home_t:s0 file1-rw-rw-r--  user1 group1 system_u:object_r:samba_share_t:s0 file2~]$ rm file1 file2
In this example, the context is defined with the -Z option. Without the -Z option, file2 would be labeled with the unconfined_u:object_r:user_home_t context.
Copying a File Over an Existing File
When a file is copied over an existing file, the existing file's context is preserved (unless an option is used to preserve contexts). For example:
~]# touch /etc/file1~]# ls -Z /etc/file1-rw-r--r--  root root unconfined_u:object_r:etc_t:s0   /etc/file1~]# touch /tmp/file2~]# ls -Z /tmp/file2-rw-r--r--  root root unconfined_u:object_r:user_tmp_t:s0 /tmp/file2~]# cp /tmp/file2 /etc/file1~]# ls -Z /etc/file1-rw-r--r--  root root unconfined_u:object_r:etc_t:s0   /etc/file1
In this example, two files are created: /etc/file1, labeled with the etc_t type, and /tmp/file2, labeled with the user_tmp_t type. The cp /tmp/file2 /etc/file1 command overwrites file1 with file2. After copying, the ls -Z /etc/file1 command shows file1 labeled with the etc_t type, not the user_tmp_t type from /tmp/file2 that replaced /etc/file1.

Important

Copy files and directories, rather than moving them. This helps ensure they are labeled with the correct SELinux contexts. Incorrect SELinux contexts can prevent processes from accessing such files and directories.

5.10.2. Moving Files and Directories

Files and directories keep their current SELinux context when they are moved. In many cases, this is incorrect for the location they are being moved to. The following example demonstrates moving a file from a user's home directory to /var/www/html/, which is used by the Apache HTTP Server. Since the file is moved, it does not inherit the correct SELinux context:
  1. Run the cd command without any arguments to change into your home directory. Once in your home directory, run the touch file1 command to create a file. This file is labeled with the user_home_t type:
    ~]$ ls -Z file1-rw-rw-r--  user1 group1 unconfined_u:object_r:user_home_t:s0 file1
  2. Run the ls -dZ /var/www/html/ command to view the SELinux context of the /var/www/html/ directory:
    ~]$ ls -dZ /var/www/html/drwxr-xr-x  root root system_u:object_r:httpd_sys_content_t:s0 /var/www/html/
    By default, the /var/www/html/ directory is labeled with the httpd_sys_content_t type. Files and directories created under the /var/www/html/ directory inherit this type, and as such, they are labeled with this type.
  3. As the Linux root user, run the mv file1 /var/www/html/ command to move file1 to the /var/www/html/ directory. Since this file is moved, it keeps its current user_home_t type:
    ~]# mv file1 /var/www/html/~]# ls -Z /var/www/html/file1-rw-rw-r--  user1 group1 unconfined_u:object_r:user_home_t:s0 /var/www/html/file1
By default, the Apache HTTP Server can not read files that are labeled with the user_home_t type. If all files comprising a web page are labeled with the user_home_t type, or another type that the Apache HTTP Server can not read, permission is denied when attempting to access them via Firefox or text-based Web browsers.

Important

Moving files and directories with the mv command may result in the wrong SELinux context, preventing processes, such as the Apache HTTP Server and Samba, from accessing such files and directories.

5.10.3. Checking the Default SELinux Context

Use the matchpathcon command to check if files and directories have the correct SELinux context. From the matchpathcon(8) manual page: "matchpathcon queries the system policy and outputs the default security context associated with the file path."[11]. The following example demonstrates using the matchpathcon command to verify that files in /var/www/html/ directory are labeled correctly:
  1. As the Linux root user, run the touch /var/www/html/file{1,2,3} command to create three files (file1, file2, and file3). These files inherit the httpd_sys_content_t type from the /var/www/html/ directory:
    ~]# touch /var/www/html/file{1,2,3}~]# ls -Z /var/www/html/-rw-r--r--  root root unconfined_u:object_r:httpd_sys_content_t:s0 file1-rw-r--r--  root root unconfined_u:object_r:httpd_sys_content_t:s0 file2-rw-r--r--  root root unconfined_u:object_r:httpd_sys_content_t:s0 file3
  2. As the Linux root user, run the chcon -t samba_share_t /var/www/html/file1 command to change the file1 type to samba_share_t. Note: the Apache HTTP Server can not read files or directories labeled with the samba_share_t type.
  3. The matchpathcon -V option compares the current SELinux context to the correct, default context in SELinux policy. Run the matchpathcon -V /var/www/html/* command to check all files in the /var/www/html/ directory:
    ~]$ matchpathcon -V /var/www/html/*/var/www/html/file1 has context unconfined_u:object_r:samba_share_t:s0, should be system_u:object_r:httpd_sys_content_t:s0/var/www/html/file2 verified./var/www/html/file3 verified.
The following output from the matchpathcon command explains that file1 is labeled with the samba_share_t type, but should be labeled with the httpd_sys_content_t type:
/var/www/html/file1 has context unconfined_u:object_r:samba_share_t:s0, should be system_u:object_r:httpd_sys_content_t:s0
To resolve the label problem and allow the Apache HTTP Server access to file1, as the Linux root user, run the restorecon -v /var/www/html/file1 command:
~]# restorecon -v /var/www/html/file1restorecon reset /var/www/html/file1 context unconfined_u:object_r:samba_share_t:s0->system_u:object_r:httpd_sys_content_t:s0

5.10.4. Archiving Files with tar

tar does not retain extended attributes by default. Since SELinux contexts are stored in extended attributes, contexts can be lost when archiving files. Use tar --selinux to create archives that retain contexts. If a tar archive contains files without extended attributes, or if you want the extended attributes to match the system defaults, run the archive through restorecon:
~]$ tar -xvf archive.tar | restorecon -f -
Note: depending on the directory, you may need to be the Linux root user to run the restorecon command.
The following example demonstrates creating a tar archive that retains SELinux contexts:
  1. As the Linux root user, run the touch /var/www/html/file{1,2,3} command to create three files (file1, file2, and file3). These files inherit the httpd_sys_content_t type from the /var/www/html/ directory:
    ~]# touch /var/www/html/file{1,2,3}~]# ls -Z /var/www/html/-rw-r--r--  root root unconfined_u:object_r:httpd_sys_content_t:s0 file1-rw-r--r--  root root unconfined_u:object_r:httpd_sys_content_t:s0 file2-rw-r--r--  root root unconfined_u:object_r:httpd_sys_content_t:s0 file3
  2. Run the cd /var/www/html/ command to change into the /var/www/html/ directory. Once in this directory, as the Linux root user, run the tar --selinux -cf test.tar file{1,2,3} command to create a tar archive named test.tar.
  3. As the Linux root user, run the mkdir /test command to create a new directory, and then, run the chmod 777 /test/ command to allow all users full-access to the /test/ directory.
  4. Run the cp /var/www/html/test.tar /test/ command to copy the test.tar file in to the /test/ directory.
  5. Run the cd /test/ command to change into the /test/ directory. Once in this directory, run the tar -xvf test.tar command to extract the tar archive.
  6. Run the ls -lZ /test/ command to view the SELinux contexts. The httpd_sys_content_t type has been retained, rather than being changed to default_t, which would have happened had the --selinux not been used:
    ~]$ ls -lZ /test/-rw-r--r--  user1 group1 unconfined_u:object_r:httpd_sys_content_t:s0 file1-rw-r--r--  user1 group1 unconfined_u:object_r:httpd_sys_content_t:s0 file2-rw-r--r--  user1 group1 unconfined_u:object_r:httpd_sys_content_t:s0 file3-rw-r--r--  user1 group1 unconfined_u:object_r:default_t:s0 test.tar
  7. If the /test/ directory is no longer required, as the Linux root user, run the rm -ri /test/ command to remove it, as well as all files in it.
Refer to the tar(1) manual page for further information about tar, such as the --xattrs option that retains all extended attributes.

5.10.5. Archiving Files with star

star does not retain extended attributes by default. Since SELinux contexts are stored in extended attributes, contexts can be lost when archiving files. Use star -xattr -H=exustar to create archives that retain contexts. The star package is not installed by default. To install star, run the yum install star command as the Linux root user.
The following example demonstrates creating a Star archive that retains SELinux contexts:
  1. As the Linux root user, run the touch /var/www/html/file{1,2,3} command to create three files (file1, file2, and file3). These files inherit the httpd_sys_content_t type from the /var/www/html/ directory:
    ~]# touch /var/www/html/file{1,2,3}~]# ls -Z /var/www/html/-rw-r--r--  root root unconfined_u:object_r:httpd_sys_content_t:s0 file1-rw-r--r--  root root unconfined_u:object_r:httpd_sys_content_t:s0 file2-rw-r--r--  root root unconfined_u:object_r:httpd_sys_content_t:s0 file3
  2. Run the cd /var/www/html/ command to change into the /var/www/html/ directory. Once in this directory, as the Linux root user, run the star -xattr -H=exustar -c -f=test.star file{1,2,3} command to create a Star archive named test.star:
    ~]# star -xattr -H=exustar -c -f=test.star file{1,2,3}star: 1 blocks + 0 bytes (total of 10240 bytes = 10.00k).
  3. As the Linux root user, run the mkdir /test command to create a new directory, and then, run the chmod 777 /test/ command to allow all users full-access to the /test/ directory.
  4. Run the cp /var/www/html/test.star /test/ command to copy the test.star file in to the /test/ directory.
  5. Run the cd /test/ command to change into the /test/ directory. Once in this directory, run the star -x -f=test.star command to extract the Star archive:
    ~]$ star -x -f=test.star star: 1 blocks + 0 bytes (total of 10240 bytes = 10.00k).
  6. Run the ls -lZ /test/ command to view the SELinux contexts. The httpd_sys_content_t type has been retained, rather than being changed to default_t, which would have happened had the -xattr -H=exustar option not been used:
    ~]$ ls -lZ /test/-rw-r--r--  user1 group1 unconfined_u:object_r:httpd_sys_content_t:s0 file1-rw-r--r--  user1 group1 unconfined_u:object_r:httpd_sys_content_t:s0 file2-rw-r--r--  user1 group1 unconfined_u:object_r:httpd_sys_content_t:s0 file3-rw-r--r--  user1 group1 unconfined_u:object_r:default_t:s0 test.star
  7. If the /test/ directory is no longer required, as the Linux root user, run the rm -ri /test/ command to remove it, as well as all files in it.
  8. If star is no longer required, as the Linux root user, run the yum remove star command to remove the package.
Refer to the star(1) manual page for further information about star.

5.11. Information Gathering Tools

These tools are command line tools, providing formatted output. They are harder to use as part of command line piping, but they provide gathered and well formatted information quickly.
avcstat
This command provides a short output of the access vector cache statistics since boot. You can watch the statistics in real time by specifying a time interval in seconds. This provides updated statistics since the initial output. The statistics file used is /selinux/avc/cache_stats, and you can specify a different cache file with the -f /path/to/file option.
~]# avcstat    lookups   hits misses allocs   reclaims  frees  47517410   47504630  12780  12780  12176  12275
seinfo
This utility is useful in describing the break-down of a policy, such as the number of classes, types, Booleans, allow rules, and others. seinfo is a command line utility that uses a policy.conf file (a single text file containing policy source for versions 12 through 21), a binary policy file, a modular list of policy packages, or a policy list file as input. You must have the setools-console package installed to use the seinfo utility.
The output of seinfo will vary between binary and source files. For example, the policy source file uses the { } brackets to group multiple rule elements onto a single line. A similar effect happens with attributes, where a single attribute expands into one or many types. Because these are expanded and no longer relevant in the binary policy file, they have a return value of zero in the search results. However, the number of rules greatly increases as each formerly one line rule using brackets is now a number of individual lines.
Some items are not present in the binary policy. For example, neverallow rules are only checked during policy compile, not during runtime, and initial SIDs are not part of the binary policy since they are required prior to the policy being loaded by the kernel during boot.
~]# seinfoStatistics for policy file: /etc/selinux/targeted/policy/policy.24Policy Version  & Type: v.24 (binary, mls)   Classes: 77 Permissions:   229   Sensitivities:   1 Categories:   1024   Types: 3001 Attributes: 244   Users:   9 Roles:  13   Booleans:  158 Cond. Expr.:   193   Allow:  262796 Neverallow:  0   Auditallow: 44 Dontaudit:  156710   Type_trans:  10760 Type_change: 38   Type_member: 44 Role allow: 20   Role_trans: 237 Range_trans:  2546   Constraints: 62 Validatetrans:   0   Initial SIDs:   27 Fs_use: 22   Genfscon:   82 Portcon:   373   Netifcon: 0 Nodecon: 0   Permissives: 22 Polcap:  2
The seinfo command can also list the number of types with the domain attribute, giving an estimate of the number of different confined processes:
~]# seinfo -adomain -x | wc -l550
Not all domain types are confined. To look at the number of unconfined domains, use the unconfined_domain attribute:
~]# seinfo -aunconfined_domain_type -x | wc -l52
Permissive domains can be counted with the --permissive option.
~]# seinfo --permissive -x | wc -l31
Remove the | wc -l option in the above commands to see the full lists.
sesearch
You can use the sesearch command to search for a particular type in the policy. You can search either policy source files or the binary file. For example:
~]$ sesearch --role_allow -t httpd_sys_content_t /etc/selinux/targeted/policy/policy.24Found 20 role allow rules:   allow system_r sysadm_r;   allow sysadm_r system_r;   allow sysadm_r staff_r;   allow sysadm_r user_r;   allow system_r git_shell_r;   allow system_r guest_r;   allow logadm_r system_r;   allow system_r logadm_r;   allow system_r nx_server_r;   allow system_r staff_r;   allow staff_r logadm_r;   allow staff_r sysadm_r;   allow staff_r unconfined_r;   allow staff_r webadm_r;   allow unconfined_r system_r;   allow system_r unconfined_r;   allow system_r user_r;   allow webadm_r system_r;   allow system_r webadm_r;   allow system_r xguest_r;
The sesearch command can provide the number of allow rules:
~]# sesearch --allow | wc -l262798
And the number of dontaudit rules:
~]# sesearch --dontaudit | wc -l156712

5.12. Multi-Level Security (MLS)

The Multi-Level Security technology refers to a security scheme that enforces the Bell-La Padula Mandatory Access Model. Under MLS, users and processes are called subjects, and files, devices, and other passive components of the system are called objects. Both subjects and objects are labeled with a security level, which entails a subject's clearance or an object's classification. Each security level is composed of a sensitivity and a category, for example, an internal release schedule is filed under the internal documents category with a confidential sensitivity.
Figure 5.1, "Levels of clearance" shows levels of clearance as originally designed by the US defense community. Relating to our internal schedule example above, only users that have gained the confidential clearance are allowed to view documents in the confidential category. However, users who only have the confidential clearance are not allowed to view documents that require higher levels or clearance; they are allowed read access only to documents with lower levels of clearance, and write access to documents with higher levels of clearance.
Levels of clearance
Levels of Clearance

Figure 5.1. Levels of clearance


Figure 5.2, "Allowed data flows using MLS" shows all allowed data flows between a subject running under the "Secret" security level and various objects with different security levels. In simple terms, the Bell-LaPadula model enforces two properties: no read up and no write down.
Allowed data flows using MLS
Allowed data flows using MLS

Figure 5.2. Allowed data flows using MLS


5.12.1. MLS and System Privileges

MLS access rules are always combined with conventional access permissions (file permissions). For example, if a user with a security level of "Secret" uses Discretionary Access Control (DAC) to block access to a file by other users, this also blocks access by users with a security level of "Top Secret". It is important to remember that SELinux MLS policy rules are checked after DAC rules. A higher security clearance does not automatically give permission to arbitrarily browse a file system.
Users with top-level clearances do not automatically acquire administrative rights on multi-level systems. While they may have access to all information on the computer, this is different from having administrative rights.

5.12.2. Enabling MLS in SELinux

Note

It is not recommended to use the MLS policy on a system that is running the X Window System.
Follow these steps to enable the SELinux MLS policy on your system.
  1. Install the selinux-policy-mls package:
    ~]# yum install selinux-policy-mls
  2. Before the MLS policy is enabled, each file on the file system must be relabeled with an MLS label. When the file system is relabeled, confined domains may be denied access, which may prevent your system from booting correctly. To prevent this from happening, configure SELINUX=permissive in the /etc/selinux/config file. Also, enable the MLS policy by configuring SELINUXTYPE=mls. Your configuration file should look like this:
    # This file controls the state of SELinux on the system.# 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.SELINUX=permissive# SELINUXTYPE= can take one of these two values:#   targeted - Targeted processes are protected,#   mls - Multi Level Security protection.SELINUXTYPE=mls
  3. Make sure SELinux is running in the permissive mode:
    ~]# setenforce 0~]# getenforcePermissive
  4. Create the .autorelabel file in root's home directory to ensure that files are relabeled upon next reboot:
    ~]# touch /.autorelabel
  5. Reboot your system. During the next boot, all file systems will be relabeled according to the MLS policy. The label process labels all files with an appropriate SELinux context:
    *** Warning -- SELinux mls policy relabel is required.*** Relabeling could take a very long time, depending on file*** system size and speed of hard drives.***********
    Each * (asterisk) character on the bottom line represents 1000 files that have been labeled. In the above example, eleven * characters represent 11000 files which have been labeled. The time it takes to label all files depends upon the number of files on the system, and the speed of the hard disk drives. On modern systems, this process can take as little as 10 minutes. Once the labeling process finishes, the system will automatically reboot.
  6. In permissive mode, SELinux policy is not enforced, but denials are still logged for actions that would have been denied if running in enforcing mode. Before changing to enforcing mode, as the Linux root user, run the grep "SELinux is preventing" /var/log/messages command to confirm that SELinux did not deny actions during the last boot. If SELinux did not deny actions during the last boot, this command does not return any output. Refer to Chapter 8, Troubleshooting for troubleshooting information if SELinux denied access during boot.
  7. If there were no denial messages in /var/log/messages, or you have resolved all existing denials, configure SELINUX=enforcing in the /etc/selinux/config file:
    # This file controls the state of SELinux on the system.# 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.SELINUX=enforcing# SELINUXTYPE= can take one of these two values:#   targeted - Targeted processes are protected,#   mls - Multi Level Security protection.SELINUXTYPE=mls
  8. Reboot your system and make sure SELinux is running in permissive mode:
    ~]$ getenforceEnforcing
    and the MLS policy is enabled:
    ~]# sestatus |grep mlsPolicy from config file: mls

5.12.3. Creating a User With a Specific MLS Range

Follow these steps to create a new Linux user with a specific MLS range:
  1. Add a new Linux user via the useradd command and map the new Linux user to an existing SELinux user (in this case, user_u):
    ~]# useradd -Z user_u john
  2. Assign the newly-created Linux user a password:
    ~]# passwd john
  3. Run the semanage login -l command to view the mapping between SELinux and Linux users. The output should be as follows:
    Login Name SELinux User  MLS/MCS Range__default__   user_u s0john  user_u s0root  root  s0-s15:c0.c1023system_u  system_u  s0-s15:c0.c1023
  4. Define a specific range for user john:
    ~]# semanage login --modify --seuser user_u --range s2:c100 john
  5. Run the semanage login -l command to view the mapping between SELinux and Linux users. Note that the user john now has a specific MLS range defined:
    Login Name SELinux User  MLS/MCS Range__default__   user_u s0john  user_u s2:c100root  root  s0-s15:c0.c1023system_u  system_u  s0-s15:c0.c1023
  6. To correct the label on john's home directory (if needed), run the following command:
    ~]# chcon -R -l s2:c100 /home/john

5.12.4. Setting Up Polyinstantiated Directories

The /tmp and /var/tmp directories are normally used for temporary storage by all programs, services, and users. Such setup, however, makes these directories vulnerable to race condition attacks, or an information leak based on file names. SELinux offers a solution in the form of polyinstantiated directories. This effectively means that both /tmp and /var/tmp are instantiated, making them appear private for each user. When instantiation of directories is enabled, each user's /tmp and /var/tmp directory is automatically mounted under /tmp-inst and /var/tmp/tmp-inst.
Follow these steps to enable polyinstantiation of directories:
  1. Uncomment the last three lines in the /etc/security/namespace.conf file to enable instantiation of the /tmp, /var/tmp, and users' home directories:
    ~]$ tail -n 3 /etc/security/namespace.conf/tmp /tmp-inst/ level  root,adm/var/tmp /var/tmp/tmp-inst/ level  root,adm$HOME $HOME/$USER.inst/ level
  2. Ensure that in the /etc/pam.d/login file, the pam_namespace.so module is configured for session:
    ~]$ grep namespace /etc/pam.d/loginsession required pam_namespace.so
  3. Reboot your system.


[8] Brindle, Joshua. "Re: blurb for fedora setools packages" Email to Murray McAllister. 1 November 2008. Any edits or changes in this version were done by Murray McAllister.
[9] Files in /etc/selinux/targeted/contexts/files/ define contexts for files and directories. Files in this directory are read by restorecon and setfiles to restore files and directories to their default contexts.
[10] Morris, James. "Filesystem Labeling in SELinux". Published 1 October 2004. Accessed 14 October 2008: http://www.linuxjournal.com/article/7426.
[11] The matchpathcon(8) manual page, as shipped with the libselinux-utils package in Red Hat Enterprise Linux, is written by Daniel Walsh. Any edits or changes in this version were done by Murray McAllister.
(Sebelumnya) 8 : Security-Enhanced Linux8 : Chapter 6. Confining Users ... (Berikutnya)