Groups | Blog | Home
all groups > dotnet security > march 2006 >

dotnet security : SecurityPermission problem


Itay Sandbank
3/29/2006 6:26:02 AM
Hi.

I'm trying to understand how to use CAS, and found something strange. I'm
trying to deny my program of a few permissions to see what happens. I created
a small program that creates the file c:\hello.txt and exits:

[assembly: FileIOPermission(SecurityAction.RequestRefuse,
ViewAndModify="c:\\")]
namespace CodeAccessSecurity
{
class Program
{
static void Main(string[] args)
{
FileIOPermission fip = new
FileIOPermission(FileIOPermissionAccess.AllAccess, "c:\\hello.txt");
fip.Demand();
FileStream fw = new FileStream("c:\\hello.txt", FileMode.Create);
}
}
}

When I run it, I see a SecurityException thrown, as can be expected.
However, it is thrown when I create the FileStream and not when I Demand the
FileIOPermission.

When running from the local intranet zone (I changed the debugger's
security settings), the exception is thrown on the Demand - as I expected in
the first place.

What's going on here?

Thanks,
Itay Sandbank
3/30/2006 8:11:02 AM
I get it, thanks.

This is confusing behavior - having Demand check ALMOST everything. Is
there a reason for it or is it a bug?

Itay.

[quoted text, click to view]
Nicole Calinoiu
3/30/2006 10:03:42 AM
The Demand method skips the call stack frame for the method from which it is
called. In order to have your assembly included in the stack walk initiated
by Demand, you'll need to move it into a separate method since the Main
method has no within-assembly callers. e.g.:

static void Main(string[] args)
{
DemandFileIOPermission();
FileStream fw = new FileStream("c:\\hello.txt", FileMode.Create);
}

private static void DemandFileIOPermission()
{
FileIOPermission fip = new
FileIOPermission(FileIOPermissionAccess.AllAccess, "c:\\hello.txt");
fip.Demand();
}


[quoted text, click to view]
Nicole Calinoiu
3/30/2006 12:29:28 PM
This is by design (see the remarks section at
http://msdn.microsoft.com/library/en-us/cpref/html/frlrfsystemsecuritycodeaccesspermissionclassdemandtopic.asp
for details).

Demands are intended to be made by code defining resources that require
protection. Their purpose is to determine whether code attempting to use a
resource possess the necessary permission(s), not whether the code defining
the resource has those same permissions. For example, the FileStream code
that actually accesses a file on disk makes a FileIOPermission demand. It
defines the resource, so it makes the demand. Since it's calling into
unmanaged code, it gets subjected to a different demand. However, there's
no point in asking it to fulfill the FileIOPermission demand that it invokes
since it can obviously bypass that same demand simply by not making it in
the first place.


[quoted text, click to view]
AddThis Social Bookmark Button