C ++: can I get out of the bounds of my application memory with a pointer?

If I have stupid code like this:

int nBlah = 123; int* pnBlah = &nBlah; pnBlah += 80000; *pnBlah = 65; 

Can I change another application memory?

You explained to me that this is evil, I know. But I was just interested. And it is not easy to try. I do not know what will happen.

thanks

+4
c ++ pointers memory
source share
5 answers

In C ++ terms, this behavior is undefined. What actually happens depends on many factors, but most importantly, it depends on the operating system (OS) used. On modern memory operating systems, your application will be terminated with a “segmentation error” (the actual term depends on the OS) to attempt to access memory outside the address space of your process. Some operating systems, however, do not have this protection, and you can perforce and destroy things belonging to other programs. This also usually happens if your code is inside kernel space, for example. in the device driver.

+13
source share

No, it's not that simple. :)

Modern operating systems use virtual memory.

Each process is provided with a full virtual address space.

Each process is provided with its own "view" of all addresses (from 0x00000000 to 0xffffffff on a 32-bit system). Processes A and B can write to the same address without affecting each other, since they do not have access to physical memory addresses, but to virtual addresses. When a process tries to access a virtual address, the OS translates this to another physical address in order to avoid collisions.

Essentially, the OS keeps track of the memory page allocation table for each process. It keeps track of which address ranges have been allocated to the process, and to which physical addresses they are mapped. If a process tries to access an address that is not assigned to it, you will receive an access / segmentation violation. And if you try to access the address that is assigned to your process, you will get your own data. Thus, there is no way to read data from other processes by simply typing the “wrong” address.

+5
source share

In modern operating systems, you do not get access to real memory, but rather a virtual memory space of 4 GB (up to 32 bits). Bottom 2gb is for you, and the top 2gb is reserved for the operating system.

This does not reflect the actual bytes of memory in RAM.

Each application receives the same virtual address space, so there is no direct access to another process memory space.

+4
source share

I think this will raise 0x00000005, window access violation

+1
source share

Modern operating systems have various means of protection against these types of exploits, which are written to the memory space of other programs. Your code will not work anyway, I don’t think so.

For more information, check out buffer overflow exploits and how they got to hell before the release of Windows XP Service Pack 2 (SP2).

0
source share

All Articles