Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 #include <linux/compat.h>
0003 #include <linux/uaccess.h>
0004 #include <linux/ptrace.h>
0005 
0006 /*
0007  * The compat_siginfo_t structure and handing code is very easy
0008  * to break in several ways.  It must always be updated when new
0009  * updates are made to the main siginfo_t, and
0010  * copy_siginfo_to_user32() must be updated when the
0011  * (arch-independent) copy_siginfo_to_user() is updated.
0012  *
0013  * It is also easy to put a new member in the compat_siginfo_t
0014  * which has implicit alignment which can move internal structure
0015  * alignment around breaking the ABI.  This can happen if you,
0016  * for instance, put a plain 64-bit value in there.
0017  */
0018 static inline void signal_compat_build_tests(void)
0019 {
0020     int _sifields_offset = offsetof(compat_siginfo_t, _sifields);
0021 
0022     /*
0023      * If adding a new si_code, there is probably new data in
0024      * the siginfo.  Make sure folks bumping the si_code
0025      * limits also have to look at this code.  Make sure any
0026      * new fields are handled in copy_siginfo_to_user32()!
0027      */
0028     BUILD_BUG_ON(NSIGILL  != 11);
0029     BUILD_BUG_ON(NSIGFPE  != 15);
0030     BUILD_BUG_ON(NSIGSEGV != 9);
0031     BUILD_BUG_ON(NSIGBUS  != 5);
0032     BUILD_BUG_ON(NSIGTRAP != 6);
0033     BUILD_BUG_ON(NSIGCHLD != 6);
0034     BUILD_BUG_ON(NSIGSYS  != 2);
0035 
0036     /* This is part of the ABI and can never change in size: */
0037     BUILD_BUG_ON(sizeof(siginfo_t) != 128);
0038     BUILD_BUG_ON(sizeof(compat_siginfo_t) != 128);
0039 
0040     /* This is a part of the ABI and can never change in alignment */
0041     BUILD_BUG_ON(__alignof__(siginfo_t) != 8);
0042     BUILD_BUG_ON(__alignof__(compat_siginfo_t) != 4);
0043 
0044     /*
0045      * The offsets of all the (unioned) si_fields are fixed
0046      * in the ABI, of course.  Make sure none of them ever
0047      * move and are always at the beginning:
0048      */
0049     BUILD_BUG_ON(offsetof(compat_siginfo_t, _sifields) != 3 * sizeof(int));
0050 #define CHECK_CSI_OFFSET(name)    BUILD_BUG_ON(_sifields_offset != offsetof(compat_siginfo_t, _sifields.name))
0051 
0052     BUILD_BUG_ON(offsetof(siginfo_t, si_signo) != 0);
0053     BUILD_BUG_ON(offsetof(siginfo_t, si_errno) != 4);
0054     BUILD_BUG_ON(offsetof(siginfo_t, si_code)  != 8);
0055 
0056     BUILD_BUG_ON(offsetof(compat_siginfo_t, si_signo) != 0);
0057     BUILD_BUG_ON(offsetof(compat_siginfo_t, si_errno) != 4);
0058     BUILD_BUG_ON(offsetof(compat_siginfo_t, si_code)  != 8);
0059      /*
0060      * Ensure that the size of each si_field never changes.
0061      * If it does, it is a sign that the
0062      * copy_siginfo_to_user32() code below needs to updated
0063      * along with the size in the CHECK_SI_SIZE().
0064      *
0065      * We repeat this check for both the generic and compat
0066      * siginfos.
0067      *
0068      * Note: it is OK for these to grow as long as the whole
0069      * structure stays within the padding size (checked
0070      * above).
0071      */
0072 #define CHECK_CSI_SIZE(name, size) BUILD_BUG_ON(size != sizeof(((compat_siginfo_t *)0)->_sifields.name))
0073 #define CHECK_SI_SIZE(name, size) BUILD_BUG_ON(size != sizeof(((siginfo_t *)0)->_sifields.name))
0074 
0075     CHECK_CSI_OFFSET(_kill);
0076     CHECK_CSI_SIZE  (_kill, 2*sizeof(int));
0077     CHECK_SI_SIZE   (_kill, 2*sizeof(int));
0078 
0079     BUILD_BUG_ON(offsetof(siginfo_t, si_pid) != 0x10);
0080     BUILD_BUG_ON(offsetof(siginfo_t, si_uid) != 0x14);
0081     BUILD_BUG_ON(offsetof(compat_siginfo_t, si_pid) != 0xC);
0082     BUILD_BUG_ON(offsetof(compat_siginfo_t, si_uid) != 0x10);
0083 
0084     CHECK_CSI_OFFSET(_timer);
0085     CHECK_CSI_SIZE  (_timer, 3*sizeof(int));
0086     CHECK_SI_SIZE   (_timer, 6*sizeof(int));
0087 
0088     BUILD_BUG_ON(offsetof(siginfo_t, si_tid)     != 0x10);
0089     BUILD_BUG_ON(offsetof(siginfo_t, si_overrun) != 0x14);
0090     BUILD_BUG_ON(offsetof(siginfo_t, si_value)   != 0x18);
0091     BUILD_BUG_ON(offsetof(compat_siginfo_t, si_tid)     != 0x0C);
0092     BUILD_BUG_ON(offsetof(compat_siginfo_t, si_overrun) != 0x10);
0093     BUILD_BUG_ON(offsetof(compat_siginfo_t, si_value)   != 0x14);
0094 
0095     CHECK_CSI_OFFSET(_rt);
0096     CHECK_CSI_SIZE  (_rt, 3*sizeof(int));
0097     CHECK_SI_SIZE   (_rt, 4*sizeof(int));
0098 
0099     BUILD_BUG_ON(offsetof(siginfo_t, si_pid)   != 0x10);
0100     BUILD_BUG_ON(offsetof(siginfo_t, si_uid)   != 0x14);
0101     BUILD_BUG_ON(offsetof(siginfo_t, si_value) != 0x18);
0102     BUILD_BUG_ON(offsetof(compat_siginfo_t, si_pid)   != 0x0C);
0103     BUILD_BUG_ON(offsetof(compat_siginfo_t, si_uid)   != 0x10);
0104     BUILD_BUG_ON(offsetof(compat_siginfo_t, si_value) != 0x14);
0105 
0106     CHECK_CSI_OFFSET(_sigchld);
0107     CHECK_CSI_SIZE  (_sigchld, 5*sizeof(int));
0108     CHECK_SI_SIZE   (_sigchld, 8*sizeof(int));
0109 
0110     BUILD_BUG_ON(offsetof(siginfo_t, si_pid)    != 0x10);
0111     BUILD_BUG_ON(offsetof(siginfo_t, si_uid)    != 0x14);
0112     BUILD_BUG_ON(offsetof(siginfo_t, si_status) != 0x18);
0113     BUILD_BUG_ON(offsetof(siginfo_t, si_utime)  != 0x20);
0114     BUILD_BUG_ON(offsetof(siginfo_t, si_stime)  != 0x28);
0115     BUILD_BUG_ON(offsetof(compat_siginfo_t, si_pid)    != 0x0C);
0116     BUILD_BUG_ON(offsetof(compat_siginfo_t, si_uid)    != 0x10);
0117     BUILD_BUG_ON(offsetof(compat_siginfo_t, si_status) != 0x14);
0118     BUILD_BUG_ON(offsetof(compat_siginfo_t, si_utime)  != 0x18);
0119     BUILD_BUG_ON(offsetof(compat_siginfo_t, si_stime)  != 0x1C);
0120 
0121 #ifdef CONFIG_X86_X32_ABI
0122     CHECK_CSI_OFFSET(_sigchld_x32);
0123     CHECK_CSI_SIZE  (_sigchld_x32, 7*sizeof(int));
0124     /* no _sigchld_x32 in the generic siginfo_t */
0125     BUILD_BUG_ON(offsetof(compat_siginfo_t, _sifields._sigchld_x32._utime)  != 0x18);
0126     BUILD_BUG_ON(offsetof(compat_siginfo_t, _sifields._sigchld_x32._stime)  != 0x20);
0127 #endif
0128 
0129     CHECK_CSI_OFFSET(_sigfault);
0130     CHECK_CSI_SIZE  (_sigfault, 4*sizeof(int));
0131     CHECK_SI_SIZE   (_sigfault, 8*sizeof(int));
0132 
0133     BUILD_BUG_ON(offsetof(siginfo_t, si_addr) != 0x10);
0134     BUILD_BUG_ON(offsetof(compat_siginfo_t, si_addr) != 0x0C);
0135 
0136     BUILD_BUG_ON(offsetof(siginfo_t, si_trapno) != 0x18);
0137     BUILD_BUG_ON(offsetof(compat_siginfo_t, si_trapno) != 0x10);
0138 
0139     BUILD_BUG_ON(offsetof(siginfo_t, si_addr_lsb) != 0x18);
0140     BUILD_BUG_ON(offsetof(compat_siginfo_t, si_addr_lsb) != 0x10);
0141 
0142     BUILD_BUG_ON(offsetof(siginfo_t, si_lower) != 0x20);
0143     BUILD_BUG_ON(offsetof(siginfo_t, si_upper) != 0x28);
0144     BUILD_BUG_ON(offsetof(compat_siginfo_t, si_lower) != 0x14);
0145     BUILD_BUG_ON(offsetof(compat_siginfo_t, si_upper) != 0x18);
0146 
0147     BUILD_BUG_ON(offsetof(siginfo_t, si_pkey) != 0x20);
0148     BUILD_BUG_ON(offsetof(compat_siginfo_t, si_pkey) != 0x14);
0149 
0150     BUILD_BUG_ON(offsetof(siginfo_t, si_perf_data) != 0x18);
0151     BUILD_BUG_ON(offsetof(siginfo_t, si_perf_type) != 0x20);
0152     BUILD_BUG_ON(offsetof(siginfo_t, si_perf_flags) != 0x24);
0153     BUILD_BUG_ON(offsetof(compat_siginfo_t, si_perf_data) != 0x10);
0154     BUILD_BUG_ON(offsetof(compat_siginfo_t, si_perf_type) != 0x14);
0155     BUILD_BUG_ON(offsetof(compat_siginfo_t, si_perf_flags) != 0x18);
0156 
0157     CHECK_CSI_OFFSET(_sigpoll);
0158     CHECK_CSI_SIZE  (_sigpoll, 2*sizeof(int));
0159     CHECK_SI_SIZE   (_sigpoll, 4*sizeof(int));
0160 
0161     BUILD_BUG_ON(offsetof(siginfo_t, si_band)   != 0x10);
0162     BUILD_BUG_ON(offsetof(siginfo_t, si_fd)     != 0x18);
0163     BUILD_BUG_ON(offsetof(compat_siginfo_t, si_band) != 0x0C);
0164     BUILD_BUG_ON(offsetof(compat_siginfo_t, si_fd)   != 0x10);
0165 
0166     CHECK_CSI_OFFSET(_sigsys);
0167     CHECK_CSI_SIZE  (_sigsys, 3*sizeof(int));
0168     CHECK_SI_SIZE   (_sigsys, 4*sizeof(int));
0169 
0170     BUILD_BUG_ON(offsetof(siginfo_t, si_call_addr) != 0x10);
0171     BUILD_BUG_ON(offsetof(siginfo_t, si_syscall)   != 0x18);
0172     BUILD_BUG_ON(offsetof(siginfo_t, si_arch)      != 0x1C);
0173     BUILD_BUG_ON(offsetof(compat_siginfo_t, si_call_addr) != 0x0C);
0174     BUILD_BUG_ON(offsetof(compat_siginfo_t, si_syscall)   != 0x10);
0175     BUILD_BUG_ON(offsetof(compat_siginfo_t, si_arch)      != 0x14);
0176 
0177     /* any new si_fields should be added here */
0178 }
0179 
0180 void sigaction_compat_abi(struct k_sigaction *act, struct k_sigaction *oact)
0181 {
0182     signal_compat_build_tests();
0183 
0184     if (!act)
0185         return;
0186 
0187     if (in_ia32_syscall())
0188         act->sa.sa_flags |= SA_IA32_ABI;
0189     if (in_x32_syscall())
0190         act->sa.sa_flags |= SA_X32_ABI;
0191 }