Reland "[Zucchini] (raw) Apply fuzzer"

This is a reland of f4a598ff5adfe27f8153bd36984ee9cb549f99e9

Windows cannot resolve #!/usr/bin/env python depending on how it is
configured. To fix this explicitly use python in the subprocess call.

Interestingly, the Tryjobs didn't catch this and only the official
build waterfall does...

Original change's description:
> [Zucchini] (raw) Apply fuzzer
>
> This is part of a series of Fuzzers to be added to Zucchini for
> security review. This tests the raw data patch application logic
> exercising the patch reader and apply process. It only covers ~20%
> of code in 100000 executions as the bulk of the remaining code is
> associated with the much more complex and expensive to fuzz reference
> related code.
>
> With the supplied seed corpus the fuzzer reaches approximately 11000
> execs/s.
>
> This found a couple bugs which are fixed in:
> https://chromium-review.googlesource.com/c/chromium/src/+/1028575
>
>
> Bug: 835341
> Change-Id: Idc1d862bfaa6eb6313f39e10536f4750c05ab863
> Reviewed-on: https://chromium-review.googlesource.com/1028570
> Commit-Queue: Calder Kitagawa <ckitagawa@google.com>
> Reviewed-by: Samuel Huang <huangs@chromium.org>
> Reviewed-by: Greg Thompson <grt@chromium.org>
> Reviewed-by: Max Moroz <mmoroz@chromium.org>
> Reviewed-by: Jonathan Metzman <metzman@chromium.org>
> Cr-Commit-Position: refs/heads/master@{#557185}

Bug: 835341
Change-Id: I24e94dd0c2035d84c84636f0a0a30756ae7f0c36
Reviewed-on: https://chromium-review.googlesource.com/1052567
Commit-Queue: Calder Kitagawa <ckitagawa@google.com>
Reviewed-by: Samuel Huang <huangs@chromium.org>
Cr-Commit-Position: refs/heads/master@{#557286}
NOKEYCHECK=True
GitOrigin-RevId: 8e7c08d3d11c61d08ad05d3ebc283aa2d6bf7c91
9 files changed
tree: b72c4b4eed92c531dd7e007311744432d19162c9
  1. fuzzers/
  2. testdata/
  3. abs32_utils.cc
  4. abs32_utils.h
  5. abs32_utils_unittest.cc
  6. address_translator.cc
  7. address_translator.h
  8. address_translator_unittest.cc
  9. algorithm.h
  10. algorithm_unittest.cc
  11. binary_data_histogram.cc
  12. binary_data_histogram.h
  13. binary_data_histogram_unittest.cc
  14. buffer_sink.cc
  15. buffer_sink.h
  16. buffer_sink_unittest.cc
  17. buffer_source.cc
  18. buffer_source.h
  19. buffer_source_unittest.cc
  20. buffer_view.h
  21. buffer_view_unittest.cc
  22. BUILD.gn
  23. crc32.cc
  24. crc32.h
  25. crc32_unittest.cc
  26. disassembler.cc
  27. disassembler.h
  28. disassembler_dex.cc
  29. disassembler_dex.h
  30. disassembler_dex_unittest.cc
  31. disassembler_no_op.cc
  32. disassembler_no_op.h
  33. disassembler_win32.cc
  34. disassembler_win32.h
  35. element_detection.cc
  36. element_detection.h
  37. element_detection_unittest.cc
  38. encoded_view.cc
  39. encoded_view.h
  40. encoded_view_unittest.cc
  41. ensemble_matcher.cc
  42. ensemble_matcher.h
  43. equivalence_map.cc
  44. equivalence_map.h
  45. equivalence_map_unittest.cc
  46. heuristic_ensemble_matcher.cc
  47. heuristic_ensemble_matcher.h
  48. image_index.cc
  49. image_index.h
  50. image_index_unittest.cc
  51. image_utils.h
  52. image_utils_unittest.cc
  53. imposed_ensemble_matcher.cc
  54. imposed_ensemble_matcher.h
  55. imposed_ensemble_matcher_unittest.cc
  56. integration_test.cc
  57. io_utils.cc
  58. io_utils.h
  59. io_utils_unittest.cc
  60. main_utils.cc
  61. main_utils.h
  62. mapped_file.cc
  63. mapped_file.h
  64. mapped_file_unittest.cc
  65. OWNERS
  66. patch_read_write_unittest.cc
  67. patch_reader.cc
  68. patch_reader.h
  69. patch_utils.h
  70. patch_utils_unittest.cc
  71. patch_writer.cc
  72. patch_writer.h
  73. README.md
  74. reference_bytes_mixer.cc
  75. reference_bytes_mixer.h
  76. reference_set.cc
  77. reference_set.h
  78. reference_set_unittest.cc
  79. rel32_finder.cc
  80. rel32_finder.h
  81. rel32_finder_unittest.cc
  82. rel32_utils.cc
  83. rel32_utils.h
  84. rel32_utils_unittest.cc
  85. reloc_utils.cc
  86. reloc_utils.h
  87. reloc_utils_unittest.cc
  88. suffix_array.h
  89. suffix_array_unittest.cc
  90. target_pool.cc
  91. target_pool.h
  92. target_pool_unittest.cc
  93. targets_affinity.cc
  94. targets_affinity.h
  95. targets_affinity_unittest.cc
  96. test_disassembler.cc
  97. test_disassembler.h
  98. test_reference_reader.cc
  99. test_reference_reader.h
  100. test_utils.cc
  101. test_utils.h
  102. type_dex.h
  103. type_win_pe.h
  104. typed_value.h
  105. typed_value_unittest.cc
  106. zucchini.h
  107. zucchini_apply.cc
  108. zucchini_apply.h
  109. zucchini_apply_unittest.cc
  110. zucchini_commands.cc
  111. zucchini_commands.h
  112. zucchini_exe_version.rc.version
  113. zucchini_gen.cc
  114. zucchini_gen.h
  115. zucchini_gen_unittest.cc
  116. zucchini_integration.cc
  117. zucchini_integration.h
  118. zucchini_main.cc
  119. zucchini_tools.cc
  120. zucchini_tools.h
README.md

Basic Definitions for Patching

Binary: Executable image and data. Binaries may persist in an archive (e.g., chrome.7z), and need to be periodically updated. Formats for binaries include {PE files EXE / DLL, ELF, DEX}. Architectures binaries include {x86, x64, ARM, AArch64, Dalvik}. A binary is also referred to as an executable or an image file.

Patching: Sending a "new" file to clients who have an "old" file by computing and transmitting a "patch" that can be used to transform "old" into "new". Patches are compressed for transmission. A key performance metric is patch size, which refers to the size of compressed patch file. For our experiments we use 7z.

Patch generation: Computation of a "patch" from "old" and "new". This can be expensive (e.g., ~15-20 min for Chrome, using 1 GB of RAM), but since patch generation is a run-once step on the server-side when releasing "new" binaries, the expense is not too critical.

Patch application: Transformation from "old" binaries to "new", using a (downloaded) "patch". This is executed on client side on updates, so resource constraints (e.g., time, RAM, disk space) is more stringent. Also, fault- tolerance is important. This is usually achieved by an update system by having a fallback method of directly downloading "new" in case of patching failure.

Offset: Position relative to the start of a file.

Local offset: An offset relative to the start of a region of a file.

Element: A region in a file with associated executable type, represented by the tuple (exe_type, offset, length). Every Element in new file is associated with an Element in old file and patched independently.

Reference: A directed connection between two offsets in a binary. For example, consider jump instructions in x86:

00401000: E9 3D 00 00 00     jmp         00401042

Here, the 4 bytes [3D 00 00 00] starting at address 00401001 point to address 00401042 in memory. This forms a reference from offset(00401001) (length 4) to offset(00401042), where offset(addr) indicates the disk offset corresponding to addr. A reference has a location, length (implicitly determined by reference type), body, and target.

Location: The starting offset of bytes that store a reference. In the preceding example, offset(00401001) is a location. Each location is the beginning of a reference body.

Body: The span of bytes that encodes reference data, i.e., [location, location + length) = [location, location + 1, ..., location + length - 1]. In the preceding example, length = 4, so the reference body is [00401001, 00401001 + 4) = [00401001, 00401002, 00401003, 00401004]. All reference bodies in an image must not overlap, and often regions boundaries are required to not straddle a reference body.

Target: The offset that's the destination of a reference. In the preceding example, offset(00401042) is the target. Different references can share common targets. For example, in

00401000: E9 3D 00 00 00     jmp         00401042
00401005: EB 3B              jmp         00401042

we have two references with different locations and bodies, but same target of 00401042.

Because the bytes that encode a reference depend on its target, and potentially on its location, they are more likely to get modified from an old version of a binary to a newer version. This is why "naive" patching does not do well on binaries.

Disassembler: Architecture specific data and operations, used to extract and correct references in a binary.

Type of reference: The type of a reference determines the binary representation used to encode its target. This affects how references are parsed and written by a disassembler. There can be many types of references in the same binary.

A reference is represented by the tuple (disassembler, location, target, type). This tuple contains sufficient information to write the reference in a binary.

Pool of targets: Collection of targets that is assumed to have some semantic relationship. Each reference type belong to exactly one reference pool. Targets for references in the same pool are shared.

For example, the following describes two pools defined for Dalvik Executable format (DEX). Both pools spawn multiple types of references.

  1. Index in string table.
  • From bytecode to string index using 16 bits.
  • From bytecode to string index using 32 bits.
  • From field item to string index using 32 bits.
  1. Address in code.
  • Relative 16 bits pointer.
  • Relative 32 bits pointer.

Boundaries between different pools can be ambiguous. Having all targets belong to the same pool can reduce redundancy, but will use more memory and might cause larger corrections to happen, so this is a trade-off that can be resolved with benchmarks.

Abs32 references: References whose targets are adjusted by the OS during program load. In an image, a relocation table typically provides locations of abs32 references. At each abs32 location, the stored bytes then encode semantic information about the target (e.g., as RVA).

Rel32 references: References embedded within machine code, in which targets are encoded as some delta relative to the reference's location. Typical examples of rel32 references are branching instructions and instruction pointer-relative memory access.

Equivalence: A (src_offset, dst_offset, length) tuple describing a region of "old" binary, at an offset of |src_offset|, that is similar to a region of "new" binary, at an offset of |dst_offset|.

Raw delta unit: Describes a raw modification to apply on the new image, as a pair (copy_offset, diff), where copy_offset describes the position in new file as an offset in the data that was copied from the old file, and diff is the bytewise difference to apply.

Associated Targets: A target in "old" binary is associated with a target in "new" binary if both targets:

  1. are part of similar regions from the same equivalence, and
  2. have the same local offset (relative to respective start regions), and
  3. are not part of any larger region from a different equivalence. Not all targets are necessarily associated with another target.

Label: An (offset, index) pair, where |offset| is a target, and |index| is an integer used to uniquely identify |offset| in its corresponding pool of targets. Labels are created for each Reference in "old" and "new" binary as part of generating a patch, and used to alias targets when searching for similar regions that will form equivalences. Labels are created such that associated targets in old and new binaries share the same |index|, and such that indices in a pool are tightly packed. For example, suppose "old" Labels are:

  • (0x1111, 0), (0x3333, 4), (0x5555, 1), (0x7777, 3) and given the following association of targets between "old" and "new":
  • 0x1111 <=> 0x6666, 0x3333 <=> 0x2222. then we could assign indices for "new" Labels as:
  • (0x2222, 4}, (0x4444, 8), (0x6666, 0), (0x8888, 2)

Encoded Image: The result of projecting the content of an image to scalar values that describe content on a higher level of abstraction, masking away undesirable noise in raw content. Notably, the projection encodes references based on their associated label.

Zucchini Ensemble Patch Format

Types

int8: 8-bit unsigned int.

uint32: 32-bit unsigned int, little-endian.

int32: 32-bit signed int, little-endian.

Varints: This is a generic variable-length encoding for integer quantities that strips away leading (most-significant) null bytes. The Varints format is borrowed from protocol-buffers, see documentation for more info.

varuint32: A uint32 encoded using Varints format.

varint32: A int32 encoded using Varints format.

File Layout

NameFormatDescription
headerPatchHeaderThe header.
elements_countuint32Number of patch units.
elementsPatchElement[elements_count]List of all patch elements.

Position of elements in new file is ascending.

Structures

PatchHeader

NameFormatDescription
magicuint32 = kMagicMagic value.
old_sizeuint32Size of old file in bytes.
old_crcuint32CRC32 of old file.
new_sizeuint32Size of new file in bytes.
new_crcuint32CRC32 of new file.

kMagic == 'Z' | ('u' << 8) | ('c' << 16)

PatchElement Contains all the information required to produce a single element in new file.

NameFormatDescription
headerPatchElementHeaderThe header.
equivalencesEquivalenceListList of equivalences.
raw_deltasRawDeltaListList of raw deltas.
reference_deltasReferenceDeltaListList of reference deltas.
pool_countuint32Number of pools.
extra_targetsExtraTargetList[pool_count]Lists of extra targets.

PatchElementHeader Describes a correspondence between an element in old and in new files. Some redundancy arise from storing |new_offset|, but it is necessary to make PatchElement self contained.

NameFormatDescription
old_offsetuint32Starting offset of the element in old file.
old_lengthuint32Length of the element in old file.
new_offsetuint32Starting offset of the element in new file.
new_lengthuint32Length of the element in new file.
exe_typeuint32Executable type for this unit, see enum ExecutableType.

EquivalenceList Encodes a list of equivalences, where dst offsets (in new image) are ascending.

NameFormatDescription
src_skipBufferSrc offset for each equivalence, delta encoded.
dst_skipBufferDst offset for each equivalence, delta encoded.
copy_countBufferLength for each equivalence.

RawDeltaList Encodes a list of raw delta units, with ascending copy offsets.

NameFormatDescription
raw_delta_skipBufferCopy offset for each delta unit, delta encoded and biased by -1.
raw_delta_diffBufferBytewise difference for each delta unit.

ReferenceDeltaList Encodes a list of reference deltas, in the order they appear in the new image file. A reference delta is a signed integer representing a jump through a list of targets.

NameFormatDescription
reference_deltaBufferVector of reference deltas.

ExtraTargetList Encodes a list of additional targets in the new image file, in ascending order.

NameFormatDescription
pool_taguint8_tUnique identifier for this pool of targets.
extra_targetsBufferAdditional targets, delta encoded and biased by -1.

Buffer A generic vector of data.

NameFormatDescription
sizeuint32Size of content in bytes.
contentT[]List of integers.