proc: Fix the issue of proc_mem_open returning NULL
Subsystems (3)
PATCH 1/1 proc: Fix the issue of proc_mem_open returning NULL
The following functions call proc_mem_open but do not handle the case where it returns NULL:
__mem_open in fs/proc/base.c
proc_maps_open in fs/proc/task_mmu.c
smaps_rollup_open in fs/proc/task_mmu.c
pagemap_open in fs/proc/task_mmu.c
maps_open in fs/proc/task_nommu.c
The following reported bugs may be related to this issue:
https://lore.kernel.org/all/[email protected]
https://lore.kernel.org/all/[email protected]
Fix:
Modify proc_mem_open to return an error code in case of errors, instead of returning NULL.
--- fs/proc/base.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
fs/proc/base.c +1 -1
@@ -840,7 +840,7 @@ struct mm_struct *proc_mem_open(struct inode *inode, unsigned int mode)put_task_struct (task);if (IS_ERR (mm)) -return mm ==ERR_PTR (-ESRCH ) ?NULL : mm; +return mm;/* ensure this mm_struct can't be freed */ mmgrab (mm);
Signature · 1 line
2.17.1
Quoted 11 lines · Penglei Jiang
The following functions call
proc_mem_openbut do not handle the case where it returns NULL:
__mem_openinfs/proc/base.c
proc_maps_openinfs/proc/task_mmu.c
smaps_rollup_openinfs/proc/task_mmu.c
pagemap_openinfs/proc/task_mmu.c
maps_openinfs/proc/task_nommu.cThe following reported bugs may be related to this issue:
Quoted 8 lines
https://lore.kernel.org/all/[email protected]
https://lore.kernel.org/all/[email protected]Fix:
Modify
proc_mem_opento return an error code in case of errors, instead of returning NULL.
The rw routines associated with these consumers explictly NULL check mm, which becomes redundant with the patch.
While I find it fishy that returning NULL was ever a thing to begin with, it is unclear to me if it can be easily changed now from userspace-visible behavior standpoint.
I think the best way forward for the time being is to add the missing NULL checks instead.
Quoted 21 lines
Signed-off-by Penglei Jiang <[email protected]>--- fs/proc/base.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)fs/proc/base.c +1
-1@@ -840,7 +840,7 @@ struct mm_struct *proc_mem_open(struct inode *inode, unsigned int mode)put_task_struct (task);if (IS_ERR (mm)) -return mm ==ERR_PTR (-ESRCH ) ?NULL : mm; +return mm;/* ensure this mm_struct can't be freed */ mmgrab (mm);Signature · 1 line
2.17.1
PATCH v2 1/1 proc: Fix the issue of proc_mem_open returning NULL
Hi, all
Quoted 47 lines · Mateusz Guzik
Quoted 11 lines · Penglei Jiang
The following functions call
proc_mem_openbut do not handle the case where it returns NULL:
__mem_openinfs/proc/base.c
proc_maps_openinfs/proc/task_mmu.c
smaps_rollup_openinfs/proc/task_mmu.c
pagemap_openinfs/proc/task_mmu.c
maps_openinfs/proc/task_nommu.cThe following reported bugs may be related to this issue:
Quoted 8 lines
https://lore.kernel.org/all/[email protected]
https://lore.kernel.org/all/[email protected]Fix:
Modify
proc_mem_opento return an error code in case of errors, instead of returning NULL.The rw routines associated with these consumers explictly NULL check mm, which becomes redundant with the patch.
While I find it fishy that returning NULL was ever a thing to begin with, it is unclear to me if it can be easily changed now from userspace-visible behavior standpoint.
I think the best way forward for the time being is to add the missing NULL checks instead.
Quoted 16 lines
Signed-off-by Penglei Jiang <[email protected]>--- fs/proc/base.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)fs/proc/base.c +1
-1@@ -840,7 +840,7 @@ struct mm_struct *proc_mem_open(struct inode *inode, unsigned int mode)put_task_struct (task);if (IS_ERR (mm)) -return mm ==ERR_PTR (-ESRCH ) ?NULL : mm; +return mm;
Quoted 5 lines
Quoted 5 lines
fs/proc/base.c +0
-1/* ensure this mm_struct can't be freed */ mmgrab (mm); --2.17.1
Mateusz Guzik provides valuable suggestions.
Complete the missing NULL checks.
--- fs/proc/base.c | 4 ++-- fs/proc/task_mmu.c | 12 ++++++------ fs/proc/task_nommu.c | 4 ++-- 3 files changed, 10 insertions(+), 10 deletions(-)
fs/proc/base.c +2 -2
@@ -854,8 +854,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 ;
fs/proc/task_mmu.c +6 -6
@@ -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;
@@ -1312,8 +1312,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;
@@ -2045,8 +2045,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 ; }
fs/proc/task_nommu.c +2 -2
@@ -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;
Signature · 1 line
2.17.1
Andrew Morton
maintainerQuoted 12 lines · On Mon, 24 Mar 2025 21:14:48 -0700 Penglei Jiang <[email protected]> wrote:
Quoted 9 lines
Quoted 9 lines
if (
IS_ERR(mm))(quoted diff) +1
-2-return mm == ERR_PTR(-ESRCH) ? NULL : mm; +return mm; /* ensure this mm_struct can't be freed */ mmgrab(mm); --2.17.1
Mateusz Guzik provides valuable suggestions.
Quoted 1 line
Complete the missing NULL checks.
proc_mem_open() can return errno, NULL or mm_struct*. It isn't obvious why.
While you're in there can you please add documentation to proc_mem_open() which explains its return values?
PATCH v3 1/1 proc: Fix the issue of proc_mem_open returning NULL
Quoted 14 lines · On Thu, 27 Mar 2025 12:24:45 -0700 Andrew Morton <[email protected]>…
Quoted 12 lines · On Mon, 24 Mar 2025 21:14:48 -0700 Penglei Jiang <[email protected]> wrote:
Quoted 9 lines
Quoted 9 lines
if (
IS_ERR(mm))(quoted diff) +1
-2-return mm == ERR_PTR(-ESRCH) ? NULL : mm; +return mm; /* ensure this mm_struct can't be freed */ mmgrab(mm); --2.17.1
Mateusz Guzik provides valuable suggestions.
Quoted 7 lines
Quoted 1 line
Complete the missing NULL checks.
proc_mem_open()can return errno, NULL or mm_struct*. It isn't obvious why.While you're in there can you please add documentation to
proc_mem_open()which explains its return values?
I apologize for the delayed response.
Add documentation comments to proc_mem_open() and add NULL checks in several call sites.
--- fs/proc/base.c | 12 +++++++++--- fs/proc/task_mmu.c | 12 ++++++------ fs/proc/task_nommu.c | 4 ++-- 3 files changed, 17 insertions(+), 11 deletions(-)
fs/proc/base.c +9 -3
@@ -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 (task->flags & PF_KTHREAD) +* - 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 ;
fs/proc/task_mmu.c +6 -6
@@ -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;
@@ -1312,8 +1312,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;
@@ -2045,8 +2045,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 ; }
fs/proc/task_nommu.c +2 -2
@@ -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;
Signature · 1 line
2.17.1
PATCH v4 1/1 proc: Fix the issue of proc_mem_open returning NULL
Quoted 21 lines · On Mon, 31 Mar 2025 02:16:35 -0700 Penglei Jiang <[email protected]> wrote:
Quoted 19 lines · On Thu, 27 Mar 2025 12:24:45 -0700 Andrew Morton <[email protected]>…
Quoted 12 lines · On Mon, 24 Mar 2025 21:14:48 -0700 Penglei Jiang <[email protected]> wrote:
Quoted 9 lines
Quoted 9 lines
if (
IS_ERR(mm))(quoted diff) +1
-2-return mm == ERR_PTR(-ESRCH) ? NULL : mm; +return mm; /* ensure this mm_struct can't be freed */ mmgrab(mm); --2.17.1
Mateusz Guzik provides valuable suggestions.
Quoted 1 line
Complete the missing NULL checks.
proc_mem_open()can return errno, NULL or mm_struct*. It isn't obvious why.
Quoted 7 lines
Quoted 2 lines
While you're in there can you please add documentation to
proc_mem_open()which explains its return values?I apologize for the delayed response.
Add documentation comments to
proc_mem_open()and add NULL checks in several call sites.
Adjust comments based on the V3 patch.
--- fs/proc/base.c | 12 +++++++++--- fs/proc/task_mmu.c | 12 ++++++------ fs/proc/task_nommu.c | 4 ++-- 3 files changed, 17 insertions(+), 11 deletions(-)
fs/proc/base.c +9 -3
@@ -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 ;
fs/proc/task_mmu.c +6 -6
@@ -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;
@@ -1312,8 +1312,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;
@@ -2045,8 +2045,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 ; }
fs/proc/task_nommu.c +2 -2
@@ -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;
Signature · 1 line
2.17.1
PATCH v5 1/1 proc: Fix the issue of proc_mem_open returning NULL
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.
---
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(-)
fs/proc/base.c +9 -3
@@ -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 ;
fs/proc/task_mmu.c +6 -6
@@ -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 ; }
fs/proc/task_nommu.c +2 -2
@@ -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;
Signature · 1 line
2.17.1