Skip to contentSkip to conversation

mtdchar: fix overflows in adjustment of `count`

PATCH Applied Jann Horn 2 participants 5 messages Jul 7, 2018 – Jul 18, 2018

Subsystems (2)
lore
Share & tools
lore

Jann Horn

author
#0 lore raw

PATCH 1/1 mtdchar: fix overflows in adjustment of `count`

The first checks in mtdchar_read() and mtdchar_write() attempt to limit count such that *ppos + count <= mtd->size. However, they ignore the possibility of *ppos > mtd->size, allowing the calculation of count to wrap around. mtdchar_lseek() prevents seeking beyond mtd->size, but the pread/pwrite syscalls bypass this.

I haven't found any codepath on which this actually causes dangerous behavior, but it seems like a sensible change anyway.

Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")
---
 drivers/mtd/mtdchar.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
drivers/mtd/mtdchar.c +7 -3
index cd67c85cc87d..02389528f622 100644
@@ -160,8 +160,12 @@ static ssize_t mtdchar_read(struct file *file, char __user *buf, size_t count,
 
 	pr_debug("MTD_read\n");
 
-	if (*ppos + count > mtd->size)
-		count = mtd->size - *ppos;
+	if (*ppos + count > mtd->size) {
+		if (*ppos < mtd->size)
+			count = mtd->size - *ppos;
+		else
+			count = 0;
+	}
 
 	if (!count)
 		return 0;
@@ -246,7 +250,7 @@ static ssize_t mtdchar_write(struct file *file, const char __user *buf, size_t c
 
 	pr_debug("MTD_write\n");
 
-	if (*ppos == mtd->size)
+	if (*ppos >= mtd->size)
 		return -ENOSPC;
 
 	if (*ppos + count > mtd->size)
Signature · 1 line
2.18.0.203.gfac676dfb9-goog
#1 lore raw
Quoted 21 lines · On Sat, 7 Jul 2018 05:37:22 +0200 Jann Horn <[email protected]> wrote:

The first checks in mtdchar_read() and mtdchar_write() attempt to limit count such that *ppos + count <= mtd->size. However, they ignore the possibility of *ppos > mtd->size, allowing the calculation of count to wrap around. mtdchar_lseek() prevents seeking beyond mtd->size, but the pread/pwrite syscalls bypass this.

I haven't found any codepath on which this actually causes dangerous behavior, but it seems like a sensible change anyway.

Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")
---
 drivers/mtd/mtdchar.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
drivers/mtd/mtdchar.c +0 -0
index cd67c85cc87d..02389528f622 100644
@@ -160,8 +160,12 @@ static ssize_t mtdchar_read(struct file *file, char __user *buf, size_t count,
 

drivers/mtd/mtdchar.c static ssize_t mtdchar_read(struct file *file, char __user *buf, size_t count, · line 168

Quoted 10 lines
drivers/mtd/mtdchar.c +6 -2
 	pr_debug("MTD_read\n");
 
-	if (*ppos + count > mtd->size)
-		count = mtd->size - *ppos;
+	if (*ppos + count > mtd->size) {
+		if (*ppos < mtd->size)
+			count = mtd->size - *ppos;
+		else
+			count = 0;
+	}

Hm, shouldn't we return -ERANGE or -EINVAL if *ppos >= mtd->size?

Quoted 12 lines
drivers/mtd/mtdchar.c +1 -1
 	if (!count)
 		return 0;
@@ -246,7 +250,7 @@ static ssize_t mtdchar_write(struct file *file, const char __user *buf, size_t c
 
 	pr_debug("MTD_write\n");
 
-	if (*ppos == mtd->size)
+	if (*ppos >= mtd->size)
 		return -ENOSPC;
 
 	if (*ppos + count > mtd->size)
author
#2 lore raw

+cc linux-api

Quoted 27 lines · Boris Brezillon
Quoted 23 lines · On Sat, 7 Jul 2018 05:37:22 +0200 Jann Horn <[email protected]> wrote:

The first checks in mtdchar_read() and mtdchar_write() attempt to limit count such that *ppos + count <= mtd->size. However, they ignore the possibility of *ppos > mtd->size, allowing the calculation of count to wrap around. mtdchar_lseek() prevents seeking beyond mtd->size, but the pread/pwrite syscalls bypass this.

I haven't found any codepath on which this actually causes dangerous behavior, but it seems like a sensible change anyway.

Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")
---
 drivers/mtd/mtdchar.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
drivers/mtd/mtdchar.c +0 -0
index cd67c85cc87d..02389528f622 100644
@@ -160,8 +160,12 @@ static ssize_t mtdchar_read(struct file *file, char __user *buf, size_t count,
 
      pr_debug("MTD_read\n");
 
Quoted 10 lines

drivers/mtd/mtdchar.c static ssize_t mtdchar_read(struct file *file, char __user *buf, size_t count, · line 168

Quoted 8 lines
drivers/mtd/mtdchar.c +6 -2
-     if (*ppos + count > mtd->size)
-             count = mtd->size - *ppos;
+     if (*ppos + count > mtd->size) {
+             if (*ppos < mtd->size)
+                     count = mtd->size - *ppos;
+             else
+                     count = 0;
+     }

Hm, shouldn't we return -ERANGE or -EINVAL if *ppos >= mtd->size?

Hmm, good question.
The pread() manpage says that pread() can return the same errors as lseek(), and the lseek() manpage says that -EINVAL is for when you're trying to seek beyond the end of a seekable device. So from the documentation, it sounds as if you're right.
But testing pread() beyond end of file for various things on my machine seems to just return 0:

# cat pread.c
#include <unistd.h>
#include <stdlib.h>
int main(int argc, char **argv) {
  char buf[0x1000];
  off_t off = strtoul(argv[1], NULL, 0);
  pread(0, buf, 0x1000, off);
}
# gcc -o pread pread.c
# strace -e trace=pread64 ./pread 200000000 < /dev/sda1
pread64(0, "", 4096, 200000000)         = 0
+++ exited with 0 +++
# strace -e trace=pread64 ./pread 100000 <
/sys/kernel/debug/x86/tlb_single_page_flush_ceiling
pread64(0, "", 4096, 100000)            = 0
+++ exited with 0 +++
# strace -e trace=pread64 ./pread 20000000000 < /dev/dm-2
pread64(0, "", 4096, 20000000000)       = 0
+++ exited with 0 +++

Do you know of precedent for returning -EINVAL if *ppos is beyond the end of the device?

Quoted 5 lines
Quoted 5 lines
drivers/mtd/mtdchar.c +0 -0
      if (!count)
              return 0;
@@ -246,7 +250,7 @@ static ssize_t mtdchar_write(struct file *file, const char __user *buf, size_t c
 
Quoted 8 lines

drivers/mtd/mtdchar.c static ssize_t mtdchar_write(struct file *file, const char __user *buf, size_t c · line 256

Quoted 7 lines
drivers/mtd/mtdchar.c +1 -1
      pr_debug("MTD_write\n");
 
-     if (*ppos == mtd->size)
+     if (*ppos >= mtd->size)
              return -ENOSPC;
 
      if (*ppos + count > mtd->size)

______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

#3 lore raw
Quoted 70 lines · On Sat, 7 Jul 2018 11:03:00 +0200 Jann Horn <[email protected]> wrote:

+cc linux-api

Quoted 27 lines · Boris Brezillon
Quoted 23 lines · On Sat, 7 Jul 2018 05:37:22 +0200 Jann Horn <[email protected]> wrote:

The first checks in mtdchar_read() and mtdchar_write() attempt to limit count such that *ppos + count <= mtd->size. However, they ignore the possibility of *ppos > mtd->size, allowing the calculation of count to wrap around. mtdchar_lseek() prevents seeking beyond mtd->size, but the pread/pwrite syscalls bypass this.

I haven't found any codepath on which this actually causes dangerous behavior, but it seems like a sensible change anyway.

Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")
---
 drivers/mtd/mtdchar.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
drivers/mtd/mtdchar.c +0 -0
index cd67c85cc87d..02389528f622 100644
@@ -160,8 +160,12 @@ static ssize_t mtdchar_read(struct file *file, char __user *buf, size_t count,
 
      pr_debug("MTD_read\n");
 
Quoted 10 lines

drivers/mtd/mtdchar.c static ssize_t mtdchar_read(struct file *file, char __user *buf, size_t count, · line 168

Quoted 8 lines
drivers/mtd/mtdchar.c +6 -2
-     if (*ppos + count > mtd->size)
-             count = mtd->size - *ppos;
+     if (*ppos + count > mtd->size) {
+             if (*ppos < mtd->size)
+                     count = mtd->size - *ppos;
+             else
+                     count = 0;
+     }  

Hm, shouldn't we return -ERANGE or -EINVAL if *ppos >= mtd->size?

Hmm, good question.
The pread() manpage says that pread() can return the same errors as lseek(), and the lseek() manpage says that -EINVAL is for when you're trying to seek beyond the end of a seekable device. So from the documentation, it sounds as if you're right.
But testing pread() beyond end of file for various things on my machine seems to just return 0:

# cat pread.c
#include <unistd.h>
#include <stdlib.h>
int main(int argc, char **argv) {
  char buf[0x1000];
  off_t off = strtoul(argv[1], NULL, 0);
  pread(0, buf, 0x1000, off);
}
# gcc -o pread pread.c
# strace -e trace=pread64 ./pread 200000000 < /dev/sda1
pread64(0, "", 4096, 200000000)         = 0
+++ exited with 0 +++
# strace -e trace=pread64 ./pread 100000 <
/sys/kernel/debug/x86/tlb_single_page_flush_ceiling
pread64(0, "", 4096, 100000)            = 0
+++ exited with 0 +++
# strace -e trace=pread64 ./pread 20000000000 < /dev/dm-2
pread64(0, "", 4096, 20000000000)       = 0
+++ exited with 0 +++
Quoted 2 lines

Do you know of precedent for returning -EINVAL if *ppos is beyond the end of the device?

Nope, it just made more sense to me than returning 0. Anyway, let's keep the most common behavior, even if it's not documented this way ;-).

Quoted 18 lines
Quoted 6 lines
Quoted 6 lines
drivers/mtd/mtdchar.c +0 -0
      if (!count)
              return 0;
@@ -246,7 +250,7 @@ static ssize_t mtdchar_write(struct file *file, const char __user *buf, size_t c
 
Quoted 8 lines

drivers/mtd/mtdchar.c static ssize_t mtdchar_write(struct file *file, const char __user *buf, size_t c · line 256

Quoted 7 lines
drivers/mtd/mtdchar.c +1 -1
      pr_debug("MTD_write\n");
 
-     if (*ppos == mtd->size)
+     if (*ppos >= mtd->size)
              return -ENOSPC;
 
      if (*ppos + count > mtd->size)  

______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

#4 lore raw
In reply to Jann Horn
Quoted 11 lines · On Sat, 7 Jul 2018 05:37:22 +0200 Jann Horn <[email protected]> wrote:

The first checks in mtdchar_read() and mtdchar_write() attempt to limit count such that *ppos + count <= mtd->size. However, they ignore the possibility of *ppos > mtd->size, allowing the calculation of count to wrap around. mtdchar_lseek() prevents seeking beyond mtd->size, but the pread/pwrite syscalls bypass this.

I haven't found any codepath on which this actually causes dangerous behavior, but it seems like a sensible change anyway.

Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")

Applied.

Thanks,

Boris

Quoted 32 lines
---
 drivers/mtd/mtdchar.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
drivers/mtd/mtdchar.c +7 -3
index cd67c85cc87d..02389528f622 100644
@@ -160,8 +160,12 @@ static ssize_t mtdchar_read(struct file *file, char __user *buf, size_t count,
 
 	pr_debug("MTD_read\n");
 
-	if (*ppos + count > mtd->size)
-		count = mtd->size - *ppos;
+	if (*ppos + count > mtd->size) {
+		if (*ppos < mtd->size)
+			count = mtd->size - *ppos;
+		else
+			count = 0;
+	}
 
 	if (!count)
 		return 0;
@@ -246,7 +250,7 @@ static ssize_t mtdchar_write(struct file *file, const char __user *buf, size_t c
 
 	pr_debug("MTD_write\n");
 
-	if (*ppos == mtd->size)
+	if (*ppos >= mtd->size)
 		return -ENOSPC;
 
 	if (*ppos + count > mtd->size)