Access control lists with drb/acl

How to restrict which IP addresses can connect to a dRuby server using ACL.new and DRb.install_acl, with a working example and its limits.

Updated

drb/acl provides the ACL class, a list of allow and deny rules matched against the address of each connecting peer. Install one with DRb.install_acl before you start the service, and dRuby closes connections from addresses the list does not permit.

A working example

require 'drb/drb'
require 'drb/acl'

acl = ACL.new(%w[
  deny all
  allow 127.0.0.1
  allow ::1
  allow 192.168.1.0/24
])

DRb.install_acl(acl)
DRb.start_service('druby://:8787', Object.new)
DRb.thread.join

This server listens on all interfaces but accepts connections only from the machine itself and from addresses in the 192.168.1.0/24 range. Everyone else is disconnected before any request is read.

How the list is written

ACL.new takes a flat array of strings in pairs: a keyword, then a pattern.

  • The keyword is allow or deny.
  • The pattern is all, a single address such as 127.0.0.1 or ::1, or a range in CIDR form such as 192.168.1.0/24.

%w[...] is just a convenient way to write that array; the line breaks are for the reader.

How rules are evaluated

The second argument to ACL.new sets the order, and the default is ACL::DENY_ALLOW. Under that default:

  1. If the address matches any allow rule, it is accepted.
  2. Otherwise, if it matches any deny rule, it is rejected.
  3. Otherwise it is accepted.

Step 3 is the one to remember. With the default order, a list that has no deny all lets through everyone it does not mention. Start every list with deny all and then name what you allow, as in the example.

ACL::ALLOW_DENY reverses the logic: an address that matches a deny rule is rejected, otherwise one that matches an allow rule is accepted, and anything left over is rejected.

Order of calls

Call DRb.install_acl before DRb.start_service. The server picks up the default ACL when it is created, so installing one afterwards does not protect a service that is already running.

You can also give a single server its own list through the config hash, which is useful when one process runs more than one service:

DRb.start_service('druby://:8787', Object.new, { tcp_acl: acl })

Checking a list without a server

ACL#allow_addr? takes an address in the form a socket reports it, an array whose last two entries are the host name and the IP address, so you can test your rules directly:

require 'drb/acl'

acl = ACL.new(%w[deny all allow 127.0.0.1 allow 192.168.1.0/24])

p acl.allow_addr?(['AF_INET', 0, 'localhost', '127.0.0.1'])         # => true
p acl.allow_addr?(['AF_INET', 0, '192.168.1.77', '192.168.1.77'])   # => true
p acl.allow_addr?(['AF_INET', 0, 'example', '203.0.113.9'])         # => false

IPv4 and IPv6

A client may arrive over IPv6 even on one machine, so allow ::1 as well as 127.0.0.1 when you mean "this host".

What an ACL does not do

  • It does not authenticate anyone. It trusts the peer's IP address, so every user and every program on an allowed machine is allowed.
  • It does not encrypt. Traffic is still readable and changeable by anyone on the path.
  • It does not replace a firewall. A rejected peer has still opened a connection to your process. Block the port at the network edge as well.

An ACL is one layer. Combine it with the binding choices on localhost, UNIX sockets and SSL, and read why dRuby must not face untrusted networks for the reasoning.