[PATCH 01/10] Return EOF on out-of-bounds read from /dev/mem

From: Petr Tesarik
Date: Fri Jun 17 2011 - 04:57:41 EST

The off parameter (type loff_t) may specify an offset that cannot
be represented by a long. Currently, /dev/mem wraps around, which
may to cause applications to read/write incorrect regions of memory
by accident.

Follow the usual file semantics here and return 0 when reading or
-EFBIG when writing beyond the accessible range.

Signed-off-by: Petr Tesarik <ptesarik@xxxxxxx>
drivers/char/mem.c | 6 ++++++
1 files changed, 6 insertions(+), 0 deletions(-)

diff --git a/drivers/char/mem.c b/drivers/char/mem.c
index 8fc04b4..f5cbd4e 100644
--- a/drivers/char/mem.c
+++ b/drivers/char/mem.c
@@ -97,6 +97,9 @@ static ssize_t read_mem(struct file *file, char __user *buf,
ssize_t read, sz;
char *ptr;

+ if (p != *ppos)
+ return 0;
if (!valid_phys_addr_range(p, count))
return -EFAULT;
read = 0;
@@ -155,6 +158,9 @@ static ssize_t write_mem(struct file *file, const char __user *buf,
unsigned long copied;
void *ptr;

+ if (p != *ppos)
+ return -EFBIG;
if (!valid_phys_addr_range(p, count))
return -EFAULT;

To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/