I am playing around with ptrace in linux. I am trying to write the memory of the traced process using /proc/pid/mem interface.
the function I ma using for accomplish this task is :
void write_proc(pid_t child, unsigned long int addr) {
char mem_file_name[100];
char buf[10]="hope";
int mem_fd;
memset( (void*)mem_file_name, 0, 100);
memset( (void *)buf, 0, 10);
sprintf(mem_file_name, "/proc/%d/mem", child);
mem_fd = open(mem_file_name, O_RDONLY);
lseek(mem_fd, addr , SEEK_SET);
if (write(mem_fd, buf, 5) < 0 )
perror("Writing");
return;
}
But I always get the error : Writing: Bad file descriptor.
Is it possible to write the traced process using this method?
You are opening the file in read-only mode (
O_RDONLY
). I'd suggest trying again withO_RDWR
instead:However, from
man proc
it's not clear this will work:EDIT:
I was curious too, so I put together this example using just
ptrace()
directly:ptrace(2) is a very arcane syscall, only used by debuggers and the like.
For sure, the documented
PTRACE_POKEDATA
request toptrace
should work (when the traced process is stopped) and gives you the ability to write into the memory of the traced process. I don't know if writing (ormmap
-ing) to/proc/$pid/mem
should work or not.Googling on
linux write /proc /mem
give me notably this which suggests that/proc/$pid/mem
was designed to be read-only, but might have been made writable in recent kernels. But recent Documentation/filesystems/proc.txt from kernel source tree don't say much.I would be cautious about writing on
/proc/$pid/mem
; if it works (and it might not) it probably is very kernel version specific.Perhaps
mmap
-ing some segments of that/proc/$pid/mem
file does work (but I don't know). Have you tried that?In contrast,
PTRACE_POKEDATA
should work (it has existed in SunOS and many other Unixes before Linux existed). Of course, it is fairly slow.