An in-depth look at SELinux and AppArmor

An in-depth look at SELinux and AppArmor

Mar 04, 2025

image

The importance of security on Linux servers.

Security is one of the most critical aspects of Linux servers and if not taken care of, it can lead to unauthorized access to the server and vulnerabilities that can lead to severe consequences.

Here we will take a look at two prominent solutions: SELinux and AppArmor security policy enforcement.

These tools provide us with RAC or Robust Access Control mechanisms when malicious activities occur in the system.

Now let’s compare these two policies and determine where each one can be used?


The Concept of Mandatory Access Control (MAC)

Traditional Linux security models typically use Discretionary Access Control (DAC).
DAC allows users to define and set access permissions for files and resources, but there is a major limitation: if a user’s account is compromised, an attacker can change these permissions and gain access to other parts of the system.

On the other hand, Mandatory Access Control, or MAC, takes a more restrictive, policy-driven approach. MAC enforces security policies at the system level, preventing users and applications from exceeding their intended privileges. This makes MAC essential for use on critical and mission-critical servers, where strict access control is required.

When MAC is implemented on a server, organizations can mitigate the risks associated with unauthorized privilege escalation and ensure that security policies are consistently enforced across all parts of the system.

What is SELinux?

Security-Enhanced Linux (SELinux) was developed by the National Security Agency (NSA) for enhancing security through Mandatory Access Control (MAC). It was introduced into the Linux kernel to provide fine-grained control of system resources and prevent malicious access.

SELinux enforces security through clearly defined policies that specify what processes can do or not do. The most widely used policy, targeted policy, enforces tight controls on certain services only while the remainder of the system operates under traditional discretionary access control (DAC).

Central to SELinux is its security context, which assigns a label to each file, process, and system object. The labels define the access permissions based on the policy rules. SELinux operates in three modes: Enforcing, where all the rules are enforced; Permissive, where the violations are logged but not prevented (a suitable mode for debugging); and Disabled, where SELinux is turned off completely.

AppArmor is an extremely fine-grained security framework that can block privilege escalation attacks and is included with most Linux distributions as a default feature. It may be difficult to configure and debug, however, and can clash with typical system behavior if not configured correctly.

Practical Example: Running Apache with SELinux

To allow Apache to serve files from a custom directory, SELinux policies must be configured:

1. Assign the correct security context:

semanage fcontext -a -t httpd_sys_content_t "/custom/path(/.*)?"

restorecon -Rv /custom/path

2. Allow Apache to connect to the network:

setsebool -P httpd_can_network_connect on

What is AppArmor?

Unlike SELinux, which relies on security contexts and labels, AppArmor takes a simpler approach by using path-based access control. It restricts applications based on predefined profiles, making it easier to configure and manage.

AppArmor works by defining profiles that specify what an application can and cannot do, including file and network access restrictions. It operates in two modes: Enforcing, which strictly applies the rules, and Complaining, which only logs violations without blocking anything—useful for testing and debugging.

AppArmor is easier to set up and manage compared to SELinux, causes fewer disruptions to system operations, and effectively confines applications to enhance security. However, it lacks the fine-grained control of SELinux and relies on well-maintained profiles to function optimally.

Practical Example: Restricting MySQL with AppArmor

To confine MySQL using AppArmor:

  1. Locate the MySQL profile:

    ls /etc/apparmor.d/usr.sbin.mysqld

  2. Modify the profile to restrict file access as needed.

  3. Reload AppArmor to apply changes:

systemctl restart apparmor

One of the main reasons AppArmor is considered more user-friendly than SELinux is that it offers a straightforward, path-based access control. Unlike SELinux, which requires handling security labels and contexts, AppArmor makes it easy for users to implement security policies with minimal complexity. This is why it is a great choice for new Linux security users who want effective protection without experiencing complex configurations.

image

Flexibility and Security: The Power of Policy Enforcement in SELinux

SELinux uses a more advanced approach by enforcing access control using security labels and contexts, with a highly fine-grained security model. This allows for stricter policy enforcement, where applications and users execute within specified security constraints. While this makes SELinux a powerful security tool, it takes more effort to install and debug.

Default Distributions: SELinux in RHEL/Fedora vs. AppArmor in Ubuntu/Debian

SELinux is the default security module within distributions like RHEL, CentOS, and Fedora, where it is firmly integrated into the system security infrastructure. As an alternative, AppArmor is enabled by default in Ubuntu, Debian, and SUSE, offering a simpler solution for mandatory access control (MAC) security.

Best Use Cases for Each Tool

SELinux is best suited to high-security environments where strict access control and detailed policy enforcement are needed. AppArmor, by contrast, is a better fit for those requiring a lighter and more manageable security solution that can still provide strong application confinement.

Common Issues and Debugging Methods

Debugging SELinux : Using audit2allow to analyze and generate policies:

ausearch -m AVC -c "httpd" | audit2allow -M mycustom_policy

semodule -i mycustompolicy.pp

Checking SELinux logs for denied actions:

cat /var/log/audit/audit.log | grep AVC

Debugging AppArmor : Enabling logging and reviewing logs:

journalctl -xe | grep apparmor

tail -f /var/log/syslog | grep APPARMOR

Switching a profile to complain mode for troubleshooting:

aa-complain /etc/apparmor.d/usr.sbin.mysqld

Conclusion

SELinux and AppArmor are each powerful in its own right with varying use cases to which each is well suited. SELinux has a powerful security model, with high granularities of access control and strict access checking. But complexity is the price: configuring and debugging SELinux is difficult, especially for users who do not comprehend its policy model.

AppArmor, however, has a less complex mechanism with its path-based profiles. This is more user-friendly to install and maintain, hence a reasonable choice for users who need security without a steep learning curve. While AppArmor can limit applications well, it does not have the fine-grained control of SELinux.

Both of them are generally selected according to the requirements of the system. SELinux is the default one for enterprise-level security, particularly in systems where strict access control is of extreme importance. It is the default in distributions like RHEL and Fedora, where security policies are tightly integrated. AppArmor is enabled by default in Ubuntu and Debian, and therefore is suitable for those requiring simpler security management with low overhead.

Lastly, understanding how these tools work allows system administrators to choose the right one based on their security requirements, finding a balance between ease of use and security needs.

Gefällt dir dieser Beitrag?

Kaufe ASTRODEVS einen Kaffee