linux-fsdevel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v3] proc: fix missing pde_set_flags() for net proc files
@ 2025-08-21 10:58 wangzijie
  2025-08-23  8:43 ` Stefano Brivio
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: wangzijie @ 2025-08-21 10:58 UTC (permalink / raw)
  To: akpm, brauner, viro, adobriyan, rick.p.edgecombe, ast, k.shutemov,
	jirislaby, linux-fsdevel
  Cc: polynomial-c, gregkh, stable, regressions, wangzijie

To avoid potential UAF issues during module removal races, we use pde_set_flags()
to save proc_ops flags in PDE itself before proc_register(), and then use
pde_has_proc_*() helpers instead of directly dereferencing pde->proc_ops->*.

However, the pde_set_flags() call was missing when creating net related proc files.
This omission caused incorrect behavior which FMODE_LSEEK was being cleared
inappropriately in proc_reg_open() for net proc files. Lars reported it in this link[1].

Fix this by ensuring pde_set_flags() is called when register proc entry, and add
NULL check for proc_ops in pde_set_flags().

[1]: https://lore.kernel.org/all/20250815195616.64497967@chagall.paradoxon.rec/

Fixes: ff7ec8dc1b64 ("proc: use the same treatment to check proc_lseek as ones for proc_read_iter et.al")
Cc: stable@vger.kernel.org
Reported-by: Lars Wendler <polynomial-c@gmx.de>
Signed-off-by: wangzijie <wangzijie1@honor.com>
---
v3:
- followed by Christian's suggestion to stash pde->proc_ops in a local const variable
v2:
- followed by Jiri's suggestion to refractor code and reformat commit message
---
 fs/proc/generic.c | 38 +++++++++++++++++++++-----------------
 1 file changed, 21 insertions(+), 17 deletions(-)

diff --git a/fs/proc/generic.c b/fs/proc/generic.c
index 76e800e38..bd0c099cf 100644
--- a/fs/proc/generic.c
+++ b/fs/proc/generic.c
@@ -367,6 +367,25 @@ static const struct inode_operations proc_dir_inode_operations = {
 	.setattr	= proc_notify_change,
 };
 
+static void pde_set_flags(struct proc_dir_entry *pde)
+{
+	const struct proc_ops *proc_ops = pde->proc_ops;
+
+	if (!proc_ops)
+		return;
+
+	if (proc_ops->proc_flags & PROC_ENTRY_PERMANENT)
+		pde->flags |= PROC_ENTRY_PERMANENT;
+	if (proc_ops->proc_read_iter)
+		pde->flags |= PROC_ENTRY_proc_read_iter;
+#ifdef CONFIG_COMPAT
+	if (proc_ops->proc_compat_ioctl)
+		pde->flags |= PROC_ENTRY_proc_compat_ioctl;
+#endif
+	if (proc_ops->proc_lseek)
+		pde->flags |= PROC_ENTRY_proc_lseek;
+}
+
 /* returns the registered entry, or frees dp and returns NULL on failure */
 struct proc_dir_entry *proc_register(struct proc_dir_entry *dir,
 		struct proc_dir_entry *dp)
@@ -374,6 +393,8 @@ struct proc_dir_entry *proc_register(struct proc_dir_entry *dir,
 	if (proc_alloc_inum(&dp->low_ino))
 		goto out_free_entry;
 
+	pde_set_flags(dp);
+
 	write_lock(&proc_subdir_lock);
 	dp->parent = dir;
 	if (pde_subdir_insert(dir, dp) == false) {
@@ -561,20 +582,6 @@ struct proc_dir_entry *proc_create_reg(const char *name, umode_t mode,
 	return p;
 }
 
-static void pde_set_flags(struct proc_dir_entry *pde)
-{
-	if (pde->proc_ops->proc_flags & PROC_ENTRY_PERMANENT)
-		pde->flags |= PROC_ENTRY_PERMANENT;
-	if (pde->proc_ops->proc_read_iter)
-		pde->flags |= PROC_ENTRY_proc_read_iter;
-#ifdef CONFIG_COMPAT
-	if (pde->proc_ops->proc_compat_ioctl)
-		pde->flags |= PROC_ENTRY_proc_compat_ioctl;
-#endif
-	if (pde->proc_ops->proc_lseek)
-		pde->flags |= PROC_ENTRY_proc_lseek;
-}
-
 struct proc_dir_entry *proc_create_data(const char *name, umode_t mode,
 		struct proc_dir_entry *parent,
 		const struct proc_ops *proc_ops, void *data)
@@ -585,7 +592,6 @@ struct proc_dir_entry *proc_create_data(const char *name, umode_t mode,
 	if (!p)
 		return NULL;
 	p->proc_ops = proc_ops;
-	pde_set_flags(p);
 	return proc_register(parent, p);
 }
 EXPORT_SYMBOL(proc_create_data);
@@ -636,7 +642,6 @@ struct proc_dir_entry *proc_create_seq_private(const char *name, umode_t mode,
 	p->proc_ops = &proc_seq_ops;
 	p->seq_ops = ops;
 	p->state_size = state_size;
-	pde_set_flags(p);
 	return proc_register(parent, p);
 }
 EXPORT_SYMBOL(proc_create_seq_private);
@@ -667,7 +672,6 @@ struct proc_dir_entry *proc_create_single_data(const char *name, umode_t mode,
 		return NULL;
 	p->proc_ops = &proc_single_ops;
 	p->single_show = show;
-	pde_set_flags(p);
 	return proc_register(parent, p);
 }
 EXPORT_SYMBOL(proc_create_single_data);
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH v3] proc: fix missing pde_set_flags() for net proc files
  2025-08-21 10:58 [PATCH v3] proc: fix missing pde_set_flags() for net proc files wangzijie
@ 2025-08-23  8:43 ` Stefano Brivio
  2025-08-24  9:00 ` Petr Vaněk
  2025-09-03  6:57 ` wangzijie
  2 siblings, 0 replies; 5+ messages in thread
From: Stefano Brivio @ 2025-08-23  8:43 UTC (permalink / raw)
  To: wangzijie
  Cc: akpm, brauner, viro, adobriyan, rick.p.edgecombe, ast, k.shutemov,
	jirislaby, linux-fsdevel, polynomial-c, gregkh, stable,
	regressions

On Thu, 21 Aug 2025 18:58:06 +0800
wangzijie <wangzijie1@honor.com> wrote:

> To avoid potential UAF issues during module removal races, we use pde_set_flags()
> to save proc_ops flags in PDE itself before proc_register(), and then use
> pde_has_proc_*() helpers instead of directly dereferencing pde->proc_ops->*.
> 
> However, the pde_set_flags() call was missing when creating net related proc files.
> This omission caused incorrect behavior which FMODE_LSEEK was being cleared
> inappropriately in proc_reg_open() for net proc files. Lars reported it in this link[1].
> 
> Fix this by ensuring pde_set_flags() is called when register proc entry, and add
> NULL check for proc_ops in pde_set_flags().
> 
> [1]: https://lore.kernel.org/all/20250815195616.64497967@chagall.paradoxon.rec/
> 
> Fixes: ff7ec8dc1b64 ("proc: use the same treatment to check proc_lseek as ones for proc_read_iter et.al")
> Cc: stable@vger.kernel.org
> Reported-by: Lars Wendler <polynomial-c@gmx.de>
> Signed-off-by: wangzijie <wangzijie1@honor.com>

Tested-by: Stefano Brivio <sbrivio@redhat.com>

For the records, see also my report and obsolete patch at:

  https://lore.kernel.org/linux-fsdevel/20250822172335.3187858-1-sbrivio@redhat.com/

-- 
Stefano


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v3] proc: fix missing pde_set_flags() for net proc files
  2025-08-21 10:58 [PATCH v3] proc: fix missing pde_set_flags() for net proc files wangzijie
  2025-08-23  8:43 ` Stefano Brivio
@ 2025-08-24  9:00 ` Petr Vaněk
  2025-08-27  5:32   ` Lars Wendler
  2025-09-03  6:57 ` wangzijie
  2 siblings, 1 reply; 5+ messages in thread
From: Petr Vaněk @ 2025-08-24  9:00 UTC (permalink / raw)
  To: wangzijie
  Cc: akpm, brauner, viro, adobriyan, rick.p.edgecombe, ast, k.shutemov,
	jirislaby, linux-fsdevel, polynomial-c, gregkh, stable,
	regressions

On Thu, Aug 21, 2025 at 06:58:06PM +0800, wangzijie wrote:
> To avoid potential UAF issues during module removal races, we use pde_set_flags()
> to save proc_ops flags in PDE itself before proc_register(), and then use
> pde_has_proc_*() helpers instead of directly dereferencing pde->proc_ops->*.
> 
> However, the pde_set_flags() call was missing when creating net related proc files.
> This omission caused incorrect behavior which FMODE_LSEEK was being cleared
> inappropriately in proc_reg_open() for net proc files. Lars reported it in this link[1].
> 
> Fix this by ensuring pde_set_flags() is called when register proc entry, and add
> NULL check for proc_ops in pde_set_flags().
> 
> [1]: https://lore.kernel.org/all/20250815195616.64497967@chagall.paradoxon.rec/
> 
> Fixes: ff7ec8dc1b64 ("proc: use the same treatment to check proc_lseek as ones for proc_read_iter et.al")
> Cc: stable@vger.kernel.org
> Reported-by: Lars Wendler <polynomial-c@gmx.de>
> Signed-off-by: wangzijie <wangzijie1@honor.com>

Tested-by: Petr Vaněk <pv@excello.cz>

We have noticed lseek issue with /proc/self/net/sockstat file recently
and this patch fixes it for us.

Thanks,
Petr

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v3] proc: fix missing pde_set_flags() for net proc files
  2025-08-24  9:00 ` Petr Vaněk
@ 2025-08-27  5:32   ` Lars Wendler
  0 siblings, 0 replies; 5+ messages in thread
From: Lars Wendler @ 2025-08-27  5:32 UTC (permalink / raw)
  To: Petr Vaněk
  Cc: wangzijie, akpm, brauner, viro, adobriyan, rick.p.edgecombe, ast,
	k.shutemov, jirislaby, linux-fsdevel, gregkh, stable, regressions

Am Sun, 24 Aug 2025 11:00:55 +0200
schrieb Petr Vaněk <pv@excello.cz>:

> On Thu, Aug 21, 2025 at 06:58:06PM +0800, wangzijie wrote:
> > To avoid potential UAF issues during module removal races, we use
> > pde_set_flags() to save proc_ops flags in PDE itself before
> > proc_register(), and then use pde_has_proc_*() helpers instead of
> > directly dereferencing pde->proc_ops->*.
> > 
> > However, the pde_set_flags() call was missing when creating net
> > related proc files. This omission caused incorrect behavior which
> > FMODE_LSEEK was being cleared inappropriately in proc_reg_open()
> > for net proc files. Lars reported it in this link[1].
> > 
> > Fix this by ensuring pde_set_flags() is called when register proc
> > entry, and add NULL check for proc_ops in pde_set_flags().
> > 
> > [1]:
> > https://lore.kernel.org/all/20250815195616.64497967@chagall.paradoxon.rec/
> > 
> > Fixes: ff7ec8dc1b64 ("proc: use the same treatment to check
> > proc_lseek as ones for proc_read_iter et.al") Cc:
> > stable@vger.kernel.org Reported-by: Lars Wendler
> > <polynomial-c@gmx.de> Signed-off-by: wangzijie
> > <wangzijie1@honor.com>
> 
> Tested-by: Petr Vaněk <pv@excello.cz>
> 
> We have noticed lseek issue with /proc/self/net/sockstat file recently
> and this patch fixes it for us.
> 
> Thanks,
> Petr

Applied to linux-6.12.43 and it fixes the issue in linux-6.12.y branch.
I have yet to test it in linux-6.6.y branch.

Tested by: Lars Wendler <polynomial-c@gmx.de>

Thanks
Lars

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v3] proc: fix missing pde_set_flags() for net proc files
  2025-08-21 10:58 [PATCH v3] proc: fix missing pde_set_flags() for net proc files wangzijie
  2025-08-23  8:43 ` Stefano Brivio
  2025-08-24  9:00 ` Petr Vaněk
@ 2025-09-03  6:57 ` wangzijie
  2 siblings, 0 replies; 5+ messages in thread
From: wangzijie @ 2025-09-03  6:57 UTC (permalink / raw)
  To: wangzijie1
  Cc: adobriyan, akpm, ast, brauner, gregkh, jirislaby, k.shutemov,
	linux-fsdevel, polynomial-c, regressions, rick.p.edgecombe,
	stable, viro, spender

> To avoid potential UAF issues during module removal races, we use pde_set_flags()
> to save proc_ops flags in PDE itself before proc_register(), and then use
> pde_has_proc_*() helpers instead of directly dereferencing pde->proc_ops->*.
> 
> However, the pde_set_flags() call was missing when creating net related proc files.
> This omission caused incorrect behavior which FMODE_LSEEK was being cleared
> inappropriately in proc_reg_open() for net proc files. Lars reported it in this link[1].
> 
> Fix this by ensuring pde_set_flags() is called when register proc entry, and add
> NULL check for proc_ops in pde_set_flags().
> 
> [1]: https://lore.kernel.org/all/20250815195616.64497967@chagall.paradoxon.rec/
> 
> Fixes: ff7ec8dc1b64 ("proc: use the same treatment to check proc_lseek as ones for proc_read_iter et.al")
> Cc: stable@vger.kernel.org
> Reported-by: Lars Wendler <polynomial-c@gmx.de>
> Signed-off-by: wangzijie <wangzijie1@honor.com>
> ---
> v3:
> - followed by Christian's suggestion to stash pde->proc_ops in a local const variable
> v2:
> - followed by Jiri's suggestion to refractor code and reformat commit message
> ---
>  fs/proc/generic.c | 38 +++++++++++++++++++++-----------------
>  1 file changed, 21 insertions(+), 17 deletions(-)
> 
> diff --git a/fs/proc/generic.c b/fs/proc/generic.c
> index 76e800e38..bd0c099cf 100644
> --- a/fs/proc/generic.c
> +++ b/fs/proc/generic.c
> @@ -367,6 +367,25 @@ static const struct inode_operations proc_dir_inode_operations = {
>  	.setattr	= proc_notify_change,
>  };
>  
> +static void pde_set_flags(struct proc_dir_entry *pde)
> +{
> +	const struct proc_ops *proc_ops = pde->proc_ops;
> +
> +	if (!proc_ops)
> +		return;
> +
> +	if (proc_ops->proc_flags & PROC_ENTRY_PERMANENT)
> +		pde->flags |= PROC_ENTRY_PERMANENT;
> +	if (proc_ops->proc_read_iter)
> +		pde->flags |= PROC_ENTRY_proc_read_iter;
> +#ifdef CONFIG_COMPAT
> +	if (proc_ops->proc_compat_ioctl)
> +		pde->flags |= PROC_ENTRY_proc_compat_ioctl;
> +#endif
> +	if (proc_ops->proc_lseek)
> +		pde->flags |= PROC_ENTRY_proc_lseek;
> +}
> +
>  /* returns the registered entry, or frees dp and returns NULL on failure */
>  struct proc_dir_entry *proc_register(struct proc_dir_entry *dir,
>  		struct proc_dir_entry *dp)
> @@ -374,6 +393,8 @@ struct proc_dir_entry *proc_register(struct proc_dir_entry *dir,
>  	if (proc_alloc_inum(&dp->low_ino))
>  		goto out_free_entry;
>  
> +	pde_set_flags(dp);
> +
>  	write_lock(&proc_subdir_lock);
>  	dp->parent = dir;
>  	if (pde_subdir_insert(dir, dp) == false) {
> @@ -561,20 +582,6 @@ struct proc_dir_entry *proc_create_reg(const char *name, umode_t mode,
>  	return p;
>  }
>  
> -static void pde_set_flags(struct proc_dir_entry *pde)
> -{
> -	if (pde->proc_ops->proc_flags & PROC_ENTRY_PERMANENT)
> -		pde->flags |= PROC_ENTRY_PERMANENT;
> -	if (pde->proc_ops->proc_read_iter)
> -		pde->flags |= PROC_ENTRY_proc_read_iter;
> -#ifdef CONFIG_COMPAT
> -	if (pde->proc_ops->proc_compat_ioctl)
> -		pde->flags |= PROC_ENTRY_proc_compat_ioctl;
> -#endif
> -	if (pde->proc_ops->proc_lseek)
> -		pde->flags |= PROC_ENTRY_proc_lseek;
> -}
> -
>  struct proc_dir_entry *proc_create_data(const char *name, umode_t mode,
>  		struct proc_dir_entry *parent,
>  		const struct proc_ops *proc_ops, void *data)
> @@ -585,7 +592,6 @@ struct proc_dir_entry *proc_create_data(const char *name, umode_t mode,
>  	if (!p)
>  		return NULL;
>  	p->proc_ops = proc_ops;
> -	pde_set_flags(p);
>  	return proc_register(parent, p);
>  }
>  EXPORT_SYMBOL(proc_create_data);
> @@ -636,7 +642,6 @@ struct proc_dir_entry *proc_create_seq_private(const char *name, umode_t mode,
>  	p->proc_ops = &proc_seq_ops;
>  	p->seq_ops = ops;
>  	p->state_size = state_size;
> -	pde_set_flags(p);
>  	return proc_register(parent, p);
>  }
>  EXPORT_SYMBOL(proc_create_seq_private);
> @@ -667,7 +672,6 @@ struct proc_dir_entry *proc_create_single_data(const char *name, umode_t mode,
>  		return NULL;
>  	p->proc_ops = &proc_single_ops;
>  	p->single_show = show;
> -	pde_set_flags(p);
>  	return proc_register(parent, p);
>  }
>  EXPORT_SYMBOL(proc_create_single_data);
> -- 
> 2.25.1

Hi all,

Sorry for disturbing, Brad reported type confusion introduced by this patch
to me, we missed a part in the definition of proc_dir_entry:
        union {
                const struct proc_ops *proc_ops;
                const struct file_operations *proc_dir_ops;
        };

Quoting the email he sent to me:
"
any dereference of ->proc_ops assuming it is a proc_ops structure results in type confusion,
it probably won't be (easily) noticed without CONFIG_RANDSTRUCT because of the way in which
proc_flags overlaps with the module owner pointer in the file_operations structure(and the
alignment of the struct module pointed to).

With RANDSTRUCT enabled, users are likely to observe warnings on proc dir removal like:
[ 19.093369] removing permanent /proc entry '12/i8042'
[ 19.093396] WARNING: CPU: 0 PID: 1 at fs/proc/generic.c:886
remove_proc_subtree+0xcc/0x218
"

I examined where proc_register() is called, we can do !S_ISDIR(dp->mode) test before calling
pde_set_flags() to fix it(Brad also suggested to do this), or we can drop this patch and
apply the initial version[1]. I think the former is better.

Dose anyone have any suggestions?

[1] https://lore.kernel.org/all/20250818040535.564611-1-wangzijie1@honor.com/.


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2025-09-03  6:58 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-08-21 10:58 [PATCH v3] proc: fix missing pde_set_flags() for net proc files wangzijie
2025-08-23  8:43 ` Stefano Brivio
2025-08-24  9:00 ` Petr Vaněk
2025-08-27  5:32   ` Lars Wendler
2025-09-03  6:57 ` wangzijie

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).