Why dRuby must not face untrusted networks

Anyone who can connect to a dRuby port can make the server unmarshal data and call methods, which can lead to running arbitrary code. What that means in practice.

Updated

Never expose a dRuby port to the internet or to any network with machines or users you do not trust. Anyone who can connect can send the server data to unmarshal and can call methods on its objects, and both of those can be turned into running arbitrary code with the server's privileges. dRuby has no login step, and by default it does not encrypt anything.

The two ways in

Unmarshaling

Every dRuby message is decoded with Marshal.load. Ruby's own documentation for Marshal.load warns against using it on untrusted data, because rebuilding arbitrary objects from attacker-supplied bytes can lead to code execution. A dRuby server calls it on whatever a connecting peer sends, before any of your code has had a chance to look at the request.

Calling methods

The front object is a normal Ruby object, and normal Ruby objects inherit powerful public methods. dRuby refuses calls to private methods, but that still leaves methods such as instance_eval. The security section of dRuby's own documentation uses exactly this as its example of what a hostile client can do:

# What a hostile client could send to an unprotected server
remote = DRbObject.new_with_uri('druby://server.example:8787')
remote.instance_eval('...any Ruby code, run inside the server...')

It does not matter how harmless your front object's own methods are.

It applies in both directions

A process that passes blocks or by-reference objects is also a server, as described on callbacks and blocks. And a client unmarshals whatever the server sends back. So a dRuby client should only connect to servers it trusts, just as a server should only accept peers it trusts.

What to do

In order of importance:

  1. Bind to localhost or a UNIX socket whenever all the processes are on one machine. See localhost, UNIX sockets and SSL.
  2. Keep it on a private network you control when it must cross machines, with a firewall that blocks the port from everywhere else.
  3. Add an ACL so only named addresses can connect. See access control lists.
  4. Use drbssl:// with client certificates if the traffic must be encrypted and each peer identified.
  5. Run the server with as few privileges as possible, so that a compromise is limited to what that user account can do.

What does not make it safe

  • An ACL alone on a hostile network. An ACL filters by the peer's IP address. It does not encrypt traffic and it cannot tell who is using an allowed machine.
  • An obscure port number. Ports are easy to scan.
  • A front object with only a few methods. The unmarshaling risk exists before any method is chosen, and inherited methods are callable too.
  • Encryption without client verification. SSL that accepts any client still lets any client in.

If you need a public interface

Put something designed for untrusted input in front: an HTTP API that parses a plain data format such as JSON, validates it, and then makes the dRuby call itself over localhost. dRuby stays an internal detail between processes you run.