def verify_record(self): """ Verify an individual record """ rtype, length = self.unpack_exact(RH_FORMAT) if rtype not in rec_type_to_str: raise StreamError("Unrecognised record type %x" % (rtype, )) self.info("Libxl Record: %s, length %d" % (rec_type_to_str[rtype], length)) contentsz = (length + 7) & ~7 content = self.rdexact(contentsz) padding = content[length:] if padding != b"\x00" * len(padding): raise StreamError("Padding containing non0 bytes found") if rtype not in record_verifiers: raise RuntimeError( "No verification function for libxl record '%s'" % rec_type_to_str[rtype]) else: record_verifiers[rtype](self, content[:length]) return rtype
def verify_hdr(self): """ Verify a Header """ ident, version, options = self.unpack_exact(HDR_FORMAT) if ident != HDR_IDENT: raise StreamError("Bad image id: Expected 0x%x, got 0x%x" % (HDR_IDENT, ident)) if version != HDR_VERSION: raise StreamError("Unknown image version: Expected %d, got %d" % (HDR_VERSION, version)) if options & HDR_OPT_RESZ_MASK: raise StreamError( "Reserved bits set in image options field: 0x%x" % (options & HDR_OPT_RESZ_MASK)) if ((sys.byteorder == "little") and ((options & HDR_OPT_BIT_ENDIAN) != HDR_OPT_LE)): raise StreamError( "Stream is not native endianess - unable to validate") endian = ["little", "big"][options & HDR_OPT_LE] if options & HDR_OPT_LEGACY: self.info("Libxl Header: %s endian, legacy converted" % (endian, )) else: self.info("Libxl Header: %s endian" % (endian, ))
def verify_record(self): """ Verify an individual record """ rtype, length = self.unpack_exact(RH_FORMAT) if rtype not in rec_type_to_str: raise StreamError("Unrecognised record type 0x%x" % (rtype, )) contentsz = (length + 7) & ~7 content = self.rdexact(contentsz) if rtype != REC_TYPE_page_data: if self.squashed_pagedata_records > 0: self.info("Squashed %d Page Data records together" % (self.squashed_pagedata_records, )) self.squashed_pagedata_records = 0 self.info("Libxc Record: %s, length %d" % (rec_type_to_str[rtype], length)) else: self.squashed_pagedata_records += 1 padding = content[length:] if padding != b"\x00" * len(padding): raise StreamError("Padding containing non0 bytes found") if rtype not in record_verifiers: raise RuntimeError("No verification function for libxc record '%s'" % rec_type_to_str[rtype]) else: record_verifiers[rtype](self, content[:length]) return rtype
def verify_ihdr(self): """ Verify an Image Header """ marker, ident, version, options, res1, res2 = \ self.unpack_exact(IHDR_FORMAT) if marker != IHDR_MARKER: raise StreamError("Bad image marker: Expected 0x%x, got 0x%x" % (IHDR_MARKER, marker)) if ident != IHDR_IDENT: raise StreamError("Bad image id: Expected 0x%x, got 0x%x" % (IHDR_IDENT, ident)) if version != IHDR_VERSION: raise StreamError("Unknown image version: Expected %d, got %d" % (IHDR_VERSION, version)) if options & IHDR_OPT_RESZ_MASK: raise StreamError("Reserved bits set in image options field: 0x%x" % (options & IHDR_OPT_RESZ_MASK)) if res1 != 0 or res2 != 0: raise StreamError("Reserved bits set in image header: 0x%04x:0x%08x" % (res1, res2)) if ( (sys.byteorder == "little") and ((options & IHDR_OPT_BIT_ENDIAN) != IHDR_OPT_LE) ): raise StreamError( "Stream is not native endianess - unable to validate") endian = ["little", "big"][options & IHDR_OPT_LE] self.info("Libxc Image Header: %s endian" % (endian, ))
def verify_dhdr(self): """ Verify a domain header """ gtype, page_shift, res1, major, minor = \ self.unpack_exact(DHDR_FORMAT) if gtype not in dhdr_type_to_str: raise StreamError("Unrecognised domain type 0x%x" % (gtype, )) if res1 != 0: raise StreamError("Reserved bits set in domain header 0x%04x" % (res1, )) if page_shift != 12: raise StreamError("Page shift expected to be 12. Got %d" % (page_shift, )) if major == 0: self.info("Domain Header: legacy converted %s" % (dhdr_type_to_str[gtype], )) else: self.info("Domain Header: %s from Xen %d.%d" % (dhdr_type_to_str[gtype], major, minor))
def verify_record_tsc_info(self, content): """ tsc info record """ sz = calcsize(X86_TSC_INFO_FORMAT) if len(content) != sz: raise RecordError("Length should be %u bytes" % (sz, )) mode, khz, nsec, incarn, res1 = unpack(X86_TSC_INFO_FORMAT, content) if res1 != 0: raise StreamError("Reserved bits set in X86_TSC_INFO: 0x%08x" % (res1, )) self.info(" Mode %u, %u kHz, %u ns, incarnation %d" % (mode, khz, nsec, incarn))
def verify_record_x86_pv_vcpu_generic(self, content, name): """ Generic for all REC_TYPE_x86_pv_vcpu_{basic,extended,xsave,msrs} """ minsz = calcsize(X86_PV_VCPU_HDR_FORMAT) if len(content) <= minsz: raise RecordError("X86_PV_VCPU_%s record length must be at least %d" " bytes long" % (name, minsz)) vcpuid, res1 = unpack(X86_PV_VCPU_HDR_FORMAT, content[:minsz]) if res1 != 0: raise StreamError( "Reserved bits set in x86_pv_vcpu_%s record 0x%04x" % (name, res1)) self.info(" vcpu%d %s context, %d bytes" % (vcpuid, name, len(content) - minsz))
def verify_record_page_data(self, content): """ Page Data record """ minsz = calcsize(PAGE_DATA_FORMAT) if len(content) <= minsz: raise RecordError("PAGE_DATA record must be at least %d bytes long" % (minsz, )) count, res1 = unpack(PAGE_DATA_FORMAT, content[:minsz]) if res1 != 0: raise StreamError("Reserved bits set in PAGE_DATA record 0x%04x" % (res1, )) pfnsz = count * 8 if (len(content) - minsz) < pfnsz: raise RecordError("PAGE_DATA record must contain a pfn record for " "each count") pfns = list(unpack("=%dQ" % (count,), content[minsz:minsz + pfnsz])) nr_pages = 0 for idx, pfn in enumerate(pfns): if pfn & PAGE_DATA_PFN_RESZ_MASK: raise RecordError("Reserved bits set in pfn[%d]: 0x%016x", idx, pfn & PAGE_DATA_PFN_RESZ_MASK) if pfn >> PAGE_DATA_TYPE_SHIFT in (5, 6, 7, 8): raise RecordError("Invalid type value in pfn[%d]: 0x%016x", idx, pfn & PAGE_DATA_TYPE_LTAB_MASK) # We expect page data for each normal page or pagetable if PAGE_DATA_TYPE_NOTAB <= (pfn & PAGE_DATA_TYPE_LTABTYPE_MASK) \ <= PAGE_DATA_TYPE_L4TAB: nr_pages += 1 pagesz = nr_pages * 4096 if len(content) != minsz + pfnsz + pagesz: raise RecordError("Expected %u + %u + %u, got %u" % (minsz, pfnsz, pagesz, len(content)))
def verify_record_x86_pv_info(self, content): """ x86 PV Info record """ expectedsz = calcsize(X86_PV_INFO_FORMAT) if len(content) != expectedsz: raise RecordError("x86_pv_info: expected length of %d, got %d" % (expectedsz, len(content))) width, levels, res1, res2 = unpack(X86_PV_INFO_FORMAT, content) if width not in (4, 8): raise RecordError("Expected width of 4 or 8, got %d" % (width, )) if levels not in (3, 4): raise RecordError("Expected levels of 3 or 4, got %d" % (levels, )) if res1 != 0 or res2 != 0: raise StreamError("Reserved bits set in X86_PV_INFO: 0x%04x 0x%08x" % (res1, res2)) bitness = {4:32, 8:64}[width] self.info(" %sbit guest, %d levels of pagetables" % (bitness, levels))