Hackernoon logoAdvanced Docker Security with AppArmor - An Overview by@sudip-sengupta

Advanced Docker Security with AppArmor - An Overview

Author profile picture

@sudip-senguptaSudip Sengupta

Solution Architect | Technical Content Writer

So you have your Docker Containers deployed, which in turn are hosting critical applications of your organization? Great! So far, so good!

For the interest of the organization, it remains extremely crucial to keep not only the Containers but also the hosted applications protected from security threats. By default, a deployed Docker originally remains secured through an auto-generated profileĀ docker-defaultĀ for its containers.

This profile, however, provides moderate security on the application level, and thus it remains highly recommended to implement a security profile through AppArmor which works at the process/program level of an application.

What is AppArmor?

AppArmor (Application Armor) is a Linux Security Module that allows implementing security on a program/process level. Specifically developed security profiles through AppArmor can allow capabilities like folder access, network access, and permission(or not) to read, write, or execute files.

One of the beauties of AppArmor is that it allows aĀ Learning ModeĀ which logs profile violations without preventing them proactively. TheĀ Learning ModeĀ Log eventually helps administrators to create a security profile which forms a much hardened security armor based on an application's process execution. Default Security policies when clubbed withĀ Learning ModeĀ Logs, help forming security policies for even very complex applications in quick turnaround.

AppArmor proactively protects the operating system and applications from external or internal threats and even zero-day attacks by enforcing a specific rule set on a per-application basis. Security policies completely define what system resources individual applications can access, and with what privileges. Access is denied by default if no profile says otherwise.

Installing and Enabling AppArmor

Though AppArmor comes inbuilt with all Linux Kernels, it is not by default the security profile loaded with every boot. Apparmor can be set as the default security profile on every boot by setting the following parameter on kernel :

apparmor=1 security=apparmor

To load all AppArmor security profiles on boot,Ā enableĀ 


Display AppArmor loaded profiles

The system default AppArmor comes with a number of security profiles, on top of which an administrator can add his own security profiles based on theĀ Learning Mode. To check the list of AppArmor security profiles correctly loaded :

$ aa-enabled

To display the current loaded status useĀ 


# apparmor_statusapparmor module is loaded. 
29 profiles are loaded. 
29 profiles are in enforce mode.  
0 profiles are in complain mode. 
0 processes have profiles defined. 
0 processes are in enforce mode. 
0 processes are in complain mode. 
0 processes are unconfined but have a profile defined.

Above you can see the loaded profiles and processes with their respective statuses.

Parsing AppArmor profiles

AppArmor allows a number of options usingĀ 

Ā to parse either its default or custom generated profiles.Ā 
Ā is widely used to load, unload, debug, remove, replace, cache and match-strings within profiles out of the other available options.

Ā - Default Action to load a new profile in enforce mode.
Ā - Loading a new profile in complain mode.
Ā - Overwrite an existing profile.
Ā - Remove an existing profile in the kernel.
Ā - Display the profile version.
Ā - Display reference guide.

Understanding AppArmor profiles

AppArmor profiles are text files found underĀ 

. A quick look into a profile file explains its execution as shown below:


#include <tunables/global>  
profile test /usr/lib/test/test_binary 
	#include <abstractions/base>      
    # Main libraries and plugins     
    /usr/share/TEST/** r,
    /usr/lib/TEST/** rm,
    # Configuration files and logs     
    @{HOME}/.config/ r,     
    @{HOME}/.config/TEST/** rw, 

Strings following theĀ 

Ā symbol are variables defined under abstractions (
), tunables (
) or by the profile itself.Ā 
Ā includes other profile-files directly. Paths followed by a set of characters areĀ access permissionsĀ while theĀ Globbing SyntaxĀ helps with pattern matching.

Commonly used command options on profile files :

Ā - reading data
Ā - creating, deleting or write on an existing file
Ā - executing a file
Ā - memory mapping an executable file

Creating a new AppArmor profile

Creating an AppArmor profile can be done through aĀ SystemicĀ or Stand-AloneĀ method.

1)Ā Stand-Alone Profile Creation


) : Used for creating a profile affecting single program/application which runs for a finite amount of time, such as a web browsing client, mail client, etc. Though a Stand-Alone profile is comparatively quicker and easier to be developed, it comes with its own limitations as such the profiling is lost on a reboot. AĀ Stand-AloneĀ profile can be created through AppArmor'sĀ 
Ā profile generating utility. It runsĀ 
Ā on the specified program/application by creating an approximate profile, sets it to complain mode, reloads it into AppArmor, marks the log, and prompts the user to execute the program and exercise its functionality.

aa-genprof [ -dĀ 
Ā ] Ā 

2) Systemic Profile Creation


): Used for creating a profile affecting multiple programs and/or applications that runs indefinitely or continuously across reboots, such as network server applications like mail servers, security policies, etc. This method updates all of the profiles on the system at once, as opposed to one or a few targeted byĀ Stand-AloneĀ profiling.

Steps to createĀ SystemicĀ profile for a programĀ :

  1. Run an initialĀ 
    Ā to create an approximate profile for a program - this lets AppArmor consider the program for monitoring.
  2. Activate learning or complain mode for all profiled programs by enteringĀ 
    aa-complain /etc/apparmor.d/*
  3. Run the application. Ensure that the running program gets to access each file representing its access needs. As a result, the execution might run for several days through multiple system reboots.
  4. Analyze the log withĀ 
  5. Repeat Step 3 and Step 4 to generate an optimalĀ SystemicĀ profile. Subsequent iterations generate fewer messages and run faster.
  6. Edit the profiles inĀ 
    Ā as required.
  7. Return to enforce mode usingĀ 
    aa-enfore /etc/apparmor.d/*
    Ā which eventually enforces the rules of the profiles.
  8. Rescan all kernel profiles to ensure no conflict.

Modifying an existing AppArmor profile

  • Monitor the system for AppArmor denials
  • dmesg, /var/log/kern.log, /var/log/messages, etc
  • aa-notify
  • RunĀ 
    Ā to update the policy

Disabling AppArmor

In case you would like to disable AppArmor for the current session, you can do so by clearing out all AppArmor profiles for the current session byĀ 

# aa-teardown.

Additionally, to prevent the kernel from loading AppArmor profiles at the next bootĀ disableĀ 

Ā andĀ removeĀ 
apparmor=1 security=apparmor
Ā from kernel parameters.

AppArmor when implemented properly, provides an enhanced level of security to the deployed containers at a program level. There are endless possibilities of creating varied profiles throughĀ Learning Mode, and hence makes it stand apart from the system generatedĀ 

Ā profile.


Join Hacker Noon

Create your free account to unlock your custom reading experience.