Files
OpenCellular/cgpt/cgpt_repair.c
Andrey Ulanov 1eb83efdba 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>
2015-06-13 02:53:36 +00:00

57 lines
1.7 KiB
C

// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <string.h>
#include "cgpt.h"
#include "cgptlib_internal.h"
#include "vboot_host.h"
int CgptRepair(CgptRepairParams *params) {
struct drive drive;
if (params == NULL)
return CGPT_FAILED;
if (CGPT_OK != DriveOpen(params->drive_name, &drive, O_RDWR,
params->drive_size))
return CGPT_FAILED;
int gpt_retval = GptSanityCheck(&drive.gpt);
if (params->verbose)
printf("GptSanityCheck() returned %d: %s\n",
gpt_retval, GptError(gpt_retval));
GptHeader *header;
if (MASK_PRIMARY == drive.gpt.valid_headers ||
MASK_BOTH == drive.gpt.valid_headers) {
header = (GptHeader *)(drive.gpt.primary_header);
} else {
header = (GptHeader *)(drive.gpt.secondary_header);
}
if (MASK_PRIMARY == drive.gpt.valid_entries) {
free(drive.gpt.secondary_entries);
drive.gpt.secondary_entries =
malloc(header->size_of_entry * header->number_of_entries);
} else if (MASK_SECONDARY == drive.gpt.valid_entries) {
free(drive.gpt.primary_entries);
drive.gpt.primary_entries =
malloc(header->size_of_entry * header->number_of_entries);
}
GptRepair(&drive.gpt);
if (drive.gpt.modified & GPT_MODIFIED_HEADER1)
printf("Primary Header is updated.\n");
if (drive.gpt.modified & GPT_MODIFIED_ENTRIES1)
printf("Primary Entries is updated.\n");
if (drive.gpt.modified & GPT_MODIFIED_ENTRIES2)
printf("Secondary Entries is updated.\n");
if (drive.gpt.modified & GPT_MODIFIED_HEADER2)
printf("Secondary Header is updated.\n");
return DriveClose(&drive, 1);
}