Skip to contentSkip to conversation

x86/hvm: Correct hvm_ioreq_server_alloc_rangesets() failure path

PATCH In review Andrew Cooper 3 participants 4 messages Jun 4, 2014

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/hvm: Correct hvm_ioreq_server_alloc_rangesets() failure path

Coverity-ID: 1220092 "Unsigned compare against 0"
Coverity-ID: 1220093 "Out-of-bounds read"

Both of these are cased by the the while() loop in the fail path, which results in an infinite loop and memory corruption from rangeset_destroy().

Move hvm_ioreq_server_free_rangesets() up and use it for cleanup on the failure path.

CC Keir Fraser <[email protected]>
CC Jan Beulich <[email protected]>
CC Paul Durrant <[email protected]>
---
 xen/arch/x86/hvm/hvm.c |   27 +++++++++++++--------------
 1 file changed, 13 insertions(+), 14 deletions(-)
xen/arch/x86/hvm/hvm.c +13 -14
index 4f993f4..1f13329 100644
@@ -824,6 +824,18 @@ static void hvm_ioreq_server_unmap_pages(struct hvm_ioreq_server *s,
     }
 }
 
+static void hvm_ioreq_server_free_rangesets(struct hvm_ioreq_server *s,
+                                            bool_t is_default)
+{
+    unsigned int i;
+
+    if ( is_default )
+        return;
+
+    for ( i = 0; i < NR_IO_RANGE_TYPES; i++ )
+        rangeset_destroy(s->range[i]);
+}
+
 static int hvm_ioreq_server_alloc_rangesets(struct hvm_ioreq_server *s, 
                                             bool_t is_default)
 {
@@ -861,24 +873,11 @@ static int hvm_ioreq_server_alloc_rangesets(struct hvm_ioreq_server *s,
     return 0;
 
  fail:
-    while ( --i >= 0 )
-        rangeset_destroy(s->range[i]);
+    hvm_ioreq_server_free_rangesets(s, 0);
 
     return rc;
 }
 
-static void hvm_ioreq_server_free_rangesets(struct hvm_ioreq_server *s, 
-                                            bool_t is_default)
-{
-    unsigned int i;
-
-    if ( is_default )
-        return;
-
-    for ( i = 0; i < NR_IO_RANGE_TYPES; i++ )
-        rangeset_destroy(s->range[i]);
-}
-
 static void hvm_ioreq_server_enable(struct hvm_ioreq_server *s,
                                     bool_t is_default)
 {
Signature · 1 line
1.7.10.4
Reviewed-by on v1 1/1
#1 lore raw
Quoted 14 lines

-----Original Message-----
From: Andrew Cooper [mailto:[email protected]]
Sent: 04 June 2014 12:01
To: Xen-devel
Cc: Andrew Cooper; Keir (Xen.org); Jan Beulich; Paul Durrant
Subject: [PATCH] x86/hvm: Correct hvm_ioreq_server_alloc_rangesets() failure path

Coverity-ID: 1220092 "Unsigned compare against 0"
Coverity-ID: 1220093 "Out-of-bounds read"

Both of these are cased by the the while() loop in the fail path, which results in an infinite loop and memory corruption from rangeset_destroy().

Quoted 7 lines

Move hvm_ioreq_server_free_rangesets() up and use it for cleanup on the failure path.

CC Keir Fraser <[email protected]>
CC Jan Beulich <[email protected]>
CC Paul Durrant <[email protected]>
Quoted 57 lines
---
 xen/arch/x86/hvm/hvm.c |   27 +++++++++++++--------------
 1 file changed, 13 insertions(+), 14 deletions(-)
xen/arch/x86/hvm/hvm.c +12 -0
index 4f993f4..1f13329 100644
@@ -824,6 +824,18 @@ static void hvm_ioreq_server_unmap_pages(struct
 hvm_ioreq_server *s,
     }
 }
 
+static void hvm_ioreq_server_free_rangesets(struct hvm_ioreq_server *s,
+                                            bool_t is_default)
+{
+    unsigned int i;
+
+    if ( is_default )
+        return;
+
+    for ( i = 0; i < NR_IO_RANGE_TYPES; i++ )
+        rangeset_destroy(s->range[i]);
+}
+
 static int hvm_ioreq_server_alloc_rangesets(struct hvm_ioreq_server *s,
                                             bool_t is_default)

{

xen/arch/x86/hvm/hvm.c +1 -14
@@ -861,24 +873,11 @@ static int hvm_ioreq_server_alloc_rangesets(struct
 hvm_ioreq_server *s,
     return 0;
 
  fail:
-    while ( --i >= 0 )
-        rangeset_destroy(s->range[i]);
+    hvm_ioreq_server_free_rangesets(s, 0);
 
     return rc;
 }
 
-static void hvm_ioreq_server_free_rangesets(struct hvm_ioreq_server *s,
-                                            bool_t is_default)
-{
-    unsigned int i;
-
-    if ( is_default )
-        return;
-
-    for ( i = 0; i < NR_IO_RANGE_TYPES; i++ )
-        rangeset_destroy(s->range[i]);
-}
-
 static void hvm_ioreq_server_enable(struct hvm_ioreq_server *s,
                                     bool_t is_default)

{
--
1.7.10.4

#2 lore raw
In reply to Andrew Cooper
Quoted 11 lines
Quoted 1 line
Quoted 1 line · On 04.06.14 at 13:00, <[email protected]> wrote:

Coverity-ID: 1220092 "Unsigned compare against 0"
Coverity-ID: 1220093 "Out-of-bounds read"

Both of these are cased by the the while() loop in the fail path, which results in an infinite loop and memory corruption from rangeset_destroy().

Move hvm_ioreq_server_free_rangesets() up and use it for cleanup on the failure path.

Since you and Paul appear to agree to fix it that way, it'll go in as is, but ...

xen/arch/x86/hvm/hvm.c static int hvm_ioreq_server_alloc_rangesets(struct hvm_ioreq_server *s, · line 864

Quoted 5 lines
xen/arch/x86/hvm/hvm.c +0 -1
@@ -861,24 +873,11 @@ static int hvm_ioreq_server_alloc_rangesets(struct hvm_ioreq_server *s,
     return 0;
 
  fail:
-    while ( --i >= 0 )

... the one line equivalent fix would have been to use while ( i-- ).

Jan

#3 lore raw
Quoted 9 lines · Jan Beulich

xen/arch/x86/hvm/hvm.c static int hvm_ioreq_server_alloc_rangesets(struct hvm_ioreq_server *s, · line 864

Quoted 6 lines
xen/arch/x86/hvm/hvm.c +0 -1
@@ -861,24 +873,11 @@ static int hvm_ioreq_server_alloc_rangesets(struct hvm_ioreq_server *s,
     return 0;
 
  fail:
-    while ( --i >= 0 )

... the one line equivalent fix would have been to use while ( i-- ).

Jan

I was considering the possible case where alloc/free_rangesets() gain further moving parts, although I guess is probably unlikely.

~Andrew