[email protected] | abd4cb2 | 2014-05-16 05:22:56 | [diff] [blame] | 1 | // Copyright 2014 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 "extensions/browser/computed_hashes.h" |
| 6 | |
dcheng | f1950200 | 2016-09-14 15:18:18 | [diff] [blame] | 7 | #include <memory> |
| 8 | #include <utility> |
| 9 | |
[email protected] | abd4cb2 | 2014-05-16 05:22:56 | [diff] [blame] | 10 | #include "base/base64.h" |
[email protected] | abd4cb2 | 2014-05-16 05:22:56 | [diff] [blame] | 11 | #include "base/files/file_path.h" |
thestig | 9471270 | 2014-09-10 07:46:59 | [diff] [blame] | 12 | #include "base/files/file_util.h" |
[email protected] | abd4cb2 | 2014-05-16 05:22:56 | [diff] [blame] | 13 | #include "base/json/json_reader.h" |
| 14 | #include "base/json/json_writer.h" |
Istiaque Ahmed | 588df56 | 2018-07-06 20:16:04 | [diff] [blame^] | 15 | #include "base/metrics/histogram_macros.h" |
[email protected] | de00aeb | 2014-08-06 09:13:39 | [diff] [blame] | 16 | #include "base/stl_util.h" |
Istiaque Ahmed | 588df56 | 2018-07-06 20:16:04 | [diff] [blame^] | 17 | #include "base/timer/elapsed_timer.h" |
[email protected] | de00aeb | 2014-08-06 09:13:39 | [diff] [blame] | 18 | #include "base/values.h" |
| 19 | #include "crypto/secure_hash.h" |
| 20 | #include "crypto/sha2.h" |
[email protected] | abd4cb2 | 2014-05-16 05:22:56 | [diff] [blame] | 21 | |
Jens Widell | fa216f1 | 2018-02-02 08:48:35 | [diff] [blame] | 22 | namespace extensions { |
| 23 | |
| 24 | namespace computed_hashes { |
[email protected] | abd4cb2 | 2014-05-16 05:22:56 | [diff] [blame] | 25 | const char kBlockHashesKey[] = "block_hashes"; |
[email protected] | de00aeb | 2014-08-06 09:13:39 | [diff] [blame] | 26 | const char kBlockSizeKey[] = "block_size"; |
| 27 | const char kFileHashesKey[] = "file_hashes"; |
| 28 | const char kPathKey[] = "path"; |
| 29 | const char kVersionKey[] = "version"; |
| 30 | const int kVersion = 2; |
Jens Widell | fa216f1 | 2018-02-02 08:48:35 | [diff] [blame] | 31 | } // namespace computed_hashes |
[email protected] | abd4cb2 | 2014-05-16 05:22:56 | [diff] [blame] | 32 | |
Istiaque Ahmed | 588df56 | 2018-07-06 20:16:04 | [diff] [blame^] | 33 | namespace { |
| 34 | |
| 35 | // Helper to record UMA for ComputedHashes::Reader::InitFromFile. |
| 36 | // Records failure UMA if RecordSuccess() isn't explicitly called. |
| 37 | class ScopedUMARecorder { |
| 38 | public: |
| 39 | ScopedUMARecorder() = default; |
| 40 | |
| 41 | ~ScopedUMARecorder() { |
| 42 | if (recorded_) |
| 43 | return; |
| 44 | RecordImpl(false); |
| 45 | } |
| 46 | |
| 47 | void RecordSuccess() { |
| 48 | recorded_ = true; |
| 49 | RecordImpl(true); |
| 50 | } |
| 51 | |
| 52 | private: |
| 53 | void RecordImpl(bool succeeded) { |
| 54 | UMA_HISTOGRAM_BOOLEAN( |
| 55 | "Extensions.ContentVerification.ComputedHashesReadResult", succeeded); |
| 56 | if (succeeded) { |
| 57 | UMA_HISTOGRAM_TIMES( |
| 58 | "Extensions.ContentVerification.ComputedHashesInitTime", |
| 59 | timer_.Elapsed()); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | bool recorded_ = false; |
| 64 | base::ElapsedTimer timer_; |
| 65 | DISALLOW_COPY_AND_ASSIGN(ScopedUMARecorder); |
| 66 | }; |
| 67 | |
| 68 | } // namespace |
| 69 | |
[email protected] | abd4cb2 | 2014-05-16 05:22:56 | [diff] [blame] | 70 | ComputedHashes::Reader::Reader() { |
| 71 | } |
[email protected] | de00aeb | 2014-08-06 09:13:39 | [diff] [blame] | 72 | |
[email protected] | abd4cb2 | 2014-05-16 05:22:56 | [diff] [blame] | 73 | ComputedHashes::Reader::~Reader() { |
| 74 | } |
| 75 | |
| 76 | bool ComputedHashes::Reader::InitFromFile(const base::FilePath& path) { |
Istiaque Ahmed | 588df56 | 2018-07-06 20:16:04 | [diff] [blame^] | 77 | ScopedUMARecorder uma_recorder; |
[email protected] | abd4cb2 | 2014-05-16 05:22:56 | [diff] [blame] | 78 | std::string contents; |
| 79 | if (!base::ReadFileToString(path, &contents)) |
| 80 | return false; |
| 81 | |
[email protected] | de00aeb | 2014-08-06 09:13:39 | [diff] [blame] | 82 | base::DictionaryValue* top_dictionary = NULL; |
dcheng | f5d24108 | 2016-04-21 03:43:11 | [diff] [blame] | 83 | std::unique_ptr<base::Value> value(base::JSONReader::Read(contents)); |
[email protected] | de00aeb | 2014-08-06 09:13:39 | [diff] [blame] | 84 | if (!value.get() || !value->GetAsDictionary(&top_dictionary)) |
| 85 | return false; |
| 86 | |
| 87 | // For now we don't support forwards or backwards compatability in the |
| 88 | // format, so we return false on version mismatch. |
| 89 | int version = 0; |
Jens Widell | fa216f1 | 2018-02-02 08:48:35 | [diff] [blame] | 90 | if (!top_dictionary->GetInteger(computed_hashes::kVersionKey, &version) || |
| 91 | version != computed_hashes::kVersion) |
[email protected] | de00aeb | 2014-08-06 09:13:39 | [diff] [blame] | 92 | return false; |
| 93 | |
| 94 | base::ListValue* all_hashes = NULL; |
Jens Widell | fa216f1 | 2018-02-02 08:48:35 | [diff] [blame] | 95 | if (!top_dictionary->GetList(computed_hashes::kFileHashesKey, &all_hashes)) |
[email protected] | abd4cb2 | 2014-05-16 05:22:56 | [diff] [blame] | 96 | return false; |
| 97 | |
| 98 | for (size_t i = 0; i < all_hashes->GetSize(); i++) { |
| 99 | base::DictionaryValue* dictionary = NULL; |
| 100 | if (!all_hashes->GetDictionary(i, &dictionary)) |
| 101 | return false; |
| 102 | |
| 103 | std::string relative_path_utf8; |
Jens Widell | fa216f1 | 2018-02-02 08:48:35 | [diff] [blame] | 104 | if (!dictionary->GetString(computed_hashes::kPathKey, &relative_path_utf8)) |
[email protected] | abd4cb2 | 2014-05-16 05:22:56 | [diff] [blame] | 105 | return false; |
| 106 | |
| 107 | int block_size; |
Jens Widell | fa216f1 | 2018-02-02 08:48:35 | [diff] [blame] | 108 | if (!dictionary->GetInteger(computed_hashes::kBlockSizeKey, &block_size)) |
[email protected] | abd4cb2 | 2014-05-16 05:22:56 | [diff] [blame] | 109 | return false; |
| 110 | if (block_size <= 0 || ((block_size % 1024) != 0)) { |
| 111 | LOG(ERROR) << "Invalid block size: " << block_size; |
[email protected] | abd4cb2 | 2014-05-16 05:22:56 | [diff] [blame] | 112 | return false; |
| 113 | } |
| 114 | |
| 115 | base::ListValue* hashes_list = NULL; |
Jens Widell | fa216f1 | 2018-02-02 08:48:35 | [diff] [blame] | 116 | if (!dictionary->GetList(computed_hashes::kBlockHashesKey, &hashes_list)) |
[email protected] | abd4cb2 | 2014-05-16 05:22:56 | [diff] [blame] | 117 | return false; |
| 118 | |
| 119 | base::FilePath relative_path = |
| 120 | base::FilePath::FromUTF8Unsafe(relative_path_utf8); |
[email protected] | 4f9bdf6 | 2014-06-28 01:08:22 | [diff] [blame] | 121 | relative_path = relative_path.NormalizePathSeparatorsTo('/'); |
[email protected] | abd4cb2 | 2014-05-16 05:22:56 | [diff] [blame] | 122 | |
| 123 | data_[relative_path] = HashInfo(block_size, std::vector<std::string>()); |
| 124 | std::vector<std::string>* hashes = &(data_[relative_path].second); |
| 125 | |
| 126 | for (size_t j = 0; j < hashes_list->GetSize(); j++) { |
| 127 | std::string encoded; |
| 128 | if (!hashes_list->GetString(j, &encoded)) |
| 129 | return false; |
| 130 | |
| 131 | hashes->push_back(std::string()); |
| 132 | std::string* decoded = &hashes->back(); |
| 133 | if (!base::Base64Decode(encoded, decoded)) { |
| 134 | hashes->clear(); |
| 135 | return false; |
| 136 | } |
| 137 | } |
| 138 | } |
Istiaque Ahmed | 588df56 | 2018-07-06 20:16:04 | [diff] [blame^] | 139 | uma_recorder.RecordSuccess(); |
[email protected] | abd4cb2 | 2014-05-16 05:22:56 | [diff] [blame] | 140 | return true; |
| 141 | } |
| 142 | |
| 143 | bool ComputedHashes::Reader::GetHashes(const base::FilePath& relative_path, |
| 144 | int* block_size, |
Istiaque Ahmed | 9bdd9d9 | 2017-12-16 04:53:27 | [diff] [blame] | 145 | std::vector<std::string>* hashes) const { |
[email protected] | 4f9bdf6 | 2014-06-28 01:08:22 | [diff] [blame] | 146 | base::FilePath path = relative_path.NormalizePathSeparatorsTo('/'); |
Istiaque Ahmed | 9bdd9d9 | 2017-12-16 04:53:27 | [diff] [blame] | 147 | std::map<base::FilePath, HashInfo>::const_iterator i = data_.find(path); |
asargent | 79369191 | 2014-10-04 01:12:21 | [diff] [blame] | 148 | if (i == data_.end()) { |
| 149 | // If we didn't find the entry using exact match, it's possible the |
| 150 | // developer is using a path with some letters in the incorrect case, which |
| 151 | // happens to work on windows/osx. So try doing a linear scan to look for a |
| 152 | // case-insensitive match. In practice most extensions don't have that big |
| 153 | // a list of files so the performance penalty is probably not too big |
| 154 | // here. Also for crbug.com/29941 we plan to start warning developers when |
| 155 | // they are making this mistake, since their extension will be broken on |
| 156 | // linux/chromeos. |
| 157 | for (i = data_.begin(); i != data_.end(); ++i) { |
| 158 | const base::FilePath& entry = i->first; |
| 159 | if (base::FilePath::CompareEqualIgnoreCase(entry.value(), path.value())) |
| 160 | break; |
| 161 | } |
| 162 | if (i == data_.end()) |
| 163 | return false; |
| 164 | } |
Istiaque Ahmed | 9bdd9d9 | 2017-12-16 04:53:27 | [diff] [blame] | 165 | const HashInfo& info = i->second; |
[email protected] | abd4cb2 | 2014-05-16 05:22:56 | [diff] [blame] | 166 | *block_size = info.first; |
| 167 | *hashes = info.second; |
| 168 | return true; |
| 169 | } |
| 170 | |
[email protected] | de00aeb | 2014-08-06 09:13:39 | [diff] [blame] | 171 | ComputedHashes::Writer::Writer() : file_list_(new base::ListValue) { |
[email protected] | abd4cb2 | 2014-05-16 05:22:56 | [diff] [blame] | 172 | } |
[email protected] | de00aeb | 2014-08-06 09:13:39 | [diff] [blame] | 173 | |
[email protected] | abd4cb2 | 2014-05-16 05:22:56 | [diff] [blame] | 174 | ComputedHashes::Writer::~Writer() { |
| 175 | } |
| 176 | |
| 177 | void ComputedHashes::Writer::AddHashes(const base::FilePath& relative_path, |
| 178 | int block_size, |
| 179 | const std::vector<std::string>& hashes) { |
Jeremy Roman | 16529d0e | 2017-08-24 18:13:47 | [diff] [blame] | 180 | auto block_hashes = std::make_unique<base::ListValue>(); |
jdoerrie | 6ff270ca | 2017-06-07 10:31:45 | [diff] [blame] | 181 | block_hashes->GetList().reserve(hashes.size()); |
| 182 | for (const auto& hash : hashes) { |
| 183 | std::string encoded; |
| 184 | base::Base64Encode(hash, &encoded); |
| 185 | block_hashes->GetList().emplace_back(std::move(encoded)); |
| 186 | } |
| 187 | |
Jeremy Roman | 16529d0e | 2017-08-24 18:13:47 | [diff] [blame] | 188 | auto dict = std::make_unique<base::DictionaryValue>(); |
Jens Widell | fa216f1 | 2018-02-02 08:48:35 | [diff] [blame] | 189 | dict->SetString(computed_hashes::kPathKey, |
[email protected] | 4f9bdf6 | 2014-06-28 01:08:22 | [diff] [blame] | 190 | relative_path.NormalizePathSeparatorsTo('/').AsUTF8Unsafe()); |
Jens Widell | fa216f1 | 2018-02-02 08:48:35 | [diff] [blame] | 191 | dict->SetInteger(computed_hashes::kBlockSizeKey, block_size); |
| 192 | dict->Set(computed_hashes::kBlockHashesKey, std::move(block_hashes)); |
dcheng | f1950200 | 2016-09-14 15:18:18 | [diff] [blame] | 193 | file_list_->Append(std::move(dict)); |
[email protected] | abd4cb2 | 2014-05-16 05:22:56 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | bool ComputedHashes::Writer::WriteToFile(const base::FilePath& path) { |
| 197 | std::string json; |
[email protected] | de00aeb | 2014-08-06 09:13:39 | [diff] [blame] | 198 | base::DictionaryValue top_dictionary; |
Jens Widell | fa216f1 | 2018-02-02 08:48:35 | [diff] [blame] | 199 | top_dictionary.SetInteger(computed_hashes::kVersionKey, |
| 200 | computed_hashes::kVersion); |
| 201 | top_dictionary.Set(computed_hashes::kFileHashesKey, std::move(file_list_)); |
[email protected] | de00aeb | 2014-08-06 09:13:39 | [diff] [blame] | 202 | |
estade | 8d04646 | 2015-05-16 01:02:34 | [diff] [blame] | 203 | if (!base::JSONWriter::Write(top_dictionary, &json)) |
[email protected] | abd4cb2 | 2014-05-16 05:22:56 | [diff] [blame] | 204 | return false; |
| 205 | int written = base::WriteFile(path, json.data(), json.size()); |
| 206 | if (static_cast<unsigned>(written) != json.size()) { |
[email protected] | de00aeb | 2014-08-06 09:13:39 | [diff] [blame] | 207 | LOG(ERROR) << "Error writing " << path.AsUTF8Unsafe() |
[email protected] | abd4cb2 | 2014-05-16 05:22:56 | [diff] [blame] | 208 | << " ; write result:" << written << " expected:" << json.size(); |
| 209 | return false; |
| 210 | } |
| 211 | return true; |
| 212 | } |
| 213 | |
[email protected] | de00aeb | 2014-08-06 09:13:39 | [diff] [blame] | 214 | void ComputedHashes::ComputeHashesForContent(const std::string& contents, |
| 215 | size_t block_size, |
| 216 | std::vector<std::string>* hashes) { |
| 217 | size_t offset = 0; |
| 218 | // Even when the contents is empty, we want to output at least one hash |
| 219 | // block (the hash of the empty string). |
| 220 | do { |
| 221 | const char* block_start = contents.data() + offset; |
| 222 | DCHECK(offset <= contents.size()); |
| 223 | size_t bytes_to_read = std::min(contents.size() - offset, block_size); |
dcheng | f5d24108 | 2016-04-21 03:43:11 | [diff] [blame] | 224 | std::unique_ptr<crypto::SecureHash> hash( |
[email protected] | de00aeb | 2014-08-06 09:13:39 | [diff] [blame] | 225 | crypto::SecureHash::Create(crypto::SecureHash::SHA256)); |
| 226 | hash->Update(block_start, bytes_to_read); |
| 227 | |
| 228 | hashes->push_back(std::string()); |
| 229 | std::string* buffer = &(hashes->back()); |
| 230 | buffer->resize(crypto::kSHA256Length); |
Ryan Sleevi | 972b2ff | 2018-05-14 15:45:10 | [diff] [blame] | 231 | hash->Finish(base::data(*buffer), buffer->size()); |
[email protected] | de00aeb | 2014-08-06 09:13:39 | [diff] [blame] | 232 | |
| 233 | // If |contents| is empty, then we want to just exit here. |
| 234 | if (bytes_to_read == 0) |
| 235 | break; |
| 236 | |
| 237 | offset += bytes_to_read; |
| 238 | } while (offset < contents.size()); |
| 239 | } |
| 240 | |
[email protected] | abd4cb2 | 2014-05-16 05:22:56 | [diff] [blame] | 241 | } // namespace extensions |