blob: 51243d1f1831df9b8a1d759e206e2e6053c8a852 [file] [log] [blame]
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001// Copyright (c) 2011 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_ENUMERATE_MODULES_MODEL_WIN_H_
6#define CHROME_BROWSER_ENUMERATE_MODULES_MODEL_WIN_H_
7
8#include <utility>
9#include <vector>
10
11#include "base/gtest_prod_util.h"
12#include "base/memory/ref_counted.h"
13#include "base/memory/singleton.h"
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010014#include "base/strings/string16.h"
Ben Murdocheb525c52013-07-10 11:40:50 +010015#include "base/timer/timer.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000016#include "content/public/browser/browser_thread.h"
Ben Murdocheb525c52013-07-10 11:40:50 +010017#include "url/gurl.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000018
19class EnumerateModulesModel;
Torne (Richard Coles)58218062012-11-14 11:43:16 +000020
21namespace base {
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000022class FilePath;
Torne (Richard Coles)58218062012-11-14 11:43:16 +000023class ListValue;
24}
25
26// A helper class that implements the enumerate module functionality on the File
27// thread.
28class ModuleEnumerator : public base::RefCountedThreadSafe<ModuleEnumerator> {
29 public:
30 // What type of module we are dealing with. Loaded modules are modules we
31 // detect as loaded in the process at the time of scanning. The others are
32 // modules of interest and may or may not be loaded in the process at the
33 // time of scan.
34 enum ModuleType {
35 LOADED_MODULE = 1 << 0,
36 SHELL_EXTENSION = 1 << 1,
37 WINSOCK_MODULE_REGISTRATION = 1 << 2,
38 };
39
40 // The blacklist status of the module. Suspected Bad modules have been
41 // partially matched (ie. name matches and location, but not description)
42 // whereas Confirmed Bad modules have been identified further (ie.
43 // AuthentiCode signer matches).
44 enum ModuleStatus {
45 // This is returned by the matching function when comparing against the
46 // blacklist and the module does not match the current entry in the
47 // blacklist.
48 NOT_MATCHED,
49 // The module is not on the blacklist. Assume it is good.
50 GOOD,
51 // Module is a suspected bad module.
52 SUSPECTED_BAD,
53 // Module is a bad bad dog.
54 CONFIRMED_BAD,
55 };
56
57 // A bitmask with the possible resolutions for bad modules.
58 enum RecommendedAction {
59 NONE = 0,
60 INVESTIGATING = 1 << 0,
61 UNINSTALL = 1 << 1,
62 DISABLE = 1 << 2,
63 UPDATE = 1 << 3,
64 SEE_LINK = 1 << 4,
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010065 NOTIFY_USER = 1 << 5,
66 };
67
68 // Which Windows OS is affected.
69 enum OperatingSystem {
70 ALL = -1,
71 XP = 1 << 0,
Torne (Richard Coles)58218062012-11-14 11:43:16 +000072 };
73
74 // The structure we populate when enumerating modules.
75 struct Module {
76 // The type of module found
77 ModuleType type;
78 // The module status (benign/bad/etc).
79 ModuleStatus status;
80 // The module path, not including filename.
81 string16 location;
82 // The name of the module (filename).
83 string16 name;
84 // The name of the product the module belongs to.
85 string16 product_name;
86 // The module file description.
87 string16 description;
88 // The module version.
89 string16 version;
90 // The signer of the digital certificate for the module.
91 string16 digital_signer;
92 // The help tips bitmask.
93 RecommendedAction recommended_action;
94 // The duplicate count within each category of modules.
95 int duplicate_count;
96 // Whether this module has been normalized (necessary before checking it
97 // against blacklist).
98 bool normalized;
99 };
100
101 // A vector typedef of all modules enumerated.
102 typedef std::vector<Module> ModulesVector;
103
104 // A structure we populate with the blacklist entries.
105 struct BlacklistEntry {
106 const char* filename;
107 const char* location;
108 const char* desc_or_signer;
109 const char* version_from; // Version where conflict started.
110 const char* version_to; // First version that works.
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100111 OperatingSystem os; // Bitmask, representing what OS this entry applies to.
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000112 RecommendedAction help_tip;
113 };
114
115 // A static function that normalizes the module information in the |module|
116 // struct. Module information needs to be normalized before comparing against
117 // the blacklist. This is because the same module can be described in many
118 // different ways, ie. file paths can be presented in long/short name form,
119 // and are not case sensitive on Windows. Also, the version string returned
120 // can include appended text, which we don't want to use during comparison
121 // against the blacklist.
122 static void NormalizeModule(Module* module);
123
124 // A static function that checks whether |module| has been |blacklisted|.
125 static ModuleStatus Match(const Module& module,
126 const BlacklistEntry& blacklisted);
127
128 explicit ModuleEnumerator(EnumerateModulesModel* observer);
129 ~ModuleEnumerator();
130
131 // Start scanning the loaded module list (if a scan is not already in
132 // progress). This function does not block while reading the module list
133 // (unless we are in limited_mode, see below), and will notify when done
134 // through the MODULE_LIST_ENUMERATED notification.
135 // The process will also send MODULE_INCOMPATIBILITY_BADGE_CHANGE to let
136 // observers know when it is time to update the wrench menu badge.
137 // When in |limited_mode|, this function will not leverage the File thread
138 // to run asynchronously and will therefore block until scanning is done
139 // (and will also not send out any notifications).
140 void ScanNow(ModulesVector* list, bool limited_mode);
141
142 private:
143 FRIEND_TEST_ALL_PREFIXES(EnumerateModulesTest, CollapsePath);
144
145 // The (currently) hard coded blacklist of known bad modules.
146 static const BlacklistEntry kModuleBlacklist[];
147
148 // This function does the actual file scanning work on the FILE thread (or
149 // block the main thread when in limited_mode). It enumerates all loaded
150 // modules in the process and other modules of interest, such as the
151 // registered Winsock LSP modules and stores them in |enumerated_modules_|.
152 // It then normalizes the module info and matches them against a blacklist
153 // of known bad modules. Finally, it calls ReportBack to let the observer
154 // know we are done.
155 void ScanImpl();
156
157 // Enumerate all modules loaded into the Chrome process.
158 void EnumerateLoadedModules();
159
160 // Enumerate all registered Windows shell extensions.
161 void EnumerateShellExtensions();
162
163 // Enumerate all registered Winsock LSP modules.
164 void EnumerateWinsockModules();
165
166 // Reads the registered shell extensions found under |parent| key in the
167 // registry.
168 void ReadShellExtensions(HKEY parent);
169
170 // Given a |module|, initializes the structure and loads additional
171 // information using the location field of the module.
172 void PopulateModuleInformation(Module* module);
173
174 // Checks the module list to see if a |module| of the same type, location
175 // and name has been added before and if so, increments its duplication
176 // counter. If it doesn't appear in the list, it is added.
177 void AddToListWithoutDuplicating(const Module&);
178
179 // Builds up a vector of path values mapping to environment variable,
180 // with pairs like [c:\windows\, %systemroot%]. This is later used to
181 // collapse paths like c:\windows\system32 into %systemroot%\system32, which
182 // we can use for comparison against our blacklist (which uses only env vars).
183 // NOTE: The vector will not contain an exhaustive list of environment
184 // variables, only the ones currently found on the blacklist or ones that are
185 // likely to appear there.
186 void PreparePathMappings();
187
188 // For a given |module|, collapse the path from c:\windows to %systemroot%,
189 // based on the |path_mapping_| vector.
190 void CollapsePath(Module* module);
191
192 // Takes each module in the |enumerated_modules_| vector and matches it
193 // against a fixed blacklist of bad and suspected bad modules.
194 void MatchAgainstBlacklist();
195
196 // This function executes on the UI thread when the scanning and matching
197 // process is done. It notifies the observer.
198 void ReportBack();
199
200 // Given a filename, returns the Subject (who signed it) retrieved from
201 // the digital signature (Authenticode).
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000202 string16 GetSubjectNameFromDigitalSignature(const base::FilePath& filename);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000203
204 // The typedef for the vector that maps a regular file path to %env_var%.
205 typedef std::vector< std::pair<string16, string16> > PathMapping;
206
207 // The vector of paths to %env_var%, used to account for differences in
208 // where people keep there files, c:\windows vs. d:\windows, etc.
209 PathMapping path_mapping_;
210
211 // The vector containing all the enumerated modules (loaded and modules of
212 // interest).
213 ModulesVector* enumerated_modules_;
214
215 // The observer, who needs to be notified when we are done.
216 EnumerateModulesModel* observer_;
217
218 // See limited_mode below.
219 bool limited_mode_;
220
221 // The thread that we need to call back on to report that we are done.
222 content::BrowserThread::ID callback_thread_id_;
223
224 DISALLOW_COPY_AND_ASSIGN(ModuleEnumerator);
225};
226
227// This is a singleton class that enumerates all modules loaded into Chrome,
228// both currently loaded modules (called DLLs on Windows) and modules 'of
229// interest', such as WinSock LSP modules. This class also marks each module
230// as benign or suspected bad or outright bad, using a supplied blacklist that
231// is currently hard-coded.
232//
233// To use this class, grab the singleton pointer and call ScanNow().
234// Then wait to get notified through MODULE_LIST_ENUMERATED when the list is
235// ready.
236//
237// This class can be used on the UI thread as it asynchronously offloads the
238// file work over to the FILE thread and reports back to the caller with a
239// notification.
240class EnumerateModulesModel {
241 public:
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100242 // UMA histogram constants.
243 enum UmaModuleConflictHistogramOptions {
244 ACTION_BUBBLE_SHOWN = 0,
245 ACTION_BUBBLE_LEARN_MORE,
246 ACTION_MENU_LEARN_MORE,
247 ACTION_BOUNDARY, // Must be the last value.
248 };
249
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000250 static EnumerateModulesModel* GetInstance();
251
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100252 // Record via UMA what the user selected.
253 static void RecordLearnMoreStat(bool from_menu);
254
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000255 // Returns true if we should show the conflict notification. The conflict
256 // notification is only shown once during the lifetime of the process.
257 bool ShouldShowConflictWarning() const;
258
259 // Called when the user has acknowledged the conflict notification.
260 void AcknowledgeConflictNotification();
261
262 // Returns the number of suspected bad modules found in the last scan.
263 // Returns 0 if no scan has taken place yet.
264 int suspected_bad_modules_detected() const {
265 return suspected_bad_modules_detected_;
266 }
267
268 // Returns the number of confirmed bad modules found in the last scan.
269 // Returns 0 if no scan has taken place yet.
270 int confirmed_bad_modules_detected() const {
271 return confirmed_bad_modules_detected_;
272 }
273
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100274 // Returns how many modules to notify the user about.
275 int modules_to_notify_about() const {
276 return modules_to_notify_about_;
277 }
278
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000279 // Set to true when we the scanning process can not rely on certain Chrome
280 // services to exists.
281 void set_limited_mode(bool limited_mode) {
282 limited_mode_ = limited_mode;
283 }
284
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100285 // Checks to see if a scanning task should be started and sets one off, if so.
286 void MaybePostScanningTask();
287
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000288 // Asynchronously start the scan for the loaded module list, except when in
289 // limited_mode (in which case it blocks).
290 void ScanNow();
291
292 // Gets the whole module list as a ListValue.
293 base::ListValue* GetModuleList() const;
294
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100295 // Gets the Help Center URL for the first *notable* conflict module that we've
296 // elected to notify the user about.
297 GURL GetFirstNotableConflict();
298
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000299 private:
300 friend struct DefaultSingletonTraits<EnumerateModulesModel>;
301 friend class ModuleEnumerator;
302
303 EnumerateModulesModel();
304 virtual ~EnumerateModulesModel();
305
306 // Called on the UI thread when the helper class is done scanning.
307 void DoneScanning();
308
309 // Constructs a Help Center article URL for help with a particular module.
310 // The module must have the SEE_LINK attribute for |recommended_action| set,
311 // otherwise this returns a blank string.
312 GURL ConstructHelpCenterUrl(const ModuleEnumerator::Module& module) const;
313
314 // The vector containing all the modules enumerated. Will be normalized and
315 // any bad modules will be marked.
316 ModuleEnumerator::ModulesVector enumerated_modules_;
317
318 // The object responsible for enumerating the modules on the File thread.
319 scoped_refptr<ModuleEnumerator> module_enumerator_;
320
321 // When this singleton object is constructed we go and fire off this timer to
322 // start scanning for modules after a certain amount of time has passed.
323 base::OneShotTimer<EnumerateModulesModel> check_modules_timer_;
324
325 // While normally |false|, this mode can be set to indicate that the scanning
326 // process should not rely on certain services normally available to Chrome,
327 // such as the resource bundle and the notification system, not to mention
328 // having multiple threads. This mode is useful during diagnostics, which
329 // runs without firing up all necessary Chrome services first.
330 bool limited_mode_;
331
332 // True if we are currently scanning for modules.
333 bool scanning_;
334
335 // Whether the conflict notification has been acknowledged by the user.
336 bool conflict_notification_acknowledged_;
337
338 // The number of confirmed bad modules (not including suspected bad ones)
339 // found during last scan.
340 int confirmed_bad_modules_detected_;
341
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100342 // The number of bad modules the user needs to be aggressively notified about.
343 int modules_to_notify_about_;
344
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000345 // The number of suspected bad modules (not including confirmed bad ones)
346 // found during last scan.
347 int suspected_bad_modules_detected_;
348
349 DISALLOW_COPY_AND_ASSIGN(EnumerateModulesModel);
350};
351
352#endif // CHROME_BROWSER_ENUMERATE_MODULES_MODEL_WIN_H_