]> granicus.if.org Git - icinga2/blob - doc/21-selinux.md
Docs: Add note on percentage values for disk-windows thresholds
[icinga2] / doc / 21-selinux.md
1 # <a id="selinux"></a> SELinux
2
3 ## <a id="selinux-introduction"></a> Introduction
4
5 SELinux is a mandatory access control (MAC) system on Linux which adds a fine-grained permission system for access to all system resources such as files, devices, networks and inter-process communication.
6
7 The most important questions are answered briefly in the [FAQ of the SELinux Project](http://selinuxproject.org/page/FAQ). For more details on SELinux and how to actually use and administrate it on your system have a look at [Red Hat Enterprise Linux 7 - SELinux User's and Administrator's Guide](https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/7/html/SELinux_Users_and_Administrators_Guide/index.html). For a simplified (and funny) introduction download the [SELinux Coloring Book](https://github.com/mairin/selinux-coloring-book).
8
9 This documentation will use a format similar to the SELinux User's and Administrator's Guide.
10
11 ### <a id="selinux-policy"></a> Policy
12
13 Icinga 2 provides its own SELinux policy. Development target is a policy package for Red Hat Enterprise Linux 7 and derivatives running the targeted policy which confines Icinga 2 with all features and all checks executed. All other distributions will require some tweaks.
14
15 ### <a id="selinux-policy-installation"></a> Installation
16
17 There are two ways of installing the SELinux Policy for Icinga 2 on Enterprise Linux 7. The preferred way is to install the package. The other option involves installing the SELinux policy manually which might be necessary if you need some fixes which haven't made their way into a release yet.
18
19 If the system runs in enforcing mode and you encounter problems you can set Icinga 2's domain to permissive mode.
20
21     # sestatus
22     SELinux status:                 enabled
23     SELinuxfs mount:                /sys/fs/selinux
24     SELinux root directory:         /etc/selinux
25     Loaded policy name:             targeted
26     Current mode:                   enforcing
27     Mode from config file:          enforcing
28     Policy MLS status:              enabled
29     Policy deny_unknown status:     allowed
30     Max kernel policy version:      28
31
32 You can change the configured mode by editing `/etc/selinux/config` and the current mode by executing `setenforce 0`.
33
34 #### <a id="selinux-policy-installation-package"></a> Package installation
35
36 Simply add the `icinga2-selinux` package to your installation.
37
38     # yum install icinga2-selinux
39
40 Ensure that the `icinga2` process is running in its own `icinga2_t` domain after installing the policy package:
41
42     # systemctl restart icinga2.service
43     # ps -eZ | grep icinga2
44     system_u:system_r:icinga2_t:s0   2825 ?        00:00:00 icinga2
45
46 #### <a id="selinux-policy-installation-manual"></a> Manual installation
47
48 This section describes the installation to support development and testing. It assumes that Icinga 2 is already installed from packages and running on the system.
49
50 As a prerequisite install the `git`, `selinux-policy-devel` and `audit` packages. Enable and start the audit daemon afterwards:
51
52     # yum install git selinux-policy-devel audit
53     # systemctl enable auditd.service
54     # systemctl start auditd.service
55
56 After that clone the icinga2 git repository:
57
58     # git clone https://github.com/icinga/icinga2
59
60 To create and install the policy package run the installation script which also labels the resources. (The script assumes Icinga 2 was started once after system startup, the labeling of the port will only happen once and fail later on.)
61
62     # cd tools/selinux/
63     # ./icinga.sh
64
65 After that restart Icinga 2 and verify it running in its own domain `icinga2_t`.
66
67     # systemctl restart icinga2.service
68     # ps -eZ | grep icinga2
69     system_u:system_r:icinga2_t:s0   2825 ?        00:00:00 icinga2
70
71 ### <a id="selinux-policy-general"></a> General
72
73 When the SELinux policy package for Icinga 2 is installed, the Icinga 2 daemon (icinga2) runs in its own domain `icinga2_t` and is separated from other confined services.
74
75 Files have to be labeled correctly in order for Icinga 2 to be able to access them. For example the Icinga 2 log files have to have the `icinga2_log_t` label. Also the API port is labeled with `icinga_port_t`. Furthermore Icinga 2 can open high ports and UNIX sockets to connect to databases and features like Graphite. It executes the Nagios plugins and transitions to their context if those are labeled for example `nagios_services_plugin_exec_t` or `nagios_system_plugin_exec_t`.
76
77 Additionally the Apache web server is allowed to connect to Icinga 2's command pipe in order to allow web interfaces to send commands to icinga2. This will perhaps change later on while investigating Icinga Web 2 for SELinux!
78
79 ### <a id="selinux-policy-types"></a> Types
80
81 The command pipe is labeled `icinga2_command_t` and other services can request access to it by using the interface `icinga2_send_commands`.
82
83 The nagios plugins use their own contexts and icinga2 will transition to it. This means plugins have to be labeled correctly for their required permissions. The plugins installed from package should have set their permissions by the corresponding policy module and you can restore them using `restorecon -R -v /usr/lib64/nagios/plugins/`. To label your own plugins use `chcon -t type /path/to/plugin`, for the type have a look at table below.
84
85 Type                              | Domain                       | Use case                                                         | Provided by policy package
86 ----------------------------------|------------------------------|------------------------------------------------------------------|---------------------------
87 nagios_admin_plugin_exec_t        | nagios_admin_plugin_t        | Plugins which require require read access on all file attributes | nagios
88 nagios_checkdisk_plugin_exec_t    | nagios_checkdisk_plugin_t    | Plugins which require read access to all filesystem attributes   | nagios
89 nagios_mail_plugin_exec_t         | nagios_mail_plugin_t         | Plugins which access the local mail service                      | nagios
90 nagios_services_plugin_exec_t     | nagios_services_plugin_t     | Plugins monitoring network services                              | nagios
91 nagios_system_plugin_exec_t       | nagios_system_plugin_t       | Plugins checking local system state                              | nagios
92 nagios_unconfined_plugin_exec_t   | nagios_unconfined_plugin_t   | Plugins running without confinement                              | nagios
93 nagios_eventhandler_plugin_exec_t | nagios_eventhandler_plugin_t | Eventhandler (actually running unconfined)                       | nagios
94 nagios_openshift_plugin_exec_t    | nagios_openshift_plugin_t    | Plugins monitoring openshift                                     | nagios
95 nagios_notification_plugin_exec_t | nagios_notification_plugin_t | Notification commands                                            | icinga (will be moved later)
96
97 If one of those plugin domains causes problems you can set it to permissive by executing `semanage permissive -a domain`.
98
99 The policy provides a role `icinga2adm_r` for confining an user which enables an administrative user managing only Icinga 2 on the system. This user will also execute the plugins in their domain instead of the users one, so you can verify their execution with the same restrictions like they have when executed by icinga2.
100
101 ### <a id="selinux-policy-booleans"></a> Booleans
102
103 SELinux is based on the least level of access required for a service to run. Using booleans you can grant more access in a defined way. The Icinga 2 policy package provides the following booleans.
104
105 **icinga2_can_connect_all** 
106
107 Having this boolean enabled allows icinga2 to connect to all ports. This can be neccesary if you use features which connect to unconfined services.
108
109 **httpd_can_write_icinga2_command** 
110
111 Having this boolean enabled allows httpd to write to the command pipe of icinga2. This is enabled by default, if not needed you can disable it for more security.
112
113 **httpd_can_connect_icinga2_api** 
114
115 Having this boolean enabled allows httpd to connect to the API of icinga2 (Ports labeled icinga2_port_t). This is enabled by default, if not needed you can disable it for more security.
116
117 ### <a id="selinux-policy-examples"></a> Configuration Examples
118
119 #### <a id="selinux-policy-examples-permissive"></a> Run the icinga2 service permissive
120
121 If problems occur while running the system in enforcing mode and those problems are only caused by the policy of the icinga2 domain, you can set this domain to permissive instead of the complete system. This can be done by executing `semanage permissive -a icinga2_t`.
122
123 Make sure to report the bugs in the policy afterwards.
124
125 #### <a id="selinux-policy-examples-plugin"></a> Confining a plugin
126
127 Download and install a plugin, for example check_mysql_health.
128
129     # wget http://labs.consol.de/download/shinken-nagios-plugins/check_mysql_health-2.1.9.2.tar.gz
130     # tar xvzf check_mysql_health-2.1.9.2.tar.gz
131     # cd check_mysql_health-2.1.9.2/
132     # ./configure --libexecdir /usr/lib64/nagios/plugins
133     # make
134     # make install
135
136 It is labeled `nagios_unconfined_plugins_exec_t` by default, so it runs without restrictions.
137
138     # ls -lZ /usr/lib64/nagios/plugins/check_mysql_health
139     -rwxr-xr-x. root root system_u:object_r:nagios_unconfined_plugin_exec_t:s0 /usr/lib64/nagios/plugins/check_mysql_health
140
141 In this case the plugin is monitoring a service, so it should be labeled `nagios_services_plugin_exec_t` to restrict its permissions.
142
143     # chcon -t nagios_services_plugin_exec_t /usr/lib64/nagios/plugins/check_mysql_health
144     # ls -lZ /usr/lib64/nagios/plugins/check_mysql_health
145     -rwxr-xr-x. root root system_u:object_r:nagios_services_plugin_exec_t:s0 /usr/lib64/nagios/plugins/check_mysql_health
146
147 The plugin still runs fine but if someone changes the script to do weird stuff it will fail to do so.
148
149 #### <a id="selinux-policy-examples-connectall"></a> Allow icinga to connect to all ports.
150
151 You are running graphite on a different port than `2003` and want `icinga2` to connect to it.
152
153 Change the port value for the graphite feature according to your graphite installation before enabling it.
154
155     # cat /etc/icinga2/features-enabled/graphite.conf
156     /**
157      * The GraphiteWriter type writes check result metrics and
158      * performance data to a graphite tcp socket.
159      */
160
161     library "perfdata"
162
163     object GraphiteWriter "graphite" {
164       //host = "127.0.0.1"
165       //port = 2003
166       port = 2004
167     }
168     # icinga2 feature enable graphite
169
170 Before you restart the icinga2 service allow it to connect to all ports by enabling the boolean ´icinga2_can_connect_all` (now and permanent).
171
172     # setsebool icinga2_can_connect_all true
173     # setsebool -P icinga2_can_connect_all true
174
175 If you restart the daemon now it will successfully connect to graphite.
176
177 #### <a id="selinux-policy-examples-user"></a> Confining a user
178
179 If you want to have an administrative account capable of only managing icinga2 and not the complete system, you can restrict the privileges by confining
180 this user. This is completly optional!
181
182 Start by adding the Icinga 2 administrator role `icinga2adm_r` to the administrative SELinux user `staff_u`.
183
184     # semanage user -m -R "staff_r sysadm_r system_r unconfined_r icinga2adm_r" staff_u
185
186 Confine your user login and create a sudo rule.
187
188     # semanage login -a dirk -s staff_u
189     # echo "dirk ALL=(ALL) NOPASSWD: ALL" > /etc/sudoers.d/dirk
190
191 Login to the system using ssh and verify your id.
192
193     $ id -Z
194     staff_u:staff_r:staff_t:s0-s0:c0.c1023
195
196 Try to execute some commands as root using sudo.
197
198     $ sudo id -Z
199     staff_u:staff_r:staff_t:s0-s0:c0.c1023
200     $ sudo vi /etc/icinga2/icinga2.conf
201     "/etc/icinga2/icinga2.conf" [Permission Denied]
202     $ sudo cat /var/log/icinga2/icinga2.log
203     cat: /var/log/icinga2/icinga2.log: Keine Berechtigung
204     $ sudo systemctl reload icinga2.service
205     Failed to get D-Bus connection: No connection to service manager.
206
207 Those commands fail because you only switch to root but do not change your SELinux role. Try again but tell sudo also to switch the SELinux role and type.
208
209     $ sudo -r icinga2adm_r -t icinga2adm_t id -Z
210     staff_u:icinga2adm_r:icinga2adm_t:s0-s0:c0.c1023
211     $ sudo -r icinga2adm_r -t icinga2adm_t vi /etc/icinga2/icinga2.conf
212     "/etc/icinga2/icinga2.conf"
213     $ sudo -r icinga2adm_r -t icinga2adm_t cat /var/log/icinga2/icinga2.log
214     [2015-03-26 20:48:14 +0000] information/DynamicObject: Dumping program state to file '/var/lib/icinga2/icinga2.state'
215     $ sudo -r icinga2adm_r -t icinga2adm_t systemctl reload icinga2.service
216
217 Now the commands will work, but you have always to remember to add the arguments, so change the sudo rule to set it by default.
218
219     # echo "dirk ALL=(ALL) ROLE=icinga2adm_r TYPE=icinga2adm_t NOPASSWD: ALL" > /etc/sudoers.d/dirk
220
221 Now try the commands again without providing the role and type and they will work, but if you try to read apache logs or restart apache for example it will still fail.
222
223     $ sudo cat /var/log/httpd/error_log
224     /bin/cat: /var/log/httpd/error_log: Keine Berechtigung
225     $ sudo systemctl reload httpd.service
226     Failed to issue method call: Access denied
227
228 ## <a id="selinux-bugreports"></a> Bugreports
229
230 If you experience any problems while running in enforcing mode try to reproduce it in permissive mode. If the problem persists it is not related to SELinux because in permissive mode SELinux will not deny anything.
231
232 After some feedback Icinga 2 is now running in a enforced domain, but still adds also some rules for other necessary services so no problems should occure at all. But you can help to enhance the policy by testing Icinga 2 running confined by SELinux.
233
234 Please add the following information to [bug reports](https://www.icinga.com/community/get-involved/):
235
236 * Versions, configuration snippets, etc.
237 * Output of `semodule -l | grep -e icinga2 -e nagios -e apache`
238 * Output of `ps -eZ | grep icinga2`
239 * Output of `semanage port -l | grep icinga2`
240 * Output of `audit2allow -li /var/log/audit/audit.log`
241
242 If access to a file is blocked and you can tell which one please provided the output of `ls -lZ /path/to/file` (and perhaps the directory above).
243
244 If asked for full audit.log add `-w /etc/shadow -p w` to `/etc/audit/rules.d/audit.rules`, restart the audit daemon, reproduce the problem and add `/var/log/audit/audit.log` to the bug report. With the added audit rule it will include the path of files access was denied to.
245
246 If asked to provide full audit log with dontaudit rules disabled executed `semodule -DB` before reproducing the problem. After that enable the rules again to prevent auditd spamming your logfile by executing `semodule -B`.