signal_32.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 1991, 1992 Linus Torvalds
  4. *
  5. * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson
  6. * 2000-06-20 Pentium III FXSR, SSE support by Gareth Hughes
  7. * 2000-12-* x86-64 compatibility mode signal handling by Andi Kleen
  8. */
  9. #include <linux/sched.h>
  10. #include <linux/sched/task_stack.h>
  11. #include <linux/mm.h>
  12. #include <linux/smp.h>
  13. #include <linux/kernel.h>
  14. #include <linux/errno.h>
  15. #include <linux/wait.h>
  16. #include <linux/unistd.h>
  17. #include <linux/stddef.h>
  18. #include <linux/personality.h>
  19. #include <linux/compat.h>
  20. #include <linux/binfmts.h>
  21. #include <linux/syscalls.h>
  22. #include <asm/ucontext.h>
  23. #include <linux/uaccess.h>
  24. #include <asm/fpu/signal.h>
  25. #include <asm/ptrace.h>
  26. #include <asm/user32.h>
  27. #include <uapi/asm/sigcontext.h>
  28. #include <asm/proto.h>
  29. #include <asm/vdso.h>
  30. #include <asm/sigframe.h>
  31. #include <asm/sighandling.h>
  32. #include <asm/smap.h>
  33. #include <asm/gsseg.h>
  34. #ifdef CONFIG_IA32_EMULATION
  35. #include <asm/unistd_32_ia32.h>
  36. static inline void reload_segments(struct sigcontext_32 *sc)
  37. {
  38. unsigned int cur;
  39. savesegment(gs, cur);
  40. if ((sc->gs | 0x03) != cur)
  41. load_gs_index(sc->gs | 0x03);
  42. savesegment(fs, cur);
  43. if ((sc->fs | 0x03) != cur)
  44. loadsegment(fs, sc->fs | 0x03);
  45. savesegment(ds, cur);
  46. if ((sc->ds | 0x03) != cur)
  47. loadsegment(ds, sc->ds | 0x03);
  48. savesegment(es, cur);
  49. if ((sc->es | 0x03) != cur)
  50. loadsegment(es, sc->es | 0x03);
  51. }
  52. #define sigset32_t compat_sigset_t
  53. #define siginfo32_t compat_siginfo_t
  54. #define restore_altstack32 compat_restore_altstack
  55. #define unsafe_save_altstack32 unsafe_compat_save_altstack
  56. #else
  57. #define sigset32_t sigset_t
  58. #define siginfo32_t siginfo_t
  59. #define __NR_ia32_sigreturn __NR_sigreturn
  60. #define __NR_ia32_rt_sigreturn __NR_rt_sigreturn
  61. #define restore_altstack32 restore_altstack
  62. #define unsafe_save_altstack32 unsafe_save_altstack
  63. #define __copy_siginfo_to_user32 copy_siginfo_to_user
  64. #endif
  65. /*
  66. * Do a signal return; undo the signal stack.
  67. */
  68. static bool ia32_restore_sigcontext(struct pt_regs *regs,
  69. struct sigcontext_32 __user *usc)
  70. {
  71. struct sigcontext_32 sc;
  72. /* Always make any pending restarted system calls return -EINTR */
  73. current->restart_block.fn = do_no_restart_syscall;
  74. if (unlikely(copy_from_user(&sc, usc, sizeof(sc))))
  75. return false;
  76. /* Get only the ia32 registers. */
  77. regs->bx = sc.bx;
  78. regs->cx = sc.cx;
  79. regs->dx = sc.dx;
  80. regs->si = sc.si;
  81. regs->di = sc.di;
  82. regs->bp = sc.bp;
  83. regs->ax = sc.ax;
  84. regs->sp = sc.sp;
  85. regs->ip = sc.ip;
  86. /* Get CS/SS and force CPL3 */
  87. regs->cs = sc.cs | 0x03;
  88. regs->ss = sc.ss | 0x03;
  89. regs->flags = (regs->flags & ~FIX_EFLAGS) | (sc.flags & FIX_EFLAGS);
  90. /* disable syscall checks */
  91. regs->orig_ax = -1;
  92. #ifdef CONFIG_IA32_EMULATION
  93. /*
  94. * Reload fs and gs if they have changed in the signal
  95. * handler. This does not handle long fs/gs base changes in
  96. * the handler, but does not clobber them at least in the
  97. * normal case.
  98. */
  99. reload_segments(&sc);
  100. #else
  101. loadsegment(gs, sc.gs);
  102. regs->fs = sc.fs;
  103. regs->es = sc.es;
  104. regs->ds = sc.ds;
  105. #endif
  106. return fpu__restore_sig(compat_ptr(sc.fpstate), 1);
  107. }
  108. SYSCALL32_DEFINE0(sigreturn)
  109. {
  110. struct pt_regs *regs = current_pt_regs();
  111. struct sigframe_ia32 __user *frame = (struct sigframe_ia32 __user *)(regs->sp-8);
  112. sigset_t set;
  113. if (!access_ok(frame, sizeof(*frame)))
  114. goto badframe;
  115. if (__get_user(set.sig[0], &frame->sc.oldmask)
  116. || __get_user(((__u32 *)&set)[1], &frame->extramask[0]))
  117. goto badframe;
  118. set_current_blocked(&set);
  119. if (!ia32_restore_sigcontext(regs, &frame->sc))
  120. goto badframe;
  121. return regs->ax;
  122. badframe:
  123. signal_fault(regs, frame, "32bit sigreturn");
  124. return 0;
  125. }
  126. SYSCALL32_DEFINE0(rt_sigreturn)
  127. {
  128. struct pt_regs *regs = current_pt_regs();
  129. struct rt_sigframe_ia32 __user *frame;
  130. sigset_t set;
  131. frame = (struct rt_sigframe_ia32 __user *)(regs->sp - 4);
  132. if (!access_ok(frame, sizeof(*frame)))
  133. goto badframe;
  134. if (__get_user(*(__u64 *)&set, (__u64 __user *)&frame->uc.uc_sigmask))
  135. goto badframe;
  136. set_current_blocked(&set);
  137. if (!ia32_restore_sigcontext(regs, &frame->uc.uc_mcontext))
  138. goto badframe;
  139. if (restore_altstack32(&frame->uc.uc_stack))
  140. goto badframe;
  141. return regs->ax;
  142. badframe:
  143. signal_fault(regs, frame, "32bit rt sigreturn");
  144. return 0;
  145. }
  146. /*
  147. * Set up a signal frame.
  148. */
  149. #define get_user_seg(seg) ({ unsigned int v; savesegment(seg, v); v; })
  150. static __always_inline int
  151. __unsafe_setup_sigcontext32(struct sigcontext_32 __user *sc,
  152. void __user *fpstate,
  153. struct pt_regs *regs, unsigned int mask)
  154. {
  155. unsafe_put_user(get_user_seg(gs), (unsigned int __user *)&sc->gs, Efault);
  156. #ifdef CONFIG_IA32_EMULATION
  157. unsafe_put_user(get_user_seg(fs), (unsigned int __user *)&sc->fs, Efault);
  158. unsafe_put_user(get_user_seg(ds), (unsigned int __user *)&sc->ds, Efault);
  159. unsafe_put_user(get_user_seg(es), (unsigned int __user *)&sc->es, Efault);
  160. #else
  161. unsafe_put_user(regs->fs, (unsigned int __user *)&sc->fs, Efault);
  162. unsafe_put_user(regs->es, (unsigned int __user *)&sc->es, Efault);
  163. unsafe_put_user(regs->ds, (unsigned int __user *)&sc->ds, Efault);
  164. #endif
  165. unsafe_put_user(regs->di, &sc->di, Efault);
  166. unsafe_put_user(regs->si, &sc->si, Efault);
  167. unsafe_put_user(regs->bp, &sc->bp, Efault);
  168. unsafe_put_user(regs->sp, &sc->sp, Efault);
  169. unsafe_put_user(regs->bx, &sc->bx, Efault);
  170. unsafe_put_user(regs->dx, &sc->dx, Efault);
  171. unsafe_put_user(regs->cx, &sc->cx, Efault);
  172. unsafe_put_user(regs->ax, &sc->ax, Efault);
  173. unsafe_put_user(current->thread.trap_nr, &sc->trapno, Efault);
  174. unsafe_put_user(current->thread.error_code, &sc->err, Efault);
  175. unsafe_put_user(regs->ip, &sc->ip, Efault);
  176. unsafe_put_user(regs->cs, (unsigned int __user *)&sc->cs, Efault);
  177. unsafe_put_user(regs->flags, &sc->flags, Efault);
  178. unsafe_put_user(regs->sp, &sc->sp_at_signal, Efault);
  179. unsafe_put_user(regs->ss, (unsigned int __user *)&sc->ss, Efault);
  180. unsafe_put_user(ptr_to_compat(fpstate), &sc->fpstate, Efault);
  181. /* non-iBCS2 extensions.. */
  182. unsafe_put_user(mask, &sc->oldmask, Efault);
  183. unsafe_put_user(current->thread.cr2, &sc->cr2, Efault);
  184. return 0;
  185. Efault:
  186. return -EFAULT;
  187. }
  188. #define unsafe_put_sigcontext32(sc, fp, regs, set, label) \
  189. do { \
  190. if (__unsafe_setup_sigcontext32(sc, fp, regs, set->sig[0])) \
  191. goto label; \
  192. } while(0)
  193. int ia32_setup_frame(struct ksignal *ksig, struct pt_regs *regs)
  194. {
  195. sigset32_t *set = (sigset32_t *) sigmask_to_save();
  196. struct sigframe_ia32 __user *frame;
  197. void __user *restorer;
  198. void __user *fp = NULL;
  199. /* copy_to_user optimizes that into a single 8 byte store */
  200. static const struct {
  201. u16 poplmovl;
  202. u32 val;
  203. u16 int80;
  204. } __attribute__((packed)) code = {
  205. 0xb858, /* popl %eax ; movl $...,%eax */
  206. __NR_ia32_sigreturn,
  207. 0x80cd, /* int $0x80 */
  208. };
  209. frame = get_sigframe(ksig, regs, sizeof(*frame), &fp);
  210. if (ksig->ka.sa.sa_flags & SA_RESTORER) {
  211. restorer = ksig->ka.sa.sa_restorer;
  212. } else {
  213. /* Return stub is in 32bit vsyscall page */
  214. if (current->mm->context.vdso)
  215. restorer = current->mm->context.vdso +
  216. vdso_image_32.sym___kernel_sigreturn;
  217. else
  218. restorer = &frame->retcode;
  219. }
  220. if (!user_access_begin(frame, sizeof(*frame)))
  221. return -EFAULT;
  222. unsafe_put_user(ksig->sig, &frame->sig, Efault);
  223. unsafe_put_sigcontext32(&frame->sc, fp, regs, set, Efault);
  224. unsafe_put_user(set->sig[1], &frame->extramask[0], Efault);
  225. unsafe_put_user(ptr_to_compat(restorer), &frame->pretcode, Efault);
  226. /*
  227. * These are actually not used anymore, but left because some
  228. * gdb versions depend on them as a marker.
  229. */
  230. unsafe_put_user(*((u64 *)&code), (u64 __user *)frame->retcode, Efault);
  231. user_access_end();
  232. /* Set up registers for signal handler */
  233. regs->sp = (unsigned long) frame;
  234. regs->ip = (unsigned long) ksig->ka.sa.sa_handler;
  235. /* Make -mregparm=3 work */
  236. regs->ax = ksig->sig;
  237. regs->dx = 0;
  238. regs->cx = 0;
  239. #ifdef CONFIG_IA32_EMULATION
  240. loadsegment(ds, __USER_DS);
  241. loadsegment(es, __USER_DS);
  242. #else
  243. regs->ds = __USER_DS;
  244. regs->es = __USER_DS;
  245. #endif
  246. regs->cs = __USER32_CS;
  247. regs->ss = __USER_DS;
  248. return 0;
  249. Efault:
  250. user_access_end();
  251. return -EFAULT;
  252. }
  253. int ia32_setup_rt_frame(struct ksignal *ksig, struct pt_regs *regs)
  254. {
  255. sigset32_t *set = (sigset32_t *) sigmask_to_save();
  256. struct rt_sigframe_ia32 __user *frame;
  257. void __user *restorer;
  258. void __user *fp = NULL;
  259. /* unsafe_put_user optimizes that into a single 8 byte store */
  260. static const struct {
  261. u8 movl;
  262. u32 val;
  263. u16 int80;
  264. u8 pad;
  265. } __attribute__((packed)) code = {
  266. 0xb8,
  267. __NR_ia32_rt_sigreturn,
  268. 0x80cd,
  269. 0,
  270. };
  271. frame = get_sigframe(ksig, regs, sizeof(*frame), &fp);
  272. if (!user_access_begin(frame, sizeof(*frame)))
  273. return -EFAULT;
  274. unsafe_put_user(ksig->sig, &frame->sig, Efault);
  275. unsafe_put_user(ptr_to_compat(&frame->info), &frame->pinfo, Efault);
  276. unsafe_put_user(ptr_to_compat(&frame->uc), &frame->puc, Efault);
  277. /* Create the ucontext. */
  278. if (static_cpu_has(X86_FEATURE_XSAVE))
  279. unsafe_put_user(UC_FP_XSTATE, &frame->uc.uc_flags, Efault);
  280. else
  281. unsafe_put_user(0, &frame->uc.uc_flags, Efault);
  282. unsafe_put_user(0, &frame->uc.uc_link, Efault);
  283. unsafe_save_altstack32(&frame->uc.uc_stack, regs->sp, Efault);
  284. if (ksig->ka.sa.sa_flags & SA_RESTORER)
  285. restorer = ksig->ka.sa.sa_restorer;
  286. else
  287. restorer = current->mm->context.vdso +
  288. vdso_image_32.sym___kernel_rt_sigreturn;
  289. unsafe_put_user(ptr_to_compat(restorer), &frame->pretcode, Efault);
  290. /*
  291. * Not actually used anymore, but left because some gdb
  292. * versions need it.
  293. */
  294. unsafe_put_user(*((u64 *)&code), (u64 __user *)frame->retcode, Efault);
  295. unsafe_put_sigcontext32(&frame->uc.uc_mcontext, fp, regs, set, Efault);
  296. unsafe_put_user(*(__u64 *)set, (__u64 __user *)&frame->uc.uc_sigmask, Efault);
  297. user_access_end();
  298. if (__copy_siginfo_to_user32(&frame->info, &ksig->info))
  299. return -EFAULT;
  300. /* Set up registers for signal handler */
  301. regs->sp = (unsigned long) frame;
  302. regs->ip = (unsigned long) ksig->ka.sa.sa_handler;
  303. /* Make -mregparm=3 work */
  304. regs->ax = ksig->sig;
  305. regs->dx = (unsigned long) &frame->info;
  306. regs->cx = (unsigned long) &frame->uc;
  307. #ifdef CONFIG_IA32_EMULATION
  308. loadsegment(ds, __USER_DS);
  309. loadsegment(es, __USER_DS);
  310. #else
  311. regs->ds = __USER_DS;
  312. regs->es = __USER_DS;
  313. #endif
  314. regs->cs = __USER32_CS;
  315. regs->ss = __USER_DS;
  316. return 0;
  317. Efault:
  318. user_access_end();
  319. return -EFAULT;
  320. }
  321. /*
  322. * The siginfo_t structure and handing code is very easy
  323. * to break in several ways. It must always be updated when new
  324. * updates are made to the main siginfo_t, and
  325. * copy_siginfo_to_user32() must be updated when the
  326. * (arch-independent) copy_siginfo_to_user() is updated.
  327. *
  328. * It is also easy to put a new member in the siginfo_t
  329. * which has implicit alignment which can move internal structure
  330. * alignment around breaking the ABI. This can happen if you,
  331. * for instance, put a plain 64-bit value in there.
  332. */
  333. /*
  334. * If adding a new si_code, there is probably new data in
  335. * the siginfo. Make sure folks bumping the si_code
  336. * limits also have to look at this code. Make sure any
  337. * new fields are handled in copy_siginfo_to_user32()!
  338. */
  339. static_assert(NSIGILL == 11);
  340. static_assert(NSIGFPE == 15);
  341. static_assert(NSIGSEGV == 10);
  342. static_assert(NSIGBUS == 5);
  343. static_assert(NSIGTRAP == 6);
  344. static_assert(NSIGCHLD == 6);
  345. static_assert(NSIGSYS == 2);
  346. /* This is part of the ABI and can never change in size: */
  347. static_assert(sizeof(siginfo32_t) == 128);
  348. /* This is a part of the ABI and can never change in alignment */
  349. static_assert(__alignof__(siginfo32_t) == 4);
  350. /*
  351. * The offsets of all the (unioned) si_fields are fixed
  352. * in the ABI, of course. Make sure none of them ever
  353. * move and are always at the beginning:
  354. */
  355. static_assert(offsetof(siginfo32_t, _sifields) == 3 * sizeof(int));
  356. static_assert(offsetof(siginfo32_t, si_signo) == 0);
  357. static_assert(offsetof(siginfo32_t, si_errno) == 4);
  358. static_assert(offsetof(siginfo32_t, si_code) == 8);
  359. /*
  360. * Ensure that the size of each si_field never changes.
  361. * If it does, it is a sign that the
  362. * copy_siginfo_to_user32() code below needs to updated
  363. * along with the size in the CHECK_SI_SIZE().
  364. *
  365. * We repeat this check for both the generic and compat
  366. * siginfos.
  367. *
  368. * Note: it is OK for these to grow as long as the whole
  369. * structure stays within the padding size (checked
  370. * above).
  371. */
  372. #define CHECK_SI_OFFSET(name) \
  373. static_assert(offsetof(siginfo32_t, _sifields) == \
  374. offsetof(siginfo32_t, _sifields.name))
  375. #define CHECK_SI_SIZE(name, size) \
  376. static_assert(sizeof_field(siginfo32_t, _sifields.name) == size)
  377. CHECK_SI_OFFSET(_kill);
  378. CHECK_SI_SIZE (_kill, 2*sizeof(int));
  379. static_assert(offsetof(siginfo32_t, si_pid) == 0xC);
  380. static_assert(offsetof(siginfo32_t, si_uid) == 0x10);
  381. CHECK_SI_OFFSET(_timer);
  382. #ifdef CONFIG_COMPAT
  383. /* compat_siginfo_t doesn't have si_sys_private */
  384. CHECK_SI_SIZE (_timer, 3*sizeof(int));
  385. #else
  386. CHECK_SI_SIZE (_timer, 4*sizeof(int));
  387. #endif
  388. static_assert(offsetof(siginfo32_t, si_tid) == 0x0C);
  389. static_assert(offsetof(siginfo32_t, si_overrun) == 0x10);
  390. static_assert(offsetof(siginfo32_t, si_value) == 0x14);
  391. CHECK_SI_OFFSET(_rt);
  392. CHECK_SI_SIZE (_rt, 3*sizeof(int));
  393. static_assert(offsetof(siginfo32_t, si_pid) == 0x0C);
  394. static_assert(offsetof(siginfo32_t, si_uid) == 0x10);
  395. static_assert(offsetof(siginfo32_t, si_value) == 0x14);
  396. CHECK_SI_OFFSET(_sigchld);
  397. CHECK_SI_SIZE (_sigchld, 5*sizeof(int));
  398. static_assert(offsetof(siginfo32_t, si_pid) == 0x0C);
  399. static_assert(offsetof(siginfo32_t, si_uid) == 0x10);
  400. static_assert(offsetof(siginfo32_t, si_status) == 0x14);
  401. static_assert(offsetof(siginfo32_t, si_utime) == 0x18);
  402. static_assert(offsetof(siginfo32_t, si_stime) == 0x1C);
  403. CHECK_SI_OFFSET(_sigfault);
  404. CHECK_SI_SIZE (_sigfault, 4*sizeof(int));
  405. static_assert(offsetof(siginfo32_t, si_addr) == 0x0C);
  406. static_assert(offsetof(siginfo32_t, si_trapno) == 0x10);
  407. static_assert(offsetof(siginfo32_t, si_addr_lsb) == 0x10);
  408. static_assert(offsetof(siginfo32_t, si_lower) == 0x14);
  409. static_assert(offsetof(siginfo32_t, si_upper) == 0x18);
  410. static_assert(offsetof(siginfo32_t, si_pkey) == 0x14);
  411. static_assert(offsetof(siginfo32_t, si_perf_data) == 0x10);
  412. static_assert(offsetof(siginfo32_t, si_perf_type) == 0x14);
  413. static_assert(offsetof(siginfo32_t, si_perf_flags) == 0x18);
  414. CHECK_SI_OFFSET(_sigpoll);
  415. CHECK_SI_SIZE (_sigpoll, 2*sizeof(int));
  416. static_assert(offsetof(siginfo32_t, si_band) == 0x0C);
  417. static_assert(offsetof(siginfo32_t, si_fd) == 0x10);
  418. CHECK_SI_OFFSET(_sigsys);
  419. CHECK_SI_SIZE (_sigsys, 3*sizeof(int));
  420. static_assert(offsetof(siginfo32_t, si_call_addr) == 0x0C);
  421. static_assert(offsetof(siginfo32_t, si_syscall) == 0x10);
  422. static_assert(offsetof(siginfo32_t, si_arch) == 0x14);
  423. /* any new si_fields should be added here */