signal_compat.c 6.3 KB

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