1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef CHROME_BROWSER_WIN_CONFLICTS_MODULE_INFO_H_
6 #define CHROME_BROWSER_WIN_CONFLICTS_MODULE_INFO_H_
7 
8 #include <string>
9 
10 #include "base/files/file_path.h"
11 #include "base/macros.h"
12 #include "base/optional.h"
13 #include "base/time/time.h"
14 #include "chrome/browser/win/conflicts/module_info_util.h"
15 #include "content/public/common/process_type.h"
16 
17 // ModuleInfoKey and ModuleInfoData are used in pair by the ModuleDatabase to
18 // maintain information about a module, usually in a std::map.
19 
20 // This is the constant portion of the module information, and is used to
21 // uniquely identify one.
22 struct ModuleInfoKey {
23   ModuleInfoKey(const base::FilePath& module_path,
24                 uint32_t module_size,
25                 uint32_t module_time_date_stamp);
26 
27   // Less-than operator allowing this object to be used in std::map.
28   bool operator<(const ModuleInfoKey& mi) const;
29 
30   // Full path to the module on disk. Part of the key for a ModuleInfo.
31   base::FilePath module_path;
32 
33   // The module size. Part of the key for a ModuleInfo. This is taken from
34   // SizeOfImage from the module's IMAGE_OPTIONAL_HEADER.
35   uint32_t module_size;
36 
37   // The module time date stamp. Part of the key for a ModuleInfo. Taken from
38   // TimeDateStamp from the module's IMAGE_FILE_HEADER.
39   uint32_t module_time_date_stamp;
40 };
41 
42 // Holds more detailed information about a given module. Because all of this
43 // information is expensive to gather and requires disk access, it should be
44 // collected via InspectModule() on a task runner that allow blocking.
45 //
46 // Note: Any modification to this structure should be reflected in
47 //       SerializeInspectionResult() and DeserializeInspectionResult() in
48 //       chrome/browser/win/conflicts/inspection_results_cache.cc.
49 struct ModuleInspectionResult {
50   ModuleInspectionResult();
51   ModuleInspectionResult(const ModuleInspectionResult& other);
52   ModuleInspectionResult(ModuleInspectionResult&& other);
53 
54   ModuleInspectionResult& operator=(const ModuleInspectionResult& other);
55   ModuleInspectionResult& operator=(ModuleInspectionResult&& other);
56 
57   ~ModuleInspectionResult();
58 
59   // The lowercase module path, not including the basename.
60   base::string16 location;
61 
62   // The basename of the module.
63   base::string16 basename;
64 
65   // The name of the product the module belongs to.
66   base::string16 product_name;
67 
68   // The module file description.
69   base::string16 description;
70 
71   // The module version. This is usually in the form a.b.c.d (where a, b, c and
72   // d are integers), but may also have fewer than 4 components.
73   base::string16 version;
74 
75   // The certificate info for the module.
76   CertificateInfo certificate_info;
77 };
78 
79 // Contains the inspection result of a module and additional information that is
80 // useful to the ModuleDatabase.
81 struct ModuleInfoData {
82   // Different properties that the module can have. Used as bit set values.
83   enum ModuleProperty : uint32_t {
84     // These modules are or were loaded into one of chrome's process at some
85     // point.
86     kPropertyLoadedModule = 1 << 0,
87     // These modules are registered as a shell extension.
88     kPropertyShellExtension = 1 << 1,
89     // These modules are registered as an Input Method Editor.
90     kPropertyIme = 1 << 2,
91     // The module was added to the module blacklist cache.
92     kPropertyAddedToBlacklist = 1 << 3,
93     // These modules were blocked from loading into the process.
94     kPropertyBlocked = 1 << 4,
95   };
96 
97   ModuleInfoData();
98   ~ModuleInfoData();
99 
100   ModuleInfoData(ModuleInfoData&& module_data) noexcept;
101 
102   // Set of all process types in which this module has been seen (may not be
103   // currently present in a process of that type). This is a conversion of
104   // ProcessType enumeration to a bitfield. See "ProcessTypeToBit" and
105   // "BitIndexToProcessType" for details.
106   uint32_t process_types;
107 
108   // Set that describes the properties of the module.
109   uint32_t module_properties;
110 
111   // The inspection result obtained via InspectModule().
112   base::Optional<ModuleInspectionResult> inspection_result;
113 };
114 
115 // Given a module located at |module_path|, returns a populated
116 // ModuleInspectionResult that contains detailed information about the module on
117 // disk. This is a blocking task that requires access to disk.
118 ModuleInspectionResult InspectModule(const base::FilePath& module_path);
119 
120 // Returns the date stamp to be used in the inspection results cache.
121 // Represents the number of hours between |time| and the Windows epoch
122 // (1601-01-01 00:00:00 UTC).
123 uint32_t CalculateTimeStamp(base::Time time);
124 
125 // Generate the code id of a module.
126 std::string GenerateCodeId(const ModuleInfoKey& module_key);
127 
128 // Converts a valid |process_type| to a bit for use in a bitmask of process
129 // values. Exposed in the header for testing.
130 uint32_t ProcessTypeToBit(content::ProcessType process_type);
131 
132 // Converts a |bit_index| (which maps to the bit 1 << bit_index) to the
133 // corresponding process type. Exposed in the header for testing.
134 content::ProcessType BitIndexToProcessType(uint32_t bit_index);
135 
136 // Returns true if |process_types| has at least one bit corresponding to a
137 // process type where the blocking of third-party modules is enabled.
138 bool IsBlockingEnabledInProcessTypes(uint32_t process_types);
139 
140 namespace internal {
141 
142 // Normalizes the information already contained in |inspection_result|. In
143 // particular:
144 // - The path is split in 2 parts: The basename and the location.
145 // - If it uses commas, the version string is modified to use periods.
146 // - If there is one, the version string suffix is removed.
147 //
148 // Exposed for testing.
149 void NormalizeInspectionResult(ModuleInspectionResult* inspection_result);
150 
151 }  // namespace internal
152 
153 #endif  // CHROME_BROWSER_WIN_CONFLICTS_MODULE_INFO_H_
154