<p>I'm currently attempting to look into whether it's possible to prevent a Kubernetes user from creating privileged containers via RBAC. I'm aware that as of Kubernetes 1.1, privileged containers are enabled by default to support underlying Docker requirements. This is fine, I'm not looking to block <em>everybody</em> from being able to run privileged containers.</p>
<p>I would, however, like to work on a principle of least privilege. For example, I'd like to prevent users being able to use something such as <a href="https://github.com/kvaps/kubectl-node-shell" rel="nofollow noreferrer">kubectl node-shell</a> from being able to gain root access to a worker node, preferably through RBAC.</p>
<p>Is this possible?</p>

Adam Thomason
<p>There are several ways to achieve this and I'd like to start with the first, official one: <a href="https://kubernetes.io/docs/concepts/policy/pod-security-policy/" rel="noreferrer">Pod Security Policy</a>.</p>
<p>The policy you want to implement is the following: <a href="https://kubernetes.io/docs/concepts/policy/pod-security-policy/#privileged" rel="noreferrer">Privileged</a></p>
<blockquote>
<p>determines if any container in a pod can enable privileged mode. By default a container is not allowed to access any devices on the host, but a &quot;privileged&quot; container is given access to all devices on the host. This allows the container nearly all the same access as processes running on the host. This is useful for containers that want to use linux capabilities like manipulating the network stack and accessing devices.</p>
</blockquote>
<p>The Kubernetes documentation provides few examples on how to interact with PSP, although a big disclaimer must be stated: the <a href="https://kubernetes.io/docs/reference/access-authn-authz/admission-controllers/#podsecuritypolicy" rel="noreferrer">PodSecurityPolicy</a> admission controlled must be activated on the API Server.</p>
<p>Besides the official admission controller, there are other projects that allow performing the denial of containers matching criteria, as well as running as privileged: <a href="https://github.com/open-policy-agent/gatekeeper" rel="noreferrer">Gatekeeper</a>, <a href="https://github.com/kyverno/kyverno" rel="noreferrer">Kyverno</a>.</p>
<p>There are other projects that can address your use-case but wanted to share these last two since they seem the most promising ones.</p>
<h3>Examples</h3>
<ul>
<li><a href="https://github.com/open-policy-agent/gatekeeper-library/tree/master/library/pod-security-policy/privileged-containers" rel="noreferrer">Gatekeeper</a></li>
<li><a href="https://github.com/kyverno/kyverno/blob/main/samples/DisallowPrivilegedContainers.md" rel="noreferrer">Kyverno</a></li>
</ul>
