/*-
* Copyright (c) 1998 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Charles M. Hannum.
*
* 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 NETBSD FOUNDATION, INC. 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 FOUNDATION 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.
*/
/*-
* Copyright (c) 1989, 1992, 1993
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software developed by the Computer Systems
* Engineering group at Lawrence Berkeley Laboratory under DARPA contract
* BG 91-66 and contributed to Berkeley.
*
* 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.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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.
*/
/*
* Proc traversal interface for kvm. ps and w are (probably) the exclusive
* users of this code, so we've factored it out into a separate module.
* Thus, we keep this grunge out of the other kvm applications (i.e.,
* most other applications are interested only in open/close/read/nlist).
*/
/*
* Common info from kinfo_proc and kinfo_proc2 used by helper routines.
*/
struct miniproc {
struct vmspace *p_vmspace;
char p_stat;
vaddr_t p_psstrp;
struct proc *p_paddr;
pid_t p_pid;
};
/*
* Convert from struct proc and kinfo_proc to miniproc.
*/
#define KPTOMINI(kp, p) \
do { \
(p)->p_stat = (kp)->kp_proc.p_stat; \
(p)->p_pid = (kp)->kp_proc.p_pid; \
(p)->p_paddr = (kp)->kp_eproc.e_paddr; \
(p)->p_vmspace = (kp)->kp_proc.p_vmspace; \
} while (0)
/*
* NetBSD uses kauth(9) to manage credentials, which are stored in kauth_cred_t,
* a kernel-only opaque type. This is an embedded version which is *INTERNAL* to
* kvm(3) so dumps can be read properly.
*
* Whenever NetBSD starts exporting credentials to userland consistently (using
* 'struct uucred', or something) this will have to be updated again.
*/
struct kvm_kauth_cred {
u_int cr_refcnt; /* reference count */
#if COHERENCY_UNIT > 4
uint8_t cr_pad[COHERENCY_UNIT - 4];
#endif
uid_t cr_uid; /* user id */
uid_t cr_euid; /* effective user id */
uid_t cr_svuid; /* saved effective user id */
gid_t cr_gid; /* group id */
gid_t cr_egid; /* effective group id */
gid_t cr_svgid; /* saved effective group id */
u_int cr_ngroups; /* number of groups */
gid_t cr_groups[NGROUPS]; /* group memberships */
specificdata_reference cr_sd; /* specific data */
};
if (kd->swapspc == NULL) {
kd->swapspc = _kvm_malloc(kd, (size_t)kd->nbpg);
if (kd->swapspc == NULL)
return (NULL);
}
/*
* Look through the address map for the memory object
* that corresponds to the given virtual address.
* The header just has the entire valid range.
*/
head = (u_long)&p->p_vmspace->vm_map.header;
addr = head;
for (;;) {
if (KREAD(kd, addr, &vme))
return (NULL);
if (va >= vme.start && va < vme.end &&
vme.aref.ar_amap != NULL)
break;
addr = (u_long)vme.next;
if (addr == head)
return (NULL);
}
/*
* we found the map entry, now to find the object...
*/
if (vme.aref.ar_amap == NULL)
return (NULL);
addr = (u_long)vme.aref.ar_amap;
if (KREAD(kd, addr, &amap))
return (NULL);
offset = va - vme.start;
slot = offset / kd->nbpg + vme.aref.ar_pageoff;
/* sanity-check slot number */
if (slot > amap.am_nslot)
return (NULL);
/* Found the page. */
offset %= kd->nbpg;
*cnt = kd->nbpg - offset;
return (&kd->swapspc[(size_t)offset]);
}
/*
* Convert credentials located in kernel space address 'cred' and store
* them in the appropriate members of 'eproc'.
*/
static int
_kvm_convertcred(kvm_t *kd, u_long cred, struct eproc *eproc)
{
struct kvm_kauth_cred kauthcred;
struct ki_pcred *pc = &eproc->e_pcred;
struct ki_ucred *uc = &eproc->e_ucred;
if (KREAD(kd, cred, &kauthcred) != 0)
return (-1);
/* inlined version of kauth_cred_to_pcred, see kauth(9). */
pc->p_ruid = kauthcred.cr_uid;
pc->p_svuid = kauthcred.cr_svuid;
pc->p_rgid = kauthcred.cr_gid;
pc->p_svgid = kauthcred.cr_svgid;
pc->p_refcnt = kauthcred.cr_refcnt;
pc->p_pad = NULL;
/* inlined version of kauth_cred_to_ucred(), see kauth(9). */
uc->cr_ref = kauthcred.cr_refcnt;
uc->cr_uid = kauthcred.cr_euid;
uc->cr_gid = kauthcred.cr_egid;
uc->cr_ngroups = (uint32_t)MIN(kauthcred.cr_ngroups,
sizeof(uc->cr_groups) / sizeof(uc->cr_groups[0]));
memcpy(uc->cr_groups, kauthcred.cr_groups,
uc->cr_ngroups * sizeof(uc->cr_groups[0]));
return (0);
}
/*
* Read proc's from memory file into buffer bp, which has space to hold
* at most maxcnt procs.
*/
static int
kvm_proclist(kvm_t *kd, int what, int arg, struct proc *p,
struct kinfo_proc *bp, int maxcnt)
{
int cnt = 0;
int nlwps;
struct kinfo_lwp *kl;
struct eproc eproc;
struct pgrp pgrp;
struct session sess;
struct tty tty;
struct proc proc;
for (; cnt < maxcnt && p != NULL; p = proc.p_list.le_next) {
if (KREAD(kd, (u_long)p, &proc)) {
_kvm_err(kd, kd->program, "can't read proc at %p", p);
return (-1);
}
if (_kvm_convertcred(kd, (u_long)proc.p_cred, &eproc) != 0) {
_kvm_err(kd, kd->program,
"can't read proc credentials at %p", p);
return (-1);
}
switch (what) {
case KERN_PROC_PID:
if (proc.p_pid != (pid_t)arg)
continue;
break;
case KERN_PROC_UID:
if (eproc.e_ucred.cr_uid != (uid_t)arg)
continue;
break;
case KERN_PROC_RUID:
if (eproc.e_pcred.p_ruid != (uid_t)arg)
continue;
break;
}
/*
* We're going to add another proc to the set. If this
* will overflow the buffer, assume the reason is because
* nprocs (or the proc list) is corrupt and declare an error.
*/
if (cnt >= maxcnt) {
_kvm_err(kd, kd->program, "nprocs corrupt");
return (-1);
}
/*
* gather eproc
*/
eproc.e_paddr = p;
if (KREAD(kd, (u_long)proc.p_pgrp, &pgrp)) {
_kvm_err(kd, kd->program, "can't read pgrp at %p",
proc.p_pgrp);
return (-1);
}
eproc.e_sess = pgrp.pg_session;
eproc.e_pgid = pgrp.pg_id;
eproc.e_jobc = pgrp.pg_jobc;
if (KREAD(kd, (u_long)pgrp.pg_session, &sess)) {
_kvm_err(kd, kd->program, "can't read session at %p",
pgrp.pg_session);
return (-1);
}
if ((proc.p_lflag & PL_CONTROLT) && sess.s_ttyp != NULL) {
if (KREAD(kd, (u_long)sess.s_ttyp, &tty)) {
_kvm_err(kd, kd->program,
"can't read tty at %p", sess.s_ttyp);
return (-1);
}
eproc.e_tdev = (uint32_t)tty.t_dev;
eproc.e_tsess = tty.t_session;
if (tty.t_pgrp != NULL) {
if (KREAD(kd, (u_long)tty.t_pgrp, &pgrp)) {
_kvm_err(kd, kd->program,
"can't read tpgrp at %p",
tty.t_pgrp);
return (-1);
}
eproc.e_tpgid = pgrp.pg_id;
} else
eproc.e_tpgid = -1;
} else
eproc.e_tdev = (uint32_t)NODEV;
eproc.e_flag = sess.s_ttyvp ? EPROC_CTTY : 0;
eproc.e_sid = sess.s_sid;
if (sess.s_leader == p)
eproc.e_flag |= EPROC_SLEADER;
/*
* Fill in the old-style proc.p_wmesg by copying the wmesg
* from the first available LWP.
*/
kl = kvm_getlwps(kd, proc.p_pid,
(u_long)PTRTOUINT64(eproc.e_paddr),
sizeof(struct kinfo_lwp), &nlwps);
if (kl) {
if (nlwps > 0) {
strcpy(eproc.e_wmesg, kl[0].l_wmesg);
}
}
(void)kvm_read(kd, (u_long)proc.p_vmspace, &eproc.e_vm,
sizeof(eproc.e_vm));
/*
* Build proc info array by reading in proc list from a crash dump.
* Return number of procs read. maxcnt is the max we will read.
*/
static int
kvm_deadprocs(kvm_t *kd, int what, int arg, u_long a_allproc,
u_long a_zombproc, int maxcnt)
{
struct kinfo_proc *bp = kd->procbase;
int acnt, zcnt;
struct proc *p;
if (np == NULL)
_kvm_err(kd, kd->program, "out of memory");
return (np);
}
/*
* Read in an argument vector from the user address space of process p.
* addr if the user-space base address of narg null-terminated contiguous
* strings. This is used to read in both the command arguments and
* environment strings. Read at most maxcnt characters of strings.
*/
static char **
kvm_argv(kvm_t *kd, const struct miniproc *p, u_long addr, int narg,
int maxcnt)
{
char *np, *cp, *ep, *ap;
u_long oaddr = (u_long)~0L;
u_long len;
size_t cc;
char **argv;
/*
* Check that there aren't an unreasonable number of arguments,
* and that the address is in user space.
*/
if (narg > ARG_MAX || addr < kd->min_uva || addr >= kd->max_uva)
return (NULL);
if (kd->argv == NULL) {
/*
* Try to avoid reallocs.
*/
kd->argc = MAX(narg + 1, 32);
kd->argv = _kvm_malloc(kd, kd->argc * sizeof(*kd->argv));
if (kd->argv == NULL)
return (NULL);
} else if (narg + 1 > kd->argc) {
kd->argc = MAX(2 * kd->argc, narg + 1);
kd->argv = _kvm_realloc(kd, kd->argv, kd->argc *
sizeof(*kd->argv));
if (kd->argv == NULL)
return (NULL);
}
if (kd->argspc == NULL) {
kd->argspc = _kvm_malloc(kd, (size_t)kd->nbpg);
if (kd->argspc == NULL)
return (NULL);
kd->argspc_len = kd->nbpg;
}
if (kd->argbuf == NULL) {
kd->argbuf = _kvm_malloc(kd, (size_t)kd->nbpg);
if (kd->argbuf == NULL)
return (NULL);
}
cc = sizeof(char *) * narg;
if (kvm_ureadm(kd, p, addr, (void *)kd->argv, cc) != cc)
return (NULL);
ap = np = kd->argspc;
argv = kd->argv;
len = 0;
/*
* Loop over pages, filling in the argument vector.
*/
while (argv < kd->argv + narg && *argv != NULL) {
addr = (u_long)*argv & ~(kd->nbpg - 1);
if (addr != oaddr) {
if (kvm_ureadm(kd, p, addr, kd->argbuf,
(size_t)kd->nbpg) != kd->nbpg)
return (NULL);
oaddr = addr;
}
addr = (u_long)*argv & (kd->nbpg - 1);
cp = kd->argbuf + (size_t)addr;
cc = kd->nbpg - (size_t)addr;
if (maxcnt > 0 && cc > (size_t)(maxcnt - len))
cc = (size_t)(maxcnt - len);
ep = memchr(cp, '\0', cc);
if (ep != NULL)
cc = ep - cp + 1;
if (len + cc > kd->argspc_len) {
ptrdiff_t off;
char **pp;
uintptr_t op = (uintptr_t)kd->argspc;
kd->argspc_len *= 2;
kd->argspc = _kvm_realloc(kd, kd->argspc,
kd->argspc_len);
if (kd->argspc == NULL)
return (NULL);
/*
* Adjust argv pointers in case realloc moved
* the string space.
*/
off = (uintptr_t)kd->argspc - op;
for (pp = kd->argv; pp < argv; pp++)
*pp += off;
ap += off;
np += off;
}
memcpy(np, cp, cc);
np += cc;
len += cc;
if (ep != NULL) {
*argv++ = ap;
ap = np;
} else
*argv += cc;
if (maxcnt > 0 && len >= maxcnt) {
/*
* We're stopping prematurely. Terminate the
* current string.
*/
if (ep == NULL) {
*np = '\0';
*argv++ = ap;
}
break;
}
}
/* Make sure argv is terminated. */
*argv = NULL;
return (kd->argv);
}
static void
ps_str_a(struct ps_strings *p, u_long *addr, int *n)
{
/*
* Determine if the proc indicated by p is still active.
* This test is not 100% foolproof in theory, but chances of
* being wrong are very low.
*/
static int
proc_verify(kvm_t *kd, u_long kernp, const struct miniproc *p)
{
struct proc kernproc;
/*
* Just read in the whole proc. It's not that big relative
* to the cost of the read system call.
*/
if (kvm_read(kd, kernp, &kernproc, sizeof(kernproc)) !=
sizeof(kernproc))
return (0);
return (p->p_pid == kernproc.p_pid &&
(kernproc.p_stat != SZOMB || p->p_stat == SZOMB));
}
static char **
kvm_doargv(kvm_t *kd, const struct miniproc *p, int nchr,
void (*info)(struct ps_strings *, u_long *, int *))
{
char **ap;
u_long addr;
int cnt;
struct ps_strings arginfo;
/*
* Pointers are stored at the top of the user stack.
*/
if (p->p_stat == SZOMB)
return (NULL);
cnt = (int)kvm_ureadm(kd, p, p->p_psstrp,
(void *)&arginfo, sizeof(arginfo));
if (cnt != sizeof(arginfo))
return (NULL);
(*info)(&arginfo, &addr, &cnt);
if (cnt == 0)
return (NULL);
ap = kvm_argv(kd, p, addr, cnt, nchr);
/*
* For live kernels, make sure this process didn't go away.
*/
if (ap != NULL && ISALIVE(kd) &&
!proc_verify(kd, (u_long)p->p_paddr, p))
ap = NULL;
return (ap);
}
/*
* Get the command args. This code is now machine independent.
*/
char **
kvm_getargv(kvm_t *kd, const struct kinfo_proc *kp, int nchr)
{
struct miniproc p;
bp = kd->argspc;
bp[kd->argspc_len-1] = '\0'; /* make sure the string ends with nul */
ap = kd->argv;
endp = bp + MIN(nchr, bufs);
while (bp < endp) {
*ap++ = bp;
/*
* XXX: don't need following anymore, or stick check
* for max argc in above while loop?
*/
if (ap >= kd->argv + kd->argc) {
kd->argc *= 2;
kd->argv = _kvm_realloc(kd, kd->argv,
kd->argc * sizeof(*kd->argv));
ap = kd->argv;
}
bp += strlen(bp) + 1;
}
*ap = NULL;
/*
* Read from user space. The user context is given by p.
*/
static ssize_t
kvm_ureadm(kvm_t *kd, const struct miniproc *p, u_long uva,
char *buf, size_t len)
{
char *cp;