我們已經討論完使用者的登入、使用者登入方式的限制、是否可以提權以及網路的限制後,今天要討論的是使用者在下指令時的限制,像是當使用者可以執行 sudo 後,是不是可以進一步限制能做什麼,除了原先的 user group 外,我們也可以透過第三方的服務來進行限制,限制時主要會限制 syscall 的呼叫。
touch /tmp/error.log
strace
,如果要列出使用哪些 syscall 以及時間可以用 strace -c
strace touch /tmp/error.log
(2) 追蹤 system
pidof etcd
strace -p <pid>
使用時會像是這樣輸出
strace -c ls
audit.json
% time seconds usecs/call calls errors syscall
------ ----------- ----------- --------- --------- ----------------
17.93 0.000216 12 18 mmap
10.71 0.000129 18 7 openat
10.46 0.000126 18 7 mprotect
9.96 0.000120 13 9 close
9.13 0.000110 13 8 newfstatat
6.97 0.000084 21 4 pread64
6.47 0.000078 15 5 read
4.23 0.000051 25 2 getdents64
3.57 0.000043 21 2 2 statfs
2.99 0.000036 18 2 ioctl
2.90 0.000035 35 1 write
2.82 0.000034 11 3 brk
2.07 0.000025 25 1 munmap
1.66 0.000020 10 2 2 access
1.41 0.000017 17 1 prlimit64
1.41 0.000017 17 1 getrandom
1.33 0.000016 8 2 1 arch_prctl
1.33 0.000016 16 1 set_tid_address
1.33 0.000016 16 1 set_robust_list
1.33 0.000016 16 1 rseq
0.00 0.000000 0 1 execve
------ ----------- ----------- --------- --------- ----------------
100.00 0.001205 15 79 5 total
可以透過 tracee 監控 container 中的 syscall
使用時可以透過配置 seccomp 以限制從 userspace 到 kernel 的請求
"SCMP_ACT_ERRNO"
並且由 SCMP_ACT_ALLOW
正向表列放行{
"defaultAction": "SCMP_ACT_ERRNO",
"architectures": [
"SCMP_ARCH_X86_64",
"SCMP_ARCH_X86",
"SCMP_ARCH_X32"
],
"syscalls": [
{
"names": [
"accept4",
"epoll_wait",
...
],
"action": "SCMP_ACT_ALLOW"
}
]
}
{
"defaultAction": "SCMP_ACT_ALLOW",
"architectures": [
"SCMP_ARCH_X86_64",
"SCMP_ARCH_X86",
"SCMP_ARCH_X32"
],
"syscalls": [
{
"names": [
"accept4",
"epoll_wait",
...
],
"action": "SCMP_ACT_ERRNO"
}
]
}
需要在 securityContext
中加入 seccompProfile
apiVersion: v1
kind: Pod
metadata:
name: default-pod
labels:
app: default-pod
spec:
securityContext:
seccompProfile:
type: RuntimeDefault
containers:
- name: test-container
image: hashicorp/http-echo:1.0
args:
- "-text=just made some more syscalls!"
securityContext:
allowPrivilegeEscalation: false
apiVersion: v1
kind: Pod
metadata:
name: audit-pod
labels:
app: audit-pod
spec:
securityContext:
seccompProfile:
type: Localhost
localhostProfile: profiles/audit.json # 可指定配置檔案路徑
containers:
- name: test-container
image: hashicorp/http-echo:1.0
args:
- "-text=just made some syscalls!"
securityContext:
allowPrivilegeEscalation: false
相較於 syscall 的限制 Apparmor 提供的是對於檔案 rwx 直接的限制,只能讀/寫/執行哪些路徑底下的檔案
安裝
apt-get install -y apparmor-utils
查看 Apparmor 狀態
cat /sys/module/apparmor/parameters/enabled
查看目前運作的 profile
sudo cat /sys/kernel/security/apparmor/profiles
# 或者是
aa-status
匯入自建檔案
sudo apparmor_parser -q <file.path>
profile 名稱 k8s-apparmor-example-deny-write
#include <tunables/global>
profile k8s-apparmor-example-deny-write flags=(attach_disconnected) {
#include <abstractions/base>
file,
# Deny all file writes.
deny /** w,
}
apiVersion: v1
kind: Pod
metadata:
name: hello-apparmor
spec:
securityContext:
appArmorProfile:
type: Localhost
localhostProfile: k8s-apparmor-example-deny-write
containers:
- name: hello
image: busybox:1.28
command: [ "sh", "-c", "echo 'Hello AppArmor!' && sleep 1h" ]
Linux Capabilities 是一個精細化的權限控制系統,用來取代傳統的 root/非root 的權限模式,將 root 的「超級權限」拆分成多個獨立的能力
常見會使用的有
修改系統時間 - CAP_SYS_TIME
掛載檔案系統 - CAP_SYS_ADMIN