SELinux and AppArmor
Quick answer
Mandatory access control (MAC) constrains access according to system policy in addition to ordinary ownership and permission checks. SELinux commonly uses labels and types; AppArmor commonly applies path-based profiles to programs.
A file may have ordinary permissions that allow access while mandatory policy denies it. Keep enforcement active and collect the relevant denial, identity and resource context. Correct the intended policy or labeling through supported tools rather than broadly disabling protection.
Prepare the practice environment
Run python3 lab-fixtures/security/setup.py from the extracted root as an ordinary user. Enter the printed fresh private path and check pwd. Use fresh fixtures, Bash with set -e -o pipefail, LC_ALL=C and umask 077. Run each snippet in a separate Bash process, not your interactive shell. Only private files are used; optional examples require an authorized snapshot VM. Do not modify system services or security policy. Review the lab guide.
Prerequisites
Review: Capabilities and seccomp.
How it works
An access denial can occur even when ordinary mode bits allow the operation. SELinux enforcing applies policy; permissive records many would-be denials without enforcing them. AppArmor enforce and complain modes differ, and explicit deny rules may still deny in complain mode. Do not assume both frameworks are active on every distribution. Start diagnosis with ordinary permissions, service identity, expected resource path, labels or profile, and the exact operation. In a SELinux AVC record, scontext identifies the source security context, tcontext the target, and tclass the object class. A denial is not proof of malicious activity. Confirm expected behavior and correct labeling or narrow reviewed policy rather than disabling enforcement. The example log is simplified synthetic data, not a real audit record.
Try the examples
A web-service domain tried to read a file with a generic type. Inspect intended labeling and access requirements before deciding on a fix.
Expected stdout:
avc: denied { read } scontext=system_u:system_r:httpd_t:s0 tcontext=system_u:object_r:default_t:s0 tclass=fileThis extracts the label field from the simplified record, not the actual filesystem label.
Expected stdout:
tcontext=system_u:object_r:default_t:s0Optional read-only inspection on an authorized SELinux-enabled distribution. Expected output depends on actual configuration. AppArmor users can inspect aa-status with authorized access instead.
Manual example; not executed locally. Follow its stated environment restrictions.
Common mistakes
Do not turn off SELinux or AppArmor to silence a denial. Do not automatically convert every denial into an allow rule.
Practice
Interpret the synthetic denial: operation, source, target and object class. Describe a labeling check and a narrow correction proposal without changing policy.
Related references
Study: getenforce, sestatus, aa-status. These are reference names, not deployed links.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
SELinux and AppArmor — Knowledge Check
Apply defensive security concepts to synthetic fixtures and VM-only scenarios.
Can MAC deny access allowed by ordinary mode bits?
What is tcontext in an AVC record?
What is the first response to an unexpected denial?
Does a denial alone prove an attack?