Add a protective section footer

Badly formatted migration streams can go undetected or produce
misleading errors due to a lock of checking at the end of sections.
In particular a section that adds an extra 0x00 at the end
causes what looks like a normal end of stream and thus doesn't produce
any errors, and something that ends in a 0x01..0x04 kind of look
like real section headers and then fail when the section parser tries
to figure out which section they are.  This is made worse by the
choice of 0x00..0x04 being small numbers that are particularly common
in normal section data.

This patch adds a section footer consisting of a marker (0x7e - ~)
followed by the section-id that was also sent in the header.  If
they mismatch then it throws an error explaining which section was
being loaded.

Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
Reviewed-by: Juan Quintela <quintela@redhat.com>
Signed-off-by: Juan Quintela <quintela@redhat.com>
This commit is contained in:
Dr. David Alan Gilbert 2015-05-19 12:29:52 +01:00 committed by Juan Quintela
parent 37fb569c01
commit f68945d42b
2 changed files with 62 additions and 0 deletions

View File

@ -34,6 +34,7 @@
#define QEMU_VM_SECTION_FULL 0x04 #define QEMU_VM_SECTION_FULL 0x04
#define QEMU_VM_SUBSECTION 0x05 #define QEMU_VM_SUBSECTION 0x05
#define QEMU_VM_VMDESCRIPTION 0x06 #define QEMU_VM_VMDESCRIPTION 0x06
#define QEMU_VM_SECTION_FOOTER 0x7e
struct MigrationParams { struct MigrationParams {
bool blk; bool blk;

View File

@ -639,6 +639,53 @@ static void save_section_header(QEMUFile *f, SaveStateEntry *se,
} }
} }
/*
* Write a footer onto device sections that catches cases misformatted device
* sections.
*/
static void save_section_footer(QEMUFile *f, SaveStateEntry *se)
{
if (!skip_section_footers) {
qemu_put_byte(f, QEMU_VM_SECTION_FOOTER);
qemu_put_be32(f, se->section_id);
}
}
/*
* Read a footer off the wire and check that it matches the expected section
*
* Returns: true if the footer was good
* false if there is a problem (and calls error_report to say why)
*/
static bool check_section_footer(QEMUFile *f, SaveStateEntry *se)
{
uint8_t read_mark;
uint32_t read_section_id;
if (skip_section_footers) {
/* No footer to check */
return true;
}
read_mark = qemu_get_byte(f);
if (read_mark != QEMU_VM_SECTION_FOOTER) {
error_report("Missing section footer for %s", se->idstr);
return false;
}
read_section_id = qemu_get_be32(f);
if (read_section_id != se->section_id) {
error_report("Mismatched section id in footer for %s -"
" read 0x%x expected 0x%x",
se->idstr, read_section_id, se->section_id);
return false;
}
/* All good */
return true;
}
bool qemu_savevm_state_blocked(Error **errp) bool qemu_savevm_state_blocked(Error **errp)
{ {
SaveStateEntry *se; SaveStateEntry *se;
@ -686,6 +733,7 @@ void qemu_savevm_state_begin(QEMUFile *f,
save_section_header(f, se, QEMU_VM_SECTION_START); save_section_header(f, se, QEMU_VM_SECTION_START);
ret = se->ops->save_live_setup(f, se->opaque); ret = se->ops->save_live_setup(f, se->opaque);
save_section_footer(f, se);
if (ret < 0) { if (ret < 0) {
qemu_file_set_error(f, ret); qemu_file_set_error(f, ret);
break; break;
@ -723,6 +771,7 @@ int qemu_savevm_state_iterate(QEMUFile *f)
ret = se->ops->save_live_iterate(f, se->opaque); ret = se->ops->save_live_iterate(f, se->opaque);
trace_savevm_section_end(se->idstr, se->section_id, ret); trace_savevm_section_end(se->idstr, se->section_id, ret);
save_section_footer(f, se);
if (ret < 0) { if (ret < 0) {
qemu_file_set_error(f, ret); qemu_file_set_error(f, ret);
@ -770,6 +819,7 @@ void qemu_savevm_state_complete(QEMUFile *f)
ret = se->ops->save_live_complete(f, se->opaque); ret = se->ops->save_live_complete(f, se->opaque);
trace_savevm_section_end(se->idstr, se->section_id, ret); trace_savevm_section_end(se->idstr, se->section_id, ret);
save_section_footer(f, se);
if (ret < 0) { if (ret < 0) {
qemu_file_set_error(f, ret); qemu_file_set_error(f, ret);
return; return;
@ -796,6 +846,7 @@ void qemu_savevm_state_complete(QEMUFile *f)
json_end_object(vmdesc); json_end_object(vmdesc);
trace_savevm_section_end(se->idstr, se->section_id, 0); trace_savevm_section_end(se->idstr, se->section_id, 0);
save_section_footer(f, se);
} }
qemu_put_byte(f, QEMU_VM_EOF); qemu_put_byte(f, QEMU_VM_EOF);
@ -900,6 +951,8 @@ static int qemu_save_device_state(QEMUFile *f)
save_section_header(f, se, QEMU_VM_SECTION_FULL); save_section_header(f, se, QEMU_VM_SECTION_FULL);
vmstate_save(f, se, NULL); vmstate_save(f, se, NULL);
save_section_footer(f, se);
} }
qemu_put_byte(f, QEMU_VM_EOF); qemu_put_byte(f, QEMU_VM_EOF);
@ -1027,6 +1080,10 @@ int qemu_loadvm_state(QEMUFile *f)
" device '%s'", instance_id, idstr); " device '%s'", instance_id, idstr);
goto out; goto out;
} }
if (!check_section_footer(f, le->se)) {
ret = -EINVAL;
goto out;
}
break; break;
case QEMU_VM_SECTION_PART: case QEMU_VM_SECTION_PART:
case QEMU_VM_SECTION_END: case QEMU_VM_SECTION_END:
@ -1050,6 +1107,10 @@ int qemu_loadvm_state(QEMUFile *f)
section_id, le->se->idstr); section_id, le->se->idstr);
goto out; goto out;
} }
if (!check_section_footer(f, le->se)) {
ret = -EINVAL;
goto out;
}
break; break;
default: default:
error_report("Unknown savevm section type %d", section_type); error_report("Unknown savevm section type %d", section_type);