Skip to content
Kubernetes Security Context Settings
Jimmy MestaMay 17, 2023 9:12:13 AM8 min read

Why Use K8 Security Context Settings to Prevent Privilege Escalation?


In the rapidly evolving arena of container orchestration, Kubernetes provides a wealth of benefits for scaling and managing applications. However, Kubernetes was never meant to come secure out of the box. Kubernetes security context is one of the most critical security concepts to be aware of because of the potential role it could play in unwanted cluster privilege escalation.

Download the Kubernetes Security Master Guide


What is a Kubernetes Security Context?

A Kubernetes security context is a powerful feature that defines a pod or container‘s privilege and access control settings. By defining these parameters, you can easily and precisely manage the security of your applications, ensuring they follow the best practices and recommendations for resource allocation, isolation, and access control.

One critical aspect of a Kubernetes security context is ‘securityContext‘ deployment. With this configuration, you can fine-tune the access privileges for each task by setting security parameters at the pod or container level. You can adjust these settings to suit the security needs of various applications without impacting the cluster's overall security posture.

In addition, the 'securityContext' offers several specific settings, including 'fsgroup,' 'runAsUser,' and 'runAsUser root.' The 'securityContext fsGroup' assigns a group ID to the container's filesystem, which is especially helpful for regulating access to shared volumes. Consequently, you prevent unwanted access and potential security breaches, as only authorized users can access the required files.

Using the 'securityContext runAsUser', you can also control the user ID under which the container runs, effectively limiting the container's privileges within the cluster. This is a crucial security measure, as it helps to minimize the potential attack surface by preventing containers from running with excessive permissions.

The 'securityContext' runAsUser rootsetting further allows containers to run as the root user when necessary. However, using this option judiciously is essential, as running containers with root privileges can expose your infrastructure to potential security risks.

Using the Kubernetes Security Context as a Path to Better Access Management

A well-configured container security context allows you to examine who can access your workloads and the extent of their permissions, thus granting access only to those who truly need it. By striking the right balance between security and usability, you can maintain both the integrity of your infrastructure and your team's productivity. To be clear, RBAC is the main mode of authorization inside Kubernetes and cloud IAM is usually how authentication is done to the API itself. Security context is another configuration that could work with or separately from RBAC to determine privileges. See here for more detail on the difference between RBAC and cloud IAM, or here for the main ways RBAC can be overpermissioned.

The Kubernetes security context medium allows you to fine-tune your access management effortlessly. Using the various settings available, you can configure your clusters and resources to match your organization's specific security requirements. This granular approach ensures that your security measures are both efficient and effective.

A common conversation that arises is the question of the difference between Kubernetes 'runAsUser' vs Docker user. While both settings allow you to control the user ID under which a container runs, there are some notable distinctions. Kubernetes' runAsUser is a part of the security context configuration, while Docker's user is a native feature of the Docker platform. The 'runAsUser' setting provides a more comprehensive solution for Kubernetes environments, enabling seamless integration with other security context options.

What is the pod security context?

The pod security context capabilities gives you the power to define specific permissions for each container, such as the ability to use certain host resources or run privileged operations. These capabilities enable you to maintain a secure environment, even when working with diverse workloads that demand varying access levels.

However, it's crucial to stay updated on the latest Kubernetes changes, as some pod security context features may be deprecated over time. Keeping an eye on the official documentation and best practices ensures that your security configurations remain compatible with newer Kubernetes versions.

It's also important to understand the difference between 'podSecurityContext' and 'securityContext'. The former applies settings at the pod level, while the latter targets individual containers within the pod. By using these two configurations in tandem, you can create a robust security strategy that caters to both overarching and granular access requirements.

Kubernetes Security Context Capabilities: Implementing and Monitoring for Optimal Protection

While defining privilege and access control settings using a security context is essential for securing your Kubernetes environment, it's equally important to implement and monitor security measures to maintain the highest level of protection. Let's delve into some key security context capabilities to help you achieve a more secure and well-monitored Kubernetes infrastructure.

The 'fsGroup' feature in Kubernetes allows you to specify a group ID for the volumes your pod is using, making it easier to manage access permissions for shared storage resources. When you effectively use the 'fsGroup', you can create a more secure environment by ensuring that only authorized users can access sensitive data.

Kubernetes also enables you to manage the user ID under which a container runs with the 'runAsUser' setting. This is an important capability, as running containers with the least possible privilege reduces the risk of unauthorized access or malicious activity. When configuring Kubernetes 'runAsUser', it's crucial to avoid using the root user (also known as Kubernetes 'runAsUser' root) or any user with elevated privileges. Instead, assign a specific, non-root user ID to minimize potential security risks.

The 'capabilities' setting allows you to add or drop specific Linux capabilities for your containers. Using 'Kubernetes security context capabilities drop all', you can remove all capabilities from a container and selectively grant only the ones required for your workload. This approach significantly reduces the container's attack surface and ensures it operates with the least possible privilege.

To maintain a comprehensive security strategy, it's important to include a 'securityContext' configuration in your Kubernetes deployments. Take note that monitoring remains a critical aspect of security management. Continuously reviewing logs, conducting regular audits, and utilizing security tools can help you detect and address potential threats early on.

Kubernetes Security Context Example: Minimizing Over-Permissioning With Access Management

One effective way to bolster your Kubernetes infrastructure's security is granting and revoking access on a per-resource basis to minimize unnecessary over-permissioning, and reduce potential attack vectors. To further put this into context, let’s explore a practical Kubernetes security context example to achieve this goal.

Consider a container that requires temporary root access to perform a specific task. Instead of running the container with the Kubernetes `runAsRoot` setting permanently enabled, you can use the 'runAsNonRoot’ configuration to prohibit it from running with root privileges. Switching to a Kubernetes 'runAsNonRoot false' setting ensures the container operates with a non-root user by default, significantly reducing its attack surface.

Next, you can leverage the 'readOnlyRootFilesystem' feature in Kubernetes. This setting makes the container's root filesystem read-only, further enhancing security by preventing unauthorized modifications to critical system files. Combining the 'readOnlyRootFilesystem' Kubernetes setting with 'runAsNonRoot' creates a secure environment where containers function with minimal privileges, effectively reducing the risk of unauthorized access.

Lastly, you can disable the Kubernetes 'allowPrivilegeEscalation' setting which controls whether a container can gain more privileges than its parent process. Disabling this feature restricts containers from escalating their privileges, further enhancing the security of your Kubernetes environment.

Pod Security Context: Enhancing Security for All Containers in a Pod

You can create a consistent and safe environment by specifying security parameters at the pod level and ensuring they apply to all containers inside the pod. Let's break down the key security parameters to include in your pod's configuration file in this section, focusing on the 'securityContext' field.

The 'fsGroup' Kubernetes setting allows you to define the group ID for the pod's volumes. Setting the 'fsGroup' in your Kubernetes pod allows you to manage the ownership and permissions of the files and directories within the pod's volumes, reinforcing your data security and access control. Assigning a unique 'fsGroup' to each pod maintains a clear separation between resources and minimizes the risk of unauthorized access.

Another crucial security measure is using the 'capabilities' feature to drop all unnecessary Linux capabilities for the containers in a pod. By setting 'capabilities' to 'drop all', you restrict the containers' capabilities, bolstering security by minimizing the potential for privilege escalation attacks. This approach ensures your containers only have the necessary privileges to perform their intended tasks, reducing their attack surface.

You can use the Kubernetes deployment 'securityContext' field to apply these settings consistently across all containers within a pod. 

Reducing privileged containers; an example

Finally, it's crucial to be cautious when dealing with privileged containers. While they can provide valuable functionality, privileged containers come with inherent security risks. 

For example, to create a privileged container in Kubernetes, you would add the privileged flag to the container's security context. In this example, the privileged flag is set to true under the 'securityContext' field. This configuration grants the container full access to the host's devices, resources, and capabilities, which can be useful for tasks such as managing network interfaces or mounting volumes but also poses significant system-wide breach risk.

To minimize these risks, consider implementing the following mitigation strategies:

  1. Limit the use of privileged containers: Only use privileged containers when absolutely necessary, and ensure that their usage is well-documented and monitored.
  2. Implement least privilege principles: Rather than granting full privileges, use the capabilities feature to assign only the necessary capabilities to your containers.
  3. Monitor and audit container activity: Regularly review logs and use monitoring tools to detect any suspicious activity or unauthorized access within privileged containers.
  4. Implement network segmentation: Isolate privileged containers within a separate network segment to limit their potential attack surface and restrict communication with other sensitive systems.
  5. Keep container images up to date: Regularly update the container images used in privileged containers to ensure they include the latest security patches and minimize vulnerabilities.

How KSOC can help

You can use KSOC’s real-time Kubernetes Security posture management and admission control features to set policies that govern where and how Kubernetes security context can be used, as well as alert you in real-time when privilege escalation occurs. Book a demo with us today to see what real-time KSPM looks like.


Jimmy Mesta

Jimmy Mesta is the founder and Chief Technology Officer at RAD Security. He is responsible for the technological vision for the RAD Security platform. A veteran security engineering leader focused on building cloud-native security solutions, Jimmy has held various leadership positions with enterprises navigating the growth of cloud services and containerization. Previously, Jimmy was an independent consultant focused on building large-scale cloud security programs, delivering technical security training, producing research and securing some of the largest containerized environments in the world.