| 1 | # gokrazy rsync |
| 2 | |
| 3 | [](https://github.com/gokrazy/rsync/actions/workflows/main.yml) |
| 4 | [](https://sourcegraph.com/github.com/gokrazy/rsync??badge) |
| 5 | |
| 6 | This repository contains a native Go rsync implementation: the `gokr-rsync` |
| 7 | command implements an rsync client and server, which can send or receive files |
| 8 | (all directions supported). Daemon mode is supported, meaning you can deploy |
| 9 | `gokr-rsync` behind SSH (anonymous or authorized), as command or daemon, or |
| 10 | listening directly on the network (on port 873/tcp by default). |
| 11 | |
| 12 | This project accepts contributions as time permits to merge them (best effort). |
| 13 | |
| 14 | ## How do I know this project won’t eat my data? |
| 15 | |
| 16 | This rsync implementation is not as well-tested as the original “tridge” |
| 17 | implementation from the Samba project. gokrazy/rsync was started in 2021 and |
| 18 | doesn’t have many users yet. |
| 19 | |
| 20 | With that warning out of the way, the rsync protocol uses MD4 checksums over |
| 21 | file contents, so at least your file contents should never be able to be |
| 22 | corrupted. |
| 23 | |
| 24 | There is enough other functionality (delta transfers, file metadata, special |
| 25 | files like symlinks or devices, directory structures, etc.) in the rsync |
| 26 | protocol that provides opportunities for bugs to hide. |
| 27 | |
| 28 | I recommend you carefully check that your transfers work, and please do report |
| 29 | any issues you run into! |
| 30 | |
| 31 | ## Existing rsync implementation survey |
| 32 | |
| 33 | | Language | URL | Note | Max Protocol | Server mode? | |
| 34 | |----------|-------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------|--------------| |
| 35 | | C | [RsyncProject/rsync](https://github.com/RsyncProject/rsync) (formerly WayneD/rsync) | original “tridge” implementation; I found [older versions](https://github.com/WayneD/rsync/tree/v2.6.1pre2) easier to study | [32](https://github.com/RsyncProject/rsync/blob/v3.4.1/rsync.h#L114) | ✔ yes | |
| 36 | | C | [kristapsdz/openrsync](https://github.com/kristapsdz/openrsync) | OpenBSD, good docs | [27](https://github.com/kristapsdz/openrsync/blob/e54d57f7572381da2b549d39c7968fc79dac8e1d/extern.h#L30) | ✔ yes | |
| 37 | | **Go** | [gokrazy/rsync](https://github.com/gokrazy/rsync) | → you are here ← | [27](https://github.com/gokrazy/rsync/blob/b3b58770b864613551036a2ef2827b74ace77749/internal/rsyncd/rsyncd.go#L317) | ✔ yes 🎉 | |
| 38 | | **Go** | [jbreiding/rsync-go](https://github.com/jbreiding/rsync-go) | rsync algorithm | | ❌ no | |
| 39 | | **Go** | [kaiakz/rsync-os](https://github.com/kaiakz/rsync-os) | only client/receiver | [27](https://github.com/kaiakz/rsync-os/blob/64e84daeabb1fa4d2c7cf766c196306adfba6cb2/rsync/const.go#L4) | ❌ no | |
| 40 | | **Go** | [knight42](https://gist.github.com/knight42/6ad35ce6fbf96519259b43a8c3f37478) | proxy | | ❌ no | |
| 41 | | **Go** | [c4milo/gsync](https://github.com/c4milo/gsync) | | | ❌ no | |
| 42 | | Java | [APNIC-net/repositoryd](https://github.com/APNIC-net/repositoryd) | archived | | ✔ yes | |
| 43 | | Java | [JohannesBuchner/Jarsync](https://github.com/JohannesBuchner/Jarsync/) | archived, [internet draft RFC “The rsync Network Protocol”](https://github.com/JohannesBuchner/Jarsync/blob/master/jarsync/rsync.txt) | | ✔ yes | |
| 44 | | Java | [perlundq/yajsync](https://github.com/perlundq/yajsync#example) | | | ✔ yes | |
| 45 | | C++ | [gilbertchen/acrosync-library](https://github.com/gilbertchen/acrosync-library) | commercial | | ❌ no | |
| 46 | | Rust | [sourcefrog/rsyn](https://github.com/sourcefrog/rsyn#why-do-this) | archived, client, “rsyn is rsync with no c” | [27](https://github.com/sourcefrog/rsyn/blob/2ebbfcfe999fdf2d1a434d8614d07aa93873461b/src/connection.rs#L38) | ❌ no | |
| 47 | |
| 48 | ## Getting started |
| 49 | |
| 50 | To serve the `/usr/share/man` directory via rsync on `localhost:8730`, use: |
| 51 | |
| 52 | ``` |
| 53 | go install github.com/gokrazy/rsync/cmd/gokr-rsync@latest |
| 54 | gokr-rsync --daemon --gokr.listen=localhost:8730 --gokr.modulemap=man=/usr/share/man |
| 55 | ``` |
| 56 | |
| 57 | You can then copy the contents of the current directory with clients such as |
| 58 | `rsync(1)`: |
| 59 | |
| 60 | ``` |
| 61 | % rsync -v --archive --port 8730 rsync://localhost/man/ man |
| 62 | receiving file list ... done |
| 63 | created directory man |
| 64 | ./ |
| 65 | ar/ |
| 66 | ar/man1/ |
| 67 | […] |
| 68 | zh_TW/man8/userdel.8.gz |
| 69 | zh_TW/man8/usermod.8.gz |
| 70 | |
| 71 | sent 658.973 bytes received 88.012.067 bytes 3.940.935,11 bytes/sec |
| 72 | total size is 84.504.170 speedup is 0,95 |
| 73 | ``` |
| 74 | |
| 75 | …or [`openrsync(1)`](https://github.com/kristapsdz/openrsync), shown doing a |
| 76 | differential update: |
| 77 | |
| 78 | ``` |
| 79 | % openrsync -v --archive --port 8730 rsync://localhost/man/ man |
| 80 | openrsync: warning: connect refused: ::1, localhost |
| 81 | Transfer starting: 40202 files |
| 82 | […] |
| 83 | zh_TW/man8/userdel.8.gz (732 B, 100.0% downloaded) |
| 84 | zh_TW/man8/usermod.8.gz (1.8 KB, 100.0% downloaded) |
| 85 | Transfer complete: 83.93 MB sent, 643.5 KB read, 80.59 MB file size |
| 86 | ``` |
| 87 | |
| 88 | ## Usage / Setup |
| 89 | |
| 90 | | setup | encrypted | authenticated | private files? | privileges | protocol version | config required | |
| 91 | |-----------------------------------------|-----------|--------------------|------------------------|-----------------------------------------------------------------|------------------|---------------------------------------| |
| 92 | | 1. rsync daemon protocol (TCP port 873) | ❌ no | ⚠ rsync (insecure) | ❌ only world-readable | ✔ dropped + [namespace](#privileged-linux-including-gokrazyorg) | ✔ negotiated | config required | |
| 93 | | 2. anon SSH (daemon) | ✔ yes | ✔ rsync | ❌ only world-readable | ✔ dropped + [namespace](#privileged-linux-including-gokrazyorg) | ✔ negotiated | config required | |
| 94 | | 3. SSH (command) | ✔ yes | ✔ SSH | ✔ yes | ⚠ full user | ⚠ assumed | no config | |
| 95 | | 4. SSH (daemon) | ✔ yes | ✔ SSH (+ rsync) | ✔ yes | ⚠ full user | ✔ negotiated | `~/.config/gokr-rsyncd.toml` required | |
| 96 | |
| 97 | Regarding protocol version “assumed”: the flags to send over the network are |
| 98 | computed *before* starting SSH and hence the remote rsync process. You might |
| 99 | need to specify `--protocol=27` explicitly on the client. Once the connection is |
| 100 | established, both sides *do* negotiate the protocol, though. |
| 101 | |
| 102 | ### Setup 1: rsync daemon protocol (TCP port 873) |
| 103 | |
| 104 | Serving rsync daemon protocol on TCP port 873 is only safe where the network |
| 105 | layer ensures trusted communication, e.g. in a local network (LAN), or when |
| 106 | using [Tailscale](https://tailscale.com/) or similar. In untrusted networks, |
| 107 | attackers can eavesdrop on file transfers and possibly even modify file |
| 108 | contents. |
| 109 | |
| 110 | Prefer setup 2 instead. |
| 111 | |
| 112 | Example: |
| 113 | * Server: `gokr-rsync --daemon --gokr.modulemap=module=/srv/rsync-module` |
| 114 | * Client: `rsync rsync://webserver/module/path` |
| 115 | |
| 116 | ### Setup 2: anon SSH (daemon) |
| 117 | |
| 118 | This setup is well suited for serving world-readable files without |
| 119 | authentication. |
| 120 | |
| 121 | Example: |
| 122 | * Server: `gokr-rsync --daemon --gokr.modulemap=module=/srv/rsync-module --gokr.anonssh_listen=:22873` |
| 123 | * Client: `rsync -e ssh rsync://webserver/module/path` |
| 124 | |
| 125 | |
| 126 | ### Setup 3: SSH (command) |
| 127 | |
| 128 | This setup is well suited for interactive one-off transfers or regular backups, |
| 129 | and uses SSH for both encryption and authentication. |
| 130 | |
| 131 | Note that because `gokr-rsync` is invoked with user privileges (not root |
| 132 | privileges), it cannot do [namespacing](#privileged-linux-including-gokrazyorg) |
| 133 | and hence retains more privileges. When serving public data, it is generally |
| 134 | preferable to use setup 2 instead. |
| 135 | |
| 136 | Note that `rsync(1)` assumes the server process understands all flags that it |
| 137 | sends, i.e. is running the same version on client and server, or at least a |
| 138 | compatible-enough version. You can either specify `--protocol=27` on the client, |
| 139 | or use setup 4, which negotiates the protocol version, side-stepping possible |
| 140 | compatibility gaps between rsync clients and `gokr-rsync`. |
| 141 | |
| 142 | Example: |
| 143 | * Server will be started via SSH |
| 144 | * Client: `rsync --rsync-path=gokr-rsync webserver:path` |
| 145 | |
| 146 | ### Setup 4: SSH (daemon) |
| 147 | |
| 148 | This setup is more reliable than setup 3 because the rsync protocol version will |
| 149 | be negotiated between client and server. This setup is slightly inconvenient |
| 150 | because it requires a config file to be present on the server in |
| 151 | `~/.config/gokr-rsyncd.toml`. |
| 152 | |
| 153 | Note that this mode of operation is only implemented by the original “trigde” |
| 154 | rsync, not in openrsync. Apple started shipping openrsync with macOS 15 Sequoia. |
| 155 | For a while, `/usr/libexec/rsync/rsync.samba` was still available, but on more |
| 156 | recent macOS versions you need to use homebrew or Nix to get tridge rsync. |
| 157 | |
| 158 | Example: |
| 159 | * Server will be started via SSH |
| 160 | * Client: `rsync -e ssh --rsync-path=gokr-rsync rsync://webserver/module/path` |
| 161 | |
| 162 | ## Limitations |
| 163 | |
| 164 | ### Bandwidth |
| 165 | |
| 166 | In my tests, `gokr-rsync` can easily transfer data at > 6 Gbit/s. The current |
| 167 | bottleneck is the MD4 algorithm itself (not sure whether in the “tridge” rsync |
| 168 | client, or in `gokr-rsync`). Implementing support for more recent protocol |
| 169 | versions would help here, as these include hash algorithm negotiation with more |
| 170 | recent choices. |
| 171 | |
| 172 | ### Protocol related limitations |
| 173 | |
| 174 | * xattrs (including acls) was introduced in rsync protocol 30, so is currently |
| 175 | not supported. |
| 176 | |
| 177 | ## Supported environments and privilege dropping |
| 178 | |
| 179 | Supported environments: |
| 180 | |
| 181 | 1. systemd (Linux) |
| 182 | 1. privileged Linux (running as `root`, or in a user namespace) |
| 183 | 1. privileged non-Linux |
| 184 | 1. unprivileged Linux, Mac or Windows |
| 185 | |
| 186 | In all environments, the default instructions will take care that: |
| 187 | |
| 188 | * `gokr-rsync` uses Go’s [Traversal-resistant `os.Root` file |
| 189 | APIs](https://go.dev/blog/osroot) to restrict all file access to the specified |
| 190 | paths. |
| 191 | * (On privileged Linux only) Only configured rsync modules from the host file |
| 192 | system are mounted (**read-only**, unless the module is `writable`) into a |
| 193 | Linux mount namespace for `gokr-rsync`, to guard against data modification and |
| 194 | data exfiltration. |
| 195 | * (On Linux only) File system access is restricted using the |
| 196 | [Landlock](https://docs.kernel.org/userspace-api/landlock.html) Linux kernel |
| 197 | security module, which works similar to OpenBSD’s |
| 198 | [`unveil(2)`](https://man.openbsd.org/unveil.2) API. |
| 199 | * (On privileged environments) `gokr-rsync` drops privileges to user `nobody`, |
| 200 | to limit the scope of what an attacker can do when exploiting a vulnerability. |
| 201 | |
| 202 | Known gaps: |
| 203 | |
| 204 | * `gokr-rsync` does not guard against denial of service attacks, i.e. consuming |
| 205 | too many resources (connections, bandwidth, CPU, …). |
| 206 | * See also [Per-IP rate limiting with |
| 207 | iptables](https://making.pusher.com/per-ip-rate-limiting-with-iptables/). |
| 208 | |
| 209 | |
| 210 | ### systemd (unprivileged) |
| 211 | |
| 212 | We provide [a `gokr-rsyncd.socket` and `gokr-rsyncd.service` |
| 213 | file](https://github.com/gokrazy/rsync/tree/main/systemd/) for systemd. These |
| 214 | files enables most of systemd’s security features. You can check by running |
| 215 | `systemd-analyze security gokr-rsyncd.service`, which should result in an |
| 216 | exposure level of “0.2 SAFE” as of systemd 249 (September 2021). |
| 217 | |
| 218 | First, configure your server flags by creating a systemd service override file: |
| 219 | |
| 220 | ```shell |
| 221 | systemctl edit gokr-rsyncd.service |
| 222 | ``` |
| 223 | |
| 224 | In the opened editor, change the file to: |
| 225 | ``` |
| 226 | [Service] |
| 227 | ExecStart= |
| 228 | ExecStart=/usr/bin/gokr-rsync --daemon --gokr.modulemap=pwd=/etc/tmpfiles.d |
| 229 | ``` |
| 230 | |
| 231 | Close the editor and install the service using: |
| 232 | |
| 233 | ```shell |
| 234 | systemctl enable --now gokr-rsyncd.socket |
| 235 | ``` |
| 236 | |
| 237 | Additional hardening recommendations: |
| 238 | |
| 239 | * Restrict which IP addresses are allowed to connect to your rsync server, for example: |
| 240 | * using iptables or nftables on your host system |
| 241 | * using [`gokr-rsync`’s built-in IP allow/deny mechanism](https://github.com/gokrazy/rsync/commit/322543c7c9ee5f9b2128b6f7ccc931d05ae21df1) |
| 242 | * using [systemd’s `IPAddressDeny` and `IPAddressAllow`](https://manpages.debian.org/systemd.resource-control.5) in `gokr-rsyncd.socket` |
| 243 | * To reduce the impact of Denial Of Service attacks, you can restrict resources |
| 244 | with systemd, see [Managing |
| 245 | Resources](http://0pointer.de/blog/projects/resources.html). |
| 246 | * To hide system directories not relevant to any rsync module, use [systemd’s |
| 247 | `TemporaryFileSystem=` and |
| 248 | `BindReadOnlyPaths=`](https://manpages.debian.org/systemd.exec.5) directives |
| 249 | as described in [Use TemporaryFileSystem to hide files or directories from |
| 250 | systemd |
| 251 | services](https://www.sherbers.de/use-temporaryfilesystem-to-hide-files-or-directories-from-systemd-services/). Note |
| 252 | that you [may need to disable `ProtectSystem=strict` due to a |
| 253 | bug](https://github.com/systemd/systemd/issues/18999). |
| 254 | |
| 255 | ### privileged Linux (including gokrazy.org) |
| 256 | |
| 257 | When started as `root` on Linux, `gokr-rsync` will create a [Linux mount |
| 258 | namespace](https://manpages.debian.org/mount_namespaces.7), mount all configured |
| 259 | rsync modules read-only into the namespace, then change into the namespace using |
| 260 | [`chroot(2)`](https://manpages.debian.org/chroot.2) and drop privileges using |
| 261 | [`setuid(2)`](https://manpages.debian.org/setuid.2). |
| 262 | |
| 263 | **Tip:** you can verify which file system objects the daemon process can see by |
| 264 | using `ls -l /proc/$(pidof gokr-rsync)/root/`. |
| 265 | |
| 266 | Additional hardening recommendations: |
| 267 | |
| 268 | * Restrict which IP addresses are allowed to connect to your rsync server, for example: |
| 269 | * using iptables or nftables on your host system |
| 270 | * using [`gokr-rsync`’s built-in IP allow/deny mechanism](https://github.com/gokrazy/rsync/commit/322543c7c9ee5f9b2128b6f7ccc931d05ae21df1) |
| 271 | |
| 272 | ### privileged non-Linux (e.g. Mac) |
| 273 | |
| 274 | When started as `root` on non-Linux (e.g. Mac), `gokr-rsync` will drop |
| 275 | privileges using [`setuid(2)`](https://manpages.debian.org/setuid.2). |
| 276 | |
| 277 | ### unprivileged with write permission (e.g. from a shell) |
| 278 | |
| 279 | To prevent accidental misconfiguration, `gokr-rsync` refuses to start when it |
| 280 | detects that it has write permission in any configured rsync module. |
| 281 |