fork_exec.c 671 B

12345678910111213141516171819202122232425262728293031
  1. #include <errno.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <sys/wait.h>
  5. #include <unistd.h>
  6. int main(int argc, char* const* argv, const char* const* envp) {
  7. int newfd = dup(1), outfd = dup(1);
  8. char fd_argv[4];
  9. snprintf(fd_argv, 4, "%d", newfd);
  10. char* const new_argv[] = {"./exec_victim", fd_argv, NULL};
  11. setenv("IN_EXECVE", "1", 1);
  12. int pid = fork();
  13. if (pid == 0) {
  14. close(outfd);
  15. execv(new_argv[0], new_argv);
  16. }
  17. wait(NULL);
  18. FILE* out = fdopen(outfd, "a");
  19. if (!out) {
  20. printf("cannot open file descriptor\n");
  21. return -1;
  22. }
  23. fprintf(out, "Goodbye world!\n");
  24. return 0;
  25. }