summaryrefslogtreecommitdiffstats
path: root/tools/testing/selftests/x86/nx_stack.c
blob: ea4a4e246879ab1b350f9625481d41eebb8dbbc1 (plain)
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
/*
 * Copyright (c) 2023 Alexey Dobriyan <adobriyan@gmail.com>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */
/*
 * Test that userspace stack is NX. Requires linking with -Wl,-z,noexecstack
 * because I don't want to bother with PT_GNU_STACK detection.
 *
 * Fill the stack with INT3's and then try to execute some of them:
 * SIGSEGV -- good, SIGTRAP -- bad.
 *
 * Regular stack is completely overwritten before testing.
 * Test doesn't exit SIGSEGV handler after first fault at INT3.
 */
#undef _GNU_SOURCE
#define _GNU_SOURCE
#undef NDEBUG
#include <assert.h>
#include <signal.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/resource.h>
#include <unistd.h>

#define PAGE_SIZE 4096

/*
 * This is memset(rsp, 0xcc, -1); but down.
 * It will SIGSEGV when bottom of the stack is reached.
 * Byte-size access is important! (see rdi tweak in the signal handler).
 */
void make_stack1(void);
asm(
".pushsection .text\n"
".globl make_stack1\n"
".align 16\n"
"make_stack1:\n"
	"mov $0xcc, %al\n"
#if defined __amd64__
	"mov %rsp, %rdi\n"
	"mov $-1, %rcx\n"
#elif defined __i386__
	"mov %esp, %edi\n"
	"mov $-1, %ecx\n"
#else
#error
#endif
	"std\n"
	"rep stosb\n"
	/* unreachable */
	"hlt\n"
".type make_stack1,@function\n"
".size make_stack1,.-make_stack1\n"
".popsection\n"
);

/*
 * memset(p, 0xcc, -1);
 * It will SIGSEGV when top of the stack is reached.
 */
void make_stack2(uint64_t p);
asm(
".pushsection .text\n"
".globl make_stack2\n"
".align 16\n"
"make_stack2:\n"
	"mov $0xcc, %al\n"
#if defined __amd64__
	"mov $-1, %rcx\n"
#elif defined __i386__
	"mov $-1, %ecx\n"
#else
#error
#endif
	"cld\n"
	"rep stosb\n"
	/* unreachable */
	"hlt\n"
".type make_stack2,@function\n"
".size make_stack2,.-make_stack2\n"
".popsection\n"
);

static volatile int test_state = 0;
static volatile unsigned long stack_min_addr;

#if defined __amd64__
#define RDI	REG_RDI
#define RIP	REG_RIP
#define RIP_STRING "rip"
#elif defined __i386__
#define RDI	REG_EDI
#define RIP	REG_EIP
#define RIP_STRING "eip"
#else
#error
#endif

static void sigsegv(int _, siginfo_t *__, void *uc_)
{
	/*
	 * Some Linux versions didn't clear DF before entering signal
	 * handler. make_stack1() doesn't have a chance to clear DF
	 * either so we clear it by hand here.
	 */
	asm volatile ("cld" ::: "memory");

	ucontext_t *uc = uc_;

	if (test_state == 0) {
		/* Stack is faulted and cleared from RSP to the lowest address. */
		stack_min_addr = ++uc->uc_mcontext.gregs[RDI];
		if (1) {
			printf("stack min %lx\n", stack_min_addr);
		}
		uc->uc_mcontext.gregs[RIP] = (uintptr_t)&make_stack2;
		test_state = 1;
	} else if (test_state == 1) {
		/* Stack has been cleared from top to bottom. */
		unsigned long stack_max_addr = uc->uc_mcontext.gregs[RDI];
		if (1) {
			printf("stack max %lx\n", stack_max_addr);
		}
		/* Start faulting pages on stack and see what happens. */
		uc->uc_mcontext.gregs[RIP] = stack_max_addr - PAGE_SIZE;
		test_state = 2;
	} else if (test_state == 2) {
		/* Stack page is NX -- good, test next page. */
		uc->uc_mcontext.gregs[RIP] -= PAGE_SIZE;
		if (uc->uc_mcontext.gregs[RIP] == stack_min_addr) {
			/* One more SIGSEGV and test ends. */
			test_state = 3;
		}
	} else {
		printf("PASS\tAll stack pages are NX\n");
		_exit(EXIT_SUCCESS);
	}
}

static void sigtrap(int _, siginfo_t *__, void *uc_)
{
	const ucontext_t *uc = uc_;
	unsigned long rip = uc->uc_mcontext.gregs[RIP];
	printf("FAIL\texecutable page on the stack: " RIP_STRING " %lx\n", rip);
	_exit(EXIT_FAILURE);
}

int main(void)
{
	{
		struct sigaction act = {};
		sigemptyset(&act.sa_mask);
		act.sa_flags = SA_SIGINFO;
		act.sa_sigaction = &sigsegv;
		int rv = sigaction(SIGSEGV, &act, NULL);
		assert(rv == 0);
	}
	{
		struct sigaction act = {};
		sigemptyset(&act.sa_mask);
		act.sa_flags = SA_SIGINFO;
		act.sa_sigaction = &sigtrap;
		int rv = sigaction(SIGTRAP, &act, NULL);
		assert(rv == 0);
	}
	{
		struct rlimit rlim;
		int rv = getrlimit(RLIMIT_STACK, &rlim);
		assert(rv == 0);
		/* Cap stack at time-honored 8 MiB value. */
		rlim.rlim_max = rlim.rlim_cur;
		if (rlim.rlim_max > 8 * 1024 * 1024) {
			rlim.rlim_max = 8 * 1024 * 1024;
		}
		rv = setrlimit(RLIMIT_STACK, &rlim);
		assert(rv == 0);
	}
	{
		/*
		 * We don't know now much stack SIGSEGV handler uses.
		 * Bump this by 1 page every time someone complains,
		 * or rewrite it in assembly.
		 */
		const size_t len = SIGSTKSZ;
		void *p = mmap(NULL, len, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
		assert(p != MAP_FAILED);
		stack_t ss = {};
		ss.ss_sp = p;
		ss.ss_size = len;
		int rv = sigaltstack(&ss, NULL);
		assert(rv == 0);
	}
	make_stack1();
	/*
	 * Unreachable, but if _this_ INT3 is ever reached, it's a bug somewhere.
	 * Fold it into main SIGTRAP pathway.
	 */
	__builtin_trap();
}