Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(12)

Side by Side Diff: chrome/common/component_flash_hint_file_linux.cc

Issue 1261333004: Add support for Flash Player Component updates on Linux (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Updates per bauerb's code review. Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 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 #include "chrome/common/component_flash_hint_file.h"
6
7 #include <fcntl.h>
8 #include <sys/mman.h>
9
10 #include "base/base64.h"
11 #include "base/files/file_path.h"
12 #include "base/files/file_util.h"
13 #include "base/files/important_file_writer.h"
14 #include "base/files/memory_mapped_file.h"
15 #include "base/files/scoped_file.h"
16 #include "base/json/json_string_value_serializer.h"
17 #include "base/path_service.h"
18 #include "base/posix/eintr_wrapper.h"
19 #include "base/stl_util.h"
20 #include "base/values.h"
21 #include "chrome/common/chrome_paths.h"
22 #include "crypto/secure_hash.h"
23 #include "crypto/secure_util.h"
24 #include "crypto/sha2.h"
25
26 namespace chrome {
27
28 namespace component_flash_hint_file {
29
30 namespace {
31 // The current version of the hints file.
32 const int kCurrentHintFileVersion = 0x10;
33 // The earliest version of the hints file.
34 const int kEarliestHintFileVersion = 0x10;
35 // The Version field in the JSON encoded file.
36 const char kVersionField[] = "Version";
37 // The HashAlgorithm field in the JSON encoded file.
38 const char kHashAlgoField[] = "HashAlgorithm";
39 // The Hash field in the JSON encoded file.
40 const char kHashField[] = "Hash";
41 // The PluginPath field in the JSON encoded file.
42 const char kPluginPath[] = "PluginPath";
43 // The PluginVersion field in the JSON encoded file.
44 const char kPluginVersion[] = "PluginVersion";
45 // For use with the scoped_ptr of an mmap-ed buffer
46 struct MmapDeleter {
47 explicit MmapDeleter(size_t map_size) { map_size = map_size_; }
48 inline void operator()(uint8_t* ptr) const {
49 if (ptr != MAP_FAILED)
50 munmap(ptr, map_size_);
51 }
52
53 private:
54 size_t map_size_;
55 };
56
57 // Hashes the plugin file and returns the result in the out params.
58 // |mapped_file| is the file to be hashed.
59 // |result| is the buffer, which must be of size crypto::kSHA256Length, which
60 // will contain the hash.
61 // |len| is the size of the buffer, which must be crypto::kSHA256Length.
62 void SHA256Hash(const base::MemoryMappedFile& mapped_file,
63 void* result,
64 size_t len) {
65 CHECK_EQ(crypto::kSHA256Length, len);
66 scoped_ptr<crypto::SecureHash> secure_hash(
67 crypto::SecureHash::Create(crypto::SecureHash::SHA256));
68 secure_hash->Update(mapped_file.data(), mapped_file.length());
69 secure_hash->Finish(result, len);
70 }
71
72 // This will serialize the file to disk as JSON. The format is:
73 // {
74 // "Version": 0x10,
75 // "HashAlgorithm": SecureHash::SHA256,
76 // "Hash": <Base64 Encoded Hash>,
77 // "PluginPath": /path/to/component/updated/flash.so,
78 // "PluginVersion": "1.0.0.1"
79 // }
80 bool WriteToDisk(const int version,
81 const crypto::SecureHash::Algorithm algorithm,
82 const std::string& hash,
83 const base::FilePath& plugin_path,
84 const std::string& flash_version) {
85 base::FilePath hint_file_path;
86 if (!PathService::Get(chrome::FILE_COMPONENT_FLASH_HINT, &hint_file_path))
87 return false;
88
89 std::string encoded_hash;
90 base::Base64Encode(hash, &encoded_hash);
91
92 // Now construct a Value object to convert to JSON.
93 base::DictionaryValue dict;
94 dict.SetInteger(kVersionField, version);
95 dict.SetInteger(kHashAlgoField, crypto::SecureHash::SHA256);
96 dict.SetString(kHashField, encoded_hash);
97 dict.SetString(kPluginPath, plugin_path.value());
98 dict.SetString(kPluginVersion, flash_version);
99 // Do the serialization of the DictionaryValue to JSON.
100 std::string json_string;
101 JSONStringValueSerializer serializer(&json_string);
102 if (!serializer.Serialize(dict))
103 return false;
104
105 return base::ImportantFileWriter::WriteFileAtomically(hint_file_path,
106 json_string);
107 }
108
109 } // namespace
110
111 bool TestExecutableMapping(const base::FilePath& path) {
112 const base::ScopedFD fd(
113 HANDLE_EINTR(open(path.value().c_str(), O_RDONLY | O_CLOEXEC)));
114 if (!fd.is_valid())
115 return false;
116 const size_t map_size = sizeof(uint8_t);
117 const MmapDeleter deleter(map_size);
118 scoped_ptr<uint8_t, MmapDeleter> buf_ptr(
119 reinterpret_cast<uint8_t*>(mmap(nullptr, map_size, PROT_READ | PROT_EXEC,
120 MAP_PRIVATE, fd.get(), 0)),
121 deleter);
122 return buf_ptr.get() != MAP_FAILED;
123 }
124
125 bool RecordFlashUpdate(const base::FilePath& unpacked_plugin,
126 const base::FilePath& moved_plugin,
127 const std::string& version) {
128 base::MemoryMappedFile mapped_file;
129 if (!mapped_file.Initialize(unpacked_plugin))
130 return false;
131
132 std::string hash(crypto::kSHA256Length, 0);
133 SHA256Hash(mapped_file, string_as_array(&hash), hash.size());
134
135 return WriteToDisk(kCurrentHintFileVersion,
136 crypto::SecureHash::Algorithm::SHA256, hash, moved_plugin,
137 version);
138 }
139
140 bool DoesHintFileExist() {
141 base::FilePath hint_file_path;
142 if (!PathService::Get(chrome::FILE_COMPONENT_FLASH_HINT, &hint_file_path))
143 return false;
144 return base::PathExists(hint_file_path);
145 }
146
147 bool VerifyAndReturnFlashLocation(base::FilePath* path,
148 std::string* flash_version) {
149 base::FilePath hint_file_path;
150 if (!PathService::Get(chrome::FILE_COMPONENT_FLASH_HINT, &hint_file_path))
151 return false;
152
153 std::string json_string;
154 if (!base::ReadFileToString(hint_file_path, &json_string))
155 return false;
156
157 int error_code;
158 std::string error_message;
159 JSONStringValueDeserializer deserializer(json_string);
160 const scoped_ptr<base::Value> value(
161 deserializer.Deserialize(&error_code, &error_message));
162
163 if (!value) {
164 LOG(ERROR)
165 << "Could not deserialize the component updated flash hint file. Error "
166 << error_code << ": " << error_message;
167 return false;
168 }
169
170 base::DictionaryValue* dict = nullptr;
171 if (!value->GetAsDictionary(&dict))
172 return false;
173
174 int version;
175 if (!dict->GetInteger(kVersionField, &version))
176 return false;
177 if (version < kEarliestHintFileVersion || version > kCurrentHintFileVersion)
178 return false;
179
180 int hash_algorithm;
181 if (!dict->GetInteger(kHashAlgoField, &hash_algorithm))
182 return false;
183 if (hash_algorithm != crypto::SecureHash::SHA256)
184 return false;
185
186 std::string hash;
187 if (!dict->GetString(kHashField, &hash))
188 return false;
189
190 std::string plugin_path_str;
191 if (!dict->GetString(kPluginPath, &plugin_path_str))
192 return false;
193
194 std::string plugin_version_str;
195 if (!dict->GetString(kPluginVersion, &plugin_version_str))
196 return false;
197
198 std::string decoded_hash;
199 if (!base::Base64Decode(hash, &decoded_hash))
200 return false;
201
202 const base::FilePath plugin_path(plugin_path_str);
203 base::MemoryMappedFile plugin_file;
204 if (!plugin_file.Initialize(plugin_path))
205 return false;
206
207 std::vector<uint8_t> file_hash(crypto::kSHA256Length, 0);
208 SHA256Hash(plugin_file, &file_hash[0], file_hash.size());
209 if (!crypto::SecureMemEqual(&file_hash[0], string_as_array(&decoded_hash),
210 crypto::kSHA256Length)) {
211 LOG(ERROR)
212 << "The hash recorded in the component flash hint file does not "
213 "match the actual hash of the flash plugin found on disk. The "
214 "component flash plugin will not be loaded.";
215 return false;
216 }
217
218 *path = plugin_path;
219 flash_version->assign(plugin_version_str);
220 return true;
221 }
222
223 } // namespace component_flash_hint_file
224
225 } // namespace chrome
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698