|
| 1 | +# SELinux |
| 2 | + |
| 3 | +https://www.redhat.com/en/topics/linux/what-is-selinux |
| 4 | + |
| 5 | +# Table of Contents |
| 6 | +- [Prerequisites](#prerequisites) |
| 7 | +- [Enable SELinux](#enable-selinux) |
| 8 | +- [Install NGINX Agent Policy](#install-nginx-agent-policy) |
| 9 | +- [Updating existing policy](#updating-existing-policy) |
| 10 | +- [Troubleshooting](#troubleshooting) |
| 11 | + - [Policy version does not match](#policy-version-does-not-match) |
| 12 | + - [Unknown Type](#unknown-type) |
| 13 | +- [Debugging](#debugging) |
| 14 | +- [References](#references) |
| 15 | + |
| 16 | +## Prerequisites |
| 17 | +``` |
| 18 | +sudo yum install policycoreutils-devel rpm-build |
| 19 | +``` |
| 20 | + |
| 21 | +## Enable SELinux |
| 22 | +To enable SELinux, update the file `/etc/selinux/config` by setting `SELINUX=enforcing`. Then reboot the machine for the change to take affect. |
| 23 | + |
| 24 | +To validate that SELinux is enabled run the following command: |
| 25 | +``` |
| 26 | +sestatus |
| 27 | +``` |
| 28 | +The output should look something like this: |
| 29 | +``` |
| 30 | +SELinux status: enabled |
| 31 | +SELinuxfs mount: /sys/fs/selinux |
| 32 | +SELinux root directory: /etc/selinux |
| 33 | +Loaded policy name: targeted |
| 34 | +Current mode: enforcing |
| 35 | +Mode from config file: enforcing |
| 36 | +Policy MLS status: enabled |
| 37 | +Policy deny_unknown status: allowed |
| 38 | +Max kernel policy version: 31 |
| 39 | +``` |
| 40 | + |
| 41 | + |
| 42 | +## Install NGINX Agent Policy |
| 43 | +To install the nginx-agent policy run the following commands: |
| 44 | +``` |
| 45 | +sudo semodule -n -i /usr/share/selinux/packages/nginx_agent.pp |
| 46 | +sudo /usr/sbin/load_policy |
| 47 | +sudo restorecon -R /usr/bin/nginx-agent |
| 48 | +sudo restorecon -R /var/log/nginx-agent |
| 49 | +sudo restorecon -R /etc/nginx-agent |
| 50 | +``` |
| 51 | + |
| 52 | +## Updating existing policy |
| 53 | +Check for errors by using the `ausearch` command: |
| 54 | +``` |
| 55 | +sudo ausearch -m AVC,USER_AVC,SELINUX_ERR,USER_SELINUX_ERR --raw -se nginx_agent -ts recent |
| 56 | +``` |
| 57 | +Generate new rule based on the errors by using `audit2allow`: |
| 58 | +``` |
| 59 | +sudo ausearch -m AVC,USER_AVC,SELINUX_ERR,USER_SELINUX_ERR --raw -se nginx_agent -ts recent | audit2allow |
| 60 | +``` |
| 61 | + |
| 62 | +Update the `scripts/selinux/nginx_agent.te` file with the output from the `audit2allow` command. |
| 63 | + |
| 64 | +Copy the `scripts/selinux/nginx_agent.te` file to a RHEL 8 machine and build a new `nginx_agent.pp` file by running the following command: |
| 65 | +``` |
| 66 | +make -f /usr/share/selinux/devel/Makefile nginx_agent.pp |
| 67 | +``` |
| 68 | +**[NOTE: The policy has to be built on a RHEL 8 machine. If it is built on a different OS like RHEL 8/9 then we will encounter this issue [Policy version does not match](#policy-version-does-not-match) when installing it on an older OS like RHEL 8. Even if the `audit2allow` command was run on a RHEL 8/9 machine the updates to the policy need to be made on a RHEL 8 machine.]** |
| 69 | + |
| 70 | +Install the policy by following the steps here [Install NGINX Agent Policy](#install-nginx-agent-policy) |
| 71 | + |
| 72 | +Then create a PR with the changes made to the `nginx_agent.te` and `nginx_agent.pp` files. |
| 73 | + |
| 74 | +## Troubleshooting |
| 75 | +### Updated Policy Not Working |
| 76 | + |
| 77 | +If after installing an updated policy the following command |
| 78 | +``` |
| 79 | +ps -efZ | grep nginx-agent |
| 80 | +``` |
| 81 | +shows nginx-agent is unconfined `system_u:system_r:unconfined_service_t` |
| 82 | + |
| 83 | +On a RHEL 8 machine run the following command to generate a new policy |
| 84 | +``` |
| 85 | +sepolicy generate --init /usr/bin/nginx-agent |
| 86 | +``` |
| 87 | + |
| 88 | +Replace the `nginx_agent.te` file on the RHEL 8 machine with the `scripts/selinux/nginx_agent.te` file |
| 89 | + |
| 90 | +Run the following command on the RHEL 8 machine to build the new policy |
| 91 | +``` |
| 92 | +sudo ./nginx_agent.sh |
| 93 | +``` |
| 94 | + |
| 95 | +Make a PR with the changes to `nginx_agent.fc` `nginx_agent.if` `nginx_agent.pp` and `nginx_agent.te` |
| 96 | + |
| 97 | +**[NOTE: If you need to make additional changes to the policy, you will need to delete the generated files on the RHEL 8 machine and repeat all the steps above again]** |
| 98 | + |
| 99 | +### Policy version does not match |
| 100 | +If running the command |
| 101 | +``` |
| 102 | +sudo semodule -n -i /usr/share/selinux/packages/nginx_agent.pp |
| 103 | +``` |
| 104 | +results in the following error |
| 105 | +``` |
| 106 | +libsemanage.semanage_pipe_data: Child process /usr/libexec/selinux/hll/pp failed with code: 255. (No such file or directory). |
| 107 | +nginx_agent: libsepol.policydb_read: policydb module version 21 does not match my version range 4-19 |
| 108 | +nginx_agent: libsepol.sepol_module_package_read: invalid module in module package (at section 0) |
| 109 | +nginx_agent: Failed to read policy package |
| 110 | +libsemanage.semanage_direct_commit: Failed to compile hll files into cil files. |
| 111 | + (No such file or directory). |
| 112 | +semodule: Failed! |
| 113 | +``` |
| 114 | +this usually means that the policy file was built on a newer environment than isn't complicate with the environment the policy is being installed on. |
| 115 | + |
| 116 | +To resolve this issue the policy file needs to be rebuilt on a RHEL 8 environment. See [Updating existing policy](#updating-existing-policy) for instruction on how to rebuild a policy file. |
| 117 | + |
| 118 | +### Unknown Type |
| 119 | +If running the command |
| 120 | +``` |
| 121 | +sudo semodule -n -i /usr/share/selinux/packages/nginx_agent.pp |
| 122 | +``` |
| 123 | +results in the following error |
| 124 | +``` |
| 125 | +/usr/bin/checkmodule: loading policy configuration from tmp/nginx_agent.tmp |
| 126 | +nginx_agent.te:52:ERROR 'unknown type bin_t' at token ';' on line 4301: |
| 127 | +``` |
| 128 | +that means that the type is unknown and needs to be added to the require block in the `nginx_agent.te` file like this: |
| 129 | +``` |
| 130 | +require { |
| 131 | + bin_t |
| 132 | +} |
| 133 | +``` |
| 134 | + |
| 135 | +## Debugging |
| 136 | +* To check for policy violation look at the file `/var/log/audit/audit.log` |
| 137 | +* To check if NGINX Agent is confined by selinux: `ps -efZ | grep nginx-agent` |
| 138 | +* For debugging nginx selinux issues refer to this nginx blog: https://www.nginx.com/blog/using-nginx-plus-with-selinux |
| 139 | + |
| 140 | +## References |
| 141 | +* https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/8/html/using_selinux/writing-a-custom-selinux-policy_using-selinux |
0 commit comments