forked from ColinIanKing/stress-ng
-
Notifications
You must be signed in to change notification settings - Fork 1
/
stress-apparmor.c
753 lines (657 loc) · 15.9 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
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
/*
* 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"
#include "core-capabilities.h"
#if defined(HAVE_SYS_APPARMOR_H)
#include <sys/apparmor.h>
#endif
#if defined(HAVE_SYS_SELECT_H)
#include <sys/select.h>
#endif
static const stress_help_t help[] = {
{ NULL, "apparmor", "start N workers exercising AppArmor interfaces" },
{ NULL, "apparmor-ops N", "stop after N bogo AppArmor worker bogo operations" },
{ NULL, NULL, NULL }
};
#if defined(HAVE_APPARMOR) && \
defined(HAVE_SYS_APPARMOR_H) && \
defined(HAVE_SYS_SELECT_H)
#define APPARMOR_BUF_SZ (4096)
typedef int (*stress_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
*/
static int stress_apparmor_supported(const char *name)
{
int fd;
char path[PATH_MAX];
if (!stress_check_capability(SHIM_CAP_MAC_ADMIN)) {
pr_inf_skip("%s stressor will be skipped, "
"need to be running with CAP_SYS_ADMIN "
"rights for this stressor\n", name);
return -1;
}
/* Initial sanity checks for AppArmor */
if (!aa_is_enabled()) {
pr_inf_skip("apparmor stressor will be skipped, "
"AppArmor is not enabled\n");
return -1;
}
if (aa_find_mountpoint(&apparmor_path) < 0) {
pr_inf_skip("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)stress_mk_filename(path, sizeof(path), apparmor_path, "profiles");
if ((fd = open(path, O_RDONLY)) < 0) {
switch (errno) {
case EACCES:
pr_inf_skip("apparmor stressor will be skipped, "
"stress-ng needs CAP_MAC_ADMIN "
"privilege to access AppArmor /sys files.\n");
break;
case ENOENT:
pr_inf_skip("apparmor stressor will be skipped, "
"AppArmor /sys files do not exist\n");
break;
default:
pr_inf_skip("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_TEXT stress_apparmor_alrm_handler(int signum)
{
(void)signum;
apparmor_run = false;
}
static void MLOCKED_TEXT stress_apparmor_usr1_handler(int signum)
{
(void)signum;
}
/*
* 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 (keep_stressing_flag() &&
apparmor_run &&
(i < (4096 * APPARMOR_BUF_SZ))) {
ssize_t ret, sz = 1 + (stress_mwc32() % sizeof(buffer));
redo:
if (!keep_stressing_flag() || !apparmor_run)
break;
ret = read(fd, buffer, (size_t)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 (!keep_stressing_flag() || !apparmor_run)
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 (!keep_stressing_flag() || !apparmor_run)
break;
if (stress_is_dot_filename(d->d_name))
continue;
switch (d->d_type) {
case DT_DIR:
if (recurse) {
(void)stress_mk_filename(name, sizeof(name), path, d->d_name);
stress_apparmor_dir(name, recurse, depth + 1);
}
break;
case DT_REG:
(void)stress_mk_filename(name, sizeof(name), 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 stress_args_t *args,
const uint64_t max_ops,
uint64_t *counter,
stress_apparmor_func func)
{
pid_t pid;
again:
pid = fork();
if (pid < 0) {
if (stress_redo_fork(errno))
goto again;
return -1;
}
if (pid == 0) {
int ret = EXIT_SUCCESS;
if (!keep_stressing_flag())
goto abort;
if (stress_sighandler(args->name, SIGALRM,
stress_apparmor_alrm_handler, NULL) < 0) {
_exit(EXIT_FAILURE);
}
(void)setpgid(0, g_pgrp);
(void)sched_settings_apply(true);
stress_parent_died_alarm();
if (!keep_stressing_flag() || !apparmor_run)
goto abort;
ret = func(args->name, max_ops, counter);
abort:
free(apparmor_path);
(void)kill(args->pid, SIGUSR1);
_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)stress_mk_filename(path, sizeof(path), apparmor_path, "profiles");
do {
stress_apparmor_read(path);
(*counter)++;
} while (keep_stressing_flag() &&
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)stress_mk_filename(path, sizeof(path), apparmor_path, "features");
do {
stress_apparmor_dir(path, true, 0);
(*counter)++;
} while (keep_stressing_flag() &&
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);
/*
* 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) {
aa_kernel_interface_unref(kern_if);
pr_fail("%s: aa_kernel_interface_remove_policy() failed, errno=%d (%s)\n",
name, errno, strerror(errno));
rc = EXIT_FAILURE;
break;
}
}
aa_kernel_interface_unref(kern_if);
(*counter)++;
} while (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 = stress_mwc32() % 17;
for (i = 0; i < n; i++) {
uint32_t rnd = stress_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)
p = 0;
copy[p] ^= (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)
p = 0;
copy[p] &= ~(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)
p = 0;
copy[p] |= (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[stress_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 = stress_mwc32() % 17;
for (i = 0; i < n; i++) {
uint32_t rnd = stress_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 = stress_mwc32() % 17;
for (i = 0; i < n; i++) {
uint32_t rnd = stress_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[stress_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[stress_mwc32() % len] = (char)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)stress_mwc32() % (len * sizeof(*copy));
for (i = 0; i < 32; i++) {
if (p > len)
p = 0;
copy[p] ^= (1 << (p & 7));
p++;
}
}
/*
* apparmor_corrupt_flip_one_bit_random()
* randomly flip 1 bit
*/
static inline void apparmor_corrupt_flip_one_bit_random(
char *copy, const size_t len)
{
uint32_t rnd = stress_mwc32();
copy[rnd % len] ^= (1 << ((stress_mwc8() & 7)));
}
/*
* 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) % 11) {
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;
case 10:
apparmor_corrupt_flip_one_bit_random(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) &&
errno != ENOENT) {
pr_inf("%s: aa_kernel_interface_replace_policy() failed, "
"errno=%d (%s)\n", name, errno,
strerror(errno));
}
}
aa_kernel_interface_unref(kern_if);
(*counter)++;
} while (keep_stressing_flag() &&
apparmor_run &&
(!max_ops || (*counter < max_ops)));
return rc;
}
static const stress_apparmor_func apparmor_funcs[] = {
apparmor_stress_profiles,
apparmor_stress_features,
apparmor_stress_kernel_interface,
apparmor_stress_corruption,
};
/*
* stress_apparmor()
* stress AppArmor
*/
static int stress_apparmor(const stress_args_t *args)
{
const size_t n = SIZEOF_ARRAY(apparmor_funcs);
pid_t pids[n];
size_t i;
uint64_t *counters, max_ops, ops_per_child, ops;
const size_t counters_sz = n * sizeof(*counters);
if (stress_sighandler(args->name, SIGUSR1, stress_apparmor_usr1_handler, NULL) < 0)
return EXIT_FAILURE;
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));
return EXIT_FAILURE;
}
(void)memset(counters, 0, counters_sz);
if (args->max_ops > 0) {
if ((args->max_ops < n)) {
max_ops = n;
ops_per_child = 1;
} else {
max_ops = args->max_ops;
ops_per_child = (args->max_ops / n);
}
/*
* ops is the number of ops left over when dividing
* max_ops amongst the child processes
*/
ops = max_ops - (ops_per_child * n);
} else {
max_ops = 0;
ops_per_child = 0;
ops = 0;
}
for (i = 0; i < n; i++) {
pids[i] = apparmor_spawn(args,
ops_per_child + ((i == 0) ? ops : 0),
&counters[i], apparmor_funcs[i]);
}
stress_set_proc_state(args->name, STRESS_STATE_RUN);
while (keep_stressing(args)) {
uint64_t tmp_counter = 0;
(void)select(0, NULL, NULL, NULL, NULL);
for (i = 0; i < n; i++)
tmp_counter += counters[i];
if (max_ops && tmp_counter >= max_ops)
break;
}
stress_set_proc_state(args->name, STRESS_STATE_DEINIT);
/* Wakeup, time to die */
for (i = 0; i < n; i++) {
if (pids[i] >= 0)
(void)kill(pids[i], SIGALRM);
}
/* Now apply death grip */
for (i = 0; i < n; i++) {
int status;
if (pids[i] >= 0) {
(void)kill(pids[i], SIGKILL);
(void)shim_waitpid(pids[i], &status, 0);
add_counter(args, counters[i]);
}
}
(void)munmap(counters, counters_sz);
free(apparmor_path);
apparmor_path = NULL;
return EXIT_SUCCESS;
}
stressor_info_t stress_apparmor_info = {
.stressor = stress_apparmor,
.supported = stress_apparmor_supported,
.class = CLASS_OS | CLASS_SECURITY,
.help = help
};
#else
static int stress_apparmor_supported(const char *name)
{
pr_inf_skip("%s: stressor will be skipped, "
"AppArmor is not available\n", name);
return -1;
}
stressor_info_t stress_apparmor_info = {
.stressor = stress_not_implemented,
.supported = stress_apparmor_supported,
.class = CLASS_OS | CLASS_SECURITY,
.verify = VERIFY_ALWAYS,
.help = help
};
#endif