From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f50.google.com (mail-wm1-f50.google.com [209.85.128.50]) (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 B8F0C2DBF76 for ; Mon, 15 Dec 2025 07:55:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.50 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765785326; cv=none; b=XCJCmxF5qXJEKTVbz4CwNmpv+m8hZgI/WjYr8Dq9Vs0oGEV2A/eKaGMYPcmtBQUq8I6mIsuPaxi3M93tPZyiXk6S2ACnr4NL+0T85s+uF73olyDW5P4xOdjYrxFf1PFNYjLe7u7NZGcPRmz3hTluY0U1bacBe7XLGoszJqX15Cg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765785326; c=relaxed/simple; bh=rpWO1Lw+/wys8vqxU8weBkbLcPTQdb4u8Gynbmh1sJ0=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=hneKRqG+rcvCqfZio1mz2X2eSPA6OleFZnpbYZ9K58xJ/g9U5IzpWF11KGcTxivfii3H6kG1oY1IKeEZm8HNIMFvHdX/BBXZqyHaisqRNty4jMULxYZCqonFf7b7snwg9aJguT+yV4cmzpyX1ATKAyj8c/WrsSceMXVGRftw1rQ= 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=cvJoAThi; arc=none smtp.client-ip=209.85.128.50 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="cvJoAThi" Received: by mail-wm1-f50.google.com with SMTP id 5b1f17b1804b1-47774d3536dso30858175e9.0 for ; Sun, 14 Dec 2025 23:55:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1765785323; x=1766390123; darn=vger.kernel.org; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=oGiffYXDmLpUHcgTV053zI1JEa1BaS01NZOjhrqNB9E=; b=cvJoAThiLvIlCX5poTZrAivi0Le8zZ3nWV7LWL6Z5n3p6cFPYiiQzvpxqmas0OVzGE /5J9yllA3zIsYctVbhVG1ovSAFoIkthIizdYayWpgmtED8Thibd/it/sT+Ugp6Vhi5py uD1iL+9z3gEacI+sfVYPgwbMgRQA3YEk9uY3ka1NPZSzSHrUriayB85NH0VdJ48Sz2JD WZUZZWaqz/W366TZGuw8zadyiazYlHjU9o5tie2Y86pBC9wNvVRK5u34WH6PlqKQTqSg eeiqnDL2EwqJi412AoLtcjbFcRW/5IAXTme1WymXQlzigcTVIGB8AZ+fwbD+CVcfARMv 3s6w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1765785323; x=1766390123; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :x-gm-gg:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=oGiffYXDmLpUHcgTV053zI1JEa1BaS01NZOjhrqNB9E=; b=qvcN12gdIWZoI9elYI/+ABjqMtsbzTy9fJI6FDHqJVyAbvLMsypwmWzgdP2Nv101bp Ud2SA3ctTFdlR3SDe4iAPzp4Nd3ep7++ga9UkMbu0vBhQavYodeFn2fUf1uO7vjGMeBE mLBrwCWJOEOyMcK7wcYbu48bfYFWPHr1+QT7mGlU1nzuDaMiqYS2RiZQZOFO6J/qmTSv 7kQNFnYLGnUF71omeZ6fcPsarq8gJh44cT6hEoHOcDn2xg2RIOD42lEtMDKv8byoltKi O8lCCfEGfA5fJaQ3Z1Wxhh8+IWX75s8co2OKmLWbyWwMeA/FuUchV9a8uSsJFt9AMjia PSFQ== X-Gm-Message-State: AOJu0YxB0DwUgnfgA4JsN4M5+NW94lBEi+wbXUvmv8IR08XJojAf2s0S OEVWNTqPZDc6eEKXcTzpGuLJvCCRwijSXy3Bf4PeIoVG+Yt4TwOpbH35 X-Gm-Gg: AY/fxX49RFvK4kFw98n4FsBQo3ckhMkAKs6rGlND5Hl1RnmkrTR+Co8EvR6zxJhSH7g lbiTDO2iPFbVeoyV4QR8MS/JeQNaAuIwvwOEgO6ThuNdo0d46dtHEZpSfpqP3mb7+jDZLq6ntQZ MKOv71PaHqwANZTvDCOslx69IfEurt6GVa3pOJh+EQcDeSaadqBTIe3OtYjremQMaAtFbY0+k83 JZQJ4oE+sbxe25tBydecnCN11ExSpUPpFeVYnnaHzgCL381fAb/CnoqIyA/99o6TUcGqdLb4gSm XLgGFPx/epsnHjtWTd+Eg9NUVII+QZjODcsXUNQ6I6Zc0tX9tu6st23oG8PSnmY1rKXkt/atl/M bEy+HDnKJpX8Sz0PaWHEwdLM6Yh2sQKdet7j1lXXZzdouEImNVkjkwZVfY3RZamVmruZpzSFA2X qO8m1561sf X-Google-Smtp-Source: AGHT+IGJbVrc3MlQArX/ud5BCk9xqPXuXshLNiWcj9TIPp3lJaq6Fy4CI3xx/+d/DkJqU6yPxPjj+A== X-Received: by 2002:a05:600c:214d:b0:477:3fcf:368c with SMTP id 5b1f17b1804b1-47a89ecc6b0mr92023775e9.9.1765785322830; Sun, 14 Dec 2025 23:55:22 -0800 (PST) Received: from [10.66.66.2] ([46.10.223.24]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-430f984a268sm7798067f8f.1.2025.12.14.23.55.22 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 14 Dec 2025 23:55:22 -0800 (PST) Message-ID: <32789c5c-9223-4345-88e0-d806bd1b0b31@gmail.com> Date: Mon, 15 Dec 2025 09:55:21 +0200 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH] bfs: Fix calculation of offset when moving blocks To: aivazian.tigran@gmail.com Cc: linux-kernel@vger.kernel.org, syzbot+f51a2a34984e4d8888fd@syzkaller.appspotmail.com References: <20251124172815.100111-1-zlatistiv@gmail.com> Content-Language: en-US From: "Nikola Z. Ivanov" In-Reply-To: <20251124172815.100111-1-zlatistiv@gmail.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On 11/24/25 7:28 PM, Nikola Z. Ivanov wrote: > bfs_move_blocks performs an incorrect calculation when > looping over the blocks that are to be moved. > > When looping over the section of blocks: > > for (i = start; i <= end; i++) > if(bfs_move_block(i, where + i, sb)) { > > Here bfs_move_block will not move > blocks "start"-"end" to "where"-"where+size" > but instead it will add "i" (which is initialized to "start") > to "where", adding additional unintended offset. > > Example reproducer of the bug: > > dd if=/dev/zero of=./bfs.img count=200 bs=1M > mkfs.bfs ./bfs.img > losetup /dev/loop0 ./bfs.img > mount /dev/loop0 /mnt > dd if=/dev/urandom of=/mnt/file0 count=75 bs=1M > dd if=/dev/urandom of=/mnt/file1 count=25 bs=1M > dd if=/dev/urandom of=/mnt/file2 count=25 bs=1M > echo 123 >> /mnt/file1 > > Essentially this will create 3 files, file1 located > right between file0 and file2 on-disk. Writing more > data to file1 will cause bfs to attempt to move it > after file2, where we have enough space to store it, > but due to the incorrect calculation it will attempt > to move it past the end of device and trigger a NULL > pointer dereference in bfs_move_block. > > Even if we have enough space to account for the > unintended offset, the content of file1 will now be > garbage due to bfs_get_block expecting file1 to be > right after file2. > > For example: > > dd if=/dev/zero of=./bfs.img count=200 bs=1M > mkfs.bfs ./bfs.img > losetup /dev/loop0 ./bfs.img > mount /dev/loop0 /mnt > dd if=/dev/urandom of=/mnt/file0 count=1 bs=512 > dd if=/dev/urandom of=/mnt/file1 count=1 bs=512 > dd if=/dev/urandom of=/mnt/file2 count=1 bs=512 > sync; echo 1 > /proc/sys/vm/drop_caches > xxd /mnt/file1 > echo 123 >> /mnt/file1 > sync; echo 1 > /proc/sys/vm/drop_caches > xxd /mnt/file1 > > The hexdump reveals that our file now contains zeroes, > except for the "123\n" we echoed at the end. > > Fix this by correcting the calculation. Additionally add > a check for new == NULL inside bfs_move_block and propagate > the return value of bfs_move_block as a return value of > bfs_move_blocks in case of error. > > Reported-by: syzbot+f51a2a34984e4d8888fd@syzkaller.appspotmail.com > Closes: https://syzkaller.appspot.com/bug?extid=f51a2a34984e4d8888fd > Signed-off-by: Nikola Z. Ivanov > --- > The example reproducers might trigger a WARNING() in mark_buffer_dirty, > which is observed often when blocks are moved around, > syzbot has already hit that bug here: > https://syzkaller.appspot.com/bug?extid=2327bccb02eef9291c1c > I will follow up with a patch for that soon. > > fs/bfs/file.c | 17 ++++++++++++----- > 1 file changed, 12 insertions(+), 5 deletions(-) > > diff --git a/fs/bfs/file.c b/fs/bfs/file.c > index d33d6bde992b..649105018014 100644 > --- a/fs/bfs/file.c > +++ b/fs/bfs/file.c > @@ -40,6 +40,10 @@ static int bfs_move_block(unsigned long from, unsigned long to, > if (!bh) > return -EIO; > new = sb_getblk(sb, to); > + if (!new) { > + brelse(bh); > + return -EIO; > + } > memcpy(new->b_data, bh->b_data, bh->b_size); > mark_buffer_dirty(new); > bforget(bh); > @@ -51,14 +55,17 @@ static int bfs_move_blocks(struct super_block *sb, unsigned long start, > unsigned long end, unsigned long where) > { > unsigned long i; > + int err; > > dprintf("%08lx-%08lx->%08lx\n", start, end, where); > - for (i = start; i <= end; i++) > - if(bfs_move_block(i, where + i, sb)) { > - dprintf("failed to move block %08lx -> %08lx\n", i, > - where + i); > - return -EIO; > + for (i = 0; i <= end - start; i++) { > + err = bfs_move_block(start + i, where + i, sb); > + if (err) { > + dprintf("failed to move block %08lx -> %08lx\n", > + start + i, where + i); > + return err; > } > + } > return 0; > } > Hi Tigran, I hope I'm not being too persuasive by sending this reminder, but did you have a chance to look at this patch? Thank you!