Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does declaring a variable as "private" in C# protect the memory in windows from being accessed by a memory scanner?

My workmate always tells me that if we declare anything as "public" then it is dangerous because then any program can access that memory and that the solution is to use the "private" access modifier.

I am wondering if this is infact true.

like image 933
James Hulse Avatar asked Jul 21 '10 03:07

James Hulse


2 Answers

That is not, in fact, true.

Access modifiers are only there to help organize your code. They only protect it in the sense that you protect your glass from being knocked over by setting it out of reach of the cat.

like image 117
Matthew King Avatar answered Oct 05 '22 23:10

Matthew King


public and private access modifiers only have to do with the visibility of those structures (classes, method, or variables) to other classes within the same application. Memory protection between processes and users is enforced by the operating system. In the case of Windows, it does ensure that non-administrator level (and system ring) processes/threads do not have access to memory that is not explicitly shared (such as shared memory) with open permissions. Actually, Windows allows processes to grant very specific rights to specific areas of memory, but this is not provided in the language definition of C#. You would need to access system APIs to control grant that kinds of access to specific blocks of memory; by default all blocks of memory are protected by the OS.

Now, if the memory scanner is running in ring-0 or with specific elevated privileges there is nothing you can do in your process to block that access.

like image 23
Kevin Brock Avatar answered Oct 05 '22 23:10

Kevin Brock