From patchwork Wed Mar 27 20:11:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Amjad Alsharafi X-Patchwork-Id: 1917013 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=R3RF3w6I; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=patchwork.ozlabs.org) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4V4dtJ0n8tz1yWr for ; Thu, 28 Mar 2024 07:43:35 +1100 (AEDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rpa74-0000oF-NT; Wed, 27 Mar 2024 16:43:10 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rpZeE-0003B5-4B; Wed, 27 Mar 2024 16:13:22 -0400 Received: from mail-pl1-x62f.google.com ([2607:f8b0:4864:20::62f]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rpZeB-0005we-Bg; Wed, 27 Mar 2024 16:13:21 -0400 Received: by mail-pl1-x62f.google.com with SMTP id d9443c01a7336-1e0d6356ce9so1897385ad.3; Wed, 27 Mar 2024 13:13:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1711570397; x=1712175197; darn=nongnu.org; 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; bh=iFVIMyRlD8/BSmSylP4xvcbNRqi2JvuqJ8ggirzVAcY=; b=R3RF3w6Ij0fQZjkSqWzDXwiKF4NaxCCVq2POBDwvvEK4kTQ6LsvAgBJ35DKm6JOUbI gUkCAPVGlJOjN/XUHHpejSO6LNLueLWOplrDjarRBofuIgeKoZ+w6EXeWy27+ZFvJkuo f3PIJhhRUhRkcrrPsmqS4JuJLbPOaDKGzUUpEDNvtG9aHX/loxlCpqB/oYrx83fH95tv 25+VpBHPpIUM6LZ/prgIpbrOXi/KzEwY0M7vNZ87eV+v7qjAvm4Zl8S3ybqhCIE5SmcB Onp0TgVKjtJc8nAxMHeY/6PDYWQz0TMqQ3LWWTryDNn62/yGAVaMZ06AkG0IXU3TbVHN XICQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711570397; x=1712175197; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=iFVIMyRlD8/BSmSylP4xvcbNRqi2JvuqJ8ggirzVAcY=; b=hFBVvrWhxWBUWeXRcWoMWYIpPOvlProW3m3117QTnjsNrcTDICCN4qymMIft1i21pu rEMigIcXd+2rYe5CNs+tVvhyUSJ5Ovv+ICg7fRi9LeZeb3f6do3xObsOJ8KWGy2Jp32F dmQHCwPNq7kovl+KKsV3VJON/3q/W0Lr0t2n7u02rueh31WFyAQPRQ5TMX0HDoUO5OEu mZ4H6b5IcaWeZl5vMLGyZmYIwZjAgtOm5roIhGt1iu6EmERqN18inF0XbgUa+2YtrV3m 7CvlEeuSOq929rOImbQdyZF793LT4lApQHCHkUEbThjW1/6G0R1TvoEBpDEfIyCpDPuS R0cQ== X-Forwarded-Encrypted: i=1; AJvYcCUkmSLmPrWx/P8JhJftzvqTuh52z6WGeIsSjYWMRcr/1t99Xa/Qq4b+pTyYa7+wvXWu6Z7sCA7LPcf+7/Wg09wgIighFIw= X-Gm-Message-State: AOJu0Yxg/mGPjO3E17pSnesnqfwoszkQDGa5AJbMYuXzFMUzDjiutuL+ Td7tD/Y1/EGaUyoiCfQQ6LD5TxT3YsXWLNT4O1yaap0ab/N6kl0Qdp59EWqMoIFJQIip X-Google-Smtp-Source: AGHT+IH9uPGf6E2jInJ2aHBvY8xvsboNk/P2+w02eTE13anqRI3+m3UUFCMBDGYAOKRAdljxbL1kHg== X-Received: by 2002:a17:902:8f95:b0:1e0:c7b6:ef4a with SMTP id z21-20020a1709028f9500b001e0c7b6ef4amr752410plo.40.1711570396973; Wed, 27 Mar 2024 13:13:16 -0700 (PDT) Received: from localhost.localdomain ([192.228.205.68]) by smtp.gmail.com with ESMTPSA id b10-20020a170902650a00b001e08d5a7638sm9682673plk.99.2024.03.27.13.13.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Mar 2024 13:13:16 -0700 (PDT) From: Amjad Alsharafi To: qemu-devel@nongnu.org Cc: Kevin Wolf , Hanna Reitz , qemu-block@nongnu.org (open list:vvfat), Amjad Alsharafi Subject: [PATCH 1/3] vvfat: Fix bug in writing to middle of file Date: Thu, 28 Mar 2024 04:11:25 +0800 Message-ID: <20240327201231.31046-2-amjadsharafi10@gmail.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240327201231.31046-1-amjadsharafi10@gmail.com> References: <20240327201231.31046-1-amjadsharafi10@gmail.com> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::62f; envelope-from=amjadsharafi10@gmail.com; helo=mail-pl1-x62f.google.com X-Spam_score_int: -17 X-Spam_score: -1.8 X-Spam_bar: - X-Spam_report: (-1.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_ENVFROM_END_DIGIT=0.25, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-Mailman-Approved-At: Wed, 27 Mar 2024 16:43:07 -0400 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Before this commit, the behavior when calling `commit_one_file` for example with `offset=0x2000` (second cluster), what will happen is that we won't fetch the next cluster from the fat, and instead use the first cluster for the read operation. This is due to off-by-one error here, where `i=0x2000 !< offset=0x2000`, thus not fetching the next cluster. Signed-off-by: Amjad Alsharafi --- block/vvfat.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/block/vvfat.c b/block/vvfat.c index 9d050ba3ae..ab342f0743 100644 --- a/block/vvfat.c +++ b/block/vvfat.c @@ -2525,7 +2525,7 @@ commit_one_file(BDRVVVFATState* s, int dir_index, uint32_t offset) return -1; } - for (i = s->cluster_size; i < offset; i += s->cluster_size) + for (i = s->cluster_size; i <= offset; i += s->cluster_size) c = modified_fat_get(s, c); fd = qemu_open_old(mapping->path, O_RDWR | O_CREAT | O_BINARY, 0666); From patchwork Wed Mar 27 20:11:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Amjad Alsharafi X-Patchwork-Id: 1917014 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=JZIws/Rm; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=patchwork.ozlabs.org) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4V4dty1Gh2z1yWr for ; Thu, 28 Mar 2024 07:44:10 +1100 (AEDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rpa77-0000p1-4m; Wed, 27 Mar 2024 16:43:14 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rpZeF-0003BR-EL; Wed, 27 Mar 2024 16:13:23 -0400 Received: from mail-pf1-x42d.google.com ([2607:f8b0:4864:20::42d]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rpZeD-0005x6-LG; Wed, 27 Mar 2024 16:13:22 -0400 Received: by mail-pf1-x42d.google.com with SMTP id d2e1a72fcca58-6e6fb9a494aso283337b3a.0; Wed, 27 Mar 2024 13:13:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1711570399; x=1712175199; darn=nongnu.org; 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; bh=ipqqr2xOwh8qICJewRkxYEwaPQGJHL+EZFPMDeo3faU=; b=JZIws/Rmu2sgS7nIYjLTIXVDkuMu5VokuaCjYJMDsn6v+Y6TNSjeymLc98qXUjm967 md3Gg2ciQEiBwhG7Fpp2HTwuCZeZVIjh/FHLWebR0inHfkQFqfMAm35oBSd7OeCzOHwY KAmAK6N/e/RWJS6P+srIjDgkezx2uc8CToMi4pOdw85nMCHwxQKDeCKKxX8alFBUJ7dU IevvEBPvL0KmkzaWKmIljyC+UUfFAB8IZM9IhDD0F63QX5jZ/nPZTjjQR+4PibXI+7XJ JvMuTIcnWj7KWT282lC+WuAqKKClgMOHP2BVjbGyOQ85+JDyyQTNVMldO47ddqQPd/lB iv1Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711570399; x=1712175199; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ipqqr2xOwh8qICJewRkxYEwaPQGJHL+EZFPMDeo3faU=; b=o+jP7MrH5BKPKLRnl/371wHmWSzScWpHjtKPmawX9Rmel3qHFwdz/ms/EsehHOC9Z7 T2B8ixFmlNZZSX7+tlsfpp52RXnFBdHH75Cmyja1HLyq6nboVfLWhqAqE79tqhDa1JKu MVs2gqOzvvsI5rXhAK58oi1999F5UCKX4kTG18VHajBJzbMDTXjryY/NIN6phZ4SmMAQ 2pFF/+CGSg6GbKz10Qj8YqZFbGCt6VhGnRWXPplr/kiWhTvnnJWwnmTpFcZ1fJbBejqc ynYmBLjjXxOqdnnZbPLaK9YxxTrHKWLxhuzshdy2gCwgdMj8EHv4i4fFjI3j1/gDey0F v2cQ== X-Forwarded-Encrypted: i=1; AJvYcCVu+MHhlrFLbqt2M8CFY9gRBdO4DCelIYpevqj63JJl1XNeY/vsRhoh/4U/LkZz7HAsfdhmYCmaRoep3FOW8QC1EPvqOB4= X-Gm-Message-State: AOJu0Yw/w3C6oJO9Hr5+934g4ky63iHiK1J5L0NJk9NGKEu7c45X7Gdd db8NR0ti8WECcedI/bUh0Ikxkx2PRs3xio8k7Q8Q6jSRdLjSzbRXKwq/UBPRl6GAX3Yp X-Google-Smtp-Source: AGHT+IFgP8n+DIqi1Vus3n5GTJgd93MOpsqUInmVuepXpJcwfJmTy+BRwGNXRaLtB2KzcMNAK8FOWw== X-Received: by 2002:a17:902:c409:b0:1e2:86e:a0dd with SMTP id k9-20020a170902c40900b001e2086ea0ddmr708227plk.23.1711570398987; Wed, 27 Mar 2024 13:13:18 -0700 (PDT) Received: from localhost.localdomain ([192.228.205.68]) by smtp.gmail.com with ESMTPSA id b10-20020a170902650a00b001e08d5a7638sm9682673plk.99.2024.03.27.13.13.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Mar 2024 13:13:18 -0700 (PDT) From: Amjad Alsharafi To: qemu-devel@nongnu.org Cc: Kevin Wolf , Hanna Reitz , qemu-block@nongnu.org (open list:vvfat), Amjad Alsharafi Subject: [PATCH 2/3] vvfat: Fix usage of `info.file.offset` Date: Thu, 28 Mar 2024 04:11:26 +0800 Message-ID: <20240327201231.31046-3-amjadsharafi10@gmail.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240327201231.31046-1-amjadsharafi10@gmail.com> References: <20240327201231.31046-1-amjadsharafi10@gmail.com> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::42d; envelope-from=amjadsharafi10@gmail.com; helo=mail-pf1-x42d.google.com X-Spam_score_int: -17 X-Spam_score: -1.8 X-Spam_bar: - X-Spam_report: (-1.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_ENVFROM_END_DIGIT=0.25, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-Mailman-Approved-At: Wed, 27 Mar 2024 16:43:07 -0400 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org The field is marked as "the offset in the file (in clusters)", but it was being used like this `cluster_size*(nums)+mapping->info.file.offset`, which is incorrect. Additionally, removed the `abort` when `first_mapping_index` does not match, as this matches the case when adding new clusters for files, and its inevitable that we reach this condition when doing that if the clusters are not after one another, so there is no reason to `abort` here, execution continues and the new clusters are written to disk correctly. Signed-off-by: Amjad Alsharafi --- block/vvfat.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/block/vvfat.c b/block/vvfat.c index ab342f0743..cb3ab81e29 100644 --- a/block/vvfat.c +++ b/block/vvfat.c @@ -1408,7 +1408,7 @@ read_cluster_directory: assert(s->current_fd); - offset=s->cluster_size*(cluster_num-s->current_mapping->begin)+s->current_mapping->info.file.offset; + offset=s->cluster_size*((cluster_num - s->current_mapping->begin) + s->current_mapping->info.file.offset); if(lseek(s->current_fd, offset, SEEK_SET)!=offset) return -3; s->cluster=s->cluster_buffer; @@ -1929,8 +1929,8 @@ get_cluster_count_for_direntry(BDRVVVFATState* s, direntry_t* direntry, const ch (mapping->mode & MODE_DIRECTORY) == 0) { /* was modified in qcow */ - if (offset != mapping->info.file.offset + s->cluster_size - * (cluster_num - mapping->begin)) { + if (offset != s->cluster_size + * ((cluster_num - mapping->begin) + mapping->info.file.offset)) { /* offset of this cluster in file chain has changed */ abort(); copy_it = 1; @@ -1944,7 +1944,6 @@ get_cluster_count_for_direntry(BDRVVVFATState* s, direntry_t* direntry, const ch if (mapping->first_mapping_index != first_mapping_index && mapping->info.file.offset > 0) { - abort(); copy_it = 1; } @@ -2404,7 +2403,7 @@ static int commit_mappings(BDRVVVFATState* s, (mapping->end - mapping->begin); } else next_mapping->info.file.offset = mapping->info.file.offset + - mapping->end - mapping->begin; + (mapping->end - mapping->begin); mapping = next_mapping; } From patchwork Wed Mar 27 20:11:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Amjad Alsharafi X-Patchwork-Id: 1917016 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=NAySpR+m; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=patchwork.ozlabs.org) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4V4dv661Hfz1yWr for ; Thu, 28 Mar 2024 07:44:18 +1100 (AEDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rpa73-0000nH-47; Wed, 27 Mar 2024 16:43:09 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rpZeI-0003Bi-L7; Wed, 27 Mar 2024 16:13:26 -0400 Received: from mail-pl1-x629.google.com ([2607:f8b0:4864:20::629]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rpZeF-0005xR-HH; Wed, 27 Mar 2024 16:13:24 -0400 Received: by mail-pl1-x629.google.com with SMTP id d9443c01a7336-1dee27acf7aso1694985ad.2; Wed, 27 Mar 2024 13:13:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1711570401; x=1712175201; darn=nongnu.org; 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; bh=dq8wco9LxYm2lwgJnPrcuNFswulKN41BsT8tTWCz16o=; b=NAySpR+meuVJYq+vGr8eAzVgGeTktLNMtPoPWOb1+DoTdQTt53rJ35lydio3jk0Ss4 TJacZofHPKySWV2CAD8UR06YS6fhIVeWaG14HsTeyprUbbVPyDrzD7oCfQe12CjFkJyU zbBbx0xZMdGD887eQaPsotb4sbiG+T6csfzTdqAN1D7eR0alZy5VCLDXG3WCp4DvJS1W GBR07TsLTVL4waUa+S7zRdBO+NpOxLZaoeAGfBPRzkiI9fxWDuV/9Hb6gE4ntALRSm/q g48jWzdn33hJRdrQIMqWWdcmgsiViJPhw0wSidqEpejdFDMc8WfsRy99pxdCr8nlzQ9V ZYMQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711570401; x=1712175201; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=dq8wco9LxYm2lwgJnPrcuNFswulKN41BsT8tTWCz16o=; b=rcFpNevvGrP/msHhnSBzSbz+sX+4AWAGWCJ4nXVA+R8n9JyDiMZfRpMt05s24YdQIi S+sNj1W66RR3eyczjiKLOirkAFf0eAlDiIYeZk/XaoIKEe3Q7CTosrKlUVpYv2hr3HWB /XJEha72XJxo2Slc6hgwkMTt+oWn/lroyu5uv7D+oG0H4vLLCAcmqPwll6numSxDFSXY SlIstY+9i5mD/rxfQkW6oSiTAD/ijeJT3w26qqXUAz2SiuQYjp5u02T84Pfptx/cli3h dHC7yOLF+lRYooAAf10a2QUVHK5yLQ31/oY58KJDJ6iYX8cBBfQS/0RNI5/L4TmGEaTB GDfQ== X-Forwarded-Encrypted: i=1; AJvYcCWObuEwThn4sFyjOSmZPfnQ3rrF7uAafo5tS1vAl7z1EoxMM/q9UIAsqZeHMzZsS09eARd+CdhzA8979GCIH/5EZVdFsP4= X-Gm-Message-State: AOJu0YxkgvxsVZN3CNy4n1IR06CfbmhzYl9H0PFExLuL/CL8QcLu/VlV NAnq2IMjlWMDaQIg1yjbiJD20s0wFlve9Nb/6ORH+sB5D9xZaoutd6uzdJrk5kJD4RbG X-Google-Smtp-Source: AGHT+IFHjmKzcKotGaW+Xb3J5dmnycZ9iUC8b7zmjMRrpv/Fl73s8Zghq682oUJYFxqdlFtocJxJzg== X-Received: by 2002:a17:902:6808:b0:1e0:e2b1:7395 with SMTP id h8-20020a170902680800b001e0e2b17395mr761150plk.14.1711570400992; Wed, 27 Mar 2024 13:13:20 -0700 (PDT) Received: from localhost.localdomain ([192.228.205.68]) by smtp.gmail.com with ESMTPSA id b10-20020a170902650a00b001e08d5a7638sm9682673plk.99.2024.03.27.13.13.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Mar 2024 13:13:20 -0700 (PDT) From: Amjad Alsharafi To: qemu-devel@nongnu.org Cc: Kevin Wolf , Hanna Reitz , qemu-block@nongnu.org (open list:vvfat), Amjad Alsharafi Subject: [PATCH 3/3] ffvat: Fix reading files with non-continuous clusters Date: Thu, 28 Mar 2024 04:11:27 +0800 Message-ID: <20240327201231.31046-4-amjadsharafi10@gmail.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240327201231.31046-1-amjadsharafi10@gmail.com> References: <20240327201231.31046-1-amjadsharafi10@gmail.com> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::629; envelope-from=amjadsharafi10@gmail.com; helo=mail-pl1-x629.google.com X-Spam_score_int: -17 X-Spam_score: -1.8 X-Spam_bar: - X-Spam_report: (-1.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_ENVFROM_END_DIGIT=0.25, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-Mailman-Approved-At: Wed, 27 Mar 2024 16:43:07 -0400 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org When reading with `read_cluster` we get the `mapping` with `find_mapping_for_cluster` and then we call `open_file` for this mapping. The issue appear when its the same file, but a second cluster that is not immediately after it, imagine clusters `500 -> 503`, this will give us 2 mappings one has the range `500..501` and another `503..504`, both point to the same file, but different offsets. When we don't open the file since the path is the same, we won't assign `s->current_mapping` and thus accessing way out of bound of the file. From our example above, after `open_file` (that didn't open anything) we will get the offset into the file with `s->cluster_size*(cluster_num-s->current_mapping->begin)`, which will give us `0x2000 * (504-500)`, which is out of bound for this mapping and will produce some issues. Signed-off-by: Amjad Alsharafi --- block/vvfat.c | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/block/vvfat.c b/block/vvfat.c index cb3ab81e29..87165abc26 100644 --- a/block/vvfat.c +++ b/block/vvfat.c @@ -1360,15 +1360,22 @@ static int open_file(BDRVVVFATState* s,mapping_t* mapping) { if(!mapping) return -1; + int new_path = 1; if(!s->current_mapping || - strcmp(s->current_mapping->path,mapping->path)) { - /* open file */ - int fd = qemu_open_old(mapping->path, + s->current_mapping->first_mapping_index!=mapping->first_mapping_index || + (new_path = strcmp(s->current_mapping->path,mapping->path))) { + + if (new_path) { + /* open file */ + int fd = qemu_open_old(mapping->path, O_RDONLY | O_BINARY | O_LARGEFILE); - if(fd<0) - return -1; - vvfat_close_current_file(s); - s->current_fd = fd; + if(fd<0) + return -1; + vvfat_close_current_file(s); + + s->current_fd = fd; + } + assert(s->current_fd); s->current_mapping = mapping; } return 0;