The scenario is I try to re-link the program to a directory via /proc
where these directories into an elf executable.
First, I create a directory with name test
$ mkdir test
Link to an hello binary
# ln /bin/ping test
# exit
Open a file descriptor to the target binary
$ exec 3< test
You know, this descriptor should now be accessible via /proc
$ ls -l /proc/$$/fd/3
lr-x------ 1 febri febri 64 Jul 17 11:09 /proc/2930/fd/3 -> /home/febri/test
Remove the directory previously created
$ rm -rf test
The /proc
link should still exist, but now will be marked deleted.
$ ls -l /proc/$$/fd/3
lr-x------ 1 febri febri 64 Jul 17 11:09 /proc/2930/fd/3 -> /home/febri/test (deleted)
Replace the directory with example payload like :
$ cat hello.c
#include <stdio.h>
int main(int argc, char ** argv) {
printf("hello!\n");
return 0;
}
$ gcc -w -fPIC -shared -o test hello.c
$ ls -l test
-rwxrwxr-x 1 febri febri 6894 Jul 17 11:20 test
$ file test
test: ELF 32-bit LSB shared object, Intel 80386, version 1 (SYSV), dynamically linked, BuildID[sha1]=361c522d3d9db35ad24de9f3162f80f8a26c9c5b, not stripped
So, I running the linked program and the output is :
$ ./test
Segmentation fault (core dumped)
My question is :
Why the program crash when executed? if anyone can explain?