| From 8619e5bdeee8b2c685d686281f2d2a6017c4bc15 Mon Sep 17 00:00:00 2001 |
| From: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp> |
| Date: Mon, 26 Aug 2019 22:13:25 +0900 |
| Subject: /dev/mem: Bail out upon SIGKILL. |
| |
| From: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp> |
| |
| commit 8619e5bdeee8b2c685d686281f2d2a6017c4bc15 upstream. |
| |
| 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> |
| Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> |
| |
| --- |
| drivers/char/mem.c | 21 +++++++++++++++++++++ |
| 1 file changed, 21 insertions(+) |
| |
| --- a/drivers/char/mem.c |
| +++ b/drivers/char/mem.c |
| @@ -96,6 +96,13 @@ void __weak unxlate_dev_mem_ptr(phys_add |
| } |
| #endif |
| |
| +static inline bool should_stop_iteration(void) |
| +{ |
| + if (need_resched()) |
| + cond_resched(); |
| + return fatal_signal_pending(current); |
| +} |
| + |
| /* |
| * This funcion reads the *physical* memory. The f_pos points directly to the |
| * memory location. |
| @@ -162,6 +169,8 @@ static ssize_t read_mem(struct file *fil |
| p += sz; |
| count -= sz; |
| read += sz; |
| + if (should_stop_iteration()) |
| + break; |
| } |
| |
| *ppos += read; |
| @@ -233,6 +242,8 @@ static ssize_t write_mem(struct file *fi |
| p += sz; |
| count -= sz; |
| written += sz; |
| + if (should_stop_iteration()) |
| + break; |
| } |
| |
| *ppos += written; |
| @@ -446,6 +457,10 @@ static ssize_t read_kmem(struct file *fi |
| read += sz; |
| low_count -= sz; |
| count -= sz; |
| + if (should_stop_iteration()) { |
| + count = 0; |
| + break; |
| + } |
| } |
| } |
| |
| @@ -470,6 +485,8 @@ static ssize_t read_kmem(struct file *fi |
| buf += sz; |
| read += sz; |
| p += sz; |
| + if (should_stop_iteration()) |
| + break; |
| } |
| free_page((unsigned long)kbuf); |
| } |
| @@ -522,6 +539,8 @@ static ssize_t do_write_kmem(unsigned lo |
| p += sz; |
| count -= sz; |
| written += sz; |
| + if (should_stop_iteration()) |
| + break; |
| } |
| |
| *ppos += written; |
| @@ -573,6 +592,8 @@ static ssize_t write_kmem(struct file *f |
| buf += sz; |
| virtr += sz; |
| p += sz; |
| + if (should_stop_iteration()) |
| + break; |
| } |
| free_page((unsigned long)kbuf); |
| } |