shim_sigaction.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  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. /* do nothing */
  71. return 0;
  72. }
  73. int shim_do_sigprocmask (int how, const __sigset_t * set, __sigset_t * oldset)
  74. {
  75. __sigset_t * old, tmp, set_tmp;
  76. if (how != SIG_BLOCK && how != SIG_UNBLOCK &&
  77. how != SIG_SETMASK)
  78. return -EINVAL;
  79. if (set && test_user_memory((void *) set, sizeof(*set), false))
  80. return -EFAULT;
  81. if (oldset && test_user_memory(oldset, sizeof(*oldset), false))
  82. return -EFAULT;
  83. struct shim_thread * cur = get_cur_thread();
  84. int err = 0;
  85. lock(&cur->lock);
  86. old = get_sig_mask(cur);
  87. if (oldset) {
  88. memcpy(&tmp, old, sizeof(__sigset_t));
  89. old = &tmp;
  90. }
  91. /* if set is NULL, then the signal mask is unchanged, but the current
  92. value of the signal mask is nevertheless returned in oldset */
  93. if (!set)
  94. goto out;
  95. memcpy(&set_tmp, old, sizeof(__sigset_t));
  96. switch (how) {
  97. case SIG_BLOCK:
  98. __sigorset(&set_tmp, &set_tmp, set);
  99. break;
  100. case SIG_UNBLOCK:
  101. __signotset(&set_tmp, &set_tmp, set);
  102. break;
  103. case SIG_SETMASK:
  104. memcpy(&set_tmp, set, sizeof(__sigset_t));
  105. break;
  106. }
  107. set_sig_mask(cur, &set_tmp);
  108. out:
  109. unlock(&cur->lock);
  110. if (!err && oldset)
  111. memcpy(oldset, old, sizeof(__sigset_t));
  112. return err;
  113. }
  114. int shim_do_sigaltstack (const stack_t * ss, stack_t * oss)
  115. {
  116. if (ss && (ss->ss_flags & ~SS_DISABLE))
  117. return -EINVAL;
  118. struct shim_thread * cur = get_cur_thread();
  119. lock(&cur->lock);
  120. stack_t * cur_ss = &cur->signal_altstack;
  121. if (oss)
  122. *oss = *cur_ss;
  123. void * sp = shim_get_tls()->context.sp;
  124. /* check if thread is currently executing on an active altstack */
  125. if (!(cur_ss->ss_flags & SS_DISABLE) &&
  126. sp &&
  127. cur_ss->ss_sp <= sp &&
  128. sp < cur_ss->ss_sp + cur_ss->ss_size) {
  129. if (oss)
  130. oss->ss_flags |= SS_ONSTACK;
  131. if (ss) {
  132. unlock(&cur->lock);
  133. return -EPERM;
  134. }
  135. }
  136. if (ss) {
  137. if (ss->ss_flags & SS_DISABLE) {
  138. memset(cur_ss, 0, sizeof(*cur_ss));
  139. cur_ss->ss_flags = SS_DISABLE;
  140. } else {
  141. if (ss->ss_size < MINSIGSTKSZ) {
  142. unlock(&cur->lock);
  143. return -ENOMEM;
  144. }
  145. *cur_ss = *ss;
  146. }
  147. }
  148. unlock(&cur->lock);
  149. return 0;
  150. }
  151. int shim_do_sigsuspend (const __sigset_t * mask)
  152. {
  153. if (!mask || test_user_memory((void *) mask, sizeof(*mask), false))
  154. return -EFAULT;
  155. __sigset_t * old, tmp;
  156. struct shim_thread * cur = get_cur_thread();
  157. lock(&cur->lock);
  158. old = get_sig_mask(cur);
  159. memcpy(&tmp, old, sizeof(__sigset_t));
  160. old = &tmp;
  161. set_sig_mask(cur, mask);
  162. cur->suspend_on_signal = true;
  163. thread_setwait(NULL, NULL);
  164. thread_sleep(NO_TIMEOUT);
  165. unlock(&cur->lock);
  166. set_sig_mask(cur, old);
  167. return -EINTR;
  168. }
  169. int shim_do_sigpending (__sigset_t * set, size_t sigsetsize)
  170. {
  171. if (!set || test_user_memory(set, sizeof(*set), false))
  172. return -EFAULT;
  173. struct shim_thread * cur = get_cur_thread();
  174. __sigemptyset(set);
  175. if (!cur->signal_logs)
  176. return 0;
  177. for (int sig = 1 ; sig <= NUM_SIGS ; sig++) {
  178. if (atomic_read(&cur->signal_logs[sig - 1].head) !=
  179. atomic_read(&cur->signal_logs[sig - 1].tail))
  180. __sigaddset(set, sig);
  181. }
  182. return 0;
  183. }
  184. struct walk_arg {
  185. struct shim_thread * current;
  186. IDTYPE sender;
  187. IDTYPE id;
  188. int sig;
  189. bool use_ipc;
  190. };
  191. static inline void __append_signal (struct shim_thread * thread, int sig,
  192. IDTYPE sender)
  193. {
  194. debug("Thread %d killed by signal %d\n", thread->tid, sig);
  195. siginfo_t info;
  196. memset(&info, 0, sizeof(siginfo_t));
  197. info.si_signo = sig;
  198. info.si_pid = sender;
  199. append_signal(thread, sig, &info, true);
  200. }
  201. static int __kill_proc (struct shim_thread * thread, void * arg,
  202. bool * unlocked)
  203. {
  204. struct walk_arg * warg = (struct walk_arg *) arg;
  205. int srched = 0;
  206. if (!warg->use_ipc && !thread->in_vm)
  207. return 0;
  208. if (thread->tgid != warg->id)
  209. return 0;
  210. if (warg->current == thread)
  211. return 1;
  212. /* DEP: Let's do a racy read of is_alive and in_vm.
  213. * If either of these are zero it is a stable condition,
  214. * and we can elide the lock acquire (which helps perf).
  215. */
  216. if (!thread->is_alive)
  217. goto out;
  218. if (!thread->in_vm) {
  219. unlock(&thread_list_lock);
  220. *unlocked = true;
  221. return (!ipc_pid_kill_send(warg->sender, warg->id, KILL_PROCESS,
  222. warg->sig)) ? 1 : 0;
  223. } else {
  224. lock(&thread->lock);
  225. if (!thread->is_alive)
  226. goto out_locked;
  227. if (thread->in_vm) {
  228. if (warg->sig > 0)
  229. __append_signal(thread, warg->sig, warg->sender);
  230. srched = 1;
  231. } else {
  232. /* This double-check case is probably unnecessary, but keep it for now */
  233. unlock(&thread->lock);
  234. unlock(&thread_list_lock);
  235. *unlocked = true;
  236. return (!ipc_pid_kill_send(warg->sender, warg->id, KILL_PROCESS,
  237. warg->sig)) ? 1 : 0;
  238. }
  239. }
  240. out_locked:
  241. unlock(&thread->lock);
  242. out:
  243. return srched;
  244. }
  245. static int __kill_proc_simple (struct shim_simple_thread * sthread,
  246. void * arg, bool * unlocked)
  247. {
  248. struct walk_arg * warg = (struct walk_arg *) arg;
  249. int srched = 0;
  250. if (sthread->tgid != warg->id)
  251. return 0;
  252. lock(&sthread->lock);
  253. if (sthread->is_alive) {
  254. unlock(&sthread->lock);
  255. unlock(&thread_list_lock);
  256. *unlocked = true;
  257. return (!ipc_pid_kill_send(warg->sender, warg->id, KILL_PROCESS,
  258. warg->sig)) ? 1 : 0;
  259. }
  260. unlock(&sthread->lock);
  261. return srched;
  262. }
  263. int do_kill_proc (IDTYPE sender, IDTYPE tgid, int sig, bool use_ipc)
  264. {
  265. struct shim_thread * cur = get_cur_thread();
  266. if (!tgid) {
  267. /* DEP: cur->tgid never changes. No lock needed */
  268. tgid = cur->tgid;
  269. }
  270. struct walk_arg arg;
  271. arg.current = cur;
  272. arg.sender = sender;
  273. arg.id = tgid;
  274. arg.sig = sig;
  275. arg.use_ipc = use_ipc;
  276. bool srched = false;
  277. if (!walk_thread_list(__kill_proc, &arg, false))
  278. srched = true;
  279. if (!use_ipc || srched)
  280. goto out;
  281. if (!walk_simple_thread_list(__kill_proc_simple, &arg, false))
  282. srched = true;
  283. if (!srched && !ipc_pid_kill_send(sender, tgid, KILL_PROCESS, sig))
  284. srched = true;
  285. out:
  286. return srched ? 0 : -ESRCH;
  287. }
  288. static int __kill_pgroup (struct shim_thread * thread, void * arg,
  289. bool * unlocked)
  290. {
  291. struct walk_arg * warg = (struct walk_arg *) arg;
  292. int srched = 0;
  293. if (!warg->use_ipc && !thread->in_vm)
  294. return 0;
  295. if (thread->pgid != warg->id)
  296. return 0;
  297. if (warg->current == thread)
  298. return 1;
  299. lock(&thread->lock);
  300. if (!thread->is_alive)
  301. goto out;
  302. if (thread->in_vm) {
  303. if (warg->sig > 0)
  304. __append_signal(thread, warg->sig, warg->sender);
  305. srched = 1;
  306. } else {
  307. unlock(&thread->lock);
  308. unlock(&thread_list_lock);
  309. *unlocked = true;
  310. return (!ipc_pid_kill_send(warg->sender, warg->id, KILL_PGROUP,
  311. warg->sig)) ? 1 : 0;
  312. }
  313. out:
  314. unlock(&thread->lock);
  315. return srched;
  316. }
  317. static int __kill_pgroup_simple (struct shim_simple_thread * sthread,
  318. void * arg, bool * unlocked)
  319. {
  320. struct walk_arg * warg = (struct walk_arg *) arg;
  321. int srched = 0;
  322. if (sthread->pgid != warg->id)
  323. return 0;
  324. lock(&sthread->lock);
  325. if (sthread->is_alive) {
  326. unlock(&sthread->lock);
  327. unlock(&thread_list_lock);
  328. *unlocked = true;
  329. return (!ipc_pid_kill_send(warg->sender, warg->id, KILL_PGROUP,
  330. warg->sig)) ? 1 : 0;
  331. }
  332. unlock(&sthread->lock);
  333. return srched;
  334. }
  335. int do_kill_pgroup (IDTYPE sender, IDTYPE pgid, int sig, bool use_ipc)
  336. {
  337. struct shim_thread * cur = get_cur_thread();
  338. if (!pgid) {
  339. pgid = cur->pgid;
  340. }
  341. struct walk_arg arg;
  342. arg.current = cur;
  343. arg.sender = sender;
  344. arg.id = pgid;
  345. arg.sig = sig;
  346. arg.use_ipc = use_ipc;
  347. bool srched = false;
  348. if (!walk_thread_list(__kill_pgroup, &arg, false))
  349. srched = true;
  350. if (!use_ipc || srched)
  351. goto out;
  352. if (!walk_simple_thread_list(__kill_pgroup_simple, &arg, false))
  353. srched = true;
  354. if (!srched && !ipc_pid_kill_send(sender, pgid, KILL_PGROUP, sig))
  355. srched = true;
  356. out:
  357. return srched ? 0 : -ESRCH;
  358. }
  359. static int __kill_all_threads (struct shim_thread * thread, void * arg,
  360. bool * unlocked)
  361. {
  362. int srched = 0;
  363. struct walk_arg * warg = (struct walk_arg *) arg;
  364. if (thread->tgid != thread->tid)
  365. return 0;
  366. if (warg->current == thread)
  367. return 1;
  368. lock(&thread->lock);
  369. if (thread->in_vm) {
  370. __append_signal(thread, warg->sig, warg->sender);
  371. srched = 1;
  372. }
  373. unlock(&thread->lock);
  374. return srched;
  375. }
  376. int broadcast_signal (IDTYPE sender, int sig);
  377. int kill_all_threads (struct shim_thread * cur, IDTYPE sender, int sig)
  378. {
  379. struct walk_arg arg;
  380. arg.current = cur;
  381. arg.sender = sender;
  382. arg.id = 0;
  383. arg.sig = sig;
  384. arg.use_ipc = false;
  385. walk_thread_list(__kill_all_threads, &arg, false);
  386. return 0;
  387. }
  388. int shim_do_kill (pid_t pid, int sig)
  389. {
  390. INC_PROFILE_OCCURENCE(syscall_use_ipc);
  391. if (sig < 0 || sig > NUM_SIGS)
  392. return -EINVAL;
  393. struct shim_thread * cur = get_cur_thread();
  394. int ret = 0;
  395. bool send_to_self = false;
  396. /* If pid equals 0, then sig is sent to every process in the process group
  397. of the calling process. */
  398. if (pid == 0) {
  399. ret = do_kill_pgroup(cur->tgid, 0, sig, true);
  400. send_to_self = true;
  401. }
  402. /* If pid equals -1, then sig is sent to every process for which the
  403. calling process has permission to send */
  404. else if (pid == -1) {
  405. broadcast_signal(cur->tid, sig);
  406. kill_all_threads(cur, cur->tid, sig);
  407. send_to_self = true;
  408. }
  409. /* If pid is positive, then signal sig is sent to the process with the ID
  410. specified by pid. */
  411. else if (pid > 0) {
  412. ret = do_kill_proc(cur->tid, pid, sig, true);
  413. send_to_self = (pid == cur->tgid);
  414. }
  415. /* If pid is less than -1, then sig is sent to every process in the
  416. process group whose id is -pid */
  417. else {
  418. ret = do_kill_pgroup(cur->tid, -pid, sig, true);
  419. send_to_self = (-pid == cur->pgid);
  420. }
  421. if (send_to_self) {
  422. if (ret == -ESRCH)
  423. ret = 0;
  424. if (sig) {
  425. siginfo_t info;
  426. memset(&info, 0, sizeof(siginfo_t));
  427. info.si_signo = sig;
  428. info.si_pid = cur->tid;
  429. deliver_signal(&info, NULL);
  430. }
  431. }
  432. return ret < 0 ? ret : 0;
  433. }
  434. int do_kill_thread (IDTYPE sender, IDTYPE tgid, IDTYPE tid, int sig,
  435. bool use_ipc)
  436. {
  437. if (sig < 0 || sig > NUM_SIGS)
  438. return -EINVAL;
  439. struct shim_thread * thread = lookup_thread(tid);
  440. int ret = 0;
  441. if (thread) {
  442. lock(&thread->lock);
  443. if (thread->in_vm) {
  444. if (!tgid || thread->tgid == tgid)
  445. __append_signal(thread, sig, sender);
  446. else
  447. ret = -ESRCH;
  448. } else {
  449. unlock(&thread->lock);
  450. return ipc_pid_kill_send(sender, tid, KILL_THREAD, sig);
  451. }
  452. unlock(&thread->lock);
  453. return ret;
  454. }
  455. if (!use_ipc)
  456. return -ESRCH;
  457. return ipc_pid_kill_send(sender, tid, KILL_THREAD, sig);
  458. }
  459. int shim_do_tkill (pid_t tid, int sig)
  460. {
  461. INC_PROFILE_OCCURENCE(syscall_use_ipc);
  462. if (tid <= 0)
  463. return -EINVAL;
  464. struct shim_thread * cur = get_cur_thread();
  465. if (tid == cur->tid) {
  466. if (sig) {
  467. siginfo_t info;
  468. memset(&info, 0, sizeof(siginfo_t));
  469. info.si_signo = sig;
  470. info.si_pid = cur->tid;
  471. deliver_signal(&info, NULL);
  472. }
  473. return 0;
  474. }
  475. return do_kill_thread(cur->tgid, 0, tid, sig, true);
  476. }
  477. int shim_do_tgkill (pid_t tgid, pid_t tid, int sig)
  478. {
  479. INC_PROFILE_OCCURENCE(syscall_use_ipc);
  480. if (tgid < -1 || tgid == 0 || tid <= 0)
  481. return -EINVAL;
  482. if (tgid == -1)
  483. tgid = 0;
  484. struct shim_thread * cur = get_cur_thread();
  485. if (tid == cur->tid) {
  486. if (sig) {
  487. siginfo_t info;
  488. memset(&info, 0, sizeof(siginfo_t));
  489. info.si_signo = sig;
  490. info.si_pid = cur->tid;
  491. deliver_signal(&info, NULL);
  492. }
  493. return 0;
  494. }
  495. return do_kill_thread(cur->tgid, tgid, tid, sig, true);
  496. }