qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [Qemu-devel] [PATCH for-2.9] block/parallels: Avoid overflows
@ 2017-03-31 17:05 Max Reitz
  2017-03-31 18:33 ` Eric Blake
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Max Reitz @ 2017-03-31 17:05 UTC (permalink / raw)
  To: qemu-block
  Cc: qemu-devel, Max Reitz, Stefan Hajnoczi, Denis V . Lunev,
	Kevin Wolf, Peter Maydell

Change the types of variables in allocate_clusters() to int64_t so we do
not have to worry about potential overflows.

Add an assertion that our accesses to s->bat[] do not result in a buffer
overflow and that the implicit conversion performed when invoking
bat_entry_off() does not result in an integer overflow.

Coverity-id: 1307776
Signed-off-by: Max Reitz <mreitz@redhat.com>
---
This supercedes Peter's patch "block/parallels.c: avoid integer overflow
in allocate_clusters()".
---
 block/parallels.c | 19 +++++++++++++------
 1 file changed, 13 insertions(+), 6 deletions(-)

diff --git a/block/parallels.c b/block/parallels.c
index 4173b3fb9d..90acf79687 100644
--- a/block/parallels.c
+++ b/block/parallels.c
@@ -192,8 +192,7 @@ static int64_t allocate_clusters(BlockDriverState *bs, int64_t sector_num,
                                  int nb_sectors, int *pnum)
 {
     BDRVParallelsState *s = bs->opaque;
-    uint32_t idx, to_allocate, i;
-    int64_t pos, space;
+    int64_t pos, space, idx, to_allocate, i;
 
     pos = block_status(s, sector_num, nb_sectors, pnum);
     if (pos > 0) {
@@ -201,11 +200,19 @@ static int64_t allocate_clusters(BlockDriverState *bs, int64_t sector_num,
     }
 
     idx = sector_num / s->tracks;
-    if (idx >= s->bat_size) {
-        return -EINVAL;
-    }
-
     to_allocate = DIV_ROUND_UP(sector_num + *pnum, s->tracks) - idx;
+
+    /* This function is called only by parallels_co_writev(), which will never
+     * pass a sector_num at or beyond the end of the image (because the block
+     * layer never passes such a sector_num to that function). Therefore, idx
+     * is always below s->bat_size.
+     * block_status() will limit *pnum so that sector_num + *pnum will not
+     * exceed the image end. Therefore, idx + to_allocate cannot exceed
+     * s->bat_size.
+     * Note that s->bat_size is an unsigned int, therefore idx + to_allocate
+     * will always fit into a uint32_t. */
+    assert(idx < s->bat_size && idx + to_allocate <= s->bat_size);
+
     space = to_allocate * s->tracks;
     if (s->data_end + space > bdrv_getlength(bs->file->bs) >> BDRV_SECTOR_BITS) {
         int ret;
-- 
2.12.1

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [Qemu-devel] [PATCH for-2.9] block/parallels: Avoid overflows
  2017-03-31 17:05 [Qemu-devel] [PATCH for-2.9] block/parallels: Avoid overflows Max Reitz
@ 2017-03-31 18:33 ` Eric Blake
  2017-03-31 20:31 ` Philippe Mathieu-Daudé
  2017-04-03 13:03 ` Max Reitz
  2 siblings, 0 replies; 4+ messages in thread
From: Eric Blake @ 2017-03-31 18:33 UTC (permalink / raw)
  To: Max Reitz, qemu-block
  Cc: Kevin Wolf, Peter Maydell, qemu-devel, Stefan Hajnoczi,
	Denis V . Lunev

[-- Attachment #1: Type: text/plain, Size: 830 bytes --]

On 03/31/2017 12:05 PM, Max Reitz wrote:
> Change the types of variables in allocate_clusters() to int64_t so we do
> not have to worry about potential overflows.
> 
> Add an assertion that our accesses to s->bat[] do not result in a buffer
> overflow and that the implicit conversion performed when invoking
> bat_entry_off() does not result in an integer overflow.
> 
> Coverity-id: 1307776
> Signed-off-by: Max Reitz <mreitz@redhat.com>
> ---
> This supercedes Peter's patch "block/parallels.c: avoid integer overflow
> in allocate_clusters()".
> ---
>  block/parallels.c | 19 +++++++++++++------
>  1 file changed, 13 insertions(+), 6 deletions(-)
> 

Reviewed-by: Eric Blake <eblake@redhat.com>

-- 
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 604 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Qemu-devel] [PATCH for-2.9] block/parallels: Avoid overflows
  2017-03-31 17:05 [Qemu-devel] [PATCH for-2.9] block/parallels: Avoid overflows Max Reitz
  2017-03-31 18:33 ` Eric Blake
@ 2017-03-31 20:31 ` Philippe Mathieu-Daudé
  2017-04-03 13:03 ` Max Reitz
  2 siblings, 0 replies; 4+ messages in thread
From: Philippe Mathieu-Daudé @ 2017-03-31 20:31 UTC (permalink / raw)
  To: Max Reitz, qemu-block
  Cc: Kevin Wolf, Peter Maydell, qemu-devel, Stefan Hajnoczi,
	Denis V . Lunev

On 03/31/2017 02:05 PM, Max Reitz wrote:
> Change the types of variables in allocate_clusters() to int64_t so we do
> not have to worry about potential overflows.
>
> Add an assertion that our accesses to s->bat[] do not result in a buffer
> overflow and that the implicit conversion performed when invoking
> bat_entry_off() does not result in an integer overflow.
>
> Coverity-id: 1307776
> Signed-off-by: Max Reitz <mreitz@redhat.com>

Reviewed-by: Philippe Mathieu-Daudé <f4bug@amsat.org>

> ---
> This supercedes Peter's patch "block/parallels.c: avoid integer overflow
> in allocate_clusters()".
> ---
>  block/parallels.c | 19 +++++++++++++------
>  1 file changed, 13 insertions(+), 6 deletions(-)
>
> diff --git a/block/parallels.c b/block/parallels.c
> index 4173b3fb9d..90acf79687 100644
> --- a/block/parallels.c
> +++ b/block/parallels.c
> @@ -192,8 +192,7 @@ static int64_t allocate_clusters(BlockDriverState *bs, int64_t sector_num,
>                                   int nb_sectors, int *pnum)
>  {
>      BDRVParallelsState *s = bs->opaque;
> -    uint32_t idx, to_allocate, i;
> -    int64_t pos, space;
> +    int64_t pos, space, idx, to_allocate, i;
>
>      pos = block_status(s, sector_num, nb_sectors, pnum);
>      if (pos > 0) {
> @@ -201,11 +200,19 @@ static int64_t allocate_clusters(BlockDriverState *bs, int64_t sector_num,
>      }
>
>      idx = sector_num / s->tracks;
> -    if (idx >= s->bat_size) {
> -        return -EINVAL;
> -    }
> -
>      to_allocate = DIV_ROUND_UP(sector_num + *pnum, s->tracks) - idx;
> +
> +    /* This function is called only by parallels_co_writev(), which will never
> +     * pass a sector_num at or beyond the end of the image (because the block
> +     * layer never passes such a sector_num to that function). Therefore, idx
> +     * is always below s->bat_size.
> +     * block_status() will limit *pnum so that sector_num + *pnum will not
> +     * exceed the image end. Therefore, idx + to_allocate cannot exceed
> +     * s->bat_size.
> +     * Note that s->bat_size is an unsigned int, therefore idx + to_allocate
> +     * will always fit into a uint32_t. */
> +    assert(idx < s->bat_size && idx + to_allocate <= s->bat_size);
> +
>      space = to_allocate * s->tracks;
>      if (s->data_end + space > bdrv_getlength(bs->file->bs) >> BDRV_SECTOR_BITS) {
>          int ret;
>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Qemu-devel] [PATCH for-2.9] block/parallels: Avoid overflows
  2017-03-31 17:05 [Qemu-devel] [PATCH for-2.9] block/parallels: Avoid overflows Max Reitz
  2017-03-31 18:33 ` Eric Blake
  2017-03-31 20:31 ` Philippe Mathieu-Daudé
@ 2017-04-03 13:03 ` Max Reitz
  2 siblings, 0 replies; 4+ messages in thread
From: Max Reitz @ 2017-04-03 13:03 UTC (permalink / raw)
  To: qemu-block
  Cc: qemu-devel, Stefan Hajnoczi, Denis V . Lunev, Kevin Wolf,
	Peter Maydell, Eric Blake, Philippe Mathieu-Daudé

[-- Attachment #1: Type: text/plain, Size: 776 bytes --]

On 31.03.2017 19:05, Max Reitz wrote:
> Change the types of variables in allocate_clusters() to int64_t so we do
> not have to worry about potential overflows.
> 
> Add an assertion that our accesses to s->bat[] do not result in a buffer
> overflow and that the implicit conversion performed when invoking
> bat_entry_off() does not result in an integer overflow.
> 
> Coverity-id: 1307776
> Signed-off-by: Max Reitz <mreitz@redhat.com>
> ---
> This supercedes Peter's patch "block/parallels.c: avoid integer overflow
> in allocate_clusters()".
> ---
>  block/parallels.c | 19 +++++++++++++------
>  1 file changed, 13 insertions(+), 6 deletions(-)

Thanks for reviewing, applied to my block branch:

https://github.com/XanClic/qemu/commits/block

Max


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 512 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2017-04-03 13:04 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-03-31 17:05 [Qemu-devel] [PATCH for-2.9] block/parallels: Avoid overflows Max Reitz
2017-03-31 18:33 ` Eric Blake
2017-03-31 20:31 ` Philippe Mathieu-Daudé
2017-04-03 13:03 ` Max Reitz

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).