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

David Timber posted 1 patch 1 week, 5 days ago
drivers/char/mem.c | 14 +++++++++++++-
1 file changed, 13 insertions(+), 1 deletion(-)
[PATCH v1] drivers/char/mem.c: fix null and zero dev lseek
Posted by David Timber 1 week, 5 days ago
Return -EINVAL if whence is not understood by /dev/null or /dev/zero.

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 calculated 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 | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)

diff --git a/drivers/char/mem.c b/drivers/char/mem.c
index cca4529431f8..f0318e924db9 100644
--- a/drivers/char/mem.c
+++ b/drivers/char/mem.c
@@ -576,7 +576,19 @@ static ssize_t write_full(struct file *file, const char __user *buf,
  */
 static loff_t null_lseek(struct file *file, loff_t offset, int orig)
 {
-	return file->f_pos = 0;
+	loff_t ret;
+
+	switch (orig) {
+	case SEEK_SET:
+	case SEEK_CUR:
+	case SEEK_END:
+		ret = file->f_pos = 0;
+		break;
+	default:
+		ret = -EINVAL;
+	}
+
+	return ret;
 }
 
 /*
-- 
2.53.0.1.ga224b40d3f.dirty