* [BUG] ntfs: data race on ni->mrec between resident read path and MFT record mutation
@ 2026-07-25 4:24 Hyeontae Lee
2026-07-27 5:37 ` Hyunchul Lee
0 siblings, 1 reply; 6+ messages in thread
From: Hyeontae Lee @ 2026-07-25 4:24 UTC (permalink / raw)
To: linkinjeon, hyc.lee
Cc: zzzccc427, baijiaju1990, linux-fsdevel, linux-kernel, Hyeontae Lee
Hi,
KCSAN reports a data race in fs/ntfs on base_ni->mrec between the resident
read path, which does not take ni->mrec_lock, and MFT record mutation which
does. Reproduced on mainline 7.2-rc4
(48a5a7ab8d6ab7090564339e039c421f315de912).
This is close to
https://lore.kernel.org/all/20260626014706.2840003-1-zzzccc427@gmail.com/
[PATCH] ntfs: serialize read iomap lookup with mrec_lock
but that patch targets a slab-use-after-free on base_ni->attr_list and
guards its locking with NInoAttrList(base_ni). The race here is on ni->mrec
itself and reproduces on a 16-byte resident file that has no $ATTRIBUTE_LIST
at all, so neither that guard nor the per-inode attr_list rwsem discussed as
its replacement would cover it.
The read path reaches the MFT record buffer via
ntfs_read_iomap_begin_resident() -> ntfs_attr_lookup() -> ntfs_attr_find()
map_mft_record() only does atomic_inc(&ni->count) and does not take
ni->mrec_lock, so ntfs_attr_find() walks the record - reading each
attribute's ->length to advance the pointer - with no serialization against
a writer that holds mrec_lock and memmove()s the same buffer.
In fs/ntfs/iomap.c, all 15 occurrences of mrec_lock are in the write paths;
the read paths have none. The resident write path in particular holds it
from __ntfs_write_iomap_begin() through ntfs_write_iomap_end_resident(),
i.e. across the whole iomap operation, for the same IOMAP_INLINE pointer
into ni->mrec that the read path sets up without any lock.
The non-resident read path does take down_write(&ni->runlist.lock) around
ntfs_attr_vcn_to_rl(), but that protects ni->runlist.rl, not ni->mrec.
ntfs_attr_record_resize(), ntfs_attr_record_rm(), ntfs_make_room_for_attr(),
ntfs_resident_attr_record_add() and fs/ntfs/namei.c never take runlist.lock,
so those two paths hold disjoint locks.
KCSAN on 7.2-rc4 produced 40 reports in about one second, with three
different writers reached from both link() and unlink():
BUG: KCSAN: data-race in ntfs_attr_find / ntfs_attr_record_resize
write to 0xffff888100af1018 of 4 bytes by task 96 on cpu 1:
ntfs_attr_record_resize+0xd2/0x130
ntfs_attr_record_rm+0xad/0x530
ntfs_delete+0x224/0x640
ntfs_unlink+0x14d/0x280
vfs_unlink+0x157/0x520
filename_unlinkat+0x1d9/0x350
__x64_sys_unlink+0x2f/0x50
x64_sys_call+0x1d43/0x1fb0
do_syscall_64+0xed/0x450
entry_SYSCALL_64_after_hwframe+0x77/0x7f
read to 0xffff888100af1018 of 4 bytes by task 95 on cpu 0:
ntfs_attr_find+0x104/0x5b0
ntfs_attr_lookup+0x39c/0x10c0
ntfs_read_iomap_begin_resident+0xc6/0x230
ntfs_read_iomap_begin+0x5d/0xa0
iomap_iter+0x2e2/0x6e0
iomap_read_folio+0x147/0x2a0
ntfs_read_folio+0x108/0x170
filemap_read_folio+0x35/0x100
filemap_fault+0x993/0x1000
__do_fault+0x65/0x110
__handle_mm_fault+0xc06/0x1590
handle_mm_fault+0x126/0x2e0
do_user_addr_fault+0x173/0x680
exc_page_fault+0x63/0x130
asm_exc_page_fault+0x26/0x30
value changed: 0x00000250 -> 0x000001f0
BUG: KCSAN: data-race in ntfs_attr_find / ntfs_make_room_for_attr
write to 0xffff888100af1018 of 4 bytes by task 96 on cpu 1:
ntfs_make_room_for_attr+0xaa/0xf0
ntfs_resident_attr_record_add+0xd7/0x460
ntfs_attr_add+0x3a7/0x850
__ntfs_link+0x471/0x730
ntfs_link+0x148/0x260
vfs_link+0x23f/0x590
filename_linkat+0x20e/0x350
__x64_sys_link+0x58/0x80
x64_sys_call+0x10ca/0x1fb0
do_syscall_64+0xed/0x450
entry_SYSCALL_64_after_hwframe+0x77/0x7f
read to 0xffff888100af1018 of 4 bytes by task 95 on cpu 0:
ntfs_attr_find+0x104/0x5b0
ntfs_attr_lookup+0x39c/0x10c0
ntfs_read_iomap_begin_resident+0xc6/0x230
[ ... same reader stack as above ... ]
value changed: 0x000001f0 -> 0x00000250
BUG: KCSAN: data-race in ntfs_attr_find / ntfs_resident_attr_record_add
write to 0xffff888100af10e4 of 4 bytes by task 96 on cpu 1:
ntfs_resident_attr_record_add+0x10b/0x460
ntfs_attr_add+0x3a7/0x850
__ntfs_link+0x471/0x730
ntfs_link+0x148/0x260
vfs_link+0x23f/0x590
filename_linkat+0x20e/0x350
__x64_sys_link+0x58/0x80
x64_sys_call+0x10ca/0x1fb0
do_syscall_64+0xed/0x450
entry_SYSCALL_64_after_hwframe+0x77/0x7f
read to 0xffff888100af10e4 of 4 bytes by task 95 on cpu 0:
ntfs_attr_find+0x186/0x5b0
ntfs_attr_lookup+0x39c/0x10c0
ntfs_read_iomap_begin_resident+0xc6/0x230
[ ... same reader stack as above ... ]
value changed: 0x00000068 -> 0x00000060
Reports were also produced with reads originating inside
ntfs_attr_value_is_valid() (offsets +0x20, +0x32, +0x106, +0x122), so the
bounds check that would otherwise contain a torn offset is itself reading
concurrently-modified fields. These 14 were reported as "race at unknown
origin" - KCSAN observed the value change but did not capture the writing
stack - so I am citing them as evidence that the validator reads the same
unlocked buffer, not as matched pairs.
Observed transitions on the raced length/offset fields include
0x00000010 -> 0x80040001, 0x00000028 -> 0x00040600 and
0x00000050 -> 0x01000000 (3, 3 and 1 occurrences respectively). The record
is about 1 KB. CONFIG_KCSAN_REPORT_VALUE_CHANGE_ONLY was set, so these are
observed changes rather than merely concurrent access.
It is also visible without KCSAN. On a freshly created mkntfs image that is
not corrupt, the driver logs attribute corruption while the race runs:
ntfs: (device loop0): ntfs_attr_find(): mft 0x40, type 0x80 is corrupt.
Run chkdsk.
ntfs: (device loop0): ntfs_attr_value_is_valid(): Corrupt 0x80 attribute
in MFT record 64
Reproducer:
# mkntfs -F -Q ntfs.img
# mount -t ntfs -o loop ntfs.img /mnt
# printf 'AAAAAAAAAAAAAAAA' > /mnt/f
# gcc -O2 -o race race.c -lpthread
# ./race /mnt/f /mnt/l 30
Adding or removing the extra $FILE_NAME attribute resizes the record, and
the attributes after it - including the $DATA attribute the reader is
walking to - are memmove()d.
---8<---
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sched.h>
#include <pthread.h>
#include <sys/mman.h>
static const char *path_f, *path_l;
static volatile int stop;
static void pin(int cpu)
{
cpu_set_t s;
CPU_ZERO(&s);
CPU_SET(cpu, &s);
sched_setaffinity(0, sizeof(s), &s);
}
/*
* Fault the resident $DATA in, then evict the folio so that the next
* iteration re-enters ntfs_read_folio() -> ntfs_read_iomap_begin_resident().
* MADV_DONTNEED is not enough here: it only tears down the PTE and leaves
* the folio cached, so filemap_fault() just re-maps it and the read path is
* never re-entered.
*/
static void *reader(void *unused)
{
pin(0);
while (!stop) {
int fd = open(path_f, O_RDONLY);
char *m;
if (fd < 0)
continue;
m = mmap(NULL, 4096, PROT_READ, MAP_SHARED, fd, 0);
if (m != MAP_FAILED) {
volatile char c = *(volatile char *)m;
(void)c;
munmap(m, 4096);
}
posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED);
close(fd);
}
return NULL;
}
static void *writer(void *unused)
{
pin(1);
while (!stop) {
link(path_f, path_l);
unlink(path_l);
}
return NULL;
}
int main(int argc, char **argv)
{
pthread_t r, w;
if (argc < 3) {
fprintf(stderr, "usage: %s <file> <linkpath> [seconds]\n",
argv[0]);
return 1;
}
path_f = argv[1];
path_l = argv[2];
unlink(path_l);
pthread_create(&r, NULL, reader, NULL);
pthread_create(&w, NULL, writer, NULL);
sleep(argc > 3 ? atoi(argv[3]) : 30);
stop = 1;
pthread_join(r, NULL);
pthread_join(w, NULL);
return 0;
}
---8<---
I did not observe an out-of-bounds access under KASAN. The full dmesg with
all 40 reports is available if it would be useful.
Given the above, protecting attr_list alone would leave this case open. It
may be worth deciding whether ni->mrec needs its own serialization on the
read side, mirroring what the write path already does. I have the reproducer
and a KCSAN build set up, so I am happy to test a candidate fix against it.
The asymmetry is present in b041ca562526 itself (0 mrec_lock in the read
paths, 17 in the write paths), so it dates from v7.1.
Test environment:
Kernel: 7.2-rc4 (48a5a7ab8d6ab7090564339e039c421f315de912)
Config: x86_64_defconfig + CONFIG_NTFS_FS=y + CONFIG_KCSAN=y (non-strict),
KCSAN_SKIP_WATCH=500, KCSAN_UDELAY_TASK=200,
KCSAN_REPORT_VALUE_CHANGE_ONLY=y
Guest: QEMU/KVM, 4 vCPU
Volume: 64 MB image, mkntfs -F -Q, mounted -t ntfs -o loop
Fixes: b041ca562526 ("ntfs: update iomap and address space operations")
Reported-by: Hyeontae Lee <wonju345@naver.com>
Thanks,
Hyeontae Lee
^ permalink raw reply [flat|nested] 6+ messages in thread* Re: [BUG] ntfs: data race on ni->mrec between resident read path and MFT record mutation 2026-07-25 4:24 [BUG] ntfs: data race on ni->mrec between resident read path and MFT record mutation Hyeontae Lee @ 2026-07-27 5:37 ` Hyunchul Lee 2026-07-28 10:42 ` Hyeontae Lee 2026-07-31 9:10 ` [PATCH] ntfs: serialize the resident read iomap path with mrec_lock Hyeontae Lee 0 siblings, 2 replies; 6+ messages in thread From: Hyunchul Lee @ 2026-07-27 5:37 UTC (permalink / raw) To: Hyeontae Lee Cc: linkinjeon, zzzccc427, baijiaju1990, linux-fsdevel, linux-kernel Hi Hyeontae, Thank you for the detailed report. I will take a closer look at this issue. 2026년 7월 25일 (토) 오후 1:24, Hyeontae Lee <wonju345@naver.com>님이 작성: > > Hi, > > KCSAN reports a data race in fs/ntfs on base_ni->mrec between the resident > read path, which does not take ni->mrec_lock, and MFT record mutation which > does. Reproduced on mainline 7.2-rc4 > (48a5a7ab8d6ab7090564339e039c421f315de912). > > This is close to > > https://lore.kernel.org/all/20260626014706.2840003-1-zzzccc427@gmail.com/ > [PATCH] ntfs: serialize read iomap lookup with mrec_lock > > but that patch targets a slab-use-after-free on base_ni->attr_list and > guards its locking with NInoAttrList(base_ni). The race here is on ni->mrec > itself and reproduces on a 16-byte resident file that has no $ATTRIBUTE_LIST > at all, so neither that guard nor the per-inode attr_list rwsem discussed as > its replacement would cover it. > > The read path reaches the MFT record buffer via > > ntfs_read_iomap_begin_resident() -> ntfs_attr_lookup() -> ntfs_attr_find() > > map_mft_record() only does atomic_inc(&ni->count) and does not take > ni->mrec_lock, so ntfs_attr_find() walks the record - reading each > attribute's ->length to advance the pointer - with no serialization against > a writer that holds mrec_lock and memmove()s the same buffer. > > In fs/ntfs/iomap.c, all 15 occurrences of mrec_lock are in the write paths; > the read paths have none. The resident write path in particular holds it > from __ntfs_write_iomap_begin() through ntfs_write_iomap_end_resident(), > i.e. across the whole iomap operation, for the same IOMAP_INLINE pointer > into ni->mrec that the read path sets up without any lock. > > The non-resident read path does take down_write(&ni->runlist.lock) around > ntfs_attr_vcn_to_rl(), but that protects ni->runlist.rl, not ni->mrec. > ntfs_attr_record_resize(), ntfs_attr_record_rm(), ntfs_make_room_for_attr(), > ntfs_resident_attr_record_add() and fs/ntfs/namei.c never take runlist.lock, > so those two paths hold disjoint locks. > > KCSAN on 7.2-rc4 produced 40 reports in about one second, with three > different writers reached from both link() and unlink(): > > BUG: KCSAN: data-race in ntfs_attr_find / ntfs_attr_record_resize > > write to 0xffff888100af1018 of 4 bytes by task 96 on cpu 1: > ntfs_attr_record_resize+0xd2/0x130 > ntfs_attr_record_rm+0xad/0x530 > ntfs_delete+0x224/0x640 > ntfs_unlink+0x14d/0x280 > vfs_unlink+0x157/0x520 > filename_unlinkat+0x1d9/0x350 > __x64_sys_unlink+0x2f/0x50 > x64_sys_call+0x1d43/0x1fb0 > do_syscall_64+0xed/0x450 > entry_SYSCALL_64_after_hwframe+0x77/0x7f > > read to 0xffff888100af1018 of 4 bytes by task 95 on cpu 0: > ntfs_attr_find+0x104/0x5b0 > ntfs_attr_lookup+0x39c/0x10c0 > ntfs_read_iomap_begin_resident+0xc6/0x230 > ntfs_read_iomap_begin+0x5d/0xa0 > iomap_iter+0x2e2/0x6e0 > iomap_read_folio+0x147/0x2a0 > ntfs_read_folio+0x108/0x170 > filemap_read_folio+0x35/0x100 > filemap_fault+0x993/0x1000 > __do_fault+0x65/0x110 > __handle_mm_fault+0xc06/0x1590 > handle_mm_fault+0x126/0x2e0 > do_user_addr_fault+0x173/0x680 > exc_page_fault+0x63/0x130 > asm_exc_page_fault+0x26/0x30 > > value changed: 0x00000250 -> 0x000001f0 > > BUG: KCSAN: data-race in ntfs_attr_find / ntfs_make_room_for_attr > > write to 0xffff888100af1018 of 4 bytes by task 96 on cpu 1: > ntfs_make_room_for_attr+0xaa/0xf0 > ntfs_resident_attr_record_add+0xd7/0x460 > ntfs_attr_add+0x3a7/0x850 > __ntfs_link+0x471/0x730 > ntfs_link+0x148/0x260 > vfs_link+0x23f/0x590 > filename_linkat+0x20e/0x350 > __x64_sys_link+0x58/0x80 > x64_sys_call+0x10ca/0x1fb0 > do_syscall_64+0xed/0x450 > entry_SYSCALL_64_after_hwframe+0x77/0x7f > > read to 0xffff888100af1018 of 4 bytes by task 95 on cpu 0: > ntfs_attr_find+0x104/0x5b0 > ntfs_attr_lookup+0x39c/0x10c0 > ntfs_read_iomap_begin_resident+0xc6/0x230 > [ ... same reader stack as above ... ] > > value changed: 0x000001f0 -> 0x00000250 > > BUG: KCSAN: data-race in ntfs_attr_find / ntfs_resident_attr_record_add > > write to 0xffff888100af10e4 of 4 bytes by task 96 on cpu 1: > ntfs_resident_attr_record_add+0x10b/0x460 > ntfs_attr_add+0x3a7/0x850 > __ntfs_link+0x471/0x730 > ntfs_link+0x148/0x260 > vfs_link+0x23f/0x590 > filename_linkat+0x20e/0x350 > __x64_sys_link+0x58/0x80 > x64_sys_call+0x10ca/0x1fb0 > do_syscall_64+0xed/0x450 > entry_SYSCALL_64_after_hwframe+0x77/0x7f > > read to 0xffff888100af10e4 of 4 bytes by task 95 on cpu 0: > ntfs_attr_find+0x186/0x5b0 > ntfs_attr_lookup+0x39c/0x10c0 > ntfs_read_iomap_begin_resident+0xc6/0x230 > [ ... same reader stack as above ... ] > > value changed: 0x00000068 -> 0x00000060 > > Reports were also produced with reads originating inside > ntfs_attr_value_is_valid() (offsets +0x20, +0x32, +0x106, +0x122), so the > bounds check that would otherwise contain a torn offset is itself reading > concurrently-modified fields. These 14 were reported as "race at unknown > origin" - KCSAN observed the value change but did not capture the writing > stack - so I am citing them as evidence that the validator reads the same > unlocked buffer, not as matched pairs. > > Observed transitions on the raced length/offset fields include > 0x00000010 -> 0x80040001, 0x00000028 -> 0x00040600 and > 0x00000050 -> 0x01000000 (3, 3 and 1 occurrences respectively). The record > is about 1 KB. CONFIG_KCSAN_REPORT_VALUE_CHANGE_ONLY was set, so these are > observed changes rather than merely concurrent access. > > It is also visible without KCSAN. On a freshly created mkntfs image that is > not corrupt, the driver logs attribute corruption while the race runs: > > ntfs: (device loop0): ntfs_attr_find(): mft 0x40, type 0x80 is corrupt. > Run chkdsk. > ntfs: (device loop0): ntfs_attr_value_is_valid(): Corrupt 0x80 attribute > in MFT record 64 > > Reproducer: > > # mkntfs -F -Q ntfs.img > # mount -t ntfs -o loop ntfs.img /mnt > # printf 'AAAAAAAAAAAAAAAA' > /mnt/f > # gcc -O2 -o race race.c -lpthread > # ./race /mnt/f /mnt/l 30 > > Adding or removing the extra $FILE_NAME attribute resizes the record, and > the attributes after it - including the $DATA attribute the reader is > walking to - are memmove()d. > > ---8<--- > #define _GNU_SOURCE > #include <stdio.h> > #include <stdlib.h> > #include <unistd.h> > #include <fcntl.h> > #include <sched.h> > #include <pthread.h> > #include <sys/mman.h> > > static const char *path_f, *path_l; > static volatile int stop; > > static void pin(int cpu) > { > cpu_set_t s; > > CPU_ZERO(&s); > CPU_SET(cpu, &s); > sched_setaffinity(0, sizeof(s), &s); > } > > /* > * Fault the resident $DATA in, then evict the folio so that the next > * iteration re-enters ntfs_read_folio() -> ntfs_read_iomap_begin_resident(). > * MADV_DONTNEED is not enough here: it only tears down the PTE and leaves > * the folio cached, so filemap_fault() just re-maps it and the read path is > * never re-entered. > */ > static void *reader(void *unused) > { > pin(0); > while (!stop) { > int fd = open(path_f, O_RDONLY); > char *m; > > if (fd < 0) > continue; > m = mmap(NULL, 4096, PROT_READ, MAP_SHARED, fd, 0); > if (m != MAP_FAILED) { > volatile char c = *(volatile char *)m; > > (void)c; > munmap(m, 4096); > } > posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED); > close(fd); > } > return NULL; > } > > static void *writer(void *unused) > { > pin(1); > while (!stop) { > link(path_f, path_l); > unlink(path_l); > } > return NULL; > } > > int main(int argc, char **argv) > { > pthread_t r, w; > > if (argc < 3) { > fprintf(stderr, "usage: %s <file> <linkpath> [seconds]\n", > argv[0]); > return 1; > } > path_f = argv[1]; > path_l = argv[2]; > unlink(path_l); > > pthread_create(&r, NULL, reader, NULL); > pthread_create(&w, NULL, writer, NULL); > sleep(argc > 3 ? atoi(argv[3]) : 30); > stop = 1; > pthread_join(r, NULL); > pthread_join(w, NULL); > return 0; > } > ---8<--- > > I did not observe an out-of-bounds access under KASAN. The full dmesg with > all 40 reports is available if it would be useful. > > Given the above, protecting attr_list alone would leave this case open. It > may be worth deciding whether ni->mrec needs its own serialization on the > read side, mirroring what the write path already does. I have the reproducer > and a KCSAN build set up, so I am happy to test a candidate fix against it. > > The asymmetry is present in b041ca562526 itself (0 mrec_lock in the read > paths, 17 in the write paths), so it dates from v7.1. > > Test environment: > Kernel: 7.2-rc4 (48a5a7ab8d6ab7090564339e039c421f315de912) > Config: x86_64_defconfig + CONFIG_NTFS_FS=y + CONFIG_KCSAN=y (non-strict), > KCSAN_SKIP_WATCH=500, KCSAN_UDELAY_TASK=200, > KCSAN_REPORT_VALUE_CHANGE_ONLY=y > Guest: QEMU/KVM, 4 vCPU > Volume: 64 MB image, mkntfs -F -Q, mounted -t ntfs -o loop > > Fixes: b041ca562526 ("ntfs: update iomap and address space operations") > Reported-by: Hyeontae Lee <wonju345@naver.com> > > Thanks, > Hyeontae Lee -- Thanks, Hyunchul ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [BUG] ntfs: data race on ni->mrec between resident read path and MFT record mutation 2026-07-27 5:37 ` Hyunchul Lee @ 2026-07-28 10:42 ` Hyeontae Lee 2026-07-31 9:10 ` [PATCH] ntfs: serialize the resident read iomap path with mrec_lock Hyeontae Lee 1 sibling, 0 replies; 6+ messages in thread From: Hyeontae Lee @ 2026-07-28 10:42 UTC (permalink / raw) To: hyc.lee; +Cc: linkinjeon, zzzccc427, baijiaju1990, linux-fsdevel, linux-kernel Hi Hyunchul, Thanks. In case it saves you some time: I tried the naive fix - taking mrec_lock unconditionally in __ntfs_read_iomap_begin() - and it deadlocks immediately. ntfs_lookup() already holds the directory inode's mrec_lock when it reads an index folio via read_mapping_folio(), so the read iomap path re-enters and lockdep reports recursive locking: WARNING: possible recursive locking detected -------------------------------------------- bash/1 is trying to acquire lock: (&ni->mrec_lock), at: __ntfs_read_iomap_begin+0x291/0x3f0 but task is already holding lock: (&ni->mrec_lock), at: ntfs_lookup+0x7f/0x3e0 Call Trace: __mutex_lock+0xd3/0x1190 __ntfs_read_iomap_begin+0x291/0x3f0 iomap_iter+0x1a4/0x480 iomap_read_folio+0xb1/0x1c0 ntfs_read_folio+0x82/0xd0 filemap_read_folio+0x18/0x80 do_read_cache_folio+0x7a/0x1b0 ntfs_lookup_inode_by_name+0x624/0xe00 ntfs_lookup+0x93/0x3e0 path_openat+0xb4f/0x1370 So the read path is entered both with and without mrec_lock held (this one has no attribute list involved), which lines up with your point on the June thread that an unconditional lock there does not work. The comment at fs/ntfs/dir.c:295 notes the same read_mapping_folio() hazard. I will think about a different approach. Thanks, Hyeontae ^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH] ntfs: serialize the resident read iomap path with mrec_lock 2026-07-27 5:37 ` Hyunchul Lee 2026-07-28 10:42 ` Hyeontae Lee @ 2026-07-31 9:10 ` Hyeontae Lee 2026-07-31 15:00 ` Namjae Jeon 1 sibling, 1 reply; 6+ messages in thread From: Hyeontae Lee @ 2026-07-31 9:10 UTC (permalink / raw) To: linkinjeon, hyc.lee Cc: zzzccc427, baijiaju1990, linux-fsdevel, linux-kernel, Hyeontae Lee ntfs_read_iomap_begin_resident() walks the MFT record through ntfs_attr_lookup() -> ntfs_attr_find() without taking ni->mrec_lock, while ntfs_attr_record_resize(), ntfs_make_room_for_attr() and ntfs_resident_attr_record_add() memmove() the same base_ni->mrec buffer under that lock. map_mft_record() only takes a reference and does not serialize, so the reader can observe torn attribute length and offset fields while a writer is relocating the records. KCSAN reports the race between the mmap read fault path and both link() and unlink(): BUG: KCSAN: data-race in ntfs_attr_find / ntfs_attr_record_resize write to 0xffff888100af1018 of 4 bytes by task 96 on cpu 1: ntfs_attr_record_resize+0xd2/0x130 ntfs_attr_record_rm+0xad/0x530 ntfs_delete+0x224/0x640 ntfs_unlink+0x14d/0x280 vfs_unlink+0x157/0x520 read to 0xffff888100af1018 of 4 bytes by task 95 on cpu 0: ntfs_attr_find+0x104/0x5b0 ntfs_attr_lookup+0x39c/0x10c0 ntfs_read_iomap_begin_resident+0xc6/0x230 ntfs_read_iomap_begin+0x5d/0xa0 iomap_iter+0x2e2/0x6e0 iomap_read_folio+0x147/0x2a0 ntfs_read_folio+0x108/0x170 filemap_read_folio+0x35/0x100 filemap_fault+0x993/0x1000 value changed: 0x00000250 -> 0x000001f0 The address is mrec + 0x18, i.e. mft_record.bytes_in_use, and the change is the 96 bytes of one $FILE_NAME attribute being removed. Take base_ni->mrec_lock around the lookup in ntfs_read_iomap_begin_resident(). The non-resident path is left alone: ntfs_lookup() already holds the directory inode's mrec_lock when it reads an index folio through read_mapping_folio(), and taking the lock in the shared wrapper deadlocks there with recursive locking on mrec_lock. The comment above the read_mapping_folio() call in fs/ntfs/dir.c notes the same hazard. Tested with a reproducer that faults in a 16-byte resident file while another thread runs link()/unlink() on it. Before: 40 KCSAN reports in about one second. After: no reports in 180 seconds over 206,090 read iterations and 423,540 link/unlink cycles. A PROVE_LOCKING build shows no lockdep splat with the same reproducer running for 60 seconds. Fixes: b041ca562526 ("ntfs: update iomap and address space operations") Link: https://lore.kernel.org/all/20260725042421.109599-1-wonju345@naver.com/ Suggested-by: Hyunchul Lee <hyc.lee@gmail.com> Signed-off-by: Hyeontae Lee <wonju345@naver.com> --- fs/ntfs/iomap.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/fs/ntfs/iomap.c b/fs/ntfs/iomap.c index 52eecf5cb2..f9fdceeeb3 100644 --- a/fs/ntfs/iomap.c +++ b/fs/ntfs/iomap.c @@ -95,6 +95,8 @@ static int ntfs_read_iomap_begin_resident(struct inode *inode, loff_t offset, lo else base_ni = ni; + mutex_lock(&base_ni->mrec_lock); + ctx = ntfs_attr_get_search_ctx(base_ni, NULL); if (!ctx) { err = -ENOMEM; @@ -138,6 +140,8 @@ static int ntfs_read_iomap_begin_resident(struct inode *inode, loff_t offset, lo if (ctx) ntfs_attr_put_search_ctx(ctx); + mutex_unlock(&base_ni->mrec_lock); + return err; } -- 2.43.0 ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] ntfs: serialize the resident read iomap path with mrec_lock 2026-07-31 9:10 ` [PATCH] ntfs: serialize the resident read iomap path with mrec_lock Hyeontae Lee @ 2026-07-31 15:00 ` Namjae Jeon 2026-08-01 6:27 ` Hyeontae Lee 0 siblings, 1 reply; 6+ messages in thread From: Namjae Jeon @ 2026-07-31 15:00 UTC (permalink / raw) To: Hyeontae Lee Cc: hyc.lee, zzzccc427, baijiaju1990, linux-fsdevel, linux-kernel [-- Attachment #1: Type: text/plain, Size: 896 bytes --] > diff --git a/fs/ntfs/iomap.c b/fs/ntfs/iomap.c > index 52eecf5cb2..f9fdceeeb3 100644 > --- a/fs/ntfs/iomap.c > +++ b/fs/ntfs/iomap.c > @@ -95,6 +95,8 @@ static int ntfs_read_iomap_begin_resident(struct inode *inode, loff_t offset, lo > else > base_ni = ni; > > + mutex_lock(&base_ni->mrec_lock); > + > ctx = ntfs_attr_get_search_ctx(base_ni, NULL); > if (!ctx) { > err = -ENOMEM; > @@ -138,6 +140,8 @@ static int ntfs_read_iomap_begin_resident(struct inode *inode, loff_t offset, lo > if (ctx) > ntfs_attr_put_search_ctx(ctx); > > + mutex_unlock(&base_ni->mrec_lock); We need to hold the lock until iomap_end() because iomap_begin() only returns an inline_data pointer into the MFT record. The actual data copy happens later in the iomap core. So can you check if the attached patch fixes this issue ? [-- Attachment #2: 0001-ntfs-serialize-resident-iomap-reads-with-mrec_lock.patch --] [-- Type: text/x-patch, Size: 5767 bytes --] From a33fee1b75a933bfdc42d59fb9e9c7be1b84a06f Mon Sep 17 00:00:00 2001 From: Hyeontae Lee <wonju345@naver.com> Date: Fri, 31 Jul 2026 18:10:35 +0900 Subject: [PATCH] ntfs: serialize resident iomap reads with mrec_lock ntfs_read_iomap_begin_resident() walks the MFT record through ntfs_attr_lookup() -> ntfs_attr_find() without taking ni->mrec_lock, while ntfs_attr_record_resize(), ntfs_make_room_for_attr() and ntfs_resident_attr_record_add() memmove() the same base_ni->mrec buffer under that lock. map_mft_record() only takes a reference and does not serialize, so the reader can observe torn attribute length and offset fields while a writer is relocating the records. KCSAN reports the race between the mmap read fault path and both link() and unlink(): BUG: KCSAN: data-race in ntfs_attr_find / ntfs_attr_record_resize write to 0xffff888100af1018 of 4 bytes by task 96 on cpu 1: ntfs_attr_record_resize+0xd2/0x130 ntfs_attr_record_rm+0xad/0x530 ntfs_delete+0x224/0x640 ntfs_unlink+0x14d/0x280 vfs_unlink+0x157/0x520 read to 0xffff888100af1018 of 4 bytes by task 95 on cpu 0: ntfs_attr_find+0x104/0x5b0 ntfs_attr_lookup+0x39c/0x10c0 ntfs_read_iomap_begin_resident+0xc6/0x230 ntfs_read_iomap_begin+0x5d/0xa0 iomap_iter+0x2e2/0x6e0 iomap_read_folio+0x147/0x2a0 ntfs_read_folio+0x108/0x170 filemap_read_folio+0x35/0x100 filemap_fault+0x993/0x1000 value changed: 0x00000250 -> 0x000001f0 The address is mrec + 0x18, i.e. mft_record.bytes_in_use, and the change is the 96 bytes of one $FILE_NAME attribute being removed. Keep base_ni->mrec_lock from the resident read iomap lookup through iomap_end(). This protects both the attribute walk and the subsequent copy from iomap->inline_data, which points into the MFT record. The non-resident path is left alone: ntfs_lookup() already holds the directory inode's mrec_lock when it reads an index folio through read_mapping_folio(), and taking the lock in the shared wrapper deadlocks there with recursive locking on mrec_lock. The comment above the read_mapping_folio() call in fs/ntfs/dir.c notes the same hazard. The seek path uses the same lookup helper but does not dereference iomap->inline_data. Release the lock before returning from that path, whereas the regular read path records base_ni in iomap->private and releases the lock from its iomap_end() callback. Tested with a reproducer that faults in a 16-byte resident file while another thread runs link()/unlink() on it. Before: 40 KCSAN reports in about one second. After: no reports in 180 seconds over 206,090 read iterations and 423,540 link/unlink cycles. A PROVE_LOCKING build shows no lockdep splat with the same reproducer running for 60 seconds. Fixes: b041ca562526 ("ntfs: update iomap and address space operations") Link: https://lore.kernel.org/all/20260725042421.109599-1-wonju345@naver.com/ Signed-off-by: Hyeontae Lee <wonju345@naver.com> --- fs/ntfs/iomap.c | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/fs/ntfs/iomap.c b/fs/ntfs/iomap.c index 26a1831a2c18..73c50171285a 100644 --- a/fs/ntfs/iomap.c +++ b/fs/ntfs/iomap.c @@ -81,7 +81,7 @@ const struct iomap_write_ops ntfs_iomap_folio_ops = { }; static int ntfs_read_iomap_begin_resident(struct inode *inode, loff_t offset, loff_t length, - unsigned int flags, struct iomap *iomap) + unsigned int flags, struct iomap *iomap, bool keep_mrec_lock) { struct ntfs_inode *base_ni, *ni = NTFS_I(inode); struct ntfs_attr_search_ctx *ctx; @@ -95,6 +95,8 @@ static int ntfs_read_iomap_begin_resident(struct inode *inode, loff_t offset, lo else base_ni = ni; + mutex_lock(&base_ni->mrec_lock); + ctx = ntfs_attr_get_search_ctx(base_ni, NULL); if (!ctx) { err = -ENOMEM; @@ -138,6 +140,13 @@ static int ntfs_read_iomap_begin_resident(struct inode *inode, loff_t offset, lo if (ctx) ntfs_attr_put_search_ctx(ctx); + if (!err && keep_mrec_lock && iomap->type == IOMAP_INLINE) { + iomap->private = base_ni; + return 0; + } + + mutex_unlock(&base_ni->mrec_lock); + return err; } @@ -261,24 +270,35 @@ static int ntfs_read_iomap_begin_non_resident(struct inode *inode, loff_t offset static int __ntfs_read_iomap_begin(struct inode *inode, loff_t offset, loff_t length, unsigned int flags, struct iomap *iomap, struct iomap *srcmap, - bool need_unwritten) + bool need_unwritten, bool keep_mrec_lock) { if (NInoNonResident(NTFS_I(inode))) return ntfs_read_iomap_begin_non_resident(inode, offset, length, flags, iomap, need_unwritten); return ntfs_read_iomap_begin_resident(inode, offset, length, - flags, iomap); + flags, iomap, keep_mrec_lock); } static int ntfs_read_iomap_begin(struct inode *inode, loff_t offset, loff_t length, unsigned int flags, struct iomap *iomap, struct iomap *srcmap) { return __ntfs_read_iomap_begin(inode, offset, length, flags, iomap, - srcmap, true); + srcmap, true, true); +} + +static int ntfs_read_iomap_end(struct inode *inode, loff_t pos, loff_t length, + ssize_t written, unsigned int flags, struct iomap *iomap) +{ + struct ntfs_inode *base_ni = iomap->private; + + if (base_ni) + mutex_unlock(&base_ni->mrec_lock); + return written; } const struct iomap_ops ntfs_read_iomap_ops = { .iomap_begin = ntfs_read_iomap_begin, + .iomap_end = ntfs_read_iomap_end, }; /* @@ -318,7 +338,7 @@ static int ntfs_seek_iomap_begin(struct inode *inode, loff_t offset, loff_t leng unsigned int flags, struct iomap *iomap, struct iomap *srcmap) { return __ntfs_read_iomap_begin(inode, offset, length, flags, iomap, - srcmap, false); + srcmap, false, false); } static int ntfs_zero_read_iomap_end(struct inode *inode, loff_t pos, loff_t length, -- 2.25.1 ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] ntfs: serialize the resident read iomap path with mrec_lock 2026-07-31 15:00 ` Namjae Jeon @ 2026-08-01 6:27 ` Hyeontae Lee 0 siblings, 0 replies; 6+ messages in thread From: Hyeontae Lee @ 2026-08-01 6:27 UTC (permalink / raw) To: linkinjeon Cc: hyc.lee, zzzccc427, baijiaju1990, linux-fsdevel, linux-kernel, Hyeontae Lee Hi Namjae, You are right - my version drops the lock before the iomap core copies from iomap->inline_data, so the copy itself was still unprotected. I tested your patch and it fixes the issue: KCSAN, two runs of 180 seconds each on the same reproducer: run 1: 206,015 read faults, 415,351 link/unlink cycles run 2: 205,732 read faults, 421,672 link/unlink cycles No data-race reports in ntfs_attr_find(), ntfs_attr_value_is_valid() or ntfs_read_iomap_begin_resident(), and no "is corrupt" messages. Before the fix the same reproducer produced 40 reports in about one second. PROVE_LOCKING, 60 seconds with the same reproducer: 1,199,159 read faults, 1,396,557 link/unlink cycles, no lockdep splat. Mount, read, write and unmount all behave normally. Tested-by: Hyeontae Lee <wonju345@naver.com> Thanks, Hyeontae ^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2026-08-01 6:37 UTC | newest] Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2026-07-25 4:24 [BUG] ntfs: data race on ni->mrec between resident read path and MFT record mutation Hyeontae Lee 2026-07-27 5:37 ` Hyunchul Lee 2026-07-28 10:42 ` Hyeontae Lee 2026-07-31 9:10 ` [PATCH] ntfs: serialize the resident read iomap path with mrec_lock Hyeontae Lee 2026-07-31 15:00 ` Namjae Jeon 2026-08-01 6:27 ` Hyeontae Lee
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®