1 // Copyright (c) 2012 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_EXTENSIONS_SANDBOXED_UNPACKER_H_ 6 #define CHROME_BROWSER_EXTENSIONS_SANDBOXED_UNPACKER_H_ 7 8 #include <string> 9 10 #include "base/files/file_path.h" 11 #include "base/files/scoped_temp_dir.h" 12 #include "base/memory/ref_counted.h" 13 #include "base/time/time.h" 14 #include "content/public/browser/utility_process_host_client.h" 15 #include "extensions/common/manifest.h" 16 17 class SkBitmap; 18 19 namespace base { 20 class DictionaryValue; 21 class SequencedTaskRunner; 22 } 23 24 namespace extensions { 25 class Extension; 26 27 class SandboxedUnpackerClient 28 : public base::RefCountedThreadSafe<SandboxedUnpackerClient> { 29 public: 30 // temp_dir - A temporary directory containing the results of the extension 31 // unpacking. The client is responsible for deleting this directory. 32 // 33 // extension_root - The path to the extension root inside of temp_dir. 34 // 35 // original_manifest - The parsed but unmodified version of the manifest, 36 // with no modifications such as localization, etc. 37 // 38 // extension - The extension that was unpacked. The client is responsible 39 // for deleting this memory. 40 // 41 // install_icon - The icon we will display in the installation UI, if any. 42 virtual void OnUnpackSuccess(const base::FilePath& temp_dir, 43 const base::FilePath& extension_root, 44 const base::DictionaryValue* original_manifest, 45 const Extension* extension, 46 const SkBitmap& install_icon) = 0; 47 virtual void OnUnpackFailure(const base::string16& error) = 0; 48 49 protected: 50 friend class base::RefCountedThreadSafe<SandboxedUnpackerClient>; 51 ~SandboxedUnpackerClient()52 virtual ~SandboxedUnpackerClient() {} 53 }; 54 55 // SandboxedUnpacker unpacks extensions from the CRX format into a 56 // directory. This is done in a sandboxed subprocess to protect the browser 57 // process from parsing complex formats like JPEG or JSON from untrusted 58 // sources. 59 // 60 // Unpacking an extension using this class makes minor changes to its source, 61 // such as transcoding all images to PNG, parsing all message catalogs 62 // and rewriting the manifest JSON. As such, it should not be used when the 63 // output is not intended to be given back to the author. 64 // 65 // 66 // Lifetime management: 67 // 68 // This class is ref-counted by each call it makes to itself on another thread, 69 // and by UtilityProcessHost. 70 // 71 // Additionally, we hold a reference to our own client so that it lives at least 72 // long enough to receive the result of unpacking. 73 // 74 // 75 // NOTE: This class should only be used on the file thread. 76 class SandboxedUnpacker : public content::UtilityProcessHostClient { 77 public: 78 // Unpacks the extension in |crx_path| into a temporary directory and calls 79 // |client| with the result. If |run_out_of_process| is provided, unpacking 80 // is done in a sandboxed subprocess. Otherwise, it is done in-process. 81 SandboxedUnpacker(const base::FilePath& crx_path, 82 Manifest::Location location, 83 int creation_flags, 84 const base::FilePath& extensions_dir, 85 base::SequencedTaskRunner* unpacker_io_task_runner, 86 SandboxedUnpackerClient* client); 87 88 // Start unpacking the extension. The client is called with the results. 89 void Start(); 90 91 private: 92 class ProcessHostClient; 93 94 // Enumerate all the ways unpacking can fail. Calls to ReportFailure() 95 // take a failure reason as an argument, and put it in histogram 96 // Extensions.SandboxUnpackFailureReason. 97 enum FailureReason { 98 // SandboxedUnpacker::CreateTempDirectory() 99 COULD_NOT_GET_TEMP_DIRECTORY, 100 COULD_NOT_CREATE_TEMP_DIRECTORY, 101 102 // SandboxedUnpacker::Start() 103 FAILED_TO_COPY_EXTENSION_FILE_TO_TEMP_DIRECTORY, 104 COULD_NOT_GET_SANDBOX_FRIENDLY_PATH, 105 106 // SandboxedUnpacker::OnUnpackExtensionSucceeded() 107 COULD_NOT_LOCALIZE_EXTENSION, 108 INVALID_MANIFEST, 109 110 // SandboxedUnpacker::OnUnpackExtensionFailed() 111 UNPACKER_CLIENT_FAILED, 112 113 // SandboxedUnpacker::OnProcessCrashed() 114 UTILITY_PROCESS_CRASHED_WHILE_TRYING_TO_INSTALL, 115 116 // SandboxedUnpacker::ValidateSignature() 117 CRX_FILE_NOT_READABLE, 118 CRX_HEADER_INVALID, 119 CRX_MAGIC_NUMBER_INVALID, 120 CRX_VERSION_NUMBER_INVALID, 121 CRX_EXCESSIVELY_LARGE_KEY_OR_SIGNATURE, 122 CRX_ZERO_KEY_LENGTH, 123 CRX_ZERO_SIGNATURE_LENGTH, 124 CRX_PUBLIC_KEY_INVALID, 125 CRX_SIGNATURE_INVALID, 126 CRX_SIGNATURE_VERIFICATION_INITIALIZATION_FAILED, 127 CRX_SIGNATURE_VERIFICATION_FAILED, 128 129 // SandboxedUnpacker::RewriteManifestFile() 130 ERROR_SERIALIZING_MANIFEST_JSON, 131 ERROR_SAVING_MANIFEST_JSON, 132 133 // SandboxedUnpacker::RewriteImageFiles() 134 COULD_NOT_READ_IMAGE_DATA_FROM_DISK, 135 DECODED_IMAGES_DO_NOT_MATCH_THE_MANIFEST, 136 INVALID_PATH_FOR_BROWSER_IMAGE, 137 ERROR_REMOVING_OLD_IMAGE_FILE, 138 INVALID_PATH_FOR_BITMAP_IMAGE, 139 ERROR_RE_ENCODING_THEME_IMAGE, 140 ERROR_SAVING_THEME_IMAGE, 141 ABORTED_DUE_TO_SHUTDOWN, 142 143 // SandboxedUnpacker::RewriteCatalogFiles() 144 COULD_NOT_READ_CATALOG_DATA_FROM_DISK, 145 INVALID_CATALOG_DATA, 146 INVALID_PATH_FOR_CATALOG, 147 ERROR_SERIALIZING_CATALOG, 148 ERROR_SAVING_CATALOG, 149 150 NUM_FAILURE_REASONS 151 }; 152 153 friend class ProcessHostClient; 154 friend class SandboxedUnpackerTest; 155 156 virtual ~SandboxedUnpacker(); 157 158 // Set |temp_dir_| as a temporary directory to unpack the extension in. 159 // Return true on success. 160 virtual bool CreateTempDirectory(); 161 162 // Validates the signature of the extension and extract the key to 163 // |public_key_|. Returns true if the signature validates, false otherwise. 164 // 165 // NOTE: Having this method here is a bit ugly. This code should really live 166 // in extensions::Unpacker as it is not specific to sandboxed unpacking. It 167 // was put here because we cannot run windows crypto code in the sandbox. But 168 // we could still have this method statically on extensions::Unpacker so that 169 // code just for unpacking is there and code just for sandboxing of unpacking 170 // is here. 171 bool ValidateSignature(); 172 173 // Starts the utility process that unpacks our extension. 174 void StartProcessOnIOThread(const base::FilePath& temp_crx_path); 175 176 // UtilityProcessHostClient 177 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; 178 virtual void OnProcessCrashed(int exit_code) OVERRIDE; 179 180 // IPC message handlers. 181 void OnUnpackExtensionSucceeded(const base::DictionaryValue& manifest); 182 void OnUnpackExtensionFailed(const base::string16& error_message); 183 184 void ReportFailure(FailureReason reason, const base::string16& message); 185 void ReportSuccess(const base::DictionaryValue& original_manifest, 186 const SkBitmap& install_icon); 187 188 // Overwrites original manifest with safe result from utility process. 189 // Returns NULL on error. Caller owns the returned object. 190 base::DictionaryValue* RewriteManifestFile( 191 const base::DictionaryValue& manifest); 192 193 // Overwrites original files with safe results from utility process. 194 // Reports error and returns false if it fails. 195 bool RewriteImageFiles(SkBitmap* install_icon); 196 bool RewriteCatalogFiles(); 197 198 // Cleans up temp directory artifacts. 199 void Cleanup(); 200 201 // The path to the CRX to unpack. 202 base::FilePath crx_path_; 203 204 // Our client. 205 scoped_refptr<SandboxedUnpackerClient> client_; 206 207 // The Extensions directory inside the profile. 208 base::FilePath extensions_dir_; 209 210 // A temporary directory to use for unpacking. 211 base::ScopedTempDir temp_dir_; 212 213 // The root directory of the unpacked extension. This is a child of temp_dir_. 214 base::FilePath extension_root_; 215 216 // Represents the extension we're unpacking. 217 scoped_refptr<Extension> extension_; 218 219 // Whether we've received a response from the utility process yet. 220 bool got_response_; 221 222 // The public key that was extracted from the CRX header. 223 std::string public_key_; 224 225 // The extension's ID. This will be calculated from the public key in the crx 226 // header. 227 std::string extension_id_; 228 229 // Time at which unpacking started. Used to compute the time unpacking takes. 230 base::TimeTicks unpack_start_time_; 231 232 // Location to use for the unpacked extension. 233 Manifest::Location location_; 234 235 // Creation flags to use for the extension. These flags will be used 236 // when calling Extenion::Create() by the crx installer. 237 int creation_flags_; 238 239 // Sequenced task runner where file I/O operations will be performed at. 240 scoped_refptr<base::SequencedTaskRunner> unpacker_io_task_runner_; 241 }; 242 243 } // namespace extensions 244 245 #endif // CHROME_BROWSER_EXTENSIONS_SANDBOXED_UNPACKER_H_ 246