Chapter 25. Extending, customizing, and troubleshooting kernel integrity subsystem
Extend, customize, and troubleshoot the kernel integrity subsystem to support diverse security requirements and operational environments.
25.1. Generate good reference values for IMA appraisal 링크 복사링크가 클립보드에 복사되었습니다!
Generate valid reference values stored in the security.ima extended attribute for all files governed by IMA-appraisal rules before deploying the policy to prevent boot failures or access denials.
ima-appraise-file </path/to/file>
# ima-appraise-file </path/to/file>
25.1.1. Adding IMA signatures as good references for immutable files 링크 복사링크가 클립보드에 복사되었습니다!
Use IMA signatures as trusted reference values for immutable files to support integrity verification and ensure only files with valid signatures are accessed.
Prerequisites
- You have created an IMA policy that includes IMA-appraisal rules.
Procedure
Install the
rpm-plugin-ima:sudo dnf install rpm-plugin-ima -yq
$ sudo dnf install rpm-plugin-ima -yqCopy to Clipboard Copied! Toggle word wrap Toggle overflow This ensures that package files have IMA signature stored in
security.xattrautomatically during package installation, reinstallation, or upgradation.Reinstall all the packages:
sudo dnf reinstall "*" -y
$ sudo dnf reinstall "*" -yCopy to Clipboard Copied! Toggle word wrap Toggle overflow This ensures that the
security.xattrextended attribute is updated for all packages.Enable the dracut integrity module so the official IMA code-signing key in
/etc/keys/imaloads automatically on boot:sudo dracut -f
$ sudo dracut -fCopy to Clipboard Copied! Toggle word wrap Toggle overflow
Verification
Verify that signature is correctly stored in
security.imaextended attribute:# evmctl ima_verify -k /etc/keys/ima/redhatimarelease-10.der /usr/lib/systemd/systemd
$ # evmctl ima_verify -k /etc/keys/ima/redhatimarelease-10.der /usr/lib/systemd/systemdCopy to Clipboard Copied! Toggle word wrap Toggle overflow keyid d3320449 (from /etc/keys/ima/redhatimarelease-10.der) key 1: d3320449 /etc/keys/ima/redhatimarelease-10.der /usr/lib/systemd/systemd: verification is OK
keyid d3320449 (from /etc/keys/ima/redhatimarelease-10.der) key 1: d3320449 /etc/keys/ima/redhatimarelease-10.der /usr/lib/systemd/systemd: verification is OKCopy to Clipboard Copied! Toggle word wrap Toggle overflow # evmctl ima_verify -k /etc/keys/ima/redhatimarelease-10.der /bin/bash
$ # evmctl ima_verify -k /etc/keys/ima/redhatimarelease-10.der /bin/bashCopy to Clipboard Copied! Toggle word wrap Toggle overflow keyid d3320449 (from /etc/keys/ima/redhatimarelease-10.der) key 1: d3320449 /etc/keys/ima/redhatimarelease-10.der /bin/bash: verification is OK ...
keyid d3320449 (from /etc/keys/ima/redhatimarelease-10.der) key 1: d3320449 /etc/keys/ima/redhatimarelease-10.der /bin/bash: verification is OK ...Copy to Clipboard Copied! Toggle word wrap Toggle overflow
25.1.2. Generating good reference values for mutable files 링크 복사링크가 클립보드에 복사되었습니다!
Generate and update reference values for mutable files to maintain integrity and ensure the system accurately verifies file authenticity.
Prerequisites
- You have root privileges on the system.
- You have created an IMA policy that includes IMA-appraisal rules.
- You have generated good reference values for IMA appraisal.
- Secure Boot is disabled.
Procedure
Optional: Enable your chosen IMA-appraisal policy or skip this step if you only use your custom policy. Take built-in
ima_policy=appraise_tcbas an example:grubby --update-kernel=/boot/vmlinuz-$(uname -r) --args="ima_policy=appraise_tcb"
# grubby --update-kernel=/boot/vmlinuz-$(uname -r) --args="ima_policy=appraise_tcb"Copy to Clipboard Copied! Toggle word wrap Toggle overflow Additionally for
s390xsystems:zipl
# ziplCopy to Clipboard Copied! Toggle word wrap Toggle overflow
Enable IMA-appraisal fix mode by adding the
ima_appraise=fixkernel command line parameter:grubby --update-kernel=/boot/vmlinuz-$(uname -r) --args="ima_appraise=fix"
# grubby --update-kernel=/boot/vmlinuz-$(uname -r) --args="ima_appraise=fix"Copy to Clipboard Copied! Toggle word wrap Toggle overflow Additionally for
s390xsystems:zipl
# ziplCopy to Clipboard Copied! Toggle word wrap Toggle overflow
Reboot the system:
reboot
# rebootCopy to Clipboard Copied! Toggle word wrap Toggle overflow Optional: Load your custom IMA policy:
echo <path_to_your_custom_ima_policy> > /sys/kernel/security/ima/policy
# echo <path_to_your_custom_ima_policy> > /sys/kernel/security/ima/policyCopy to Clipboard Copied! Toggle word wrap Toggle overflow Re-label the whole system:
find / -fstype xfs -type f -uid 0 -exec head -c 0 '{}' \;# find / -fstype xfs -type f -uid 0 -exec head -c 0 '{}' \;Copy to Clipboard Copied! Toggle word wrap Toggle overflow Turn off IMA-appraisal fix mode by removing the
ima_appraise=fixkernel command line parameter:grubby --update-kernel=/boot/vmlinuz-$(uname -r) --remove-args="ima_appraise=fix"
# grubby --update-kernel=/boot/vmlinuz-$(uname -r) --remove-args="ima_appraise=fix"Copy to Clipboard Copied! Toggle word wrap Toggle overflow Additionally for
s390xsystems:zipl
# ziplCopy to Clipboard Copied! Toggle word wrap Toggle overflow
- Enable the secure boot if it is disabled.
Additional resources
25.2. Writing custom IMA policy 링크 복사링크가 클립보드에 복사되었습니다!
Create custom IMA policy rules when built-in policies or sample policies do not meet your requirements. Systemd loads custom policies from /etc/ima/ima-policy, replacing the built-in IMA policy.
After you define your IMA policy, generate good reference values if the policy includes IMA-appraisal rules before you deploy it. If your policy does not include IMA-appraisal rules, you can verify the policy by running echo /PATH-TO-YOUR-DRAFT-IMA-POLICY > /sys/kernel/security/integrity/ima/policy. This approach helps prevent system boot failures.
An IMA policy rule uses the format action [condition …] to specify an action that is triggered under certain conditions. For example, the sample policy in /usr/share/ima/policies/01-appraise-executable-and-lib-signatures includes the following rules:
The first rule, dont_appraise fsmagic=0x9fa0, instructs IMA to skip appraising files in the PROC_SUPER_MAGIC filesystem. The last rule, appraise func=BPRM_CHECK appraise_type=imasig, enforces signature verification when a file is executed.
25.3. Creating custom IMA keys using OpenSSL 링크 복사링크가 클립보드에 복사되었습니다!
Generate a CSR with OpenSSL to create digital certificates for code security. The kernel validates IMA signatures using code signing keys in the .ima keyring. Before adding a key, ensure it is signed by an IMA CA key present in the trusted keyrings.
Prerequisites
The custom IMA CA key has the following extensions:
- the basic constraints extension with the CA boolean asserted.
-
the
KeyUsageextension with thekeyCertSignbit asserted but without thedigitalSignatureasserted.
The custom IMA code signing key falls under the following criteria:
- The IMA CA key signed this custom IMA code signing key.
-
The custom key includes the
subjectKeyIdentifierextension.
-
UEFI Secure Boot on
x86_64oraarch64systems or PowerVM Secure Boot onppc64lesystems is enabled.
Procedure
To generate a custom IMA CA key pair, run:
openssl req -new -x509 -utf8 -sha256 -days 3650 -batch -config ima_ca.conf -outform DER -out custom_ima_ca.der -keyout custom_ima_ca.priv
# openssl req -new -x509 -utf8 -sha256 -days 3650 -batch -config ima_ca.conf -outform DER -out custom_ima_ca.der -keyout custom_ima_ca.privCopy to Clipboard Copied! Toggle word wrap Toggle overflow Optional: To check the content of the
ima_ca.conffile, run:Copy to Clipboard Copied! Toggle word wrap Toggle overflow To generate a private key and a certificate signing request (CSR) for the IMA code signing key, run:
openssl req -new -utf8 -sha256 -days 365 -batch -config ima.conf -out custom_ima.csr -keyout custom_ima.priv
# openssl req -new -utf8 -sha256 -days 365 -batch -config ima.conf -out custom_ima.csr -keyout custom_ima.privCopy to Clipboard Copied! Toggle word wrap Toggle overflow Optional: To check the content of the
ima.conffile, run:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Use the IMA CA private key to sign the CSR to create the IMA code signing certificate:
openssl x509 -req -in custom_ima.csr -days 365 -extfile ima.conf -extensions code_signing -CA custom_ima_ca.der -CAkey custom_ima_ca.priv -CAcreateserial -outform DER -out ima.der
# openssl x509 -req -in custom_ima.csr -days 365 -extfile ima.conf -extensions code_signing -CA custom_ima_ca.der -CAkey custom_ima_ca.priv -CAcreateserial -outform DER -out ima.derCopy to Clipboard Copied! Toggle word wrap Toggle overflow
25.4. Loading an IMA policy signed by your custom IMA key 링크 복사링크가 클립보드에 복사되었습니다!
Load an IMA policy signed with your custom IMA key to maintain system integrity and ensure only trusted, authenticated policies are applied during system startup or runtime.
This procedure applies only to x86_64 and aarch64 systems with UEFI Secure Boot enabled, and to ppc64le systems running PowerVM Secure Boot.
Prerequisites
- You must have root privileges on your system.
-
UEFI Secure Boot is enabled for Red Hat Enterprise Linux or the kernel is booted with the
ima_policy=secure_bootparameter to ensure only signed IMA policy can be loaded. - The custom IMA CA key has been added to the MOK list. For more information, see Enrolling public key on target system by adding the public key to the MOK list.
- The kernel version is 5.14 or later.
- Good reference values have been generated for the IMA policy. For more information, see Generate good reference values for IMA appraisal.
Procedure
Add your custom IMA code signing key to the
.imakeyring:keyctl padd asymmetric <KEY_SUBJECT> %:.ima < <PATH_TO_YOUR_CUSTOM_IMA_KEY>
# keyctl padd asymmetric <KEY_SUBJECT> %:.ima < <PATH_TO_YOUR_CUSTOM_IMA_KEY>Copy to Clipboard Copied! Toggle word wrap Toggle overflow Prepare your IMA policy and sign it with your custom IMA code signing key:
evmctl ima_sign <PATH_TO_YOUR_CUSTOM_IMA_POLICY> -k <PATH_TO_YOUR_CUSTOM_IMA_KEY>
# evmctl ima_sign <PATH_TO_YOUR_CUSTOM_IMA_POLICY> -k <PATH_TO_YOUR_CUSTOM_IMA_KEY>Copy to Clipboard Copied! Toggle word wrap Toggle overflow Load the signed IMA policy:
echo <PATH_TO_YOUR_CUSTOM_SIGNED_IMA_POLICY> > /sys/kernel/security/ima/policy
# echo <PATH_TO_YOUR_CUSTOM_SIGNED_IMA_POLICY> > /sys/kernel/security/ima/policyCopy to Clipboard Copied! Toggle word wrap Toggle overflow Check the exit status of the previous command:
echo $?
# echo $?Copy to Clipboard Copied! Toggle word wrap Toggle overflow 0
0Copy to Clipboard Copied! Toggle word wrap Toggle overflow 0indicates that the IMA policy was loaded successfully. If the command returns a nonzero value, the IMA policy was not loaded successfully.
WarningDo not skip this step. If you do, your system might fail to boot and you need to recover your system.
If the IMA policy fails to load, repeat the steps 2, 3, and 4 to fix the issue.
Copy the signed IMA policy to
/etc/ima/ima-policyto enable systemd load it automatically on boot:cp --preserve=xattr <PATH_TO_YOUR_CUSTOM_IMA_POLICY> /etc/ima/ima-policy
# cp --preserve=xattr <PATH_TO_YOUR_CUSTOM_IMA_POLICY> /etc/ima/ima-policyCopy to Clipboard Copied! Toggle word wrap Toggle overflow Copy your custom IMA code signing key to
/etc/keys/ima/:cp <PATH_TO_YOUR_CUSTOM_IMA_KEY> /etc/keys/ima/
# cp <PATH_TO_YOUR_CUSTOM_IMA_KEY> /etc/keys/ima/Copy to Clipboard Copied! Toggle word wrap Toggle overflow Copy the
dracutintegrity module configuration file:cp --preserve=xattr /usr/share/ima/dracut-98-integrity.conf /etc/dracut.conf.d/98-integrity.conf
# cp --preserve=xattr /usr/share/ima/dracut-98-integrity.conf /etc/dracut.conf.d/98-integrity.confCopy to Clipboard Copied! Toggle word wrap Toggle overflow Regenerate the initial RAM disk:
dracut -f
# dracut -fCopy to Clipboard Copied! Toggle word wrap Toggle overflow Additionally for
s390xsystems:zipl
# ziplCopy to Clipboard Copied! Toggle word wrap Toggle overflow
Verification
Verify that the IMA policy is loaded successfully:
cat /sys/kernel/security/ima/policy
# cat /sys/kernel/security/ima/policyCopy to Clipboard Copied! Toggle word wrap Toggle overflow The output should include the rules from your custom IMA policy.
25.5. Troubleshooting systemd failure to load the IMA policy 링크 복사링크가 클립보드에 복사되었습니다!
If systemd fails to load the /etc/ima/ima-policy file, the system may hang and display a Freezing execution error. You can troubleshoot and recover the system from this state by using the methods described in this procedure.
[ 5.829882] ima: policy update failed [ 5.830094] ima: signed policy file (specified as an absolute pathname) required [!!!!!!] Failed to load IMA policy. … [ 5.859994] systemd[1]: Freezing execution.
[ 5.829882] ima: policy update failed
[ 5.830094] ima: signed policy file (specified as an absolute pathname) required
[!!!!!!] Failed to load IMA policy.
…
[ 5.859994] systemd[1]: Freezing execution.
There are three methods that you can use to recover your system.
25.5.1. Turn off Secure Boot 링크 복사링크가 클립보드에 복사되었습니다!
If system startup fails because an unsigned IMA policy cannot load, temporarily disable Secure Boot. With disabled Secure Boot, you can resolve the error before re-enabling security features.
As a workaround, you can turn off Secure Boot temporarily and follow Deploying a custom signed IMA policy for UEFI systems to fix the issue.
25.5.2. Booting the system with the init=/bin/bash kernel parameter 링크 복사링크가 클립보드에 복사되었습니다!
Boot the system with the init=/bin/bash kernel parameter to access a shell for system recovery or troubleshooting.
-
Modify the bootloader entry and add the
init=/bin/bashkernel parameter. After you access the shell, remount the system with write permissions:
mount -o remount,rw /
# mount -o remount,rw /Copy to Clipboard Copied! Toggle word wrap Toggle overflow Rename
/etc/ima/ima-policyto/etc/ima/ima-policy.bak:mv /etc/ima/ima-policy /etc/ima/ima-policy.bak
# mv /etc/ima/ima-policy /etc/ima/ima-policy.bakCopy to Clipboard Copied! Toggle word wrap Toggle overflow Enable the
SysRqkey:echo 1 > /proc/sys/kernel/sysrq
# echo 1 > /proc/sys/kernel/sysrqCopy to Clipboard Copied! Toggle word wrap Toggle overflow Reboot the system:
printf "s\nb" > /proc/sysrq-trigger
# printf "s\nb" > /proc/sysrq-triggerCopy to Clipboard Copied! Toggle word wrap Toggle overflow Resolve any issues in
/etc/ima/ima-policy.bakand verify that the policy can be loaded:echo /etc/ima/ima-policy.bak >> /sys/kernel/security/integrity/ima/policy
# echo /etc/ima/ima-policy.bak >> /sys/kernel/security/integrity/ima/policyCopy to Clipboard Copied! Toggle word wrap Toggle overflow Rename
/etc/ima/ima-policy.bakto/etc/ima/ima-policy:mv /etc/ima/ima-policy.bak /etc/ima/ima-policy
# mv /etc/ima/ima-policy.bak /etc/ima/ima-policyCopy to Clipboard Copied! Toggle word wrap Toggle overflow
25.5.3. Booting the system with the initcall_blacklist=init_ima kernel parameter 링크 복사링크가 클립보드에 복사되었습니다!
Boot the system with the initcall_blacklist=init_ima kernel parameter to disable the IMA policy when the system hangs with systemd[1]: Freezing execution.
-
Modify the boot loader entry and add the
initcall_blacklist=init_imakernel parameter. Rename
/etc/ima/ima-policyto/etc/ima/ima-policy.bak:mv /etc/ima/ima-policy /etc/ima/ima-policy.bak
# mv /etc/ima/ima-policy /etc/ima/ima-policy.bakCopy to Clipboard Copied! Toggle word wrap Toggle overflow Reboot the system:
systemctl reboot
# systemctl rebootCopy to Clipboard Copied! Toggle word wrap Toggle overflow Resolve any issues in
/etc/ima/ima-policy.bakand verify that the policy can be loaded:echo /etc/ima/ima-policy.bak >> /sys/kernel/security/integrity/ima/policy
# echo /etc/ima/ima-policy.bak >> /sys/kernel/security/integrity/ima/policyCopy to Clipboard Copied! Toggle word wrap Toggle overflow Rename
/etc/ima/ima-policy.bakto/etc/ima/ima-policy:mv /etc/ima/ima-policy.bak /etc/ima/ima-policy
# mv /etc/ima/ima-policy.bak /etc/ima/ima-policyCopy to Clipboard Copied! Toggle word wrap Toggle overflow
25.6. Signing custom built packages 링크 복사링크가 클립보드에 복사되었습니다!
Sign custom built packages before deployment using the rpm-sign tool and IMA code signing key to maintain system integrity.
Prerequisites
- You have root privileges on your system.
- You have a custom built package that you want to sign.
- You have the IMA code signing key.
-
You have the
rpm-signtool installed. - Custom IMA keys are created. See Creating custom IMA keys using OpenSSL.
Procedure
Use
rpmsign –signfilesto sign package files:rpmsign --define "gpg_name _<GPG_KEY_NAME>" --addsign --signfiles --fskpass --fskpath=<PATH_TO_YOUR_PRIVATE_IMA_CODE_SIGNING_KEY> <PATH_TO_YOUR_RPM>
# rpmsign --define "gpg_name _<GPG_KEY_NAME>" --addsign --signfiles --fskpass --fskpath=<PATH_TO_YOUR_PRIVATE_IMA_CODE_SIGNING_KEY> <PATH_TO_YOUR_RPM>Copy to Clipboard Copied! Toggle word wrap Toggle overflow --define "gpg_name _<GPG_KEY_NAME>"- The GPG key signs the package, and the IMA code signing key signs each file in the package.
--addsign- Adds the signature to the package.
--signfiles- Signs each file in the package.
--fskpass- Avoids repeatedly entering the password for the IMA code signing key.
--fskpath- Specifies the path to the IMA code signing key.
Verification
To verify that the package is signed, you can use the following command:
rpm -q --queryformat "[%{FILENAMES} %{FILESIGNATURES}\n] <PATH_TO_YOUR_RPM>"# rpm -q --queryformat "[%{FILENAMES} %{FILESIGNATURES}\n] <PATH_TO_YOUR_RPM>"Copy to Clipboard Copied! Toggle word wrap Toggle overflow /usr/bin/YOUR_BIN 030204... /usr/lib/YOUR_LIB.so 030204... ...
/usr/bin/YOUR_BIN 030204... /usr/lib/YOUR_LIB.so 030204... ...Copy to Clipboard Copied! Toggle word wrap Toggle overflow
25.7. Comparison of IMA and fapolicyd 링크 복사링크가 클립보드에 복사되었습니다!
Compare IMA and fapolicyd tools for enforcing file integrity. IMA verifies file integrity at boot time, while fapolicyd verifies at runtime.
The following list can help you determine which tool meets your requirements:
-
IMA verifies digital signatures to ensure integrity, while
fapolicydcurrently supports only hash-based verification. -
IMA operates in kernel space, while
fapolicydoperates in user space. -
fapolicydsupports basic integrity verification by checking file size and can also verify reference hash values stored insecurity.ima. -
IMA and
fapolicyduse different policy syntax. For example,fapolicydsupports path-based policies, but IMA does not.