OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/policy/preg_parser_win.h" |
| 6 |
| 7 #include <algorithm> |
| 8 #include <iterator> |
| 9 |
| 10 #include <windows.h> |
| 11 |
| 12 #include "base/basictypes.h" |
| 13 #include "base/files/file_path.h" |
| 14 #include "base/files/memory_mapped_file.h" |
| 15 #include "base/logging.h" |
| 16 #include "base/stl_util.h" |
| 17 #include "base/string16.h" |
| 18 #include "base/string_util.h" |
| 19 #include "base/sys_byteorder.h" |
| 20 #include "base/utf_string_conversions.h" |
| 21 #include "base/values.h" |
| 22 |
| 23 namespace policy { |
| 24 namespace preg_parser { |
| 25 |
| 26 // The magic header in PReg files: ASCII "PReg" + version (0x0001). |
| 27 const char kPolicyRegistryFileHeader[] = "PReg\x01\x00\x00\x00"; |
| 28 |
| 29 // Maximum PReg file size we're willing to accept. |
| 30 const int64 kMaxPRegFileSize = 1024 * 1024 * 16; |
| 31 |
| 32 // Constants for PReg file delimiters. |
| 33 const char16 kDelimBracketOpen = L'['; |
| 34 const char16 kDelimBracketClose = L']'; |
| 35 const char16 kDelimSemicolon = L';'; |
| 36 |
| 37 // Registry path separator. |
| 38 const char16 kRegistryPathSeparator[] = L"\\"; |
| 39 |
| 40 // Magic strings for the PReg value field to trigger special actions. |
| 41 const char kActionTriggerPrefix[] = "**"; |
| 42 const char kActionTriggerDeleteValues[] = "deletevalues"; |
| 43 const char kActionTriggerDel[] = "del."; |
| 44 const char kActionTriggerDelVals[] = "delvals"; |
| 45 const char kActionTriggerDeleteKeys[] = "deletekeys"; |
| 46 const char kActionTriggerSecureKey[] = "securekey"; |
| 47 const char kActionTriggerSoft[] = "soft"; |
| 48 |
| 49 // Returns the character at |cursor| and increments it, unless the end is here |
| 50 // in which case -1 is returned. |
| 51 int NextChar(const uint8** cursor, const uint8* end) { |
| 52 // Only read the character if a full char16 is available. |
| 53 if (*cursor + sizeof(char16) > end) |
| 54 return -1; |
| 55 |
| 56 int result = **cursor | (*(*cursor + 1) << 8); |
| 57 *cursor += sizeof(char16); |
| 58 return result; |
| 59 } |
| 60 |
| 61 // Reads a fixed-size field from a PReg file. |
| 62 bool ReadFieldBinary(const uint8** cursor, |
| 63 const uint8* end, |
| 64 int size, |
| 65 uint8* data) { |
| 66 const uint8* field_end = *cursor + size; |
| 67 if (field_end > end) |
| 68 return false; |
| 69 std::copy(*cursor, field_end, data); |
| 70 *cursor = field_end; |
| 71 return true; |
| 72 } |
| 73 |
| 74 bool ReadField32(const uint8** cursor, const uint8* end, uint32* data) { |
| 75 uint32 value = 0; |
| 76 if (!ReadFieldBinary(cursor, end, sizeof(uint32), |
| 77 reinterpret_cast<uint8*>(&value))) { |
| 78 return false; |
| 79 } |
| 80 *data = base::ByteSwapToLE32(value); |
| 81 return true; |
| 82 } |
| 83 |
| 84 // Reads a string field from a file. |
| 85 bool ReadFieldString(const uint8** cursor, const uint8* end, string16* str) { |
| 86 int current = -1; |
| 87 while ((current = NextChar(cursor, end)) > 0x0000) |
| 88 *str += current; |
| 89 |
| 90 return current == L'\0'; |
| 91 } |
| 92 |
| 93 std::string DecodePRegStringValue(const std::vector<uint8>& data) { |
| 94 size_t len = data.size() / sizeof(char16); |
| 95 if (len <= 0) |
| 96 return std::string(); |
| 97 |
| 98 const char16* chars = reinterpret_cast<const char16*>(vector_as_array(&data)); |
| 99 string16 result; |
| 100 std::transform(chars, chars + len - 1, std::back_inserter(result), |
| 101 std::ptr_fun(base::ByteSwapToLE16)); |
| 102 return UTF16ToUTF8(result); |
| 103 } |
| 104 |
| 105 // Decodes a value from a PReg file given as a uint8 vector. |
| 106 bool DecodePRegValue(uint32 type, |
| 107 const std::vector<uint8>& data, |
| 108 scoped_ptr<base::Value>* value) { |
| 109 switch (type) { |
| 110 case REG_SZ: |
| 111 case REG_EXPAND_SZ: |
| 112 value->reset(base::Value::CreateStringValue(DecodePRegStringValue(data))); |
| 113 return true; |
| 114 case REG_DWORD_LITTLE_ENDIAN: |
| 115 case REG_DWORD_BIG_ENDIAN: |
| 116 if (data.size() == sizeof(uint32)) { |
| 117 uint32 val = *reinterpret_cast<const uint32*>(vector_as_array(&data)); |
| 118 if (type == REG_DWORD_BIG_ENDIAN) |
| 119 val = base::NetToHost32(val); |
| 120 else |
| 121 val = base::ByteSwapToLE32(val); |
| 122 value->reset(base::Value::CreateIntegerValue(static_cast<int>(val))); |
| 123 return true; |
| 124 } else { |
| 125 LOG(ERROR) << "Bad data size " << data.size(); |
| 126 } |
| 127 break; |
| 128 case REG_NONE: |
| 129 case REG_LINK: |
| 130 case REG_MULTI_SZ: |
| 131 case REG_RESOURCE_LIST: |
| 132 case REG_FULL_RESOURCE_DESCRIPTOR: |
| 133 case REG_RESOURCE_REQUIREMENTS_LIST: |
| 134 case REG_QWORD_LITTLE_ENDIAN: |
| 135 default: |
| 136 LOG(ERROR) << "Unsupported registry data type " << type; |
| 137 } |
| 138 |
| 139 return false; |
| 140 } |
| 141 |
| 142 // Adds the record data passed via parameters to |dict| in case the data is |
| 143 // relevant policy for Chromium. |
| 144 void HandleRecord(const string16& key_name, |
| 145 const string16& value, |
| 146 uint32 type, |
| 147 const std::vector<uint8>& data, |
| 148 base::DictionaryValue* dict) { |
| 149 // Locate/create the dictionary to place the value in. |
| 150 std::vector<string16> path; |
| 151 |
| 152 Tokenize(key_name, kRegistryPathSeparator, &path); |
| 153 for (std::vector<string16>::const_iterator entry(path.begin()); |
| 154 entry != path.end(); ++entry) { |
| 155 if (entry->empty()) |
| 156 continue; |
| 157 base::DictionaryValue* subdict = NULL; |
| 158 const std::string name = UTF16ToUTF8(*entry); |
| 159 if (!dict->GetDictionaryWithoutPathExpansion(name, &subdict) || |
| 160 !subdict) { |
| 161 subdict = new DictionaryValue(); |
| 162 dict->SetWithoutPathExpansion(name, subdict); |
| 163 } |
| 164 dict = subdict; |
| 165 } |
| 166 |
| 167 if (value.empty()) |
| 168 return; |
| 169 |
| 170 std::string value_name(UTF16ToUTF8(value)); |
| 171 if (!StartsWithASCII(value_name, kActionTriggerPrefix, true)) { |
| 172 scoped_ptr<base::Value> value; |
| 173 if (DecodePRegValue(type, data, &value)) |
| 174 dict->Set(value_name, value.release()); |
| 175 return; |
| 176 } |
| 177 |
| 178 std::string action_trigger(StringToLowerASCII(value_name.substr( |
| 179 arraysize(kActionTriggerPrefix) - 1))); |
| 180 if (action_trigger == kActionTriggerDeleteValues || |
| 181 StartsWithASCII(action_trigger, kActionTriggerDeleteKeys, true)) { |
| 182 std::vector<std::string> keys; |
| 183 Tokenize(DecodePRegStringValue(data), ";", &keys); |
| 184 for (std::vector<std::string>::const_iterator key(keys.begin()); |
| 185 key != keys.end(); ++key) { |
| 186 dict->RemoveWithoutPathExpansion(*key, NULL); |
| 187 } |
| 188 } else if (StartsWithASCII(action_trigger, kActionTriggerDel, true)) { |
| 189 dict->RemoveWithoutPathExpansion( |
| 190 value_name.substr(arraysize(kActionTriggerPrefix) - 1 + |
| 191 arraysize(kActionTriggerDel) - 1), |
| 192 NULL); |
| 193 } else if (StartsWithASCII(action_trigger, kActionTriggerDelVals, true)) { |
| 194 // Delete all values, but keep keys (i.e. retain dictionary entries). |
| 195 base::DictionaryValue new_dict; |
| 196 for (base::DictionaryValue::Iterator it(*dict); it.HasNext(); |
| 197 it.Advance()) { |
| 198 base::DictionaryValue* subdict = NULL; |
| 199 if (dict->GetDictionaryWithoutPathExpansion(it.key(), &subdict)) { |
| 200 scoped_ptr<base::DictionaryValue> new_subdict( |
| 201 new base::DictionaryValue()); |
| 202 new_subdict->Swap(subdict); |
| 203 new_dict.Set(it.key(), new_subdict.release()); |
| 204 } |
| 205 } |
| 206 dict->Swap(&new_dict); |
| 207 } else if (StartsWithASCII(action_trigger, kActionTriggerSecureKey, true) || |
| 208 StartsWithASCII(action_trigger, kActionTriggerSoft, true)) { |
| 209 // Doesn't affect values. |
| 210 } else { |
| 211 LOG(ERROR) << "Bad action trigger " << value_name; |
| 212 } |
| 213 } |
| 214 |
| 215 bool ReadFile(const base::FilePath& file_path, |
| 216 const string16& root, |
| 217 base::DictionaryValue* dict) { |
| 218 base::MemoryMappedFile mapped_file; |
| 219 if (!mapped_file.Initialize(file_path) || !mapped_file.IsValid()) { |
| 220 PLOG(ERROR) << "Failed to map " << file_path.value(); |
| 221 return false; |
| 222 } |
| 223 |
| 224 if (mapped_file.length() > kMaxPRegFileSize) { |
| 225 LOG(ERROR) << "PReg file " << file_path.value() << " too large: " |
| 226 << mapped_file.length(); |
| 227 return false; |
| 228 } |
| 229 |
| 230 // Check the header. |
| 231 const int kHeaderSize = arraysize(kPolicyRegistryFileHeader) - 1; |
| 232 if (mapped_file.length() < kHeaderSize || |
| 233 memcmp(kPolicyRegistryFileHeader, mapped_file.data(), kHeaderSize) != 0) { |
| 234 LOG(ERROR) << "Bad policy file " << file_path.value(); |
| 235 return false; |
| 236 } |
| 237 |
| 238 // Parse file contents, which is UCS-2 and little-endian. The latter I |
| 239 // couldn't find documentation on, but the example I saw were all |
| 240 // little-endian. It'd be interesting to check on big-endian hardware. |
| 241 const uint8* cursor = mapped_file.data() + kHeaderSize; |
| 242 const uint8* end = mapped_file.data() + mapped_file.length(); |
| 243 while (true) { |
| 244 if (cursor == end) |
| 245 return true; |
| 246 |
| 247 if (NextChar(&cursor, end) != kDelimBracketOpen) |
| 248 break; |
| 249 |
| 250 // Read the record fields. |
| 251 string16 key_name; |
| 252 string16 value; |
| 253 uint32 type = 0; |
| 254 uint32 size = 0; |
| 255 std::vector<uint8> data; |
| 256 |
| 257 if (!ReadFieldString(&cursor, end, &key_name)) |
| 258 break; |
| 259 |
| 260 int current = NextChar(&cursor, end); |
| 261 if (current == kDelimSemicolon) { |
| 262 if (!ReadFieldString(&cursor, end, &value)) |
| 263 break; |
| 264 current = NextChar(&cursor, end); |
| 265 } |
| 266 |
| 267 if (current == kDelimSemicolon) { |
| 268 if (!ReadField32(&cursor, end, &type)) |
| 269 break; |
| 270 current = NextChar(&cursor, end); |
| 271 } |
| 272 |
| 273 if (current == kDelimSemicolon) { |
| 274 if (!ReadField32(&cursor, end, &size)) |
| 275 break; |
| 276 current = NextChar(&cursor, end); |
| 277 } |
| 278 |
| 279 if (current == kDelimSemicolon) { |
| 280 if (size > kMaxPRegFileSize) |
| 281 break; |
| 282 data.resize(size); |
| 283 if (!ReadFieldBinary(&cursor, end, size, vector_as_array(&data))) |
| 284 break; |
| 285 current = NextChar(&cursor, end); |
| 286 } |
| 287 |
| 288 if (current != kDelimBracketClose) |
| 289 break; |
| 290 |
| 291 // Process the record if it is within the |root| subtree. |
| 292 if (StartsWith(key_name, root, false)) |
| 293 HandleRecord(key_name.substr(root.size()), value, type, data, dict); |
| 294 } |
| 295 |
| 296 LOG(ERROR) << "Error parsing " << file_path.value() << " at offset " |
| 297 << reinterpret_cast<const uint8*>(cursor - 1) - mapped_file.data(); |
| 298 return false; |
| 299 } |
| 300 |
| 301 } // namespace preg_parser |
| 302 } // namespace policy |
OLD | NEW |