summary refs log tree commit diff
path: root/fail.c
blob: 5d1b80b17ce37303f9ec965e501118b34357e47a (plain) (blame)
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
/* fail - crash in various possible ways */

#include <linux/seccomp.h>

#include <sys/auxv.h>
#include <sys/mman.h>
#include <sys/prctl.h>
#include <sys/ptrace.h>
#include <sys/stat.h>
#include <sys/types.h>

#include <alloca.h>
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

void
segfault()
{
	volatile int *nullp = 0;

	*nullp = 42;
}

void _start();

__attribute__((__constructor__))
void
dlcrash()
{
	volatile int *nullp = 0;

	// don't trigger if we are executed as a program
	if (getauxval(AT_ENTRY) != (unsigned long)_start)
		*nullp = 1337;
}

void
uninterruptible()
{
	printf("pid %d is now in state D\n", getpid());
	vfork();
	pause();
	exit(1);
}

// can lockup your machine
void
oom()
{
	long c = 0;
	static long last = 0;

	int fd;
	fd = open("/proc/self/oom_score_adj", O_WRONLY);
	write(fd, "1000\n", 5);
	close(fd);

	while (1) {
		long *m = malloc(4096*4096);
		if (!m) {
			write(1, "\n", 1);
			exit(3);
		}
		m[0] = last;
		m[1] = c++;
		last = (long)m;
		write(1, ".", 1);
	}
}

void
recurse(char *n)
{
	char m[512];
	recurse(m);
	if (n)
		m[0] = n[0] = 42;
}

void
recurse_alloca(char *n)
{
	char *m = alloca(1024*1024);
	recurse_alloca(m);
	if (n)
		m[0] = n[0] = 42;
}

void
abortme()
{
	abort();
}

void
killme()
{
	raise(SIGKILL);
}

void illegalins()
{
#if defined(__x86_64__) || defined(__i386__)
	__asm__ __volatile__ (".byte 0x0f, 0xb9" : : : "memory");
#elif defined(__arm__)
	__asm__ __volatile__ (
	#ifndef __thumb__
		".word 0xe7f000f0"
	#else
		".short 0xdeff"
	#endif
		: : : "memory");
#elif defined(__aarch64__)
	__asm__ __volatile__ (".word 0x00800011" : : : "memory");
#else
	#error implement illegalins for this architecture
#endif
}

void trap()
{
	__builtin_trap();
}

int zero = 0;
void divtrap()
{
	zero = 1/zero;
}

void
violate_seccomp()
{
	prctl(PR_SET_SECCOMP, SECCOMP_MODE_STRICT);
	chdir("/");
}

void
mmap_sigbus()
{
	int fd = open("/bin/sh", O_RDONLY);
	if (fd < 0)
		exit(1);
	char *m = mmap(0, 10*1024*1024, PROT_READ, MAP_SHARED, fd, 0);
	if (m == MAP_FAILED)
		exit(1);

	((volatile char *)m)[9*1024*1024];

	exit(1);
}

int
main(int argc, char *argv[])
{
	int c;

	while ((c = getopt(argc, argv, "123DORabcdikrst")) != -1) {
		switch (c) {
		case '1': exit(-1); break;
		case '2': exit(2); break;
		case '3': exit(111); break;
		case 'D': uninterruptible(); break;
		case 'O': oom(); break;
		case 'R': recurse_alloca(0); break;
		case 'a': abortme(); break;
		case 'b': mmap_sigbus(); break;
		case 'c': violate_seccomp(); break;
		case 'd': divtrap(); break;
		case 'i': illegalins(); break;
		case 'k': killme(); break;
		case 'r': recurse(0); break;
		case 's': segfault(); break;
		case 't': trap(); break;
		}
	}

	write(2, "Usage: fail [-123ORabcdikrst]\n", 30);
	exit(1);
}