Home » C# Development

Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

20. June 2009 by jtstroup 13 Comments

 

I received a very interesting bug that’s been plaguing our product for a long time now. The problem is that intermittently our WinForm client users would view an image using the WebBrowser Control that would cause the program to crash. Listing this error:

 

Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

 

This problem happen when our users attempted to use a SVG Viewer or Tiff Viewer ActiveX object. (Which worked perfectly in IE outside of our app) and this did not occur on every machine. Some machines would work, others would not. We were finally able to narrow it down to those using MS Vista’s 32 bit operating system.

 

After loads of research I was able to narrow it down to Vistas Data Execution Prevention (DEP).

 

Snippet from Wikipedia about this:

 

Data Execution Prevention (DEP) is a security feature that is intended to prevent an application or service from executing code from a non-executable memory region. This helps prevent certain exploits that store code via a buffer overflow, for example. DEP runs in two modes: hardware-enforced DEP for CPUs that can mark memory pages as nonexecutable, and software-enforced DEP with a limited prevention for CPUs that do not have hardware support. Software-enforced DEP does not protect from execution of code in data pages, but instead from another type of attack (SEH overwrite).

DEP was introduced in Windows XP Service Pack 2 and is included in Windows XP Tablet PC Edition 2005, Windows Server 2003Service Pack 1 and later,[1] Windows Vista, and Windows Server 2008.

 

Software configuration

 

So how does one configure DEP. There are multiple ways of configuring this. Boot.ini (XP and VISTA) ,command line and using Microsoft’s ACT 5.0. By far the easiest method is to use the command line.

 

1)      Find your Command prompt and right click and select “run as administrator

2)      Then type “bcdedit.exe /set {current} nx XXXXXXX”

a.       Where XXXXXXX can be the following:

OptIn ( 2 ): This setting is the default configuration for Windows XP. On systems with processors that can implement hardware-enforced DEP, DEP is enabled by default for limited system binaries and programs that "opt-in." With this option, only Windows system binaries are covered by DEP by default.

OptOut( 3 ): This setting is the default configuration for Windows 2003 SP1. DEP is enabled by default for all processes. A list of specific programs that should not have DEP applied can be entered using the System dialog box in Control Panel. Network administrators can use the Application Compatibility Toolkit to "opt-out" one or more programs from DEP protection. System compatibility fixes, or shims, for DEP do take effect. Also note that Windows silently disables DEP for certain executables, such as those packaged with ASPack. [5]

AlwaysOn( 1 ): This setting provides full DEP coverage for the whole system. All processes always run with DEP applied. The exceptions list to exempt specific programs from DEP protection is not available. System compatibility fixes for DEP do not take effect. Programs that have been opted-out by using the Application Compatibility Toolkit run with DEP applied.

AlwaysOff( 0 ): This setting does not provide any DEP coverage for any part of the system, regardless of hardware DEP support. (except in Windows Vista Ultimate)

 

So your commandline might look something like this:

“ bcdedit.exe /set {current} nx AlwaysOff

Once you are done with this you will need to restart your computer.

 

To check the status of your DEP Policy you can run this command:

 

wmic os get dataexecutionprevention_supportpolicy” This will return a number value.  See values above to see what numbers mean.

 

Now this is how we defined what the problem was. But turning off DEP entirely isn’t a viable solution since that will expose your computer to many evil virus and hacking code.

 

Vista provides an interface to select specific programs to be marked as DEP non compliance. You can get to it by going to:

 

1)      Start

2)      Right Click “My Computer” à Properties

3)      Advanced System Settings

4)      Advanced Tab

5)      Under Performance select “Settings

6)      Select the tab “Data Execution Prevention

 

Here is where you are able to add DEP for all programs and services except those I select. This will set your DEP Policy to 3 (OptOut). You can then select the exe programs that enable you to mark that program as DEP noncompliant.

 

Well that might work for other people, but it didn’t work for me on my machine.  Everytime I tried to select my program’s exe, I got the message “This program must run with data execution protection(DEP) enabled. You cannot turn off DEP for this program.

 

Later I found a website that lists a NXCOMPAT and the C# compiler.

Turns out that you can add this switch to your postbuild event of your project and it will mark your compiled project as DEP non compliant.

 

1) Right click on project in Visual Studio

2) Properties

3) Build Events

4) Edit Post Build ...

 

REM Mark project as DEP Noncompliant

call "$(DevEnvDir)..\..\VC\bin\vcvars32.bat"

call "$(DevEnvDir)..\..\VC\bin\editbin.exe" /NXCOMPAT:NO "$(TargetPath)"

 

Another way to do this is to open up the Visual Studio command prompt, browse to your exe location and type:

editbin.exe /NXCOMPAT:NO YourProgram.exe

 

That’s it, now our product will compile and it fixes many crash log errors we were receiving previously.

 

Comments

Maria Smart
United States Maria Smart said:

Thank You so much. this solved my problem. I really thought it was unsolvable.
Thank You.

Crazy Images Comment
United States Crazy Images Comment said:

finally i find something that i want to know..
thanks for this usefull informations..

Hussain
Saudi Arabia Hussain said:

What a post!
I spent two days trying to fix this error without luck!
Thanks to you now it works!
by the way I used the command:
editbin.exe /NXCOMPAT:NO YourProgram.exe

bo ra
United States bo ra said:

thanks so much!

agnes
Indonesia agnes said:

Can anyone tells me how to run cmd in visual studio?
I just tried to type editbin.exe/NXCOMPAT:NO DealerSoftware.exe in the arguments, and it doesnt work out...
Please help me
Thanks

Kzee
Kzee said:

Thanks very much for this detailed article.

Andrew Baxter
Andrew Baxter said:

I admire the valuable information you offer in your articles. I will bookmark your blog and have my children check up here often. I am quite sure they will learn lots of new stuff here than anybody else!

Jesusa Oviatt
Jesusa Oviatt said:

Cool, there is actually some good points on here some of my readers will maybe find this relevant, will send a link, many thanks.

Verkaufsdisplay
Verkaufsdisplay said:

Very good post. I've found your site via Bing and I'm really glad about the information you provide in your posts. Btw your sites layout is really broken on the Chrome browser. Would be great if you could fix that. Anyhow keep up the good work!

Verkaufsdisplay
Verkaufsdisplay said:

Very informative article. I've found your blog via Yahoo and I'm really happy  about the information you provide in your posts. Btw your blogs layout is really messed up on the Kmelon browser. Would be really great if you could fix that. Anyhow keep up the good work!

Računalništvo
Računalništvo said:

I have been reading the articles on this blog for the first time. This is my first comment. Your blog has been very useful for me and it provides very good content.

Daniel Millions
Daniel Millions said:

Have you thought about adding some sort of bookmarking buttons or links on website?

dail
dail said:

Hi HI  .NET Battlebook | Attempted to read or write protected memory. This is often an indication that other memory is corrupt. First time jumped here on your site, founde on Yahoo. That is my first impression before I comment on the content. I am delighted to find your wonderful website online. I look for manuals in pdf. I found shop withowners manual by look for free user manusl. Do you now any free website with pdf's? dail Send me news to r2rtakeover@gmail.com

Add comment




biuquote
  • Comment
  • Preview
Loading