xref: /freebsd/lib/libkvm/kvm_vnet.c (revision b13788e396c2b24f88697e7d4a74bab429ef4d0c)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2009 Robert N. M. Watson
5  * Copyright (c) 2009 Bjoern A. Zeeb <bz@FreeBSD.org>
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 #include <sys/param.h>
34 
35 #define	_WANT_PRISON
36 #define	_WANT_UCRED
37 #define	_WANT_VNET
38 
39 #include <sys/_lock.h>
40 #include <sys/_mutex.h>
41 #include <sys/_task.h>
42 #include <sys/jail.h>
43 #include <sys/proc.h>
44 #include <sys/types.h>
45 
46 #include <stdbool.h>
47 #include <net/vnet.h>
48 
49 #include <kvm.h>
50 #include <limits.h>
51 #include <stdlib.h>
52 #include <unistd.h>
53 
54 #include "kvm_private.h"
55 
56 /*
57  * Set up libkvm to handle virtual network stack symbols by selecting a
58  * starting pid.
59  */
60 int
61 _kvm_vnet_selectpid(kvm_t *kd, pid_t pid)
62 {
63 	struct proc proc;
64 	struct ucred cred;
65 	struct prison prison;
66 	struct vnet vnet;
67 	struct kvm_nlist nl[] = {
68 		/*
69 		 * Note: kvm_nlist strips the first '_' so add an extra one
70 		 * here to __{start,stop}_set_vnet.
71 		 */
72 #define	NLIST_START_VNET	0
73 		{ .n_name = "___start_" VNET_SETNAME },
74 #define	NLIST_STOP_VNET		1
75 		{ .n_name = "___stop_" VNET_SETNAME },
76 #define	NLIST_VNET_HEAD		2
77 		{ .n_name = "vnet_head" },
78 #define	NLIST_ALLPROC		3
79 		{ .n_name = "allproc" },
80 #define	NLIST_DUMPTID		4
81 		{ .n_name = "dumptid" },
82 #define	NLIST_PROC0		5
83 		{ .n_name = "proc0" },
84 		{ .n_name = NULL },
85 	};
86 	uintptr_t procp, credp;
87 #define	VMCORE_VNET_OF_PROC0
88 #ifndef VMCORE_VNET_OF_PROC0
89 	struct thread td;
90 	uintptr_t tdp;
91 #endif
92 	lwpid_t dumptid;
93 
94 	/*
95 	 * XXX: This only works for native kernels for now.
96 	 */
97 	if (!kvm_native(kd))
98 		return (-1);
99 
100 	/*
101 	 * Locate and cache locations of important symbols
102 	 * using the internal version of _kvm_nlist, turning
103 	 * off initialization to avoid recursion in case of
104 	 * unresolveable symbols.
105 	 */
106 	if (_kvm_nlist(kd, nl, 0) != 0) {
107 		/*
108 		 * XXX-BZ: ___start_/___stop_VNET_SETNAME may fail.
109 		 * For now do not report an error here as we are called
110 		 * internally and in `void context' until we merge the
111 		 * functionality to optionally activate this into programs.
112 		 * By that time we can properly fail and let the callers
113 		 * handle the error.
114 		 */
115 		/* _kvm_err(kd, kd->program, "%s: no namelist", __func__); */
116 		return (-1);
117 	}
118 
119 	/*
120 	 * Auto-detect if this is a crashdump by reading dumptid.
121 	 */
122 	dumptid = 0;
123 	if (nl[NLIST_DUMPTID].n_value) {
124 		if (kvm_read(kd, nl[NLIST_DUMPTID].n_value, &dumptid,
125 		    sizeof(dumptid)) != sizeof(dumptid)) {
126 			_kvm_err(kd, kd->program, "%s: dumptid", __func__);
127 			return (-1);
128 		}
129 	}
130 
131 	/*
132 	 * First, find the process for this pid.  If we are working on a
133 	 * dump, either locate the thread dumptid is referring to or proc0.
134 	 * Based on either, take the address of the ucred.
135 	 */
136 	credp = 0;
137 
138 	procp = nl[NLIST_ALLPROC].n_value;
139 #ifdef VMCORE_VNET_OF_PROC0
140 	if (dumptid > 0) {
141 		procp = nl[NLIST_PROC0].n_value;
142 		pid = 0;
143 	}
144 #endif
145 	while (procp != 0) {
146 		if (kvm_read(kd, procp, &proc, sizeof(proc)) != sizeof(proc)) {
147 			_kvm_err(kd, kd->program, "%s: proc", __func__);
148 			return (-1);
149 		}
150 #ifndef VMCORE_VNET_OF_PROC0
151 		if (dumptid > 0) {
152 			tdp = (uintptr_t)TAILQ_FIRST(&proc.p_threads);
153 			while (tdp != 0) {
154 				if (kvm_read(kd, tdp, &td, sizeof(td)) !=
155 				    sizeof(td)) {
156 					_kvm_err(kd, kd->program, "%s: thread",
157 					    __func__);
158 					return (-1);
159 				}
160 				if (td.td_tid == dumptid) {
161 					credp = (uintptr_t)td.td_ucred;
162 					break;
163 				}
164 				tdp = (uintptr_t)TAILQ_NEXT(&td, td_plist);
165 			}
166 		} else
167 #endif
168 		if (proc.p_pid == pid)
169 			credp = (uintptr_t)proc.p_ucred;
170 		if (credp != 0)
171 			break;
172 		procp = (uintptr_t)LIST_NEXT(&proc, p_list);
173 	}
174 	if (credp == 0) {
175 		_kvm_err(kd, kd->program, "%s: pid/tid not found", __func__);
176 		return (-1);
177 	}
178 	if (kvm_read(kd, (uintptr_t)credp, &cred, sizeof(cred)) !=
179 	    sizeof(cred)) {
180 		_kvm_err(kd, kd->program, "%s: cred", __func__);
181 		return (-1);
182 	}
183 	if (cred.cr_prison == NULL) {
184 		_kvm_err(kd, kd->program, "%s: no jail", __func__);
185 		return (-1);
186 	}
187 	if (kvm_read(kd, (uintptr_t)cred.cr_prison, &prison, sizeof(prison)) !=
188 	    sizeof(prison)) {
189 		_kvm_err(kd, kd->program, "%s: prison", __func__);
190 		return (-1);
191 	}
192 	if (prison.pr_vnet == NULL) {
193 		_kvm_err(kd, kd->program, "%s: no vnet", __func__);
194 		return (-1);
195 	}
196 	if (kvm_read(kd, (uintptr_t)prison.pr_vnet, &vnet, sizeof(vnet)) !=
197 	    sizeof(vnet)) {
198 		_kvm_err(kd, kd->program, "%s: vnet", __func__);
199 		return (-1);
200 	}
201 	if (vnet.vnet_magic_n != VNET_MAGIC_N) {
202 		_kvm_err(kd, kd->program, "%s: invalid vnet magic#", __func__);
203 		return (-1);
204 	}
205 	kd->vnet_initialized = 1;
206 	kd->vnet_start = nl[NLIST_START_VNET].n_value;
207 	kd->vnet_stop = nl[NLIST_STOP_VNET].n_value;
208 	kd->vnet_current = (uintptr_t)prison.pr_vnet;
209 	kd->vnet_base = vnet.vnet_data_base;
210 	return (0);
211 }
212 
213 /*
214  * Check whether the vnet module has been initialized successfully
215  * or not, initialize it if permitted.
216  */
217 int
218 _kvm_vnet_initialized(kvm_t *kd, int intialize)
219 {
220 
221 	if (kd->vnet_initialized || !intialize)
222 		return (kd->vnet_initialized);
223 
224 	(void) _kvm_vnet_selectpid(kd, getpid());
225 
226 	return (kd->vnet_initialized);
227 }
228 
229 /*
230  * Check whether the value is within the vnet symbol range and
231  * only if so adjust the offset relative to the current base.
232  */
233 kvaddr_t
234 _kvm_vnet_validaddr(kvm_t *kd, kvaddr_t value)
235 {
236 
237 	if (value == 0)
238 		return (value);
239 
240 	if (!kd->vnet_initialized)
241 		return (value);
242 
243 	if (value < kd->vnet_start || value >= kd->vnet_stop)
244 		return (value);
245 
246 	return (kd->vnet_base + value);
247 }
248