Skip to content

Commit 148a1bc

Browse files
rmurphy-armgregkh
authored andcommitted
drivers: char: mem: Check {read,write}_kmem() addresses
Arriving at read_kmem() with an offset representing a bogus kernel address (e.g. 0 from a simple "cat /dev/kmem") leads to copy_to_user faulting on the kernel-side read. x86_64 happens to get away with this since the optimised implementation uses "rep movs*", thus the user write (which is allowed to fault) and the kernel read are the same instruction, the kernel-side fault falls into the user-side fixup handler and the chain of events which transpires ends up returning an error as one might expect, even if it's an inappropriate -EFAULT. On other architectures, though, the read is not covered by the fixup entry for the write, and we get a big scary "Unable to hande kernel paging request..." dump. The more typical use-case of mmap_kmem() has always (within living memory at least) returned -EIO for addresses which don't satisfy pfn_valid(), so let's make that consistent across {read,write}_kem() too. Reported-by: Kefeng Wang <wangkefeng.wang@huawei.com> Signed-off-by: Robin Murphy <robin.murphy@arm.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent d61f308 commit 148a1bc

1 file changed

Lines changed: 6 additions & 0 deletions

File tree

‎drivers/char/mem.c‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,9 @@ 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+
384387
read = 0;
385388
if (p < (unsigned long) high_memory) {
386389
low_count = count;
@@ -509,6 +512,9 @@ static ssize_t write_kmem(struct file *file, const char __user *buf,
509512
char *kbuf; /* k-addr because vwrite() takes vmlist_lock rwlock */
510513
int err = 0;
511514

515+
if (!pfn_valid(PFN_DOWN(p)))
516+
return -EIO;
517+
512518
if (p < (unsigned long) high_memory) {
513519
unsigned long to_write = min_t(unsigned long, count,
514520
(unsigned long)high_memory - p);

0 commit comments

Comments
 (0)