setpgid-across-sessions-without-leader.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. * Copyright (c) 2020, Ben Wiederhake <BenWiederhake.GitHub@gmx.de>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Assertions.h>
  7. #include <AK/Format.h>
  8. #include <errno.h>
  9. #include <fcntl.h>
  10. #include <serenity.h>
  11. #include <stdio.h>
  12. #include <unistd.h>
  13. /*
  14. * Bug:
  15. * A process can join a process group across sessions if both process groups
  16. * do not have a leader (anymore). This can be used to join a session
  17. * illegitimately. (Or, more harmlessly, to change the own PGID to an unused
  18. * but arbitrary one, for example the PGID 0xDEADBEEF or the one that's going
  19. * to be your program's session ID in the short-term future.)
  20. *
  21. * So what needs to happen:
  22. * - There is session SA
  23. * - There is session SB
  24. * - There is a Process Group PGA in SA
  25. * - There is a Process Group PGB in SB
  26. * - PGA does not have a leader
  27. * - PGB does not have a leader
  28. * - There is a Process PA2 in PGA
  29. * - There is a Process PB2 in PGB
  30. * - PA2 calls setpgid(0, PGB)
  31. * - Now PA2 and PB2 are in the same processgroup, but not in the same session. WHAAAAT! :^)
  32. *
  33. * Here's how to demonstrate the bug:
  34. * - Time 0: PX forks into PA1
  35. * - Time 1: PA1 creates a new session (SA) and pgrp (PGA)
  36. * - Time 2: PA1 forks into PA2
  37. * - Time 3: PA1 dies (PGA now has no leader)
  38. * Note: PA2 never dies. Too much hassle.
  39. * - Time 4: PX forks into PB1
  40. * - Time 5: PB1 creates a new session (SB) and pgrp (PGB)
  41. * - Time 6: PB1 forks into PB2
  42. * - Time 7: PB1 dies (PGB now has no leader)
  43. * - Time 8: PB2 calls pgrp(0, PGA)
  44. * Note: PB2 writes "1" (exploit successful) or "0" (bug is fixed) to a pipe
  45. * - Time 9: If PX hasn't received any message yet through the pipe, it declares the test as failed (for lack of knowledge). Otherwise, it outputs accordingly.
  46. */
  47. static constexpr useconds_t STEP_SIZE = 1100000;
  48. static void fork_into(void (*fn)(void*), void* arg)
  49. {
  50. const pid_t rc = fork();
  51. if (rc < 0) {
  52. perror("fork");
  53. exit(1);
  54. }
  55. if (rc > 0) {
  56. const int disown_rc = disown(rc);
  57. if (disown_rc < 0) {
  58. perror("disown");
  59. dbgln("This might cause PA1 to remain in the Zombie state, "
  60. "and thus in the process list, meaning the leader is "
  61. "still 'alive' for the purpose of lookup.");
  62. }
  63. return;
  64. }
  65. fn(arg);
  66. dbgln("child finished (?)");
  67. exit(1);
  68. }
  69. static void sleep_steps(useconds_t steps)
  70. {
  71. const int rc = usleep(steps * STEP_SIZE);
  72. if (rc < 0) {
  73. perror("usleep");
  74. VERIFY_NOT_REACHED();
  75. }
  76. }
  77. static void run_pa1(void*);
  78. static void run_pa2(void*);
  79. static void run_pb1(void*);
  80. static void run_pb2(void*);
  81. int main(int, char**)
  82. {
  83. // This entire function is the entirety of process PX.
  84. // Time 0: PX forks into PA1
  85. int fds[2];
  86. // Serenity doesn't support O_NONBLOCK for pipes yet, so
  87. // sadly the test will hang if something goes wrong.
  88. if (pipe2(fds, 0) < 0) {
  89. perror("pipe");
  90. exit(1);
  91. }
  92. dbgln("PX starts with SID={}, PGID={}, PID={}.", getsid(0), getpgid(0), getpid());
  93. dbgln("PX forks into PA1");
  94. fork_into(run_pa1, nullptr);
  95. sleep_steps(4);
  96. // Time 4: PX forks into PB1
  97. dbgln("PX forks into PB1");
  98. fork_into(run_pb1, &(fds[1]));
  99. sleep_steps(5);
  100. // Time 9: If PX hasn't received any message yet through the pipe, it declares
  101. // the test as failed (for lack of knowledge). Otherwise, it outputs accordingly.
  102. dbgln("PX reads from pipe");
  103. unsigned char buf = 42;
  104. ssize_t rc = read(fds[0], &buf, 1);
  105. if (rc == 0) {
  106. // In fact, we only reach this branch when *all* processes have died,
  107. // including this one. So … should be unreachable.
  108. printf("DOUBLE FAIL: pipe is closed, but we still have it open.\n"
  109. "See debug log, some process probably crashed.\n");
  110. exit(1);
  111. }
  112. if (rc < 0) {
  113. if (errno == EAGAIN) {
  114. printf("FAIL: pipe has no data. See debug log, some process os probably hanging.\n");
  115. } else {
  116. perror("read (unknown)");
  117. }
  118. exit(1);
  119. }
  120. VERIFY(rc == 1);
  121. if (buf == 0) {
  122. printf("PASS\n");
  123. return 0;
  124. }
  125. if (buf == 1) {
  126. printf("FAIL (exploit successful)\n");
  127. return 1;
  128. }
  129. printf("FAIL, for some reason %c\n", buf);
  130. return 1;
  131. }
  132. static void run_pa1(void*)
  133. {
  134. // Time 0: PX forks into PA1
  135. sleep_steps(1);
  136. // Time 1: PA1 creates a new session (SA) and pgrp (PGA)
  137. dbgln("PA1 starts with SID={}, PGID={}, PID={}.", getsid(0), getpgid(0), getpid());
  138. dbgln("PA1 calls setsid()");
  139. int rc = setsid();
  140. if (rc < 0) {
  141. perror("setsid (PA)");
  142. VERIFY_NOT_REACHED();
  143. }
  144. dbgln("PA1 did setsid() -> PGA={}, SA={}, yay!", rc, getsid(0));
  145. sleep_steps(1);
  146. // Time 2: PA1 forks into PA2
  147. dbgln("PA1 forks into PA2");
  148. fork_into(run_pa2, nullptr);
  149. sleep_steps(1);
  150. // Time 3: PA1 dies (PGA now has no leader)
  151. dbgln("PA1 dies. You should see a 'Reaped unparented process' "
  152. "message with my ID next, OR THIS TEST IS MEANINGLESS "
  153. "(see fork_into()).");
  154. exit(0);
  155. }
  156. static void run_pa2(void*)
  157. {
  158. // Time 2: PA1 forks into PA2
  159. dbgln("PA2 starts with SID={}, PGID={}, PID={}.", getsid(0), getpgid(0), getpid());
  160. sleep_steps(18);
  161. // pa_2 never *does* anything.
  162. dbgln("PA2 dies from boredom.");
  163. exit(1);
  164. }
  165. static void run_pb1(void* pipe_fd_ptr)
  166. {
  167. // Time 4: PX forks into PB1
  168. sleep_steps(1);
  169. // Time 5: PB1 creates a new session (SB) and pgrp (PGB)
  170. dbgln("PB1 starts with SID={}, PGID={}, PID={}.", getsid(0), getpgid(0), getpid());
  171. dbgln("PB1 calls setsid()");
  172. int rc = setsid();
  173. if (rc < 0) {
  174. perror("setsid (PB)");
  175. VERIFY_NOT_REACHED();
  176. }
  177. dbgln("PB1 did setsid() -> PGB={}, SB={}, yay!", rc, getsid(0));
  178. sleep_steps(1);
  179. // Time 6: PB1 forks into PB2
  180. dbgln("PB1 forks into PB2");
  181. fork_into(run_pb2, pipe_fd_ptr);
  182. sleep_steps(1);
  183. // Time 7: PB1 dies (PGB now has no leader)
  184. dbgln("PB1 dies. You should see a 'Reaped unparented process' "
  185. "message with my ID next, OR THIS TEST IS MEANINGLESS "
  186. "(see fork_into()).");
  187. exit(0);
  188. }
  189. static void simulate_sid_from_pgid(pid_t pgid)
  190. {
  191. pid_t rc = getpgid(pgid); // Same confusion as in the Kernel
  192. int saved_errno = errno;
  193. if (rc < 0 && saved_errno == ESRCH) {
  194. dbgln("The old get_sid_from_pgid({}) would return -1", pgid);
  195. } else if (rc >= 0) {
  196. dbgln("FAIL: Process {} still exists?! PGID is {}.", pgid, rc);
  197. } else {
  198. perror("pgid (probably fail)");
  199. }
  200. }
  201. static void run_pb2(void* pipe_fd_ptr)
  202. {
  203. // Time 6: PB1 forks into PB2
  204. sleep_steps(2);
  205. // Time 8: PB2 calls pgrp(0, PGA)
  206. // Note: PB2 writes "1" (exploit successful) or "0" (bug is fixed) to a pipe
  207. dbgln("PB2 starts with SID={}, PGID={}, PID={}.", getsid(0), getpgid(0), getpid());
  208. dbgln("PB2 calls pgrp(0, PGA)");
  209. int pga = getpid() - 3;
  210. dbgln("PB2: Actually, what is PGA? I guess it's {}?", pga);
  211. simulate_sid_from_pgid(pga);
  212. int rc = setpgid(0, pga);
  213. unsigned char to_write = 123;
  214. if (rc == 0) {
  215. dbgln("PB2: setgpid SUCCESSFUL! CHANGED PGROUP!");
  216. to_write = 1;
  217. } else {
  218. VERIFY(rc == -1);
  219. switch (errno) {
  220. case EACCES:
  221. dbgln("PB2: Failed with EACCES. Huh?!");
  222. to_write = 101;
  223. break;
  224. case EINVAL:
  225. dbgln("PB2: Failed with EINVAL. Huh?!");
  226. to_write = 102;
  227. break;
  228. case ESRCH:
  229. dbgln("PB2: Failed with ESRCH. Huh?!");
  230. to_write = 103;
  231. break;
  232. case EPERM:
  233. dbgln("PB2: Failed with EPERM. Aww, no exploit today :^)");
  234. to_write = 0;
  235. break;
  236. default:
  237. dbgln("PB2: Failed with errno={}?!", errno);
  238. perror("setpgid");
  239. to_write = 104;
  240. break;
  241. }
  242. }
  243. dbgln("PB2 ends with SID={}, PGID={}, PID={}.", getsid(0), getpgid(0), getpid());
  244. int* pipe_fd = static_cast<int*>(pipe_fd_ptr);
  245. VERIFY(*pipe_fd);
  246. rc = write(*pipe_fd, &to_write, 1);
  247. if (rc != 1) {
  248. dbgln("Wrote only {} bytes instead of 1?!", rc);
  249. exit(1);
  250. }
  251. exit(0);
  252. }