vvfat: Fix bugs in writing and reading files
cover vvfat: Fix bugs in writing and reading files
These patches fix some bugs found when modifying files in vvfat.
First, there was a bug when writing to the second+ cluster of a file, it will copy the cluster before it instead.
Another issue was modifying the clusters of a file and adding new clusters, this showed 2 issues:
- If the new cluster is not immediately after the last cluster, it will cause issues when reading from this file in the future.
- Generally, the usage of info.file.offset was incorrect, and the system would crash on abort() when the file is modified and a new cluster was added.
PATCH 1/3 vvfat: Fix bug in writing to middle of file
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.
--- block/vvfat.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
block/vvfat.c +1 -1
@@ -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 );
Signature · 1 line
2.44.0
PATCH 2/3 vvfat: Fix usage of `info.file.offset`
In reply to Amjad AlsharafiThe 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.
--- block/vvfat.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-)
block/vvfat.c +4 -5
@@ -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 chif (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; }
Signature · 1 line
2.44.0
PATCH 3/3 ffvat: Fix reading files with non-continuous clusters
In reply to Amjad AlsharafiWhen 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.
--- block/vvfat.c | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-)
block/vvfat.c +14 -7
@@ -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 ;
Signature · 1 line
2.44.0
Re: [PATCH 3/3] ffvat: Fix reading files with non-continuous clusters
I noticed the issue in the commit message 'ffvat' should be 'vvfat', I'll fix it in the next version.
Quoted 59 lines · Amjad Alsharafi
When reading with
read_clusterwe get themappingwithfind_mapping_for_clusterand then we callopen_filefor this mapping.
The issue appear when its the same file, but a second cluster that is not immediately after it, imagine clusters500 -> 503, this will give us 2 mappings one has the range500..501and another503..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_mappingand 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 withs->cluster_size*(cluster_num-s->current_mapping->begin), which will give us0x2000 * (504-500), which is out of bound for this mapping and will produce some issues.Signed-off-by Amjad Alsharafi <[email protected]>--- block/vvfat.c | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-)block/vvfat.c +14
-7@@ -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 ;Signature · 1 line
2.44.0
Re: [PATCH 0/3] vvfat: Fix bugs in writing and reading files -- PING
In reply to Amjad AlsharafiPing to the vvfat maintainers.
Re: [PATCH 0/3] vvfat: Fix bugs in writing and reading files -- PING
Ping again
Quoted 2 lines · Amjad Alsharafi
Ping to the vvfat maintainers.