9.e Manage SELinux port labels
...

One way in which SELinux secures the systen is by monitoring ports. Specifically, it monitors the name of the processes which are running on each port and determines their SELinux Port Type. If the specified port is not in the list (nonstandard), that process is blocked.

This topic includes:

  • Configuring SELinux port labels
  • Short tcp, udp explanation
  • Port label example

Commands:

  • semanage-port(8)
  • systemctl(1)

View SELinux Port Labels
...

SELinux comes pre-configured with an large number of port-application mappings. You can interact with these mappings using the semanage port commands. To list them add the -l or --list argument.

1
semanage port -l | head
2
SELinux Port Type Proto Port Number
3

4
afs3_callback_port_t tcp 7001
5
afs3_callback_port_t udp 7001
6
afs_bos_port_t udp 7007
7
afs_fs_port_t tcp 2040
8
afs_fs_port_t udp 7000, 7005
9
afs_ka_port_t udp 7004
10
afs_pt_port_t tcp 7002
11
afs_pt_port_t udp 7002

📌 EXAM TIP: To navigate through the list of ports on-the-fly, you can pipe the output into less and search for a port type or port number using /.

All port mappings use either the TCP or UDP protocol.

TCP, UDP
...

Both protocols are essential in the modern world. People are generally more exposed to TCP applications. Here are some protocols based on each one:

TCP UDP
Protocols TCP is used by HTTP, HTTPs, FTP, SMTP and Telnet. UDP is used by DNS, DHCP, TFTP, SNMP, RIP, and VoIP.

Briefly, the key difference between these protocols is that TCP requires a handshake. For a bit more info on this topic here is a more comprehensive table.

Adding, Removing Port Labels
...

As a template, manage SELinux port labels as so:

  • semanage port -<a|d> -t <port_type> -p <tcp|udp> <port>
    • -a : add a label
    • -d : delete a label
    • -t : port type
    • -p : protocol

Managing SELinux ports example
...

In this example, a nonstandard port mapping will be added to allow a httpd service to run on it.

  1. Assume a working httpd server:
1
[root@server ~]$ dnf install -y httpd
2
[root@server ~]$ systemctl enable --now httpd
3
[root@server ~]$ curl localhost:80
4
# <h1>Hello World</h1>
  1. Now, the server will be configured so that instead of listening on port 80, it listens on port 8012:
1
[root@server ~]$ vim /etc/httpd/conf/httpd.conf # holds port config. not needed in exam. for demonstration.
2
# ...
3
Listen 8012
4
# ...
  1. Restart httpd, an error occurs:
1
[root@server ~]$ systemctl restart httpd
2
# Job for httpd.service failed because the control process exited with error code.
3
# See "systemctl status httpd.service" and "journalctl -xeu httpd.service" for details.
4

5
[root@server ~]$ systemctl status httpd
6
× httpd.service - The Apache HTTP Server
7
Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
8
Active: failed (Result: exit-code) since Mon 2023-02-13 11:55:52 GMT; 6s ago
9
Docs: man:httpd.service(8) ...
10

11
Feb 13 11:55:52 server systemd[1]: Starting The Apache HTTP Server...
12
Feb 13 11:55:52 server httpd[2174]: (13)Permission denied: AH00072: make_sock: could not bind to address [::]:8012
13
Feb 13 11:55:52 server httpd[2174]: (13)Permission denied: AH00072: make_sock: could not bind to address 0.0.0.0:8012
14
Feb 13 11:55:52 server httpd[2174]: no listening sockets available, shutting down
15
Feb 13 11:55:52 server systemd[1]: Failed to start The Apache HTTP Server.
  1. Add the nonstandard port mapping to SELinux.
1
semanage port -a -t http_port_t -p tcp 8012
  1. Restart httpd
1
[root@server ~]$ systemctl restart httpd
2

3
[root@server ~]$ systemctl status httpd
4
● httpd.service - The Apache HTTP Server
5
Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
6
Active: active (running) since Mon 2023-02-13 11:58:59 GMT; 1h 27min ago
7
Docs: man:httpd.service(8)
8

9
Feb 13 11:58:59 server systemd[1]: Starting The Apache HTTP Server...
10
Feb 13 11:58:59 server httpd[2255]: Server configured, listening on: port 8012
11
Feb 13 11:58:59 server systemd[1]: Started The Apache HTTP Server.
  1. The service now listens on the desired port
1
[root@server ~]$ curl localhost:8012
2
# <h1>Hello World</h1>
3
[root@server ~]$ curl localhost:80
4
# curl: (7) Failed to connect to localhost port 80 after 0 ms: Connection refused