Skip to content

cmd/strelaysrv: Add optional auth token (fixes #3987) - #8561

Merged
AudriusButkevicius merged 8 commits into
syncthing:mainfrom
entity0xfe:relay_check_token
Oct 1, 2022
Merged

AudriusButkevicius merged 8 commits into
syncthing:mainfrom
entity0xfe:relay_check_token

Conversation

@entity0xfe

Copy link
Copy Markdown
Contributor

Attempt to implement a sort of authentication for relaysrv by checking for a simple token before allowing clients to join.

tested with

./bin/strelaysrv -debug -pools "" -token 'thetoken'
./cmd/strelaysrv/testutil/testutil -relay=relay://localhost:22067?token='thetoken' -keys=/home/user/.config/syncthing/ -test

I'm not quite sure if this is a good way to implement this, or if there are security-implications so i am happy for comments.

Relevant issue: #3987

Make replaysrv check for a token before allowing clients to
join. The token can be set via the replay-uri.
Comment thread cmd/strelaysrv/listener.go Resolved
Comment thread lib/relay/protocol/packets_xdr.go Resolved
@AudriusButkevicius

Copy link
Copy Markdown
Member

In principle, looks good to me.

@calmh / @imsodin ?

@calmh calmh left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor organizational comments...

Comment thread cmd/strelaysrv/listener.go Resolved
Comment thread lib/relay/client/static.go Resolved
@imsodin imsodin changed the title implement authentication via token for relaysrv (fixes #3987) cmd/strelaysrv: Add optional auth token (fixes #3987) Sep 29, 2022
Comment thread cmd/strelaysrv/main.go Resolved
@AudriusButkevicius

AudriusButkevicius commented Sep 29, 2022 •

Copy link
Copy Markdown
Member

Also, I think we should disable joining the pool if its provided, given that's the default behaviour?

@calmh calmh left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So unfortunately it's not that simple. The relay protocol uses the simple, old and outdated XDR encoding and it's not the case there that we can simply add a field. As written the relay server now always expects the token field to be there when deserializing, though it may of course be zero bytes, so it will reject "old" Syncthing clients with an error. The quick hack solution here is probably to check the incoming join request length before deserializing it -- if it's zero bytes it's the old variant and we don't need to deserialize, if it's more than zero bytes it's the new version. A longer term solution would be to evolve the protocol to protobuf but that requires a few more changes.

In prior versions of the protocol JoinRelayRequest did not have a
token field. Trying to unmarshal such a request will result in
an error. Return an empty JoinRelayRequest, that is a request
without token, instead.
@entity0xfe

Copy link
Copy Markdown
Contributor Author

I tried the quick-fix for now, giving the protobuf-change a shot requires some prior learning on my part, but i may tackle that in the future.

@calmh calmh left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

messageTimeout: time.Minute * 2,
connectTimeout: timeout,

token: uri.Query().Get("token"),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just being cautious, are we sure this doesn't panic if you don't have a query string/query param?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just tested it but also the documentation of net/url reads

Query parses RawQuery and returns the corresponding values. It silently discards malformed value pairs.

and

Get gets the first value associated with the given key. If there are no values associated with the key, Get returns the empty string.

@AudriusButkevicius AudriusButkevicius left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lgtm other than the last question

@AudriusButkevicius
AudriusButkevicius merged commit ad986f3 into syncthing:main Oct 1, 2022
calmh added a commit to calmh/syncthing that referenced this pull request Oct 3, 2022
* main:
  docker: Fix handling of PCAP variable (fixes syncthing#8567)
  cmd/stcrashreceiver: Update to more modern panicparse (syncthing#8566)
  cmd/strelaysrv: Add optional auth token (fixes syncthing#3987) (syncthing#8561)
  lib/fs: Skip xattrs test on EOPNOTSUPP (fixes syncthing#8564) (syncthing#8565)
  gui: Add connection status icons to Remote Devices (fixes syncthing#8244) (syncthing#8553)
@calmh calmh added this to the v1.22.1 milestone Oct 17, 2022
@GermanCoding

Copy link
Copy Markdown
Contributor

I don't know if this does what it's supposed to do. As this is currently implemented, it just leaks the relayserver token to discovery, where it can be retrieved by anyone, without authentication. Anyone can then use the token to join the relay, so there isn't really any security right now.

The only requirement is that you need the device ID of a device connected to a "protected" relay. Discovery then tells you the token as part of the announced addresses. Given that syncthing does not consider device ID's to be a secret, it means that the token implemented here is not a secret either.

@calmh

calmh commented Nov 12, 2022

Copy link
Copy Markdown
Member

:D that should probably be improved

@entity0xfe

Copy link
Copy Markdown
Contributor Author

I don't know if this does what it's supposed to do. As this is currently implemented, it just leaks the relayserver token to discovery, where it can be retrieved by anyone, without authentication. Anyone can then use the token to join the relay, so there isn't really any security right now.

The only requirement is that you need the device ID of a device connected to a "protected" relay. Discovery then tells you the token as part of the announced addresses. Given that syncthing does not consider device ID's to be a secret, it means that the token implemented here is not a secret either.

That is unfortunate, i was not aware of this. It is my understanding that, as long as you also run your own discovery-server with a private certificate (requiring its ID on the client), the token should not get leaked to devices you do not control. Is that correct?

@GermanCoding

Copy link
Copy Markdown
Contributor

It is my understanding that, as long as you also run your own discovery-server with a private certificate (requiring its ID on the client), the token should not get leaked to devices you do not control. Is that correct?

I think that depends on how visible/reachable the private discovery server is. AFAIK, discovery servers do not require client certificates on GET requests (only when clients announce/POST themselves), so if your server is reachable for a potential attacker they can still query this information. All clients that have access to the discovery server can see the token.

The server-side certificate on the discovery server doesn't matter, as the attacker can simply skip certificate validation. It's there to ensure authenticity, not for access control.

@entity0xfe

Copy link
Copy Markdown
Contributor Author

It is my understanding that, as long as you also run your own discovery-server with a private certificate (requiring its ID on the client), the token should not get leaked to devices you do not control. Is that correct?

I think that depends on how visible/reachable the private discovery server is. AFAIK, discovery servers do not require client certificates on GET requests (only when clients announce/POST themselves), so if your server is reachable for a potential attacker they can still query this information. All clients that have access to the discovery server can see the token.

The server-side certificate on the discovery server doesn't matter, as the attacker can simply skip certificate validation. It's there to ensure authenticity, not for access control.

Don't you still need to know the ID of the discovery-server, even if it is reachable for a potential attacker?

But i understand, that the behavior as implemented at the moment, is not optimal. Maybe actually using client-certificates for authentication would be a possible way?

@GermanCoding

Copy link
Copy Markdown
Contributor

Don't you still need to know the ID of the discovery-server, even if it is reachable for a potential attacker?

No. The ID of the discovery server is a human-friendly encoding of the discovery server's certificate hash (just like normal st clients). The syncthing client uses it for identity verification (authentication) of the discovery server. This prevents MITM of the connection, but it is not a mechanism that can be used for authorization. Any attacker can immediatly deduce the ID from the certificate simply by connecting to the discovery server (under the assumption that the attacker is not itself subject to MITM during this connection).

But i understand, that the behavior as implemented at the moment, is not optimal. Maybe actually using client-certificates for authentication would be a possible way?

Client certificates are currently used for authentication. Whenever a syncthing client wants to announce itself on the discovery server, it needs to present its certificate (incl. proof of key possession) to authenticate itself. For device probing (requesting info about others) authentication of the client is currently irrelevant, so there's no client authentication here.

I presume your goal is authorization: Authorization is access-control (ensuring that someone is allowed to access something), while authentication is simply the assertion of an identity ("yes, I am really syncthing client 12345"). The discovery server can only perform authentication, as it cannot possibly know which devices are authorized to access it. The design of the discovery server is such that any device is allowed to access it. Identity is asserted (authentication) where necessary, to prevent clients from stealing others identity (which could be misused to advertise malicious addresses in the name of others).

You can of course look into adding some sort of authorization into the discovery server: A list of preshared device IDs that are allowed for example, or a password like with this PR.


With respect to this PR, I would definetly suggest preventing the leak of the token to discovery altogether. Adding authorization to discovery is certainly something that could be interesting for some use cases, but I would consider it a bandaid for the relay server token issue: We should not assume that the relayserver token feature is only used in conjunction with a private discovery server.

The reason the token is currently leaked to discovery appears to be because custom relayservers are currently treated as an URL/URI in the syncthing client. The token is configured as a parameter within this URL, and the entire URL (incl. token) is announced to discovery. This used to be necessary, because the URL also contains the ID of the relay server, which is needed for authentication of the relay server by others. Hence the syncthing client posts the entire URL. However, now that we have added a token to that URL, it is also announced.

A possible solution I imagine (not having actually looked at the codebase too much) is to change the handling of relay server URLs. Perhaps the token could be treated as a special parameter that is filtered from the URL before announcing it to discovery. This would mean that clients attempting to dial a relay server (joining an existing session in relay protocol speak?) cannot know the correct token, but as its implemented in this PR, the token isn't actually required for that purpose, right? So it would just work, even if dialing clients don't know the token.

@entity0xfe

Copy link
Copy Markdown
Contributor Author

Thanks @GermanCoding for the detailed answer. Please bear with me here:

I presume your goal is authorization: Authorization is access-control (ensuring that someone is allowed to access something), while authentication is simply the assertion of an identity ("yes, I am really syncthing client 12345"). The discovery server can only perform authentication, as it cannot possibly know which devices are authorized to access it.

If one would generate the client certificates (~/.config/syncthing/*.pem) by hand with an own CA, would it not be hypothetically possible for the discovery- and realy-server to check if the certificate was signed by that CA and therefore authorize the client? Than one "just" needs to distribute certificates to the private clients and there would be no need for tokens.


This would mean that clients attempting to dial a relay server (joining an existing session in relay protocol speak?) cannot know the correct token, but as its implemented in this PR, the token isn't actually required for that purpose, right? So it would just work, even if dialing clients don't know the token.

I'm not sure if i understand: after your proposed change, a client not having the token configured in its configuration would not be able to deduce the token via discovery and therefore not be able to use the relay announced by discovery, what is what we want correct? What do you mean with "it would just work"?

@GermanCoding

Copy link
Copy Markdown
Contributor

If one would generate the client certificates (~/.config/syncthing/*.pem) by hand with an own CA, would it not be hypothetically possible for the discovery- and realy-server to check if the certificate was signed by that CA and therefore authorize the client? Than one "just" needs to distribute certificates to the private clients and there would be no need for tokens.

For the relay server: In theory yes, but that's a significant philosophy change for syncthing. Syncthing is not based around the PKI-world, but rather uses certificates as a type of preshared public key. CA-signed certificates don't really play well with this. For example, in order to use your own certificates in syncthing you need to enable advanced options to allow different CN names. Syncthing is also currently hardcoded to expect exactly 1 certificate, so its difficult to use things like intermediate certificates - a necessity for a secure PKI. Validating certificate chains is also a rabbit hole in itself. In general this adds a lot of new complexity and moving parts in the syncthing core where previously was none. Securely running your own PKI is also not a trivial task, asking a lot from even experienced users. Syncthing also has no method of revoking certificates currently, another thing that would need to be considered.

For the discovery server, even more code + design changes are needed, since the certificates are currently optional for GET operations and would need to become mandatory in order to enable this.

I'm not sure if i understand: after your proposed change, a client not having the token configured in its configuration would not be able to deduce the token via discovery and therefore not be able to use the relay announced by discovery, what is what we want correct? What do you mean with "it would just work"?

I probably worded this confusingly. What I meant:

My understanding of the syncthing codebase is, that relays work as follows:

  • A syncthing client A "joins" a relay server, meaning that it connects to a relay with a JoinRelayRequest, then lets the connection idle. This is the permanent protocol submode described in the docs.
  • This idling relay server connection is announced via discovery.
  • Syncthing client B wants to connect to client A, and finds the relay server address from discovery.
  • Client B then opens a session towards A, by connecting to the relay via the temporary protocol submode. This involves mostly a ConnectRequest, plus some other unimportant stuff.

WIth the current PR, the current state appears to be that the token is required only for opening a new session with a relay server (the permanent protocol submode). The token is not required in order to join an existing session on a protected relay server (the temporary protocol submode). (I deduced this just from looking at the code, but I'm not 100% sure on this).

If the previous paragraph is the current state, a fix is (more or less) trivial: Just don't leak the token to discovery, and everything will still work as it is now. If the previous paragraph is false, more changes might be needed.

(My thought process was that currently both syncthing clients A and B have the token due to the discovery leak. If the discovery leak is removed only A knows the token. This may be a problem, but with the current logic, it's not)

calmh pushed a commit to syncthing/docs that referenced this pull request Sep 3, 2023
Documentation for PR syncthing/syncthing#8561

Also took the opportunity to document missing flags `-pprof` and
`-version`
@st-review st-review added the frozen-due-to-age Issues closed and untouched for a long time, together with being locked for discussion label Oct 12, 2023
@syncthing syncthing locked and limited conversation to collaborators Oct 12, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

frozen-due-to-age Issues closed and untouched for a long time, together with being locked for discussion

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants