Skip to contentSkip to conversation

hugetlbfs: emit hugetlbfs_free_inode from ->destroy_inode()

PATCH In review Joas Antonio dos Santos 2 participants 3 messages Oct 29

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

PATCH 1/1 hugetlbfs: emit hugetlbfs_free_inode from ->destroy_inode()

hugetlbfs_free_inode() calls trace_hugetlbfs_free_inode(), and the hugetlbfs__inode event class records inode->i_sb->s_dev. ->free_inode() runs from an RCU callback (destroy_inode() queues i_callback() with call_rcu()), and Documentation/filesystems/porting.rst says that this callback "is *NOT* ordered wrt superblock destruction; as the matter of fact, the superblock and all associated structures might be already gone". put_super() frees the superblock with call_rcu() followed by a workqueue item (destroy_super_rcu() -> destroy_super_work()). syzbot hit this with the event enabled:

  BUG: KASAN: slab-use-after-free in trace_event_raw_event_hugetlbfs__inode+0x45b/0x4c0 include/trace/events/hugetlbfs.h:36
  Read of size 4 at addr ffff888038850010 by task syz-executor952/6134
   hugetlbfs_free_inode+0x15e/0x1f0 fs/hugetlbfs/inode.c:1182
   i_callback+0x46/0x80 fs/inode.c:317
   rcu_do_batch kernel/rcu/tree.c:2650 [inline]
   rcu_core+0x5f0/0x1220 kernel/rcu/tree.c:2919
  Allocated by task 6134:
   alloc_super fs/super.c:326 [inline]
   sget_fc+0x1ba/0x1f60 fs/super.c:874
   hugetlbfs_get_tree+0x1e7/0x2b0 fs/hugetlbfs/inode.c:1431
  Freed by task 855:
   kfree+0x22b/0x6d0 mm/slub.c:6923
   process_one_work+0xac7/0x1b10 kernel/workqueue.c:3495

Emit the event from hugetlbfs_destroy_inode() instead. destroy_inode() calls ->destroy_inode() synchronously, before it queues the RCU callback, while the superblock is still alive; hugetlbfs_destroy_inode() already dereferences inode->i_sb there. ->free_inode() is left with only the kmem_cache_free().

Fixes 014ad7c42a69 ("hugetlbfs: use tracepoints in hugetlbfs functions.")
Reported-by [email protected]
Assisted-by Yaga Agent HackerSec Claude:claude-opus-5-5

---
Tested on net/main 94cea342e891 (arm64, QEMU, generic KASAN, tracing enabled) with the syzbot C reproducer, which enables events/hugetlbfs/hugetlbfs_free_inode and races hugetlbfs mounts and lazy unmounts with open + unlink. Without this patch KASAN reports the slab-use-after-free in trace_event_raw_event_hugetlbfs__inode() from hugetlbfs_free_inode() <- i_callback() during the first 10 second run, on 3 of 3 boots. With it, 12 runs over 2 boots are clean, and the trace buffer still holds the hugetlbfs_free_inode events (about 46k per boot) with valid dev numbers. fs/hugetlbfs/inode.o builds cleanly with W=1.

The event now fires when the inode is destroyed instead of when its memory goes back to the slab cache, one RCU grace period earlier; the recorded fields are the same.

 fs/hugetlbfs/inode.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
fs/hugetlbfs/inode.c +5 -1
index 6656807..27a3937 100644
@@ -1179,12 +1179,16 @@ static struct inode *hugetlbfs_alloc_inode(struct super_block *sb)
 
 static void hugetlbfs_free_inode(struct inode *inode)
 {
-	trace_hugetlbfs_free_inode(inode);
 	kmem_cache_free(hugetlbfs_inode_cachep, HUGETLBFS_I(inode));
 }
 
 static void hugetlbfs_destroy_inode(struct inode *inode)
 {
+	/*
+	 * Not in ->free_inode(): that runs from an RCU callback, when the
+	 * superblock the event reads may already be freed.
+	 */
+	trace_hugetlbfs_free_inode(inode);
 	hugetlbfs_inc_free_inodes(HUGETLBFS_SB(inode->i_sb));
 }
 
Signature · 1 line
2.39.5
#1 lore raw
Quoted 66 lines · Joas Antonio dos Santos

hugetlbfs_free_inode() calls trace_hugetlbfs_free_inode(), and the hugetlbfs__inode event class records inode->i_sb->s_dev. ->free_inode() runs from an RCU callback (destroy_inode() queues i_callback() with call_rcu()), and Documentation/filesystems/porting.rst says that this callback "is *NOT* ordered wrt superblock destruction; as the matter of fact, the superblock and all associated structures might be already gone". put_super() frees the superblock with call_rcu() followed by a workqueue item (destroy_super_rcu() -> destroy_super_work()). syzbot hit this with the event enabled:

  BUG: KASAN: slab-use-after-free in trace_event_raw_event_hugetlbfs__inode+0x45b/0x4c0 include/trace/events/hugetlbfs.h:36
  Read of size 4 at addr ffff888038850010 by task syz-executor952/6134
   hugetlbfs_free_inode+0x15e/0x1f0 fs/hugetlbfs/inode.c:1182
   i_callback+0x46/0x80 fs/inode.c:317
   rcu_do_batch kernel/rcu/tree.c:2650 [inline]
   rcu_core+0x5f0/0x1220 kernel/rcu/tree.c:2919
  Allocated by task 6134:
   alloc_super fs/super.c:326 [inline]
   sget_fc+0x1ba/0x1f60 fs/super.c:874
   hugetlbfs_get_tree+0x1e7/0x2b0 fs/hugetlbfs/inode.c:1431
  Freed by task 855:
   kfree+0x22b/0x6d0 mm/slub.c:6923
   process_one_work+0xac7/0x1b10 kernel/workqueue.c:3495

Emit the event from hugetlbfs_destroy_inode() instead. destroy_inode() calls ->destroy_inode() synchronously, before it queues the RCU callback, while the superblock is still alive; hugetlbfs_destroy_inode() already dereferences inode->i_sb there. ->free_inode() is left with only the kmem_cache_free().

Fixes 014ad7c42a69 ("hugetlbfs: use tracepoints in hugetlbfs functions.")
Reported-by [email protected]
Assisted-by Yaga Agent HackerSec Claude:claude-opus-5-5

---
Tested on net/main 94cea342e891 (arm64, QEMU, generic KASAN, tracing enabled) with the syzbot C reproducer, which enables events/hugetlbfs/hugetlbfs_free_inode and races hugetlbfs mounts and lazy unmounts with open + unlink. Without this patch KASAN reports the slab-use-after-free in trace_event_raw_event_hugetlbfs__inode() from hugetlbfs_free_inode() <- i_callback() during the first 10 second run, on 3 of 3 boots. With it, 12 runs over 2 boots are clean, and the trace buffer still holds the hugetlbfs_free_inode events (about 46k per boot) with valid dev numbers. fs/hugetlbfs/inode.o builds cleanly with W=1.

The event now fires when the inode is destroyed instead of when its memory goes back to the slab cache, one RCU grace period earlier; the recorded fields are the same.

 fs/hugetlbfs/inode.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
fs/hugetlbfs/inode.c +0 -1
index 6656807..27a3937 100644
@@ -1179,12 +1179,16 @@ static struct inode *hugetlbfs_alloc_inode(struct super_block *sb)
 
 static void hugetlbfs_free_inode(struct inode *inode)
 {
-	trace_hugetlbfs_free_inode(inode);
 	kmem_cache_free(hugetlbfs_inode_cachep, HUGETLBFS_I(inode));
 }
 

fs/hugetlbfs/inode.c static struct inode *hugetlbfs_alloc_inode(struct super_block *sb) · line 1194

Quoted 10 lines
fs/hugetlbfs/inode.c +5 -0
 static void hugetlbfs_destroy_inode(struct inode *inode)
 {
+	/*
+	 * Not in ->free_inode(): that runs from an RCU callback, when the
+	 * superblock the event reads may already be freed.
+	 */
+	trace_hugetlbfs_free_inode(inode);
 	hugetlbfs_inc_free_inodes(HUGETLBFS_SB(inode->i_sb));
 }
 

Should we rename the tracepoint then?

Signature · 3 lines
Cheers,

David
#2 lore raw
Quoted 1 line · David Hildenbrand

Should we rename the tracepoint then?

Fair point, "free_inode" no longer matches where it fires.

I'd keep this patch as is, though, since it is tagged for stable:
the event name is visible to users in tracefs (events/hugetlbfs/hugetlbfs_free_inode), and renaming it in stable kernels would break existing tracing setups there just to fix the use-after-free.

I can send a follow-up on top of it for mm-unstable that renames the event to hugetlbfs_destroy_inode, which also matches the hugetlbfs_evict_inode name next to it. Would that work for you, or would you prefer the rename folded into a v2?

Thanks,
Joas