| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/common/component_flash_hint_file_linux.h" | 5 #include "chrome/common/component_flash_hint_file_linux.h" |
| 6 | 6 |
| 7 #include <fcntl.h> | 7 #include <fcntl.h> |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 #include <stdint.h> | 9 #include <stdint.h> |
| 10 #include <sys/mman.h> | 10 #include <sys/mman.h> |
| 11 | 11 |
| 12 #include <memory> |
| 13 |
| 12 #include "base/base64.h" | 14 #include "base/base64.h" |
| 13 #include "base/files/file_path.h" | 15 #include "base/files/file_path.h" |
| 14 #include "base/files/file_util.h" | 16 #include "base/files/file_util.h" |
| 15 #include "base/files/important_file_writer.h" | 17 #include "base/files/important_file_writer.h" |
| 16 #include "base/files/memory_mapped_file.h" | 18 #include "base/files/memory_mapped_file.h" |
| 17 #include "base/files/scoped_file.h" | 19 #include "base/files/scoped_file.h" |
| 18 #include "base/json/json_string_value_serializer.h" | 20 #include "base/json/json_string_value_serializer.h" |
| 19 #include "base/memory/scoped_ptr.h" | |
| 20 #include "base/path_service.h" | 21 #include "base/path_service.h" |
| 21 #include "base/posix/eintr_wrapper.h" | 22 #include "base/posix/eintr_wrapper.h" |
| 22 #include "base/stl_util.h" | 23 #include "base/stl_util.h" |
| 23 #include "base/values.h" | 24 #include "base/values.h" |
| 24 #include "chrome/common/chrome_paths.h" | 25 #include "chrome/common/chrome_paths.h" |
| 25 #include "crypto/secure_hash.h" | 26 #include "crypto/secure_hash.h" |
| 26 #include "crypto/secure_util.h" | 27 #include "crypto/secure_util.h" |
| 27 #include "crypto/sha2.h" | 28 #include "crypto/sha2.h" |
| 28 | 29 |
| 29 namespace component_flash_hint_file { | 30 namespace component_flash_hint_file { |
| (...skipping 28 matching lines...) Expand all Loading... |
| 58 | 59 |
| 59 // Hashes the plugin file and returns the result in the out params. | 60 // Hashes the plugin file and returns the result in the out params. |
| 60 // |mapped_file| is the file to be hashed. | 61 // |mapped_file| is the file to be hashed. |
| 61 // |result| is the buffer, which must be of size crypto::kSHA256Length, which | 62 // |result| is the buffer, which must be of size crypto::kSHA256Length, which |
| 62 // will contain the hash. | 63 // will contain the hash. |
| 63 // |len| is the size of the buffer, which must be crypto::kSHA256Length. | 64 // |len| is the size of the buffer, which must be crypto::kSHA256Length. |
| 64 void SHA256Hash(const base::MemoryMappedFile& mapped_file, | 65 void SHA256Hash(const base::MemoryMappedFile& mapped_file, |
| 65 void* result, | 66 void* result, |
| 66 size_t len) { | 67 size_t len) { |
| 67 CHECK_EQ(crypto::kSHA256Length, len); | 68 CHECK_EQ(crypto::kSHA256Length, len); |
| 68 scoped_ptr<crypto::SecureHash> secure_hash( | 69 std::unique_ptr<crypto::SecureHash> secure_hash( |
| 69 crypto::SecureHash::Create(crypto::SecureHash::SHA256)); | 70 crypto::SecureHash::Create(crypto::SecureHash::SHA256)); |
| 70 secure_hash->Update(mapped_file.data(), mapped_file.length()); | 71 secure_hash->Update(mapped_file.data(), mapped_file.length()); |
| 71 secure_hash->Finish(result, len); | 72 secure_hash->Finish(result, len); |
| 72 } | 73 } |
| 73 | 74 |
| 74 // This will serialize the file to disk as JSON. The format is: | 75 // This will serialize the file to disk as JSON. The format is: |
| 75 // { | 76 // { |
| 76 // "Version": 0x10, | 77 // "Version": 0x10, |
| 77 // "HashAlgorithm": SecureHash::SHA256, | 78 // "HashAlgorithm": SecureHash::SHA256, |
| 78 // "Hash": <Base64 Encoded Hash>, | 79 // "Hash": <Base64 Encoded Hash>, |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 110 | 111 |
| 111 } // namespace | 112 } // namespace |
| 112 | 113 |
| 113 bool TestExecutableMapping(const base::FilePath& path) { | 114 bool TestExecutableMapping(const base::FilePath& path) { |
| 114 const base::ScopedFD fd( | 115 const base::ScopedFD fd( |
| 115 HANDLE_EINTR(open(path.value().c_str(), O_RDONLY | O_CLOEXEC))); | 116 HANDLE_EINTR(open(path.value().c_str(), O_RDONLY | O_CLOEXEC))); |
| 116 if (!fd.is_valid()) | 117 if (!fd.is_valid()) |
| 117 return false; | 118 return false; |
| 118 const size_t map_size = sizeof(uint8_t); | 119 const size_t map_size = sizeof(uint8_t); |
| 119 const MmapDeleter deleter(map_size); | 120 const MmapDeleter deleter(map_size); |
| 120 scoped_ptr<uint8_t, MmapDeleter> buf_ptr( | 121 std::unique_ptr<uint8_t, MmapDeleter> buf_ptr( |
| 121 reinterpret_cast<uint8_t*>(mmap(nullptr, map_size, PROT_READ | PROT_EXEC, | 122 reinterpret_cast<uint8_t*>(mmap(nullptr, map_size, PROT_READ | PROT_EXEC, |
| 122 MAP_PRIVATE, fd.get(), 0)), | 123 MAP_PRIVATE, fd.get(), 0)), |
| 123 deleter); | 124 deleter); |
| 124 return buf_ptr.get() != MAP_FAILED; | 125 return buf_ptr.get() != MAP_FAILED; |
| 125 } | 126 } |
| 126 | 127 |
| 127 bool RecordFlashUpdate(const base::FilePath& unpacked_plugin, | 128 bool RecordFlashUpdate(const base::FilePath& unpacked_plugin, |
| 128 const base::FilePath& moved_plugin, | 129 const base::FilePath& moved_plugin, |
| 129 const std::string& version) { | 130 const std::string& version) { |
| 130 base::MemoryMappedFile mapped_file; | 131 base::MemoryMappedFile mapped_file; |
| (...skipping 21 matching lines...) Expand all Loading... |
| 152 if (!PathService::Get(chrome::FILE_COMPONENT_FLASH_HINT, &hint_file_path)) | 153 if (!PathService::Get(chrome::FILE_COMPONENT_FLASH_HINT, &hint_file_path)) |
| 153 return false; | 154 return false; |
| 154 | 155 |
| 155 std::string json_string; | 156 std::string json_string; |
| 156 if (!base::ReadFileToString(hint_file_path, &json_string)) | 157 if (!base::ReadFileToString(hint_file_path, &json_string)) |
| 157 return false; | 158 return false; |
| 158 | 159 |
| 159 int error_code; | 160 int error_code; |
| 160 std::string error_message; | 161 std::string error_message; |
| 161 JSONStringValueDeserializer deserializer(json_string); | 162 JSONStringValueDeserializer deserializer(json_string); |
| 162 const scoped_ptr<base::Value> value = | 163 const std::unique_ptr<base::Value> value = |
| 163 deserializer.Deserialize(&error_code, &error_message); | 164 deserializer.Deserialize(&error_code, &error_message); |
| 164 | 165 |
| 165 if (!value) { | 166 if (!value) { |
| 166 LOG(ERROR) | 167 LOG(ERROR) |
| 167 << "Could not deserialize the component updated Flash hint file. Error " | 168 << "Could not deserialize the component updated Flash hint file. Error " |
| 168 << error_code << ": " << error_message; | 169 << error_code << ": " << error_message; |
| 169 return false; | 170 return false; |
| 170 } | 171 } |
| 171 | 172 |
| 172 base::DictionaryValue* dict = nullptr; | 173 base::DictionaryValue* dict = nullptr; |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 216 "component flash plugin will not be loaded."; | 217 "component flash plugin will not be loaded."; |
| 217 return false; | 218 return false; |
| 218 } | 219 } |
| 219 | 220 |
| 220 *path = plugin_path; | 221 *path = plugin_path; |
| 221 flash_version->assign(plugin_version_str); | 222 flash_version->assign(plugin_version_str); |
| 222 return true; | 223 return true; |
| 223 } | 224 } |
| 224 | 225 |
| 225 } // namespace component_flash_hint_file | 226 } // namespace component_flash_hint_file |
| OLD | NEW |