linux-fsdevel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] proc: use the same treatment to check proc_lseek as ones for proc_read_iter et.al.
@ 2025-06-07  2:13 wangzijie
  2025-08-25 12:30 ` Łukasz P. Michalik
  0 siblings, 1 reply; 3+ messages in thread
From: wangzijie @ 2025-06-07  2:13 UTC (permalink / raw)
  To: viro, akpm, rick.p.edgecombe, ast, adobriyan, kirill.shutemov,
	linux-fsdevel
  Cc: wangzijie

Check pde->proc_ops->proc_lseek directly may cause UAF in rmmod scenario. It's a gap
in proc_reg_open() after commit 654b33ada4ab("proc: fix UAF in proc_get_inode()").
Followed by AI Viro's suggestion, fix it in same manner.

Signed-off-by: wangzijie <wangzijie1@honor.com>
---
 fs/proc/generic.c       | 2 ++
 fs/proc/inode.c         | 2 +-
 fs/proc/internal.h      | 5 +++++
 include/linux/proc_fs.h | 1 +
 4 files changed, 9 insertions(+), 1 deletion(-)

diff --git a/fs/proc/generic.c b/fs/proc/generic.c
index a3e22803c..e0e50914a 100644
--- a/fs/proc/generic.c
+++ b/fs/proc/generic.c
@@ -569,6 +569,8 @@ static void pde_set_flags(struct proc_dir_entry *pde)
 	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,
diff --git a/fs/proc/inode.c b/fs/proc/inode.c
index a3eb3b740..73074b9c7 100644
--- a/fs/proc/inode.c
+++ b/fs/proc/inode.c
@@ -473,7 +473,7 @@ static int proc_reg_open(struct inode *inode, struct file *file)
 	typeof_member(struct proc_ops, proc_open) open;
 	struct pde_opener *pdeo;
 
-	if (!pde->proc_ops->proc_lseek)
+	if (!pde_has_proc_lseek(pde))
 		file->f_mode &= ~FMODE_LSEEK;
 
 	if (pde_is_permanent(pde)) {
diff --git a/fs/proc/internal.h b/fs/proc/internal.h
index 96122e91c..3d48ffe72 100644
--- a/fs/proc/internal.h
+++ b/fs/proc/internal.h
@@ -99,6 +99,11 @@ static inline bool pde_has_proc_compat_ioctl(const struct proc_dir_entry *pde)
 #endif
 }
 
+static inline bool pde_has_proc_lseek(const struct proc_dir_entry *pde)
+{
+	return pde->flags & PROC_ENTRY_proc_lseek;
+}
+
 extern struct kmem_cache *proc_dir_entry_cache;
 void pde_free(struct proc_dir_entry *pde);
 
diff --git a/include/linux/proc_fs.h b/include/linux/proc_fs.h
index ea62201c7..703d0c76c 100644
--- a/include/linux/proc_fs.h
+++ b/include/linux/proc_fs.h
@@ -27,6 +27,7 @@ enum {
 
 	PROC_ENTRY_proc_read_iter	= 1U << 1,
 	PROC_ENTRY_proc_compat_ioctl	= 1U << 2,
+	PROC_ENTRY_proc_lseek		= 1U << 3,
 };
 
 struct proc_ops {
-- 
2.25.1


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

* Re: [PATCH] proc: use the same treatment to check proc_lseek as ones for proc_read_iter et.al.
  2025-06-07  2:13 [PATCH] proc: use the same treatment to check proc_lseek as ones for proc_read_iter et.al wangzijie
@ 2025-08-25 12:30 ` Łukasz P. Michalik
  2025-08-25 12:47   ` wangzijie
  0 siblings, 1 reply; 3+ messages in thread
From: Łukasz P. Michalik @ 2025-08-25 12:30 UTC (permalink / raw)
  To: wangzijie1
  Cc: adobriyan, akpm, ast, kirill.shutemov, linux-fsdevel,
	rick.p.edgecombe, viro

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain; charset=./reply, Size: 467 bytes --]

Hello!

This change makes /proc/net/dev a non-seekable file.  There is
software that depends on it to be seekable (1), surprisingly some
other files in /proc still work fine after this change.

Minimal C testcase:
==
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>

int main() {
	int fd = open("/proc/net/dev", 0);
	off_t o = lseek(fd, 0, SEEK_SET);
	if (o == -1) {
		perror("lseek");
	}
}
==

1) https://github.com/scottchiefbaker/dool/issues/111

--
ŁPM

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

* Re: [PATCH] proc: use the same treatment to check proc_lseek as ones for proc_read_iter et.al.
  2025-08-25 12:30 ` Łukasz P. Michalik
@ 2025-08-25 12:47   ` wangzijie
  0 siblings, 0 replies; 3+ messages in thread
From: wangzijie @ 2025-08-25 12:47 UTC (permalink / raw)
  To: lpmichalik
  Cc: adobriyan, akpm, ast, kirill.shutemov, linux-fsdevel,
	rick.p.edgecombe, viro, wangzijie1

> Hello!
> 
> This change makes /proc/net/dev a non-seekable file.  There is
> software that depends on it to be seekable (1), surprisingly some
> other files in /proc still work fine after this change.
> 
> Minimal C testcase:
> ==
> #include <fcntl.h>
> #include <stdio.h>
> #include <unistd.h>
> 
> int main() {
> 	int fd = open("/proc/net/dev", 0);
> 	off_t o = lseek(fd, 0, SEEK_SET);
> 	if (o == -1) {
> 		perror("lseek");
> 	}
> }
> ==
> 
> 1) https://github.com/scottchiefbaker/dool/issues/111
> 
> --
> ŁPM

Thanks for your testcase, I have submitted a patch to fix this:
https://lore.kernel.org/all/20250821105806.1453833-1-wangzijie1@honor.com

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

end of thread, other threads:[~2025-08-25 12:47 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-06-07  2:13 [PATCH] proc: use the same treatment to check proc_lseek as ones for proc_read_iter et.al wangzijie
2025-08-25 12:30 ` Łukasz P. Michalik
2025-08-25 12:47   ` 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).