Skip to content

Commit

Permalink
/dev/mem: handle out-of-bounds read/write
Browse files Browse the repository at this point in the history
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: Petr Tesarik <ptesarik@suse.cz>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
  • Loading branch information
Petr Tesarik authored and Greg Kroah-Hartman committed Feb 15, 2014
1 parent 1bc9fac commit 08d2d00
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions drivers/char/mem.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,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;
Expand Down Expand Up @@ -157,6 +160,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;

Expand Down

0 comments on commit 08d2d00

Please sign in to comment.