Page Menu
Home
FreeBSD
Search
Configure Global Search
Log In
Files
F178179719
D62267.1796208179.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Authored By
Unknown
Size
17 KB
Referenced Files
None
Subscribers
None
D62267.1796208179.diff
View Options
diff --git a/share/man/man4/filemon.4 b/share/man/man4/filemon.4
--- a/share/man/man4/filemon.4
+++ b/share/man/man4/filemon.4
@@ -29,7 +29,7 @@
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
-.Dd April 19, 2025
+.Dd November 20, 2026
.Dt FILEMON 4
.Os
.Sh NAME
@@ -53,6 +53,16 @@
is not intended to be a security auditing tool.
Many system calls are not tracked and binaries using a non-native ABI may not
be fully audited.
+File operations of
+.Xr linux 4
+binaries are only recorded when the
+.Pa filemon_linux
+(and, for 32-bit binaries on 64-bit kernels,
+.Pa filemon_linux32 )
+module is loaded.
+Their paths are recorded as passed by the binary, without the
+.Xr linux 4
+emulation directory prefix.
It is intended for auditing of processes for the purpose of determining their
dependencies using an efficient and easily parsable format.
An example of this is
@@ -241,7 +251,8 @@
.Xr ktrace 1 ,
.Xr script 1 ,
.Xr truss 1 ,
-.Xr ioctl 2
+.Xr ioctl 2 ,
+.Xr linux 4
.Sh HISTORY
A
.Nm
diff --git a/sys/dev/filemon/filemon.h b/sys/dev/filemon/filemon.h
--- a/sys/dev/filemon/filemon.h
+++ b/sys/dev/filemon/filemon.h
@@ -32,3 +32,17 @@
#define FILEMON_VERSION 5 /* output format
(bump when adding record types) */
+
+#ifdef _KERNEL
+/*
+ * Record a successful file operation for the current process, if it is
+ * being traced. Paths are user-space addresses. These allow syscall
+ * tables other than the native ones (e.g., the Linuxulator) to be traced.
+ */
+struct thread;
+void filemon_log_chdir(struct thread *, const char *);
+void filemon_log_link(struct thread *, const char *, const char *);
+void filemon_log_open(struct thread *, const char *, int, int);
+void filemon_log_rename(struct thread *, const char *, const char *);
+void filemon_log_unlink(struct thread *, const char *);
+#endif
diff --git a/sys/dev/filemon/filemon_linux.c b/sys/dev/filemon/filemon_linux.c
new file mode 100644
--- /dev/null
+++ b/sys/dev/filemon/filemon_linux.c
@@ -0,0 +1,372 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2026, David E. O'Brien.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+/*
+ * filemon(4) support for Linuxulator processes.
+ *
+ * filemon(4) only wraps the native (and freebsd32) syscall tables, so a
+ * Linux binary run under a traced process (e.g., a Linux cross toolchain
+ * run by bmake in meta mode) produced only E, F and X records. This
+ * module wraps the file-related entries of the Linux syscall table and
+ * hands successful operations to filemon(4) for logging.
+ */
+
+#include <sys/param.h>
+#include <sys/systm.h>
+#include <sys/fcntl.h>
+#include <sys/kernel.h>
+#include <sys/module.h>
+#include <sys/proc.h>
+#include <sys/sysent.h>
+#include <machine/atomic.h>
+
+#ifdef FILEMON_LINUX32
+#if defined(__amd64__)
+#include <amd64/linux32/linux.h>
+#include <amd64/linux32/linux32_proto.h>
+#include <amd64/linux32/linux32_syscall.h>
+#else
+#error Only amd64 is supported.
+#endif
+#define MODNAME filemon_linux32
+#define LINUXMOD linux
+#define LSYS(name) LINUX32_SYS_linux_##name
+#define LMAXSYSCALL LINUX32_SYS_MAXSYSCALL
+#define LSYSENT linux32_sysent
+#else
+#if defined(__amd64__)
+#include <amd64/linux/linux.h>
+#include <amd64/linux/linux_proto.h>
+#include <amd64/linux/linux_syscall.h>
+#define LINUXMOD linux64
+#elif defined(__aarch64__)
+#include <arm64/linux/linux.h>
+#include <arm64/linux/linux_proto.h>
+#include <arm64/linux/linux_syscall.h>
+#define LINUXMOD linux64elf
+#elif defined(__i386__)
+#include <i386/linux/linux.h>
+#include <i386/linux/linux_proto.h>
+#include <i386/linux/linux_syscall.h>
+#define LINUXMOD linux
+#else
+#error Only aarch64, amd64 and i386 are supported.
+#endif
+#define MODNAME filemon_linux
+#define LSYS(name) LINUX_SYS_linux_##name
+#define LMAXSYSCALL LINUX_SYS_MAXSYSCALL
+#define LSYSENT linux_sysent
+#endif
+
+#include <compat/linux/linux_file.h>
+
+#include <dev/filemon/filemon.h>
+
+/* The arm64 Linux ABI only has the *at() forms of these syscalls. */
+#ifndef __aarch64__
+#define FILEMON_LINUX_LEGACY
+#endif
+
+extern struct sysent LSYSENT[];
+
+static sy_call_t *filemon_linux_orig[LMAXSYSCALL];
+
+#define ORIG(name, td, uap) (filemon_linux_orig[LSYS(name)]((td), (uap)))
+
+static int
+filemon_linux_fd(int dfd)
+{
+
+ return (dfd == LINUX_AT_FDCWD ? AT_FDCWD : dfd);
+}
+
+static int
+filemon_linux_oflags(int lflags)
+{
+
+ switch (lflags & LINUX_O_ACCMODE) {
+ case LINUX_O_WRONLY:
+ return (O_WRONLY);
+ case LINUX_O_RDWR:
+ return (O_RDWR);
+ default:
+ return (O_RDONLY);
+ }
+}
+
+static int
+filemon_linux_chdir(struct thread *td, void *arg)
+{
+ struct linux_chdir_args *uap = arg;
+ int ret;
+
+ if ((ret = ORIG(chdir, td, uap)) == 0)
+ filemon_log_chdir(td, uap->path);
+
+ return (ret);
+}
+
+static int
+filemon_linux_openat(struct thread *td, void *arg)
+{
+ struct linux_openat_args *uap = arg;
+ int ret;
+
+ if ((ret = ORIG(openat, td, uap)) == 0)
+ filemon_log_open(td, uap->filename,
+ filemon_linux_oflags(uap->flags),
+ filemon_linux_fd(uap->dfd));
+
+ return (ret);
+}
+
+static int
+filemon_linux_renameat(struct thread *td, void *arg)
+{
+ struct linux_renameat_args *uap = arg;
+ int ret;
+
+ if ((ret = ORIG(renameat, td, uap)) == 0)
+ filemon_log_rename(td, uap->oldname, uap->newname);
+
+ return (ret);
+}
+
+static int
+filemon_linux_renameat2(struct thread *td, void *arg)
+{
+ struct linux_renameat2_args *uap = arg;
+ int ret;
+
+ if ((ret = ORIG(renameat2, td, uap)) == 0)
+ filemon_log_rename(td, uap->oldname, uap->newname);
+
+ return (ret);
+}
+
+static int
+filemon_linux_unlinkat(struct thread *td, void *arg)
+{
+ struct linux_unlinkat_args *uap = arg;
+ int ret;
+
+ /* Like filemon(4), do not log rmdir. */
+ if ((ret = ORIG(unlinkat, td, uap)) == 0 &&
+ (uap->flag & LINUX_AT_REMOVEDIR) == 0)
+ filemon_log_unlink(td, uap->pathname);
+
+ return (ret);
+}
+
+static int
+filemon_linux_linkat(struct thread *td, void *arg)
+{
+ struct linux_linkat_args *uap = arg;
+ int ret;
+
+ if ((ret = ORIG(linkat, td, uap)) == 0)
+ filemon_log_link(td, uap->oldname, uap->newname);
+
+ return (ret);
+}
+
+static int
+filemon_linux_symlinkat(struct thread *td, void *arg)
+{
+ struct linux_symlinkat_args *uap = arg;
+ int ret;
+
+ if ((ret = ORIG(symlinkat, td, uap)) == 0)
+ filemon_log_link(td, uap->oldname, uap->newname);
+
+ return (ret);
+}
+
+#ifdef FILEMON_LINUX_LEGACY
+static int
+filemon_linux_open(struct thread *td, void *arg)
+{
+ struct linux_open_args *uap = arg;
+ int ret;
+
+ if ((ret = ORIG(open, td, uap)) == 0)
+ filemon_log_open(td, uap->path,
+ filemon_linux_oflags(uap->flags), AT_FDCWD);
+
+ return (ret);
+}
+
+static int
+filemon_linux_creat(struct thread *td, void *arg)
+{
+ struct linux_creat_args *uap = arg;
+ int ret;
+
+ if ((ret = ORIG(creat, td, uap)) == 0)
+ filemon_log_open(td, uap->path, O_WRONLY, AT_FDCWD);
+
+ return (ret);
+}
+
+static int
+filemon_linux_rename(struct thread *td, void *arg)
+{
+ struct linux_rename_args *uap = arg;
+ int ret;
+
+ if ((ret = ORIG(rename, td, uap)) == 0)
+ filemon_log_rename(td, uap->from, uap->to);
+
+ return (ret);
+}
+
+static int
+filemon_linux_unlink(struct thread *td, void *arg)
+{
+ struct linux_unlink_args *uap = arg;
+ int ret;
+
+ if ((ret = ORIG(unlink, td, uap)) == 0)
+ filemon_log_unlink(td, uap->path);
+
+ return (ret);
+}
+
+static int
+filemon_linux_link(struct thread *td, void *arg)
+{
+ struct linux_link_args *uap = arg;
+ int ret;
+
+ if ((ret = ORIG(link, td, uap)) == 0)
+ filemon_log_link(td, uap->path, uap->to);
+
+ return (ret);
+}
+
+static int
+filemon_linux_symlink(struct thread *td, void *arg)
+{
+ struct linux_symlink_args *uap = arg;
+ int ret;
+
+ if ((ret = ORIG(symlink, td, uap)) == 0)
+ filemon_log_link(td, uap->path, uap->to);
+
+ return (ret);
+}
+#endif /* FILEMON_LINUX_LEGACY */
+
+static const struct {
+ int num;
+ sy_call_t *hook;
+} filemon_linux_hooks[] = {
+ { LSYS(chdir), filemon_linux_chdir },
+ { LSYS(openat), filemon_linux_openat },
+ { LSYS(renameat), filemon_linux_renameat },
+ { LSYS(renameat2), filemon_linux_renameat2 },
+ { LSYS(unlinkat), filemon_linux_unlinkat },
+ { LSYS(linkat), filemon_linux_linkat },
+ { LSYS(symlinkat), filemon_linux_symlinkat },
+#ifdef FILEMON_LINUX_LEGACY
+ { LSYS(open), filemon_linux_open },
+ { LSYS(creat), filemon_linux_creat },
+ { LSYS(rename), filemon_linux_rename },
+ { LSYS(unlink), filemon_linux_unlink },
+ { LSYS(link), filemon_linux_link },
+ { LSYS(symlink), filemon_linux_symlink },
+#endif
+};
+
+static void
+filemon_linux_install(void)
+{
+ u_int i;
+
+ for (i = 0; i < nitems(filemon_linux_hooks); i++)
+ filemon_linux_orig[filemon_linux_hooks[i].num] =
+ LSYSENT[filemon_linux_hooks[i].num].sy_call;
+ /* The originals must be visible before any wrapper can run. */
+ atomic_thread_fence_rel();
+ for (i = 0; i < nitems(filemon_linux_hooks); i++)
+ LSYSENT[filemon_linux_hooks[i].num].sy_call =
+ filemon_linux_hooks[i].hook;
+}
+
+static void
+filemon_linux_deinstall(void)
+{
+ u_int i;
+
+ for (i = 0; i < nitems(filemon_linux_hooks); i++)
+ LSYSENT[filemon_linux_hooks[i].num].sy_call =
+ filemon_linux_orig[filemon_linux_hooks[i].num];
+}
+
+static int
+filemon_linux_modevent(module_t mod __unused, int type, void *data __unused)
+{
+ int error = 0;
+
+ switch (type) {
+ case MOD_LOAD:
+ filemon_linux_install();
+ break;
+
+ case MOD_UNLOAD:
+ filemon_linux_deinstall();
+ break;
+
+ case MOD_QUIESCE:
+ /*
+ * As with filemon(4), a thread may still be running in a
+ * wrapper. Require forcing an unload.
+ */
+ error = EBUSY;
+ break;
+
+ case MOD_SHUTDOWN:
+ break;
+
+ default:
+ error = EOPNOTSUPP;
+ break;
+ }
+
+ return (error);
+}
+
+static moduledata_t filemon_linux_mod = {
+ __XSTRING(MODNAME),
+ filemon_linux_modevent,
+ NULL
+};
+
+DECLARE_MODULE(MODNAME, filemon_linux_mod, SI_SUB_EXEC, SI_ORDER_ANY);
+MODULE_VERSION(MODNAME, 1);
+MODULE_DEPEND(MODNAME, filemon, 1, 1, 1);
+MODULE_DEPEND(MODNAME, LINUXMOD, 1, 1, 1);
diff --git a/sys/dev/filemon/filemon_wrapper.c b/sys/dev/filemon/filemon_wrapper.c
--- a/sys/dev/filemon/filemon_wrapper.c
+++ b/sys/dev/filemon/filemon_wrapper.c
@@ -88,26 +88,33 @@
filemon_output(filemon, filemon->msgbufr, len);
}
-static int
-filemon_wrapper_chdir(struct thread *td, struct chdir_args *uap)
+void
+filemon_log_chdir(struct thread *td __unused, const char *upath)
{
- int error, ret;
+ int error;
struct filemon *filemon;
- if ((ret = sys_chdir(td, uap)) == 0) {
- if ((filemon = filemon_proc_get(curproc)) != NULL) {
- if ((error = copyinstr(uap->path, filemon->fname1,
- sizeof(filemon->fname1), NULL)) != 0) {
- filemon->error = error;
- goto copyfail;
- }
+ if ((filemon = filemon_proc_get(curproc)) != NULL) {
+ if ((error = copyinstr(upath, filemon->fname1,
+ sizeof(filemon->fname1), NULL)) != 0) {
+ filemon->error = error;
+ goto copyfail;
+ }
- filemon_output_event(filemon, "C %d %s\n",
- curproc->p_pid, filemon->fname1);
+ filemon_output_event(filemon, "C %d %s\n",
+ curproc->p_pid, filemon->fname1);
copyfail:
- filemon_drop(filemon);
- }
+ filemon_drop(filemon);
}
+}
+
+static int
+filemon_wrapper_chdir(struct thread *td, struct chdir_args *uap)
+{
+ int ret;
+
+ if ((ret = sys_chdir(td, uap)) == 0)
+ filemon_log_chdir(td, uap->path);
return (ret);
}
@@ -144,9 +151,8 @@
}
}
-static void
-_filemon_wrapper_openat(struct thread *td, const char *upath, int flags,
- int fd)
+void
+filemon_log_open(struct thread *td, const char *upath, int flags, int fd)
{
int error;
struct file *fp;
@@ -215,7 +221,7 @@
int ret;
if ((ret = sys_open(td, uap)) == 0)
- _filemon_wrapper_openat(td, uap->path, uap->flags, AT_FDCWD);
+ filemon_log_open(td, uap->path, uap->flags, AT_FDCWD);
return (ret);
}
@@ -226,39 +232,47 @@
int ret;
if ((ret = sys_openat(td, uap)) == 0)
- _filemon_wrapper_openat(td, uap->path, uap->flag, uap->fd);
+ filemon_log_open(td, uap->path, uap->flag, uap->fd);
return (ret);
}
-static int
-filemon_wrapper_rename(struct thread *td, struct rename_args *uap)
+void
+filemon_log_rename(struct thread *td __unused, const char *ufrom,
+ const char *uto)
{
- int error, ret;
+ int error;
struct filemon *filemon;
- if ((ret = sys_rename(td, uap)) == 0) {
- if ((filemon = filemon_proc_get(curproc)) != NULL) {
- if (((error = copyinstr(uap->from, filemon->fname1,
- sizeof(filemon->fname1), NULL)) != 0) ||
- ((error = copyinstr(uap->to, filemon->fname2,
- sizeof(filemon->fname2), NULL)) != 0)) {
- filemon->error = error;
- goto copyfail;
- }
+ if ((filemon = filemon_proc_get(curproc)) != NULL) {
+ if (((error = copyinstr(ufrom, filemon->fname1,
+ sizeof(filemon->fname1), NULL)) != 0) ||
+ ((error = copyinstr(uto, filemon->fname2,
+ sizeof(filemon->fname2), NULL)) != 0)) {
+ filemon->error = error;
+ goto copyfail;
+ }
- filemon_output_event(filemon, "M %d '%s' '%s'\n",
- curproc->p_pid, filemon->fname1, filemon->fname2);
+ filemon_output_event(filemon, "M %d '%s' '%s'\n",
+ curproc->p_pid, filemon->fname1, filemon->fname2);
copyfail:
- filemon_drop(filemon);
- }
+ filemon_drop(filemon);
}
+}
+
+static int
+filemon_wrapper_rename(struct thread *td, struct rename_args *uap)
+{
+ int ret;
+
+ if ((ret = sys_rename(td, uap)) == 0)
+ filemon_log_rename(td, uap->from, uap->to);
return (ret);
}
-static void
-_filemon_wrapper_link(struct thread *td, const char *upath1,
+void
+filemon_log_link(struct thread *td __unused, const char *upath1,
const char *upath2)
{
struct filemon *filemon;
@@ -286,7 +300,7 @@
int ret;
if ((ret = sys_link(td, uap)) == 0)
- _filemon_wrapper_link(td, uap->path, uap->link);
+ filemon_log_link(td, uap->path, uap->link);
return (ret);
}
@@ -297,7 +311,7 @@
int ret;
if ((ret = sys_symlink(td, uap)) == 0)
- _filemon_wrapper_link(td, uap->path, uap->link);
+ filemon_log_link(td, uap->path, uap->link);
return (ret);
}
@@ -308,7 +322,7 @@
int ret;
if ((ret = sys_linkat(td, uap)) == 0)
- _filemon_wrapper_link(td, uap->path1, uap->path2);
+ filemon_log_link(td, uap->path1, uap->path2);
return (ret);
}
@@ -337,26 +351,33 @@
}
}
-static int
-filemon_wrapper_unlink(struct thread *td, struct unlink_args *uap)
+void
+filemon_log_unlink(struct thread *td __unused, const char *upath)
{
- int error, ret;
+ int error;
struct filemon *filemon;
- if ((ret = sys_unlink(td, uap)) == 0) {
- if ((filemon = filemon_proc_get(curproc)) != NULL) {
- if ((error = copyinstr(uap->path, filemon->fname1,
- sizeof(filemon->fname1), NULL)) != 0) {
- filemon->error = error;
- goto copyfail;
- }
+ if ((filemon = filemon_proc_get(curproc)) != NULL) {
+ if ((error = copyinstr(upath, filemon->fname1,
+ sizeof(filemon->fname1), NULL)) != 0) {
+ filemon->error = error;
+ goto copyfail;
+ }
- filemon_output_event(filemon, "D %d %s\n",
- curproc->p_pid, filemon->fname1);
+ filemon_output_event(filemon, "D %d %s\n",
+ curproc->p_pid, filemon->fname1);
copyfail:
- filemon_drop(filemon);
- }
+ filemon_drop(filemon);
}
+}
+
+static int
+filemon_wrapper_unlink(struct thread *td, struct unlink_args *uap)
+{
+ int ret;
+
+ if ((ret = sys_unlink(td, uap)) == 0)
+ filemon_log_unlink(td, uap->path);
return (ret);
}
diff --git a/sys/modules/Makefile b/sys/modules/Makefile
--- a/sys/modules/Makefile
+++ b/sys/modules/Makefile
@@ -555,6 +555,10 @@
.endif
.if ${MACHINE_CPUARCH} == "amd64" || ${MACHINE_CPUARCH} == "i386"
SUBDIR+= linux
+SUBDIR+= filemon_linux
+.endif
+.if ${MACHINE_CPUARCH} == "amd64"
+SUBDIR+= filemon_linux32
.endif
.if ${MACHINE_CPUARCH} == "aarch64" || ${MACHINE_CPUARCH} == "amd64" || ${MACHINE_ARCH:Mpowerpc64*}
SUBDIR+= linux64
diff --git a/sys/modules/filemon/Makefile b/sys/modules/filemon/Makefile
--- a/sys/modules/filemon/Makefile
+++ b/sys/modules/filemon/Makefile
@@ -4,4 +4,10 @@
SRCS= ${KMOD}.c
SRCS+= vnode_if.h
+EXPORT_SYMS= filemon_log_chdir \
+ filemon_log_link \
+ filemon_log_open \
+ filemon_log_rename \
+ filemon_log_unlink
+
.include <bsd.kmod.mk>
diff --git a/sys/modules/filemon/Makefile b/sys/modules/filemon_linux/Makefile
copy from sys/modules/filemon/Makefile
copy to sys/modules/filemon_linux/Makefile
--- a/sys/modules/filemon/Makefile
+++ b/sys/modules/filemon_linux/Makefile
@@ -1,7 +1,6 @@
.PATH: ${SRCTOP}/sys/dev/filemon
-KMOD= filemon
+KMOD= filemon_linux
SRCS= ${KMOD}.c
-SRCS+= vnode_if.h
.include <bsd.kmod.mk>
diff --git a/sys/modules/filemon_linux32/Makefile b/sys/modules/filemon_linux32/Makefile
new file mode 100644
--- /dev/null
+++ b/sys/modules/filemon_linux32/Makefile
@@ -0,0 +1,8 @@
+.PATH: ${SRCTOP}/sys/dev/filemon
+
+KMOD= filemon_linux32
+SRCS= filemon_linux.c
+
+CFLAGS+= -DFILEMON_LINUX32
+
+.include <bsd.kmod.mk>
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Wed, Dec 2, 10:42 AM (21 h, 5 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
42140986
Default Alt Text
D62267.1796208179.diff (17 KB)
Attached To
Mode
D62267: filemon: record file operations of Linuxulator processes
Attached
Detach File
Event Timeline