f2fs: fix NULL pointer dereference in f2fs_verity_work()
authorChao Yu <yuchao0@huawei.com>
Mon, 23 Mar 2020 09:43:04 +0000 (17:43 +0800)
committerJaegeuk Kim <jaegeuk@kernel.org>
Tue, 31 Mar 2020 03:46:25 +0000 (20:46 -0700)
If both compression and fsverity feature is on, generic/572 will
report below NULL pointer dereference bug.

 BUG: kernel NULL pointer dereference, address: 0000000000000018
 RIP: 0010:f2fs_verity_work+0x60/0x90 [f2fs]
 #PF: supervisor read access in kernel mode
 Workqueue: fsverity_read_queue f2fs_verity_work [f2fs]
 RIP: 0010:f2fs_verity_work+0x60/0x90 [f2fs]
 Call Trace:
  process_one_work+0x16c/0x3f0
  worker_thread+0x4c/0x440
  ? rescuer_thread+0x350/0x350
  kthread+0xf8/0x130
  ? kthread_unpark+0x70/0x70
  ret_from_fork+0x35/0x40

There are two issue in f2fs_verity_work():
- it needs to traverse and verify all pages in bio.
- if pages in bio belong to non-compressed cluster, accessing
decompress IO context stored in page private will cause NULL
pointer dereference.

Fix them.

Signed-off-by: Chao Yu <yuchao0@huawei.com>
Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
fs/f2fs/compress.c
fs/f2fs/data.c

index d66d372d104dc97bcba12c128b887f052bed18d2..adac6fcb2830ee4667679cddcac99c163cfe33fc 100644 (file)
@@ -477,6 +477,8 @@ out_vunmap_cbuf:
 out_vunmap_rbuf:
        vunmap(dic->rbuf);
 out_free_dic:
+       if (verity)
+               refcount_add(dic->nr_cpages - 1, &dic->ref);
        if (!verity)
                f2fs_decompress_end_io(dic->rpages, dic->cluster_size,
                                                                ret, false);
index bb66faf09eeaffeb399ce2ccad111c2b07b80985..0197b7b80d191736f8041fa87576110ad51ba399 100644 (file)
@@ -187,12 +187,37 @@ static void f2fs_verify_pages(struct page **rpages, unsigned int cluster_size)
 
 static void f2fs_verify_bio(struct bio *bio)
 {
-       struct page *page = bio_first_page_all(bio);
-       struct decompress_io_ctx *dic =
-                       (struct decompress_io_ctx *)page_private(page);
+       struct bio_vec *bv;
+       struct bvec_iter_all iter_all;
+
+       bio_for_each_segment_all(bv, bio, iter_all) {
+               struct page *page = bv->bv_page;
+               struct decompress_io_ctx *dic;
+
+               dic = (struct decompress_io_ctx *)page_private(page);
+
+               if (dic) {
+                       if (refcount_dec_not_one(&dic->ref))
+                               continue;
+                       f2fs_verify_pages(dic->rpages,
+                                               dic->cluster_size);
+                       f2fs_free_dic(dic);
+                       continue;
+               }
+
+               if (bio->bi_status || PageError(page))
+                       goto clear_uptodate;
 
-       f2fs_verify_pages(dic->rpages, dic->cluster_size);
-       f2fs_free_dic(dic);
+               if (fsverity_verify_page(page)) {
+                       SetPageUptodate(page);
+                       goto unlock;
+               }
+clear_uptodate:
+               ClearPageUptodate(page);
+               ClearPageError(page);
+unlock:
+               unlock_page(page);
+       }
 }
 #endif