Skip to contentSkip to conversation

pidfs: fix ERR_PTR dereference in pidfd_info()

PATCH In review Zhen Ni 2 participants 3 messages Oct 11, 2025 – Oct 13, 2025

Subsystems (2)
lore
Share & tools
lore

Zhen Ni

author
#0 lore raw

PATCH 1/1 pidfs: fix ERR_PTR dereference in pidfd_info()

pidfd_pid() may return an ERR_PTR() when the file does not refer to a valid pidfs file. Currently pidfd_info() calls pid_in_current_pidns() directly on the returned value, which risks dereferencing an ERR_PTR.

Fix it by explicitly checking IS_ERR(pid) and returning PTR_ERR(pid) before further use.

Fixes 7477d7dce48a ("pidfs: allow to retrieve exit information")
---
 fs/pidfs.c | 3 +++
 1 file changed, 3 insertions(+)
fs/pidfs.c +3 -0
index 0ef5b47d796a..16670648bb09 100644
@@ -314,6 +314,9 @@ static long pidfd_info(struct file *file, unsigned int cmd, unsigned long arg)
 	if (copy_from_user(&mask, &uinfo->mask, sizeof(mask)))
 		return -EFAULT;
 
+	if (IS_ERR(pid))
+		return PTR_ERR(pid);
+
 	/*
 	 * Restrict information retrieval to tasks within the caller's pid
 	 * namespace hierarchy.
Signature · 1 line
2.20.1
#1 lore raw
Quoted 19 lines · Zhen Ni

pidfd_pid() may return an ERR_PTR() when the file does not refer to a valid pidfs file. Currently pidfd_info() calls pid_in_current_pidns() directly on the returned value, which risks dereferencing an ERR_PTR.

Fix it by explicitly checking IS_ERR(pid) and returning PTR_ERR(pid) before further use.

Fixes 7477d7dce48a ("pidfs: allow to retrieve exit information")
---
 fs/pidfs.c | 3 +++
 1 file changed, 3 insertions(+)
fs/pidfs.c +0 -0
index 0ef5b47d796a..16670648bb09 100644

fs/pidfs.c static long pidfd_info(struct file *file, unsigned int cmd, unsigned long arg) · line 319

Quoted 6 lines
fs/pidfs.c +3 -0
@@ -314,6 +314,9 @@ static long pidfd_info(struct file *file, unsigned int cmd, unsigned long arg)
 	if (copy_from_user(&mask, &uinfo->mask, sizeof(mask)))
 		return -EFAULT;
 
+	if (IS_ERR(pid))
+		return PTR_ERR(pid);
+

Is that something you ran into or perhaps you are going off of reading the code?

The only way that I see to get here requires a file with pidfs_file_operations, so AFAICS this shouuld never trigger.

In the worst case perhaps this can WARN_ON?

author
#2 lore raw
Quoted 10 lines · Mateusz Guzik

Is that something you ran into or perhaps you are going off of reading the code?

The only way that I see to get here requires a file with pidfs_file_operations, so AFAICS this shouuld never trigger.

In the worst case perhaps this can WARN_ON?

Thanks for the review.

I did not hit this in real testing, but only inferred the potential issue from reading the code context. After taking another look at the call paths into pidfd_info(), I agree this is indeed an over-defensive check and not actually reachable.

I'll drop this patch. Thanks for the clarification.

Zhen