Este conteúdo não está disponível no idioma selecionado.
Managing Confined Services
Guide to configuring services under control of SELinux
Abstract
Chapter 1. Introduction
ls -l
command to view file permissions:
$ ls -l file1 -rwxrw-r-- 1 user1 group1 0 2010-01-29 09:17 file1
rwx
, control the access rights that the Linux user1
user (in this case, the owner) has to file1
. The next three permission bits, rw-
, control the access rights that the Linux group1
group has to file1
. The last three permission bits, r--
, control the access rights that everyone else has to file1
, which includes all users and processes.
ls -Z
command:
$ ls -Z file1 -rwxrw-r-- user1 group1 unconfined_u:object_r:user_home_t:s0 file1
unconfined_u
), a role (object_r
), a type (user_home_t
), and a level (s0
) for the file1
file. This information is used to make access control decisions. This example also displays the DAC rules, which are shown in the SELinux context via the ls -Z
command. SELinux policy rules are checked after DAC rules. SELinux policy rules are not applied if DAC rules deny access first.
Targeted Policy
Chapter 2. The Apache HTTP Server
rpm -q httpd
command to see if the httpd package is installed. If it is not installed and you want to use the Apache HTTP Server, run the following command as the root user to install it:
~]# yum install httpd
2.1. The Apache HTTP Server and SELinux
httpd
) runs confined by default. Confined processes run in their own domains, and are separated from other confined processes. If a confined process is compromised by an attacker, depending on SELinux policy configuration, an attacker's access to resources and the possible damage they can do is limited. The following example demonstrates the httpd
processes running in their own domain. This example assumes the httpd, setroubleshoot, setroubleshoot-server and policycoreutils-python packages are installed:
- Run the
getenforce
command to confirm SELinux is running in enforcing mode:~]$
getenforce
EnforcingThegetenforce
command returnsEnforcing
when SELinux is running in enforcing mode. - Run the
service httpd start
command as the root user to starthttpd
:~]#
service httpd start
Starting httpd: [ OK ] - Run the
ps -eZ | grep httpd
command to view thehttpd
processes:~]$
ps -eZ | grep httpd
unconfined_u:system_r:httpd_t:s0 2850 ? 00:00:00 httpd unconfined_u:system_r:httpd_t:s0 2852 ? 00:00:00 httpd unconfined_u:system_r:httpd_t:s0 2853 ? 00:00:00 httpd unconfined_u:system_r:httpd_t:s0 2854 ? 00:00:00 httpd unconfined_u:system_r:httpd_t:s0 2855 ? 00:00:00 httpd unconfined_u:system_r:httpd_t:s0 2856 ? 00:00:00 httpd unconfined_u:system_r:httpd_t:s0 2857 ? 00:00:00 httpd unconfined_u:system_r:httpd_t:s0 2858 ? 00:00:00 httpd unconfined_u:system_r:httpd_t:s0 2859 ? 00:00:00 httpdThe SELinux context associated with thehttpd
processes isunconfined_u:system_r:httpd_t:s0
. The second last part of the context,httpd_t
, is the type. A type defines a domain for processes and a type for files. In this case, thehttpd
processes are running in thehttpd_t
domain.
httpd_t
) interact with files, other processes, and the system in general. Files must be labeled correctly to allow httpd
access to them. For example, httpd
can read files labeled with the httpd_sys_content_t
type, but cannot write to them, even if Linux (DAC) permissions allow write access. Booleans must be enabled to allow certain behavior, such as allowing scripts network access, allowing httpd
access to NFS and CIFS volumes, and httpd
being allowed to execute Common Gateway Interface (CGI) scripts.
/etc/httpd/conf/httpd.conf
is configured so httpd
listens on a port other than TCP ports 80, 443, 488, 8008, 8009, or 8443, the semanage port
command must be used to add the new port number to SELinux policy configuration. The following example demonstrates configuring httpd
to listen on a port that is not already defined in SELinux policy configuration for httpd
, and, as a consequence, httpd
failing to start. This example also demonstrates how to then configure the SELinux system to allow httpd
to successfully listen on a non-standard port that is not already defined in the policy. This example assumes the httpd package is installed. Run each command in the example as the root user:
- Run the
service httpd status
command to confirmhttpd
is not running:~]#
service httpd status
httpd is stoppedIf the output differs, run theservice httpd stop
command to stop the process:~]#
service httpd stop
Stopping httpd: [ OK ] - Run the
semanage port -l | grep -w http_port_t
command to view the ports SELinux allowshttpd
to listen on:~]#
semanage port -l | grep -w http_port_t
http_port_t tcp 80, 443, 488, 8008, 8009, 8443 - Edit
/etc/httpd/conf/httpd.conf
as the root user. Configure theListen
option so it lists a port that is not configured in SELinux policy configuration forhttpd
. In this example,httpd
is configured to listen on port 12345:# Change this to Listen on specific IP addresses as shown below to # prevent Apache from glomming onto all bound IP addresses (0.0.0.0) # #Listen 12.34.56.78:80 Listen 127.0.0.1:12345
- Run the
service httpd start
command to starthttpd
:~]#
service httpd start
Starting httpd: (13)Permission denied: make_sock: could not bind to address 127.0.0.1:12345 no listening sockets available, shutting down Unable to open logs [FAILED]An SELinux denial similar to the following is logged:setroubleshoot: SELinux is preventing the httpd (httpd_t) from binding to port 12345. For complete SELinux messages. run sealert -l f18bca99-db64-4c16-9719-1db89f0d8c77
- For SELinux to allow
httpd
to listen on port 12345, as used in this example, the following command is required:~]#
semanage port -a -t http_port_t -p tcp 12345
- Run
service httpd start
again to starthttpd
and have it listen on the new port:~]#
service httpd start
Starting httpd: [ OK ] - Now that SELinux has been configured to allow
httpd
to listen on a non-standard port (TCP 12345 in this example),httpd
starts successfully on this port. - To prove that
httpd
is listening and communicating on TCP port 12345, open a telnet connection to the specified port and issue a HTTP GET command, as follows:~]#
telnet localhost 12345
Trying 127.0.0.1... Connected to localhost. Escape character is '^]'. GET / HTTP/1.0 HTTP/1.1 200 OK Date: Wed, 02 Dec 2009 14:36:34 GMT Server: Apache/2.2.13 (Red Hat) Accept-Ranges: bytes Content-Length: 3985 Content-Type: text/html; charset=UTF-8 [...continues...]
2.2. Types
/var/www/html/
directory, and shows the file inheriting the httpd_sys_content_t
type from its parent directory (/var/www/html/
):
- Run the
ls -dZ /var/www/html
command to view the SELinux context of/var/www/html/
:~]$
ls -dZ /var/www/html
drwxr-xr-x root root system_u:object_r:httpd_sys_content_t:s0 /var/www/htmlThis shows/var/www/html/
is labeled with thehttpd_sys_content_t
type. - Run the
touch /var/www/html/file1
command as the root user to create a new file. - Run the
ls -Z /var/www/html/file1
command to view the SELinux context:~]$
ls -Z /var/www/html/file1
-rw-r--r-- root root unconfined_u:object_r:httpd_sys_content_t:s0 /var/www/html/file1
ls -Z
command shows file1
labeled with the httpd_sys_content_t
type. SELinux allows httpd
to read files labeled with this type, but not write to them, even if Linux permissions allow write access. SELinux policy defines what types a process running in the httpd_t
domain (where httpd
runs) can read and write to. This helps prevent processes from accessing files intended for use by another process.
httpd
can access files labeled with the httpd_sys_content_t
type (intended for the Apache HTTP Server), but by default, cannot access files labeled with the samba_share_t
type (intended for Samba). Also, files in user home directories are labeled with the user_home_t
type: by default, this prevents httpd
from reading or writing to files in user home directories.
httpd
. Different types allow you to configure flexible access:
httpd_sys_content_t
- Use this type for static web content, such as
.html
files used by a static website. Files labeled with this type are accessible (read only) tohttpd
and scripts executed byhttpd
. By default, files and directories labeled with this type cannot be written to or modified byhttpd
or other processes. Note that by default, files created in or copied into/var/www/html/
are labeled with thehttpd_sys_content_t
type. httpd_sys_script_exec_t
- Use this type for scripts you want
httpd
to execute. This type is commonly used for Common Gateway Interface (CGI) scripts in/var/www/cgi-bin/
. By default, SELinux policy preventshttpd
from executing CGI scripts. To allow this, label the scripts with thehttpd_sys_script_exec_t
type and enable thehttpd_enable_cgi
Boolean. Scripts labeled withhttpd_sys_script_exec_t
run in thehttpd_sys_script_t
domain when executed byhttpd
. Thehttpd_sys_script_t
domain has access to other system domains, such aspostgresql_t
andmysqld_t
. httpd_sys_rw_content_t
- Files labeled with this type can be written to by scripts labeled with the
httpd_sys_script_exec_t
type, but cannot be modified by scripts labeled with any other type. You must use thehttpd_sys_rw_content_t
type to label files that will be read from and written to by scripts labeled with thehttpd_sys_script_exec_t
type. httpd_sys_ra_content_t
- Files labeled with this type can be appended to by scripts labeled with the
httpd_sys_script_exec_t
type, but cannot be modified by scripts labeled with any other type. You must use thehttpd_sys_ra_content_t
type to label files that will be read from and appended to by scripts labeled with thehttpd_sys_script_exec_t
type. httpd_unconfined_script_exec_t
- Scripts labeled with this type run without SELinux protection. Only use this type for complex scripts, after exhausting all other options. It is better to use this type instead of disabling SELinux protection for
httpd
, or for the entire system.
Note
~]$ grep httpd /etc/selinux/targeted/contexts/files/file_contexts
Procedure 2.1. Changing the SELinux Context
chcon
command. Changes made with chcon
do not survive a file system relabel or the restorecon
command. SELinux policy controls whether users are able to modify the SELinux context for any given file. The following example demonstrates creating a new directory and an index.html
file for use by httpd
, and labeling that file and directory to allow httpd
access to them:
- Run the
mkdir -p /my/website
command as the root user to create a top-level directory structure to store files to be used byhttpd
. - Files and directories that do not match a pattern in file-context configuration may be labeled with the
default_t
type. This type is inaccessible to confined services:~]$
ls -dZ /my
drwxr-xr-x root root unconfined_u:object_r:default_t:s0 /my - Run the
chcon -R -t httpd_sys_content_t /my/
command as the root user to change the type of the/my/
directory and subdirectories, to a type accessible tohttpd
. Now, files created under/my/website/
inherit thehttpd_sys_content_t
type, rather than thedefault_t
type, and are therefore accessible to httpd:~]#
chcon -R -t httpd_sys_content_t /my/
~]#touch /my/website/index.html
~]#ls -Z /my/website/index.html
-rw-r--r-- root root unconfined_u:object_r:httpd_sys_content_t:s0 /my/website/index.html
chcon
.
semanage fcontext
command (semanage
is provided by the policycoreutils-python package) to make label changes that survive a relabel and the restorecon
command. This command adds changes to file-context configuration. Then, run restorecon
, which reads file-context configuration, to apply the label change. The following example demonstrates creating a new directory and an index.html
file for use by httpd
, and persistently changing the label of that directory and file to allow httpd
access to them:
- Run the
mkdir -p /my/website
command as the root user to create a top-level directory structure to store files to be used byhttpd
. - Run the following command as the root user to add the label change to file-context configuration:
~]#
semanage fcontext -a -t httpd_sys_content_t "/my(/.*)?"
The"/my(/.*)?"
expression means the label change applies to the/my/
directory and all files and directories under it. - Run the
touch /my/website/index.html
command as the root user to create a new file. - Run the
restorecon -R -v /my/
command as the root user to apply the label changes (restorecon
reads file-context configuration, which was modified by thesemanage
command in step 2):~]#
restorecon -R -v /my/
restorecon reset /my context unconfined_u:object_r:default_t:s0->system_u:object_r:httpd_sys_content_t:s0 restorecon reset /my/website context unconfined_u:object_r:default_t:s0->system_u:object_r:httpd_sys_content_t:s0 restorecon reset /my/website/index.html context unconfined_u:object_r:default_t:s0->system_u:object_r:httpd_sys_content_t:s0
2.3. Booleans
setsebool
command. For example, to enable the allow_httpd_anon_write
Boolean, run the following command as the root user:
~]# setsebool -P allow_httpd_anon_write on
on
to off
in the command, as shown below:
~]# setsebool -P allow_httpd_anon_write off
Note
-P
option if you do not want setsebool
changes to persist across reboots.
httpd
is running:
allow_httpd_anon_write
- When disabled, this Boolean allows
httpd
to only have read access to files labeled with thepublic_content_rw_t
type. Enabling this Boolean will allowhttpd
to write to files labeled with thepublic_content_rw_t
type, such as a public directory containing files for a public file transfer service. allow_httpd_mod_auth_ntlm_winbind
- Enabling this Boolean allows access to NTLM and Winbind authentication mechanisms via the
mod_auth_ntlm_winbind
module inhttpd
. allow_httpd_mod_auth_pam
- Enabling this Boolean allows access to PAM authentication mechanisms via the
mod_auth_pam
module inhttpd
. allow_httpd_sys_script_anon_write
- This Boolean defines whether or not HTTP scripts are allowed write access to files labeled with the
public_content_rw_t
type, as used in a public file transfer service. httpd_builtin_scripting
- This Boolean defines access to
httpd
scripting. Having this Boolean enabled is often required for PHP content. httpd_can_network_connect
- When disabled, this Boolean prevents HTTP scripts and modules from initiating a connection to a network or remote port. Enable this Boolean to allow this access.
httpd_can_network_connect_db
- When disabled, this Boolean prevents HTTP scripts and modules from initiating a connection to database servers. Enable this Boolean to allow this access.
httpd_can_network_relay
- Enable this Boolean when
httpd
is being used as a forward or reverse proxy. httpd_can_sendmail
- When disabled, this Boolean prevents HTTP modules from sending mail. This can prevent spam attacks should a vulnerability be found in
httpd
. Enable this Boolean to allow HTTP modules to send mail. httpd_dbus_avahi
- When off, this Boolean denies
httpd
access to theavahi
service viaD-Bus
. Enable this Boolean to allow this access. httpd_enable_cgi
- When disabled, this Boolean prevents
httpd
from executing CGI scripts. Enable this Boolean to allowhttpd
to execute CGI scripts (CGI scripts must be labeled with thehttpd_sys_script_exec_t
type). httpd_enable_ftp_server
- Enabling this Boolean will allow
httpd
to listen on the FTP port and act as an FTP server. httpd_enable_homedirs
- When disabled, this Boolean prevents
httpd
from accessing user home directories. Enable this Boolean to allowhttpd
access to user home directories; for example, content in/home/*/
. httpd_execmem
- When enabled, this Boolean allows
httpd
to execute programs that require memory addresses that are both executable and writable. Enabling this Boolean is not recommended from a security standpoint as it reduces protection against buffer overflows, however certain modules and applications (such as Java and Mono applications) require this privilege. httpd_ssi_exec
- This Boolean defines whether or not server side include (SSI) elements in a web page can be executed.
httpd_tty_comm
- This Boolean defines whether or not
httpd
is allowed access to the controlling terminal. Usually this access is not required, however in cases such as configuring an SSL certificate file, terminal access is required to display and process a password prompt. httpd_unified
- When enabled, this Boolean allows
httpd_t
complete access to all of thehttpd
types (that is to execute, read, or write sys_content_t). When disabled, there is separation in place between web content that is read-only, writable or executable. Disabling this Boolean ensures an extra level of security but adds the administrative overhead of having to individually label scripts and other web content based on the file access that each should have. httpd_use_cifs
- Enable this Boolean to allow
httpd
access to files on CIFS volumes that are labeled with thecifs_t
type, such as file systems mounted via Samba. httpd_use_nfs
- Enable this Boolean to allow
httpd
access to files on NFS volumes that are labeled with thenfs_t
type, such as file systems mounted via NFS.
Note
~]# semanage boolean -l | grep service_name
2.4. Configuration examples
2.4.1. Running a static site
.html
files for that website with the httpd_sys_content_t
type. By default, the Apache HTTP Server cannot write to files that are labeled with the httpd_sys_content_t
type. The following example creates a new directory to store files for a read-only website:
- Run the
mkdir /mywebsite
command as the root user to create a top-level directory. - As the root user, create a
/mywebsite/index.html
file. Copy and paste the following content into/mywebsite/index.html
:<html> <h2>index.html from /mywebsite/</h2> </html>
- To allow the Apache HTTP Server read only access to
/mywebsite/
, as well as files and subdirectories under it, label/mywebsite/
with thehttpd_sys_content_t
type. Run the following command as the root user to add the label change to file-context configuration:~]#
semanage fcontext -a -t httpd_sys_content_t "/mywebsite(/.*)?"
- Run the
restorecon -R -v /mywebsite
command as the root user to make the label changes:~]#
restorecon -R -v /mywebsite
restorecon reset /mywebsite context unconfined_u:object_r:default_t:s0->system_u:object_r:httpd_sys_content_t:s0 restorecon reset /mywebsite/index.html context unconfined_u:object_r:default_t:s0->system_u:object_r:httpd_sys_content_t:s0 - For this example, edit
/etc/httpd/conf/httpd.conf
as the root user. Comment out the existingDocumentRoot
option. Add aDocumentRoot "/mywebsite"
option. After editing, these options should look as follows:#DocumentRoot "/var/www/html" DocumentRoot "/mywebsite"
- Run the
service httpd status
command as the root user to see the status of the Apache HTTP Server. If the server is stopped, run theservice httpd start
command as the root user to start it. If the server is running, run theservice httpd restart
command as the root user to restart the service (this also applies any changes made tohttpd.conf
). - Use a web browser to navigate to
http://localhost/index.html
. The following is displayed:index.html from /mywebsite/
2.4.2. Sharing NFS and CIFS volumes
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.
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 enabled or disabled to control which services are allowed to access the nfs_t
and cifs_t
types.
httpd_use_nfs
Boolean to allow httpd
to access and share NFS volumes (labeled with the nfs_t
type). Run the setsebool
command as the root user to enable the Boolean:
~]# setsebool -P httpd_use_nfs on
httpd_use_cifs
Boolean to allow httpd
to access and share CIFS volumes (labeled with the cifs_t
type). Run the setsebool
command as the root user to enable the Boolean:
~]# setsebool -P httpd_use_cifs on
Note
-P
option if you do not want setsebool
changes to persist across reboots.
2.4.3. Sharing files between services
httpd_sys_content_t
type, which are intended for use by the Apache HTTP Server. Files can be shared between the Apache HTTP Server, FTP, rsync, and Samba, if the desired files are labeled with the public_content_t
or public_content_rw_t
type.
- Run the
mkdir /shares
command as the root user to create a new top-level directory to share files between multiple services. - Files and directories that do not match a pattern in file-context configuration may be labeled with the
default_t
type. This type is inaccessible to confined services:~]$
ls -dZ /shares
drwxr-xr-x root root unconfined_u:object_r:default_t:s0 /shares - As the root user, create a
/shares/index.html
file. Copy and paste the following content into/shares/index.html
:<html> <body> <p>Hello</p> </body> </html>
- Labeling
/shares/
with thepublic_content_t
type allows read-only access by the Apache HTTP Server, FTP, rsync, and Samba. Run the following command as the root user to add the label change to file-context configuration:~]#
semanage fcontext -a -t public_content_t "/shares(/.*)?"
- Run the
restorecon -R -v /shares/
command as the root user to apply the label changes:~]#
restorecon -R -v /shares/
restorecon reset /shares context unconfined_u:object_r:default_t:s0->system_u:object_r:public_content_t:s0 restorecon reset /shares/index.html context unconfined_u:object_r:default_t:s0->system_u:object_r:public_content_t:s0
/shares/
through Samba:
- Run the
rpm -q samba samba-common samba-client
command to confirm the samba, samba-common, and samba-client packages are installed (version numbers may differ):~]$
rpm -q samba samba-common samba-client
samba-3.4.0-0.41.el6.3.i686 samba-common-3.4.0-0.41.el6.3.i686 samba-client-3.4.0-0.41.el6.3.i686If any of these packages are not installed, install them by running theyum install package-name
command as the root user. - Edit
/etc/samba/smb.conf
as the root user. Add the following entry to the bottom of this file to share the/shares/
directory through Samba:[shares] comment = Documents for Apache HTTP Server, FTP, rsync, and Samba path = /shares public = yes writable = no
- A Samba account is required to mount a Samba file system. Run the
smbpasswd -a username
command as the root user to create a Samba account, where username is an existing Linux user. For example,smbpasswd -a testuser
creates a Samba account for the Linuxtestuser
user:~]#
smbpasswd -a testuser
New SMB password: Enter a password Retype new SMB password: Enter the same password again Added user testuser.Runningsmbpasswd -a username
, where username is the user name of a Linux account that does not exist on the system, causes aCannot locate Unix account for 'username'!
error. - Run the
service smb start
command as the root user to start the Samba service:~]#
service smb start
Starting SMB services: [ OK ] - Run the
smbclient -U username -L localhost
command to list the available shares, where username is the Samba account added in step 3. When prompted for a password, enter the password assigned to the Samba account in step 3 (version numbers may differ):~]$
smbclient -U username -L localhost
Enter username's password: Domain=[HOSTNAME] OS=[Unix] Server=[Samba 3.4.0-0.41.el6] Sharename Type Comment --------- ---- ------- shares Disk Documents for Apache HTTP Server, FTP, rsync, and Samba IPC$ IPC IPC Service (Samba Server Version 3.4.0-0.41.el6) username Disk Home Directories Domain=[HOSTNAME] OS=[Unix] Server=[Samba 3.4.0-0.41.el6] Server Comment --------- ------- Workgroup Master --------- ------- - Run the
mkdir /test/
command as the root user to create a new directory. This directory will be used to mount theshares
Samba share. - Run the following command as the root user to mount the
shares
Samba share to/test/
, replacing username with the user name from step 3:~]#
mount //localhost/shares /test/ -o user=username
Enter the password for username, which was configured in step 3. - Run the
cat /test/index.html
command to view the file, which is being shared through Samba:~]$
cat /test/index.html
<html> <body> <p>Hello</p> </body> </html>
/shares/
through the Apache HTTP Server:
- Run the
rpm -q httpd
command to confirm the httpd package is installed (version number may differ):~]$
rpm -q httpd
httpd-2.2.11-6.i386If this package is not installed, run theyum install httpd
command as the root user to install it. - Change into the
/var/www/html/
directory. Run the following command as the root user to create a link (namedshares
) to the/shares/
directory:~]#
ln -s /shares/ shares
- Run the
service httpd start
command as the root user to start the Apache HTTP Server:~]#
service httpd start
Starting httpd: [ OK ] - Use a web browser to navigate to
http://localhost/shares
. The/shares/index.html
file is displayed.
index.html
file if it exists. If /shares/
did not have index.html
, and instead had file1
, file2
, and file3
, a directory listing would occur when accessing http://localhost/shares
:
- Run the
rm -i /shares/index.html
command as the root user to remove theindex.html
file. - Run the
touch /shares/file{1,2,3}
command as the root user to create three files in/shares/
:~]#
touch /shares/file{1,2,3}
~]#ls -Z /shares/
-rw-r--r-- root root system_u:object_r:public_content_t:s0 file1 -rw-r--r-- root root unconfined_u:object_r:public_content_t:s0 file2 -rw-r--r-- root root unconfined_u:object_r:public_content_t:s0 file3 - Run the
service httpd status
command as the root user to see the status of the Apache HTTP Server. If the server is stopped, runservice httpd start
as the root user to start it. - Use a web browser to navigate to
http://localhost/shares
. A directory listing is displayed:
2.4.4. Changing port numbers
semanage port -l | grep -w "http_port_t"
command as the root user to list the ports SELinux allows httpd
to listen on:
~]# semanage port -l | grep -w http_port_t
http_port_t tcp 80, 443, 488, 8008, 8009, 8443
http
to listen on TCP ports 80, 443, 488, 8008, 8009, or 8443. If /etc/httpd/conf/httpd.conf
is configured so that httpd
listens on any port not listed for http_port_t
, httpd
fails to start.
httpd
to run on a port other than TCP ports 80, 443, 488, 8008, 8009, or 8443:
- Edit
/etc/httpd/conf/httpd.conf
as the root user so theListen
option lists a port that is not configured in SELinux policy forhttpd
. The following example configureshttpd
to listen on the 10.0.0.1 IP address, and on TCP port 12345:# Change this to Listen on specific IP addresses as shown below to # prevent Apache from glomming onto all bound IP addresses (0.0.0.0) # #Listen 12.34.56.78:80 Listen 10.0.0.1:12345
- Run the
semanage port -a -t http_port_t -p tcp 12345
command as the root user to add the port to SELinux policy configuration. - Run the
semanage port -l | grep -w http_port_t
command as the root user to confirm the port is added:~]#
semanage port -l | grep -w http_port_t
http_port_t tcp 12345, 80, 443, 488, 8008, 8009, 8443
httpd
on port 12345, run the semanage port -d -t http_port_t -p tcp 12345
command as the root user to remove the port from policy configuration.
Chapter 3. Samba
rpm -q samba
command to see if the samba package is installed. If it is not installed and you want to use Samba, run the following command as the root user to install it:
~]# yum install samba
3.1. Samba and SELinux
smbd
) runs confined by default. Confined services run in their own domains, and are separated from other confined services. The following example demonstrates the smbd
process running in its own domain. This example assumes the samba package is installed:
- Run the
getenforce
command to confirm SELinux is running in enforcing mode:~]$
getenforce
EnforcingThegetenforce
command returnsEnforcing
when SELinux is running in enforcing mode. - Run the
service smbd start
command as the root user to startsmbd
:~]#
service smb start
Starting SMB services: [ OK ] - Run the
ps -eZ | grep smb
command to view thesmbd
processes:~]$
ps -eZ | grep smb
unconfined_u:system_r:smbd_t:s0 16420 ? 00:00:00 smbd unconfined_u:system_r:smbd_t:s0 16422 ? 00:00:00 smbdThe SELinux context associated with thesmbd
processes isunconfined_u:system_r:smbd_t:s0
. The second last part of the context,smbd_t
, is the type. A type defines a domain for processes and a type for files. In this case, thesmbd
processes are running in the smbd_t domain.
smbd
to access and share them. For example, smbd
can read and write to files labeled with the samba_share_t
type, but by default, cannot access files labeled with the httpd_sys_content_t
type, which is intended for use by the Apache HTTP Server. Booleans must be enabled to allow certain behavior, such as allowing home directories and NFS volumes to be exported through Samba, as well as to allow Samba to act as a domain controller.
3.2. Types
samba_share_t
type to allow Samba to share them. Only label files you have created, and do not relabel system files with the samba_share_t
type: Booleans can be enabled to share such files and directories. SELinux allows Samba to write to files labeled with the samba_share_t
type, as long as /etc/samba/smb.conf
and Linux permissions are set accordingly.
samba_etc_t
type is used on certain files in /etc/samba/
, such as smb.conf
. Do not manually label files with the samba_etc_t
type. If files in /etc/samba/
are not labeled correctly, run the restorecon -R -v /etc/samba
command as the root user to restore such files to their default contexts. If /etc/samba/smb.conf
is not labeled with the samba_etc_t
type, the service smb start
command may fail and an SELinux denial may be logged. The following is an example denial when /etc/samba/smb.conf
was labeled with the httpd_sys_content_t
type:
setroubleshoot: SELinux is preventing smbd (smbd_t) "read" to ./smb.conf (httpd_sys_content_t). For complete SELinux messages. run sealert -l deb33473-1069-482b-bb50-e4cd05ab18af
3.3. Booleans
allow_smbd_anon_write
- Having this Boolean enabled allows
smbd
to write to a public directory, such as an area reserved for common files that otherwise has no special access restrictions. samba_create_home_dirs
- Having this Boolean enabled allows Samba to create new home directories independently. This is often done by mechanisms such as PAM.
samba_domain_controller
- When enabled, this Boolean allows Samba to act as a domain controller, as well as giving it permission to execute related commands such as
useradd
,groupadd
andpasswd
. samba_enable_home_dirs
- Enabling this Boolean allows Samba to share users' home directories.
samba_export_all_ro
- Export any file or directory, allowing read-only permissions. This allows files and directories that are not labeled with the
samba_share_t
type to be shared through Samba. When thesamba_export_all_ro
Boolean is on, but thesamba_export_all_rw
Boolean is off, write access to Samba shares is denied, even if write access is configured in/etc/samba/smb.conf
, as well as Linux permissions allowing write access. samba_export_all_rw
- Export any file or directory, allowing read and write permissions. This allows files and directories that are not labeled with the
samba_share_t
type to be exported through Samba. Permissions in/etc/samba/smb.conf
and Linux permissions must be configured to allow write access. samba_run_unconfined
- Having this Boolean enabled allows Samba to run unconfined scripts in the
/var/lib/samba/scripts/
directory. samba_share_fusefs
- This Boolean must be enabled for Samba to share fusefs file systems.
samba_share_nfs
- Disabling this Boolean prevents
smbd
from having full access to NFS shares via Samba. Enabling this Boolean will allow Samba to share NFS volumes. use_samba_home_dirs
- Enable this Boolean to use a remote server for Samba home directories.
virt_use_samba
- Allow virtual machine access to CIFS files.
Note
~]# semanage boolean -l | grep service_name
3.4. Configuration examples
3.4.1. Sharing directories you create
- Run the
rpm -q samba samba-common samba-client
command to confirm the samba, samba-common, and samba-client packages are installed. If any of these packages are not installed, install them by running theyum install package-name
command as the root user. - Run the
mkdir /myshare
command as the root user to create a new top-level directory to share files through Samba. - Run the
touch /myshare/file1
command as the root user to create an empty file. This file is used later to verify the Samba share mounted correctly. - SELinux allows Samba to read and write to files labeled with the
samba_share_t
type, as long as/etc/samba/smb.conf
and Linux permissions are set accordingly. Run the following command as the root user to add the label change to file-context configuration:~]#
semanage fcontext -a -t samba_share_t "/myshare(/.*)?"
- Run the
restorecon -R -v /myshare
command as the root user to apply the label changes:~]#
restorecon -R -v /myshare
restorecon reset /myshare context unconfined_u:object_r:default_t:s0->system_u:object_r:samba_share_t:s0 restorecon reset /myshare/file1 context unconfined_u:object_r:default_t:s0->system_u:object_r:samba_share_t:s0 - Edit
/etc/samba/smb.conf
as the root user. Add the following to the bottom of this file to share the/myshare/
directory through Samba:[myshare] comment = My share path = /myshare public = yes writable = no
- A Samba account is required to mount a Samba file system. Run the
smbpasswd -a username
command as the root user to create a Samba account, where username is an existing Linux user. For example,smbpasswd -a testuser
creates a Samba account for the Linuxtestuser
user:~]#
smbpasswd -a testuser
New SMB password: Enter a password Retype new SMB password: Enter the same password again Added user testuser.Runningsmbpasswd -a username
, where username is the user name of a Linux account that does not exist on the system, causes aCannot locate Unix account for 'username'!
error. - Run the
service smb start
command as the root user to start the Samba service:~]#
service smb start
Starting SMB services: [ OK ] - Run the
smbclient -U username -L localhost
command to list the available shares, where username is the Samba account added in step 7. When prompted for a password, enter the password assigned to the Samba account in step 7 (version numbers may differ):~]$
smbclient -U username -L localhost
Enter username's password: Domain=[HOSTNAME] OS=[Unix] Server=[Samba 3.4.0-0.41.el6] Sharename Type Comment --------- ---- ------- myshare Disk My share IPC$ IPC IPC Service (Samba Server Version 3.4.0-0.41.el6) username Disk Home Directories Domain=[HOSTNAME] OS=[Unix] Server=[Samba 3.4.0-0.41.el6] Server Comment --------- ------- Workgroup Master --------- ------- - Run the
mkdir /test/
command as the root user to create a new directory. This directory will be used to mount themyshare
Samba share. - Run the following command as the root user to mount the
myshare
Samba share to/test/
, replacing username with the user name from step 7:~]#
mount //localhost/myshare /test/ -o user=username
Enter the password for username, which was configured in step 7. - Run the
ls /test/
command to view thefile1
file created in step 3:~]$
ls /test/
file1
3.4.2. Sharing a website
samba_share_t
type, for example, when wanting to share a website in /var/www/html/
. For these cases, use the samba_export_all_ro
Boolean to share any file or directory (regardless of the current label), allowing read only permissions, or the samba_export_all_rw
Boolean to share any file or directory (regardless of the current label), allowing read and write permissions.
/var/www/html/
, and then shares that file through Samba, allowing read and write permissions. This example assumes the httpd, samba, samba-common, samba-client, and wget packages are installed:
- As the root user, create a
/var/www/html/file1.html
file. Copy and paste the following content into/var/www/html/file1.html
:<html> <h2>File being shared through the Apache HTTP Server and Samba.</h2> </html>
- Run the
ls -Z /var/www/html/file1.html
command to view the SELinux context offile1.html
:~]$
ls -Z /var/www/html/file1.html
-rw-r--r--. root root unconfined_u:object_r:httpd_sys_content_t:s0 /var/www/html/file1.htmlfile1.index.html
is labeled with thehttpd_sys_content_t
. By default, the Apache HTTP Server can access this type, but Samba cannot. - Run the
service httpd start
command as the root user to start the Apache HTTP Server:~]#
service httpd start
Starting httpd: [ OK ] - Change into a directory your user has write access to, and run the
wget http://localhost/file1.html
command. Unless there are changes to the default configuration, this command succeeds:~]$
wget http://localhost/file1.html
Resolving localhost... 127.0.0.1 Connecting to localhost|127.0.0.1|:80... connected. HTTP request sent, awaiting response... 200 OK Length: 84 [text/html] Saving to: `file1.html.1' 100%[=======================>] 84 --.-K/s in 0s `file1.html.1' saved [84/84] - Edit
/etc/samba/smb.conf
as the root user. Add the following to the bottom of this file to share the/var/www/html/
directory through Samba:[website] comment = Sharing a website path = /var/www/html/ public = no writable = no
- The
/var/www/html/
directory is labeled with thehttpd_sys_content_t
type. By default, Samba cannot access files and directories labeled with thehttpd_sys_content_t
type, even if Linux permissions allow it. To allow Samba access, run the following command as the root user to enable thesamba_export_all_ro
Boolean:~]#
setsebool -P samba_export_all_ro on
Do not use the-P
option if you do not want the change to persist across reboots. Note that enabling thesamba_export_all_ro
Boolean allows Samba to access any type. - Run
service smb start
as the root user to startsmbd
:~]#
service smb start
Starting SMB services: [ OK ]
Chapter 4. File Transfer Protocol
vsftpd
) is designed from the ground up to be fast, stable, and, most importantly, secure. Its ability to handle large numbers of connections efficiently and securely is why vsftpd
is the only stand-alone FTP distributed with Red Hat Enterprise Linux.
rpm -q vsftpd
command to see if vsftpd is installed:
~]$ rpm -q vsftpd
~]# yum install vsftpd
4.1. FTP and SELinux
vsftpd
FTP daemon runs confined by default. SELinux policy defines how vsftpd
interacts with files, processes, and with the system in general. For example, when an authenticated user logs in via FTP, they cannot read from or write to files in their home directories: SELinux prevents vsftpd
from accessing user home directories by default. Also, by default, vsftpd
does not have access to NFS or CIFS volumes, and anonymous users do not have write access, even if such write access is configured in /etc/vsftpd/vsftpd.conf
. Booleans can be enabled to allow the previously mentioned access.
- Run the
rpm -q ftp
command to see if the ftp package is installed. If it is not, run theyum install ftp
command as the root user to install it. - Run the
rpm -q vsftpd
command to see if the vsftpd package is installed. If it is not, run theyum install vsftpd
command as the root user to install it. - In Red Hat Enterprise Linux,
vsftpd
only allows anonymous users to log in by default. To allow authenticated users to log in, edit/etc/vsftpd/vsftpd.conf
as the root user. Make sure thelocal_enable=YES
option is uncommented:# Uncomment this to allow local users to log in. local_enable=YES
- Run the
service vsftpd start
command as the root user to startvsftpd
. If the service was running before editingvsftpd.conf
, run theservice vsftpd restart
command as the root user to apply the configuration changes:~]#
service vsftpd start
Starting vsftpd for vsftpd: [ OK ] - Run the
ftp localhost
command as the user you are currently logged in with. When prompted for your name, make sure your user name is displayed. If the correct user name is displayed, press Enter, otherwise, enter the correct user name:~]
ftp localhost
Connected to localhost (127.0.0.1). 220 (vsFTPd 2.1.0) Name (localhost:username): 331 Please specify the password. Password: Enter your password 500 OOPS: cannot change directory:/home/username Login failed. ftp> - An SELinux denial similar to the following is logged:
setroubleshoot: SELinux is preventing the ftp daemon from reading users home directories (username). For complete SELinux messages. run sealert -l c366e889-2553-4c16-b73f-92f36a1730ce
- Access to home directories has been denied by SELinux. This can be fixed by activating the
ftp_home_dir
Boolean. Enable thisftp_home_dir
Boolean by running the following command as the root user:~]#
setsebool -P ftp_home_dir=1
Note
Do not use the -P option if you do not want changes to persist across reboots.Try to log in again. Now that SELinux is allowing access to home directories via theftp_home_dir
Boolean, logging in will succeed.
4.2. Types
/var/ftp/
when they log in via FTP. This directory is labeled with the public_content_t
type, allowing only read access, even if write access is configured in /etc/vsftpd/vsftpd.conf
. The public_content_t
type is accessible to other services, such as Apache HTTP Server, Samba, and NFS.
public_content_t
- Label files and directories you have created with the
public_content_t
type to share them read-only through vsftpd. Other services, such as Apache HTTP Server, Samba, and NFS, also have access to files labeled with this type. Files labeled with thepublic_content_t
type cannot be written to, even if Linux permissions allow write access. If you require write access, use thepublic_content_rw_t
type. public_content_rw_t
- Label files and directories you have created with the
public_content_rw_t
type to share them with read and write permissions throughvsftpd
. Other services, such as Apache HTTP Server, Samba, and NFS, also have access to files labeled with this type. Remember that Booleans for each service must be enabled before they can write to files labeled with this type.
4.3. Booleans
allow_ftpd_anon_write
- When disabled, this Boolean prevents
vsftpd
from writing to files and directories labeled with thepublic_content_rw_t
type. Enable this Boolean to allow users to upload files via FTP. The directory where files are uploaded to must be labeled with thepublic_content_rw_t
type and Linux permissions set accordingly. allow_ftpd_full_access
- When this Boolean is on, only Linux (DAC) permissions are used to control access, and authenticated users can read and write to files that are not labeled with the
public_content_t
orpublic_content_rw_t
types. allow_ftpd_use_cifs
- Having this Boolean enabled allows
vsftpd
to access files and directories labeled with thecifs_t
type; therefore, having this Boolean enabled allows you to share file systems mounted via Samba throughvsftpd
. allow_ftpd_use_nfs
- Having this Boolean enabled allows
vsftpd
to access files and directories labeled with thenfs_t
type; therefore, having this Boolean enabled allows you to share file systems mounted via NFS throughvsftpd
. ftp_home_dir
- Having this Boolean enabled allows authenticated users to read and write to files in their home directories. When this Boolean is off, attempting to download a file from a home directory results in an error such as
550 Failed to open file
. An SELinux denial is logged. ftpd_connect_db
- Allow FTP daemons to initiate a connection to a database.
httpd_enable_ftp_server
- Allow
httpd
to listen on the FTP port and act as a FTP server. tftp_anon_write
- Having this Boolean enabled allows TFTP access to a public directory, such as an area reserved for common files that otherwise has no special access restrictions.
Note
~]# semanage boolean -l | grep service_name
4.4. Configuration Examples
4.4.1. Uploading to an FTP site
- Run the
setsebool ftp_home_dir=1
command as the root user to enable access to FTP home directories. - Run the
mkdir -p /myftp/pub
command as the root user to create a new top-level directory. - Set Linux permissions on the
/myftp/pub/
directory to allow a Linux user write access. This example changes the owner and group from root to owneruser1
and group root. Replaceuser1
with the user you want to give write access to:~]#
chown user1:root /myftp/pub
~]#chmod 775 /myftp/pub
Thechown
command changes the owner and group permissions. Thechmod
command changes the mode, allowing theuser1
user read, write, and execute permissions, and members of the root group read, write, and execute permissions. Everyone else has read and execute permissions, which allows the Apache HTTP Server to read files from this directory. - When running SELinux, files and directories must be labeled correctly to allow access. Setting Linux permissions is not enough. Files labeled with the
public_content_t
type allow them to be read by FTP, Apache HTTP Server, Samba, and rsync. Files labeled with thepublic_content_rw_t
type can be written to by FTP. Other services, such as Samba, require Booleans to be set before they can write to files labeled with thepublic_content_rw_t
type. Label the top-level directory (/myftp/
) with thepublic_content_t
type, to prevent copied or newly-created files under/myftp/
from being written to or modified by services. Run the following command as the root user to add the label change to file-context configuration:~]#
semanage fcontext -a -t public_content_t /myftp
- Run the
restorecon -R -v /myftp/
command to apply the label change:~]#
restorecon -R -v /myftp/
restorecon reset /myftp context unconfined_u:object_r:default_t:s0->system_u:object_r:public_content_t:s0 - Confirm
/myftp
is labeled with thepublic_content_t
type, and/myftp/pub/
is labeled with thedefault_t
type:~]$
ls -dZ /myftp/
drwxr-xr-x. root root system_u:object_r:public_content_t:s0 /myftp/ ~]$ls -dZ /myftp/pub/
drwxrwxr-x. user1 root unconfined_u:object_r:default_t:s0 /myftp/pub/ - FTP must be allowed to write to a directory before users can upload files via FTP. SELinux allows FTP to write to directories labeled with the
public_content_rw_t
type. This example uses/myftp/pub/
as the directory FTP can write to. Run the following command as the root user to add the label change to file-context configuration:~]#
semanage fcontext -a -t public_content_rw_t "/myftp/pub(/.*)?"
- Run the
restorecon -R -v /myftp/pub
command as the root user to apply the label change:~]#
restorecon -R -v /myftp/pub
restorecon reset /myftp/pub context system_u:object_r:default_t:s0->system_u:object_r:public_content_rw_t:s0 - The
allow_ftpd_anon_write
Boolean must be on to allowvsftpd
to write to files that are labeled with thepublic_content_rw_t
type. Run the following command as the root user to enable this Boolean:~]#
setsebool -P allow_ftpd_anon_write on
Do not use the-P
option if you do not want changes to persist across reboots.
user1
user from the previous example, where user1
is the dedicated owner of the /myftp/pub/
directory:
- Run the
cd ~/
command to change into your home directory. Then, run themkdir myftp
command to create a directory to store files to upload via FTP. - Run the
cd ~/myftp
command to change into the~/myftp/
directory. In this directory, create anftpupload
file. Copy the following contents into this file:File upload via FTP from a home directory.
- Run the
getsebool allow_ftpd_anon_write
command to confirm theallow_ftpd_anon_write
Boolean is on:~]$
getsebool allow_ftpd_anon_write
allow_ftpd_anon_write --> onIf this Boolean is off, run thesetsebool -P allow_ftpd_anon_write on
command as the root user to enable it. Do not use the-P
option if you do not want the change to persist across reboots. - Run the
service vsftpd start
command as the root user to startvsftpd
:~]#
service vsftpd start
Starting vsftpd for vsftpd: [ OK ] - Run the
ftp localhost
command. When prompted for a user name, enter the user name of the user who has write access, then, enter the correct password for that user:~]$
ftp localhost
Connected to localhost (127.0.0.1). 220 (vsFTPd 2.1.0) Name (localhost:username): 331 Please specify the password. Password: Enter the correct password 230 Login successful. Remote system type is UNIX. Using binary mode to transfer files. ftp> cd myftp 250 Directory successfully changed. ftp> put ftpupload local: ftpupload remote: ftpupload 227 Entering Passive Mode (127,0,0,1,241,41). 150 Ok to send data. 226 File receive OK. ftp> 221 Goodbye.The upload succeeds as theallow_ftpd_anon_write
Boolean is enabled.
Chapter 5. Network File System
rpm -q nfs-utils
command to see if the nfs-utils is installed. If it is not installed and you want to use NFS, run the following command as the root user to install it:
~]# yum install nfs-utils
5.1. NFS and SELinux
NFS
daemons are confined by default. SELinux policy allows NFS to share files by default.
5.2. Types
nfs_t
type. The following types are used with NFS. Different types allow you to configure flexible access:
var_lib_nfs_t
- This type is used for existing and new files copied to or created in the
/var/lib/nfs/
directory. This type should not need to be changed in normal operation. To restore changes to the default settings, run therestorecon -R -v /var/lib/nfs
command as the root user. nfsd_exec_t
- The
/usr/sbin/rpc.nfsd
file is labeled with thenfsd_exec_t
, as are other system executables and libraries related to NFS. Users should not label any files with this type.nfsd_exec_t
will transition tonfsd_t
.
5.3. Booleans
allow_ftpd_use_nfs
- When enabled, this Boolean allows the
ftpd
daemon to access NFS volumes. cobbler_use_nfs
- When enabled, this Boolean allows the
cobblerd
daemon to access NFS volumes. git_system_use_nfs
- When enabled, this Boolean allows the Git system daemon to read system shared repositories on NFS volumes.
httpd_use_nfs
- When enabled, this Boolean allows the
httpd
daemon to access files stored on NFS volumes. qemu_use_nfs
- When enabled, this Boolean allows Qemu to use NFS volumes.
rsync_use_nfs
- When enabled, this Boolean allows
rsync
servers to share NFS volumes. samba_share_nfs
- When enabled, this Boolean allows the
smbd
daemon to share NFS volumes. When disabled, this Boolean preventssmbd
from having full access to NFS shares via Samba. sanlock_use_nfs
- When enabled, this Boolean allows the
sanlock
daemon to manage NFS volumes. sge_use_nfs
- When enabled, this Boolean allows the
sge
scheduler to access NFS volumes. use_nfs_home_dirs
- When enabled, this Boolean adds support for NFS home directories.
virt_use_nfs
- When enabled, this Boolean allows confident virtual guests to manage files on NFS volumes.
xen_use_nfs
- When enabled, this Boolean allows
Xen
to manage files on NFS volumes. git_cgi_use_nfs
- When enabled, this Boolean allows the Git Common Gateway Interface (CGI) to access NFS volumes.
tftp_use_nfs
- When enabled, this Boolean allows The Trivial File Transfer Protocol (TFTP) to read from NFS volumes for public file transfer services.
Note
~]# semanage boolean -l | grep service_name
Chapter 6. Berkeley Internet Name Domain
named
daemon. BIND lets users locate computer resources and services by name instead of numerical addresses.
rpm -q bind
command to see if the bind package is installed. If it is not installed, run the following command as the root user to install it:
~]# yum install bind
6.1. BIND and SELinux
/var/named/slaves/
, /var/named/dynamic/
and /var/named/data/
directories allow zone files to be updated via zone transfers and dynamic DNS updates. Files in /var/named/
are labeled with the named_zone_t
type, which is used for master zone files.
/etc/named.conf
to place slave zones in /var/named/slaves/
. The following is an example of a domain entry in /etc/named.conf
for a slave DNS server that stores the zone file for testdomain.com
in /var/named/slaves/
:
zone "testdomain.com" { type slave; masters { IP-address; }; file "/var/named/slaves/db.testdomain.com"; };
named_zone_t
, the named_write_master_zones
Boolean must be enabled to allow zone transfers and dynamic DNS to update the zone file. Also, the mode of the parent directory has to be changed to allow the named
user or group read, write and execute access.
/var/named/
are labeled with the named_cache_t
type, a file system relabel or running restorecon -R /var/
will change their type to named_zone_t
.
6.2. Types
named_zone_t
- Used for master zone files. Other services cannot modify files of this type.
named
can only modify files of this type if thenamed_write_master_zones
Boolean is enabled. named_cache_t
- By default,
named
can write to files labeled with this type, without additional Booleans being set. Files copied or created in the/var/named/slaves/
,/var/named/dynamic/
and/var/named/data/
directories are automatically labeled with thenamed_cache_t
type. named_var_run_t
- Files copied or created in the
/var/run/bind/
,/var/run/named/
, and/var/run/unbound/
directories are automatically labeled with thenamed_var_run_t
type. named_conf_t
- BIND-related configuration files, usually stored in the
/etc/
directory, are automatically labeled with thenamed_conf_t
type. named_exec_t
- BIND-related executable files, usually stored in the
/usr/sbin/
directory, are automatically labeled with thenamed_exec_t
type. named_log_t
- BIND-related log files, usually stored in the
/var/log/
directory, are automatically labeled with thenamed_log_t
type. named_initrc_exec_t
- Executable BIND-related files in the
/etc/rc.d/init.d/
directory are automatically labeled with thenamed_initrc_exec_t
type.
6.3. Booleans
named_write_master_zones
- When disabled, this Boolean prevents
named
from writing to zone files or directories labeled with thenamed_zone_t
type.named
does not usually need to write to zone files; but in the case that it needs to, or if a secondary server needs to write to zone files, enable this Boolean to allow this action. named_bind_http_port
- When enabled, this Boolean allows BIND to bind an Apache port.
Note
~]# semanage boolean -l | grep service_name
6.4. Configuration Examples
6.4.1. Dynamic DNS
/var/named/dynamic/
directory for zone files you want updated via dynamic DNS. Files created in or copied into /var/named/dynamic/
inherit Linux permissions that allow named
to write to them. As such files are labeled with the named_cache_t
type, SELinux allows named
to write to them.
/var/named/dynamic/
is labeled with the named_zone_t
type, dynamic DNS updates may not be successful for a certain period of time as the update needs to be written to a journal first before being merged. If the zone file is labeled with the named_zone_t
type when the journal attempts to be merged, an error such as the following is logged:
named[PID]: dumping master file: rename: /var/named/dynamic/zone-name: permission denied
setroubleshoot: SELinux is preventing named (named_t) "unlink" to zone-name (named_zone_t)
restorecon -R -v /var/named/dynamic
command as the Linux root user.
Chapter 7. Concurrent Versioning System
rpm -q cvs
command to see if the cvs package is installed. If it is not installed and you want to use CVS, run the following command as the root user to install it:
~]# yum install cvs
7.1. CVS and SELinux
cvs
daemon runs as cvs_t
. By default in Red Hat Enterprise Linux, CVS is only allowed to read and write certain directories. The label cvs_data_t
defines which areas the cvs
daemon has read and write access to. When using CVS with SELinux, assigning the correct label is essential for clients to have full access to the area reserved for CVS data.
7.2. Types
cvs_data_t
- This type is used for data in a CVS repository. CVS can only gain full access to data if it has this type.
cvs_exec_t
- This type is used for the
/usr/bin/cvs
binary.
7.3. Booleans
allow_cvs_read_shadow
- This Boolean allows the
cvs
daemon to access the/etc/shadow
file for user authentication.
Note
~]# semanage boolean -l | grep service_name
7.4. Configuration Examples
7.4.1. Setting up CVS
cvs-srv
with an IP address of 192.168.1.1
and a client with a host name of cvs-client
and an IP address of 192.168.1.100
. Both hosts are on the same subnet (192.168.1.0/24). This is an example only and assumes that the cvs and xinetd packages are installed, that the SELinux targeted policy is used, and that SELinux is running in enforced mode.
Note
cvs-srv
.
- This example requires the cvs and xinetd packages. Run the
rpm -q cvs
command to see if the cvs package is installed. If it is not installed, run the following command as the root user to install cvs:~]#
yum install cvs
Run therpm -q xinetd
command to see if the xinetd package is installed. If it is not installed, run the following command as the root user to install xinetd:~]#
yum install xinetd
- Create a group named
CVS
. This can be done via thegroupadd CVS
command as the root user, or by using thesystem-config-users
tool. - Create a user with a user name of
cvsuser
and make this user a member of the CVS group. This can be done using thesystem-config-users
tool. - Edit the
/etc/services
file and make sure that the CVS server has uncommented entries looking similar to the following:cvspserver 2401/tcp # CVS client/server operations cvspserver 2401/udp # CVS client/server operations
- Create the CVS repository in the root area of the file system. When using SELinux, it is best to have the repository in the root file system so that recursive labels can be given to it without affecting any other subdirectories. For example, as the root user, create a
/cvs/
directory to house the repository:[root@cvs-srv]#
mkdir /cvs
- Give full permissions to the
/cvs/
directory to all users:[root@cvs-srv]#
chmod -R 777 /cvs
Warning
This is an example only and these permissions should not be used in a production system. - Edit the
/etc/xinetd.d/cvs
file and make sure that the CVS section is uncommented and configured to use the/cvs/
directory. The file should look similar to:service cvspserver { disable = no port = 2401 socket_type = stream protocol = tcp wait = no user = root passenv = PATH server = /usr/bin/cvs env = HOME=/cvs server_args = -f --allow-root=/cvs pserver # bind = 127.0.0.1
- Start the
xinetd
daemon by running theservice xinetd start
command as the root user. - Add a rule which allows inbound connections using TCP on port 2401 by using the
system-config-firewall
tool. - As the
cvsuser
user, run the following command:[cvsuser@cvs-client]$
cvs -d /cvs init
- At this point, CVS has been configured but SELinux will still deny logins and file access. To demonstrate this, set the
$CVSROOT
variable oncvs-client
and try to log in remotely. The following step should be performed oncvs-client
:[cvsuser@cvs-client]$
export CVSROOT=:pserver:cvsuser@192.168.1.1:/cvs
[cvsuser@cvs-client]$ [cvsuser@cvs-client]$cvs login
Logging in to :pserver:cvsuser@192.168.1.1:2401/cvs CVS password: ******** cvs [login aborted]: unrecognized auth response from 192.168.100.1: cvs pserver: cannot open /cvs/CVSROOT/config: Permission deniedSELinux has blocked access. In order to get SELinux to allow this access, the following step should be performed oncvs-srv
: - Change the context of the
/cvs/
directory as the root user in order to recursively label any existing and new data in the/cvs/
directory, giving it thecvs_data_t
type:[root@cvs-srv]#
semanage fcontext -a -t cvs_data_t '/cvs(/.*)?'
[root@cvs-srv]#restorecon -R -v /cvs
- The client,
cvs-client
should now be able to log in and access all CVS resources in this repository:[cvsuser@cvs-client]$
export CVSROOT=:pserver:cvsuser@192.168.1.1:/cvs
[cvsuser@cvs-client]$ [cvsuser@cvs-client]$cvs login
Logging in to :pserver:cvsuser@192.168.1.1:2401/cvs CVS password: ******** [cvsuser@cvs-client]$
Chapter 8. Squid Caching Proxy
rpm -q squid
command to see if the squid package is installed. If it is not installed and you want to use squid, run the following command as the root user to install it:
~]# yum install squid
8.1. Squid Caching Proxy and SELinux
- Run the
getenforce
command to confirm SELinux is running in enforcing mode:~]$
getenforce
EnforcingThegetenforce
command returnsEnforcing
when SELinux is running in enforcing mode. - Run the
service squid start
command as the root user to startsquid
:~]#
service squid start
Starting squid: [ OK ] - Run the
ps -eZ | grep squid
command to view thesquid
processes:~]$
ps -eZ | grep squid
unconfined_u:system_r:squid_t:s0 2522 ? 00:00:00 squid unconfined_u:system_r:squid_t:s0 2524 ? 00:00:00 squid unconfined_u:system_r:squid_t:s0 2526 ? 00:00:00 ncsa_auth unconfined_u:system_r:squid_t:s0 2527 ? 00:00:00 ncsa_auth unconfined_u:system_r:squid_t:s0 2528 ? 00:00:00 ncsa_auth unconfined_u:system_r:squid_t:s0 2529 ? 00:00:00 ncsa_auth unconfined_u:system_r:squid_t:s0 2530 ? 00:00:00 ncsa_auth unconfined_u:system_r:squid_t:s0 2531 ? 00:00:00 unlinkdThe SELinux context associated with thesquid
processes isunconfined_u:system_r:squid_t:s0
. The second last part of the context,squid_t
, is the type. A type defines a domain for processes and a type for files. In this case, thesquid
processes are running in thesquid_t
domain.
squid_t
, interact with files, other processes, and the system in general. Files must be labeled correctly to allow squid access to them.
/etc/squid/squid.conf
is configured so squid
listens on a port other than the default TCP ports 3128, 3401 or 4827, the semanage port
command must be used to add the required port number to the SELinux policy configuration. The following example demonstrates configuring squid
to listen on a port that is not initially defined in SELinux policy configuration for squid
, and, as a consequence, squid
failing to start. This example also demonstrates how to then configure the SELinux system to allow squid
to successfully listen on a non-standard port that is not already defined in the policy. This example assumes the squid package is installed. Run each command in the example as the root user:
- Run the
service squid status
command to confirmsquid
is not running:~]#
service squid status
squid is stoppedIf the output differs, run theservice squid stop
command to stop the process:~]#
service squid stop
Stopping squid: [ OK ] - Run the
semanage port -l | grep -w squid_port_t
command to view the ports SELinux allowssquid
to listen on:~]#
semanage port -l | grep -w -i squid_port_t
squid_port_t tcp 3401, 4827 squid_port_t udp 3401, 4827 - Edit
/etc/squid/squid.conf
as the root user. Configure thehttp_port
option so it lists a port that is not configured in SELinux policy configuration forsquid
. In this example,squid
is configured to listen on port 10000:# Squid normally listens to port 3128 http_port 10000
- Run the
setsebool
command to make sure thesquid_connect_any
Boolean is set to off. This ensures squid is only permitted to operate on specific ports:~]#
setsebool -P squid_connect_any 0
- Run the
service squid start
command to startsquid
:~]#
service squid start
Starting squid: .................... [FAILED]An SELinux denial similar to the following is logged:localhost setroubleshoot: SELinux is preventing the squid (squid_t) from binding to port 10000. For complete SELinux messages. run sealert -l 97136444-4497-4fff-a7a7-c4d8442db982
- For SELinux to allow
squid
to listen on port 10000, as used in this example, the following command is required:~]#
semanage port -a -t squid_port_t -p tcp 10000
- Run
service squid start
again to startsquid
and have it listen on the new port:~]#
service squid start
Starting squid: [ OK ] - Now that SELinux has been configured to allow
squid
to listen on a non-standard port (TCP 10000 in this example),squid
starts successfully on this port.
8.2. Types
squid
. Different types allow you to configure flexible access:
httpd_squid_script_exec_t
- This type is used for utilities such as
cachemgr.cgi
, which provides a variety of statistics about squid and its configuration. squid_cache_t
- Use this type for data that is cached by squid, as defined by the
cache_dir
directive in/etc/squid/squid.conf
. By default, files created in or copied into/var/cache/squid/
and/var/spool/squid/
are labeled with thesquid_cache_t
type. Files for the squidGuard URL redirector plugin forsquid
created in or copied to/var/squidGuard/
are also labeled with thesquid_cache_t
type. Squid is only able to use files and directories that are labeled with this type for its cached data. squid_conf_t
- This type is used for the directories and files that
squid
uses for its configuration. Existing files, or those created in or copied to/etc/squid/
and/usr/share/squid/
are labeled with this type, including error messages and icons. squid_exec_t
- This type is used for the squid binary,
/usr/sbin/squid
. squid_log_t
- This type is used for logs. Existing files, or those created in or copied to
/var/log/squid/
or/var/log/squidGuard/
must be labeled with this type. squid_initrc_exec_t
- This type is used for the initialization file required to start
squid
which is located at/etc/rc.d/init.d/squid
. squid_var_run_t
- This type is used by files in
/var/run/
, especially the process id (PID) named/var/run/squid.pid
which is created by squid when it runs.
8.3. Booleans
squid_connect_any
- When enabled, this Boolean permits squid to initiate a connection to a remote host on any port.
Note
~]# semanage boolean -l | grep service_name
8.4. Configuration Examples
8.4.1. Squid Connecting to Non-Standard Ports
- As the root user, install the squid package. Run the
rpm -q squid
command to see if the squid package is installed. If it is not installed, run theyum install squid
command as the root user to install it. - Edit the main configuration file,
/etc/squid/squid.conf
and confirm that thecache_dir
directive is uncommented and looks similar to the following:cache_dir ufs /var/spool/squid 100 16 256
This line specifies the default settings for thecache_dir
directive to be used in this example; it consists of the Squid storage format (ufs), the directory on the system where the cache resides (/var/spool/squid), the amount of disk space in megabytes to be used for the cache (100), and finally the number of first-level and second-level cache directories to be created (16 and 256 respectively). - In the same configuration file, make sure the
http_access allow localnet
directive is uncommented. This allows traffic from thelocalnet
ACL which is automatically configured in a default installation of Squid on Red Hat Enterprise Linux. It will allow client machines on any existing RFC1918 network to have access through the proxy, which is sufficient for this simple example. - In the same configuration file, make sure the
visible_hostname
directive is uncommented and is configured to the host name of the machine. The value should be the fully qualified domain name (FQDN) of the host:visible_hostname squid.example.com
- As the root user, run the
service squid start
command to startsquid
. As this is the first timesquid
has started, this command will initialise the cache directories as specified above in thecache_dir
directive and will then start thesquid
daemon. The output is as follows ifsquid
starts successfully:~]#
/sbin/service squid start
init_cache_dir /var/spool/squid... Starting squid: . [ OK ] - Confirm that the
squid
process ID (PID) has started as a confined service, as seen here by thesquid_var_run_t
value:~]#
ls -lZ /var/run/squid.pid
-rw-r--r--. root squid unconfined_u:object_r:squid_var_run_t:s0 /var/run/squid.pid - At this point, a client machine connected to the
localnet
ACL configured earlier is successfully able to use the internal interface of this host as its proxy. This can be configured in the settings for all common web browsers, or system-wide. Squid is now listening on the default port of the target machine (TCP 3128), but the target machine will only allow outgoing connections to other services on the Internet via common ports. This is a policy defined by SELinux itself. SELinux will deny access to non-standard ports, as shown in the next step: - When a client makes a request using a non-standard port through the Squid proxy such as a website listening on TCP port 10000, a denial similar to the following is logged:
SELinux is preventing the squid daemon from connecting to network port 10000
- To allow this access, the
squid_connect_any
Boolean must be modified, as it is disabled by default. To enable thesquid_connect_any
Boolean, run the following command as the root user:~]#
setsebool -P squid_connect_any on
Note
Do not use the-P
option if you do not wantsetsebool
changes to persist across reboots. - The client will now be able to access non-standard ports on the Internet as Squid is now permitted to initiate connections to any port, on behalf of its clients.
Chapter 9. MySQL
mysqld
) and many client programs and libraries.[7]
rpm -q mysql-server
command to see if the mysql-server package is installed. If it is not installed, run the following command as the root user to install it:
~]# yum install mysql-server
9.1. MySQL and SELinux
- Run the
getenforce
command to confirm SELinux is running in enforcing mode:~]$
getenforce
EnforcingThegetenforce
command returnsEnforcing
when SELinux is running in enforcing mode. - Run the
service mysqld start
command as the root user to startmysqld
:~]#
service mysqld start
Initializing MySQL database: Installing MySQL system tables... [ OK ] Starting MySQL: [ OK ] - Run the
ps -eZ | grep mysqld
command to view themysqld
processes:~]$
ps -eZ | grep mysqld
unconfined_u:system_r:mysqld_safe_t:s0 6035 pts/1 00:00:00 mysqld_safe unconfined_u:system_r:mysqld_t:s0 6123 pts/1 00:00:00 mysqldThe SELinux context associated with themysqld
processes isunconfined_u:system_r:mysqld_t:s0
. The second last part of the context,mysqld_t
, is the type. A type defines a domain for processes and a type for files. In this case, themysqld
processes are running in themysqld_t
domain.
9.2. Types
mysql
. Different types allow you to configure flexible access:
mysqld_db_t
- This type is used for the location of the MySQL database. In Red Hat Enterprise Linux, the default location for the database is
/var/lib/mysql/
, however this can be changed. If the location for the MySQL database is changed, the new location must be labeled with this type. Refer to the following example for instructions on how to change the default database location and how to label the new section appropriately. mysqld_etc_t
- This type is used for the MySQL main configuration file
/etc/my.cnf
and any other configuration files in the/etc/mysql/
directory. mysqld_exec_t
- This type is used for the
mysqld
binary located at/usr/libexec/mysqld
, which is the default location for the MySQL binary on Red Hat Enterprise Linux. Other systems may locate this binary at/usr/sbin/mysqld
which should also be labeled with this type. mysqld_initrc_exec_t
- This type is used for the initialization file for MySQL, located at
/etc/rc.d/init.d/mysqld
by default in Red Hat Enterprise Linux. mysqld_log_t
- Logs for MySQL need to be labeled with this type for proper operation. All log files in
/var/log/
matching themysql.*
wildcard must be labeled with this type. mysqld_var_run_t
- This type is used by files in
/var/run/mysqld/
, specifically the process id (PID) named/var/run/mysqld/mysqld.pid
which is created by themysqld
daemon when it runs. This type is also used for related socket files such as/var/lib/mysql/mysql.sock
. Files such as these must be labeled correctly for proper operation as a confined service.
9.3. Booleans
allow_user_mysql_connect
- When enabled, this Boolean allows users to connect to MySQL.
exim_can_connect_db
- When enabled, this Boolean allows the
exim
mailer to initiate connections to a database server. ftpd_connect_db
- When enabled, this Boolean allows
ftp
daemons to initiate connections to a database server. httpd_can_network_connect_db
- Enabling this Boolean is required for a web server to communicate with a database server.
Note
~]# semanage boolean -l | grep service_name
9.4. Configuration Examples
9.4.1. MySQL Changing Database Location
/var/lib/mysql/
. This is where SELinux expects it to be by default, and hence this area is already labeled appropriately for you, using the mysqld_db_t
type.
auditd
service is running, and that there is a valid database in the default location of /var/lib/mysql/
.
- Run the
ls -lZ /var/lib/mysql
command to view the SELinux context of the default database location formysql
:~]#
ls -lZ /var/lib/mysql
drwx------. mysql mysql unconfined_u:object_r:mysqld_db_t:s0 mysqlThis showsmysqld_db_t
which is the default context element for the location of database files. This context will have to be manually applied to the new database location that will be used in this example in order for it to function properly. - Enter
mysqlshow -u root -p
and enter themysqld
root password to show the available databases:~]#
mysqlshow -u root -p
Enter password: ******* +--------------------+ | Databases | +--------------------+ | information_schema | | mysql | | test | | wikidb | +--------------------+ - Shut down the
mysqld
daemon with theservice mysqld stop
command as the root user:~]#
service mysqld stop
Stopping MySQL: [ OK ] - Create a new directory for the new location of the database(s). In this example,
/mysql/
is used:~]#
mkdir -p /mysql
- Copy the database files from the old location to the new location:
~]#
cp -R /var/lib/mysql/* /mysql/
- Change the ownership of this location to allow access by the mysql user and group. This sets the traditional Unix permissions which SELinux will still observe.
~]#
chown -R mysql:mysql /mysql
- Run the
ls -lZ /opt
command to see the initial context of the new directory:~]#
ls -lZ /opt
drwxr-xr-x. mysql mysql unconfined_u:object_r:usr_t:s0 mysqlThe contextusr_t
of this newly created directory is not currently suitable to SELinux as a location for MySQL database files. Once the context has been changed, MySQL will be able to function properly in this area. - Open the main MySQL configuration file
/etc/my.cnf
with a text editor and modify thedatadir
option so that it refers to the new location. In this example the value that should be entered is/mysql
.[mysqld] datadir=/mysql
Save this file and exit. - Run the
service mysqld start
command as the root user to startmysqld
. The service should fail to start, and a denial will be logged to the/var/log/messages
file. However, if theaudit
daemon is running alongside thesetroubleshoot
service, the denial will be logged to the/var/log/audit/audit.log
file instead:SELinux is preventing /usr/libexec/mysqld "write" access on /mysql. For complete SELinux messages. run sealert -l b3f01aff-7fa6-4ebe-ad46-abaef6f8ad71
The reason for this denial is that/mysql/
is not labeled correctly for MySQL data files. SELinux is stopping MySQL from having access to the content labeled asusr_t
. Perform the following steps to resolve this problem: - Run the following
semanage
command to add a context mapping for/mysql
. Note thatsemanage
is not installed by default. If it is missing on your system, install the policycoreutils-python package.~]#
semanage fcontext -a -t mysqld_db_t "/mysql(/.*)?"
- This mapping is written to the
/etc/selinux/targeted/contexts/files/file_contexts.local
file:~]#
grep -i mysql /etc/selinux/targeted/contexts/files/file_contexts.local
/mysql(/.*)? system_u:object_r:mysqld_db_t:s0 - Now use the
restorecon
command to apply this context mapping to the running system:~]#
restorecon -R -v /mysql
- Now that the
/mysql/
location has been labeled with the correct context for MySQL, themysqld
daemon starts:~]#
service mysqld start
Starting MySQL: [ OK ] - Confirm the context has changed for
/mysql/
:~]$
ls -lZ /opt
drwxr-xr-x. mysql mysql system_u:object_r:mysqld_db_t:s0 mysql - The location has been changed and labeled, and the
mysqld
daemon has started successfully. At this point all running services should be tested to confirm normal operation.
Chapter 10. PostgreSQL
rpm -q postgresql-server
command to see if the postgresql-server package is installed. If it is not installed, run the following command as the root user to install it:
~]# yum install postgresql-server
10.1. PostgreSQL and SELinux
- Run the
getenforce
command to confirm SELinux is running in enforcing mode:~]$
getenforce
EnforcingThegetenforce
command returnsEnforcing
when SELinux is running in enforcing mode. - Run the
service postgresql start
command as the root user to startpostgresql
:~]#
service postgresql start
Starting postgresql service: [ OK ] - Run the
ps -eZ | grep postgres
command to view thepostgresql
processes:~]$
ps -eZ | grep postgres
unconfined_u:system_r:postgresql_t:s0 395 ? 00:00:00 postmaster unconfined_u:system_r:postgresql_t:s0 397 ? 00:00:00 postmaster unconfined_u:system_r:postgresql_t:s0 399 ? 00:00:00 postmaster unconfined_u:system_r:postgresql_t:s0 400 ? 00:00:00 postmaster unconfined_u:system_r:postgresql_t:s0 401 ? 00:00:00 postmaster unconfined_u:system_r:postgresql_t:s0 402 ? 00:00:00 postmasterThe SELinux context associated with thepostgresql
processes isunconfined_u:system_r:postgresql_t:s0
. The second last part of the context,postgresql_t
, is the type. A type defines a domain for processes and a type for files. In this case, thepostgresql
processes are running in thepostgresql_t
domain.
10.2. Types
postgresql
. Different types allow you to configure flexible access:
postgresql_db_t
- This type is used for several locations. The locations labeled with this type are used for data files for PostgreSQL:
/usr/lib/pgsql/test/regres
/usr/share/jonas/pgsql
/var/lib/pgsql/data
/var/lib/postgres(ql)?
postgresql_etc_t
- This type is used for configuration files in
/etc/postgresql/
. postgresql_exec_t
- This type is used for several locations. The locations labeled with this type are used for binaries for PostgreSQL:
/usr/bin/initdb(.sepgsql)?
/usr/bin/(se)?postgres
/usr/lib(64)?/postgresql/bin/.*
/usr/lib/phsql/test/regress/pg_regress
postgresql_initrc_exec_t
- This type is used for the PostgreSQL initialization file located at
/etc/rc.d/init.d/postgresql
. postgresql_log_t
- This type is used for several locations. The locations labeled with this type are used for log files:
/var/lib/pgsql/logfile
/var/lib/pgsql/pgstartup.log
/var/lib/sepgsql/pgstartup.log
/var/log/postgresql
/var/log/postgres.log.*
/var/log/rhdb/rhdb
/var/log/sepostgresql.log.*
postgresql_var_run_t
- This type is used for run-time files for PostgreSQL, such as the process id (PID) in
/var/run/postgresql/
.
10.3. Booleans
allow_user_postgresql_connect
- Having this Boolean enabled allows any user domain (as defined by PostgreSQL) to make connections to the database server.
postgresql_can_rsync
- When enabled, this Boolean allows PostgreSQL to use the SSH protocol and the
rsync
utility.
Note
~]# semanage boolean -l | grep service_name
10.4. Configuration Examples
10.4.1. PostgreSQL Changing Database Location
/var/lib/pgsql/data
. This is where SELinux expects it to be by default, and hence this area is already labeled appropriately for you, using the postgresql_db_t
type.
- Run the
ls -lZ /var/lib/pgsql
command to view the SELinux context of the default database location forpostgresql
:~]#
ls -lZ /var/lib/pgsql
drwx------. postgres postgres system_u:object_r:postgresql_db_t:s0 dataThis showspostgresql_db_t
which is the default context element for the location of database files. This context will have to be manually applied to the new database location that will be used in this example in order for it to function properly. - Create a new directory for the new location of the database(s). In this example,
/opt/postgresql/data/
is used. If you use a different location, replace the text in the following steps with your location:~]#
mkdir -p /opt/postgresql/data
- Perform a directory listing of the new location. Note that the initial context of the new directory is usr_t. This context is not sufficient for SELinux to offer its protection mechanisms to PostgreSQL. Once the context has been changed, it will be able to function properly in the new area.
~]#
ls -lZ /opt/postgresql/
drwxr-xr-x. root root unconfined_u:object_r:usr_t:s0 data - Change the ownership of the new location to allow access by the postgres user and group. This sets the traditional Unix permissions which SELinux will still observe.
~]#
chown -R postgres:postgres /opt/postgresql
- Open the PostgreSQL init file
/etc/rc.d/init.d/postgresql
with a text editor and modify thePGDATA
andPGLOG
variables to point to the new location:~]#
vi /etc/rc.d/init.d/postgresql
PGDATA=/opt/postgresql/data PGLOG=/opt/postgresql/data/pgstartup.logSave this file and exit the text editor. - Initialize the database in the new location.
~]$
su - postgres -c "initdb -D /opt/postgresql/data"
- Having changed the database location, starting the service will fail at this point:
~]#
service postgresql start
Starting postgresql service: [FAILED]SELinux has caused the service to not start. This is because the new location is not properly labelled. The following steps explain how to label the new location (/opt/postgresql/
) and start the postgresql service properly: - Run the
semanage
command to add a context mapping for/opt/postgresql/
and any other directories/files within it:~]#
semanage fcontext -a -t postgresql_db_t "/opt/postgresql(/.*)?"
- This mapping is written to the
/etc/selinux/targeted/contexts/files/file_contexts.local
file:~]#
grep -i postgresql /etc/selinux/targeted/contexts/files/file_contexts.local
/opt/postgresql(/.*)? system_u:object_r:postgresql_db_t:s0 - Now use the
restorecon
command to apply this context mapping to the running system:~]#
restorecon -R -v /opt/postgresql
- Now that the
/opt/postgresql/
location has been labeled with the correct context for PostgreSQL, thepostgresql
service will start successfully:~]#
service postgresql start
Starting postgreSQL service: [ OK ] - Confirm the context is correct for
/opt/postgresql/
:~]$
ls -lZ /opt
drwxr-xr-x. root root system_u:object_r:postgresql_db_t:s0 postgresql - Check with the
ps
command that thepostgresql
process displays the new location:~]#
ps aux | grep -i postmaster
postgres 21564 0.3 0.3 42308 4032 ? S 10:13 0:00 /usr/bin/postmaster -p 5432 -D /opt/postgresql/data/ - The location has been changed and labeled, and the
postgresql
daemon has started successfully. At this point all running services should be tested to confirm normal operation.
Chapter 11. rsync
rsync
utility performs fast file transfer and it is used for synchronizing data between systems. [9]
rpm -q rsync
command to see if the rsync package is installed. If it is not installed, run the following command as the root user to install it:
~]# yum install rsync
11.1. rsync and SELinux
rsync
daemon, you must label the files and directories with the public_content_t
type. Like most services, correct labeling is required for SELinux to perform its protection mechanisms over rsync
.[10]
11.2. Types
rsync
. Different types all you to configure flexible access:
public_content_t
- This is a generic type used for the location of files (and the actual files) to be shared via
rsync
. If a special directory is created to house files to be shared withrsync
, the directory and its contents need to have this label applied to them. rsync_exec_t
- This type is used for the
/usr/bin/rsync
system binary. rsync_log_t
- This type is used for the
rsync
log file, located at/var/log/rsync.log
by default. To change the location of the file rsync logs to, use the--log-file=FILE
option to thersync
command at run-time. rsync_var_run_t
- This type is used for the
rsyncd
lock file, located at/var/run/rsyncd.lock
. This lock file is used by thersync
server to manage connection limits. rsync_data_t
- This type is used for files and directories which you want to use as rsync domains and isolate them from the access scope of other services. Also, the
public_content_t
is a general SELinux context type, which can be used when a file or a directory interacts with multiple services (for example, FTP and NFS directory as an rsync domain). rsync_etc_t
- This type is used for rsync-related files in the
/etc/
directory.
11.3. Booleans
allow_rsync_anon_write
- Having this Boolean enabled allows
rsync
in the rsync_t domain to manage files, links and directories that have a type of public_content_rw_t. Often these are public files used for public file transfer services. Files and directories must be labeledpublic_content_rw_t
. rsync_client
- Having this Boolean enabled allows
rsync
to initiate connections to ports defined as rsync_port_t, as well as allowingrsync
to manage files, links and directories that have a type of rsync_data_t. Note that thersync
daemon must be in the rsync_t domain in order for SELinux to enact its control overrsync
. The configuration example in this chapter demonstratesrsync
running in the rsync_t domain. rsync_export_all_ro
- Having this Boolean enabled allows
rsync
in the rsync_t domain to export NFS and CIFS volumes with read-only access to clients.
Note
~]# semanage boolean -l | grep service_name
11.4. Configuration Examples
11.4.1. Rsync as a daemon
rsync
daemon to run normally on a non-standard port.
Procedure 11.1. Getting rsync to launch as rsync_t
- Run the
getenforce
command to confirm SELinux is running in enforcing mode:~]$
getenforce
EnforcingThegetenforce
command returnsEnforcing
when SELinux is running in enforcing mode. - Run the
which
command to confirm that the rsync binary is in the system path:~]$
which rsync
/usr/bin/rsync - When running
rsync
as a daemon, a configuration file should be used and saved as/etc/rsyncd.conf
. Note that the following configuration file used in this example is very simple and is not indicative of all the possible options that are available, rather it is just enough to demonstrate thersync
daemon:log file = /var/log/rsync.log pid file = /var/run/rsyncd.pid lock file = /var/run/rsync.lock [files] path = /srv/files comment = file area read only = false timeout = 300
- Now that a simple configuration file exists for rsync to operate in daemon mode, this step demonstrates that simply running the
rsync --daemon
command is not sufficient for SELinux to offer its protection over rsync. See the following output:~]#
rsync --daemon
~]#ps x | grep rsync
8231 ? Ss 0:00 rsync --daemon 8233 pts/3 S+ 0:00 grep rsync ~]#ps -eZ | grep rsync
unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 8231 ? 00:00:00 rsyncNote that in the output from the finalps
command, the context shows thersync
daemon running in theunconfined_t
domain. This indicates that rsync has not transitioned to thersync_t
domain as it was launched by thersync --daemon
command. At this point, SELinux cannot enforce its rules and policy over this daemon. See the following steps to see how to fix this problem.In the following steps,rsync
transitions to thersync_t
domain because it launched it from a properly-labeled init script. Only then can SELinux and its protection mechanisms have an effect overrsync
. Thisrsync
process should be killed before proceeding to the next step. - A custom init script for rsync is needed for this step. Save the following to
/etc/rc.d/init.d/rsyncd
.#!/bin/bash # Source function library. . /etc/rc.d/init.d/functions [ -f /usr/bin/rsync ] || exit 0 case "$1" in start) action "Starting rsyncd: " /usr/bin/rsync --daemon ;; stop) action "Stopping rsyncd: " killall rsync ;; *) echo "Usage: rsyncd {start|stop}" exit 1 esac exit 0
The following steps show how to label this script asinitrc_exec_t
: - Run the
semanage
command to add a context mapping for/etc/rc.d/init.d/rsyncd
:~]#
semanage fcontext -a -t initrc_exec_t "/etc/rc.d/init.d/rsyncd"
- This mapping is written to the
/etc/selinux/targeted/contexts/files/file_contexts.local
file:~]#
grep rsync /etc/selinux/targeted/contexts/files/file_contexts.local
/etc/rc.d/init.d/rsyncd system_u:object_r:initrc_exec_t:s0 - Now use the
restorecon
command to apply this context mapping to the running system:~]#
restorecon -R -v /etc/rc.d/init.d/rsyncd
- Run the
ls -lZ
command to confirm the script has been labeled appropriately. Note that in the following output, the script has been labeled asinitrc_exec_t
:~]$
ls -lZ /etc/rc.d/init.d/rsyncd
-rwxr-xr-x. root root system_u:object_r:initrc_exec_t:s0 /etc/rc.d/init.d/rsyncd - Turn on the
rsync_server
SELinux boolean:~]#
setsebool rsync_server on
Note that this setting is not permanent and as such, it will revert to its original state after a reboot. To make the setting permanent, use the-P
option with thesetsebool
command. - Launch
rsyncd
via the new script. Now that rsync has started from an init script that had been appropriately labeled, the process has started asrsync_t
:~]#
service rsyncd start
Starting rsyncd: [ OK ] $ ps -eZ | grep rsync unconfined_u:system_r:rsync_t:s0 9794 ? 00:00:00 rsyncSELinux can now enforce its protection mechanisms over thersync
daemon as it is now runing in thersync_t
domain.
rsyncd
running in the rsync_t
domain. The next example shows how to get this daemon successfully running on a non-default port. TCP port 10000 is used in the next example.
Procedure 11.2. Running the rsync daemon on a non-default port
- Modify the
/etc/rsyncd.conf
file and add theport = 10000
line at the top of the file in the global configuration area (that is, before any file areas are defined). The new configuration file will look like:log file = /var/log/rsyncd.log pid file = /var/run/rsyncd.pid lock file = /var/run/rsync.lock port = 10000 [files] path = /srv/files comment = file area read only = false timeout = 300
- After launching rsync from the init script with this new setting, a denial similar to the following is logged by SELinux:
Jul 22 10:46:59 localhost setroubleshoot: SELinux is preventing the rsync (rsync_t) from binding to port 10000. For complete SELinux messages, run sealert -l c371ab34-639e-45ae-9e42-18855b5c2de8
- Run
semanage
to add TCP port 10000 to SELinux policy inrsync_port_t
:~]#
semanage port -a -t rsync_port_t -p tcp 10000
- Now that TCP port 10000 has been added to SELinux policy for
rsync_port_t
,rsyncd
will start and operate normally on this port:~]#
service rsyncd start
Starting rsyncd: [ OK ]~]#
netstat -lnp | grep 10000
tcp 0 0 0.0.0.0:10000 0.0.0.0:* LISTEN 9910/rsync
rsyncd
to operate on TCP port 10000.
Chapter 12. Postfix
rpm -q postfix
command to see if the postfix package is installed. If it is not installed, run the following command as the root user to install it:
~]# yum install postfix
12.1. Postfix and SELinux
- Run the
getenforce
command to confirm SELinux is running in enforcing mode:~]$
getenforce
EnforcingThegetenforce
command returnsEnforcing
when SELinux is running in enforcing mode. - Run the
service postfix start
command as the root user to startpostfix
:~]#
service postfix start
Starting postfix: [ OK ] - Run the
ps -eZ | grep postfix
command to view thepostfix
processes:~]$
ps -eZ | grep postfix
system_u:system_r:postfix_master_t:s0 1651 ? 00:00:00 master system_u:system_r:postfix_pickup_t:s0 1662 ? 00:00:00 pickup system_u:system_r:postfix_qmgr_t:s0 1663 ? 00:00:00 qmgrIn the output above, the SELinux context associated with the Postfixmaster
process issystem_u:system_r:postfix_master_t:s0
. The second last part of the context,postfix_master_t
, is the type for this process. A type defines a domain for processes and a type for files. In this case, themaster
process is running in thepostfix_master_t
domain.
12.2. Types
postfix_etc_t
- This type is used for configuration files for Postfix in the
/etc/postfix/
directory. postfix_data_t
- This type is used for Postfix data files in the
/var/lib/postfix/
directory. postfix_var_run_t
- This type is used for Postfix files stored in the
/run/
directory. postfix_initrc_exec_t
- This type is used for transition of Postfix executable files to the
postfix_initrc_t
domain. postfix_spool_t
- This type is used for Postfix files stored in the
/var/spool/
directory.
Note
~]$ grep postfix /etc/selinux/targeted/contexts/files/file_contexts
12.3. Booleans
allow_postfix_local_write_mail_spool
- Having this Boolean enables Postfix to write to the local mail spool on the system. Postfix requires this Boolean to be enabled for normal operation when local spools are used.
Note
~]# semanage boolean -l | grep service_name
12.4. Configuration Examples
12.4.1. SpamAssassin and Postfix
rpm -q spamassassin
command to see if the spamassassin package is installed. If it is not installed, run the following command as the root user to install it:
~]# yum install spamassassin
Procedure 12.1. Running SpamAssassin on a non-default port
- Run the
semanage
command to show the port that SELinux allowsspamd
to listen on by default:~]#
semanage port -l | grep spamd
spamd_port_t tcp 783This output shows that TCP/783 is defined inspamd_port_t
as the port for SpamAssassin to operate on. - Edit the
/etc/sysconfig/spamassassin
configuration file and modify it so that it will start SpamAssassin on the example port TCP/10000:# Options to spamd SPAMDOPTIONS="-d -p 10000 -c m5 -H"
This line now specifies that SpamAssassin will operate on port 10000. The rest of this example will show how to modify SELinux policy to allow this socket to be opened. - Start SpamAssassin and an error message similar to the following will appear:
~]#
service spamassassin start
Starting spamd: [2203] warn: server socket setup failed, retry 1: spamd: could not create INET socket on 127.0.0.1:10000: Permission denied [2203] warn: server socket setup failed, retry 2: spamd: could not create INET socket on 127.0.0.1:10000: Permission denied [2203] error: spamd: could not create INET socket on 127.0.0.1:10000: Permission denied spamd: could not create INET socket on 127.0.0.1:10000: Permission denied [FAILED]This output means that SELinux has blocked access to this port. - A denial similar to the following will be logged by SELinux:
SELinux is preventing the spamd (spamd_t) from binding to port 10000.
- As the root user, run
semanage
to modify SELinux policy in order to allow SpamAssassin to operate on the example port (TCP/10000):~]#
semanage port -a -t spamd_port_t -p tcp 10000
- Confirm that SpamAssassin will now start and is operating on TCP port 10000:
~]#
service spamassassin start
Starting spamd: [ OK ] ~]#netstat -lnp | grep 10000
tcp 0 0 127.0.0.1:10000 0.0.0.0:* LISTEN 2224/spamd.pid - At this point,
spamd
is properly operating on TCP port 10000 as it has been allowed access to that port by SELinux policy.
Chapter 13. DHCP
dhcpd
. Run the rpm -q dhcp
command to see if the dhcp package is installed. If it is not installed, run the following command as the root user to install it:
~]# yum install dhcp
13.1. DHCP and SELinux
- Run the
getenforce
command to confirm SELinux is running in enforcing mode:~]$
getenforce
EnforcingThegetenforce
command returnsEnforcing
when SELinux is running in enforcing mode. - Run the
service dhcpd start
command as the root user to startDHCPD
:~]#
service dhcpd start
Starting dhcpd: [ OK ] - Run the
ps -eZ | grep dhcpd
command to view thedhcpd
processes:~]$
ps -eZ | grep dhcpd
unconfined_u:system_r:dhcpd_t:s0 5483 ? 00:00:00 dhcpdThe SELinux context associated with the dhcpd process isunconfined_u:system_r:dhcpd_t:s0
.
13.2. Types
dhcpd
:
dhcp_etc_t
- This type is mainly used for files in
/etc
, including configuration files. dhcpd_var_run_t
- This type is used for the PID file for dhcpd, in
/var/run
. dhcpd_exec_t
- This type is used for transition of DHCP executable files to the
dhcpd_t
domain. dhcpd_initrc_exec_t
- This type is used for transition of DHCP executable files to the
dhcpd_initrc_t
domain.
Note
~]$ grep dhcp /etc/selinux/targeted/contexts/files/file_contexts
Chapter 14. OpenShift by Red Hat
14.1. OpenShift and SELinux
14.2. Types
Process types
openshift_t
- The OpenShift process is associated with the
openshift_t
SELinux type.
Types on executables
openshift_cgroup_read_exec_t
- SELinux allows files with this type to transition an executable to the
openshift_cgroup_read_t
domain. openshift_cron_exec_t
- SELinux allows files with this type to transition an executable to the
openshift_cron_t
domain. openshift_initrc_exec_t
- SELinux allows files with this type to transition an executable to the
openshift_initrc_t
domain.
Writable types
openshift_cgroup_read_tmp_t
- This type allows OpenShift control groups (cgroup) read and access temporary files in the
/tmp/
directory. openshift_cron_tmp_t
- This type allows storing temporary files of the OpenShift cron jobs in
/tmp/
. openshift_initrc_tmp_t
- This type allows storing the OpenShift
initrc
temporary files in/tmp/
. openshift_log_t
- Files with this type are treated as OpenShift log data, usually stored under the
/var/log/
directory. openshift_rw_file_t
- OpenShift have permission to read and to write to files labeled with this type.
openshift_tmp_t
- This type is used for storing the OpenShift temporary files in
/tmp/
. openshift_tmpfs_t
- This type allows storing the OpenShift data on a tmpfs file system.
openshift_var_lib_t
- This type allows storing the OpenShift files in the
/var/lib/
directory. openshift_var_run_t
- This type allows storing the OpenShift files in the
/run/
or/var/run/
directory.
14.3. Booleans
openshift_use_nfs
- Having this Boolean enabled allows installing OpenShift on an NFS share.
Note
~]# semanage boolean -l | grep service_name
14.4. Configuration Examples
14.4.1. Changing the Default OpenShift Directory
/var/lib/openshift/
directory, which is labeled with the openshift_var_lib_t
SELinux type. To allow OpenShift to store data in a different directory, label the new directory with the proper SELinux context.
/srv/openshift/
:
Procedure 14.1. Changing the Default OpenShift Directory for Storing Data
- As root, create a new
/openshift/
directory within the/srv/
directory. The new directory is labeled with thevar_t
type:~]#
mkdir /srv/openshift
~]$
ls -Zd /srv/openshift
drwxr-xr-x. root root unconfined_u:object_r:var_t:s0 openshift/ - As root, use the
semanage
utility to map/srv/openshift/
to the proper SELinux context:~]#
semanage fcontext -a -e /var/lib/openshift /srv/openshift
- Then, use the
restorecon
utility as root to apply the changes:~]#
restorecon -R -v /srv/openshift
- The
/srv/openshift/
directory is now labeled with the correctopenshift_var_lib_t
type:~]$
ls -Zd /srv/openshift
drwxr-xr-x. root root unconfined_u:object_r:openshift_var_lib_t:s0 openshift/
Chapter 15. Red Hat Gluster Storage
15.1. Red Hat Gluster Storage and SELinux
glusterd
(GlusterFS Management Service) and glusterfsd
(NFS server) processes as a part of Red Hat Gluster Storage. These processes have advanced process isolation unbounded with the glusterd_t
SELinux type.
15.2. Types
Process types
glusterd_t
- The Gluster processes are associated with the
glusterd_t
SELinux type.
Types on executables
glusterd_initrc_exec_t
- The SELinux-specific script type context for the Gluster init script files.
glusterd_exec_t
- The SELinux-specific executable type context for the Gluster executable files.
Port Types
gluster_port_t
- This type is defined for
glusterd
. By default,glusterd
uses 204007-24027, and 38465-38469 TCP ports.
File Contexts
glusterd_brick_t
- This type is used for files threated as
glusterd
brick data. glusterd_conf_t
- This type is associated with the
glusterd
configuration data, usually stored in the/etc/
directory. glusterd_log_t
- Files with this type are treated as
glusterd
log data, usually stored under the/var/log/
directory. glusterd_tmp_t
- This type is used for storing the
glusterd
temporary files in the/tmp/
directory. glusterd_var_lib_t
- This type allows storing the
glusterd
files in the/var/lib/
directory. glusterd_var_run_t
- This type allows storing the
glusterd
files in the/run/
or/var/run/
directory.
15.3. Booleans
gluster_export_all_ro
- Having this Boolean enabled allows
glusterfsd
to share files and directory as read-only. This Boolean is disabled by default. gluster_export_all_rw
- Having this Boolean enabled allows
glusterfsd
to share files and directories with read and write access. This Boolean is enabled by default. gluster_anon_write
- Having this Boolean enabled allows
glusterfsd
to modify public files labeled with thepublic_content_rw_t
SELinux type.
Note
~]# semanage boolean -l | grep service_name
15.4. Configuration Examples
15.4.1. Labeling Gluster Bricks
glusterd_brick_t
, SELinux denies certain file access operations and generates various AVC messages.
/dev/rhgs/gluster
, to be used as the Gluster brick.
Procedure 15.1. How to Label a Gluster Brick
- Create a directory to mount the previously formatted logical volume. For example:
~]#
mkdir /mnt/brick1
- Mount the logical volume, in this case
/dev/vg-group/gluster
, to the/mnt/brick1/
directory created in the previous step.~]#
mount /dev/vg-group/gluster /mnt/brick1/
Note that themount
command mounts devices only temporarily. To mount the device permanently, add an entry similar as the following one to the/etc/fstab
file:/dev/vg-group/gluster /mnt/brick1 xfs rw,inode64,noatime,nouuid 1 2
For more information, see the fstab(5) manual page. - Check the SELinux context of
/mnt/brick1/
:~]$
ls -lZd /mnt/brick1/
drwxr-xr-x. root root system_u:object_r:unlabeled_t:s0 /mnt/brick1/The directory is labeled with theunlabeled_t
SELinux type. - Change the SELinux type of
/mnt/brick1/
to theglusterd_brick_t
SELinux type:~]#
semanage fcontext -a -t glusterd_brick_t "/mnt/brick1(/.*)?"
- Use the
restorecon
utility to apply the changes:~]#
restorecon -Rv /mnt/brick1
- Finally, verify that the context has been successfully changed:
~]$
ls -lZd /mnt/brick1
drwxr-xr-x. root root system_u:object_r:glusterd_brick_t:s0 /mnt/brick1/
Chapter 16. References
Books
- SELinux by Example
- Mayer, MacMillan, and CaplanPrentice Hall, 2007
- SELinux: NSA's Open Source Security Enhanced Linux
- Bill McCartyO'Reilly Media Inc., 2004
Tutorials and Help
- Tutorials and talks from Russell Coker
- Dan Walsh's Journal
- Red Hat Knowledgebase
General Information
- NSA SELinux main website
- NSA SELinux FAQ
Mailing Lists
- NSA SELinux mailing list
- Fedora SELinux mailing list
Community
- SELinux Project Wiki
- SELinux community page
- IRC
- irc.freenode.net, #selinux
Appendix A. Revision History
Revision History | |||
---|---|---|---|
Revision 9-1 | Wed Mar 15 2017 | ||
| |||
Revision 9-0 | Wed Dec 21 2016 | ||
| |||
Revision 8-0 | Wed May 3 2016 | ||
| |||
Revision 7-1 | Thu Jul 9 2015 | ||
| |||
Revision 6-0 | Fri Oct 10 2014 | ||
| |||
Revision 5-0 | Tue Nov 19 2013 | ||
| |||
Revision 4-0 | Fri Feb 22 2013 | ||
| |||
Revision 3-0 | Wed Jun 20 2012 | ||
| |||
Revision 2-0 | Tue Dec 6 2011 | ||
| |||
Revision 1-0 | Thu May 19 2011 | ||
| |||
Revision 0-0 | Tue Nov 9 2010 | ||
|