[email protected] 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. #
  84. # NOTE: This is set to `CAP_CHOWN CAP_FOWNER` to avoid breaking users that have
  85. # set `AmbientCapabilities=CAP_CHOWN CAP_FOWNER` to enable the `syncOwnership`
  86. # option as described in:
  87. # https://docs.syncthing.net/users/autostart.html#permissions
  88. #
  89. # If you do not use the `syncOwnership` option, you can set this to:
  90. # CapabilityBoundingSet=
  91. CapabilityBoundingSet=CAP_CHOWN CAP_FOWNER
  92. # Start with empty (superuser) capabilities.
  93. # This option _expands_ capabilities.
  94. # AmbientCapabilities should equal CapabilityBoundingSet.
  95. #
  96. # NOTE: IFF you wish to use the `syncOwnership` option, you must set this to:
  97. # AmbientCapabilities=CAP_CHOWN CAP_FOWNER
  98. # in a systemd drop-in file. Be aware that this gives syncthing the ability to
  99. # change or ignore file ownership across the entire operating system.
  100. AmbientCapabilities=
  101. # Disables `personality` system call; it can be used for privilege escalation.
  102. LockPersonality=true
  103. # Prevents circumvention of restrictions through the use of x86 syscalls on
  104. # x86-64 systems.
  105. SystemCallArchitectures=native
  106. # Clean up IPC objects after service stops.
  107. RemoveIPC=true
  108. # Create private namespace for System V IPC.
  109. # NOTE: This does not apply to AF_UNIX sockets which are more commonly used.
  110. PrivateIPC=true
  111. # Completely isolated /tmp and /var/tmp
  112. PrivateTmp=disconnected
  113. # New /dev with safe virtual devices like /dev/null
  114. PrivateDevices=true
  115. # Allow access to devices explicitly listed with DeviceAllow and pseudo devices
  116. # like /dev/null.
  117. DevicePolicy=closed
  118. # Creates a new PID namespace. /proc now contains only entries for processes
  119. # in this PID namespace.
  120. PrivatePIDs=true
  121. # Make processes owned by other users hidden in /proc/
  122. ProtectProc=invisible
  123. # Prevent access to non-pid interfaces in /proc.
  124. ProcSubset=pid
  125. # System call allow-list. `@system-service` is a systemd-provided category that
  126. # allows common syscalls needed for system services.
  127. SystemCallFilter=@system-service
  128. # Return EPERM when a disallowed syscall is made instead of killing the process.
  129. SystemCallErrorNumber=EPERM
  130. # Digits from left to right; disallow creation of files with:
  131. # - special security-related bits like setuid/setgid
  132. # - (no restrictions on file owner permissions)
  133. # - group-writable access
  134. # - world-readable access
  135. # NOTE: The default value is 0022. We are only restricting special security bits
  136. # and world-readable access.
  137. # NOTE: Syncthing can still _explicitly_ change file permissions using `chmod`.
  138. UMask=7027
  139. # The default HOME folder for system users on Debian-like systems is
  140. # /nonexistent, which should never exist.
  141. # We prevent syncthing from accessing that folder it if was previously created
  142. # through misconfiguration, or from creating it if it's (correctly) missing.
  143. InaccessiblePaths=-/nonexistent
  144. ##################
  145. # OPTIONAL CONFIG
  146. ##################
  147. #
  148. # Users that want to tweak this service file should add a systemd drop-in
  149. # file to avoid changing the original file.
  150. #
  151. # Documentation describing drop-in files:
  152. # https://www.freedesktop.org/software/systemd/man/latest/systemd.unit.html
  153. #
  154. # Example drop-in file location (assuming user "syncthing"):
  155. # /etc/systemd/system/[email protected]/override.conf
  156. #
  157. ## Elevated permissions to sync ownership (disabled by default),
  158. ## see https://docs.syncthing.net/advanced/folder-sync-ownership
  159. ##
  160. ## NOTE:
  161. ## - Use the same value for *both* of these options.
  162. ## - PrivateUsers=false must be set (false is the default, but you might have
  163. ## changed it to true in the "extra credit" section below).
  164. #AmbientCapabilities=CAP_CHOWN CAP_FOWNER
  165. #CapabilityBoundingSet=CAP_CHOWN CAP_FOWNER
  166. #########################
  167. # EXTRA CREDIT FOR USERS
  168. #########################
  169. #
  170. # Users that want to harden their systems further should set the following
  171. # properties. (Also through a systemd drop-in file; see comments above.)
  172. #
  173. ## Makes all of / read-only *except*:
  174. ## - /dev/, /proc/ and /sys/ (see other Protect* options)
  175. ## - ReadWritePaths=
  176. ## - StateDirectory=, LogsDirectory= and similar
  177. ##
  178. ## This cannot be enabled by default because we don't know which folders you wish to
  179. ## share. If enabling this option, enable it along with ReadWritePaths=, e.g.:
  180. ## ReadWritePaths=/my/shared/dir1 /my/shared/dir2
  181. #ProtectSystem=strict
  182. #
  183. ## When enabled, sets up a new user namespace. Maps the "root" user and group as
  184. ## well as the unit's own user and group to themselves and everything else to
  185. ## the "nobody" user and group.
  186. ## This is useful to securely detach the user and group databases used by the
  187. ## unit from the rest of the system, and thus to create an effective sandbox
  188. ## environment.
  189. #PrivateUsers=true
  190. #
  191. ## Makes /home, /root and /run/user *invisible* while allowing BindPaths= and
  192. ## BindReadOnlyPaths= to "carve out" access to parts of those dirs.
  193. ## (Use 'true' instead of 'tmpfs' if you don't need to carve out anything.)
  194. ##
  195. ## "Invisible" is superior to read-only provided by ProtectSystem=strict because
  196. ## it prevents information disclosure of private user data in case of service
  197. ## compromise.
  198. #ProtectHome=tmpfs
  199. #
  200. ## Disallow execution of all binaries. ExecPaths= below carves out exceptions.
  201. ## Can't be enabled by default due to the External File Versioning feature:
  202. ## https://docs.syncthing.net/users/versioning.html#external-file-versioning
  203. ##
  204. ## If you do not use that feature, you can enable both NoExecPaths and
  205. ## ExecPaths.
  206. ## If you do use that featuer, you can still use these options; just add
  207. ## the paths to the binaries you invoke to ExecPaths so they can be executed.
  208. #NoExecPaths=/
  209. ## Allow execution of syncthing and system shared libraries.
  210. ## NOTE: If you are seeing an error like
  211. ## "Failed to execute /some/path/to/syncthing: Permission denied", this is the
  212. ## option you need to update to use your non-standard install location.
  213. #ExecPaths=/usr/bin/syncthing /usr/lib
  214. [Install]
  215. WantedBy=multi-user.target