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