7.7. SELinux 프로필 관리
SELinux 프로필을 생성 및 관리하고 워크로드에 바인딩합니다.
Security Profiles Operator는 RHCOS(Red Hat Enterprise Linux CoreOS) 작업자 노드만 지원합니다. RHEL(Red Hat Enterprise Linux) 노드는 지원되지 않습니다.
7.7.1. SELinux 프로필 생성 링크 복사링크가 클립보드에 복사되었습니다!
SelinuxProfile 오브젝트를 사용하여 프로필을 생성합니다.
SelinuxProfile 오브젝트에는 보안 강화 및 가독성을 개선할 수 있는 몇 가지 기능이 있습니다.
-
현재 네임스페이스 또는 시스템 전체 프로필로 상속하도록 프로필을 제한합니다. 일반적으로 시스템에는 많은 프로필이 설치되어 있지만 클러스터 워크로드만 사용해야 하므로 상속 가능한 시스템 프로필은
spec.selinuxOptions.allowedSystemProfiles의spod인스턴스에 나열됩니다. - 권한, 클래스 및 라벨에 대한 기본 검증을 수행합니다.
-
정책을 사용하는 프로세스를 설명하는 새 키워드
@self를 추가합니다. 이를 통해 정책 사용이 이름과 네임스페이스를 기반으로 하므로 워크로드와 네임스페이스 간 정책을 쉽게 재사용할 수 있습니다. - SELinux CIL 언어로 직접 프로필을 작성하는 것과 비교하여 보안 강화 및 가독성 향상을 위한 기능을 추가합니다.
절차
다음 명령을 실행하여 프로젝트를 생성합니다.
$ oc new-project nginx-deploy다음
SelinuxProfile오브젝트를 생성하여 권한이 없는 워크로드와 함께 사용할 수 있는 정책을 생성합니다.apiVersion: security-profiles-operator.x-k8s.io/v1alpha2 kind: SelinuxProfile metadata: name: nginx-secure namespace: nginx-deploy spec: allow: '@self': tcp_socket: - listen http_cache_port_t: tcp_socket: - name_bind node_t: tcp_socket: - node_bind inherit: - kind: System name: container다음 명령을 실행하여
selinuxd가 정책을 설치할 때까지 기다립니다.$ oc wait --for=condition=ready -n nginx-deploy selinuxprofile nginx-secure출력 예
selinuxprofile.security-profiles-operator.x-k8s.io/nginx-secure condition met정책은 Security Profiles Operator가 소유한 컨테이너의
emptyDir에 배치됩니다. 정책은/etc/selinux.d/<name>_<namespace>.cil의 CIL(Common Intermediate Language) 형식으로 저장됩니다.다음 명령을 실행하여 Pod에 액세스합니다.
$ oc -n openshift-security-profiles rsh -c selinuxd ds/spod
검증
다음 명령을 실행하여
cat을 사용하여 파일 내용을 확인합니다.$ cat /etc/selinux.d/nginx-secure_nginx-deploy.cil출력 예
(block nginx-secure_nginx-deploy (blockinherit container) (allow process nginx-secure_nginx-deploy.process ( tcp_socket ( listen ))) (allow process http_cache_port_t ( tcp_socket ( name_bind ))) (allow process node_t ( tcp_socket ( node_bind ))) )다음 명령을 실행하여 정책이 설치되었는지 확인합니다.
$ semodule -l | grep nginx-secure출력 예
nginx-secure_nginx-deploy
7.7.2. 포드에 SELinux 프로필 적용 링크 복사링크가 클립보드에 복사되었습니다!
생성된 프로필 중 하나를 적용하려면 Pod를 생성합니다.
SELinux 프로필의 경우 권한 있는 워크로드를 허용하려면 네임스페이스에 라벨을 지정해야 합니다.
절차
다음 명령을 실행하여
scc.podSecurityLabelSync=false라벨을nginx-deploy네임스페이스에 적용합니다.$ oc label ns nginx-deploy security.openshift.io/scc.podSecurityLabelSync=false다음 명령을 실행하여
nginx-deploy네임스페이스에privileged레이블을 적용합니다.$ oc label ns nginx-deploy --overwrite=true pod-security.kubernetes.io/enforce=privileged다음 명령을 실행하여 SELinux 프로필 사용 문자열을 가져옵니다.
$ oc get selinuxprofile.security-profiles-operator.x-k8s.io/nginx-secure -n nginx-deploy -ojsonpath='{.status.usage}'출력 예
nginx-secure_nginx-deploy.process.spec.containers[].securityContext.seLinuxOptions속성의 워크로드 매니페스트에 출력 문자열을 적용합니다.apiVersion: v1 kind: Pod metadata: name: nginx-secure namespace: nginx-deploy spec: containers: - image: nginxinc/nginx-unprivileged:1.21 name: nginx securityContext: seLinuxOptions: # NOTE: This uses an appropriate SELinux type type: nginx-secure_nginx-deploy.process중요워크로드를 생성하기 전에 SELinux
유형이있어야 합니다.
7.7.2.1. SELinux 로그 정책 적용 링크 복사링크가 클립보드에 복사되었습니다!
정책 위반 또는 AVC 거부를 기록하려면 SElinuxProfile 프로필을 허용으로 설정합니다.
이 절차에서는 로깅 정책을 정의합니다. 시행 정책을 설정하지 않습니다.
절차
SElinuxProfile에permissive: true를 추가합니다.apiVersion: security-profiles-operator.x-k8s.io/v1alpha2 kind: SelinuxProfile metadata: name: nginx-secure namespace: nginx-deploy spec: permissive: true
7.7.2.2. ProfileBindings를 사용하여 워크로드에 프로필 바인딩 링크 복사링크가 클립보드에 복사되었습니다!
ProfileBinding 리소스를 사용하여 보안 프로필을 컨테이너의 SecurityContext 에 바인딩할 수 있습니다.
절차
quay.io/security-profiles-operator/test-nginx-unprivileged:1.21이미지를 예제SelinuxProfile프로필로 사용하는 Pod를 바인딩하려면 pod 및SelinuxProfile오브젝트와 동일한 네임스페이스에ProfileBinding오브젝트를 생성합니다.apiVersion: security-profiles-operator.x-k8s.io/v1alpha1 kind: ProfileBinding metadata: namespace: my-namespace name: nginx-binding spec: profileRef: kind: SelinuxProfile1 name: profile2 image: quay.io/security-profiles-operator/test-nginx-unprivileged:1.213 중요image: "*"와일드카드 속성을 사용하면 모든 새 Pod가 지정된 네임스페이스의 기본 보안 프로필로 바인딩됩니다.다음 명령을 실행하여
enable-binding=true로 네임스페이스에 레이블을 지정합니다.$ oc label ns my-namespace spo.x-k8s.io/enable-binding=truetest-pod.yaml이라는 Pod를 정의합니다.apiVersion: v1 kind: Pod metadata: name: test-pod spec: containers: - name: test-container image: quay.io/security-profiles-operator/test-nginx-unprivileged:1.21Pod를 생성합니다.
$ oc create -f test-pod.yaml참고Pod가 이미 존재하는 경우 바인딩이 제대로 작동하려면 Pod를 다시 생성해야 합니다.
검증
다음 명령을 실행하여 Pod가
ProfileBinding을 상속하는지 확인합니다.$ oc get pod test-pod -o jsonpath='{.spec.containers[*].securityContext.seLinuxOptions.type}'출력 예
profile_nginx-binding.process
7.7.2.3. 컨트롤러 및 SecurityContextConstraints 복제 링크 복사링크가 클립보드에 복사되었습니다!
배포 또는 데몬 세트와 같은 컨트롤러를 복제하기 위한 SELinux 정책을 배포할 때 컨트롤러에서 생성한 Pod 오브젝트는 워크로드를 생성하는 사용자의 ID로 실행되지 않습니다. ServiceAccount 를 선택하지 않으면 사용자 정의 보안 정책 사용을 허용하지 않는 제한된 SCC( SecurityContextConstraints )를 사용하여 Pod를 되돌릴 수 있습니다.
절차
다음 명령을 실행하여 프로젝트를 생성합니다.
$ oc new-project nginx-securenginx-secure네임스페이스에서 SELinux 정책을 사용할 수 있도록 다음RoleBinding오브젝트를 생성합니다.kind: RoleBinding apiVersion: rbac.authorization.k8s.io/v1 metadata: name: spo-nginx namespace: nginx-secure subjects: - kind: ServiceAccount name: spo-deploy-test roleRef: kind: Role name: spo-nginx apiGroup: rbac.authorization.k8s.ioRole오브젝트를 생성합니다.apiVersion: rbac.authorization.k8s.io/v1 kind: Role metadata: creationTimestamp: null name: spo-nginx namespace: nginx-secure rules: - apiGroups: - security.openshift.io resources: - securitycontextconstraints resourceNames: - privileged verbs: - useServiceAccount오브젝트를 생성합니다.apiVersion: v1 kind: ServiceAccount metadata: creationTimestamp: null name: spo-deploy-test namespace: nginx-secureDeployment오브젝트를 생성합니다.apiVersion: apps/v1 kind: Deployment metadata: name: selinux-test namespace: nginx-secure metadata: labels: app: selinux-test spec: replicas: 3 selector: matchLabels: app: selinux-test template: metadata: labels: app: selinux-test spec: serviceAccountName: spo-deploy-test securityContext: seLinuxOptions: type: nginx-secure_nginx-secure.process1 containers: - name: nginx-unpriv image: quay.io/security-profiles-operator/test-nginx-unprivileged:1.21 ports: - containerPort: 8080- 1
- 배포를 생성하기 전에
.seLinuxOptions.type이 있어야 합니다.
참고SELinux 유형은 워크로드에 지정되지 않으며 SCC에서 처리합니다. 배포 및
ReplicaSet을 통해 Pod를 생성하면 Pod가 적절한 프로필과 함께 실행됩니다.
올바른 서비스 계정에서만 SCC를 사용할 수 있는지 확인합니다. 자세한 내용은 추가 리소스를 참조하십시오.
7.7.3. 워크로드에서 프로필 기록 링크 복사링크가 클립보드에 복사되었습니다!
Security Profiles Operator는 ProfileRecording 오브젝트로 시스템 호출을 기록할 수 있으므로 애플리케이션에 대한 기준 프로필을 더 쉽게 생성할 수 있습니다.
SELinux 프로필 기록에 로그 보강기를 사용하는 경우 로그 보강기 기능이 활성화되어 있는지 확인합니다. 자세한 내용은 추가 리소스를 참조하십시오.
privileged: true security context restraints가 있는 컨테이너는 로그 기반 레코딩을 방지합니다. 권한 있는 컨테이너에는 SELinux 정책이 적용되지 않으며 로그 기반 레코딩은 이벤트를 기록하기 위해 특수 SELinux 프로필을 사용합니다.
절차
다음 명령을 실행하여 프로젝트를 생성합니다.
$ oc new-project my-namespace다음 명령을 실행하여
enable-recording=true로 네임스페이스에 레이블을 지정합니다.$ oc label ns my-namespace spo.x-k8s.io/enable-recording=truerecorder: logs변수를 포함하는ProfileRecording오브젝트를 생성합니다.apiVersion: security-profiles-operator.x-k8s.io/v1alpha1 kind: ProfileRecording metadata: namespace: my-namespace name: test-recording spec: kind: SelinuxProfile recorder: logs podSelector: matchLabels: app: my-app기록할 워크로드를 생성합니다.
apiVersion: v1 kind: Pod metadata: namespace: my-namespace name: my-pod labels: app: my-app spec: containers: - name: nginx image: quay.io/security-profiles-operator/test-nginx-unprivileged:1.21 ports: - containerPort: 8080 - name: redis image: quay.io/security-profiles-operator/redis:6.2.1다음 명령을 입력하여 Pod가
Running상태인지 확인합니다.$ oc -n my-namespace get pods출력 예
NAME READY STATUS RESTARTS AGE my-pod 2/2 Running 0 18s보강기에서 해당 컨테이너에 대한 감사 로그를 수신함을 나타냅니다.
$ oc -n openshift-security-profiles logs --since=1m --selector name=spod -c log-enricher출력 예
I0517 13:55:36.383187 348295 enricher.go:376] log-enricher "msg"="audit" "container"="redis" "namespace"="my-namespace" "node"="ip-10-0-189-53.us-east-2.compute.internal" "perm"="name_bind" "pod"="my-pod" "profile"="test-recording_redis_6kmrb_1684331729" "scontext"="system_u:system_r:selinuxrecording.process:s0:c4,c27" "tclass"="tcp_socket" "tcontext"="system_u:object_r:redis_port_t:s0" "timestamp"="1684331735.105:273965" "type"="selinux"
검증
Pod를 제거합니다.
$ oc -n my-namepace delete pod my-podSecurity Profiles Operator가 두 가지 SELinux 프로필을 조정하는지 확인합니다.
$ oc get selinuxprofiles -lspo.x-k8s.io/recording-id=test-recording -n my-namespaceselinuxprofile의 출력 예
NAME USAGE STATE test-recording-nginx test-recording-nginx_my-namespace.process Installed test-recording-redis test-recording-redis_my-namespace.process Installed
7.7.3.1. 컨테이너별 프로필 인스턴스 병합 링크 복사링크가 클립보드에 복사되었습니다!
기본적으로 각 컨테이너 인스턴스는 별도의 프로필에 기록됩니다. Security Profiles Operator는 컨테이너별 프로필을 단일 프로필에 병합할 수 있습니다. 프로필 병합은 ReplicaSet 또는 Deployment 오브젝트를 사용하여 애플리케이션을 배포할 때 유용합니다.
절차
mergeStrategy: containers변수를 포함하도록ProfileRecording오브젝트를 편집합니다.apiVersion: security-profiles-operator.x-k8s.io/v1alpha1 kind: ProfileRecording metadata: # The name of the Recording is the same as the resulting SelinuxProfile CRD # after reconciliation. name: test-recording namespace: my-namespace spec: kind: SelinuxProfile recorder: logs mergeStrategy: containers podSelector: matchLabels: app: sp-record다음 명령을 실행하여 네임스페이스에 레이블을 지정합니다.
$ oc label ns my-namespace security.openshift.io/scc.podSecurityLabelSync=false pod-security.kubernetes.io/enforce=privileged pod-security.kubernetes.io/audit=privileged pod-security.kubernetes.io/warn=privileged --overwrite=true다음 YAML을 사용하여 워크로드를 생성합니다.
apiVersion: apps/v1 kind: Deployment metadata: name: nginx-deploy namespace: my-namespace spec: replicas: 3 selector: matchLabels: app: sp-record template: metadata: labels: app: sp-record spec: serviceAccountName: spo-record-sa containers: - name: nginx-record image: quay.io/security-profiles-operator/test-nginx-unprivileged:1.21 ports: - containerPort: 8080개별 프로필을 기록하려면 다음 명령을 실행하여 배포를 삭제합니다.
$ oc delete deployment nginx-deploy -n my-namespace프로필을 병합하려면 다음 명령을 실행하여 프로파일 레코딩을 삭제합니다.
$ oc delete profilerecording test-recording -n my-namespace병합 작업을 시작하고 결과 프로필을 생성하려면 다음 명령을 실행합니다.
$ oc get selinuxprofiles -lspo.x-k8s.io/recording-id=test-recording -n my-namespaceselinuxprofiles의 출력 예
NAME USAGE STATE test-recording-nginx-record test-recording-nginx-record_my-namespace.process Installed컨테이너에서 사용하는 권한을 보려면 다음 명령을 실행합니다.
$ oc get selinuxprofiles test-recording-nginx-record -o yaml
7.7.3.2. SELinuxContext 정보: RunAsAny 링크 복사링크가 클립보드에 복사되었습니다!
SELinux 정책 기록은 기록 중인 Pod에 특수 SELinux 유형을 삽입하는 Webhook를 사용하여 구현됩니다. SELinux 유형을 사용하면 Pod가 허용 모드에서 실행되고 모든 AVC 거부를 audit.log 에 로깅합니다. 기본적으로 워크로드는 사용자 지정 SELinux 정책으로 실행할 수 없지만 자동 생성된 유형을 사용합니다.
워크로드를 기록하려면 워크로드에서 Webhook에서 허용 SELinux 유형을 삽입할 수 있는 SCC를 사용할 수 있는 권한이 있는 서비스 계정을 사용해야 합니다. 권한 있는 SCC에는 seLinuxContext: RunAsAny 가 포함되어 있습니다.
또한 권한이 있는 Pod 보안 표준 에서만 사용자 지정 SELinux 정책을 사용할 수 있기 때문에 클러스터가 Pod Security Admission 을 활성화하는 경우 pod-security.kubernetes.io/enforce: 로 네임스페이스에 라벨을 지정해야 합니다.
privileged