22441b0
commit 3bac39a10abf292d332bb20ab58c6dd5c28f9108
22441b0
Author: Eugene Syromyatnikov <evgsyr@gmail.com>
22441b0
Date:   Fri Mar 8 04:07:00 2019 +0100
22441b0
22441b0
    include/vki: fix vki_siginfo_t definition on amd64, arm64, and ppc64
22441b0
    
22441b0
    As it turned out, the size of vki_siginfo_t is incorrect on these 64-bit
22441b0
    architectures:
22441b0
    
22441b0
        (gdb) p sizeof(vki_siginfo_t)
22441b0
        $1 = 136
22441b0
        (gdb) ptype struct vki_siginfo
22441b0
        type = struct vki_siginfo {
22441b0
            int si_signo;
22441b0
            int si_errno;
22441b0
            int si_code;
22441b0
            union {
22441b0
                int _pad[29];
22441b0
                struct {...} _kill;
22441b0
                struct {...} _timer;
22441b0
                struct {...} _rt;
22441b0
                struct {...} _sigchld;
22441b0
                struct {...} _sigfault;
22441b0
                struct {...} _sigpoll;
22441b0
            } _sifields;
22441b0
        }
22441b0
    
22441b0
    It looks like that for this architecture, __VKI_ARCH_SI_PREAMBLE_SIZE
22441b0
    hasn't been defined properly, which resulted in incorrect
22441b0
    VKI_SI_PAD_SIZE calculation (29 instead of 28).
22441b0
    
22441b0
        <6a9e4>   DW_AT_name        : (indirect string, offset: 0xcf59): _sifields
22441b0
        <6a9ef>   DW_AT_data_member_location: 16
22441b0
    
22441b0
    This issue has been discovered with strace's "make check-valgrind-memcheck",
22441b0
    which produced false out-of-bounds writes on ptrace(PTRACE_GETSIGINFO) calls:
22441b0
    
22441b0
        SYSCALL[24264,1](101) sys_ptrace ( 16898, 24283, 0x0, 0x606bd40 )
22441b0
        ==24264== Syscall param ptrace(getsiginfo) points to unaddressable byte(s)
22441b0
        ==24264==    at 0x575C06E: ptrace (ptrace.c:45)
22441b0
        ==24264==    by 0x443244: next_event (strace.c:2431)
22441b0
        ==24264==    by 0x443D30: main (strace.c:2845)
22441b0
        ==24264==  Address 0x606bdc0 is 0 bytes after a block of size 144 alloc'd
22441b0
    
22441b0
    (Note that the address passed is 0x606bd40 and the address reported is
22441b0
    0x606bdc0).
22441b0
    
22441b0
    After the patch, no such errors observed.
22441b0
    
22441b0
    * include/vki/vki-amd64-linux.h [__x86_64__ && __ILP32__]
22441b0
    (__vki_kernel_si_clock_t): New typedef.
22441b0
    [__x86_64__ && __ILP32__] (__VKI_ARCH_SI_CLOCK_T,
22441b0
    __VKI_ARCH_SI_ATTRIBUTES): New macros.
22441b0
    [__x86_64__ && !__ILP32__] (__VKI_ARCH_SI_PREAMBLE_SIZE): New macro,
22441b0
    define to 4 ints.
22441b0
    * include/vki/vki-arm64-linux.h (__VKI_ARCH_SI_PREAMBLE_SIZE): Likewise.
22441b0
    * include/vki/vki-ppc64-linux.h [__powerpc64__] (__VKI_ARCH_SI_PREAMBLE_SIZE):
22441b0
    Likewise.
22441b0
    * include/vki/vki-linux.h [!__VKI_ARCH_SI_CLOCK_T]
22441b0
    (__VKI_ARCH_SI_CLOCK_T): New macro, define to vki_clock_t.
22441b0
    [!__VKI_ARCH_SI_ATTRIBUTES] (__VKI_ARCH_SI_ATTRIBUTES): New macro,
22441b0
    define to nil.
22441b0
    (struct vki_siginfo): Use __VKI_ARCH_SI_CLOCK_T type for _utime and
22441b0
    _stime fields.  Add __VKI_ARCH_SI_ATTRIBUTES.
22441b0
    
22441b0
    Resolves: https://bugs.kde.org/show_bug.cgi?id=405201
22441b0
    Reported-by: Dmitry V. Levin <ldv@altlinux.org>
22441b0
    Signed-off-by: Eugene Syromyatnikov <evgsyr@gmail.com>
22441b0
22441b0
diff --git a/include/vki/vki-amd64-linux.h b/include/vki/vki-amd64-linux.h
22441b0
index d6a5a77e6..fbd353aed 100644
22441b0
--- a/include/vki/vki-amd64-linux.h
22441b0
+++ b/include/vki/vki-amd64-linux.h
22441b0
@@ -297,6 +297,21 @@ struct vki_f_owner_ex {
22441b0
 #define VKI_RLIMIT_CORE		4	/* max core file size */
22441b0
 #define VKI_RLIMIT_NOFILE	7	/* max number of open files */
22441b0
 
22441b0
+//----------------------------------------------------------------------
22441b0
+// From linux-5.0.0/arch/x86/include/uapi/asm/siginfo.h
22441b0
+//----------------------------------------------------------------------
22441b0
+
22441b0
+/* We need that to ensure that sizeof(siginfo) == 128. */
22441b0
+#ifdef __x86_64__
22441b0
+# ifdef __ILP32__
22441b0
+typedef long long __vki_kernel_si_clock_t __attribute__((aligned(4)));
22441b0
+#  define __VKI_ARCH_SI_CLOCK_T             __vki_kernel_si_clock_t
22441b0
+#  define __VKI_ARCH_SI_ATTRIBUTES          __attribute__((aligned(8)))
22441b0
+# else
22441b0
+#  define __VKI_ARCH_SI_PREAMBLE_SIZE (4 * sizeof(int))
22441b0
+# endif
22441b0
+#endif
22441b0
+
22441b0
 //----------------------------------------------------------------------
22441b0
 // From linux-2.6.9/include/asm-x86_64/socket.h
22441b0
 //----------------------------------------------------------------------
22441b0
diff --git a/include/vki/vki-arm64-linux.h b/include/vki/vki-arm64-linux.h
22441b0
index ecea8cc78..69fb3ed00 100644
22441b0
--- a/include/vki/vki-arm64-linux.h
22441b0
+++ b/include/vki/vki-arm64-linux.h
22441b0
@@ -193,6 +193,12 @@ struct vki_sigcontext {
22441b0
         __vki_u8 __reserved[4096] __attribute__((__aligned__(16)));
22441b0
 };
22441b0
 
22441b0
+//----------------------------------------------------------------------
22441b0
+// From linux-5.0.0/arch/arm64/include/uapi/asm/siginfo.h
22441b0
+//----------------------------------------------------------------------
22441b0
+
22441b0
+#define __VKI_ARCH_SI_PREAMBLE_SIZE (4 * sizeof(int))
22441b0
+
22441b0
 //----------------------------------------------------------------------
22441b0
 // From linux-3.10.5/uapi/include/asm-generic/mman-common.h
22441b0
 //----------------------------------------------------------------------
22441b0
diff --git a/include/vki/vki-linux.h b/include/vki/vki-linux.h
22441b0
index 6024f2165..6903c77db 100644
22441b0
--- a/include/vki/vki-linux.h
22441b0
+++ b/include/vki/vki-linux.h
22441b0
@@ -426,6 +426,14 @@ typedef union vki_sigval {
22441b0
 #define __VKI_ARCH_SI_BAND_T long
22441b0
 #endif
22441b0
 
22441b0
+#ifndef __VKI_ARCH_SI_CLOCK_T
22441b0
+#define __VKI_ARCH_SI_CLOCK_T vki_clock_t
22441b0
+#endif
22441b0
+
22441b0
+#ifndef __VKI_ARCH_SI_ATTRIBUTES
22441b0
+#define __VKI_ARCH_SI_ATTRIBUTES
22441b0
+#endif
22441b0
+
22441b0
 // [[Nb: this type changed between 2.4 and 2.6, but not in a way that
22441b0
 // affects Valgrind.]]
22441b0
 typedef struct vki_siginfo {
22441b0
@@ -463,8 +471,8 @@ typedef struct vki_siginfo {
22441b0
 			vki_pid_t _pid;		/* which child */
22441b0
 			__VKI_ARCH_SI_UID_T _uid;	/* sender's uid */
22441b0
 			int _status;		/* exit code */
22441b0
-			vki_clock_t _utime;
22441b0
-			vki_clock_t _stime;
22441b0
+			__VKI_ARCH_SI_CLOCK_T _utime;
22441b0
+			__VKI_ARCH_SI_CLOCK_T _stime;
22441b0
 		} _sigchld;
22441b0
 
22441b0
 		/* SIGILL, SIGFPE, SIGSEGV, SIGBUS */
22441b0
@@ -481,7 +489,7 @@ typedef struct vki_siginfo {
22441b0
 			int _fd;
22441b0
 		} _sigpoll;
22441b0
 	} _sifields;
22441b0
-} vki_siginfo_t;
22441b0
+} __VKI_ARCH_SI_ATTRIBUTES vki_siginfo_t;
22441b0
 #endif
22441b0
 
22441b0
 #define __VKI_SI_FAULT	0
22441b0
diff --git a/include/vki/vki-ppc64-linux.h b/include/vki/vki-ppc64-linux.h
22441b0
index a5e64dd39..04f72048a 100644
22441b0
--- a/include/vki/vki-ppc64-linux.h
22441b0
+++ b/include/vki/vki-ppc64-linux.h
22441b0
@@ -335,6 +335,14 @@ struct vki_sigcontext {
22441b0
   long             vmx_reserve[VKI_ELF_NVRREG+VKI_ELF_NVRREG+1];
22441b0
 };
22441b0
 
22441b0
+//----------------------------------------------------------------------
22441b0
+// From linux-5.0.0/arch/powerpc/include/uapi/asm/siginfo.h
22441b0
+//----------------------------------------------------------------------
22441b0
+
22441b0
+#ifdef __powerpc64__
22441b0
+# define __VKI_ARCH_SI_PREAMBLE_SIZE     (4 * sizeof(int))
22441b0
+#endif
22441b0
+
22441b0
 //----------------------------------------------------------------------
22441b0
 // From linux-2.6.13/include/asm-ppc64/mman.h
22441b0
 //----------------------------------------------------------------------