xref: /netbsd-src/sys/kern/kern_crashme.c (revision 122b5006ee1bd67145794b4cde92f4fe4781a5ec)
1 /*	$NetBSD: kern_crashme.c,v 1.4 2021/09/07 11:00:13 riastradh Exp $	*/
2 
3 /*
4  * Copyright (c) 2018, 2019 Matthew R. Green
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  * 3. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 /*
32  * kern_crashme.c:  special debugging routines, designed for debugging
33  * kernel crashes.
34  *
35  * supports crashme sysctl nodes, to test various ways the system can
36  * panic or crash.  you can add and remove nodes.
37  */
38 
39 #ifdef _KERNEL_OPT
40 #include "opt_ddb.h"
41 #endif
42 
43 #include <sys/param.h>
44 #include <sys/sysctl.h>
45 #include <sys/systm.h>
46 #include <sys/kthread.h>
47 #include <sys/kmem.h>
48 #include <sys/mutex.h>
49 #include <sys/crashme.h>
50 
51 #ifdef DDB
52 #include <ddb/ddb.h>
53 #endif
54 
55 #define DPRINTF(fmt, ...) \
56 	printf("%s:%d: " fmt "\n", __func__, __LINE__, ## __VA_ARGS__)
57 
58 static int crashme_sysctl_forwarder(SYSCTLFN_PROTO);
59 
60 static int crashme_panic(int);
61 static int crashme_null_deref(int);
62 #ifdef DDB
63 static int crashme_ddb(int);
64 #endif
65 
66 #define CMNODE(name, lname, func)	\
67     {					\
68 	.cn_name = name,		\
69 	.cn_longname = lname,		\
70 	.cn_fn = func,			\
71     }
72 
73 static crashme_node nodes[] = {
74     CMNODE("panic", "plain old panic", crashme_panic),
75     CMNODE("null_deref", "null dereference", crashme_null_deref),
76 #ifdef DDB
77     CMNODE("ddb", "enter ddb directly", crashme_ddb),
78 #endif
79 };
80 static crashme_node *first_node;
81 static kmutex_t crashme_lock;
82 static const struct sysctlnode *crashme_root = NULL;
83 static bool crashme_enable = 0;
84 
85 /*
86  * add a crashme node dynamically.  return -1 on failure, 0 on success.
87  */
88 int
89 crashme_add(crashme_node *ncn)
90 {
91 	int rv = -1;
92 	crashme_node *cn;
93 	crashme_node *last = NULL;
94 
95 	if (crashme_root == NULL)
96 		return -1;
97 
98 	mutex_enter(&crashme_lock);
99 	for (cn = first_node; cn; last = cn, cn = cn->cn_next) {
100 		if (strcmp(cn->cn_name, ncn->cn_name) == 0)
101 			break;
102 	}
103 	if (!cn) {
104 		ncn->cn_next = NULL;
105 
106 		rv = sysctl_createv(NULL, 0,
107 				    &crashme_root, &ncn->cn_sysctl,
108 				    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
109 				    CTLTYPE_INT, ncn->cn_name,
110 				    SYSCTL_DESCR(ncn->cn_longname),
111 				    crashme_sysctl_forwarder, 0,
112 				    NULL, 0,
113 				    CTL_CREATE, CTL_EOL);
114 
115 		/* don't insert upon failure */
116 		if (rv == 0) {
117 			if (last)
118 				last->cn_next = ncn;
119 			if (first_node == NULL)
120 				first_node = ncn;
121 		}
122 	}
123 	mutex_exit(&crashme_lock);
124 
125 	return rv;
126 }
127 
128 /*
129  * remove a crashme node.  return -1 on failure, 0 on success.
130  */
131 int
132 crashme_remove(crashme_node *rcn)
133 {
134 	crashme_node *cn, *prev = NULL;
135 
136 	mutex_enter(&crashme_lock);
137 	for (cn = first_node; cn; prev = cn, cn = cn->cn_next) {
138 		int rv;
139 
140 		if (cn != rcn)
141 			continue;
142 
143 		if (cn == first_node)
144 			first_node = cn->cn_next;
145 		if (prev)
146 			prev->cn_next = cn->cn_next;
147 
148 		if ((rv = sysctl_destroyv(NULL, CTL_DEBUG, crashme_root,
149 					  cn->cn_name, CTL_EOL)) == 0)
150 			printf("%s: unable to remove %s from sysctl\n",
151 			       __func__, cn->cn_name);
152 		break;
153 	}
154 	mutex_exit(&crashme_lock);
155 
156 	if (cn == NULL)
157 		return -1;
158 
159 	return 0;
160 }
161 
162 /*
163  * list or execute a crashme node
164  */
165 static int
166 crashme_sysctl_forwarder(SYSCTLFN_ARGS)
167 {
168 	struct sysctlnode node;
169 	crashme_node *cn;
170 	int error, arg = 0;
171 
172 	for (cn = first_node; cn; cn = cn->cn_next) {
173 		if (cn->cn_sysctl == rnode)
174 			break;
175 	}
176 	if (!cn) {
177 		return EINVAL;
178 	}
179 
180 	node = *rnode;
181 	node.sysctl_data = &arg;
182 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
183 	if (error || newp == NULL)
184 		return (error);
185 
186 	if (!crashme_enable)
187 		return EACCES;
188 
189 	DPRINTF("invoking \"%s\" (%s)", cn->cn_name, cn->cn_longname);
190 	if ((*cn->cn_fn)(arg) != 0)
191 		panic("crashme on %s failed!\n", cn->cn_name);
192 	return 0;
193 }
194 
195 /*
196  * register the various nodes with sysctl.
197  */
198 SYSCTL_SETUP(selfdebug_crashme, "sysctl crashme setup")
199 {
200 	int rv;
201 	size_t n;
202 
203 	mutex_init(&crashme_lock, MUTEX_DEFAULT, IPL_NONE);
204 
205 	rv = sysctl_createv(NULL, 0, NULL, &crashme_root,
206 	    CTLFLAG_PERMANENT,
207 	    CTLTYPE_NODE, "crashme",
208 	    SYSCTL_DESCR("Crashme options"),
209 	    NULL, 0, NULL, 0,
210 	    CTL_DEBUG, CTL_CREATE, CTL_EOL);
211 
212 	if (rv != 0 || crashme_root == NULL)
213 		return;
214 
215 	rv = sysctl_createv(NULL, 0, NULL, NULL,
216 	    CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
217 	    CTLTYPE_BOOL, "crashme_enable",
218 	    SYSCTL_DESCR("Enable crashme"),
219 	    NULL, 0, &crashme_enable, 0,
220 	    CTL_DEBUG, CTL_CREATE, CTL_EOL);
221 
222 	for (n = 0; n < __arraycount(nodes); n++) {
223 		crashme_node *cn = &nodes[n];
224 
225 		rv = crashme_add(cn);
226 
227 		/* don't insert */
228 		if (rv != 0)
229 			continue;
230 	}
231 }
232 
233 /*
234  * actual panic functions below
235  */
236 static int
237 crashme_panic(int flags)
238 {
239 
240 	panic("crashme plain old panic");
241 	return -1;
242 }
243 
244 static int
245 crashme_null_deref(int flags)
246 {
247 
248 	*(volatile char *)0 = 0;
249 	return -1;
250 }
251 
252 #ifdef DDB
253 static int
254 crashme_ddb(int flags)
255 {
256 
257 	Debugger();
258 	return 0;
259 }
260 #endif
261