Re: Limiting RMI to localhost



On Mon, 30 Jun 2008, Ronny Schuetz wrote:

I'm writing a big application that I divide into 2 processes (server and interface) with RMI to communicate between them. I would like to limit the access to the server only from the interface process and also only from the local machine. Is there a way to limit RMI to localhost only?

I cannot block all sockets outside the machine since I use them in the
server process.

Can't you setup to RMI server socket to explicitly listen on localhost:<your port>? This way it shouldn't be accessible from outside.

A server socket bound to localhost will be able to receive connections from outside. Unless you mean that <your port> would also be firewalled? That should work.

How about using the version of UnicastRemoteObject.exportObject that takes a pair of socket factories? The important one would be the RMIServerSocketFactory; you could use that to make a special subclass of ServerSocket that overrides accept() to check the client's address. Something like:

public Socket accept() throws IOException {
while (true) {
Socket sock = super.accept() ;
if (sock.getRemoteAddress().equals(sock.getLocalAddress())) {
return sock ;
} else {
// might want to log the attempted connection
try {
sock.close() ;
} catch (IOException e) {
// might want to log this too
}
}
}
}

I'm not absolutely sure that this will always work; if you bind the server socket to localhost, but open connections to yourself via an explicit name, the addreses might not match. I don't really know how to deal with that - what's a good general way of asking if an InetAddress refers to an interface on the local machine? Perhaps you should just make the rule even stricter, and say that only connections via the loopback interface will be accepted; this avoids the problem.

The RMIClientSocketFactory would just open sockets in the normal way. It wouldn't do the clever stuff that RMI's default socket factory does - falling back to HTTP and so on. It could even do the remote-vs-local address checking itself, and abort any attempts to connect to remote machines. This obviously wouldn't be enough to guarantee security, as a malicious attacker could just not use this factory.

Another really cool thing to do would be to write a pair of client and server socket factories that used unix domain sockets (AF_UNIX), rather than TCP/IP sockets (AF_INET). These are local-only by definition, and ought to be faster (but probably aren't). They would only work on unix, though. And there isn't AF_UNIX support in the standard java libraries.

You might also be able to do something by using RMI-IIOP and using the controls your ORB provides.

tom

--
It's odd to discover your quips in other people's .sig files. --
Benjamin Rosenbaum
.



Relevant Pages

  • Re: TCP server stop receiving new connections
    ... before I start the server. ... Maybe has something to do with the Socket ... integrate in your application not just network interface. ...
    (microsoft.public.win32.programmer.networks)
  • Re: Limiting RMI to localhost
    ... and interface) with RMI to communicate between them. ... limit the access to the server only from the interface process and also ... A server socket bound to localhost will be able to receive connections ... server socket factories that used unix domain sockets, ...
    (comp.lang.java.programmer)
  • Re: How to terminate a socket in CLOSE_WAIT state
    ... FTP Server fixed for certain FTP clients who use both passive ... This was causing a PASSIVE opened socket to be left ... but instead expect the "half close" from the receiver. ... this sends a TCP/IP FIN packet to the ...
    (microsoft.public.win32.programmer.kernel)
  • Re: Socket switch delay
    ... both at the client and at the server (and why ... would you set the send buffer size to zero on a non-overlapped ... One glaring error is your client does ... So when you use a single socket, ...
    (microsoft.public.win32.programmer.networks)
  • Re: Applet Hangs when submitting data to servlet
    ... to put a time limit on my socket connections and socket reads. ... public void setTimeoutthrows UnknownHostException, ... on our web server. ... private JButton theSubmitButton_, theClearButton_; ...
    (comp.lang.java.programmer)