diff options
author | Sebastian Huber <sebastian.huber@embedded-brains.de> | 2013-02-15 16:53:53 +0100 |
---|---|---|
committer | Sebastian Huber <sebastian.huber@embedded-brains.de> | 2013-02-15 17:01:55 +0100 |
commit | df8c2d77f60d21280e28aad115639a9f391a4657 (patch) | |
tree | da0b0929b5c4dbb2aeed0f2baa169f5f88a16019 /cpukit | |
parent | dosfs: Ensure valid data cluster count (diff) | |
download | rtems-df8c2d77f60d21280e28aad115639a9f391a4657.tar.bz2 |
dosfs: Fix file extend
Only append a valid cluster chain (cluster added > 0), otherwise we
overwrite the root directory cluster (cluster 0) of a FAT12 or FAT16
with arbitrary data.
Diffstat (limited to 'cpukit')
-rw-r--r-- | cpukit/libfs/src/dosfs/fat_file.c | 47 |
1 files changed, 24 insertions, 23 deletions
diff --git a/cpukit/libfs/src/dosfs/fat_file.c b/cpukit/libfs/src/dosfs/fat_file.c index 7f05447187..b71745ff5b 100644 --- a/cpukit/libfs/src/dosfs/fat_file.c +++ b/cpukit/libfs/src/dosfs/fat_file.c @@ -637,42 +637,43 @@ fat_file_extend( new_length -= (cls2add - cls_added) << fs_info->vol.bpc_log2; } - /* add new chain to the end of existed */ - if ( fat_fd->fat_file_size == 0 ) + if (cls_added > 0) { - fat_fd->map.disk_cln = fat_fd->cln = chain; - fat_fd->map.file_cln = 0; - } - else - { - if (fat_fd->map.last_cln != FAT_UNDEFINED_VALUE) + /* add new chain to the end of existing */ + if ( fat_fd->fat_file_size == 0 ) { - old_last_cl = fat_fd->map.last_cln; + fat_fd->map.disk_cln = fat_fd->cln = chain; + fat_fd->map.file_cln = 0; } else { - rc = fat_file_ioctl(fs_info, fat_fd, F_CLU_NUM, - (fat_fd->fat_file_size - 1), &old_last_cl); + if (fat_fd->map.last_cln != FAT_UNDEFINED_VALUE) + { + old_last_cl = fat_fd->map.last_cln; + } + else + { + rc = fat_file_ioctl(fs_info, fat_fd, F_CLU_NUM, + (fat_fd->fat_file_size - 1), &old_last_cl); + if ( rc != RC_OK ) + { + fat_free_fat_clusters_chain(fs_info, chain); + return rc; + } + } + + rc = fat_set_fat_cluster(fs_info, old_last_cl, chain); if ( rc != RC_OK ) { fat_free_fat_clusters_chain(fs_info, chain); return rc; } + fat_buf_release(fs_info); } - rc = fat_set_fat_cluster(fs_info, old_last_cl, chain); - if ( rc != RC_OK ) - { - fat_free_fat_clusters_chain(fs_info, chain); - return rc; - } - fat_buf_release(fs_info); - } - - /* update number of the last cluster of the file if it changed */ - if (cls_added != 0) - { + /* update number of the last cluster of the file */ fat_fd->map.last_cln = last_cl; + if (fat_fd->fat_file_type == FAT_DIRECTORY) { rc = fat_init_clusters_chain(fs_info, chain); |