OLD | NEW |
---|---|
(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/sha2.h" | |
24 | |
25 namespace chrome { | |
26 | |
27 namespace { | |
28 // The current version of the hints file. | |
29 const int kCurrentHintFileVersion = 0x10; | |
30 // The earliest version of the hints file. | |
31 const int kEarliestHintFileVersion = 0x10; | |
32 // The Version field in the JSON encoded file. | |
33 const char kVersionField[] = "Version"; | |
34 // The HashAlgorithm field in the JSON encoded file. | |
35 const char kHashAlgoField[] = "HashAlgorithm"; | |
36 // The Hash field in the JSON encoded file. | |
37 const char kHashField[] = "Hash"; | |
38 // The PluginPath field in the JSON encoded file. | |
39 const char kPluginPath[] = "PluginPath"; | |
40 // The PluginVersion field in the JSON encoded file. | |
41 const char kPluginVersion[] = "PluginVersion"; | |
42 // For use with the scoped_ptr of an mmap-ed buffer | |
43 struct MmapDeleter { | |
44 size_t map_size; | |
45 inline void operator()(uint8_t* ptr) const { | |
46 if (ptr != MAP_FAILED) | |
47 munmap(ptr, map_size); | |
48 } | |
49 }; | |
50 | |
51 // Hashes the plugin file and returns the result in the out params. | |
52 void SHA256Hash(const base::MemoryMappedFile& mapped_file, | |
53 void* result, | |
54 size_t len) { | |
55 CHECK(len == crypto::kSHA256Length); | |
jln (very slow on Chromium)
2015/08/06 18:48:15
CHECK_EQ()
Greg K
2015/08/07 21:15:29
Done.
| |
56 scoped_ptr<crypto::SecureHash> secure_hash( | |
57 crypto::SecureHash::Create(crypto::SecureHash::SHA256)); | |
58 size_t bytes_remaining = mapped_file.length(), bytes_read = 0; | |
jln (very slow on Chromium)
2015/08/06 18:48:15
Style: one line per variable (can't find where tha
Greg K
2015/08/07 21:15:29
Done.
| |
59 const size_t page_size = sysconf(_SC_PAGE_SIZE); | |
60 | |
61 while (bytes_remaining > 0) { | |
62 const size_t len = | |
63 bytes_remaining >= page_size ? page_size : bytes_remaining; | |
64 CHECK(bytes_remaining >= len); | |
jln (very slow on Chromium)
2015/08/06 18:48:14
CHECK_GE()
Greg K
2015/08/07 21:15:29
Acknowledged.
| |
65 secure_hash->Update(mapped_file.data() + bytes_read, len); | |
jln (very slow on Chromium)
2015/08/06 18:48:14
Why force this page by page? Why not ask update to
Greg K
2015/08/07 21:15:29
Done.
| |
66 bytes_remaining -= len; | |
67 bytes_read += len; | |
68 } | |
69 secure_hash->Finish(result, len); | |
70 } | |
71 | |
72 } // namespace | |
73 | |
74 // static | |
75 bool ComponentFlashHintFile::TestExecutableMapping(const base::FilePath& path) { | |
76 const base::ScopedFD fd(HANDLE_EINTR(open(path.value().c_str(), O_RDONLY))); | |
jln (very slow on Chromium)
2015/08/06 18:48:15
Do CLOEXEC while you're at it.
Greg K
2015/08/07 21:15:29
Done.
| |
77 if (!fd.is_valid()) | |
78 return false; | |
79 const MmapDeleter deleter = {.map_size = sizeof(uint8_t)}; | |
jln (very slow on Chromium)
2015/08/06 18:48:14
Style: I don't think we allow designed initializer
Greg K
2015/08/07 21:15:29
Done.
| |
80 scoped_ptr<uint8_t, MmapDeleter> buf_ptr( | |
81 reinterpret_cast<uint8_t*>(mmap(NULL, deleter.map_size, | |
jln (very slow on Chromium)
2015/08/06 18:48:14
Given the simplicity here, I would not use the Mma
Greg K
2015/08/07 21:15:29
Makes sense, but I will leave it there in case som
| |
82 PROT_READ | PROT_EXEC, MAP_PRIVATE, | |
83 fd.get(), 0)), | |
84 deleter); | |
85 return buf_ptr.get() != MAP_FAILED; | |
86 } | |
87 | |
88 // static | |
89 bool ComponentFlashHintFile::RecordFlashUpdate( | |
90 const base::FilePath& unpacked_plugin, | |
91 const base::FilePath& moved_plugin, | |
92 const std::string& version) { | |
93 base::MemoryMappedFile mapped_file; | |
94 if (!mapped_file.Initialize(unpacked_plugin)) | |
95 return false; | |
96 | |
97 std::string hash(crypto::kSHA256Length, 0); | |
98 SHA256Hash(mapped_file, string_as_array(&hash), hash.size()); | |
99 | |
100 return WriteToDisk(kCurrentHintFileVersion, | |
101 crypto::SecureHash::Algorithm::SHA256, hash, moved_plugin, | |
102 version); | |
103 } | |
104 | |
105 // static | |
106 bool ComponentFlashHintFile::DoesHintFileExist() { | |
107 base::FilePath hint_file_path; | |
108 if (!PathService::Get(chrome::FILE_COMPONENT_FLASH_HINT, &hint_file_path)) | |
109 return false; | |
110 return base::PathExists(hint_file_path); | |
111 } | |
112 | |
113 // static | |
114 bool ComponentFlashHintFile::WriteToDisk( | |
115 const int version, | |
116 const crypto::SecureHash::Algorithm algorithm, | |
117 const std::string& hash, | |
118 const base::FilePath& plugin_path, | |
119 const std::string& flash_version) { | |
120 base::FilePath hint_file_path; | |
121 if (!PathService::Get(chrome::FILE_COMPONENT_FLASH_HINT, &hint_file_path)) | |
122 return false; | |
123 | |
124 std::string encoded_hash; | |
125 base::Base64Encode(hash, &encoded_hash); | |
126 | |
127 // Now construct a Value object to convert to JSON. | |
128 base::DictionaryValue dict; | |
129 dict.SetInteger(kVersionField, version); | |
130 dict.SetInteger(kHashAlgoField, crypto::SecureHash::SHA256); | |
131 dict.SetString(kHashField, encoded_hash); | |
132 dict.SetString(kPluginPath, plugin_path.value()); | |
133 dict.SetString(kPluginVersion, flash_version); | |
134 // Do the serialization of the DictionaryValue to JSON. | |
135 std::string json_string; | |
136 JSONStringValueSerializer serializer(&json_string); | |
137 if (!serializer.Serialize(dict)) | |
138 return false; | |
139 | |
140 return base::ImportantFileWriter::WriteFileAtomically(hint_file_path, | |
141 json_string); | |
142 } | |
143 | |
144 // static | |
145 bool ComponentFlashHintFile::VerifyAndReturnFlashLocation( | |
146 base::FilePath* path, | |
147 std::string* flash_version) { | |
148 base::FilePath hint_file_path; | |
149 if (!PathService::Get(chrome::FILE_COMPONENT_FLASH_HINT, &hint_file_path)) | |
150 return false; | |
151 | |
152 std::string json_string; | |
153 if (!base::ReadFileToString(hint_file_path, &json_string)) | |
154 return false; | |
155 | |
156 int error_code; | |
157 std::string error_message; | |
158 JSONStringValueDeserializer deserializer(json_string); | |
159 const scoped_ptr<base::Value> value( | |
160 deserializer.Deserialize(&error_code, &error_message)); | |
161 | |
162 if (value == nullptr) { | |
163 LOG(ERROR) | |
164 << "Could not deserialize the component updated flash hint file. Error " | |
165 << error_code << ": " << error_message; | |
166 return false; | |
167 } | |
168 | |
169 base::DictionaryValue* dict = nullptr; | |
170 if (!value->GetAsDictionary(&dict)) | |
171 return false; | |
172 | |
173 int version; | |
174 if (!dict->GetInteger(kVersionField, &version)) | |
175 return false; | |
176 if (version < kEarliestHintFileVersion || version > kCurrentHintFileVersion) | |
177 return false; | |
178 | |
179 int hash_algorithm; | |
180 if (!dict->GetInteger(kHashAlgoField, &hash_algorithm)) | |
181 return false; | |
182 if (hash_algorithm != crypto::SecureHash::SHA256) | |
183 return false; | |
184 | |
185 std::string hash; | |
186 if (!dict->GetString(kHashField, &hash)) | |
187 return false; | |
188 | |
189 std::string plugin_path_str; | |
190 if (!dict->GetString(kPluginPath, &plugin_path_str)) | |
191 return false; | |
192 | |
193 std::string plugin_version_str; | |
194 if (!dict->GetString(kPluginVersion, &plugin_version_str)) | |
195 return false; | |
196 | |
197 std::string decoded_hash; | |
198 if (!base::Base64Decode(hash, &decoded_hash)) | |
199 return false; | |
200 | |
201 const base::FilePath plugin_path(plugin_path_str); | |
202 base::MemoryMappedFile plugin_file; | |
203 if (!plugin_file.Initialize(plugin_path)) | |
204 return false; | |
205 | |
206 std::vector<uint8_t> file_hash(crypto::kSHA256Length, 0); | |
207 SHA256Hash(plugin_file, &file_hash[0], file_hash.size()); | |
208 if (memcmp(&file_hash[0], string_as_array(&decoded_hash), | |
jln (very slow on Chromium)
2015/08/06 18:48:15
So there is nothing better than memcmp in crypto/
Greg K
2015/08/07 21:15:29
Turns out there is SecureEqualMem.
| |
209 crypto::kSHA256Length) != 0) { | |
210 LOG(ERROR) | |
211 << "The hash recorded in the component flash hint file does not " | |
212 "match the actual hash of the flash plugin found on disk. The " | |
213 "component flash plugin will not be loaded."; | |
214 return false; | |
215 } | |
216 | |
217 *path = plugin_path; | |
218 flash_version->assign(plugin_version_str); | |
219 return true; | |
220 } | |
221 | |
222 } // namespace chrome | |
OLD | NEW |