Is there any way by which a process P2 can access the local variable of another process P1?

Suppose there are two processes P1, P2(which is a virus) in linux.
Can P2 access a local varable (say x) of P1 ?

On searching on web, I found that since the addresses used in processes are logical addresses, P2 can't access the local variable 'x' of P1.
But I am wondering if P2 generates random addresses and one of which resolves to the same physical address as of 'x' then can't it access it ?
Is it really possible for P2 to access 'x' of P1 ?
If yes, how? (If it can be accessed through any tricks, please let me know)
And if no, why?

P1 code :

1
2
3
4
int main() {
  int x = 20;
  return 0;
}


p2 code :

1
2
3
4
5
6
7
int main() {
  /*
  generate random addresses and access them.
  one of them might resolve to physical address of 'x' in P1
  */
  return 0;
}
Last edited on
On a modern OS, the short answer is no.

The long answer is read all about it; https://en.wikipedia.org/wiki/Segmentation_fault

The system knows what memory addresses a given process is allowed to ask for. If the process asks for one it's not allowed, the system kills it.


Some people call this discipline "computer science". The key to science is experimentation. You very nearly wrote enough code to experiment with this and try it yourself. Here, start experimenting:

1
2
3
4
5
6
7
int main() 
{
  int* p = (int*) 0x12345678;   // some random memory address
  *p = 17; // try to write to it

  return 0;
}
Last edited on
Topic archived. No new replies allowed.