summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorArtem Bityutskiy <artem.bityutskiy@intel.com>2013-04-10 09:03:46 +0300
committerArtem Bityutskiy <artem.bityutskiy@intel.com>2013-04-19 16:24:14 +0300
commitc77a89ab68f922f36b3a94a01ef3fdfceecd7b3f (patch)
tree17c4b017c0cb306edb709a6b24adbc90f7e16a78
parentcbf20bb9d118e377ff8ffc2fa6d06501a8b87794 (diff)
downloadmic-c77a89ab68f922f36b3a94a01ef3fdfceecd7b3f.tar.gz
mic-c77a89ab68f922f36b3a94a01ef3fdfceecd7b3f.tar.bz2
mic-c77a89ab68f922f36b3a94a01ef3fdfceecd7b3f.zip
gpt_parser: validate GPT header CRC32
Change-Id: I2837d64600876b3fcca187dd112db4590eb96ffe Signed-off-by: Artem Bityutskiy <artem.bityutskiy@intel.com>
-rw-r--r--mic/utils/gpt_parser.py17
1 files changed, 17 insertions, 0 deletions
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