2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2018 The FreeBSD Foundation
7 * This software was developed by Konstantin Belousov <kib@FreeBSD.org>
8 * under sponsorship from the FreeBSD Foundation.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * $Id: popss.c,v 1.28 2018/05/09 21:35:29 kostik Exp kostik $
34 * cc -m32 -Wall -Wextra -O2 -g -o popss popss.c
35 * Use as "popss <instruction>", where instruction is one of
36 * bound, into, int1, int3, int80, syscall, sysenter.
39 #include <sys/param.h>
40 #include <sys/ptrace.h>
48 #include <machine/reg.h>
61 error = waitpid(child, &status, WTRAPPED | WEXITED);
64 error = ptrace(PT_GETREGS, child, (caddr_t)&r, 0);
66 err(1, "ptrace PT_GETREGS");
67 printf("child %d stopped eip %#x esp %#x\n", child, r.r_eip, r.r_esp);
69 error = ptrace(PT_GETDBREGS, child, (caddr_t)&dbr, 0);
71 err(1, "ptrace PT_GETDBREGS");
72 dbr.dr[7] &= ~DBREG_DR7_MASK(0);
73 dbr.dr[7] |= DBREG_DR7_SET(0, DBREG_DR7_LEN_4, DBREG_DR7_RDWR,
74 DBREG_DR7_LOCAL_ENABLE | DBREG_DR7_GLOBAL_ENABLE);
75 dbr.dr[0] = (uintptr_t)stk;
76 error = ptrace(PT_SETDBREGS, child, (caddr_t)&dbr, 0);
78 err(1, "ptrace PT_SETDBREGS");
79 error = ptrace(PT_CONTINUE, child, (caddr_t)1, 0);
81 err(1, "ptrace PT_CONTINUE fire");
83 for (i = 0; i < ITERATIONS; i++) {
84 error = waitpid(child, &status, WTRAPPED | WEXITED);
87 if (WIFEXITED(status))
89 error = ptrace(PT_GETREGS, child, (caddr_t)&r, 0);
91 err(1, "ptrace PT_GETREGS");
92 error = ptrace(PT_GETDBREGS, child, (caddr_t)&dbr, 0);
94 err(1, "ptrace PT_GETDBREGS");
95 printf("child %d stopped eip %#x esp %#x dr0 %#x "
96 "dr6 %#x dr7 %#x\n", child, r.r_eip, r.r_esp,
97 dbr.dr[0], dbr.dr[6], dbr.dr[7]);
98 error = ptrace(PT_CONTINUE, child, (caddr_t)1, 0);
100 err(1, "ptrace PT_CONTINUE tail");
102 if (i == ITERATIONS) {
103 kill(child, SIGKILL);
104 ptrace(PT_DETACH, child, NULL, 0);
108 static u_long tmpstk[1024 * 128];
115 __asm volatile("movl\t%%ss,%0" : "=r" (res));
119 #define PROLOGUE "int3;movl\t%0,%%esp;popl\t%%ss;"
125 static const int boundx[2] = {0, 1};
127 printf("child pid %d, stk at %p\n", getpid(), stk);
130 error = ptrace(PT_TRACE_ME, 0, NULL, 0);
132 err(1, "ptrace PT_TRACE_ME");
134 if (strcmp(cmd, "bound") == 0) {
135 /* XXX BOUND args order clang ias bug */
136 __asm volatile("int3;movl\t$11,%%eax;"
137 "movl\t%0,%%esp;popl\t%%ss;bound\t%1,%%eax"
138 : : "r" (stk), "m" (boundx) : "memory");
139 } else if (strcmp(cmd, "int1") == 0) {
140 __asm volatile(PROLOGUE ".byte 0xf1"
141 : : "r" (stk) : "memory");
142 } else if (strcmp(cmd, "int3") == 0) {
143 __asm volatile(PROLOGUE "int3"
144 : : "r" (stk) : "memory");
145 } else if (strcmp(cmd, "into") == 0) {
146 __asm volatile("int3;movl\t$0x80000000,%%eax;"
147 "addl\t%%eax,%%eax;movl\t%0,%%esp;popl\t%%ss;into"
148 : : "r" (stk) : "memory");
149 } else if (strcmp(cmd, "int80") == 0) {
150 __asm volatile(PROLOGUE "int\t$0x80"
151 : : "r" (stk) : "memory");
152 } else if (strcmp(cmd, "syscall") == 0) {
153 __asm volatile(PROLOGUE "syscall"
154 : : "r" (stk) : "memory");
155 } else if (strcmp(cmd, "sysenter") == 0) {
156 __asm volatile(PROLOGUE "sysenter"
157 : : "r" (stk) : "memory");
159 fprintf(stderr, "unknown instruction\n");
166 main(int argc, char *argv[])
172 "Usage: popss [bound|int1|int3|into|int80|syscall|sysenter]\n");
175 stk = &tmpstk[nitems(tmpstk) - 1];