473,405 Members | 2,310 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,405 software developers and data experts.

Asserting the permission to execute unmanaged code

Hi,
What is the "declaration" (class) I need to assert the permission that
my code can run unmanaged code.

I have:
================
[System.Runtime.InteropServices.DllImport("KERNEL32 ")]

private static extern bool QueryPerformanceCounter( ref long

lpPerformanceCount);

[System.Runtime.InteropServices.DllImport("KERNEL32 ")]

private static extern bool QueryPerformanceFrequency( ref long

lpFrequency);

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

and before calling QueryPerformanceCounter, I want to Assert() the
permission, but I completely miss the syntax, having not even a real clue
about the class to use (for illustration, SecurityAction, but probably
something else):

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

static Timings()

{
// Initialize the frequency

CodeAccessPermission perm = new SecurityAction??? ; // <<< HERE

perm.Assert();

dummy=QueryPerformanceFrequency( ref frequency);

dummy=QueryPerformanceCounter(ref fpsStartingCounter);

perm.RevertAssert();

....

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

Thanks in advance,

Vanderghast, Access MVP


Nov 16 '05 #1
10 2518
Michel,

You want to create an instance of the SecurityPermission class, passing
in the UnmanagedCode value from the SecurityPermissionFlag enumeration.
From there, you can call Assert.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Michel Walsh" <vanderghast@VirusAreFunnierThanSpam> wrote in message
news:u9**************@TK2MSFTNGP14.phx.gbl...
Hi,
What is the "declaration" (class) I need to assert the permission that
my code can run unmanaged code.

I have:
================
[System.Runtime.InteropServices.DllImport("KERNEL32 ")]

private static extern bool QueryPerformanceCounter( ref long

lpPerformanceCount);

[System.Runtime.InteropServices.DllImport("KERNEL32 ")]

private static extern bool QueryPerformanceFrequency( ref long

lpFrequency);

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

and before calling QueryPerformanceCounter, I want to Assert() the
permission, but I completely miss the syntax, having not even a real clue
about the class to use (for illustration, SecurityAction, but probably
something else):

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

static Timings()

{
// Initialize the frequency

CodeAccessPermission perm = new SecurityAction??? ; // <<< HERE

perm.Assert();

dummy=QueryPerformanceFrequency( ref frequency);

dummy=QueryPerformanceCounter(ref fpsStartingCounter);

perm.RevertAssert();

...

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

Thanks in advance,

Vanderghast, Access MVP

Nov 16 '05 #2
This should do the trick (in a slightly safer form <g>):

IStackWalk perm = new SecurityPermission
SecurityPermissionFlag.UnmanagedCode);
perm.Assert();

try
{
dummy = QueryPerformanceFrequency(ref frequency);
dummy = QueryPerformanceCounter(ref fpsStartingCounter);
}
finally
{
CodeAccessPermission.RevertAll();
}
"Michel Walsh" <vanderghast@VirusAreFunnierThanSpam> wrote in message
news:u9**************@TK2MSFTNGP14.phx.gbl...
Hi,
What is the "declaration" (class) I need to assert the permission that
my code can run unmanaged code.

I have:
================
[System.Runtime.InteropServices.DllImport("KERNEL32 ")]

private static extern bool QueryPerformanceCounter( ref long

lpPerformanceCount);

[System.Runtime.InteropServices.DllImport("KERNEL32 ")]

private static extern bool QueryPerformanceFrequency( ref long

lpFrequency);

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

and before calling QueryPerformanceCounter, I want to Assert() the
permission, but I completely miss the syntax, having not even a real clue
about the class to use (for illustration, SecurityAction, but probably
something else):

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

static Timings()

{
// Initialize the frequency

CodeAccessPermission perm = new SecurityAction??? ; // <<< HERE

perm.Assert();

dummy=QueryPerformanceFrequency( ref frequency);

dummy=QueryPerformanceCounter(ref fpsStartingCounter);

perm.RevertAssert();

...

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

Thanks in advance,

Vanderghast, Access MVP

Nov 16 '05 #3
Sorry, weird copy-paste artifact. That should have been:

IStackWalk perm = new
SecurityPermission(SecurityPermissionFlag.Unmanage dCode);
perm.Assert();
....
"Michel Walsh" <vanderghast@VirusAreFunnierThanSpam> wrote in message
news:u9**************@TK2MSFTNGP14.phx.gbl...
Hi,
What is the "declaration" (class) I need to assert the permission that
my code can run unmanaged code.

I have:
================
[System.Runtime.InteropServices.DllImport("KERNEL32 ")]

private static extern bool QueryPerformanceCounter( ref long

lpPerformanceCount);

[System.Runtime.InteropServices.DllImport("KERNEL32 ")]

private static extern bool QueryPerformanceFrequency( ref long

lpFrequency);

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

and before calling QueryPerformanceCounter, I want to Assert() the
permission, but I completely miss the syntax, having not even a real clue
about the class to use (for illustration, SecurityAction, but probably
something else):

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

static Timings()

{
// Initialize the frequency

CodeAccessPermission perm = new SecurityAction??? ; // <<< HERE

perm.Assert();

dummy=QueryPerformanceFrequency( ref frequency);

dummy=QueryPerformanceCounter(ref fpsStartingCounter);

perm.RevertAssert();

...

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

Thanks in advance,

Vanderghast, Access MVP

Nov 16 '05 #4
Hi Nicole,
That is here that you hide yourself in these days... :-)

When I compared with FileIOPermission, which takes two arguments,
the first argument allowing to Append permissions as example, the fact that
SecurityPermission accepts just one argument, not two, throw me back, but
now that I think about it, that makes sense too.
Is there a relatively good reference on similar security aspects,
with C#; I already have "Professional C#" (Wrox, first edition), and, sure,
the help file.

Thanks again, and to Nicholas too.

Vanderghast, Access MVP

"Nicole Calinoiu" <calinoiu REMOVETHIS AT gmail DOT com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
This should do the trick (in a slightly safer form <g>):

IStackWalk perm = new SecurityPermission
SecurityPermissionFlag.UnmanagedCode);
perm.Assert();

try
{
dummy = QueryPerformanceFrequency(ref frequency);
dummy = QueryPerformanceCounter(ref fpsStartingCounter);
}
finally
{
CodeAccessPermission.RevertAll();
}
"Michel Walsh" <vanderghast@VirusAreFunnierThanSpam> wrote in message
news:u9**************@TK2MSFTNGP14.phx.gbl...
Hi,
What is the "declaration" (class) I need to assert the permission that
my code can run unmanaged code.

I have:
================
[System.Runtime.InteropServices.DllImport("KERNEL32 ")]

private static extern bool QueryPerformanceCounter( ref long

lpPerformanceCount);

[System.Runtime.InteropServices.DllImport("KERNEL32 ")]

private static extern bool QueryPerformanceFrequency( ref long

lpFrequency);

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

and before calling QueryPerformanceCounter, I want to Assert() the
permission, but I completely miss the syntax, having not even a real clue
about the class to use (for illustration, SecurityAction, but probably
something else):

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

static Timings()

{
// Initialize the frequency

CodeAccessPermission perm = new SecurityAction??? ; // <<< HERE

perm.Assert();

dummy=QueryPerformanceFrequency( ref frequency);

dummy=QueryPerformanceCounter(ref fpsStartingCounter);

perm.RevertAssert();

...

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

Thanks in advance,

Vanderghast, Access MVP


Nov 16 '05 #5
You could also put the two calls into their own internal class and apply the

[SuppressUnmanagedCodeSecurity]

attribute to the class. This will stop the stack walk from starting in the first place for these two interop calls.

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

This should do the trick (in a slightly safer form <g>):

IStackWalk perm = new SecurityPermission
SecurityPermissionFlag.UnmanagedCode);
perm.Assert();

try
{
dummy = QueryPerformanceFrequency(ref frequency);
dummy = QueryPerformanceCounter(ref fpsStartingCounter);
}
finally
{
CodeAccessPermission.RevertAll();
}

Nov 16 '05 #6
Hi,
That is exactly what does one of my reference, and I was wondering what
that code was really implying. Definitively, that is preferable in this
case, where high precision timing is expected.

Thanks,
Vanderghast, Access MVP

"Richard Blewett [DevelopMentor]" <ri******@develop.com> wrote in message
news:eB*************@TK2MSFTNGP10.phx.gbl...
You could also put the two calls into their own internal class and apply
the

[SuppressUnmanagedCodeSecurity]

attribute to the class. This will stop the stack walk from starting in the
first place for these two interop calls.

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

This should do the trick (in a slightly safer form <g>):

IStackWalk perm = new SecurityPermission
SecurityPermissionFlag.UnmanagedCode);
perm.Assert();

try
{
dummy = QueryPerformanceFrequency(ref frequency);
dummy = QueryPerformanceCounter(ref fpsStartingCounter);
}
finally
{
CodeAccessPermission.RevertAll();
}

Nov 16 '05 #7
"Michel Walsh" <vanderghast@VirusAreFunnierThanSpam> wrote in message
news:Os**************@TK2MSFTNGP11.phx.gbl...
Hi Nicole,
That is here that you hide yourself in these days... :-)
Yeah, it's been quite some time since I used that "other" product for
anything other than support of old apps that just won't die... <g>

When I compared with FileIOPermission, which takes two arguments,
the first argument allowing to Append permissions as example, the fact
that SecurityPermission accepts just one argument, not two, throw me back,
but now that I think about it, that makes sense too.
Is there a relatively good reference on similar security aspects,
with C#; I already have "Professional C#" (Wrox, first edition), and,
sure, the help file.
Last time I went book shopping, "Programming .NET Security" (Freeman and
Jones, from O'Reilly) was the best I found. That was about a year ago, so
there might be something better out by now. Also, I never actually got
around to the reading the thing, so I can't give a real recommendation. If
you would like to borrow it for a bit (I _do_ intend to read it eventually
<g>), just give a private shout...


Thanks again, and to Nicholas too.

Vanderghast, Access MVP

"Nicole Calinoiu" <calinoiu REMOVETHIS AT gmail DOT com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
This should do the trick (in a slightly safer form <g>):

IStackWalk perm = new SecurityPermission
SecurityPermissionFlag.UnmanagedCode);
perm.Assert();

try
{
dummy = QueryPerformanceFrequency(ref frequency);
dummy = QueryPerformanceCounter(ref fpsStartingCounter);
}
finally
{
CodeAccessPermission.RevertAll();
}
"Michel Walsh" <vanderghast@VirusAreFunnierThanSpam> wrote in message
news:u9**************@TK2MSFTNGP14.phx.gbl...
Hi,
What is the "declaration" (class) I need to assert the permission
that my code can run unmanaged code.

I have:
================
[System.Runtime.InteropServices.DllImport("KERNEL32 ")]

private static extern bool QueryPerformanceCounter( ref long

lpPerformanceCount);

[System.Runtime.InteropServices.DllImport("KERNEL32 ")]

private static extern bool QueryPerformanceFrequency( ref long

lpFrequency);

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

and before calling QueryPerformanceCounter, I want to Assert() the
permission, but I completely miss the syntax, having not even a real
clue about the class to use (for illustration, SecurityAction, but
probably something else):

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

static Timings()

{
// Initialize the frequency

CodeAccessPermission perm = new SecurityAction??? ; // <<< HERE

perm.Assert();

dummy=QueryPerformanceFrequency( ref frequency);

dummy=QueryPerformanceCounter(ref fpsStartingCounter);

perm.RevertAssert();

...

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

Thanks in advance,

Vanderghast, Access MVP



Nov 16 '05 #8
It would probably be a good idea to add a level of abstraction to that
arrangement since it has a rather high likelihood of becoming less secure
with subsequent modification. A somewhat safer appropach would be to make
the interop calls private to the new internal class, with internal method
wrappers exposed for calling the private methods. This would at least
ensure that a checkpoint exists for screening all callers to the unmanaged
code, should this ever become necessary or desirable (i.e.: if security
concerns are ever found to outweigh performance concerns).
"Richard Blewett [DevelopMentor]" <ri******@develop.com> wrote in message
news:eB*************@TK2MSFTNGP10.phx.gbl...
You could also put the two calls into their own internal class and apply
the

[SuppressUnmanagedCodeSecurity]

attribute to the class. This will stop the stack walk from starting in the
first place for these two interop calls.

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

This should do the trick (in a slightly safer form <g>):

IStackWalk perm = new SecurityPermission
SecurityPermissionFlag.UnmanagedCode);
perm.Assert();

try
{
dummy = QueryPerformanceFrequency(ref frequency);
dummy = QueryPerformanceCounter(ref fpsStartingCounter);
}
finally
{
CodeAccessPermission.RevertAll();
}

Nov 16 '05 #9
It depends on the size of the library that is performing this of course. On the basis that this class is internal if the library is not large then, as you in control of the code, then changing te level of abstraction is not necessarily a huge deal. Adding a level of abstraction adds complexity to the library creator (not the consumer who is insulated as this class is internal) which may or may not be justified in the the component in question.

I wouldn't at all suggest adding that attribute to a public class.

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

It would probably be a good idea to add a level of abstraction to that
arrangement since it has a rather high likelihood of becoming less secure
with subsequent modification. A somewhat safer appropach would be to make
the interop calls private to the new internal class, with internal method
wrappers exposed for calling the private methods. This would at least
ensure that a checkpoint exists for screening all callers to the unmanaged
code, should this ever become necessary or desirable (i.e.: if security
concerns are ever found to outweigh performance concerns).


Nov 16 '05 #10
At the time that the p/invoke methods are moved into a separate class marked
with SuppressUnmanagedCodeSecurityAttribute, adding the wrappers would be a
trivial operation since the methods were previously private to their source
class, and every single call to the methods in that source class needs to be
changed to point at the new class anyway. This holds true regardless of the
the size of the library.

The reason that I suggested adding the wrappers wasn't merely for typical
abstraction reasons. Use of SuppressUnmanagedCodeSecurityAttribute is risky
and requires some minimal precautions (or perhaps even the "extreme care"
recommended by the documentation on the attribute <g>). These include
precautions against one's own possible future mistakes, the pool of which
immediately grows when the visibility of the methods is increased.


"Richard Blewett [DevelopMentor]" <ri******@develop.com> wrote in message
news:eP**************@TK2MSFTNGP12.phx.gbl...
It depends on the size of the library that is performing this of course.
On the basis that this class is internal if the library is not large then,
as you in control of the code, then changing te level of abstraction is
not necessarily a huge deal. Adding a level of abstraction adds complexity
to the library creator (not the consumer who is insulated as this class is
internal) which may or may not be justified in the the component in
question.

I wouldn't at all suggest adding that attribute to a public class.

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

It would probably be a good idea to add a level of abstraction to that
arrangement since it has a rather high likelihood of becoming less secure
with subsequent modification. A somewhat safer appropach would be to make
the interop calls private to the new internal class, with internal method
wrappers exposed for calling the private methods. This would at least
ensure that a checkpoint exists for screening all callers to the unmanaged
code, should this ever become necessary or desirable (i.e.: if security
concerns are ever found to outweigh performance concerns).

Nov 16 '05 #11

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

Similar topics

12
by: Russ | last post by:
Hello. My new dev machine is running XP Pro. In the past all equipment has only used Windows 2000. I have had a lot of problems getting my projects up and running on the new machine. The current...
1
by: Mohan | last post by:
Hi, I have developed a .NET Windows application. I able to run(Execute) it from the local drive of my PC. If I try to execute it from the SourceControl(Clear case) or from Shared drives, then it...
8
by: Jiggaz | last post by:
Hi, In my ASPX Page, i have a form for signup. And whene user click on the button, the event Button1_Click must use a stored procedure. But instead of use stored proc, i get this exception :...
7
by: Mike L. | last post by:
Hi, I got this 'EXECUTE permission denied on object <mySproc>' error message everytime I try executing my SQL server Sproc. What's this and how to fix this err? many thnaks in advance, mike
3
by: Marius Groenendijk | last post by:
Hi group, I want my app to show a msg if my it doesn't have the required permission(s), however this simply doesn't work. What am I overlooking/doing wrong?? Try Dim x As New...
0
by: Mamatha | last post by:
Hi i hava wrote the code to restore a file of SQL server database through ASP.It can access the backup file on server and restored but some times or in some systems it gives error like permission...
8
by: mike2036 | last post by:
I have an application (that has unmanaged code) and when I launch it without 'FullTrust' permissions (LocalIntranet_Zone), it crashes. When I set 'FullTrust' permissions, it launches fine. Is...
4
by: Richard MSL | last post by:
I have a simple application in C++ that uses a dll I wrote in C#. It works fine when I run it on C: on my local PC, but when I attempt to run it on the i: drive on my LAN, it gets a loading...
1
by: Michael Tissington | last post by:
How can I enable execute permissions for a folder on my website using the web.config file ?
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.