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

Side by Side Diff: components/policy/core/common/preg_parser.cc

Issue 2791193005: Add fuzzer test for preg_parser (Closed)
Patch Set: Compile preg parser on Linux Created 3 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 (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 "components/policy/core/common/preg_parser.h" 5 #include "components/policy/core/common/preg_parser.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include <algorithm> 10 #include <algorithm>
11 #include <functional> 11 #include <functional>
12 #include <iterator> 12 #include <iterator>
13 #include <limits> 13 #include <limits>
14 #include <memory> 14 #include <memory>
15 #include <string> 15 #include <string>
16 #include <utility> 16 #include <utility>
17 #include <vector> 17 #include <vector>
18 18
19 #include "base/files/file_path.h" 19 #include "base/files/file_path.h"
20 #include "base/files/memory_mapped_file.h" 20 #include "base/files/memory_mapped_file.h"
21 #include "base/logging.h" 21 #include "base/logging.h"
22 #include "base/macros.h" 22 #include "base/macros.h"
23 #include "base/memory/ptr_util.h" 23 #include "base/memory/ptr_util.h"
24 #include "base/strings/string16.h" 24 #include "base/strings/string16.h"
25 #include "base/strings/string_split.h" 25 #include "base/strings/string_split.h"
26 #include "base/strings/string_util.h" 26 #include "base/strings/string_util.h"
27 #include "base/strings/stringprintf.h"
27 #include "base/strings/utf_string_conversions.h" 28 #include "base/strings/utf_string_conversions.h"
28 #include "base/sys_byteorder.h" 29 #include "base/sys_byteorder.h"
29 #include "base/values.h" 30 #include "base/values.h"
30 #include "components/policy/core/common/policy_load_status.h"
31 #include "components/policy/core/common/registry_dict.h" 31 #include "components/policy/core/common/registry_dict.h"
32 32
33 #if defined(OS_WIN) 33 #if defined(OS_WIN)
34 #include "windows.h" 34 #include "windows.h"
35 #else 35 #else
36 // Registry data type constants. 36 // Registry data type constants.
37 #define REG_NONE 0 37 #define REG_NONE 0
38 #define REG_SZ 1 38 #define REG_SZ 1
39 #define REG_EXPAND_SZ 2 39 #define REG_EXPAND_SZ 2
40 #define REG_BINARY 3 40 #define REG_BINARY 3
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after
209 209
210 std::string value_name(base::UTF16ToUTF8(value)); 210 std::string value_name(base::UTF16ToUTF8(value));
211 if (!base::StartsWith(value_name, kActionTriggerPrefix, 211 if (!base::StartsWith(value_name, kActionTriggerPrefix,
212 base::CompareCase::SENSITIVE)) { 212 base::CompareCase::SENSITIVE)) {
213 std::unique_ptr<base::Value> value; 213 std::unique_ptr<base::Value> value;
214 if (DecodePRegValue(type, data, &value)) 214 if (DecodePRegValue(type, data, &value))
215 dict->SetValue(value_name, std::move(value)); 215 dict->SetValue(value_name, std::move(value));
216 return; 216 return;
217 } 217 }
218 218
219 std::string action_trigger(base::ToLowerASCII(value_name.substr( 219 std::string action_trigger(base::ToLowerASCII(
220 arraysize(kActionTriggerPrefix) - 1))); 220 value_name.substr(arraysize(kActionTriggerPrefix) - 1)));
221 if (action_trigger == kActionTriggerDeleteValues) { 221 if (action_trigger == kActionTriggerDeleteValues) {
222 for (const std::string& value : 222 for (const std::string& value :
223 base::SplitString(DecodePRegStringValue(data), ";", 223 base::SplitString(DecodePRegStringValue(data), ";",
224 base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY)) 224 base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY))
225 dict->RemoveValue(value); 225 dict->RemoveValue(value);
226 } else if (base::StartsWith(action_trigger, kActionTriggerDeleteKeys, 226 } else if (base::StartsWith(action_trigger, kActionTriggerDeleteKeys,
227 base::CompareCase::SENSITIVE)) { 227 base::CompareCase::SENSITIVE)) {
228 for (const std::string& key : 228 for (const std::string& key :
229 base::SplitString(DecodePRegStringValue(data), ";", 229 base::SplitString(DecodePRegStringValue(data), ";",
230 base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY)) 230 base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY))
231 dict->RemoveKey(key); 231 dict->RemoveKey(key);
232 } else if (base::StartsWith(action_trigger, kActionTriggerDel, 232 } else if (base::StartsWith(action_trigger, kActionTriggerDel,
233 base::CompareCase::SENSITIVE)) { 233 base::CompareCase::SENSITIVE)) {
234 dict->RemoveValue( 234 dict->RemoveValue(value_name.substr(arraysize(kActionTriggerPrefix) - 1 +
235 value_name.substr(arraysize(kActionTriggerPrefix) - 1 + 235 arraysize(kActionTriggerDel) - 1));
236 arraysize(kActionTriggerDel) - 1));
237 } else if (base::StartsWith(action_trigger, kActionTriggerDelVals, 236 } else if (base::StartsWith(action_trigger, kActionTriggerDelVals,
238 base::CompareCase::SENSITIVE)) { 237 base::CompareCase::SENSITIVE)) {
239 // Delete all values. 238 // Delete all values.
240 dict->ClearValues(); 239 dict->ClearValues();
241 } else if (base::StartsWith(action_trigger, kActionTriggerSecureKey, 240 } else if (base::StartsWith(action_trigger, kActionTriggerSecureKey,
242 base::CompareCase::SENSITIVE) || 241 base::CompareCase::SENSITIVE) ||
243 base::StartsWith(action_trigger, kActionTriggerSoft, 242 base::StartsWith(action_trigger, kActionTriggerSoft,
244 base::CompareCase::SENSITIVE)) { 243 base::CompareCase::SENSITIVE)) {
245 // Doesn't affect values. 244 // Doesn't affect values.
246 } else { 245 } else {
247 LOG(ERROR) << "Bad action trigger " << value_name; 246 LOG(ERROR) << "Bad action trigger " << value_name;
248 } 247 }
249 } 248 }
250 249
251 } // namespace 250 } // namespace
252 251
253 namespace policy { 252 namespace policy {
254 namespace preg_parser { 253 namespace preg_parser {
255 254
256 const char kPRegFileHeader[8] = 255 const char kPRegFileHeader[8] = {'P', 'R', 'e', 'g',
257 { 'P', 'R', 'e', 'g', '\x01', '\x00', '\x00', '\x00' }; 256 '\x01', '\x00', '\x00', '\x00'};
258 257
259 bool ReadFile(const base::FilePath& file_path, 258 bool ReadFile(const base::FilePath& file_path,
260 const base::string16& root, 259 const base::string16& root,
261 RegistryDict* dict, 260 RegistryDict* dict,
262 PolicyLoadStatusSample* status) { 261 PolicyLoadStatusSample* status_sample) {
263 base::MemoryMappedFile mapped_file; 262 base::MemoryMappedFile mapped_file;
264 if (!mapped_file.Initialize(file_path) || !mapped_file.IsValid()) { 263 if (!mapped_file.Initialize(file_path) || !mapped_file.IsValid()) {
265 PLOG(ERROR) << "Failed to map " << file_path.value(); 264 PLOG(ERROR) << "Failed to map " << file_path.value();
266 status->Add(POLICY_LOAD_STATUS_READ_ERROR); 265 status_sample->Add(POLICY_LOAD_STATUS_READ_ERROR);
267 return false; 266 return false;
268 } 267 }
269 268
270 if (mapped_file.length() > kMaxPRegFileSize) { 269 PolicyLoadStatus status = POLICY_LOAD_STATUS_SIZE;
271 LOG(ERROR) << "PReg file " << file_path.value() << " too large: " 270 bool res = ReadDataInternal(
272 << mapped_file.length(); 271 mapped_file.data(), mapped_file.length(), root, dict, &status,
273 status->Add(POLICY_LOAD_STATUS_TOO_BIG); 272 base::StringPrintf("file '%s'", file_path.value().c_str()));
273 if (!res) {
274 DCHECK(status != POLICY_LOAD_STATUS_SIZE);
275 status_sample->Add(status);
276 }
277 return res;
278 }
279
280 POLICY_EXPORT bool ReadDataInternal(const uint8_t* data,
brucedawson 2017/04/20 18:18:12 Having a function argument named |data| is confusi
281 size_t data_size,
282 const base::string16& root,
283 RegistryDict* dict,
284 PolicyLoadStatus* status,
285 const std::string& debug_name) {
286 DCHECK(status);
287
288 // Check data size.
289 if (data_size > kMaxPRegFileSize) {
290 LOG(ERROR) << "PReg " << debug_name << " too large: " << data_size;
291 *status = POLICY_LOAD_STATUS_TOO_BIG;
274 return false; 292 return false;
275 } 293 }
276 294
277 // Check the header. 295 // Check the header.
278 const int kHeaderSize = arraysize(kPRegFileHeader); 296 const int kHeaderSize = arraysize(kPRegFileHeader);
279 if (mapped_file.length() < kHeaderSize || 297 if (!data || data_size < kHeaderSize ||
280 memcmp(kPRegFileHeader, mapped_file.data(), kHeaderSize) != 0) { 298 memcmp(kPRegFileHeader, data, kHeaderSize) != 0) {
281 LOG(ERROR) << "Bad policy file " << file_path.value(); 299 LOG(ERROR) << "Bad PReg " << debug_name;
282 status->Add(POLICY_LOAD_STATUS_PARSE_ERROR); 300 *status = POLICY_LOAD_STATUS_PARSE_ERROR;
283 return false; 301 return false;
284 } 302 }
285 303
286 // Parse file contents, which is UCS-2 and little-endian. The latter I 304 // Parse data, which is expected to be UCS-2 and little-endian. The latter I
287 // couldn't find documentation on, but the example I saw were all 305 // couldn't find documentation on, but the example I saw were all
288 // little-endian. It'd be interesting to check on big-endian hardware. 306 // little-endian. It'd be interesting to check on big-endian hardware.
289 const uint8_t* cursor = mapped_file.data() + kHeaderSize; 307 const uint8_t* cursor = data + kHeaderSize;
290 const uint8_t* end = mapped_file.data() + mapped_file.length(); 308 const uint8_t* end = data + data_size;
291 while (true) { 309 while (true) {
292 if (cursor == end) 310 if (cursor == end)
293 return true; 311 return true;
294 312
295 if (NextChar(&cursor, end) != kDelimBracketOpen) 313 if (NextChar(&cursor, end) != kDelimBracketOpen)
296 break; 314 break;
297 315
298 // Read the record fields. 316 // Read the record fields.
299 base::string16 key_name; 317 base::string16 key_name;
300 base::string16 value; 318 base::string16 value;
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
334 } 352 }
335 353
336 if (current != kDelimBracketClose) 354 if (current != kDelimBracketClose)
337 break; 355 break;
338 356
339 // Process the record if it is within the |root| subtree. 357 // Process the record if it is within the |root| subtree.
340 if (base::StartsWith(key_name, root, base::CompareCase::INSENSITIVE_ASCII)) 358 if (base::StartsWith(key_name, root, base::CompareCase::INSENSITIVE_ASCII))
341 HandleRecord(key_name.substr(root.size()), value, type, data, dict); 359 HandleRecord(key_name.substr(root.size()), value, type, data, dict);
342 } 360 }
343 361
344 LOG(ERROR) << "Error parsing " << file_path.value() << " at offset " 362 LOG(ERROR) << "Error parsing PReg " << debug_name << " at offset "
345 << reinterpret_cast<const uint8_t*>(cursor - 1) - 363 << (reinterpret_cast<const uint8_t*>(cursor - 1) - data);
346 mapped_file.data(); 364 *status = POLICY_LOAD_STATUS_PARSE_ERROR;
347 status->Add(POLICY_LOAD_STATUS_PARSE_ERROR);
348 return false; 365 return false;
349 } 366 }
350 367
351 } // namespace preg_parser 368 } // namespace preg_parser
352 } // namespace policy 369 } // namespace policy
OLDNEW
« no previous file with comments | « components/policy/core/common/preg_parser.h ('k') | components/policy/core/common/preg_parser_fuzzer.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698