futility: vbutil_kernel should check the kernel a bit more

Trying to repack an invalid kernel blob should notice and
complain that the blob is invalid instead of just segfaulting.

BUG=chromium:452353
BRANCH=ToT
TEST=manual

See the bug report for an example test case. Many more tests are
being added as futility development continues.

Change-Id: I2bbfb8ab41a0f596b25a76f76f74d7b47b7e4a17
Signed-off-by: Bill Richardson <wfrichar@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/243583
Reviewed-by: Mike Frysinger <vapier@chromium.org>
Reviewed-by: Randall Spangler <rspangler@chromium.org>
diff --git a/futility/cmd_vbutil_kernel.c b/futility/cmd_vbutil_kernel.c
index 3b2643e..40deba9 100644
--- a/futility/cmd_vbutil_kernel.c
+++ b/futility/cmd_vbutil_kernel.c
@@ -21,6 +21,7 @@
 #include "futility.h"
 #include "host_common.h"
 #include "kernel_blob.h"
+#include "traversal.h"
 #include "vb1_helper.h"
 
 static void Fatal(const char *format, ...)
@@ -461,6 +462,11 @@
 		/* Load the kernel partition */
 		kpart_data = ReadOldKPartFromFileOrDie(oldfile, &kpart_size);
 
+		/* Make sure we have a kernel partition */
+		if (FILE_TYPE_KERN_PREAMBLE !=
+		    futil_what_file_type_buf(kpart_data, kpart_size))
+			Fatal("%s is not a kernel blob\n", oldfile);
+
 		kblob_data = UnpackKPart(kpart_data, kpart_size, opt_pad,
 					 &keyblock, &preamble, &kblob_size);