Managing seccomp profiles¶
Create and manage seccomp profiles and bind them to workloads.
Warning
The Security Profiles Operator supports only Red Hat Enterprise Linux CoreOS (RHCOS) worker nodes. Red Hat Enterprise Linux (RHEL) nodes are not supported.
Create seccomp profiles¶
Use the SeccompProfile object to create seccomp profiles.
SeccompProfile objects can restrict syscalls within a container, limiting the access of your application.
Procedure
-
Create a project by running the following command:
-
Create the
SeccompProfileobject:apiVersion: security-profiles-operator.x-k8s.io/v1beta1 kind: SeccompProfile metadata: name: profile1 spec: defaultAction: SCMP_ACT_LOGThe seccomp profile will be saved in
/var/lib/kubelet/seccomp/operator/<namespace>/<name>.json.An
initcontainer creates the root directory of the Security Profiles Operator to run the Operator withoutrootgroup or user ID privileges. A symbolic link is created from the rootless profile storage/var/lib/openshift-security-profilesto the defaultseccomproot path inside of the kubelet root/var/lib/kubelet/seccomp/operator.
Apply seccomp profiles to a pod¶
To enforce a recorded or custom seccomp profile on a workload, create a pod that references the profile in its security context.
Procedure
-
Create a pod object that defines a
securityContext:apiVersion: v1 kind: Pod metadata: name: test-pod spec: securityContext: runAsNonRoot: true seccompProfile: type: Localhost localhostProfile: operator/profile1.json containers: - name: test-container image: quay.io/security-profiles-operator/test-nginx-unprivileged:1.21 securityContext: allowPrivilegeEscalation: false capabilities: drop: [ALL] -
View the profile path of the
seccompProfile.localhostProfileattribute by running the following command: -
View the path to the localhost profile by running the following command:
-
Apply the
localhostProfileoutput to the patch file: -
Apply the profile to any other workload, such as a
Deploymentobject, by running the following command:
Verification
-
Confirm the profile was applied correctly by running the following command:
Bind workloads to profiles with ProfileBindings¶
You can use the ProfileBinding resource to bind a security profile to the SecurityContext of a container.
Procedure
-
To bind a pod that uses a
quay.io/security-profiles-operator/test-nginx-unprivileged:1.21image to the exampleSeccompProfileprofile, create aProfileBindingobject in the same namespace with the pod and theSeccompProfileobjects:apiVersion: security-profiles-operator.x-k8s.io/v1alpha1 kind: ProfileBinding metadata: namespace: my-namespace name: nginx-binding spec: profileRef: kind: SeccompProfile name: profile image: quay.io/security-profiles-operator/test-nginx-unprivileged:1.21where:
spec.profileRef.kind- Specifies the kind of the profile.
spec.profileRef.name- Specifies the name of the profile.
spec.image- Allows you to enable a default security profile by using a wildcard in the image attribute:
image: "*"
Warning
Using the
image: "*"wildcard attribute binds all new pods with a default security profile in a given namespace. -
Label the namespace with
enable-binding=trueby running the following command: -
Define a pod named
test-pod.yaml: -
Create the pod:
Note
If the pod already exists, you must re-create the pod for the binding to work properly.
Verification
-
Confirm the pod inherits the
ProfileBindingby running the following command:
Record profiles from workloads¶
The Security Profiles Operator can record system calls with ProfileRecording objects to create baseline profiles for applications.
When using the log enricher for recording seccomp profiles, verify the log enricher feature is enabled. See Additional resources for more information.
Note
A container with privileged: true security context restraints prevents log-based recording. Privileged containers are not subject to seccomp policies, and log-based recording makes use of a special seccomp profile to record events.
Procedure
-
Create a project by running the following command:
-
Label the namespace with
enable-recording=trueby running the following command: -
Create a
ProfileRecordingobject containing arecorder: logsvariable: -
Create a workload to record:
apiVersion: v1 kind: Pod metadata: namespace: my-namespace name: my-pod labels: app: my-app spec: securityContext: runAsNonRoot: true seccompProfile: type: RuntimeDefault containers: - name: nginx image: quay.io/security-profiles-operator/test-nginx-unprivileged:1.21 ports: - containerPort: 8080 securityContext: allowPrivilegeEscalation: false capabilities: drop: [ALL] - name: redis image: quay.io/security-profiles-operator/redis:6.2.1 securityContext: allowPrivilegeEscalation: false capabilities: drop: [ALL] -
Confirm the pod is in a
Runningstate by entering the following command: -
Confirm the enricher indicates that it receives audit logs for those containers:
Example outputI0523 14:19:08.747313 430694 enricher.go:445] log-enricher "msg"="audit" "container"="redis" "executable"="/usr/local/bin/redis-server" "namespace"="my-namespace" "node"="xiyuan-23-5g2q9-worker-eastus2-6rpgf" "pid"=656802 "pod"="my-pod" "syscallID"=0 "syscallName"="read" "timestamp"="1684851548.745:207179" "type"="seccomp"
Verification
-
Remove the pod:
-
Confirm the Security Profiles Operator reconciles the two seccomp profiles:
Merge per-container profile instances¶
To reuse one recorded profile when deploying applications with a ReplicaSet or Deployment, configure the Security Profiles Operator to merge per-container profile instances into a single profile instead of keeping a separate profile for each container.
Procedure
-
Edit a
ProfileRecordingobject to include amergeStrategy: containersvariable:apiVersion: security-profiles-operator.x-k8s.io/v1alpha1 kind: ProfileRecording metadata: # The name of the Recording is the same as the resulting SeccompProfile CRD # after reconciliation. name: test-recording namespace: my-namespace spec: kind: SeccompProfile recorder: logs mergeStrategy: containers podSelector: matchLabels: app: sp-record -
Label the namespace by running the following command:
-
Create the workload with the following 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 -
To record the individual profiles, delete the deployment by running the following command:
-
To merge the profiles, delete the profile recording by running the following command:
-
To start the merge operation and generate the results profile, run the following command:
-
To view the permissions used by any of the containers, run the following command:
Additional resources