signal_32.c 15 KB

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