Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make pointer to point on another C program [duplicate]

Tags:

c

If I have C program called A with the following code:

int main()
{
   int x=5; 
   int *pX=&x;
   return 0;
}

pX point to address of x, Let's say is 0x123456.

And I have another program called B:

int main()
{
   int y=5; 
   return 0;
}

And let's say the address of y is 0x123488 how does the mechanism (virtual memory) that prevent me to change the value of pX to point on 0x123488 (address of y) and to read/change the value of y from program A if I know the address of y work?

NOTE: Let's assume I debug both programs and I know the current address for each value.

like image 249
user16634242 Avatar asked Jan 21 '26 17:01

user16634242


1 Answers

And let's say the address of y is 0x123488 which mechanism prevent me to change the value of pX to point on 0x123488 (address of y) and to read/change the value of y from program A if I know the address of y?

It is the virtual address space.

Meaning that while your code sees the address of y to be 0x123488 that is only the virtual address of y in the context of your program. The actual physical address is managed by the OS(more specifically, the kernel).

However the good news is that you can change the variable of another process. All of the interaction between two processes must be done through your OS's API.

A lot of debugging programs and game cheats use this method to debug or well, cheat at the game(do not attempt, the anti-cheat will detect this method and ban you)

For windows you can use WriteProcessMemory and ReadProcessMemory. I don't know how to do it on Linux but I am sure there is a way.

Also as onVal mentioned in his answer, you should look up Inter Process Communication as it might be more useful.

like image 62
Marko Borković Avatar answered Jan 23 '26 07:01

Marko Borković



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!