]> git.baikalelectronics.ru Git - kernel.git/commit
loop: access lo_backing_file only when the loop device is Lo_bound
authorDongli Zhang <dongli.zhang@oracle.com>
Mon, 18 Mar 2019 12:23:17 +0000 (20:23 +0800)
committerJens Axboe <axboe@kernel.dk>
Mon, 18 Mar 2019 14:20:53 +0000 (08:20 -0600)
commit308883506189187c18d6d3bcb8665060be57419d
treec788565eb16ff83ad2bd624a4487e4f8aeabfa9f
parent089c0a1bc01f7c2dfcba5a0e72b6a3b31069a5ed
loop: access lo_backing_file only when the loop device is Lo_bound

Commit 95ce40e5aa8f ("loop: set GENHD_FL_NO_PART_SCAN after
blkdev_reread_part()") separates "lo->lo_backing_file = NULL" and
"lo->lo_state = Lo_unbound" into different critical regions protected by
loop_ctl_mutex.

However, there is below race that the NULL lo->lo_backing_file would be
accessed when the backend of a loop is another loop device, e.g., loop0's
backend is a file, while loop1's backend is loop0.

loop0's backend is file            loop1's backend is loop0

__loop_clr_fd()
  mutex_lock(&loop_ctl_mutex);
  lo->lo_backing_file = NULL; --> set to NULL
  mutex_unlock(&loop_ctl_mutex);
                                   loop_set_fd()
                                     mutex_lock_killable(&loop_ctl_mutex);
                                     loop_validate_file()
                                       f = l->lo_backing_file; --> NULL
                                         access if loop0 is not Lo_unbound
  mutex_lock(&loop_ctl_mutex);
  lo->lo_state = Lo_unbound;
  mutex_unlock(&loop_ctl_mutex);

lo->lo_backing_file should be accessed only when the loop device is
Lo_bound.

In fact, the problem has been introduced already in commit 289105e740ad
("loop: Push loop_ctl_mutex down into loop_clr_fd()") after which
loop_validate_file() could see devices in Lo_rundown state with which it
did not count. It was harmless at that point but still.

Fixes: 289105e740ad ("loop: Push loop_ctl_mutex down into loop_clr_fd()")
Reported-by: syzbot+9bdc1adc1c55e7fe765b@syzkaller.appspotmail.com
Signed-off-by: Dongli Zhang <dongli.zhang@oracle.com>
Reviewed-by: Jan Kara <jack@suse.cz>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
drivers/block/loop.c