Skip to contentSkip to conversation

hvm/load: Correct length checks for zeroextended records

PATCH In review Andrew Cooper 2 participants 3 messages Oct 23, 2014 – Oct 24, 2014

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hvm/load: Correct length checks for zeroextended records

In the case that Xen is attempting to load a zeroextended HVM record where the difference needing extending would overflow the data blob, _hvm_check_entry() will incorrectly fail before working out that it would have been safe.

The "len + sizeof(*d)" check is wrong. Consider zeroextending a 16 byte record into a 32 byte structure. "32 + hdr" will fail the overall context length check even though the pre-extended record in the stream is 16 bytes.

The first condition is reduced to just a length check for hvm save header, while the second condition is extended to include a check that the record in the stream not exceeding the stream length.

The error messages are extended to include further useful information.

---
 xen/common/hvm/save.c |   17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)
xen/common/hvm/save.c +10 -7
index 6c16399..e20b1e6 100644
@@ -292,19 +292,22 @@ int _hvm_check_entry(struct hvm_domain_context *h,
 {
     struct hvm_save_descriptor *d 
         = (struct hvm_save_descriptor *)&h->data[h->cur];
-    if ( len + sizeof (*d) > h->size - h->cur)
+    if ( sizeof(*d) > h->size - h->cur)
     {
         printk(XENLOG_G_WARNING
-               "HVM restore: not enough data left to read %u bytes "
-               "for type %u\n", len, type);
+               "HVM restore: not enough data left to read %zu bytes "
+               "for type %u header\n", sizeof(*d), type);
         return -1;
-    }    
+    }
     if ( (type != d->typecode) || (len < d->length) ||
-         (strict_length && (len != d->length)) )
+         (strict_length && (len != d->length)) ||
+         (d->length > (h->size - h->cur - sizeof(*d))) )
     {
         printk(XENLOG_G_WARNING
-               "HVM restore mismatch: expected type %u length %u, "
-               "saw type %u length %u\n", type, len, d->typecode, d->length);
+               "HVM restore mismatch: expected %s type %u length %u, "
+               "saw type %u length %u.  %zu bytes remaining\n",
+               strict_length ? "strict" : "zeroextended", type, len,
+               d->typecode, d->length, h->size - h->cur - sizeof(*d));
         return -1;
     }
     h->cur += sizeof(*d);
Signature · 1 line
1.7.10.4
Reviewed-by on v1 1/1
#1 lore raw
Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 23.10.14 at 12:50, <[email protected]> wrote:

In the case that Xen is attempting to load a zeroextended HVM record where the difference needing extending would overflow the data blob, _hvm_check_entry()
will incorrectly fail before working out that it would have been safe.

The "len + sizeof(*d)" check is wrong. Consider zeroextending a 16 byte record into a 32 byte structure. "32 + hdr" will fail the overall context length check even though the pre-extended record in the stream is 16 bytes.

Quoted 8 lines

The first condition is reduced to just a length check for hvm save header, while the second condition is extended to include a check that the record in the stream not exceeding the stream length.

The error messages are extended to include further useful information.

albeit with a comment:

Quoted 10 lines
xen/common/hvm/save.c +1 -1
@@ -292,19 +292,22 @@ int _hvm_check_entry(struct hvm_domain_context *h,
 {
     struct hvm_save_descriptor *d 
         = (struct hvm_save_descriptor *)&h->data[h->cur];
-    if ( len + sizeof (*d) > h->size - h->cur)
+    if ( sizeof(*d) > h->size - h->cur)
     {
         printk(XENLOG_G_WARNING

xen/common/hvm/save.c int _hvm_check_entry(struct hvm_domain_context *h, · line 303

Quoted 10 lines
xen/common/hvm/save.c +4 -4
-               "HVM restore: not enough data left to read %u bytes "
-               "for type %u\n", len, type);
+               "HVM restore: not enough data left to read %zu bytes "
+               "for type %u header\n", sizeof(*d), type);
         return -1;
-    }    
+    }
     if ( (type != d->typecode) || (len < d->length) ||
-         (strict_length && (len != d->length)) )
+         (strict_length && (len != d->length)) ||

If this is already being overhauled, I'd really like to at once switch to

     if ( (type != d->typecode) ||
         (strict_length ? (len != d->length) : (len < d->length)) ||

to make more obvious what is really being checked here. Definitely no reason to re-submit though.

Jan

Quoted 12 lines
xen/common/hvm/save.c +5 -2
+         (d->length > (h->size - h->cur - sizeof(*d))) )
     {
         printk(XENLOG_G_WARNING
-               "HVM restore mismatch: expected type %u length %u, "
-               "saw type %u length %u\n", type, len, d->typecode, d->length);
+               "HVM restore mismatch: expected %s type %u length %u, "
+               "saw type %u length %u.  %zu bytes remaining\n",
+               strict_length ? "strict" : "zeroextended", type, len,
+               d->typecode, d->length, h->size - h->cur - sizeof(*d));
         return -1;
     }
     h->cur += sizeof(*d);
#2 lore raw
Quoted 43 lines · Jan Beulich
Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 23.10.14 at 12:50, <[email protected]> wrote:

In the case that Xen is attempting to load a zeroextended HVM record where the difference needing extending would overflow the data blob, _hvm_check_entry()
will incorrectly fail before working out that it would have been safe.

The "len + sizeof(*d)" check is wrong. Consider zeroextending a 16 byte record into a 32 byte structure. "32 + hdr" will fail the overall context length check even though the pre-extended record in the stream is 16 bytes.

Quoted 8 lines

The first condition is reduced to just a length check for hvm save header, while the second condition is extended to include a check that the record in the stream not exceeding the stream length.

The error messages are extended to include further useful information.

albeit with a comment:

Quoted 10 lines
xen/common/hvm/save.c +1 -1
@@ -292,19 +292,22 @@ int _hvm_check_entry(struct hvm_domain_context *h,
 {
     struct hvm_save_descriptor *d 
         = (struct hvm_save_descriptor *)&h->data[h->cur];
-    if ( len + sizeof (*d) > h->size - h->cur)
+    if ( sizeof(*d) > h->size - h->cur)
     {
         printk(XENLOG_G_WARNING

xen/common/hvm/save.c int _hvm_check_entry(struct hvm_domain_context *h, · line 303

Quoted 10 lines
xen/common/hvm/save.c +4 -4
-               "HVM restore: not enough data left to read %u bytes "
-               "for type %u\n", len, type);
+               "HVM restore: not enough data left to read %zu bytes "
+               "for type %u header\n", sizeof(*d), type);
         return -1;
-    }    
+    }
     if ( (type != d->typecode) || (len < d->length) ||
-         (strict_length && (len != d->length)) )
+         (strict_length && (len != d->length)) ||

If this is already being overhauled, I'd really like to at once switch to

Quoted 5 lines
     if ( (type != d->typecode) ||
         (strict_length ? (len != d->length) : (len < d->length)) ||

to make more obvious what is really being checked here. Definitely no reason to re-submit though.

Looks fine, if you are happy to fix this up on commit?

~Andrew