Di dunia Linux, keamanan sistem bukan cuma soal firewall dan password yang kuat. Ada lapisan proteksi tambahan yang sering jadi momok buat pemula, tapi sebenarnya sangat powerful kalau sudah paham: SELinux (Security-Enhanced Linux).
Banyak admin yang langsung disable SELinux begitu ada masalah. Padahal, dengan konfigurasi yang tepat, SELinux bisa jadi tameng tambahan yang melindungi sistem dari eksploitasi.
Panduan ini menjelaskan konsep dasar, mode operasi, dan cara mengelola SELinux tanpa harus mematikannya.
Hal yang Perlu Dipahami Sebelum Mulai
1. Apa itu SELinux?
SELinux adalah implementasi Mandatory Access Control (MAC) yang dikembangkan oleh NSA untuk Linux.
Perbedaan dengan permission Linux tradisional (DAC):Table
| Aspek | DAC (Traditional) | MAC (SELinux) |
|---|---|---|
| Kontrol | User-owner menentukan akses | System policy menentukan akses |
| Root vulnerability | Root = all powerful | Root tetap dibatasi oleh policy |
| Granularity | User, Group, Other | Context, Type, Role, Level |
Intinya: SELinux membatasi apa yang bisa dilakukan oleh proses, meskipun jalan sebagai root.
2. Konsep Dasar SELinux
Security Context
Setiap file, proses, dan resource punya label yang disebut security context:
# Cek context file
ls -Z /etc/passwd
# Output: system_u:object_r:passwd_file_t:s0 /etc/passwd
# Cek context proses
ps auxZ | grep httpd
# Output: system_u:system_r:httpd_t:s0 ...
Format context: user:role:type:level
- Type (misal:
httpd_t,passwd_file_t) → yang paling sering digunakan - Role → untuk RBAC (Role-Based Access Control)
- Level → untuk MLS/MCS (Multi-Level Security)
Policy
Aturan yang menentukan siapa boleh akses apa. Distro umumnya pakai targeted policy (default) atau strict policy.
3. Mode Operasi SELinux
| Mode | Deskripsi | Gunakan saat |
|---|---|---|
| Enforcing | Policy diterapkan, violation ditolak | Production server |
| Permissive | Policy dilog tapi tidak diterapkan | Troubleshooting |
| Disabled | SELinux non-aktif sama sekali | Jangan dipakai kecuali darurat |
⚠️ Peringatan: Disable SELinux mengurangi security posture sistem secara signifikan.
Prosedur Konfigurasi dan Management
Step 1 — Cek Status SELinux
# Cek mode saat ini
getenforce
# Output: Enforcing, Permissive, atau Disabled
# Cek detail konfigurasi
sestatus
# Output contoh:
# SELinux status: enabled
# SELinuxfs mount: /sys/fs/selinux
# SELinux root directory: /etc/selinux
# Loaded policy name: targeted
# Current mode: enforcing
# Mode from config file: enforcing
# Policy MLS status: enabled
# Policy deny_unknown status: allowed
# Memory protection checking: actual (secure)
# Max kernel policy version: 33
Step 2 — Mengubah Mode SELinux
A. Temporary (tidak persisten setelah reboot):
# Ke Permissive (untuk troubleshooting)
sudo setenforce 0
# Ke Enforcing (kembali ke mode ketat)
sudo setenforce 1
B. Permanent (persisten):
Edit file konfigurasi:
sudo nano /etc/selinux/config
Ubah baris:
# Untuk enforcing
SELINUX=enforcing
# Atau permissive
# SELINUX=permissive
# JANGAN PAKE INI kecuali benar-benar perlu:
# SELINUX=disabled
📌 Best practice: Kalau ada aplikasi yang bermasalah, pakai Permissive sementara, jangan langsung Disabled.
Step 3 — Memahami dan Memperbaiki Denial
Cek log denial:
# Cek log AVC (Access Vector Cache)
sudo ausearch -m avc -ts recent
# Atau pakai audit.log
sudo grep "denied" /var/log/audit/audit.log | tail -20
# Tool yang lebih user-friendly
sudo cat /var/log/audit/audit.log | grep denied | audit2why
Contoh log denial:
type=AVC msg=audit(1234567890.123:12345): avc: denied { read } for pid=1234
comm="httpd" name="custom.conf" dev="sda1" ino=123456
scontext=system_u:system_r:httpd_t:s0
tcontext=unconfined_u:object_r:admin_home_t:s0
tclass=file
Terjemahan:
- scontext (source): httpd_t → proses Apache
- tcontext (target): admin_home_t → file di home admin
- { read }: akses yang diminta
- denied: akses ditolak
Apache (httpd_t) mencoba baca file dengan label admin_home_t → ditolak karena policy tidak mengizinkan.
Step 4 — Troubleshooting dengan audit2allow
Tool audit2allow bisa generate policy module dari log denial:
# Install setools-console kalau belum ada
sudo dnf install policycoreutils-python-utils setools-console
# Generate policy dari log denial
sudo grep "denied" /var/log/audit/audit.log | audit2allow -M mypol
# Ini akan buat 2 file:
# mypol.te (Type Enforcement - source code)
# mypol.pp (Compiled policy package)
# Install policy module
sudo semodule -i mypol.pp
⚠️ Hati-hati: Jangan asal install policy tanpa paham apa yang di-allow. Bisa jadi membuka security hole.
Step 5 — Mengelabel File dan Direktori
Cek dan ubah context file:
# Cek context file/direktori
ls -Zd /var/www/html
# Output: unconfined_u:object_r:httpd_sys_content_t:s0
# Cek context yang diharapkan untuk direktori web
sudo matchpathcon /var/www/html
# Output: /var/www/html system_u:object_r:httpd_sys_content_t:s0
# Ubah context file (temporary, hilang setelah restorecon)
sudo chcon -t httpd_sys_content_t /path/to/file
# Ubah context recursive
sudo chcon -R -t httpd_sys_content_t /var/www/mysite
# Restore ke default context (dari policy)
sudo restorecon -Rv /var/www/mysite
Contoh kasus: DocumentRoot Apache di lokasi non-standar
Kalau Apache diarahkan ke /webdata (bukan /var/www/html):
# Cek context saat ini
ls -Zd /webdata
# Output: unconfined_u:object_r:default_t:s0 ← salah
# Set context yang benar untuk web content
sudo semanage fcontext -a -t httpd_sys_content_t "/webdata(/.*)?"
# Apply perubahan
sudo restorecon -Rv /webdata
# Verifikasi
ls -Zd /webdata
# Output: unconfined_u:object_r:httpd_sys_content_t:s0 ← benar
Penjelasan:
semanage fcontext→ tambahkan rule ke policy (persisten)restorecon→ apply rule ke filesystem
Step 6 — Mengelola Boolean
Boolean adalah switch untuk mengaktifkan/nonaktifkan fitur tertentu dalam policy:
# List semua boolean
getsebool -a
# Cek boolean spesifik
getsebool httpd_can_network_connect
# List boolean dengan deskripsi
sudo semanage boolean -l
# Enable boolean (persisten)
sudo setsebool -P httpd_can_network_connect on
# Disable boolean
sudo setsebool -P httpd_can_network_connect off
Boolean umum untuk web server:
| Boolean | Fungsi |
|---|---|
httpd_can_network_connect | Allow httpd connect ke network (database external, API) |
httpd_can_sendmail | Allow httpd kirim email |
httpd_execmem | Allow httpd execute memory (untuk某些 PHP extension) |
httpd_enable_homedirs | Allow httpd akses home directories |
Step 7 — Troubleshooting Port Binding
Kalau aplikasi tidak bisa bind ke port non-standar:
sudo semanage port -l | grep http
# Output: http_port_t tcp 80, 81, 443, 488, 8008, 8009, 8443, 9000
# Tambahkan port custom (misal: 8080)
sudo semanage port -a -t http_port_t -p tcp 8080
# Atau modify port yang sudah ada
sudo semanage port -m -t http_port_t -p tcp 8080
Cheat Sheet Perintah SELinux
| Perintah | Fungsi |
|---|---|
getenforce | Cek mode aktif |
setenforce 0/1 | Ubah mode temporary |
sestatus | Detail status SELinux |
ls -Z | Lihat security context file |
ps auxZ | Lihat security context proses |
chcon | Ubah context temporary |
restorecon | Restore context ke default |
semanage fcontext | Manage file context rules |
semanage port | Manage port labeling |
semanage boolean | Manage boolean |
getsebool | Cek nilai boolean |
setsebool | Ubah nilai boolean |
semodule -l | List loaded policy modules |
ausearch -m avc | Cek AVC denial logs |
audit2why | Interpretasi log denial |
audit2allow | Generate policy dari log |
Troubleshooting Umum
“Permission denied” padahal chmod sudah 777
Ini klasik SELinux denial. Cek dulu:
# Cek apakah SELinux yang nge-block
sudo ausearch -m avc -ts recent
# Kalau iya, cek context-nya
ls -Z /path/to/file
# Fix dengan restorecon atau chcon yang benar
Aplikasi tidak bisa start setelah enable SELinux
sudo journalctl -xe | grep selinux
sudo cat /var/log/audit/audit.log | grep denied
# Temporary ke permissive untuk identifikasi
sudo setenforce 0
# Start aplikasi, cek apa yang di-deny
# Generate policy atau fix context
sudo setenforce 1
SELinux preventing SSH key authentication
ls -Z ~/.ssh/id_rsa
# Harusnya: unconfined_u:object_r:ssh_home_t:s0
# Fix kalau salah context
sudo restorecon -Rv ~/.ssh
# Atau kalau file baru dari backup
sudo chcon -t ssh_home_t ~/.ssh/id_rsa
Catatan Penting
- Jangan disable SELinux di production hanya karena “ribet” → pelajari dulu cara fix-nya
- Test di Permissive mode dulu sebelum deploy ke Enforcing
- Backup policy kalau melakukan custom module:
sudo semodule -l > backup-modules.txt - Dokumentasikan setiap custom policy yang dibuat dengan
audit2allow - Gunakan
semanageuntuk perubahan persistent, jangan cumachcon - Distro berbeda, policy berbeda → RHEL/CentOS/Alma lebih strict dibanding Fedora