默认的IMA策略在所有内核源码路径Documentation/ABI/testing/ima_policy
下有说明
# PROC_SUPER_MAGIC
dont_measure fsmagic=0x9fa0
dont_appraise fsmagic=0x9fa0
# SYSFS_MAGIC
dont_measure fsmagic=0x62656572
dont_appraise fsmagic=0x62656572
# DEBUGFS_MAGIC
dont_measure fsmagic=0x64626720
dont_appraise fsmagic=0x64626720
# TMPFS_MAGIC
dont_measure fsmagic=0x01021994
dont_appraise fsmagic=0x01021994
# RAMFS_MAGIC
dont_measure fsmagic=0x858458f6
dont_appraise fsmagic=0x858458f6
# SECURITYFS_MAGIC
dont_measure fsmagic=0x73636673
dont_appraise fsmagic=0x73636673
measure func=BPRM_CHECK
measure func=FILE_MMAP mask=MAY_EXEC
measure func=FILE_CHECK mask=MAY_READ uid=0
measure func=MODULE_CHECK uid=0
appraise fowner=0
相关关键字说明:
action:
- measure: 度量,将文件的完整性度量值存储在内核的一个链表中,如果有TPM硬件存在,还会将此度量值映射到TPM的某个PCR中。
- dont_measure
- appraise 评估,将文件现在的完整性度量值和存储在文件扩展属性“security.ima”中的度量值做比较。
- dont_appraise
- audit: 审计,生成一条审计日志消息,传给内核审计子系统
func:
BPRM_CHECK
: 二进制执行程序MMAP_CHECK
: 共享链接库FILE_CHECK
: 文件MODULE_CHECK
:内核模块FIRMWARE_CHECK
KEXEC_KERNEL_CHECK
mask: 在文件进行何种操作时进行度量
- MAY_READ
- MAY_WRITE
- MAY_APPEND
- MAY_EXEC
fsmagic:内核文件系统
super_block
结构体中s_magic
。如proc文件系统的s_magic
的值是0x9fa0fsuuid:= file system UUID (e.g 8bcbe394-4f13-4144-be8e-5aa9ea2ce2f6) 通过blkid 获得
uid:= user id .比如0(root) 或者普通用户(id -u)。这个uid在资源统计和资源分配中使用。比如限制某用户拥有的进程数量
euid:= effective bid 即有效uid。在内核做特权判断时使用它。它的引入和提升权限有关。比如内核在做IPC和KEY的访问控制时也使用euid
fowner:=decimal value 比如 0(root)
测试
尝试通过修改IMA策略来忽略掉对/tmp、/var/cache等等的度量
我们注释掉root open文件这一项,将其写入IMA的policy文件
文件/etc/ima_policy
# PROC_SUPER_MAGIC
dont_measure fsmagic=0x9fa0
dont_appraise fsmagic=0x9fa0
# SYSFS_MAGIC
dont_measure fsmagic=0x62656572
dont_appraise fsmagic=0x62656572
# DEBUGFS_MAGIC
dont_measure fsmagic=0x64626720
dont_appraise fsmagic=0x64626720
# TMPFS_MAGIC
dont_measure fsmagic=0x01021994
dont_appraise fsmagic=0x01021994
# RAMFS_MAGIC
dont_measure fsmagic=0x858458f6
dont_appraise fsmagic=0x858458f6
# SECURITYFS_MAGIC
dont_measure fsmagic=0x73636673
dont_appraise fsmagic=0x73636673
measure func=BPRM_CHECK
# dont_measure func=FILE_CHECK
measure func=FILE_MMAP mask=MAY_EXEC
# measure func=FILE_CHECK mask=MAY_READ uid=0
measure func=MODULE_CHECK uid=0
# appraise fowner=0
修改两个文件:
/etc/initramfs-tools/hooks/ima.sh
#!/bin/sh
echo "Adding IMA binaries"
. /usr/share/initramfs-tools/hook-functions
copy_exec /etc/ima_policy
/etc/initramfs-tools/scripts/local-top/ima.sh
#!/bin/sh -e
PREREQ=""
# Output pre-requisites
prereqs()
{
echo "$PREREQ"
}
case "$1" in
prereqs)
prereqs
exit 0
;;
esac
grep -q "ima=off" /proc/cmdline && exit 1
mount -n -t securityfs securityfs /sys/kernel/security
IMA_POLICY=/sys/kernel/security/ima/policy
LSM_POLICY=/etc/ima_policy
grep -v "^#" $LSM_POLICY >$IMA_POLICY
为文件增加可执行权限
chmod +x /etc/initramfs-tools/hooks/ima.sh
chmod +x /etc/initramfs-tools/scripts/local-top/ima.sh
grub命令行中加入ima:修改/etc/default/grub
GRUB_CMDLINE_LINUX="ima"
重新生成grub
update-grub2
生成新的initramfs
update-initramfs -k $(uname -r) -u
重启后发现IMA的ML(/sys/kernel/security/ima/ascii_runtime_measurements
)里已经没有了诸如/tmp、/var/cache等等文件的度量信息,测试完成