Skip to contentSkip to conversation

state loading security issues

RFC PATCH Superseded Michael S. Tsirkin 2 participants 7 messages Mar 24, 2014 – Mar 25, 2014

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v3 cover state loading security issues

In an attempt to provide a generic solution for this set of issues, this adds a way to add validators in the middle of the structure.

On failure, we assert on output (should never happen) and fail migration on input.

The last patch in the series shows how the new infrastructure is used.
I'll wait a bit for feedback, if there's none I'll go ahead and use this to fix the state loading CVEs.

Changes from v2:
address comments by dgilbert

Michael S. Tsirkin (3):
vmstate: add VMS_MUST_EXIST
vmstate: add VMSTATE_TEST
hpet: fix buffer overrun on invalid state load

 include/migration/vmstate.h |  7 +++++++
 hw/timer/hpet.c             | 13 +++++++++++++
 vmstate.c                   | 10 ++++++++++
 3 files changed, 30 insertions(+)
Signature · 1 line
MST
#1 lore raw

RFC PATCH v3 1/3 vmstate: add VMS_MUST_EXIST

Can be used to verify a required field exists or validate state in some other way.

---
 include/migration/vmstate.h |  1 +
 vmstate.c                   | 10 ++++++++++
 2 files changed, 11 insertions(+)
include/migration/vmstate.h +1 -0
index e7e1705..de970ab 100644
@@ -100,6 +100,7 @@ enum VMStateFlags {
     VMS_MULTIPLY         = 0x200,  /* multiply "size" field by field_size */
     VMS_VARRAY_UINT8     = 0x400,  /* Array with size in uint8_t field*/
     VMS_VARRAY_UINT32    = 0x800,  /* Array with size in uint32_t field*/
+    VMS_MUST_EXIST       = 0x1000, /* Field must exist in input */
 };
 
 typedef struct {
vmstate.c +10 -0
index 18b3732..236ce0b 100644
@@ -103,6 +103,10 @@ int vmstate_load_state(QEMUFile *f, const VMStateDescription *vmsd,
                     return ret;
                 }
             }
+        } else if (field->flags & VMS_MUST_EXIST) {
+            fprintf(stderr, "Input validation failed: %s/%s\n",
+                    vmsd->name, field->name);
+            return -1;
         }
         field++;
     }
@@ -146,6 +150,12 @@ void vmstate_save_state(QEMUFile *f, const VMStateDescription *vmsd,
                     field->info->put(f, addr, size);
                 }
             }
+        } else {
+            if (field->flags & VMS_MUST_EXIST) {
+                fprintf(stderr, "Output state validation failed: %s/%s\n",
+                        vmsd->name, field->name);
+                assert(!(field->flags & VMS_MUST_EXIST));
+            }
         }
         field++;
     }
Signature · 1 line
MST
#3 lore raw

RFC PATCH v3 2/3 vmstate: add VMSTATE_TEST

In reply to Michael S. Tsirkin

Can validate state using VMS_NONE and VMS_MUST_EXIST

---
 include/migration/vmstate.h | 6 ++++++
 1 file changed, 6 insertions(+)
include/migration/vmstate.h +6 -0
index de970ab..97629b7 100644
@@ -204,6 +204,12 @@ extern const VMStateInfo vmstate_info_bitmap;
     .offset       = vmstate_offset_value(_state, _field, _type),     \
 }
 
+#define VMSTATE_TEST(_name, _test) { \
+    .name         = (_name),                                         \
+    .field_exists = (_test),                                         \
+    .flags        = VMS_ARRAY | VMS_MUST_EXIST,                      \
+}
+
 #define VMSTATE_POINTER(_field, _state, _version, _info, _type) {    \
     .name       = (stringify(_field)),                               \
     .version_id = (_version),                                        \
Signature · 1 line
MST
#5 lore raw

RFC PATCH v3 3/3 hpet: fix buffer overrun on invalid state load

In reply to Michael S. Tsirkin

CVE-2013-4527 hw/timer/hpet.c buffer overrun

hpet is a VARRAY with a uint8 size but static array of 32

To fix, make sure num_timers is valid using VMSTATE_TEST hook.

---
 hw/timer/hpet.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)
hw/timer/hpet.c +13 -0
index 1ee0640..7303ade 100644
@@ -248,6 +248,18 @@ static int hpet_pre_load(void *opaque)
     return 0;
 }
 
+static bool hpet_validate_num_timers(void *opaque, int version_id)
+{
+    HPETState *s = opaque;
+
+    if (s->num_timers < HPET_MIN_TIMERS) {
+        return false;
+    } else if (s->num_timers > HPET_MAX_TIMERS) {
+        return false;
+    }
+    return true;
+}
+
 static int hpet_post_load(void *opaque, int version_id)
 {
     HPETState *s = opaque;
@@ -318,6 +330,7 @@ static const VMStateDescription vmstate_hpet = {
         VMSTATE_UINT64(isr, HPETState),
         VMSTATE_UINT64(hpet_counter, HPETState),
         VMSTATE_UINT8_V(num_timers, HPETState, 2),
+        VMSTATE_TEST("num_timers in range", hpet_validate_num_timers),
         VMSTATE_STRUCT_VARRAY_UINT8(timer, HPETState, num_timers, 0,
                                     vmstate_hpet_timer, HPETTimer),
         VMSTATE_END_OF_LIST()
Signature · 1 line
MST
Reviewed-by on v3 1/3
#2 lore raw

Re: [Qemu-devel] [PATCH RFC v3 1/3] vmstate: add VMS_MUST_EXIST

In reply to Michael S. Tsirkin
Quoted 51 lines · * Michael S. Tsirkin ([email protected]) wrote:

Can be used to verify a required field exists or validate state in some other way.

---
 include/migration/vmstate.h |  1 +
 vmstate.c                   | 10 ++++++++++
 2 files changed, 11 insertions(+)
include/migration/vmstate.h +1 -0
index e7e1705..de970ab 100644
@@ -100,6 +100,7 @@ enum VMStateFlags {
     VMS_MULTIPLY         = 0x200,  /* multiply "size" field by field_size */
     VMS_VARRAY_UINT8     = 0x400,  /* Array with size in uint8_t field*/
     VMS_VARRAY_UINT32    = 0x800,  /* Array with size in uint32_t field*/
+    VMS_MUST_EXIST       = 0x1000, /* Field must exist in input */
 };
 
 typedef struct {
vmstate.c +10 -0
index 18b3732..236ce0b 100644
@@ -103,6 +103,10 @@ int vmstate_load_state(QEMUFile *f, const VMStateDescription *vmsd,
                     return ret;
                 }
             }
+        } else if (field->flags & VMS_MUST_EXIST) {
+            fprintf(stderr, "Input validation failed: %s/%s\n",
+                    vmsd->name, field->name);
+            return -1;
         }
         field++;
     }
@@ -146,6 +150,12 @@ void vmstate_save_state(QEMUFile *f, const VMStateDescription *vmsd,
                     field->info->put(f, addr, size);
                 }
             }
+        } else {
+            if (field->flags & VMS_MUST_EXIST) {
+                fprintf(stderr, "Output state validation failed: %s/%s\n",
+                        vmsd->name, field->name);
+                assert(!(field->flags & VMS_MUST_EXIST));
+            }
         }
         field++;
     }
Signature · 1 line
MST

Dave
--
Dr. David Alan Gilbert / [email protected] / Manchester, UK

#4 lore raw

Re: [Qemu-devel] [PATCH RFC v3 2/3] vmstate: add VMSTATE_TEST

In reply to Michael S. Tsirkin
Quoted 1 line · * Michael S. Tsirkin ([email protected]) wrote:

Can validate state using VMS_NONE and VMS_MUST_EXIST

Old comment, VMS_NONE being dead.

Quoted 10 lines
---
 include/migration/vmstate.h | 6 ++++++
 1 file changed, 6 insertions(+)
include/migration/vmstate.h +0 -0
index de970ab..97629b7 100644

include/migration/vmstate.h extern const VMStateInfo vmstate_info_bitmap; · line 210

Quoted 7 lines
include/migration/vmstate.h +4 -0
@@ -204,6 +204,12 @@ extern const VMStateInfo vmstate_info_bitmap;
     .offset       = vmstate_offset_value(_state, _field, _type),     \
 }
 
+#define VMSTATE_TEST(_name, _test) { \
+    .name         = (_name),                                         \
+    .field_exists = (_test),                                         \
+    .flags        = VMS_ARRAY | VMS_MUST_EXIST,                      \

Please comment this to say it's using the 0 sized array trick, (personally I'd explicitly set .num = 0 as well).

Dave

Quoted 8 lines
include/migration/vmstate.h +2 -0
+}
+
 #define VMSTATE_POINTER(_field, _state, _version, _info, _type) {    \
     .name       = (stringify(_field)),                               \
     .version_id = (_version),                                        \
Signature · 1 line
MST

--
Dr. David Alan Gilbert / [email protected] / Manchester, UK

Reviewed-by on v3 3/3
#6 lore raw

Re: [Qemu-devel] [PATCH RFC v3 3/3] hpet: fix buffer overrun on invalid state load

In reply to Michael S. Tsirkin
Quoted 8 lines · * Michael S. Tsirkin ([email protected]) wrote:

CVE-2013-4527 hw/timer/hpet.c buffer overrun

hpet is a VARRAY with a uint8 size but static array of 32

To fix, make sure num_timers is valid using VMSTATE_TEST hook.

Quoted 38 lines
---
 hw/timer/hpet.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)
hw/timer/hpet.c +13 -0
index 1ee0640..7303ade 100644
@@ -248,6 +248,18 @@ static int hpet_pre_load(void *opaque)
     return 0;
 }
 
+static bool hpet_validate_num_timers(void *opaque, int version_id)
+{
+    HPETState *s = opaque;
+
+    if (s->num_timers < HPET_MIN_TIMERS) {
+        return false;
+    } else if (s->num_timers > HPET_MAX_TIMERS) {
+        return false;
+    }
+    return true;
+}
+
 static int hpet_post_load(void *opaque, int version_id)
 {
     HPETState *s = opaque;
@@ -318,6 +330,7 @@ static const VMStateDescription vmstate_hpet = {
         VMSTATE_UINT64(isr, HPETState),
         VMSTATE_UINT64(hpet_counter, HPETState),
         VMSTATE_UINT8_V(num_timers, HPETState, 2),
+        VMSTATE_TEST("num_timers in range", hpet_validate_num_timers),
         VMSTATE_STRUCT_VARRAY_UINT8(timer, HPETState, num_timers, 0,
                                     vmstate_hpet_timer, HPETTimer),
         VMSTATE_END_OF_LIST()
Signature · 1 line
MST

--
Dr. David Alan Gilbert / [email protected] / Manchester, UK