aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp>2019-08-26 09:13:25 -0400
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>2019-09-04 07:31:28 -0400
commit8619e5bdeee8b2c685d686281f2d2a6017c4bc15 (patch)
tree283230edb902e413d58a9b51f2e938d70aebb134
parent4feb80faf428a02d407a9ea1952004af01308765 (diff)
/dev/mem: Bail out upon SIGKILL.
syzbot found that a thread can stall for minutes inside read_mem() or write_mem() after that thread was killed by SIGKILL [1]. Reading from iomem areas of /dev/mem can be slow, depending on the hardware. While reading 2GB at one read() is legal, delaying termination of killed thread for minutes is bad. Thus, allow reading/writing /dev/mem and /dev/kmem to be preemptible and killable. [ 1335.912419][T20577] read_mem: sz=4096 count=2134565632 [ 1335.943194][T20577] read_mem: sz=4096 count=2134561536 [ 1335.978280][T20577] read_mem: sz=4096 count=2134557440 [ 1336.011147][T20577] read_mem: sz=4096 count=2134553344 [ 1336.041897][T20577] read_mem: sz=4096 count=2134549248 Theoretically, reading/writing /dev/mem and /dev/kmem can become "interruptible". But this patch chose "killable". Future patch will make them "interruptible" so that we can revert to "killable" if some program regressed. [1] https://syzkaller.appspot.com/bug?id=a0e3436829698d5824231251fad9d8e998f94f5e Signed-off-by: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp> Cc: stable <stable@vger.kernel.org> Reported-by: syzbot <syzbot+8ab2d0f39fb79fe6ca40@syzkaller.appspotmail.com> Link: https://lore.kernel.org/r/1566825205-10703-1-git-send-email-penguin-kernel@I-love.SAKURA.ne.jp Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
-rw-r--r--drivers/char/mem.c21
1 files changed, 21 insertions, 0 deletions
diff --git a/drivers/char/mem.c b/drivers/char/mem.c
index b08dc50f9f26..9eb564c002f6 100644
--- a/drivers/char/mem.c
+++ b/drivers/char/mem.c
@@ -97,6 +97,13 @@ void __weak unxlate_dev_mem_ptr(phys_addr_t phys, void *addr)
97} 97}
98#endif 98#endif
99 99
100static inline bool should_stop_iteration(void)
101{
102 if (need_resched())
103 cond_resched();
104 return fatal_signal_pending(current);
105}
106
100/* 107/*
101 * This funcion reads the *physical* memory. The f_pos points directly to the 108 * This funcion reads the *physical* memory. The f_pos points directly to the
102 * memory location. 109 * memory location.
@@ -175,6 +182,8 @@ static ssize_t read_mem(struct file *file, char __user *buf,
175 p += sz; 182 p += sz;
176 count -= sz; 183 count -= sz;
177 read += sz; 184 read += sz;
185 if (should_stop_iteration())
186 break;
178 } 187 }
179 kfree(bounce); 188 kfree(bounce);
180 189
@@ -251,6 +260,8 @@ static ssize_t write_mem(struct file *file, const char __user *buf,
251 p += sz; 260 p += sz;
252 count -= sz; 261 count -= sz;
253 written += sz; 262 written += sz;
263 if (should_stop_iteration())
264 break;
254 } 265 }
255 266
256 *ppos += written; 267 *ppos += written;
@@ -468,6 +479,10 @@ static ssize_t read_kmem(struct file *file, char __user *buf,
468 read += sz; 479 read += sz;
469 low_count -= sz; 480 low_count -= sz;
470 count -= sz; 481 count -= sz;
482 if (should_stop_iteration()) {
483 count = 0;
484 break;
485 }
471 } 486 }
472 } 487 }
473 488
@@ -492,6 +507,8 @@ static ssize_t read_kmem(struct file *file, char __user *buf,
492 buf += sz; 507 buf += sz;
493 read += sz; 508 read += sz;
494 p += sz; 509 p += sz;
510 if (should_stop_iteration())
511 break;
495 } 512 }
496 free_page((unsigned long)kbuf); 513 free_page((unsigned long)kbuf);
497 } 514 }
@@ -544,6 +561,8 @@ static ssize_t do_write_kmem(unsigned long p, const char __user *buf,
544 p += sz; 561 p += sz;
545 count -= sz; 562 count -= sz;
546 written += sz; 563 written += sz;
564 if (should_stop_iteration())
565 break;
547 } 566 }
548 567
549 *ppos += written; 568 *ppos += written;
@@ -595,6 +614,8 @@ static ssize_t write_kmem(struct file *file, const char __user *buf,
595 buf += sz; 614 buf += sz;
596 virtr += sz; 615 virtr += sz;
597 p += sz; 616 p += sz;
617 if (should_stop_iteration())
618 break;
598 } 619 }
599 free_page((unsigned long)kbuf); 620 free_page((unsigned long)kbuf);
600 } 621 }