diff mbox

[BUG,BTRFS-PROGS] Fix Bug to corrupt the img file

Message ID CAMRmmpL01PqR1nZPGhyDv1ECdWaBDrhVbx-9FH83x-3ez+M_pQ@mail.gmail.com (mailing list archive)
State New, archived
Headers show

Commit Message

Rock Lee Oct. 24, 2012, 2:04 a.m. UTC
Fix Bug to corrupt the img file

Reproduce steps:
  > dd if=/dev/zero of=btrfs-small.img bs=1M count=1
  > ls -lh btrfs-small.img
  -rw-rw-r--. 1 rock rock 1.0M Oct 24 09:51 btrfs-small.img
  > mkfs.btrfs btrfs-small.img
  -rw-rw-r--. 1 rock rock 2.0M Oct 24 09:53 btrfs-small.img

Here you can see the original img file's size goes larger to 2Mbytes.

Signed-off-by: Rock Lee <zimilo@code-trick.com>
---
 utils.c |    8 ++++++--
 1 files changed, 6 insertions(+), 2 deletions(-)

                exit(1);

Comments

David Sterba Oct. 25, 2012, 3:37 p.m. UTC | #1
On Wed, Oct 24, 2012 at 10:04:47AM +0800, Rock Lee wrote:
> @@ -572,7 +576,7 @@ int btrfs_prepare_device(int fd, char *file, int
> zero_end, u64 *block_count_ret,
>                 discard_blocks(fd, 0, block_count);
>         }
> 
> -       ret = zero_dev_start(fd);
> +       ret = zero_dev_start(fd, block_count);
>         if (ret) {
>                 fprintf(stderr, "failed to zero device start %d\n", ret);
>                 exit(1);

Please add a separate check for a minimal image size instead, right after

 553         block_count = device_size(fd, &st);
 554         if (block_count == 0) {
 555                 fprintf(stderr, "unable to find %s size\n", file);
 556                 exit(1);
 557         }
 558         if (max_block_count)
 559                 block_count = min(block_count, max_block_count);

	     if (block_count < 2 MB ) {
		     fprintf(stderr, "Image too small (less than 2MB)");
		     exit
	     }

thanks,
david
--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Rock Lee Oct. 25, 2012, 3:58 p.m. UTC | #2
Got it.

2012/10/25 David Sterba <dave@jikos.cz>:
> On Wed, Oct 24, 2012 at 10:04:47AM +0800, Rock Lee wrote:
>> @@ -572,7 +576,7 @@ int btrfs_prepare_device(int fd, char *file, int
>> zero_end, u64 *block_count_ret,
>>                 discard_blocks(fd, 0, block_count);
>>         }
>>
>> -       ret = zero_dev_start(fd);
>> +       ret = zero_dev_start(fd, block_count);
>>         if (ret) {
>>                 fprintf(stderr, "failed to zero device start %d\n", ret);
>>                 exit(1);
>
> Please add a separate check for a minimal image size instead, right after
>
>  553         block_count = device_size(fd, &st);
>  554         if (block_count == 0) {
>  555                 fprintf(stderr, "unable to find %s size\n", file);
>  556                 exit(1);
>  557         }
>  558         if (max_block_count)
>  559                 block_count = min(block_count, max_block_count);
>
>              if (block_count < 2 MB ) {
>                      fprintf(stderr, "Image too small (less than 2MB)");
>                      exit
>              }
>
> thanks,
> david
--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

diff --git a/utils.c b/utils.c
index 205e667..3c88d2e 100644
--- a/utils.c
+++ b/utils.c
@@ -441,7 +441,7 @@  static int zero_blocks(int fd, off_t start, size_t len)
        return ret;
 }

-static int zero_dev_start(int fd)
+static int zero_dev_start(int fd, u64 dev_size)
 {
        off_t start = 0;
        size_t len = 2 * 1024 * 1024;
@@ -451,6 +451,10 @@  static int zero_dev_start(int fd)
        start = 1024;
        len -= 1024;
 #endif
+
+       if (dev_size < len)
+               return -EIO;
+
        return zero_blocks(fd, start, len);
 }

@@ -572,7 +576,7 @@  int btrfs_prepare_device(int fd, char *file, int
zero_end, u64 *block_count_ret,
                discard_blocks(fd, 0, block_count);
        }

-       ret = zero_dev_start(fd);
+       ret = zero_dev_start(fd, block_count);
        if (ret) {
                fprintf(stderr, "failed to zero device start %d\n", ret);