LCOV - code coverage report
Current view: top level - arch/x86/kernel - signal_compat.c (source / functions) Hit Total Coverage
Test: combined.info Lines: 91 92 98.9 %
Date: 2022-03-28 15:32:58 Functions: 1 1 100.0 %
Branches: 5 6 83.3 %

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

Generated by: LCOV version 1.14