Commit Graph

65 Commits

Author SHA1 Message Date
Mateusz Guzik
cc426dd319 Remove unused argument to priv_check_cred.
Patch mostly generated with cocinnelle:

@@
expression E1,E2;
@@

- priv_check_cred(E1,E2,0)
+ priv_check_cred(E1,E2)

Sponsored by:	The FreeBSD Foundation
2018-12-11 19:32:16 +00:00
Brooks Davis
12e69f96a2 Add const to input-only char * arguments.
These arguments are mostly paths handled by NAMEI*() macros which already
take const char * arguments.

This change improves the match between syscalls.master and the public
declerations of system calls.

Reviewed by:	kib (prior version)
Obtained from:	CheriBSD
Sponsored by:	DARPA, AFRL
Differential Revision:	https://reviews.freebsd.org/D17812
2018-11-02 20:50:22 +00:00
Bryan Drewery
bc0d7285f9 Fix some filemon path logging issues.
- Properly handle snprintf return value for truncation and avoid
  overflowing the later write with the bogus length.
- Increase the msgbufr size to handle a rename of 2 full files.

The larger allocation causes a slight performance hit which will be mitigated
in the future.  A rewrite with sbufs will likely be done as well.

Reported by:	Ilja Van Sprundel <ivansprundel@ioactive.com>
MFC after:	2 weeks
Approved by:	so (gtetlow)
Reviewed by:	kib
Sponsored by:	Dell EMC
Differential Revision:	https://reviews.freebsd.org/D16098
2018-08-03 19:24:04 +00:00
Matt Macy
cbd92ce62e Eliminate the overhead of gratuitous repeated reinitialization of cap_rights
- Add macros to allow preinitialization of cap_rights_t.

- Convert most commonly used code paths to use preinitialized cap_rights_t.
  A 3.6% speedup in fstat was measured with this change.

Reported by:	mjg
Reviewed by:	oshogbo
Approved by:	sbruno
MFC after:	1 month
2018-05-09 18:47:24 +00:00
Brooks Davis
6469bdcdb6 Move most of the contents of opt_compat.h to opt_global.h.
opt_compat.h is mentioned in nearly 180 files. In-progress network
driver compabibility improvements may add over 100 more so this is
closer to "just about everywhere" than "only some files" per the
guidance in sys/conf/options.

Keep COMPAT_LINUX32 in opt_compat.h as it is confined to a subset of
sys/compat/linux/*.c.  A fake _COMPAT_LINUX option ensure opt_compat.h
is created on all architectures.

Move COMPAT_LINUXKPI to opt_dontuse.h as it is only used to control the
set of compiled files.

Reviewed by:	kib, cem, jhb, jtl
Sponsored by:	DARPA, AFRL
Differential Revision:	https://reviews.freebsd.org/D14941
2018-04-06 17:35:35 +00:00
Pedro F. Giffuni
718cf2ccb9 sys/dev: further adoption of SPDX licensing ID tags.
Mainly focus on files that use BSD 2-Clause license, however the tool I
was using misidentified many licenses so this was mostly a manual - error
prone - task.

The Software Package Data Exchange (SPDX) group provides a specification
to make it easier for automated tools to detect and summarize well known
opensource licenses. We are gradually adopting the specification, noting
that the tags are considered only advisory and do not, in any way,
superceed or replace the license texts.
2017-11-27 14:52:40 +00:00
Bryan Drewery
f566b25e0d Avoid taking PROC_LOCK in syscalls if not being traced.
MFC after:	1 week
Sponsored by:	EMC / Isilon Storage Division
2016-08-12 16:05:53 +00:00
Bryan Drewery
6c37a3d469 Cleanup COMPAT_FREEBSD32 support.
This is a NOP.

The COMPAT_IA32 was renamed in r205014 to COMPAT_FREEBSD32 and
COMPAT_ARCH32 does not seem to have existed.  Also remove some
leftovers from the sysent rework in r301404.  Include
freebsd32_util.h for the freebsd32_sysent prototype.

X-MFC-With:	r301404
Reported by:	kib
MFC after:	3 days
Sponsored by:	EMC / Isilon Storage Division
2016-06-05 18:16:33 +00:00
Bryan Drewery
f738a972ec Fix build after r301404.
X-MFC-With:	r301404
MFC after:	1 week
2016-06-04 23:34:51 +00:00
Bryan Drewery
66afa4152f Support all architectures by just using sysent.
PowerPC64 has two different ABIs, neither of which is elf64_freebsd_sysvec.
Using sysent and freebsd32_sysent achieves the same effect.

X-MFC-With:	r301130
MFC after:	1 week
Sponsored by:	EMC / Isilon Storage Division
2016-06-04 17:39:42 +00:00
Bryan Drewery
b7622235d0 Don't truncate existing error when writing the log.
Suggested by:	markj
MFC after:	2 weeks
Sponsored by:	EMC / Isilon Storage Division
2016-05-27 23:58:00 +00:00
Bryan Drewery
33094a87c4 Rename function to be less generic.
MFC after:	2 weeks
Sponsored by:	EMC / Isilon Storage Division
2016-05-27 23:57:57 +00:00
Bryan Drewery
7ae27ff963 Write to the log using the tracer's credentials.
MFC after:	2 weeks
Sponsored by:	EMC / Isilon Storage Division
2016-05-27 23:57:53 +00:00
Bryan Drewery
f14fbe72ec exec: Cease tracing if credentials will change with the new image.
This also prevents tracing to a P_INEXEC process since it could race
with other processes attaching to it in filemon_event_process_exec() due
to the filemon_get_proc() race of incrementing ref and then locking the
filemon.  With the no-P_INEXEC invariant in place the p_filemon may only
be the same or NULL when trying to drop it in
filemon_event_process_exec().

MFC after:	2 weeks
Sponsored by:	EMC / Isilon Storage Division
Differential Revision:	https://reviews.freebsd.org/D6545
2016-05-27 23:57:43 +00:00
Bryan Drewery
23ae5cd74e filemon exec: Use imgp->execpath rather than vn_fullpath(9).
This will be more accurate as the actual name is provided if ran
from an absolute path in do_execve().

MFC after:	2 weeks
Sponsored by:	EMC / Isilon Storage Division
2016-05-26 23:27:08 +00:00
Bryan Drewery
3733897532 Remove unneeded return left from refactoring.
MFC after:	2 weeks
Sponsored by:	EMC / Isilon Storage Division
2016-03-25 00:14:56 +00:00
Bryan Drewery
9b511ce96b Handle copyin failures.
Skip the log entry as there is nothing good to write out.  Don't fail
the syscall though since it already succeeded.  There's no reason
filemon's tracing failure should fail the already-succeeded syscall.

Record the error for later to return from close(2) on the filemon devfs
file descriptor.

Discussed with:	markj, sjg, kib (briefly with kib)
Reported by:	mjg
MFC after:	2 weeks
Sponsored by:	EMC / Isilon Storage Division
2016-03-22 22:41:14 +00:00
Bryan Drewery
eac5c9f26f Remove unused done argument to copyinstr(9).
MFC after:	2 weeks
Sponsored by:	EMC / Isilon Storage Division
2016-03-22 22:41:10 +00:00
Bryan Drewery
4177d9f7d3 Return any log write failure encountered when closing the filemon fd.
Discussed with:	sjg, markj
Reviewed by:	sjg
MFC after:	2 weeks
Sponsored by:	EMC / Isilon Storage Division
2016-03-22 22:41:07 +00:00
Bryan Drewery
4d9fbc5518 Follow-up r297156: Close the log in filemon_dtr rather than in the last reference.
If the tracer has decided to the close the log then it should be fully
written, not getting more entries, when close(2) returns.  This was
a regression in r297156 in that it allowed a traced process to continue
a traced syscall and add more entries to the log while the tracer had
already closed its fd or exited.  This was only really part of the
daemonized process case which is abnormal.

MFC after:	2 weeks
Sponsored by:	EMC / Isilon Storage Division
2016-03-22 22:41:03 +00:00
Bryan Drewery
d77f7e8c9d Consolidate common link(2) logic.
MFC after:	2 weeks
Sponsored by:	EMC / Isilon Storage Division
2016-03-21 23:22:19 +00:00
Bryan Drewery
8536d1b7e1 Attempt to use the namecache for openat(2) path resolution.
This finishes the work done in D2810.

MFC after:	2 weeks
Sponsored by:	EMC / Isilon Storage Division
2016-03-21 20:29:53 +00:00
Bryan Drewery
4f0d785142 Use curthread for vn_fullpath.
No functional change.

MFC after:	2 weeks
Sponsored by:	EMC / Isilon Storage Division
2016-03-21 20:29:46 +00:00
Bryan Drewery
4d3cfa1d9c Consolidate open(2) and openat(2) code.
MFC after:	2 weeks
Sponsored by:	EMC / Isilon Storage Division
2016-03-21 20:29:43 +00:00
Bryan Drewery
6d2077c2be Stop tracking stat(2).
None of lstat(2), fstat(2), fstatat(2) were tracked either.

The other filemon implementations also do not track stat(2), nor
does bmake utilize it.  The act of opening a file for read should
be enough to decide that a file is a dependency.  There could be
rare cases where just having a file would cause a dependency but it
is unlikely.

MFC after:	2 weeks
Also noted by:	sjg
Sponsored by:	EMC / Isilon Storage Division
2016-03-21 20:29:39 +00:00
Bryan Drewery
e0d84b9ee9 Track filemon usage via a proc.p_filemon pointer rather than its own lists.
- proc.p_filemon is added which is protected by PROC_LOCK.  This improves
  performance and avoids double-fork issues, taking allproc_lock
  while in syscalls, and walking the process tree in syscalls.  A
  particular proc.p_filemon can only be changed to NULL or another
  filemon, or the filemon inherited, while the filemon->lock is held.
- Filemon are reference counted.  On the last reference the log will be closed.
- When closing the devfs file handle, the filemon will be detached from all
  processes and inheritance prevented.
- Disallow attaching to a process already being traced since filemon is
  typically intended to be used on children only.  This is allowed for
  curproc as bmake relies on this behavior for rare cases when combining
  .MAKE with .META.
- Detach any previously tracked process on ioctl(FILEMON_SET_PID).
- Handle error from devfs_set_cdevpriv() in filemon_open().
- The global filemon lock and lists are removed.
- A free list is no longer kept.  Previously this list was
  forever-expanding and never garbage cleaned.
- No longer loses track of double-forks.  If the process holding the filemon
  handle closes it will close the log rather than wait on a daemonized process,
  but it will log all activity until it closes its handle.  The filemon
  will be removed from the process and not inherited.
- A separate process count is kept only as an optimization for
  forced detachment to avoid taking allproc_lock and walking the entire
  process tree.
- struct filemon access is protected by sx(9) filemon->lock as it was before.
- Add more comments and KASSERTS.

MFC after:	2 weeks
Reviewed by:	kib, mjg, markj (all on previous versions)
Sponsored by:	EMC / Isilon Storage Division
Differential Revision:	https://reviews.freebsd.org/D5520
2016-03-21 20:29:27 +00:00
Bryan Drewery
044fd54366 FILEMON_SET_FD: Disallow changing the fd.
MFC after:	1 week
Suggested by:	mjg
Sponsored by:	EMC / Isilon Storage Division
2016-03-09 19:50:35 +00:00
Bryan Drewery
1e35cdf678 Add missing break for r296472.
This was lost in git rebasing, though it has no functional change.

X-MFC-With:	r296472
MFC after:	1 week
2016-03-07 21:45:24 +00:00
Bryan Drewery
4039c53163 Require kldunload -f to unload.
Code may still be executing from the wrappers at unload time and thus is
not generally safe to unload.  Converting the wrappers to use
EVENTHANDLER(9) will allow this to safely drain on active threads in
hooks.  More work on EVENTHANDLER(9) is needed first.

MFC after:	1 week
Sponsored by:	EMC / Isilon Storage Division
2016-03-07 21:39:29 +00:00
Bryan Drewery
8a81693ad5 Only call bwillwrite() for logging to vnodes, as other fo_write() calls do.
MFC after:	1 week
Sponsored by:	EMC / Isilon Storage Division
2016-03-07 21:10:19 +00:00
Bryan Drewery
e0dae8f1c0 Remove filemon->lock wrappers.
MFC after:	1 week
Sponsored by:	EMC / Isilon Storage Division
2016-03-02 00:13:13 +00:00
Bryan Drewery
5b83ad4bb4 Remove old compatibility checks.
MFC after:	3 days
Sponsored by:	EMC / Isilon Storage Division
2016-02-27 21:08:27 +00:00
Bryan Drewery
a2109e0056 Filemon: Fix panic when fork1() is called from kproc_create().
MFC after:	2 weeks
Sponsored by:	EMC / Isilon Storage Division
2016-02-16 02:14:25 +00:00
Bryan Drewery
d5e53f99be filemon: Track the process pointer rather than a pid.
The process is not held since the process_exit hook is called after the
exithold.  There is no need to hold the process since the hook will
always see it exiting via the process_exit event.

MFC after:	2 weeks
Sponsored by:	EMC / Isilon Storage Division
2016-01-29 00:44:32 +00:00
Bryan Drewery
6137c5d990 filemon_open: Don't record a process to trace here.
Only ioctl(FILEMON_SET_PID) should be setting the process-to-be-traced.

MFC after:	2 weeks
Sponsored by:	EMC / Isilon Storage Division
2016-01-29 00:44:28 +00:00
Bryan Drewery
f889a61ae5 filemon: Use process_exec EVENTHANDLER to capture sys_execve.
MFC after:	2 weeks
Sponsored by:	EMC / Isilon Storage Division
2016-01-28 21:45:25 +00:00
Bryan Drewery
e95c55e50d Follow-up r294967: Mark flags unused.
X-MFC-With:	r294967
MFC after:	2 weeks
Sponsored by:	EMC / Isilon Storage Division
2016-01-28 01:19:19 +00:00
Bryan Drewery
2f6009620f filemon: Trace fork via process_fork event.
This avoids needing ugly hooks and needing both a vfork and fork
handler.

MFC after:	2 weeks
Sponsored by:	EMC / Isilon Storage Division
2016-01-28 01:17:55 +00:00
Bryan Drewery
79d699139b filemon: Use process_exit EVENTHANDLER to capture process exit.
This fixes some cases where a process could exit without being untracked
by filemon.

Reported by:	mjg
MFC after:	2 weeks
Sponsored by:	EMC / Isilon Storage Division
2016-01-28 00:51:17 +00:00
Bryan Drewery
50c99f32a9 filemon_dtr: Lock the associated filemon handle before writing to it.
Reported by:	mjg
MFC after:	2 weeks
Sponsored by:	EMC / Isilon Storage Division
2016-01-27 21:37:43 +00:00
Bryan Drewery
2b198fe92f filemon_comment has nothing to do with wrappers so move it out of filemon_wrapper.c.
It only prints the header from filemon_ioctl.  Keep the name though to stay
closer to other implementations.

MFC after:	2 weeks
Sponsored by:	EMC / Isilon Storage Division
2016-01-27 21:17:43 +00:00
Bryan Drewery
64c368a488 filemon_ioctl: Lock the associated filemon handle before writing to it.
Reported by:	mjg
MFC after:	2 weeks
Sponsored by:	EMC / Isilon Storage Division
2016-01-27 21:14:09 +00:00
Bryan Drewery
e8c87a09dc filemon_ioctl: Handle error from devfs_get_cdevpriv(9).
MFC after:	2 weeks
Sponsored by:	EMC / Isilon Storage Division
2016-01-27 21:12:18 +00:00
Bryan Drewery
fac4a7ac31 Drop any previous fd when setting a new one.
Reported by:	mjg
Sponsored by:	EMC / Isilon Storage Division
MFC after:	2 weeks
2016-01-27 19:11:11 +00:00
Bryan Drewery
b76e673f66 Remove unneeded mutex.h include, missed in r287155.
MFC after:	1 week
Sponsored by:	EMC / Isilon Storage Division
2015-11-04 22:49:34 +00:00
Bryan Drewery
243c115e92 Remove unneeded NULL as this is initialized with M_ZERO.
MFC after:	2 weeks
Sponsored by:	EMC / Isilon Storage Division
2015-10-29 23:56:34 +00:00
Bryan Drewery
8183f2e312 Fix filemon locking races.
Convert filemon_lock and struct filemon* lock to sx(9), rather than a
self-rolled reader-writer lock, and hold it for the entire time needed.

At least filemon_lock_write() was not checking for active readers when
it would successfully return with the write lock "held".  This led to
a race with reading entries from filemon_inuse as they were removed.  This
could be seen with QUEUE_MACRO_DEBUG enabled, causing -1 to be read as an
entry rather than a valid struct filemon*.

Fixing filemon_lock_write() to check readers was insufficient to fix the
races.

sx(9) was used as the lock could be held while taking proctree_lock and sleeping
in fo_write.

Sponsored by:	EMC / Isilon Storage Division
MFC after:	2 weeks
2015-08-26 03:44:48 +00:00
Bryan Drewery
8b64fa1e12 Avoid taking proctree_lock and searching parents in wrappers if not needed.
This should help the case where filemon is loaded but not in use.

Sponsored by:	EMC / Isilon Storage Division
MFC after:	2 weeks
2015-08-26 03:37:18 +00:00
Bryan Drewery
9fa45e4864 Remove unneeded inuse list locking in filemon_comment().
Sponsored by:	EMC / Isilon Storage Division
MFC after:	2 weeks
2015-08-26 03:33:34 +00:00
Bryan Drewery
1ab9f216d2 Move common locking for filemon_inuse and struct filemon* to filemon_pid_check().
This keeps the lock for the filemon_inuse list held only while reading
the list.

Sponsored by:	EMC / Isilon Storage Division
MFC after:	2 weeks
2015-08-26 03:32:47 +00:00