forked from ColinIanKing/stress-ng
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstress-cap.c
197 lines (171 loc) · 4.43 KB
/
stress-cap.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
/*
* 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_SYS_CAPABILITY_H)
#include <sys/capability.h>
#endif
static const stress_help_t help[] = {
{ NULL, "cap N", "start N workers exercising capget" },
{ NULL, "cap-ops N", "stop cap workers after N bogo capget operations" },
{ NULL, NULL, NULL }
};
#if defined(HAVE_SYS_CAPABILITY_H) && \
defined(_LINUX_CAPABILITY_U32S_3)
static int stress_capgetset_pid(
const stress_args_t *args,
const pid_t pid,
const bool do_set,
const bool exists)
{
int ret;
struct __user_cap_header_struct uch;
struct __user_cap_data_struct ucd[_LINUX_CAPABILITY_U32S_3];
(void)memset(&uch, 0, sizeof uch);
(void)memset(ucd, 0, sizeof ucd);
#if defined(_LINUX_CAPABILITY_VERSION_3)
uch.version = _LINUX_CAPABILITY_VERSION_3;
uch.pid = pid;
ret = capget(&uch, ucd);
if (ret < 0) {
if (((errno == ESRCH) && exists) ||
(errno != ESRCH)) {
pr_fail("%s: capget on pid %" PRIdMAX " failed: errno=%d (%s)\n",
args->name, (intmax_t)pid, errno, strerror(errno));
}
}
if (do_set) {
ret = capset(&uch, ucd);
if (ret < 0) {
if (((errno == ESRCH) && exists) ||
(errno != ESRCH)) {
pr_fail("%s: capget on pid %" PRIdMAX " failed: errno=%d (%s)\n",
args->name, (intmax_t)pid, errno, strerror(errno));
}
}
/*
* Exercise invalid pid -> EPERM
*/
uch.pid = INT_MIN;
VOID_RET(int, capset(&uch, ucd));
/*
* Exercise invalid version -> EINVAL
*/
uch.version = 0x1234dead;
uch.pid = pid;
VOID_RET(int, capset(&uch, ucd));
}
#else
UNEXPECTED
#endif
/*
* Exercise invalid version -> EINVAL
*/
uch.version = 0x1234dead;
uch.pid = pid;
VOID_RET(int, capget(&uch, ucd));
#if defined(_LINUX_CAPABILITY_VERSION_3)
/*
* Exercise invalid PID -> EINVAL
*/
uch.version = _LINUX_CAPABILITY_VERSION_3;
uch.pid = -pid;
VOID_RET(int, capget(&uch, ucd));
#else
UNEXPECTED
#endif
#if defined(_LINUX_CAPABILITY_VERSION_3)
/*
* Exercise invalid pid
*/
uch.version = _LINUX_CAPABILITY_VERSION_3;
uch.pid = stress_get_unused_pid_racy(false);
VOID_RET(int, capget(&uch, ucd));
#else
UNEXPECTED
#endif
/*
* Exercise older capability versions
*/
#if defined(_LINUX_CAPABILITY_VERSION_2)
uch.version = _LINUX_CAPABILITY_VERSION_2;
uch.pid = pid;
VOID_RET(int, capget(&uch, ucd));
#endif
#if defined(_LINUX_CAPABILITY_VERSION_1)
uch.version = _LINUX_CAPABILITY_VERSION_1;
uch.pid = pid;
VOID_RET(int, capget(&uch, ucd));
#endif
uch.version = ~0U;
uch.pid = pid;
ret = capget(&uch, ucd);
inc_counter(args);
return ret;
}
/*
* stress_cap
* stress capabilities (trivial)
*/
static int stress_cap(const stress_args_t *args)
{
stress_set_proc_state(args->name, STRESS_STATE_RUN);
do {
DIR *dir;
stress_capgetset_pid(args, 1, false, true);
if (!keep_stressing(args))
break;
stress_capgetset_pid(args, args->pid, true, true);
if (!keep_stressing(args))
break;
stress_capgetset_pid(args, args->ppid, false, false);
if (!keep_stressing(args))
break;
dir = opendir("/proc");
if (dir) {
struct dirent *d;
while ((d = readdir(dir)) != NULL) {
intmax_t p;
if (!isdigit(d->d_name[0]))
continue;
if (sscanf(d->d_name, "%" SCNdMAX, &p) != 1)
continue;
stress_capgetset_pid(args, (pid_t)p, false, false);
if (!keep_stressing(args))
break;
}
(void)closedir(dir);
}
} while (keep_stressing(args));
stress_set_proc_state(args->name, STRESS_STATE_DEINIT);
return EXIT_SUCCESS;
}
stressor_info_t stress_cap_info = {
.stressor = stress_cap,
.class = CLASS_OS,
.verify = VERIFY_ALWAYS,
.help = help
};
#else
stressor_info_t stress_cap_info = {
.stressor = stress_not_implemented,
.class = CLASS_OS,
.help = help
};
#endif