Groups | Blog | Home
all groups > dotnet security > july 2004 >

dotnet security : SecurityException "Request Failed"


VJ
7/25/2004 7:35:07 PM
I have a locally installed (Full trust) assembly that is
using reflection to invoke methods on a type that is in a
partially trusted assembly and I get a security exception
(Trace at the end of the post).

The method that is making the call has Unrestricted
reflection permission asserted. Any idea why this fails.

I've tried asserting SecurityPermission+UnmanagedCode in
addition and that didn't help. The only thing that seems
to work PermissionSet(unrestricted). So, what permissions
is this call expecting? Any tools that can help me here?

Two more questions.

Does the invoked method have all the asserted permissions?
I need these permissions to invoke the method dynamically,
but I don't necessarily want the method to execute with
these privileges.

How do I assert a FileIOPermission that allows the right
to write to the Console? i.e How is the console
represented as a file name?

Any help is appreciated.

Thx,
VJ


System.Security.SecurityException: Request failed.
at System.Reflection.RuntimeMethodInfo.InternalInvoke
(Object obj, BindingFlag
s invokeAttr, Binder binder, Object[] parameters,
CultureInfo culture, Boolean i
sBinderDefault, Assembly caller, Boolean verifyAccess)
at System.Reflection.RuntimeMethodInfo.InternalInvoke
(Object obj, BindingFlag
s invokeAttr, Binder binder, Object[] parameters,
CultureInfo culture, Boolean v
erifyAccess)
at System.Reflection.RuntimeMethodInfo.Invoke(Object
obj, BindingFlags invoke
Attr, Binder binder, Object[] parameters, CultureInfo
culture)
at System.Reflection.MethodBase.Invoke(Object obj,
Nicole Calinoiu
7/26/2004 9:48:00 AM
[quoted text, click to view]

If the calling assembly has full trust, there should be no need to assert
reflection permissions. The most likely reason for the failure is that the
callee requires some permission that is not automatically granted to fully
trusted code. Did you author the callee code? What's the accessibility
level on the classes and methods you're attempting to call? Can you call
them successfully without using reflection?

VJ
7/26/2004 10:31:05 AM
The calling assembly does have full trust. But the call is
part of a stack frame that has the partially trusted
assembly higher up in the call sequence . In other words,
the method that is asserting the permissions is being
called by the partially trusted assembly, and this method
in turn is trying to invoke a method of another class in
the partially trusted assembly.

eg:
a() calls b() calls c()

Both a() and c() are in the partially trusted assembly.
b() is the method that is asserting the permissions.

The type and methods being called are all public.

VJ

[quoted text, click to view]
VJ
7/26/2004 3:09:53 PM
Yes, and yes :(

[quoted text, click to view]
Nicole Calinoiu
7/26/2004 3:50:40 PM
Do either or both of the following scenarios work?

1. Call c() from b() without using reflection.
2. Call c() from b() using reflection, but with c() being launched directly
(not via reflection) from other fully trusted code instead of from a().



[quoted text, click to view]

shawnfa NO[at]SPAM online.microsoft.com (
7/27/2004 12:27:04 AM
One possibility is that your partially trusted assembly is doing a LinkDemand, which will fail because instead of your code making the call into the
assembly, now Microsoft code is. The Microsoft reflection code won't have much of the same evidence as your code does, so this makes the
LinkDemand extremely likely to fail.

As for your other questions:
[quoted text, click to view]
Technically no, it doesn't, but it will behave as though it does. The stack walk will hit your assert, and the security demand will pass (assuming
there are no denies on the callstack first).

[quoted text, click to view]
Currently there are no permissions needed to write to the Console.

-Shawn
http://blogs.msdn.com/shawnfa

--

This posting is provided "AS IS" with no warranties, and confers no rights.
Note: For the benefit of the community-at-large, all responses to this message are best directed to the newsgroup/thread from which they
originated.
--------------------
[quoted text, click to view]

Nicole Calinoiu
7/27/2004 1:51:24 PM
Unfortunately, I can reproduce the problem just fine, and it definitely runs
counter to the available documentation on the permissions that are supposed
to be required for this sort of invocation via reflection. I'll try to play
with it a bit more to see if I can narrow down the problem a bit. However,
in case it's not possible to make the problem without unrestricted
permissions, perhaps it might be best if you could describe what exactly it
is you are trying to do. Maybe someone can suggest an alternative that
wouldn't require reflection at all...



[quoted text, click to view]

Nicole Calinoiu
7/27/2004 2:43:21 PM
Of course, I found one possible problem during my first bit of fiddling
after my last post... <g> Is your partially trusted assembly strongly
named? What I found was that the documented permissions for the reflection
methods are sufficient to permit the full chain to complete as expected
unless the assembly being invoked via reflection is strongly named, in which
case your original exception is seen.

If your partially trusted assembly is strongly named, the big remaining
question would be what the minimum permission set is for this apparently
undocumented case. While it's possible that unrestricted permissions are
necessary to invoke a strongly named assembly in this way, it's also
possible that only one or two additional permissions are required. However,
without documentation, this could mean a lot of trial and error...


[quoted text, click to view]

VJ
7/30/2004 10:24:57 AM
Thanks for the answers.

[quoted text, click to view]
is doing a LinkDemand, which will fail because instead of
your code making the call into the
[quoted text, click to view]
reflection code won't have much of the same evidence as
your code does, so this makes the
[quoted text, click to view]

I'm afraid not. The code in the PTA (Partially trusted
assembly) has no link demands.

[quoted text, click to view]
it does. The stack walk will hit your assert, and the
security demand will pass (assuming
[quoted text, click to view]

Interesting. Do you know this for a fact? This would be a
serious security risk. Between the point where the invoke
is called (which is Full trust code) and the point where
the invoked method in the PTA starts executing, everything
in the stack is MS dynamic invocation code. So, there is
no opportunity to deny anything. So, since I have to
assert (say even the minimum documented
ReflectionPermission, although it appears the only set
that works is full unrestricted permissions) to call the
method, the method i'm invoking will always have the
asserted permissions.


[quoted text, click to view]
Console.

Thanks. I'm not sure I am seeing this behavior, but I'll
double check.

[quoted text, click to view]
responses to this message are best directed to the
newsgroup/thread from which they
[quoted text, click to view]
VJ
7/30/2004 10:34:00 AM
Thanks for taking the time to reproduce the problem.

First, my Partially trusted assembly(PTA) is strong named.
I've tried making my best guesses as to what the
permissions needed are, but haven't been able to figure it
out. I can say it is definitely a CAS issue, as when I
assert full permissions, the call goes through.

W/o giving away too much ;) I have a library A that is
locally installed and is used by applications that may be
PTA in certain scenarios (application is downloaded etc).
In the library, I attempt to instantiate classes in the
PTA based on some criteria (I walk through the types in
the PTA and check for criteria) upon initialization.


[quoted text, click to view]
Nicole Calinoiu
8/1/2004 2:33:22 AM
Is there any reason you couldn't use delegates for this?


[quoted text, click to view]

Nicole Calinoiu
8/2/2004 9:41:36 AM
Something had been nagging at me about this, and I just got my memory jogged
this morning...

Your PTA is strongly named, which means that it needs the
AllowPartiallyTrustedCallersAttribute to be set in order to avoid FullTrust
link demands even though it is not fully trusted itself. Reflection turns
link demands into full demands, so the call is failing due to the PTA being
present on the stack. To avoid the problem, use
AllowPartiallyTrustedCallersAttribute to allow partially trusted callers
into your PTA.

HTH,
Nicole


[quoted text, click to view]

VJ
8/2/2004 1:25:55 PM
Cool! That totally makes sense. I will try it out
immediately and let you know! And thanks, thanks, thanks
again for taking the time! The help is totally appreciated.


[quoted text, click to view]
anonymous NO[at]SPAM discussions.microsoft.com
8/2/2004 1:29:32 PM
Scratch all that I said before :) To answer my own
question, there is no problem here. A security stack walk
fails on the first frame that does not have the
appropriate permission. So, All stack walks for demands
downstream from the dynamically invoked method will fail
unless the dynamically invoked method itself has the
appropriate permissions. The stack walk will never ever
reach as far as the Full trust assembly! So, it doesn't
matter that the FT assembly has asserted the permissions.

[quoted text, click to view]


VJ
8/2/2004 4:51:11 PM
It worked!

Thank you, O wise one :)


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