forked from ColinIanKing/stress-ng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stress-apparmor.c
669 lines (581 loc) · 13.8 KB
/
stress-apparmor.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
/*
* Copyright (C) 2013-2017 Canonical, Ltd.
*
* 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.
*
* This code is a complete clean re-write of the stress tool by
* Colin Ian King <[email protected]> and attempts to be
* backwardly compatible with the stress tool by Amos Waterland
* <[email protected]> but has more stress tests and more
* functionality.
*
*/
#include "stress-ng.h"
#if defined(__linux__) && defined(HAVE_APPARMOR)
#include <sys/select.h>
#include <sys/apparmor.h>
#define APPARMOR_BUF_SZ (4096)
typedef int (*apparmor_func)(const char *name,
const uint64_t max_ops, uint64_t *counter);
static volatile bool apparmor_run = true;
static char *apparmor_path = NULL;
extern char g_apparmor_data[];
extern const size_t g_apparmor_data_len;
/*
* stress_apparmor_supported()
* check if AppArmor is supported
*/
int stress_apparmor_supported(void)
{
int fd;
char path[PATH_MAX];
/* Initial sanity checks for AppArmor */
if (!aa_is_enabled()) {
pr_inf("apparmor stressor will be skipped, "
"AppArmor is not enabled\n");
return -1;
}
if (aa_find_mountpoint(&apparmor_path) < 0) {
pr_inf("apparmor stressor will be skipped, "
"cannot get AppArmor path, errno=%d (%s)\n",
errno, strerror(errno));
return -1;
}
/* ..and see if profiles are accessible */
(void)snprintf(path, sizeof(path), "%s/%s", apparmor_path, "profiles");
if ((fd = open(path, O_RDONLY)) < 0) {
switch (errno) {
case EACCES:
pr_inf("apparmor stressor will be skipped, "
"stress-ng needs to be run with root "
"privilege to access AppArmor /sys files.\n");
break;
case ENOENT:
pr_inf("apparmor stressor will be skipped, "
"AppArmor /sys files do not exist\n");
break;
default:
pr_inf("apparmor stressor will be skipped, "
"cannot access AppArmor /sys files: "
"errno=%d (%s)\n", errno, strerror(errno));
break;
}
free(apparmor_path);
apparmor_path = NULL;
return -1;
}
(void)close(fd);
return 0;
}
/*
* stress_apparmor_handler()
* signal handler
*/
static void MLOCKED stress_apparmor_handler(int dummy)
{
(void)dummy;
apparmor_run = false;
}
/*
* stress_apparmor_read()
* read a proc file
*/
static void stress_apparmor_read(const char *path)
{
int fd;
ssize_t i = 0;
char buffer[APPARMOR_BUF_SZ];
if ((fd = open(path, O_RDONLY | O_NONBLOCK)) < 0)
return;
/*
* Multiple randomly sized reads
*/
while (apparmor_run && (i < (4096 * APPARMOR_BUF_SZ))) {
ssize_t ret, sz = 1 + (mwc32() % sizeof(buffer));
redo:
if (!g_keep_stressing_flag)
break;
ret = read(fd, buffer, sz);
if (ret < 0) {
if ((errno == EAGAIN) || (errno == EINTR))
goto redo;
break;
}
if (ret < sz)
break;
i += sz;
}
(void)close(fd);
}
/*
* stress_apparmor_dir()
* read directory
*/
static void stress_apparmor_dir(
const char *path,
const bool recurse,
const int depth)
{
DIR *dp;
struct dirent *d;
if (!g_keep_stressing_flag)
return;
/* Don't want to go too deep */
if (depth > 8)
return;
dp = opendir(path);
if (dp == NULL)
return;
while ((d = readdir(dp)) != NULL) {
char name[PATH_MAX];
if (!g_keep_stressing_flag)
break;
if (!strcmp(d->d_name, ".") ||
!strcmp(d->d_name, ".."))
continue;
switch (d->d_type) {
case DT_DIR:
if (recurse) {
(void)snprintf(name, sizeof(name),
"%s/%s", path, d->d_name);
stress_apparmor_dir(name, recurse, depth + 1);
}
break;
case DT_REG:
(void)snprintf(name, sizeof(name),
"%s/%s", path, d->d_name);
stress_apparmor_read(name);
break;
default:
break;
}
}
(void)closedir(dp);
}
/*
* apparmor_spawn()
* spawn a process
*/
static pid_t apparmor_spawn(
const char *name,
const uint64_t max_ops,
uint64_t *counter,
apparmor_func func)
{
pid_t pid;
again:
pid = fork();
if (pid < 0) {
if (g_keep_stressing_flag && (errno == EAGAIN))
goto again;
return -1;
}
if (pid == 0) {
int ret;
if (stress_sighandler(name, SIGALRM,
stress_apparmor_handler, NULL) < 0)
exit(EXIT_FAILURE);
(void)setpgid(0, g_pgrp);
stress_parent_died_alarm();
ret = func(name, max_ops, counter);
free(apparmor_path);
exit(ret);
}
(void)setpgid(pid, g_pgrp);
return pid;
}
/*
* apparmor_stress_profiles()
* hammer profile reading
*/
static int apparmor_stress_profiles(
const char *name,
const uint64_t max_ops,
uint64_t *counter)
{
char path[PATH_MAX];
(void)name;
(void)snprintf(path, sizeof(path), "%s/%s", apparmor_path, "profiles");
do {
stress_apparmor_read(path);
(*counter)++;
} while (apparmor_run && (!max_ops || *counter < max_ops));
return EXIT_SUCCESS;
}
/*
* apparmor_stress_features()
* hammer features reading
*/
static int apparmor_stress_features(
const char *name,
const uint64_t max_ops,
uint64_t *counter)
{
char path[PATH_MAX];
(void)name;
(void)snprintf(path, sizeof(path), "%s/%s", apparmor_path, "features");
do {
stress_apparmor_dir(path, true, 0);
(*counter)++;
} while (apparmor_run && (!max_ops || *counter < max_ops));
return EXIT_SUCCESS;
}
/*
* apparmor_stress_kernel_interface()
* load/replace/unload stressing
*/
static int apparmor_stress_kernel_interface(
const char *name,
const uint64_t max_ops,
uint64_t *counter)
{
int rc = EXIT_SUCCESS;
aa_kernel_interface *kern_if;
/*
* Try and create a lot of contention and load
*/
do {
int ret = aa_kernel_interface_new(&kern_if, NULL, NULL);
if (ret < 0) {
pr_fail("%s: aa_kernel_interface_new() failed, "
"errno=%d (%s)\n", name,
errno, strerror(errno));
rc = EXIT_FAILURE;
break;
}
/*
* Loading a policy may fail if another stressor has
* already loaded the same policy, so we may get EEXIST
*/
ret = aa_kernel_interface_load_policy(kern_if,
g_apparmor_data, g_apparmor_data_len);
if (ret < 0) {
if (errno != EEXIST) {
pr_fail("%s: aa_kernel_interface_load_policy() failed, "
"errno=%d (%s)\n", name, errno, strerror(errno));
rc = EXIT_FAILURE;
}
}
/*
* Replacing should always be atomic and not fail when
* competing against other stressors if I understand the
* interface correctly.
*/
ret = aa_kernel_interface_replace_policy(kern_if,
g_apparmor_data, g_apparmor_data_len);
if (ret < 0) {
aa_kernel_interface_unref(kern_if);
pr_fail("%s: aa_kernel_interface_replace_policy() failed, "
"errno=%d (%s)\n", name, errno,
strerror(errno));
rc = EXIT_FAILURE;
}
/*
* Removal may fail if another stressor has already removed the
* policy, so we may get ENOENT
*/
ret = aa_kernel_interface_remove_policy(kern_if,
"/usr/bin/pulseaudio-eg");
if (ret < 0) {
if (errno != ENOENT) {
pr_fail("%s: aa_kernel_interface_remove_policy() failed, "
"errno=%d (%s)\n", name, errno,
strerror(errno));
rc = EXIT_FAILURE;
}
}
aa_kernel_interface_unref(kern_if);
(*counter)++;
} while (g_keep_stressing_flag && apparmor_run && (!max_ops || *counter < max_ops));
return rc;
}
/*
* apparmor_corrupt_flip_bits_random()
* bit flip random bits in data
*/
static inline void apparmor_corrupt_flip_bits_random(
char *copy, const size_t len)
{
uint32_t i;
const uint32_t n = mwc32() % 17;
for (i = 0; i < n; i++) {
uint32_t rnd = mwc32();
copy[rnd % len] ^= (1 << ((rnd >> 16) & 7));
}
}
/*
* apparmor_corrupt_flip_seq()
* sequentially flip bits
*/
static inline void apparmor_corrupt_flip_seq(
char *copy, const size_t len)
{
static size_t p = 0;
if (p > (len * sizeof(char)))
p = 0;
copy[p / sizeof(char)] ^= (1 << (p & 7));
p++;
}
/*
* apparmor_corrupt_clr_seq()
* sequentially clear bits
*/
static inline void apparmor_corrupt_clr_seq(
char *copy, const size_t len)
{
static size_t p = 0;
if (p > (len * sizeof(char)))
p = 0;
copy[p / sizeof(char)] &= ~(1 << (p & 7));
p++;
}
/*
* apparmor_corrupt_set_seq()
* sequentially set bits
*/
static inline void apparmor_corrupt_set_seq(
char *copy, const size_t len)
{
static size_t p = 0;
if (p > (len * sizeof(char)))
p = 0;
copy[p / sizeof(char)] |= (1 << (p & 7));
p++;
}
/*
* apparmor_corrupt_flip_byte_random()
* ramndomly flip entire bytes
*/
static inline void apparmor_corrupt_flip_byte_random(
char *copy, const size_t len)
{
copy[mwc32() % len] ^= 0xff;
}
/*
* apparmor_corrupt_clr_bits_random()
* randomly clear bits
*/
static inline void apparmor_corrupt_clr_bits_random(
char *copy, const size_t len)
{
uint32_t i;
const uint32_t n = mwc32() % 17;
for (i = 0; i < n; i++) {
uint32_t rnd = mwc32();
copy[rnd % len] &= ~(1 << ((rnd >> 16) & 7));
}
}
/*
* apparmor_corrupt_set_bits_random()
* randomly set bits
*/
static inline void apparmor_corrupt_set_bits_random(
char *copy, const size_t len)
{
uint32_t i;
const uint32_t n = mwc32() % 17;
for (i = 0; i < n; i++) {
uint32_t rnd = mwc32();
copy[rnd % len] |= (1 << ((rnd >> 16) & 7));
}
}
/*
* apparmor_corrupt_clr_byte_random()
* randomly clear an entire byte
*/
static inline void apparmor_corrupt_clr_byte_random(
char *copy, const size_t len)
{
copy[mwc32() % len] = 0;
}
/*
* apparmor_corrupt_set_byte_random()
* randomly set an entire byte
*/
static inline void apparmor_corrupt_set_byte_random(
char *copy, const size_t len)
{
copy[mwc32() % len] = 0xff;
}
/*
* apparmor_corrupt_flip_bits_random_burst
* randomly flip a burst of contiguous bits
*/
static inline void apparmor_corrupt_flip_bits_random_burst(
char *copy, const size_t len)
{
uint32_t i;
size_t p = (size_t)mwc32() % (len * sizeof(char));
for (i = 0; i < 32; i++) {
if (p > (len * sizeof(char)))
p = 0;
copy[p / sizeof(char)] ^= (1 << (p & 7));
p++;
}
}
/*
* apparmor_stress_corruption()
* corrupt data and see if we can oops the loader
* parser.
*/
static int apparmor_stress_corruption(
const char *name,
const uint64_t max_ops,
uint64_t *counter)
{
char copy[g_apparmor_data_len];
int rc = EXIT_SUCCESS;
aa_kernel_interface *kern_if;
/*
* Lets feed AppArmor with some bit corrupted data...
*/
do {
int ret;
(void)memcpy(copy, g_apparmor_data, g_apparmor_data_len);
/*
* Apply various corruption methods
*/
switch ((*counter) % 10) {
case 0:
apparmor_corrupt_flip_seq(copy, g_apparmor_data_len);
break;
case 1:
apparmor_corrupt_clr_seq(copy, g_apparmor_data_len);
break;
case 2:
apparmor_corrupt_set_seq(copy, g_apparmor_data_len);
break;
case 3:
apparmor_corrupt_flip_bits_random(copy,
g_apparmor_data_len);
break;
case 4:
apparmor_corrupt_flip_byte_random(copy,
g_apparmor_data_len);
break;
case 5:
apparmor_corrupt_clr_bits_random(copy,
g_apparmor_data_len);
break;
case 6:
apparmor_corrupt_set_bits_random(copy,
g_apparmor_data_len);
break;
case 7:
apparmor_corrupt_clr_byte_random(copy,
g_apparmor_data_len);
break;
case 8:
apparmor_corrupt_set_byte_random(copy,
g_apparmor_data_len);
break;
case 9:
apparmor_corrupt_flip_bits_random_burst(copy,
g_apparmor_data_len);
break;
default:
/* Should not happen */
break;
}
ret = aa_kernel_interface_new(&kern_if, NULL, NULL);
if (ret < 0) {
pr_fail("%s: aa_kernel_interface_new() failed, "
"errno=%d (%s)\n", name, errno,
strerror(errno));
return EXIT_FAILURE;
}
/*
* Expect EPROTO failures
*/
ret = aa_kernel_interface_replace_policy(kern_if,
copy, g_apparmor_data_len);
if (ret < 0) {
if ((errno != EPROTO) &&
(errno != EPROTONOSUPPORT)) {
pr_fail("%s: aa_kernel_interface_replace_policy() failed, "
"errno=%d (%s)\n", name, errno,
strerror(errno));
rc = EXIT_FAILURE;
}
}
aa_kernel_interface_unref(kern_if);
(*counter)++;
} while (g_keep_stressing_flag && apparmor_run &&
(!max_ops || *counter < max_ops));
return rc;
}
static const apparmor_func apparmor_funcs[] = {
apparmor_stress_profiles,
apparmor_stress_features,
apparmor_stress_kernel_interface,
apparmor_stress_corruption,
};
/*
* stress_apparmor()
* stress AppArmor
*/
int stress_apparmor(const args_t *args)
{
const size_t n = SIZEOF_ARRAY(apparmor_funcs);
const size_t counters_sz = n * sizeof(uint64_t);
pid_t pids[n];
size_t i;
uint64_t *counters, tmp_counter = 0, ops;
counters = mmap(NULL, counters_sz, PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_ANONYMOUS, -1, 0);
if (counters == MAP_FAILED) {
pr_err("%s: mmap failed: errno=%d (%s)\n",
args->name, errno, strerror(errno));
exit(EXIT_FAILURE);
}
(void)memset(counters, 0, counters_sz);
if ((args->max_ops > 0) && (args->max_ops < n))
ops = n;
else
ops = args->max_ops / n;
for (i = 0; i < n; i++) {
pids[i] = apparmor_spawn(args->name, ops,
&counters[i], apparmor_funcs[i]);
}
do {
(void)select(0, NULL, NULL, NULL, NULL);
for (i = 0; i < n; i++)
tmp_counter += counters[i];
} while (keep_stressing());
for (i = 0; i < n; i++) {
int status;
if (pids[i] >= 0) {
(void)kill(pids[i], SIGALRM);
(void)waitpid(pids[i], &status, 0);
*(args->counter) += counters[i];
}
}
(void)munmap(counters, counters_sz);
free(apparmor_path);
apparmor_path = NULL;
return EXIT_SUCCESS;
}
#else
int stress_apparmor_supported(void)
{
pr_inf("apparmor stressor will be skipped, "
"AppArmor is not available\n");
return -1;
}
int stress_apparmor(const args_t *args)
{
return stress_not_implemented(args);
}
#endif