Dieser Inhalt ist in der von Ihnen ausgewählten Sprache nicht verfügbar.
Chapter 10. Blocking and allowing applications by using fapolicyd
Setting and enforcing a policy that either allows or denies application execution based on a rule set efficiently prevents the execution of unknown and potentially malicious software.
10.1. The fapolicyd framework structure Link kopierenLink in die Zwischenablage kopiert!
The fapolicyd software framework controls the execution of applications based on a user-defined policy. This is one of the most efficient ways to prevent running untrusted and possibly malicious applications on the system.
10.1.1. Components and trust Link kopierenLink in die Zwischenablage kopiert!
The fapolicyd framework provides the following components:
-
fapolicydservice -
fapolicydcommand-line utilities -
fapolicydRPM plugin -
fapolicydrule language -
fagenrulesscript
The administrator can define allow and deny execution rules for any application with the option to audit based on a path, hash, MIME type, or trust.
The fapolicyd framework introduces the concept of trust. An application is trusted when the system package manager correctly installs it and therefore registered in the system RPM database. The fapolicyd daemon uses the RPM database as a list of trusted binaries and scripts.
The fapolicyd RPM plugin registers any system update that is handled by either the DNF or RPM package manager. The plugin notifies the fapolicyd daemon about changes in this database. Other ways of adding applications require the creation of custom rules and restarting the fapolicyd service.
For more information, see the fapolicyd-related man pages listed by using the man -k fapolicyd command on your system.
10.1.2. Configuration files and directories Link kopierenLink in die Zwischenablage kopiert!
The fapolicyd service configuration is located in the /etc/fapolicyd/ directory with the following structure:
-
The
/etc/fapolicyd/fapolicyd.trustfile contains a list of trusted files. You can also use multiple trust files in the/etc/fapolicyd/trust.d/directory. -
The
/etc/fapolicyd/rules.d/directory contains files withallowanddenyexecution rules. Thefagenrulesscript merges these component rules files to the/etc/fapolicyd/compiled.rulesfile. -
The
fapolicyd.conffile contains the daemon’s configuration options. This file is useful primarily for performance-tuning purposes.
10.1.3. Rules Link kopierenLink in die Zwischenablage kopiert!
Rules in /etc/fapolicyd/rules.d/ are organized in several files, each representing a different policy goal. The numbers at the beginning of the corresponding file names determine the order in /etc/fapolicyd/compiled.rules:
- 10
- Language rules.
- 20
- Dracut-related Rules.
- 21
- Rules for updaters.
- 30
- Patterns.
- 40
- ELF rules.
- 41
- Shared objects rules.
- 42
- Trusted ELF rules.
- 70
- Trusted language rules.
- 72
- Shell rules.
- 90
- Deny execute rules.
- 95
- Allow open rules.
For more information and examples, see the documentation installed with the fapolicyd package in the /usr/share/doc/fapolicyd/ directory, the /usr/share/fapolicyd/sample-rules/README-rules file, and the fapolicyd.rules(5) and fagenrules(8) man pages on your system.
10.1.4. Integrity checking Link kopierenLink in die Zwischenablage kopiert!
You can use one of the following ways for fapolicyd integrity checking:
- File-size checking
- Comparing SHA-256 hashes
- Integrity Measurement Architecture (IMA) subsystem
By default, fapolicyd does no integrity checking. Integrity checking based on the file size is fast, but an attacker can replace the content of the file and preserve its byte size. Computing and checking SHA-256 checksums is more secure, but it affects the performance of the system. The integrity = ima option in fapolicyd.conf requires support for files' extended attributes (also known as xattr) on all file systems containing executable files.
10.2. Deploying fapolicyd Link kopierenLink in die Zwischenablage kopiert!
When deploying the fapolicyd application allowlisting framework, you can either try your configuration in permissive mode first or directly enable the service in the default configuration.
Procedure
Install the
fapolicydpackage:dnf install fapolicyd
# dnf install fapolicydCopy to Clipboard Copied! Toggle word wrap Toggle overflow Set the Audit subsystem for recording
fapolicydevents:auditctl -w /etc/fapolicyd/ -p wa -k fapolicyd_changes service try-restart auditd
# auditctl -w /etc/fapolicyd/ -p wa -k fapolicyd_changes # service try-restart auditdCopy to Clipboard Copied! Toggle word wrap Toggle overflow Optional: To try your configuration first, change mode to permissive.
Open the
/etc/fapolicyd/fapolicyd.conffile in a text editor of your choice, for example:vi /etc/fapolicyd/fapolicyd.conf
# vi /etc/fapolicyd/fapolicyd.confCopy to Clipboard Copied! Toggle word wrap Toggle overflow Change the value of the
permissiveoption from0to1, save the file, and exit the editor:permissive = 1
permissive = 1Copy to Clipboard Copied! Toggle word wrap Toggle overflow Alternatively, you can debug your configuration by using the
fapolicyd --debug-deny --permissivecommand before you start the service. See the Troubleshooting problems related to fapolicyd section for more information.
Enable and start the
fapolicydservice:systemctl enable --now fapolicyd
# systemctl enable --now fapolicydCopy to Clipboard Copied! Toggle word wrap Toggle overflow If you enabled permissive mode through
/etc/fapolicyd/fapolicyd.conf:- Use your applications.
Check Audit logs for
fanotifydenials, for example:ausearch -ts recent -m fanotify
# ausearch -ts recent -m fanotifyCopy to Clipboard Copied! Toggle word wrap Toggle overflow When debugged, disable permissive mode by changing the corresponding value back to
permissive = 0, and restart the service:systemctl restart fapolicyd
# systemctl restart fapolicydCopy to Clipboard Copied! Toggle word wrap Toggle overflow
Verification
Verify that the
fapolicydservice is running correctly:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Log in as a user without root privileges, and check that
fapolicydis working, for example:cp /bin/ls /tmp /tmp/ls
$ cp /bin/ls /tmp $ /tmp/ls bash: /tmp/ls: Operation not permittedCopy to Clipboard Copied! Toggle word wrap Toggle overflow
10.3. Marking files as trusted using an additional source of trust Link kopierenLink in die Zwischenablage kopiert!
The fapolicyd framework trusts files contained in the RPM database. You can mark additional files as trusted by modifying sources of trust.
You can modify the /etc/fapolicyd/fapolicyd.trust plain text file or files in the /etc/fapolicyd/trust.d directory, either directly using a text editor or through fapolicyd-cli commands. See the fapolicyd.trust(13) and fapolicyd-cli(8) man pages on your system for more details.
For performance reasons, mark files as trusted using fapolicyd.trust or trust.d/ rather than write custom fapolicyd rules.
Prerequisites
-
The
fapolicydframework is deployed on your system.
Procedure
Copy your custom binary to the required directory, for example:
cp /bin/ls /tmp /tmp/ls
$ cp /bin/ls /tmp $ /tmp/ls bash: /tmp/ls: Operation not permittedCopy to Clipboard Copied! Toggle word wrap Toggle overflow Mark your custom binary as trusted, and store the corresponding entry to the
myappfile in/etc/fapolicyd/trust.d/:fapolicyd-cli --file add /tmp/ls --trust-file myapp
# fapolicyd-cli --file add /tmp/ls --trust-file myappCopy to Clipboard Copied! Toggle word wrap Toggle overflow -
If you skip the
--trust-fileoption, then the previous command adds the corresponding line to/etc/fapolicyd/fapolicyd.trust. To mark all existing files in a directory as trusted, provide the directory path as an argument of the
--fileoption, for example:fapolicyd-cli --file add /tmp/my_bin_dir/ --trust-file myapp
# fapolicyd-cli --file add /tmp/my_bin_dir/ --trust-file myappCopy to Clipboard Copied! Toggle word wrap Toggle overflow
-
If you skip the
Update the
fapolicyddatabase:fapolicyd-cli --update
# fapolicyd-cli --updateCopy to Clipboard Copied! Toggle word wrap Toggle overflow NoteChanging the content of a trusted file or directory changes its checksum, and therefore,
fapolicydno longer considers it trusted.To restore trust in the new content, refresh the file trust database by using the
fapolicyd-cli --file updatecommand. If you do not provide any argument, the entire database refreshes. Alternatively, you can specify a path to a specific file or directory. Then, update the database by usingfapolicyd-cli --update.
Verification
Check that you can execute your custom binary, for example:
/tmp/ls
$ /tmp/ls lsCopy to Clipboard Copied! Toggle word wrap Toggle overflow
10.4. Adding custom allow and deny rules for fapolicyd Link kopierenLink in die Zwischenablage kopiert!
The default set of fapolicyd rules does not affect system functions. For custom scenarios, such as storing binaries and scripts in a non-standard directory or adding applications without the DNF or RPM installers, you must either mark additional files as trusted or add new custom rules.
For basic scenarios, see Marking files as trusted using an additional source of trust. In more advanced scenarios such as allowing to execute a custom binary only for specific user and group identifiers, add new custom rules to the /etc/fapolicyd/rules.d/ directory.
The following steps demonstrate adding a new rule to allow a custom binary.
For more information and examples, see the documentation installed with the fapolicyd package in the /usr/share/doc/fapolicyd/ directory, the /usr/share/fapolicyd/sample-rules/README-rules file, and the fapolicyd.rules(5) and fagenrules(8) man pages on your system.
Prerequisites
-
The
fapolicydframework is deployed on your system.
Procedure
Copy your custom binary to the required directory, for example:
cp /bin/ls /tmp /tmp/ls
$ cp /bin/ls /tmp $ /tmp/ls bash: /tmp/ls: Operation not permittedCopy to Clipboard Copied! Toggle word wrap Toggle overflow Stop the
fapolicydservice:systemctl stop fapolicyd
# systemctl stop fapolicydCopy to Clipboard Copied! Toggle word wrap Toggle overflow Use debug mode to identify a corresponding rule. Because the output of the
fapolicyd --debugcommand is verbose and you can stop it only by pressing Ctrl+C or killing the corresponding process, redirect the error output to a file. In this case, you can limit the output only to access denials by using the--debug-denyoption instead of--debug:fapolicyd --debug-deny 2> fapolicy.output &
# fapolicyd --debug-deny 2> fapolicy.output & [1] 51341Copy to Clipboard Copied! Toggle word wrap Toggle overflow Alternatively, you can run
fapolicyddebug mode in another terminal.Repeat the command that
fapolicyddenied:/tmp/ls
$ /tmp/ls bash: /tmp/ls: Operation not permittedCopy to Clipboard Copied! Toggle word wrap Toggle overflow Stop debug mode by resuming it in the foreground and pressing Ctrl+C:
fg
# fg fapolicyd --debug 2> fapolicy.output ^C …Copy to Clipboard Copied! Toggle word wrap Toggle overflow Alternatively, kill the process of
fapolicyddebug mode:kill 51341
# kill 51341Copy to Clipboard Copied! Toggle word wrap Toggle overflow Find a rule that denies the execution of your application:
cat fapolicy.output | grep 'deny_audit' … rule=13 dec=deny_audit perm=execute auid=0 pid=6855 exe=/usr/bin/bash : path=/tmp/ls ftype=application/x-executable trust=0
# cat fapolicy.output | grep 'deny_audit' … rule=13 dec=deny_audit perm=execute auid=0 pid=6855 exe=/usr/bin/bash : path=/tmp/ls ftype=application/x-executable trust=0Copy to Clipboard Copied! Toggle word wrap Toggle overflow Locate the file that contains a rule that denies the execution of your custom binary. In this case, the
deny_audit perm=executerule belongs to the90-deny-execute.rulesfile:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Add a new
allowrule to a file that lexically precedes the rule file that contains the rule that denied the execution of your custom binary in the/etc/fapolicyd/rules.d/directory.Create the rule file and open it in a text editor of your choice, for example:
touch /etc/fapolicyd/rules.d/80-myapps.rules vi /etc/fapolicyd/rules.d/80-myapps.rules
# touch /etc/fapolicyd/rules.d/80-myapps.rules # vi /etc/fapolicyd/rules.d/80-myapps.rulesCopy to Clipboard Copied! Toggle word wrap Toggle overflow Insert the following rule to the
80-myapps.rulesfile:allow perm=execute exe=/usr/bin/bash trust=1 : path=/tmp/ls ftype=application/x-executable trust=0
allow perm=execute exe=/usr/bin/bash trust=1 : path=/tmp/ls ftype=application/x-executable trust=0Copy to Clipboard Copied! Toggle word wrap Toggle overflow Alternatively, you can allow executions of all binaries in the
/tmpdirectory by adding the following rule to the rule file in/etc/fapolicyd/rules.d/:allow perm=execute exe=/usr/bin/bash trust=1 : dir=/tmp/ trust=0
allow perm=execute exe=/usr/bin/bash trust=1 : dir=/tmp/ trust=0Copy to Clipboard Copied! Toggle word wrap Toggle overflow ImportantTo make a rule effective recursively on all directories under the specified directory, add a trailing slash to the value of the
dir=parameter in the rule (/tmp/in the previous example).
Prevent changes in the content of your custom binary
Define the required rule using an SHA-256 checksum:
sha256sum /tmp/ls
$ sha256sum /tmp/ls 780b75c90b2d41ea41679fcb358c892b1251b68d1927c80fbc0d9d148b25e836 lsCopy to Clipboard Copied! Toggle word wrap Toggle overflow Change the rule to the following definition:
allow perm=execute exe=/usr/bin/bash trust=1 : sha256hash=780b75c90b2d41ea41679fcb358c892b1251b68d1927c80fbc0d9d148b25e836
allow perm=execute exe=/usr/bin/bash trust=1 : sha256hash=780b75c90b2d41ea41679fcb358c892b1251b68d1927c80fbc0d9d148b25e836Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Check that the list of compiled rules differs from the rule set in
/etc/fapolicyd/rules.d/:fagenrules --check
# fagenrules --check /usr/sbin/fagenrules: Rules have changed and should be updatedCopy to Clipboard Copied! Toggle word wrap Toggle overflow Update the list, which is stored in the
/etc/fapolicyd/compiled.rulesfile:fagenrules --load
# fagenrules --loadCopy to Clipboard Copied! Toggle word wrap Toggle overflow Check that your custom rule is in the list of
fapolicydrules before the rule that denied the execution:fapolicyd-cli --list
# fapolicyd-cli --list ... 13. allow perm=execute exe=/usr/bin/bash trust=1 : path=/tmp/ls ftype=application/x-executable trust=0 14. deny_audit perm=execute all : all …Copy to Clipboard Copied! Toggle word wrap Toggle overflow Start the
fapolicydservice:systemctl start fapolicyd
# systemctl start fapolicydCopy to Clipboard Copied! Toggle word wrap Toggle overflow
Verification
Check that your custom binary can be now executed, for example:
/tmp/ls
$ /tmp/ls lsCopy to Clipboard Copied! Toggle word wrap Toggle overflow
10.5. Enabling fapolicyd integrity checks Link kopierenLink in die Zwischenablage kopiert!
By default, fapolicyd does not perform integrity checking. You can configure fapolicyd to perform integrity checks by comparing either file sizes or SHA-256 hashes. You can also set integrity checks by using the Integrity Measurement Architecture (IMA) subsystem.
Prerequisites
-
The
fapolicydframework is deployed on your system.
Procedure
Open the
/etc/fapolicyd/fapolicyd.conffile in a text editor of your choice, for example:vi /etc/fapolicyd/fapolicyd.conf
# vi /etc/fapolicyd/fapolicyd.confCopy to Clipboard Copied! Toggle word wrap Toggle overflow Change the value of the
integrityoption fromnonetosha256, save the file, and exit the editor:integrity = sha256
integrity = sha256Copy to Clipboard Copied! Toggle word wrap Toggle overflow Restart the
fapolicydservice:systemctl restart fapolicyd
# systemctl restart fapolicydCopy to Clipboard Copied! Toggle word wrap Toggle overflow
Verification
Back up the file used for the verification:
cp /bin/more /bin/more.bak
# cp /bin/more /bin/more.bakCopy to Clipboard Copied! Toggle word wrap Toggle overflow Change the content of the
/bin/morebinary:cat /bin/less > /bin/more
# cat /bin/less > /bin/moreCopy to Clipboard Copied! Toggle word wrap Toggle overflow Attempt to use the changed binary as a regular user and verify
fapolicyddenies the operation:su example.user /bin/more /etc/redhat-release
# su example.user $ /bin/more /etc/redhat-release bash: /bin/more: Operation not permittedCopy to Clipboard Copied! Toggle word wrap Toggle overflow Revert the changes:
mv -f /bin/more.bak /bin/more
# mv -f /bin/more.bak /bin/moreCopy to Clipboard Copied! Toggle word wrap Toggle overflow
10.7. Preventing users from executing untrustworthy code by using the fapolicyd RHEL system role Link kopierenLink in die Zwischenablage kopiert!
You can automate the installation and configuration of the fapolicyd service by using the fapolicyd RHEL system role.
With this role, you can remotely configure the service to allow users to execute only trusted applications, for example, the ones which are listed in the RPM database and in an allow list. Additionally, the service can perform integrity checks before it executes an allowed application.
Prerequisites
- You have prepared the control node and the managed nodes.
- You are logged in to the control node as a user who can run playbooks on the managed nodes.
-
The account you use to connect to the managed nodes has
sudopermissions on them.
Procedure
Create a playbook file, for example,
~/playbook.yml, with the following content:Copy to Clipboard Copied! Toggle word wrap Toggle overflow The settings specified in the example playbook include the following:
fapolicyd_setup_permissive: <true|false>-
Enables or disables sending policy decisions to the kernel for enforcement. Set this variable for debugging and testing purposes to
false. fapolicyd_setup_integrity: <type_type>Defines the integrity checking method. You can set one of the following values:
-
none(default): Disables integrity checking. -
size: The service compares only the file sizes of allowed applications. -
ima: The service checks the SHA-256 hash that the kernel’s Integrity Measurement Architecture (IMA) stored in a file’s extended attribute. Additionally, the service performs a size check. Note that the role does not configure the IMA kernel subsystem. To use this option, you must manually configure the IMA subsystem. -
sha256: The service compares the SHA-256 hash of allowed applications.
-
fapolicyd_setup_trust: <trust_backends>-
Defines the list of trust backends. If you include the
filebackend, specify the allowed executable files in thefapolicyd_add_trusted_filelist.
For details about all variables used in the playbook, see the
/usr/share/ansible/roles/rhel-system-roles.fapolicyd.README.mdfile on the control node.Validate the playbook syntax:
ansible-playbook ~/playbook.yml --syntax-check
$ ansible-playbook ~/playbook.yml --syntax-checkCopy to Clipboard Copied! Toggle word wrap Toggle overflow Note that this command only validates the syntax and does not protect against a wrong but valid configuration.
Run the playbook:
ansible-playbook ~/playbook.yml
$ ansible-playbook ~/playbook.ymlCopy to Clipboard Copied! Toggle word wrap Toggle overflow
Verification
Execute a binary application that is not on the allow list as a user:
ansible managed-node-01.example.com -m command -a 'su -c "/bin/not_authorized_application " <user_name>'
$ ansible managed-node-01.example.com -m command -a 'su -c "/bin/not_authorized_application " <user_name>' bash: line 1: /bin/not_authorized_application: Operation not permitted non-zero return codeCopy to Clipboard Copied! Toggle word wrap Toggle overflow