kern/42030 - tracking of file descriptors by ktrace/kdump

This commit is contained in:
alnsn 2011-06-01 21:24:59 +00:00
parent bc07e228a2
commit a739efc5b5
9 changed files with 110 additions and 15 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: kern_exec.c,v 1.314 2011/04/26 16:36:42 joerg Exp $ */ /* $NetBSD: kern_exec.c,v 1.315 2011/06/01 21:24:59 alnsn Exp $ */
/*- /*-
* Copyright (c) 2008 The NetBSD Foundation, Inc. * Copyright (c) 2008 The NetBSD Foundation, Inc.
@ -59,7 +59,7 @@
*/ */
#include <sys/cdefs.h> #include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: kern_exec.c,v 1.314 2011/04/26 16:36:42 joerg Exp $"); __KERNEL_RCSID(0, "$NetBSD: kern_exec.c,v 1.315 2011/06/01 21:24:59 alnsn Exp $");
#include "opt_ktrace.h" #include "opt_ktrace.h"
#include "opt_modular.h" #include "opt_modular.h"
@ -979,6 +979,10 @@ execve1(struct lwp *l, const char *path, char * const *args,
cwdexec(p); cwdexec(p);
fd_closeexec(); /* handle close on exec */ fd_closeexec(); /* handle close on exec */
if (__predict_false(ktrace_on))
fd_ktrexecfd();
execsigs(p); /* reset catched signals */ execsigs(p); /* reset catched signals */
l->l_ctxlink = NULL; /* reset ucontext link */ l->l_ctxlink = NULL; /* reset ucontext link */

View File

@ -1,4 +1,4 @@
/* $NetBSD: kern_ktrace.c,v 1.156 2011/04/27 00:00:46 joerg Exp $ */ /* $NetBSD: kern_ktrace.c,v 1.157 2011/06/01 21:25:00 alnsn Exp $ */
/*- /*-
* Copyright (c) 2006, 2007, 2008 The NetBSD Foundation, Inc. * Copyright (c) 2006, 2007, 2008 The NetBSD Foundation, Inc.
@ -61,7 +61,7 @@
*/ */
#include <sys/cdefs.h> #include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: kern_ktrace.c,v 1.156 2011/04/27 00:00:46 joerg Exp $"); __KERNEL_RCSID(0, "$NetBSD: kern_ktrace.c,v 1.157 2011/06/01 21:25:00 alnsn Exp $");
#include <sys/param.h> #include <sys/param.h>
#include <sys/systm.h> #include <sys/systm.h>
@ -673,6 +673,25 @@ ktr_execenv(const void *bf, size_t len)
ktr_kmem(l, KTR_EXEC_ENV, bf, len); ktr_kmem(l, KTR_EXEC_ENV, bf, len);
} }
void
ktr_execfd(int fd, u_int dtype)
{
struct ktrace_entry *kte;
struct ktr_execfd* ktp;
lwp_t *l = curlwp;
if (!KTRPOINT(l->l_proc, KTR_EXEC_FD))
return;
if (ktealloc(&kte, (void *)&ktp, l, KTR_EXEC_FD, sizeof(*ktp)))
return;
ktp->ktr_fd = fd;
ktp->ktr_dtype = dtype;
ktraddentry(l, kte, KTA_WAITOK);
}
static void static void
ktr_kmem(lwp_t *l, int type, const void *bf, size_t len) ktr_kmem(lwp_t *l, int type, const void *bf, size_t len)
{ {

View File

@ -1,4 +1,4 @@
/* $NetBSD: kern_stub.c,v 1.32 2011/05/31 23:28:53 dyoung Exp $ */ /* $NetBSD: kern_stub.c,v 1.33 2011/06/01 21:25:01 alnsn Exp $ */
/*- /*-
* Copyright (c) 2007, 2008 The NetBSD Foundation, Inc. * Copyright (c) 2007, 2008 The NetBSD Foundation, Inc.
@ -62,7 +62,7 @@
*/ */
#include <sys/cdefs.h> #include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: kern_stub.c,v 1.32 2011/05/31 23:28:53 dyoung Exp $"); __KERNEL_RCSID(0, "$NetBSD: kern_stub.c,v 1.33 2011/06/01 21:25:01 alnsn Exp $");
#include "opt_ptrace.h" #include "opt_ptrace.h"
#include "opt_ktrace.h" #include "opt_ktrace.h"
@ -111,6 +111,7 @@ __weak_alias(ktr_kuser,nullop);
__weak_alias(ktr_mib,nullop); __weak_alias(ktr_mib,nullop);
__weak_alias(ktr_execarg,nullop); __weak_alias(ktr_execarg,nullop);
__weak_alias(ktr_execenv,nullop); __weak_alias(ktr_execenv,nullop);
__weak_alias(ktr_execfd,nullop);
__weak_alias(sys_fktrace,sys_nosys); /* Syscalls */ __weak_alias(sys_fktrace,sys_nosys); /* Syscalls */
__weak_alias(sys_ktrace,sys_nosys); __weak_alias(sys_ktrace,sys_nosys);

View File

@ -1,4 +1,4 @@
/* $NetBSD: subr_exec_fd.c,v 1.5 2011/02/15 15:54:28 pooka Exp $ */ /* $NetBSD: subr_exec_fd.c,v 1.6 2011/06/01 21:25:01 alnsn Exp $ */
/*- /*-
* Copyright (c) 2008 The NetBSD Foundation, Inc. * Copyright (c) 2008 The NetBSD Foundation, Inc.
@ -27,7 +27,7 @@
*/ */
#include <sys/cdefs.h> #include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: subr_exec_fd.c,v 1.5 2011/02/15 15:54:28 pooka Exp $"); __KERNEL_RCSID(0, "$NetBSD: subr_exec_fd.c,v 1.6 2011/06/01 21:25:01 alnsn Exp $");
#include <sys/param.h> #include <sys/param.h>
#include <sys/file.h> #include <sys/file.h>
@ -36,6 +36,35 @@ __KERNEL_RCSID(0, "$NetBSD: subr_exec_fd.c,v 1.5 2011/02/15 15:54:28 pooka Exp $
#include <sys/namei.h> #include <sys/namei.h>
#include <sys/syslog.h> #include <sys/syslog.h>
#include <sys/vnode.h> #include <sys/vnode.h>
#include <sys/ktrace.h>
void
fd_ktrexecfd(void)
{
proc_t *p;
filedesc_t *fdp;
fdfile_t *ff;
lwp_t *l;
fdtab_t *dt;
int fd;
l = curlwp;
p = l->l_proc;
fdp = p->p_fd;
dt = fdp->fd_dt;
for (fd = 0; fd <= fdp->fd_lastfile; fd++) {
if ((ff = dt->dt_ff[fd]) == NULL) {
KASSERT(fd >= NDFDFILE);
continue;
}
KASSERT(fd >= NDFDFILE ||
ff == (fdfile_t *)fdp->fd_dfdfile[fd]);
if (ff->ff_file == NULL)
continue;
ktr_execfd(fd, ff->ff_file->f_type);
}
}
/* /*
* It is unsafe for set[ug]id processes to be started with file * It is unsafe for set[ug]id processes to be started with file

View File

@ -1,4 +1,4 @@
/* $NetBSD: filedesc.h,v 1.59 2011/04/10 15:45:33 christos Exp $ */ /* $NetBSD: filedesc.h,v 1.60 2011/06/01 21:25:02 alnsn Exp $ */
/*- /*-
* Copyright (c) 2008 The NetBSD Foundation, Inc. * Copyright (c) 2008 The NetBSD Foundation, Inc.
@ -193,6 +193,7 @@ void fd_share(proc_t *);
void fd_hold(lwp_t *); void fd_hold(lwp_t *);
void fd_free(void); void fd_free(void);
void fd_closeexec(void); void fd_closeexec(void);
void fd_ktrexecfd(void);
int fd_checkstd(void); int fd_checkstd(void);
file_t *fd_getfile(unsigned); file_t *fd_getfile(unsigned);
file_t *fd_getfile2(proc_t *, unsigned); file_t *fd_getfile2(proc_t *, unsigned);

View File

@ -1,4 +1,4 @@
/* $NetBSD: ktrace.h,v 1.57 2011/04/27 00:00:46 joerg Exp $ */ /* $NetBSD: ktrace.h,v 1.58 2011/06/01 21:25:02 alnsn Exp $ */
/* /*
* Copyright (c) 1988, 1993 * Copyright (c) 1988, 1993
@ -217,6 +217,14 @@ struct ktr_saupcall {
#define KTR_MIB 14 #define KTR_MIB 14
/* Record contains MIB name */ /* Record contains MIB name */
/*
* KTR_EXEC_FD - Opened file descriptor from exec
*/
#define KTR_EXEC_FD 15
struct ktr_execfd {
int ktr_fd;
u_int ktr_dtype; /* one of DTYPE_* constants */
};
/* /*
* kernel trace points (in p_traceflag) * kernel trace points (in p_traceflag)
@ -234,6 +242,7 @@ struct ktr_saupcall {
#define KTRFAC_EXEC_ENV (1<<KTR_EXEC_ENV) #define KTRFAC_EXEC_ENV (1<<KTR_EXEC_ENV)
#define KTRFAC_SAUPCALL (1<<KTR_SAUPCALL) #define KTRFAC_SAUPCALL (1<<KTR_SAUPCALL)
#define KTRFAC_MIB (1<<KTR_MIB) #define KTRFAC_MIB (1<<KTR_MIB)
#define KTRFAC_EXEC_FD (1<<KTR_EXEC_FD)
/* /*
* trace flags (also in p_traceflags) * trace flags (also in p_traceflags)
*/ */
@ -286,6 +295,7 @@ void ktr_kuser(const char *, void *, size_t);
void ktr_mib(const int *a , u_int b); void ktr_mib(const int *a , u_int b);
void ktr_execarg(const void *, size_t); void ktr_execarg(const void *, size_t);
void ktr_execenv(const void *, size_t); void ktr_execenv(const void *, size_t);
void ktr_execfd(int, u_int);
void ktr_saupcall(struct lwp *, int, int, int, void *, void *, void *); void ktr_saupcall(struct lwp *, int, int, int, void *, void *, void *);
static inline bool static inline bool
@ -392,6 +402,13 @@ ktrexecenv(const void *a, size_t b)
ktr_execenv(a, b); ktr_execenv(a, b);
} }
static inline void
ktrexecfd(int fd, u_int dtype)
{
if (__predict_false(ktrace_on))
ktr_execfd(fd, dtype);
}
static inline void static inline void
ktrsaupcall(struct lwp *a, int b, int c, int d, void *e, void *f, void *g) ktrsaupcall(struct lwp *a, int b, int c, int d, void *e, void *f, void *g)
{ {

View File

@ -1,4 +1,4 @@
/* $NetBSD: kdump.c,v 1.111 2011/04/27 00:00:47 joerg Exp $ */ /* $NetBSD: kdump.c,v 1.112 2011/06/01 21:28:32 alnsn Exp $ */
/*- /*-
* Copyright (c) 1988, 1993 * Copyright (c) 1988, 1993
@ -39,12 +39,14 @@ __COPYRIGHT("@(#) Copyright (c) 1988, 1993\
#if 0 #if 0
static char sccsid[] = "@(#)kdump.c 8.4 (Berkeley) 4/28/95"; static char sccsid[] = "@(#)kdump.c 8.4 (Berkeley) 4/28/95";
#else #else
__RCSID("$NetBSD: kdump.c,v 1.111 2011/04/27 00:00:47 joerg Exp $"); __RCSID("$NetBSD: kdump.c,v 1.112 2011/06/01 21:28:32 alnsn Exp $");
#endif #endif
#endif /* not lint */ #endif /* not lint */
#include <sys/param.h> #include <sys/param.h>
#include <sys/proc.h> /* XXX #include <sys/file.h> fails without this header */
#define _KERNEL #define _KERNEL
#include <sys/file.h>
#include <sys/errno.h> #include <sys/errno.h>
#undef _KERNEL #undef _KERNEL
#include <sys/time.h> #include <sys/time.h>
@ -115,6 +117,7 @@ static void ktrpsig(void *, int);
static void ktrcsw(struct ktr_csw *); static void ktrcsw(struct ktr_csw *);
static void ktruser(struct ktr_user *, int); static void ktruser(struct ktr_user *, int);
static void ktrmib(int *, int); static void ktrmib(int *, int);
static void ktrexecfd(struct ktr_execfd *);
static void usage(void) __dead; static void usage(void) __dead;
static void eprint(int); static void eprint(int);
static void rprint(register_t); static void rprint(register_t);
@ -294,6 +297,9 @@ main(int argc, char **argv)
case KTR_EXEC_ENV: case KTR_EXEC_ENV:
visdump_buf(m, ktrlen, col); visdump_buf(m, ktrlen, col);
break; break;
case KTR_EXEC_FD:
ktrexecfd(m);
break;
case KTR_MIB: case KTR_MIB:
ktrmib(m, ktrlen); ktrmib(m, ktrlen);
break; break;
@ -363,6 +369,9 @@ dumpheader(struct ktr_header *kth)
case KTR_EXEC_ARG: case KTR_EXEC_ARG:
type = "ARG"; type = "ARG";
break; break;
case KTR_EXEC_FD:
type = "FD";
break;
case KTR_SAUPCALL: case KTR_SAUPCALL:
type = "SAU"; type = "SAU";
break; break;
@ -631,6 +640,16 @@ ktrsysret(struct ktr_sysret *ktr, int len)
(void)putchar('\n'); (void)putchar('\n');
} }
static void
ktrexecfd(struct ktr_execfd *ktr)
{
static const char *dnames[] = { DTYPE_NAMES };
if (ktr->ktr_dtype < __arraycount(dnames))
printf("%s %d\n", dnames[ktr->ktr_dtype], ktr->ktr_fd);
else
printf("UNKNOWN(%u) %d\n", ktr->ktr_dtype, ktr->ktr_fd);
}
static void static void
rprint(register_t ret) rprint(register_t ret)
{ {

View File

@ -1,4 +1,4 @@
.\" $NetBSD: ktrace.1,v 1.40 2011/04/27 00:00:47 joerg Exp $ .\" $NetBSD: ktrace.1,v 1.41 2011/06/01 21:28:33 alnsn Exp $
.\" .\"
.\" Copyright (c) 1990, 1993 .\" Copyright (c) 1990, 1993
.\" The Regents of the University of California. All rights reserved. .\" The Regents of the University of California. All rights reserved.
@ -181,6 +181,8 @@ trace exec arguments
trace system calls trace system calls
.It Cm e .It Cm e
trace emulation changes trace emulation changes
.It Cm f
trace open file descriptors after exec
.It Cm i .It Cm i
trace trace
.Tn I/O .Tn I/O

View File

@ -1,4 +1,4 @@
/* $NetBSD: subr.c,v 1.17 2011/04/27 00:00:47 joerg Exp $ */ /* $NetBSD: subr.c,v 1.18 2011/06/01 21:28:33 alnsn Exp $ */
/*- /*-
* Copyright (c) 1988, 1993 * Copyright (c) 1988, 1993
@ -34,7 +34,7 @@
#if 0 #if 0
static char sccsid[] = "@(#)subr.c 8.2 (Berkeley) 4/28/95"; static char sccsid[] = "@(#)subr.c 8.2 (Berkeley) 4/28/95";
#else #else
__RCSID("$NetBSD: subr.c,v 1.17 2011/04/27 00:00:47 joerg Exp $"); __RCSID("$NetBSD: subr.c,v 1.18 2011/06/01 21:28:33 alnsn Exp $");
#endif #endif
#endif /* not lint */ #endif /* not lint */
@ -73,6 +73,9 @@ getpoints(int facs, char *s)
case 'e': case 'e':
fac = KTRFAC_EMUL; fac = KTRFAC_EMUL;
break; break;
case 'f':
fac = KTRFAC_EXEC_FD;
break;
case 'i': case 'i':
fac = KTRFAC_GENIO; fac = KTRFAC_GENIO;
break; break;