Skip to contentSkip to conversation

ntfs: fix two kmap_local bugs on 32-bit kernels

PATCH Applied Karl Mehltretter 3 participants 5 messages Oct 6

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v2 cover ntfs: fix two kmap_local bugs on 32-bit kernels

Two kmap_local bugs in the mount path of fs/ntfs. Both only show on 32-bit kernels with HIGHMEM, where kmap_local_folio() hands out fixmap slots from a per task stack of 16 entries. multi_v7_defconfig enables both NTFS_FS and HIGHMEM, so the stock 32-bit ARM configuration is affected.

Patch 1: check_mft_mirror() unmaps pointers that were advanced past the end of the page. On 32-bit ARM this clears the wrong fixmap entry and the mount dies with a BUG a few calls later. syzkaller found it on a multi_v7_defconfig kernel. A fresh mkntfs volume reproduces it on the first mount.

Patch 2: ntfs_check_logfile() maps the same page once per loop iteration and unmaps it once, so a mount leaves three entries on the stack of the mounting task. After one mount the CPU it ran on can no longer be taken offline. A process that mounts ntfs volumes five times hits the BUG_ON() in kmap_local_idx_push(). x86-32 also warns when mount(2) returns.

The two fixes are independent of each other. Patch 2 was tested on top of patch 1. Both were tested on 32-bit ARM and x86-32 in QEMU. Details are below the --- line of each patch.

Changes in v2:
- v1 carried an older copy of patch 2 as a second 2/2 by mistake. The patches are unchanged.

v1:
https://lore.kernel.org/r/[email protected]/

Karl Mehltretter (2):
ntfs: fix kunmap_local() of advanced pointers in check_mft_mirror()
ntfs: fix kmap_local leak in ntfs_check_logfile()

 fs/ntfs/logfile.c | 11 ++++++-----
 fs/ntfs/super.c   | 20 +++++++++++---------
 2 files changed, 17 insertions(+), 14 deletions(-)

base-commit: 551c722f40809618230001baccf219193e22fc5a

Signature · 1 line
2.53.0
#1 lore raw

PATCH v2 1/2 ntfs: fix kunmap_local() of advanced pointers in check_mft_mirror()

check_mft_mirror() compares the mft records of a page with the mirror records by advancing kmft and kmirr by the record size for every record. It then passes the advanced pointers to kunmap_local(). After the last record of a page they point one page past the mapping.

With HIGHMEM kunmap_local() warns that the address does not match the top of the kmap_local stack. What it does next depends on how the architecture finds the page table entry:

- 32-bit ARM looks the entry up by that address. It clears the entry of the neighbouring fixmap slot and leaves the real mapping in place. The next kmap_local() on that CPU that reaches the slot hits the BUG_ON(!pte_none()) in __kmap_local_pfn_prot().
- x86-32 takes the entry from the stack index, so the wrong address does not select a wrong entry. In QEMU the mount carried on after the warning.

With 4 KiB pages and 1 KiB mft records the four mirror records fill exactly one page, so every mount whose $MFT and $MFTMirr folios are in highmem hits it. A fresh mkntfs volume on 32-bit ARM (multi_v7_defconfig, 256 MiB of highmem) fails on the first mount:

  WARNING: mm/highmem.c:623 at kunmap_local_indexed+0x254/0x26c, CPU#1: mount/85
   kunmap_local_indexed from ntfs_fill_super+0x224c/0x35a0
   ntfs_fill_super from get_tree_bdev_flags+0x1d8/0x2a4
  ntfs: volume version 3.1, dev loop0, cluster size 4096
  kernel BUG at mm/highmem.c:565!
  PC is at __kmap_local_pfn_prot+0x210/0x214
   __kmap_local_page_prot from ntfs_check_logfile+0x1fc/0x1210

Found with syzkaller. Kernels without HIGHMEM do not show it. kmap_local_folio() returns the direct map address there and kunmap_local() has no mapping to remove.

Keep the addresses returned by kmap_local_folio() and unmap with those.

Fixes 6251f0b0de7d ("ntfs: update super block operations")
Assisted-by LLM

---
Tested in QEMU on mainline 551c722f4080 with a fresh mkntfs volume and with the syzkaller program that found the bug.

ARM multi_v7_defconfig plus KCOV, virt, cortex-a15, 2 CPUs,
1 GiB RAM, 256 MiB highmem
x86-32 i386_defconfig plus HIGHMEM4G and NTFS_FS, q35, 2 CPUs,
2 GiB RAM, 1.1 GiB highmem

In the table, warning is the WARNING at mm/highmem.c:623 and BUG is the one at mm/highmem.c:565.

                          mainline            with this patch
  ARM, mount              warning, then BUG   3 mounts clean
  ARM, syzkaller program  warning, then BUG   3 runs clean
  x86-32, mount           warning, no BUG     warning gone

x86-32 still warns when mount(2) returns. That is the bug fixed by patch 2.

The other kmap_local users in fs/ntfs unmap an address inside the mapped page. Only check_mft_mirror() advances the pointer it later unmaps.

 fs/ntfs/super.c | 20 +++++++++++---------
 1 file changed, 11 insertions(+), 9 deletions(-)
fs/ntfs/super.c +11 -9
index 4066bacabe37..a53f9997c152 100644
@@ -959,7 +959,7 @@ static bool check_mft_mirror(struct ntfs_volume *vol)
 	struct super_block *sb = vol->sb;
 	struct ntfs_inode *mirr_ni;
 	struct folio *mft_folio = NULL, *mirr_folio = NULL;
-	u8 *kmft = NULL, *kmirr = NULL;
+	u8 *kmft = NULL, *kmirr = NULL, *kmft_base = NULL, *kmirr_base = NULL;
 	struct runlist_element *rl, rl2[2];
 	pgoff_t index;
 	int mrecs_per_page, i;
@@ -974,9 +974,9 @@ static bool check_mft_mirror(struct ntfs_volume *vol)
 		/* Switch pages if necessary. */
 		if (!(i % mrecs_per_page)) {
 			if (index) {
-				kunmap_local(kmirr);
+				kunmap_local(kmirr_base);
 				folio_put(mirr_folio);
-				kunmap_local(kmft);
+				kunmap_local(kmft_base);
 				folio_put(mft_folio);
 			}
 			/* Get the $MFT page. */
@@ -986,7 +986,8 @@ static bool check_mft_mirror(struct ntfs_volume *vol)
 				ntfs_error(sb, "Failed to read $MFT.");
 				return false;
 			}
-			kmft = kmap_local_folio(mft_folio, 0);
+			kmft_base = kmap_local_folio(mft_folio, 0);
+			kmft = kmft_base;
 			/* Get the $MFTMirr page. */
 			mirr_folio = read_mapping_folio(vol->mftmirr_ino->i_mapping,
 					index, NULL);
@@ -994,7 +995,8 @@ static bool check_mft_mirror(struct ntfs_volume *vol)
 				ntfs_error(sb, "Failed to read $MFTMirr.");
 				goto mft_unmap_out;
 			}
-			kmirr = kmap_local_folio(mirr_folio, 0);
+			kmirr_base = kmap_local_folio(mirr_folio, 0);
+			kmirr = kmirr_base;
 			++index;
 		}
 
@@ -1006,10 +1008,10 @@ static bool check_mft_mirror(struct ntfs_volume *vol)
 					"Incomplete multi sector transfer detected in mft record %i.",
 					i);
 mm_unmap_out:
-				kunmap_local(kmirr);
+				kunmap_local(kmirr_base);
 				folio_put(mirr_folio);
 mft_unmap_out:
-				kunmap_local(kmft);
+				kunmap_local(kmft_base);
 				folio_put(mft_folio);
 				return false;
 			}
@@ -1045,9 +1047,9 @@ static bool check_mft_mirror(struct ntfs_volume *vol)
 		kmirr += vol->mft_record_size;
 	} while (++i < vol->mftmirr_size);
 	/* Release the last folios. */
-	kunmap_local(kmirr);
+	kunmap_local(kmirr_base);
 	folio_put(mirr_folio);
-	kunmap_local(kmft);
+	kunmap_local(kmft_base);
 	folio_put(mft_folio);
 
 	/* Construct the mft mirror runlist by hand. */
Signature · 1 line
2.53.0
#2 lore raw

PATCH v2 2/2 ntfs: fix kmap_local leak in ntfs_check_logfile()

In reply to Karl Mehltretter

ntfs_check_logfile() calls kmap_local_folio() on every iteration of its search loop but kunmap_local() only once per folio. The loop visits positions 0, 512, 1024, 2048, 4096 and so on. With 4 KiB pages the first four are in one page, so a mount whose first LogFile folio is in highmem leaves three entries on the kmap_local stack of the mounting task. Kernels without HIGHMEM are not affected.

The leaked entries keep migration disabled. nr_pinned of the runqueue the mount ran on stays raised after the task exits, so taking that CPU offline blocks forever in balance_hotplug_wait().

The fifth mount in one task reaches KM_MAX_IDX (12 leaked entries plus 4 in the loop) and hits the BUG_ON() in kmap_local_idx_push():

kernel BUG at mm/highmem.c:480!

On x86-32 the generic entry code also warns on return to user space:

DEBUG_LOCKS_WARN_ON(current->kmap_ctrl.idx)

  WARNING: include/linux/highmem-internal.h:17 at __do_fast_syscall_32+0x21e/0x550

Map each page once, when it is read, and unmap that address.

Fixes 5218cd102aec ("ntfs: update misc operations")
Assisted-by LLM

---
Tested in QEMU on top of patch 1 with a fresh mkntfs volume, on the ARM and x86-32 kernels described there. ARM and x86-32 behave the same in all three tests.

- One mount on CPU 1 with taskset, then writing 0 to /sys/devices/system/cpu/cpu1/online: without this patch the write was still blocked after 60 seconds and new ssh logins hung. With it CPU 1 goes offline. The same write before the mount works on both kernels.
- A program that calls mount(2) and umount(2) eight times in one
process: without this patch mounts one to four succeed and the fifth hits the BUG_ON() in kmap_local_idx_push(). With it all eight complete.
- Three mounts in a row with mount(8) are clean. x86-32 no longer warns when mount(2) returns.

 fs/ntfs/logfile.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
fs/ntfs/logfile.c +6 -5
index 1404664dacc0..ce38ce87f8fc 100644
@@ -457,7 +457,7 @@ bool ntfs_check_logfile(struct inode *log_vi, struct restart_page_header **rp)
 	struct ntfs_volume *vol = NTFS_SB(log_vi->i_sb);
 	struct address_space *mapping = log_vi->i_mapping;
 	struct folio *folio = NULL;
-	u8 *kaddr = NULL;
+	u8 *kaddr = NULL, *kbase = NULL;
 	struct restart_page_header *rstr1_ph = NULL;
 	struct restart_page_header *rstr2_ph = NULL;
 	int log_page_size, err;
@@ -511,7 +511,7 @@ bool ntfs_check_logfile(struct inode *log_vi, struct restart_page_header **rp)
 
 		if (!folio || folio->index != idx) {
 			if (folio) {
-				kunmap_local(kaddr);
+				kunmap_local(kbase);
 				folio_put(folio);
 			}
 			folio = read_mapping_folio(mapping, idx, NULL);
@@ -520,8 +520,9 @@ bool ntfs_check_logfile(struct inode *log_vi, struct restart_page_header **rp)
 						idx);
 				goto err_out;
 			}
+			kbase = kmap_local_folio(folio, 0);
 		}
-		kaddr = (u8 *)kmap_local_folio(folio, 0) + (pos & ~PAGE_MASK);
+		kaddr = kbase + (pos & ~PAGE_MASK);
 		/*
 		 * A non-empty block means the logfile is not empty while an
 		 * empty block after a non-empty block has been encountered
@@ -574,7 +575,7 @@ bool ntfs_check_logfile(struct inode *log_vi, struct restart_page_header **rp)
 		 * find a valid one further in the file.
 		 */
 		if (err != -EINVAL) {
-			kunmap_local(kaddr);
+			kunmap_local(kbase);
 			folio_put(folio);
 			goto err_out;
 		}
@@ -583,7 +584,7 @@ bool ntfs_check_logfile(struct inode *log_vi, struct restart_page_header **rp)
 			pos = NTFS_BLOCK_SIZE >> 1;
 	}
 	if (folio) {
-		kunmap_local(kaddr);
+		kunmap_local(kbase);
 		folio_put(folio);
 	}
 	if (logfile_is_empty) {
Signature · 1 line
2.53.0
maintainer Reviewed-by on the v2 series
#3 lore raw
In reply to Karl Mehltretter
Quoted 27 lines · Karl Mehltretter

Two kmap_local bugs in the mount path of fs/ntfs. Both only show on 32-bit kernels with HIGHMEM, where kmap_local_folio() hands out fixmap slots from a per task stack of 16 entries. multi_v7_defconfig enables both NTFS_FS and HIGHMEM, so the stock 32-bit ARM configuration is affected.

Patch 1: check_mft_mirror() unmaps pointers that were advanced past the end of the page. On 32-bit ARM this clears the wrong fixmap entry and the mount dies with a BUG a few calls later. syzkaller found it on a multi_v7_defconfig kernel. A fresh mkntfs volume reproduces it on the first mount.

Patch 2: ntfs_check_logfile() maps the same page once per loop iteration and unmaps it once, so a mount leaves three entries on the stack of the mounting task. After one mount the CPU it ran on can no longer be taken offline. A process that mounts ntfs volumes five times hits the BUG_ON() in kmap_local_idx_push(). x86-32 also warns when mount(2) returns.

The two fixes are independent of each other. Patch 2 was tested on top of patch 1. Both were tested on 32-bit ARM and x86-32 in QEMU. Details are below the --- line of each patch.

Changes in v2:
- v1 carried an older copy of patch 2 as a second 2/2 by mistake. The patches are unchanged.

Quoted 6 lines

v1:
https://lore.kernel.org/r/[email protected]/

Karl Mehltretter (2):
ntfs: fix kunmap_local() of advanced pointers in check_mft_mirror()
ntfs: fix kmap_local leak in ntfs_check_logfile()

The whole series look good to me.

Quoted 10 lines
 fs/ntfs/logfile.c | 11 ++++++-----
 fs/ntfs/super.c   | 20 +++++++++++---------
 2 files changed, 17 insertions(+), 14 deletions(-)

base-commit: 551c722f40809618230001baccf219193e22fc5a

Signature · 1 line
2.53.0
Signature · 2 lines
Thanks,
Hyunchul
maintainer Applied
#4 lore raw
In reply to Karl Mehltretter
Quoted 28 lines · Karl Mehltretter

Two kmap_local bugs in the mount path of fs/ntfs. Both only show on 32-bit kernels with HIGHMEM, where kmap_local_folio() hands out fixmap slots from a per task stack of 16 entries. multi_v7_defconfig enables both NTFS_FS and HIGHMEM, so the stock 32-bit ARM configuration is affected.

Patch 1: check_mft_mirror() unmaps pointers that were advanced past the end of the page. On 32-bit ARM this clears the wrong fixmap entry and the mount dies with a BUG a few calls later. syzkaller found it on a multi_v7_defconfig kernel. A fresh mkntfs volume reproduces it on the first mount.

Patch 2: ntfs_check_logfile() maps the same page once per loop iteration and unmaps it once, so a mount leaves three entries on the stack of the mounting task. After one mount the CPU it ran on can no longer be taken offline. A process that mounts ntfs volumes five times hits the BUG_ON() in kmap_local_idx_push(). x86-32 also warns when mount(2) returns.

The two fixes are independent of each other. Patch 2 was tested on top of patch 1. Both were tested on 32-bit ARM and x86-32 in QEMU. Details are below the --- line of each patch.

Changes in v2:
- v1 carried an older copy of patch 2 as a second 2/2 by mistake. The patches are unchanged.

Quoted 6 lines

v1:
https://lore.kernel.org/r/[email protected]/

Karl Mehltretter (2):
ntfs: fix kunmap_local() of advanced pointers in check_mft_mirror()
ntfs: fix kmap_local leak in ntfs_check_logfile()

Applied them to #ntfs-next.
Thanks!