473,386 Members | 1,801 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,386 software developers and data experts.

C# and const?

Is it possible to secure a reference parameter from modification using c#?

E.I. something like const would do in c++.

I've tried to do it by limiting the object to an interface with only get
functions, but that doesnt stop a malicious user from using reflection to
set it...Like:

---Example--------

public interface IFoo
{
int Param {get; }
}

public class Foo : IFoo
{
private int param;
public int Param
{
get { return param; }
set { param = value; }
}
}
public class UntrustedClass
{
public static void Bar(IFoo foo)
{
// Following line will generate a compile error. (Good)
// foo.Param = 15;
// However, this wont. (Bad). And it will execute correctly. (Really bad).
PropertyInfo[] ps = foo.GetType().GetProperties();
ps[0].SetValue(foo, 15, null);
}
}
---Example End--------

So..What are my options? Can I use attributes to stop reflection? Can I use
other security options? I plan on giving the untrusted code its own
AppDomain anyway, but can it stop reflection?

Nov 22 '05 #1
6 1553
Why is this a problem?

No offense, but unless you are protecting a password, you are worrying about
something that is pretty darn close to meaningless.

Sure... it's not secure.

Interfaces are there to encapsulate code, not protect it. Breaking
encapsulation isn't criminal... it's stupid.
Why protect yourself from stupid programmers?

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
"Jesper" <no@spam.com> wrote in message
news:uF**************@tk2msftngp13.phx.gbl...
Is it possible to secure a reference parameter from modification using c#?

E.I. something like const would do in c++.

I've tried to do it by limiting the object to an interface with only get
functions, but that doesnt stop a malicious user from using reflection to
set it...Like:

---Example--------

public interface IFoo
{
int Param {get; }
}

public class Foo : IFoo
{
private int param;
public int Param
{
get { return param; }
set { param = value; }
}
}
public class UntrustedClass
{
public static void Bar(IFoo foo)
{
// Following line will generate a compile error. (Good)
// foo.Param = 15;
// However, this wont. (Bad). And it will execute correctly. (Really bad).
PropertyInfo[] ps = foo.GetType().GetProperties();
ps[0].SetValue(foo, 15, null);
}
}
---Example End--------

So..What are my options? Can I use attributes to stop reflection? Can I
use other security options? I plan on giving the untrusted code its own
AppDomain anyway, but can it stop reflection?

Nov 22 '05 #2
"Nick Malik [Microsoft]"
Why is this a problem?

No offense, but unless you are protecting a password, you are worrying
about something that is pretty darn close to meaningless.

Sure... it's not secure.

Interfaces are there to encapsulate code, not protect it. Breaking
encapsulation isn't criminal... it's stupid.
Why protect yourself from stupid programmers?

Im not trying to protect myself from stupid programmers. I'm trying to
protect myself from *malicious* programmers, and in my case, it makes good
sense:

My host application is being written to accept user-created plugins. And I
can't trust those. I require that they support a specified interface, and as
mentioned I'm giving them their own AppDomain to run in. My problem is,
however, that I want to give them read access to alot of data in my host
process. Giving them an interface wrapping of my data was just mentioned
because thats the 'solution' I had seen others give to replace 'const'. I'll
take other solutions.

My last resort is to create a new class which only has read properties, and
copy all my data into this class before each call to the plugin. But for
performance reasons (its alot of data), and maintenance reasons
(syncronizing 2 classes) I 'd prefer if I could avoid it.
Nov 22 '05 #3
You could try using some security classes to mitigate the problems --
StrongNameIdentityPermission and ReflectionPermission.
"Jesper" <no@spam.com> wrote in message
news:ej**************@TK2MSFTNGP14.phx.gbl...
"Nick Malik [Microsoft]"
Why is this a problem?

No offense, but unless you are protecting a password, you are worrying
about something that is pretty darn close to meaningless.

Sure... it's not secure.

Interfaces are there to encapsulate code, not protect it. Breaking
encapsulation isn't criminal... it's stupid.
Why protect yourself from stupid programmers?

Im not trying to protect myself from stupid programmers. I'm trying to
protect myself from *malicious* programmers, and in my case, it makes good
sense:

My host application is being written to accept user-created plugins. And I
can't trust those. I require that they support a specified interface, and

as mentioned I'm giving them their own AppDomain to run in. My problem is,
however, that I want to give them read access to alot of data in my host
process. Giving them an interface wrapping of my data was just mentioned
because thats the 'solution' I had seen others give to replace 'const'. I'll take other solutions.

My last resort is to create a new class which only has read properties, and copy all my data into this class before each call to the plugin. But for
performance reasons (its alot of data), and maintenance reasons
(syncronizing 2 classes) I 'd prefer if I could avoid it.

Nov 22 '05 #4
Jesper wrote:
Im not trying to protect myself from stupid programmers. I'm trying to
protect myself from *malicious* programmers, and in my case, it makes good
sense:
But is very hard: The user can just run your program in their own
implementation of the .NET framework, or under a simulator and inspect
and change the data if they really want to.

If you don't worry about the alternative runtime-implementations, you
can use the interface solution you mention.

You can probably prevent the use of reflection by using the appropriate
access specifier: private, or internal on the IFoo implementation.
Although you will have to check yourself, I don't have that much
knowledge on .NET introspection and reflection.

The standard way would probably be to pass a proxy which protect access:

private class ConstFoo: IFoo {
private IFoo real;
public ConstFoo(IFoo real) { this.real = real; }
public int Param {
get { return real.Param; }
}
}
Untrusted.Bar(new ConstFoo(foo));

But you still rely on .NET enforcing access to "real" through reflection
here.

Anyway defending against malicious plugin programmers isn't likely to be
the area of development where you get the most value for your
development-time.
process. Giving them an interface wrapping of my data was just mentioned
because thats the 'solution' I had seen others give to replace 'const'. I'll
take other solutions.
It works moderately fine, probably good for most things.
My last resort is to create a new class which only has read properties, and
copy all my data into this class before each call to the plugin. But for
performance reasons (its alot of data), and maintenance reasons
(syncronizing 2 classes) I 'd prefer if I could avoid it.


1. don't try to defend against maliciousness
2. don't copy, proxy.
3. ???
4. PROFIT!

--
Helge Jensen
mailto:he**********@slog.dk
sip:he**********@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-
Nov 22 '05 #5
// no need to go through reflection to break it. The following line will
also break it:

((Foo)foo).Param = 15;

If you want to prevent it, you have to restrict the accessibility on Foo
(make it internal or private), or on the set accessor for Param (you need
VS2005 for that because VS2003 won't let you set different accessibility
levels on set and get).

Bruno.

"Jesper" <no@spam.com> wrote in message
news:uF**************@tk2msftngp13.phx.gbl...
Is it possible to secure a reference parameter from modification using c#?

E.I. something like const would do in c++.

I've tried to do it by limiting the object to an interface with only get
functions, but that doesnt stop a malicious user from using reflection to
set it...Like:

---Example--------

public interface IFoo
{
int Param {get; }
}

public class Foo : IFoo
{
private int param;
public int Param
{
get { return param; }
set { param = value; }
}
}
public class UntrustedClass
{
public static void Bar(IFoo foo)
{
// Following line will generate a compile error. (Good)
// foo.Param = 15;
// However, this wont. (Bad). And it will execute correctly. (Really bad).
PropertyInfo[] ps = foo.GetType().GetProperties();
ps[0].SetValue(foo, 15, null);
}
}
---Example End--------

So..What are my options? Can I use attributes to stop reflection? Can I
use other security options? I plan on giving the untrusted code its own
AppDomain anyway, but can it stop reflection?

Nov 22 '05 #6
Hi Jesper,

"Jesper" <no@spam.com> wrote in message
news:ej**************@TK2MSFTNGP14.phx.gbl...
"Nick Malik [Microsoft]"
Interfaces are there to encapsulate code, not protect it. Breaking
encapsulation isn't criminal... it's stupid.
Why protect yourself from stupid programmers?

Im not trying to protect myself from stupid programmers. I'm trying to
protect myself from *malicious* programmers, and in my case, it makes good
sense:

My host application is being written to accept user-created plugins. And I
can't trust those. I require that they support a specified interface, and
as mentioned I'm giving them their own AppDomain to run in. My problem is,
however, that I want to give them read access to alot of data in my host
process. Giving them an interface wrapping of my data was just mentioned
because thats the 'solution' I had seen others give to replace 'const'.
I'll take other solutions.


Perhaps I just need to understand a little better.

When I approach security concerns, I use a technique called "Threat
Modeling". In this technique, I first identify the things that need
protection, and the I identify the different approaches to "harming" these
things, and then I characterize an attack that will effect that harm. In
other words, it a technique to make programmers think like hackers. :-)

So, let me understand the threat that reflection poses for you.

You have an object that you use for returning and updating a sizable set of
data. You want to share this object with plug-ins written by users. You
said something about synchronization, so I assume you want to allow these
users, in some cases, to update the data... but not all of it. Some of the
data should not be updatable. (I'm guessing here... please correct me).

The threat, if I understand correctly, is that your application has the
ability to modify a great deal of data. You want to share the data but not
the ability to modify it. The threat scenario would then be: malicious
programmer writes plug-in to access your objects, and uses reflection to get
access to data items that you don't want him or her to change, changes those
data items, and your class will react by writing that data back to the
server.

This leads to a few questions. I can see a couple of different designs, but
I don't know which to recommend without further info...

How many of the fields in your data structure do you want the compliant
(non-malicious) user to be able to change?
Is the data structured in a hierarchy of objects, or is this a single object
with many fields?
Does your application have a data access layer that is composed of objects
using non-static methods?

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
Nov 22 '05 #7

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

Similar topics

8
by: Sergey Tolstov | last post by:
Hello, I am working with Visual C++ 6.0 compiler. In the following declaration: int const A = 10, B = 10; both A and B are const. However, in declaration
20
by: Corno | last post by:
Hi all, There's probably a good reason why a const object can call non const functions of the objects where it's member pointers point to. I just don't see it. For me, that makes the the const...
6
by: Virendra Verma | last post by:
This sounds weird, but I am looking for separate behaviors for destruction of a const and non-const object. I am trying to develop a smart/auto pointer class for writing objects to disk...
7
by: johny smith | last post by:
Can someone please explain to me the difference between these two: function1( const int a) function2( int const a) Both seemed to compile, but what is the difference between the two above....
3
by: Steven T. Hatton | last post by:
Sorry about the big code dump. I tried to get it down to the minimum required to demonstrate the problem. Although this is all done with GNU, I believe the problem I'm having may be more general. ...
15
by: Dave | last post by:
Hello NG, It is well known that memory-allocating definitions should not be put in a header file. I believe, however, that this does not apply to const definitions. For example: #ifndef...
4
by: chrisstankevitz | last post by:
This code does not compile on gcc 3.4.4. Should it? Thanks for your help, Chris //================ #include <set> int main()
10
by: d3x0xr | last post by:
---- Section 1 ---- ------ x.c int main( void ) { char **a; char const *const *b; b = a; // line(9)
0
by: d3x0xr | last post by:
Heh, spelled out in black and white even :) Const is useles... do NOT follow the path of considering any data consatant, because in time, you will have references to it that C does not handle,...
4
by: grizggg | last post by:
I have searched and not found an answer to this question. I ran upon the following statement in a *.cpp file in a member function: static const char * const pacz_HTMLContentTypeHeader =...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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...

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.