| Amin Hassani | f94b643 | 2018-01-26 17:39:47 -0800 | [diff] [blame] | 1 | # | 
|  | 2 | # Copyright (C) 2013 The Android Open Source Project | 
|  | 3 | # | 
|  | 4 | # Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | # you may not use this file except in compliance with the License. | 
|  | 6 | # You may obtain a copy of the License at | 
|  | 7 | # | 
|  | 8 | #      http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | # | 
|  | 10 | # Unless required by applicable law or agreed to in writing, software | 
|  | 11 | # distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | # See the License for the specific language governing permissions and | 
|  | 14 | # limitations under the License. | 
|  | 15 | # | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 16 |  | 
|  | 17 | """Verifying the integrity of a Chrome OS update payload. | 
|  | 18 |  | 
|  | 19 | This module is used internally by the main Payload class for verifying the | 
|  | 20 | integrity of an update payload. The interface for invoking the checks is as | 
|  | 21 | follows: | 
|  | 22 |  | 
|  | 23 | checker = PayloadChecker(payload) | 
|  | 24 | checker.Run(...) | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 25 | """ | 
|  | 26 |  | 
| Gilad Arnold | f583a7d | 2015-02-05 13:23:55 -0800 | [diff] [blame] | 27 | from __future__ import print_function | 
|  | 28 |  | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 29 | import array | 
|  | 30 | import base64 | 
| Tudor Brindus | 8d05a7e | 2018-06-14 11:18:18 -0700 | [diff] [blame] | 31 | import collections | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 32 | import hashlib | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 33 | import itertools | 
| Gilad Arnold | 9b90c93 | 2013-05-22 17:12:56 -0700 | [diff] [blame] | 34 | import os | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 35 | import subprocess | 
|  | 36 |  | 
| Amin Hassani | b05a65a | 2017-12-18 15:15:32 -0800 | [diff] [blame] | 37 | from update_payload import common | 
|  | 38 | from update_payload import error | 
|  | 39 | from update_payload import format_utils | 
|  | 40 | from update_payload import histogram | 
|  | 41 | from update_payload import update_metadata_pb2 | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 42 |  | 
|  | 43 |  | 
|  | 44 | # | 
| Gilad Arnold | 9b90c93 | 2013-05-22 17:12:56 -0700 | [diff] [blame] | 45 | # Constants. | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 46 | # | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 47 |  | 
| Gilad Arnold | eaed0d1 | 2013-04-30 15:38:22 -0700 | [diff] [blame] | 48 | _CHECK_DST_PSEUDO_EXTENTS = 'dst-pseudo-extents' | 
|  | 49 | _CHECK_MOVE_SAME_SRC_DST_BLOCK = 'move-same-src-dst-block' | 
|  | 50 | _CHECK_PAYLOAD_SIG = 'payload-sig' | 
|  | 51 | CHECKS_TO_DISABLE = ( | 
| Gilad Arnold | 382df5c | 2013-05-03 12:49:28 -0700 | [diff] [blame] | 52 | _CHECK_DST_PSEUDO_EXTENTS, | 
|  | 53 | _CHECK_MOVE_SAME_SRC_DST_BLOCK, | 
|  | 54 | _CHECK_PAYLOAD_SIG, | 
| Gilad Arnold | eaed0d1 | 2013-04-30 15:38:22 -0700 | [diff] [blame] | 55 | ) | 
|  | 56 |  | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 57 | _TYPE_FULL = 'full' | 
|  | 58 | _TYPE_DELTA = 'delta' | 
|  | 59 |  | 
|  | 60 | _DEFAULT_BLOCK_SIZE = 4096 | 
|  | 61 |  | 
| Gilad Arnold | 9b90c93 | 2013-05-22 17:12:56 -0700 | [diff] [blame] | 62 | _DEFAULT_PUBKEY_BASE_NAME = 'update-payload-key.pub.pem' | 
|  | 63 | _DEFAULT_PUBKEY_FILE_NAME = os.path.join(os.path.dirname(__file__), | 
|  | 64 | _DEFAULT_PUBKEY_BASE_NAME) | 
|  | 65 |  | 
| Gilad Arnold | 0d575cd | 2015-07-13 17:29:21 -0700 | [diff] [blame] | 66 | # Supported minor version map to payload types allowed to be using them. | 
|  | 67 | _SUPPORTED_MINOR_VERSIONS = { | 
|  | 68 | 0: (_TYPE_FULL,), | 
|  | 69 | 1: (_TYPE_DELTA,), | 
|  | 70 | 2: (_TYPE_DELTA,), | 
| Sen Jiang | 912c4df | 2015-12-10 12:17:13 -0800 | [diff] [blame] | 71 | 3: (_TYPE_DELTA,), | 
| Sen Jiang | 92161a7 | 2016-06-28 16:09:38 -0700 | [diff] [blame] | 72 | 4: (_TYPE_DELTA,), | 
| Amin Hassani | 77d7cbc | 2018-02-07 16:21:33 -0800 | [diff] [blame] | 73 | 5: (_TYPE_DELTA,), | 
| Gilad Arnold | 0d575cd | 2015-07-13 17:29:21 -0700 | [diff] [blame] | 74 | } | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 75 |  | 
| Gilad Arnold | 06eea33 | 2015-07-13 18:06:33 -0700 | [diff] [blame] | 76 | _OLD_DELTA_USABLE_PART_SIZE = 2 * 1024 * 1024 * 1024 | 
|  | 77 |  | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 78 | # | 
|  | 79 | # Helper functions. | 
|  | 80 | # | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 81 |  | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 82 | def _IsPowerOfTwo(val): | 
|  | 83 | """Returns True iff val is a power of two.""" | 
|  | 84 | return val > 0 and (val & (val - 1)) == 0 | 
|  | 85 |  | 
|  | 86 |  | 
|  | 87 | def _AddFormat(format_func, value): | 
|  | 88 | """Adds a custom formatted representation to ordinary string representation. | 
|  | 89 |  | 
|  | 90 | Args: | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 91 | format_func: A value formatter. | 
|  | 92 | value: Value to be formatted and returned. | 
| Gilad Arnold | f583a7d | 2015-02-05 13:23:55 -0800 | [diff] [blame] | 93 |  | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 94 | Returns: | 
|  | 95 | A string 'x (y)' where x = str(value) and y = format_func(value). | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 96 | """ | 
| Gilad Arnold | 6a3a387 | 2013-10-04 18:18:45 -0700 | [diff] [blame] | 97 | ret = str(value) | 
|  | 98 | formatted_str = format_func(value) | 
|  | 99 | if formatted_str: | 
|  | 100 | ret += ' (%s)' % formatted_str | 
|  | 101 | return ret | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 102 |  | 
|  | 103 |  | 
|  | 104 | def _AddHumanReadableSize(size): | 
|  | 105 | """Adds a human readable representation to a byte size value.""" | 
|  | 106 | return _AddFormat(format_utils.BytesToHumanReadable, size) | 
|  | 107 |  | 
|  | 108 |  | 
|  | 109 | # | 
|  | 110 | # Payload report generator. | 
|  | 111 | # | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 112 |  | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 113 | class _PayloadReport(object): | 
|  | 114 | """A payload report generator. | 
|  | 115 |  | 
|  | 116 | A report is essentially a sequence of nodes, which represent data points. It | 
|  | 117 | is initialized to have a "global", untitled section. A node may be a | 
|  | 118 | sub-report itself. | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 119 | """ | 
|  | 120 |  | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 121 | # Report nodes: Field, sub-report, section. | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 122 | class Node(object): | 
|  | 123 | """A report node interface.""" | 
|  | 124 |  | 
|  | 125 | @staticmethod | 
|  | 126 | def _Indent(indent, line): | 
|  | 127 | """Indents a line by a given indentation amount. | 
|  | 128 |  | 
|  | 129 | Args: | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 130 | indent: The indentation amount. | 
|  | 131 | line: The line content (string). | 
| Gilad Arnold | f583a7d | 2015-02-05 13:23:55 -0800 | [diff] [blame] | 132 |  | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 133 | Returns: | 
|  | 134 | The properly indented line (string). | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 135 | """ | 
|  | 136 | return '%*s%s' % (indent, '', line) | 
|  | 137 |  | 
|  | 138 | def GenerateLines(self, base_indent, sub_indent, curr_section): | 
|  | 139 | """Generates the report lines for this node. | 
|  | 140 |  | 
|  | 141 | Args: | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 142 | base_indent: Base indentation for each line. | 
|  | 143 | sub_indent: Additional indentation for sub-nodes. | 
|  | 144 | curr_section: The current report section object. | 
| Gilad Arnold | f583a7d | 2015-02-05 13:23:55 -0800 | [diff] [blame] | 145 |  | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 146 | Returns: | 
|  | 147 | A pair consisting of a list of properly indented report lines and a new | 
|  | 148 | current section object. | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 149 | """ | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 150 | raise NotImplementedError | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 151 |  | 
|  | 152 | class FieldNode(Node): | 
|  | 153 | """A field report node, representing a (name, value) pair.""" | 
|  | 154 |  | 
|  | 155 | def __init__(self, name, value, linebreak, indent): | 
|  | 156 | super(_PayloadReport.FieldNode, self).__init__() | 
|  | 157 | self.name = name | 
|  | 158 | self.value = value | 
|  | 159 | self.linebreak = linebreak | 
|  | 160 | self.indent = indent | 
|  | 161 |  | 
|  | 162 | def GenerateLines(self, base_indent, sub_indent, curr_section): | 
|  | 163 | """Generates a properly formatted 'name : value' entry.""" | 
|  | 164 | report_output = '' | 
|  | 165 | if self.name: | 
|  | 166 | report_output += self.name.ljust(curr_section.max_field_name_len) + ' :' | 
|  | 167 | value_lines = str(self.value).splitlines() | 
|  | 168 | if self.linebreak and self.name: | 
|  | 169 | report_output += '\n' + '\n'.join( | 
|  | 170 | ['%*s%s' % (self.indent, '', line) for line in value_lines]) | 
|  | 171 | else: | 
|  | 172 | if self.name: | 
|  | 173 | report_output += ' ' | 
|  | 174 | report_output += '%*s' % (self.indent, '') | 
|  | 175 | cont_line_indent = len(report_output) | 
|  | 176 | indented_value_lines = [value_lines[0]] | 
|  | 177 | indented_value_lines.extend(['%*s%s' % (cont_line_indent, '', line) | 
|  | 178 | for line in value_lines[1:]]) | 
|  | 179 | report_output += '\n'.join(indented_value_lines) | 
|  | 180 |  | 
|  | 181 | report_lines = [self._Indent(base_indent, line + '\n') | 
|  | 182 | for line in report_output.split('\n')] | 
|  | 183 | return report_lines, curr_section | 
|  | 184 |  | 
|  | 185 | class SubReportNode(Node): | 
|  | 186 | """A sub-report node, representing a nested report.""" | 
|  | 187 |  | 
|  | 188 | def __init__(self, title, report): | 
|  | 189 | super(_PayloadReport.SubReportNode, self).__init__() | 
|  | 190 | self.title = title | 
|  | 191 | self.report = report | 
|  | 192 |  | 
|  | 193 | def GenerateLines(self, base_indent, sub_indent, curr_section): | 
|  | 194 | """Recurse with indentation.""" | 
|  | 195 | report_lines = [self._Indent(base_indent, self.title + ' =>\n')] | 
|  | 196 | report_lines.extend(self.report.GenerateLines(base_indent + sub_indent, | 
|  | 197 | sub_indent)) | 
|  | 198 | return report_lines, curr_section | 
|  | 199 |  | 
|  | 200 | class SectionNode(Node): | 
|  | 201 | """A section header node.""" | 
|  | 202 |  | 
|  | 203 | def __init__(self, title=None): | 
|  | 204 | super(_PayloadReport.SectionNode, self).__init__() | 
|  | 205 | self.title = title | 
|  | 206 | self.max_field_name_len = 0 | 
|  | 207 |  | 
|  | 208 | def GenerateLines(self, base_indent, sub_indent, curr_section): | 
|  | 209 | """Dump a title line, return self as the (new) current section.""" | 
|  | 210 | report_lines = [] | 
|  | 211 | if self.title: | 
|  | 212 | report_lines.append(self._Indent(base_indent, | 
|  | 213 | '=== %s ===\n' % self.title)) | 
|  | 214 | return report_lines, self | 
|  | 215 |  | 
|  | 216 | def __init__(self): | 
|  | 217 | self.report = [] | 
|  | 218 | self.last_section = self.global_section = self.SectionNode() | 
|  | 219 | self.is_finalized = False | 
|  | 220 |  | 
|  | 221 | def GenerateLines(self, base_indent, sub_indent): | 
|  | 222 | """Generates the lines in the report, properly indented. | 
|  | 223 |  | 
|  | 224 | Args: | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 225 | base_indent: The indentation used for root-level report lines. | 
|  | 226 | sub_indent: The indentation offset used for sub-reports. | 
| Gilad Arnold | f583a7d | 2015-02-05 13:23:55 -0800 | [diff] [blame] | 227 |  | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 228 | Returns: | 
|  | 229 | A list of indented report lines. | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 230 | """ | 
|  | 231 | report_lines = [] | 
|  | 232 | curr_section = self.global_section | 
|  | 233 | for node in self.report: | 
|  | 234 | node_report_lines, curr_section = node.GenerateLines( | 
|  | 235 | base_indent, sub_indent, curr_section) | 
|  | 236 | report_lines.extend(node_report_lines) | 
|  | 237 |  | 
|  | 238 | return report_lines | 
|  | 239 |  | 
|  | 240 | def Dump(self, out_file, base_indent=0, sub_indent=2): | 
|  | 241 | """Dumps the report to a file. | 
|  | 242 |  | 
|  | 243 | Args: | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 244 | out_file: File object to output the content to. | 
|  | 245 | base_indent: Base indentation for report lines. | 
|  | 246 | sub_indent: Added indentation for sub-reports. | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 247 | """ | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 248 | report_lines = self.GenerateLines(base_indent, sub_indent) | 
|  | 249 | if report_lines and not self.is_finalized: | 
|  | 250 | report_lines.append('(incomplete report)\n') | 
|  | 251 |  | 
|  | 252 | for line in report_lines: | 
|  | 253 | out_file.write(line) | 
|  | 254 |  | 
|  | 255 | def AddField(self, name, value, linebreak=False, indent=0): | 
|  | 256 | """Adds a field/value pair to the payload report. | 
|  | 257 |  | 
|  | 258 | Args: | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 259 | name: The field's name. | 
|  | 260 | value: The field's value. | 
|  | 261 | linebreak: Whether the value should be printed on a new line. | 
|  | 262 | indent: Amount of extra indent for each line of the value. | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 263 | """ | 
|  | 264 | assert not self.is_finalized | 
|  | 265 | if name and self.last_section.max_field_name_len < len(name): | 
|  | 266 | self.last_section.max_field_name_len = len(name) | 
|  | 267 | self.report.append(self.FieldNode(name, value, linebreak, indent)) | 
|  | 268 |  | 
|  | 269 | def AddSubReport(self, title): | 
|  | 270 | """Adds and returns a sub-report with a title.""" | 
|  | 271 | assert not self.is_finalized | 
|  | 272 | sub_report = self.SubReportNode(title, type(self)()) | 
|  | 273 | self.report.append(sub_report) | 
|  | 274 | return sub_report.report | 
|  | 275 |  | 
|  | 276 | def AddSection(self, title): | 
|  | 277 | """Adds a new section title.""" | 
|  | 278 | assert not self.is_finalized | 
|  | 279 | self.last_section = self.SectionNode(title) | 
|  | 280 | self.report.append(self.last_section) | 
|  | 281 |  | 
|  | 282 | def Finalize(self): | 
|  | 283 | """Seals the report, marking it as complete.""" | 
|  | 284 | self.is_finalized = True | 
|  | 285 |  | 
|  | 286 |  | 
|  | 287 | # | 
|  | 288 | # Payload verification. | 
|  | 289 | # | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 290 |  | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 291 | class PayloadChecker(object): | 
|  | 292 | """Checking the integrity of an update payload. | 
|  | 293 |  | 
|  | 294 | This is a short-lived object whose purpose is to isolate the logic used for | 
|  | 295 | verifying the integrity of an update payload. | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 296 | """ | 
|  | 297 |  | 
| Gilad Arnold | eaed0d1 | 2013-04-30 15:38:22 -0700 | [diff] [blame] | 298 | def __init__(self, payload, assert_type=None, block_size=0, | 
|  | 299 | allow_unhashed=False, disabled_tests=()): | 
| Gilad Arnold | 272a499 | 2013-05-08 13:12:53 -0700 | [diff] [blame] | 300 | """Initialize the checker. | 
| Gilad Arnold | eaed0d1 | 2013-04-30 15:38:22 -0700 | [diff] [blame] | 301 |  | 
|  | 302 | Args: | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 303 | payload: The payload object to check. | 
|  | 304 | assert_type: Assert that payload is either 'full' or 'delta' (optional). | 
|  | 305 | block_size: Expected filesystem / payload block size (optional). | 
|  | 306 | allow_unhashed: Allow operations with unhashed data blobs. | 
|  | 307 | disabled_tests: Sequence of tests to disable. | 
| Gilad Arnold | eaed0d1 | 2013-04-30 15:38:22 -0700 | [diff] [blame] | 308 | """ | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 309 | if not payload.is_init: | 
|  | 310 | raise ValueError('Uninitialized update payload.') | 
| Gilad Arnold | eaed0d1 | 2013-04-30 15:38:22 -0700 | [diff] [blame] | 311 |  | 
|  | 312 | # Set checker configuration. | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 313 | self.payload = payload | 
| Gilad Arnold | eaed0d1 | 2013-04-30 15:38:22 -0700 | [diff] [blame] | 314 | self.block_size = block_size if block_size else _DEFAULT_BLOCK_SIZE | 
|  | 315 | if not _IsPowerOfTwo(self.block_size): | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 316 | raise error.PayloadError( | 
|  | 317 | 'Expected block (%d) size is not a power of two.' % self.block_size) | 
| Gilad Arnold | eaed0d1 | 2013-04-30 15:38:22 -0700 | [diff] [blame] | 318 | if assert_type not in (None, _TYPE_FULL, _TYPE_DELTA): | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 319 | raise error.PayloadError('Invalid assert_type value (%r).' % | 
|  | 320 | assert_type) | 
| Gilad Arnold | eaed0d1 | 2013-04-30 15:38:22 -0700 | [diff] [blame] | 321 | self.payload_type = assert_type | 
|  | 322 | self.allow_unhashed = allow_unhashed | 
|  | 323 |  | 
|  | 324 | # Disable specific tests. | 
|  | 325 | self.check_dst_pseudo_extents = ( | 
|  | 326 | _CHECK_DST_PSEUDO_EXTENTS not in disabled_tests) | 
|  | 327 | self.check_move_same_src_dst_block = ( | 
|  | 328 | _CHECK_MOVE_SAME_SRC_DST_BLOCK not in disabled_tests) | 
|  | 329 | self.check_payload_sig = _CHECK_PAYLOAD_SIG not in disabled_tests | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 330 |  | 
|  | 331 | # Reset state; these will be assigned when the manifest is checked. | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 332 | self.sigs_offset = 0 | 
|  | 333 | self.sigs_size = 0 | 
| Tudor Brindus | 8d05a7e | 2018-06-14 11:18:18 -0700 | [diff] [blame] | 334 | self.old_part_info = {} | 
|  | 335 | self.new_part_info = {} | 
|  | 336 | self.new_fs_sizes = collections.defaultdict(int) | 
|  | 337 | self.old_fs_sizes = collections.defaultdict(int) | 
| Gilad Arnold | 0d575cd | 2015-07-13 17:29:21 -0700 | [diff] [blame] | 338 | self.minor_version = None | 
| Tudor Brindus | 40506cd | 2018-06-18 20:18:17 -0700 | [diff] [blame] | 339 | self.major_version = None | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 340 |  | 
|  | 341 | @staticmethod | 
|  | 342 | def _CheckElem(msg, name, report, is_mandatory, is_submsg, convert=str, | 
|  | 343 | msg_name=None, linebreak=False, indent=0): | 
|  | 344 | """Adds an element from a protobuf message to the payload report. | 
|  | 345 |  | 
|  | 346 | Checks to see whether a message contains a given element, and if so adds | 
|  | 347 | the element value to the provided report. A missing mandatory element | 
|  | 348 | causes an exception to be raised. | 
|  | 349 |  | 
|  | 350 | Args: | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 351 | msg: The message containing the element. | 
|  | 352 | name: The name of the element. | 
|  | 353 | report: A report object to add the element name/value to. | 
|  | 354 | is_mandatory: Whether or not this element must be present. | 
|  | 355 | is_submsg: Whether this element is itself a message. | 
|  | 356 | convert: A function for converting the element value for reporting. | 
|  | 357 | msg_name: The name of the message object (for error reporting). | 
|  | 358 | linebreak: Whether the value report should induce a line break. | 
|  | 359 | indent: Amount of indent used for reporting the value. | 
| Gilad Arnold | f583a7d | 2015-02-05 13:23:55 -0800 | [diff] [blame] | 360 |  | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 361 | Returns: | 
|  | 362 | A pair consisting of the element value and the generated sub-report for | 
|  | 363 | it (if the element is a sub-message, None otherwise). If the element is | 
|  | 364 | missing, returns (None, None). | 
| Gilad Arnold | f583a7d | 2015-02-05 13:23:55 -0800 | [diff] [blame] | 365 |  | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 366 | Raises: | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 367 | error.PayloadError if a mandatory element is missing. | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 368 | """ | 
| Tudor Brindus | 8d05a7e | 2018-06-14 11:18:18 -0700 | [diff] [blame] | 369 | element_result = collections.namedtuple('element_result', ['msg', 'report']) | 
|  | 370 |  | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 371 | if not msg.HasField(name): | 
|  | 372 | if is_mandatory: | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 373 | raise error.PayloadError('%smissing mandatory %s %r.' % | 
|  | 374 | (msg_name + ' ' if msg_name else '', | 
|  | 375 | 'sub-message' if is_submsg else 'field', | 
|  | 376 | name)) | 
| Tudor Brindus | 8d05a7e | 2018-06-14 11:18:18 -0700 | [diff] [blame] | 377 | return element_result(None, None) | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 378 |  | 
|  | 379 | value = getattr(msg, name) | 
|  | 380 | if is_submsg: | 
| Tudor Brindus | 8d05a7e | 2018-06-14 11:18:18 -0700 | [diff] [blame] | 381 | return element_result(value, report and report.AddSubReport(name)) | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 382 | else: | 
|  | 383 | if report: | 
|  | 384 | report.AddField(name, convert(value), linebreak=linebreak, | 
|  | 385 | indent=indent) | 
| Tudor Brindus | 8d05a7e | 2018-06-14 11:18:18 -0700 | [diff] [blame] | 386 | return element_result(value, None) | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 387 |  | 
|  | 388 | @staticmethod | 
| Tudor Brindus | 40506cd | 2018-06-18 20:18:17 -0700 | [diff] [blame] | 389 | def _CheckRepeatedElemNotPresent(msg, field_name, msg_name): | 
|  | 390 | """Checks that a repeated element is not specified in the message. | 
|  | 391 |  | 
|  | 392 | Args: | 
|  | 393 | msg: The message containing the element. | 
|  | 394 | field_name: The name of the element. | 
|  | 395 | msg_name: The name of the message object (for error reporting). | 
|  | 396 |  | 
|  | 397 | Raises: | 
|  | 398 | error.PayloadError if the repeated element is present or non-empty. | 
|  | 399 | """ | 
|  | 400 | if getattr(msg, field_name, None): | 
|  | 401 | raise error.PayloadError('%sfield %r not empty.' % | 
|  | 402 | (msg_name + ' ' if msg_name else '', field_name)) | 
|  | 403 |  | 
|  | 404 | @staticmethod | 
|  | 405 | def _CheckElemNotPresent(msg, field_name, msg_name): | 
|  | 406 | """Checks that an element is not specified in the message. | 
|  | 407 |  | 
|  | 408 | Args: | 
|  | 409 | msg: The message containing the element. | 
|  | 410 | field_name: The name of the element. | 
|  | 411 | msg_name: The name of the message object (for error reporting). | 
|  | 412 |  | 
|  | 413 | Raises: | 
|  | 414 | error.PayloadError if the repeated element is present. | 
|  | 415 | """ | 
|  | 416 | if msg.HasField(field_name): | 
|  | 417 | raise error.PayloadError('%sfield %r exists.' % | 
|  | 418 | (msg_name + ' ' if msg_name else '', field_name)) | 
|  | 419 |  | 
|  | 420 | @staticmethod | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 421 | def _CheckMandatoryField(msg, field_name, report, msg_name, convert=str, | 
|  | 422 | linebreak=False, indent=0): | 
|  | 423 | """Adds a mandatory field; returning first component from _CheckElem.""" | 
|  | 424 | return PayloadChecker._CheckElem(msg, field_name, report, True, False, | 
|  | 425 | convert=convert, msg_name=msg_name, | 
|  | 426 | linebreak=linebreak, indent=indent)[0] | 
|  | 427 |  | 
|  | 428 | @staticmethod | 
|  | 429 | def _CheckOptionalField(msg, field_name, report, convert=str, | 
|  | 430 | linebreak=False, indent=0): | 
|  | 431 | """Adds an optional field; returning first component from _CheckElem.""" | 
|  | 432 | return PayloadChecker._CheckElem(msg, field_name, report, False, False, | 
|  | 433 | convert=convert, linebreak=linebreak, | 
|  | 434 | indent=indent)[0] | 
|  | 435 |  | 
|  | 436 | @staticmethod | 
|  | 437 | def _CheckMandatorySubMsg(msg, submsg_name, report, msg_name): | 
|  | 438 | """Adds a mandatory sub-message; wrapper for _CheckElem.""" | 
|  | 439 | return PayloadChecker._CheckElem(msg, submsg_name, report, True, True, | 
|  | 440 | msg_name) | 
|  | 441 |  | 
|  | 442 | @staticmethod | 
|  | 443 | def _CheckOptionalSubMsg(msg, submsg_name, report): | 
|  | 444 | """Adds an optional sub-message; wrapper for _CheckElem.""" | 
|  | 445 | return PayloadChecker._CheckElem(msg, submsg_name, report, False, True) | 
|  | 446 |  | 
|  | 447 | @staticmethod | 
|  | 448 | def _CheckPresentIff(val1, val2, name1, name2, obj_name): | 
|  | 449 | """Checks that val1 is None iff val2 is None. | 
|  | 450 |  | 
|  | 451 | Args: | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 452 | val1: first value to be compared. | 
|  | 453 | val2: second value to be compared. | 
|  | 454 | name1: name of object holding the first value. | 
|  | 455 | name2: name of object holding the second value. | 
|  | 456 | obj_name: Name of the object containing these values. | 
| Gilad Arnold | f583a7d | 2015-02-05 13:23:55 -0800 | [diff] [blame] | 457 |  | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 458 | Raises: | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 459 | error.PayloadError if assertion does not hold. | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 460 | """ | 
|  | 461 | if None in (val1, val2) and val1 is not val2: | 
|  | 462 | present, missing = (name1, name2) if val2 is None else (name2, name1) | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 463 | raise error.PayloadError('%r present without %r%s.' % | 
|  | 464 | (present, missing, | 
|  | 465 | ' in ' + obj_name if obj_name else '')) | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 466 |  | 
|  | 467 | @staticmethod | 
| Tudor Brindus | 40506cd | 2018-06-18 20:18:17 -0700 | [diff] [blame] | 468 | def _CheckPresentIffMany(vals, name, obj_name): | 
|  | 469 | """Checks that a set of vals and names imply every other element. | 
|  | 470 |  | 
|  | 471 | Args: | 
|  | 472 | vals: The set of values to be compared. | 
|  | 473 | name: The name of the objects holding the corresponding value. | 
|  | 474 | obj_name: Name of the object containing these values. | 
|  | 475 |  | 
|  | 476 | Raises: | 
|  | 477 | error.PayloadError if assertion does not hold. | 
|  | 478 | """ | 
|  | 479 | if any(vals) and not all(vals): | 
|  | 480 | raise error.PayloadError('%r is not present in all values%s.' % | 
|  | 481 | (name, ' in ' + obj_name if obj_name else '')) | 
|  | 482 |  | 
|  | 483 | @staticmethod | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 484 | def _Run(cmd, send_data=None): | 
|  | 485 | """Runs a subprocess, returns its output. | 
|  | 486 |  | 
|  | 487 | Args: | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 488 | cmd: Sequence of command-line argument for invoking the subprocess. | 
|  | 489 | send_data: Data to feed to the process via its stdin. | 
| Gilad Arnold | f583a7d | 2015-02-05 13:23:55 -0800 | [diff] [blame] | 490 |  | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 491 | Returns: | 
|  | 492 | A tuple containing the stdout and stderr output of the process. | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 493 | """ | 
|  | 494 | run_process = subprocess.Popen(cmd, stdin=subprocess.PIPE, | 
|  | 495 | stdout=subprocess.PIPE) | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 496 | try: | 
|  | 497 | result = run_process.communicate(input=send_data) | 
|  | 498 | finally: | 
|  | 499 | exit_code = run_process.wait() | 
|  | 500 |  | 
|  | 501 | if exit_code: | 
|  | 502 | raise RuntimeError('Subprocess %r failed with code %r.' % | 
|  | 503 | (cmd, exit_code)) | 
|  | 504 |  | 
|  | 505 | return result | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 506 |  | 
|  | 507 | @staticmethod | 
|  | 508 | def _CheckSha256Signature(sig_data, pubkey_file_name, actual_hash, sig_name): | 
|  | 509 | """Verifies an actual hash against a signed one. | 
|  | 510 |  | 
|  | 511 | Args: | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 512 | sig_data: The raw signature data. | 
|  | 513 | pubkey_file_name: Public key used for verifying signature. | 
|  | 514 | actual_hash: The actual hash digest. | 
|  | 515 | sig_name: Signature name for error reporting. | 
| Gilad Arnold | f583a7d | 2015-02-05 13:23:55 -0800 | [diff] [blame] | 516 |  | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 517 | Raises: | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 518 | error.PayloadError if signature could not be verified. | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 519 | """ | 
|  | 520 | if len(sig_data) != 256: | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 521 | raise error.PayloadError( | 
|  | 522 | '%s: signature size (%d) not as expected (256).' % | 
|  | 523 | (sig_name, len(sig_data))) | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 524 | signed_data, _ = PayloadChecker._Run( | 
|  | 525 | ['openssl', 'rsautl', '-verify', '-pubin', '-inkey', pubkey_file_name], | 
|  | 526 | send_data=sig_data) | 
|  | 527 |  | 
| Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 528 | if len(signed_data) != len(common.SIG_ASN1_HEADER) + 32: | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 529 | raise error.PayloadError('%s: unexpected signed data length (%d).' % | 
|  | 530 | (sig_name, len(signed_data))) | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 531 |  | 
| Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 532 | if not signed_data.startswith(common.SIG_ASN1_HEADER): | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 533 | raise error.PayloadError('%s: not containing standard ASN.1 prefix.' % | 
|  | 534 | sig_name) | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 535 |  | 
| Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 536 | signed_hash = signed_data[len(common.SIG_ASN1_HEADER):] | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 537 | if signed_hash != actual_hash: | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 538 | raise error.PayloadError( | 
|  | 539 | '%s: signed hash (%s) different from actual (%s).' % | 
|  | 540 | (sig_name, common.FormatSha256(signed_hash), | 
|  | 541 | common.FormatSha256(actual_hash))) | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 542 |  | 
|  | 543 | @staticmethod | 
|  | 544 | def _CheckBlocksFitLength(length, num_blocks, block_size, length_name, | 
|  | 545 | block_name=None): | 
|  | 546 | """Checks that a given length fits given block space. | 
|  | 547 |  | 
|  | 548 | This ensures that the number of blocks allocated is appropriate for the | 
|  | 549 | length of the data residing in these blocks. | 
|  | 550 |  | 
|  | 551 | Args: | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 552 | length: The actual length of the data. | 
|  | 553 | num_blocks: The number of blocks allocated for it. | 
|  | 554 | block_size: The size of each block in bytes. | 
|  | 555 | length_name: Name of length (used for error reporting). | 
|  | 556 | block_name: Name of block (used for error reporting). | 
| Gilad Arnold | f583a7d | 2015-02-05 13:23:55 -0800 | [diff] [blame] | 557 |  | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 558 | Raises: | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 559 | error.PayloadError if the aforementioned invariant is not satisfied. | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 560 | """ | 
|  | 561 | # Check: length <= num_blocks * block_size. | 
| Gilad Arnold | 382df5c | 2013-05-03 12:49:28 -0700 | [diff] [blame] | 562 | if length > num_blocks * block_size: | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 563 | raise error.PayloadError( | 
|  | 564 | '%s (%d) > num %sblocks (%d) * block_size (%d).' % | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 565 | (length_name, length, block_name or '', num_blocks, block_size)) | 
|  | 566 |  | 
|  | 567 | # Check: length > (num_blocks - 1) * block_size. | 
| Gilad Arnold | 382df5c | 2013-05-03 12:49:28 -0700 | [diff] [blame] | 568 | if length <= (num_blocks - 1) * block_size: | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 569 | raise error.PayloadError( | 
|  | 570 | '%s (%d) <= (num %sblocks - 1 (%d)) * block_size (%d).' % | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 571 | (length_name, length, block_name or '', num_blocks - 1, block_size)) | 
|  | 572 |  | 
| Gilad Arnold | 0d575cd | 2015-07-13 17:29:21 -0700 | [diff] [blame] | 573 | def _CheckManifestMinorVersion(self, report): | 
|  | 574 | """Checks the payload manifest minor_version field. | 
| Allie Wood | 7cf9f13 | 2015-02-26 14:28:19 -0800 | [diff] [blame] | 575 |  | 
|  | 576 | Args: | 
|  | 577 | report: The report object to add to. | 
| Allie Wood | 7cf9f13 | 2015-02-26 14:28:19 -0800 | [diff] [blame] | 578 |  | 
|  | 579 | Raises: | 
| Gilad Arnold | 0d575cd | 2015-07-13 17:29:21 -0700 | [diff] [blame] | 580 | error.PayloadError if any of the checks fail. | 
| Allie Wood | 7cf9f13 | 2015-02-26 14:28:19 -0800 | [diff] [blame] | 581 | """ | 
| Gilad Arnold | 0d575cd | 2015-07-13 17:29:21 -0700 | [diff] [blame] | 582 | self.minor_version = self._CheckOptionalField(self.payload.manifest, | 
|  | 583 | 'minor_version', report) | 
|  | 584 | if self.minor_version in _SUPPORTED_MINOR_VERSIONS: | 
|  | 585 | if self.payload_type not in _SUPPORTED_MINOR_VERSIONS[self.minor_version]: | 
| Allie Wood | 7cf9f13 | 2015-02-26 14:28:19 -0800 | [diff] [blame] | 586 | raise error.PayloadError( | 
| Gilad Arnold | 0d575cd | 2015-07-13 17:29:21 -0700 | [diff] [blame] | 587 | 'Minor version %d not compatible with payload type %s.' % | 
|  | 588 | (self.minor_version, self.payload_type)) | 
|  | 589 | elif self.minor_version is None: | 
|  | 590 | raise error.PayloadError('Minor version is not set.') | 
| Allie Wood | 7cf9f13 | 2015-02-26 14:28:19 -0800 | [diff] [blame] | 591 | else: | 
| Gilad Arnold | 0d575cd | 2015-07-13 17:29:21 -0700 | [diff] [blame] | 592 | raise error.PayloadError('Unsupported minor version: %d' % | 
|  | 593 | self.minor_version) | 
| Allie Wood | 7cf9f13 | 2015-02-26 14:28:19 -0800 | [diff] [blame] | 594 |  | 
| Tudor Brindus | 8d05a7e | 2018-06-14 11:18:18 -0700 | [diff] [blame] | 595 | def _CheckManifest(self, report, part_sizes=None): | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 596 | """Checks the payload manifest. | 
|  | 597 |  | 
|  | 598 | Args: | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 599 | report: A report object to add to. | 
| Tudor Brindus | 8d05a7e | 2018-06-14 11:18:18 -0700 | [diff] [blame] | 600 | part_sizes: Map of partition label to partition size in bytes. | 
| Gilad Arnold | f583a7d | 2015-02-05 13:23:55 -0800 | [diff] [blame] | 601 |  | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 602 | Returns: | 
|  | 603 | A tuple consisting of the partition block size used during the update | 
|  | 604 | (integer), the signatures block offset and size. | 
| Gilad Arnold | f583a7d | 2015-02-05 13:23:55 -0800 | [diff] [blame] | 605 |  | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 606 | Raises: | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 607 | error.PayloadError if any of the checks fail. | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 608 | """ | 
| Tudor Brindus | 40506cd | 2018-06-18 20:18:17 -0700 | [diff] [blame] | 609 | self.major_version = self.payload.header.version | 
| Tudor Brindus | 8d05a7e | 2018-06-14 11:18:18 -0700 | [diff] [blame] | 610 |  | 
| Tudor Brindus | 40506cd | 2018-06-18 20:18:17 -0700 | [diff] [blame] | 611 | part_sizes = collections.defaultdict(int, part_sizes) | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 612 | manifest = self.payload.manifest | 
|  | 613 | report.AddSection('manifest') | 
|  | 614 |  | 
|  | 615 | # Check: block_size must exist and match the expected value. | 
|  | 616 | actual_block_size = self._CheckMandatoryField(manifest, 'block_size', | 
|  | 617 | report, 'manifest') | 
|  | 618 | if actual_block_size != self.block_size: | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 619 | raise error.PayloadError('Block_size (%d) not as expected (%d).' % | 
|  | 620 | (actual_block_size, self.block_size)) | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 621 |  | 
|  | 622 | # Check: signatures_offset <==> signatures_size. | 
|  | 623 | self.sigs_offset = self._CheckOptionalField(manifest, 'signatures_offset', | 
|  | 624 | report) | 
|  | 625 | self.sigs_size = self._CheckOptionalField(manifest, 'signatures_size', | 
|  | 626 | report) | 
|  | 627 | self._CheckPresentIff(self.sigs_offset, self.sigs_size, | 
|  | 628 | 'signatures_offset', 'signatures_size', 'manifest') | 
|  | 629 |  | 
| Tudor Brindus | 40506cd | 2018-06-18 20:18:17 -0700 | [diff] [blame] | 630 | if self.major_version == 1: | 
| Tudor Brindus | b220d66 | 2018-07-10 23:55:51 -0700 | [diff] [blame] | 631 | for real_name, proto_name in common.CROS_PARTITIONS: | 
|  | 632 | self.old_part_info[real_name] = self._CheckOptionalSubMsg( | 
|  | 633 | manifest, 'old_%s_info' % proto_name, report) | 
|  | 634 | self.new_part_info[real_name] = self._CheckMandatorySubMsg( | 
|  | 635 | manifest, 'new_%s_info' % proto_name, report, 'manifest') | 
| Tudor Brindus | 8d05a7e | 2018-06-14 11:18:18 -0700 | [diff] [blame] | 636 |  | 
| Tudor Brindus | 40506cd | 2018-06-18 20:18:17 -0700 | [diff] [blame] | 637 | # Check: old_kernel_info <==> old_rootfs_info. | 
|  | 638 | self._CheckPresentIff(self.old_part_info[common.KERNEL].msg, | 
|  | 639 | self.old_part_info[common.ROOTFS].msg, | 
|  | 640 | 'old_kernel_info', 'old_rootfs_info', 'manifest') | 
|  | 641 | else: | 
|  | 642 | for part in manifest.partitions: | 
|  | 643 | name = part.partition_name | 
|  | 644 | self.old_part_info[name] = self._CheckOptionalSubMsg( | 
|  | 645 | part, 'old_partition_info', report) | 
|  | 646 | self.new_part_info[name] = self._CheckMandatorySubMsg( | 
|  | 647 | part, 'new_partition_info', report, 'manifest.partitions') | 
| Tudor Brindus | 8d05a7e | 2018-06-14 11:18:18 -0700 | [diff] [blame] | 648 |  | 
| Tudor Brindus | 40506cd | 2018-06-18 20:18:17 -0700 | [diff] [blame] | 649 | # Check: Old-style partition infos should not be specified. | 
| Tudor Brindus | b220d66 | 2018-07-10 23:55:51 -0700 | [diff] [blame] | 650 | for _, part in common.CROS_PARTITIONS: | 
| Tudor Brindus | 40506cd | 2018-06-18 20:18:17 -0700 | [diff] [blame] | 651 | self._CheckElemNotPresent(manifest, 'old_%s_info' % part, 'manifest') | 
|  | 652 | self._CheckElemNotPresent(manifest, 'new_%s_info' % part, 'manifest') | 
|  | 653 |  | 
|  | 654 | # Check: If old_partition_info is specified anywhere, it must be | 
|  | 655 | # specified everywhere. | 
|  | 656 | old_part_msgs = [part.msg for part in self.old_part_info.values() if part] | 
|  | 657 | self._CheckPresentIffMany(old_part_msgs, 'old_partition_info', | 
|  | 658 | 'manifest.partitions') | 
|  | 659 |  | 
|  | 660 | is_delta = any(part and part.msg for part in self.old_part_info.values()) | 
|  | 661 | if is_delta: | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 662 | # Assert/mark delta payload. | 
|  | 663 | if self.payload_type == _TYPE_FULL: | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 664 | raise error.PayloadError( | 
|  | 665 | 'Apparent full payload contains old_{kernel,rootfs}_info.') | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 666 | self.payload_type = _TYPE_DELTA | 
|  | 667 |  | 
| Tudor Brindus | 40506cd | 2018-06-18 20:18:17 -0700 | [diff] [blame] | 668 | for part, (msg, part_report) in self.old_part_info.iteritems(): | 
| Tudor Brindus | 8d05a7e | 2018-06-14 11:18:18 -0700 | [diff] [blame] | 669 | # Check: {size, hash} present in old_{kernel,rootfs}_info. | 
|  | 670 | field = 'old_%s_info' % part | 
| Tudor Brindus | 8d05a7e | 2018-06-14 11:18:18 -0700 | [diff] [blame] | 671 | self.old_fs_sizes[part] = self._CheckMandatoryField(msg, 'size', | 
| Tudor Brindus | 40506cd | 2018-06-18 20:18:17 -0700 | [diff] [blame] | 672 | part_report, field) | 
|  | 673 | self._CheckMandatoryField(msg, 'hash', part_report, field, | 
| Tudor Brindus | 8d05a7e | 2018-06-14 11:18:18 -0700 | [diff] [blame] | 674 | convert=common.FormatSha256) | 
| Gilad Arnold | 382df5c | 2013-05-03 12:49:28 -0700 | [diff] [blame] | 675 |  | 
| Tudor Brindus | 8d05a7e | 2018-06-14 11:18:18 -0700 | [diff] [blame] | 676 | # Check: old_{kernel,rootfs} size must fit in respective partition. | 
|  | 677 | if self.old_fs_sizes[part] > part_sizes[part] > 0: | 
|  | 678 | raise error.PayloadError( | 
|  | 679 | 'Old %s content (%d) exceed partition size (%d).' % | 
|  | 680 | (part, self.old_fs_sizes[part], part_sizes[part])) | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 681 | else: | 
|  | 682 | # Assert/mark full payload. | 
|  | 683 | if self.payload_type == _TYPE_DELTA: | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 684 | raise error.PayloadError( | 
|  | 685 | 'Apparent delta payload missing old_{kernel,rootfs}_info.') | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 686 | self.payload_type = _TYPE_FULL | 
|  | 687 |  | 
| Tudor Brindus | 8d05a7e | 2018-06-14 11:18:18 -0700 | [diff] [blame] | 688 | # Check: new_{kernel,rootfs}_info present; contains {size, hash}. | 
| Tudor Brindus | 40506cd | 2018-06-18 20:18:17 -0700 | [diff] [blame] | 689 | for part, (msg, part_report) in self.new_part_info.iteritems(): | 
| Tudor Brindus | 8d05a7e | 2018-06-14 11:18:18 -0700 | [diff] [blame] | 690 | field = 'new_%s_info' % part | 
| Tudor Brindus | 40506cd | 2018-06-18 20:18:17 -0700 | [diff] [blame] | 691 | self.new_fs_sizes[part] = self._CheckMandatoryField(msg, 'size', | 
|  | 692 | part_report, field) | 
|  | 693 | self._CheckMandatoryField(msg, 'hash', part_report, field, | 
| Tudor Brindus | 8d05a7e | 2018-06-14 11:18:18 -0700 | [diff] [blame] | 694 | convert=common.FormatSha256) | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 695 |  | 
| Tudor Brindus | 8d05a7e | 2018-06-14 11:18:18 -0700 | [diff] [blame] | 696 | # Check: new_{kernel,rootfs} size must fit in respective partition. | 
|  | 697 | if self.new_fs_sizes[part] > part_sizes[part] > 0: | 
|  | 698 | raise error.PayloadError( | 
|  | 699 | 'New %s content (%d) exceed partition size (%d).' % | 
|  | 700 | (part, self.new_fs_sizes[part], part_sizes[part])) | 
| Gilad Arnold | 382df5c | 2013-05-03 12:49:28 -0700 | [diff] [blame] | 701 |  | 
| Allie Wood | f5c4f3e | 2015-02-20 16:57:46 -0800 | [diff] [blame] | 702 | # Check: minor_version makes sense for the payload type. This check should | 
|  | 703 | # run after the payload type has been set. | 
| Gilad Arnold | 0d575cd | 2015-07-13 17:29:21 -0700 | [diff] [blame] | 704 | self._CheckManifestMinorVersion(report) | 
| Allie Wood | f5c4f3e | 2015-02-20 16:57:46 -0800 | [diff] [blame] | 705 |  | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 706 | def _CheckLength(self, length, total_blocks, op_name, length_name): | 
|  | 707 | """Checks whether a length matches the space designated in extents. | 
|  | 708 |  | 
|  | 709 | Args: | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 710 | length: The total length of the data. | 
|  | 711 | total_blocks: The total number of blocks in extents. | 
|  | 712 | op_name: Operation name (for error reporting). | 
|  | 713 | length_name: Length name (for error reporting). | 
| Gilad Arnold | f583a7d | 2015-02-05 13:23:55 -0800 | [diff] [blame] | 714 |  | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 715 | Raises: | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 716 | error.PayloadError is there a problem with the length. | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 717 | """ | 
|  | 718 | # Check: length is non-zero. | 
|  | 719 | if length == 0: | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 720 | raise error.PayloadError('%s: %s is zero.' % (op_name, length_name)) | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 721 |  | 
|  | 722 | # Check that length matches number of blocks. | 
|  | 723 | self._CheckBlocksFitLength(length, total_blocks, self.block_size, | 
|  | 724 | '%s: %s' % (op_name, length_name)) | 
|  | 725 |  | 
| Gilad Arnold | 382df5c | 2013-05-03 12:49:28 -0700 | [diff] [blame] | 726 | def _CheckExtents(self, extents, usable_size, block_counters, name, | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 727 | allow_pseudo=False, allow_signature=False): | 
|  | 728 | """Checks a sequence of extents. | 
|  | 729 |  | 
|  | 730 | Args: | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 731 | extents: The sequence of extents to check. | 
|  | 732 | usable_size: The usable size of the partition to which the extents apply. | 
|  | 733 | block_counters: Array of counters corresponding to the number of blocks. | 
|  | 734 | name: The name of the extent block. | 
|  | 735 | allow_pseudo: Whether or not pseudo block numbers are allowed. | 
|  | 736 | allow_signature: Whether or not the extents are used for a signature. | 
| Gilad Arnold | f583a7d | 2015-02-05 13:23:55 -0800 | [diff] [blame] | 737 |  | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 738 | Returns: | 
|  | 739 | The total number of blocks in the extents. | 
| Gilad Arnold | f583a7d | 2015-02-05 13:23:55 -0800 | [diff] [blame] | 740 |  | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 741 | Raises: | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 742 | error.PayloadError if any of the entailed checks fails. | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 743 | """ | 
|  | 744 | total_num_blocks = 0 | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 745 | for ex, ex_name in common.ExtentIter(extents, name): | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 746 | # Check: Mandatory fields. | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 747 | start_block = PayloadChecker._CheckMandatoryField(ex, 'start_block', | 
|  | 748 | None, ex_name) | 
|  | 749 | num_blocks = PayloadChecker._CheckMandatoryField(ex, 'num_blocks', None, | 
|  | 750 | ex_name) | 
|  | 751 | end_block = start_block + num_blocks | 
|  | 752 |  | 
|  | 753 | # Check: num_blocks > 0. | 
|  | 754 | if num_blocks == 0: | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 755 | raise error.PayloadError('%s: extent length is zero.' % ex_name) | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 756 |  | 
|  | 757 | if start_block != common.PSEUDO_EXTENT_MARKER: | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 758 | # Check: Make sure we're within the partition limit. | 
| Gilad Arnold | 382df5c | 2013-05-03 12:49:28 -0700 | [diff] [blame] | 759 | if usable_size and end_block * self.block_size > usable_size: | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 760 | raise error.PayloadError( | 
|  | 761 | '%s: extent (%s) exceeds usable partition size (%d).' % | 
| Gilad Arnold | 382df5c | 2013-05-03 12:49:28 -0700 | [diff] [blame] | 762 | (ex_name, common.FormatExtent(ex, self.block_size), usable_size)) | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 763 |  | 
|  | 764 | # Record block usage. | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 765 | for i in xrange(start_block, end_block): | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 766 | block_counters[i] += 1 | 
| Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 767 | elif not (allow_pseudo or (allow_signature and len(extents) == 1)): | 
|  | 768 | # Pseudo-extents must be allowed explicitly, or otherwise be part of a | 
|  | 769 | # signature operation (in which case there has to be exactly one). | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 770 | raise error.PayloadError('%s: unexpected pseudo-extent.' % ex_name) | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 771 |  | 
|  | 772 | total_num_blocks += num_blocks | 
|  | 773 |  | 
|  | 774 | return total_num_blocks | 
|  | 775 |  | 
|  | 776 | def _CheckReplaceOperation(self, op, data_length, total_dst_blocks, op_name): | 
| Amin Hassani | 0de7f78 | 2017-12-07 12:13:03 -0800 | [diff] [blame] | 777 | """Specific checks for REPLACE/REPLACE_BZ/REPLACE_XZ operations. | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 778 |  | 
|  | 779 | Args: | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 780 | op: The operation object from the manifest. | 
|  | 781 | data_length: The length of the data blob associated with the operation. | 
|  | 782 | total_dst_blocks: Total number of blocks in dst_extents. | 
|  | 783 | op_name: Operation name for error reporting. | 
| Gilad Arnold | f583a7d | 2015-02-05 13:23:55 -0800 | [diff] [blame] | 784 |  | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 785 | Raises: | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 786 | error.PayloadError if any check fails. | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 787 | """ | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 788 | # Check: Does not contain src extents. | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 789 | if op.src_extents: | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 790 | raise error.PayloadError('%s: contains src_extents.' % op_name) | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 791 |  | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 792 | # Check: Contains data. | 
| Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 793 | if data_length is None: | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 794 | raise error.PayloadError('%s: missing data_{offset,length}.' % op_name) | 
| Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 795 |  | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 796 | if op.type == common.OpType.REPLACE: | 
|  | 797 | PayloadChecker._CheckBlocksFitLength(data_length, total_dst_blocks, | 
|  | 798 | self.block_size, | 
|  | 799 | op_name + '.data_length', 'dst') | 
|  | 800 | else: | 
| Sen Jiang | 771f648 | 2018-04-04 17:59:10 -0700 | [diff] [blame] | 801 | # Check: data_length must be smaller than the allotted dst blocks. | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 802 | if data_length >= total_dst_blocks * self.block_size: | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 803 | raise error.PayloadError( | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 804 | '%s: data_length (%d) must be less than allotted dst block ' | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 805 | 'space (%d * %d).' % | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 806 | (op_name, data_length, total_dst_blocks, self.block_size)) | 
|  | 807 |  | 
|  | 808 | def _CheckMoveOperation(self, op, data_offset, total_src_blocks, | 
|  | 809 | total_dst_blocks, op_name): | 
|  | 810 | """Specific checks for MOVE operations. | 
|  | 811 |  | 
|  | 812 | Args: | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 813 | op: The operation object from the manifest. | 
|  | 814 | data_offset: The offset of a data blob for the operation. | 
|  | 815 | total_src_blocks: Total number of blocks in src_extents. | 
|  | 816 | total_dst_blocks: Total number of blocks in dst_extents. | 
|  | 817 | op_name: Operation name for error reporting. | 
| Gilad Arnold | f583a7d | 2015-02-05 13:23:55 -0800 | [diff] [blame] | 818 |  | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 819 | Raises: | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 820 | error.PayloadError if any check fails. | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 821 | """ | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 822 | # Check: No data_{offset,length}. | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 823 | if data_offset is not None: | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 824 | raise error.PayloadError('%s: contains data_{offset,length}.' % op_name) | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 825 |  | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 826 | # Check: total_src_blocks == total_dst_blocks. | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 827 | if total_src_blocks != total_dst_blocks: | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 828 | raise error.PayloadError( | 
|  | 829 | '%s: total src blocks (%d) != total dst blocks (%d).' % | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 830 | (op_name, total_src_blocks, total_dst_blocks)) | 
|  | 831 |  | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 832 | # Check: For all i, i-th src block index != i-th dst block index. | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 833 | i = 0 | 
|  | 834 | src_extent_iter = iter(op.src_extents) | 
|  | 835 | dst_extent_iter = iter(op.dst_extents) | 
|  | 836 | src_extent = dst_extent = None | 
|  | 837 | src_idx = src_num = dst_idx = dst_num = 0 | 
|  | 838 | while i < total_src_blocks: | 
|  | 839 | # Get the next source extent, if needed. | 
|  | 840 | if not src_extent: | 
|  | 841 | try: | 
|  | 842 | src_extent = src_extent_iter.next() | 
|  | 843 | except StopIteration: | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 844 | raise error.PayloadError('%s: ran out of src extents (%d/%d).' % | 
|  | 845 | (op_name, i, total_src_blocks)) | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 846 | src_idx = src_extent.start_block | 
|  | 847 | src_num = src_extent.num_blocks | 
|  | 848 |  | 
|  | 849 | # Get the next dest extent, if needed. | 
|  | 850 | if not dst_extent: | 
|  | 851 | try: | 
|  | 852 | dst_extent = dst_extent_iter.next() | 
|  | 853 | except StopIteration: | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 854 | raise error.PayloadError('%s: ran out of dst extents (%d/%d).' % | 
|  | 855 | (op_name, i, total_dst_blocks)) | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 856 | dst_idx = dst_extent.start_block | 
|  | 857 | dst_num = dst_extent.num_blocks | 
|  | 858 |  | 
| Allie Wood | b065e13 | 2015-04-24 10:20:27 -0700 | [diff] [blame] | 859 | # Check: start block is not 0. See crbug/480751; there are still versions | 
|  | 860 | # of update_engine which fail when seeking to 0 in PReadAll and PWriteAll, | 
|  | 861 | # so we need to fail payloads that try to MOVE to/from block 0. | 
|  | 862 | if src_idx == 0 or dst_idx == 0: | 
|  | 863 | raise error.PayloadError( | 
|  | 864 | '%s: MOVE operation cannot have extent with start block 0' % | 
|  | 865 | op_name) | 
|  | 866 |  | 
| Gilad Arnold | eaed0d1 | 2013-04-30 15:38:22 -0700 | [diff] [blame] | 867 | if self.check_move_same_src_dst_block and src_idx == dst_idx: | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 868 | raise error.PayloadError( | 
|  | 869 | '%s: src/dst block number %d is the same (%d).' % | 
|  | 870 | (op_name, i, src_idx)) | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 871 |  | 
|  | 872 | advance = min(src_num, dst_num) | 
|  | 873 | i += advance | 
|  | 874 |  | 
|  | 875 | src_idx += advance | 
|  | 876 | src_num -= advance | 
|  | 877 | if src_num == 0: | 
|  | 878 | src_extent = None | 
|  | 879 |  | 
|  | 880 | dst_idx += advance | 
|  | 881 | dst_num -= advance | 
|  | 882 | if dst_num == 0: | 
|  | 883 | dst_extent = None | 
|  | 884 |  | 
| Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 885 | # Make sure we've exhausted all src/dst extents. | 
|  | 886 | if src_extent: | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 887 | raise error.PayloadError('%s: excess src blocks.' % op_name) | 
| Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 888 | if dst_extent: | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 889 | raise error.PayloadError('%s: excess dst blocks.' % op_name) | 
| Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 890 |  | 
| Amin Hassani | 8ad22ba | 2017-10-11 10:15:11 -0700 | [diff] [blame] | 891 | def _CheckZeroOperation(self, op, op_name): | 
|  | 892 | """Specific checks for ZERO operations. | 
|  | 893 |  | 
|  | 894 | Args: | 
|  | 895 | op: The operation object from the manifest. | 
|  | 896 | op_name: Operation name for error reporting. | 
|  | 897 |  | 
|  | 898 | Raises: | 
|  | 899 | error.PayloadError if any check fails. | 
|  | 900 | """ | 
|  | 901 | # Check: Does not contain src extents, data_length and data_offset. | 
|  | 902 | if op.src_extents: | 
|  | 903 | raise error.PayloadError('%s: contains src_extents.' % op_name) | 
|  | 904 | if op.data_length: | 
|  | 905 | raise error.PayloadError('%s: contains data_length.' % op_name) | 
|  | 906 | if op.data_offset: | 
|  | 907 | raise error.PayloadError('%s: contains data_offset.' % op_name) | 
|  | 908 |  | 
| Amin Hassani | efa62d9 | 2017-11-09 13:46:56 -0800 | [diff] [blame] | 909 | def _CheckAnyDiffOperation(self, op, data_length, total_dst_blocks, op_name): | 
|  | 910 | """Specific checks for BSDIFF, SOURCE_BSDIFF, PUFFDIFF and BROTLI_BSDIFF | 
|  | 911 | operations. | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 912 |  | 
|  | 913 | Args: | 
| Amin Hassani | efa62d9 | 2017-11-09 13:46:56 -0800 | [diff] [blame] | 914 | op: The operation. | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 915 | data_length: The length of the data blob associated with the operation. | 
|  | 916 | total_dst_blocks: Total number of blocks in dst_extents. | 
|  | 917 | op_name: Operation name for error reporting. | 
| Gilad Arnold | f583a7d | 2015-02-05 13:23:55 -0800 | [diff] [blame] | 918 |  | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 919 | Raises: | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 920 | error.PayloadError if any check fails. | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 921 | """ | 
| Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 922 | # Check: data_{offset,length} present. | 
|  | 923 | if data_length is None: | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 924 | raise error.PayloadError('%s: missing data_{offset,length}.' % op_name) | 
| Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 925 |  | 
| Sen Jiang | 771f648 | 2018-04-04 17:59:10 -0700 | [diff] [blame] | 926 | # Check: data_length is strictly smaller than the allotted dst blocks. | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 927 | if data_length >= total_dst_blocks * self.block_size: | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 928 | raise error.PayloadError( | 
| Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 929 | '%s: data_length (%d) must be smaller than allotted dst space ' | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 930 | '(%d * %d = %d).' % | 
| Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 931 | (op_name, data_length, total_dst_blocks, self.block_size, | 
|  | 932 | total_dst_blocks * self.block_size)) | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 933 |  | 
| Amin Hassani | efa62d9 | 2017-11-09 13:46:56 -0800 | [diff] [blame] | 934 | # Check the existence of src_length and dst_length for legacy bsdiffs. | 
|  | 935 | if (op.type == common.OpType.BSDIFF or | 
|  | 936 | (op.type == common.OpType.SOURCE_BSDIFF and self.minor_version <= 3)): | 
|  | 937 | if not op.HasField('src_length') or not op.HasField('dst_length'): | 
|  | 938 | raise error.PayloadError('%s: require {src,dst}_length.' % op_name) | 
|  | 939 | else: | 
|  | 940 | if op.HasField('src_length') or op.HasField('dst_length'): | 
|  | 941 | raise error.PayloadError('%s: unneeded {src,dst}_length.' % op_name) | 
|  | 942 |  | 
| Allie Wood | f5c4f3e | 2015-02-20 16:57:46 -0800 | [diff] [blame] | 943 | def _CheckSourceCopyOperation(self, data_offset, total_src_blocks, | 
|  | 944 | total_dst_blocks, op_name): | 
|  | 945 | """Specific checks for SOURCE_COPY. | 
|  | 946 |  | 
|  | 947 | Args: | 
| Allie Wood | f5c4f3e | 2015-02-20 16:57:46 -0800 | [diff] [blame] | 948 | data_offset: The offset of a data blob for the operation. | 
|  | 949 | total_src_blocks: Total number of blocks in src_extents. | 
|  | 950 | total_dst_blocks: Total number of blocks in dst_extents. | 
|  | 951 | op_name: Operation name for error reporting. | 
|  | 952 |  | 
|  | 953 | Raises: | 
|  | 954 | error.PayloadError if any check fails. | 
|  | 955 | """ | 
|  | 956 | # Check: No data_{offset,length}. | 
|  | 957 | if data_offset is not None: | 
|  | 958 | raise error.PayloadError('%s: contains data_{offset,length}.' % op_name) | 
|  | 959 |  | 
|  | 960 | # Check: total_src_blocks == total_dst_blocks. | 
|  | 961 | if total_src_blocks != total_dst_blocks: | 
|  | 962 | raise error.PayloadError( | 
|  | 963 | '%s: total src blocks (%d) != total dst blocks (%d).' % | 
|  | 964 | (op_name, total_src_blocks, total_dst_blocks)) | 
|  | 965 |  | 
| Sen Jiang | d6122bb | 2015-12-11 10:27:04 -0800 | [diff] [blame] | 966 | def _CheckAnySourceOperation(self, op, total_src_blocks, op_name): | 
| Sen Jiang | 912c4df | 2015-12-10 12:17:13 -0800 | [diff] [blame] | 967 | """Specific checks for SOURCE_* operations. | 
|  | 968 |  | 
|  | 969 | Args: | 
|  | 970 | op: The operation object from the manifest. | 
|  | 971 | total_src_blocks: Total number of blocks in src_extents. | 
|  | 972 | op_name: Operation name for error reporting. | 
|  | 973 |  | 
|  | 974 | Raises: | 
|  | 975 | error.PayloadError if any check fails. | 
|  | 976 | """ | 
|  | 977 | # Check: total_src_blocks != 0. | 
|  | 978 | if total_src_blocks == 0: | 
|  | 979 | raise error.PayloadError('%s: no src blocks in a source op.' % op_name) | 
|  | 980 |  | 
| Sen Jiang | 92161a7 | 2016-06-28 16:09:38 -0700 | [diff] [blame] | 981 | # Check: src_sha256_hash present in minor version >= 3. | 
|  | 982 | if self.minor_version >= 3 and op.src_sha256_hash is None: | 
| Sen Jiang | 912c4df | 2015-12-10 12:17:13 -0800 | [diff] [blame] | 983 | raise error.PayloadError('%s: source hash missing.' % op_name) | 
|  | 984 |  | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 985 | def _CheckOperation(self, op, op_name, is_last, old_block_counters, | 
| Gilad Arnold | 4f50b41 | 2013-05-14 09:19:17 -0700 | [diff] [blame] | 986 | new_block_counters, old_usable_size, new_usable_size, | 
| Gilad Arnold | eaed0d1 | 2013-04-30 15:38:22 -0700 | [diff] [blame] | 987 | prev_data_offset, allow_signature, blob_hash_counts): | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 988 | """Checks a single update operation. | 
|  | 989 |  | 
|  | 990 | Args: | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 991 | op: The operation object. | 
|  | 992 | op_name: Operation name string for error reporting. | 
|  | 993 | is_last: Whether this is the last operation in the sequence. | 
|  | 994 | old_block_counters: Arrays of block read counters. | 
|  | 995 | new_block_counters: Arrays of block write counters. | 
|  | 996 | old_usable_size: The overall usable size for src data in bytes. | 
|  | 997 | new_usable_size: The overall usable size for dst data in bytes. | 
|  | 998 | prev_data_offset: Offset of last used data bytes. | 
|  | 999 | allow_signature: Whether this may be a signature operation. | 
|  | 1000 | blob_hash_counts: Counters for hashed/unhashed blobs. | 
| Gilad Arnold | f583a7d | 2015-02-05 13:23:55 -0800 | [diff] [blame] | 1001 |  | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 1002 | Returns: | 
|  | 1003 | The amount of data blob associated with the operation. | 
| Gilad Arnold | f583a7d | 2015-02-05 13:23:55 -0800 | [diff] [blame] | 1004 |  | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 1005 | Raises: | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 1006 | error.PayloadError if any check has failed. | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 1007 | """ | 
|  | 1008 | # Check extents. | 
|  | 1009 | total_src_blocks = self._CheckExtents( | 
| Gilad Arnold | 4f50b41 | 2013-05-14 09:19:17 -0700 | [diff] [blame] | 1010 | op.src_extents, old_usable_size, old_block_counters, | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 1011 | op_name + '.src_extents', allow_pseudo=True) | 
|  | 1012 | allow_signature_in_extents = (allow_signature and is_last and | 
|  | 1013 | op.type == common.OpType.REPLACE) | 
|  | 1014 | total_dst_blocks = self._CheckExtents( | 
| Gilad Arnold | 382df5c | 2013-05-03 12:49:28 -0700 | [diff] [blame] | 1015 | op.dst_extents, new_usable_size, new_block_counters, | 
| Gilad Arnold | eaed0d1 | 2013-04-30 15:38:22 -0700 | [diff] [blame] | 1016 | op_name + '.dst_extents', | 
|  | 1017 | allow_pseudo=(not self.check_dst_pseudo_extents), | 
|  | 1018 | allow_signature=allow_signature_in_extents) | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 1019 |  | 
|  | 1020 | # Check: data_offset present <==> data_length present. | 
|  | 1021 | data_offset = self._CheckOptionalField(op, 'data_offset', None) | 
|  | 1022 | data_length = self._CheckOptionalField(op, 'data_length', None) | 
|  | 1023 | self._CheckPresentIff(data_offset, data_length, 'data_offset', | 
|  | 1024 | 'data_length', op_name) | 
|  | 1025 |  | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 1026 | # Check: At least one dst_extent. | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 1027 | if not op.dst_extents: | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 1028 | raise error.PayloadError('%s: dst_extents is empty.' % op_name) | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 1029 |  | 
|  | 1030 | # Check {src,dst}_length, if present. | 
|  | 1031 | if op.HasField('src_length'): | 
|  | 1032 | self._CheckLength(op.src_length, total_src_blocks, op_name, 'src_length') | 
|  | 1033 | if op.HasField('dst_length'): | 
|  | 1034 | self._CheckLength(op.dst_length, total_dst_blocks, op_name, 'dst_length') | 
|  | 1035 |  | 
|  | 1036 | if op.HasField('data_sha256_hash'): | 
|  | 1037 | blob_hash_counts['hashed'] += 1 | 
|  | 1038 |  | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 1039 | # Check: Operation carries data. | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 1040 | if data_offset is None: | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 1041 | raise error.PayloadError( | 
|  | 1042 | '%s: data_sha256_hash present but no data_{offset,length}.' % | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 1043 | op_name) | 
|  | 1044 |  | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 1045 | # Check: Hash verifies correctly. | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 1046 | actual_hash = hashlib.sha256(self.payload.ReadDataBlob(data_offset, | 
|  | 1047 | data_length)) | 
|  | 1048 | if op.data_sha256_hash != actual_hash.digest(): | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 1049 | raise error.PayloadError( | 
|  | 1050 | '%s: data_sha256_hash (%s) does not match actual hash (%s).' % | 
| Gilad Arnold | 9640537 | 2013-05-04 00:24:58 -0700 | [diff] [blame] | 1051 | (op_name, common.FormatSha256(op.data_sha256_hash), | 
|  | 1052 | common.FormatSha256(actual_hash.digest()))) | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 1053 | elif data_offset is not None: | 
|  | 1054 | if allow_signature_in_extents: | 
|  | 1055 | blob_hash_counts['signature'] += 1 | 
| Gilad Arnold | eaed0d1 | 2013-04-30 15:38:22 -0700 | [diff] [blame] | 1056 | elif self.allow_unhashed: | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 1057 | blob_hash_counts['unhashed'] += 1 | 
|  | 1058 | else: | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 1059 | raise error.PayloadError('%s: unhashed operation not allowed.' % | 
|  | 1060 | op_name) | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 1061 |  | 
|  | 1062 | if data_offset is not None: | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 1063 | # Check: Contiguous use of data section. | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 1064 | if data_offset != prev_data_offset: | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 1065 | raise error.PayloadError( | 
|  | 1066 | '%s: data offset (%d) not matching amount used so far (%d).' % | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 1067 | (op_name, data_offset, prev_data_offset)) | 
|  | 1068 |  | 
|  | 1069 | # Type-specific checks. | 
|  | 1070 | if op.type in (common.OpType.REPLACE, common.OpType.REPLACE_BZ): | 
|  | 1071 | self._CheckReplaceOperation(op, data_length, total_dst_blocks, op_name) | 
| Amin Hassani | 0de7f78 | 2017-12-07 12:13:03 -0800 | [diff] [blame] | 1072 | elif op.type == common.OpType.REPLACE_XZ and (self.minor_version >= 3 or | 
|  | 1073 | self.major_version >= 2): | 
|  | 1074 | self._CheckReplaceOperation(op, data_length, total_dst_blocks, op_name) | 
| Gilad Arnold | 0d575cd | 2015-07-13 17:29:21 -0700 | [diff] [blame] | 1075 | elif op.type == common.OpType.MOVE and self.minor_version == 1: | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 1076 | self._CheckMoveOperation(op, data_offset, total_src_blocks, | 
|  | 1077 | total_dst_blocks, op_name) | 
| Amin Hassani | 8ad22ba | 2017-10-11 10:15:11 -0700 | [diff] [blame] | 1078 | elif op.type == common.OpType.ZERO and self.minor_version >= 4: | 
|  | 1079 | self._CheckZeroOperation(op, op_name) | 
| Gilad Arnold | 0d575cd | 2015-07-13 17:29:21 -0700 | [diff] [blame] | 1080 | elif op.type == common.OpType.BSDIFF and self.minor_version == 1: | 
| Amin Hassani | efa62d9 | 2017-11-09 13:46:56 -0800 | [diff] [blame] | 1081 | self._CheckAnyDiffOperation(op, data_length, total_dst_blocks, op_name) | 
| Sen Jiang | 92161a7 | 2016-06-28 16:09:38 -0700 | [diff] [blame] | 1082 | elif op.type == common.OpType.SOURCE_COPY and self.minor_version >= 2: | 
| Allie Wood | f5c4f3e | 2015-02-20 16:57:46 -0800 | [diff] [blame] | 1083 | self._CheckSourceCopyOperation(data_offset, total_src_blocks, | 
|  | 1084 | total_dst_blocks, op_name) | 
| Sen Jiang | d6122bb | 2015-12-11 10:27:04 -0800 | [diff] [blame] | 1085 | self._CheckAnySourceOperation(op, total_src_blocks, op_name) | 
| Sen Jiang | 92161a7 | 2016-06-28 16:09:38 -0700 | [diff] [blame] | 1086 | elif op.type == common.OpType.SOURCE_BSDIFF and self.minor_version >= 2: | 
| Amin Hassani | efa62d9 | 2017-11-09 13:46:56 -0800 | [diff] [blame] | 1087 | self._CheckAnyDiffOperation(op, data_length, total_dst_blocks, op_name) | 
| Sen Jiang | 92161a7 | 2016-06-28 16:09:38 -0700 | [diff] [blame] | 1088 | self._CheckAnySourceOperation(op, total_src_blocks, op_name) | 
| Amin Hassani | 77d7cbc | 2018-02-07 16:21:33 -0800 | [diff] [blame] | 1089 | elif op.type == common.OpType.BROTLI_BSDIFF and self.minor_version >= 4: | 
|  | 1090 | self._CheckAnyDiffOperation(op, data_length, total_dst_blocks, op_name) | 
|  | 1091 | self._CheckAnySourceOperation(op, total_src_blocks, op_name) | 
|  | 1092 | elif op.type == common.OpType.PUFFDIFF and self.minor_version >= 5: | 
| Amin Hassani | efa62d9 | 2017-11-09 13:46:56 -0800 | [diff] [blame] | 1093 | self._CheckAnyDiffOperation(op, data_length, total_dst_blocks, op_name) | 
| Sen Jiang | d6122bb | 2015-12-11 10:27:04 -0800 | [diff] [blame] | 1094 | self._CheckAnySourceOperation(op, total_src_blocks, op_name) | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 1095 | else: | 
| Allie Wood | 7cf9f13 | 2015-02-26 14:28:19 -0800 | [diff] [blame] | 1096 | raise error.PayloadError( | 
|  | 1097 | 'Operation %s (type %d) not allowed in minor version %d' % | 
| Gilad Arnold | 0d575cd | 2015-07-13 17:29:21 -0700 | [diff] [blame] | 1098 | (op_name, op.type, self.minor_version)) | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 1099 | return data_length if data_length is not None else 0 | 
|  | 1100 |  | 
| Gilad Arnold | 382df5c | 2013-05-03 12:49:28 -0700 | [diff] [blame] | 1101 | def _SizeToNumBlocks(self, size): | 
|  | 1102 | """Returns the number of blocks needed to contain a given byte size.""" | 
|  | 1103 | return (size + self.block_size - 1) / self.block_size | 
|  | 1104 |  | 
|  | 1105 | def _AllocBlockCounters(self, total_size): | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 1106 | """Returns a freshly initialized array of block counters. | 
|  | 1107 |  | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 1108 | Note that the generated array is not portable as is due to byte-ordering | 
|  | 1109 | issues, hence it should not be serialized. | 
|  | 1110 |  | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 1111 | Args: | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 1112 | total_size: The total block size in bytes. | 
| Gilad Arnold | f583a7d | 2015-02-05 13:23:55 -0800 | [diff] [blame] | 1113 |  | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 1114 | Returns: | 
| Gilad Arnold | 9753f3d | 2013-07-23 08:34:45 -0700 | [diff] [blame] | 1115 | An array of unsigned short elements initialized to zero, one for each of | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 1116 | the blocks necessary for containing the partition. | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 1117 | """ | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 1118 | return array.array('H', | 
|  | 1119 | itertools.repeat(0, self._SizeToNumBlocks(total_size))) | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 1120 |  | 
| Gilad Arnold | 382df5c | 2013-05-03 12:49:28 -0700 | [diff] [blame] | 1121 | def _CheckOperations(self, operations, report, base_name, old_fs_size, | 
| Amin Hassani | ae85374 | 2017-10-11 10:27:27 -0700 | [diff] [blame] | 1122 | new_fs_size, old_usable_size, new_usable_size, | 
|  | 1123 | prev_data_offset, allow_signature): | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 1124 | """Checks a sequence of update operations. | 
|  | 1125 |  | 
|  | 1126 | Args: | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 1127 | operations: The sequence of operations to check. | 
|  | 1128 | report: The report object to add to. | 
|  | 1129 | base_name: The name of the operation block. | 
|  | 1130 | old_fs_size: The old filesystem size in bytes. | 
|  | 1131 | new_fs_size: The new filesystem size in bytes. | 
| Amin Hassani | ae85374 | 2017-10-11 10:27:27 -0700 | [diff] [blame] | 1132 | old_usable_size: The overall usable size of the old partition in bytes. | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 1133 | new_usable_size: The overall usable size of the new partition in bytes. | 
|  | 1134 | prev_data_offset: Offset of last used data bytes. | 
|  | 1135 | allow_signature: Whether this sequence may contain signature operations. | 
| Gilad Arnold | f583a7d | 2015-02-05 13:23:55 -0800 | [diff] [blame] | 1136 |  | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 1137 | Returns: | 
| Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 1138 | The total data blob size used. | 
| Gilad Arnold | f583a7d | 2015-02-05 13:23:55 -0800 | [diff] [blame] | 1139 |  | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 1140 | Raises: | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 1141 | error.PayloadError if any of the checks fails. | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 1142 | """ | 
|  | 1143 | # The total size of data blobs used by operations scanned thus far. | 
|  | 1144 | total_data_used = 0 | 
|  | 1145 | # Counts of specific operation types. | 
|  | 1146 | op_counts = { | 
|  | 1147 | common.OpType.REPLACE: 0, | 
|  | 1148 | common.OpType.REPLACE_BZ: 0, | 
| Amin Hassani | 0de7f78 | 2017-12-07 12:13:03 -0800 | [diff] [blame] | 1149 | common.OpType.REPLACE_XZ: 0, | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 1150 | common.OpType.MOVE: 0, | 
| Amin Hassani | 8ad22ba | 2017-10-11 10:15:11 -0700 | [diff] [blame] | 1151 | common.OpType.ZERO: 0, | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 1152 | common.OpType.BSDIFF: 0, | 
| Allie Wood | f5c4f3e | 2015-02-20 16:57:46 -0800 | [diff] [blame] | 1153 | common.OpType.SOURCE_COPY: 0, | 
|  | 1154 | common.OpType.SOURCE_BSDIFF: 0, | 
| Amin Hassani | 5ef5d45 | 2017-08-04 13:10:59 -0700 | [diff] [blame] | 1155 | common.OpType.PUFFDIFF: 0, | 
| Amin Hassani | efa62d9 | 2017-11-09 13:46:56 -0800 | [diff] [blame] | 1156 | common.OpType.BROTLI_BSDIFF: 0, | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 1157 | } | 
|  | 1158 | # Total blob sizes for each operation type. | 
|  | 1159 | op_blob_totals = { | 
|  | 1160 | common.OpType.REPLACE: 0, | 
|  | 1161 | common.OpType.REPLACE_BZ: 0, | 
| Amin Hassani | 0de7f78 | 2017-12-07 12:13:03 -0800 | [diff] [blame] | 1162 | common.OpType.REPLACE_XZ: 0, | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 1163 | # MOVE operations don't have blobs. | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 1164 | common.OpType.BSDIFF: 0, | 
| Allie Wood | f5c4f3e | 2015-02-20 16:57:46 -0800 | [diff] [blame] | 1165 | # SOURCE_COPY operations don't have blobs. | 
|  | 1166 | common.OpType.SOURCE_BSDIFF: 0, | 
| Amin Hassani | 5ef5d45 | 2017-08-04 13:10:59 -0700 | [diff] [blame] | 1167 | common.OpType.PUFFDIFF: 0, | 
| Amin Hassani | efa62d9 | 2017-11-09 13:46:56 -0800 | [diff] [blame] | 1168 | common.OpType.BROTLI_BSDIFF: 0, | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 1169 | } | 
|  | 1170 | # Counts of hashed vs unhashed operations. | 
|  | 1171 | blob_hash_counts = { | 
|  | 1172 | 'hashed': 0, | 
|  | 1173 | 'unhashed': 0, | 
|  | 1174 | } | 
|  | 1175 | if allow_signature: | 
|  | 1176 | blob_hash_counts['signature'] = 0 | 
|  | 1177 |  | 
|  | 1178 | # Allocate old and new block counters. | 
| Amin Hassani | ae85374 | 2017-10-11 10:27:27 -0700 | [diff] [blame] | 1179 | old_block_counters = (self._AllocBlockCounters(old_usable_size) | 
| Gilad Arnold | 382df5c | 2013-05-03 12:49:28 -0700 | [diff] [blame] | 1180 | if old_fs_size else None) | 
|  | 1181 | new_block_counters = self._AllocBlockCounters(new_usable_size) | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 1182 |  | 
|  | 1183 | # Process and verify each operation. | 
|  | 1184 | op_num = 0 | 
|  | 1185 | for op, op_name in common.OperationIter(operations, base_name): | 
|  | 1186 | op_num += 1 | 
|  | 1187 |  | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 1188 | # Check: Type is valid. | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 1189 | if op.type not in op_counts.keys(): | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 1190 | raise error.PayloadError('%s: invalid type (%d).' % (op_name, op.type)) | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 1191 | op_counts[op.type] += 1 | 
|  | 1192 |  | 
|  | 1193 | is_last = op_num == len(operations) | 
|  | 1194 | curr_data_used = self._CheckOperation( | 
|  | 1195 | op, op_name, is_last, old_block_counters, new_block_counters, | 
| Amin Hassani | ae85374 | 2017-10-11 10:27:27 -0700 | [diff] [blame] | 1196 | old_usable_size, new_usable_size, | 
| Gilad Arnold | 4f50b41 | 2013-05-14 09:19:17 -0700 | [diff] [blame] | 1197 | prev_data_offset + total_data_used, allow_signature, | 
|  | 1198 | blob_hash_counts) | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 1199 | if curr_data_used: | 
|  | 1200 | op_blob_totals[op.type] += curr_data_used | 
|  | 1201 | total_data_used += curr_data_used | 
|  | 1202 |  | 
|  | 1203 | # Report totals and breakdown statistics. | 
|  | 1204 | report.AddField('total operations', op_num) | 
|  | 1205 | report.AddField( | 
|  | 1206 | None, | 
|  | 1207 | histogram.Histogram.FromCountDict(op_counts, | 
|  | 1208 | key_names=common.OpType.NAMES), | 
|  | 1209 | indent=1) | 
|  | 1210 | report.AddField('total blobs', sum(blob_hash_counts.values())) | 
|  | 1211 | report.AddField(None, | 
|  | 1212 | histogram.Histogram.FromCountDict(blob_hash_counts), | 
|  | 1213 | indent=1) | 
|  | 1214 | report.AddField('total blob size', _AddHumanReadableSize(total_data_used)) | 
|  | 1215 | report.AddField( | 
|  | 1216 | None, | 
|  | 1217 | histogram.Histogram.FromCountDict(op_blob_totals, | 
|  | 1218 | formatter=_AddHumanReadableSize, | 
|  | 1219 | key_names=common.OpType.NAMES), | 
|  | 1220 | indent=1) | 
|  | 1221 |  | 
|  | 1222 | # Report read/write histograms. | 
|  | 1223 | if old_block_counters: | 
|  | 1224 | report.AddField('block read hist', | 
|  | 1225 | histogram.Histogram.FromKeyList(old_block_counters), | 
|  | 1226 | linebreak=True, indent=1) | 
|  | 1227 |  | 
| Gilad Arnold | 382df5c | 2013-05-03 12:49:28 -0700 | [diff] [blame] | 1228 | new_write_hist = histogram.Histogram.FromKeyList( | 
|  | 1229 | new_block_counters[:self._SizeToNumBlocks(new_fs_size)]) | 
|  | 1230 | report.AddField('block write hist', new_write_hist, linebreak=True, | 
|  | 1231 | indent=1) | 
|  | 1232 |  | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 1233 | # Check: Full update must write each dst block once. | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 1234 | if self.payload_type == _TYPE_FULL and new_write_hist.GetKeys() != [1]: | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 1235 | raise error.PayloadError( | 
|  | 1236 | '%s: not all blocks written exactly once during full update.' % | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 1237 | base_name) | 
|  | 1238 |  | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 1239 | return total_data_used | 
|  | 1240 |  | 
|  | 1241 | def _CheckSignatures(self, report, pubkey_file_name): | 
|  | 1242 | """Checks a payload's signature block.""" | 
|  | 1243 | sigs_raw = self.payload.ReadDataBlob(self.sigs_offset, self.sigs_size) | 
|  | 1244 | sigs = update_metadata_pb2.Signatures() | 
|  | 1245 | sigs.ParseFromString(sigs_raw) | 
|  | 1246 | report.AddSection('signatures') | 
|  | 1247 |  | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 1248 | # Check: At least one signature present. | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 1249 | if not sigs.signatures: | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 1250 | raise error.PayloadError('Signature block is empty.') | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 1251 |  | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 1252 | last_ops_section = (self.payload.manifest.kernel_install_operations or | 
|  | 1253 | self.payload.manifest.install_operations) | 
|  | 1254 | fake_sig_op = last_ops_section[-1] | 
| Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 1255 | # Check: signatures_{offset,size} must match the last (fake) operation. | 
|  | 1256 | if not (fake_sig_op.type == common.OpType.REPLACE and | 
|  | 1257 | self.sigs_offset == fake_sig_op.data_offset and | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 1258 | self.sigs_size == fake_sig_op.data_length): | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 1259 | raise error.PayloadError( | 
|  | 1260 | 'Signatures_{offset,size} (%d+%d) does not match last operation ' | 
|  | 1261 | '(%d+%d).' % | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 1262 | (self.sigs_offset, self.sigs_size, fake_sig_op.data_offset, | 
|  | 1263 | fake_sig_op.data_length)) | 
|  | 1264 |  | 
|  | 1265 | # Compute the checksum of all data up to signature blob. | 
|  | 1266 | # TODO(garnold) we're re-reading the whole data section into a string | 
|  | 1267 | # just to compute the checksum; instead, we could do it incrementally as | 
|  | 1268 | # we read the blobs one-by-one, under the assumption that we're reading | 
|  | 1269 | # them in order (which currently holds). This should be reconsidered. | 
|  | 1270 | payload_hasher = self.payload.manifest_hasher.copy() | 
|  | 1271 | common.Read(self.payload.payload_file, self.sigs_offset, | 
|  | 1272 | offset=self.payload.data_offset, hasher=payload_hasher) | 
|  | 1273 |  | 
|  | 1274 | for sig, sig_name in common.SignatureIter(sigs.signatures, 'signatures'): | 
|  | 1275 | sig_report = report.AddSubReport(sig_name) | 
|  | 1276 |  | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 1277 | # Check: Signature contains mandatory fields. | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 1278 | self._CheckMandatoryField(sig, 'version', sig_report, sig_name) | 
|  | 1279 | self._CheckMandatoryField(sig, 'data', None, sig_name) | 
|  | 1280 | sig_report.AddField('data len', len(sig.data)) | 
|  | 1281 |  | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 1282 | # Check: Signatures pertains to actual payload hash. | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 1283 | if sig.version == 1: | 
|  | 1284 | self._CheckSha256Signature(sig.data, pubkey_file_name, | 
|  | 1285 | payload_hasher.digest(), sig_name) | 
|  | 1286 | else: | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 1287 | raise error.PayloadError('Unknown signature version (%d).' % | 
|  | 1288 | sig.version) | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 1289 |  | 
| Amin Hassani | a86b108 | 2018-03-08 15:48:59 -0800 | [diff] [blame] | 1290 | def Run(self, pubkey_file_name=None, metadata_sig_file=None, metadata_size=0, | 
| Tudor Brindus | 2d22c1a | 2018-06-15 13:07:13 -0700 | [diff] [blame] | 1291 | part_sizes=None, report_out_file=None): | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 1292 | """Checker entry point, invoking all checks. | 
|  | 1293 |  | 
|  | 1294 | Args: | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 1295 | pubkey_file_name: Public key used for signature verification. | 
|  | 1296 | metadata_sig_file: Metadata signature, if verification is desired. | 
| Tudor Brindus | 2d22c1a | 2018-06-15 13:07:13 -0700 | [diff] [blame] | 1297 | metadata_size: Metadata size, if verification is desired. | 
|  | 1298 | part_sizes: Mapping of partition label to size in bytes (default: infer | 
|  | 1299 | based on payload type and version or filesystem). | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 1300 | report_out_file: File object to dump the report to. | 
| Gilad Arnold | f583a7d | 2015-02-05 13:23:55 -0800 | [diff] [blame] | 1301 |  | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 1302 | Raises: | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 1303 | error.PayloadError if payload verification failed. | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 1304 | """ | 
| Gilad Arnold | 9b90c93 | 2013-05-22 17:12:56 -0700 | [diff] [blame] | 1305 | if not pubkey_file_name: | 
|  | 1306 | pubkey_file_name = _DEFAULT_PUBKEY_FILE_NAME | 
|  | 1307 |  | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 1308 | report = _PayloadReport() | 
|  | 1309 |  | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 1310 | # Get payload file size. | 
|  | 1311 | self.payload.payload_file.seek(0, 2) | 
|  | 1312 | payload_file_size = self.payload.payload_file.tell() | 
|  | 1313 | self.payload.ResetFile() | 
|  | 1314 |  | 
|  | 1315 | try: | 
| Amin Hassani | a86b108 | 2018-03-08 15:48:59 -0800 | [diff] [blame] | 1316 | # Check metadata_size (if provided). | 
|  | 1317 | if metadata_size and self.payload.data_offset != metadata_size: | 
|  | 1318 | raise error.PayloadError('Invalid payload metadata size in payload(%d) ' | 
|  | 1319 | 'vs given(%d)' % (self.payload.data_offset, | 
|  | 1320 | metadata_size)) | 
|  | 1321 |  | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 1322 | # Check metadata signature (if provided). | 
|  | 1323 | if metadata_sig_file: | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 1324 | metadata_sig = base64.b64decode(metadata_sig_file.read()) | 
|  | 1325 | self._CheckSha256Signature(metadata_sig, pubkey_file_name, | 
|  | 1326 | self.payload.manifest_hasher.digest(), | 
|  | 1327 | 'metadata signature') | 
|  | 1328 |  | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 1329 | # Part 1: Check the file header. | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 1330 | report.AddSection('header') | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 1331 | # Check: Payload version is valid. | 
| Tudor Brindus | 40506cd | 2018-06-18 20:18:17 -0700 | [diff] [blame] | 1332 | if self.payload.header.version not in (1, 2): | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 1333 | raise error.PayloadError('Unknown payload version (%d).' % | 
|  | 1334 | self.payload.header.version) | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 1335 | report.AddField('version', self.payload.header.version) | 
|  | 1336 | report.AddField('manifest len', self.payload.header.manifest_len) | 
|  | 1337 |  | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 1338 | # Part 2: Check the manifest. | 
| Tudor Brindus | 2d22c1a | 2018-06-15 13:07:13 -0700 | [diff] [blame] | 1339 | self._CheckManifest(report, part_sizes) | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 1340 | assert self.payload_type, 'payload type should be known by now' | 
|  | 1341 |  | 
| Tudor Brindus | 40506cd | 2018-06-18 20:18:17 -0700 | [diff] [blame] | 1342 | manifest = self.payload.manifest | 
| Gilad Arnold | 06eea33 | 2015-07-13 18:06:33 -0700 | [diff] [blame] | 1343 |  | 
| Tudor Brindus | 40506cd | 2018-06-18 20:18:17 -0700 | [diff] [blame] | 1344 | # Part 3: Examine partition operations. | 
|  | 1345 | install_operations = [] | 
|  | 1346 | if self.major_version == 1: | 
|  | 1347 | # partitions field should not ever exist in major version 1 payloads | 
|  | 1348 | self._CheckRepeatedElemNotPresent(manifest, 'partitions', 'manifest') | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 1349 |  | 
| Tudor Brindus | 40506cd | 2018-06-18 20:18:17 -0700 | [diff] [blame] | 1350 | install_operations.append((common.ROOTFS, manifest.install_operations)) | 
|  | 1351 | install_operations.append((common.KERNEL, | 
|  | 1352 | manifest.kernel_install_operations)) | 
|  | 1353 |  | 
|  | 1354 | else: | 
|  | 1355 | self._CheckRepeatedElemNotPresent(manifest, 'install_operations', | 
|  | 1356 | 'manifest') | 
|  | 1357 | self._CheckRepeatedElemNotPresent(manifest, 'kernel_install_operations', | 
|  | 1358 | 'manifest') | 
|  | 1359 |  | 
|  | 1360 | for update in manifest.partitions: | 
|  | 1361 | install_operations.append((update.partition_name, update.operations)) | 
|  | 1362 |  | 
|  | 1363 | total_blob_size = 0 | 
|  | 1364 | for part, operations in install_operations: | 
|  | 1365 | report.AddSection('%s operations' % part) | 
|  | 1366 |  | 
|  | 1367 | new_fs_usable_size = self.new_fs_sizes[part] | 
|  | 1368 | old_fs_usable_size = self.old_fs_sizes[part] | 
|  | 1369 |  | 
|  | 1370 | if part_sizes.get(part, None): | 
|  | 1371 | new_fs_usable_size = old_fs_usable_size = part_sizes[part] | 
|  | 1372 | # Infer the usable partition size when validating rootfs operations: | 
|  | 1373 | # - If rootfs partition size was provided, use that. | 
|  | 1374 | # - Otherwise, if this is an older delta (minor version < 2), stick with | 
|  | 1375 | #   a known constant size. This is necessary because older deltas may | 
|  | 1376 | #   exceed the filesystem size when moving data blocks around. | 
|  | 1377 | # - Otherwise, use the encoded filesystem size. | 
|  | 1378 | elif self.payload_type == _TYPE_DELTA and part == common.ROOTFS and \ | 
|  | 1379 | self.minor_version in (None, 1): | 
|  | 1380 | new_fs_usable_size = old_fs_usable_size = _OLD_DELTA_USABLE_PART_SIZE | 
|  | 1381 |  | 
|  | 1382 | # TODO(garnold)(chromium:243559) only default to the filesystem size if | 
|  | 1383 | # no explicit size provided *and* the partition size is not embedded in | 
|  | 1384 | # the payload; see issue for more details. | 
|  | 1385 | total_blob_size += self._CheckOperations( | 
|  | 1386 | operations, report, '%s_install_operations' % part, | 
|  | 1387 | self.old_fs_sizes[part], self.new_fs_sizes[part], | 
|  | 1388 | old_fs_usable_size, new_fs_usable_size, total_blob_size, | 
|  | 1389 | self.major_version == 1 and part == common.KERNEL) | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 1390 |  | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 1391 | # Check: Operations data reach the end of the payload file. | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 1392 | used_payload_size = self.payload.data_offset + total_blob_size | 
|  | 1393 | if used_payload_size != payload_file_size: | 
| Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 1394 | raise error.PayloadError( | 
|  | 1395 | 'Used payload size (%d) different from actual file size (%d).' % | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 1396 | (used_payload_size, payload_file_size)) | 
|  | 1397 |  | 
| Tudor Brindus | 40506cd | 2018-06-18 20:18:17 -0700 | [diff] [blame] | 1398 | # Part 4: Handle payload signatures message. | 
| Gilad Arnold | eaed0d1 | 2013-04-30 15:38:22 -0700 | [diff] [blame] | 1399 | if self.check_payload_sig and self.sigs_size: | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 1400 | self._CheckSignatures(report, pubkey_file_name) | 
|  | 1401 |  | 
| Tudor Brindus | 40506cd | 2018-06-18 20:18:17 -0700 | [diff] [blame] | 1402 | # Part 5: Summary. | 
| Gilad Arnold | 553b0ec | 2013-01-26 01:00:39 -0800 | [diff] [blame] | 1403 | report.AddSection('summary') | 
|  | 1404 | report.AddField('update type', self.payload_type) | 
|  | 1405 |  | 
|  | 1406 | report.Finalize() | 
|  | 1407 | finally: | 
|  | 1408 | if report_out_file: | 
|  | 1409 | report.Dump(report_out_file) |