Skip to content

Commit 488debb

Browse files
rmurphy-armgregkh
authored andcommitted
drivers: char: mem: Fix thinkos in kmem address checks
When borrowing the pfn_valid() check from mmap_kmem(), somebody managed to get physical and virtual addresses spectacularly muddled up, such that we've ended up with checks for one being the other. Whilst this does indeed prevent out-of-bounds accesses crashing, on most systems it also prevents the more desirable use-case of working at all ever. Check the *virtual* offset correctly for what it is. Furthermore, do so in the right place - a read or write may span multiple pages, so a single up-front check is insufficient. High memory accesses already have a similar validity check just before the copy_to_user() call, so just make the low memory path fully consistent with that. Reported-by: Jason A. Donenfeld <Jason@zx2c4.com> CC: stable@vger.kernel.org Fixes: 148a1bc ("drivers: char: mem: Check {read,write}_kmem() addresses") Signed-off-by: Robin Murphy <robin.murphy@arm.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 7ee7f45 commit 488debb

1 file changed

Lines changed: 4 additions & 6 deletions

File tree

‎drivers/char/mem.c‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -381,9 +381,6 @@ static ssize_t read_kmem(struct file *file, char __user *buf,
381381
char *kbuf; /* k-addr because vread() takes vmlist_lock rwlock */
382382
int err = 0;
383383

384-
if (!pfn_valid(PFN_DOWN(p)))
385-
return -EIO;
386-
387384
read = 0;
388385
if (p < (unsigned long) high_memory) {
389386
low_count = count;
@@ -412,6 +409,8 @@ static ssize_t read_kmem(struct file *file, char __user *buf,
412409
* by the kernel or data corruption may occur
413410
*/
414411
kbuf = xlate_dev_kmem_ptr((void *)p);
412+
if (!virt_addr_valid(kbuf))
413+
return -ENXIO;
415414

416415
if (copy_to_user(buf, kbuf, sz))
417416
return -EFAULT;
@@ -482,6 +481,8 @@ static ssize_t do_write_kmem(unsigned long p, const char __user *buf,
482481
* corruption may occur.
483482
*/
484483
ptr = xlate_dev_kmem_ptr((void *)p);
484+
if (!virt_addr_valid(ptr))
485+
return -ENXIO;
485486

486487
copied = copy_from_user(ptr, buf, sz);
487488
if (copied) {
@@ -512,9 +513,6 @@ static ssize_t write_kmem(struct file *file, const char __user *buf,
512513
char *kbuf; /* k-addr because vwrite() takes vmlist_lock rwlock */
513514
int err = 0;
514515

515-
if (!pfn_valid(PFN_DOWN(p)))
516-
return -EIO;
517-
518516
if (p < (unsigned long) high_memory) {
519517
unsigned long to_write = min_t(unsigned long, count,
520518
(unsigned long)high_memory - p);

0 commit comments

Comments
 (0)