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