All technological notes.
SELinux context
If a file is copied to a different directory, the destination file will receive the destination directory’s context, unless the --preserve=context switch is specified with the cp command to retain the source file’s original context.复制新文件:适应新标签
If a copy operation overwrites the destination file in the same or different directory, the file being copied will receive the context of the overwritten file, unless the --preserve=context switch is specified with the cp command to preserve the source file’s original context. 覆盖复制:保留旧文件标签
If a file is moved to the same or different directory, the SELinux context will remain intact, which may differ from the destination directory’s context. 移动:保留旧标签
If a file is archived with the tar command, use the --selinux option to preserve the context. 存档:默认不保留标签
touch /tmp/sefile2
ll -Z /tmp/sefile2
# -rw-r--r--. 1 root root unconfined_u:object_r:user_tmp_t:s0 0 Feb 10 16:45 /tmp/sefile2
# copy file without context
cp /tmp/sefile2 /etc/default
ll -Z /etc/default/sefile2
# -rw-r--r--. 1 root root unconfined_u:object_r:etc_t:s0 0 Feb 10 16:48 /etc/default/sefile2
The target file (/etc/default/sefile2) received the default context of the destination directory (/etc/default).
# Remove the /etc/default/sefile2 file, and copy it again with the --preserve=context option:
rm /etc/default/sefile2
cp /tmp/sefile2 /etc/default --preserve=context
# confirm
ll -Z /etc/default/sefile2
# -rw-r--r--. 1 root root unconfined_u:object_r:user_tmp_t:s0 0 Feb 10 16:51 /etc/default/sefile2
- The original context (user_tmp_t) is preserved on the target file after the copy operation has finished.
user:role:type:levelUser:
SELinux user associated with the object or process.system_u, unconfined_u.Role:
SELinux types a user can access.object_r: Used for files, directories, and other objects.system_r: Used for system processes.Type:
httpd_sys_content_t: For web server content.ssh_t: For SSH processes.SELinux policies define the access permissions between types.Level:
s0.| CMD | DESC |
|---|---|
ls -Z /var/www/html |
Display SELinux contexts of files and directories |
ps -eZ \| grep httpd |
Display SELinux contexts of running processes |
| CMD | DESC |
|---|---|
matchpathcon /var/www/html/index.html |
Check the default SELinux context for a specific file |
matchpathcon /var/www/html |
Check the default context for a directory |
chcon are not persistent and will reset after a system relabel.| CMD | DESC |
|---|---|
chcon -t httpd_sys_content_t /var/www/html/index.html |
Change the type of a file |
chcon -u system_u -r object_r /var/www/html/index.html |
Change the user and role (rarely needed) |
semanage fcontext commandrelabel command| CMD | DESC |
|---|---|
semanage fcontext -a -t httpd_sys_content_t '/custom/path(/.*)?' |
Add a custom file context rule |
restorecon -Rv /custom/path |
Apply the changes |
| CMD | DESC |
|---|---|
ausearch -m avc -ts recent |
Check the SELinux logs for denials |
grep "denied" /var/log/audit/audit.log |
Analyze AVC Denials |
grep denied /var/log/audit/audit.log \| audit2why |
Generate Human-Readable Explanations |
sealert -a /var/log/audit/audit.log |
View Troubleshooting Suggestions |
# create file
mkdir /tmp/sedir1
touch /tmp/sedir1/sefile1
# list context
ll -dZ /tmp/sedir1
# drwxr-xr-x. 2 root root unconfined_u:object_r:user_tmp_t:s0 21 Feb 10 00:44 /tmp/sedir1
ll -Z /tmp/sedir1/sefile1
# -rw-r--r--. 1 root root unconfined_u:object_r:user_tmp_t:s0 0 Feb 10 00:44 /tmp/sedir1/sefile1
# change contenxt
chcon -v -u user_u -t public_content_t -R /tmp/sedir1/
# changing security context of '/tmp/sedir1/sefile1'
# changing security context of '/tmp/sedir1/'
# confirm
ll -dZ /tmp/sedir1
# drwxr-xr-x. 2 root root user_u:object_r:public_content_t:s0 21 Feb 10 00:44 /tmp/sedir1
ll -Z /tmp/sedir1/sefile1
# -rw-r--r--. 1 root root user_u:object_r:public_content_t:s0 0 Feb 10 00:44 /tmp/sedir1/sefile1
semanage fcontext -lC
# SELinux fcontext type Context
# /tmp/sedir1(/.*)? all files user_u:object_r:public_content_t:s0
# add context to the policy database
semanage fcontext -a -s user_u -t public_content_t "/tmp/sedir1(/.*)?"
semanage fcontext -lC
# SELinux fcontext type Context
# /tmp/sedir1(/.*)? all files user_u:object_r:public_content_t:s0
# Temporarily change context
chcon -v -u staff_u -t etc_t -R /tmp/sedir1
# changing security context of '/tmp/sedir1/sefile1'
# changing security context of '/tmp/sedir1'
ll -dZ /tmp/sedir1; ll -Z /tmp/sedir1/sefile1
# drwxr-xr-x. 2 root root staff_u:object_r:etc_t:s0 21 Feb 10 16:12 /tmp/sedir1
# -rw-r--r--. 1 root root staff_u:object_r:etc_t:s0 0 Feb 10 16:12 /tmp/sedir1/sefile1
# Apply the New Context to the Files
restorecon -Rv /tmp/sedir1
# Relabeled /tmp/sedir1 from staff_u:object_r:etc_t:s0 to staff_u:object_r:public_content_t:s0
# Relabeled /tmp/sedir1/sefile1 from staff_u:object_r:etc_t:s0 to staff_u:object_r:public_content_t:s0
# confirm
ll -dZ /tmp/sedir1; ll -Z /tmp/sedir1/sefile1
# drwxr-xr-x. 2 root root staff_u:object_r:public_content_t:s0 21 Feb 10 16:12 /tmp/sedir1
# -rw-r--r--. 1 root root staff_u:object_r:public_content_t:s0 0 Feb 10 16:12 /tmp/sedir1/sefile1
context of processes (subjects) and files (objects).httpd needs to serve a file located in /var/www/html/index.html.httpd_t) can access the file (httpd_sys_content_t) based on the defined policyhttpd_t type.ps -eZ | grep httpd
# system_u:system_r:httpd_t:s0 1066 ? 00:00:00 httpd
/var/www/html should have the type httpd_sys_content_t to allow the web server to access them.ls -Z /var/www/html/hello_world/index.html
# unconfined_u:object_r:httpd_sys_content_t:s0 /var/www/html/hello_world/index.html
Policy Rules
Access Request
/var/www/html/hello_world/index.html, SELinux checks:
httpd_t).httpd_sys_content_t).Handling Denied Access
ausearch -m avc -ts recentFixing Contexts
restorecon or chcon.SELinux Ports
http_port_t type.ssh_port_t type.http_port_t: For HTTP services.ssh_port_t: For SSH services.mysqld_port_t: For MySQL services.TCP/UDP Ports
TCP and UDP ports.Audit Logs for Port Denials
/var/log/audit/audit.log for denial messages.firewalldsemanage port ensures the changes are permanent for SELinux./var/log/audit/audit.log for details.netstat -tuln or ss -tuln to verify services listening on ports.semanage export to back up SELinux configurations, including port mappings.semanage export > selinux_config_backup.txt| CMD | DESC |
|---|---|
semanage port -l |
List all SELinux-managed ports and their associated types |
semanage port -l \| grep ':8080' |
View a Specific Port’s Context |
semanage port -a -t http_port_t -p tcp 8080 |
Add a New Port Context |
semanage port -a -t ftp_port_t -p tcp 2100-2105 |
Add an SELinux type to a range of ports. |
semanage port -m -t mysqld_port_t -p tcp 8080 |
Modify an Existing Port Context |
semanage port -d -t http_port_t -p tcp 8080 |
Remove a port from a specific SELinux type. |
# List (-l) the ports for the httpd service
semanage port -l | grep ^http_port
# http_port_t tcp 80, 81, 443, 488, 8008, 8009, 8443, 9000
# Add (-a) port 8010 with type (-t) http_port_t and protocol (-p) tcp to the policy
semanage port -a -t http_port_t -p tcp 8010
# confirm
semanage port -Cl
# SELinux Port Type Proto Port Number
# http_port_t tcp 8010
semanage port -l | grep ^http_port
# http_port_t tcp 8010, 80, 81, 443, 488, 8008, 8009, 8443, 9000
# Delete (-d) port 8010 from the policy and confirm:
semanage port -d -p tcp 8010
# confirm
semanage port -Cl
# return nothting
semanage port -l | grep ^http_port
# http_port_t tcp 80, 81, 443, 488, 8008, 8009, 8443, 9000