Skip to content

Commit 08d2d00

Browse files
Petr Tesarikgregkh
authored andcommitted
/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: Petr Tesarik <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 1bc9fac commit 08d2d00

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

drivers/char/mem.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@ static ssize_t read_mem(struct file *file, char __user *buf,
9999
ssize_t read, sz;
100100
char *ptr;
101101

102+
if (p != *ppos)
103+
return 0;
104+
102105
if (!valid_phys_addr_range(p, count))
103106
return -EFAULT;
104107
read = 0;
@@ -157,6 +160,9 @@ static ssize_t write_mem(struct file *file, const char __user *buf,
157160
unsigned long copied;
158161
void *ptr;
159162

163+
if (p != *ppos)
164+
return -EFBIG;
165+
160166
if (!valid_phys_addr_range(p, count))
161167
return -EFAULT;
162168

0 commit comments

Comments
 (0)