Commit 08d2d00b authored by Petr Tesarik's avatar Petr Tesarik Committed by Greg Kroah-Hartman

/dev/mem: handle out-of-bounds read/write

The loff_t type may be wider than phys_addr_t (e.g. on 32-bit systems).
Consequently, the file offset may be truncated in the assignment.
Currently, /dev/mem wraps around, which may cause applications to read
or write incorrect regions of memory by accident.

Let's follow POSIX file semantics here and return 0 when reading from
and -EFBIG when writing to an offset that cannot be represented by a
phys_addr_t.

Note that the conditional is optimized out by the compiler if loff_t
has the same size as phys_addr_t.
Signed-off-by: default avatarPetr Tesarik <ptesarik@suse.cz>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
parent 1bc9fac3
...@@ -99,6 +99,9 @@ static ssize_t read_mem(struct file *file, char __user *buf, ...@@ -99,6 +99,9 @@ static ssize_t read_mem(struct file *file, char __user *buf,
ssize_t read, sz; ssize_t read, sz;
char *ptr; char *ptr;
if (p != *ppos)
return 0;
if (!valid_phys_addr_range(p, count)) if (!valid_phys_addr_range(p, count))
return -EFAULT; return -EFAULT;
read = 0; read = 0;
...@@ -157,6 +160,9 @@ static ssize_t write_mem(struct file *file, const char __user *buf, ...@@ -157,6 +160,9 @@ static ssize_t write_mem(struct file *file, const char __user *buf,
unsigned long copied; unsigned long copied;
void *ptr; void *ptr;
if (p != *ppos)
return -EFBIG;
if (!valid_phys_addr_range(p, count)) if (!valid_phys_addr_range(p, count))
return -EFAULT; return -EFAULT;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment