signal_compat.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/compat.h>
  3. #include <linux/uaccess.h>
  4. #include <linux/ptrace.h>
  5. /*
  6. * The compat_siginfo_t structure and handing code is very easy
  7. * to break in several ways. It must always be updated when new
  8. * updates are made to the main siginfo_t, and
  9. * copy_siginfo_to_user32() must be updated when the
  10. * (arch-independent) copy_siginfo_to_user() is updated.
  11. *
  12. * It is also easy to put a new member in the compat_siginfo_t
  13. * which has implicit alignment which can move internal structure
  14. * alignment around breaking the ABI. This can happen if you,
  15. * for instance, put a plain 64-bit value in there.
  16. */
  17. static inline void signal_compat_build_tests(void)
  18. {
  19. int _sifields_offset = offsetof(compat_siginfo_t, _sifields);
  20. /*
  21. * If adding a new si_code, there is probably new data in
  22. * the siginfo. Make sure folks bumping the si_code
  23. * limits also have to look at this code. Make sure any
  24. * new fields are handled in copy_siginfo_to_user32()!
  25. */
  26. BUILD_BUG_ON(NSIGILL != 11);
  27. BUILD_BUG_ON(NSIGFPE != 15);
  28. BUILD_BUG_ON(NSIGSEGV != 7);
  29. BUILD_BUG_ON(NSIGBUS != 5);
  30. BUILD_BUG_ON(NSIGTRAP != 5);
  31. BUILD_BUG_ON(NSIGCHLD != 6);
  32. BUILD_BUG_ON(NSIGSYS != 1);
  33. /* This is part of the ABI and can never change in size: */
  34. BUILD_BUG_ON(sizeof(compat_siginfo_t) != 128);
  35. /*
  36. * The offsets of all the (unioned) si_fields are fixed
  37. * in the ABI, of course. Make sure none of them ever
  38. * move and are always at the beginning:
  39. */
  40. BUILD_BUG_ON(offsetof(compat_siginfo_t, _sifields) != 3 * sizeof(int));
  41. #define CHECK_CSI_OFFSET(name) BUILD_BUG_ON(_sifields_offset != offsetof(compat_siginfo_t, _sifields.name))
  42. BUILD_BUG_ON(offsetof(siginfo_t, si_signo) != 0);
  43. BUILD_BUG_ON(offsetof(siginfo_t, si_errno) != 4);
  44. BUILD_BUG_ON(offsetof(siginfo_t, si_code) != 8);
  45. BUILD_BUG_ON(offsetof(compat_siginfo_t, si_signo) != 0);
  46. BUILD_BUG_ON(offsetof(compat_siginfo_t, si_errno) != 4);
  47. BUILD_BUG_ON(offsetof(compat_siginfo_t, si_code) != 8);
  48. /*
  49. * Ensure that the size of each si_field never changes.
  50. * If it does, it is a sign that the
  51. * copy_siginfo_to_user32() code below needs to updated
  52. * along with the size in the CHECK_SI_SIZE().
  53. *
  54. * We repeat this check for both the generic and compat
  55. * siginfos.
  56. *
  57. * Note: it is OK for these to grow as long as the whole
  58. * structure stays within the padding size (checked
  59. * above).
  60. */
  61. #define CHECK_CSI_SIZE(name, size) BUILD_BUG_ON(size != sizeof(((compat_siginfo_t *)0)->_sifields.name))
  62. #define CHECK_SI_SIZE(name, size) BUILD_BUG_ON(size != sizeof(((siginfo_t *)0)->_sifields.name))
  63. CHECK_CSI_OFFSET(_kill);
  64. CHECK_CSI_SIZE (_kill, 2*sizeof(int));
  65. CHECK_SI_SIZE (_kill, 2*sizeof(int));
  66. BUILD_BUG_ON(offsetof(siginfo_t, si_pid) != 0x10);
  67. BUILD_BUG_ON(offsetof(siginfo_t, si_uid) != 0x14);
  68. BUILD_BUG_ON(offsetof(compat_siginfo_t, si_pid) != 0xC);
  69. BUILD_BUG_ON(offsetof(compat_siginfo_t, si_uid) != 0x10);
  70. CHECK_CSI_OFFSET(_timer);
  71. CHECK_CSI_SIZE (_timer, 3*sizeof(int));
  72. CHECK_SI_SIZE (_timer, 6*sizeof(int));
  73. BUILD_BUG_ON(offsetof(siginfo_t, si_tid) != 0x10);
  74. BUILD_BUG_ON(offsetof(siginfo_t, si_overrun) != 0x14);
  75. BUILD_BUG_ON(offsetof(siginfo_t, si_value) != 0x18);
  76. BUILD_BUG_ON(offsetof(compat_siginfo_t, si_tid) != 0x0C);
  77. BUILD_BUG_ON(offsetof(compat_siginfo_t, si_overrun) != 0x10);
  78. BUILD_BUG_ON(offsetof(compat_siginfo_t, si_value) != 0x14);
  79. CHECK_CSI_OFFSET(_rt);
  80. CHECK_CSI_SIZE (_rt, 3*sizeof(int));
  81. CHECK_SI_SIZE (_rt, 4*sizeof(int));
  82. BUILD_BUG_ON(offsetof(siginfo_t, si_pid) != 0x10);
  83. BUILD_BUG_ON(offsetof(siginfo_t, si_uid) != 0x14);
  84. BUILD_BUG_ON(offsetof(siginfo_t, si_value) != 0x18);
  85. BUILD_BUG_ON(offsetof(compat_siginfo_t, si_pid) != 0x0C);
  86. BUILD_BUG_ON(offsetof(compat_siginfo_t, si_uid) != 0x10);
  87. BUILD_BUG_ON(offsetof(compat_siginfo_t, si_value) != 0x14);
  88. CHECK_CSI_OFFSET(_sigchld);
  89. CHECK_CSI_SIZE (_sigchld, 5*sizeof(int));
  90. CHECK_SI_SIZE (_sigchld, 8*sizeof(int));
  91. BUILD_BUG_ON(offsetof(siginfo_t, si_pid) != 0x10);
  92. BUILD_BUG_ON(offsetof(siginfo_t, si_uid) != 0x14);
  93. BUILD_BUG_ON(offsetof(siginfo_t, si_status) != 0x18);
  94. BUILD_BUG_ON(offsetof(siginfo_t, si_utime) != 0x20);
  95. BUILD_BUG_ON(offsetof(siginfo_t, si_stime) != 0x28);
  96. BUILD_BUG_ON(offsetof(compat_siginfo_t, si_pid) != 0x0C);
  97. BUILD_BUG_ON(offsetof(compat_siginfo_t, si_uid) != 0x10);
  98. BUILD_BUG_ON(offsetof(compat_siginfo_t, si_status) != 0x14);
  99. BUILD_BUG_ON(offsetof(compat_siginfo_t, si_utime) != 0x18);
  100. BUILD_BUG_ON(offsetof(compat_siginfo_t, si_stime) != 0x1C);
  101. #ifdef CONFIG_X86_X32_ABI
  102. CHECK_CSI_OFFSET(_sigchld_x32);
  103. CHECK_CSI_SIZE (_sigchld_x32, 7*sizeof(int));
  104. /* no _sigchld_x32 in the generic siginfo_t */
  105. BUILD_BUG_ON(offsetof(compat_siginfo_t, _sifields._sigchld_x32._utime) != 0x18);
  106. BUILD_BUG_ON(offsetof(compat_siginfo_t, _sifields._sigchld_x32._stime) != 0x20);
  107. #endif
  108. CHECK_CSI_OFFSET(_sigfault);
  109. CHECK_CSI_SIZE (_sigfault, 4*sizeof(int));
  110. CHECK_SI_SIZE (_sigfault, 8*sizeof(int));
  111. BUILD_BUG_ON(offsetof(siginfo_t, si_addr) != 0x10);
  112. BUILD_BUG_ON(offsetof(compat_siginfo_t, si_addr) != 0x0C);
  113. BUILD_BUG_ON(offsetof(siginfo_t, si_addr_lsb) != 0x18);
  114. BUILD_BUG_ON(offsetof(compat_siginfo_t, si_addr_lsb) != 0x10);
  115. BUILD_BUG_ON(offsetof(siginfo_t, si_lower) != 0x20);
  116. BUILD_BUG_ON(offsetof(siginfo_t, si_upper) != 0x28);
  117. BUILD_BUG_ON(offsetof(compat_siginfo_t, si_lower) != 0x14);
  118. BUILD_BUG_ON(offsetof(compat_siginfo_t, si_upper) != 0x18);
  119. BUILD_BUG_ON(offsetof(siginfo_t, si_pkey) != 0x20);
  120. BUILD_BUG_ON(offsetof(compat_siginfo_t, si_pkey) != 0x14);
  121. CHECK_CSI_OFFSET(_sigpoll);
  122. CHECK_CSI_SIZE (_sigpoll, 2*sizeof(int));
  123. CHECK_SI_SIZE (_sigpoll, 4*sizeof(int));
  124. BUILD_BUG_ON(offsetof(siginfo_t, si_band) != 0x10);
  125. BUILD_BUG_ON(offsetof(siginfo_t, si_fd) != 0x18);
  126. BUILD_BUG_ON(offsetof(compat_siginfo_t, si_band) != 0x0C);
  127. BUILD_BUG_ON(offsetof(compat_siginfo_t, si_fd) != 0x10);
  128. CHECK_CSI_OFFSET(_sigsys);
  129. CHECK_CSI_SIZE (_sigsys, 3*sizeof(int));
  130. CHECK_SI_SIZE (_sigsys, 4*sizeof(int));
  131. BUILD_BUG_ON(offsetof(siginfo_t, si_call_addr) != 0x10);
  132. BUILD_BUG_ON(offsetof(siginfo_t, si_syscall) != 0x18);
  133. BUILD_BUG_ON(offsetof(siginfo_t, si_arch) != 0x1C);
  134. BUILD_BUG_ON(offsetof(compat_siginfo_t, si_call_addr) != 0x0C);
  135. BUILD_BUG_ON(offsetof(compat_siginfo_t, si_syscall) != 0x10);
  136. BUILD_BUG_ON(offsetof(compat_siginfo_t, si_arch) != 0x14);
  137. /* any new si_fields should be added here */
  138. }
  139. void sigaction_compat_abi(struct k_sigaction *act, struct k_sigaction *oact)
  140. {
  141. signal_compat_build_tests();
  142. /* Don't leak in-kernel non-uapi flags to user-space */
  143. if (oact)
  144. oact->sa.sa_flags &= ~(SA_IA32_ABI | SA_X32_ABI);
  145. if (!act)
  146. return;
  147. /* Don't let flags to be set from userspace */
  148. act->sa.sa_flags &= ~(SA_IA32_ABI | SA_X32_ABI);
  149. if (in_ia32_syscall())
  150. act->sa.sa_flags |= SA_IA32_ABI;
  151. if (in_x32_syscall())
  152. act->sa.sa_flags |= SA_X32_ABI;
  153. }