forked from ColinIanKing/stress-ng
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstress-chmod.c
320 lines (288 loc) · 7.02 KB
/
stress-chmod.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*
* 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_LIBGEN_H)
#include <libgen.h>
#endif
static const stress_help_t help[] = {
{ NULL, "chmod N", "start N workers thrashing chmod file mode bits " },
{ NULL, "chmod-ops N", "stop chmod workers after N bogo operations" },
{ NULL, NULL, NULL }
};
static const mode_t modes[] = {
#if defined(S_ISUID)
S_ISUID,
#endif
#if defined(S_ISGID)
S_ISGID,
#endif
#if defined(S_ISVTX)
S_ISVTX,
#endif
#if defined(S_IRUSR)
S_IRUSR,
#endif
#if defined(S_IWUSR)
S_IWUSR,
#endif
#if defined(S_IXUSR)
S_IXUSR,
#endif
#if defined(S_IRGRP)
S_IRGRP,
#endif
#if defined(S_IWGRP)
S_IWGRP,
#endif
#if defined(S_IXGRP)
S_IXGRP,
#endif
#if defined(S_IROTH)
S_IROTH,
#endif
#if defined(S_IWOTH)
S_IWOTH,
#endif
#if defined( S_IXOTH)
S_IXOTH,
#endif
0
};
/*
* BSD systems can return EFTYPE which we can ignore
* as a "known" error on invalid chmod mode bits
*/
#if defined(EFTYPE)
#define CHECK(x) \
if ((x) && \
((errno != ENOSYS) && \
(errno != EPERM) && \
(errno != EFTYPE))) \
return -1
#else
#define CHECK(x) \
if ((x) && \
((errno != ENOSYS) && \
(errno != EPERM))) \
return -1
#endif
/*
* do_fchmod()
* fchmod the 4 different masks from a mode flag, namely:
* mode flag
* all mode flags or'd together
* inverse mode flag
* inverse all mode flags or'd together
*/
static int do_fchmod(
const int fd,
const int bad_fd,
const size_t i,
const mode_t mask,
const mode_t all_mask)
{
CHECK(fchmod(fd, modes[i]) < 0);
CHECK(fchmod(fd, mask) < 0);
CHECK(fchmod(fd, modes[i] ^ all_mask) < 0);
CHECK(fchmod(fd, mask ^ all_mask) < 0);
/*
* Exercise bad fchmod, ignore failure
*/
VOID_RET(int, fchmod(bad_fd, modes[i]));
return 0;
}
/*
* do_chmod()
* chmod the 4 different masks from a mode flag, namely:
* mode flag
* all mode flags or'd together
* inverse mode flag
* inverse all mode flags or'd together
*/
static int do_chmod(
const int dfd,
const int bad_fd,
const char *filebase,
const char *filename,
const char *longpath,
const size_t i,
const mode_t mask,
const mode_t all_mask,
const size_t mode_count,
const int *mode_perms)
{
static int index;
(void)chmod(filename, (mode_t)mode_perms[index]);
index++;
index %= mode_count;
CHECK(chmod(filename, modes[i]) < 0);
CHECK(chmod(filename, mask) < 0);
CHECK(chmod(filename, modes[i] ^ all_mask) < 0);
CHECK(chmod(filename, mask ^ all_mask) < 0);
#if defined(HAVE_FCHMODAT)
if (dfd >= 0) {
CHECK(fchmodat(dfd, filebase, modes[i], 0) < 0);
CHECK(fchmodat(dfd, filebase, mask, 0) < 0);
CHECK(fchmodat(dfd, filebase, modes[i] ^ all_mask, 0) < 0);
CHECK(fchmodat(dfd, filebase, mask ^ all_mask, 0) < 0);
/*
* Exercise bad fchmodat, ignore failure
*/
VOID_RET(int, fchmodat(bad_fd, filebase, modes[i], 0));
}
#else
(void)dfd;
(void)bad_fd;
(void)filebase;
#endif
/*
* Exercise illegal filename
*/
VOID_RET(int, chmod("", modes[i]));
/*
* Exercise illegal overly long pathname
*/
VOID_RET(int, chmod(longpath, modes[i]));
return 0;
}
/*
* stress_chmod
* stress chmod
*/
static int stress_chmod(const stress_args_t *args)
{
const pid_t ppid = getppid();
int fd = -1, rc = EXIT_FAILURE, retries = 0, dfd = -1;
size_t i;
const int bad_fd = stress_get_bad_fd();
mode_t all_mask = 0;
char filename[PATH_MAX], pathname[PATH_MAX], longpath[PATH_MAX + 16];
char tmp[PATH_MAX], *filebase;
int *mode_perms;
size_t mode_count;
for (i = 0; modes[i]; i++)
all_mask |= modes[i];
mode_count = stress_flag_permutation((int)all_mask, &mode_perms);
/*
* Allow for multiple workers to chmod the *same* file
*/
stress_temp_dir(pathname, sizeof(pathname), args->name, ppid, 0);
if (mkdir(pathname, S_IRUSR | S_IRWXU) < 0) {
if (errno != EEXIST) {
rc = exit_status(errno);
pr_fail("%s: mkdir %s failed, errno=%d (%s)\n",
args->name, pathname, errno, strerror(errno));
return rc;
}
}
#if defined(O_DIRECTORY)
dfd = open(pathname, O_DIRECTORY | O_RDONLY);
#else
UNEXPECTED
#endif
stress_strnrnd(longpath, sizeof(longpath));
longpath[0] = '/';
(void)stress_temp_filename(filename, sizeof(filename),
args->name, ppid, 0, 0);
(void)shim_strlcpy(tmp, filename, sizeof(tmp));
filebase = basename(tmp);
if (args->instance == 0) {
if ((fd = creat(filename, S_IRUSR | S_IWUSR)) < 0) {
rc = exit_status(errno);
pr_fail("%s: create %s failed, errno=%d (%s)\n",
args->name, filename, errno, strerror(errno));
goto tidy;
}
} else {
/* Other instances must try to open the file */
for (;;) {
if ((fd = open(filename, O_RDWR, S_IRUSR | S_IWUSR)) > - 1)
break;
#if defined(__NetBSD__)
/* For some reason usleep blocks */
(void)shim_sched_yield();
#else
(void)shim_usleep(100000);
#endif
/* Timed out, then give up */
if (!keep_stressing_flag()) {
rc = EXIT_SUCCESS;
goto tidy;
}
/* Too many retries? */
if (++retries >= 10000) {
pr_err("%s: chmod: file %s took %d "
"retries to open and gave up "
"(instance %" PRIu32 ")\n",
args->name, filename, retries, args->instance);
goto tidy;
}
}
}
stress_set_proc_state(args->name, STRESS_STATE_RUN);
do {
mode_t mask = 0;
for (i = 0; modes[i]; i++) {
mask |= modes[i];
if (do_fchmod(fd, bad_fd, i, mask, all_mask) < 0) {
pr_fail("%s: fchmod failed, errno=%d (%s)\n",
args->name, errno, strerror(errno));
}
if (do_chmod(dfd, bad_fd, filebase, filename, longpath, i,
mask, all_mask, mode_count, mode_perms) < 0) {
if ((errno == ENOENT) || (errno == ENOTDIR)) {
/*
* File was removed during test by
* another worker
*/
rc = EXIT_SUCCESS;
goto tidy;
}
pr_fail("%s: chmod %s failed, errno=%d (%s)\n",
args->name, filename, errno, strerror(errno));
}
}
inc_counter(args);
} while (keep_stressing(args));
rc = EXIT_SUCCESS;
tidy:
stress_set_proc_state(args->name, STRESS_STATE_DEINIT);
#if defined(O_DIRECTORY)
if (dfd >= 0)
(void)close(dfd);
#else
UNEXPECTED
#endif
if (fd >= 0) {
(void)fchmod(fd, 0666);
(void)close(fd);
}
(void)shim_unlink(filename);
(void)shim_rmdir(pathname);
if (mode_perms)
free(mode_perms);
return rc;
}
stressor_info_t stress_chmod_info = {
.stressor = stress_chmod,
.class = CLASS_FILESYSTEM | CLASS_OS,
.verify = VERIFY_ALWAYS,
.help = help
};