Skip to contentSkip to conversation

btrfs: fix use-after-free on quota enable allocation failure

PATCH Superseded [email protected] 3 participants 6 messages Oct 7 Btrfs file system

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 btrfs: fix use-after-free on quota enable allocation failure

From: Yalagada Pavan Kumar <[email protected]>

The quota root remains on the transaction's dirty root list when kzalloc_obj() fails and btrfs_quota_enable() releases it without aborting the transaction. Later add_root_to_dirty_list() then accesses the freed dirty_list, causing a slab-use-after-free.

Abort the transaction on allocation failure to clean up the dirty root before releasing the quota root.

Reported-by [email protected]
Fixes 8d54518b5e52 ("btrfs: qgroup: pre-allocate btrfs_qgroup to reduce GFP_ATOMIC usage")
---
 fs/btrfs/qgroup.c | 1 +
 1 file changed, 1 insertion(+)
fs/btrfs/qgroup.c +1 -0
index f68b696b4bf7..42be06675c90 100644
@@ -1204,6 +1204,7 @@ int btrfs_quota_enable(struct btrfs_fs_info *fs_info,
 	prealloc = kzalloc_obj(*prealloc, GFP_NOFS);
 	if (!prealloc) {
 		ret = -ENOMEM;
+		btrfs_abort_transaction(trans, ret);
 		goto out_free_path;
 	}
 	qgroup = add_qgroup_rb(fs_info, prealloc, BTRFS_FS_TREE_OBJECTID);
Signature · 1 line
2.43.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 7 lines · 在 2026/10/7 19:36, [email protected] 写道:

From: Yalagada Pavan Kumar <[email protected]>

The quota root remains on the transaction's dirty root list when kzalloc_obj() fails and btrfs_quota_enable() releases it without aborting the transaction. Later add_root_to_dirty_list() then accesses the freed dirty_list, causing a slab-use-after-free.

Quoted 7 lines

Abort the transaction on allocation failure to clean up the dirty root before releasing the quota root.

Reported-by [email protected]
Fixes 8d54518b5e52 ("btrfs: qgroup: pre-allocate btrfs_qgroup to reduce GFP_ATOMIC usage")
Quoted 16 lines
---
  fs/btrfs/qgroup.c | 1 +
  1 file changed, 1 insertion(+)
fs/btrfs/qgroup.c +1 -0
index f68b696b4bf7..42be06675c90 100644
@@ -1204,6 +1204,7 @@ int btrfs_quota_enable(struct btrfs_fs_info *fs_info,
  	prealloc = kzalloc_obj(*prealloc, GFP_NOFS);
  	if (!prealloc) {
  		ret = -ENOMEM;
+		btrfs_abort_transaction(trans, ret);
  		goto out_free_path;
  	}
  	qgroup = add_qgroup_rb(fs_info, prealloc, BTRFS_FS_TREE_OBJECTID);
#2 lore raw
Quoted 24 lines · Qu Wenruo
Quoted 7 lines · 在 2026/10/7 19:36, [email protected] 写道:

From: Yalagada Pavan Kumar <[email protected]>

The quota root remains on the transaction's dirty root list when kzalloc_obj() fails and btrfs_quota_enable() releases it without aborting the transaction. Later add_root_to_dirty_list() then accesses the freed dirty_list, causing a slab-use-after-free.

Quoted 7 lines

Abort the transaction on allocation failure to clean up the dirty root before releasing the quota root.

Reported-by [email protected]
Fixes 8d54518b5e52 ("btrfs: qgroup: pre-allocate btrfs_qgroup to reduce GFP_ATOMIC usage")
Quoted 4 lines

---
  fs/btrfs/qgroup.c | 1 +

  1 file changed, 1 insertion(+)
Quoted 11 lines

fs/btrfs/qgroup.c int btrfs_quota_enable(struct btrfs_fs_info · line 1209

Quoted 11 lines
fs/btrfs/qgroup.c +1 -0
index f68b696b4bf7..42be06675c90 100644
@@ -1204,6 +1204,7 @@ int btrfs_quota_enable(struct btrfs_fs_info 
 *fs_info,
       prealloc = kzalloc_obj(*prealloc, GFP_NOFS);
       if (!prealloc) {
           ret = -ENOMEM;
+        btrfs_abort_transaction(trans, ret);
           goto out_free_path;

BTW, you are on an older branch.

The latest for-next branch has the commit "btrfs: qgroup: merge error labels in btrfs_quota_enable()", which changed the branch.

Next time check the MAINTAINERS file to grab the latest devel branch.

Quoted 3 lines
Quoted 2 lines
fs/btrfs/qgroup.c +0 -0
       }

      qgroup = add_qgroup_rb(fs_info, prealloc, BTRFS_FS_TREE_OBJECTID);

#3 lore raw
In reply to Qu Wenruo
Quoted 13 lines · Qu Wenruo
Quoted 10 lines · 在 2026/10/7 19:36, [email protected] 写道:

From: Yalagada Pavan Kumar <[email protected]>

The quota root remains on the transaction's dirty root list when kzalloc_obj() fails and btrfs_quota_enable() releases it without aborting the transaction. Later add_root_to_dirty_list() then accesses the freed dirty_list, causing a slab-use-after-free.

Abort the transaction on allocation failure to clean up the dirty root before releasing the quota root.

Quoted 6 lines
Quoted 4 lines
Reported-by [email protected]
Fixes 8d54518b5e52 ("btrfs: qgroup: pre-allocate btrfs_qgroup to reduce GFP_ATOMIC usage")

My bad, Sashiko exposed a valid but very rare race that the temporary quota_root can be added to fs_info->dirty_cowonly_roots list.

This requires enough subvolumes to make quota_root to be higher than level 0 in the first place, which is not common but definitely possible.

Although all readers of fs_info->dirty_cowonly_roots won't be reached after the transaction is aborted, there is still a very small window that another thread is already holding a trans handler just after the transaction is aborted and the quota root is freed.

In that case the other thread may access the already freed quota_root through fs_info->dirty_cowonly_roots->next.

I'm afraid we need to call list_del(&quota_root->dirty_list) with proper trans_lock hold during error handling.

Quoted 19 lines
Quoted 18 lines

---
  fs/btrfs/qgroup.c | 1 +

  1 file changed, 1 insertion(+)
fs/btrfs/qgroup.c +1 -0
index f68b696b4bf7..42be06675c90 100644
@@ -1204,6 +1204,7 @@ int btrfs_quota_enable(struct btrfs_fs_info 
 *fs_info,
       prealloc = kzalloc_obj(*prealloc, GFP_NOFS);
       if (!prealloc) {
           ret = -ENOMEM;
+        btrfs_abort_transaction(trans, ret);
           goto out_free_path;
       }

      qgroup = add_qgroup_rb(fs_info, prealloc, BTRFS_FS_TREE_OBJECTID);

#4 lore raw
Quoted 35 lines · Qu Wenruo
Quoted 13 lines · Qu Wenruo
Quoted 10 lines · 在 2026/10/7 19:36, [email protected] 写道:

From: Yalagada Pavan Kumar <[email protected]>

The quota root remains on the transaction's dirty root list when kzalloc_obj() fails and btrfs_quota_enable() releases it without aborting the transaction. Later add_root_to_dirty_list() then accesses the freed dirty_list, causing a slab-use-after-free.

Abort the transaction on allocation failure to clean up the dirty root before releasing the quota root.

Quoted 7 lines
Quoted 5 lines
Reported-by [email protected]
Fixes 8d54518b5e52 ("btrfs: qgroup: pre-allocate btrfs_qgroup to

reduce GFP_ATOMIC usage")

My bad, Sashiko exposed a valid but very rare race that the temporary quota_root can be added to fs_info->dirty_cowonly_roots list.

This requires enough subvolumes to make quota_root to be higher than level 0 in the first place, which is not common but definitely possible.

Although all readers of fs_info->dirty_cowonly_roots won't be reached after the transaction is aborted, there is still a very small window that another thread is already holding a trans handler just after the transaction is aborted and the quota root is freed.

Quoted 5 lines

In that case the other thread may access the already freed quota_root through fs_info->dirty_cowonly_roots->next.

I'm afraid we need to call list_del(&quota_root->dirty_list) with proper trans_lock hold during error handling.

Thanks, Qu. I understand.

I'll update the error handling to remove quota_root->dirty_list under trans_lock before dropping the quota root reference, based on the latest for-next. I'll send v2.

Thanks,
Pavan

Quoted 20 lines
Quoted 19 lines
Quoted 18 lines

---
  fs/btrfs/qgroup.c | 1 +

  1 file changed, 1 insertion(+)
fs/btrfs/qgroup.c +1 -0
index f68b696b4bf7..42be06675c90 100644
@@ -1204,6 +1204,7 @@ int btrfs_quota_enable(struct btrfs_fs_info
 *fs_info,
       prealloc = kzalloc_obj(*prealloc, GFP_NOFS);
       if (!prealloc) {
           ret = -ENOMEM;
+        btrfs_abort_transaction(trans, ret);
           goto out_free_path;
       }

      qgroup = add_qgroup_rb(fs_info, prealloc, BTRFS_FS_TREE_OBJECTID);

#5 lore raw
Quoted 39 lines · Yalagada Pavan Kumar
Quoted 38 lines · Qu Wenruo
Quoted 13 lines · Qu Wenruo
Quoted 10 lines · 在 2026/10/7 19:36, [email protected] 写道:

From: Yalagada Pavan Kumar <[email protected]>

The quota root remains on the transaction's dirty root list when kzalloc_obj() fails and btrfs_quota_enable() releases it without aborting the transaction. Later add_root_to_dirty_list() then accesses the freed dirty_list, causing a slab-use-after-free.

Abort the transaction on allocation failure to clean up the dirty root before releasing the quota root.

Quoted 7 lines
Quoted 5 lines
Reported-by [email protected]
Fixes 8d54518b5e52 ("btrfs: qgroup: pre-allocate btrfs_qgroup to

reduce GFP_ATOMIC usage")

My bad, Sashiko exposed a valid but very rare race that the temporary quota_root can be added to fs_info->dirty_cowonly_roots list.

This requires enough subvolumes to make quota_root to be higher than level 0 in the first place, which is not common but definitely possible.

Although all readers of fs_info->dirty_cowonly_roots won't be reached after the transaction is aborted, there is still a very small window that another thread is already holding a trans handler just after the transaction is aborted and the quota root is freed.

In that case the other thread may access the already freed quota_root through fs_info->dirty_cowonly_roots->next.

Quoted 7 lines
Quoted 2 lines

I'm afraid we need to call list_del(&quota_root->dirty_list) with proper trans_lock hold during error handling.

Thanks, Qu. I understand.

I'll update the error handling to remove quota_root->dirty_list under trans_lock before dropping the quota root reference, based on the latest for-next. I'll send v2.

I'm exploring the idea to put such list_del() into btrfs_put_root(), which should handle it more gracefully and cover all other situations where a new root is created and dirtied.

Thanks,
Qu

Quoted 25 lines

Thanks,
Pavan

Quoted 20 lines
Quoted 19 lines
Quoted 18 lines
---
   fs/btrfs/qgroup.c | 1 +
   1 file changed, 1 insertion(+)
fs/btrfs/qgroup.c +1 -0
index f68b696b4bf7..42be06675c90 100644
@@ -1204,6 +1204,7 @@ int btrfs_quota_enable(struct btrfs_fs_info
 *fs_info,
       prealloc = kzalloc_obj(*prealloc, GFP_NOFS);
       if (!prealloc) {
           ret = -ENOMEM;
+        btrfs_abort_transaction(trans, ret);
           goto out_free_path;
       }

      qgroup = add_qgroup_rb(fs_info, prealloc, BTRFS_FS_TREE_OBJECTID);