Are you thinking about the process of writing to some /proc/1234/mem another pid_t 1234 process?
Or are you thinking of writing a process to the ELF executable of another process?
Both scenarios are very unusual and specific to Linux (other Posix do not have them), so I don’t know what might happen in this case. But at least the permitting technique should protect some.
See also ETXTBSY error.
In practice (as shown in strace -f /usr/bin/gcc hello.c -o hello ), the compiler and linker remove the executable to open - to write the executable, so most compilations are never written to the old executable:
870 stat("hello", {st_mode=S_IFREG|0755, st_size=6096, ...}) = 0 870 unlink("hello") = 0 870 open("hello", O_RDWR|O_CREAT|O_TRUNC|O_CLOEXEC, 0777) = 17 870 fstat(17, {st_mode=S_IFREG|0755, st_size=0, ...}) = 0
So, to write to the executable, you need to try. Of course, when you do this, mischievous accidents can occur.
Basile starynkevitch
source share