forked from ColinIanKing/stress-ng
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstress-ptrace.c
216 lines (193 loc) · 5.56 KB
/
stress-ptrace.c
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
* Copyright (C) 2013-2021 Canonical, Ltd.
* Copyright (C) 2022 Colin Ian King.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include "stress-ng.h"
#if defined(HAVE_PTRACE)
#include <sys/ptrace.h>
#endif
#if defined(HAVE_PTRACE_REQUEST)
#define shim_ptrace_request enum __ptrace_request
#else
#define shim_ptrace_request int
#endif
static const stress_help_t help[] = {
{ NULL, "ptrace N", "start N workers that trace a child using ptrace" },
{ NULL, "ptrace-ops N", "stop ptrace workers after N system calls are traced" },
{ NULL, NULL, NULL }
};
#if defined(HAVE_PTRACE)
/*
* main syscall ptrace loop
*/
static inline bool stress_syscall_wait(
const stress_args_t *args,
const pid_t pid)
{
while (keep_stressing_flag()) {
int status;
if (ptrace(PTRACE_SYSCALL, pid, 0, 0) < 0) {
if ((errno != ESRCH) && (errno != EPERM) && (errno != EACCES)) {
pr_fail("%s: ptrace failed, errno=%d (%s)\n",
args->name, errno, strerror(errno));
return true;
}
}
if (shim_waitpid(pid, &status, 0) < 0) {
if ((errno != EINTR) && (errno != ECHILD))
pr_fail("%s: waitpid failed, errno=%d (%s)\n",
args->name, errno, strerror(errno));
return true;
}
if (WIFSTOPPED(status) &&
(WSTOPSIG(status) & 0x80))
return false;
if (WIFEXITED(status))
return true;
}
return true;
}
/*
* stress_ptrace()
* stress ptracing
*/
static int stress_ptrace(const stress_args_t *args)
{
pid_t pid;
stress_set_proc_state(args->name, STRESS_STATE_RUN);
again:
pid = fork();
if (pid < 0) {
if (stress_redo_fork(errno))
goto again;
if (!keep_stressing(args))
goto finish;
pr_fail("%s: fork failed, errno=%d (%s)\n",
args->name, errno, strerror(errno));
return EXIT_FAILURE;
} else if (pid == 0) {
(void)setpgid(0, g_pgrp);
stress_parent_died_alarm();
(void)sched_settings_apply(true);
/*
* Child to be traced, we abort if we detect
* we are already being traced by someone else
* as this makes life way too complex
*/
if (ptrace(PTRACE_TRACEME) != 0) {
pr_inf_skip("%s: child cannot be traced, "
"skipping stressor: errno=%d (%s)\n",
args->name, errno, strerror(errno));
_exit(EXIT_SUCCESS);
}
/* Wait for parent to start tracing me */
(void)kill(getpid(), SIGSTOP);
/*
* A simple mix of system calls
*/
while (keep_stressing_flag()) {
VOID_RET(pid_t, getppid());
#if defined(HAVE_GETPGRP)
VOID_RET(pid_t, getpgrp());
#endif
VOID_RET(gid_t, getgid());
VOID_RET(gid_t, getegid());
VOID_RET(uid_t, getuid());
VOID_RET(uid_t, geteuid());
VOID_RET(time_t, time(NULL));
}
_exit(0);
} else {
/* Parent to do the tracing */
int status;
int i = 0;
(void)setpgid(pid, g_pgrp);
if (shim_waitpid(pid, &status, 0) < 0) {
if ((errno != EINTR) && (errno != ECHILD)) {
pr_fail("%s: waitpid failed, errno=%d (%s)\n",
args->name, errno, strerror(errno));
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
if (ptrace(PTRACE_SETOPTIONS, pid,
0, PTRACE_O_TRACESYSGOOD) < 0) {
pr_inf_skip("%s: child cannot be traced, "
"skipping stressor: errno=%d (%s)\n",
args->name, errno, strerror(errno));
if ((errno == ESRCH) || (errno == EPERM) || (errno == EACCES)) {
/* Ensure child is really dead and reap */
(void)kill(pid, SIGKILL);
if (shim_waitpid(pid, &status, 0) < 0) {
if ((errno != EINTR) && (errno != ECHILD)) {
pr_fail("%s: waitpid failed, errno=%d (%s)\n",
args->name, errno, strerror(errno));
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
return WEXITSTATUS(status);
}
pr_fail("%s: ptrace failed, errno=%d (%s)\n",
args->name, errno, strerror(errno));
return EXIT_FAILURE;
}
do {
/*
* We do two of the following per syscall,
* one at the start, and one at the end to catch
* the return. In this stressor we don't really
* care which is which, we just care about counting
* them
*/
if (stress_syscall_wait(args, pid))
break;
/* periodicially perform invalid ptrace calls */
if ((i & 0x1ff) == 0) {
const pid_t bad_pid = stress_get_unused_pid_racy(false);
/* exercise invalid options */
VOID_RET(long, ptrace((shim_ptrace_request)~0L, pid, 0, PTRACE_O_TRACESYSGOOD));
/* exercise invalid pid */
VOID_RET(long, ptrace(PTRACE_SETOPTIONS, bad_pid, 0, PTRACE_O_TRACESYSGOOD));
}
i++;
inc_counter(args);
} while (keep_stressing(args));
/* Terminate child */
(void)kill(pid, SIGKILL);
if (shim_waitpid(pid, &status, 0) < 0)
pr_fail("%s: waitpid failed, errno=%d (%s)\n",
args->name, errno, strerror(errno));
}
finish:
stress_set_proc_state(args->name, STRESS_STATE_DEINIT);
return EXIT_SUCCESS;
}
stressor_info_t stress_ptrace_info = {
.stressor = stress_ptrace,
.class = CLASS_OS,
.verify = VERIFY_ALWAYS,
.help = help
};
#else
stressor_info_t stress_ptrace_info = {
.stressor = stress_not_implemented,
.class = CLASS_OS,
.help = help
};
#endif