You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@felix.apache.org by BOTTARO Andre RD-MAPS-GRE <an...@orange-ftgroup.com> on 2007/01/17 16:39:48 UTC

Code sharing and code isolation

 
For security purposes, I would like to prevent bundles from using classes that belong to private packages of any bundle.

I checked that if I have a hacking bundle (B) with the following code, it will be able to call a method (criticalAlert) on a private class (private.packages.Sample) of a bundle (A), i.e. a class which neither exported by A nor imported by B. In order to do that, B must acces the classloader of A, thus I suppose that B imports an exported package (public.packages) from A:

	public void start(BundleContext bc) throws Exception {	
		//Getting an instance of an exported class from A...
		public.packages.Sample sample = new public.packages.Sample();
	
		//Trying to access the private class from A...
		ClassLoader loader = sample.getClass().getClassLoader();
		Class clazz = loader.loadClass("private.packages.Sample");
		Object o = clazz.newInstance();
		Method m = o.getClass().getMethod("criticalAlert", new Class[]{});
		m.invoke(o,new Object[]{});
	}

So, tell me if I am wrong, the OSGi solution to this bundle isolation issue is to use Java 2 permissions (with OSGi CPA or PA) to forbid the use of reflection (Class.newInstance and java.lang.reflect) to untrusted bundles. 

There is also another threat (even more direct) in OSGi R4 : the use of Fragments. Are there other threats ?

André

Re: Code sharing and code isolation

Posted by "Richard S. Hall" <he...@ungoverned.org>.
BOTTARO Andre RD-MAPS-GRE wrote:
>  
> For security purposes, I would like to prevent bundles from using classes that belong to private packages of any bundle.
>
> I checked that if I have a hacking bundle (B) with the following code, it will be able to call a method (criticalAlert) on a private class (private.packages.Sample) of a bundle (A), i.e. a class which neither exported by A nor imported by B. In order to do that, B must acces the classloader of A, thus I suppose that B imports an exported package (public.packages) from A:
>
> 	public void start(BundleContext bc) throws Exception {	
> 		//Getting an instance of an exported class from A...
> 		public.packages.Sample sample = new public.packages.Sample();
> 	
> 		//Trying to access the private class from A...
> 		ClassLoader loader = sample.getClass().getClassLoader();
> 		Class clazz = loader.loadClass("private.packages.Sample");
> 		Object o = clazz.newInstance();
> 		Method m = o.getClass().getMethod("criticalAlert", new Class[]{});
> 		m.invoke(o,new Object[]{});
> 	}
>
> So, tell me if I am wrong, the OSGi solution to this bundle isolation issue is to use Java 2 permissions (with OSGi CPA or PA) to forbid the use of reflection (Class.newInstance and java.lang.reflect) to untrusted bundles. 
>
> There is also another threat (even more direct) in OSGi R4 : the use of Fragments. Are there other threats ?

Well, bundle B would need permission to do "getClassLoader" if security 
were enabled. Without security enabled, we have no way to prevent bundle 
B from accessing private classes from A, since A's class loader must be 
able to access them otherwise A would not be able to access them.

-> richard

Re: Code sharing and code isolation

Posted by Peter Kriens <Pe...@aQute.biz>.
That is a very wide question ...

I think you must reverse it, giving NO permissions to a bundle and
then adding permissions as needed. Dangerous things as having access
to the class loader or allowing Bundle.loadClass() are of course
dangerous.

However, isn't it better to create a special permission to call
createAlert()? It is very hard to analyze your code that no path can
enter criticalAlert from normal code calling. Having a specific
permission would solve that problem because all parties on the stack
would be checked. Sounds safer to me than hoping nobody can call your
code, and more in line with the Java security model.

Kind regards,

     Peter Kriens




BARMG>  
BARMG> For security purposes, I would like to prevent bundles from
BARMG> using classes that belong to private packages of any bundle.

BARMG> I checked that if I have a hacking bundle (B) with the
BARMG> following code, it will be able to call a method
BARMG> (criticalAlert) on a private class (private.packages.Sample) of
BARMG> a bundle (A), i.e. a class which neither exported by A nor
BARMG> imported by B. In order to do that, B must acces the
BARMG> classloader of A, thus I suppose that B imports an exported
BARMG> package (public.packages) from A:

BARMG>         public void start(BundleContext bc) throws Exception {  
BARMG>                 //Getting an instance of an exported class from A...
BARMG>                 public.packages.Sample sample = new public.packages.Sample();
BARMG>         
BARMG>                 //Trying to access the private class from A...
BARMG>                 ClassLoader loader =
BARMG> sample.getClass().getClassLoader();
BARMG>                 Class clazz =
BARMG> loader.loadClass("private.packages.Sample");
BARMG>                 Object o = clazz.newInstance();
BARMG>                 Method m =
BARMG> o.getClass().getMethod("criticalAlert", new Class[]{});
BARMG>                 m.invoke(o,new Object[]{});
BARMG>         }

BARMG> So, tell me if I am wrong, the OSGi solution to this bundle
BARMG> isolation issue is to use Java 2 permissions (with OSGi CPA or
BARMG> PA) to forbid the use of reflection (Class.newInstance and
BARMG> java.lang.reflect) to untrusted bundles. 

BARMG> There is also another threat (even more direct) in OSGi R4 :
BARMG> the use of Fragments. Are there other threats ?

BARMG> André


-- 
Peter Kriens                              Tel +33467542167
9C, Avenue St. Drézéry                    AOL,Yahoo: pkriens
34160 Beaulieu, France                    ICQ 255570717
Skype pkriens                             Fax +1 8153772599