process.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2015 Thomas Meyer (thomas@m3y3r.de)
  4. * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
  5. */
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <unistd.h>
  9. #include <errno.h>
  10. #include <signal.h>
  11. #include <fcntl.h>
  12. #include <limits.h>
  13. #include <linux/futex.h>
  14. #include <sys/mman.h>
  15. #include <sys/ptrace.h>
  16. #include <sys/prctl.h>
  17. #include <sys/wait.h>
  18. #include <asm/unistd.h>
  19. #include <init.h>
  20. #include <longjmp.h>
  21. #include <os.h>
  22. #include <skas/skas.h>
  23. void os_alarm_process(int pid)
  24. {
  25. if (pid <= 0)
  26. return;
  27. kill(pid, SIGALRM);
  28. }
  29. void os_kill_process(int pid, int reap_child)
  30. {
  31. if (pid <= 0)
  32. return;
  33. /* Block signals until child is reaped */
  34. block_signals();
  35. kill(pid, SIGKILL);
  36. if (reap_child)
  37. CATCH_EINTR(waitpid(pid, NULL, __WALL));
  38. unblock_signals();
  39. }
  40. /* Kill off a ptraced child by all means available. kill it normally first,
  41. * then PTRACE_KILL it, then PTRACE_CONT it in case it's in a run state from
  42. * which it can't exit directly.
  43. */
  44. void os_kill_ptraced_process(int pid, int reap_child)
  45. {
  46. if (pid <= 0)
  47. return;
  48. /* Block signals until child is reaped */
  49. block_signals();
  50. kill(pid, SIGKILL);
  51. ptrace(PTRACE_KILL, pid);
  52. ptrace(PTRACE_CONT, pid);
  53. if (reap_child)
  54. CATCH_EINTR(waitpid(pid, NULL, __WALL));
  55. unblock_signals();
  56. }
  57. pid_t os_reap_child(void)
  58. {
  59. int status;
  60. /* Try to reap a child */
  61. return waitpid(-1, &status, WNOHANG);
  62. }
  63. /* Don't use the glibc version, which caches the result in TLS. It misses some
  64. * syscalls, and also breaks with clone(), which does not unshare the TLS.
  65. */
  66. int os_getpid(void)
  67. {
  68. return syscall(__NR_getpid);
  69. }
  70. int os_map_memory(void *virt, int fd, unsigned long long off, unsigned long len,
  71. int r, int w, int x)
  72. {
  73. void *loc;
  74. int prot;
  75. prot = (r ? PROT_READ : 0) | (w ? PROT_WRITE : 0) |
  76. (x ? PROT_EXEC : 0);
  77. loc = mmap64((void *) virt, len, prot, MAP_SHARED | MAP_FIXED,
  78. fd, off);
  79. if (loc == MAP_FAILED)
  80. return -errno;
  81. return 0;
  82. }
  83. int os_protect_memory(void *addr, unsigned long len, int r, int w, int x)
  84. {
  85. int prot = ((r ? PROT_READ : 0) | (w ? PROT_WRITE : 0) |
  86. (x ? PROT_EXEC : 0));
  87. if (mprotect(addr, len, prot) < 0)
  88. return -errno;
  89. return 0;
  90. }
  91. int os_unmap_memory(void *addr, int len)
  92. {
  93. int err;
  94. err = munmap(addr, len);
  95. if (err < 0)
  96. return -errno;
  97. return 0;
  98. }
  99. #ifndef MADV_REMOVE
  100. #define MADV_REMOVE KERNEL_MADV_REMOVE
  101. #endif
  102. int os_drop_memory(void *addr, int length)
  103. {
  104. int err;
  105. err = madvise(addr, length, MADV_REMOVE);
  106. if (err < 0)
  107. err = -errno;
  108. return err;
  109. }
  110. int __init can_drop_memory(void)
  111. {
  112. void *addr;
  113. int fd, ok = 0;
  114. printk(UM_KERN_INFO "Checking host MADV_REMOVE support...");
  115. fd = create_mem_file(UM_KERN_PAGE_SIZE);
  116. if (fd < 0) {
  117. printk(UM_KERN_ERR "Creating test memory file failed, "
  118. "err = %d\n", -fd);
  119. goto out;
  120. }
  121. addr = mmap64(NULL, UM_KERN_PAGE_SIZE, PROT_READ | PROT_WRITE,
  122. MAP_SHARED, fd, 0);
  123. if (addr == MAP_FAILED) {
  124. printk(UM_KERN_ERR "Mapping test memory file failed, "
  125. "err = %d\n", -errno);
  126. goto out_close;
  127. }
  128. if (madvise(addr, UM_KERN_PAGE_SIZE, MADV_REMOVE) != 0) {
  129. printk(UM_KERN_ERR "MADV_REMOVE failed, err = %d\n", -errno);
  130. goto out_unmap;
  131. }
  132. printk(UM_KERN_CONT "OK\n");
  133. ok = 1;
  134. out_unmap:
  135. munmap(addr, UM_KERN_PAGE_SIZE);
  136. out_close:
  137. close(fd);
  138. out:
  139. return ok;
  140. }
  141. void init_new_thread_signals(void)
  142. {
  143. set_handler(SIGSEGV);
  144. set_handler(SIGTRAP);
  145. set_handler(SIGFPE);
  146. set_handler(SIGILL);
  147. set_handler(SIGBUS);
  148. signal(SIGHUP, SIG_IGN);
  149. set_handler(SIGIO);
  150. /* We (currently) only use the child reaper IRQ in seccomp mode */
  151. if (using_seccomp)
  152. set_handler(SIGCHLD);
  153. signal(SIGWINCH, SIG_IGN);
  154. }
  155. void os_set_pdeathsig(void)
  156. {
  157. prctl(PR_SET_PDEATHSIG, SIGKILL);
  158. }
  159. int os_futex_wait(void *uaddr, unsigned int val)
  160. {
  161. int r;
  162. CATCH_EINTR(r = syscall(__NR_futex, uaddr, FUTEX_WAIT, val,
  163. NULL, NULL, 0));
  164. return r < 0 ? -errno : r;
  165. }
  166. int os_futex_wake(void *uaddr)
  167. {
  168. int r;
  169. CATCH_EINTR(r = syscall(__NR_futex, uaddr, FUTEX_WAKE, INT_MAX,
  170. NULL, NULL, 0));
  171. return r < 0 ? -errno : r;
  172. }