Home All Groups Group Topic Archive Search About

ReflectionPermission weird behavior?

Author
15 Feb 2006 3:52 PM
shaharh
Hi,
I am trying to prevent reflection access to my dll.
the dll have one class name Class1:
public class Class1
{
    private int n;
    public int Value
    {
        get { return n; }
    }
}
I added an attribute to the AssemblyInfo.cs:
[assembly: ReflectionPermission(SecurityAction.RequestRefuse, Flags =
ReflectionPermissionFlag.AllFlags)]

but when I have a tester(other assembly an exe one) and trying to use
reflection on this dll no exception is  thrown:
public static void DoThat()
{
    Class1 p = new Class1();
    Console.WriteLine(p.Value);
    Type type = typeof(Class1);
    System.Reflection.FieldInfo field = type.GetField("n",
        System.Reflection.BindingFlags.Instance |
System.Reflection.BindingFlags.NonPublic);
    field.SetValue(p, 69);
    Console.WriteLine(p.Value);
}
am I missing something here?

I also tried to use ReflectionPermissionAttribute in the class level
and ReflectionPermission with Deny().
Something is wrong and I am sute that it is me.
Thanx

Author
15 Feb 2006 8:29 PM
Nicole Calinoiu
<shah***@gmail.com> wrote in message
news:1140018769.594732.37840@z14g2000cwz.googlegroups.com...
> Hi,
> I am trying to prevent reflection access to my dll.

Unfortunately, that's just not possible.


> I added an attribute to the AssemblyInfo.cs:
> [assembly: ReflectionPermission(SecurityAction.RequestRefuse, Flags =
> ReflectionPermissionFlag.AllFlags)]

This will only block your assembly from being able to perform reflection
actions.  It will not block other code from accessing your assembly via
reflection.


> am I missing something here?

Only that what you're trying to do simply isn't possible.  Do you have some
specific goal that you are attempting to accomplish via preventing
reflection into your code?  If so and you could explain what it is, it might
be possible for someone here to suggest an alternate approach.


> I also tried to use ReflectionPermissionAttribute in the class level
> and ReflectionPermission with Deny().

Unfortunately, denials are just as useless as RequestRefuse wrt attempting
to prevent reflection into your assembly.
Author
15 Feb 2006 9:03 PM
shaharh
I want to prevent other code that is use my dll to invoke private
members.
Show quoteHide quote
>From your response I see that it is not in my hands am I right?
Author
15 Feb 2006 11:02 PM
Henning Krause [MVP]
Hello,

in a full-trust environment, its not possible.

Greetings,
Henning Krause

<shah***@gmail.com> wrote in message
Show quoteHide quote
news:1140037431.064808.143970@o13g2000cwo.googlegroups.com...
>I want to prevent other code that is use my dll to invoke private
> members.
>>From your response I see that it is not in my hands am I right?
>
Author
16 Feb 2006 1:17 AM
Nicole Calinoiu
You cannot prevent fully trusted code from invoking your private members.
If you want to be absolutely sure that your private members cannot be
called, you'll need to keep your assembly off any machine where it may be
subject to execution by potentially malicious (at least from your point of
view) highly privileged code.  That said, there are some scenarios in which
one can reduce the potential problems caused by invocation of private
members.  For example, if one suspects that private fields may be
manipulated in unexpected ways, one might wish to revalidate them before
using them as opposed to only validating them via their setters.  In
addition, you may wish to protect some of your private methods with license
or other "right to call" verifications in a manner similar to what you might
do for public methods.  Obviously, approaches like these will have
performance consequences, and they can be bypassed by a sufficiently
determined and highly privileged attacker, so ymmv...



<shah***@gmail.com> wrote in message
Show quoteHide quote
news:1140037431.064808.143970@o13g2000cwo.googlegroups.com...
>I want to prevent other code that is use my dll to invoke private
> members.
>>From your response I see that it is not in my hands am I right?
>