Can you edit address values in “/proc/$pid/mem” to reflect changes in that process

hexkernelmemoryprocprogramming

I would like to see if I can edit a value in my program while it is running by directly changing the memory.

My thought process (untested) is to do the following:

  1. Run the program.
  2. Open up "System Monitor" app, and "stop" (not end or kill) the program.
  3. Navigate to /proc/<ID#>/
  4. chmod the file mem to give me read-write access
  5. Edit mem with a hex editor
  6. Continue program and see if my changes were reflected.

Is this the right way to go about this?

If not, how would I go about trying to achieve this?

Best Answer

According to man 5 proc:

/proc/[pid]/mem
  This file can be used to access the pages of a process's  memory  through  open(2),
  read(2), and lseek(2).

That's to say this interface only provides read access. You cannot write or modify this file:

# chmod +w /proc/$pid/mem
chmod: changing permissions of '/proc/2905/mem': Operation not permitted

If you want to modify a running process's memory, one way I can think of is to attach a debugger to the process, and then use it to set variables.

Related Question