fork/static-web-server

git clone https://git.julia.blue/fork/static-web-server

Jose Quintanadocs: sws systemd service example [skip ci]506f54e

allow-symlink
3.8 KiB110 linesraw
1# Example systemd service unit file (see systemd.service(5) man page) for use
2# with the --fd option of static-web-server.  This allows e.g. binding the
3# server to a TCP port number 0 - 1023 without running the server as root,
4# and/or running sws in an isolated network name space.
5#
6# This also allows sws to be started on-demand.  If sws is restart (e.g. after
7# updating its SSL certificates, or reconfiguring its content directory), new
8# inbound connections will be queued until sws is up and running again.
9#
10# A comprehensive description can be found in:
11# http://0pointer.de/blog/projects/socket-activation.html
12# ...and the linked articles.
13
14[Unit]
15Description=Static Web Server
16Wants=static-web-server.socket
17After=static-web-server.socket
18
19# The options below reflect a reasonably comprehensive sandboxing based on the
20# features available in systemd v247.  Newer versions of systemd may offer
21# additional options for sandboxing.
22#
23# The options below focus on security, when making changes to this unit file
24# you may wish to evaluated the output of:
25# systemd-analyze security static-web-server.service
26#
27# Beyond the limits used here, additional limits can be placed on CPU, memory,
28# and disk I/O, as well as network traffic filters (via eBPF and other
29# mechanisms), and implemented for this server using the systemd override
30# facilities.  See systemd.resource-control(5) for details.
31
32[Service]
33Type=simple
34
35# An example environment file for static-web-server is included in the file:
36# systemd/etc_default_static-web-server
37EnvironmentFile=/etc/default/static-web-server
38
39# File descriptor 0 corresponds to the standard input...
40ExecStart=/usr/local/bin/static-web-server --fd 0
41
42# ...so the following line attaches fd 0 of the static web server process to
43# the socket defined by the corresponding `static-web-server.socket` unit file.
44# Each instance of static-web-server currently only supports listening on a
45# single socket.
46StandardInput=fd:static-web-server.socket
47
48# Debug and tracing output goes to stderr, and can be viewed with e.g.
49# `journalctl -u static-web-server.service`.
50StandardError=journal
51
52Restart=always
53RestartSec=5
54DynamicUser=true
55
56# Make sure to change this value with an existing user 
57SupplementaryGroups=www-data
58
59NoNewPrivileges=yes
60PrivateTmp=yes
61ProtectSystem=strict
62ProtectHome=yes
63CapabilityBoundingSet=
64RestrictNamespaces=true
65
66#RestrictAddressFamilies=none
67# ☟ workaround to implement ☝in older versions of systemd.
68#   see: https://github.com/systemd/systemd/issues/15753
69RestrictAddressFamilies=AF_UNIX
70RestrictAddressFamilies=~AF_UNIX
71
72PrivateDevices=true
73PrivateUsers=true
74PrivateNetwork=true
75ProtectClock=true
76ProtectControlGroups=true
77ProtectKernelLogs=true
78ProtectKernelModules=true
79ProtectKernelTunables=true
80ProtectProc=invisible
81ProcSubset=pid
82RestrictSUIDSGID=true
83SystemCallArchitectures=native
84RestrictRealtime=true
85LockPersonality=true
86RemoveIPC=true
87MemoryDenyWriteExecute=true
88UMask=077
89ProtectHostname=true
90
91# Restrict the use of exotic system calls (bugs in seldom-used syscalls are a
92# historical source of kernel vulnerabilities)...
93SystemCallFilter=@system-service
94# ... It may be possible to restrict this further.  e.g.
95#SystemCallFilter=@signal @basic-io @io-event @network-io @process statx fstat sched_getaffinity getrandom
96# but a process to discover the set of system calls used (e.g. as part of the
97# unit tests) will probably be needed to avoid regressions e.g. due to changes
98# in crates which are used by static-web-server. The following may be useful to
99# record system calls performed:
100# "/usr/bin/strace --summary-only -o sws.syscallstats -- static-web-server [...]"
101# You can view the sets of system calls defined by systemd using:
102# "systemd-analyze syscall-filter"
103
104DevicePolicy=strict
105DeviceAllow=/dev/null rw
106DeviceAllow=/dev/random r
107DeviceAllow=/dev/urandom r
108
109[Install]
110WantedBy=multi-user.target