pidfs: fix ERR_PTR dereference in pidfd_info()
Subsystems (2)
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.
--- fs/pidfs.c | 3 +++ 1 file changed, 3 insertions(+)
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); +/* * Restrict information retrieval to tasks within the caller's pid * namespace hierarchy.
Signature · 1 line
2.20.1
Quoted 19 lines · Zhen Ni
pidfd_pid()may return anERR_PTR()when the file does not refer to a valid pidfs file. Currentlypidfd_info()callspid_in_current_pidns()directly on the returned value, which risks dereferencing anERR_PTR.Fix it by explicitly checking
IS_ERR(pid)and returningPTR_ERR(pid)before further use.Fixes 7477d7dce48a ("pidfs: allow to retrieve exit information")Signed-off-by Zhen Ni <[email protected]>--- fs/pidfs.c | 3 +++ 1 file changed, 3 insertions(+)fs/pidfs.c +0
-0
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?
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