aboutsummaryrefslogtreecommitdiff
path: root/fail.c
blob: 6e986ee4baff12d34f8903481470d9ffe90c1344 (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
/* fail - crash in various possible ways */

#include <linux/seccomp.h>

#include <sys/auxv.h>
#include <sys/prctl.h>
#include <sys/ptrace.h>
#include <sys/stat.h>
#include <sys/types.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[1024];
	recurse(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__ ("ud1" : : : "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("/");
}

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

	while ((c = getopt(argc, argv, "123DOacdikrst")) != -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 'a': abortme(); 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 [-123Oacdikrst]\n", 28);
	exit(1);
}