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

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: Rebase to fix merge conflicts 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/sha2.h"
23
24 namespace chrome {
25
26 namespace {
27 // The current version of the hints file.
28 const int kCurrentHintFileVersion = 0x10;
29 // The earliest version of the hints file.
30 const int kEarliestHintFileVersion = 0x10;
31 // The Version field in the JSON encoded file.
32 const char kVersionField[] = "Version";
33 // The HashAlgorithm field in the JSON encoded file.
34 const char kHashAlgoField[] = "HashAlgorithm";
35 // The Hash field in the JSON encoded file.
36 const char kHashField[] = "Hash";
37 // The PluginPath field in the JSON encoded file.
38 const char kPluginPath[] = "PluginPath";
39 // The PluginVersion field in the JSON encoded file.
40 const char kPluginVersion[] = "PluginVersion";
41 // For use with the scoped_ptr of an mmap-ed buffer
42 struct MmapDeleter {
43 size_t map_size;
44 inline void operator()(uint8_t* ptr) const {
45 if (ptr != MAP_FAILED)
46 munmap(ptr, map_size);
47 }
48 };
49
50 } // namespace
51
52 // static
53 bool ComponentFlashHintFile::TestExecutableMapping(const base::FilePath& path) {
54 base::ScopedFD fd(HANDLE_EINTR(open(path.value().c_str(), O_RDONLY)));
55 if (!fd.is_valid())
56 return false;
57 MmapDeleter deleter = {.map_size = sizeof(uint8_t)};
58 scoped_ptr<uint8_t, MmapDeleter> buf_ptr(
59 reinterpret_cast<uint8_t*>(mmap(NULL, deleter.map_size,
60 PROT_READ | PROT_EXEC, MAP_PRIVATE,
61 fd.get(), 0)),
62 deleter);
63 return buf_ptr.get() != MAP_FAILED;
64 }
65
66 // static
67 bool ComponentFlashHintFile::RecordFlashUpdate(
68 const base::FilePath& unpacked_plugin,
69 const base::FilePath& moved_plugin,
70 const std::string& version) {
71 base::MemoryMappedFile mapped_file;
72 if (!mapped_file.Initialize(unpacked_plugin))
73 return false;
74
75 std::string hash(crypto::kSHA256Length, 0);
76 crypto::SHA256HashBytes(mapped_file.data(), mapped_file.length(),
77 string_as_array(&hash), hash.size());
78
79 return WriteToDisk(kCurrentHintFileVersion,
80 crypto::SecureHash::Algorithm::SHA256, hash, moved_plugin,
81 version);
82 }
83
84 // static
85 bool ComponentFlashHintFile::DoesHintFileExist() {
86 base::FilePath hint_file_path;
87 if (!PathService::Get(chrome::FILE_COMPONENT_FLASH_HINT, &hint_file_path))
88 return false;
89 return base::PathExists(hint_file_path);
90 }
91
92 // static
93 bool ComponentFlashHintFile::WriteToDisk(
94 int version,
95 crypto::SecureHash::Algorithm algorithm,
96 const std::string& hash,
97 const base::FilePath& plugin_path,
98 const std::string& flash_version) {
99 base::FilePath hint_file_path;
100 if (!PathService::Get(chrome::FILE_COMPONENT_FLASH_HINT, &hint_file_path))
101 return false;
102
103 std::string encoded_hash;
104 base::Base64Encode(hash, &encoded_hash);
105
106 // Now construct a Value object to convert to JSON.
107 base::DictionaryValue dict;
108 dict.SetInteger(kVersionField, version);
109 dict.SetInteger(kHashAlgoField, crypto::SecureHash::SHA256);
110 dict.SetString(kHashField, encoded_hash);
111 dict.SetString(kPluginPath, plugin_path.value());
112 dict.SetString(kPluginVersion, flash_version);
113 // Do the serialization of the DictionaryValue to JSON.
114 std::string json_string;
115 JSONStringValueSerializer serializer(&json_string);
116 if (!serializer.Serialize(dict))
117 return false;
118
119 return base::ImportantFileWriter::WriteFileAtomically(hint_file_path,
120 json_string);
121 }
122
123 // static
124 bool ComponentFlashHintFile::VerifyAndReturnFlashLocation(
125 base::FilePath* path,
126 std::string* flash_version) {
127 base::FilePath hint_file_path;
128 if (!PathService::Get(chrome::FILE_COMPONENT_FLASH_HINT, &hint_file_path))
129 return false;
130
131 std::string json_string;
132 if (!base::ReadFileToString(hint_file_path, &json_string))
133 return false;
134
135 int error_code;
136 std::string error_message;
137 JSONStringValueDeserializer deserializer(json_string);
138 scoped_ptr<base::Value> value(
139 deserializer.Deserialize(&error_code, &error_message));
140
141 if (value == nullptr) {
142 LOG(ERROR)
143 << "Could not deserialize the component updated flash hint file. Error "
144 << error_code << ": " << error_message;
145 return false;
146 }
147
148 base::DictionaryValue* dict = nullptr;
149 if (!value->GetAsDictionary(&dict))
150 return false;
151
152 int version;
153 if (!dict->GetInteger(kVersionField, &version))
154 return false;
155 if (version < kEarliestHintFileVersion || version > kCurrentHintFileVersion)
156 return false;
157
158 int hash_algorithm;
159 if (!dict->GetInteger(kHashAlgoField, &hash_algorithm))
160 return false;
161 if (hash_algorithm != crypto::SecureHash::SHA256)
162 return false;
163
164 std::string hash;
165 if (!dict->GetString(kHashField, &hash))
166 return false;
167
168 std::string plugin_path_str;
169 if (!dict->GetString(kPluginPath, &plugin_path_str))
170 return false;
171
172 std::string plugin_version_str;
173 if (!dict->GetString(kPluginVersion, &plugin_version_str))
174 return false;
175
176 std::string decoded_hash;
177 if (!base::Base64Decode(hash, &decoded_hash))
178 return false;
179
180 base::FilePath plugin_path(plugin_path_str);
181 base::MemoryMappedFile plugin_file;
182 if (!plugin_file.Initialize(plugin_path))
183 return false;
184
185 std::vector<uint8_t> file_hash(crypto::kSHA256Length, 0);
186 crypto::SHA256HashBytes(plugin_file.data(), plugin_file.length(),
187 &file_hash[0], file_hash.size());
188 if (memcmp(&file_hash[0], string_as_array(&decoded_hash),
jln (very slow on Chromium) 2015/08/04 01:08:33 Ugh. There isn't a better API in crypto for this?
Greg K 2015/08/04 18:30:00 I will talk to Sleevi.
189 crypto::kSHA256Length) != 0) {
190 LOG(ERROR)
191 << "The hash recorded in the component flash hint file does not "
192 "match the actual hash of the flash plugin found on disk. The "
193 "component flash plugin will not be loaded.";
194 return false;
195 }
196
197 *path = plugin_path;
198 flash_version->assign(plugin_version_str);
199 return true;
200 }
201
202 } // namespace chrome
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698