shim_sigaction.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  1. /* -*- mode:c; c-file-style:"k&r"; c-basic-offset: 4; tab-width:4; indent-tabs-mode:nil; mode:auto-fill; fill-column:78; -*- */
  2. /* vim: set ts=4 sw=4 et tw=78 fo=cqt wm=0: */
  3. /* Copyright (C) 2014 Stony Brook University
  4. This file is part of Graphene Library OS.
  5. Graphene Library OS is free software: you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public License
  7. as published by the Free Software Foundation, either version 3 of the
  8. License, or (at your option) any later version.
  9. Graphene Library OS is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. /*
  16. * shim_sigaction.c
  17. *
  18. * Implementation of system call "sigaction", "sigreturn", "sigprocmask",
  19. * "kill", "tkill" and "tgkill".
  20. */
  21. #include <shim_internal.h>
  22. #include <shim_utils.h>
  23. #include <shim_table.h>
  24. #include <shim_thread.h>
  25. #include <shim_ipc.h>
  26. #include <shim_profile.h>
  27. #include <pal.h>
  28. #include <pal_error.h>
  29. #include <errno.h>
  30. #include <linux/signal.h>
  31. int shim_do_sigaction (int signum, const struct __kernel_sigaction * act,
  32. struct __kernel_sigaction * oldact, size_t sigsetsize)
  33. {
  34. /* SIGKILL and SIGSTOP cannot be caught or ignored */
  35. if (signum == SIGKILL || signum == SIGSTOP ||
  36. signum <= 0 || signum > NUM_SIGS ||
  37. sigsetsize != sizeof(__sigset_t))
  38. return -EINVAL;
  39. if (act && test_user_memory((void *) act, sizeof(*act), false))
  40. return -EFAULT;
  41. if (oldact && test_user_memory(oldact, sizeof(*oldact), false))
  42. return -EFAULT;
  43. struct shim_thread * cur = get_cur_thread();
  44. int err = 0;
  45. assert(!act || (void *) act->k_sa_handler != (void *) 0x11);
  46. struct shim_signal_handle * sighdl = &cur->signal_handles[signum - 1];
  47. lock(cur->lock);
  48. if (oldact) {
  49. if (sighdl->action) {
  50. memcpy(oldact, sighdl->action, sizeof(struct __kernel_sigaction));
  51. } else {
  52. memset(oldact, 0, sizeof(struct __kernel_sigaction));
  53. oldact->k_sa_handler = SIG_DFL;
  54. }
  55. }
  56. if (act) {
  57. if (!(sighdl->action))
  58. sighdl->action = malloc(sizeof(struct __kernel_sigaction));
  59. if (!(sighdl->action)) {
  60. err = -ENOMEM;
  61. goto out;
  62. }
  63. memcpy(sighdl->action, act, sizeof(struct __kernel_sigaction));
  64. }
  65. err = 0;
  66. out:
  67. unlock(cur->lock);
  68. return err;
  69. }
  70. int shim_do_sigreturn (int __unused)
  71. {
  72. /* do nothing */
  73. return 0;
  74. }
  75. int shim_do_sigprocmask (int how, const __sigset_t * set, __sigset_t * oldset)
  76. {
  77. __sigset_t * old, tmp, set_tmp;
  78. if (how != SIG_BLOCK && how != SIG_UNBLOCK &&
  79. how != SIG_SETMASK)
  80. return -EINVAL;
  81. if (set && test_user_memory((void *) set, sizeof(*set), false))
  82. return -EFAULT;
  83. if (oldset && test_user_memory(oldset, sizeof(*oldset), false))
  84. return -EFAULT;
  85. struct shim_thread * cur = get_cur_thread();
  86. int err = 0;
  87. lock(cur->lock);
  88. old = get_sig_mask(cur);
  89. if (oldset) {
  90. memcpy(&tmp, old, sizeof(__sigset_t));
  91. old = &tmp;
  92. }
  93. /* if set is NULL, then the signal mask is unchanged, but the current
  94. value of the signal mask is nevertheless returned in oldset */
  95. if (!set)
  96. goto out;
  97. memcpy(&set_tmp, old, sizeof(__sigset_t));
  98. switch (how) {
  99. case SIG_BLOCK:
  100. __sigorset(&set_tmp, &set_tmp, set);
  101. break;
  102. case SIG_UNBLOCK:
  103. __signotset(&set_tmp, &set_tmp, set);
  104. break;
  105. case SIG_SETMASK:
  106. memcpy(&set_tmp, set, sizeof(__sigset_t));
  107. break;
  108. }
  109. set_sig_mask(cur, &set_tmp);
  110. out:
  111. unlock(cur->lock);
  112. if (!err && oldset)
  113. memcpy(oldset, old, sizeof(__sigset_t));
  114. return err;
  115. }
  116. int shim_do_sigaltstack (const stack_t * ss, stack_t * oss)
  117. {
  118. if (ss && (ss->ss_flags & ~SS_DISABLE))
  119. return -EINVAL;
  120. struct shim_thread * cur = get_cur_thread();
  121. lock(cur->lock);
  122. if (oss)
  123. *oss = cur->signal_altstack;
  124. if (ss) {
  125. if (ss->ss_size < MINSIGSTKSZ) {
  126. unlock(cur->lock);
  127. return -ENOMEM;
  128. }
  129. cur->signal_altstack = *ss;
  130. }
  131. unlock(cur->lock);
  132. return 0;
  133. }
  134. int shim_do_sigsuspend (const __sigset_t * mask)
  135. {
  136. if (!mask || test_user_memory((void *) mask, sizeof(*mask), false))
  137. return -EFAULT;
  138. __sigset_t * old, tmp;
  139. struct shim_thread * cur = get_cur_thread();
  140. lock(cur->lock);
  141. old = get_sig_mask(cur);
  142. memcpy(&tmp, old, sizeof(__sigset_t));
  143. old = &tmp;
  144. set_sig_mask(cur, mask);
  145. cur->suspend_on_signal = true;
  146. thread_setwait(NULL, NULL);
  147. thread_sleep(NO_TIMEOUT);
  148. unlock(cur->lock);
  149. set_sig_mask(cur, old);
  150. return -EINTR;
  151. }
  152. int shim_do_sigpending (__sigset_t * set, size_t sigsetsize)
  153. {
  154. if (!set || test_user_memory(set, sizeof(*set), false))
  155. return -EFAULT;
  156. struct shim_thread * cur = get_cur_thread();
  157. __sigemptyset(set);
  158. if (!cur->signal_logs)
  159. return 0;
  160. for (int sig = 1 ; sig <= NUM_SIGS ; sig++) {
  161. if (atomic_read(&cur->signal_logs[sig - 1].head) !=
  162. atomic_read(&cur->signal_logs[sig - 1].tail))
  163. __sigaddset(set, sig);
  164. }
  165. return 0;
  166. }
  167. struct walk_arg {
  168. struct shim_thread * current;
  169. IDTYPE sender;
  170. IDTYPE id;
  171. int sig;
  172. bool use_ipc;
  173. };
  174. static inline void __append_signal (struct shim_thread * thread, int sig,
  175. IDTYPE sender)
  176. {
  177. debug("Thread %d killed by signal %d\n", thread->tid, sig);
  178. siginfo_t info;
  179. memset(&info, 0, sizeof(siginfo_t));
  180. info.si_signo = sig;
  181. info.si_pid = sender;
  182. append_signal(thread, sig, &info, true);
  183. }
  184. static int __kill_proc (struct shim_thread * thread, void * arg,
  185. bool * unlocked)
  186. {
  187. struct walk_arg * warg = (struct walk_arg *) arg;
  188. int srched = 0;
  189. if (!warg->use_ipc && !thread->in_vm)
  190. return 0;
  191. if (thread->tgid != warg->id)
  192. return 0;
  193. if (warg->current == thread)
  194. return 1;
  195. /* DEP: Let's do a racy read of is_alive and in_vm.
  196. * If either of these are zero it is a stable condition,
  197. * and we can elide the lock acquire (which helps perf).
  198. */
  199. if (!thread->is_alive)
  200. goto out;
  201. if (!thread->in_vm) {
  202. unlock(thread_list_lock);
  203. *unlocked = true;
  204. return (!ipc_pid_kill_send(warg->sender, warg->id, KILL_PROCESS,
  205. warg->sig)) ? 1 : 0;
  206. } else {
  207. lock(thread->lock);
  208. if (!thread->is_alive)
  209. goto out_locked;
  210. if (thread->in_vm) {
  211. if (warg->sig > 0)
  212. __append_signal(thread, warg->sig, warg->sender);
  213. srched = 1;
  214. } else {
  215. /* This double-check case is probably unnecessary, but keep it for now */
  216. unlock(thread->lock);
  217. unlock(thread_list_lock);
  218. *unlocked = true;
  219. return (!ipc_pid_kill_send(warg->sender, warg->id, KILL_PROCESS,
  220. warg->sig)) ? 1 : 0;
  221. }
  222. }
  223. out_locked:
  224. unlock(thread->lock);
  225. out:
  226. return srched;
  227. }
  228. static int __kill_proc_simple (struct shim_simple_thread * sthread,
  229. void * arg, bool * unlocked)
  230. {
  231. struct walk_arg * warg = (struct walk_arg *) arg;
  232. int srched = 0;
  233. if (sthread->tgid != warg->id)
  234. return 0;
  235. lock(sthread->lock);
  236. if (sthread->is_alive) {
  237. unlock(sthread->lock);
  238. unlock(thread_list_lock);
  239. *unlocked = true;
  240. return (!ipc_pid_kill_send(warg->sender, warg->id, KILL_PROCESS,
  241. warg->sig)) ? 1 : 0;
  242. }
  243. unlock(sthread->lock);
  244. return srched;
  245. }
  246. int do_kill_proc (IDTYPE sender, IDTYPE tgid, int sig, bool use_ipc)
  247. {
  248. struct shim_thread * cur = get_cur_thread();
  249. if (!tgid) {
  250. /* DEP: cur->tgid never changes. No lock needed */
  251. tgid = cur->tgid;
  252. }
  253. struct walk_arg arg;
  254. arg.current = cur;
  255. arg.sender = sender;
  256. arg.id = tgid;
  257. arg.sig = sig;
  258. arg.use_ipc = use_ipc;
  259. bool srched = false;
  260. if (!walk_thread_list(__kill_proc, &arg, false))
  261. srched = true;
  262. if (!use_ipc || srched)
  263. goto out;
  264. if (!walk_simple_thread_list(__kill_proc_simple, &arg, false))
  265. srched = true;
  266. if (!srched && !ipc_pid_kill_send(sender, tgid, KILL_PROCESS, sig))
  267. srched = true;
  268. out:
  269. return srched ? 0 : -ESRCH;
  270. }
  271. static int __kill_pgroup (struct shim_thread * thread, void * arg,
  272. bool * unlocked)
  273. {
  274. struct walk_arg * warg = (struct walk_arg *) arg;
  275. int srched = 0;
  276. if (!warg->use_ipc && !thread->in_vm)
  277. return 0;
  278. if (thread->pgid != warg->id)
  279. return 0;
  280. if (warg->current == thread)
  281. return 1;
  282. lock(thread->lock);
  283. if (!thread->is_alive)
  284. goto out;
  285. if (thread->in_vm) {
  286. if (warg->sig > 0)
  287. __append_signal(thread, warg->sig, warg->sender);
  288. srched = 1;
  289. } else {
  290. unlock(thread->lock);
  291. unlock(thread_list_lock);
  292. *unlocked = true;
  293. return (!ipc_pid_kill_send(warg->sender, warg->id, KILL_PGROUP,
  294. warg->sig)) ? 1 : 0;
  295. }
  296. out:
  297. unlock(thread->lock);
  298. return srched;
  299. }
  300. static int __kill_pgroup_simple (struct shim_simple_thread * sthread,
  301. void * arg, bool * unlocked)
  302. {
  303. struct walk_arg * warg = (struct walk_arg *) arg;
  304. int srched = 0;
  305. if (sthread->pgid != warg->id)
  306. return 0;
  307. lock(sthread->lock);
  308. if (sthread->is_alive) {
  309. unlock(sthread->lock);
  310. unlock(thread_list_lock);
  311. *unlocked = true;
  312. return (!ipc_pid_kill_send(warg->sender, warg->id, KILL_PGROUP,
  313. warg->sig)) ? 1 : 0;
  314. }
  315. unlock(sthread->lock);
  316. return srched;
  317. }
  318. int do_kill_pgroup (IDTYPE sender, IDTYPE pgid, int sig, bool use_ipc)
  319. {
  320. struct shim_thread * cur = get_cur_thread();
  321. if (!pgid) {
  322. pgid = cur->pgid;
  323. }
  324. struct walk_arg arg;
  325. arg.current = cur;
  326. arg.sender = sender;
  327. arg.id = pgid;
  328. arg.sig = sig;
  329. arg.use_ipc = use_ipc;
  330. bool srched = false;
  331. if (!walk_thread_list(__kill_pgroup, &arg, false))
  332. srched = true;
  333. if (!use_ipc || srched)
  334. goto out;
  335. if (!walk_simple_thread_list(__kill_pgroup_simple, &arg, false))
  336. srched = true;
  337. if (!srched && !ipc_pid_kill_send(sender, pgid, KILL_PGROUP, sig))
  338. srched = true;
  339. out:
  340. return srched ? 0 : -ESRCH;
  341. }
  342. static int __kill_all_threads (struct shim_thread * thread, void * arg,
  343. bool * unlocked)
  344. {
  345. int srched = 0;
  346. struct walk_arg * warg = (struct walk_arg *) arg;
  347. if (thread->tgid != thread->tid)
  348. return 0;
  349. if (warg->current == thread)
  350. return 1;
  351. lock(thread->lock);
  352. if (thread->in_vm) {
  353. __append_signal(thread, warg->sig, warg->sender);
  354. srched = 1;
  355. }
  356. unlock(thread->lock);
  357. return srched;
  358. }
  359. int broadcast_signal (IDTYPE sender, int sig);
  360. int kill_all_threads (struct shim_thread * cur, IDTYPE sender, int sig)
  361. {
  362. struct walk_arg arg;
  363. arg.current = cur;
  364. arg.sender = sender;
  365. arg.id = 0;
  366. arg.sig = sig;
  367. arg.use_ipc = false;
  368. walk_thread_list(__kill_all_threads, &arg, false);
  369. return 0;
  370. }
  371. int shim_do_kill (pid_t pid, int sig)
  372. {
  373. INC_PROFILE_OCCURENCE(syscall_use_ipc);
  374. if (sig < 0 || sig > NUM_SIGS)
  375. return -EINVAL;
  376. struct shim_thread * cur = get_cur_thread();
  377. int ret = 0;
  378. bool send_to_self = false;
  379. /* If pid equals 0, then sig is sent to every process in the process group
  380. of the calling process. */
  381. if (pid == 0) {
  382. ret = do_kill_pgroup(cur->tgid, 0, sig, true);
  383. send_to_self = true;
  384. }
  385. /* If pid equals -1, then sig is sent to every process for which the
  386. calling process has permission to send */
  387. else if (pid == -1) {
  388. broadcast_signal(cur->tid, sig);
  389. kill_all_threads(cur, cur->tid, sig);
  390. send_to_self = true;
  391. }
  392. /* If pid is positive, then signal sig is sent to the process with the ID
  393. specified by pid. */
  394. else if (pid > 0) {
  395. ret = do_kill_proc(cur->tid, pid, sig, true);
  396. send_to_self = (pid == cur->tgid);
  397. }
  398. /* If pid is less than -1, then sig is sent to every process in the
  399. process group whose id is -pid */
  400. else {
  401. ret = do_kill_pgroup(cur->tid, -pid, sig, true);
  402. send_to_self = (-pid == cur->pgid);
  403. }
  404. if (send_to_self) {
  405. if (ret == -ESRCH)
  406. ret = 0;
  407. if (sig) {
  408. siginfo_t info;
  409. memset(&info, 0, sizeof(siginfo_t));
  410. info.si_signo = sig;
  411. info.si_pid = cur->tid;
  412. deliver_signal(&info, NULL);
  413. }
  414. }
  415. return ret < 0 ? ret : 0;
  416. }
  417. int do_kill_thread (IDTYPE sender, IDTYPE tgid, IDTYPE tid, int sig,
  418. bool use_ipc)
  419. {
  420. if (sig < 0 || sig > NUM_SIGS)
  421. return -EINVAL;
  422. struct shim_thread * thread = lookup_thread(tid);
  423. int ret = 0;
  424. if (thread) {
  425. lock(thread->lock);
  426. if (thread->in_vm) {
  427. if (!tgid || thread->tgid == tgid)
  428. __append_signal(thread, sig, sender);
  429. else
  430. ret = -ESRCH;
  431. } else {
  432. unlock(thread->lock);
  433. return ipc_pid_kill_send(sender, tid, KILL_THREAD, sig);
  434. }
  435. unlock(thread->lock);
  436. return ret;
  437. }
  438. if (!use_ipc)
  439. return -ESRCH;
  440. return ipc_pid_kill_send(sender, tid, KILL_THREAD, sig);
  441. }
  442. int shim_do_tkill (pid_t tid, int sig)
  443. {
  444. INC_PROFILE_OCCURENCE(syscall_use_ipc);
  445. if (tid <= 0)
  446. return -EINVAL;
  447. struct shim_thread * cur = get_cur_thread();
  448. if (tid == cur->tid) {
  449. if (sig) {
  450. siginfo_t info;
  451. memset(&info, 0, sizeof(siginfo_t));
  452. info.si_signo = sig;
  453. info.si_pid = cur->tid;
  454. deliver_signal(&info, NULL);
  455. }
  456. return 0;
  457. }
  458. return do_kill_thread(cur->tgid, 0, tid, sig, true);
  459. }
  460. int shim_do_tgkill (pid_t tgid, pid_t tid, int sig)
  461. {
  462. INC_PROFILE_OCCURENCE(syscall_use_ipc);
  463. if (tgid < -1 || tgid == 0 || tid <= 0)
  464. return -EINVAL;
  465. if (tgid == -1)
  466. tgid = 0;
  467. struct shim_thread * cur = get_cur_thread();
  468. if (tid == cur->tid) {
  469. if (sig) {
  470. siginfo_t info;
  471. memset(&info, 0, sizeof(siginfo_t));
  472. info.si_signo = sig;
  473. info.si_pid = cur->tid;
  474. deliver_signal(&info, NULL);
  475. }
  476. return 0;
  477. }
  478. return do_kill_thread(cur->tgid, tgid, tid, sig, true);
  479. }