Skip to main content
In this guide, we will walk through the process of creating custom AppArmor profiles. After reviewing several example profiles in previous lessons, it’s time to build an application-specific profile from scratch.

Example Bash Script

Below is a sample Bash script named “add_data.sh” that creates directories under the /opt filesystem and writes a log file within the new directory:
To run the script, execute the following command in your terminal:
Expected terminal output:
You can verify the content of the log file with:
This should display:

Generating an AppArmor Profile for the Script

Instead of creating a profile manually, you can use AppArmor’s built-in tools. First, install the AppArmor-utils package. On Ubuntu, run:
The installation output will resemble:
Once installed, generate a profile for the Bash script using the following command:
The output will be similar to:

Profiling the Script

  1. Open a separate terminal window and run the Bash script to generate AppArmor events:
  2. Return to the aa-genprof prompt and press s to scan the system logs. The tool will then display multiple prompts for each event encountered, such as:
    To allow the execution of the mkdir command, choose the inherit option by entering i.
  3. Further prompts might appear. For example:
    Again, select the appropriate option—typically i for inherit if needed.
  4. Another prompt may request permission to access the tty interface. If a prompt with severity 9 appears when printing to the console, enter a (allow).
  5. You might encounter a prompt asking for read access to a system file. For instance:
    Since the script does not need access to this file, choose d to deny access.
Ensure that you only allow permissions essential for your application to operate. Deny any unnecessary access to maintain a secure profile.
After processing all events, press S to save and F to finish. You should see output similar to:
Your new AppArmor profile is now running in enforce mode.

Verifying the Profile

To confirm that the profile is in enforce mode, use the following command:
Expected output:
The new profile, along with other existing profiles, is stored in the /etc/apparmor.d directory. An example profile for “add_data.sh” might look like this:

Testing the Enforced Profile

To verify that the enforced profile restricts unauthorized access, modify the script to change the log file path from /opt/app/data to /opt. Update the script as follows:
When you run the modified script:
You should see an output similar to:
This confirms that while the script can output to the terminal, the AppArmor profile restricts write access only to the /opt/app directory, yielding a permission denied error when attempting to write directly to /opt.

Working with Existing AppArmor Profiles

To load an existing profile, use the AppArmor parser command. If no output is returned, the profile has been successfully loaded.
To disable a profile, use the same command with the -r flag and create a symlink to the profile in the /etc/apparmor.d/disable directory.
Now that you’ve learned how to create and enforce AppArmor profiles for a custom application, you can explore securing applications running within Kubernetes pods using AppArmor for enhanced security.
This concludes the lesson on creating AppArmor profiles. Next, we will explore securing an application running inside a Kubernetes pod with AppArmor profiles. Happy securing!

Watch Video