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

Side by Side Diff: services/authentication/accounts_db_manager.cc

Issue 1800753005: C++ bindings: A struct's Deserialize() now does validation before deserializing. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: fix char* - >char bug in unittest. FixedBuffer can accept sizes that aren't 8 byte multiples. Created 4 years, 8 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
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 "services/authentication/accounts_db_manager.h" 5 #include "services/authentication/accounts_db_manager.h"
6 6
7 #include <vector> 7 #include <vector>
8 8
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/strings/string_tokenizer.h" 10 #include "base/strings/string_tokenizer.h"
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after
174 174
175 if (error != mojo::files::Error::OK) { 175 if (error != mojo::files::Error::OK) {
176 LOG(ERROR) << "Read() error on accounts db: " << error; 176 LOG(ERROR) << "Read() error on accounts db: " << error;
177 error_ = CREDENTIALS_DB_READ_ERROR; 177 error_ = CREDENTIALS_DB_READ_ERROR;
178 return; 178 return;
179 } 179 }
180 180
181 if (bytes_read.size() != 0) { 181 if (bytes_read.size() != 0) {
182 // Deserialize data from file 182 // Deserialize data from file
183 const char* data = reinterpret_cast<const char*>(&bytes_read[0]); 183 const char* data = reinterpret_cast<const char*>(&bytes_read[0]);
184 184 if (!creds_store_.Deserialize((void*)data, bytes_read.size())) {
185 // Validate the file contents before deserializing 185 LOG(ERROR) << "Deserialize() error on accounts db.";
186 mojo::internal::BoundsChecker bounds_checker(data, bytes_read.size(), 0);
187 std::string error;
188 mojo::internal::ValidationError verror =
189 internal::CredentialStore_Data::Validate(data, &bounds_checker, &error);
190 if (verror != mojo::internal::ValidationError::NONE) {
191 LOG(ERROR) << "Validation() error on accounts db ["
192 << ValidationErrorToString(verror) << "][" << error << "]";
193 error_ = CREDENTIALS_DB_VALIDATE_ERROR; 186 error_ = CREDENTIALS_DB_VALIDATE_ERROR;
194 return; 187 return;
195 } 188 }
196
197 creds_store_.Deserialize((void*)data);
198 // When we have multiple versions, this is not a fatal error, but a sign 189 // When we have multiple versions, this is not a fatal error, but a sign
199 // that we need to update (or reinitialize) the db. 190 // that we need to update (or reinitialize) the db.
200 CHECK_EQ(creds_store_.version, kCredsDbVersion); 191 CHECK_EQ(creds_store_.version, kCredsDbVersion);
201 } else { 192 } else {
202 creds_store_.version = kCredsDbVersion; 193 creds_store_.version = kCredsDbVersion;
203 } 194 }
204 195
205 db_init_option_ |= CREDENTIALS_DB_INIT_SUCCESS; 196 db_init_option_ |= CREDENTIALS_DB_INIT_SUCCESS;
206 } 197 }
207 198
208 void AccountsDbManager::OnAuthorizationsFileReadResponse( 199 void AccountsDbManager::OnAuthorizationsFileReadResponse(
209 const mojo::files::Error error, 200 const mojo::files::Error error,
210 const mojo::Array<uint8_t> bytes_read) { 201 const mojo::Array<uint8_t> bytes_read) {
211 CHECK(error_ == NONE); 202 CHECK(error_ == NONE);
212 203
213 if (error != mojo::files::Error::OK) { 204 if (error != mojo::files::Error::OK) {
214 LOG(ERROR) << "Read() error on auth db: " << error; 205 LOG(ERROR) << "Read() error on auth db: " << error;
215 error_ = AUTHORIZATIONS_DB_READ_ERROR; 206 error_ = AUTHORIZATIONS_DB_READ_ERROR;
216 return; 207 return;
217 } 208 }
218 209
219 if (bytes_read.size() != 0) { 210 if (bytes_read.size() != 0) {
220 // Deserialize data from file 211 // Deserialize data from file
221 const char* data = reinterpret_cast<const char*>(&bytes_read[0]); 212 const char* data = reinterpret_cast<const char*>(&bytes_read[0]);
222 213 if (!auth_grants_.Deserialize((void*)data, bytes_read.size())) {
223 // Validate the file contents before deserializing 214 LOG(ERROR) << "Deserialize() error on auth db.";
224 mojo::internal::BoundsChecker bounds_checker(data, bytes_read.size(), 0);
225 if (internal::Db_Data::Validate(data, &bounds_checker, nullptr) !=
226 mojo::internal::ValidationError::NONE) {
227 LOG(ERROR) << "Validation() error on auth db.";
228 error_ = AUTHORIZATIONS_DB_VALIDATE_ERROR; 215 error_ = AUTHORIZATIONS_DB_VALIDATE_ERROR;
229 return; 216 return;
230 } 217 }
231 218
232 auth_grants_.Deserialize((void*)data);
233 // When we have multiple versions, this is not a fatal error, but a sign 219 // When we have multiple versions, this is not a fatal error, but a sign
234 // that we need to update (or reinitialize) the db. 220 // that we need to update (or reinitialize) the db.
235 CHECK_EQ(auth_grants_.version, kAuthDbVersion); 221 CHECK_EQ(auth_grants_.version, kAuthDbVersion);
236 } else { 222 } else {
237 auth_grants_.version = kAuthDbVersion; 223 auth_grants_.version = kAuthDbVersion;
238 } 224 }
239 225
240 db_init_option_ |= AUTHORIZATIONS_DB_INIT_SUCCESS; 226 db_init_option_ |= AUTHORIZATIONS_DB_INIT_SUCCESS;
241 } 227 }
242 228
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
283 CHECK(error_ == NONE); 269 CHECK(error_ == NONE);
284 270
285 if (mojo::files::Error::OK != error) { 271 if (mojo::files::Error::OK != error) {
286 LOG(ERROR) << "Write() error on auth db:" << error; 272 LOG(ERROR) << "Write() error on auth db:" << error;
287 error_ = AUTHORIZATIONS_DB_WRITE_ERROR; 273 error_ = AUTHORIZATIONS_DB_WRITE_ERROR;
288 return; 274 return;
289 } 275 }
290 } 276 }
291 277
292 } // namespace authentication 278 } // namespace authentication
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698