1/* 2 * Copyright (c) 2002 Andi Kleen <ak@suse.de> 3 * Copyright (c) 2002 Michal Ludvig <mludvig@suse.cz> 4 * Copyright (c) 2002 Roland McGrath <roland@redhat.com> 5 * Copyright (c) 2008-2013 Denys Vlasenko <vda.linux@googlemail.com> 6 * Copyright (c) 2012 H.J. Lu <hongjiu.lu@intel.com> 7 * Copyright (c) 2010-2015 Dmitry V. Levin <ldv@altlinux.org> 8 * Copyright (c) 2015-2017 The strace developers. 9 * All rights reserved. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. The name of the author may not be used to endorse or promote products 20 * derived from this software without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34#ifdef X86_64 35# define X32_PERSONALITY_NUMBER 2 36#else 37# define X32_PERSONALITY_NUMBER 0 38#endif 39 40/* Return codes: 1 - ok, 0 - ignore, other - error. */ 41static int 42arch_get_scno(struct tcb *tcp) 43{ 44 kernel_ulong_t scno = 0; 45 unsigned int currpers; 46 47#ifndef __X32_SYSCALL_BIT 48# define __X32_SYSCALL_BIT 0x40000000 49#endif 50 51#if 1 52 /* 53 * GETREGSET of NT_PRSTATUS tells us regset size, 54 * which unambiguously detects i386. 55 * 56 * Linux kernel distinguishes x86-64 and x32 processes 57 * solely by looking at __X32_SYSCALL_BIT: 58 * arch/x86/include/asm/compat.h::is_x32_task(): 59 * if (task_pt_regs(current)->orig_ax & __X32_SYSCALL_BIT) 60 * return true; 61 */ 62 if (x86_io.iov_len == sizeof(i386_regs)) { 63 scno = i386_regs.orig_eax; 64 currpers = 1; 65 } else { 66 scno = x86_64_regs.orig_rax; 67 currpers = 0; 68 if (scno & __X32_SYSCALL_BIT) { 69 /* 70 * Syscall number -1 requires special treatment: 71 * it might be a side effect of SECCOMP_RET_ERRNO 72 * filtering that sets orig_rax to -1 73 * in some versions of linux kernel. 74 * If that is the case, then 75 * __X32_SYSCALL_BIT logic does not apply. 76 */ 77 if ((long long) x86_64_regs.orig_rax != -1) { 78 scno -= __X32_SYSCALL_BIT; 79 currpers = 2; 80 } else { 81# ifdef X32 82 currpers = 2; 83# endif 84 } 85 } 86 } 87 88#elif 0 89 /* 90 * cs = 0x33 for long mode (native 64 bit and x32) 91 * cs = 0x23 for compatibility mode (32 bit) 92 * ds = 0x2b for x32 mode (x86-64 in 32 bit) 93 */ 94 scno = x86_64_regs.orig_rax; 95 switch (x86_64_regs.cs) { 96 case 0x23: 97 currpers = 1; 98 break; 99 case 0x33: 100 if (x86_64_regs.ds == 0x2b) { 101 currpers = 2; 102 scno &= ~__X32_SYSCALL_BIT; 103 } else 104 currpers = 0; 105 break; 106 default: 107 error_msg("Unknown value CS=0x%08X while " 108 "detecting personality of process PID=%d", 109 (int)x86_64_regs.cs, tcp->pid); 110 currpers = current_personality; 111 break; 112 } 113#elif 0 114 /* 115 * This version analyzes the opcode of a syscall instruction. 116 * (int 0x80 on i386 vs. syscall on x86-64) 117 * It works, but is too complicated, and strictly speaking, unreliable. 118 */ 119 unsigned long call, rip = x86_64_regs.rip; 120 /* sizeof(syscall) == sizeof(int 0x80) == 2 */ 121 rip -= 2; 122 errno = 0; 123 call = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)rip, (char *)0); 124 if (errno) 125 perror_msg("ptrace_peektext failed"); 126 switch (call & 0xffff) { 127 /* x86-64: syscall = 0x0f 0x05 */ 128 case 0x050f: 129 currpers = 0; 130 break; 131 /* i386: int 0x80 = 0xcd 0x80 */ 132 case 0x80cd: 133 currpers = 1; 134 break; 135 default: 136 currpers = current_personality; 137 error_msg("Unknown syscall opcode (0x%04X) while " 138 "detecting personality of process PID=%d", 139 (int)call, tcp->pid); 140 break; 141 } 142#endif 143 144#ifdef X32 145 /* 146 * If we are built for a x32 system, then personality 0 is x32 147 * (not x86_64), and stracing of x86_64 apps is not supported. 148 * Stracing of i386 apps is still supported. 149 */ 150 if (currpers == 0) { 151 error_msg("syscall_%" PRI_klu "(...) in unsupported " 152 "64-bit mode of process PID=%d", scno, tcp->pid); 153 return 0; 154 } 155 currpers &= ~2; /* map 2,1 to 0,1 */ 156#endif /* X32 */ 157 158 update_personality(tcp, currpers); 159 tcp->scno = scno; 160 return 1; 161} 162