Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

/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>

authored by

Tetsuo Handa and committed by
Greg Kroah-Hartman
8619e5bd 4feb80fa

+21
+21
drivers/char/mem.c
··· 97 97 } 98 98 #endif 99 99 100 + static 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 108 * This funcion reads the *physical* memory. The f_pos points directly to the 102 109 * memory location. ··· 182 175 p += sz; 183 176 count -= sz; 184 177 read += sz; 178 + if (should_stop_iteration()) 179 + break; 185 180 } 186 181 kfree(bounce); 187 182 ··· 260 251 p += sz; 261 252 count -= sz; 262 253 written += sz; 254 + if (should_stop_iteration()) 255 + break; 263 256 } 264 257 265 258 *ppos += written; ··· 479 468 read += sz; 480 469 low_count -= sz; 481 470 count -= sz; 471 + if (should_stop_iteration()) { 472 + count = 0; 473 + break; 474 + } 482 475 } 483 476 } 484 477 ··· 507 492 buf += sz; 508 493 read += sz; 509 494 p += sz; 495 + if (should_stop_iteration()) 496 + break; 510 497 } 511 498 free_page((unsigned long)kbuf); 512 499 } ··· 561 544 p += sz; 562 545 count -= sz; 563 546 written += sz; 547 + if (should_stop_iteration()) 548 + break; 564 549 } 565 550 566 551 *ppos += written; ··· 614 595 buf += sz; 615 596 virtr += sz; 616 597 p += sz; 598 + if (should_stop_iteration()) 599 + break; 617 600 } 618 601 free_page((unsigned long)kbuf); 619 602 }