From: Penglei Jiang <superman.xpt@gmail.com>
To: superman.xpt@gmail.com
Cc: adrian.ratiu@collabora.com, akpm@linux-foundation.org,
brauner@kernel.org, felix.moessbauer@siemens.com,
jlayton@kernel.org, linux-fsdevel@vger.kernel.org,
linux-kernel@vger.kernel.org, lorenzo.stoakes@oracle.com,
mjguzik@gmail.com,
syzbot+02e64be5307d72e9c309@syzkaller.appspotmail.com,
syzbot+f9238a0a31f9b5603fef@syzkaller.appspotmail.com,
tglx@linutronix.de, viro@zeniv.linux.org.uk, xu.xin16@zte.com.cn
Subject: [PATCH V5] proc: Fix the issue of proc_mem_open returning NULL
Date: Thu, 3 Apr 2025 23:33:57 -0700 [thread overview]
Message-ID: <20250404063357.78891-1-superman.xpt@gmail.com> (raw)
In-Reply-To: <20250331170647.36285-1-superman.xpt@gmail.com>
The function proc_mem_open() can return an errno, NULL, or mm_struct*.
If it fails to acquire mm, it returns NULL, but the caller does not
check for the case when the return value is NULL.
The following conditions lead to failure in acquiring mm:
- The task is a kernel thread (PF_KTHREAD)
- The task is exiting (PF_EXITING)
Changes:
- Add documentation comments for the return value of proc_mem_open().
- Add checks in the caller to return -ESRCH when proc_mem_open()
returns NULL.
Reported-by: syzbot+f9238a0a31f9b5603fef@syzkaller.appspotmail.com
Closes: https://lore.kernel.org/all/000000000000f52642060d4e3750@google.com
Signed-off-by: Penglei Jiang <superman.xpt@gmail.com>
---
V4 -> V5: Completed the changelog
V3 -> V4: Revised the comments
V2 -> V3: Added comments to explain the proc_mem_open() return value
V1 -> V2: Added the missing NULL check, dropped the proc_mem_open() modification
fs/proc/base.c | 12 +++++++++---
fs/proc/task_mmu.c | 12 ++++++------
fs/proc/task_nommu.c | 4 ++--
3 files changed, 17 insertions(+), 11 deletions(-)
diff --git a/fs/proc/base.c b/fs/proc/base.c
index b0d4e1908b22..85a3f5e253d4 100644
--- a/fs/proc/base.c
+++ b/fs/proc/base.c
@@ -827,7 +827,13 @@ static const struct file_operations proc_single_file_operations = {
.release = single_release,
};
-
+/*
+ * proc_mem_open() can return errno, NULL or mm_struct*.
+ *
+ * - Returns NULL if the task has no mm (PF_KTHREAD or PF_EXITING)
+ * - Returns mm_struct* on success
+ * - Returns error code on failure
+ */
struct mm_struct *proc_mem_open(struct inode *inode, unsigned int mode)
{
struct task_struct *task = get_proc_task(inode);
@@ -854,8 +860,8 @@ static int __mem_open(struct inode *inode, struct file *file, unsigned int mode)
{
struct mm_struct *mm = proc_mem_open(inode, mode);
- if (IS_ERR(mm))
- return PTR_ERR(mm);
+ if (IS_ERR_OR_NULL(mm))
+ return mm ? PTR_ERR(mm) : -ESRCH;
file->private_data = mm;
return 0;
diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c
index 994cde10e3f4..b9e3bd006346 100644
--- a/fs/proc/task_mmu.c
+++ b/fs/proc/task_mmu.c
@@ -212,8 +212,8 @@ static int proc_maps_open(struct inode *inode, struct file *file,
priv->inode = inode;
priv->mm = proc_mem_open(inode, PTRACE_MODE_READ);
- if (IS_ERR(priv->mm)) {
- int err = PTR_ERR(priv->mm);
+ if (IS_ERR_OR_NULL(priv->mm)) {
+ int err = priv->mm ? PTR_ERR(priv->mm) : -ESRCH;
seq_release_private(inode, file);
return err;
@@ -1325,8 +1325,8 @@ static int smaps_rollup_open(struct inode *inode, struct file *file)
priv->inode = inode;
priv->mm = proc_mem_open(inode, PTRACE_MODE_READ);
- if (IS_ERR(priv->mm)) {
- ret = PTR_ERR(priv->mm);
+ if (IS_ERR_OR_NULL(priv->mm)) {
+ ret = priv->mm ? PTR_ERR(priv->mm) : -ESRCH;
single_release(inode, file);
goto out_free;
@@ -2069,8 +2069,8 @@ static int pagemap_open(struct inode *inode, struct file *file)
struct mm_struct *mm;
mm = proc_mem_open(inode, PTRACE_MODE_READ);
- if (IS_ERR(mm))
- return PTR_ERR(mm);
+ if (IS_ERR_OR_NULL(mm))
+ return mm ? PTR_ERR(mm) : -ESRCH;
file->private_data = mm;
return 0;
}
diff --git a/fs/proc/task_nommu.c b/fs/proc/task_nommu.c
index bce674533000..59bfd61d653a 100644
--- a/fs/proc/task_nommu.c
+++ b/fs/proc/task_nommu.c
@@ -260,8 +260,8 @@ static int maps_open(struct inode *inode, struct file *file,
priv->inode = inode;
priv->mm = proc_mem_open(inode, PTRACE_MODE_READ);
- if (IS_ERR(priv->mm)) {
- int err = PTR_ERR(priv->mm);
+ if (IS_ERR_OR_NULL(priv->mm)) {
+ int err = priv->mm ? PTR_ERR(priv->mm) : -ESRCH;
seq_release_private(inode, file);
return err;
--
2.17.1
prev parent reply other threads:[~2025-04-04 6:35 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-03-24 16:23 [PATCH] proc: Fix the issue of proc_mem_open returning NULL Penglei Jiang
2025-03-24 17:48 ` Mateusz Guzik
2025-03-25 4:14 ` [PATCH V2] " Penglei Jiang
2025-03-27 19:24 ` Andrew Morton
2025-03-31 9:16 ` [PATCH V3] " Penglei Jiang
2025-03-31 17:06 ` [PATCH V4] " Penglei Jiang
2025-04-04 6:33 ` Penglei Jiang [this message]
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=20250404063357.78891-1-superman.xpt@gmail.com \
--to=superman.xpt@gmail.com \
--cc=adrian.ratiu@collabora.com \
--cc=akpm@linux-foundation.org \
--cc=brauner@kernel.org \
--cc=felix.moessbauer@siemens.com \
--cc=jlayton@kernel.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=lorenzo.stoakes@oracle.com \
--cc=mjguzik@gmail.com \
--cc=syzbot+02e64be5307d72e9c309@syzkaller.appspotmail.com \
--cc=syzbot+f9238a0a31f9b5603fef@syzkaller.appspotmail.com \
--cc=tglx@linutronix.de \
--cc=viro@zeniv.linux.org.uk \
--cc=xu.xin16@zte.com.cn \
/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).