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