Mastering Process Sandboxing on Ubuntu with aa-exec

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

Installing apparmor-utils package

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.

Locating aa-exec executable

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.

Checking kernel AppArmor enforcement

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 until exec.
  • -v, --verbose : Outputs detailed runtime lifecycle feedback.
  • -h, --help : Prints the comprehensive syntax reference and switch options.

Reviewing aa-exec usage 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

Creating staging directory

Generate an authorized target file to validate legitimate read operations:

echo 'Hello Secure World' > /tmp/aa_demo/allowed.txt

Writing authorized file contents

Generate an unauthorized file intended to remain strictly off-limits to the confined process:

echo 'Confidential Data' > /tmp/aa_demo/secret.txt

Creating restricted confidential file

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

Writing sandbox-test profile definition

Compile and register the newly defined rules directly into kernel memory:

sudo apparmor_parser -r /etc/apparmor.d/sandbox-test

Loading profile via apparmor_parser

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.

Verifying sandbox-test active profile

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.

Authorized file read execution

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.

Blocked file access attempt

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.

Inspecting AppArmor audit logs

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

Generating no-net-script profile

Compile and insert the network isolation profile into the kernel:

sudo apparmor_parser -r /etc/apparmor.d/no-net-script

Compiling no-net-script policy

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.

Demonstrating outbound network blockage

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

Deleting profile files

Purge the compiled sandbox-test profile from active kernel memory:

sudo apparmor_parser -R /etc/apparmor.d/sandbox-test 2>/dev/null || true

Unloading sandbox-test profile

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

Unloading no-net-script profile

Erase the temporary directory and test artifacts:

rm -rf /tmp/aa_demo

Deleting temporary files

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.

Validating successful profile cleanup

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.