1 // Copyright 2013 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.
5 #ifndef COMPONENTS_NACL_BROWSER_NACL_BROWSER_H_
6 #define COMPONENTS_NACL_BROWSER_NACL_BROWSER_H_
10 #include "base/bind.h"
11 #include "base/containers/mru_cache.h"
12 #include "base/files/file.h"
13 #include "base/memory/singleton.h"
14 #include "base/memory/weak_ptr.h"
15 #include "base/time/time.h"
16 #include "components/nacl/browser/nacl_browser_delegate.h"
17 #include "components/nacl/browser/nacl_validation_cache.h"
28 // Open an immutable executable file that can be mmapped.
29 // This function should only be called on a thread that can perform file IO.
30 base::File OpenNaClExecutableImpl(const base::FilePath& file_path);
32 // Represents shared state for all NaClProcessHost objects in the browser.
35 static NaClBrowser* GetInstance();
37 // Will it be possible to launch a NaCl process, eventually?
40 // Are we ready to launch a NaCl process now? Implies IsOk().
43 // Attempt to asynchronously acquire all resources needed to start a process.
44 // This method is idempotent - it is safe to call multiple times.
45 void EnsureAllResourcesAvailable();
47 // Enqueues reply() in the message loop when all the resources needed to start
48 // a process have been acquired.
49 void WaitForResources(const base::Closure& reply);
51 // Asynchronously attempt to get the IRT open.
52 // This is entailed by EnsureInitialized. This method is exposed as part of
53 // the public interface, however, so the IRT can be explicitly opened as
54 // early as possible to prevent autoupdate issues.
55 void EnsureIrtAvailable();
57 // Path to IRT. Available even before IRT is loaded.
58 const base::FilePath& GetIrtFilePath();
60 // IRT file handle, only available when IsReady().
61 const base::File& IrtFile() const;
63 // Methods for testing GDB debug stub in browser. If test adds debug stub
64 // port listener, Chrome will allocate a currently-unused TCP port number for
65 // debug stub server instead of a fixed one.
67 // Notify listener that new debug stub TCP port is allocated.
68 void FireGdbDebugStubPortOpened(int port);
69 bool HasGdbDebugStubPortListener();
70 void SetGdbDebugStubPortListener(base::Callback<void(int)> listener);
71 void ClearGdbDebugStubPortListener();
73 bool ValidationCacheIsEnabled() const {
74 return validation_cache_is_enabled_;
77 const std::string& GetValidationCacheKey() const {
78 return validation_cache_.GetValidationCacheKey();
81 // The NaCl singleton keeps information about NaCl executable files opened via
82 // PPAPI. This allows the NaCl process to get trusted information about the
83 // file directly from the browser process. In theory, a compromised renderer
84 // could provide a writable file handle or lie about the file's path. If we
85 // trusted the handle was read only but it was not, an mmapped file could be
86 // modified after validation, allowing an escape from the NaCl sandbox.
87 // Similarly, if we trusted the file path corresponded to the file handle but
88 // it did not, the validation cache could be tricked into bypassing validation
90 // Instead of allowing these attacks, the NaCl process only trusts information
91 // it gets directly from the browser process. Because the information is
92 // stored in a cache of bounded size, it is not guaranteed the browser process
93 // will be able to provide the requested information. In these cases, the
94 // NaCl process must make conservative assumptions about the origin of the
96 // In theory, a compromised renderer could guess file tokens in an attempt to
97 // read files it normally doesn't have access to. This would not compromise
98 // the NaCl sandbox, however, and only has a 1 in ~2**120 chance of success
100 // TODO(ncbray): move the cache onto NaClProcessHost so that we don't need to
101 // rely on tokens being unguessable by another process.
102 void PutFilePath(const base::FilePath& path, uint64* file_token_lo,
103 uint64* file_token_hi);
104 bool GetFilePath(uint64 file_token_lo, uint64 file_token_hi,
105 base::FilePath* path);
107 bool QueryKnownToValidate(const std::string& signature, bool off_the_record);
108 void SetKnownToValidate(const std::string& signature, bool off_the_record);
109 void ClearValidationCache(const base::Closure& callback);
111 // Get path to NaCl loader on the filesystem if possible.
112 // |exe_path| does not change if the method fails.
113 bool GetNaCl64ExePath(base::FilePath* exe_path);
117 static void SetDelegate(NaClBrowserDelegate* delegate);
118 static NaClBrowserDelegate* GetDelegate();
120 // Support for NaCl crash throttling.
121 // Each time a NaCl module crashes, the browser is notified.
122 void OnProcessCrashed();
123 // If "too many" crashes occur within a given time period, NaCl is throttled
124 // until the rate again drops below the threshold.
128 friend struct DefaultSingletonTraits<NaClBrowser>;
130 enum NaClResourceState {
131 NaClResourceUninitialized,
132 NaClResourceRequested,
139 void InitIrtFilePath();
141 void OpenIrtLibraryFile();
143 void OnIrtOpened(scoped_ptr<base::FileProxy> file_proxy,
144 base::File::Error error_code);
146 void InitValidationCacheFilePath();
147 void EnsureValidationCacheAvailable();
148 void OnValidationCacheLoaded(const std::string* data);
149 void RunWithoutValidationCache();
151 // Dispatch waiting tasks if we are ready, or if we know we'll never be ready.
154 // Indicate that it is impossible to launch a NaCl process.
157 void MarkValidationCacheAsModified();
158 void PersistValidationCache();
160 // Singletons get destroyed at shutdown.
161 base::WeakPtrFactory<NaClBrowser> weak_factory_;
163 base::File irt_file_;
164 base::FilePath irt_filepath_;
165 NaClResourceState irt_state_;
166 NaClValidationCache validation_cache_;
167 NaClValidationCache off_the_record_validation_cache_;
168 base::FilePath validation_cache_file_path_;
169 bool validation_cache_is_enabled_;
170 bool validation_cache_is_modified_;
171 NaClResourceState validation_cache_state_;
172 base::Callback<void(int)> debug_stub_port_listener_;
174 typedef base::HashingMRUCache<std::string, base::FilePath> PathCacheType;
175 PathCacheType path_cache_;
179 // A list of pending tasks to start NaCl processes.
180 std::vector<base::Closure> waiting_;
182 scoped_ptr<NaClBrowserDelegate> browser_delegate_;
184 std::deque<base::Time> crash_times_;
186 DISALLOW_COPY_AND_ASSIGN(NaClBrowser);
191 #endif // COMPONENTS_NACL_BROWSER_NACL_BROWSER_H_