[PATCH] drivers/char/mem.c: fix null and zero dev lseek

From: David Timber

Date: Fri Mar 20 2026 - 00:17:47 EST


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@xxxxxxxxxxxx>
---
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