shim_sigaction.c 16 KB

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