summaryrefslogtreecommitdiff
path: root/regress/sys/kern/ptrace/ptrace.c
blob: cb88f4e5b17fb71fb04807c7553c8f81a20939a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*	$OpenBSD: ptrace.c,v 1.4 2017/09/06 04:20:12 guenther Exp $	*/
/*
 * Copyright (c) 2005 Artur Grabowski <art@openbsd.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <sys/types.h>
#include <sys/ptrace.h>
#include <sys/mman.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>
#include <err.h>

static void
usage(void)
{
	fprintf(stderr, "Usage: ptrace [-bdirwI]\n");
	exit(1);
}

#define MAGIC	"asdfblahblahspam1235432blah"
#define MAGIC_I	0x47114217

int
main(int argc, char **argv)
{
	int ch;
	int bad = 0, i, write, I;
	pid_t pid;
	char *m;
	int ps;
	int status;
	int ret;

	ps = getpagesize();

	I = 0;
	i = 0;
	write = 0;

	while ((ch = getopt(argc, argv, "bdirwI")) != -1) {
		switch (ch) {
		case 'b':
			bad = 1;
			break;
		case 'i':
			i = 1;
			break;
		case 'd':
			i = 0;
			break;
		case 'r':
			write = 0;
			break;
		case 'w':
			write = 1;
			break;
		case 'I':
			I = 1;
			break;
		default:
			usage();
		}
	}

	m = mmap(0, ps, PROT_READ|PROT_WRITE, MAP_ANON|MAP_SHARED, -1, 0);
	if (m == MAP_FAILED)
		err(1, "mmap");

	if (!write) {
		if (I)
			memcpy(m, MAGIC, sizeof(MAGIC));
		else
			*(int *)m = MAGIC_I;
	}
	if (bad)
		if (mprotect(m, ps, PROT_NONE))
			err(1, "mprotect");

	switch ((pid = fork())) {
	case 0:
		pause();
		_exit(0);
	case -1:
		err(1, "fork");
	}

	ret = 0;

	if (ptrace(PT_ATTACH, pid, 0, 0) == -1) {
		warn("ptrace(PT_ATTACH)");
		ret = -1;
		goto out;
	}

	if (wait(&status) != pid) {
		warn("wait");
		ret = -1;
		goto out;
	}

	if (!write) {
		int req;

		if (I) {
			char foo[1024];
			struct ptrace_io_desc piod;

			if (i)
				piod.piod_op = PIOD_READ_I;
			else
				piod.piod_op = PIOD_READ_D;
			piod.piod_offs = m;
			piod.piod_addr = &foo;
			piod.piod_len = sizeof(MAGIC);

			if (ptrace(PT_IO, pid, (caddr_t)&piod, 0) == -1) {
				warn("ptrace(PT_IO)");
				if (errno == EACCES)
					ret = 1;
				else
					ret = -1;
				goto out;
			}

			if (memcmp(foo, MAGIC, sizeof(MAGIC))) {
				warnx("mismatch %s != %s", foo, MAGIC);
				ret = 1;
				goto out;
			}
		} else {
			if (i)
				req = PT_READ_I;
			else
				req = PT_READ_D;

			i = ptrace(req, pid, m, sizeof(i));
			if (i != MAGIC_I) {
				warn("ptrace(%d): %d != %d", req, i, MAGIC_I);
				ret = 1;
				goto out;
			}
		}
	} else {
		errx(1, "lazy bum");
	}

out:
	if (ret == -1) {
		/* other errors */
		ret = 1;
	} else if (bad) {
		if (ret == 0) {
			warnx("protected memory unprotected");
			ret = 1;
		} else {
			ret = 0;
		}
	}

	kill(pid, SIGKILL);

	return ret;
}