mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] drivers/char/mem.c: fix null and zero dev lseek
@ 2026-03-20  4:17 David Timber
  2026-03-20  6:02 ` Greg KH
  0 siblings, 1 reply; 5+ messages in thread
From: David Timber @ 2026-03-20  4:17 UTC (permalink / raw)
  To: arnd, gregkh; +Cc: linux-kernel, David Timber

lseek() on /dev/null and /dev/zero always returns 0. This is
problematic for userland programs that detect holes and advancing
the offset by the delta (SEEK_HOLE - SEEK_DATA), which will always be
calculated as zero.

Link: https://github.com/util-linux/util-linux/pull/4132

Signed-off-by: David Timber <dxdt@dev.snart.me>
---
 drivers/char/mem.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/drivers/char/mem.c b/drivers/char/mem.c
index cca4529431f8..e2bde4ad5677 100644
--- a/drivers/char/mem.c
+++ b/drivers/char/mem.c
@@ -573,9 +573,21 @@ static ssize_t write_full(struct file *file, const char __user *buf,
  * Special lseek() function for /dev/null and /dev/zero.  Most notably, you
  * can fopen() both devices with "a" now.  This was previously impossible.
  * -- SRB.
+ *
+ * For SEEK_DATA and SEEK_HOLE, return an error. Otherwise, userland conforming
+ * to the POSIX spec could end up in an infinite loop.
  */
 static loff_t null_lseek(struct file *file, loff_t offset, int orig)
 {
+	switch (orig) {
+	case SEEK_CUR:
+	case SEEK_SET:
+	case SEEK_END:
+		break;
+	default:
+		return -EINVAL;
+	}
+
 	return file->f_pos = 0;
 }
 
-- 
2.53.0.1.ga224b40d3f.dirty


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

end of thread, other threads:[~2026-03-22 13:20 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-03-20  4:17 [PATCH] drivers/char/mem.c: fix null and zero dev lseek David Timber
2026-03-20  6:02 ` Greg KH
2026-03-22 11:52   ` [PATCH v1] " David Timber
2026-03-22 11:52   ` [PATCH] " David Timber
2026-03-22 13:20     ` David Timber

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®