950-0842-f2fs-fix-to-avoid-NULL-pointer-dereference-in-f2fs_i.patch 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. From bf9fb25f3265605572f04e5c7836bb83ee345236 Mon Sep 17 00:00:00 2001
  2. From: Chao Yu <[email protected]>
  3. Date: Fri, 30 Dec 2022 23:43:32 +0800
  4. Subject: [PATCH] f2fs: fix to avoid NULL pointer dereference in
  5. f2fs_issue_flush()
  6. commit b3d83066cbebc76dbac8a5fca931f64b4c6fff34 upstream.
  7. With below two cases, it will cause NULL pointer dereference when
  8. accessing SM_I(sbi)->fcc_info in f2fs_issue_flush().
  9. a) If kthread_run() fails in f2fs_create_flush_cmd_control(), it will
  10. release SM_I(sbi)->fcc_info,
  11. - mount -o noflush_merge /dev/vda /mnt/f2fs
  12. - mount -o remount,flush_merge /dev/vda /mnt/f2fs -- kthread_run() fails
  13. - dd if=/dev/zero of=/mnt/f2fs/file bs=4k count=1 conv=fsync
  14. b) we will never allocate memory for SM_I(sbi)->fcc_info w/ below
  15. testcase,
  16. - mount -o ro /dev/vda /mnt/f2fs
  17. - mount -o rw,remount /dev/vda /mnt/f2fs
  18. - dd if=/dev/zero of=/mnt/f2fs/file bs=4k count=1 conv=fsync
  19. In order to fix this issue, let change as below:
  20. - fix error path handling in f2fs_create_flush_cmd_control().
  21. - allocate SM_I(sbi)->fcc_info even if readonly is on.
  22. Signed-off-by: Chao Yu <[email protected]>
  23. Signed-off-by: Jaegeuk Kim <[email protected]>
  24. ---
  25. fs/f2fs/segment.c | 12 ++++--------
  26. 1 file changed, 4 insertions(+), 8 deletions(-)
  27. --- a/fs/f2fs/segment.c
  28. +++ b/fs/f2fs/segment.c
  29. @@ -665,9 +665,7 @@ init_thread:
  30. "f2fs_flush-%u:%u", MAJOR(dev), MINOR(dev));
  31. if (IS_ERR(fcc->f2fs_issue_flush)) {
  32. err = PTR_ERR(fcc->f2fs_issue_flush);
  33. - kfree(fcc);
  34. - SM_I(sbi)->fcc_info = NULL;
  35. - return err;
  36. + fcc->f2fs_issue_flush = NULL;
  37. }
  38. return err;
  39. @@ -5064,11 +5062,9 @@ int f2fs_build_segment_manager(struct f2
  40. init_f2fs_rwsem(&sm_info->curseg_lock);
  41. - if (!f2fs_readonly(sbi->sb)) {
  42. - err = f2fs_create_flush_cmd_control(sbi);
  43. - if (err)
  44. - return err;
  45. - }
  46. + err = f2fs_create_flush_cmd_control(sbi);
  47. + if (err)
  48. + return err;
  49. err = create_discard_cmd_control(sbi);
  50. if (err)