From: "Alex Bennée" <alex.bennee@linaro.org>
To: qemu-devel@nongnu.org, Yeqi Fu <fufuyqqqqqq@gmail.com>
Cc: "Laurent Vivier" <laurent@vivier.eu>,
"Alex Bennée" <alex.bennee@linaro.org>
Subject: [RFC PATCH 1/2] linux-user: implement some basic FD<->path tracking
Date: Fri, 11 Aug 2023 17:28:29 +0100 [thread overview]
Message-ID: <20230811162830.2278032-2-alex.bennee@linaro.org> (raw)
In-Reply-To: <20230811162830.2278032-1-alex.bennee@linaro.org>
This will be useful in later patches for tracking the paths associated
with mmap operations. This will be useful to the upcoming -dfilter
changes to track execution only certain libraries.
Signed-off-by: Alex Bennée <alex.bennee@linaro.org>
---
linux-user/syscall.c | 59 ++++++++++++++++++++++++++++++++++++++++----
1 file changed, 54 insertions(+), 5 deletions(-)
diff --git a/linux-user/syscall.c b/linux-user/syscall.c
index 9353268cc1..e191163c49 100644
--- a/linux-user/syscall.c
+++ b/linux-user/syscall.c
@@ -8557,6 +8557,58 @@ static int open_hardware(CPUArchState *cpu_env, int fd)
}
#endif
+/*
+ * Handle non-intercepted guest open operations. This gives us the
+ * opportunity to track some information
+ */
+
+static QemuMutex fd_tracking_lock;
+static GHashTable *fd_path;
+
+__attribute__((constructor))
+static void fd_tracking_init(void)
+{
+ qemu_mutex_init(&fd_tracking_lock);
+}
+
+static int do_plain_guest_openat(int dirfd, const char *pathname,
+ int flags, mode_t mode, bool safe)
+{
+ const char * real_path = path(pathname);
+ int fd;
+
+ if (safe) {
+ fd = safe_openat(dirfd, real_path, flags, mode);
+ } else {
+ fd = openat(dirfd, real_path, flags, mode);
+ }
+
+ /* If we opened an fd save some details */
+ if (fd >= 0) {
+ WITH_QEMU_LOCK_GUARD(&fd_tracking_lock) {
+ if (!fd_path) {
+ fd_path = g_hash_table_new(NULL, NULL);
+ }
+
+ if (!g_hash_table_insert(fd_path, GINT_TO_POINTER(fd), g_strdup(real_path))) {
+ fprintf(stderr, "%s: duplicate fd %d in fd_path hash\n", __func__, fd);
+ }
+ }
+ }
+
+ return fd;
+}
+
+static void fd_path_cleanup(int fd) {
+ WITH_QEMU_LOCK_GUARD(&fd_tracking_lock) {
+ /*
+ * Assume success, if we failed to cleanup its totally
+ * possible the guest got confused and closed something twice.
+ */
+ g_hash_table_remove(fd_path, GINT_TO_POINTER(fd));
+ }
+}
+
int do_guest_openat(CPUArchState *cpu_env, int dirfd, const char *fname,
int flags, mode_t mode, bool safe)
@@ -8643,11 +8695,7 @@ int do_guest_openat(CPUArchState *cpu_env, int dirfd, const char *fname,
return fd;
}
- if (safe) {
- return safe_openat(dirfd, path(pathname), flags, mode);
- } else {
- return openat(dirfd, path(pathname), flags, mode);
- }
+ return do_plain_guest_openat(dirfd, pathname, flags, mode, safe);
}
ssize_t do_guest_readlink(const char *pathname, char *buf, size_t bufsiz)
@@ -9355,6 +9403,7 @@ static abi_long do_syscall1(CPUArchState *cpu_env, int num, abi_long arg1,
return get_errno(pidfd_getfd(arg1, arg2, arg3));
#endif
case TARGET_NR_close:
+ fd_path_cleanup(arg1);
fd_trans_unregister(arg1);
return get_errno(close(arg1));
#if defined(__NR_close_range) && defined(TARGET_NR_close_range)
--
2.39.2
next prev parent reply other threads:[~2023-08-11 16:29 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-08-11 16:28 [RFC PATCH 0/2] linux-user: extend -dfilter to accept paths Alex Bennée
2023-08-11 16:28 ` Alex Bennée [this message]
2023-08-11 16:28 ` [RFC PATCH 2/2] linux-user: implement name lookup for dfilter Alex Bennée
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230811162830.2278032-2-alex.bennee@linaro.org \
--to=alex.bennee@linaro.org \
--cc=fufuyqqqqqq@gmail.com \
--cc=laurent@vivier.eu \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).