Apparmor可以对程序进行多方面的限制,主要内容如下。
1. 文件系统的访问控制
File是可选的,如果省略该关键字并且不存在其他以某个关键字(例如 network 或 mount)开头的规则类型,则自动隐式应用该前缀,即下面是等效的。
file /example/rule r,
/example/rule r,
file,等效于/** rwmlk,授予对所有文件的访问权限
- 文件权限访问模式:
r:读取
w:写入,w 和 a 权限标志不能结合使用,它们是互斥的
a:追加,w 和 a 权限标志不能结合使用,它们是互斥的
k:文件锁定模式
l:creation hard links
link FILE -> TARGET:链接对规则
m:memory map as executable
px:discrete profile execute
Px:discrete profile execute – scrub the environment
cx:transition to subprofile on execute
Cx:transition to subprofile on execute – scrub the environment
ux:unconfined execute
Ux:execute unconfined, after cleaning the environment
ix:execute another program with the new program inheriting policy
- 允许规则
allow是可选的,如果未指定并且不使用 deny关键字,则按惯常隐式应用该前缀。
allow file /example r,
- 拒绝规则
deny /home/*/.ssh/** w,
- 拥有者条件规则
按条件应用于文件的拥有者用户(fsuid 需与文件的 uid 相匹配)。
owner /home/*/** rw
2. 审计规则
AppArmor 可以设置和控制应用程序的资源限制(rlimit,也称为 ulimit)。
默认情况下,AppArmor 不会控制应用程序的 rlimit,而只控制限制配置文件中指定的这些限制。
set rlimit RESOURCE <= value,
RESOURCE可为:
- cpu:时间限制,以秒为单位
- fsize、data、stack、core、rss、as、memlock、msgqueue:
以字节为单位的数字,或者带后缀的数字,例如,该后缀可以是 K/KB(千字节)、M/MB(兆字节)、G/GB(千兆字节) - fsize、nofile、locks、sigpending、nproc*、rtprio:
大于或等于 0 的数字 - nice:
-20 到 19 的值
3. 网络访问控制(newtwork)
AppArmor 允许基于地址类型和地址族调解网络访问。
network [[][][]]
- 支持的域:inet、ax25、ipx、appletalk、netrom、bridge、x25、inet6、rose、netbeui、security、key、packet、ash、econet、atmsvc、sna、pppox、wanpipe、bluetooth、unix、atmpvc,netlink、llc、can、tipc、iucv、rxrpc、isdn、phonet、ieee802154、caif、alg、nfc、vsock
- 支持的类型:stream、dgram、seqpacket、rdm、raw、packet
- 支持的协议:tcp、udp、icmp
network #允许所有网络。不应用与域、类型或协议相关的限制。
network tcp #允许使用 IPv4 和 IPv6 TCP 网络。
network inet stream #允许使用 IPv4 TCP 网络
4. Capability条目
Linux capability是一种对于对 root 权限进行更细粒度的控制,实现按需授权,从而减小系统的安全攻击面的安全机制,用于补充对于root权限粒度控制不足的划分。
主要分为文件和进程capability,具体可参考man capabilities中的capability列表。
capability, # grant all capabilities
5. Mount控制(mount/remount/umount)
AppArmor 可以限制装入和卸载操作,包括文件系统类型和装入标志。
规则语法基于 mount 命令语法,以 mount、remount 或 umount 关键字开头。
条件是可选项,如果不指定条件,则认为要匹配所有项。
mount options=ro /dev/foo -E /mnt/,
mount options in (ro,atime) /dev/foo -> /mnt/,
mount options=ro options=atime
6. Pivot root控制
AppArmor 可以限制对根文件系统的更改
pivot_root [oldroot=OLD_ROOT] NEW_ROOT
7. Ptrace控制
AppArmor 支持限制 ptrace 系统调用。
trace 和 tracedby 权限控制 ptrace(2);read 和 readby 控制 proc(5) 文件系统访问、kcmp(2)、futexes (get_robust_list(2)) 和 perf 跟踪事件。
ptrace, # Allow all PTrace access
ptrace (read, readby, trace, tracedby), # Explicitly allow all PTrace access,
deny ptrace (trace), # Explicitly deny use of ptrace(2)
ptrace (readby, tracedby) peer=unconfined, # Allow unconfined processes (eg, a debugger) to ptrace us
ptrace (trace) peer=/usr/bin/foo, # Allow ptrace of a process running under the /usr/bin/foo profile
8. Signal控制
AppArmor 支持限制进程间的信号,授予的信号权限是全部所列信号规则权限的并集。如果规则未显式指明访问列表,则隐式应用 AppArmor 信号权限。
发送方进程和接收方进程都必须拥有正确的权限。
9. Dbus控制
dbus是其中的桌面消息机制的一个标准,由freedesktop组织制定。
dbus分为两种总线,一种叫SystemBus,一种叫SessionBus。SystemBus只有一条,SessionBus是一个用户会话时会产生一条。SystemBus一般是用于权限较高的系统级(root)进程与其他进程(可以是普通进程)的通信,而SessionBus是用于普通的用户进程之间的交流。
include <abstractions/dbus>
@{run}/dbus/system_bus_socket rw,
10. Unix socket控制
socket套接字是一种可以进行网络通信的内核对象,它是一个唯一的标示符,一般称它为socket描述符。
@{run}/avahi-daemon/socket w,
11. Change_profile控制
profile,路径:/etc/profile,用于设置系统级的环境变量和启动程序,在这个文件下配置会对所有用户生效。当用户登录(login)时,文件会被执行,并从/etc/profile.d目录的配置文件中查找shell设置。
change_profile -> php-fpm//*,# allow switching processes to those subprofiles
12. 审计规则
AppArmor 提供用于审计给定规则的功能,当匹配这些规则时,审计日志中会显示审计消息。
要对给定的规则启用审计消息,可在规则的前面添加 audit 关键字。
audit /etc/foo/* rw,