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

Side by Side Diff: net/disk_cache/simple/simple_synchronous_entry.cc

Issue 2205073002: Simple Cache: validate lengths before allocations. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@2743
Patch Set: Created 4 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
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 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 "net/disk_cache/simple/simple_synchronous_entry.h" 5 #include "net/disk_cache/simple/simple_synchronous_entry.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <cstring> 8 #include <cstring>
9 #include <functional> 9 #include <functional>
10 #include <limits> 10 #include <limits>
(...skipping 647 matching lines...) Expand 10 before | Expand all | Expand 10 after
658 } 658 }
659 659
660 void SimpleSynchronousEntry::CheckEOFRecord(int index, 660 void SimpleSynchronousEntry::CheckEOFRecord(int index,
661 const SimpleEntryStat& entry_stat, 661 const SimpleEntryStat& entry_stat,
662 uint32_t expected_crc32, 662 uint32_t expected_crc32,
663 int* out_result) const { 663 int* out_result) const {
664 DCHECK(initialized_); 664 DCHECK(initialized_);
665 uint32_t crc32; 665 uint32_t crc32;
666 bool has_crc32; 666 bool has_crc32;
667 bool has_key_sha256; 667 bool has_key_sha256;
668 int stream_size; 668 int32_t stream_size;
669 *out_result = GetEOFRecordData(index, entry_stat, &has_crc32, &has_key_sha256, 669 *out_result = GetEOFRecordData(index, entry_stat, &has_crc32, &has_key_sha256,
670 &crc32, &stream_size); 670 &crc32, &stream_size);
671 if (*out_result != net::OK) { 671 if (*out_result != net::OK) {
672 Doom(); 672 Doom();
673 return; 673 return;
674 } 674 }
675 if (has_crc32 && crc32 != expected_crc32) { 675 if (has_crc32 && crc32 != expected_crc32) {
676 DVLOG(1) << "EOF record had bad crc."; 676 DVLOG(1) << "EOF record had bad crc.";
677 *out_result = net::ERR_CACHE_CHECKSUM_MISMATCH; 677 *out_result = net::ERR_CACHE_CHECKSUM_MISMATCH;
678 RecordCheckEOFResult(cache_type_, CHECK_EOF_RESULT_CRC_MISMATCH); 678 RecordCheckEOFResult(cache_type_, CHECK_EOF_RESULT_CRC_MISMATCH);
(...skipping 482 matching lines...) Expand 10 before | Expand all | Expand 10 after
1161 } 1161 }
1162 1162
1163 int SimpleSynchronousEntry::ReadAndValidateStream0( 1163 int SimpleSynchronousEntry::ReadAndValidateStream0(
1164 int file_size, 1164 int file_size,
1165 SimpleEntryStat* out_entry_stat, 1165 SimpleEntryStat* out_entry_stat,
1166 scoped_refptr<net::GrowableIOBuffer>* stream_0_data, 1166 scoped_refptr<net::GrowableIOBuffer>* stream_0_data,
1167 uint32_t* out_stream_0_crc32) { 1167 uint32_t* out_stream_0_crc32) {
1168 // Pretend this file has a null stream zero, and contains the optional key 1168 // Pretend this file has a null stream zero, and contains the optional key
1169 // SHA256. This is good enough to read the EOF record on the file, which gives 1169 // SHA256. This is good enough to read the EOF record on the file, which gives
1170 // the actual size of stream 0. 1170 // the actual size of stream 0.
1171 int total_data_size = GetDataSizeFromFileSize(key_.size(), file_size); 1171 int temp_data_size = GetDataSizeFromFileSize(key_.size(), file_size);
1172 out_entry_stat->set_data_size(0, 0); 1172 out_entry_stat->set_data_size(0, 0);
1173 out_entry_stat->set_data_size( 1173 out_entry_stat->set_data_size(
1174 1, 1174 1, temp_data_size - sizeof(net::SHA256HashValue) - sizeof(SimpleFileEOF));
1175 total_data_size - sizeof(net::SHA256HashValue) - sizeof(SimpleFileEOF));
1176 1175
1177 bool has_crc32; 1176 bool has_crc32;
1178 bool has_key_sha256; 1177 bool has_key_sha256;
1179 uint32_t read_crc32; 1178 uint32_t read_crc32;
1180 int stream_0_size; 1179 int32_t stream_0_size;
1181 int ret_value_crc32 = 1180 int ret_value_crc32 =
1182 GetEOFRecordData(0, *out_entry_stat, &has_crc32, &has_key_sha256, 1181 GetEOFRecordData(0, *out_entry_stat, &has_crc32, &has_key_sha256,
1183 &read_crc32, &stream_0_size); 1182 &read_crc32, &stream_0_size);
1184 if (ret_value_crc32 != net::OK) 1183 if (ret_value_crc32 != net::OK)
1185 return ret_value_crc32; 1184 return ret_value_crc32;
1186 // Calculate and set the real values for data size. 1185
1187 int stream_1_size = out_entry_stat->data_size(1) - stream_0_size; 1186 // Calculate and set the real values for the two streams.
1187 int32_t total_size = out_entry_stat->data_size(1);
1188 if (!has_key_sha256) 1188 if (!has_key_sha256)
1189 stream_1_size += sizeof(net::SHA256HashValue); 1189 total_size += sizeof(net::SHA256HashValue);
1190 if (stream_1_size < 0) 1190 if (stream_0_size > total_size)
1191 return net::ERR_FAILED; 1191 return net::ERR_FAILED;
1192 out_entry_stat->set_data_size(0, stream_0_size); 1192 out_entry_stat->set_data_size(0, stream_0_size);
1193 out_entry_stat->set_data_size(1, stream_1_size); 1193 out_entry_stat->set_data_size(1, total_size - stream_0_size);
1194 1194
1195 // Put stream 0 data in memory. 1195 // Put stream 0 data in memory.
1196 *stream_0_data = new net::GrowableIOBuffer(); 1196 *stream_0_data = new net::GrowableIOBuffer();
1197 (*stream_0_data)->SetCapacity(stream_0_size + sizeof(net::SHA256HashValue)); 1197 (*stream_0_data)->SetCapacity(stream_0_size + sizeof(net::SHA256HashValue));
1198 int file_offset = out_entry_stat->GetOffsetInFile(key_.size(), 0, 0); 1198 int file_offset = out_entry_stat->GetOffsetInFile(key_.size(), 0, 0);
1199 int read_size = stream_0_size; 1199 int read_size = stream_0_size;
1200 if (has_key_sha256) 1200 if (has_key_sha256)
1201 read_size += sizeof(net::SHA256HashValue); 1201 read_size += sizeof(net::SHA256HashValue);
1202 if (files_[0].Read(file_offset, (*stream_0_data)->data(), read_size) != 1202 if (files_[0].Read(file_offset, (*stream_0_data)->data(), read_size) !=
1203 read_size) 1203 read_size)
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
1239 1239
1240 RecordCheckEOFResult(cache_type_, CHECK_EOF_RESULT_SUCCESS); 1240 RecordCheckEOFResult(cache_type_, CHECK_EOF_RESULT_SUCCESS);
1241 return net::OK; 1241 return net::OK;
1242 } 1242 }
1243 1243
1244 int SimpleSynchronousEntry::GetEOFRecordData(int index, 1244 int SimpleSynchronousEntry::GetEOFRecordData(int index,
1245 const SimpleEntryStat& entry_stat, 1245 const SimpleEntryStat& entry_stat,
1246 bool* out_has_crc32, 1246 bool* out_has_crc32,
1247 bool* out_has_key_sha256, 1247 bool* out_has_key_sha256,
1248 uint32_t* out_crc32, 1248 uint32_t* out_crc32,
1249 int* out_data_size) const { 1249 int32_t* out_data_size) const {
1250 SimpleFileEOF eof_record; 1250 SimpleFileEOF eof_record;
1251 int file_offset = entry_stat.GetEOFOffsetInFile(key_.size(), index); 1251 int file_offset = entry_stat.GetEOFOffsetInFile(key_.size(), index);
1252 int file_index = GetFileIndexFromStreamIndex(index); 1252 int file_index = GetFileIndexFromStreamIndex(index);
1253 File* file = const_cast<File*>(&files_[file_index]); 1253 File* file = const_cast<File*>(&files_[file_index]);
1254 if (file->Read(file_offset, reinterpret_cast<char*>(&eof_record), 1254 if (file->Read(file_offset, reinterpret_cast<char*>(&eof_record),
1255 sizeof(eof_record)) != 1255 sizeof(eof_record)) !=
1256 sizeof(eof_record)) { 1256 sizeof(eof_record)) {
1257 RecordCheckEOFResult(cache_type_, CHECK_EOF_RESULT_READ_FAILURE); 1257 RecordCheckEOFResult(cache_type_, CHECK_EOF_RESULT_READ_FAILURE);
1258 return net::ERR_CACHE_CHECKSUM_READ_FAILURE; 1258 return net::ERR_CACHE_CHECKSUM_READ_FAILURE;
1259 } 1259 }
1260 1260
1261 if (eof_record.final_magic_number != kSimpleFinalMagicNumber) { 1261 if (eof_record.final_magic_number != kSimpleFinalMagicNumber) {
1262 RecordCheckEOFResult(cache_type_, CHECK_EOF_RESULT_MAGIC_NUMBER_MISMATCH); 1262 RecordCheckEOFResult(cache_type_, CHECK_EOF_RESULT_MAGIC_NUMBER_MISMATCH);
1263 DVLOG(1) << "EOF record had bad magic number."; 1263 DVLOG(1) << "EOF record had bad magic number.";
1264 return net::ERR_CACHE_CHECKSUM_READ_FAILURE; 1264 return net::ERR_CACHE_CHECKSUM_READ_FAILURE;
1265 } 1265 }
1266 1266
1267 if (!base::IsValueInRangeForNumericType<int32_t>(eof_record.stream_size))
1268 return net::ERR_FAILED;
1269
1267 *out_has_crc32 = (eof_record.flags & SimpleFileEOF::FLAG_HAS_CRC32) == 1270 *out_has_crc32 = (eof_record.flags & SimpleFileEOF::FLAG_HAS_CRC32) ==
1268 SimpleFileEOF::FLAG_HAS_CRC32; 1271 SimpleFileEOF::FLAG_HAS_CRC32;
1269 *out_has_key_sha256 = 1272 *out_has_key_sha256 =
1270 (eof_record.flags & SimpleFileEOF::FLAG_HAS_KEY_SHA256) == 1273 (eof_record.flags & SimpleFileEOF::FLAG_HAS_KEY_SHA256) ==
1271 SimpleFileEOF::FLAG_HAS_KEY_SHA256; 1274 SimpleFileEOF::FLAG_HAS_KEY_SHA256;
1272 *out_crc32 = eof_record.data_crc32; 1275 *out_crc32 = eof_record.data_crc32;
1273 *out_data_size = eof_record.stream_size; 1276 *out_data_size = eof_record.stream_size;
1274 SIMPLE_CACHE_UMA(BOOLEAN, "SyncCheckEOFHasCrc", cache_type_, *out_has_crc32); 1277 SIMPLE_CACHE_UMA(BOOLEAN, "SyncCheckEOFHasCrc", cache_type_, *out_has_crc32);
1275 return net::OK; 1278 return net::OK;
1276 } 1279 }
(...skipping 312 matching lines...) Expand 10 before | Expand all | Expand 10 after
1589 range.offset = offset; 1592 range.offset = offset;
1590 range.length = len; 1593 range.length = len;
1591 range.data_crc32 = data_crc32; 1594 range.data_crc32 = data_crc32;
1592 range.file_offset = data_file_offset; 1595 range.file_offset = data_file_offset;
1593 sparse_ranges_.insert(std::make_pair(offset, range)); 1596 sparse_ranges_.insert(std::make_pair(offset, range));
1594 1597
1595 return true; 1598 return true;
1596 } 1599 }
1597 1600
1598 } // namespace disk_cache 1601 } // namespace disk_cache
OLDNEW
« no previous file with comments | « net/disk_cache/simple/simple_synchronous_entry.h ('k') | net/disk_cache/simple/simple_test_util.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698