Skip to content

Commit 4f066d0

Browse files
authored
systemd: tight sandboxing (#103)
wireproxy needs very little permissions, we can restrict it to basically nothing. DynamicUser means the system will generate a UID on demand for service, also CAP_NET_BIND_SERVICE can be used to allow this user to bind to a port < 1024 if desired. Also LoadCredential lets us read a file with tight permissions i.e. root:root 0400 and pass it to only wireproxy in an ephemeral and constrained manner. Signed-off-by: J. Dekker <jdek@itanimul.li>
1 parent c710def commit 4f066d0

2 files changed

Lines changed: 38 additions & 22 deletions

File tree

systemd/README.md

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,12 @@ The provided systemd unit assumes you have the wireproxy executable installed on
1313

1414
2. If necessary, customize the unit.
1515

16-
Edit the parts with `ExecStartPre=` and `ExecStart=` to point to the executable and the configuration file. For example, if wireproxy is installed on `/usr/bin` and the configuration file is located in `/opt/myfiles/wireproxy.conf` do the following change:
16+
Edit the parts with `LoadCredential`, `ExecStartPre=` and `ExecStart=` to point to the executable and the configuration file. For example, if wireproxy is installed on `/usr/bin` and the configuration file is located in `/opt/myfiles/wireproxy.conf` do the following change:
1717
```service
18-
ExecStartPre=/usr/bin/wireproxy -n -c /opt/myfiles/wireproxy.conf
19-
ExecStart=/usr/bin/wireproxy -c /opt/myfiles/wireproxy.conf
18+
LoadCredential=conf:/opt/myfiles/wireproxy.conf
19+
ExecStartPre=/usr/bin/wireproxy -n -c ${CREDENTIALS_DIRECTORY}/conf
20+
ExecStart=/usr/bin/wireproxy -c ${CREDENTIALS_DIRECTORY}/conf
2021
```
21-
#### 2.2 Drop root privileges (optional, but recommended)
22-
Without any modifications, this Wireproxy service will run as root. You might want to drop those privileges. One way to do this is to simply create a system account for Wireproxy (or just use your own user account to run it instead).
23-
```bash
24-
sudo useradd --comment "Wireproxy tunnel" --system wireproxy
25-
```
26-
Then uncomment these lines from the wireproxy.service:
27-
```service
28-
#User=wireproxy
29-
#Group=wireproxy
30-
```
31-
Caveats:
32-
1) Make sure `wireproxy` user can read the wireproxy configuration file.
33-
2) Also note that unprivileged user cannot bind to ports below 1024 by default.
3422

3523
4. Reload systemd and enable the unit.
3624
```bash

systemd/wireproxy.service

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,43 @@ Wants=network-online.target
44
After=network-online.target
55

66
[Service]
7-
#Uncomment and/or change these if you don't want to run Wireproxy as root
8-
#User=wireproxy
9-
#Group=wireproxy
7+
User=wireproxy
8+
Group=wireproxy
9+
SyslogIdentifier=wireproxy
1010
Type=simple
1111
Restart=on-failure
1212
RestartSec=30s
13-
ExecStartPre=/opt/wireproxy/wireproxy -n -c /etc/wireproxy.conf
14-
ExecStart=/opt/wireproxy/wireproxy -c /etc/wireproxy.conf
15-
SyslogIdentifier=wireproxy
13+
14+
DynamicUser=yes
15+
LoadCredential=conf:/etc/wireproxy.conf
16+
ExecStartPre=/opt/wireproxy/wireproxy -n -c ${CREDENTIALS_DIRECTORY}/conf
17+
ExecStart=/opt/wireproxy/wireproxy -c ${CREDENTIALS_DIRECTORY}/conf
18+
19+
# Required if <1024 port
20+
#AmbientCapabilities=CAP_NET_BIND_SERVICE
21+
#CapabilityBoundingSet=CAP_NET_BIND_SERVICE
22+
LimitNPROC=64
23+
LockPersonality=true
24+
MemoryDenyWriteExecute=true
25+
NoNewPrivileges=true
26+
PrivateDevices=true
27+
PrivateTmp=true
28+
PrivateUsers=true
29+
ProcSubset=pid
30+
ProtectClock=true
31+
ProtectControlGroups=true
32+
ProtectHome=true
33+
ProtectHostname=true
34+
ProtectKernelLogs=true
35+
ProtectKernelModules=true
36+
ProtectKernelTunables=true
37+
ProtectProc=invisible
38+
ProtectSystem=strict
39+
RestrictAddressFamilies=AF_INET AF_INET6 AF_NETLINK
40+
RestrictNamespaces=true
41+
RestrictRealtime=true
42+
SystemCallArchitectures=native
43+
SystemCallFilter=@system-service
1644

1745
[Install]
1846
WantedBy=multi-user.target

0 commit comments

Comments
 (0)