xref: /freebsd/sys/vm/redzone.c (revision 924226fba12cc9a228c73b956e1b7fa24c60b055)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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 __FBSDID("$FreeBSD$");
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/stack.h>
36 #include <sys/sysctl.h>
37 
38 #include <vm/redzone.h>
39 
40 #ifdef KASAN
41 #error KASAN and DEBUG_REDZONE cannot be configured together
42 #endif
43 
44 static SYSCTL_NODE(_vm, OID_AUTO, redzone, CTLFLAG_RW | CTLFLAG_MPSAFE, NULL,
45     "RedZone data");
46 static u_long redzone_extra_mem = 0;
47 SYSCTL_ULONG(_vm_redzone, OID_AUTO, extra_mem, CTLFLAG_RD, &redzone_extra_mem,
48     0, "Extra memory allocated by redzone");
49 static int redzone_panic = 0;
50 SYSCTL_INT(_vm_redzone, OID_AUTO, panic, CTLFLAG_RWTUN, &redzone_panic, 0,
51     "Panic when buffer corruption is detected");
52 
53 #define	REDZONE_CHSIZE	(16)
54 #define	REDZONE_CFSIZE	(16)
55 #define	REDZONE_HSIZE	(sizeof(struct stack) + sizeof(u_long) + REDZONE_CHSIZE)
56 #define	REDZONE_FSIZE	(REDZONE_CFSIZE)
57 
58 static u_long
59 redzone_roundup(u_long n)
60 {
61 
62 	if (n < REDZONE_HSIZE)
63 		n = REDZONE_HSIZE;
64 	if (n <= 128)
65 		return (128);
66 	else if (n <= 256)
67 		return (256);
68 	else if (n <= 512)
69 		return (512);
70 	else if (n <= 1024)
71 		return (1024);
72 	else if (n <= 2048)
73 		return (2048);
74 	return (PAGE_SIZE);
75 }
76 
77 u_long
78 redzone_get_size(caddr_t naddr)
79 {
80 	u_long nsize;
81 
82 	bcopy(naddr - REDZONE_CHSIZE - sizeof(u_long), &nsize, sizeof(nsize));
83 	return (nsize);
84 }
85 
86 u_long
87 redzone_size_ntor(u_long nsize)
88 {
89 
90 	return (nsize + redzone_roundup(nsize) + REDZONE_FSIZE);
91 }
92 
93 void *
94 redzone_addr_ntor(caddr_t naddr)
95 {
96 
97 	return (naddr - redzone_roundup(redzone_get_size(naddr)));
98 }
99 
100 /*
101  * Set redzones and remember allocation backtrace.
102  */
103 void *
104 redzone_setup(caddr_t raddr, u_long nsize)
105 {
106 	struct stack st;
107 	caddr_t haddr, faddr;
108 
109 	atomic_add_long(&redzone_extra_mem, redzone_size_ntor(nsize) - nsize);
110 
111 	haddr = raddr + redzone_roundup(nsize) - REDZONE_HSIZE;
112 	faddr = haddr + REDZONE_HSIZE + nsize;
113 
114 	/* Redzone header. */
115 	stack_save(&st);
116 	bcopy(&st, haddr, sizeof(st));
117 	haddr += sizeof(st);
118 	bcopy(&nsize, haddr, sizeof(nsize));
119 	haddr += sizeof(nsize);
120 	memset(haddr, 0x42, REDZONE_CHSIZE);
121 	haddr += REDZONE_CHSIZE;
122 
123 	/* Redzone footer. */
124 	memset(faddr, 0x42, REDZONE_CFSIZE);
125 
126 	return (haddr);
127 }
128 
129 /*
130  * Verify redzones.
131  * This function is called on free() and realloc().
132  */
133 void
134 redzone_check(caddr_t naddr)
135 {
136 	struct stack ast, fst;
137 	caddr_t haddr, faddr;
138 	u_int ncorruptions;
139 	u_long nsize;
140 	int i;
141 
142 	haddr = naddr - REDZONE_HSIZE;
143 	bcopy(haddr, &ast, sizeof(ast));
144 	haddr += sizeof(ast);
145 	bcopy(haddr, &nsize, sizeof(nsize));
146 	haddr += sizeof(nsize);
147 
148 	atomic_subtract_long(&redzone_extra_mem,
149 	    redzone_size_ntor(nsize) - nsize);
150 
151 	/* Look for buffer underflow. */
152 	ncorruptions = 0;
153 	for (i = 0; i < REDZONE_CHSIZE; i++, haddr++) {
154 		if (*(u_char *)haddr != 0x42)
155 			ncorruptions++;
156 	}
157 	if (ncorruptions > 0) {
158 		printf("REDZONE: Buffer underflow detected. %u byte%s "
159 		    "corrupted before %p (%lu bytes allocated).\n",
160 		    ncorruptions, ncorruptions == 1 ? "" : "s", naddr, nsize);
161 		printf("Allocation backtrace:\n");
162 		stack_print_ddb(&ast);
163 		printf("Free backtrace:\n");
164 		stack_save(&fst);
165 		stack_print_ddb(&fst);
166 		if (redzone_panic)
167 			panic("Stopping here.");
168 	}
169 	faddr = naddr + nsize;
170 	/* Look for buffer overflow. */
171 	ncorruptions = 0;
172 	for (i = 0; i < REDZONE_CFSIZE; i++, faddr++) {
173 		if (*(u_char *)faddr != 0x42)
174 			ncorruptions++;
175 	}
176 	if (ncorruptions > 0) {
177 		printf("REDZONE: Buffer overflow detected. %u byte%s corrupted "
178 		    "after %p (%lu bytes allocated).\n", ncorruptions,
179 		    ncorruptions == 1 ? "" : "s", naddr + nsize, nsize);
180 		printf("Allocation backtrace:\n");
181 		stack_print_ddb(&ast);
182 		printf("Free backtrace:\n");
183 		stack_save(&fst);
184 		stack_print_ddb(&fst);
185 		if (redzone_panic)
186 			panic("Stopping here.");
187 	}
188 }
189