cgpt repair: fix segfault which occurs when one of the headers is bad

When one of GPT headers is invalid the corresponding partition table
is not loaded and corresponding pointers in GptData are NULL.
GptRepair will try to memcpy one entries table to another which
results in SIGSEGV.

This change fixes it by freeing and then reallocating bad copy of
partition table. This potentially fixes problems which would occur
if two tables have different size.

Change that initially introduced this problem by not always allocating
secondary_entries:
https://chromium-review.googlesource.com/223800

TEST="cgpt repair" works where it previously didn't
TEST=make runtests
BUG=brillo:1203
BRANCH=none

Change-Id: Ibb2fcf33faa5ba157b0865d04c90ee3f26eee113
Reviewed-on: https://chromium-review.googlesource.com/276766
Reviewed-by: Randall Spangler <rspangler@chromium.org>
Commit-Queue: Andrey Ulanov <andreyu@google.com>
Tested-by: Andrey Ulanov <andreyu@google.com>
This commit is contained in:
Andrey Ulanov
2015-06-10 20:02:06 -07:00
committed by ChromeOS Commit Bot
parent 637ff03502
commit 1eb83efdba
4 changed files with 40 additions and 2 deletions

View File

@@ -125,6 +125,10 @@ int GptSanityCheck(GptData *gpt);
* Repair GPT data by copying from one set of valid headers/entries to the
* other. Assumes GptSanityCheck() has been run to determine which headers
* and/or entries are already valid.
*
* The caller must make sure that even if one of the entries table is invalid
* then corresponding buffer is allocated and big enough to accommodate entries
* from the other (good) table.
*/
void GptRepair(GptData *gpt);