ntfs: serialize attribute-list replacement with lookups
Subsystems (2)
RFC PATCH v5 1/1 ntfs: serialize attribute-list replacement with lookups
ntfs_external_attr_find() walks base_ni->attr_list with raw attr_list_entry cursors stored in ctx->al_entry and local variables. Other metadata paths can replace base_ni->attr_list and free the old buffer while a lookup still carries one of those cursors.
The buggy scenario involves two paths, with each column showing the order within that path:
attribute lookup path: attribute-list mutation path:
1. enters ntfs_attr_lookup() 1. enters an attr-list add/remove
2. reads base_ni->attr_list 2. builds a replacement buffer
3. keeps ctx->al_entry/al_entry 3. publishes base_ni->attr_list
pointing into that buffer 4. frees the previous buffer 4. continues the attribute search
5. dereferences the saved cursor
Validation reproduced this kernel report:
BUG: KASAN: slab-use-after-free in ntfs_attr_lookup+0x24b2/0x2bf0 Call Trace: <TASK> dump_stack_lvl+0x66/0xa0 print_report+0xce/0x630 ? ntfs_attr_lookup+0x24b2/0x2bf0 ? srso_alias_return_thunk+0x5/0xfbef5 ? __virt_addr_valid+0x20d/0x410 ? ntfs_attr_lookup+0x24b2/0x2bf0 kasan_report+0xe0/0x110 ? ntfs_attr_lookup+0x24b2/0x2bf0 ntfs_attr_lookup+0x24b2/0x2bf0 ? ntfs_attr_lookup+0x9/0x2bf0 ? srso_alias_return_thunk+0x5/0xfbef5 ? 0xffffffffc00000da ? __pfx_ntfs_attr_lookup+0x10/0x10 ntfs_attr_map_whole_runlist+0x488/0x8b0 ? __pfx_ntfs_attr_map_whole_runlist+0x10/0x10 ? __pfx_do_raw_spin_trylock+0x10/0x10 ntfs_attr_vcn_to_rl+0x13a/0x1d0 ? filemap_fault+0xf03/0x2080 ntfs_read_iomap_begin_non_resident+0x142/0x920 ? __pfx_ntfs_read_iomap_begin_non_resident+0x10/0x10 ? srso_alias_return_thunk+0x5/0xfbef5 ? __pfx_ntfs_read_iomap_begin+0x10/0x10 iomap_iter+0x6de/0x11e0 ? srso_alias_return_thunk+0x5/0xfbef5 ? prepare_alloc_pages+0x1c4/0x4d0 iomap_readahead+0x239/0x910 ? folios_put_refs+0x1ed/0x4f0 ? __pfx_iomap_readahead+0x10/0x10 ? __pfx_folios_put_refs+0x10/0x10 ? lock_release+0x1e0/0x280 ? srso_alias_return_thunk+0x5/0xfbef5 ? xas_store+0x871/0x1650 ? xas_find_conflict+0x64b/0xb90 ? srso_alias_return_thunk+0x5/0xfbef5 ? lock_release+0x1e0/0x280 ntfs_readahead+0x193/0x1d0 ? __pfx_ntfs_readahead+0x10/0x10 ? __filemap_add_folio+0x869/0xa60 ? srso_alias_return_thunk+0x5/0xfbef5 ? lock_acquire+0x2b8/0x2f0 ? __pfx___filemap_add_folio+0x10/0x10 read_pages+0x18f/0x890 ? srso_alias_return_thunk+0x5/0xfbef5 ? folio_alloc_noprof+0x69/0xb0 ? __pfx_read_pages+0x10/0x10 ? srso_alias_return_thunk+0x5/0xfbef5 page_cache_ra_unbounded+0x389/0x6c0 do_page_cache_ra+0x109/0x170 filemap_fault+0xf03/0x2080 ? __pfx_filemap_fault+0x10/0x10 ? srso_alias_return_thunk+0x5/0xfbef5 ? lock_acquire+0x2b8/0x2f0 ? lockdep_init_map_type+0x5c/0x230 __do_fault+0xf0/0x260 __handle_mm_fault+0xdcf/0x1ca0 ? __pfx___handle_mm_fault+0x10/0x10 ? srso_alias_return_thunk+0x5/0xfbef5 ? srso_alias_return_thunk+0x5/0xfbef5 ? lock_release+0x1e0/0x280 handle_mm_fault+0x19c/0x470 do_user_addr_fault+0x23b/0x9c0 exc_page_fault+0x5c/0xc0 asm_exc_page_fault+0x26/0x30 Allocated by task 511: kasan_save_stack+0x33/0x60 kasan_save_track+0x14/0x30 __kasan_kmalloc+0xaa/0xb0 __kvmalloc_node_noprof+0x353/0x920 ntfs_attrlist_entry_rm+0x28d/0x510 ntfs_attr_record_rm+0x4dd/0xda0 ntfs_delete+0x50a/0xf90 ntfs_unlink+0x212/0x4d0 vfs_unlink+0x259/0xb00 filename_unlinkat+0x2db/0x5f0 __x64_sys_unlink+0x46/0x70 do_syscall_64+0x115/0x6a0 entry_SYSCALL_64_after_hwframe+0x77/0x7f Freed by task 511: kasan_save_stack+0x33/0x60 kasan_save_track+0x14/0x30 kasan_save_free_info+0x3b/0x60 __kasan_slab_free+0x5f/0x80 kfree+0x307/0x580 ntfs_attrlist_entry_add+0xc1b/0x1110 ntfs_resident_attr_record_add+0x6a8/0xa10 ntfs_attr_add+0x682/0xcd0 __ntfs_link+0x815/0xde0 ntfs_link+0x223/0x3d0 vfs_link+0x465/0xcf0 filename_linkat+0x316/0x540 __x64_sys_link+0x81/0xb0 do_syscall_64+0x115/0x6a0 entry_SYSCALL_64_after_hwframe+0x77/0x7f
Add a per-ntfs_inode attr_list_lock and couple it to ntfs_attr_search_ctx. Search contexts that may carry attr-list cursors take the read side for their full lifetime, so ctx->al_entry remains protected until ntfs_attr_put_search_ctx(). Attribute-list mutation paths use the write side while deriving offsets from the old list and publishing the replacement list. Contexts used under runlist.lock use an explicit NONE mode so the ordering stays:
mrec_lock => attr_list_lock => runlist.lock
--- fs/ntfs/aops.c | 55 +++- fs/ntfs/attrib.c | 783 +++++++++++++++++++++++++++++++++++++++++++---------- fs/ntfs/attrib.h | 45 ++- fs/ntfs/attrlist.c | 93 +++++-- fs/ntfs/attrlist.h | 1 + fs/ntfs/compress.c | 6 +- fs/ntfs/dir.c | 76 +++++- fs/ntfs/dir.h | 2 + fs/ntfs/ea.c | 2 +- fs/ntfs/file.c | 164 ++++++++--- fs/ntfs/index.c | 261 +++++++++++++++--- fs/ntfs/index.h | 3 + fs/ntfs/inode.c | 417 ++++++++++++++++++++++------ fs/ntfs/inode.h | 17 ++ fs/ntfs/iomap.c | 154 +++++++++-- fs/ntfs/iomap.h | 3 + fs/ntfs/logfile.c | 4 + fs/ntfs/mft.c | 63 +++-- fs/ntfs/namei.c | 19 +- fs/ntfs/ntfs.h | 1 + fs/ntfs/reparse.c | 30 +- fs/ntfs/super.c | 13 +- 22 files changed, 1792 insertions(+), 420 deletions(-)
fs/ntfs/aops.c +49 -6
@@ -67,7 +67,7 @@ static const struct iomap_read_ops ntfs_iomap_bio_read_ops = {* * Return: 0 on success, or -errno on error. */ -static int ntfs_read_folio (struct file *file,struct folio *folio) +static int __ntfs_read_folio (struct folio *folio,const struct iomap_ops *ops) {struct ntfs_inode *ni =NTFS_I (folio->mapping->host);struct iomap_read_folio_ctx ctx = {
@@ -97,10 +97,25 @@ static int ntfs_read_folio(struct file *file, struct folio *folio)return ntfs_read_compressed_block (folio); } -iomap_read_folio (&ntfs_read_iomap_ops, &ctx,NULL ); +iomap_read_folio (ops, &ctx,NULL );return 0 ; } +static int ntfs_read_folio (struct file *file,struct folio *folio) +{ +return __ntfs_read_folio (folio, &ntfs_read_iomap_ops); +} + +static int ntfs_read_folio_nested (struct file *file,struct folio *folio) +{ +return __ntfs_read_folio (folio, &ntfs_read_iomap_nested_ops); +} + +int ntfs_read_folio_nolock (struct file *file,struct folio *folio) +{ +return __ntfs_read_folio (folio, &ntfs_read_iomap_nolock_ops); +} +/* * ntfs_bmap - map logical file block to physical device block * @mapping: address space mapping to which the block to be mapped belongs
@@ -131,6 +146,7 @@ static sector_t ntfs_bmap(struct address_space *mapping, sector_t block)s64 lcn;unsigned long blocksize, flags;struct ntfs_inode *ni =NTFS_I (mapping->host); +struct ntfs_inode *attr_lock_ni;struct ntfs_volume *vol = ni->vol;unsigned int delta;unsigned char blocksize_bits;
@@ -160,10 +176,12 @@ static sector_t ntfs_bmap(struct address_space *mapping, sector_t block)*/ if (unlikely (ofs >= size || (ofs + blocksize > size && size < i_size))) goto hole; + attr_lock_ni =ntfs_attr_list_lock (ni,NTFS_ATTR_CTX_LOCK_READ );down_read (&ni->runlist.lock); lcn =ntfs_attr_vcn_to_lcn_nolock (ni,ntfs_bytes_to_cluster (vol, ofs), false);up_read (&ni->runlist.lock); +ntfs_attr_list_unlock (attr_lock_ni,NTFS_ATTR_CTX_LOCK_READ );if (unlikely (lcn <LCN_HOLE )) {/* * Step down to an integer to avoid gcc doing a long long
@@ -222,7 +240,8 @@ static sector_t ntfs_bmap(struct address_space *mapping, sector_t block)return block; } -static void ntfs_readahead (struct readahead_control *rac) +static void __ntfs_readahead (struct readahead_control *rac, +const struct iomap_ops *ops) {struct address_space *mapping = rac->mapping;struct inode *inode = mapping->host;
@@ -238,7 +257,17 @@ static void ntfs_readahead(struct readahead_control *rac)*/ if (!NInoNonResident (ni) ||NInoCompressed (ni))return ; -iomap_readahead (&ntfs_read_iomap_ops, &ctx,NULL ); +iomap_readahead (ops, &ctx,NULL ); +} + +static void ntfs_readahead (struct readahead_control *rac) +{ +__ntfs_readahead (rac, &ntfs_read_iomap_ops); +} + +static void ntfs_readahead_nested (struct readahead_control *rac) +{ +__ntfs_readahead (rac, &ntfs_read_iomap_nested_ops); } static int ntfs_writepages(struct address_space *mapping,
@@ -291,9 +320,23 @@ const struct address_space_operations ntfs_aops = { .swap_activate = ntfs_swap_activate, }; +const struct address_space_operations ntfs_system_aops = { + .read_folio = ntfs_read_folio_nested, + .readahead = ntfs_readahead_nested, + .writepages = ntfs_writepages, + .dirty_folio = iomap_dirty_folio, + .bmap = ntfs_bmap, + .migrate_folio = filemap_migrate_folio, + .is_partially_uptodate = iomap_is_partially_uptodate, + .error_remove_folio = generic_error_remove_folio, + .release_folio = iomap_release_folio, + .invalidate_folio = iomap_invalidate_folio, + .swap_activate = ntfs_swap_activate, +}; +const struct address_space_operations ntfs_mft_aops = { - .read_folio = ntfs_read_folio, - .readahead = ntfs_readahead, + .read_folio = ntfs_read_folio_nested, + .readahead = ntfs_readahead_nested, .writepages = ntfs_mft_writepages, .dirty_folio = iomap_dirty_folio, .bmap = ntfs_bmap,
fs/ntfs/attrib.c +634 -149
1,579 lines not on this page · raw message
fs/ntfs/attrib.h +42 -3
112 lines not on this page · raw message
fs/ntfs/attrlist.c +76 -17
188 lines not on this page · raw message
fs/ntfs/attrlist.h +1 -0
@@ -15,6 +15,7 @@int ntfs_attrlist_need (struct ntfs_inode *ni);int ntfs_attrlist_entry_add (struct ntfs_inode *ni,struct attr_record *attr);int ntfs_attrlist_entry_rm (struct ntfs_attr_search_ctx *ctx); +int ntfs_attrlist_update_nolock (struct ntfs_inode *base_ni);int ntfs_attrlist_update (struct ntfs_inode *base_ni);#endif /* defined _NTFS_ATTRLIST_H */
fs/ntfs/compress.c +5 -1
@@ -1287,6 +1287,7 @@ static int ntfs_write_cb(struct ntfs_inode *ni, loff_t pos, struct page **pages,/* more compressed zeroes, to be followed by some count */ static char morezeroes[] = {0x03 ,0xb0 ,0x02 ,0x00 };struct page **pages_disk =NULL , *pg; +struct ntfs_inode *attr_lock_ni;s64 bio_lcn;struct runlist_element *rlc, *rl;int i, err;
@@ -1390,10 +1391,12 @@ static int ntfs_write_cb(struct ntfs_inode *ni, loff_t pos, struct page **pages, } bio_lcn = rlc->lcn; + attr_lock_ni =ntfs_attr_list_lock (ni,NTFS_ATTR_CTX_LOCK_WRITE );down_write (&ni->runlist.lock); rl =ntfs_runlists_merge (&ni->runlist, rlc,0 , &new_rl_count);if (IS_ERR (rl)) {up_write (&ni->runlist.lock); +ntfs_attr_list_unlock (attr_lock_ni,NTFS_ATTR_CTX_LOCK_WRITE );ntfs_error (vol->sb,"Failed to merge runlists" ); err =PTR_ERR (rl);if (ntfs_cluster_free_from_rl (vol, rlc))
@@ -1405,8 +1408,9 @@ static int ntfs_write_cb(struct ntfs_inode *ni, loff_t pos, struct page **pages, ni->runlist.count = new_rl_count; ni->runlist.rl = rl; - err =ntfs_attr_update_mapping_pairs (ni,0 );up_write (&ni->runlist.lock); + err =ntfs_attr_update_mapping_pairs_locked (ni,0 , &attr_lock_ni); +ntfs_attr_list_unlock (attr_lock_ni,NTFS_ATTR_CTX_LOCK_WRITE );if (err) { err = -EIO ; goto out;
fs/ntfs/dir.c +62 -14
157 lines not on this page · raw message
fs/ntfs/dir.h +2 -0
@@ -27,6 +27,8 @@ extern __le16 I30[5];u64 ntfs_lookup_inode_by_name (struct ntfs_inode *dir_ni,const __le16 *uname,const int uname_len,struct ntfs_name **res); +int ntfs_check_empty_dir_nolock (struct ntfs_inode *ni, +struct mft_record *ni_mrec);int ntfs_check_empty_dir (struct ntfs_inode *ni,struct mft_record *ni_mrec);#endif /* _LINUX_NTFS_FS_DIR_H */
fs/ntfs/ea.c +1 -1
@@ -587,7 +587,7 @@ static int ntfs_new_attr_flags(struct ntfs_inode *ni, __le32 fattr)if (IS_ERR (m))return PTR_ERR (m); - ctx =ntfs_attr_get_search_ctx (ni, m); + ctx =ntfs_attr_get_search_ctx (ni, m,NTFS_ATTR_CTX_LOCK_READ );if (!ctx) { err = -ENOMEM ; goto err_out;
fs/ntfs/file.c +129 -35
243 lines not on this page · raw message
fs/ntfs/index.c +217 -44
508 lines not on this page · raw message
fs/ntfs/index.h +3 -0
@@ -39,6 +39,7 @@* @block_size: size of index blocks in bytes (from $INDEX_ROOT or $Boot) * @vcn_size_bits: log2(cluster size) * @sync_write: true if synchronous writeback is requested for this context +* @attr_list_lock_mode: lock mode to use when reacquiring @actx * * @idx_ni is the index inode this context belongs to. *
@@ -87,6 +88,7 @@ struct ntfs_index_context {u32 block_size;u8 vcn_size_bits;bool sync_write; +enum ntfs_attr_search_ctx_lock_mode attr_list_lock_mode; }; int ntfs_index_root_inconsistent(struct ntfs_volume *vol,
@@ -107,6 +109,7 @@ int ntfs_index_lookup(const void *key, const u32 key_len,void ntfs_index_entry_mark_dirty (struct ntfs_index_context *ictx);int ntfs_index_add_filename (struct ntfs_inode *ni,struct file_name_attr *fn,u64 mref); +int ntfs_index_remove_nested (struct ntfs_inode *ni,const void *key,const u32 keylen);int ntfs_index_remove (struct ntfs_inode *ni,const void *key,const u32 keylen);struct ntfs_inode *ntfs_ia_open (struct ntfs_index_context *icx,struct ntfs_inode *ni);struct index_entry *ntfs_index_walk_down (struct index_entry *ie,struct ntfs_index_context *ictx);
fs/ntfs/inode.c +333 -84
35 lines not on this page · raw message
fs/ntfs/iomap.c +124 -30
154 lines not on this page · raw message
fs/ntfs/mft.c +46 -17
63 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
Signature · 1 line
2.43.0
Part of the diff is not shown here. View the raw message.
Hyunchul Lee
maintainerHi Cen,
This approach introduces too many changes. I apologize as I originally recommended this design.
Therefore, I am now considering alternative approaches:
Design A:
* Eliminates the raw pointer (ctx->al_entry) entirely from search_ctx.
* Instead, it tracks the cursor using 'al_cursor.off' (offset within the buffer) and 'al_cursor.gen' (generation counter matching base_ni->attr_list_gen).
* If the generation counter changes, the lookup detects this and restarts safely using an exact search key (al_exact).
* Since we no longer carry raw pointers, attr_list_lock only needs to be held briefly during in-memory list operations.
* To strictly maintain the lock order (runlist.lock -> attr_list_lock) and prevent deadlocks, functions that previously held attr_list_lock while calling runlist-locking operations must be refactored.
* Pros:
- Short-lived locks.
- Narrow lock scope makes it easy to maintain codes.
* Cons:
- More complex implementation.
Design B (runlist.lock style):
* Retains ctx->al_entry (raw pointers), but manages attr_list_lock as a top-level coarse-grained lock similar to runlist.lock.
* We must establish a strict global lock hierarchy: runlist.lock -> attr_list_lock.
* Pros:
- Simpler implementation.
* Cons:
- Could reduce performance.
- Wide lock scope makes it difficult to maintain codes.
- May introduce a lot of '_nolock' helper functions to avoid self-deadlock.
Which approaches do you think would be better, or other approaches? And please let me know if you would like me to implement it.
Hi Hyunchul.
Thanks.
Quoted 31 lines
Design A:
* Eliminates the raw pointer (ctx->al_entry) entirely fromsearch_ctx.
* Instead, it tracks the cursor using 'al_cursor.off' (offset within the buffer) and 'al_cursor.gen' (generation counter matchingbase_ni->attr_list_gen).
* If the generation counter changes, the lookup detects this and restarts safely using an exact search key (al_exact).
* Since we no longer carry raw pointers,attr_list_lockonly needs to be held briefly during in-memory list operations.
* To strictly maintain the lock order (runlist.lock ->attr_list_lock) and prevent deadlocks, functions that previously heldattr_list_lockwhile calling runlist-locking operations must be refactored.
* Pros:
- Short-lived locks.
- Narrow lock scope makes it easy to maintain codes.
* Cons:
- More complex implementation.Design B (runlist.lock style):
* Retainsctx->al_entry(raw pointers), but managesattr_list_lockas a top-level coarse-grained lock similar to runlist.lock.
* We must establish a strict global lock hierarchy: runlist.lock ->attr_list_lock.
* Pros:
- Simpler implementation.
* Cons:
- Could reduce performance.
- Wide lock scope makes it difficult to maintain codes.
- May introduce a lot of '_nolock' helper functions to avoid self-deadlock.
Quoted 1 line
Which approaches do you think would be better, or other approaches?
I think Design A is the better long-term direction.
Design B is simpler at first glance, but I think v5 already shows its main problem. As long as search_ctx keeps ctx->al_entry as a raw pointer into base_ni->attr_list, the lock has to cover the whole lifetime of that pointer. Then attr_list_lock starts to spread through many helper layers, and we need more _nolock variants or nested-lock variants to avoid self-deadlocks and lock ordering problems. That makes the patch large and hard to maintain.
Design A looks more complex internally, but it avoids the root problem: the search context would no longer carry a raw pointer into a replaceable buffer. Using an offset plus generation counter, and restarting the lookup when the generation changes, seems easier to reason about than keeping a wide attr_list_lock scope across many NTFS paths.
Quoted 1 line
And please let me know if you would like me to implement it.
I have limited bandwidth at the moment, and you know this code much better than I do. So if you are willing to take the implementation side, I would really appreciate it. I will also help review and test it so we can move this larger fix forward together. :)
Best regards,
Cen Zhang