1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2019 Cyril Hrubis <chrubis@suse.cz>
4 */
5
6 /*
7 * Test that acces after guarded buffer causes segfault.
8 */
9
10 #include <stdlib.h>
11 #include <sys/wait.h>
12 #include "tst_test.h"
13
14 #define BUF1_LEN 10
15 #define BUF2_LEN 4096
16 #define BUF3_LEN 12004
17
18 static char *buf1;
19 static char *buf2;
20 static char *buf3;
21
do_test(unsigned int n)22 static void do_test(unsigned int n)
23 {
24 int pid;
25 int status;
26
27 if (n == 6) {
28 buf1[-1] = 0;
29 buf3[-1] = 0;
30 tst_res(TPASS, "Buffers dirtied!");
31 }
32
33 pid = SAFE_FORK();
34 if (!pid) {
35 switch (n) {
36 case 0:
37 buf1[BUF1_LEN - 1] = 0;
38 break;
39 case 1:
40 buf2[BUF2_LEN - 1] = 0;
41 break;
42 case 2:
43 buf3[BUF3_LEN - 1] = 0;
44 break;
45 case 3:
46 buf1[BUF1_LEN] = 0;
47 break;
48 case 4:
49 buf2[BUF2_LEN] = 0;
50 break;
51 case 5:
52 buf3[BUF3_LEN] = 0;
53 break;
54 case 6:
55 buf1[-2] = 0;
56 buf3[-2] = 0;
57 break;
58 }
59
60 exit(0);
61 }
62
63 SAFE_WAITPID(pid, &status, 0);
64
65 if (n == 6)
66 return;
67
68 if (n < 3) {
69 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
70 tst_res(TPASS, "Exitted normally");
71 return;
72 }
73 } else {
74 if (WIFSIGNALED(status) && WTERMSIG(status) == SIGSEGV) {
75 tst_res(TPASS, "Killed by SIGSEGV");
76 return;
77 }
78 }
79
80 tst_res(TFAIL, "Child %s", tst_strstatus(status));
81 }
82
83 static struct tst_test test = {
84 .forks_child = 1,
85 .test = do_test,
86 .tcnt = 7,
87 .bufs = (struct tst_buffers []) {
88 {&buf1, .size = BUF1_LEN},
89 {&buf2, .size = BUF2_LEN},
90 {&buf3, .size = BUF3_LEN},
91 {}
92 }
93 };
94