From c77a89ab68f922f36b3a94a01ef3fdfceecd7b3f Mon Sep 17 00:00:00 2001 From: Artem Bityutskiy Date: Wed, 10 Apr 2013 09:03:46 +0300 Subject: [PATCH] gpt_parser: validate GPT header CRC32 Change-Id: I2837d64600876b3fcca187dd112db4590eb96ffe Signed-off-by: Artem Bityutskiy --- mic/utils/gpt_parser.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/mic/utils/gpt_parser.py b/mic/utils/gpt_parser.py index f2abdb8..d96b6b6 100644 --- a/mic/utils/gpt_parser.py +++ b/mic/utils/gpt_parser.py @@ -35,6 +35,18 @@ def _stringify_uuid(binary_uuid): return uuid_str.upper() +def _calc_header_crc(raw_hdr): + """ Calculate GPT header CRC32 checksum. The 'raw_hdr' parameter has to + be a list or a tuple containing all the elements of the GPT header in a + "raw" form, meaning that it should simply contain "unpacked" disk data. + """ + + raw_hdr = list(raw_hdr) + raw_hdr[3] = 0 + raw_hdr = struct.pack(_GPT_HEADER_FORMAT, *raw_hdr) + + return binascii.crc32(raw_hdr) & 0xFFFFFFFF + class GptParser: """ GPT partition table parser. The current implementation is simplified and it assumes that the partition table is correct, so it does not check @@ -99,6 +111,11 @@ class GptParser: raise MountError("Bad GPT header size: %d bytes, expected %d" % \ (header[2], struct.calcsize(_GPT_HEADER_FORMAT))) + crc = _calc_header_crc(header) + if header[3] != crc: + raise MountError("GPT header crc mismatch: %#x, should be %#x" % \ + (crc, header[3])) + return (header[0], # 0. Signature header[1], # 1. Revision header[2], # 2. Header size in bytes -- 2.7.4