iT邦幫忙

2025 iThome 鐵人賽

DAY 14
0
Cloud Native

我在 CKS 考完只拿 47% 後痛定思痛決定好好準備內容系列 第 14

[Day14] 3-4. Appropriately use kernel hardening tools such as AppArmor, seccomp

  • 分享至 

  • xImage
  •  

我們已經討論完使用者的登入、使用者登入方式的限制、是否可以提權以及網路的限制後,今天要討論的是使用者在下指令時的限制,像是當使用者可以執行 sudo 後,是不是可以進一步限制能做什麼,除了原先的 user group 外,我們也可以透過第三方的服務來進行限制,限制時主要會限制 syscall 的呼叫。

Linux syscalls

  1. Linux 可以分成三層
    (1) Application / Process (User Space)
  • C, Java, Python, Ruby
  • Open a File, Write a File ......
    (2) Linux Kernal (Kernel Spec)
  • Kernal Code
  • Kernal Extensions
  • Device Drivers
    (3) Memory, CPU, Devices
  1. 背後運作方式
    當開啟用個檔案 touch /tmp/error.log
    會執行
  • open()
  • close()
  • evecve ()
  • readdir()
  • strlen()
  • closedir()
  1. Tracing syscalls
    使用 strace,如果要列出使用哪些 syscall 以及時間可以用 strace -c
    (1) 追蹤行為
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

可以透過 tracee 監控 container 中的 syscall

[軟體]seccomp Restrict syscalls using seccomp

使用時可以透過配置 seccomp 以限制從 userspace 到 kernel 的請求

syscall 配置 - 可以分成白名單或者黑名單的模式

  1. 白名單 - 透過 defaultAction 給 "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"
        }
    ]
}    
  1. 黑名單 - defaultAction 與 action 描述對調
{
    "defaultAction": "SCMP_ACT_ALLOW", 
    "architectures": [
        "SCMP_ARCH_X86_64",
        "SCMP_ARCH_X86",
        "SCMP_ARCH_X32"
    ],
    "syscalls": [
        {
            "names": [
                "accept4",
                "epoll_wait",
    ...
            ],
            "action": "SCMP_ACT_ERRNO"
        }
    ]
}    

pod 配置

需要在 securityContext 中加入 seccompProfile

  1. 指定類型
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
  1. 可指定配置檔案路徑
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

[軟體]Apparmor - 檔案讀取限制

相較於 syscall 的限制 Apparmor 提供的是對於檔案 rwx 直接的限制,只能讀/寫/執行哪些路徑底下的檔案

  1. 使用指令

安裝

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>
  1. Config 配置檔

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,
}
  1. pod 配置指定Apparmor
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

Linux Capabilities 是一個精細化的權限控制系統,用來取代傳統的 root/非root 的權限模式,將 root 的「超級權限」拆分成多個獨立的能力
常見會使用的有
修改系統時間 - CAP_SYS_TIME
掛載檔案系統 - CAP_SYS_ADMIN


上一篇
[Day13] 3-3. Minimize external access to the network
下一篇
[Day15] 4-1. Use appropriate pod security standards
系列文
我在 CKS 考完只拿 47% 後痛定思痛決定好好準備內容17
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言