Overview
Mandatory Access Control (MAC) mechanisms in Linux provide powerful guardrails against security breaches. This tutorial explores the architecture and hands-on usage of aa-exec, an AppArmor utility designed to launch ad-hoc executables constrained by designated security policies. By walking through real-world scenarios—such as jailing untrusted binaries, enforcing strict file access boundaries, and blocking outbound network traffic—we demonstrate how to establish lightweight sandboxes without the overhead of container engines.
1. Installing AppArmor Utilities and Inspecting aa-exec
Before configuring custom security contexts, install the administrative tool suite containing aa-exec on Ubuntu:
sudo apt-get update -qq && sudo apt-get install -y apparmor-utils

Verify where the newly installed binary lives within the system search path:
which aa-exec
The system confirms the command is deployed at /usr/bin/aa-exec.

Next, ensure that AppArmor is actively integrated into the running Linux kernel:
sudo aa-status --enabled
A silent return code of 0 validates that AppArmor kernel modules are armed and operational.

The aa-exec binary attaches targeted AppArmor profiles to processes at invocation time.
⚙️ Key Command-Line Arguments:
-p PROFILE,--profile=PROFILE: Names the confinement profile to bind to the executed process.-n NAMESPACE,--namespace=NAMESPACE: Executes the binary inside a specific AppArmor namespace.-d,--debug: Emits diagnostic tracing output during execution.-i,--immediate: Transitions into the confined profile instantly instead of deferring untilexec.-v,--verbose: Outputs detailed runtime lifecycle feedback.-h,--help: Prints the comprehensive syntax reference and switch options.

2. Defining and Compiling a Restrictive AppArmor Profile
To stage our isolation test bed, create a dedicated temporary working space and switch to it:
mkdir -p /tmp/aa_demo && cd /tmp/aa_demo

Generate an authorized target file to validate legitimate read operations:
echo 'Hello Secure World' > /tmp/aa_demo/allowed.txt

Generate an unauthorized file intended to remain strictly off-limits to the confined process:
echo 'Confidential Data' > /tmp/aa_demo/secret.txt

Draft a minimal policy named sandbox-test that permits running cat while granting read privileges exclusively to allowed.txt:
printf '#include <tunables/global>\nprofile sandbox-test {\n #include <abstractions/base>\n /bin/cat mr,\n /usr/bin/cat mr,\n /tmp/aa_demo/allowed.txt r,\n}\n' | sudo tee /etc/apparmor.d/sandbox-test > /dev/null

Compile and register the newly defined rules directly into kernel memory:
sudo apparmor_parser -r /etc/apparmor.d/sandbox-test

Query the active kernel security tables to ensure sandbox-test is loaded in enforce mode:
sudo aa-status | grep sandbox-test
The resulting match confirms that the policy has been successfully activated.

3. Applying Profiles via aa-exec and Validating Access Controls
Instruct aa-exec to launch cat under the sandbox-test profile against the permitted file:
aa-exec -p sandbox-test -- cat /tmp/aa_demo/allowed.txt
As expected, the file contents stream to standard output without hindrance.

Now, attempt to read the unauthorized sensitive file within the identical security context:
aa-exec -p sandbox-test -- cat /tmp/aa_demo/secret.txt || true
Because the profile lacks read entitlements for secret.txt, the kernel rejects the open syscall and triggers a permission failure.

Inspect the kernel ring buffer to examine the audit events recorded during the restriction:
sudo dmesg | grep -i apparmor | tail -n 5
The resulting log entries document the exact timestamp, profile name, and denied system operation.

4. Practical Workflow: Constructing a Network-Isolated Jail
Malicious or untrusted scripts frequently attempt outbound data exfiltration. To prevent this, construct a profile named no-net-script that allows standard execution of bash and curl while explicitly revoking socket access with deny network:
printf '#include <tunables/global>\nprofile no-net-script {\n #include <abstractions/base>\n /bin/bash rix,\n /usr/bin/curl rix,\n deny network,\n}\n' | sudo tee /etc/apparmor.d/no-net-script > /dev/null

Compile and insert the network isolation profile into the kernel:
sudo apparmor_parser -r /etc/apparmor.d/no-net-script

Spawn curl confined by no-net-script and attempt an outbound HTTP request:
aa-exec -p no-net-script -- curl -s --max-time 2 http://example.com || true
The kernel immediately suppresses socket creation, causing the transfer to terminate instantly without transmitting data.

5. Environment Teardown and Post-Lab Cleanup
To restore the test host to a clean baseline, remove the profile definition files from /etc/apparmor.d/:
sudo rm -f /etc/apparmor.d/sandbox-test /etc/apparmor.d/no-net-script

Purge the compiled sandbox-test profile from active kernel memory:
sudo apparmor_parser -R /etc/apparmor.d/sandbox-test 2>/dev/null || true

Similarly, unload the no-net-script policy from the AppArmor table:
sudo apparmor_parser -R /etc/apparmor.d/no-net-script 2>/dev/null || true

Erase the temporary directory and test artifacts:
rm -rf /tmp/aa_demo

Finally, verify that both experimental policies have been cleanly purged from the enforcement list:
sudo aa-status | grep -E 'sandbox-test|no-net-script' || echo 'Cleaned successfully'
The terminal echoes Cleaned successfully, certifying that all temporary sandbox policies have been unloaded.

Using aa-exec provides systems engineers with an agile, OS-native tool to enforce discretionary least-privilege policies. Integrating targeted AppArmor confinement into your automation pipelines effectively neutralizes potential security threats from arbitrary scripts without adding architectural friction.