From: AnonymeMeow <anonymemeow@gmail.com>
To: jack@suse.cz
Cc: amir73il@gmail.com, brauner@kernel.org,
linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org,
AnonymeMeow <anonymemeow@gmail.com>
Subject: [PATCH v5 2/2] fanotify: allow reporting pidfds for reaped tasks
Date: Sun, 7 Jun 2026 08:33:43 +0800 [thread overview]
Message-ID: <20260607003343.425939-3-anonymemeow@gmail.com> (raw)
In-Reply-To: <20260607003343.425939-1-anonymemeow@gmail.com>
Fanotify used to refuse to report pidfds for reaped tasks by applying a
pid_has_task() check before calling pidfd_prepare(). This prevented
userspace from obtaining information about the task.
Register the event pid with pidfs when creating the fanotify event if
pidfd reporting was requested, so pidfd_prepare() can later create a
pidfd for the reaped task.
Suggested-by: Christian Brauner <brauner@kernel.org>
Link: https://lore.kernel.org/linux-fsdevel/20260528-schmuckvoll-heilen-garen-be77b4208671@brauner/
Signed-off-by: AnonymeMeow <anonymemeow@gmail.com>
---
fs/notify/fanotify/fanotify.c | 17 +++++++++++------
fs/notify/fanotify/fanotify_user.c | 18 +++++-------------
fs/pidfs.c | 10 ++++++----
include/linux/pidfs.h | 18 +++++++++++++++++-
4 files changed, 39 insertions(+), 24 deletions(-)
diff --git a/fs/notify/fanotify/fanotify.c b/fs/notify/fanotify/fanotify.c
index 38290b9c07f7..ece9523e775b 100644
--- a/fs/notify/fanotify/fanotify.c
+++ b/fs/notify/fanotify/fanotify.c
@@ -14,6 +14,7 @@
#include <linux/sched/mm.h>
#include <linux/statfs.h>
#include <linux/stringhash.h>
+#include <linux/pidfs.h>
#include "fanotify.h"
@@ -842,6 +843,15 @@ static struct fanotify_event *fanotify_alloc_event(
/* Whoever is interested in the event, pays for the allocation. */
old_memcg = set_active_memcg(group->memcg);
+ if (FAN_GROUP_FLAG(group, FAN_REPORT_TID))
+ pid = task_pid(current);
+ else
+ pid = task_tgid(current);
+
+ if (FAN_GROUP_FLAG(group, FAN_REPORT_PIDFD) &&
+ pidfs_register_pid_gfp(pid, gfp))
+ goto out;
+
if (fanotify_is_perm_event(mask)) {
event = fanotify_alloc_perm_event(data, data_type, gfp);
} else if (fanotify_is_error_event(mask)) {
@@ -863,15 +873,10 @@ static struct fanotify_event *fanotify_alloc_event(
if (!event)
goto out;
- if (FAN_GROUP_FLAG(group, FAN_REPORT_TID))
- pid = get_pid(task_pid(current));
- else
- pid = get_pid(task_tgid(current));
-
/* Mix event info, FAN_ONDIR flag and pid into event merge key */
hash ^= hash_long((unsigned long)pid | ondir, FANOTIFY_EVENT_HASH_BITS);
fanotify_init_event(event, hash, mask);
- event->pid = pid;
+ event->pid = get_pid(pid);
out:
set_active_memcg(old_memcg);
diff --git a/fs/notify/fanotify/fanotify_user.c b/fs/notify/fanotify/fanotify_user.c
index ebdd48942029..b604e3da58ad 100644
--- a/fs/notify/fanotify/fanotify_user.c
+++ b/fs/notify/fanotify/fanotify_user.c
@@ -904,20 +904,12 @@ static ssize_t copy_event_to_user(struct fsnotify_group *group,
metadata.fd = fd >= 0 ? fd : FAN_NOFD;
if (pidfd_mode) {
- unsigned int tid_mode = FAN_GROUP_FLAG(group, FAN_REPORT_TID);
- enum pid_type pidtype = tid_mode ? PIDTYPE_PID : PIDTYPE_TGID;
- unsigned int pidfd_flags = tid_mode ? PIDFD_THREAD : 0;
+ unsigned int pidfd_flags = PIDFD_STALE;
- /*
- * The pid_has_task() check for an event->pid is performed
- * preemptively in an attempt to catch out cases where the event
- * listener reads events after the event generating task has
- * already terminated. Depending on flag FAN_REPORT_FD_ERROR,
- * report either -ESRCH or FAN_NOPIDFD to the event listener in
- * those cases with all other pidfd creation errors reported as
- * the error code itself or as FAN_EPIDFD.
- */
- if (metadata.pid && pid_has_task(event->pid, pidtype))
+ if (FAN_GROUP_FLAG(group, FAN_REPORT_TID))
+ pidfd_flags |= PIDFD_THREAD;
+
+ if (metadata.pid)
pidfd = pidfd_prepare(event->pid, pidfd_flags, &pidfd_file);
if (!FAN_GROUP_FLAG(group, FAN_REPORT_FD_ERROR) && pidfd < 0)
diff --git a/fs/pidfs.c b/fs/pidfs.c
index 1cce4f34a051..15efecf5cb07 100644
--- a/fs/pidfs.c
+++ b/fs/pidfs.c
@@ -991,14 +991,16 @@ static void pidfs_put_data(void *data)
}
/**
- * pidfs_register_pid - register a struct pid in pidfs
+ * pidfs_register_pid_gfp - register a struct pid in pidfs with custom GFP
+ * flags
* @pid: pid to pin
+ * @gfp: GFP flags for memory allocation
*
- * Register a struct pid in pidfs.
+ * Register a struct pid in pidfs with custom GFP flags.
*
* Return: On success zero, on error a negative error code is returned.
*/
-int pidfs_register_pid(struct pid *pid)
+int pidfs_register_pid_gfp(struct pid *pid, gfp_t gfp)
{
struct pidfs_attr *new_attr __free(kfree) = NULL;
struct pidfs_attr *attr;
@@ -1014,7 +1016,7 @@ int pidfs_register_pid(struct pid *pid)
if (attr)
return 0;
- new_attr = kmem_cache_zalloc(pidfs_attr_cachep, GFP_KERNEL);
+ new_attr = kmem_cache_zalloc(pidfs_attr_cachep, gfp);
if (!new_attr)
return -ENOMEM;
diff --git a/include/linux/pidfs.h b/include/linux/pidfs.h
index 416bdff4d6ce..0abf7da9ab23 100644
--- a/include/linux/pidfs.h
+++ b/include/linux/pidfs.h
@@ -2,6 +2,8 @@
#ifndef _LINUX_PID_FS_H
#define _LINUX_PID_FS_H
+#include <linux/gfp_types.h>
+
struct coredump_params;
struct file *pidfs_alloc_file(struct pid *pid, unsigned int flags);
@@ -14,7 +16,21 @@ void pidfs_exit(struct task_struct *tsk);
void pidfs_coredump(const struct coredump_params *cprm);
#endif
extern const struct dentry_operations pidfs_dentry_operations;
-int pidfs_register_pid(struct pid *pid);
+int pidfs_register_pid_gfp(struct pid *pid, gfp_t gfp);
+
+/**
+ * pidfs_register_pid - register a struct pid in pidfs
+ * @pid: pid to pin
+ *
+ * Register a struct pid in pidfs.
+ *
+ * Return: On success zero, on error a negative error code is returned.
+ */
+static inline int pidfs_register_pid(struct pid *pid)
+{
+ return pidfs_register_pid_gfp(pid, GFP_KERNEL);
+}
+
void pidfs_free_pid(struct pid *pid);
#endif /* _LINUX_PID_FS_H */
--
2.54.0
next prev parent reply other threads:[~2026-06-07 0:34 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-06-07 0:33 [PATCH v5 0/2] fanotify: lift pidfd reporting restrictions AnonymeMeow
2026-06-07 0:33 ` [PATCH v5 1/2] fanotify: report thread pidfds for FAN_REPORT_TID AnonymeMeow
2026-06-07 0:33 ` AnonymeMeow [this message]
2026-06-09 10:29 ` [PATCH v5 0/2] fanotify: lift pidfd reporting restrictions Jan Kara
2026-06-10 7:31 ` Christian Brauner
2026-06-10 9:14 ` Jan Kara
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=20260607003343.425939-3-anonymemeow@gmail.com \
--to=anonymemeow@gmail.com \
--cc=amir73il@gmail.com \
--cc=brauner@kernel.org \
--cc=jack@suse.cz \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.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
all inboxes | Powered by JetHome®