From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-dl1-f68.google.com (mail-dl1-f68.google.com [74.125.82.68]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4D36521A453 for ; Sun, 7 Jun 2026 00:34:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.82.68 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780792454; cv=none; b=Iegg58mn0hO5nzAw68uQczCSp5rYPp/gLZfJ6b7mUlK/DEtIUbGT2VYh2NulVILTu48qoFgYV7uL7bTeAe3op5dsx75fAIdptS6glfi7jVe1iChgT0KLNOjmK9ecHo8yXxFqD+z3uEUgctpN3Z665fMcPE3m1bQnNzC1DZ1j7Z0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780792454; c=relaxed/simple; bh=5gxaVxo3xM8fukkjl9cAMluFaofUCDDCFLEg6biKk6s=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=SHGuYnJRM1/9KehVVgX6KqzQ2FASBS9xJ7xh6Y9h33g3uaLiB9b+CFCurycXhUjKJNMlRI9Vp6iPKCZ3mMB+s3cejybwm8SKP1oLFLeakM9GXYwuB7Z2zsBc5TEk5x7y1hOEugoVEH+S9zo9+0arCJNzl3UGOxbi0Oyu8cpxqkE= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=iSm5KO3Z; arc=none smtp.client-ip=74.125.82.68 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="iSm5KO3Z" Received: by mail-dl1-f68.google.com with SMTP id a92af1059eb24-137d464c47eso2610929c88.1 for ; Sat, 06 Jun 2026 17:34:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1780792452; x=1781397252; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Yb52RRXQv7KcVlnarae14d7zbBmk629IfEhtSbYPJos=; b=iSm5KO3Z+/ADfxk3HB/dE/Uook5dNzj07Sulr5s16CGuUcsiIy3MOqYRkNEyFsjCTK QnF1EafbPn5w749hq/I9kUBsDRWlWELQazkp3gSN61bVH91GLTblFUX5+wQ2afrJcEoD aICL/6PqexjRUg/yAF6M2gLRM/+6TH7cCnLWnwOd07EH7cOMYa7roOibwDPhcm8Ye58c Mg0ddoHy7LP4QD4qeu8KO8Vu+J1rUkVrSV3ZXOvP3aqJYlxGlK3YJVw97FzRFS+bfXtj 1gplWkwxIl0ChkD59P2lvUvQlFns8avIMZHTWEN8XVNb6E6e2hzpOdgFWCFqIc7vYBkA yzqQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1780792452; x=1781397252; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=Yb52RRXQv7KcVlnarae14d7zbBmk629IfEhtSbYPJos=; b=VDAxN0xQa/k1/en2IPPVVh9ewbLl8QK0R9SOXYyWvfPOpFxxB7tYD3Qyd6f+LSYLSZ T/mY/TAAZ2MYrPhwNHd6F3g/iE0KKEr5i3hITZR8dOwjrmiKyQJ6q4QEZopcUD9PE4+a Jb4GoWpvrtI1U8Zs6TyovMjgaN6pgGcZ8Cwyf/kRFePrcmyO71I2Euk+rhLDGhk5OUOz E2jFUOvvL4NKQY8OM5Kg7ytwXJA43re4jnoeF5zDFANG+uGhxizx3nBSpVrzOSErJxaR bz3Mol9JzXLXfKngpY47UQtI/9N//jDlL3VO6gmt+4WH5QzVzdYSTyWbHimAXsCxHd0m 9xOA== X-Forwarded-Encrypted: i=1; AFNElJ9YQCxrngk3MI/SYXhSNzJAWx3RzhFisTG4dn/0Qf+4XxnnRviB45B55fX7wlGOzpqtVtHTISOs8LFiSnk=@vger.kernel.org X-Gm-Message-State: AOJu0YzqCQ+JL9qq6a0xXWkVK6l+cGejCmTfauVk4y1RXLEyno02jH1z Y4E6V1tP/4WLnATWmfpiXSmq8eb4W73CzuvofRjLbtD7q4vmSCpzIdL1 X-Gm-Gg: Acq92OFYg89UJJ2dYbMwuaGtnKzIJ7OgyAyHjzDgYIiug6xYpewDlaI0NeS/NKlkJKK 4zEYClo6l0oKHBk16rE4ZOrUkVyvpA2vMqxZrrVJLvUq1hjshWr5JYK/QPmF5txFEO5X7MP85sQ 65cA6uqk8ar4Av0GDe2r7ZfkH7WFSqRr9hU7jp7FcxW8P6/yN+46bUB5C0j1hSV0LlBba/RV8Xh xh5MY5qWEznCgCQ4AdhIK8ospqdsPeWdp4XVq8CAX4xRvcE6torkdTffsFZQ9CVSmKR59RFV237 G5/I01I3Wzx2XDavUNzuVMuDItIAbyfWKKPgHF5bjd2xu1kkB058SYI5D02NIrvGmWBA4Mi5p9A 22NBsOD7wW+ELK2SaA26AeriiwHkgBtBQkPaMG4mpjNKeqQipC/a1dYr4TNow2K/bVgs6OOg9V/ GpZE78zsNfo3O2yAiGkoiBF6vZuJs3Dd04BBPrg67y X-Received: by 2002:a05:7022:122:b0:12d:c9b6:bbe2 with SMTP id a92af1059eb24-13807d07321mr4311493c88.2.1780792452211; Sat, 06 Jun 2026 17:34:12 -0700 (PDT) Received: from anonyme ([2605:52c0:2:2f27:be24:11ff:fe89:6f0f]) by smtp.gmail.com with ESMTPSA id a92af1059eb24-137f716017esm13451383c88.15.2026.06.06.17.34.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 06 Jun 2026 17:34:11 -0700 (PDT) From: AnonymeMeow To: jack@suse.cz Cc: amir73il@gmail.com, brauner@kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, AnonymeMeow Subject: [PATCH v5 2/2] fanotify: allow reporting pidfds for reaped tasks Date: Sun, 7 Jun 2026 08:33:43 +0800 Message-ID: <20260607003343.425939-3-anonymemeow@gmail.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260607003343.425939-1-anonymemeow@gmail.com> References: <20260607003343.425939-1-anonymemeow@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 Link: https://lore.kernel.org/linux-fsdevel/20260528-schmuckvoll-heilen-garen-be77b4208671@brauner/ Signed-off-by: AnonymeMeow --- 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 #include #include +#include #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 + 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