From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-dl2-f12.google.com (mail-dl2-f12.google.com [74.125.229.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 73785318BA8 for ; Sat, 26 Sep 2026 13:48:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.229.140 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790430504; cv=none; b=G1a13thcsVsg1ZEc4hk9OYrtgMILmoKhOPgjrj7E/sn4YzsXcsgP/iaSShRL5JFZxo58YNeUp1U6BHFM0W809r8TKuOaha+/JSNm4g6UDkFGd8qW2h0Clg+WgnyVnHeYkzvOWWeOuqed4j1OicTJhKufXJ7rqEkwyBRuLFZW6Po= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790430504; c=relaxed/simple; bh=bBJpFyWvbS+RZVCK6Git+1Asl9nlHVjk3YAQ3yUX9CQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=dH8XMIjgdFcXPYOWT4gD95XVmkIsX3Cf9JDAZenfn78MtJMgA3GcmG49yQRl2VS8wHwqaFLDG3btKMH8rQg6O0jJG7ptOaz0gBpP6+UEEke+HdYD2TkObxdiSdD0LusJsPsniixPZf88Xs57pE4rbWaBE0i5x/vL/xx5SXxGgso= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=GvkceulG; arc=none smtp.client-ip=74.125.229.140 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="GvkceulG" Received: by mail-dl2-f12.google.com with SMTP id a92af1059eb24-142dd04be84so2130644c88.3 for ; Sat, 26 Sep 2026 06:48:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1790430502; x=1791035302; darn=lists.linux.dev; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=HeFcGxb1uwCheJVrqnsn9Byp0eD2Vym48SlgBoqjnW0=; b=GvkceulGovG7WnS7uxwSgLnxDcZHhGyGzDtjawRTjEc77Xt13HIg8MTpts3oXU14kn Wz4wctC39E7iCEkft7BP6y4QZoBU2uUdopGJqTsQOliwyZTxJg5fpfaK1CY9nFmI5PKe oylcFaA7hXL5rpYV82bwCW5NtvaNimmDnpI1s845oryTfGvLMu4bBAxAGSQfZgBuTvY0 +n5W/imJKOyhytAJGHpcI5tiD+FhBKKBDeEUNkkx2FUwl4qWcDosiTpBqz4/o4Xg6B+N FcRFYFcJTNW+KfWFbb3HDfO+gguqdlWwBcmQYJOMOV4ovzntaKZ3upLWHrzx+NZOFX+4 83pQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1790430502; x=1791035302; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=HeFcGxb1uwCheJVrqnsn9Byp0eD2Vym48SlgBoqjnW0=; b=quoyAp85hu5m7ulPcEXZaDvua1d8VaVEOqTomua66MOFnbdcEqkjgSSlltXeGyZZgu 5AzAiMKOeUBu5u1Zs6dVQaV4x9MTY18seqQsVNzCMcpRwvmt5mRDUrn0sinxy5AkQFL/ 1Wyi2qVU2tf7LKjOTrooOd+lnRwZ7/o272pYVaNiIab5/+zT7jf3ZJsvRT2MRXcak9DZ INMVXcTUhnyOYtiv9hVargOuzTbRtWW0rSCaah2qZgixrqKtr0mE5TRCfbdeVYBY6Fv7 24dfP8zyXNDCayJGYGQAzF/MInX68PCrbbwx1cc38LbzgB9gKrLUikfx99EWrrIgdZHL PTRw== X-Gm-Message-State: AFuF++lqV3DxxyEQ/iY8mE/Dh8skLO3JhXWWfHwvDLjJYEuvdE/Q0ugk MX55KjRGqXmtvpySqk3VABiO9Rg0VqB3xXYGz2Z1oGM2MEup0k/hHSce X-Gm-Gg: AYBFou14yxydbpp1VwNdlDZVtmYd2NY66keYXHA7wSPt4nEtXm+WGTrFx9QfsV+FzSY VwrGsfvAGWFj4+n6oy5sv1QHRNnD0pjz3Vd+M5azlok4j/tfkTdwczlLjkqorW715f9Wl3efPK7 BqqE8604A9T2HmB3OFT1RrKXpPYGj8q7UlkKG0+7ZxnPbN5q9OBT9ptgm6MHLTh3FHutnp3JnCl WFT25nGQcYjX6eU/UW/JjMt9HZIURgBkbLsynhv97G1uX4cjYmXoLph6Nu0w0z1roA04z38bIzQ Pzgj7kShO5XQtko0x5PiCBZr1+uXYfoOVNlA0xGhgytLO9pPs357AmgTt1leGPkLY1fQrLzfUc1 /2EO91XVQoA0VrlC1l5CB4ZWnVuhhBPfeJEbKasghN2p58C+JgleLKIucauCk9Dd6j7Lk7/p9gk 5j5PhzG//JOnwYrF8sQTRNdz1s2YzSQTHw5K1TbtXoz3cZJBkngvvXLdiO5Uq5XSc= X-Received: by 2002:a05:7022:b055:10b0:145:bd2a:525a with SMTP id a92af1059eb24-146cfbd49a5mr3248533c88.26.1790430502451; Sat, 26 Sep 2026 06:48:22 -0700 (PDT) Received: from beelink.. ([187.13.30.172]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-34144172de6sm23530135eec.9.2026.09.26.06.48.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 26 Sep 2026 06:48:22 -0700 (PDT) From: Aldo Ariel Panzardo 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 , "sashiko . dev" Subject: [PATCH v2 2/2] debugfs: serialize concurrent writers in debugfs_write_file_str() Date: Sat, 26 Sep 2026 10:48:13 -0300 Message-ID: <20260926134813.1063620-1-qwe.aldo@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <2026092647-ebony-unlatch-d3d0@gregkh> References: <2026092647-ebony-unlatch-d3d0@gregkh> Precedence: bulk X-Mailing-List: driver-core@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 Signed-off-by: Aldo Ariel Panzardo --- 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