Sub topic 14
MCS Categories and How They Isolate Containers
This chapter assumes you can read a context such as system_u:system_r:container_t:s0:c12,c845 and pick out the type field, as covered in the chapters on security contexts and type enforcement. It also assumes you have Podman on a RHEL-family system with SELinux in enforcing mode. The examples use rootless Podman, but rootful behaves the same way.
The problem type enforcement cannot solve
Every container started by a runtime runs in the same domain, container_t. Every file a container may write is labeled container_file_t. Type enforcement alone would let container A read and write container B's files, because both run in the same domain and the policy allows that domain to manage that type. A breakout from one container would expose every other container's data.
Multi-Category Security (MCS) closes this gap. It adds a second, independent check on top of type enforcement. Each container receives a unique pair of labels called categories, and a process can only touch objects whose categories it holds.
The fourth field of the context
The last field of a context is the level, written sensitivity:categories. On the targeted policy the sensitivity is always s0, and the categories are c0 through c1023.
| Context fragment | Meaning |
|---|---|
| `s0` | Sensitivity 0, no categories |
| `s0:c1,c2` | Categories 1 and 2 |
| `s0:c10.c20` | A range: categories 10 through 20 |
| `s0-s0:c0.c1023` | A range of levels, from `s0` up to `s0` with all categories. This is what an unconfined login shows in `id -Z`. |
Each category is only a number. It has no built-in meaning, such as "finance" or "customer A". The policy only compares the sets of numbers.
The dominance rule
The policy contains MCS constraints. For the common access operations, such as reading and writing a file, they require that the process's level dominates the object's level. With a single sensitivity, "dominates" means the process's category set contains every category of the object.
| Process level | File level | Access |
|---|---|---|
| `s0:c1,c2` | `s0:c1,c2` | Allowed |
| `s0:c1,c2` | `s0:c3,c4` | Denied |
| `s0:c1,c2` | `s0:c1` | Allowed (subset) |
| `s0:c1` | `s0:c1,c2` | Denied (file has a category the process lacks) |
| `s0:c1,c2` | `s0` | Allowed (no categories to match) |
Both checks must pass. The type must be permitted by an allow rule, and the level must satisfy the constraint. A denial can come from either one, which matters when you diagnose it later.
How container runtimes use it
When Podman starts a container, it asks the SELinux library for a fresh, unused level. It picks two random categories and tracks the levels already in use so it avoids reusing one. It then does three things:
- It starts the container's main process with the label
system_u:system_r:container_t:s0:cX,cY. - It labels the container's own writable layer with
container_file_t:s0:cX,cY. - It records both labels so you can inspect them.
With 1024 categories there are 523,776 distinct pairs (1024 choose 2), which is enough that two live containers on one host effectively never share one.
Worked example: seeing the labels
Start two containers and read their labels.
podman run -d --name c1 registry.access.redhat.com/ubi9/ubi sleep 3600
podman run -d --name c2 registry.access.redhat.com/ubi9/ubi sleep 3600
podman inspect --format '{{.Name}} {{.ProcessLabel}} {{.MountLabel}}' c1 c2
ps -eZ | grep container_tEach container shows container_t with a different pair, for example s0:c218,c670 and s0:c93,c547. The MountLabel is the matching container_file_t label for the container's files. If two lines ever show identical categories, something is forcing a level, such as --security-opt label=level:.
Clean up when you are done:
podman rm -f c1 c2Bind mounts: `:z` versus `:Z`
A host directory mounted into a container keeps its host label. A home directory is typically user_home_t, which container_t is not allowed to use, so the container gets "Permission denied". Podman can relabel the mount for you with a suffix on the volume option.
| Suffix | Label applied to the host directory | Who can use it |
|---|---|---|
| none | Unchanged | Only if the existing label already permits it |
| `:z` | `container_file_t:s0` (no categories) | Any container, so the directory is shared |
| `:Z` | `container_file_t:s0:cX,cY` (this container's pair) | Only this container |
This example shows the private label and its effect on a second container:
mkdir -p ~/data1
echo hello > ~/data1/f
ls -Zd ~/data1
podman run --rm -v ~/data1:/data:Z registry.access.redhat.com/ubi9/ubi cat /data/f
ls -Zd ~/data1
podman run --rm -v ~/data1:/data registry.access.redhat.com/ubi9/ubi cat /data/fThe first ls -Zd shows a home-directory type such as user_home_t. The first container prints hello. The second ls -Zd now shows container_file_t with the first container's categories, which remain on disk after the container exits. The last container runs without a suffix and gets a new random pair, so it prints cat: /data/f: Permission denied. Use :z instead if several containers must share the directory.
Choosing categories yourself
Sometimes you want two containers to share data and also stay isolated from every other container. Give them the same level:
podman run -d --name app1 --security-opt label=level:s0:c100,c200 \
-v ~/shared:/data:Z registry.access.redhat.com/ubi9/ubi sleep 3600
podman run -d --name app2 --security-opt label=level:s0:c100,c200 \
-v ~/shared:/data registry.access.redhat.com/ubi9/ubi sleep 3600Both containers dominate ~/shared, since they hold the same categories, and no other container can read it. Pick levels by hand only when you need this, because random assignment is what prevents collisions. Podman also accepts label=type: to change the domain and label=disable to turn confinement off for one container. Treat label=disable as a debugging aid. It removes both the type check and the MCS check for that container.
Other users of MCS
Containers are the most common case, but the same mechanism separates virtual machines under libvirt (the sVirt feature). Each guest runs as svirt_t with its own categories, and its disk image is labeled svirt_image_t with the same pair. The idea is the same: one shared type, unique categories per instance.
Diagnosing MCS denials
An MCS denial appears as an ordinary AVC in the audit log. The sign is that the source and target contexts have the same types but different categories:
sudo ausearch -m avc -ts recentA typical record shows scontext=system_u:system_r:container_t:s0:c93,c547 and tcontext=system_u:object_r:container_file_t:s0:c218,c670. The types already match, so no type rule is missing.
Two points help here:
audit2allowcannot fix this. The denial comes from a constraint, not a missing allow rule, so the module you generate will not make the access work. Do not widen the policy.- The fix is a label. Relabel the data with
:zor:Z, run the containers with the samelabel=level:value, or put the files under a path that carries the correct level.
Be careful with restorecon. It resets files to the default label for their path, which usually has no categories. Running restorecon -R on a directory you labeled with :Z removes the private categories. A container that held them will now reach the files only if their type still permits it. Re-run the container with :Z to apply a fresh label.
Limits to remember
- MCS separates containers from each other. It does not separate a container from the host's own
container_file_tfiles that have no categories, since any level dominatess0. - A container with
--privilegedorlabel=disableis no longer confined by these checks. - Categories have no meaning of their own. You are responsible for assigning matching levels to things that should share data.
- On a system running the MLS policy, sensitivities and categories form a fuller model. On the targeted policy, MCS is the only use of the level field.
Exercise
Two rootless containers on the same host both run as container_t, and a directory has the label container_file_t:s0:c5,c6. Container A has level s0:c5,c6. Container B has level s0:c5,c9. Which can read the directory, and why? What would you change to let both read it without making it readable by every container?
Show answer
Container A can read it, because its category set contains both c5 and c6, so its level dominates the directory's level. Container B cannot, because it lacks c6, and the MCS constraint denies it even though the types match. To share the directory with both and still keep other containers out, start both containers with the same explicit level, such as --security-opt label=level:s0:c5,c6, and mount the directory so it is labeled for that level. Using :z would also work, but it removes the categories and makes the directory readable by any container.
Exercise
A container reports "Permission denied" on a bind-mounted directory, and ausearch -m avc shows matching types but different categories in scontext and tcontext. Why is running audit2allow -M the wrong fix, and what should you do?
Show answer
The denial comes from an MCS constraint on the level, not from a missing type enforcement allow rule, so a module generated by audit2allow cannot make the access work and would only add needless rules. Fix the label instead. Mount with :Z so the directory takes the container's categories, use :z if several containers must share it, or run the containers with the same label=level: value.