CVS commit: src/sys/dev/filemon

2016-01-10 Thread Paul Goyette
Module Name:src
Committed By:   pgoyette
Date:   Mon Jan 11 01:37:36 UTC 2016

Modified Files:
src/sys/dev/filemon: filemon.c filemon.h

Log Message:
Take a reference on the (activity log) file itself, and not on the
descriptor.

Should fix PR kern/50627


To generate a diff of this commit:
cvs rdiff -u -r1.27 -r1.28 src/sys/dev/filemon/filemon.c
cvs rdiff -u -r1.8 -r1.9 src/sys/dev/filemon/filemon.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/filemon/filemon.c
diff -u src/sys/dev/filemon/filemon.c:1.27 src/sys/dev/filemon/filemon.c:1.28
--- src/sys/dev/filemon/filemon.c:1.27	Sat Jan  9 07:27:31 2016
+++ src/sys/dev/filemon/filemon.c	Mon Jan 11 01:37:36 2016
@@ -1,4 +1,4 @@
-/*  $NetBSD: filemon.c,v 1.27 2016/01/09 07:27:31 pgoyette Exp $ */
+/*  $NetBSD: filemon.c,v 1.28 2016/01/11 01:37:36 pgoyette Exp $ */
 /*
  * Copyright (c) 2010, Juniper Networks, Inc.
  *
@@ -25,7 +25,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: filemon.c,v 1.27 2016/01/09 07:27:31 pgoyette Exp $");
+__KERNEL_RCSID(0, "$NetBSD: filemon.c,v 1.28 2016/01/11 01:37:36 pgoyette Exp $");
 
 #include 
 #include 
@@ -227,7 +227,6 @@ filemon_open(dev_t dev, int oflags __unu
 
 	filemon = kmem_alloc(sizeof(struct filemon), KM_SLEEP);
 	rw_init(>fm_mtx);
-	filemon->fm_fd = -1;
 	filemon->fm_fp = NULL;
 	filemon->fm_pid = curproc->p_pid;
 
@@ -270,7 +269,7 @@ filemon_close(struct file * fp)
 	 */
 	rw_enter(>fm_mtx, RW_WRITER);
 	if (filemon->fm_fp) {
-		fd_putfile(filemon->fm_fd);	/* release our reference */
+		closef(filemon->fm_fp);	/* release our reference */
 		filemon->fm_fp = NULL;
 	}
 	rw_exit(>fm_mtx);
@@ -284,6 +283,7 @@ static int
 filemon_ioctl(struct file * fp, u_long cmd, void *data)
 {
 	int error = 0;
+	int fd;
 	struct filemon *filemon;
 	struct proc *tp;
 
@@ -306,11 +306,11 @@ filemon_ioctl(struct file * fp, u_long c
 
 		/* First, release any current output file descriptor */
 		if (filemon->fm_fp)
-			fd_putfile(filemon->fm_fd);
+			closef(filemon->fm_fp);
 
 		/* Now set up the new one */
-		filemon->fm_fd = *((int *) data);
-		if ((filemon->fm_fp = fd_getfile(filemon->fm_fd)) == NULL) {
+		fd = *((int *) data);
+		if ((filemon->fm_fp = fd_getfile2(curproc, fd)) == NULL) {
 			error = EBADF;
 			break;
 		}

Index: src/sys/dev/filemon/filemon.h
diff -u src/sys/dev/filemon/filemon.h:1.8 src/sys/dev/filemon/filemon.h:1.9
--- src/sys/dev/filemon/filemon.h:1.8	Wed Nov 25 07:34:49 2015
+++ src/sys/dev/filemon/filemon.h	Mon Jan 11 01:37:36 2016
@@ -1,4 +1,4 @@
-/* $NetBSD: filemon.h,v 1.8 2015/11/25 07:34:49 pgoyette Exp $ */
+/* $NetBSD: filemon.h,v 1.9 2016/01/11 01:37:36 pgoyette Exp $ */
 /*
  * Copyright (c) 2010, Juniper Networks, Inc.
  *
@@ -41,7 +41,6 @@ struct filemon {
 	char fm_fname1[MAXPATHLEN];/* Temporary filename buffer. */
 	char fm_fname2[MAXPATHLEN];/* Temporary filename buffer. */
 	char fm_msgbufr[32 + 2 * MAXPATHLEN];	/* Output message buffer. */
-	int fm_fd;			/* Output fd */
 	struct file *fm_fp;	/* Output file pointer. */
 	krwlock_t fm_mtx;		/* Lock mutex for this filemon. */
 	TAILQ_ENTRY(filemon) fm_link;	/* Link into the in-use list. */



CVS commit: src/sys/dev/filemon

2016-01-08 Thread Paul Goyette
Module Name:src
Committed By:   pgoyette
Date:   Fri Jan  8 08:57:14 UTC 2016

Modified Files:
src/sys/dev/filemon: filemon.c

Log Message:
Don't release the proc_lock mutex until we're finished using the stuff
that the mutex protects (ie, the proc list and the entry we found in
the list).


To generate a diff of this commit:
cvs rdiff -u -r1.25 -r1.26 src/sys/dev/filemon/filemon.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/filemon/filemon.c
diff -u src/sys/dev/filemon/filemon.c:1.25 src/sys/dev/filemon/filemon.c:1.26
--- src/sys/dev/filemon/filemon.c:1.25	Fri Jan  8 07:16:13 2016
+++ src/sys/dev/filemon/filemon.c	Fri Jan  8 08:57:14 2016
@@ -1,4 +1,4 @@
-/*  $NetBSD: filemon.c,v 1.25 2016/01/08 07:16:13 dholland Exp $ */
+/*  $NetBSD: filemon.c,v 1.26 2016/01/08 08:57:14 pgoyette Exp $ */
 /*
  * Copyright (c) 2010, Juniper Networks, Inc.
  *
@@ -25,7 +25,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: filemon.c,v 1.25 2016/01/08 07:16:13 dholland Exp $");
+__KERNEL_RCSID(0, "$NetBSD: filemon.c,v 1.26 2016/01/08 08:57:14 pgoyette Exp $");
 
 #include 
 #include 
@@ -317,10 +317,10 @@ filemon_ioctl(struct file * fp, u_long c
 		/* Set the monitored process ID - if allowed. */
 		mutex_enter(proc_lock);
 		tp = proc_find(*((pid_t *) data));
-		mutex_exit(proc_lock);
 		if (tp == NULL ||
 		tp->p_emul != _netbsd) {
 			error = ESRCH;
+			mutex_exit(proc_lock);
 			break;
 		}
 
@@ -330,6 +330,7 @@ filemon_ioctl(struct file * fp, u_long c
 		if (!error) {
 			filemon->fm_pid = tp->p_pid;
 		}
+		mutex_exit(proc_lock);
 		break;
 
 	default:



CVS commit: src/sys/dev/filemon

2016-01-08 Thread Paul Goyette
Module Name:src
Committed By:   pgoyette
Date:   Sat Jan  9 07:27:31 UTC 2016

Modified Files:
src/sys/dev/filemon: filemon.c

Log Message:
When following process parent pointers, lock the new (parent) before
releasing the initial process.


To generate a diff of this commit:
cvs rdiff -u -r1.26 -r1.27 src/sys/dev/filemon/filemon.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/filemon/filemon.c
diff -u src/sys/dev/filemon/filemon.c:1.26 src/sys/dev/filemon/filemon.c:1.27
--- src/sys/dev/filemon/filemon.c:1.26	Fri Jan  8 08:57:14 2016
+++ src/sys/dev/filemon/filemon.c	Sat Jan  9 07:27:31 2016
@@ -1,4 +1,4 @@
-/*  $NetBSD: filemon.c,v 1.26 2016/01/08 08:57:14 pgoyette Exp $ */
+/*  $NetBSD: filemon.c,v 1.27 2016/01/09 07:27:31 pgoyette Exp $ */
 /*
  * Copyright (c) 2010, Juniper Networks, Inc.
  *
@@ -25,7 +25,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: filemon.c,v 1.26 2016/01/08 08:57:14 pgoyette Exp $");
+__KERNEL_RCSID(0, "$NetBSD: filemon.c,v 1.27 2016/01/09 07:27:31 pgoyette Exp $");
 
 #include 
 #include 
@@ -154,13 +154,14 @@ filemon_pid_check(struct proc * p)
 	struct filemon *filemon;
 	struct proc * lp;
 
+	KASSERT(p != NULL);
 	if (!TAILQ_EMPTY(_inuse)) {
+		/*
+		 * make sure p cannot exit
+		 * until we have moved on to p_pptr
+		 */
+		rw_enter(>p_reflock, RW_READER);
 		while (p) {
-			/*
-			 * make sure p cannot exit
-			 * until we have moved on to p_pptr
-			 */
-			rw_enter(>p_reflock, RW_READER);
 			TAILQ_FOREACH(filemon, _inuse, fm_link) {
 if (p->p_pid == filemon->fm_pid) {
 	rw_exit(>p_reflock);
@@ -169,6 +170,10 @@ filemon_pid_check(struct proc * p)
 			}
 			lp = p;
 			p = p->p_pptr;
+
+			/* lock parent before releasing child */
+			if (p != NULL)
+rw_enter(>p_reflock, RW_READER);
 			rw_exit(>p_reflock);
 		}
 	}



CVS commit: src/sys/dev/filemon

2016-01-07 Thread David A. Holland
Module Name:src
Committed By:   dholland
Date:   Fri Jan  8 07:16:13 UTC 2016

Modified Files:
src/sys/dev/filemon: filemon.c

Log Message:
typo in debug print


To generate a diff of this commit:
cvs rdiff -u -r1.24 -r1.25 src/sys/dev/filemon/filemon.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/filemon/filemon.c
diff -u src/sys/dev/filemon/filemon.c:1.24 src/sys/dev/filemon/filemon.c:1.25
--- src/sys/dev/filemon/filemon.c:1.24	Tue Jan  5 22:08:54 2016
+++ src/sys/dev/filemon/filemon.c	Fri Jan  8 07:16:13 2016
@@ -1,4 +1,4 @@
-/*  $NetBSD: filemon.c,v 1.24 2016/01/05 22:08:54 pgoyette Exp $ */
+/*  $NetBSD: filemon.c,v 1.25 2016/01/08 07:16:13 dholland Exp $ */
 /*
  * Copyright (c) 2010, Juniper Networks, Inc.
  *
@@ -25,7 +25,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: filemon.c,v 1.24 2016/01/05 22:08:54 pgoyette Exp $");
+__KERNEL_RCSID(0, "$NetBSD: filemon.c,v 1.25 2016/01/08 07:16:13 dholland Exp $");
 
 #include 
 #include 
@@ -115,7 +115,7 @@ filemon_output(struct filemon * filemon,
 		cp = strchr(msg, '\n');
 		if (cp && cp - msg <= 16)
 			x = (cp - msg) - 2;
-		log(logLevel, "filemont_output:('%.*s%s'", x,
+		log(logLevel, "filemon_output:('%.*s%s'", x,
 		(x < 16) ? "..." : "", msg);
 	}
 #endif



CVS commit: src/sys/dev/filemon

2016-01-05 Thread Paul Goyette
Module Name:src
Committed By:   pgoyette
Date:   Tue Jan  5 09:37:11 UTC 2016

Modified Files:
src/sys/dev/filemon: filemon.c

Log Message:
Although not recommended, it is possible to include filemon(4) as a
built-in module.  If we do this, don't try to call devsw_attach() as
our device structures will already have been included via ioconf.[ch].

This avoids calling the init routine twice (once for CLASS_DRIVER and
then later for CLASS_ANY), which in turn avoids trying to initialize
an already initialized lock.


To generate a diff of this commit:
cvs rdiff -u -r1.22 -r1.23 src/sys/dev/filemon/filemon.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/filemon/filemon.c
diff -u src/sys/dev/filemon/filemon.c:1.22 src/sys/dev/filemon/filemon.c:1.23
--- src/sys/dev/filemon/filemon.c:1.22	Wed Nov 25 07:34:49 2015
+++ src/sys/dev/filemon/filemon.c	Tue Jan  5 09:37:11 2016
@@ -1,4 +1,4 @@
-/*  $NetBSD: filemon.c,v 1.22 2015/11/25 07:34:49 pgoyette Exp $ */
+/*  $NetBSD: filemon.c,v 1.23 2016/01/05 09:37:11 pgoyette Exp $ */
 /*
  * Copyright (c) 2010, Juniper Networks, Inc.
  *
@@ -25,7 +25,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: filemon.c,v 1.22 2015/11/25 07:34:49 pgoyette Exp $");
+__KERNEL_RCSID(0, "$NetBSD: filemon.c,v 1.23 2016/01/05 09:37:11 pgoyette Exp $");
 
 #include 
 #include 
@@ -391,8 +391,10 @@ static int
 filemon_modcmd(modcmd_t cmd, void *data)
 {
 	int error = 0;
+#ifdef _MODULE
 	int bmajor = -1;
 	int cmajor = -1;
+#endif
 
 	switch (cmd) {
 	case MODULE_CMD_INIT:
@@ -401,15 +403,19 @@ filemon_modcmd(modcmd_t cmd, void *data)
 #endif
 
 		error = filemon_load(data);
+#ifdef _MODULE
 		if (!error)
 			error = devsw_attach("filemon", NULL, ,
 			_cdevsw, );
+#endif
 		break;
 
 	case MODULE_CMD_FINI:
 		error = filemon_unload();
+#ifdef _MODULE
 		if (!error)
 			error = devsw_detach(NULL, _cdevsw);
+#endif
 		break;
 
 	case MODULE_CMD_STAT:



CVS commit: src/sys/dev/filemon

2016-01-05 Thread Paul Goyette
Module Name:src
Committed By:   pgoyette
Date:   Tue Jan  5 22:08:54 UTC 2016

Modified Files:
src/sys/dev/filemon: filemon.c

Log Message:
KNF - blank line after (non-existant) declarations.  No functional change.


To generate a diff of this commit:
cvs rdiff -u -r1.23 -r1.24 src/sys/dev/filemon/filemon.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/filemon/filemon.c
diff -u src/sys/dev/filemon/filemon.c:1.23 src/sys/dev/filemon/filemon.c:1.24
--- src/sys/dev/filemon/filemon.c:1.23	Tue Jan  5 09:37:11 2016
+++ src/sys/dev/filemon/filemon.c	Tue Jan  5 22:08:54 2016
@@ -1,4 +1,4 @@
-/*  $NetBSD: filemon.c,v 1.23 2016/01/05 09:37:11 pgoyette Exp $ */
+/*  $NetBSD: filemon.c,v 1.24 2016/01/05 22:08:54 pgoyette Exp $ */
 /*
  * Copyright (c) 2010, Juniper Networks, Inc.
  *
@@ -25,7 +25,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: filemon.c,v 1.23 2016/01/05 09:37:11 pgoyette Exp $");
+__KERNEL_RCSID(0, "$NetBSD: filemon.c,v 1.24 2016/01/05 22:08:54 pgoyette Exp $");
 
 #include 
 #include 
@@ -142,6 +142,7 @@ filemon_printf(struct filemon *filemon, 
 static void
 filemon_comment(struct filemon * filemon)
 {
+
 	filemon_printf(filemon, "# filemon version %d\n# Target pid %d\nV %d\n",
 	   FILEMON_VERSION, curproc->p_pid, FILEMON_VERSION);
 }



CVS commit: src/sys/dev/filemon

2015-11-24 Thread Paul Goyette
Module Name:src
Committed By:   pgoyette
Date:   Wed Nov 25 07:34:49 UTC 2015

Modified Files:
src/sys/dev/filemon: filemon.c filemon.h filemon_wrapper.c

Log Message:
Be a bit more paranoid about hijacking (and restoring) syscall function
pointers.  Ensure that we have the correct "old" values before setting
new values, for both "intall" and "deinstall".

XXX This is NOT intended to encourage additional cases of hijacking!  If
XXX some other hijack feature ever gets committed, this code should be
XXX moved to (most likely) kern/kern_syscall.c.


To generate a diff of this commit:
cvs rdiff -u -r1.21 -r1.22 src/sys/dev/filemon/filemon.c
cvs rdiff -u -r1.7 -r1.8 src/sys/dev/filemon/filemon.h
cvs rdiff -u -r1.10 -r1.11 src/sys/dev/filemon/filemon_wrapper.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/filemon/filemon.c
diff -u src/sys/dev/filemon/filemon.c:1.21 src/sys/dev/filemon/filemon.c:1.22
--- src/sys/dev/filemon/filemon.c:1.21	Tue Nov 24 01:05:50 2015
+++ src/sys/dev/filemon/filemon.c	Wed Nov 25 07:34:49 2015
@@ -1,4 +1,4 @@
-/*  $NetBSD: filemon.c,v 1.21 2015/11/24 01:05:50 pgoyette Exp $ */
+/*  $NetBSD: filemon.c,v 1.22 2015/11/25 07:34:49 pgoyette Exp $ */
 /*
  * Copyright (c) 2010, Juniper Networks, Inc.
  *
@@ -25,7 +25,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: filemon.c,v 1.21 2015/11/24 01:05:50 pgoyette Exp $");
+__KERNEL_RCSID(0, "$NetBSD: filemon.c,v 1.22 2015/11/25 07:34:49 pgoyette Exp $");
 
 #include 
 #include 
@@ -340,13 +340,13 @@ filemon_ioctl(struct file * fp, u_long c
 	return (error);
 }
 
-static void
+static int
 filemon_load(void *dummy __unused)
 {
 	rw_init(_mtx);
 
 	/* Install the syscall wrappers. */
-	filemon_wrapper_install();
+	return filemon_wrapper_install();
 }
 
 /*
@@ -400,9 +400,10 @@ filemon_modcmd(modcmd_t cmd, void *data)
 		logLevel = LOG_INFO;
 #endif
 
-		filemon_load(data);
-		error = devsw_attach("filemon", NULL, ,
-		_cdevsw, );
+		error = filemon_load(data);
+		if (!error)
+			error = devsw_attach("filemon", NULL, ,
+			_cdevsw, );
 		break;
 
 	case MODULE_CMD_FINI:

Index: src/sys/dev/filemon/filemon.h
diff -u src/sys/dev/filemon/filemon.h:1.7 src/sys/dev/filemon/filemon.h:1.8
--- src/sys/dev/filemon/filemon.h:1.7	Sun Sep  6 06:01:00 2015
+++ src/sys/dev/filemon/filemon.h	Wed Nov 25 07:34:49 2015
@@ -1,4 +1,4 @@
-/* $NetBSD: filemon.h,v 1.7 2015/09/06 06:01:00 dholland Exp $ */
+/* $NetBSD: filemon.h,v 1.8 2015/11/25 07:34:49 pgoyette Exp $ */
 /*
  * Copyright (c) 2010, Juniper Networks, Inc.
  *
@@ -47,9 +47,15 @@ struct filemon {
 	TAILQ_ENTRY(filemon) fm_link;	/* Link into the in-use list. */
 };
 
+struct hijack { 
+	int hj_index; 
+	sy_call_t *hj_funcs[2];	/* [0] = original, [1] = hijack */ 
+}; 
+
 struct filemon * filemon_lookup(struct proc *);
 void filemon_output(struct filemon *, char *, size_t);
-void filemon_wrapper_install(void);
+int syscall_hijack(struct sysent *, const struct hijack *, bool);
+int filemon_wrapper_install(void);
 int  filemon_wrapper_deinstall(void);
 void filemon_printf(struct filemon *, const char *, ...) __printflike(2, 3);
 #endif

Index: src/sys/dev/filemon/filemon_wrapper.c
diff -u src/sys/dev/filemon/filemon_wrapper.c:1.10 src/sys/dev/filemon/filemon_wrapper.c:1.11
--- src/sys/dev/filemon/filemon_wrapper.c:1.10	Mon Nov 23 00:47:43 2015
+++ src/sys/dev/filemon/filemon_wrapper.c	Wed Nov 25 07:34:49 2015
@@ -1,4 +1,4 @@
-/*	$NetBSD: filemon_wrapper.c,v 1.10 2015/11/23 00:47:43 pgoyette Exp $	*/
+/*	$NetBSD: filemon_wrapper.c,v 1.11 2015/11/25 07:34:49 pgoyette Exp $	*/
 
 /*
  * Copyright (c) 2010, Juniper Networks, Inc.
@@ -26,7 +26,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: filemon_wrapper.c,v 1.10 2015/11/23 00:47:43 pgoyette Exp $");
+__KERNEL_RCSID(0, "$NetBSD: filemon_wrapper.c,v 1.11 2015/11/25 07:34:49 pgoyette Exp $");
 
 #include 
 #include 
@@ -39,6 +39,83 @@ __KERNEL_RCSID(0, "$NetBSD: filemon_wrap
 
 #include "filemon.h"
 
+static int filemon_wrapper_chdir(struct lwp *, const struct sys_chdir_args *,
+register_t *);
+static int filemon_wrapper_execve(struct lwp *, struct sys_execve_args *,
+register_t *);
+static void filemon_wrapper_sys_exit(struct lwp *, struct sys_exit_args *,
+register_t *);
+static int filemon_wrapper_fork(struct lwp *, const void *, register_t *);
+static int filemon_wrapper_link(struct lwp *, struct sys_link_args *,
+register_t *);
+static int filemon_wrapper_open(struct lwp *, struct sys_open_args *,
+register_t *);
+static int filemon_wrapper_openat(struct lwp *, struct sys_openat_args *,
+register_t *);
+static int filemon_wrapper_rename(struct lwp *, struct sys_rename_args *,
+register_t *);
+static int filemon_wrapper_symlink(struct lwp *, struct sys_symlink_args *,
+register_t *);
+static int filemon_wrapper_unlink(struct lwp *, struct sys_unlink_args *,
+register_t *);
+static 

CVS commit: src/sys/dev/filemon

2015-11-23 Thread Paul Goyette
Module Name:src
Committed By:   pgoyette
Date:   Mon Nov 23 22:20:57 UTC 2015

Modified Files:
src/sys/dev/filemon: filemon.c

Log Message:
Clean up the error paths in filemon_ioctl(), making sure to release the
rw_lock before returning.  Avoids a "locking against myself" error
reported by Brad Harder.


To generate a diff of this commit:
cvs rdiff -u -r1.17 -r1.18 src/sys/dev/filemon/filemon.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/filemon/filemon.c
diff -u src/sys/dev/filemon/filemon.c:1.17 src/sys/dev/filemon/filemon.c:1.18
--- src/sys/dev/filemon/filemon.c:1.17	Sat Nov 21 07:45:30 2015
+++ src/sys/dev/filemon/filemon.c	Mon Nov 23 22:20:57 2015
@@ -24,7 +24,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: filemon.c,v 1.17 2015/11/21 07:45:30 pgoyette Exp $");
+__KERNEL_RCSID(0, "$NetBSD: filemon.c,v 1.18 2015/11/23 22:20:57 pgoyette Exp $");
 
 #include 
 #include 
@@ -291,6 +291,8 @@ filemon_ioctl(struct file * fp, u_long c
 	if (!filemon)
 		return EBADF;
 
+	/* filemon_fp_data() has locked the entry - make sure to unlock! */
+
 	switch (cmd) {
 	case FILEMON_SET_FD:
 		/* Set the output file descriptor. */
@@ -302,8 +304,8 @@ filemon_ioctl(struct file * fp, u_long c
 		/* Now set up the new one */
 		filemon->fm_fd = *((int *) data);
 		if ((filemon->fm_fp = fd_getfile(filemon->fm_fd)) == NULL) {
-			rw_exit(>fm_mtx);
-			return EBADF;
+			error = EBADF;
+			break;
 		}
 		/* Write the file header. */
 		filemon_comment(filemon);
@@ -336,8 +338,10 @@ filemon_ioctl(struct file * fp, u_long c
 			p = p->p_pptr;
 			rw_exit(>p_reflock);
 		}
-		if (p == NULL)
-			return EPERM;
+		if (p == NULL) {
+			error = EPERM;
+			break;
+		}
 
 		error = kauth_authorize_process(curproc->p_cred,
 		KAUTH_PROCESS_CANSEE, tp,



CVS commit: src/sys/dev/filemon

2015-11-23 Thread Paul Goyette
Module Name:src
Committed By:   pgoyette
Date:   Mon Nov 23 23:27:38 UTC 2015

Modified Files:
src/sys/dev/filemon: filemon.c

Log Message:
Remove the requirement that the target (tracked) process be a descendant
of the tracking process.  The call to kauth() should be sufficient to
address any security concerns.


To generate a diff of this commit:
cvs rdiff -u -r1.18 -r1.19 src/sys/dev/filemon/filemon.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/filemon/filemon.c
diff -u src/sys/dev/filemon/filemon.c:1.18 src/sys/dev/filemon/filemon.c:1.19
--- src/sys/dev/filemon/filemon.c:1.18	Mon Nov 23 22:20:57 2015
+++ src/sys/dev/filemon/filemon.c	Mon Nov 23 23:27:38 2015
@@ -24,7 +24,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: filemon.c,v 1.18 2015/11/23 22:20:57 pgoyette Exp $");
+__KERNEL_RCSID(0, "$NetBSD: filemon.c,v 1.19 2015/11/23 23:27:38 pgoyette Exp $");
 
 #include 
 #include 
@@ -322,27 +322,6 @@ filemon_ioctl(struct file * fp, u_long c
 			break;
 		}
 
-		/* Ensure that target proc is a descendant of curproc */
-		p = tp;
-		while (p) {
-			/*
-			 * make sure p cannot exit
-			 * until we have moved on to p_pptr
-			 */
-			rw_enter(>p_reflock, RW_READER);
-			if (p == curproc) {
-rw_exit(>p_reflock);
-break;
-			}
-			lp = p;
-			p = p->p_pptr;
-			rw_exit(>p_reflock);
-		}
-		if (p == NULL) {
-			error = EPERM;
-			break;
-		}
-
 		error = kauth_authorize_process(curproc->p_cred,
 		KAUTH_PROCESS_CANSEE, tp,
 		KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_ENTRY), NULL, NULL);



CVS commit: src/sys/dev/filemon

2015-11-23 Thread Paul Goyette
Module Name:src
Committed By:   pgoyette
Date:   Tue Nov 24 01:05:51 UTC 2015

Modified Files:
src/sys/dev/filemon: filemon.c

Log Message:
Add missing /* $NetBSD$ */ keyword


To generate a diff of this commit:
cvs rdiff -u -r1.20 -r1.21 src/sys/dev/filemon/filemon.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/filemon/filemon.c
diff -u src/sys/dev/filemon/filemon.c:1.20 src/sys/dev/filemon/filemon.c:1.21
--- src/sys/dev/filemon/filemon.c:1.20	Tue Nov 24 01:01:42 2015
+++ src/sys/dev/filemon/filemon.c	Tue Nov 24 01:05:50 2015
@@ -1,3 +1,4 @@
+/*  $NetBSD: filemon.c,v 1.21 2015/11/24 01:05:50 pgoyette Exp $ */
 /*
  * Copyright (c) 2010, Juniper Networks, Inc.
  *
@@ -24,7 +25,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: filemon.c,v 1.20 2015/11/24 01:01:42 pgoyette Exp $");
+__KERNEL_RCSID(0, "$NetBSD: filemon.c,v 1.21 2015/11/24 01:05:50 pgoyette Exp $");
 
 #include 
 #include 



CVS commit: src/sys/dev/filemon

2015-11-23 Thread Paul Goyette
Module Name:src
Committed By:   pgoyette
Date:   Tue Nov 24 01:01:42 UTC 2015

Modified Files:
src/sys/dev/filemon: filemon.c

Log Message:
finish previous - remove no-longer-used variables


To generate a diff of this commit:
cvs rdiff -u -r1.19 -r1.20 src/sys/dev/filemon/filemon.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/filemon/filemon.c
diff -u src/sys/dev/filemon/filemon.c:1.19 src/sys/dev/filemon/filemon.c:1.20
--- src/sys/dev/filemon/filemon.c:1.19	Mon Nov 23 23:27:38 2015
+++ src/sys/dev/filemon/filemon.c	Tue Nov 24 01:01:42 2015
@@ -24,7 +24,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: filemon.c,v 1.19 2015/11/23 23:27:38 pgoyette Exp $");
+__KERNEL_RCSID(0, "$NetBSD: filemon.c,v 1.20 2015/11/24 01:01:42 pgoyette Exp $");
 
 #include 
 #include 
@@ -278,7 +278,7 @@ filemon_ioctl(struct file * fp, u_long c
 {
 	int error = 0;
 	struct filemon *filemon;
-	struct proc *tp, *lp, *p;
+	struct proc *tp;
 
 #ifdef DEBUG
 	log(logLevel, "filemon_ioctl(%lu)", cmd);;



CVS commit: src/sys/dev/filemon

2015-11-22 Thread Paul Goyette
Module Name:src
Committed By:   pgoyette
Date:   Mon Nov 23 00:47:43 UTC 2015

Modified Files:
src/sys/dev/filemon: filemon_wrapper.c

Log Message:
Retrieve the pathname of the exec'd file before we call sys_execve().

We cannot rely on using copyinstr() to retrieve the pathname from
user mode after a successful exec, since the user mode address space
is no longer valid (and may not even be accessible).  This worked in
earlier code but was broken by rev 1.7.


To generate a diff of this commit:
cvs rdiff -u -r1.9 -r1.10 src/sys/dev/filemon/filemon_wrapper.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/filemon/filemon_wrapper.c
diff -u src/sys/dev/filemon/filemon_wrapper.c:1.9 src/sys/dev/filemon/filemon_wrapper.c:1.10
--- src/sys/dev/filemon/filemon_wrapper.c:1.9	Sun Nov 22 01:20:52 2015
+++ src/sys/dev/filemon/filemon_wrapper.c	Mon Nov 23 00:47:43 2015
@@ -1,4 +1,4 @@
-/*	$NetBSD: filemon_wrapper.c,v 1.9 2015/11/22 01:20:52 pgoyette Exp $	*/
+/*	$NetBSD: filemon_wrapper.c,v 1.10 2015/11/23 00:47:43 pgoyette Exp $	*/
 
 /*
  * Copyright (c) 2010, Juniper Networks, Inc.
@@ -26,7 +26,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: filemon_wrapper.c,v 1.9 2015/11/22 01:20:52 pgoyette Exp $");
+__KERNEL_RCSID(0, "$NetBSD: filemon_wrapper.c,v 1.10 2015/11/23 00:47:43 pgoyette Exp $");
 
 #include 
 #include 
@@ -71,10 +71,12 @@ filemon_wrapper_execve(struct lwp * l, s
 register_t * retval)
 {
 	char fname[MAXPATHLEN];
-	int error;
+	int error, cerror;
 	size_t done;
 	struct filemon *filemon;
-	
+
+	cerror = copyinstr(SCARG(uap, path), fname, sizeof(fname), );
+
 	if ((error = sys_execve(l, uap, retval)) != EJUSTRETURN)
 		return error;
 
@@ -82,8 +84,7 @@ filemon_wrapper_execve(struct lwp * l, s
 	if (filemon == NULL)
 		return EJUSTRETURN;
 
-	error = copyinstr(SCARG(uap, path), fname, sizeof(fname), );
-	if (error)
+	if (cerror)
 		goto out;
 
 	filemon_printf(filemon, "E %d %s\n", curproc->p_pid, fname);



CVS commit: src/sys/dev/filemon

2015-11-21 Thread Paul Goyette
Module Name:src
Committed By:   pgoyette
Date:   Sun Nov 22 01:20:52 UTC 2015

Modified Files:
src/sys/dev/filemon: filemon_wrapper.c

Log Message:
Fix return-code handling for execve and chdir wrappers.

Fixes PR kern/50309


To generate a diff of this commit:
cvs rdiff -u -r1.8 -r1.9 src/sys/dev/filemon/filemon_wrapper.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/filemon/filemon_wrapper.c
diff -u src/sys/dev/filemon/filemon_wrapper.c:1.8 src/sys/dev/filemon/filemon_wrapper.c:1.9
--- src/sys/dev/filemon/filemon_wrapper.c:1.8	Fri Nov 20 01:16:04 2015
+++ src/sys/dev/filemon/filemon_wrapper.c	Sun Nov 22 01:20:52 2015
@@ -1,4 +1,4 @@
-/*	$NetBSD: filemon_wrapper.c,v 1.8 2015/11/20 01:16:04 pgoyette Exp $	*/
+/*	$NetBSD: filemon_wrapper.c,v 1.9 2015/11/22 01:20:52 pgoyette Exp $	*/
 
 /*
  * Copyright (c) 2010, Juniper Networks, Inc.
@@ -26,7 +26,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: filemon_wrapper.c,v 1.8 2015/11/20 01:16:04 pgoyette Exp $");
+__KERNEL_RCSID(0, "$NetBSD: filemon_wrapper.c,v 1.9 2015/11/22 01:20:52 pgoyette Exp $");
 
 #include 
 #include 
@@ -48,7 +48,7 @@ filemon_wrapper_chdir(struct lwp * l, co
 	struct filemon *filemon;
 	
 	if ((error = sys_chdir(l, uap, retval)) != 0)
-		return 0;
+		return error;
 
 	filemon = filemon_lookup(curproc);
 	if (filemon == NULL)
@@ -76,11 +76,11 @@ filemon_wrapper_execve(struct lwp * l, s
 	struct filemon *filemon;
 	
 	if ((error = sys_execve(l, uap, retval)) != EJUSTRETURN)
-		return 0;
+		return error;
 
 	filemon = filemon_lookup(curproc);
 	if (filemon == NULL)
-		return 0;
+		return EJUSTRETURN;
 
 	error = copyinstr(SCARG(uap, path), fname, sizeof(fname), );
 	if (error)
@@ -89,7 +89,7 @@ filemon_wrapper_execve(struct lwp * l, s
 	filemon_printf(filemon, "E %d %s\n", curproc->p_pid, fname);
 out:
 	rw_exit(>fm_mtx);
-	return 0;
+	return EJUSTRETURN;
 }
 
 



CVS commit: src/sys/dev/filemon

2015-11-20 Thread Paul Goyette
Module Name:src
Committed By:   pgoyette
Date:   Sat Nov 21 03:34:28 UTC 2015

Modified Files:
src/sys/dev/filemon: filemon.c

Log Message:
The correct default return value from a module's xxx_modcmd() routine
is ENOTTY, not EOPNOTSUPP!  The former will allow the module to be
auto-unloaded, while the latter will prevent it.

Note that manual unloading of the filemon module is unaffected, as
that is controlled by actual usage of the module.


To generate a diff of this commit:
cvs rdiff -u -r1.15 -r1.16 src/sys/dev/filemon/filemon.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/filemon/filemon.c
diff -u src/sys/dev/filemon/filemon.c:1.15 src/sys/dev/filemon/filemon.c:1.16
--- src/sys/dev/filemon/filemon.c:1.15	Fri Nov 20 02:58:19 2015
+++ src/sys/dev/filemon/filemon.c	Sat Nov 21 03:34:28 2015
@@ -24,7 +24,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: filemon.c,v 1.15 2015/11/20 02:58:19 pgoyette Exp $");
+__KERNEL_RCSID(0, "$NetBSD: filemon.c,v 1.16 2015/11/21 03:34:28 pgoyette Exp $");
 
 #include 
 #include 
@@ -426,7 +426,7 @@ filemon_modcmd(modcmd_t cmd, void *data)
 		break;
 
 	default:
-		error = EOPNOTSUPP;
+		error = ENOTTY;
 		break;
 
 	}



CVS commit: src/sys/dev/filemon

2015-11-20 Thread Paul Goyette
Module Name:src
Committed By:   pgoyette
Date:   Sat Nov 21 07:45:30 UTC 2015

Modified Files:
src/sys/dev/filemon: filemon.c

Log Message:
If a second call to the SET_FD ioctl occurs, release the reference we
hold on the earlier outout file.


To generate a diff of this commit:
cvs rdiff -u -r1.16 -r1.17 src/sys/dev/filemon/filemon.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/filemon/filemon.c
diff -u src/sys/dev/filemon/filemon.c:1.16 src/sys/dev/filemon/filemon.c:1.17
--- src/sys/dev/filemon/filemon.c:1.16	Sat Nov 21 03:34:28 2015
+++ src/sys/dev/filemon/filemon.c	Sat Nov 21 07:45:30 2015
@@ -24,7 +24,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: filemon.c,v 1.16 2015/11/21 03:34:28 pgoyette Exp $");
+__KERNEL_RCSID(0, "$NetBSD: filemon.c,v 1.17 2015/11/21 07:45:30 pgoyette Exp $");
 
 #include 
 #include 
@@ -294,6 +294,12 @@ filemon_ioctl(struct file * fp, u_long c
 	switch (cmd) {
 	case FILEMON_SET_FD:
 		/* Set the output file descriptor. */
+
+		/* First, release any current output file descriptor */
+		if (filemon->fm_fp)
+			fd_putfile(filemon->fm_fd);
+
+		/* Now set up the new one */
 		filemon->fm_fd = *((int *) data);
 		if ((filemon->fm_fp = fd_getfile(filemon->fm_fd)) == NULL) {
 			rw_exit(>fm_mtx);



CVS commit: src/sys/dev/filemon

2015-11-19 Thread Paul Goyette
Module Name:src
Committed By:   pgoyette
Date:   Fri Nov 20 01:21:05 UTC 2015

Modified Files:
src/sys/dev/filemon: filemon.c

Log Message:
Don't allow setting the PID-to-trace if the target has a non-native
emulation.


To generate a diff of this commit:
cvs rdiff -u -r1.12 -r1.13 src/sys/dev/filemon/filemon.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/filemon/filemon.c
diff -u src/sys/dev/filemon/filemon.c:1.12 src/sys/dev/filemon/filemon.c:1.13
--- src/sys/dev/filemon/filemon.c:1.12	Fri Nov 20 01:12:38 2015
+++ src/sys/dev/filemon/filemon.c	Fri Nov 20 01:21:05 2015
@@ -24,7 +24,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: filemon.c,v 1.12 2015/11/20 01:12:38 pgoyette Exp $");
+__KERNEL_RCSID(0, "$NetBSD: filemon.c,v 1.13 2015/11/20 01:21:05 pgoyette Exp $");
 
 #include 
 #include 
@@ -308,7 +308,8 @@ filemon_ioctl(struct file * fp, u_long c
 		mutex_enter(proc_lock);
 		tp = proc_find(*((pid_t *) data));
 		mutex_exit(proc_lock);
-		if (tp == NULL) {
+		if (tp == NULL ||
+		tp->p_emul != _netbsd)) {
 			error = ESRCH;
 			break;
 		}



CVS commit: src/sys/dev/filemon

2015-11-19 Thread Paul Goyette
Module Name:src
Committed By:   pgoyette
Date:   Fri Nov 20 01:12:38 UTC 2015

Modified Files:
src/sys/dev/filemon: filemon.c

Log Message:
Don't initialize twice.


To generate a diff of this commit:
cvs rdiff -u -r1.11 -r1.12 src/sys/dev/filemon/filemon.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/filemon/filemon.c
diff -u src/sys/dev/filemon/filemon.c:1.11 src/sys/dev/filemon/filemon.c:1.12
--- src/sys/dev/filemon/filemon.c:1.11	Thu Aug 20 14:40:17 2015
+++ src/sys/dev/filemon/filemon.c	Fri Nov 20 01:12:38 2015
@@ -24,7 +24,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: filemon.c,v 1.11 2015/08/20 14:40:17 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: filemon.c,v 1.12 2015/11/20 01:12:38 pgoyette Exp $");
 
 #include 
 #include 
@@ -344,7 +344,14 @@ filemon_load(void *dummy __unused)
 void
 filemonattach(int num)
 {
-filemon_load(NULL);
+
+	/*
+	 * Don't call filemon_load() here - it will be called from
+	 * filemon_modcmd() during module initialization.
+	 */
+#if 0
+	filemon_load(NULL);
+#endif
 }
 
 



CVS commit: src/sys/dev/filemon

2015-11-19 Thread Paul Goyette
Module Name:src
Committed By:   pgoyette
Date:   Fri Nov 20 01:16:04 UTC 2015

Modified Files:
src/sys/dev/filemon: filemon_wrapper.c

Log Message:
Install wrapper functions only in native emulation.

(This also removes the dependency on curproc, which may not be available
at the time that built-in kernel modules are initialized.)


To generate a diff of this commit:
cvs rdiff -u -r1.7 -r1.8 src/sys/dev/filemon/filemon_wrapper.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/filemon/filemon_wrapper.c
diff -u src/sys/dev/filemon/filemon_wrapper.c:1.7 src/sys/dev/filemon/filemon_wrapper.c:1.8
--- src/sys/dev/filemon/filemon_wrapper.c:1.7	Mon Jun 15 19:45:31 2015
+++ src/sys/dev/filemon/filemon_wrapper.c	Fri Nov 20 01:16:04 2015
@@ -1,4 +1,4 @@
-/*	$NetBSD: filemon_wrapper.c,v 1.7 2015/06/15 19:45:31 christos Exp $	*/
+/*	$NetBSD: filemon_wrapper.c,v 1.8 2015/11/20 01:16:04 pgoyette Exp $	*/
 
 /*
  * Copyright (c) 2010, Juniper Networks, Inc.
@@ -26,7 +26,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: filemon_wrapper.c,v 1.7 2015/06/15 19:45:31 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: filemon_wrapper.c,v 1.8 2015/11/20 01:16:04 pgoyette Exp $");
 
 #include 
 #include 
@@ -354,7 +354,7 @@ out:
 void
 filemon_wrapper_install(void)
 {
-	struct sysent *sv_table = curproc->p_emul->e_sysent;
+	struct sysent *sv_table = emul_netbsd.e_sysent;
 
 	sv_table[SYS_chdir].sy_call = (sy_call_t *) filemon_wrapper_chdir;
 	sv_table[SYS_execve].sy_call = (sy_call_t *) filemon_wrapper_execve;
@@ -372,7 +372,7 @@ filemon_wrapper_install(void)
 int
 filemon_wrapper_deinstall(void)
 {
-	struct sysent *sv_table = curproc->p_emul->e_sysent;
+	struct sysent *sv_table = emul_netbsd.e_sysent;
 
 	if (sv_table[SYS_chdir].sy_call != (sy_call_t *) filemon_wrapper_chdir)
 	return EBUSY;



CVS commit: src/sys/dev/filemon

2015-11-19 Thread Paul Goyette
Module Name:src
Committed By:   pgoyette
Date:   Fri Nov 20 01:33:59 UTC 2015

Modified Files:
src/sys/dev/filemon: filemon.c

Log Message:
Remove extra ')' (bad cut)


To generate a diff of this commit:
cvs rdiff -u -r1.13 -r1.14 src/sys/dev/filemon/filemon.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/filemon/filemon.c
diff -u src/sys/dev/filemon/filemon.c:1.13 src/sys/dev/filemon/filemon.c:1.14
--- src/sys/dev/filemon/filemon.c:1.13	Fri Nov 20 01:21:05 2015
+++ src/sys/dev/filemon/filemon.c	Fri Nov 20 01:33:59 2015
@@ -24,7 +24,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: filemon.c,v 1.13 2015/11/20 01:21:05 pgoyette Exp $");
+__KERNEL_RCSID(0, "$NetBSD: filemon.c,v 1.14 2015/11/20 01:33:59 pgoyette Exp $");
 
 #include 
 #include 
@@ -309,7 +309,7 @@ filemon_ioctl(struct file * fp, u_long c
 		tp = proc_find(*((pid_t *) data));
 		mutex_exit(proc_lock);
 		if (tp == NULL ||
-		tp->p_emul != _netbsd)) {
+		tp->p_emul != _netbsd) {
 			error = ESRCH;
 			break;
 		}



CVS commit: src/sys/dev/filemon

2015-11-19 Thread Paul Goyette
Module Name:src
Committed By:   pgoyette
Date:   Fri Nov 20 02:58:19 UTC 2015

Modified Files:
src/sys/dev/filemon: filemon.c

Log Message:
Ensure that the PID specified in the FILEMON_SET_PID ioctl() call
belongs to the caller or one of its descendants.


To generate a diff of this commit:
cvs rdiff -u -r1.14 -r1.15 src/sys/dev/filemon/filemon.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/filemon/filemon.c
diff -u src/sys/dev/filemon/filemon.c:1.14 src/sys/dev/filemon/filemon.c:1.15
--- src/sys/dev/filemon/filemon.c:1.14	Fri Nov 20 01:33:59 2015
+++ src/sys/dev/filemon/filemon.c	Fri Nov 20 02:58:19 2015
@@ -24,7 +24,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: filemon.c,v 1.14 2015/11/20 01:33:59 pgoyette Exp $");
+__KERNEL_RCSID(0, "$NetBSD: filemon.c,v 1.15 2015/11/20 02:58:19 pgoyette Exp $");
 
 #include 
 #include 
@@ -278,7 +278,7 @@ filemon_ioctl(struct file * fp, u_long c
 {
 	int error = 0;
 	struct filemon *filemon;
-	struct proc *tp;
+	struct proc *tp, *lp, *p;
 
 #ifdef DEBUG
 	log(logLevel, "filemon_ioctl(%lu)", cmd);;
@@ -313,6 +313,26 @@ filemon_ioctl(struct file * fp, u_long c
 			error = ESRCH;
 			break;
 		}
+
+		/* Ensure that target proc is a descendant of curproc */
+		p = tp;
+		while (p) {
+			/*
+			 * make sure p cannot exit
+			 * until we have moved on to p_pptr
+			 */
+			rw_enter(>p_reflock, RW_READER);
+			if (p == curproc) {
+rw_exit(>p_reflock);
+break;
+			}
+			lp = p;
+			p = p->p_pptr;
+			rw_exit(>p_reflock);
+		}
+		if (p == NULL)
+			return EPERM;
+
 		error = kauth_authorize_process(curproc->p_cred,
 		KAUTH_PROCESS_CANSEE, tp,
 		KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_ENTRY), NULL, NULL);



CVS commit: src/sys/dev/filemon

2015-06-16 Thread Simon J. Gerraty
Module Name:src
Committed By:   sjg
Date:   Wed Jun 17 02:17:00 UTC 2015

Modified Files:
src/sys/dev/filemon: filemon.h

Log Message:
Bump version due to openat


To generate a diff of this commit:
cvs rdiff -u -r1.5 -r1.6 src/sys/dev/filemon/filemon.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/filemon/filemon.h
diff -u src/sys/dev/filemon/filemon.h:1.5 src/sys/dev/filemon/filemon.h:1.6
--- src/sys/dev/filemon/filemon.h:1.5	Thu Mar 27 18:27:34 2014
+++ src/sys/dev/filemon/filemon.h	Wed Jun 17 02:17:00 2015
@@ -1,4 +1,4 @@
-/* $NetBSD: filemon.h,v 1.5 2014/03/27 18:27:34 christos Exp $ */
+/* $NetBSD: filemon.h,v 1.6 2015/06/17 02:17:00 sjg Exp $ */
 /*
  * Copyright (c) 2010, Juniper Networks, Inc.
  *
@@ -31,7 +31,7 @@
 #define FILEMON_SET_FD		_IOWR('S', 1, int)
 #define FILEMON_SET_PID		_IOWR('S', 2, pid_t)
 
-#define FILEMON_VERSION		4
+#define FILEMON_VERSION		5
 
 #ifdef _KERNEL
 struct filemon {



CVS commit: src/sys/dev/filemon

2015-06-15 Thread Simon J. Gerraty
Module Name:src
Committed By:   sjg
Date:   Mon Jun 15 18:11:36 UTC 2015

Modified Files:
src/sys/dev/filemon: filemon_wrapper.c

Log Message:
Latest clang uses openat, filemon thus needs to handle it.

For the simple cases where path is absolute or dirfd is for cwd
we can handle this just like open.
For the remaining case we output an 'A' record to offer a clue that
a search for open dir is needed (earlier in the trace).


To generate a diff of this commit:
cvs rdiff -u -r1.5 -r1.6 src/sys/dev/filemon/filemon_wrapper.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/filemon/filemon_wrapper.c
diff -u src/sys/dev/filemon/filemon_wrapper.c:1.5 src/sys/dev/filemon/filemon_wrapper.c:1.6
--- src/sys/dev/filemon/filemon_wrapper.c:1.5	Thu Mar 27 18:27:34 2014
+++ src/sys/dev/filemon/filemon_wrapper.c	Mon Jun 15 18:11:36 2015
@@ -24,7 +24,7 @@
  */
 
 #include sys/cdefs.h
-__KERNEL_RCSID(0, $NetBSD: filemon_wrapper.c,v 1.5 2014/03/27 18:27:34 christos Exp $);
+__KERNEL_RCSID(0, $NetBSD: filemon_wrapper.c,v 1.6 2015/06/15 18:11:36 sjg Exp $);
 
 #include sys/param.h
 #include sys/types.h
@@ -160,6 +160,51 @@ filemon_wrapper_open(struct lwp * l, str
 }
 
 static int
+filemon_wrapper_openat(struct lwp * l, struct sys_openat_args * uap,
+register_t * retval)
+{
+	int ret;
+	int error;
+	size_t done;
+	struct filemon *filemon;
+
+	if ((ret = sys_openat(l, uap, retval)) == 0) {
+		filemon = filemon_lookup(curproc);
+
+		if (filemon) {
+			error = copyinstr(SCARG(uap, path), filemon-fm_fname1,
+			sizeof(filemon-fm_fname1), done);
+			if (error == 0) {
+if (filemon-fm_fname1[0] != '/' 
+SCARG(uap, fd) != AT_FDCWD) {
+	/*
+	 * Rats we cannot just treat like open.
+	 * Output an 'A' record as a clue.
+	 */
+	filemon_printf(filemon,
+		A %d %s\n,
+		curproc-p_pid,
+		filemon-fm_fname1);
+}
+if (SCARG(uap, oflags)  O_RDWR) {
+	/* we want a separate R record */
+	filemon_printf(filemon,
+		R %d %s\n,
+		curproc-p_pid,
+		filemon-fm_fname1);
+}			
+filemon_printf(filemon,
+%c %d %s\n,
+(SCARG(uap, oflags)  O_ACCMODE) ? 'W' : 'R',
+curproc-p_pid, filemon-fm_fname1);
+			}
+			rw_exit(filemon-fm_mtx);
+		}
+	}
+	return (ret);
+}
+
+static int
 filemon_wrapper_rename(struct lwp * l, struct sys_rename_args * uap,
 register_t * retval)
 {
@@ -311,6 +356,7 @@ filemon_wrapper_install(void)
 	sv_table[SYS_fork].sy_call = (sy_call_t *) filemon_wrapper_fork;
 	sv_table[SYS_link].sy_call = (sy_call_t *) filemon_wrapper_link;
 	sv_table[SYS_open].sy_call = (sy_call_t *) filemon_wrapper_open;
+	sv_table[SYS_openat].sy_call = (sy_call_t *) filemon_wrapper_openat;
 	sv_table[SYS_rename].sy_call = (sy_call_t *) filemon_wrapper_rename;
 	sv_table[SYS_symlink].sy_call = (sy_call_t *) filemon_wrapper_symlink;
 	sv_table[SYS_unlink].sy_call = (sy_call_t *) filemon_wrapper_unlink;
@@ -330,6 +376,7 @@ filemon_wrapper_deinstall(void)
 	sv_table[SYS_fork].sy_call = (sy_call_t *) sys_fork;
 	sv_table[SYS_link].sy_call = (sy_call_t *) sys_link;
 	sv_table[SYS_open].sy_call = (sy_call_t *) sys_open;
+	sv_table[SYS_openat].sy_call = (sy_call_t *) sys_openat;
 	sv_table[SYS_rename].sy_call = (sy_call_t *) sys_rename;
 	sv_table[SYS_symlink].sy_call = (sy_call_t *) sys_symlink;
 	sv_table[SYS_unlink].sy_call = (sy_call_t *) sys_unlink;



CVS commit: src/sys/dev/filemon

2015-06-15 Thread Christos Zoulas
Module Name:src
Committed By:   christos
Date:   Mon Jun 15 19:45:31 UTC 2015

Modified Files:
src/sys/dev/filemon: filemon_wrapper.c

Log Message:
staircase flattening police.


To generate a diff of this commit:
cvs rdiff -u -r1.6 -r1.7 src/sys/dev/filemon/filemon_wrapper.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/filemon/filemon_wrapper.c
diff -u src/sys/dev/filemon/filemon_wrapper.c:1.6 src/sys/dev/filemon/filemon_wrapper.c:1.7
--- src/sys/dev/filemon/filemon_wrapper.c:1.6	Mon Jun 15 14:11:36 2015
+++ src/sys/dev/filemon/filemon_wrapper.c	Mon Jun 15 15:45:31 2015
@@ -1,3 +1,5 @@
+/*	$NetBSD: filemon_wrapper.c,v 1.7 2015/06/15 19:45:31 christos Exp $	*/
+
 /*
  * Copyright (c) 2010, Juniper Networks, Inc.
  *
@@ -24,7 +26,7 @@
  */
 
 #include sys/cdefs.h
-__KERNEL_RCSID(0, $NetBSD: filemon_wrapper.c,v 1.6 2015/06/15 18:11:36 sjg Exp $);
+__KERNEL_RCSID(0, $NetBSD: filemon_wrapper.c,v 1.7 2015/06/15 19:45:31 christos Exp $);
 
 #include sys/param.h
 #include sys/types.h
@@ -41,27 +43,27 @@ static int
 filemon_wrapper_chdir(struct lwp * l, const struct sys_chdir_args * uap,
 register_t * retval)
 {
-	int ret;
 	int error;
 	size_t done;
 	struct filemon *filemon;
 	
-	if ((ret = sys_chdir(l, uap, retval)) == 0) {
-		filemon = filemon_lookup(curproc);
+	if ((error = sys_chdir(l, uap, retval)) != 0)
+		return 0;
 
-		if (filemon) {
+	filemon = filemon_lookup(curproc);
+	if (filemon == NULL)
+		return 0;
 
-			error = copyinstr(SCARG(uap, path), filemon-fm_fname1,
-			sizeof(filemon-fm_fname1), done);
-			if (error == 0) {
-filemon_printf(filemon,
-C %d %s\n,
-curproc-p_pid, filemon-fm_fname1);
-			}
-			rw_exit(filemon-fm_mtx);
-		}
-	}
-	return (ret);
+	error = copyinstr(SCARG(uap, path), filemon-fm_fname1,
+	sizeof(filemon-fm_fname1), done);
+	if (error)
+		goto out;
+
+	filemon_printf(filemon, C %d %s\n,
+	curproc-p_pid, filemon-fm_fname1);
+out:
+	rw_exit(filemon-fm_mtx);
+	return 0;
 }
 
 static int
@@ -69,232 +71,234 @@ filemon_wrapper_execve(struct lwp * l, s
 register_t * retval)
 {
 	char fname[MAXPATHLEN];
-	int ret;
 	int error;
 	size_t done;
 	struct filemon *filemon;
 	
-	error = copyinstr(SCARG(uap, path), fname, sizeof(fname), done);
+	if ((error = sys_execve(l, uap, retval)) != EJUSTRETURN)
+		return 0;
 
-	if ((ret = sys_execve(l, uap, retval)) == EJUSTRETURN  error == 0) {
-		filemon = filemon_lookup(curproc);
+	filemon = filemon_lookup(curproc);
+	if (filemon == NULL)
+		return 0;
 
-		if (filemon) {
-			filemon_printf(filemon, E %d %s\n,
-			curproc-p_pid, fname);
-			rw_exit(filemon-fm_mtx);
-		}
-	}
-	return (ret);
+	error = copyinstr(SCARG(uap, path), fname, sizeof(fname), done);
+	if (error)
+		goto out;
+
+	filemon_printf(filemon, E %d %s\n, curproc-p_pid, fname);
+out:
+	rw_exit(filemon-fm_mtx);
+	return 0;
 }
 
 
 static int
 filemon_wrapper_fork(struct lwp * l, const void *v, register_t * retval)
 {
-	int ret;
+	int error;
 	struct filemon *filemon;
 
-	if ((ret = sys_fork(l, v, retval)) == 0) {
-		filemon = filemon_lookup(curproc);
+	if ((error = sys_fork(l, v, retval)) != 0)
+		return error;
 
-		if (filemon) {
-			filemon_printf(filemon, F %d %ld\n,
-			curproc-p_pid, (long) retval[0]);
-			rw_exit(filemon-fm_mtx);
-		}
-	}
-	return (ret);
+	filemon = filemon_lookup(curproc);
+	if (filemon == NULL)
+		return 0;
+
+	filemon_printf(filemon, F %d %ld\n, curproc-p_pid, (long) retval[0]);
+
+	rw_exit(filemon-fm_mtx);
+	return 0;
 }
 
 static int
 filemon_wrapper_vfork(struct lwp * l, const void *v, register_t * retval)
 {
-	int ret;
+	int error;
 	struct filemon *filemon;
 
-	if ((ret = sys_vfork(l, v, retval)) == 0) {
-		filemon = filemon_lookup(curproc);
+	if ((error = sys_vfork(l, v, retval)) != 0)
+		return error;
+
+	filemon = filemon_lookup(curproc);
+	if (filemon == NULL)
+		return 0;
+
+	filemon_printf(filemon, F %d %ld\n, curproc-p_pid, (long) retval[0]);
 
-		if (filemon) {
-			filemon_printf(filemon, F %d %ld\n,
-			curproc-p_pid, (long) retval[0]);
-			rw_exit(filemon-fm_mtx);
-		}
+	rw_exit(filemon-fm_mtx);
+	return 0;
+}
+
+static void
+filemon_flags(struct filemon * filemon, int f)
+{
+	if (f  O_RDWR) {
+		/* we want a separate R record */
+		filemon_printf(filemon, R %d %s\n, curproc-p_pid,
+		filemon-fm_fname1);
 	}
-	return (ret);
+
+	filemon_printf(filemon, %c %d %s\n, (f  O_ACCMODE) ? 'W' : 'R',
+	curproc-p_pid, filemon-fm_fname1);
 }
 
 static int
 filemon_wrapper_open(struct lwp * l, struct sys_open_args * uap,
 register_t * retval)
 {
-	int ret;
 	int error;
 	size_t done;
 	struct filemon *filemon;
 
-	if ((ret = sys_open(l, uap, retval)) == 0) {
-		filemon = filemon_lookup(curproc);
+	if ((error = sys_open(l, uap, retval)) != 0)
+		return error;
 
-		if (filemon) {
-			error = copyinstr(SCARG(uap, path), filemon-fm_fname1,
-			sizeof(filemon-fm_fname1), done);
-	

CVS commit: src/sys/dev/filemon

2015-05-21 Thread Christos Zoulas
Module Name:src
Committed By:   christos
Date:   Thu May 21 12:00:59 UTC 2015

Modified Files:
src/sys/dev/filemon: filemon.c

Log Message:
Avoid dereferencing NULL tp crashing the kernel (brad harder)
XXX: pullup 7


To generate a diff of this commit:
cvs rdiff -u -r1.8 -r1.9 src/sys/dev/filemon/filemon.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/filemon/filemon.c
diff -u src/sys/dev/filemon/filemon.c:1.8 src/sys/dev/filemon/filemon.c:1.9
--- src/sys/dev/filemon/filemon.c:1.8	Fri Jul 25 04:10:36 2014
+++ src/sys/dev/filemon/filemon.c	Thu May 21 08:00:59 2015
@@ -24,7 +24,7 @@
  */
 
 #include sys/cdefs.h
-__KERNEL_RCSID(0, $NetBSD: filemon.c,v 1.8 2014/07/25 08:10:36 dholland Exp $);
+__KERNEL_RCSID(0, $NetBSD: filemon.c,v 1.9 2015/05/21 12:00:59 christos Exp $);
 
 #include sys/param.h
 #include sys/kernel.h
@@ -307,12 +307,15 @@ filemon_ioctl(struct file * fp, u_long c
 		mutex_enter(proc_lock);
 		tp = proc_find(*((pid_t *) data));
 		mutex_exit(proc_lock);
+		if (tp == NULL) {
+			error = ESRCH;
+			break;
+		}
 		error = kauth_authorize_process(curproc-p_cred,
 		KAUTH_PROCESS_CANSEE, tp,
 		KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_ENTRY), NULL, NULL);
 		if (!error) {
 			filemon-fm_pid = tp-p_pid;
-
 		}
 		break;
 



CVS commit: src/sys/dev/filemon

2014-03-27 Thread Christos Zoulas
Module Name:src
Committed By:   christos
Date:   Thu Mar 27 18:27:34 UTC 2014

Modified Files:
src/sys/dev/filemon: filemon.c filemon.h filemon_wrapper.c

Log Message:
remove a bunch of repetitive code by introducing filemon_printf.


To generate a diff of this commit:
cvs rdiff -u -r1.6 -r1.7 src/sys/dev/filemon/filemon.c
cvs rdiff -u -r1.4 -r1.5 src/sys/dev/filemon/filemon.h \
src/sys/dev/filemon/filemon_wrapper.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/filemon/filemon.c
diff -u src/sys/dev/filemon/filemon.c:1.6 src/sys/dev/filemon/filemon.c:1.7
--- src/sys/dev/filemon/filemon.c:1.6	Sun Mar 16 01:20:27 2014
+++ src/sys/dev/filemon/filemon.c	Thu Mar 27 14:27:34 2014
@@ -24,7 +24,7 @@
  */
 
 #include sys/cdefs.h
-__KERNEL_RCSID(0, $NetBSD: filemon.c,v 1.6 2014/03/16 05:20:27 dholland Exp $);
+__KERNEL_RCSID(0, $NetBSD: filemon.c,v 1.7 2014/03/27 18:27:34 christos Exp $);
 
 #include sys/param.h
 #include sys/kernel.h
@@ -121,18 +121,28 @@ filemon_output(struct filemon * filemon,
 	auio, curlwp-l_cred, FOF_UPDATE_OFFSET);
 }
 
-static void
-filemon_comment(struct filemon * filemon)
+void
+filemon_printf(struct filemon *filemon, const char *fmt, ...)
 {
-	int len;
-
-	len = snprintf(filemon-fm_msgbufr, sizeof(filemon-fm_msgbufr),
-	# filemon version %d\n# Target pid %d\nV %d\n,
-		   FILEMON_VERSION, curproc-p_pid, FILEMON_VERSION);
+	size_t len;
+	va_list ap;
 
+	va_start(ap, fmt);
+	len = vsnprintf(filemon-fm_msgbufr, sizeof(filemon-fm_msgbufr),
+	fmt, ap);
+	va_end(ap);
+	if (len  sizeof(filemon-fm_msgbufr))
+		len = sizeof(filemon-fm_msgbufr);
 	filemon_output(filemon, filemon-fm_msgbufr, len);
 }
 
+static void
+filemon_comment(struct filemon * filemon)
+{
+	filemon_printf(filemon, # filemon version %d\n# Target pid %d\nV %d\n,
+	   FILEMON_VERSION, curproc-p_pid, FILEMON_VERSION);
+}
+
 
 static struct filemon *
 filemon_pid_check(struct proc * p)

Index: src/sys/dev/filemon/filemon.h
diff -u src/sys/dev/filemon/filemon.h:1.4 src/sys/dev/filemon/filemon.h:1.5
--- src/sys/dev/filemon/filemon.h:1.4	Mon Nov 19 17:20:10 2012
+++ src/sys/dev/filemon/filemon.h	Thu Mar 27 14:27:34 2014
@@ -1,4 +1,4 @@
-/* $NetBSD: filemon.h,v 1.4 2012/11/19 22:20:10 sjg Exp $ */
+/* $NetBSD: filemon.h,v 1.5 2014/03/27 18:27:34 christos Exp $ */
 /*
  * Copyright (c) 2010, Juniper Networks, Inc.
  *
@@ -49,6 +49,7 @@ struct filemon * filemon_lookup(struct p
 void filemon_output(struct filemon *, char *, size_t);
 void filemon_wrapper_install(void);
 int  filemon_wrapper_deinstall(void);
+void filemon_printf(struct filemon *, const char *, ...) __printflike(2, 3);
 #endif
 
 #endif
Index: src/sys/dev/filemon/filemon_wrapper.c
diff -u src/sys/dev/filemon/filemon_wrapper.c:1.4 src/sys/dev/filemon/filemon_wrapper.c:1.5
--- src/sys/dev/filemon/filemon_wrapper.c:1.4	Mon Nov 19 17:20:10 2012
+++ src/sys/dev/filemon/filemon_wrapper.c	Thu Mar 27 14:27:34 2014
@@ -24,7 +24,7 @@
  */
 
 #include sys/cdefs.h
-__KERNEL_RCSID(0, $NetBSD: filemon_wrapper.c,v 1.4 2012/11/19 22:20:10 sjg Exp $);
+__KERNEL_RCSID(0, $NetBSD: filemon_wrapper.c,v 1.5 2014/03/27 18:27:34 christos Exp $);
 
 #include sys/param.h
 #include sys/types.h
@@ -44,7 +44,6 @@ filemon_wrapper_chdir(struct lwp * l, co
 	int ret;
 	int error;
 	size_t done;
-	size_t len;
 	struct filemon *filemon;
 	
 	if ((ret = sys_chdir(l, uap, retval)) == 0) {
@@ -55,13 +54,9 @@ filemon_wrapper_chdir(struct lwp * l, co
 			error = copyinstr(SCARG(uap, path), filemon-fm_fname1,
 			sizeof(filemon-fm_fname1), done);
 			if (error == 0) {
-len = snprintf(filemon-fm_msgbufr,
-sizeof(filemon-fm_msgbufr),
+filemon_printf(filemon,
 C %d %s\n,
 curproc-p_pid, filemon-fm_fname1);
-
-filemon_output(filemon, filemon-fm_msgbufr,
-len);
 			}
 			rw_exit(filemon-fm_mtx);
 		}
@@ -77,7 +72,6 @@ filemon_wrapper_execve(struct lwp * l, s
 	int ret;
 	int error;
 	size_t done;
-	size_t len;
 	struct filemon *filemon;
 	
 	error = copyinstr(SCARG(uap, path), fname, sizeof(fname), done);
@@ -86,12 +80,8 @@ filemon_wrapper_execve(struct lwp * l, s
 		filemon = filemon_lookup(curproc);
 
 		if (filemon) {
-
-			len = snprintf(filemon-fm_msgbufr, sizeof(filemon-fm_msgbufr),
-			E %d %s\n,
+			filemon_printf(filemon, E %d %s\n,
 			curproc-p_pid, fname);
-
-			filemon_output(filemon, filemon-fm_msgbufr, len);
 			rw_exit(filemon-fm_mtx);
 		}
 	}
@@ -103,20 +93,14 @@ static int
 filemon_wrapper_fork(struct lwp * l, const void *v, register_t * retval)
 {
 	int ret;
-	size_t len;
 	struct filemon *filemon;
 
 	if ((ret = sys_fork(l, v, retval)) == 0) {
 		filemon = filemon_lookup(curproc);
 
 		if (filemon) {
-			len = snprintf(filemon-fm_msgbufr,
-			sizeof(filemon-fm_msgbufr),
-			F %d %ld\n,
+			filemon_printf(filemon, F %d %ld\n,
 			curproc-p_pid, (long) retval[0]);
-
-			filemon_output(filemon, 

CVS commit: src/sys/dev/filemon

2012-11-19 Thread Simon J. Gerraty
Module Name:src
Committed By:   sjg
Date:   Mon Nov 19 22:20:10 UTC 2012

Modified Files:
src/sys/dev/filemon: filemon.c filemon.h filemon_wrapper.c

Log Message:
filemon_pid_check:
Avoid recursion, and hold a reader lock on p_reflock while we
check for filemon, and until we have p_pptr.

filemon_ioctl:
Do not allow FILEMON_SET_PID unless caller would be allowed
to ptrace the target pid.

filemon_wrapper_deinstall:
Do not touch syscalls if they no longer point to us, return EBUSY.

filemon_unload:
return EBUSY if filemon_wrapper_deinstall() fails.


To generate a diff of this commit:
cvs rdiff -u -r1.4 -r1.5 src/sys/dev/filemon/filemon.c
cvs rdiff -u -r1.3 -r1.4 src/sys/dev/filemon/filemon.h \
src/sys/dev/filemon/filemon_wrapper.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/filemon/filemon.c
diff -u src/sys/dev/filemon/filemon.c:1.4 src/sys/dev/filemon/filemon.c:1.5
--- src/sys/dev/filemon/filemon.c:1.4	Sat Oct 15 00:23:08 2011
+++ src/sys/dev/filemon/filemon.c	Mon Nov 19 22:20:10 2012
@@ -24,7 +24,7 @@
  */
 
 #include sys/cdefs.h
-__KERNEL_RCSID(0, $NetBSD: filemon.c,v 1.4 2011/10/15 00:23:08 sjg Exp $);
+__KERNEL_RCSID(0, $NetBSD: filemon.c,v 1.5 2012/11/19 22:20:10 sjg Exp $);
 
 #include sys/param.h
 #include sys/kernel.h
@@ -41,6 +41,7 @@ __KERNEL_RCSID(0, $NetBSD: filemon.c,v 
 #include sys/proc.h
 #include sys/kmem.h
 #include sys/syslog.h
+#include sys/kauth.h
 
 #include filemon.h
 
@@ -137,16 +138,27 @@ static struct filemon *
 filemon_pid_check(struct proc * p)
 {
 	struct filemon *filemon;
+	struct proc * lp;
 
-	TAILQ_FOREACH(filemon, filemons_inuse, fm_link) {
-		if (p-p_pid == filemon-fm_pid)
-			return (filemon);
+	if (!TAILQ_EMPTY(filemons_inuse)) {
+		while (p) {
+			/*
+			 * make sure p cannot exit
+			 * until we have moved on to p_pptr
+			 */
+			rw_enter(p-p_reflock, RW_READER);
+			TAILQ_FOREACH(filemon, filemons_inuse, fm_link) {
+if (p-p_pid == filemon-fm_pid) {
+	rw_exit(p-p_reflock);
+	return (filemon);
+}
+			}
+			lp = p;
+			p = p-p_pptr;
+			rw_exit(lp-p_reflock);
+		}
 	}
-
-	if (p-p_pptr == NULL)
-		return (NULL);
-
-	return (filemon_pid_check(p-p_pptr));
+	return (NULL);
 }
 
 /*
@@ -254,7 +266,7 @@ filemon_ioctl(struct file * fp, u_long c
 {
 	int error = 0;
 	struct filemon *filemon;
-
+	struct proc *tp;
 
 #ifdef DEBUG
 	log(logLevel, filemon_ioctl(%lu), cmd);;
@@ -280,8 +292,17 @@ filemon_ioctl(struct file * fp, u_long c
 		break;
 
 	case FILEMON_SET_PID:
-		/* Set the monitored process ID. */
-		filemon-fm_pid = *((pid_t *) data);
+		/* Set the monitored process ID - if allowed. */
+		mutex_enter(proc_lock);
+		tp = proc_find(*((pid_t *) data));
+		mutex_exit(proc_lock);
+		error = kauth_authorize_process(curproc-p_cred,
+		KAUTH_PROCESS_CANSEE, tp,
+		KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_ENTRY), NULL, NULL);
+		if (!error) {
+			filemon-fm_pid = tp-p_pid;
+
+		}
 		break;
 
 	default:
@@ -325,7 +346,7 @@ filemon_unload(void)
 		error = EBUSY;
 	else {
 		/* Deinstall the syscall wrappers. */
-		filemon_wrapper_deinstall();
+		error = filemon_wrapper_deinstall();
 	}
 	rw_exit(filemon_mtx);
 

Index: src/sys/dev/filemon/filemon.h
diff -u src/sys/dev/filemon/filemon.h:1.3 src/sys/dev/filemon/filemon.h:1.4
--- src/sys/dev/filemon/filemon.h:1.3	Sat Sep 24 18:08:15 2011
+++ src/sys/dev/filemon/filemon.h	Mon Nov 19 22:20:10 2012
@@ -1,4 +1,4 @@
-/* $NetBSD: filemon.h,v 1.3 2011/09/24 18:08:15 sjg Exp $ */
+/* $NetBSD: filemon.h,v 1.4 2012/11/19 22:20:10 sjg Exp $ */
 /*
  * Copyright (c) 2010, Juniper Networks, Inc.
  *
@@ -48,7 +48,7 @@ struct filemon {
 struct filemon * filemon_lookup(struct proc *);
 void filemon_output(struct filemon *, char *, size_t);
 void filemon_wrapper_install(void);
-void filemon_wrapper_deinstall(void);
+int  filemon_wrapper_deinstall(void);
 #endif
 
 #endif
Index: src/sys/dev/filemon/filemon_wrapper.c
diff -u src/sys/dev/filemon/filemon_wrapper.c:1.3 src/sys/dev/filemon/filemon_wrapper.c:1.4
--- src/sys/dev/filemon/filemon_wrapper.c:1.3	Sat Sep 24 18:08:15 2011
+++ src/sys/dev/filemon/filemon_wrapper.c	Mon Nov 19 22:20:10 2012
@@ -24,7 +24,7 @@
  */
 
 #include sys/cdefs.h
-__KERNEL_RCSID(0, $NetBSD: filemon_wrapper.c,v 1.3 2011/09/24 18:08:15 sjg Exp $);
+__KERNEL_RCSID(0, $NetBSD: filemon_wrapper.c,v 1.4 2012/11/19 22:20:10 sjg Exp $);
 
 #include sys/param.h
 #include sys/types.h
@@ -378,11 +378,13 @@ filemon_wrapper_install(void)
 	sv_table[SYS_vfork].sy_call = (sy_call_t *) filemon_wrapper_vfork;
 }
 
-void
+int
 filemon_wrapper_deinstall(void)
 {
 	struct sysent *sv_table = curproc-p_emul-e_sysent;
 
+	if (sv_table[SYS_chdir].sy_call != (sy_call_t *) filemon_wrapper_chdir)
+	return EBUSY;
 	sv_table[SYS_chdir].sy_call = (sy_call_t *) sys_chdir;
 	sv_table[SYS_execve].sy_call = (sy_call_t *) sys_execve;
 	sv_table[SYS_exit].sy_call = (sy_call_t *) sys_exit;
@@ -393,4 +395,5 @@ 

CVS commit: src/sys/dev/filemon

2011-09-24 Thread Simon J. Gerraty
Module Name:src
Committed By:   sjg
Date:   Sat Sep 24 18:08:15 UTC 2011

Modified Files:
src/sys/dev/filemon: filemon.h filemon_wrapper.c

Log Message:
For files opened O_RDWR, output an R record as well as W
so that clients that only care about files read can ignore W.
Bump version.


To generate a diff of this commit:
cvs rdiff -u -r1.2 -r1.3 src/sys/dev/filemon/filemon.h \
src/sys/dev/filemon/filemon_wrapper.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/filemon/filemon.h
diff -u src/sys/dev/filemon/filemon.h:1.2 src/sys/dev/filemon/filemon.h:1.3
--- src/sys/dev/filemon/filemon.h:1.2	Mon Jul  4 23:37:30 2011
+++ src/sys/dev/filemon/filemon.h	Sat Sep 24 18:08:15 2011
@@ -1,4 +1,4 @@
-/* $NetBSD: filemon.h,v 1.2 2011/07/04 23:37:30 sjg Exp $ */
+/* $NetBSD: filemon.h,v 1.3 2011/09/24 18:08:15 sjg Exp $ */
 /*
  * Copyright (c) 2010, Juniper Networks, Inc.
  *
@@ -31,7 +31,7 @@
 #define FILEMON_SET_FD		_IOWR('S', 1, int)
 #define FILEMON_SET_PID		_IOWR('S', 2, pid_t)
 
-#define FILEMON_VERSION		3
+#define FILEMON_VERSION		4
 
 #ifdef _KERNEL
 struct filemon {
Index: src/sys/dev/filemon/filemon_wrapper.c
diff -u src/sys/dev/filemon/filemon_wrapper.c:1.2 src/sys/dev/filemon/filemon_wrapper.c:1.3
--- src/sys/dev/filemon/filemon_wrapper.c:1.2	Sun Mar 13 21:26:31 2011
+++ src/sys/dev/filemon/filemon_wrapper.c	Sat Sep 24 18:08:15 2011
@@ -24,7 +24,7 @@
  */
 
 #include sys/cdefs.h
-__KERNEL_RCSID(0, $NetBSD: filemon_wrapper.c,v 1.2 2011/03/13 21:26:31 sjg Exp $);
+__KERNEL_RCSID(0, $NetBSD: filemon_wrapper.c,v 1.3 2011/09/24 18:08:15 sjg Exp $);
 
 #include sys/param.h
 #include sys/types.h
@@ -164,6 +164,17 @@ filemon_wrapper_open(struct lwp * l, str
 			error = copyinstr(SCARG(uap, path), filemon-fm_fname1,
 			sizeof(filemon-fm_fname1), done);
 			if (error == 0) {
+if (SCARG(uap, flags)  O_RDWR) {
+	/* we want a separate R record */
+	len = snprintf(filemon-fm_msgbufr,
+		sizeof(filemon-fm_msgbufr),
+		R %d %s\n,
+		curproc-p_pid,
+		filemon-fm_fname1);
+
+	filemon_output(filemon,
+		filemon-fm_msgbufr, len);
+}			
 len = snprintf(filemon-fm_msgbufr,
 sizeof(filemon-fm_msgbufr),
 %c %d %s\n,



CVS commit: src/sys/dev/filemon

2011-07-04 Thread Simon J. Gerraty
Module Name:src
Committed By:   sjg
Date:   Mon Jul  4 23:37:30 UTC 2011

Modified Files:
src/sys/dev/filemon: filemon.c filemon.h

Log Message:
Set FILEMON_VERSION to 3


To generate a diff of this commit:
cvs rdiff -u -r1.2 -r1.3 src/sys/dev/filemon/filemon.c
cvs rdiff -u -r1.1 -r1.2 src/sys/dev/filemon/filemon.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/filemon/filemon.c
diff -u src/sys/dev/filemon/filemon.c:1.2 src/sys/dev/filemon/filemon.c:1.3
--- src/sys/dev/filemon/filemon.c:1.2	Fri May 13 22:31:08 2011
+++ src/sys/dev/filemon/filemon.c	Mon Jul  4 23:37:30 2011
@@ -24,7 +24,7 @@
  */
 
 #include sys/cdefs.h
-__KERNEL_RCSID(0, $NetBSD: filemon.c,v 1.2 2011/05/13 22:31:08 rmind Exp $);
+__KERNEL_RCSID(0, $NetBSD: filemon.c,v 1.3 2011/07/04 23:37:30 sjg Exp $);
 
 #include sys/param.h
 #include sys/kernel.h
@@ -126,8 +126,8 @@
 	int len;
 
 	len = snprintf(filemon-fm_msgbufr, sizeof(filemon-fm_msgbufr),
-	# filemon version 2\n# Target pid %d\nV 2\n,
-	curproc-p_pid);
+	# filemon version %d\n# Target pid %d\nV %d\n,
+		   FILEMON_VERSION, curproc-p_pid, FILEMON_VERSION);
 
 	filemon_output(filemon, filemon-fm_msgbufr, len);
 }

Index: src/sys/dev/filemon/filemon.h
diff -u src/sys/dev/filemon/filemon.h:1.1 src/sys/dev/filemon/filemon.h:1.2
--- src/sys/dev/filemon/filemon.h:1.1	Thu Sep  9 00:10:16 2010
+++ src/sys/dev/filemon/filemon.h	Mon Jul  4 23:37:30 2011
@@ -1,4 +1,4 @@
-/* $NetBSD: filemon.h,v 1.1 2010/09/09 00:10:16 sjg Exp $ */
+/* $NetBSD: filemon.h,v 1.2 2011/07/04 23:37:30 sjg Exp $ */
 /*
  * Copyright (c) 2010, Juniper Networks, Inc.
  *
@@ -31,6 +31,8 @@
 #define FILEMON_SET_FD		_IOWR('S', 1, int)
 #define FILEMON_SET_PID		_IOWR('S', 2, pid_t)
 
+#define FILEMON_VERSION		3
+
 #ifdef _KERNEL
 struct filemon {
 	pid_t fm_pid;		/* The process ID being monitored. */



CVS commit: src/sys/dev/filemon

2011-05-13 Thread Mindaugas Rasiukevicius
Module Name:src
Committed By:   rmind
Date:   Fri May 13 22:31:08 UTC 2011

Modified Files:
src/sys/dev/filemon: filemon.c

Log Message:
filemon_open: remove unnecessary check (which has a leak in error path).


To generate a diff of this commit:
cvs rdiff -u -r1.1 -r1.2 src/sys/dev/filemon/filemon.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/filemon/filemon.c
diff -u src/sys/dev/filemon/filemon.c:1.1 src/sys/dev/filemon/filemon.c:1.2
--- src/sys/dev/filemon/filemon.c:1.1	Thu Sep  9 00:10:16 2010
+++ src/sys/dev/filemon/filemon.c	Fri May 13 22:31:08 2011
@@ -24,7 +24,7 @@
  */
 
 #include sys/cdefs.h
-__KERNEL_RCSID(0, $NetBSD: filemon.c,v 1.1 2010/09/09 00:10:16 sjg Exp $);
+__KERNEL_RCSID(0, $NetBSD: filemon.c,v 1.2 2011/05/13 22:31:08 rmind Exp $);
 
 #include sys/param.h
 #include sys/kernel.h
@@ -195,20 +195,16 @@
 		return error;
 
 	filemon = kmem_alloc(sizeof(struct filemon), KM_SLEEP);
-	if (!filemon)
-		return ENOMEM;
-
 	rw_init(filemon-fm_mtx);
 	filemon-fm_fd = -1;
 	filemon-fm_fp = NULL;
 	filemon-fm_pid = curproc-p_pid;
 
 	rw_enter(filemon_mtx, RW_WRITER);
-	n_open++;
-
 	TAILQ_INSERT_TAIL(filemons_inuse, filemon, fm_link);
-
+	n_open++;
 	rw_exit(filemon_mtx);
+
 	return fd_clone(fp, fd, oflags, filemon_fileops, filemon);
 }
 



CVS commit: src/sys/dev/filemon

2011-03-13 Thread Simon J. Gerraty
Module Name:src
Committed By:   sjg
Date:   Sun Mar 13 21:26:31 UTC 2011

Modified Files:
src/sys/dev/filemon: filemon_wrapper.c

Log Message:
EJUSTRETURN is expected return from sys_execve()


To generate a diff of this commit:
cvs rdiff -u -r1.1 -r1.2 src/sys/dev/filemon/filemon_wrapper.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/filemon/filemon_wrapper.c
diff -u src/sys/dev/filemon/filemon_wrapper.c:1.1 src/sys/dev/filemon/filemon_wrapper.c:1.2
--- src/sys/dev/filemon/filemon_wrapper.c:1.1	Thu Sep  9 00:10:16 2010
+++ src/sys/dev/filemon/filemon_wrapper.c	Sun Mar 13 21:26:31 2011
@@ -24,7 +24,7 @@
  */
 
 #include sys/cdefs.h
-__KERNEL_RCSID(0, $NetBSD: filemon_wrapper.c,v 1.1 2010/09/09 00:10:16 sjg Exp $);
+__KERNEL_RCSID(0, $NetBSD: filemon_wrapper.c,v 1.2 2011/03/13 21:26:31 sjg Exp $);
 
 #include sys/param.h
 #include sys/types.h
@@ -82,7 +82,7 @@
 	
 	error = copyinstr(SCARG(uap, path), fname, sizeof(fname), done);
 
-	if ((ret = sys_execve(l, uap, retval)) == 0  error == 0) {
+	if ((ret = sys_execve(l, uap, retval)) == EJUSTRETURN  error == 0) {
 		filemon = filemon_lookup(curproc);
 
 		if (filemon) {