Skip to contentSkip to conversation

proc: Bring back lseek() operations for /proc/net entries

PATCH In review Stefano Brivio 3 participants 5 messages Aug 22, 2025 – Aug 23, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 proc: Bring back lseek() operations for /proc/net entries

Commit ff7ec8dc1b64 ("proc: use the same treatment to check proc_lseek as ones for proc_read_iter et.al") breaks lseek() for all /proc/net entries, as shown for instance by pasta(1), a user-mode network implementation using those entries to scan for bound ports:

  $ strace -e openat,lseek -e s=none pasta -- true
  [...]
  openat(AT_FDCWD, "/proc/net/tcp", O_RDONLY|O_CLOEXEC) = 12
  openat(AT_FDCWD, "/proc/net/tcp6", O_RDONLY|O_CLOEXEC) = 13
  lseek(12, 0, SEEK_SET)                  = -1 ESPIPE (Illegal seek)
  lseek() failed on /proc/net file: Illegal seek
  lseek(13, 0, SEEK_SET)                  = -1 ESPIPE (Illegal seek)
  lseek() failed on /proc/net file: Illegal seek
  openat(AT_FDCWD, "/proc/net/udp", O_RDONLY|O_CLOEXEC) = 14
  openat(AT_FDCWD, "/proc/net/udp6", O_RDONLY|O_CLOEXEC) = 15
  lseek(14, 0, SEEK_SET)                  = -1 ESPIPE (Illegal seek)
  lseek() failed on /proc/net file: Illegal seek
  lseek(15, 0, SEEK_SET)                  = -1 ESPIPE (Illegal seek)
  lseek() failed on /proc/net file: Illegal seek
  [...]

That's because PROC_ENTRY_proc_lseek isn't set for /proc/net entries, and it's now mandatory for lseek(). In fact, flags aren't set at all for those entries because pde_set_flags() isn't called for them.

As commit d919b33dafb3 ("proc: faster open/read/close with "permanent" files") introduced flags for procfs directory entries, along with the pde_set_flags() helper, they weren't relevant for /proc/net entries, so the lack of pde_set_flags() calls in proc_create_net_*() functions was harmless.

Now that the calls are strictly needed for lseek() functionality, add them.

Fixes ff7ec8dc1b64 ("proc: use the same treatment to check proc_lseek as ones for proc_read_iter et.al")
---
 fs/proc/generic.c  | 2 +-
 fs/proc/internal.h | 1 +
 fs/proc/proc_net.c | 4 ++++
 3 files changed, 6 insertions(+), 1 deletion(-)
fs/proc/generic.c +1 -1
index 76e800e38c8f..57ec5e385d1b 100644
@@ -561,7 +561,7 @@ 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)
+void pde_set_flags(struct proc_dir_entry *pde)
 {
 	if (pde->proc_ops->proc_flags & PROC_ENTRY_PERMANENT)
 		pde->flags |= PROC_ENTRY_PERMANENT;
fs/proc/internal.h +1 -0
index e737401d7383..a358974f14d2 100644
@@ -284,6 +284,7 @@ extern struct dentry *proc_lookup(struct inode *, struct dentry *, unsigned int)
 struct dentry *proc_lookup_de(struct inode *, struct dentry *, struct proc_dir_entry *);
 extern int proc_readdir(struct file *, struct dir_context *);
 int proc_readdir_de(struct file *, struct dir_context *, struct proc_dir_entry *);
+void pde_set_flags(struct proc_dir_entry *pde);
 
 static inline void pde_get(struct proc_dir_entry *pde)
 {
fs/proc/proc_net.c +4 -0
index 52f0b75cbce2..20bc7481b02c 100644
@@ -124,6 +124,7 @@ struct proc_dir_entry *proc_create_net_data(const char *name, umode_t mode,
 	p->proc_ops = &proc_net_seq_ops;
 	p->seq_ops = ops;
 	p->state_size = state_size;
+	pde_set_flags(p);
 	return proc_register(parent, p);
 }
 EXPORT_SYMBOL_GPL(proc_create_net_data);
@@ -170,6 +171,7 @@ struct proc_dir_entry *proc_create_net_data_write(const char *name, umode_t mode
 	p->seq_ops = ops;
 	p->state_size = state_size;
 	p->write = write;
+	pde_set_flags(p);
 	return proc_register(parent, p);
 }
 EXPORT_SYMBOL_GPL(proc_create_net_data_write);
@@ -217,6 +219,7 @@ struct proc_dir_entry *proc_create_net_single(const char *name, umode_t mode,
 	pde_force_lookup(p);
 	p->proc_ops = &proc_net_single_ops;
 	p->single_show = show;
+	pde_set_flags(p);
 	return proc_register(parent, p);
 }
 EXPORT_SYMBOL_GPL(proc_create_net_single);
@@ -261,6 +264,7 @@ struct proc_dir_entry *proc_create_net_single_write(const char *name, umode_t mo
 	p->proc_ops = &proc_net_single_ops;
 	p->single_show = show;
 	p->write = write;
+	pde_set_flags(p);
 	return proc_register(parent, p);
 }
 EXPORT_SYMBOL_GPL(proc_create_net_single_write);
Signature · 1 line
2.43.0
#1 lore raw
Quoted 25 lines · On Fri, 22 Aug 2025 19:23:35 +0200 Stefano Brivio <[email protected]> wrote:

Commit ff7ec8dc1b64 ("proc: use the same treatment to check proc_lseek as ones for proc_read_iter et.al") breaks lseek() for all /proc/net entries, as shown for instance by pasta(1), a user-mode network implementation using those entries to scan for bound ports:

  $ strace -e openat,lseek -e s=none pasta -- true
  [...]
  openat(AT_FDCWD, "/proc/net/tcp", O_RDONLY|O_CLOEXEC) = 12
  openat(AT_FDCWD, "/proc/net/tcp6", O_RDONLY|O_CLOEXEC) = 13
  lseek(12, 0, SEEK_SET)                  = -1 ESPIPE (Illegal seek)
  lseek() failed on /proc/net file: Illegal seek
  lseek(13, 0, SEEK_SET)                  = -1 ESPIPE (Illegal seek)
  lseek() failed on /proc/net file: Illegal seek
  openat(AT_FDCWD, "/proc/net/udp", O_RDONLY|O_CLOEXEC) = 14
  openat(AT_FDCWD, "/proc/net/udp6", O_RDONLY|O_CLOEXEC) = 15
  lseek(14, 0, SEEK_SET)                  = -1 ESPIPE (Illegal seek)
  lseek() failed on /proc/net file: Illegal seek
  lseek(15, 0, SEEK_SET)                  = -1 ESPIPE (Illegal seek)
  lseek() failed on /proc/net file: Illegal seek
  [...]

That's because PROC_ENTRY_proc_lseek isn't set for /proc/net entries, and it's now mandatory for lseek(). In fact, flags aren't set at all for those entries because pde_set_flags() isn't called for them.

Quoted 8 lines

As commit d919b33dafb3 ("proc: faster open/read/close with "permanent" files") introduced flags for procfs directory entries, along with the pde_set_flags() helper, they weren't relevant for /proc/net entries, so the lack of pde_set_flags() calls in proc_create_net_*() functions was harmless.

Now that the calls are strictly needed for lseek() functionality, add them.

Thanks. We already have
https://lkml.kernel.org/r/[email protected]
- does that look suitable?

#2 lore raw
Quoted 33 lines · On Fri, 22 Aug 2025 16:09:04 -0700 Andrew Morton <[email protected]>…
Quoted 31 lines · On Fri, 22 Aug 2025 19:23:35 +0200 Stefano Brivio <[email protected]> wrote:

Commit ff7ec8dc1b64 ("proc: use the same treatment to check proc_lseek as ones for proc_read_iter et.al") breaks lseek() for all /proc/net entries, as shown for instance by pasta(1), a user-mode network implementation using those entries to scan for bound ports:

  $ strace -e openat,lseek -e s=none pasta -- true
  [...]
  openat(AT_FDCWD, "/proc/net/tcp", O_RDONLY|O_CLOEXEC) = 12
  openat(AT_FDCWD, "/proc/net/tcp6", O_RDONLY|O_CLOEXEC) = 13
  lseek(12, 0, SEEK_SET)                  = -1 ESPIPE (Illegal seek)
  lseek() failed on /proc/net file: Illegal seek
  lseek(13, 0, SEEK_SET)                  = -1 ESPIPE (Illegal seek)
  lseek() failed on /proc/net file: Illegal seek
  openat(AT_FDCWD, "/proc/net/udp", O_RDONLY|O_CLOEXEC) = 14
  openat(AT_FDCWD, "/proc/net/udp6", O_RDONLY|O_CLOEXEC) = 15
  lseek(14, 0, SEEK_SET)                  = -1 ESPIPE (Illegal seek)
  lseek() failed on /proc/net file: Illegal seek
  lseek(15, 0, SEEK_SET)                  = -1 ESPIPE (Illegal seek)
  lseek() failed on /proc/net file: Illegal seek
  [...]

That's because PROC_ENTRY_proc_lseek isn't set for /proc/net entries, and it's now mandatory for lseek(). In fact, flags aren't set at all for those entries because pde_set_flags() isn't called for them.

As commit d919b33dafb3 ("proc: faster open/read/close with "permanent" files") introduced flags for procfs directory entries, along with the pde_set_flags() helper, they weren't relevant for /proc/net entries, so the lack of pde_set_flags() calls in proc_create_net_*() functions was harmless.

Quoted 6 lines
Quoted 2 lines

Now that the calls are strictly needed for lseek() functionality, add them.

Thanks. We already have
https://lkml.kernel.org/r/[email protected]
- does that look suitable?

Sorry, I didn't spot that one. It sure does!

Signature · 1 line
Stefano
#3 lore raw
In reply to Stefano Brivio
Quoted 105 lines

Commit ff7ec8dc1b64 ("proc: use the same treatment to check proc_lseek as ones for proc_read_iter et.al") breaks lseek() for all /proc/net entries, as shown for instance by pasta(1), a user-mode network implementation using those entries to scan for bound ports:

  $ strace -e openat,lseek -e s=none pasta -- true
  [...]
  openat(AT_FDCWD, "/proc/net/tcp", O_RDONLY|O_CLOEXEC) = 12
  openat(AT_FDCWD, "/proc/net/tcp6", O_RDONLY|O_CLOEXEC) = 13
  lseek(12, 0, SEEK_SET)                  = -1 ESPIPE (Illegal seek)
  lseek() failed on /proc/net file: Illegal seek
  lseek(13, 0, SEEK_SET)                  = -1 ESPIPE (Illegal seek)
  lseek() failed on /proc/net file: Illegal seek
  openat(AT_FDCWD, "/proc/net/udp", O_RDONLY|O_CLOEXEC) = 14
  openat(AT_FDCWD, "/proc/net/udp6", O_RDONLY|O_CLOEXEC) = 15
  lseek(14, 0, SEEK_SET)                  = -1 ESPIPE (Illegal seek)
  lseek() failed on /proc/net file: Illegal seek
  lseek(15, 0, SEEK_SET)                  = -1 ESPIPE (Illegal seek)
  lseek() failed on /proc/net file: Illegal seek
  [...]

That's because PROC_ENTRY_proc_lseek isn't set for /proc/net entries, and it's now mandatory for lseek(). In fact, flags aren't set at all for those entries because pde_set_flags() isn't called for them.

As commit d919b33dafb3 ("proc: faster open/read/close with "permanent" files") introduced flags for procfs directory entries, along with the pde_set_flags() helper, they weren't relevant for /proc/net entries, so the lack of pde_set_flags() calls in proc_create_net_*() functions was harmless.

Now that the calls are strictly needed for lseek() functionality, add them.

Fixes ff7ec8dc1b64 ("proc: use the same treatment to check proc_lseek as ones for proc_read_iter et.al")
---
 fs/proc/generic.c  | 2 +-
 fs/proc/internal.h | 1 +
 fs/proc/proc_net.c | 4 ++++
 3 files changed, 6 insertions(+), 1 deletion(-)
fs/proc/generic.c +1 -1
index 76e800e38c8f..57ec5e385d1b 100644
@@ -561,7 +561,7 @@ 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)
+void pde_set_flags(struct proc_dir_entry *pde)
 {
 	if (pde->proc_ops->proc_flags & PROC_ENTRY_PERMANENT)
 		pde->flags |= PROC_ENTRY_PERMANENT;
fs/proc/internal.h +1 -0
index e737401d7383..a358974f14d2 100644
@@ -284,6 +284,7 @@ extern struct dentry *proc_lookup(struct inode *, struct dentry *, unsigned int)
 struct dentry *proc_lookup_de(struct inode *, struct dentry *, struct proc_dir_entry *);
 extern int proc_readdir(struct file *, struct dir_context *);
 int proc_readdir_de(struct file *, struct dir_context *, struct proc_dir_entry *);
+void pde_set_flags(struct proc_dir_entry *pde);
 
 static inline void pde_get(struct proc_dir_entry *pde)
 {
fs/proc/proc_net.c +4 -0
index 52f0b75cbce2..20bc7481b02c 100644
@@ -124,6 +124,7 @@ struct proc_dir_entry *proc_create_net_data(const char *name, umode_t mode,
 	p->proc_ops = &proc_net_seq_ops;
 	p->seq_ops = ops;
 	p->state_size = state_size;
+	pde_set_flags(p);
 	return proc_register(parent, p);
 }
 EXPORT_SYMBOL_GPL(proc_create_net_data);
@@ -170,6 +171,7 @@ struct proc_dir_entry *proc_create_net_data_write(const char *name, umode_t mode
 	p->seq_ops = ops;
 	p->state_size = state_size;
 	p->write = write;
+	pde_set_flags(p);
 	return proc_register(parent, p);
 }
 EXPORT_SYMBOL_GPL(proc_create_net_data_write);
@@ -217,6 +219,7 @@ struct proc_dir_entry *proc_create_net_single(const char *name, umode_t mode,
 	pde_force_lookup(p);
 	p->proc_ops = &proc_net_single_ops;
 	p->single_show = show;
+	pde_set_flags(p);
 	return proc_register(parent, p);
 }
 EXPORT_SYMBOL_GPL(proc_create_net_single);
@@ -261,6 +264,7 @@ struct proc_dir_entry *proc_create_net_single_write(const char *name, umode_t mo
 	p->proc_ops = &proc_net_single_ops;
 	p->single_show = show;
 	p->write = write;
+	pde_set_flags(p);
 	return proc_register(parent, p);
 }
 EXPORT_SYMBOL_GPL(proc_create_net_single_write);
Signature · 1 line
2.43.0

Hi Stefano,
Thanks for your patch, Lars reported this bug last week:
https://lore.kernel.org/all/[email protected]/

Jiri suggested to make pde_set_flags() part of proc_register(). I think it can help to avoid lack of pde_set_flags() calls in the future and make code clean.

I have submitted a patch:
https://lore.kernel.org/all/[email protected]

#4 lore raw

Hi wangzijie,

Quoted 101 lines · On Sat, 23 Aug 2025 09:53:49 +0800 wangzijie <[email protected]> wrote:
Quoted 101 lines

Commit ff7ec8dc1b64 ("proc: use the same treatment to check proc_lseek as ones for proc_read_iter et.al") breaks lseek() for all /proc/net entries, as shown for instance by pasta(1), a user-mode network implementation using those entries to scan for bound ports:

  $ strace -e openat,lseek -e s=none pasta -- true
  [...]
  openat(AT_FDCWD, "/proc/net/tcp", O_RDONLY|O_CLOEXEC) = 12
  openat(AT_FDCWD, "/proc/net/tcp6", O_RDONLY|O_CLOEXEC) = 13
  lseek(12, 0, SEEK_SET)                  = -1 ESPIPE (Illegal seek)
  lseek() failed on /proc/net file: Illegal seek
  lseek(13, 0, SEEK_SET)                  = -1 ESPIPE (Illegal seek)
  lseek() failed on /proc/net file: Illegal seek
  openat(AT_FDCWD, "/proc/net/udp", O_RDONLY|O_CLOEXEC) = 14
  openat(AT_FDCWD, "/proc/net/udp6", O_RDONLY|O_CLOEXEC) = 15
  lseek(14, 0, SEEK_SET)                  = -1 ESPIPE (Illegal seek)
  lseek() failed on /proc/net file: Illegal seek
  lseek(15, 0, SEEK_SET)                  = -1 ESPIPE (Illegal seek)
  lseek() failed on /proc/net file: Illegal seek
  [...]

That's because PROC_ENTRY_proc_lseek isn't set for /proc/net entries, and it's now mandatory for lseek(). In fact, flags aren't set at all for those entries because pde_set_flags() isn't called for them.

As commit d919b33dafb3 ("proc: faster open/read/close with "permanent" files") introduced flags for procfs directory entries, along with the pde_set_flags() helper, they weren't relevant for /proc/net entries, so the lack of pde_set_flags() calls in proc_create_net_*() functions was harmless.

Now that the calls are strictly needed for lseek() functionality, add them.

Fixes ff7ec8dc1b64 ("proc: use the same treatment to check proc_lseek as ones for proc_read_iter et.al")
---
 fs/proc/generic.c  | 2 +-
 fs/proc/internal.h | 1 +
 fs/proc/proc_net.c | 4 ++++
 3 files changed, 6 insertions(+), 1 deletion(-)
fs/proc/generic.c +1 -1
index 76e800e38c8f..57ec5e385d1b 100644
@@ -561,7 +561,7 @@ 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)
+void pde_set_flags(struct proc_dir_entry *pde)
 {
 	if (pde->proc_ops->proc_flags & PROC_ENTRY_PERMANENT)
 		pde->flags |= PROC_ENTRY_PERMANENT;
fs/proc/internal.h +1 -0
index e737401d7383..a358974f14d2 100644
@@ -284,6 +284,7 @@ extern struct dentry *proc_lookup(struct inode *, struct dentry *, unsigned int)
 struct dentry *proc_lookup_de(struct inode *, struct dentry *, struct proc_dir_entry *);
 extern int proc_readdir(struct file *, struct dir_context *);
 int proc_readdir_de(struct file *, struct dir_context *, struct proc_dir_entry *);
+void pde_set_flags(struct proc_dir_entry *pde);
 
 static inline void pde_get(struct proc_dir_entry *pde)
 {
fs/proc/proc_net.c +4 -0
index 52f0b75cbce2..20bc7481b02c 100644
@@ -124,6 +124,7 @@ struct proc_dir_entry *proc_create_net_data(const char *name, umode_t mode,
 	p->proc_ops = &proc_net_seq_ops;
 	p->seq_ops = ops;
 	p->state_size = state_size;
+	pde_set_flags(p);
 	return proc_register(parent, p);
 }
 EXPORT_SYMBOL_GPL(proc_create_net_data);
@@ -170,6 +171,7 @@ struct proc_dir_entry *proc_create_net_data_write(const char *name, umode_t mode
 	p->seq_ops = ops;
 	p->state_size = state_size;
 	p->write = write;
+	pde_set_flags(p);
 	return proc_register(parent, p);
 }
 EXPORT_SYMBOL_GPL(proc_create_net_data_write);
@@ -217,6 +219,7 @@ struct proc_dir_entry *proc_create_net_single(const char *name, umode_t mode,
 	pde_force_lookup(p);
 	p->proc_ops = &proc_net_single_ops;
 	p->single_show = show;
+	pde_set_flags(p);
 	return proc_register(parent, p);
 }
 EXPORT_SYMBOL_GPL(proc_create_net_single);
@@ -261,6 +264,7 @@ struct proc_dir_entry *proc_create_net_single_write(const char *name, umode_t mo
 	p->proc_ops = &proc_net_single_ops;
 	p->single_show = show;
 	p->write = write;
+	pde_set_flags(p);
 	return proc_register(parent, p);
Quoted 8 lines
Quoted 4 lines
fs/proc/proc_net.c +0 -0
 }
 EXPORT_SYMBOL_GPL(proc_create_net_single_write);
Signature · 1 line
2.43.0  

Hi Stefano,
Thanks for your patch, Lars reported this bug last week:
https://lore.kernel.org/all/[email protected]/

Apologies, I didn't see that. It's definitely the same issue.

Quoted 2 lines

Jiri suggested to make pde_set_flags() part of proc_register(). I think it can help to avoid lack of pde_set_flags() calls in the future and make code clean.

Right, I was pondering to try something like that, but I just wanted to submit a minimal fix for the moment being.

In any case, you have it ready, and it's obviously cleaner than my solution, so, of course, let's discard my patch.

Quoted 2 lines

I have submitted a patch:
https://lore.kernel.org/all/[email protected]

I just tested it, it works for me. Thanks for fixing that!

Signature · 1 line
Stefano