kapil
kapil

Reputation: 271

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 :

int main() {
  int x = 20;
  return 0;
}

p2 code :

int main() {
  /*
  generate random addresses and access them.
  one of them might resolve to physical address of 'x' in P1
  */
  return 0;
}

Upvotes: 1

Views: 780

Answers (3)

Christophe
Christophe

Reputation: 73530

The detailed mechanics depend on the OS, but with modern CPU architectures that use virtual memory:

  • each process has its own virtual address space.
  • the hardware organizes the mapping between the virtual addresses and the real physical addresses.
  • any access to an unmapped virtual address will raise an error that will be caught

So if P1 uses an address 0x200, and if P2 would know this address and decide to use it to inject some stuff, this would not work. Because the virtual address 0x200 of one process is not the same than virtual address 0x200 of the other address. No address is shared between these process (except in the OS address space and this is well protected). So there's (in principle) no way for P2 to corrupt P1.

It is possible to use some special functions from the OS to share some memory, either via shared memory or via memory mapped files. But both processes need to cooperate in order to share memory.

Finally, depending on the privileges of P2, P2 could request access from the OS to the address space of P1. With windows it's the API functions ReadProcessMemory() and WriteProcessMemory(), and under linux, it's access to the /dev/mem device corresponding to the physical memory. But these privileges are given only to trustworthy processes run by trustworthy users. In principle normal processes of normal users shouldn't have these very sensitive privileges... So your P1 should be safe.

Upvotes: 1

eerorika
eerorika

Reputation: 238441

The concept of a process does not exist in the C++ language. There is no standard way to communicate between processes in C++.

There are however platform specific ways of inter process communication (IPC) provided by operating systems. A widely used IPC method are network sockets which can be used for commincation between not only processes within the same system, but also between processes on different systems connected by a network.

Upvotes: 0

Cpp Forever
Cpp Forever

Reputation: 980

Processes in linux run in protected mode. This means that a process has a virtual address and not a physical address.

The answer to question is yes because linux offers methods to write and read memory of another process.

This can be done by writing and reading the file /proc/%ld/mem where %ld is id of the process.

If you want to know more you can visit: https://nullprogram.com/blog/2016/09/03/

Upvotes: 0

Related Questions