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