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

Side by Side Diff: chrome/browser/policy/preg_parser_win.cc

Issue 13441008: Add parser for PReg files. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 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 | Annotate | Revision Log
OLDNEW
(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 <windows.h>
8
9 #include "base/basictypes.h"
10 #include "base/files/file_path.h"
11 #include "base/files/memory_mapped_file.h"
12 #include "base/logging.h"
13 #include "base/stl_util.h"
14 #include "base/string16.h"
15 #include "base/string_util.h"
16 #include "base/sys_byteorder.h"
17 #include "base/utf_string_conversions.h"
18 #include "base/values.h"
19
20 namespace policy {
21 namespace preg_parser {
22
23 // The magic header in PReg files: ASCII "PReg" + version (0x0001).
24 const char kPolicyRegistryFileHeader[] = "PReg\x01\x00\x00\x00";
25
26 // Constants for PReg file delimiters.
27 #if defined(ARCH_CPU_LITTLE_ENDIAN)
28 #define BYTE_SWAP_TO_LE_16(x) (x)
29 #else
30 #define BYTE_SWAP_TO_LE_16(x) ((((x) & 0xff00) >> 8) | (((x) & 0x00ff) << 8))
31 #endif
32 const char16 kDelimBracketOpen = BYTE_SWAP_TO_LE_16(L'[');
33 const char16 kDelimBracketClose = BYTE_SWAP_TO_LE_16(L']');
34 const char16 kDelimSemicolon = BYTE_SWAP_TO_LE_16(L';');
35 #undef BYTE_SWAP_TO_LE_16
36
37 // Magic strings for the PReg value field to trigger special actions.
38 const char kActionTriggerPrefix[] = "**";
39 const char kActionTriggerDeleteValues[] = "deletevalues";
40 const char kActionTriggerDel[] = "del.";
41 const char kActionTriggerDelVals[] = "delvals";
42 const char kActionTriggerDeleteKeys[] = "deletekeys";
43 const char kActionTriggerSecureKey[] = "securekey";
44 const char kActionTriggerSoft[] = "soft";
45
46 // Swaps all characters of a string16 from LE to host byte order.
47 void String16LEToHost(string16* str) {
48 for (string16::iterator c(str->begin()); c != str->end(); ++c)
49 *c = base::ByteSwapToLE16(*c);
50 }
51
52 // Reads a fixed-size field from a PReg file.
53 bool ReadFieldBinary(const char16** cursor,
54 const char16* end,
55 int size,
56 uint8* data) {
57 const uint8* start_bytes = reinterpret_cast<const uint8*>(*cursor);
58 const uint8* end_bytes = start_bytes + size;
59 if (end_bytes >= reinterpret_cast<const uint8*>(end))
60 return false;
61 std::copy(start_bytes, end_bytes, data);
62 *cursor = reinterpret_cast<const char16*>(end_bytes);
Joao da Silva 2013/04/05 12:51:33 Unfortunately the documentation doesn't mention wh
Mattias Nissler (ping if slow) 2013/04/05 18:12:50 Standard says this is implementation-defined. I've
63 return true;
64 }
65
66 bool ReadField32(const char16** cursor, const char16* end, uint32* data) {
67 uint32 value = 0;
68 if (!ReadFieldBinary(cursor, end, sizeof(uint32),
69 reinterpret_cast<uint8*>(&value))) {
70 return false;
71 }
72 *data = base::ByteSwapToLE32(value);
73 return true;
74 }
75
76 // Reads a string field from a file.
77 bool ReadFieldString(const char16** cursor, const char16* end, string16* str) {
78 const char16* key_start = *cursor;
79 for (; *cursor < end; ++*cursor) {
80 if (**cursor == 0x0000) {
81 str->assign(key_start, *cursor);
82 String16LEToHost(str);
83 ++*cursor;
84 return true;
85 }
86 }
87 return false;
88 }
89
90 std::string DecodePRegStringValue(const std::vector<uint8>& data) {
91 // Subtract one to account for the trailing null character.
92 const size_t len = std::max(0U, (data.size() / sizeof(char16)) - 1);
Joao da Silva 2013/04/05 12:51:33 If data.size() is 0 then the 2nd arg to max() beco
Mattias Nissler (ping if slow) 2013/04/05 18:12:50 Done.
93 const char16* chars = reinterpret_cast<const char16*>(vector_as_array(&data));
94 return UTF16ToUTF8(string16(chars, len));
95 }
96
97 // Decodes a value from a PReg file given as a uint8 vector.
98 bool DecodePRegValue(uint32 type,
99 const std::vector<uint8>& data,
100 scoped_ptr<base::Value>* value) {
101 switch (type) {
102 case REG_SZ:
103 case REG_EXPAND_SZ:
104 value->reset(base::Value::CreateStringValue(DecodePRegStringValue(data)));
105 return true;
106 case REG_DWORD_LITTLE_ENDIAN:
107 case REG_DWORD_BIG_ENDIAN:
108 if (data.size() == sizeof(uint32)) {
109 uint32 val = *reinterpret_cast<const uint32*>(vector_as_array(&data));
110 if (type == REG_DWORD_BIG_ENDIAN)
111 val = base::NetToHost32(val);
112 else
113 val = base::ByteSwapToLE32(val);
114 value->reset(base::Value::CreateIntegerValue(static_cast<int>(val)));
115 return true;
116 } else {
117 LOG(ERROR) << "Bad data size " << data.size();
118 }
119 break;
120 case REG_NONE:
121 case REG_LINK:
122 case REG_MULTI_SZ:
123 case REG_RESOURCE_LIST:
124 case REG_FULL_RESOURCE_DESCRIPTOR:
125 case REG_RESOURCE_REQUIREMENTS_LIST:
126 case REG_QWORD_LITTLE_ENDIAN:
127 default:
Joao da Silva 2013/04/05 12:51:33 Why have some cases and a default, and not just de
Mattias Nissler (ping if slow) 2013/04/05 18:12:50 To document that we're purposely not implementing
128 LOG(ERROR) << "Unsupported registry data type " << type;
129 }
130
131 return false;
132 }
133
134 // Adds the record data passed via parameters to |dict| in case the data is
135 // relevant policy for Chromium.
136 void HandleRecord(const string16& key_name,
137 const string16& value,
138 uint32 type,
139 const std::vector<uint8>& data,
140 base::DictionaryValue* dict) {
141 // Locate/create the dictionary to place the value in.
142 std::vector<std::string> path;
143 Tokenize(UTF16ToUTF8(key_name), "\\", &path);
144 for (std::vector<std::string>::const_iterator entry(path.begin());
145 entry != path.end(); ++entry) {
146 base::DictionaryValue* subdict = NULL;
147 if (!dict->GetDictionaryWithoutPathExpansion(*entry, &subdict) ||
148 !subdict) {
149 subdict = new DictionaryValue();
150 dict->SetWithoutPathExpansion(*entry, subdict);
151 }
152 dict = subdict;
153 }
154
155 if (value.empty())
156 return;
157
158 std::string value_name(UTF16ToUTF8(value));
159 if (!StartsWithASCII(value_name, kActionTriggerPrefix, true)) {
160 scoped_ptr<base::Value> value;
161 if (DecodePRegValue(type, data, &value))
162 dict->Set(value_name, value.release());
163 return;
164 }
165
166 std::string action_trigger(StringToLowerASCII(value_name.substr(
167 arraysize(kActionTriggerPrefix) - 1)));
168 if (action_trigger == kActionTriggerDeleteValues ||
169 StartsWithASCII(kActionTriggerDeleteKeys, action_trigger, true)) {
Joao da Silva 2013/04/05 12:51:33 The first 2 args to StartsWithASCII are swapped
Mattias Nissler (ping if slow) 2013/04/05 18:12:50 Good catch. Done.
170 std::vector<std::string> keys;
171 Tokenize(DecodePRegStringValue(data), ";", &keys);
172 for (std::vector<std::string>::const_iterator key(keys.begin());
173 key != keys.end(); ++key) {
174 dict->RemoveWithoutPathExpansion(*key, NULL);
175 }
176 } else if (StartsWithASCII(action_trigger, kActionTriggerDel, true)) {
177 dict->RemoveWithoutPathExpansion(
178 value_name.substr(arraysize(kActionTriggerPrefix) - 1 +
pastarmovj 2013/04/05 08:41:43 Isn't here one -1 too much? I suspect this worked
Mattias Nissler (ping if slow) 2013/04/05 18:12:50 Note that the -1 here is just for taking the termi
179 arraysize(kActionTriggerDel) - 1),
180 NULL);
181 } else if (StartsWithASCII(action_trigger, kActionTriggerDelVals, true)) {
182 dict->Clear();
Joao da Silva 2013/04/05 12:51:33 The documentation of DelVals says that it deletes
Mattias Nissler (ping if slow) 2013/04/05 18:12:50 Interesting edge case :) Fixed.
183 } else if (StartsWithASCII(action_trigger, kActionTriggerSecureKey, true) ||
184 StartsWithASCII(action_trigger, kActionTriggerSoft, true)) {
Joao da Silva 2013/04/05 12:51:33 Where did you find about "Soft"? The linked docume
Mattias Nissler (ping if slow) 2013/04/05 18:12:50 Apparently Microsoft used this at some point. I fo
185 // Doesn't affect values.
186 } else {
187 LOG(ERROR) << "Bad action trigger " << value_name;
188 }
189 }
190
191 bool ReadFile(const base::FilePath& file_path,
192 base::DictionaryValue* dict) {
193 base::MemoryMappedFile mapped_file;
194
195 if (!mapped_file.Initialize(file_path) || !mapped_file.IsValid()) {
196 PLOG(ERROR) << "Failed to map " << file_path.value();
197 return false;
198 }
199
200 // Check the header.
201 const int kHeaderSize = arraysize(kPolicyRegistryFileHeader) - 1;
202 if (mapped_file.length() < kHeaderSize ||
203 memcmp(kPolicyRegistryFileHeader, mapped_file.data(), kHeaderSize) != 0) {
204 LOG(ERROR) << "Bad policy file " << file_path.value();
205 return false;
206 }
207
208 // Parse file contents, which is UCS-2 and little-endian. The latter I
209 // couldn't find documentation on, but the example I saw were all
210 // little-endian. It'd be interesting to check on big-endian hardware.
211 //
212 // The file format is documented here:
213 // http://msdn.microsoft.com/en-us/library/windows/desktop/aa374407(v=vs.85).a spx
pastarmovj 2013/04/05 08:41:43 you can skip microsoft. and the address is still v
Joao da Silva 2013/04/05 12:51:33 Suggestion: point to the documentation at the top
Mattias Nissler (ping if slow) 2013/04/05 18:12:50 That redirects though, and the URL I have seems to
Mattias Nissler (ping if slow) 2013/04/05 18:12:50 Done.
214 const char16* cursor = reinterpret_cast<const char16*>(
215 mapped_file.data() + kHeaderSize);
216 const char16* end = reinterpret_cast<const char16*>(
217 mapped_file.data() + mapped_file.length());
Joao da Silva 2013/04/05 12:51:33 What happens if the file ends in a byte i.e. half
Mattias Nissler (ping if slow) 2013/04/05 18:12:50 Fixed.
218 bool parse_error = true;
219 while (cursor < end) {
220 if (*cursor++ != kDelimBracketOpen)
221 break;
222
223 // Read the record fields.
224 string16 key_name;
225 string16 value;
226 uint32 type = 0;
227 uint32 size = 0;
228 std::vector<uint8> data;
229
230 if (!ReadFieldString(&cursor, end, &key_name))
231 break;
232
233 if (*cursor == kDelimSemicolon) {
234 ++cursor;
235 if (!ReadFieldString(&cursor, end, &value))
236 break;
237 }
238
239 if (*cursor == kDelimSemicolon) {
240 ++cursor;
241 if (!ReadField32(&cursor, end, &type))
242 break;
243 }
244
245 if (*cursor == kDelimSemicolon) {
246 ++cursor;
247 if (!ReadField32(&cursor, end, &size))
248 break;
249 }
250
251 if (*cursor == kDelimSemicolon) {
252 ++cursor;
253 data.resize(size);
Joao da Silva 2013/04/05 12:51:33 We should impose a size limit, otherwise borked fi
Mattias Nissler (ping if slow) 2013/04/05 18:12:50 Done.
254 if (!ReadFieldBinary(&cursor, end, size, vector_as_array(&data)))
255 break;
256 }
257
258 if (*cursor != kDelimBracketClose)
259 break;
260 ++cursor;
261
262 HandleRecord(key_name, value, type, data, dict);
263
264 if (cursor == end) {
265 parse_error = false;
266 break;
pastarmovj 2013/04/05 08:41:43 Why not directly return true here?
Mattias Nissler (ping if slow) 2013/04/05 18:12:50 Good idea, done.
267 }
268 }
269
270 if (parse_error) {
pastarmovj 2013/04/05 08:41:43 ...then you can simply assume you encountered an e
Mattias Nissler (ping if slow) 2013/04/05 18:12:50 Done.
271 LOG(ERROR) << "Error parsing " << file_path.value() << " at offset "
272 << reinterpret_cast<const uint8*>(cursor) - mapped_file.data();
273 }
274
275 return !parse_error;
276 }
277
278 } // namespace preg_parser
279 } // namespace policy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698