From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 124052512F5 for ; Mon, 8 Dec 2025 18:48:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765219734; cv=none; b=VgaP2HdHIOsmIuuTlxwKBjvg2ap9OBBSZRFjXRwzjXfZFq1d1Q2lQxHECsEK4krf6IygQ4Vu2JAuH/w9ZwBoMyc7IYaGAk+I/jzjCHcC+X2s2DNojPEDyitQhj/7EN+KZUpxuVzOk6jtGN/w2jSZ7Y7RG+BZnK9WKrlCfVvEzZc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765219734; c=relaxed/simple; bh=uL8MklYiIuc/c3jR+rO3/595sE8HkECV2Mdcf0+gr5s=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=qW2ho791Z1ACQETxzrosTi8gunELFE0f/TH37+upTexpocRPYMrXmjy+CzcMYR9k7cHG/xbksA1mtScIGNtbTh/ojIxzhDoADMGo8+eI+ZK2yXfHj5pbvBoyyH7eMJzZ1IlbmLQ0Ytq6CR7ywqIwTfsRmKTx8aXVHpipullESKM= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=bA0Fsga1; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b=fSZRDdQZ; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="bA0Fsga1"; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b="fSZRDdQZ" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1765219732; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=h1R3Yr2a3oOfgX7ss1REjahyyeDQP27kHipQkKLgF34=; b=bA0Fsga1QVcjfSQf99vp3m/Bbs24oUsfIF6v7Nczm3hIJHOE7Xn+NFbXX1qsoPi7uJERb0 gXxvr7cTxLjwxkILApAqPfdSWwROlhsD4oxlwvX4QlIbiKfa/WP/Z7Oh0VoKO58wFKt4W3 Es5rtma5r9v2wRnLYImm5oAvpoBTdJ4= Received: from mail-pl1-f197.google.com (mail-pl1-f197.google.com [209.85.214.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-125-kO_QIZYePQ28Ybe9Yo1dFg-1; Mon, 08 Dec 2025 13:48:45 -0500 X-MC-Unique: kO_QIZYePQ28Ybe9Yo1dFg-1 X-Mimecast-MFC-AGG-ID: kO_QIZYePQ28Ybe9Yo1dFg_1765219724 Received: by mail-pl1-f197.google.com with SMTP id d9443c01a7336-2958a134514so25171265ad.2 for ; Mon, 08 Dec 2025 10:48:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1765219724; x=1765824524; 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; bh=h1R3Yr2a3oOfgX7ss1REjahyyeDQP27kHipQkKLgF34=; b=fSZRDdQZ/ORtSFzaWzb2hpyhN+eMnhKA/u6ataIxxSyzI6RlAeJyRfKkdWbHZrSnHN LhWkQzzGTAjqagCh4tX1zEp4d7GJQBdfVlLWBwfgxF0q+o/6d1VFWjp9DcgGkgZNX7wC B4ZMrV9xaHHd2kelQ9TdhQdC0ruuaq/C9kF0E4WKoFXql+GJGGvqcgMTYAs203twq64/ VebSwf4MhhqyOfNARS/9hAXZlRMceeMF5d0/fXDAWDPGtmtU3NDsYquT3Vn+g7AlPs0x lzIlc7eHqwbAwEE4Yz0zfhN+xtHg9rpwxUywYK7TOipiAuNWpROAy7njZ9rgu66yK8mu Qfvg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1765219724; x=1765824524; 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; bh=h1R3Yr2a3oOfgX7ss1REjahyyeDQP27kHipQkKLgF34=; b=Bz5DlzDxC9zG8Qp5HCFuHay1t+nZ3Lhx/OSiNzoaAgXuKAzXbByXxKqqxkJvJ24Xgi TrOIXqcLDo0vHgVsESjh4iif0MAFRzPEspSt5Segf0mt85AQGqi7I+xfO4wXBPZ0HhQH VM0Zbdk8t6ZQhsUlS+HMOVsuYi/2lxYsB8YveVXOZ/wj7dv18B1S/0T/5atGi3Tn2XrA lnnqXmEzBFsEwORc95OTlMVRmHrRXuKONEDKpPMjuaI6MTA1I7mmJhEyvqSvaAFm2BMr 3ITc+v9XeaKNgW3Yjz9P/qiWs1To7Rc9goHhsSr0yA5UfEc8y4IvVndkIfD/i/ixfD/A x3OA== X-Forwarded-Encrypted: i=1; AJvYcCV/QR2Sugp+8GvfzO9Js15mF/dj64jASV+XGlD9nYUck/lI7Q6jc2wssyC/4qjMkUTHXksBx4mEyfOawQg=@vger.kernel.org X-Gm-Message-State: AOJu0Yz6t8l/ReMZC9shhMCiFZinxHI+otudJohUo2D6VsT6mAnxJUnN js+h7RJQc1BxshIrLULkWUCVt+8zr+ygv14l4NYDYPB605fsrFu4Um7A2XBgwhdFtptkjPQOwM/ JH55DXVV6uHtSZKKB9lqX1807SCyKgwFEooRBlda7OXAEhx/vy9hsU7Nll8kihUyL X-Gm-Gg: ASbGnctJPenKngrxPieElXKteVOLCBJ9naZushXvkDbTHDW2gUUsvkzXgbbpzUgxcSm SEqYOseg10+gvAGQ7/vzeC1vaHJNPh8jM6p4Bmi3x534oVNNIpb6aKH7LSFIXjdLef67qg26DbG y9hWnCbald8wHpp0gAWCmC5rtt6Td8Db7TCK7ed+VVVFDJnTUSnjmpNpUZyvKBF4d5sC7RhuHxo Jxbb8sr6G9rtvTsDRKjoK0LX+yMrO/pDDsmQed6V6TwAiSDb7vOmy3VVidpj9toJCSG4bIQg+rA 4UehjyTp989y4srqCJkJ/y9CuXJm0RtK+B8EfKEO2w3bi9KuglQrUuvmaI50DtyAe5dtRPtfDPQ uYqWGwnd1QXeSqhGpET+JqVJPMWz/LE1Azn74uw== X-Received: by 2002:a05:6a20:7283:b0:363:819f:4962 with SMTP id adf61e73a8af0-36618164a7amr8848471637.40.1765219724073; Mon, 08 Dec 2025 10:48:44 -0800 (PST) X-Google-Smtp-Source: AGHT+IEWyIr86JYQPxT5xn/4CPk5SaKSoq5UZgYSwhtzIMCxOKYzrI2S+Nb6uYNMu0pMo1X/14U5zg== X-Received: by 2002:a05:6a20:7283:b0:363:819f:4962 with SMTP id adf61e73a8af0-36618164a7amr8848450637.40.1765219723624; Mon, 08 Dec 2025 10:48:43 -0800 (PST) Received: from dkarn-thinkpadp16vgen1.punetw6.csb ([2402:e280:3e0d:a45:335a:59cd:dd80:b6bf]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-34a47aa570fsm48184a91.0.2025.12.08.10.48.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 08 Dec 2025 10:48:43 -0800 (PST) From: Deepakkumar Karn To: Alexander Viro Cc: Christian Brauner , Jan Kara , linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, syzbot+e07658f51ca22ab65b4e@syzkaller.appspotmail.com, syzkaller-bugs@googlegroups.com, Deepakkumar Karn Subject: [PATCH] fs: add NULL check in drop_buffers() to prevent null-ptr-deref Date: Tue, 9 Dec 2025 00:18:07 +0530 Message-ID: <20251208184807.514738-1-dkarn@redhat.com> X-Mailer: git-send-email 2.52.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit drop_buffers() dereferences the buffer_head pointer returned by folio_buffers() without checking for NULL. This leads to a null pointer dereference when called from try_to_free_buffers() on a folio with no buffers attached. This happens when filemap_release_folio() is called on a folio belonging to a mapping with AS_RELEASE_ALWAYS set but without release_folio address_space operation defined. In such case, folio_needs_release() returns true because of AS_RELEASE_ALWAYS flag, the folio has no private buffer data, causing the try_to_free_buffers() with a folio that has no buffers. Adding NULL check for the buffer_head pointer and return false early if no buffers are attached to the folio. Reported-by: syzbot+e07658f51ca22ab65b4e@syzkaller.appspotmail.com Closes: https://syzkaller.appspot.com/bug?extid=e07658f51ca22ab65b4e Fixes: 6439476311a6 ("fs: Convert drop_buffers() to use a folio") Signed-off-by: Deepakkumar Karn --- fs/buffer.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/fs/buffer.c b/fs/buffer.c index 838c0c571022..170d15a05d2d 100644 --- a/fs/buffer.c +++ b/fs/buffer.c @@ -480,7 +480,7 @@ EXPORT_SYMBOL(mark_buffer_async_write); * try_to_free_buffers() will be operating against the *blockdev* mapping * at the time, not against the S_ISREG file which depends on those buffers. * So the locking for i_private_list is via the i_private_lock in the address_space - * which backs the buffers. Which is different from the address_space + * which backs the buffers. Which is different from the address_space * against which the buffers are listed. So for a particular address_space, * mapping->i_private_lock does *not* protect mapping->i_private_list! In fact, * mapping->i_private_list will always be protected by the backing blockdev's @@ -771,7 +771,7 @@ EXPORT_SYMBOL(block_dirty_folio); * Do this in two main stages: first we copy dirty buffers to a * temporary inode list, queueing the writes as we go. Then we clean * up, waiting for those writes to complete. - * + * * During this second stage, any subsequent updates to the file may end * up refiling the buffer on the original inode's dirty list again, so * there is a chance we will end up with a buffer queued for write but @@ -848,7 +848,7 @@ static int fsync_buffers_list(spinlock_t *lock, struct list_head *list) brelse(bh); spin_lock(lock); } - + spin_unlock(lock); err2 = osync_buffers_list(lock, list); if (err) @@ -1000,7 +1000,7 @@ static sector_t blkdev_max_block(struct block_device *bdev, unsigned int size) /* * Initialise the state of a blockdev folio's buffers. - */ + */ static sector_t folio_init_buffers(struct folio *folio, struct block_device *bdev, unsigned size) { @@ -1546,7 +1546,7 @@ bool has_bh_in_lru(int cpu, void *dummy) { struct bh_lru *b = per_cpu_ptr(&bh_lrus, cpu); int i; - + for (i = 0; i < BH_LRU_SIZE; i++) { if (b->bhs[i]) return true; @@ -2166,7 +2166,7 @@ int __block_write_begin_int(struct folio *folio, loff_t pos, unsigned len, if (folio_test_uptodate(folio)) { if (!buffer_uptodate(bh)) set_buffer_uptodate(bh); - continue; + continue; } if (!buffer_uptodate(bh) && !buffer_delay(bh) && !buffer_unwritten(bh) && @@ -2468,7 +2468,7 @@ EXPORT_SYMBOL(block_read_full_folio); /* utility function for filesystems that need to do work on expanding * truncates. Uses filesystem pagecache writes to allow the filesystem to - * deal with the hole. + * deal with the hole. */ int generic_cont_expand_simple(struct inode *inode, loff_t size) { @@ -2893,6 +2893,10 @@ drop_buffers(struct folio *folio, struct buffer_head **buffers_to_free) struct buffer_head *head = folio_buffers(folio); struct buffer_head *bh; + /* In cases of folio without buffer_head*/ + if (!head) + return false; + bh = head; do { if (buffer_busy(bh)) -- 2.52.0