×

限制 seccomp 配置文件中允许的系统调用

安全配置文件操作符默认情况下不会限制seccomp配置文件中的系统调用。您可以在spod配置中定义允许的系统调用列表。

步骤
  • 要定义allowedSyscalls列表,请通过运行以下命令调整spec参数

    $ oc -n openshift-security-profiles patch spod spod --type merge \
        -p '{"spec":{"allowedSyscalls": ["exit", "exit_group", "futex", "nanosleep"]}}'

操作符将只安装seccomp配置文件,这些配置文件具有定义在允许列表中的系统调用子集。所有不符合此规则集的配置文件都将被拒绝。

当在spod配置中修改允许的syscalls列表时,Operator会自动识别并删除已安装的非兼容配置文件。

容器运行时的基本系统调用

您可以使用baseProfileName属性来设置给定运行时启动容器所需的最小syscalls

步骤
  • 编辑SeccompProfile类型对象,并在spec字段中添加baseProfileName: runc-v1.0.0

    apiVersion: security-profiles-operator.x-k8s.io/v1beta1
    kind: SeccompProfile
    metadata:
      namespace: my-namespace
      name: example-name
    spec:
      defaultAction: SCMP_ACT_ERRNO
      baseProfileName: runc-v1.0.0
      syscalls:
        - action: SCMP_ACT_ALLOW
          names:
            - exit_group

启用spod守护进程中的内存优化

启用配置文件记录时,在spod守护进程内部运行的控制器会监控集群中所有可用的Pod。这在大型集群中可能导致非常高的内存使用率,从而导致spod守护进程内存不足或崩溃。

为防止崩溃,可以将spod守护进程配置为仅将标记为需要配置文件记录的Pod加载到缓存内存中。

SPO内存优化默认未启用。

步骤
  1. 运行以下命令启用内存优化:

    $ oc -n openshift-security-profiles patch spod spod --type=merge -p '{"spec":{"enableMemoryOptimization":true}}'
  2. 要为Pod记录安全配置文件,必须使用spo.x-k8s.io/enable-recording: "true"标签标记该Pod。

    apiVersion: v1
    kind: Pod
    metadata:
      name: my-recording-pod
      labels:
        spo.x-k8s.io/enable-recording: "true"
    # ...

自定义守护进程资源需求

可以使用spod配置中的daemonResourceRequirements字段调整守护进程容器的默认资源需求。

步骤
  • 要指定守护进程容器的内存和CPU请求和限制,请运行以下命令:

    $ oc -n openshift-security-profiles patch spod spod --type merge -p \
        '{"spec":{"daemonResourceRequirements": { \
        "requests": {"memory": "256Mi", "cpu": "250m"}, \
        "limits": {"memory": "512Mi", "cpu": "500m"}}}}'

为spod守护进程Pod设置自定义优先级类名称

spod守护进程Pod的默认优先级类名称设置为system-node-critical。可以通过在spod配置中设置priorityClassName字段的值来配置自定义优先级类名称。

步骤
  • 运行以下命令配置优先级类名称:

    $ oc -n openshift-security-profiles patch spod spod --type=merge -p '{"spec":{"priorityClassName":"my-priority-class"}}'
    示例输出
    securityprofilesoperatordaemon.openshift-security-profiles.x-k8s.io/spod patched

使用指标

openshift-security-profiles命名空间提供指标端点,这些端点受kube-rbac-proxy容器保护。所有指标都由openshift-security-profiles命名空间内的metrics服务公开。

安全配置文件Operator包含一个集群角色和相应的绑定spo-metrics-client,用于从集群内检索指标。有两个指标路径可用:

  • metrics.openshift-security-profiles/metrics:用于控制器运行时指标

  • metrics.openshift-security-profiles/metrics-spod:用于Operator守护进程指标

步骤
  1. 要查看指标服务的状况,请运行以下命令:

    $ oc get svc/metrics -n openshift-security-profiles
    示例输出
    NAME      TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
    metrics   ClusterIP   10.0.0.228   <none>        443/TCP   43s
  2. 要检索指标,请使用openshift-security-profiles命名空间中的默认ServiceAccount令牌查询服务端点,方法是运行以下命令:

    $ oc run --rm -i --restart=Never --image=registry.fedoraproject.org/fedora-minimal:latest \
        -n openshift-security-profiles metrics-test -- bash -c \
        'curl -ks -H "Authorization: Bearer $(cat /var/run/secrets/kubernetes.io/serviceaccount/token)" https://metrics.openshift-security-profiles/metrics-spod'
    示例输出
    # HELP security_profiles_operator_seccomp_profile_total Counter about seccomp profile operations.
    # TYPE security_profiles_operator_seccomp_profile_total counter
    security_profiles_operator_seccomp_profile_total{operation="delete"} 1
    security_profiles_operator_seccomp_profile_total{operation="update"} 2
  3. 要从不同的命名空间检索指标,请将ServiceAccount链接到spo-metrics-client ClusterRoleBinding,方法是运行以下命令:

    $ oc get clusterrolebinding spo-metrics-client -o wide
    示例输出
    NAME                 ROLE                             AGE   USERS   GROUPS   SERVICEACCOUNTS
    spo-metrics-client   ClusterRole/spo-metrics-client   35m                    openshift-security-profiles/default

controller-runtime 指标

controller-runtime metrics和DaemonSet端点metrics-spod提供一组默认指标。守护进程还提供其他指标,这些指标始终以security_profiles_operator_为前缀。

表1. 可用的controller-runtime指标
指标键 可能的标签 类型 用途

seccomp_profile_total

operation={delete,update}

计数器

Seccomp配置文件操作的数量。

seccomp_profile_audit_total

node, namespace, pod, container, executable, syscall

计数器

Seccomp配置文件审计操作的数量。需要启用日志增强功能。

seccomp_profile_bpf_total

node, mount_namespace, profile

计数器

Seccomp配置文件bpf操作的数量。需要启用bpf记录器。

seccomp_profile_error_total

reason={
SeccompNotSupportedOnNode,
InvalidSeccompProfile,
CannotSaveSeccompProfile,
CannotRemoveSeccompProfile,
CannotUpdateSeccompProfile,
CannotUpdateNodeStatus
}

计数器

Seccomp配置文件错误的数量。

selinux_profile_total

operation={delete,update}

计数器

SELinux配置文件操作的数量。

selinux_profile_audit_total

node, namespace, pod, container, executable, scontext,tcontext

计数器

SELinux配置文件审计操作的数量。需要启用日志增强功能。

selinux_profile_error_total

reason={
CannotSaveSelinuxPolicy,
CannotUpdatePolicyStatus,
CannotRemoveSelinuxPolicy,
CannotContactSelinuxd,
CannotWritePolicyFile,
CannotGetPolicyStatus
}

计数器

SELinux配置文件错误的数量。

使用日志增强器

安全配置文件Operator包含一个日志增强功能,该功能默认情况下处于禁用状态。日志增强器容器以privileged权限运行,以读取本地节点的审计日志。日志增强器在主机PID命名空间hostPID中运行。

日志增强器必须具有读取主机进程的权限。

步骤
  1. 运行以下命令修补spod配置以启用日志增强器:

    $ oc -n openshift-security-profiles patch spod spod \
        --type=merge -p '{"spec":{"enableLogEnricher":true}}'
    示例输出
    securityprofilesoperatordaemon.security-profiles-operator.x-k8s.io/spod patched

    安全配置文件Operator将自动重新部署spod DaemonSet。

  2. 运行以下命令查看审计日志:

    $ oc -n openshift-security-profiles logs -f ds/spod log-enricher
    示例输出
    I0623 12:51:04.257814 1854764 deleg.go:130] setup "msg"="starting component: log-enricher"  "buildDate"="1980-01-01T00:00:00Z" "compiler"="gc" "gitCommit"="unknown" "gitTreeState"="clean" "goVersion"="go1.16.2" "platform"="linux/amd64" "version"="0.4.0-dev"
    I0623 12:51:04.257890 1854764 enricher.go:44] log-enricher "msg"="Starting log-enricher on node: 127.0.0.1"
    I0623 12:51:04.257898 1854764 enricher.go:46] log-enricher "msg"="Connecting to local GRPC server"
    I0623 12:51:04.258061 1854764 enricher.go:69] log-enricher "msg"="Reading from file /var/log/audit/audit.log"
    2021/06/23 12:51:04 Seeked /var/log/audit/audit.log - &{Offset:0 Whence:2}

使用日志增强器跟踪应用程序

您可以使用安全配置文件Operator日志增强器来跟踪应用程序。

步骤
  1. 要跟踪应用程序,请创建一个SeccompProfile日志记录配置文件。

    apiVersion: security-profiles-operator.x-k8s.io/v1beta1
    kind: SeccompProfile
    metadata:
      name: log
      namespace: default
    spec:
      defaultAction: SCMP_ACT_LOG
  2. 创建一个Pod对象以使用该配置文件。

    apiVersion: v1
    kind: Pod
    metadata:
      name: log-pod
    spec:
      securityContext:
        runAsNonRoot: true
        seccompProfile:
          type: Localhost
          localhostProfile: operator/default/log.json
      containers:
      - name: log-container
        image: quay.io/security-profiles-operator/test-nginx-unprivileged:1.21
        securityContext:
          allowPrivilegeEscalation: false
          capabilities:
            drop: [ALL]
  3. 运行以下命令检查日志增强器输出:

    $ oc -n openshift-security-profiles logs -f ds/spod log-enricher
    示例输出
    …
    I0623 12:59:11.479869 1854764 enricher.go:111] log-enricher "msg"="audit"  "container"="log-container" "executable"="/" "namespace"="default" "node"="127.0.0.1" "pid"=1905792 "pod"="log-pod" "syscallID"=3 "syscallName"="close" "timestamp"="1624453150.205:1061" "type"="seccomp"
    I0623 12:59:11.487323 1854764 enricher.go:111] log-enricher "msg"="audit"  "container"="log-container" "executable"="/" "namespace"="default" "node"="127.0.0.1" "pid"=1905792 "pod"="log-pod" "syscallID"=157 "syscallName"="prctl" "timestamp"="1624453150.205:1062" "type"="seccomp"
    I0623 12:59:11.492157 1854764 enricher.go:111] log-enricher "msg"="audit"  "container"="log-container" "executable"="/" "namespace"="default" "node"="127.0.0.1" "pid"=1905792 "pod"="log-pod" "syscallID"=157 "syscallName"="prctl" "timestamp"="1624453150.205:1063" "type"="seccomp"
    …
    I0623 12:59:20.258523 1854764 enricher.go:111] log-enricher "msg"="audit"  "container"="log-container" "executable"="/usr/sbin/nginx" "namespace"="default" "node"="127.0.0.1" "pid"=1905792 "pod"="log-pod" "syscallID"=12 "syscallName"="brk" "timestamp"="1624453150.235:2873" "type"="seccomp"
    I0623 12:59:20.263349 1854764 enricher.go:111] log-enricher "msg"="audit"  "container"="log-container" "executable"="/usr/sbin/nginx" "namespace"="default" "node"="127.0.0.1" "pid"=1905792 "pod"="log-pod" "syscallID"=21 "syscallName"="access" "timestamp"="1624453150.235:2874" "type"="seccomp"
    I0623 12:59:20.354091 1854764 enricher.go:111] log-enricher "msg"="audit"  "container"="log-container" "executable"="/usr/sbin/nginx" "namespace"="default" "node"="127.0.0.1" "pid"=1905792 "pod"="log-pod" "syscallID"=257 "syscallName"="openat" "timestamp"="1624453150.235:2875" "type"="seccomp"
    I0623 12:59:20.358844 1854764 enricher.go:111] log-enricher "msg"="audit"  "container"="log-container" "executable"="/usr/sbin/nginx" "namespace"="default" "node"="127.0.0.1" "pid"=1905792 "pod"="log-pod" "syscallID"=5 "syscallName"="fstat" "timestamp"="1624453150.235:2876" "type"="seccomp"
    I0623 12:59:20.363510 1854764 enricher.go:111] log-enricher "msg"="audit"  "container"="log-container" "executable"="/usr/sbin/nginx" "namespace"="default" "node"="127.0.0.1" "pid"=1905792 "pod"="log-pod" "syscallID"=9 "syscallName"="mmap" "timestamp"="1624453150.235:2877" "type"="seccomp"
    I0623 12:59:20.454127 1854764 enricher.go:111] log-enricher "msg"="audit"  "container"="log-container" "executable"="/usr/sbin/nginx" "namespace"="default" "node"="127.0.0.1" "pid"=1905792 "pod"="log-pod" "syscallID"=3 "syscallName"="close" "timestamp"="1624453150.235:2878" "type"="seccomp"
    I0623 12:59:20.458654 1854764 enricher.go:111] log-enricher "msg"="audit"  "container"="log-container" "executable"="/usr/sbin/nginx" "namespace"="default" "node"="127.0.0.1" "pid"=1905792 "pod"="log-pod" "syscallID"=257 "syscallName"="openat" "timestamp"="1624453150.235:2879" "type"="seccomp"
    …

配置Webhook

配置文件绑定和配置文件记录对象可以使用Webhook。配置文件绑定和记录对象配置是MutatingWebhookConfiguration CR,由安全配置文件Operator管理。

要更改Webhook配置,spod CR 公开了一个webhookOptions字段,允许修改failurePolicynamespaceSelectorobjectSelector变量。这允许您将Webhook设置为“软失败”或将其限制为命名空间的子集,以便即使Webhook失败,也不会影响其他命名空间或资源。

步骤
  1. 通过创建以下补丁文件,将recording.spo.io Webhook配置设置为仅记录标记为spo-record=true的Pod:

    spec:
      webhookOptions:
        - name: recording.spo.io
          objectSelector:
            matchExpressions:
              - key: spo-record
                operator: In
                values:
                  - "true"
  2. 运行以下命令修补spod/spod实例:

    $ oc -n openshift-security-profiles patch spod \
        spod -p $(cat /tmp/spod-wh.patch) --type=merge
  3. 要查看生成的MutatingWebhookConfiguration对象,请运行以下命令:

    $ oc get MutatingWebhookConfiguration \
        spo-mutating-webhook-configuration -oyaml