Sub topic 8
Letting a service use a non-standard port
This chapter assumes you can read a security context and know that a confined process runs in a domain such as httpd_t. It also assumes you can read an AVC denial in the audit log. You need a RHEL-family system with SELinux enforcing and root access through sudo.
Ports carry labels too
Files are not the only objects SELinux labels. Network ports get a type, and the policy decides which domains may bind to a port or connect to it. The policy ships with a list of port numbers and the types they map to. TCP 80 and 443 are http_port_t, TCP 22 is ssh_port_t, and TCP 3306 is mysqld_port_t.
A confined service can only use ports whose types its domain is allowed to use. The httpd_t domain may bind to http_port_t ports. If you tell Apache to listen on a port that has a different type, or no specific type, the kernel refuses the bind() call. This happens even when Apache runs as root and the port is above 1024. Normal Unix permissions never get involved, because SELinux checks separately.
Ports that the policy does not list get the generic type unreserved_port_t. The targeted policy does not let httpd_t bind to that type.
Recognizing the symptom
A service that fails to start after you change its port is the usual sign. Apache shows an error like this:
(13)Permission denied: AH00072: make_sock: could not bind to address [::]:8888
no listening sockets available, shutting downThe message looks like a classic permissions or "port already in use" error. Check the audit log before you chase either one:
sudo ausearch -m avc -ts recentA port denial looks like this:
type=AVC msg=audit(...): avc: denied { name_bind } for pid=2417 comm="httpd" src=8888 scontext=system_u:system_r:httpd_t:s0 tcontext=system_u:object_r:unreserved_port_t:s0 tclass=tcp_socket permissive=0Three fields identify the problem:
{ name_bind }is the permission being checked. A service that connects out to a port is checked forname_connectinstead.src=8888is the port number.tcontext=...:unreserved_port_t:s0is the port's current type.tclass=tcp_socketsays it is a TCP port.
The fix is to change the type that port 8888 has, not to change the process.
Listing and adding port labels
The semanage port command manages port labels. It comes from the policycoreutils-python-utils package on RHEL 8 and later. On RHEL 7 the package is policycoreutils-python.
First, see which ports the web server type already covers:
sudo semanage port -l | grep '^http_port_t'A default system prints something like http_port_t tcp 80, 81, 443, 488, 8008, 8009, 8443, 9000. If your port is in the list, the label is not your problem. If it is not, add it:
sudo semanage port -a -t http_port_t -p tcp 8888The options are:
-aadds a new rule.-tgives the type to assign.-pgives the protocol,tcporudp. Match it to the service. Web servers using HTTP/3 need audpentry as well.- The last argument is a port number or a range such as
9000-9010.
The change is stored in the policy store on disk, so it survives a reboot. It takes effect immediately. You do not need to restart anything except the service that failed.
A worked example
You want Apache to serve an internal app on TCP 8888. Add a listener in a drop-in file:
# /etc/httpd/conf.d/internal.conf
Listen 8888
<VirtualHost *:8888>
ServerName internal.example.com
DocumentRoot /var/www/html
</VirtualHost>Test the configuration, then try the restart:
sudo apachectl configtest
sudo systemctl restart httpd
systemctl is-active httpdThe configuration test passes because the syntax is fine, but the restart fails with the bind error shown earlier. Confirm the cause, label the port, and restart:
sudo ausearch -m avc -ts recent | audit2why
sudo semanage port -a -t http_port_t -p tcp 8888
sudo systemctl restart httpd
ss -ltnp | grep 8888Apache now binds the port. You can confirm the label with sudo semanage port -l | grep 8888.
The sequence below shows what happens during the failed and the successful bind.
When the port already has another type
Some port numbers are already assigned to a different type. For example, 8080 may belong to http_cache_port_t on some policy versions. Adding it again fails:
ValueError: Port tcp/8080 already definedLook up the current assignment first:
sudo semanage port -l | grep -w 8080If the port is defined in the base policy, change its type with -m (modify) instead of -a:
sudo semanage port -m -t http_port_t -p tcp 8080Take care with this. Moving a port to a new type removes it from the old one. If another confined service, such as a caching proxy, expected that port, it now loses access. Check who else uses the port with ss -ltnp and the service documentation before you modify it.
Reviewing and removing local changes
The -C option shows only the changes made on this machine, not the built-in policy:
sudo semanage port -l -CThis is the quickest way to audit what an administrator changed. To undo a change you made, delete it:
sudo semanage port -d -t http_port_t -p tcp 8888You can only delete rules you added locally. A port that the base policy defines cannot be deleted. Use -m to move it to another type instead. If you delete a label while the service is still using that port, the service keeps its existing socket. The next restart then fails.
Outbound connections are a different check
Port labels also control what a service can reach. When Apache or nginx proxies to a backend on port 5000, the check is name_connect against the backend port's type. There are two ways to fix this kind of denial:
- If the backend port has a type that
httpd_tmay already connect to, thehttpd_can_network_connectboolean may be all you need. - If the denial names a specific port type such as
unreserved_port_t, labeling the port withsemanage portcan fix it. The booleanhttpd_can_network_relayand others also exist for specific cases.
Check the AVC line before you choose. A name_connect denial and a name_bind denial look alike but need different fixes. You can also list which port types a domain may use. This command needs the setools-console package:
sudo sesearch -A -s httpd_t -c tcp_socket -p name_bindThe output lists the port types httpd_t may bind. Pick a type from that list that matches the service's purpose. Do not pick one just because it works.
Common mistakes
- Forgetting the firewall. A port label does not open the firewall. If remote clients still cannot connect after the service starts, open the port separately with
sudo firewall-cmd --permanent --add-port=8888/tcpfollowed bysudo firewall-cmd --reload. - Using the wrong protocol. A label added for
tcpdoes nothing for a service that listens onudp. - Choosing an overly broad type. Do not assign a port to a type used by a more powerful service just to make a denial go away. Use the type that matches the service's role.
- Using
chconor editing files. Ports are not files. Onlysemanage portchanges them. - Making a mistake with ranges. A range such as
8000-8999may overlap existing definitions. Add a small, specific range or single ports.
Summary of the procedure
- Read the AVC denial. Confirm
name_bind, the port number, and the current port type. - Run
semanage port -l | grepfor the port number to see whether it already has a type. - Run
semanage port -afor a new port, or-mfor a port the policy already defines. - Restart the service and confirm with
ss -ltnp. - Open the firewall if remote access is needed.
- Record the change, and review local changes with
semanage port -l -C.
Exercise
You set nginx to listen on TCP 8443-style port 9100 and it fails to start with a permission error. ausearch -m avc shows denied { name_bind } ... src=9100 ... tcontext=system_u:object_r:unreserved_port_t:s0. Give the command that fixes this persistently, and explain why chmod or running nginx as root would not have helped.
Show answer
Run sudo semanage port -a -t http_port_t -p tcp 9100, then restart nginx. The failure is an SELinux type check on the port label, not a file permission or user ID check. The nginx_t or httpd_t domain may only bind to certain port types, and unreserved_port_t is not one of them. Running as root does not bypass SELinux, and chmod changes file permission bits, which are unrelated to port labels.
Exercise
You run sudo semanage port -a -t http_port_t -p tcp 8080 and get "Port tcp/8080 already defined". What do you do first, and which command applies the change if you decide to go ahead?
Show answer
First run sudo semanage port -l | grep -w 8080 to see which type owns the port, and check with ss -ltnp whether another service depends on it. If it is safe to move, run sudo semanage port -m -t http_port_t -p tcp 8080. The -m option modifies an existing definition, whereas -a only adds new ones.