xref: /freebsd/sys/vm/redzone.c (revision da5432eda807c4b7232d030d5157d5b417ea4f52)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2006 Pawel Jakub Dawidek <pjd@FreeBSD.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/stack.h>
34 #include <sys/sysctl.h>
35 
36 #include <vm/redzone.h>
37 
38 #ifdef KASAN
39 #error KASAN and DEBUG_REDZONE cannot be configured together
40 #endif
41 
42 static SYSCTL_NODE(_vm, OID_AUTO, redzone, CTLFLAG_RW | CTLFLAG_MPSAFE, NULL,
43     "RedZone data");
44 static u_long redzone_extra_mem = 0;
45 SYSCTL_ULONG(_vm_redzone, OID_AUTO, extra_mem, CTLFLAG_RD, &redzone_extra_mem,
46     0, "Extra memory allocated by redzone");
47 static int redzone_panic = 0;
48 SYSCTL_INT(_vm_redzone, OID_AUTO, panic, CTLFLAG_RWTUN, &redzone_panic, 0,
49     "Panic when buffer corruption is detected");
50 
51 #define	REDZONE_CHSIZE	(16)
52 #define	REDZONE_CFSIZE	(16)
53 #define	REDZONE_HSIZE	(sizeof(struct stack) + sizeof(u_long) + REDZONE_CHSIZE)
54 #define	REDZONE_FSIZE	(REDZONE_CFSIZE)
55 
56 static u_long
57 redzone_roundup(u_long n)
58 {
59 
60 	if (n < REDZONE_HSIZE)
61 		n = REDZONE_HSIZE;
62 	if (n <= 128)
63 		return (128);
64 	else if (n <= 256)
65 		return (256);
66 	else if (n <= 512)
67 		return (512);
68 	else if (n <= 1024)
69 		return (1024);
70 	else if (n <= 2048)
71 		return (2048);
72 	return (PAGE_SIZE);
73 }
74 
75 u_long
76 redzone_get_size(caddr_t naddr)
77 {
78 	u_long nsize;
79 
80 	bcopy(naddr - REDZONE_CHSIZE - sizeof(u_long), &nsize, sizeof(nsize));
81 	return (nsize);
82 }
83 
84 u_long
85 redzone_size_ntor(u_long nsize)
86 {
87 
88 	return (nsize + redzone_roundup(nsize) + REDZONE_FSIZE);
89 }
90 
91 void *
92 redzone_addr_ntor(caddr_t naddr)
93 {
94 
95 	return (naddr - redzone_roundup(redzone_get_size(naddr)));
96 }
97 
98 /*
99  * Set redzones and remember allocation backtrace.
100  */
101 void *
102 redzone_setup(caddr_t raddr, u_long nsize)
103 {
104 	struct stack st;
105 	caddr_t haddr, faddr;
106 
107 	atomic_add_long(&redzone_extra_mem, redzone_size_ntor(nsize) - nsize);
108 
109 	haddr = raddr + redzone_roundup(nsize) - REDZONE_HSIZE;
110 	faddr = haddr + REDZONE_HSIZE + nsize;
111 
112 	/* Redzone header. */
113 	stack_save(&st);
114 	bcopy(&st, haddr, sizeof(st));
115 	haddr += sizeof(st);
116 	bcopy(&nsize, haddr, sizeof(nsize));
117 	haddr += sizeof(nsize);
118 	memset(haddr, 0x42, REDZONE_CHSIZE);
119 	haddr += REDZONE_CHSIZE;
120 
121 	/* Redzone footer. */
122 	memset(faddr, 0x42, REDZONE_CFSIZE);
123 
124 	return (haddr);
125 }
126 
127 /*
128  * Verify redzones.
129  * This function is called on free() and realloc().
130  */
131 void
132 redzone_check(caddr_t naddr)
133 {
134 	struct stack ast, fst;
135 	caddr_t haddr, faddr;
136 	u_int ncorruptions;
137 	u_long nsize;
138 	int i;
139 
140 	haddr = naddr - REDZONE_HSIZE;
141 	bcopy(haddr, &ast, sizeof(ast));
142 	haddr += sizeof(ast);
143 	bcopy(haddr, &nsize, sizeof(nsize));
144 	haddr += sizeof(nsize);
145 
146 	atomic_subtract_long(&redzone_extra_mem,
147 	    redzone_size_ntor(nsize) - nsize);
148 
149 	/* Look for buffer underflow. */
150 	ncorruptions = 0;
151 	for (i = 0; i < REDZONE_CHSIZE; i++, haddr++) {
152 		if (*(u_char *)haddr != 0x42)
153 			ncorruptions++;
154 	}
155 	if (ncorruptions > 0) {
156 		printf("REDZONE: Buffer underflow detected. %u byte%s "
157 		    "corrupted before %p (%lu bytes allocated).\n",
158 		    ncorruptions, ncorruptions == 1 ? "" : "s", naddr, nsize);
159 		printf("Allocation backtrace:\n");
160 		stack_print_ddb(&ast);
161 		printf("Free backtrace:\n");
162 		stack_save(&fst);
163 		stack_print_ddb(&fst);
164 		if (redzone_panic)
165 			panic("Stopping here.");
166 	}
167 	faddr = naddr + nsize;
168 	/* Look for buffer overflow. */
169 	ncorruptions = 0;
170 	for (i = 0; i < REDZONE_CFSIZE; i++, faddr++) {
171 		if (*(u_char *)faddr != 0x42)
172 			ncorruptions++;
173 	}
174 	if (ncorruptions > 0) {
175 		printf("REDZONE: Buffer overflow detected. %u byte%s corrupted "
176 		    "after %p (%lu bytes allocated).\n", ncorruptions,
177 		    ncorruptions == 1 ? "" : "s", naddr + nsize, nsize);
178 		printf("Allocation backtrace:\n");
179 		stack_print_ddb(&ast);
180 		printf("Free backtrace:\n");
181 		stack_save(&fst);
182 		stack_print_ddb(&fst);
183 		if (redzone_panic)
184 			panic("Stopping here.");
185 	}
186 }
187