|
| 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] |
| 15 | +Description=Static Web Server |
| 16 | +Wants=static-web-server.socket |
| 17 | +After=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] |
| 33 | +Type=simple |
| 34 | + |
| 35 | +# An example environment file for static-web-server is included in the file: |
| 36 | +# systemd/etc_default_static-web-server |
| 37 | +EnvironmentFile=/etc/default/static-web-server |
| 38 | + |
| 39 | +# File descriptor 0 corresponds to the standard input... |
| 40 | +ExecStart=/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. |
| 46 | +StandardInput=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`. |
| 50 | +StandardError=journal |
| 51 | + |
| 52 | +Restart=always |
| 53 | +RestartSec=5 |
| 54 | +DynamicUser=true |
| 55 | +SupplementaryGroups=www-data |
| 56 | +NoNewPrivileges=yes |
| 57 | +PrivateTmp=yes |
| 58 | +ProtectSystem=strict |
| 59 | +ProtectHome=yes |
| 60 | +CapabilityBoundingSet= |
| 61 | +RestrictNamespaces=true |
| 62 | + |
| 63 | +#RestrictAddressFamilies=none |
| 64 | +# ☟ workaround to implement ☝in older versions of systemd. |
| 65 | +# see: https://github.com/systemd/systemd/issues/15753 |
| 66 | +RestrictAddressFamilies=AF_UNIX |
| 67 | +RestrictAddressFamilies=~AF_UNIX |
| 68 | + |
| 69 | +PrivateDevices=true |
| 70 | +PrivateUsers=true |
| 71 | +PrivateNetwork=true |
| 72 | +ProtectClock=true |
| 73 | +ProtectControlGroups=true |
| 74 | +ProtectKernelLogs=true |
| 75 | +ProtectKernelModules=true |
| 76 | +ProtectKernelTunables=true |
| 77 | +ProtectProc=invisible |
| 78 | +ProcSubset=pid |
| 79 | +RestrictSUIDSGID=true |
| 80 | +SystemCallArchitectures=native |
| 81 | +RestrictRealtime=true |
| 82 | +LockPersonality=true |
| 83 | +RemoveIPC=true |
| 84 | +MemoryDenyWriteExecute=true |
| 85 | +UMask=077 |
| 86 | +ProtectHostname=true |
| 87 | + |
| 88 | +# Restrict the use of exotic system calls (bugs in seldom-used syscalls are a |
| 89 | +# historical source of kernel vulnerabilities)... |
| 90 | +SystemCallFilter=@system-service |
| 91 | +# ... It may be possible to restrict this further. e.g. |
| 92 | +#SystemCallFilter=@signal @basic-io @io-event @network-io @process statx fstat sched_getaffinity getrandom |
| 93 | +# but a process to discover the set of system calls used (e.g. as part of the |
| 94 | +# unit tests) will probably be needed to avoid regressions e.g. due to changes |
| 95 | +# in crates which are used by static-web-server. The following may be useful to |
| 96 | +# record system calls performed: |
| 97 | +# "/usr/bin/strace --summary-only -o sws.syscallstats -- static-web-server [...]" |
| 98 | +# You can view the sets of system calls defined by systemd using: |
| 99 | +# "systemd-analyze syscall-filter" |
| 100 | + |
| 101 | +DevicePolicy=strict |
| 102 | +DeviceAllow=/dev/null rw |
| 103 | +DeviceAllow=/dev/random r |
| 104 | +DeviceAllow=/dev/urandom r |
| 105 | + |
| 106 | +[Install] |
| 107 | +WantedBy=multi-user.target |
0 commit comments