[email protected] 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. [Unit]
  2. Description=Syncthing - Open Source Continuous File Synchronization for %I
  3. Documentation=man:syncthing(1)
  4. After=network.target
  5. StartLimitIntervalSec=60
  6. StartLimitBurst=4
  7. [Service]
  8. User=%i
  9. Environment="STLOGFORMATTIMESTAMP="
  10. Environment="STLOGFORMATLEVELSTRING=false"
  11. Environment="STLOGFORMATLEVELSYSLOG=true"
  12. ExecStart=/usr/bin/syncthing serve --no-browser --no-restart
  13. Restart=on-failure
  14. RestartSec=1
  15. SuccessExitStatus=3 4
  16. RestartForceExitStatus=3 4
  17. #############
  18. # SANDBOXING
  19. #############
  20. #
  21. # This section contains best-effort sandboxing of syncthing. Such sandboxing is
  22. # useful to reduce the blast damage of a syncthing exploit.
  23. #
  24. # The sandboxing is "best-effort" only because some of these options are ignored
  25. # if your systemd or kernel are too old or configured in unusual ways. Systemd
  26. # should (but may not) tell you in the journal logs if that's the case. See the
  27. # logs (after starting the service) with:
  28. #
  29. # journalctl --boot --pager-end --unit syncthing@<user-you-used>.service
  30. #
  31. # See systemd's analysis of syncthing's sandbox with:
  32. #
  33. # systemd-analyze security syncthing@<user-you-used>.service
  34. #
  35. # Most of these sandboxing options are documented in `man systemd.exec`.
  36. #
  37. # NOTE: Some of these options _appear_ redundant with each other... but
  38. # depending on the version and configs of systemd and the kernel, some of the
  39. # "redundant" options may be non-functional while others still work.
  40. # We recommend leaving the "redundant" options in place.
  41. # Makes /usr, /boot, /efi and /etc read-only.
  42. ProtectSystem=full
  43. # Protect several system areas syncthing should not be touching.
  44. ProtectKernelTunables=true
  45. ProtectKernelModules=true
  46. ProtectKernelLogs=true
  47. ProtectControlGroups=true
  48. ProtectHostname=true
  49. ProtectClock=true
  50. # No new privileges through SUID/SGID binaries
  51. NoNewPrivileges=true
  52. # Prevents *setting* SUID/SGID bits on files/dirs
  53. RestrictSUIDSGID=true
  54. # Prevent memory pages that are both writable and executable. This kills JIT
  55. # compilers, but syncthing is precompiled.
  56. MemoryDenyWriteExecute=true
  57. # Prevents creation of unprivileged user namespaces which are a significant
  58. # source of privilege escalation exploits.
  59. #
  60. # (In 2023, Google saw 44% of kernel exploits using unpriv. user namespaces.
  61. # Source: https://ubuntu.com/blog/ubuntu-23-10-restricted-unprivileged-user-namespaces)
  62. #
  63. # The service can still be placed *inside* such user namespaces (and is, through
  64. # other sandboxing options), it just can't create any itself.
  65. RestrictNamespaces=true
  66. # RT task scheduling can be abused for denial-of-service
  67. RestrictRealtime=true
  68. # NOTE: This option is poorly named. It doesn't _restrict_ the listed families,
  69. # it _allows_ the listed families. Unlisted ones are restricted.
  70. #
  71. # Specifically, notice the absence of AF_PACKET (raw packets).
  72. # AF_UNIX is needed to support binding to UNIX sockets.
  73. # AF_NETLINK is needed to support hotplugging of network devices and because
  74. # otherwise we see the following (non-fatal) error on startup:
  75. #
  76. # Failed to list network interfaces (error="route ip+net: netlinkrib:
  77. # address family not supported by protocol" log.pkg=upnp)
  78. #
  79. # This option does NOT affect systemd socket passing using .socket units.
  80. RestrictAddressFamilies=AF_INET AF_INET6 AF_NETLINK AF_UNIX
  81. # The lifetime limit of (superuser) capabilities that syncthing can acquire.
  82. # This option _restricts_ capabilities.
  83. CapabilityBoundingSet=
  84. # Start with empty (superuser) capabilities.
  85. # This option _expands_ capabilities.
  86. # AmbientCapabilities should equal CapabilityBoundingSet.
  87. AmbientCapabilities=
  88. # Disables `personality` system call; it can be used for privilege escalation.
  89. LockPersonality=true
  90. # Prevents circumvention of restrictions through the use of x86 syscalls on
  91. # x86-64 systems.
  92. SystemCallArchitectures=native
  93. # Clean up IPC objects after service stops.
  94. RemoveIPC=true
  95. # Create private namespace for System V IPC.
  96. # NOTE: This does not apply to AF_UNIX sockets which are more commonly used.
  97. PrivateIPC=true
  98. # Completely isolated /tmp and /var/tmp
  99. PrivateTmp=disconnected
  100. # New /dev with safe virtual devices like /dev/null
  101. PrivateDevices=true
  102. # Allow access to devices explicitly listed with DeviceAllow and pseudo devices
  103. # like /dev/null.
  104. DevicePolicy=closed
  105. # Creates a new PID namespace. /proc now contains only entries for processes
  106. # in this PID namespace.
  107. PrivatePIDs=true
  108. # Make processes owned by other users hidden in /proc/
  109. ProtectProc=invisible
  110. # Prevent access to non-pid interfaces in /proc.
  111. ProcSubset=pid
  112. # System call allow-list. `@system-service` is a systemd-provided category that
  113. # allows common syscalls needed for system services.
  114. SystemCallFilter=@system-service
  115. # Explicitly disallow @privileged syscalls. Syncthing fails to start if we also
  116. # disallow @resources (which `systemd-analyze` is unhappy about).
  117. # Also disallow io_uring syscalls which are as of 2025 a significant source of
  118. # kernel exploits.
  119. # We do not include io_uring_enter2 because it's just a wrapper for
  120. # io_uring_enter and systemd issues a warning.
  121. SystemCallFilter=~@privileged io_uring_enter io_uring_register io_uring_setup
  122. # Return EPERM when a disallowed syscall is made instead of killing the process.
  123. SystemCallErrorNumber=EPERM
  124. # Digits from left to right; disallow creation of files with:
  125. # - special security-related bits like setuid/setgid
  126. # - (no restrictions on file owner permissions)
  127. # - group-writable access
  128. # - world-readable access
  129. # NOTE: The default value is 0022. We are only restricting special security bits
  130. # and world-readable access.
  131. # NOTE: Syncthing can still _explicitly_ change file permissions using `chmod`.
  132. UMask=7027
  133. # The default HOME folder for system users on Debian-like systems is
  134. # /nonexistent, which should never exist.
  135. # We prevent syncthing from accessing that folder it if was previously created
  136. # through misconfiguration, or from creating it if it's (correctly) missing.
  137. InaccessiblePaths=-/nonexistent
  138. ##################
  139. # OPTIONAL CONFIG
  140. ##################
  141. #
  142. # Users that want to tweak this service file should add a systemd drop-in
  143. # file to avoid changing the original file.
  144. #
  145. # Documentation describing drop-in files:
  146. # https://www.freedesktop.org/software/systemd/man/latest/systemd.unit.html
  147. #
  148. # Example drop-in file location (assuming user "syncthing"):
  149. # /etc/systemd/system/[email protected]/override.conf
  150. #
  151. ## Elevated permissions to sync ownership (disabled by default),
  152. ## see https://docs.syncthing.net/advanced/folder-sync-ownership
  153. ##
  154. ## NOTE:
  155. ## - Use the same value for *both* of these options.
  156. ## - PrivateUsers=false must be set (false is the default, but you might have
  157. ## changed it to true in the "extra credit" section below).
  158. #AmbientCapabilities=CAP_CHOWN CAP_FOWNER
  159. #CapabilityBoundingSet=CAP_CHOWN CAP_FOWNER
  160. #########################
  161. # EXTRA CREDIT FOR USERS
  162. #########################
  163. #
  164. # Users that want to harden their systems further should set the following
  165. # properties. (Also through a systemd drop-in file; see comments above.)
  166. #
  167. ## Makes all of / read-only *except*:
  168. ## - /dev/, /proc/ and /sys/ (see other Protect* options)
  169. ## - ReadWritePaths=
  170. ## - StateDirectory=, LogsDirectory= and similar
  171. ##
  172. ## This cannot be enabled by default because we don't know which folders you wish to
  173. ## share. If enabling this option, enable it along with ReadWritePaths=, e.g.:
  174. ## ReadWritePaths=/my/shared/dir1 /my/shared/dir2
  175. #ProtectSystem=strict
  176. #
  177. ## When enabled, sets up a new user namespace. Maps the "root" user and group as
  178. ## well as the unit's own user and group to themselves and everything else to
  179. ## the "nobody" user and group.
  180. ## This is useful to securely detach the user and group databases used by the
  181. ## unit from the rest of the system, and thus to create an effective sandbox
  182. ## environment.
  183. #PrivateUsers=true
  184. #
  185. ## Makes /home, /root and /run/user *invisible* while allowing BindPaths= and
  186. ## BindReadOnlyPaths= to "carve out" access to parts of those dirs.
  187. ## (Use 'true' instead of 'tmpfs' if you don't need to carve out anything.)
  188. ##
  189. ## "Invisible" is superior to read-only provided by ProtectSystem=strict because
  190. ## it prevents information disclosure of private user data in case of service
  191. ## compromise.
  192. #ProtectHome=tmpfs
  193. #
  194. ## Disallow execution of all binaries. ExecPaths= below carves out exceptions.
  195. ## Can't be enabled by default due to the External File Versioning feature:
  196. ## https://docs.syncthing.net/users/versioning.html#external-file-versioning
  197. ##
  198. ## If you do not use that feature, you can enable both NoExecPaths and
  199. ## ExecPaths.
  200. ## If you do use that featuer, you can still use these options; just add
  201. ## the paths to the binaries you invoke to ExecPaths so they can be executed.
  202. #NoExecPaths=/
  203. ## Allow execution of syncthing and system shared libraries.
  204. ## NOTE: If you are seeing an error like
  205. ## "Failed to execute /some/path/to/syncthing: Permission denied", this is the
  206. ## option you need to update to use your non-standard install location.
  207. #ExecPaths=/usr/bin/syncthing /usr/lib
  208. [Install]
  209. WantedBy=multi-user.target