signal_compat.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. #include <linux/compat.h>
  2. #include <linux/uaccess.h>
  3. #include <linux/ptrace.h>
  4. /*
  5. * The compat_siginfo_t structure and handing code is very easy
  6. * to break in several ways. It must always be updated when new
  7. * updates are made to the main siginfo_t, and
  8. * copy_siginfo_to_user32() must be updated when the
  9. * (arch-independent) copy_siginfo_to_user() is updated.
  10. *
  11. * It is also easy to put a new member in the compat_siginfo_t
  12. * which has implicit alignment which can move internal structure
  13. * alignment around breaking the ABI. This can happen if you,
  14. * for instance, put a plain 64-bit value in there.
  15. */
  16. static inline void signal_compat_build_tests(void)
  17. {
  18. int _sifields_offset = offsetof(compat_siginfo_t, _sifields);
  19. /*
  20. * If adding a new si_code, there is probably new data in
  21. * the siginfo. Make sure folks bumping the si_code
  22. * limits also have to look at this code. Make sure any
  23. * new fields are handled in copy_siginfo_to_user32()!
  24. */
  25. BUILD_BUG_ON(NSIGILL != 8);
  26. BUILD_BUG_ON(NSIGFPE != 8);
  27. BUILD_BUG_ON(NSIGSEGV != 4);
  28. BUILD_BUG_ON(NSIGBUS != 5);
  29. BUILD_BUG_ON(NSIGTRAP != 4);
  30. BUILD_BUG_ON(NSIGCHLD != 6);
  31. BUILD_BUG_ON(NSIGSYS != 1);
  32. /* This is part of the ABI and can never change in size: */
  33. BUILD_BUG_ON(sizeof(compat_siginfo_t) != 128);
  34. /*
  35. * The offsets of all the (unioned) si_fields are fixed
  36. * in the ABI, of course. Make sure none of them ever
  37. * move and are always at the beginning:
  38. */
  39. BUILD_BUG_ON(offsetof(compat_siginfo_t, _sifields) != 3 * sizeof(int));
  40. #define CHECK_CSI_OFFSET(name) BUILD_BUG_ON(_sifields_offset != offsetof(compat_siginfo_t, _sifields.name))
  41. /*
  42. * Ensure that the size of each si_field never changes.
  43. * If it does, it is a sign that the
  44. * copy_siginfo_to_user32() code below needs to updated
  45. * along with the size in the CHECK_SI_SIZE().
  46. *
  47. * We repeat this check for both the generic and compat
  48. * siginfos.
  49. *
  50. * Note: it is OK for these to grow as long as the whole
  51. * structure stays within the padding size (checked
  52. * above).
  53. */
  54. #define CHECK_CSI_SIZE(name, size) BUILD_BUG_ON(size != sizeof(((compat_siginfo_t *)0)->_sifields.name))
  55. #define CHECK_SI_SIZE(name, size) BUILD_BUG_ON(size != sizeof(((siginfo_t *)0)->_sifields.name))
  56. CHECK_CSI_OFFSET(_kill);
  57. CHECK_CSI_SIZE (_kill, 2*sizeof(int));
  58. CHECK_SI_SIZE (_kill, 2*sizeof(int));
  59. CHECK_CSI_OFFSET(_timer);
  60. CHECK_CSI_SIZE (_timer, 5*sizeof(int));
  61. CHECK_SI_SIZE (_timer, 6*sizeof(int));
  62. CHECK_CSI_OFFSET(_rt);
  63. CHECK_CSI_SIZE (_rt, 3*sizeof(int));
  64. CHECK_SI_SIZE (_rt, 4*sizeof(int));
  65. CHECK_CSI_OFFSET(_sigchld);
  66. CHECK_CSI_SIZE (_sigchld, 5*sizeof(int));
  67. CHECK_SI_SIZE (_sigchld, 8*sizeof(int));
  68. CHECK_CSI_OFFSET(_sigchld_x32);
  69. CHECK_CSI_SIZE (_sigchld_x32, 7*sizeof(int));
  70. /* no _sigchld_x32 in the generic siginfo_t */
  71. CHECK_CSI_OFFSET(_sigfault);
  72. CHECK_CSI_SIZE (_sigfault, 4*sizeof(int));
  73. CHECK_SI_SIZE (_sigfault, 8*sizeof(int));
  74. CHECK_CSI_OFFSET(_sigpoll);
  75. CHECK_CSI_SIZE (_sigpoll, 2*sizeof(int));
  76. CHECK_SI_SIZE (_sigpoll, 4*sizeof(int));
  77. CHECK_CSI_OFFSET(_sigsys);
  78. CHECK_CSI_SIZE (_sigsys, 3*sizeof(int));
  79. CHECK_SI_SIZE (_sigsys, 4*sizeof(int));
  80. /* any new si_fields should be added here */
  81. }
  82. void sigaction_compat_abi(struct k_sigaction *act, struct k_sigaction *oact)
  83. {
  84. /* Don't leak in-kernel non-uapi flags to user-space */
  85. if (oact)
  86. oact->sa.sa_flags &= ~(SA_IA32_ABI | SA_X32_ABI);
  87. if (!act)
  88. return;
  89. /* Don't let flags to be set from userspace */
  90. act->sa.sa_flags &= ~(SA_IA32_ABI | SA_X32_ABI);
  91. if (in_ia32_syscall())
  92. act->sa.sa_flags |= SA_IA32_ABI;
  93. if (in_x32_syscall())
  94. act->sa.sa_flags |= SA_X32_ABI;
  95. }
  96. int __copy_siginfo_to_user32(compat_siginfo_t __user *to, const siginfo_t *from,
  97. bool x32_ABI)
  98. {
  99. int err = 0;
  100. signal_compat_build_tests();
  101. if (!access_ok(VERIFY_WRITE, to, sizeof(compat_siginfo_t)))
  102. return -EFAULT;
  103. put_user_try {
  104. /* If you change siginfo_t structure, please make sure that
  105. this code is fixed accordingly.
  106. It should never copy any pad contained in the structure
  107. to avoid security leaks, but must copy the generic
  108. 3 ints plus the relevant union member. */
  109. put_user_ex(from->si_signo, &to->si_signo);
  110. put_user_ex(from->si_errno, &to->si_errno);
  111. put_user_ex((short)from->si_code, &to->si_code);
  112. if (from->si_code < 0) {
  113. put_user_ex(from->si_pid, &to->si_pid);
  114. put_user_ex(from->si_uid, &to->si_uid);
  115. put_user_ex(ptr_to_compat(from->si_ptr), &to->si_ptr);
  116. } else {
  117. /*
  118. * First 32bits of unions are always present:
  119. * si_pid === si_band === si_tid === si_addr(LS half)
  120. */
  121. put_user_ex(from->_sifields._pad[0],
  122. &to->_sifields._pad[0]);
  123. switch (from->si_code >> 16) {
  124. case __SI_FAULT >> 16:
  125. if (from->si_signo == SIGBUS &&
  126. (from->si_code == BUS_MCEERR_AR ||
  127. from->si_code == BUS_MCEERR_AO))
  128. put_user_ex(from->si_addr_lsb, &to->si_addr_lsb);
  129. if (from->si_signo == SIGSEGV) {
  130. if (from->si_code == SEGV_BNDERR) {
  131. compat_uptr_t lower = (unsigned long)from->si_lower;
  132. compat_uptr_t upper = (unsigned long)from->si_upper;
  133. put_user_ex(lower, &to->si_lower);
  134. put_user_ex(upper, &to->si_upper);
  135. }
  136. if (from->si_code == SEGV_PKUERR)
  137. put_user_ex(from->si_pkey, &to->si_pkey);
  138. }
  139. break;
  140. case __SI_SYS >> 16:
  141. put_user_ex(from->si_syscall, &to->si_syscall);
  142. put_user_ex(from->si_arch, &to->si_arch);
  143. break;
  144. case __SI_CHLD >> 16:
  145. if (!x32_ABI) {
  146. put_user_ex(from->si_utime, &to->si_utime);
  147. put_user_ex(from->si_stime, &to->si_stime);
  148. } else {
  149. put_user_ex(from->si_utime, &to->_sifields._sigchld_x32._utime);
  150. put_user_ex(from->si_stime, &to->_sifields._sigchld_x32._stime);
  151. }
  152. put_user_ex(from->si_status, &to->si_status);
  153. /* FALL THROUGH */
  154. default:
  155. case __SI_KILL >> 16:
  156. put_user_ex(from->si_uid, &to->si_uid);
  157. break;
  158. case __SI_POLL >> 16:
  159. put_user_ex(from->si_fd, &to->si_fd);
  160. break;
  161. case __SI_TIMER >> 16:
  162. put_user_ex(from->si_overrun, &to->si_overrun);
  163. put_user_ex(ptr_to_compat(from->si_ptr),
  164. &to->si_ptr);
  165. break;
  166. /* This is not generated by the kernel as of now. */
  167. case __SI_RT >> 16:
  168. case __SI_MESGQ >> 16:
  169. put_user_ex(from->si_uid, &to->si_uid);
  170. put_user_ex(from->si_int, &to->si_int);
  171. break;
  172. }
  173. }
  174. } put_user_catch(err);
  175. return err;
  176. }
  177. /* from syscall's path, where we know the ABI */
  178. int copy_siginfo_to_user32(compat_siginfo_t __user *to, const siginfo_t *from)
  179. {
  180. return __copy_siginfo_to_user32(to, from, in_x32_syscall());
  181. }
  182. int copy_siginfo_from_user32(siginfo_t *to, compat_siginfo_t __user *from)
  183. {
  184. int err = 0;
  185. u32 ptr32;
  186. if (!access_ok(VERIFY_READ, from, sizeof(compat_siginfo_t)))
  187. return -EFAULT;
  188. get_user_try {
  189. get_user_ex(to->si_signo, &from->si_signo);
  190. get_user_ex(to->si_errno, &from->si_errno);
  191. get_user_ex(to->si_code, &from->si_code);
  192. get_user_ex(to->si_pid, &from->si_pid);
  193. get_user_ex(to->si_uid, &from->si_uid);
  194. get_user_ex(ptr32, &from->si_ptr);
  195. to->si_ptr = compat_ptr(ptr32);
  196. } get_user_catch(err);
  197. return err;
  198. }