简体   繁体   中英

Does Windows 10 protect you from accessing memory that another program is using?

The following C++ code works:

int *p = new int;
p[1000] = 12;

Meaning I access a memory location that is sizeof (int) * 1000 bytes away from p.

What I was thinking is that maybe Windows or any other program is currently using the memory location &p[1000] for something. And if I tired to set p[1000] to a new value, then another program or even Windows who might be using that location to hold some memory, might crash, because I changed an important variable of that program.

Since C++ doesn't forbid this, I was wondering if at least Windows has some sort of protection against a program using a memory location currently used by someone else.

On Windows (and all other modern consumer operating systems) writing to a memory address you don't own will not directly affect memory belonging to any other process.

However, the operating system might be using that memory to provide essential services to your program, or the address might not be valid at all, so overwriting an address you don't own could cause your program to crash or behave in an unexpected way, either immediately or at some unpredictable point in the future. Google "undefined behavior" for more discussion of why this is a Bad Thing.

In the case of Windows, I have a vague recollection that the GUI uses some user-mode shared memory (for efficiency) so if you are really unlucky then writing to the wrong address might cause other GUI programs to malfunction, or perhaps even the entire GUI to become unresponsive, which would look very similar to an operating system hang from the user's point of view. I don't think I've ever seen that happen, though, so perhaps my information is out of date, or there are protective mechanisms in place to make this scenario less likely. (This does not represent a security vulnerability, because it only affects the user's other programs, and a malicious program could achieve the same effect in any number of other, more reliable ways.)

Memory is organized into PAGES. Each process sees a logical address space consisting of pages numbered 0-N.

The logical address space is divided into two ranges: user space and system space.

Each process has its own unique user space and all processes share the same system space. Your user space page 10 maps to a different physical location then some other process's user space page 10 (in most cases).

Memory in the system space is protected from user mode access. The only way to write to it is to switch to kernel mode. The operating system limits how you can do that to calls to specific system services. So, absent bugs (but we are talking M$ here) you should not be able to modify the system space willy-nilly.

It is possible for two applications to map memory in such a way that they are sharing memory locations in user mode. In that case, you can screw things up doing the type of thing you are illustrating. However, you have to explicitly map the memory in both processes.

Every process has its own address space. This space is mapped to the real adresses. They don't overlap.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM