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