473,394 Members | 1,932 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Sandboxing an assembly - mysterious SecurityExceptions

Hi,

I'm trying to create an AppDomain and use it to load an assembly with
reduced permissions (e.g. the "Internet" named permission set).
Unfortunately, the permissions seem to be reduced *too* much, and I
can't figure out which individual permissions I need to add back to make
this code work. It works fine if I use FullTrust instead of Internet,
but obviously that isn't safe.

I get these security errors from code running in the sandbox even when I
don't actually load the untrusted assembly. The Loader.Go() method below
is a very simple test case that fails - the method doesn't execute *at
all* if the call to ToInt32() is present. The interface IHoldsNum is
defined in another assembly (Snack.Runtime) which is referenced from my
program. The error doesn't occur if I replace IHoldsNum with an
interface defined inside my program.

The SecurityException has no useful information on it at all, so I can't
tell which permission is missing. As you can see in my Main method, I'm
adding just about every permission to the permission set (for testing),
but it doesn't help.

Here is the error I get:

==============================

Unhandled Exception: System.Security.SecurityException: Security error.

Server stack trace:
at SnackR.Loader.Go(Byte[] assembly)
at
System.Runtime.Remoting.Messaging.StackBuilderSink .PrivateProcessMessage(M
ethodBase mb, Object[] args, Object server, Int32 methodPtr, Boolean
fExecuteInC
ontext, Object[]& outArgs)
at
System.Runtime.Remoting.Messaging.StackBuilderSink .SyncProcessMessage(IMes
sage msg, Int32 methodPtr, Boolean fExecuteInContext)

Exception rethrown at [0]:
at
System.Runtime.Remoting.Proxies.RealProxy.HandleRe turnMessage(IMessage req
Msg, IMessage retMsg)
at
System.Runtime.Remoting.Proxies.RealProxy.PrivateI nvoke(MessageData& msgDa
ta, Int32 type)
at SnackR.Loader.Go(Byte[] assembly) in
i:\working\c#\snack\SnackR\SnackR.cs:
line 19
at SnackR.SnackR.Main(String[] args) in
i:\working\c#\snack\SnackR\SnackR.cs:
line 114

==============================

And here is my code:

==============================

internal class Loader : MarshalByRefObject
{
public void Go(byte[] assembly)
{
// IHoldsNum is defined in another assembly:
// interface IHoldsNum {
// int ToInt32();
// }

object o = new object();
IHoldsNum intf = (IHoldsNum) o;

// THIS NEXT LINE CAUSES
int s = intf.ToInt32();
}
}

class SnackR
{
[STAThread]
static void Main(string[] args)
{
string filename = System.IO.Path.GetFullPath(args[0]);

// the app runs with Internet permissions
PolicyLevel sandboxPolicy = PolicyLevel.CreateAppDomainLevel();
AllMembershipCondition allCodeMC = new AllMembershipCondition();
PermissionSet permSet = sandboxPolicy.GetNamedPermissionSet(
"Internet");
permSet.AddPermission(
new SecurityPermission(PermissionState.Unrestricted));
permSet.AddPermission(
new EnvironmentPermission(PermissionState.Unrestricted ));
permSet.AddPermission(
new FileDialogPermission(PermissionState.Unrestricted) );
permSet.AddPermission(
new FileIOPermission(PermissionState.Unrestricted));
permSet.AddPermission(
new IsolatedStorageFilePermission(PermissionState.Unre stricted));
permSet.AddPermission(
new ReflectionPermission(PermissionState.Unrestricted) );
permSet.AddPermission(
new RegistryPermission(PermissionState.Unrestricted));
permSet.AddPermission(
new UIPermission(PermissionState.Unrestricted));
PolicyStatement internetPolStmt = new PolicyStatement(permSet);
CodeGroup allCodeInternetCG =
new UnionCodeGroup(allCodeMC, internetPolStmt);
sandboxPolicy.RootCodeGroup = allCodeInternetCG;

AppDomain sandbox = AppDomain.CreateDomain("SnackSandbox");
sandbox.SetAppDomainPolicy(sandboxPolicy);

// create a loader in the sandbox
Loader ldr = (Loader) sandbox.CreateInstanceFromAndUnwrap(
Assembly.GetExecutingAssembly().Location,
typeof(Loader).FullName);

// load the assembly as an array and pass it to the loader
byte[] asm;

try {
asm = new byte[0];
ldr.Go(asm);
} catch (SecurityException se) {
throw;
}
}
}

==============================

Jesse
Nov 16 '05 #1
1 2014
I wrote:
I get these security errors from code running in the sandbox even when I
don't actually load the untrusted assembly. The Loader.Go() method below
is a very simple test case that fails - the method doesn't execute *at
all* if the call to ToInt32() is present. The interface IHoldsNum is
defined in another assembly (Snack.Runtime) which is referenced from my
program. The error doesn't occur if I replace IHoldsNum with an
interface defined inside my program.


After a lot of frustration, I found the problem. The assembly that
contains IHoldsNum needs the AllowPartiallyTrustedCallers attribute.

Jesse
Nov 16 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Gary McGill | last post by:
I have a C# solution with a dozen or so projects. There are references between the projects, and these were all added as "Project" references. Everything's been working fine for months, but...
0
by: Achim Domma | last post by:
Hi, I try to develop a localized Asp.Net 1.1 application. I have added two resource files to my app, which are called 'Labels.resx' and 'Labels.de.resx'. If I compile the app, I see a 'de'...
0
by: james | last post by:
Hi everyone, I am trying to lock down a server to only give permissions to certain assemblies, based on their strong name. I missed a couple and see SecurityExceptions on startup. Would anyone...
5
by: =?Utf-8?B?Y3JhenlsZWdzZml0eg==?= | last post by:
Can anyone suggest the best place in WCF to catch SecurityExceptions arising from failed authorization (declarative model) or CAS exceptions. I was hoping that my IErrorHandler.HandleError...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.