From: Aldo Ariel Panzardo <qwe.aldo@gmail.com>
To: gregkh@linuxfoundation.org, rafael@kernel.org, dakr@kernel.org
Cc: driver-core@lists.linux.dev, linux-kernel@vger.kernel.org,
stable@vger.kernel.org, Aldo Ariel Panzardo <qwe.aldo@gmail.com>,
"sashiko . dev" <sashiko-bot@kernel.org>
Subject: [PATCH v2 2/2] debugfs: serialize concurrent writers in debugfs_write_file_str()
Date: Sat, 26 Sep 2026 10:48:13 -0300 [thread overview]
Message-ID: <20260926134813.1063620-1-qwe.aldo@gmail.com> (raw)
In-Reply-To: <2026092647-ebony-unlatch-d3d0@gregkh>
debugfs_write_file_str() reads the current string pointer into `old`,
builds a replacement, publishes it with rcu_assign_pointer(), then frees
`old` after synchronize_rcu(). There is no mutual exclusion between
concurrent writers: two simultaneous calls can both load the same `old`
pointer before either publishes a replacement, and when both later call
kfree(old), the result is a double-free.
Serialize writers with inode_lock(). Use rcu_dereference_protected() to
load `old` under the held lock, satisfying lockdep annotation. Release
the lock before synchronize_rcu() + kfree() so RCU readers are not
stalled during the grace period.
Tested on QEMU/KVM (7.3.0-rc4 + KASAN). A reproducer that spawns
concurrent write() calls on a debugfs string file triggers the
double-free reliably (~3900 KASAN reports in a single run):
BUG: KASAN: double-free in debugfs_write_file_str+0x194/0x200
Free of addr ffff888104542ec0 by task poc/305
Call Trace:
kasan_report_invalid_free+0xb8/0xe0
check_slab_allocation+0xd9/0x100
kfree+0x163/0x510
debugfs_write_file_str+0x194/0x200
vfs_write+0x1ed/0x920
ksys_write+0xe5/0x1a0
Allocated by task 307:
__kmalloc_noprof+0x264/0x680
debugfs_write_file_str+0x106/0x200
Freed by task 306:
kfree+0x2e5/0x510
debugfs_write_file_str+0x194/0x200
Cc: stable@vger.kernel.org
Assisted-by: sashiko.dev <sashiko-bot@kernel.org>
Signed-off-by: Aldo Ariel Panzardo <qwe.aldo@gmail.com>
---
fs/debugfs/file.c | 16 ++++++++++------
1 file changed, 10 insertions(+), 6 deletions(-)
diff --git a/fs/debugfs/file.c b/fs/debugfs/file.c
index a4b7a5cb5e01..f8e2d3c9b712 100644
--- a/fs/debugfs/file.c
+++ b/fs/debugfs/file.c
@@ -1060,38 +1060,42 @@ static ssize_t debugfs_write_file_str(struct file *file, const char __user *user
if (unlikely(r))
return r;
- old = *(char **)file->private_data;
+ inode_lock(file_inode(file));
+ old = rcu_dereference_protected(*(char __rcu **)file->private_data,
+ lockdep_is_held(&file_inode(file)->i_rwsem));
/* only allow strict concatenation */
r = -EINVAL;
if (pos && pos != strlen(old))
- goto error;
+ goto unlock;
r = -E2BIG;
if (pos + count + 1 > PAGE_SIZE)
- goto error;
+ goto unlock;
r = -ENOMEM;
new = kmalloc(pos + count + 1, GFP_KERNEL);
if (!new)
- goto error;
+ goto unlock;
if (pos)
memcpy(new, old, pos);
r = -EFAULT;
if (copy_from_user(new + pos, user_buf, count))
- goto error;
+ goto unlock;
new[pos + count] = '\0';
strim(new);
rcu_assign_pointer(*(char __rcu **)file->private_data, new);
+ inode_unlock(file_inode(file));
synchronize_rcu();
kfree(old);
debugfs_file_put(dentry);
return count;
-error:
+unlock:
+ inode_unlock(file_inode(file));
kfree(new);
debugfs_file_put(dentry);
return r;
--
2.43.0
next prev parent reply other threads:[~2026-09-26 13:48 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-25 17:58 [PATCH] debugfs: fix UAF and double-free in debugfs_str read/write Aldo Ariel Panzardo
2026-09-26 6:53 ` Greg KH
2026-09-26 13:47 ` [PATCH v2 0/2] " Aldo Ariel Panzardo
2026-09-26 13:47 ` [PATCH v2 1/2] debugfs: fix use-after-free in debugfs_read_file_str() Aldo Ariel Panzardo
2026-09-26 13:48 ` Aldo Ariel Panzardo [this message]
2026-09-26 14:09 ` [PATCH v3 0/2] debugfs: fix UAF and double-free in debugfs_str read/write Aldo Ariel Panzardo
2026-09-26 14:09 ` [PATCH v3 1/2] debugfs: fix use-after-free in debugfs_read_file_str() Aldo Ariel Panzardo
2026-09-26 15:53 ` Danilo Krummrich
2026-09-26 14:09 ` [PATCH v3 2/2] debugfs: serialize concurrent writers in debugfs_write_file_str() Aldo Ariel Panzardo
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260926134813.1063620-1-qwe.aldo@gmail.com \
--to=qwe.aldo@gmail.com \
--cc=dakr@kernel.org \
--cc=driver-core@lists.linux.dev \
--cc=gregkh@linuxfoundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=rafael@kernel.org \
--cc=sashiko-bot@kernel.org \
--cc=stable@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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®