The Linux Security Model
Linux is a multi-user system. Every user has a user ID (UID) and belongs to groups (GID); the administrator account root has UID 0 and can do anything. Security rests on four ideas: authentication (login, using the files /etc/passwd and /etc/shadow, where password hashes are kept readable only by root and managed through PAM, the pluggable authentication modules), authorisation (file permissions and privileges), auditing (logs in /var/log and the audit system) and isolation (processes run with the rights of their owner). Rather than logging in as root, administrators use sudo to run single commands with extra rights, which also logs each use. Extra layers are mandatory access control frameworks: SELinux and AppArmor, which confine each program to a policy so that a compromised service cannot reach everything.
File System Security
Every file has an owner, a group and permissions for the owner, group and others, each as read (r = 4), write (w = 2) and execute (x = 1). The command chmod changes permissions (chmod 754 file), chown changes the owner. Example: 754 means owner rwx (7), group r-x (5), others r-- (4). The umask removes permissions from new files; with umask 022 new files are 644 and new directories 755. Special bits: setuid (a program runs with its owner's rights, risky if the program is flawed), setgid and the sticky bit (on /tmp, only a file's owner can delete it). Access control lists (ACLs) give finer control, and disk encryption (LUKS) protects data at rest.
Vulnerabilities
Typical problems are privilege escalation (a normal user gains root, for example through a flawed setuid program or kernel bug such as Dirty COW, 2016), weak passwords, world-writable files and directories, unpatched services (for example Shellshock in the Bash shell and Heartbleed in the OpenSSL library, both in 2014), misconfigured services and default settings, exposed ports, and malicious packages or scripts.
System Hardening
Hardening reduces the attack surface: apply updates promptly (unattended security updates); remove or disable unused services and packages; close unused ports and use a host firewall (ufw, nftables); disable root login over SSH, prefer key-based SSH, and change the default port only as a minor measure; set strong password policy and account lockout (fail2ban); give users least privilege and use sudo; set safe permissions and a restrictive umask; enable SELinux or AppArmor; use file integrity monitoring (AIDE) and central logging; and keep backups. Security benchmarks from CIS guide the settings.
Definition: Privilege escalation. An attack in which a user or program with limited rights gains higher rights, such as root access, by exploiting a flaw or misconfiguration.
Table 4.1: Linux permission bits
| Octal | Symbolic | Meaning |
|---|---|---|
| 7 | rwx | Read, write, execute |
| 6 | rw- | Read and write |
| 5 | r-x | Read and execute |
| 4 | r-- | Read only |
| 0 | --- | No access |
Example 4.1: Reading and setting permissions
Problem: Write the symbolic form of chmod 754, 644, 755 and 600. With umask 022, find the default permissions of a new file (base 666) and a new directory (base 777).
Solution:
- 754: 7 = rwx, 5 = r-x, 4 = r--, so rwxr-xr--.
- 644: rw-r--r--; 755: rwxr-xr-x; 600: rw-------.
- New file: 666 with the bits of 022 removed = 644.
- New directory: 777 with 022 removed = 755.
Result: rwxr-xr--, rw-r--r--, rwxr-xr-x and rw-------; new files 644 and new directories 755. A private key file should be 600.
def sym(octal):
out = ""
for d in str(octal):
v = int(d)
out += "r" if v & 4 else "-"
out += "w" if v & 2 else "-"
out += "x" if v & 1 else "-"
return out
for mode in (754, 644, 755, 600):
print(mode, sym(mode))
umask = 0o022
print("File:", oct(0o666 & ~umask), "| Dir:", oct(0o777 & ~umask))
Output
754 rwxr-xr--
644 rw-r--r--
755 rwxr-xr-x
600 rw-------
File: 0o644 | Dir: 0o755
Common mistakes
- Logging in and working as root all the time.
- Setting chmod 777 to fix a permission problem.
- Allowing password login for root over SSH.
- Leaving unneeded services and open ports running.
Key takeaways
- UID 0 is root; use sudo; passwords in /etc/shadow.
- Permissions: r = 4, w = 2, x = 1 for owner, group, others.
- SELinux and AppArmor add mandatory access control.
- Hardening: update, remove services, firewall, key-based SSH, monitor.
Practice questions
- What does chmod 754 mean?
- What is SELinux?
- Name two Linux hardening steps.
Answers to the practice questions
- Owner read, write, execute; group read and execute; others read only.
- A mandatory access control framework that confines programs to a security policy.
- Apply updates and disable unused services (also firewall, key-based SSH).
Sources
- Official BCA syllabus Computer Security, course code 25BCA406DS02, Unit IV
