What is SELinux?

SELinux is a special security system built into Linux computers. It helps keep your computer safe and secure. With SELinux, different programs and users on the computer have limited permissions. This means each program or user can only access certain files and do certain actions that they are allowed to do. For example, The web browser can connect to the internet but it cannot read your private documents. This prevents viruses and hackers from gaining full control over your system if they get into one program.

SELinux sets rules about what different programs and users are permitted to do. System administrators enable SELinux and set up these security rules based on their needs.

Table of Content

  • What Is SELinux?
  • How Security-Enhanced Linux Works?
    • SELinux Policies
    • SELinux Labels and Type Enforcement
    • SELinux Modes
  • How to Configure SELinux?
    • Configure SELinux Permanently
    • Configure SELinux Temporarily
  • Linux vs. SELinux: What are the Differences?
    • AppArmor vs. SELinux

What Is SELinux?

SELinux is a special security system built into Linux computers. SELinux was created by the U.S National Security Agency and Red Hat company. The main purpose of the SELinux is to control what different programs and users are allowed to access on the computer. It does this by setting the strict rules. Without SELinux, if a program gets a virus or hacker access, that program could access all files and data on the computer. This is bad. With SELinux, each program and user is limited in what they can see and do. For example, a web browser can connect to websites but cannot read your private documents. This prevents viruses and hackers from causing full damage if they get into one program.

SELinux is always running in the background on Linux systems. Users and programs cannot disable or turn it off easily. This adds an important layer of security. System administrators enable SELinux and set up the access rules for what each program and user is permitted to do on that Linux computer.

How Security-Enhanced Linux Works?

SELinux works by implementing mandatory access controls (MAC). With MAC, sysadmins define which users and processes have access to specific resources rather than relying on less secure broadly-defined permissions. To accomplish this, SELinux uses security policies.

SELinux Policies

SELinux blocks all applications and users by default, allowing access only to those specified in the security policies. Security policies are a set of simple rules that tell SELinux who is allowed to access which parts of the system. These rules set the permissions for each user, program, and resource. SELinux keeps track of every decision (allow or block access) in the Access Vector Cache (AVC). This makes checking permissions faster.

When a program tries to access something, SELinux first checks AVC to see if a decision has already been made. If so, it follows that decision quickly. If not, SELinux looks at the policy rules and makes a new decision.

One key feature is that SELinux can give different permissions to different programs. For example, a web server program might be allowed to read and write files, but other programs cannot. SELinux can also check conditions before allowing access. Maybe a web server can only read/write if the request comes from a trusted address.

SELinux Labels and Type Enforcement

SELinux uses labels with the policy rules to decide what actions to allow for each resource. Admins assign labels to every process, network port, file, etc. Labels include:

  1. User – The Linux user mapped to a SELinux user
  2. Role – The user’s authorized role for that system
  3. Type – This determines the permissions that are enforced
  4. Level (optional) – A security clearance level

The labels match up with the security policy rules, which specify access allowed for each label type.

Label format :

user:role:type:level

The type part of the label is most important. Type enforcement means SELinux checks the type labels, and only allows authorized types to access resources. Each type has a list of permitted actions. When a user/program tries to access something, SELinux compares the type labels. If the types match the policy rules, access is granted.

SELinux Modes

1. Enforcing mode : This is default and most secure. SELinux actively enforces the policy rules, denying any unauthorized access attempts. Blocked attempts are logged.

2. Permissive mode : Less secure but still monitors access. SELinux just logs what would be blocked by policies, but doesn’t actually block it. Useful for testing.

3. Disabled mode : SELinux is completely turned off removing all the access protection. This mode is Only for the troubleshooting.

How to Configure SELinux?

Linux comes with pre-set SELinux security rules, making it easy to configure. Admins can also create their own rules. There are two main ways to configure SELinux – permanently or just for the current session.

Configure SELinux Permanently

To set SELinux rules that stay even after restarting, edit the /etc/selinux/config file. This file controls the SELinux settings. In this file you can change the SELINUX variable to the below.

  • enforcing (blocks unauthorized access)
  • permissive (logs access attempts but doesn’t block)
  • disabled (turns off SELinux completely)

For example to set the permissive mode follow the below steps :

1. Open the config file in any text editor like vim, nano and gedit etc and then Change the SELINUX=permissive after that Save and exit.

2. Restart your computer and then Use the below command to verify the new setting is applied.

Command :

sudo sesstatus

Output :

Configure SELinux Temporarily

You can also change SELinux settings just for your current session. These changes get reset after the restarting. Use the “setenforce” command to Enable selinux temporarily use the first command to Turn on enforcing mode and use the second command to Turn off enforcing mode.

Command :

setenforce 0
setenforce 1

Output :

Linux vs. SELinux: What are the Differences?

1. The purpose : Linux is the operating system that manages the hardware and the software resources onto a computer. SELinux is a security part that protects the Linux from the bad software and the unauthorized access.

2. Access control enforcement : In normal Linux the users and apps can access the whole system if they have the right permissions. This is called the Discretionary Access Control (DAC). With SELinux admins set the exact permissions for each user, program, and resource. SELinux blocks any access attempts that admins do not allow through the security policies.

AppArmor vs. SELinux

  • AppArmor and SELinux both protect the systems from the bad software and the unauthorized access but they work differently.
  • AppArmor uses rule files for each program to allow or block actions. SELinux uses policies that admins make to give exact permissions for users, programs, and resources.
  • AppArmor is mainly used on SUSE and Ubuntu Linux distributions.
  • AppArmor is easier to setup and manage, but has less control. SELinux is more complex but gives admins very detailed control over permissions.

Conclusion

In short The SELinux is a crucial security system that enhances Linux by adding mandatory access controls and granular policies to tightly restrict what users and programs can access on the system.

What is SELinux – FAQs

What is SELinux and why is it important?

SELinux is a security system built into Linux computers. It is important because it stops bad programs or hackers from accessing all your files and data if they get into your computer.

How does SELinux work?

SELinux sets strict rules about what each program and user is allowed to do on the system. It blocks any attempts to access things the rules don’t allow.

Can I turn SELinux off completely?

Yes, you can disable SELinux, but this removes all the extra security protection it provides, so it’s not recommended unless absolutely necessary.

Is it hard to configure SELinux?

Setting up SELinux does require some technical knowledge, as you need to carefully define the security rules and policies. However, many Linux distributions come with pre-configured SELinux settings.

My program can’t access a file it needs – is SELinux blocking it?

Quite possibly! SELinux may be preventing that program from accessing the file based on its security policies. You would need to adjust the SELinux rules to grant access.



Contact Us