From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-lf1-f48.google.com (mail-lf1-f48.google.com [209.85.167.48]) (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 2812837E5D2 for ; Sun, 16 Aug 2026 10:51:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.48 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786877473; cv=none; b=TmMqBV45xm2qiHB9BMvo8LoieH/CvlNwD9RbNZYRspc5kHTa/hke7eqy5yBi4u+PN1CeWJbLFErTOy4teBzJ2GLrmKYCQoXsDNLkGSkukZ0+gc6gSeftPKAYcmMbQXQVbaokYTa7RfgcOrD7QaL6IzRDI2HI5oUQwGZh+z3sJS4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786877473; c=relaxed/simple; bh=CnISFodV5GRX1wXkycntu3zU/Ue7YG7mBasvqUAyKgE=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=qnWytS+UR/79WHogoeD/wqa1r58MnCWTwpXCGKBU7XVqMd7vmobzVeuRQNRGJuJlBqdmREZihMI3PL3RIV7U/2bd/+5WVZFhFLAizZjBe1xJIqsNnt3QECLpi/W+pMUYsgKdvJ5tHFm2cnarVxnWfw/UCdKVM6JmiJ1w6doCgDo= 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=qAoPniPi; arc=none smtp.client-ip=209.85.167.48 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="qAoPniPi" Received: by mail-lf1-f48.google.com with SMTP id 2adb3069b0e04-5aeb2bc82ccso2831127e87.2 for ; Sun, 16 Aug 2026 03:51:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1786877469; x=1787482269; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=SNWm5OZdk7ESNmUWJnmK7kcJsBEbFLnYr3hDCoz72XI=; b=qAoPniPi+LwKrK79srJKb4WUYiXIV8HxhMK0INBO13T49RIhr7S5++SmChttGqVj/Q yVm0akwh58LAj4B/lsQHN9LzjrXfh7ZshZYOG4tPaRo8A04yYiwbmU9mqCbfQLYzs8+j nddDz62VT2n9YraLfy49HtkFmubmsSNwziEO8mg9gcNDro0Elv598PtjqwqVnQIFTRWN wGv6+b4O6o9lyJU0nq6Zyds2MrXnPrWctpSOE/SkWEZh0zv3iI95aT9VgXSI+QQ+/eyW gS8TZTeEt2aoaRGd6xs99BD7G3acNJ76z/pZ99h6+H6m3VJtQIHUaddmENwbf0jG9u/H ZGxQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1786877469; x=1787482269; h=content-transfer-encoding:mime-version: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=SNWm5OZdk7ESNmUWJnmK7kcJsBEbFLnYr3hDCoz72XI=; b=sZ9s3yo/iN7f7zLm+R2xlwGShOHU4pJGrB69wC1zrBagkPKhcssDk1H/GMoDgAeCbL SV2htQgfienCEHoHA8Ck+4XjGujf8xzTBYF+Qw5TCVwLQt3AtMGrgwV0XdHyV57WOK3g lzId3hoiLTPkrIiKnF/wgyfqbJFjLtH9BB4TY7QEPKZz703J9OJafa9QsYghe0inK+W2 2MPav1rcI4No+DZ7txZ9i37u+3uDej8aHDPJ8tO4UpU7IGkKdtnlOwStmJnzuAurJFwP NMmdgbuCwuk8hnzzFr3c0SQvns/5k/Jf2E8a4NmKzCYc8OLyRRZf6wzzacXLJosHb6db LQ2w== X-Gm-Message-State: AOJu0Yzpq9C7nBs+cP001LjEFwgL3s68dDmJZp4M+md/Iwjj3+tDg5pP mrzUYG6nhSbhwEef2uP+G3nc9IVvoVezYeInPLhEeaYp0JKRIy7+Hsk5klsRGCR5 X-Gm-Gg: AR+sD10jh7Khb4lxo80+ZRwEHlL0EOJuuk+xFSP5i6H3aImfYKkVnRZYEI6YS6QnmJK lZttdHuxQvP5SjgOBDf7fh+mrKO6pdS4OEnAMF93Zc9ej8lcTIUJpjkvR+UyLUMvm/9RIqaqYb6 5JLo/R75ss+dwcM/VUesyiu+vgHmF1ZVlHoDcB6Yt/kzafm6xhFVsUjC5DNL2a6BVWDWm3QSzav Y7+uV2qx5TmlnOBcd46/gxqZU9RFf94OJwITZlqRVldBhep855Y11nYxF16uqyr6UcMSmjtGfxR BOAV5w5Wbol0bbBQ7vTE/y9mtvMGtU/eLfAG/W/5TAxGPb7CgBwAeTUX480SpkkoIwBG2VDX7bB 05PYeMT96jS4/IpyxDPANx15voPTcc+/Tf3H6lw2GyDTvqP01HBf0vFVukwENaRVyvKR/Og6h5/ ILEj6F91uk+g2ru1rm+b4NksDciZasnQHltnU+Dm1mFM0n7jmPszoXIZgj7nXzTsRsSN8mx1l7N 6Spa/RY/zcpyixeQTeF3aVU/JBNHqXJcGr5oLPzJbQPxRputMXWlMBeSOeHxjbeyw== X-Received: by 2002:a05:6512:668e:10b0:5b0:12cf:ae42 with SMTP id 2adb3069b0e04-5b45913c42fmr1479639e87.35.1786877469082; Sun, 16 Aug 2026 03:51:09 -0700 (PDT) Received: from debian.localdomain (95-25-156-171.broadband.corbina.ru. [95.25.156.171]) by smtp.gmail.com with ESMTPSA id 2adb3069b0e04-5b458ba4c08sm1748971e87.25.2026.08.16.03.51.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 16 Aug 2026 03:51:08 -0700 (PDT) From: Igor Putko To: hirofumi@mail.parknet.co.jp Cc: linux-kernel@vger.kernel.org, Igor Putko , syzbot+f72da8b30ddc89cc2371@syzkaller.appspotmail.com Subject: [PATCH] fat: fix data race in fat_add_entries() over unlocked buffer write Date: Sun, 16 Aug 2026 13:51:06 +0300 Message-ID: <20260816105106.3104-1-igorpetindev@gmail.com> X-Mailer: git-send-email 2.47.3 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit fat_add_entries() writes new directory entries directly into bh->b_data via memcpy() without holding the buffer lock. This is inconsistent with every other buffer-filling call site in this file (fat_zeroed_cluster(), fat_alloc_new_dir(), fat_add_new_entries()) and in fs/fat/fatent.c (fat_mirror_bhs()), all of which wrap their memcpy()/memset() calls in lock_buffer()/unlock_buffer() specifically "to avoid race with userspace read via bdev". Commit 07bfa4415ab6 ("fat: work around race with userspace's read via blockdev while mounting") added that locking to those four call sites in 2019, but missed the structurally identical pattern in fat_add_entries()'s "Second stage: filling the free entries with new entries" block, which writes both the long-name and short-name slots into pre-existing (already uptodate) buffer heads the same way. Since a FAT directory's buffer_head aliases a page in the backing block device's page cache, a concurrent write into that page via the bdev (e.g. through a loop device backed by shmem, as in the syzbot reproducer) races with this unlocked memcpy(), as reported by KCSAN. Wrap both the long-name and short-name slot writes in fat_add_entries() with lock_buffer()/unlock_buffer(), matching the pattern used everywhere else in this file. Reported-by: syzbot+f72da8b30ddc89cc2371@syzkaller.appspotmail.com Closes: https://syzkaller.appspot.com/bug?extid=f72da8b30ddc89cc2371 Fixes: 07bfa4415ab6 ("fat: work around race with userspace's read via blockdev while mounting") Signed-off-by: Igor Putko --- fs/fat/dir.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/fs/fat/dir.c b/fs/fat/dir.c index 35bdb6294..c9e2bf472 100644 --- a/fs/fat/dir.c +++ b/fs/fat/dir.c @@ -1394,7 +1394,10 @@ int fat_add_entries(struct inode *dir, void *slots, int nr_slots, /* Fill the long name slots. */ for (i = 0; i < long_bhs; i++) { int copy = umin(sb->s_blocksize - offset, size); + /* Avoid race with userspace read via bdev */ + lock_buffer(bhs[i]); memcpy(bhs[i]->b_data + offset, slots, copy); + unlock_buffer(bhs[i]); mmb_mark_buffer_dirty(bhs[i], &MSDOS_I(dir)->i_metadata_bhs); offset = 0; @@ -1406,7 +1409,10 @@ int fat_add_entries(struct inode *dir, void *slots, int nr_slots, if (!err && i < nr_bhs) { /* Fill the short name slot. */ int copy = umin(sb->s_blocksize - offset, size); + /* Avoid race with userspace read via bdev */ + lock_buffer(bhs[i]); memcpy(bhs[i]->b_data + offset, slots, copy); + unlock_buffer(bhs[i]); mmb_mark_buffer_dirty(bhs[i], &MSDOS_I(dir)->i_metadata_bhs); if (IS_DIRSYNC(dir)) -- 2.47.3