| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_COMMON_SAFE_BROWSING_PE_IMAGE_READER_WIN_H_ | 5 #ifndef CHROME_COMMON_SAFE_BROWSING_PE_IMAGE_READER_WIN_H_ |
| 6 #define CHROME_COMMON_SAFE_BROWSING_PE_IMAGE_READER_WIN_H_ | 6 #define CHROME_COMMON_SAFE_BROWSING_PE_IMAGE_READER_WIN_H_ |
| 7 | 7 |
| 8 #include <windows.h> | 8 #include <windows.h> |
| 9 | 9 |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| 11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 12 | 12 |
| 13 namespace safe_browsing { | 13 namespace safe_browsing { |
| 14 | 14 |
| 15 // Parses headers and various data from a PE image. This parser is safe for use | 15 // Parses headers and various data from a PE image. This parser is safe for use |
| 16 // on untrusted data. | 16 // on untrusted data. |
| 17 class PeImageReader { | 17 class PeImageReader { |
| 18 public: | 18 public: |
| 19 enum WordSize { | 19 enum WordSize { |
| 20 WORD_SIZE_32, | 20 WORD_SIZE_32, |
| 21 WORD_SIZE_64, | 21 WORD_SIZE_64, |
| 22 }; | 22 }; |
| 23 | 23 |
| 24 // A callback invoked by EnumCertificates once for each attribute certificate |
| 25 // entry in the image's attribute certificate table. |revision| and |
| 26 // |certificate_type| identify the contents of |certificate_data| (which is of |
| 27 // |certificate_data_size| bytes). |context| is the value provided by the |
| 28 // caller to EnumCertificates(). Implementations must return true to continue |
| 29 // the enumeration, or false to abort. |
| 30 typedef bool (*EnumCertificatesCallback)(uint16_t revision, |
| 31 uint16_t certificate_type, |
| 32 const uint8_t* certificate_data, |
| 33 size_t certificate_data_size, |
| 34 void* context); |
| 35 |
| 24 PeImageReader(); | 36 PeImageReader(); |
| 25 ~PeImageReader(); | 37 ~PeImageReader(); |
| 26 | 38 |
| 27 // Returns false if the given data does not appear to be a valid PE image. | 39 // Returns false if the given data does not appear to be a valid PE image. |
| 28 bool Initialize(const uint8_t* image_data, size_t image_size); | 40 bool Initialize(const uint8_t* image_data, size_t image_size); |
| 29 | 41 |
| 30 // Returns the machine word size for the image. | 42 // Returns the machine word size for the image. |
| 31 WordSize GetWordSize(); | 43 WordSize GetWordSize(); |
| 32 | 44 |
| 33 const IMAGE_DOS_HEADER* GetDosHeader(); | 45 const IMAGE_DOS_HEADER* GetDosHeader(); |
| 34 const IMAGE_FILE_HEADER* GetCoffFileHeader(); | 46 const IMAGE_FILE_HEADER* GetCoffFileHeader(); |
| 35 | 47 |
| 36 // Returns a pointer to the optional header and its size. | 48 // Returns a pointer to the optional header and its size. |
| 37 const uint8_t* GetOptionalHeaderData(size_t* optional_data_size); | 49 const uint8_t* GetOptionalHeaderData(size_t* optional_data_size); |
| 38 size_t GetNumberOfSections(); | 50 size_t GetNumberOfSections(); |
| 39 const IMAGE_SECTION_HEADER* GetSectionHeaderAt(size_t index); | 51 const IMAGE_SECTION_HEADER* GetSectionHeaderAt(size_t index); |
| 40 | 52 |
| 41 // Returns a pointer to the image's export data (.edata) section and its size, | 53 // Returns a pointer to the image's export data (.edata) section and its size, |
| 42 // or NULL if the section is not present. | 54 // or NULL if the section is not present. |
| 43 const uint8_t* GetExportSection(size_t* section_size); | 55 const uint8_t* GetExportSection(size_t* section_size); |
| 44 | 56 |
| 45 size_t GetNumberOfDebugEntries(); | 57 size_t GetNumberOfDebugEntries(); |
| 46 const IMAGE_DEBUG_DIRECTORY* GetDebugEntry(size_t index, | 58 const IMAGE_DEBUG_DIRECTORY* GetDebugEntry(size_t index, |
| 47 const uint8_t** raw_data, | 59 const uint8_t** raw_data, |
| 48 size_t* raw_data_size); | 60 size_t* raw_data_size); |
| 49 | 61 |
| 62 // Invokes |callback| once per attribute certificate entry. |context| is a |
| 63 // caller-specific value that is passed to |callback|. Returns true if all |
| 64 // certificate entries are visited (even if there are no such entries) and |
| 65 // |callback| returns true for each. Conversely, returns |false| if |callback| |
| 66 // returns false or if the image is malformed in any way. |
| 67 bool EnumCertificates(EnumCertificatesCallback callback, |
| 68 void* context); |
| 69 |
| 50 private: | 70 private: |
| 51 // Bits indicating what portions of the image have been validated. | 71 // Bits indicating what portions of the image have been validated. |
| 52 enum ValidationStages { | 72 enum ValidationStages { |
| 53 VALID_DOS_HEADER = 1 << 0, | 73 VALID_DOS_HEADER = 1 << 0, |
| 54 VALID_PE_SIGNATURE = 1 << 1, | 74 VALID_PE_SIGNATURE = 1 << 1, |
| 55 VALID_COFF_FILE_HEADER = 1 << 2, | 75 VALID_COFF_FILE_HEADER = 1 << 2, |
| 56 VALID_OPTIONAL_HEADER = 1 << 3, | 76 VALID_OPTIONAL_HEADER = 1 << 3, |
| 57 VALID_SECTION_HEADERS = 1 << 4, | 77 VALID_SECTION_HEADERS = 1 << 4, |
| 58 }; | 78 }; |
| 59 | 79 |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 126 const uint8_t* image_data_; | 146 const uint8_t* image_data_; |
| 127 size_t image_size_; | 147 size_t image_size_; |
| 128 uint32_t validation_state_; | 148 uint32_t validation_state_; |
| 129 scoped_ptr<OptionalHeader> optional_header_; | 149 scoped_ptr<OptionalHeader> optional_header_; |
| 130 DISALLOW_COPY_AND_ASSIGN(PeImageReader); | 150 DISALLOW_COPY_AND_ASSIGN(PeImageReader); |
| 131 }; | 151 }; |
| 132 | 152 |
| 133 } // namespace safe_browsing | 153 } // namespace safe_browsing |
| 134 | 154 |
| 135 #endif // CHROME_COMMON_SAFE_BROWSING_PE_IMAGE_READER_WIN_H_ | 155 #endif // CHROME_COMMON_SAFE_BROWSING_PE_IMAGE_READER_WIN_H_ |
| OLD | NEW |