You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@river.apache.org by Peter Firmstone <ji...@zeus.net.au> on 2011/02/05 11:57:04 UTC

The internet - Proxy Isolation - Denial of Service Attack.

The following class, isolates a proxy on it's own thread and using 
reflection, isolates it completely from the rest of the executing 
environment, by passing method calls as tasks.

Forgive the tab formatting.

Cheers,

Peter.

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.river.impl.security.dos;

import java.io.IOException;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import net.jini.io.MarshalledInstance;

/**
 * A preliminary experiment into Isolating a Smart Proxy.
 *
 * I think I'll investigate creating a Permission for additional threads
 * for improved performance and also creating an event model so clients
 * don't need to wait for remote method returns.  I'll create a new 
interface
 * for this, that can be implemented by services directly too.
 *
 * Alternative, only maintain this data structure for as long as it takes
 * to verify proxy trust, then return proxy to the client or have the client
 * thread call the smart proxy via the reflection proxy directly.
 *
 * REMIND: Investigate subclass method returns, this would be simple, if the
 * smart proxy is confined to it's own ClassLoader, we just check the class
 * of the object returned isn't from that ClassLoader, unless the smart 
proxy
 * has a ServiceAPISubclassPermission or something like that.
 *
 * @author Peter Firmstone
 */
public class ProxyIsolate implements InvocationHandler {
    private volatile Object smartProxy;
    private volatile ExecutorService proxyExecutor;
    private volatile Throwable thrown;
    private final long timeout;
    private final TimeUnit unit;
   
    @SuppressWarnings("unchecked")
    public ProxyIsolate(MarshalledInstance proxy,
                ClassLoader defaultLoader,
                boolean verifyCodebaseIntegrity,
                ClassLoader verifierLoader,
                Collection context,
                            long timeout,
                            TimeUnit timeUnit
        ) throws TimeoutException, InterruptedException, ExecutionException{
    this.timeout = timeout;
        unit = timeUnit;
    thrown = null;
    proxyExecutor = Executors.newSingleThreadExecutor(new Factory(this));
    UnmarshallProxyTask task =
        new UnmarshallProxyTask(proxy, defaultLoader,
        verifyCodebaseIntegrity, verifierLoader, context);
        smartProxy = proxyExecutor.submit(task).get(timeout, timeUnit);
    }
   
    private Object taskInvoke(Object proxy, Method method, Object[] 
args) throws
        Exception {
    String methodName = method.getName();
    if (method.getDeclaringClass() == Object.class)  {
        // Handle the Object public methods.
        if (methodName.equals("hashCode"))  {
        return new Integer(System.identityHashCode(proxy));  
        } else if (methodName.equals("equals")) {
        return (proxy == args[0] ? Boolean.TRUE : Boolean.FALSE);
        } else if (methodName.equals("toString")) {
        return proxy.getClass().getName() + '@' + 
Integer.toHexString(proxy.hashCode());
        }
    }
    return method.invoke(smartProxy, args);   
    }
   
    @SuppressWarnings("unchecked")
    public Object invoke(Object proxy, Method method, Object[] args) 
throws Throwable {
    if (isTerminated()) throw new IOException(thrown);
    Callable task = new MethodInvocationTask(this, proxy, method, args);
    return proxyExecutor.submit(task).get(timeout, unit);
    }
   
    private void terminate(Throwable e){
    thrown = e;
    smartProxy = null;
    proxyExecutor.shutdown();   
    }
   
    private boolean isTerminated(){
    if (smartProxy == null) return true;
    return false;
    }
   
    private static class Factory implements ThreadFactory{
    private static final ThreadGroup tg = new ThreadGroup("IsolatedProxy");
    {
        tg.setDaemon(true);
        tg.setMaxPriority(4);
    }
    private final ProxyIsolate proxy;
    Factory(ProxyIsolate spi){
        proxy = spi;
    }

    public Thread newThread(Runnable r) {
        Thread t = new Thread(tg, r);
        t.setUncaughtExceptionHandler(new ExceptionHandler(proxy));
        return t;
    }   
    }
   
    private static class ExceptionHandler implements 
Thread.UncaughtExceptionHandler{
    private final ProxyIsolate proxy;
   
    ExceptionHandler(ProxyIsolate spi){
        proxy = spi;
    }

    public void uncaughtException(Thread t, Throwable e) {
        // For all other Exceptions we let the ExecutorService handle it.
        if ( e instanceof Error){
        // Do we want to take different actions based on the error?
        proxy.terminate(e);
        }
    }
   
    }
   
    private static class UnmarshallProxyTask implements Callable{
    private final MarshalledInstance mi;
    private final ClassLoader defaultLoader;
    private final boolean verifyCodebaseIntegrity;
    private final ClassLoader verifierLoader;
    private final Collection context;
   
    UnmarshallProxyTask(MarshalledInstance proxy,
        ClassLoader defaultLoader,
        boolean verifyCodebaseIntegrity,
        ClassLoader verifierLoader,
        Collection context)
    {
        mi = proxy;
        this.defaultLoader = defaultLoader;
        this.verifyCodebaseIntegrity = verifyCodebaseIntegrity;
        this.verifierLoader = verifierLoader;
        this.context = context;
    }   

    public Object call() throws Exception {
        return mi.get(defaultLoader, verifyCodebaseIntegrity,
            verifierLoader, context);
    }
   
    }
   
    private static class MethodInvocationTask implements Callable {
    private final ProxyIsolate smartProxy;
    private final Object proxy;
    private final Method method;
    private final Object[] args;
    MethodInvocationTask(ProxyIsolate target, Object proxy, Method 
method, Object[] args){
        smartProxy = target;
        this.proxy = proxy;
        this.method = method;
        this.args = args;       
    }
    public Object call() throws Exception {
        return smartProxy.taskInvoke(proxy, method, args);
    }
       
    }

}


Re: The internet - Proxy Isolation - Denial of Service Attack.

Posted by Peter Firmstone <ji...@zeus.net.au>.
Notice the code below also encapsulates the codebase download too, so 
denials of service using large codebases, InputStream's, deserialization 
etc are encapsulated.

This is fine for a smart proxy that implements a known service 
interface, but what about ServiceUI?

Any thoughts?

Cheers,

Peter.

Peter Firmstone wrote:
> The following class, isolates a proxy on it's own thread and using 
> reflection, isolates it completely from the rest of the executing 
> environment, by passing method calls as tasks.
>
> Forgive the tab formatting.
>
> Cheers,
>
> Peter.
>
> /*
> * Licensed to the Apache Software Foundation (ASF) under one
> * or more contributor license agreements.  See the NOTICE file
> * distributed with this work for additional information
> * regarding copyright ownership. The ASF licenses this file
> * to you under the Apache License, Version 2.0 (the
> * "License"); you may not use this file except in compliance
> * with the License. You may obtain a copy of the License at
> *
> *      http://www.apache.org/licenses/LICENSE-2.0
> *
> * Unless required by applicable law or agreed to in writing, software
> * distributed under the License is distributed on an "AS IS" BASIS,
> * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 
> implied.
> * See the License for the specific language governing permissions and
> * limitations under the License.
> */
>
> package org.apache.river.impl.security.dos;
>
> import java.io.IOException;
> import java.lang.reflect.InvocationHandler;
> import java.lang.reflect.Method;
> import java.util.Collection;
> import java.util.concurrent.Callable;
> import java.util.concurrent.ExecutionException;
> import java.util.concurrent.ExecutorService;
> import java.util.concurrent.Executors;
> import java.util.concurrent.ThreadFactory;
> import java.util.concurrent.TimeUnit;
> import java.util.concurrent.TimeoutException;
> import net.jini.io.MarshalledInstance;
>
> /**
> * A preliminary experiment into Isolating a Smart Proxy.
> *
> * I think I'll investigate creating a Permission for additional threads
> * for improved performance and also creating an event model so clients
> * don't need to wait for remote method returns.  I'll create a new 
> interface
> * for this, that can be implemented by services directly too.
> *
> * Alternative, only maintain this data structure for as long as it takes
> * to verify proxy trust, then return proxy to the client or have the 
> client
> * thread call the smart proxy via the reflection proxy directly.
> *
> * REMIND: Investigate subclass method returns, this would be simple, 
> if the
> * smart proxy is confined to it's own ClassLoader, we just check the 
> class
> * of the object returned isn't from that ClassLoader, unless the smart 
> proxy
> * has a ServiceAPISubclassPermission or something like that.
> *
> * @author Peter Firmstone
> */
> public class ProxyIsolate implements InvocationHandler {
>    private volatile Object smartProxy;
>    private volatile ExecutorService proxyExecutor;
>    private volatile Throwable thrown;
>    private final long timeout;
>    private final TimeUnit unit;
>      @SuppressWarnings("unchecked")
>    public ProxyIsolate(MarshalledInstance proxy,
>                ClassLoader defaultLoader,
>                boolean verifyCodebaseIntegrity,
>                ClassLoader verifierLoader,
>                Collection context,
>                            long timeout,
>                            TimeUnit timeUnit
>        ) throws TimeoutException, InterruptedException, 
> ExecutionException{
>    this.timeout = timeout;
>        unit = timeUnit;
>    thrown = null;
>    proxyExecutor = Executors.newSingleThreadExecutor(new Factory(this));
>    UnmarshallProxyTask task =
>        new UnmarshallProxyTask(proxy, defaultLoader,
>        verifyCodebaseIntegrity, verifierLoader, context);
>        smartProxy = proxyExecutor.submit(task).get(timeout, timeUnit);
>    }
>      private Object taskInvoke(Object proxy, Method method, Object[] 
> args) throws
>        Exception {
>    String methodName = method.getName();
>    if (method.getDeclaringClass() == Object.class)  {
>        // Handle the Object public methods.
>        if (methodName.equals("hashCode"))  {
>        return new Integer(System.identityHashCode(proxy));         } 
> else if (methodName.equals("equals")) {
>        return (proxy == args[0] ? Boolean.TRUE : Boolean.FALSE);
>        } else if (methodName.equals("toString")) {
>        return proxy.getClass().getName() + '@' + 
> Integer.toHexString(proxy.hashCode());
>        }
>    }
>    return method.invoke(smartProxy, args);      }
>      @SuppressWarnings("unchecked")
>    public Object invoke(Object proxy, Method method, Object[] args) 
> throws Throwable {
>    if (isTerminated()) throw new IOException(thrown);
>    Callable task = new MethodInvocationTask(this, proxy, method, args);
>    return proxyExecutor.submit(task).get(timeout, unit);
>    }
>      private void terminate(Throwable e){
>    thrown = e;
>    smartProxy = null;
>    proxyExecutor.shutdown();      }
>      private boolean isTerminated(){
>    if (smartProxy == null) return true;
>    return false;
>    }
>      private static class Factory implements ThreadFactory{
>    private static final ThreadGroup tg = new 
> ThreadGroup("IsolatedProxy");
>    {
>        tg.setDaemon(true);
>        tg.setMaxPriority(4);
>    }
>    private final ProxyIsolate proxy;
>    Factory(ProxyIsolate spi){
>        proxy = spi;
>    }
>
>    public Thread newThread(Runnable r) {
>        Thread t = new Thread(tg, r);
>        t.setUncaughtExceptionHandler(new ExceptionHandler(proxy));
>        return t;
>    }      }
>      private static class ExceptionHandler implements 
> Thread.UncaughtExceptionHandler{
>    private final ProxyIsolate proxy;
>      ExceptionHandler(ProxyIsolate spi){
>        proxy = spi;
>    }
>
>    public void uncaughtException(Thread t, Throwable e) {
>        // For all other Exceptions we let the ExecutorService handle it.
>        if ( e instanceof Error){
>        // Do we want to take different actions based on the error?
>        proxy.terminate(e);
>        }
>    }
>      }
>      private static class UnmarshallProxyTask implements Callable{
>    private final MarshalledInstance mi;
>    private final ClassLoader defaultLoader;
>    private final boolean verifyCodebaseIntegrity;
>    private final ClassLoader verifierLoader;
>    private final Collection context;
>      UnmarshallProxyTask(MarshalledInstance proxy,
>        ClassLoader defaultLoader,
>        boolean verifyCodebaseIntegrity,
>        ClassLoader verifierLoader,
>        Collection context)
>    {
>        mi = proxy;
>        this.defaultLoader = defaultLoader;
>        this.verifyCodebaseIntegrity = verifyCodebaseIntegrity;
>        this.verifierLoader = verifierLoader;
>        this.context = context;
>    }  
>    public Object call() throws Exception {
>        return mi.get(defaultLoader, verifyCodebaseIntegrity,
>            verifierLoader, context);
>    }
>      }
>      private static class MethodInvocationTask implements Callable {
>    private final ProxyIsolate smartProxy;
>    private final Object proxy;
>    private final Method method;
>    private final Object[] args;
>    MethodInvocationTask(ProxyIsolate target, Object proxy, Method 
> method, Object[] args){
>        smartProxy = target;
>        this.proxy = proxy;
>        this.method = method;
>        this.args = args;          }
>    public Object call() throws Exception {
>        return smartProxy.taskInvoke(proxy, method, args);
>    }
>          }
>
> }
>
>


Re: The internet - Proxy Isolation - Denial of Service Attack.

Posted by Peter Firmstone <ji...@zeus.net.au>.
Greg Trasuk wrote:
> On Tue, 2011-02-08 at 10:49, Gregg Wonderly wrote:
>   
>> On 2/7/2011 8:44 PM, trasukg@trasuk.com wrote:
>>     
>>> Seems like this behavior ('isolate proxy') would be something you could specify as an invocation constraint when you prepare the registrar proxy.
>>>       
>> I think that they are focused specifically on the fact that the registrar is 
>> already unmarshalling the proxy before you see it to do proxy preparation.  So, 
>> anything it does in the no-args constructor is a point of exposure to DOS attacks.
>>
>>     
>
> Right, but the registrar itself is represented by a proxy (i.e.
> LookupDiscoveryManager has a 'registrarPreparer' configuration item). 
> Since this behaviour ('isolate service proxy') is orthogonal to the
> lookup method's core functionality, doesn't it make sense to put an
> invocation constraint on the _registrar_ proxy, the same as we might put
> a 'make sure communication with the registrar is confidential'
> constraint?
>
> Cheers,
>
> Greg.
>   

Interesting, I hadn't considered a constraint. It would also be possible 
to isolate the registrar's proxy as well, that would be a discovery 
constraint however.

For a registrar proxy during discovery, the time that we need to protect 
against attack (if we're going to prepare and verify a proxy) is the 
unmarshalling period, when foreign code executes, but for the registrar 
proxy and discovery, the reference to the proxy and the verification 
process is relatively safe as no foreign code is executed.  Which is why 
the discovery denial of service that I posted earlier only protects the 
client during the period where the InputStream is read (Discovery V2 
Protocol) and the registrar's proxy unmarshalled, the returned registrar 
proxy is not wrapped.

It would also be possible to encapsulate the registrar proxy in 
isolation, but I haven't chosen to do this at this stage.

How important is multi threading to proxy's?

With the constraint approach we have to trust the registrar enough for 
it to do this for us, we would have to take the registrar at it's word, 
unless the platform does it for us.  It is possible to add this 
functionality to MarshalledInstance (part of the platform). We might 
allow the client to set configuration, however that's an all or nothing 
approach, it doesn't allow the client a different choice between 
different lookup services.

We could make it a privilege, a permission that if granted to the 
registrar proxy allows it to unmarshall looked up proxy's without 
protection.

But that doesn't feel right either, although it does allow the client to 
authenticate the lookup service before deciding to grant permission.  
When permission isn't granted, it could simply fall back to the safe 
option, without throwing a security exception.

Thoughts?

Peter.


Re: The internet - Proxy Isolation - Denial of Service Attack.

Posted by Gregg Wonderly <gr...@wonderly.org>.
On 2/8/2011 9:59 AM, Greg Trasuk wrote:
>
> On Tue, 2011-02-08 at 10:49, Gregg Wonderly wrote:
>> On 2/7/2011 8:44 PM, trasukg@trasuk.com wrote:
>>> Seems like this behavior ('isolate proxy') would be something you could specify as an invocation constraint when you prepare the registrar proxy.
>> I think that they are focused specifically on the fact that the registrar is
>> already unmarshalling the proxy before you see it to do proxy preparation.  So,
>> anything it does in the no-args constructor is a point of exposure to DOS attacks.
>>
>
> Right, but the registrar itself is represented by a proxy (i.e.
> LookupDiscoveryManager has a 'registrarPreparer' configuration item).
> Since this behaviour ('isolate service proxy') is orthogonal to the
> lookup method's core functionality, doesn't it make sense to put an
> invocation constraint on the _registrar_ proxy, the same as we might put
> a 'make sure communication with the registrar is confidential'
> constraint?

Okay, yes, I see where you are coming from.  I think the ILFactory contraints 
processing is a probable location to deal with this.  BasicInvocationHandler 
itself currently does nothing with constraints after the call is sent down to 
the BasicInvocationDispatcher instance.  So there would be some study needed to 
see how this might work using constraints.

Gregg Wonderly

Re: The internet - Proxy Isolation - Denial of Service Attack.

Posted by Greg Trasuk <tr...@stratuscom.com>.
On Tue, 2011-02-08 at 10:49, Gregg Wonderly wrote:
> On 2/7/2011 8:44 PM, trasukg@trasuk.com wrote:
> > Seems like this behavior ('isolate proxy') would be something you could specify as an invocation constraint when you prepare the registrar proxy.
> I think that they are focused specifically on the fact that the registrar is 
> already unmarshalling the proxy before you see it to do proxy preparation.  So, 
> anything it does in the no-args constructor is a point of exposure to DOS attacks.
> 

Right, but the registrar itself is represented by a proxy (i.e.
LookupDiscoveryManager has a 'registrarPreparer' configuration item). 
Since this behaviour ('isolate service proxy') is orthogonal to the
lookup method's core functionality, doesn't it make sense to put an
invocation constraint on the _registrar_ proxy, the same as we might put
a 'make sure communication with the registrar is confidential'
constraint?

Cheers,

Greg.
-- 
Greg Trasuk, President
StratusCom Manufacturing Systems Inc. - We use information technology to
solve business problems on your plant floor.
http://stratuscom.com


Re: The internet - Proxy Isolation - Denial of Service Attack.

Posted by Gregg Wonderly <gr...@gmail.com>.
On 2/7/2011 8:44 PM, trasukg@trasuk.com wrote:
> Seems like this behavior ('isolate proxy') would be something you could specify as an invocation constraint when you prepare the registrar proxy.
I think that they are focused specifically on the fact that the registrar is 
already unmarshalling the proxy before you see it to do proxy preparation.  So, 
anything it does in the no-args constructor is a point of exposure to DOS attacks.

Gregg Wonderly

> Greg
>
> -----Original Message-----
> From: Peter Firmstone<ji...@zeus.net.au>
> Date: Tue, 08 Feb 2011 09:27:09
> To:<ri...@incubator.apache.org>
> Reply-To: river-dev@incubator.apache.org
> Subject: Re: The internet - Proxy Isolation - Denial of Service Attack.
>
> Dan Creswell wrote:
>> ....
>>
>> On 6 February 2011 06:31, Peter Firmstone<ji...@zeus.net.au>  wrote:
>>
>>
>>> I've refactored Proxy Isolation and added return value checks, to ensure
>>> that remote code cannot be passed to the calling thread.
>>>
>>> It's basically just an invocation handler to be used for creating a
>>> reflective proxy that can be used to pass method calls to a smart proxy,
>>> isolated in it's own thread and ClassLoader.
>>>
>>> The caller communicates with the remote code using method invocation,
>>> return values are checked to ensure remote code does not escape thread
>>> isolation.
>>>
>>> The basic idea behind this class is:
>>>
>>> On the internet, you might find a service (with a smart proxy) your
>>> interested in, but you don't know and cannot authenticate its subject.
>>>   Since purely reflective proxy's on the internet will probably experience
>>> some latency issues, it's more practical to allow execution of remote code
>>> locally.
>>>
>>>
>>>
>> Is that sufficient justification for this or should we be looking to spec
>> things so that one can authenticate etc if it's important or otherwise just
>> proceed as usual?
>>
>> Dunno, thinking out loud but my stock approach is to see about the specs
>> before worrying about code...
>>
>>
> Your right, this is an implementation detail, so we don't want the
> client being exposed to it directly.  The most logical application would
> be by a registrar that provided proxy isolation, as an option in it's
> lookup call.
>
> Which is why I've brought it up now, since we've been discussing the
> lookup service.
>
> The registrar needs to know if the caller requires isolation, and if so
> be provided a timeout either by configuration or directly(which can be
> converted to a RemoteException and logged).
>
> It's still possible for the client to authenticate the proxy and to
> perform proxy preparation and verification.
>
> This form of isolation allows safe unmarshalling of the proxy prior to
> verification, although it would also be desireable to close any input or
> output streams relating to the proxy (I think I have a solution to this
> I'll discuss later).
>
> This can't be part of the proxy verification process, since the proxy
> has been unmarshalled prior to proxy verification.
>
> This needs to be abstracted from both the client and the service proxy.
>
> Cheers,
>
> Peter.
>
>
>
>>
>>> By isolating the smart proxy within it's own thread and classloader, the
>>> smart proxy has no permissions, other than those required to communicate
>>> with it's originating server, it cannot cause a StackOverflowError, all it
>>> can do is consume it's own thread, the calling thread can catch a
>>> TimeoutException and continue.  I'm thinking of encapsulating any Exceptions
>>> in a RemoteException, so existing clients can utilise it.
>>>
>>> This might leave a background daemon thread for some time if code is
>>> malicious, eventually it will experience a stack overflow and be terminated
>>> (if stuck in an endless loop) or be interrupted, but since these threads
>>> have low priority, they can't do much harm, the number a box can withstand
>>> and continue running, depends on how much memory it has, whether it's
>>> running a 64 bit jvm and what the thread stack size is.
>>>
>>>
>>> Cheers,
>>>
>>> Peter.
>>>
>>>
>>>
>>


Re: The internet - Proxy Isolation - Denial of Service Attack.

Posted by tr...@trasuk.com.
Seems like this behavior ('isolate proxy') would be something you could specify as an invocation constraint when you prepare the registrar proxy.

Greg

-----Original Message-----
From: Peter Firmstone <ji...@zeus.net.au>
Date: Tue, 08 Feb 2011 09:27:09 
To: <ri...@incubator.apache.org>
Reply-To: river-dev@incubator.apache.org
Subject: Re: The internet - Proxy Isolation - Denial of Service Attack.

Dan Creswell wrote:
> ....
>
> On 6 February 2011 06:31, Peter Firmstone <ji...@zeus.net.au> wrote:
>
>   
>> I've refactored Proxy Isolation and added return value checks, to ensure
>> that remote code cannot be passed to the calling thread.
>>
>> It's basically just an invocation handler to be used for creating a
>> reflective proxy that can be used to pass method calls to a smart proxy,
>> isolated in it's own thread and ClassLoader.
>>
>> The caller communicates with the remote code using method invocation,
>> return values are checked to ensure remote code does not escape thread
>> isolation.
>>
>> The basic idea behind this class is:
>>
>> On the internet, you might find a service (with a smart proxy) your
>> interested in, but you don't know and cannot authenticate its subject.
>>  Since purely reflective proxy's on the internet will probably experience
>> some latency issues, it's more practical to allow execution of remote code
>> locally.
>>
>>
>>     
> Is that sufficient justification for this or should we be looking to spec
> things so that one can authenticate etc if it's important or otherwise just
> proceed as usual?
>
> Dunno, thinking out loud but my stock approach is to see about the specs
> before worrying about code...
>
>   

Your right, this is an implementation detail, so we don't want the 
client being exposed to it directly.  The most logical application would 
be by a registrar that provided proxy isolation, as an option in it's 
lookup call.

Which is why I've brought it up now, since we've been discussing the 
lookup service.

The registrar needs to know if the caller requires isolation, and if so 
be provided a timeout either by configuration or directly(which can be 
converted to a RemoteException and logged).

It's still possible for the client to authenticate the proxy and to 
perform proxy preparation and verification.

This form of isolation allows safe unmarshalling of the proxy prior to 
verification, although it would also be desireable to close any input or 
output streams relating to the proxy (I think I have a solution to this 
I'll discuss later).

This can't be part of the proxy verification process, since the proxy 
has been unmarshalled prior to proxy verification.

This needs to be abstracted from both the client and the service proxy.

Cheers,

Peter.



>   
>> By isolating the smart proxy within it's own thread and classloader, the
>> smart proxy has no permissions, other than those required to communicate
>> with it's originating server, it cannot cause a StackOverflowError, all it
>> can do is consume it's own thread, the calling thread can catch a
>> TimeoutException and continue.  I'm thinking of encapsulating any Exceptions
>> in a RemoteException, so existing clients can utilise it.
>>
>> This might leave a background daemon thread for some time if code is
>> malicious, eventually it will experience a stack overflow and be terminated
>> (if stuck in an endless loop) or be interrupted, but since these threads
>> have low priority, they can't do much harm, the number a box can withstand
>> and continue running, depends on how much memory it has, whether it's
>> running a 64 bit jvm and what the thread stack size is.
>>
>>
>> Cheers,
>>
>> Peter.
>>
>>
>>     
>
>   

Re: The internet - Proxy Isolation - Denial of Service Attack.

Posted by Peter Firmstone <ji...@zeus.net.au>.
Dan Creswell wrote:
> ....
>
> On 6 February 2011 06:31, Peter Firmstone <ji...@zeus.net.au> wrote:
>
>   
>> I've refactored Proxy Isolation and added return value checks, to ensure
>> that remote code cannot be passed to the calling thread.
>>
>> It's basically just an invocation handler to be used for creating a
>> reflective proxy that can be used to pass method calls to a smart proxy,
>> isolated in it's own thread and ClassLoader.
>>
>> The caller communicates with the remote code using method invocation,
>> return values are checked to ensure remote code does not escape thread
>> isolation.
>>
>> The basic idea behind this class is:
>>
>> On the internet, you might find a service (with a smart proxy) your
>> interested in, but you don't know and cannot authenticate its subject.
>>  Since purely reflective proxy's on the internet will probably experience
>> some latency issues, it's more practical to allow execution of remote code
>> locally.
>>
>>
>>     
> Is that sufficient justification for this or should we be looking to spec
> things so that one can authenticate etc if it's important or otherwise just
> proceed as usual?
>
> Dunno, thinking out loud but my stock approach is to see about the specs
> before worrying about code...
>
>   

Your right, this is an implementation detail, so we don't want the 
client being exposed to it directly.  The most logical application would 
be by a registrar that provided proxy isolation, as an option in it's 
lookup call.

Which is why I've brought it up now, since we've been discussing the 
lookup service.

The registrar needs to know if the caller requires isolation, and if so 
be provided a timeout either by configuration or directly(which can be 
converted to a RemoteException and logged).

It's still possible for the client to authenticate the proxy and to 
perform proxy preparation and verification.

This form of isolation allows safe unmarshalling of the proxy prior to 
verification, although it would also be desireable to close any input or 
output streams relating to the proxy (I think I have a solution to this 
I'll discuss later).

This can't be part of the proxy verification process, since the proxy 
has been unmarshalled prior to proxy verification.

This needs to be abstracted from both the client and the service proxy.

Cheers,

Peter.



>   
>> By isolating the smart proxy within it's own thread and classloader, the
>> smart proxy has no permissions, other than those required to communicate
>> with it's originating server, it cannot cause a StackOverflowError, all it
>> can do is consume it's own thread, the calling thread can catch a
>> TimeoutException and continue.  I'm thinking of encapsulating any Exceptions
>> in a RemoteException, so existing clients can utilise it.
>>
>> This might leave a background daemon thread for some time if code is
>> malicious, eventually it will experience a stack overflow and be terminated
>> (if stuck in an endless loop) or be interrupted, but since these threads
>> have low priority, they can't do much harm, the number a box can withstand
>> and continue running, depends on how much memory it has, whether it's
>> running a 64 bit jvm and what the thread stack size is.
>>
>>
>> Cheers,
>>
>> Peter.
>>
>>
>>     
>
>   


Re: The internet - Proxy Isolation - Denial of Service Attack.

Posted by Dan Creswell <da...@gmail.com>.
....

On 6 February 2011 06:31, Peter Firmstone <ji...@zeus.net.au> wrote:

> I've refactored Proxy Isolation and added return value checks, to ensure
> that remote code cannot be passed to the calling thread.
>
> It's basically just an invocation handler to be used for creating a
> reflective proxy that can be used to pass method calls to a smart proxy,
> isolated in it's own thread and ClassLoader.
>
> The caller communicates with the remote code using method invocation,
> return values are checked to ensure remote code does not escape thread
> isolation.
>
> The basic idea behind this class is:
>
> On the internet, you might find a service (with a smart proxy) your
> interested in, but you don't know and cannot authenticate its subject.
>  Since purely reflective proxy's on the internet will probably experience
> some latency issues, it's more practical to allow execution of remote code
> locally.
>
>
Is that sufficient justification for this or should we be looking to spec
things so that one can authenticate etc if it's important or otherwise just
proceed as usual?

Dunno, thinking out loud but my stock approach is to see about the specs
before worrying about code...


> By isolating the smart proxy within it's own thread and classloader, the
> smart proxy has no permissions, other than those required to communicate
> with it's originating server, it cannot cause a StackOverflowError, all it
> can do is consume it's own thread, the calling thread can catch a
> TimeoutException and continue.  I'm thinking of encapsulating any Exceptions
> in a RemoteException, so existing clients can utilise it.
>
> This might leave a background daemon thread for some time if code is
> malicious, eventually it will experience a stack overflow and be terminated
> (if stuck in an endless loop) or be interrupted, but since these threads
> have low priority, they can't do much harm, the number a box can withstand
> and continue running, depends on how much memory it has, whether it's
> running a 64 bit jvm and what the thread stack size is.
>
>
> Cheers,
>
> Peter.
>
>

Re: The internet - Proxy Isolation - Denial of Service Attack.

Posted by Peter Firmstone <ji...@zeus.net.au>.
I've refactored Proxy Isolation and added return value checks, to ensure 
that remote code cannot be passed to the calling thread.

It's basically just an invocation handler to be used for creating a 
reflective proxy that can be used to pass method calls to a smart proxy, 
isolated in it's own thread and ClassLoader.

The caller communicates with the remote code using method invocation, 
return values are checked to ensure remote code does not escape thread 
isolation.

The basic idea behind this class is:

On the internet, you might find a service (with a smart proxy) your 
interested in, but you don't know and cannot authenticate its subject.  
Since purely reflective proxy's on the internet will probably experience 
some latency issues, it's more practical to allow execution of remote 
code locally.

By isolating the smart proxy within it's own thread and classloader, the 
smart proxy has no permissions, other than those required to communicate 
with it's originating server, it cannot cause a StackOverflowError, all 
it can do is consume it's own thread, the calling thread can catch a 
TimeoutException and continue.  I'm thinking of encapsulating any 
Exceptions in a RemoteException, so existing clients can utilise it.

This might leave a background daemon thread for some time if code is 
malicious, eventually it will experience a stack overflow and be 
terminated (if stuck in an endless loop) or be interrupted, but since 
these threads have low priority, they can't do much harm, the number a 
box can withstand and continue running, depends on how much memory it 
has, whether it's running a 64 bit jvm and what the thread stack size is.

Cheers,

Peter.

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.river.impl.security.dos;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.security.AccessController;
import java.security.PrivilegedExceptionAction;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import net.jini.io.MarshalledInstance;

/**
 * A preliminary experiment into Isolating a Smart Proxy.
 *
 * I think I'll investigate creating a Permission for additional threads
 * for improved performance and also creating an event model so clients
 * don't need to wait for remote method returns.  I'll create a new 
interface
 * for this, that can be implemented by services directly too.
 *
 * Alternative, only maintain this data structure for as long as it takes
 * to verify proxy trust, then return proxy to the client or have the client
 * thread call the smart proxy via the reflection proxy directly.
 *
 * REMIND: Investigate subclassed return values, this is simple, if the
 * smart proxy is confined to it's own ClassLoader, we just check the class
 * of the object returned isn't from that ClassLoader, unless the smart 
proxy
 * has a ServiceAPISubclassPermission or something like that.  The 
reason for
 * checking that remote code hasn't escaped the IsolatedExecutor, is 
that we want to
 * ensure it cannot harm the application threads.
 *
 * @author Peter Firmstone
 */
public class ProxyIsolationHandler implements InvocationHandler {
    private volatile Object smartProxy;
    private final IsolatedExecutor isolate;
    private final long timeout;
    private final TimeUnit unit;
    private final ClassLoader proxyLoader;
   
    @SuppressWarnings("unchecked")
    public ProxyIsolationHandler(MarshalledInstance proxy,
                ClassLoader defaultLoader,
                boolean verifyCodebaseIntegrity,
                ClassLoader verifierLoader,
                Collection context,
                            long timeout,
                            TimeUnit timeUnit
        ) throws TimeoutException, InterruptedException, 
ExecutionException, IsolationException{
        if (defaultLoader == null) throw new IsolationException("A 
default " +
                "ClassLoader must be defined for effective isolation");
        isolate = new IsolatedExecutor();
    this.timeout = timeout;
        unit = timeUnit;
    UnmarshallProxyTask task =
        new UnmarshallProxyTask(proxy, defaultLoader,
        verifyCodebaseIntegrity, verifierLoader, context);
        smartProxy = isolate.process(task, timeout, timeUnit);
        proxyLoader = defaultLoader;
    }
   
    private Object taskInvoke(Object proxy, Method method, Object[] 
args) throws
        Exception {
    String methodName = method.getName();
    if (method.getDeclaringClass() == Object.class)  {
        // Handle the Object public methods.
        if (methodName.equals("hashCode"))  {
        return new Integer(System.identityHashCode(proxy));  
        } else if (methodName.equals("equals")) {
        return (proxy == args[0] ? Boolean.TRUE : Boolean.FALSE);
        } else if (methodName.equals("toString")) {
        return proxy.getClass().getName() + '@' + 
Integer.toHexString(proxy.hashCode());
        }
    }
    return method.invoke(smartProxy, args);   
    }
   
    @SuppressWarnings("unchecked")
    public Object invoke(Object proxy, Method method, Object[] args) 
throws Throwable {
    Callable task = new MethodInvocationTask(this, proxy, method, args);
        Object result = isolate.process(task, timeout, unit);
        CheckReturn cr = new CheckReturn(result, proxyLoader);
        return AccessController.doPrivileged(cr);
    }
   
    private static class UnmarshallProxyTask implements Callable{
    private final MarshalledInstance mi;
    private final ClassLoader defaultLoader;
    private final boolean verifyCodebaseIntegrity;
    private final ClassLoader verifierLoader;
    private final Collection context;
   
    UnmarshallProxyTask(MarshalledInstance proxy,
        ClassLoader defaultLoader,
        boolean verifyCodebaseIntegrity,
        ClassLoader verifierLoader,
        Collection context)
    {
        mi = proxy;
        this.defaultLoader = defaultLoader;
        this.verifyCodebaseIntegrity = verifyCodebaseIntegrity;
        this.verifierLoader = verifierLoader;
        this.context = context;
    }   

    public Object call() throws Exception {
        return mi.get(defaultLoader, verifyCodebaseIntegrity,
            verifierLoader, context);
    }
   
    }
   
    private static class MethodInvocationTask implements Callable {
    private final ProxyIsolationHandler smartProxy;
    private final Object proxy;
    private final Method method;
    private final Object[] args;
    MethodInvocationTask(ProxyIsolationHandler target, Object proxy, 
Method method, Object[] args){
        smartProxy = target;
        this.proxy = proxy;
        this.method = method;
        this.args = args;       
    }
    public Object call() throws Exception {
        return smartProxy.taskInvoke(proxy, method, args);
    }
       
    }
   
    private static class CheckReturn implements PrivilegedExceptionAction{
        private final Object checked;
        private final ClassLoader loader;
        CheckReturn (Object check, ClassLoader loader){
            checked = check;
            this.loader = loader;
        }
        public Object run() throws Exception {
            Class clazz = checked.getClass();
            Class[] classes = clazz.getDeclaredClasses();
            Set<ClassLoader> loaders = new HashSet<ClassLoader>();
            loaders.add(clazz.getClassLoader());
            int l = classes.length;
            for (int i = 0; i < l; i++){
                loaders.add(classes[i].getClassLoader());
            }
            if (loaders.contains(loader)) throw new 
IsolationException("Attempt" +
                    " to return isolated code from isolation classloader");
            return checked;
        }
       
    }

}



/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.river.impl.security.dos;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

/**
 * Performs Callable tasks in an isolated thread, which is terminated
 * if any Errors are experienced.  This may take some time to terminate 
the thread
 * as it is not forcibly stopped.  The thread priority is minimal and a 
daemon
 * thread so it would take a number of unterminated threads to cause 
problems.
 *
 * The caller can give up on the execution of the task by setting a timeout.
 *
 * @param T
 * @author peter
 */
public class IsolatedExecutor<T> {
    private volatile ExecutorService isolateExecutor;
    private volatile Throwable thrown;
   
    public IsolatedExecutor()
    {
        thrown = null;
    isolateExecutor = Executors.newSingleThreadExecutor(new Factory(this));
    }
   
    /**
     * Process Callable tasks in isolation.
     * If an ExecutionException has been thrown, the task should be 
abandoned.
     * If an IsolationException has been thrown, an Error has occurred with
     * the IsolatedExecutor and a new object should be created.
     *
     * @param task
     * @param timeout
     * @param timeUnit
     * @return
     * @throws 
org.apache.river.impl.security.dos.IsolatedExecutor.IsolationException
     * @throws java.util.concurrent.ExecutionException
     */
    public T process(Callable<T> task, long timeout, TimeUnit timeUnit) 
throws
            IsolationException, ExecutionException, InterruptedException,
            TimeoutException {
        if ( thrown != null){
            throw new IsolationException ("Isolate has" +
                  "experienced an error during processing and " +
                  "cannot perform further " +
                  "tasks", thrown);
        }
        return isolateExecutor.submit(task).get(timeout, timeUnit);
    }
   
    private void terminate(Throwable e){
    thrown = e;
    isolateExecutor.shutdownNow();   
    }
   
    private static class Factory implements ThreadFactory{
    private static final ThreadGroup tg = new ThreadGroup("Isolated");
    {
        tg.setDaemon(true);
        tg.setMaxPriority(4);
    }
    private final IsolatedExecutor isolate;
    Factory(IsolatedExecutor uri){
        isolate = uri;
    }

    public Thread newThread(Runnable r) {
        Thread t = new Thread(tg, r);
        t.setUncaughtExceptionHandler(new ExceptionHandler(isolate));
        return t;
    }   
    }
   
    private static class ExceptionHandler implements 
Thread.UncaughtExceptionHandler{
    private final IsolatedExecutor isolate;
   
    ExceptionHandler(IsolatedExecutor uri){
        isolate = uri;
    }

    public void uncaughtException(Thread t, Throwable e) {
        // For all other Exceptions we let the ExecutorService handle it.
        if ( e instanceof Error){
        // Do we want to take different actions based on the error?
        isolate.terminate(e);
        }
    }
   
    }
}