Questo contenuto non è disponibile nella lingua selezionata.
Chapter 15. Managing security context constraints
15.1. About security context constraints Copia collegamentoCollegamento copiato negli appunti!
Similar to the way that RBAC resources control user access, administrators can use security context constraints (SCCs) to control permissions for pods. These permissions include actions that a pod can perform and what resources it can access. You can use SCCs to define a set of conditions that a pod must run with to be accepted into the system.
Security context constraints allow an administrator to control:
-
Whether a pod can run privileged containers with the flag.
allowPrivilegedContainer -
Whether a pod is constrained with the flag.
allowPrivilegeEscalation - The capabilities that a container can request
- The use of host directories as volumes
- The SELinux context of the container
- The container user ID
- The use of host namespaces and networking
-
The allocation of an that owns the pod volumes
FSGroup - The configuration of allowable supplemental groups
- Whether a container requires write access to its root file system
- The usage of volume types
-
The configuration of allowable profiles
seccomp
Do not set the
openshift.io/run-level
openshift.io/run-level
15.1.1. Default security context constraints Copia collegamentoCollegamento copiato negli appunti!
The cluster contains several default security context constraints (SCCs) as described in the table below. Additional SCCs might be installed when you install Operators or other components to OpenShift Container Platform.
Do not modify the default SCCs. Customizing the default SCCs can lead to issues when some of the platform pods deploy or OpenShift Container Platform is upgraded. During upgrades between some versions of OpenShift Container Platform, the values of the default SCCs are reset to the default values, which discards all customizations to those SCCs.
Instead, create new SCCs as needed.
| Security context constraint | Description |
|---|---|
|
| Provides all features of the
|
|
| Allows access to all host namespaces but still requires pods to be run with a UID and SELinux context that are allocated to the namespace. Warning This SCC allows host access to namespaces, file systems, and PIDs. It should only be used by trusted pods. Grant with caution. |
|
| Provides all the features of the
Warning This SCC allows host file system access as any UID, including UID 0. Grant with caution. |
|
| Allows using host networking and host ports but still requires pods to be run with a UID and SELinux context that are allocated to the namespace. Warning If additional workloads are run on control plane hosts, use caution when providing access to
|
|
| Like the
|
|
| Used for the Prometheus node exporter. Warning This SCC allows host file system access as any UID, including UID 0. Grant with caution. |
|
| Provides all features of the
|
|
| Like the
|
|
| Allows access to all privileged and host features and the ability to run as any user, any group, any FSGroup, and with any SELinux context. Warning This is the most relaxed SCC and should be used only for cluster administration. Grant with caution. The
Note Setting
|
|
| Denies access to all host features and requires pods to be run with a UID, and SELinux context that are allocated to the namespace. The
In clusters that were upgraded from OpenShift Container Platform 4.10 or earlier, this SCC is available for use by any authenticated user. The
|
|
| Like the
This is the most restrictive SCC provided by a new installation and will be used by default for authenticated users. Note The
|
15.1.2. Security context constraints settings Copia collegamentoCollegamento copiato negli appunti!
Security context constraints (SCCs) are composed of settings and strategies that control the security features a pod has access to. These settings fall into three categories:
| Category | Description |
|---|---|
| Controlled by a boolean | Fields of this type default to the most restrictive value. For example,
|
| Controlled by an allowable set | Fields of this type are checked against the set to ensure their value is allowed. |
| Controlled by a strategy | Items that have a strategy to generate a value provide:
|
CRI-O has the following default list of capabilities that are allowed for each container of a pod:
-
CHOWN -
DAC_OVERRIDE -
FSETID -
FOWNER -
SETGID -
SETUID -
SETPCAP -
NET_BIND_SERVICE -
KILL
The containers use the capabilities from this default list, but pod manifest authors can alter the list by requesting additional capabilities or removing some of the default behaviors. Use the
allowedCapabilities
defaultAddCapabilities
requiredDropCapabilities
You can drop all capabilites from containers by setting the
requiredDropCapabilities
ALL
restricted-v2
15.1.3. Security context constraints strategies Copia collegamentoCollegamento copiato negli appunti!
RunAsUser
- - Requires a
MustRunAsto be configured. Uses the configuredrunAsUseras the default. Validates against the configuredrunAsUser.runAsUserExample
MustRunAssnippet... runAsUser: type: MustRunAs uid: <id> ... - - Requires minimum and maximum values to be defined if not using pre-allocated values. Uses the minimum as the default. Validates against the entire allowable range.
MustRunAsRangeExample
MustRunAsRangesnippet... runAsUser: type: MustRunAsRange uidRangeMax: <maxvalue> uidRangeMin: <minvalue> ... - - Requires that the pod be submitted with a non-zero
MustRunAsNonRootor have therunAsUserdirective defined in the image. No default provided.USERExample
MustRunAsNonRootsnippet... runAsUser: type: MustRunAsNonRoot ... - - No default provided. Allows any
RunAsAnyto be specified.runAsUserExample
RunAsAnysnippet... runAsUser: type: RunAsAny ...
SELinuxContext
-
- Requires
MustRunAsto be configured if not using pre-allocated values. UsesseLinuxOptionsas the default. Validates againstseLinuxOptions.seLinuxOptions -
- No default provided. Allows any
RunAsAnyto be specified.seLinuxOptions
SupplementalGroups
-
- Requires at least one range to be specified if not using pre-allocated values. Uses the minimum value of the first range as the default. Validates against all ranges.
MustRunAs -
- No default provided. Allows any
RunAsAnyto be specified.supplementalGroups
FSGroup
-
- Requires at least one range to be specified if not using pre-allocated values. Uses the minimum value of the first range as the default. Validates against the first ID in the first range.
MustRunAs -
- No default provided. Allows any
RunAsAnyID to be specified.fsGroup
15.1.4. Controlling volumes Copia collegamentoCollegamento copiato negli appunti!
The usage of specific volume types can be controlled by setting the
volumes
-
awsElasticBlockStore -
azureDisk -
azureFile -
cephFS -
cinder -
configMap -
downwardAPI -
emptyDir -
fc -
flexVolume -
flocker -
gcePersistentDisk -
ephemeral -
gitRepo -
glusterfs -
hostPath -
iscsi -
nfs -
persistentVolumeClaim -
photonPersistentDisk -
portworxVolume -
projected -
quobyte -
rbd -
scaleIO -
secret -
storageos -
vsphereVolume - * (A special value to allow the use of all volume types.)
-
(A special value to disallow the use of all volumes types. Exists only for backwards compatibility.)
none
The recommended minimum set of allowed volumes for new SCCs are
configMap
downwardAPI
emptyDir
persistentVolumeClaim
secret
projected
This list of allowable volume types is not exhaustive because new types are added with each release of OpenShift Container Platform.
For backwards compatibility, the usage of
allowHostDirVolumePlugin
volumes
allowHostDirVolumePlugin
volumes
hostPath
volumes
15.1.5. Admission control Copia collegamentoCollegamento copiato negli appunti!
Admission control with SCCs allows for control over the creation of resources based on the capabilities granted to a user.
In terms of the SCCs, this means that an admission controller can inspect the user information made available in the context to retrieve an appropriate set of SCCs. Doing so ensures the pod is authorized to make requests about its operating environment or to generate a set of constraints to apply to the pod.
The set of SCCs that admission uses to authorize a pod are determined by the user identity and groups that the user belongs to. Additionally, if the pod specifies a service account, the set of allowable SCCs includes any constraints accessible to the service account.
When you create a workload resource, such as deployment, only the service account is used to find the SCCs and admit the pods when they are created.
Admission uses the following approach to create the final security context for the pod:
- Retrieve all SCCs available for use.
- Generate field values for security context settings that were not specified on the request.
- Validate the final settings against the available constraints.
If a matching set of constraints is found, then the pod is accepted. If the request cannot be matched to an SCC, the pod is rejected.
A pod must validate every field against the SCC. The following are examples for just two of the fields that must be validated:
These examples are in the context of a strategy using the pre-allocated values.
An FSGroup SCC strategy of MustRunAs
If the pod defines a
fsGroup
fsGroup
If the
SecurityContextConstraints.fsGroup
RunAsAny
Pod.spec.securityContext.fsGroup
A SupplementalGroups SCC strategy of MustRunAs
If the pod specification defines one or more
supplementalGroups
openshift.io/sa.scc.supplemental-groups
If the
SecurityContextConstraints.supplementalGroups
RunAsAny
Pod.spec.securityContext.supplementalGroups
15.1.6. Security context constraints prioritization Copia collegamentoCollegamento copiato negli appunti!
Security context constraints (SCCs) have a priority field that affects the ordering when attempting to validate a request by the admission controller.
A priority value of
0
0
When the complete set of available SCCs is determined, the SCCs are ordered in the following manner:
- The highest priority SCCs are ordered first.
- If the priorities are equal, the SCCs are sorted from most restrictive to least restrictive.
- If both the priorities and restrictions are equal, the SCCs are sorted by name.
By default, the
anyuid
RunAsUser
SecurityContext
15.2. About pre-allocated security context constraints values Copia collegamentoCollegamento copiato negli appunti!
The admission controller is aware of certain conditions in the security context constraints (SCCs) that trigger it to look up pre-allocated values from a namespace and populate the SCC before processing the pod. Each SCC strategy is evaluated independently of other strategies, with the pre-allocated values, where allowed, for each policy aggregated with pod specification values to make the final values for the various IDs defined in the running pod.
The following SCCs cause the admission controller to look for pre-allocated values when no ranges are defined in the pod specification:
-
A strategy of
RunAsUserwith no minimum or maximum set. Admission looks for theMustRunAsRangeannotation to populate range fields.openshift.io/sa.scc.uid-range -
An strategy of
SELinuxContextwith no level set. Admission looks for theMustRunAsannotation to populate the level.openshift.io/sa.scc.mcs -
A strategy of
FSGroup. Admission looks for theMustRunAsannotation.openshift.io/sa.scc.supplemental-groups -
A strategy of
SupplementalGroups. Admission looks for theMustRunAsannotation.openshift.io/sa.scc.supplemental-groups
During the generation phase, the security context provider uses default values for any parameter values that are not specifically set in the pod. Default values are based on the selected strategy:
-
and
RunAsAnystrategies do not provide default values. If the pod needs a parameter value, such as a group ID, you must define the value in the pod specification.MustRunAsNonRoot -
(single value) strategies provide a default value that is always used. For example, for group IDs, even if the pod specification defines its own ID value, the namespace’s default parameter value also appears in the pod’s groups.
MustRunAs -
and
MustRunAsRange(range-based) strategies provide the minimum value of the range. As with a single valueMustRunAsstrategy, the namespace’s default parameter value appears in the running pod. If a range-based strategy is configurable with multiple ranges, it provides the minimum value of the first configured range.MustRunAs
FSGroup
SupplementalGroups
openshift.io/sa.scc.uid-range
openshift.io/sa.scc.supplemental-groups
By default, the annotation-based
FSGroup
1/3
FSGroup
1
FSGroup
The
openshift.io/sa.scc.supplemental-groups
<start>/<length
<start>-<end>
openshift.io/sa.scc.uid-range
15.3. Example security context constraints Copia collegamentoCollegamento copiato negli appunti!
The following examples show the security context constraints (SCC) format and annotations:
Annotated privileged SCC
allowHostDirVolumePlugin: true
allowHostIPC: true
allowHostNetwork: true
allowHostPID: true
allowHostPorts: true
allowPrivilegedContainer: true
allowedCapabilities:
- '*'
apiVersion: security.openshift.io/v1
defaultAddCapabilities: []
fsGroup:
type: RunAsAny
groups:
- system:cluster-admins
- system:nodes
kind: SecurityContextConstraints
metadata:
annotations:
kubernetes.io/description: 'privileged allows access to all privileged and host
features and the ability to run as any user, any group, any fsGroup, and with
any SELinux context. WARNING: this is the most relaxed SCC and should be used
only for cluster administration. Grant with caution.'
creationTimestamp: null
name: privileged
priority: null
readOnlyRootFilesystem: false
requiredDropCapabilities:
- KILL
- MKNOD
- SETUID
- SETGID
runAsUser:
type: RunAsAny
seLinuxContext:
type: RunAsAny
seccompProfiles:
- '*'
supplementalGroups:
type: RunAsAny
users:
- system:serviceaccount:default:registry
- system:serviceaccount:default:router
- system:serviceaccount:openshift-infra:build-controller
volumes:
- '*'
- 1
- A list of capabilities that a pod can request. An empty list means that none of capabilities can be requested while the special symbol
*allows any capabilities. - 2
- A list of additional capabilities that are added to any pod.
- 3
- The
FSGroupstrategy, which dictates the allowable values for the security context. - 4
- The groups that can access this SCC.
- 5
- A list of capabilities to drop from a pod. Or, specify
ALLto drop all capabilities. - 6
- The
runAsUserstrategy type, which dictates the allowable values for the Security Context. - 7
- The
seLinuxContextstrategy type, which dictates the allowable values for the Security Context. - 8
- The
supplementalGroupsstrategy, which dictates the allowable supplemental groups for the Security Context. - 9
- The users who can access this SCC.
The
users
groups
restricted-v2
Without explicit runAsUser setting
apiVersion: v1
kind: Pod
metadata:
name: security-context-demo
spec:
securityContext:
containers:
- name: sec-ctx-demo
image: gcr.io/google-samples/node-hello:1.0
- 1
- When a container or pod does not request a user ID under which it should be run, the effective UID depends on the SCC that emits this pod. Because the
restricted-v2SCC is granted to all authenticated users by default, it will be available to all users and service accounts and used in most cases. Therestricted-v2SCC usesMustRunAsRangestrategy for constraining and defaulting the possible values of thesecurityContext.runAsUserfield. The admission plugin will look for theopenshift.io/sa.scc.uid-rangeannotation on the current project to populate range fields, as it does not provide this range. In the end, a container will haverunAsUserequal to the first value of the range that is hard to predict because every project has different ranges.
With explicit runAsUser setting
apiVersion: v1
kind: Pod
metadata:
name: security-context-demo
spec:
securityContext:
runAsUser: 1000
containers:
- name: sec-ctx-demo
image: gcr.io/google-samples/node-hello:1.0
- 1
- A container or pod that requests a specific user ID will be accepted by OpenShift Container Platform only when a service account or a user is granted access to a SCC that allows such a user ID. The SCC can allow arbitrary IDs, an ID that falls into a range, or the exact user ID specific to the request.
This configuration is valid for SELinux, fsGroup, and Supplemental Groups.
15.4. Creating security context constraints Copia collegamentoCollegamento copiato negli appunti!
You can create security context constraints (SCCs) by using the OpenShift CLI (
oc
Prerequisites
-
Install the OpenShift CLI ().
oc -
Log in to the cluster as a user with the role.
cluster-admin
Procedure
Define the SCC in a YAML file named
:scc_admin.yamlSecurityContextConstraintsobject definitionkind: SecurityContextConstraints apiVersion: security.openshift.io/v1 metadata: name: scc-admin allowPrivilegedContainer: true runAsUser: type: RunAsAny seLinuxContext: type: RunAsAny fsGroup: type: RunAsAny supplementalGroups: type: RunAsAny users: - my-admin-user groups: - my-admin-groupOptionally, you can drop specific capabilities for an SCC by setting the
field with the desired values. Any specified capabilities are dropped from the container. To drop all capabilities, specifyrequiredDropCapabilities. For example, to create an SCC that drops theALL,KILL, andMKNODcapabilities, add the following to the SCC object:SYS_CHROOTrequiredDropCapabilities: - KILL - MKNOD - SYS_CHROOTNoteYou cannot list a capability in both
andallowedCapabilities.requiredDropCapabilitiesCRI-O supports the same list of capability values that are found in the Docker documentation.
Create the SCC by passing in the file:
$ oc create -f scc_admin.yamlExample output
securitycontextconstraints "scc-admin" created
Verification
Verify that the SCC was created:
$ oc get scc scc-adminExample output
NAME PRIV CAPS SELINUX RUNASUSER FSGROUP SUPGROUP PRIORITY READONLYROOTFS VOLUMES scc-admin true [] RunAsAny RunAsAny RunAsAny RunAsAny <none> false [awsElasticBlockStore azureDisk azureFile cephFS cinder configMap downwardAPI emptyDir fc flexVolume flocker gcePersistentDisk gitRepo glusterfs iscsi nfs persistentVolumeClaim photonPersistentDisk quobyte rbd secret vsphere]
15.5. Role-based access to security context constraints Copia collegamentoCollegamento copiato negli appunti!
You can specify SCCs as resources that are handled by RBAC. This allows you to scope access to your SCCs to a certain project or to the entire cluster. Assigning users, groups, or service accounts directly to an SCC retains cluster-wide scope.
You cannot assign a SCC to pods created in one of the default namespaces:
default
kube-system
kube-public
openshift-node
openshift-infra
openshift
To include access to SCCs for your role, specify the
scc
$ oc create role <role-name> --verb=use --resource=scc --resource-name=<scc-name> -n <namespace>
This results in the following role definition:
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
...
name: role-name
namespace: namespace
...
rules:
- apiGroups:
- security.openshift.io
resourceNames:
- scc-name
resources:
- securitycontextconstraints
verbs:
- use
- 1
- The role’s name.
- 2
- Namespace of the defined role. Defaults to
defaultif not specified. - 3
- The API group that includes the
SecurityContextConstraintsresource. Automatically defined whensccis specified as a resource. - 4
- An example name for an SCC you want to have access.
- 5
- Name of the resource group that allows users to specify SCC names in the
resourceNamesfield. - 6
- A list of verbs to apply to the role.
A local or cluster role with such a rule allows the subjects that are bound to it with a role binding or a cluster role binding to use the user-defined SCC called
scc-name
Because RBAC is designed to prevent escalation, even project administrators are unable to grant access to an SCC. By default, they are not allowed to use the verb
use
restricted-v2
15.6. Reference of security context constraints commands Copia collegamentoCollegamento copiato negli appunti!
You can manage security context constraints (SCCs) in your instance as normal API objects using the OpenShift CLI (
oc
You must have
cluster-admin
15.6.1. Listing security context constraints Copia collegamentoCollegamento copiato negli appunti!
To get a current list of SCCs:
$ oc get scc
Example output
NAME PRIV CAPS SELINUX RUNASUSER FSGROUP SUPGROUP PRIORITY READONLYROOTFS VOLUMES
anyuid false <no value> MustRunAs RunAsAny RunAsAny RunAsAny 10 false ["configMap","downwardAPI","emptyDir","persistentVolumeClaim","projected","secret"]
hostaccess false <no value> MustRunAs MustRunAsRange MustRunAs RunAsAny <no value> false ["configMap","downwardAPI","emptyDir","hostPath","persistentVolumeClaim","projected","secret"]
hostmount-anyuid false <no value> MustRunAs RunAsAny RunAsAny RunAsAny <no value> false ["configMap","downwardAPI","emptyDir","hostPath","nfs","persistentVolumeClaim","projected","secret"]
hostnetwork false <no value> MustRunAs MustRunAsRange MustRunAs MustRunAs <no value> false ["configMap","downwardAPI","emptyDir","persistentVolumeClaim","projected","secret"]
hostnetwork-v2 false ["NET_BIND_SERVICE"] MustRunAs MustRunAsRange MustRunAs MustRunAs <no value> false ["configMap","downwardAPI","emptyDir","persistentVolumeClaim","projected","secret"]
node-exporter true <no value> RunAsAny RunAsAny RunAsAny RunAsAny <no value> false ["*"]
nonroot false <no value> MustRunAs MustRunAsNonRoot RunAsAny RunAsAny <no value> false ["configMap","downwardAPI","emptyDir","persistentVolumeClaim","projected","secret"]
nonroot-v2 false ["NET_BIND_SERVICE"] MustRunAs MustRunAsNonRoot RunAsAny RunAsAny <no value> false ["configMap","downwardAPI","emptyDir","persistentVolumeClaim","projected","secret"]
privileged true ["*"] RunAsAny RunAsAny RunAsAny RunAsAny <no value> false ["*"]
restricted false <no value> MustRunAs MustRunAsRange MustRunAs RunAsAny <no value> false ["configMap","downwardAPI","emptyDir","persistentVolumeClaim","projected","secret"]
restricted-v2 false ["NET_BIND_SERVICE"] MustRunAs MustRunAsRange MustRunAs RunAsAny <no value> false ["configMap","downwardAPI","emptyDir","persistentVolumeClaim","projected","secret"]
15.6.2. Examining security context constraints Copia collegamentoCollegamento copiato negli appunti!
You can view information about a particular SCC, including which users, service accounts, and groups the SCC is applied to.
For example, to examine the
restricted
$ oc describe scc restricted
Example output
Name: restricted
Priority: <none>
Access:
Users: <none>
Groups: <none>
Settings:
Allow Privileged: false
Allow Privilege Escalation: true
Default Add Capabilities: <none>
Required Drop Capabilities: KILL,MKNOD,SETUID,SETGID
Allowed Capabilities: <none>
Allowed Seccomp Profiles: <none>
Allowed Volume Types: configMap,downwardAPI,emptyDir,persistentVolumeClaim,projected,secret
Allowed Flexvolumes: <all>
Allowed Unsafe Sysctls: <none>
Forbidden Sysctls: <none>
Allow Host Network: false
Allow Host Ports: false
Allow Host PID: false
Allow Host IPC: false
Read Only Root Filesystem: false
Run As User Strategy: MustRunAsRange
UID: <none>
UID Range Min: <none>
UID Range Max: <none>
SELinux Context Strategy: MustRunAs
User: <none>
Role: <none>
Type: <none>
Level: <none>
FSGroup Strategy: MustRunAs
Ranges: <none>
Supplemental Groups Strategy: RunAsAny
Ranges: <none>
To preserve customized SCCs during upgrades, do not edit settings on the default SCCs.
15.6.3. Deleting security context constraints Copia collegamentoCollegamento copiato negli appunti!
To delete an SCC:
$ oc delete scc <scc_name>
If you delete a default SCC, it will regenerate when you restart the cluster.
15.6.4. Updating security context constraints Copia collegamentoCollegamento copiato negli appunti!
To update an existing SCC:
$ oc edit scc <scc_name>
To preserve customized SCCs during upgrades, do not edit settings on the default SCCs.