1 | /* |
---|
2 | * Branched from Samba project Subversion repository, version #7470: |
---|
3 | * http://viewcvs.samba.org/cgi-bin/viewcvs.cgi/trunk/source/registry/regfio.c?rev=7470&view=auto |
---|
4 | * |
---|
5 | * Windows NT (and later) registry parsing library |
---|
6 | * |
---|
7 | * Copyright (C) 2005-2009 Timothy D. Morgan |
---|
8 | * Copyright (C) 2005 Gerald (Jerry) Carter |
---|
9 | * |
---|
10 | * This program is free software; you can redistribute it and/or modify |
---|
11 | * it under the terms of the GNU General Public License as published by |
---|
12 | * the Free Software Foundation; version 3 of the License. |
---|
13 | * |
---|
14 | * This program is distributed in the hope that it will be useful, |
---|
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
17 | * GNU General Public License for more details. |
---|
18 | * |
---|
19 | * You should have received a copy of the GNU General Public License |
---|
20 | * along with this program; if not, write to the Free Software |
---|
21 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
22 | * |
---|
23 | * $Id: regfi.c 143 2009-02-13 03:24:27Z tim $ |
---|
24 | */ |
---|
25 | |
---|
26 | #include "../include/regfi.h" |
---|
27 | |
---|
28 | |
---|
29 | /* Registry types mapping */ |
---|
30 | const unsigned int regfi_num_reg_types = 12; |
---|
31 | static const char* regfi_type_names[] = |
---|
32 | {"NONE", "SZ", "EXPAND_SZ", "BINARY", "DWORD", "DWORD_BE", "LINK", |
---|
33 | "MULTI_SZ", "RSRC_LIST", "RSRC_DESC", "RSRC_REQ_LIST", "QWORD"}; |
---|
34 | |
---|
35 | |
---|
36 | |
---|
37 | /****************************************************************************** |
---|
38 | ******************************************************************************/ |
---|
39 | void regfi_add_message(REGFI_FILE* file, uint16 msg_type, const char* fmt, ...) |
---|
40 | { |
---|
41 | /* XXX: This function is not particularly efficient, |
---|
42 | * but then it is mostly used during errors. |
---|
43 | */ |
---|
44 | uint32 buf_size, buf_used; |
---|
45 | char* new_msg; |
---|
46 | va_list args; |
---|
47 | |
---|
48 | if((file->msg_mask & msg_type) != 0) |
---|
49 | { |
---|
50 | if(file->last_message == NULL) |
---|
51 | buf_used = 0; |
---|
52 | else |
---|
53 | buf_used = strlen(file->last_message); |
---|
54 | |
---|
55 | buf_size = buf_used+strlen(fmt)+160; |
---|
56 | new_msg = realloc(file->last_message, buf_size); |
---|
57 | if(new_msg == NULL) |
---|
58 | /* XXX: should we report this? */ |
---|
59 | return; |
---|
60 | |
---|
61 | switch (msg_type) |
---|
62 | { |
---|
63 | case REGFI_MSG_INFO: |
---|
64 | strcpy(new_msg+buf_used, "INFO: "); |
---|
65 | buf_used += 6; |
---|
66 | break; |
---|
67 | case REGFI_MSG_WARN: |
---|
68 | strcpy(new_msg+buf_used, "WARN: "); |
---|
69 | buf_used += 6; |
---|
70 | break; |
---|
71 | case REGFI_MSG_ERROR: |
---|
72 | strcpy(new_msg+buf_used, "ERROR: "); |
---|
73 | buf_used += 7; |
---|
74 | break; |
---|
75 | } |
---|
76 | |
---|
77 | va_start(args, fmt); |
---|
78 | vsnprintf(new_msg+buf_used, buf_size-buf_used, fmt, args); |
---|
79 | va_end(args); |
---|
80 | strncat(new_msg, "\n", buf_size-1); |
---|
81 | |
---|
82 | file->last_message = new_msg; |
---|
83 | } |
---|
84 | } |
---|
85 | |
---|
86 | |
---|
87 | /****************************************************************************** |
---|
88 | ******************************************************************************/ |
---|
89 | char* regfi_get_messages(REGFI_FILE* file) |
---|
90 | { |
---|
91 | char* ret_val = file->last_message; |
---|
92 | file->last_message = NULL; |
---|
93 | |
---|
94 | return ret_val; |
---|
95 | } |
---|
96 | |
---|
97 | |
---|
98 | void regfi_set_message_mask(REGFI_FILE* file, uint16 mask) |
---|
99 | { |
---|
100 | file->msg_mask = mask; |
---|
101 | } |
---|
102 | |
---|
103 | |
---|
104 | /* Returns NULL on error */ |
---|
105 | const char* regfi_type_val2str(unsigned int val) |
---|
106 | { |
---|
107 | if(val == REG_KEY) |
---|
108 | return "KEY"; |
---|
109 | |
---|
110 | if(val >= regfi_num_reg_types) |
---|
111 | return NULL; |
---|
112 | |
---|
113 | return regfi_type_names[val]; |
---|
114 | } |
---|
115 | |
---|
116 | |
---|
117 | /* Returns -1 on error */ |
---|
118 | int regfi_type_str2val(const char* str) |
---|
119 | { |
---|
120 | int i; |
---|
121 | |
---|
122 | if(strcmp("KEY", str) == 0) |
---|
123 | return REG_KEY; |
---|
124 | |
---|
125 | for(i=0; i < regfi_num_reg_types; i++) |
---|
126 | if (strcmp(regfi_type_names[i], str) == 0) |
---|
127 | return i; |
---|
128 | |
---|
129 | if(strcmp("DWORD_LE", str) == 0) |
---|
130 | return REG_DWORD_LE; |
---|
131 | |
---|
132 | return -1; |
---|
133 | } |
---|
134 | |
---|
135 | |
---|
136 | /* Security descriptor formatting functions */ |
---|
137 | |
---|
138 | const char* regfi_ace_type2str(uint8 type) |
---|
139 | { |
---|
140 | static const char* map[7] |
---|
141 | = {"ALLOW", "DENY", "AUDIT", "ALARM", |
---|
142 | "ALLOW CPD", "OBJ ALLOW", "OBJ DENY"}; |
---|
143 | if(type < 7) |
---|
144 | return map[type]; |
---|
145 | else |
---|
146 | /* XXX: would be nice to return the unknown integer value. |
---|
147 | * However, as it is a const string, it can't be free()ed later on, |
---|
148 | * so that would need to change. |
---|
149 | */ |
---|
150 | return "UNKNOWN"; |
---|
151 | } |
---|
152 | |
---|
153 | |
---|
154 | /* XXX: need a better reference on the meaning of each flag. */ |
---|
155 | /* For more info, see: |
---|
156 | * http://msdn2.microsoft.com/en-us/library/aa772242.aspx |
---|
157 | */ |
---|
158 | char* regfi_ace_flags2str(uint8 flags) |
---|
159 | { |
---|
160 | static const char* flag_map[32] = |
---|
161 | { "OI", /* Object Inherit */ |
---|
162 | "CI", /* Container Inherit */ |
---|
163 | "NP", /* Non-Propagate */ |
---|
164 | "IO", /* Inherit Only */ |
---|
165 | "IA", /* Inherited ACE */ |
---|
166 | NULL, |
---|
167 | NULL, |
---|
168 | NULL, |
---|
169 | }; |
---|
170 | |
---|
171 | char* ret_val = malloc(35*sizeof(char)); |
---|
172 | char* fo = ret_val; |
---|
173 | uint32 i; |
---|
174 | uint8 f; |
---|
175 | |
---|
176 | if(ret_val == NULL) |
---|
177 | return NULL; |
---|
178 | |
---|
179 | fo[0] = '\0'; |
---|
180 | if (!flags) |
---|
181 | return ret_val; |
---|
182 | |
---|
183 | for(i=0; i < 8; i++) |
---|
184 | { |
---|
185 | f = (1<<i); |
---|
186 | if((flags & f) && (flag_map[i] != NULL)) |
---|
187 | { |
---|
188 | strcpy(fo, flag_map[i]); |
---|
189 | fo += strlen(flag_map[i]); |
---|
190 | *(fo++) = ' '; |
---|
191 | flags ^= f; |
---|
192 | } |
---|
193 | } |
---|
194 | |
---|
195 | /* Any remaining unknown flags are added at the end in hex. */ |
---|
196 | if(flags != 0) |
---|
197 | sprintf(fo, "0x%.2X ", flags); |
---|
198 | |
---|
199 | /* Chop off the last space if we've written anything to ret_val */ |
---|
200 | if(fo != ret_val) |
---|
201 | fo[-1] = '\0'; |
---|
202 | |
---|
203 | return ret_val; |
---|
204 | } |
---|
205 | |
---|
206 | |
---|
207 | char* regfi_ace_perms2str(uint32 perms) |
---|
208 | { |
---|
209 | uint32 i, p; |
---|
210 | /* This is more than is needed by a fair margin. */ |
---|
211 | char* ret_val = malloc(350*sizeof(char)); |
---|
212 | char* r = ret_val; |
---|
213 | |
---|
214 | /* Each represents one of 32 permissions bits. NULL is for undefined/reserved bits. |
---|
215 | * For more information, see: |
---|
216 | * http://msdn2.microsoft.com/en-gb/library/aa374892.aspx |
---|
217 | * http://msdn2.microsoft.com/en-gb/library/ms724878.aspx |
---|
218 | */ |
---|
219 | static const char* perm_map[32] = |
---|
220 | {/* object-specific permissions (registry keys, in this case) */ |
---|
221 | "QRY_VAL", /* KEY_QUERY_VALUE */ |
---|
222 | "SET_VAL", /* KEY_SET_VALUE */ |
---|
223 | "CREATE_KEY", /* KEY_CREATE_SUB_KEY */ |
---|
224 | "ENUM_KEYS", /* KEY_ENUMERATE_SUB_KEYS */ |
---|
225 | "NOTIFY", /* KEY_NOTIFY */ |
---|
226 | "CREATE_LNK", /* KEY_CREATE_LINK - Reserved for system use. */ |
---|
227 | NULL, |
---|
228 | NULL, |
---|
229 | "WOW64_64", /* KEY_WOW64_64KEY */ |
---|
230 | "WOW64_32", /* KEY_WOW64_32KEY */ |
---|
231 | NULL, |
---|
232 | NULL, |
---|
233 | NULL, |
---|
234 | NULL, |
---|
235 | NULL, |
---|
236 | NULL, |
---|
237 | /* standard access rights */ |
---|
238 | "DELETE", /* DELETE */ |
---|
239 | "R_CONT", /* READ_CONTROL */ |
---|
240 | "W_DAC", /* WRITE_DAC */ |
---|
241 | "W_OWNER", /* WRITE_OWNER */ |
---|
242 | "SYNC", /* SYNCHRONIZE - Shouldn't be set in registries */ |
---|
243 | NULL, |
---|
244 | NULL, |
---|
245 | NULL, |
---|
246 | /* other generic */ |
---|
247 | "SYS_SEC", /* ACCESS_SYSTEM_SECURITY */ |
---|
248 | "MAX_ALLWD", /* MAXIMUM_ALLOWED */ |
---|
249 | NULL, |
---|
250 | NULL, |
---|
251 | "GEN_A", /* GENERIC_ALL */ |
---|
252 | "GEN_X", /* GENERIC_EXECUTE */ |
---|
253 | "GEN_W", /* GENERIC_WRITE */ |
---|
254 | "GEN_R", /* GENERIC_READ */ |
---|
255 | }; |
---|
256 | |
---|
257 | |
---|
258 | if(ret_val == NULL) |
---|
259 | return NULL; |
---|
260 | |
---|
261 | r[0] = '\0'; |
---|
262 | for(i=0; i < 32; i++) |
---|
263 | { |
---|
264 | p = (1<<i); |
---|
265 | if((perms & p) && (perm_map[i] != NULL)) |
---|
266 | { |
---|
267 | strcpy(r, perm_map[i]); |
---|
268 | r += strlen(perm_map[i]); |
---|
269 | *(r++) = ' '; |
---|
270 | perms ^= p; |
---|
271 | } |
---|
272 | } |
---|
273 | |
---|
274 | /* Any remaining unknown permission bits are added at the end in hex. */ |
---|
275 | if(perms != 0) |
---|
276 | sprintf(r, "0x%.8X ", perms); |
---|
277 | |
---|
278 | /* Chop off the last space if we've written anything to ret_val */ |
---|
279 | if(r != ret_val) |
---|
280 | r[-1] = '\0'; |
---|
281 | |
---|
282 | return ret_val; |
---|
283 | } |
---|
284 | |
---|
285 | |
---|
286 | char* regfi_sid2str(WINSEC_DOM_SID* sid) |
---|
287 | { |
---|
288 | uint32 i, size = WINSEC_MAX_SUBAUTHS*11 + 24; |
---|
289 | uint32 left = size; |
---|
290 | uint8 comps = sid->num_auths; |
---|
291 | char* ret_val = malloc(size); |
---|
292 | |
---|
293 | if(ret_val == NULL) |
---|
294 | return NULL; |
---|
295 | |
---|
296 | if(comps > WINSEC_MAX_SUBAUTHS) |
---|
297 | comps = WINSEC_MAX_SUBAUTHS; |
---|
298 | |
---|
299 | left -= sprintf(ret_val, "S-%u-%u", sid->sid_rev_num, sid->id_auth[5]); |
---|
300 | |
---|
301 | for (i = 0; i < comps; i++) |
---|
302 | left -= snprintf(ret_val+(size-left), left, "-%u", sid->sub_auths[i]); |
---|
303 | |
---|
304 | return ret_val; |
---|
305 | } |
---|
306 | |
---|
307 | |
---|
308 | char* regfi_get_acl(WINSEC_ACL* acl) |
---|
309 | { |
---|
310 | uint32 i, extra, size = 0; |
---|
311 | const char* type_str; |
---|
312 | char* flags_str; |
---|
313 | char* perms_str; |
---|
314 | char* sid_str; |
---|
315 | char* ace_delim = ""; |
---|
316 | char* ret_val = NULL; |
---|
317 | char* tmp_val = NULL; |
---|
318 | bool failed = false; |
---|
319 | char field_delim = ':'; |
---|
320 | |
---|
321 | for (i = 0; i < acl->num_aces && !failed; i++) |
---|
322 | { |
---|
323 | sid_str = regfi_sid2str(acl->aces[i]->trustee); |
---|
324 | type_str = regfi_ace_type2str(acl->aces[i]->type); |
---|
325 | perms_str = regfi_ace_perms2str(acl->aces[i]->access_mask); |
---|
326 | flags_str = regfi_ace_flags2str(acl->aces[i]->flags); |
---|
327 | |
---|
328 | if(flags_str != NULL && perms_str != NULL |
---|
329 | && type_str != NULL && sid_str != NULL) |
---|
330 | { |
---|
331 | /* XXX: this is slow */ |
---|
332 | extra = strlen(sid_str) + strlen(type_str) |
---|
333 | + strlen(perms_str) + strlen(flags_str) + 5; |
---|
334 | tmp_val = realloc(ret_val, size+extra); |
---|
335 | |
---|
336 | if(tmp_val == NULL) |
---|
337 | { |
---|
338 | free(ret_val); |
---|
339 | ret_val = NULL; |
---|
340 | failed = true; |
---|
341 | } |
---|
342 | else |
---|
343 | { |
---|
344 | ret_val = tmp_val; |
---|
345 | size += snprintf(ret_val+size, extra, "%s%s%c%s%c%s%c%s", |
---|
346 | ace_delim,sid_str, |
---|
347 | field_delim,type_str, |
---|
348 | field_delim,perms_str, |
---|
349 | field_delim,flags_str); |
---|
350 | ace_delim = "|"; |
---|
351 | } |
---|
352 | } |
---|
353 | else |
---|
354 | failed = true; |
---|
355 | |
---|
356 | if(sid_str != NULL) |
---|
357 | free(sid_str); |
---|
358 | if(sid_str != NULL) |
---|
359 | free(perms_str); |
---|
360 | if(sid_str != NULL) |
---|
361 | free(flags_str); |
---|
362 | } |
---|
363 | |
---|
364 | return ret_val; |
---|
365 | } |
---|
366 | |
---|
367 | |
---|
368 | char* regfi_get_sacl(WINSEC_DESC *sec_desc) |
---|
369 | { |
---|
370 | if (sec_desc->sacl) |
---|
371 | return regfi_get_acl(sec_desc->sacl); |
---|
372 | else |
---|
373 | return NULL; |
---|
374 | } |
---|
375 | |
---|
376 | |
---|
377 | char* regfi_get_dacl(WINSEC_DESC *sec_desc) |
---|
378 | { |
---|
379 | if (sec_desc->dacl) |
---|
380 | return regfi_get_acl(sec_desc->dacl); |
---|
381 | else |
---|
382 | return NULL; |
---|
383 | } |
---|
384 | |
---|
385 | |
---|
386 | char* regfi_get_owner(WINSEC_DESC *sec_desc) |
---|
387 | { |
---|
388 | return regfi_sid2str(sec_desc->owner_sid); |
---|
389 | } |
---|
390 | |
---|
391 | |
---|
392 | char* regfi_get_group(WINSEC_DESC *sec_desc) |
---|
393 | { |
---|
394 | return regfi_sid2str(sec_desc->grp_sid); |
---|
395 | } |
---|
396 | |
---|
397 | |
---|
398 | /***************************************************************************** |
---|
399 | * This function is just like read(2), except that it continues to |
---|
400 | * re-try reading from the file descriptor if EINTR or EAGAIN is received. |
---|
401 | * regfi_read will attempt to read length bytes from fd and write them to buf. |
---|
402 | * |
---|
403 | * On success, 0 is returned. Upon failure, an errno code is returned. |
---|
404 | * |
---|
405 | * The number of bytes successfully read is returned through the length |
---|
406 | * parameter by reference. If both the return value and length parameter are |
---|
407 | * returned as 0, then EOF was encountered immediately |
---|
408 | *****************************************************************************/ |
---|
409 | uint32 regfi_read(int fd, uint8* buf, uint32* length) |
---|
410 | { |
---|
411 | uint32 rsize = 0; |
---|
412 | uint32 rret = 0; |
---|
413 | |
---|
414 | do |
---|
415 | { |
---|
416 | rret = read(fd, buf + rsize, *length - rsize); |
---|
417 | if(rret > 0) |
---|
418 | rsize += rret; |
---|
419 | }while(*length - rsize > 0 |
---|
420 | && (rret > 0 || (rret == -1 && (errno == EAGAIN || errno == EINTR)))); |
---|
421 | |
---|
422 | *length = rsize; |
---|
423 | if (rret == -1 && errno != EINTR && errno != EAGAIN) |
---|
424 | return errno; |
---|
425 | |
---|
426 | return 0; |
---|
427 | } |
---|
428 | |
---|
429 | |
---|
430 | /***************************************************************************** |
---|
431 | * |
---|
432 | *****************************************************************************/ |
---|
433 | bool regfi_parse_cell(int fd, uint32 offset, uint8* hdr, uint32 hdr_len, |
---|
434 | uint32* cell_length, bool* unalloc) |
---|
435 | { |
---|
436 | uint32 length; |
---|
437 | int32 raw_length; |
---|
438 | uint8 tmp[4]; |
---|
439 | |
---|
440 | if(lseek(fd, offset, SEEK_SET) == -1) |
---|
441 | return false; |
---|
442 | |
---|
443 | length = 4; |
---|
444 | if((regfi_read(fd, tmp, &length) != 0) || length != 4) |
---|
445 | return false; |
---|
446 | raw_length = IVALS(tmp, 0); |
---|
447 | |
---|
448 | if(raw_length < 0) |
---|
449 | { |
---|
450 | (*cell_length) = raw_length*(-1); |
---|
451 | (*unalloc) = false; |
---|
452 | } |
---|
453 | else |
---|
454 | { |
---|
455 | (*cell_length) = raw_length; |
---|
456 | (*unalloc) = true; |
---|
457 | } |
---|
458 | |
---|
459 | if(*cell_length - 4 < hdr_len) |
---|
460 | return false; |
---|
461 | |
---|
462 | if(hdr_len > 0) |
---|
463 | { |
---|
464 | length = hdr_len; |
---|
465 | if((regfi_read(fd, hdr, &length) != 0) || length != hdr_len) |
---|
466 | return false; |
---|
467 | } |
---|
468 | |
---|
469 | return true; |
---|
470 | } |
---|
471 | |
---|
472 | |
---|
473 | /******************************************************************* |
---|
474 | * Given an offset and an hbin, is the offset within that hbin? |
---|
475 | * The offset is a virtual file offset. |
---|
476 | *******************************************************************/ |
---|
477 | static bool regfi_offset_in_hbin(REGFI_HBIN* hbin, uint32 offset) |
---|
478 | { |
---|
479 | if(!hbin) |
---|
480 | return false; |
---|
481 | |
---|
482 | if((offset > hbin->first_hbin_off) |
---|
483 | && (offset < (hbin->first_hbin_off + hbin->block_size))) |
---|
484 | return true; |
---|
485 | |
---|
486 | return false; |
---|
487 | } |
---|
488 | |
---|
489 | |
---|
490 | |
---|
491 | /******************************************************************* |
---|
492 | * Provide a virtual offset and receive the correpsonding HBIN |
---|
493 | * block for it. NULL if one doesn't exist. |
---|
494 | *******************************************************************/ |
---|
495 | REGFI_HBIN* regfi_lookup_hbin(REGFI_FILE* file, uint32 offset) |
---|
496 | { |
---|
497 | return (REGFI_HBIN*)range_list_find_data(file->hbins, offset+REGFI_REGF_SIZE); |
---|
498 | } |
---|
499 | |
---|
500 | |
---|
501 | |
---|
502 | /****************************************************************************** |
---|
503 | ******************************************************************************/ |
---|
504 | REGFI_SUBKEY_LIST* regfi_load_subkeylist(REGFI_FILE* file, uint32 offset, |
---|
505 | uint32 num_keys, uint32 max_size, |
---|
506 | bool strict) |
---|
507 | { |
---|
508 | REGFI_SUBKEY_LIST* ret_val; |
---|
509 | |
---|
510 | ret_val = regfi_load_subkeylist_aux(file, offset, max_size, strict, |
---|
511 | REGFI_MAX_SUBKEY_DEPTH); |
---|
512 | if(ret_val == NULL) |
---|
513 | { |
---|
514 | regfi_add_message(file, REGFI_MSG_WARN, "Failed to load subkey list at" |
---|
515 | " offset 0x%.8X.", offset); |
---|
516 | return NULL; |
---|
517 | } |
---|
518 | |
---|
519 | if(num_keys != ret_val->num_keys) |
---|
520 | { |
---|
521 | /* Not sure which should be authoritative, the number from the |
---|
522 | * NK record, or the number in the subkey list. Just emit a warning for |
---|
523 | * now if they don't match. |
---|
524 | */ |
---|
525 | regfi_add_message(file, REGFI_MSG_WARN, "Number of subkeys listed in parent" |
---|
526 | " (%d) did not match number found in subkey list/tree (%d)" |
---|
527 | " while parsing subkey list/tree at offset 0x%.8X.", |
---|
528 | num_keys, ret_val->num_keys, offset); |
---|
529 | } |
---|
530 | |
---|
531 | return ret_val; |
---|
532 | } |
---|
533 | |
---|
534 | |
---|
535 | /****************************************************************************** |
---|
536 | ******************************************************************************/ |
---|
537 | REGFI_SUBKEY_LIST* regfi_load_subkeylist_aux(REGFI_FILE* file, uint32 offset, |
---|
538 | uint32 max_size, bool strict, |
---|
539 | uint8 depth_left) |
---|
540 | { |
---|
541 | REGFI_SUBKEY_LIST* ret_val; |
---|
542 | REGFI_SUBKEY_LIST** sublists; |
---|
543 | REGFI_HBIN* sublist_hbin; |
---|
544 | uint32 i, num_sublists, off, max_length; |
---|
545 | |
---|
546 | if(depth_left == 0) |
---|
547 | { |
---|
548 | regfi_add_message(file, REGFI_MSG_WARN, "Maximum depth reached" |
---|
549 | " while parsing subkey list/tree at offset 0x%.8X.", |
---|
550 | offset); |
---|
551 | return NULL; |
---|
552 | } |
---|
553 | |
---|
554 | ret_val = regfi_parse_subkeylist(file, offset, max_size, strict); |
---|
555 | if(ret_val == NULL) |
---|
556 | return NULL; |
---|
557 | |
---|
558 | if(ret_val->recursive_type) |
---|
559 | { |
---|
560 | num_sublists = ret_val->num_children; |
---|
561 | sublists = (REGFI_SUBKEY_LIST**)zalloc(num_sublists |
---|
562 | * sizeof(REGFI_SUBKEY_LIST*)); |
---|
563 | for(i=0; i < num_sublists; i++) |
---|
564 | { |
---|
565 | off = ret_val->elements[i].offset + REGFI_REGF_SIZE; |
---|
566 | sublist_hbin = regfi_lookup_hbin(file, ret_val->elements[i].offset); |
---|
567 | if(sublist_hbin == NULL) |
---|
568 | sublists[i] = NULL; |
---|
569 | else |
---|
570 | { |
---|
571 | max_length = sublist_hbin->block_size + sublist_hbin->file_off - off; |
---|
572 | sublists[i] = regfi_load_subkeylist_aux(file, off, max_length, strict, |
---|
573 | depth_left-1); |
---|
574 | } |
---|
575 | } |
---|
576 | free(ret_val); |
---|
577 | |
---|
578 | return regfi_merge_subkeylists(num_sublists, sublists, strict); |
---|
579 | } |
---|
580 | |
---|
581 | return ret_val; |
---|
582 | } |
---|
583 | |
---|
584 | |
---|
585 | /****************************************************************************** |
---|
586 | ******************************************************************************/ |
---|
587 | REGFI_SUBKEY_LIST* regfi_parse_subkeylist(REGFI_FILE* file, uint32 offset, |
---|
588 | uint32 max_size, bool strict) |
---|
589 | { |
---|
590 | REGFI_SUBKEY_LIST* ret_val; |
---|
591 | uint32 i, cell_length, length, elem_size; |
---|
592 | uint8* elements; |
---|
593 | uint8 buf[REGFI_SUBKEY_LIST_MIN_LEN]; |
---|
594 | bool unalloc; |
---|
595 | bool recursive_type; |
---|
596 | |
---|
597 | if(!regfi_parse_cell(file->fd, offset, buf, REGFI_SUBKEY_LIST_MIN_LEN, |
---|
598 | &cell_length, &unalloc)) |
---|
599 | { |
---|
600 | regfi_add_message(file, REGFI_MSG_WARN, "Could not parse cell while " |
---|
601 | "parsing subkey-list at offset 0x%.8X.", offset); |
---|
602 | return NULL; |
---|
603 | } |
---|
604 | |
---|
605 | if(cell_length > max_size) |
---|
606 | { |
---|
607 | regfi_add_message(file, REGFI_MSG_WARN, "Cell size longer than max_size" |
---|
608 | " while parsing subkey-list at offset 0x%.8X.", offset); |
---|
609 | if(strict) |
---|
610 | return NULL; |
---|
611 | cell_length = max_size & 0xFFFFFFF8; |
---|
612 | } |
---|
613 | |
---|
614 | recursive_type = false; |
---|
615 | if(buf[0] == 'r' && buf[1] == 'i') |
---|
616 | { |
---|
617 | recursive_type = true; |
---|
618 | elem_size = sizeof(uint32); |
---|
619 | } |
---|
620 | else if(buf[0] == 'l' && buf[1] == 'i') |
---|
621 | elem_size = sizeof(uint32); |
---|
622 | else if((buf[0] == 'l') && (buf[1] == 'f' || buf[1] == 'h')) |
---|
623 | elem_size = sizeof(REGFI_SUBKEY_LIST_ELEM); |
---|
624 | else |
---|
625 | { |
---|
626 | regfi_add_message(file, REGFI_MSG_ERROR, "Unknown magic number" |
---|
627 | " (0x%.2X, 0x%.2X) encountered while parsing" |
---|
628 | " subkey-list at offset 0x%.8X.", buf[0], buf[1], offset); |
---|
629 | return NULL; |
---|
630 | } |
---|
631 | |
---|
632 | ret_val = (REGFI_SUBKEY_LIST*)zalloc(sizeof(REGFI_SUBKEY_LIST)); |
---|
633 | if(ret_val == NULL) |
---|
634 | return NULL; |
---|
635 | |
---|
636 | ret_val->offset = offset; |
---|
637 | ret_val->cell_size = cell_length; |
---|
638 | ret_val->magic[0] = buf[0]; |
---|
639 | ret_val->magic[1] = buf[1]; |
---|
640 | ret_val->recursive_type = recursive_type; |
---|
641 | ret_val->num_children = SVAL(buf, 0x2); |
---|
642 | |
---|
643 | if(!recursive_type) |
---|
644 | ret_val->num_keys = ret_val->num_children; |
---|
645 | |
---|
646 | length = elem_size*ret_val->num_children; |
---|
647 | if(cell_length - REGFI_SUBKEY_LIST_MIN_LEN - sizeof(uint32) < length) |
---|
648 | { |
---|
649 | regfi_add_message(file, REGFI_MSG_WARN, "Number of elements too large for" |
---|
650 | " cell while parsing subkey-list at offset 0x%.8X.", |
---|
651 | offset); |
---|
652 | if(strict) |
---|
653 | { |
---|
654 | free(ret_val); |
---|
655 | return NULL; |
---|
656 | } |
---|
657 | length = cell_length - REGFI_SUBKEY_LIST_MIN_LEN - sizeof(uint32); |
---|
658 | } |
---|
659 | |
---|
660 | ret_val->elements |
---|
661 | = (REGFI_SUBKEY_LIST_ELEM*)zalloc(ret_val->num_children |
---|
662 | * sizeof(REGFI_SUBKEY_LIST_ELEM)); |
---|
663 | if(ret_val->elements == NULL) |
---|
664 | { |
---|
665 | free(ret_val); |
---|
666 | return NULL; |
---|
667 | } |
---|
668 | |
---|
669 | elements = (uint8*)zalloc(length); |
---|
670 | if(elements == NULL) |
---|
671 | { |
---|
672 | free(ret_val->elements); |
---|
673 | free(ret_val); |
---|
674 | return NULL; |
---|
675 | } |
---|
676 | |
---|
677 | if(regfi_read(file->fd, elements, &length) != 0 |
---|
678 | || length != elem_size*ret_val->num_children) |
---|
679 | { |
---|
680 | free(ret_val->elements); |
---|
681 | free(ret_val); |
---|
682 | return NULL; |
---|
683 | } |
---|
684 | |
---|
685 | if(elem_size == sizeof(uint32)) |
---|
686 | { |
---|
687 | for (i=0; i < ret_val->num_children; i++) |
---|
688 | { |
---|
689 | ret_val->elements[i].offset = IVAL(elements, i*elem_size); |
---|
690 | ret_val->elements[i].hash = 0; |
---|
691 | } |
---|
692 | } |
---|
693 | else |
---|
694 | { |
---|
695 | for (i=0; i < ret_val->num_children; i++) |
---|
696 | { |
---|
697 | ret_val->elements[i].offset = IVAL(elements, i*elem_size); |
---|
698 | ret_val->elements[i].hash = IVAL(elements, i*elem_size+4); |
---|
699 | } |
---|
700 | } |
---|
701 | free(elements); |
---|
702 | |
---|
703 | return ret_val; |
---|
704 | } |
---|
705 | |
---|
706 | |
---|
707 | /******************************************************************* |
---|
708 | *******************************************************************/ |
---|
709 | REGFI_SUBKEY_LIST* regfi_merge_subkeylists(uint16 num_lists, |
---|
710 | REGFI_SUBKEY_LIST** lists, |
---|
711 | bool strict) |
---|
712 | { |
---|
713 | uint32 i,j,k; |
---|
714 | REGFI_SUBKEY_LIST* ret_val; |
---|
715 | |
---|
716 | if(lists == NULL) |
---|
717 | return NULL; |
---|
718 | ret_val = (REGFI_SUBKEY_LIST*)zalloc(sizeof(REGFI_SUBKEY_LIST)); |
---|
719 | |
---|
720 | if(ret_val == NULL) |
---|
721 | return NULL; |
---|
722 | |
---|
723 | /* Obtain total number of elements */ |
---|
724 | ret_val->num_keys = 0; |
---|
725 | for(i=0; i < num_lists; i++) |
---|
726 | { |
---|
727 | if(lists[i] != NULL) |
---|
728 | ret_val->num_keys += lists[i]->num_children; |
---|
729 | } |
---|
730 | ret_val->num_children = ret_val->num_keys; |
---|
731 | |
---|
732 | if(ret_val->num_keys > 0) |
---|
733 | { |
---|
734 | ret_val->elements = |
---|
735 | (REGFI_SUBKEY_LIST_ELEM*)zalloc(sizeof(REGFI_SUBKEY_LIST_ELEM) |
---|
736 | * ret_val->num_keys); |
---|
737 | k=0; |
---|
738 | |
---|
739 | if(ret_val->elements != NULL) |
---|
740 | { |
---|
741 | for(i=0; i < num_lists; i++) |
---|
742 | { |
---|
743 | if(lists[i] != NULL) |
---|
744 | { |
---|
745 | for(j=0; j < lists[i]->num_keys; j++) |
---|
746 | { |
---|
747 | ret_val->elements[k].hash=lists[i]->elements[j].hash; |
---|
748 | ret_val->elements[k++].offset=lists[i]->elements[j].offset; |
---|
749 | } |
---|
750 | } |
---|
751 | } |
---|
752 | } |
---|
753 | } |
---|
754 | |
---|
755 | for(i=0; i < num_lists; i++) |
---|
756 | regfi_subkeylist_free(lists[i]); |
---|
757 | free(lists); |
---|
758 | |
---|
759 | return ret_val; |
---|
760 | } |
---|
761 | |
---|
762 | |
---|
763 | /******************************************************************* |
---|
764 | *******************************************************************/ |
---|
765 | REGFI_SK_REC* regfi_parse_sk(REGFI_FILE* file, uint32 offset, uint32 max_size, bool strict) |
---|
766 | { |
---|
767 | REGFI_SK_REC* ret_val; |
---|
768 | uint8* sec_desc_buf; |
---|
769 | uint32 cell_length, length; |
---|
770 | /*prs_struct ps;*/ |
---|
771 | uint8 sk_header[REGFI_SK_MIN_LENGTH]; |
---|
772 | bool unalloc = false; |
---|
773 | |
---|
774 | if(!regfi_parse_cell(file->fd, offset, sk_header, REGFI_SK_MIN_LENGTH, |
---|
775 | &cell_length, &unalloc)) |
---|
776 | { |
---|
777 | regfi_add_message(file, REGFI_MSG_WARN, "Could not parse SK record cell" |
---|
778 | " at offset 0x%.8X.", offset); |
---|
779 | return NULL; |
---|
780 | } |
---|
781 | |
---|
782 | if(sk_header[0] != 's' || sk_header[1] != 'k') |
---|
783 | { |
---|
784 | regfi_add_message(file, REGFI_MSG_WARN, "Magic number mismatch in parsing" |
---|
785 | " SK record at offset 0x%.8X.", offset); |
---|
786 | return NULL; |
---|
787 | } |
---|
788 | |
---|
789 | ret_val = (REGFI_SK_REC*)zalloc(sizeof(REGFI_SK_REC)); |
---|
790 | if(ret_val == NULL) |
---|
791 | return NULL; |
---|
792 | |
---|
793 | ret_val->offset = offset; |
---|
794 | /* XXX: Is there a way to be more conservative (shorter) with |
---|
795 | * cell length when cell is unallocated? |
---|
796 | */ |
---|
797 | ret_val->cell_size = cell_length; |
---|
798 | |
---|
799 | if(ret_val->cell_size > max_size) |
---|
800 | ret_val->cell_size = max_size & 0xFFFFFFF8; |
---|
801 | if((ret_val->cell_size < REGFI_SK_MIN_LENGTH) |
---|
802 | || (strict && ret_val->cell_size != (ret_val->cell_size & 0xFFFFFFF8))) |
---|
803 | { |
---|
804 | regfi_add_message(file, REGFI_MSG_WARN, "Invalid cell size found while" |
---|
805 | " parsing SK record at offset 0x%.8X.", offset); |
---|
806 | free(ret_val); |
---|
807 | return NULL; |
---|
808 | } |
---|
809 | |
---|
810 | ret_val->magic[0] = sk_header[0]; |
---|
811 | ret_val->magic[1] = sk_header[1]; |
---|
812 | |
---|
813 | ret_val->unknown_tag = SVAL(sk_header, 0x2); |
---|
814 | ret_val->prev_sk_off = IVAL(sk_header, 0x4); |
---|
815 | ret_val->next_sk_off = IVAL(sk_header, 0x8); |
---|
816 | ret_val->ref_count = IVAL(sk_header, 0xC); |
---|
817 | ret_val->desc_size = IVAL(sk_header, 0x10); |
---|
818 | |
---|
819 | if(ret_val->prev_sk_off != (ret_val->prev_sk_off & 0xFFFFFFF8) |
---|
820 | || ret_val->next_sk_off != (ret_val->next_sk_off & 0xFFFFFFF8)) |
---|
821 | { |
---|
822 | regfi_add_message(file, REGFI_MSG_WARN, "SK record's next/previous offsets" |
---|
823 | " are not a multiple of 8 while parsing SK record at" |
---|
824 | " offset 0x%.8X.", offset); |
---|
825 | free(ret_val); |
---|
826 | return NULL; |
---|
827 | } |
---|
828 | |
---|
829 | if(ret_val->desc_size + REGFI_SK_MIN_LENGTH > ret_val->cell_size) |
---|
830 | { |
---|
831 | regfi_add_message(file, REGFI_MSG_WARN, "Security descriptor too large for" |
---|
832 | " cell while parsing SK record at offset 0x%.8X.", |
---|
833 | offset); |
---|
834 | free(ret_val); |
---|
835 | return NULL; |
---|
836 | } |
---|
837 | |
---|
838 | sec_desc_buf = (uint8*)zalloc(ret_val->desc_size); |
---|
839 | if(ret_val == NULL) |
---|
840 | { |
---|
841 | free(ret_val); |
---|
842 | return NULL; |
---|
843 | } |
---|
844 | |
---|
845 | length = ret_val->desc_size; |
---|
846 | if(regfi_read(file->fd, sec_desc_buf, &length) != 0 |
---|
847 | || length != ret_val->desc_size) |
---|
848 | { |
---|
849 | regfi_add_message(file, REGFI_MSG_ERROR, "Failed to read security" |
---|
850 | " descriptor while parsing SK record at offset 0x%.8X.", |
---|
851 | offset); |
---|
852 | free(ret_val); |
---|
853 | return NULL; |
---|
854 | } |
---|
855 | |
---|
856 | if(!(ret_val->sec_desc = winsec_parse_desc(sec_desc_buf, ret_val->desc_size))) |
---|
857 | { |
---|
858 | regfi_add_message(file, REGFI_MSG_ERROR, "Failed to parse security" |
---|
859 | " descriptor while parsing SK record at offset 0x%.8X.", |
---|
860 | offset); |
---|
861 | free(sec_desc_buf); |
---|
862 | free(ret_val); |
---|
863 | return NULL; |
---|
864 | } |
---|
865 | free(sec_desc_buf); |
---|
866 | |
---|
867 | |
---|
868 | return ret_val; |
---|
869 | } |
---|
870 | |
---|
871 | |
---|
872 | uint32* regfi_parse_valuelist(REGFI_FILE* file, uint32 offset, |
---|
873 | uint32 num_values, bool strict) |
---|
874 | { |
---|
875 | uint32* ret_val; |
---|
876 | uint32 i, cell_length, length, read_len; |
---|
877 | bool unalloc; |
---|
878 | |
---|
879 | if(!regfi_parse_cell(file->fd, offset, NULL, 0, &cell_length, &unalloc)) |
---|
880 | { |
---|
881 | regfi_add_message(file, REGFI_MSG_ERROR, "Failed to read cell header" |
---|
882 | " while parsing value list at offset 0x%.8X.", offset); |
---|
883 | return NULL; |
---|
884 | } |
---|
885 | |
---|
886 | if(cell_length != (cell_length & 0xFFFFFFF8)) |
---|
887 | { |
---|
888 | if(strict) |
---|
889 | return NULL; |
---|
890 | cell_length = cell_length & 0xFFFFFFF8; |
---|
891 | } |
---|
892 | if((num_values * sizeof(uint32)) > cell_length-sizeof(uint32)) |
---|
893 | { |
---|
894 | regfi_add_message(file, REGFI_MSG_WARN, "Too many values found" |
---|
895 | " while parsing value list at offset 0x%.8X.", offset); |
---|
896 | /* XXX: During non-strict, should reduce num_values appropriately and |
---|
897 | * continue instead of bailing out. |
---|
898 | */ |
---|
899 | return NULL; |
---|
900 | } |
---|
901 | |
---|
902 | read_len = num_values*sizeof(uint32); |
---|
903 | ret_val = (uint32*)malloc(read_len); |
---|
904 | if(ret_val == NULL) |
---|
905 | return NULL; |
---|
906 | |
---|
907 | length = read_len; |
---|
908 | if((regfi_read(file->fd, (uint8*)ret_val, &length) != 0) || length != read_len) |
---|
909 | { |
---|
910 | regfi_add_message(file, REGFI_MSG_ERROR, "Failed to read value pointers" |
---|
911 | " while parsing value list at offset 0x%.8X.", offset); |
---|
912 | free(ret_val); |
---|
913 | return NULL; |
---|
914 | } |
---|
915 | |
---|
916 | for(i=0; i < num_values; i++) |
---|
917 | { |
---|
918 | /* Fix endianness */ |
---|
919 | ret_val[i] = IVAL(&ret_val[i], 0); |
---|
920 | |
---|
921 | /* Validate the first num_values values to ensure they make sense */ |
---|
922 | if(strict) |
---|
923 | { |
---|
924 | if((ret_val[i] + REGFI_REGF_SIZE > file->file_length) |
---|
925 | || ((ret_val[i] & 0xFFFFFFF8) != ret_val[i])) |
---|
926 | { |
---|
927 | regfi_add_message(file, REGFI_MSG_ERROR, "Invalid value pointer" |
---|
928 | " (0x%.8X) found while parsing value list at offset" |
---|
929 | " 0x%.8X.", ret_val[i], offset); |
---|
930 | free(ret_val); |
---|
931 | return NULL; |
---|
932 | } |
---|
933 | } |
---|
934 | } |
---|
935 | |
---|
936 | return ret_val; |
---|
937 | } |
---|
938 | |
---|
939 | |
---|
940 | |
---|
941 | /****************************************************************************** |
---|
942 | * If !strict, the list may contain NULLs, VK records may point to NULL. |
---|
943 | ******************************************************************************/ |
---|
944 | REGFI_VK_REC** regfi_load_valuelist(REGFI_FILE* file, uint32 offset, |
---|
945 | uint32 num_values, uint32 max_size, |
---|
946 | bool strict) |
---|
947 | { |
---|
948 | REGFI_VK_REC** ret_val; |
---|
949 | REGFI_HBIN* hbin; |
---|
950 | uint32 i, vk_offset, vk_max_length, usable_num_values; |
---|
951 | uint32* voffsets; |
---|
952 | |
---|
953 | if((num_values+1) * sizeof(uint32) > max_size) |
---|
954 | { |
---|
955 | if(strict) |
---|
956 | return NULL; |
---|
957 | usable_num_values = max_size/sizeof(uint32) - sizeof(uint32); |
---|
958 | } |
---|
959 | else |
---|
960 | usable_num_values = num_values; |
---|
961 | |
---|
962 | voffsets = regfi_parse_valuelist(file, offset, usable_num_values, strict); |
---|
963 | if(voffsets == NULL) |
---|
964 | return NULL; |
---|
965 | |
---|
966 | ret_val = (REGFI_VK_REC**)zalloc(sizeof(REGFI_VK_REC*) * usable_num_values); |
---|
967 | if(ret_val == NULL) |
---|
968 | { |
---|
969 | free(voffsets); |
---|
970 | return NULL; |
---|
971 | } |
---|
972 | |
---|
973 | for(i=0; i < usable_num_values; i++) |
---|
974 | { |
---|
975 | hbin = regfi_lookup_hbin(file, voffsets[i]); |
---|
976 | if(!hbin) |
---|
977 | { |
---|
978 | free(voffsets); |
---|
979 | free(ret_val); |
---|
980 | return NULL; |
---|
981 | } |
---|
982 | |
---|
983 | vk_offset = voffsets[i] + REGFI_REGF_SIZE; |
---|
984 | vk_max_length = hbin->block_size + hbin->file_off - vk_offset; |
---|
985 | ret_val[i] = regfi_parse_vk(file, vk_offset, vk_max_length, strict); |
---|
986 | if(ret_val[i] == NULL) |
---|
987 | { /* If we're being strict, throw out the whole list. |
---|
988 | * Otherwise, let it be NULL. |
---|
989 | */ |
---|
990 | if(strict) |
---|
991 | { |
---|
992 | free(voffsets); |
---|
993 | free(ret_val); |
---|
994 | return NULL; |
---|
995 | } |
---|
996 | } |
---|
997 | } |
---|
998 | |
---|
999 | free(voffsets); |
---|
1000 | return ret_val; |
---|
1001 | } |
---|
1002 | |
---|
1003 | |
---|
1004 | |
---|
1005 | /******************************************************************* |
---|
1006 | * XXX: Need to add full key caching using a |
---|
1007 | * custom cache structure. |
---|
1008 | *******************************************************************/ |
---|
1009 | REGFI_NK_REC* regfi_load_key(REGFI_FILE* file, uint32 offset, bool strict) |
---|
1010 | { |
---|
1011 | REGFI_HBIN* hbin; |
---|
1012 | REGFI_HBIN* sub_hbin; |
---|
1013 | REGFI_NK_REC* nk; |
---|
1014 | uint32 max_length, off; |
---|
1015 | |
---|
1016 | hbin = regfi_lookup_hbin(file, offset-REGFI_REGF_SIZE); |
---|
1017 | if (hbin == NULL) |
---|
1018 | return NULL; |
---|
1019 | |
---|
1020 | /* get the initial nk record */ |
---|
1021 | max_length = hbin->block_size + hbin->file_off - offset; |
---|
1022 | if ((nk = regfi_parse_nk(file, offset, max_length, true)) == NULL) |
---|
1023 | { |
---|
1024 | regfi_add_message(file, REGFI_MSG_ERROR, "Could not load NK record at" |
---|
1025 | " offset 0x%.8X.", offset); |
---|
1026 | return NULL; |
---|
1027 | } |
---|
1028 | |
---|
1029 | /* fill in values */ |
---|
1030 | if(nk->num_values && (nk->values_off!=REGFI_OFFSET_NONE)) |
---|
1031 | { |
---|
1032 | sub_hbin = hbin; |
---|
1033 | if(!regfi_offset_in_hbin(hbin, nk->values_off)) |
---|
1034 | sub_hbin = regfi_lookup_hbin(file, nk->values_off); |
---|
1035 | |
---|
1036 | if(sub_hbin == NULL) |
---|
1037 | { |
---|
1038 | if(strict) |
---|
1039 | { |
---|
1040 | free(nk); |
---|
1041 | return NULL; |
---|
1042 | } |
---|
1043 | else |
---|
1044 | nk->values = NULL; |
---|
1045 | |
---|
1046 | } |
---|
1047 | else |
---|
1048 | { |
---|
1049 | off = nk->values_off + REGFI_REGF_SIZE; |
---|
1050 | max_length = sub_hbin->block_size + sub_hbin->file_off - off; |
---|
1051 | nk->values = regfi_load_valuelist(file, off, nk->num_values, max_length, |
---|
1052 | true); |
---|
1053 | if(strict && nk->values == NULL) |
---|
1054 | { |
---|
1055 | regfi_add_message(file, REGFI_MSG_ERROR, "Could not load value list" |
---|
1056 | " for NK record at offset 0x%.8X.", |
---|
1057 | offset); |
---|
1058 | free(nk); |
---|
1059 | return NULL; |
---|
1060 | } |
---|
1061 | |
---|
1062 | } |
---|
1063 | } |
---|
1064 | |
---|
1065 | /* now get subkeys */ |
---|
1066 | if(nk->num_subkeys && (nk->subkeys_off != REGFI_OFFSET_NONE)) |
---|
1067 | { |
---|
1068 | sub_hbin = hbin; |
---|
1069 | if(!regfi_offset_in_hbin(hbin, nk->subkeys_off)) |
---|
1070 | sub_hbin = regfi_lookup_hbin(file, nk->subkeys_off); |
---|
1071 | |
---|
1072 | if (sub_hbin == NULL) |
---|
1073 | { |
---|
1074 | if(strict) |
---|
1075 | { |
---|
1076 | regfi_key_free(nk); |
---|
1077 | return NULL; |
---|
1078 | } |
---|
1079 | else |
---|
1080 | nk->subkeys = NULL; |
---|
1081 | } |
---|
1082 | else |
---|
1083 | { |
---|
1084 | off = nk->subkeys_off + REGFI_REGF_SIZE; |
---|
1085 | max_length = sub_hbin->block_size + sub_hbin->file_off - off; |
---|
1086 | nk->subkeys = regfi_load_subkeylist(file, off, nk->num_subkeys, |
---|
1087 | max_length, true); |
---|
1088 | |
---|
1089 | if(nk->subkeys == NULL) |
---|
1090 | { |
---|
1091 | regfi_add_message(file, REGFI_MSG_WARN, "Could not load subkey list" |
---|
1092 | " while parsing NK record at offset 0x%.8X.", offset); |
---|
1093 | nk->num_subkeys = 0; |
---|
1094 | } |
---|
1095 | } |
---|
1096 | } |
---|
1097 | |
---|
1098 | return nk; |
---|
1099 | } |
---|
1100 | |
---|
1101 | |
---|
1102 | /****************************************************************************** |
---|
1103 | ******************************************************************************/ |
---|
1104 | static bool regfi_find_root_nk(REGFI_FILE* file, uint32 offset, uint32 hbin_size, |
---|
1105 | uint32* root_offset) |
---|
1106 | { |
---|
1107 | uint8 tmp[4]; |
---|
1108 | int32 record_size; |
---|
1109 | uint32 length, hbin_offset = 0; |
---|
1110 | REGFI_NK_REC* nk = NULL; |
---|
1111 | bool found = false; |
---|
1112 | |
---|
1113 | for(record_size=0; !found && (hbin_offset < hbin_size); ) |
---|
1114 | { |
---|
1115 | if(lseek(file->fd, offset+hbin_offset, SEEK_SET) == -1) |
---|
1116 | return false; |
---|
1117 | |
---|
1118 | length = 4; |
---|
1119 | if((regfi_read(file->fd, tmp, &length) != 0) || length != 4) |
---|
1120 | return false; |
---|
1121 | record_size = IVALS(tmp, 0); |
---|
1122 | |
---|
1123 | if(record_size < 0) |
---|
1124 | { |
---|
1125 | record_size = record_size*(-1); |
---|
1126 | nk = regfi_parse_nk(file, offset+hbin_offset, hbin_size-hbin_offset, true); |
---|
1127 | if(nk != NULL) |
---|
1128 | { |
---|
1129 | if((nk->key_type == REGFI_NK_TYPE_ROOTKEY1) |
---|
1130 | || (nk->key_type == REGFI_NK_TYPE_ROOTKEY2)) |
---|
1131 | { |
---|
1132 | found = true; |
---|
1133 | *root_offset = nk->offset; |
---|
1134 | } |
---|
1135 | free(nk); |
---|
1136 | } |
---|
1137 | } |
---|
1138 | |
---|
1139 | hbin_offset += record_size; |
---|
1140 | } |
---|
1141 | |
---|
1142 | return found; |
---|
1143 | } |
---|
1144 | |
---|
1145 | |
---|
1146 | /******************************************************************* |
---|
1147 | * Open the registry file and then read in the REGF block to get the |
---|
1148 | * first hbin offset. |
---|
1149 | *******************************************************************/ |
---|
1150 | REGFI_FILE* regfi_open(const char* filename) |
---|
1151 | { |
---|
1152 | struct stat sbuf; |
---|
1153 | REGFI_FILE* rb; |
---|
1154 | REGFI_HBIN* hbin = NULL; |
---|
1155 | uint32 hbin_off, file_length; |
---|
1156 | int fd; |
---|
1157 | bool rla; |
---|
1158 | |
---|
1159 | /* open an existing file */ |
---|
1160 | if ((fd = open(filename, REGFI_OPEN_FLAGS)) == -1) |
---|
1161 | { |
---|
1162 | /* fprintf(stderr, "regfi_open: failure to open %s (%s)\n", filename, strerror(errno));*/ |
---|
1163 | return NULL; |
---|
1164 | } |
---|
1165 | |
---|
1166 | /* Determine file length. Must be at least big enough |
---|
1167 | * for the header and one hbin. |
---|
1168 | */ |
---|
1169 | if (fstat(fd, &sbuf) == -1) |
---|
1170 | return NULL; |
---|
1171 | file_length = sbuf.st_size; |
---|
1172 | if(file_length < REGFI_REGF_SIZE+REGFI_HBIN_ALLOC) |
---|
1173 | return NULL; |
---|
1174 | |
---|
1175 | /* read in an existing file */ |
---|
1176 | if ((rb = regfi_parse_regf(fd, true)) == NULL) |
---|
1177 | { |
---|
1178 | /* fprintf(stderr, "regfi_open: Failed to read initial REGF block\n"); */ |
---|
1179 | close(fd); |
---|
1180 | return NULL; |
---|
1181 | } |
---|
1182 | rb->file_length = file_length; |
---|
1183 | |
---|
1184 | rb->hbins = range_list_new(); |
---|
1185 | if(rb->hbins == NULL) |
---|
1186 | { |
---|
1187 | /* fprintf(stderr, "regfi_open: Failed to create HBIN list.\n"); */ |
---|
1188 | range_list_free(rb->hbins); |
---|
1189 | close(fd); |
---|
1190 | free(rb); |
---|
1191 | return NULL; |
---|
1192 | } |
---|
1193 | |
---|
1194 | rla = true; |
---|
1195 | hbin_off = REGFI_REGF_SIZE; |
---|
1196 | hbin = regfi_parse_hbin(rb, hbin_off, true); |
---|
1197 | while(hbin && rla) |
---|
1198 | { |
---|
1199 | rla = range_list_add(rb->hbins, hbin->file_off, hbin->block_size, hbin); |
---|
1200 | hbin_off = hbin->file_off + hbin->block_size; |
---|
1201 | hbin = regfi_parse_hbin(rb, hbin_off, true); |
---|
1202 | } |
---|
1203 | |
---|
1204 | /* Default message mask */ |
---|
1205 | rb->msg_mask = REGFI_MSG_ERROR|REGFI_MSG_WARN; |
---|
1206 | |
---|
1207 | /* success */ |
---|
1208 | return rb; |
---|
1209 | } |
---|
1210 | |
---|
1211 | |
---|
1212 | /******************************************************************* |
---|
1213 | *******************************************************************/ |
---|
1214 | int regfi_close( REGFI_FILE *file ) |
---|
1215 | { |
---|
1216 | int fd; |
---|
1217 | uint32 i; |
---|
1218 | |
---|
1219 | /* nothing to do if there is no open file */ |
---|
1220 | if ((file == NULL) || (file->fd == -1)) |
---|
1221 | return 0; |
---|
1222 | |
---|
1223 | fd = file->fd; |
---|
1224 | file->fd = -1; |
---|
1225 | for(i=0; i < range_list_size(file->hbins); i++) |
---|
1226 | free(range_list_get(file->hbins, i)->data); |
---|
1227 | range_list_free(file->hbins); |
---|
1228 | |
---|
1229 | free(file); |
---|
1230 | |
---|
1231 | return close(fd); |
---|
1232 | } |
---|
1233 | |
---|
1234 | |
---|
1235 | /****************************************************************************** |
---|
1236 | * There should be only *one* root key in the registry file based |
---|
1237 | * on my experience. --jerry |
---|
1238 | *****************************************************************************/ |
---|
1239 | REGFI_NK_REC* regfi_rootkey(REGFI_FILE *file) |
---|
1240 | { |
---|
1241 | REGFI_NK_REC* nk = NULL; |
---|
1242 | REGFI_HBIN* hbin; |
---|
1243 | uint32 root_offset, i, num_hbins; |
---|
1244 | |
---|
1245 | if(!file) |
---|
1246 | return NULL; |
---|
1247 | |
---|
1248 | /* Scan through the file one HBIN block at a time looking |
---|
1249 | for an NK record with a type == 0x002c. |
---|
1250 | Normally this is the first nk record in the first hbin |
---|
1251 | block (but I'm not assuming that for now) */ |
---|
1252 | |
---|
1253 | num_hbins = range_list_size(file->hbins); |
---|
1254 | for(i=0; i < num_hbins; i++) |
---|
1255 | { |
---|
1256 | hbin = (REGFI_HBIN*)range_list_get(file->hbins, i)->data; |
---|
1257 | if(regfi_find_root_nk(file, hbin->file_off+REGFI_HBIN_HEADER_SIZE, |
---|
1258 | hbin->block_size-REGFI_HBIN_HEADER_SIZE, &root_offset)) |
---|
1259 | { |
---|
1260 | nk = regfi_load_key(file, root_offset, true); |
---|
1261 | break; |
---|
1262 | } |
---|
1263 | } |
---|
1264 | |
---|
1265 | return nk; |
---|
1266 | } |
---|
1267 | |
---|
1268 | |
---|
1269 | /****************************************************************************** |
---|
1270 | *****************************************************************************/ |
---|
1271 | void regfi_key_free(REGFI_NK_REC* nk) |
---|
1272 | { |
---|
1273 | uint32 i; |
---|
1274 | |
---|
1275 | if((nk->values != NULL) && (nk->values_off!=REGFI_OFFSET_NONE)) |
---|
1276 | { |
---|
1277 | for(i=0; i < nk->num_values; i++) |
---|
1278 | { |
---|
1279 | if(nk->values[i]->valuename != NULL) |
---|
1280 | free(nk->values[i]->valuename); |
---|
1281 | if(nk->values[i]->data != NULL) |
---|
1282 | free(nk->values[i]->data); |
---|
1283 | free(nk->values[i]); |
---|
1284 | } |
---|
1285 | free(nk->values); |
---|
1286 | } |
---|
1287 | |
---|
1288 | regfi_subkeylist_free(nk->subkeys); |
---|
1289 | |
---|
1290 | if(nk->keyname != NULL) |
---|
1291 | free(nk->keyname); |
---|
1292 | if(nk->classname != NULL) |
---|
1293 | free(nk->classname); |
---|
1294 | |
---|
1295 | /* XXX: not freeing sec_desc because these are cached. This needs to be reviewed. */ |
---|
1296 | free(nk); |
---|
1297 | } |
---|
1298 | |
---|
1299 | |
---|
1300 | /****************************************************************************** |
---|
1301 | *****************************************************************************/ |
---|
1302 | void regfi_subkeylist_free(REGFI_SUBKEY_LIST* list) |
---|
1303 | { |
---|
1304 | if(list != NULL) |
---|
1305 | { |
---|
1306 | free(list->elements); |
---|
1307 | free(list); |
---|
1308 | } |
---|
1309 | } |
---|
1310 | |
---|
1311 | |
---|
1312 | /****************************************************************************** |
---|
1313 | *****************************************************************************/ |
---|
1314 | REGFI_ITERATOR* regfi_iterator_new(REGFI_FILE* fh) |
---|
1315 | { |
---|
1316 | REGFI_NK_REC* root; |
---|
1317 | REGFI_ITERATOR* ret_val = (REGFI_ITERATOR*)malloc(sizeof(REGFI_ITERATOR)); |
---|
1318 | if(ret_val == NULL) |
---|
1319 | return NULL; |
---|
1320 | |
---|
1321 | root = regfi_rootkey(fh); |
---|
1322 | if(root == NULL) |
---|
1323 | { |
---|
1324 | free(ret_val); |
---|
1325 | return NULL; |
---|
1326 | } |
---|
1327 | |
---|
1328 | ret_val->key_positions = void_stack_new(REGFI_MAX_DEPTH); |
---|
1329 | if(ret_val->key_positions == NULL) |
---|
1330 | { |
---|
1331 | free(ret_val); |
---|
1332 | free(root); |
---|
1333 | return NULL; |
---|
1334 | } |
---|
1335 | |
---|
1336 | /* This secret isn't very secret, but we don't need a good one. This |
---|
1337 | * secret is just designed to prevent someone from trying to blow our |
---|
1338 | * caching and make things slow. |
---|
1339 | */ |
---|
1340 | ret_val->sk_recs = lru_cache_create(127, 0x15DEAD05^time(NULL)^(getpid()<<16), |
---|
1341 | true); |
---|
1342 | |
---|
1343 | ret_val->f = fh; |
---|
1344 | ret_val->cur_key = root; |
---|
1345 | ret_val->cur_subkey = 0; |
---|
1346 | ret_val->cur_value = 0; |
---|
1347 | |
---|
1348 | return ret_val; |
---|
1349 | } |
---|
1350 | |
---|
1351 | |
---|
1352 | /****************************************************************************** |
---|
1353 | *****************************************************************************/ |
---|
1354 | void regfi_iterator_free(REGFI_ITERATOR* i) |
---|
1355 | { |
---|
1356 | REGFI_ITER_POSITION* cur; |
---|
1357 | |
---|
1358 | if(i->cur_key != NULL) |
---|
1359 | regfi_key_free(i->cur_key); |
---|
1360 | |
---|
1361 | while((cur = (REGFI_ITER_POSITION*)void_stack_pop(i->key_positions)) != NULL) |
---|
1362 | { |
---|
1363 | regfi_key_free(cur->nk); |
---|
1364 | free(cur); |
---|
1365 | } |
---|
1366 | |
---|
1367 | lru_cache_destroy(i->sk_recs); |
---|
1368 | |
---|
1369 | free(i); |
---|
1370 | } |
---|
1371 | |
---|
1372 | |
---|
1373 | |
---|
1374 | /****************************************************************************** |
---|
1375 | *****************************************************************************/ |
---|
1376 | /* XXX: some way of indicating reason for failure should be added. */ |
---|
1377 | bool regfi_iterator_down(REGFI_ITERATOR* i) |
---|
1378 | { |
---|
1379 | REGFI_NK_REC* subkey; |
---|
1380 | REGFI_ITER_POSITION* pos; |
---|
1381 | |
---|
1382 | pos = (REGFI_ITER_POSITION*)malloc(sizeof(REGFI_ITER_POSITION)); |
---|
1383 | if(pos == NULL) |
---|
1384 | return false; |
---|
1385 | |
---|
1386 | subkey = (REGFI_NK_REC*)regfi_iterator_cur_subkey(i); |
---|
1387 | if(subkey == NULL) |
---|
1388 | { |
---|
1389 | free(pos); |
---|
1390 | return false; |
---|
1391 | } |
---|
1392 | |
---|
1393 | pos->nk = i->cur_key; |
---|
1394 | pos->cur_subkey = i->cur_subkey; |
---|
1395 | if(!void_stack_push(i->key_positions, pos)) |
---|
1396 | { |
---|
1397 | free(pos); |
---|
1398 | regfi_key_free(subkey); |
---|
1399 | return false; |
---|
1400 | } |
---|
1401 | |
---|
1402 | i->cur_key = subkey; |
---|
1403 | i->cur_subkey = 0; |
---|
1404 | i->cur_value = 0; |
---|
1405 | |
---|
1406 | return true; |
---|
1407 | } |
---|
1408 | |
---|
1409 | |
---|
1410 | /****************************************************************************** |
---|
1411 | *****************************************************************************/ |
---|
1412 | bool regfi_iterator_up(REGFI_ITERATOR* i) |
---|
1413 | { |
---|
1414 | REGFI_ITER_POSITION* pos; |
---|
1415 | |
---|
1416 | pos = (REGFI_ITER_POSITION*)void_stack_pop(i->key_positions); |
---|
1417 | if(pos == NULL) |
---|
1418 | return false; |
---|
1419 | |
---|
1420 | regfi_key_free(i->cur_key); |
---|
1421 | i->cur_key = pos->nk; |
---|
1422 | i->cur_subkey = pos->cur_subkey; |
---|
1423 | i->cur_value = 0; |
---|
1424 | free(pos); |
---|
1425 | |
---|
1426 | return true; |
---|
1427 | } |
---|
1428 | |
---|
1429 | |
---|
1430 | /****************************************************************************** |
---|
1431 | *****************************************************************************/ |
---|
1432 | bool regfi_iterator_to_root(REGFI_ITERATOR* i) |
---|
1433 | { |
---|
1434 | while(regfi_iterator_up(i)) |
---|
1435 | continue; |
---|
1436 | |
---|
1437 | return true; |
---|
1438 | } |
---|
1439 | |
---|
1440 | |
---|
1441 | /****************************************************************************** |
---|
1442 | *****************************************************************************/ |
---|
1443 | bool regfi_iterator_find_subkey(REGFI_ITERATOR* i, const char* subkey_name) |
---|
1444 | { |
---|
1445 | REGFI_NK_REC* subkey; |
---|
1446 | bool found = false; |
---|
1447 | uint32 old_subkey = i->cur_subkey; |
---|
1448 | |
---|
1449 | if(subkey_name == NULL) |
---|
1450 | return false; |
---|
1451 | |
---|
1452 | /* XXX: this alloc/free of each sub key might be a bit excessive */ |
---|
1453 | subkey = (REGFI_NK_REC*)regfi_iterator_first_subkey(i); |
---|
1454 | while((subkey != NULL) && (found == false)) |
---|
1455 | { |
---|
1456 | if(subkey->keyname != NULL |
---|
1457 | && strcasecmp(subkey->keyname, subkey_name) == 0) |
---|
1458 | found = true; |
---|
1459 | else |
---|
1460 | { |
---|
1461 | regfi_key_free(subkey); |
---|
1462 | subkey = (REGFI_NK_REC*)regfi_iterator_next_subkey(i); |
---|
1463 | } |
---|
1464 | } |
---|
1465 | |
---|
1466 | if(found == false) |
---|
1467 | { |
---|
1468 | i->cur_subkey = old_subkey; |
---|
1469 | return false; |
---|
1470 | } |
---|
1471 | |
---|
1472 | regfi_key_free(subkey); |
---|
1473 | return true; |
---|
1474 | } |
---|
1475 | |
---|
1476 | |
---|
1477 | /****************************************************************************** |
---|
1478 | *****************************************************************************/ |
---|
1479 | bool regfi_iterator_walk_path(REGFI_ITERATOR* i, const char** path) |
---|
1480 | { |
---|
1481 | uint32 x; |
---|
1482 | if(path == NULL) |
---|
1483 | return false; |
---|
1484 | |
---|
1485 | for(x=0; |
---|
1486 | ((path[x] != NULL) && regfi_iterator_find_subkey(i, path[x]) |
---|
1487 | && regfi_iterator_down(i)); |
---|
1488 | x++) |
---|
1489 | { continue; } |
---|
1490 | |
---|
1491 | if(path[x] == NULL) |
---|
1492 | return true; |
---|
1493 | |
---|
1494 | /* XXX: is this the right number of times? */ |
---|
1495 | for(; x > 0; x--) |
---|
1496 | regfi_iterator_up(i); |
---|
1497 | |
---|
1498 | return false; |
---|
1499 | } |
---|
1500 | |
---|
1501 | |
---|
1502 | /****************************************************************************** |
---|
1503 | *****************************************************************************/ |
---|
1504 | const REGFI_NK_REC* regfi_iterator_cur_key(REGFI_ITERATOR* i) |
---|
1505 | { |
---|
1506 | return i->cur_key; |
---|
1507 | } |
---|
1508 | |
---|
1509 | |
---|
1510 | /****************************************************************************** |
---|
1511 | *****************************************************************************/ |
---|
1512 | const REGFI_SK_REC* regfi_iterator_cur_sk(REGFI_ITERATOR* i) |
---|
1513 | { |
---|
1514 | REGFI_SK_REC* ret_val = NULL; |
---|
1515 | REGFI_HBIN* hbin; |
---|
1516 | uint32 max_length, off; |
---|
1517 | |
---|
1518 | if(i->cur_key == NULL) |
---|
1519 | return NULL; |
---|
1520 | |
---|
1521 | /* First look if we have already parsed it */ |
---|
1522 | if((i->cur_key->sk_off!=REGFI_OFFSET_NONE) |
---|
1523 | && !(ret_val =(REGFI_SK_REC*)lru_cache_find(i->sk_recs, |
---|
1524 | &i->cur_key->sk_off, 4))) |
---|
1525 | { |
---|
1526 | hbin = regfi_lookup_hbin(i->f, i->cur_key->sk_off); |
---|
1527 | |
---|
1528 | if(hbin == NULL) |
---|
1529 | return NULL; |
---|
1530 | |
---|
1531 | off = i->cur_key->sk_off + REGFI_REGF_SIZE; |
---|
1532 | max_length = hbin->block_size + hbin->file_off - off; |
---|
1533 | ret_val = regfi_parse_sk(i->f, off, max_length, true); |
---|
1534 | if(ret_val == NULL) |
---|
1535 | return NULL; |
---|
1536 | |
---|
1537 | ret_val->sk_off = i->cur_key->sk_off; |
---|
1538 | lru_cache_update(i->sk_recs, &i->cur_key->sk_off, 4, ret_val); |
---|
1539 | } |
---|
1540 | |
---|
1541 | return ret_val; |
---|
1542 | } |
---|
1543 | |
---|
1544 | |
---|
1545 | |
---|
1546 | /****************************************************************************** |
---|
1547 | *****************************************************************************/ |
---|
1548 | const REGFI_NK_REC* regfi_iterator_first_subkey(REGFI_ITERATOR* i) |
---|
1549 | { |
---|
1550 | i->cur_subkey = 0; |
---|
1551 | return regfi_iterator_cur_subkey(i); |
---|
1552 | } |
---|
1553 | |
---|
1554 | |
---|
1555 | /****************************************************************************** |
---|
1556 | *****************************************************************************/ |
---|
1557 | const REGFI_NK_REC* regfi_iterator_cur_subkey(REGFI_ITERATOR* i) |
---|
1558 | { |
---|
1559 | uint32 nk_offset; |
---|
1560 | |
---|
1561 | /* see if there is anything left to report */ |
---|
1562 | if (!(i->cur_key) || (i->cur_key->subkeys_off==REGFI_OFFSET_NONE) |
---|
1563 | || (i->cur_subkey >= i->cur_key->num_subkeys)) |
---|
1564 | return NULL; |
---|
1565 | |
---|
1566 | nk_offset = i->cur_key->subkeys->elements[i->cur_subkey].offset; |
---|
1567 | |
---|
1568 | return regfi_load_key(i->f, nk_offset+REGFI_REGF_SIZE, true); |
---|
1569 | } |
---|
1570 | |
---|
1571 | |
---|
1572 | /****************************************************************************** |
---|
1573 | *****************************************************************************/ |
---|
1574 | /* XXX: some way of indicating reason for failure should be added. */ |
---|
1575 | const REGFI_NK_REC* regfi_iterator_next_subkey(REGFI_ITERATOR* i) |
---|
1576 | { |
---|
1577 | const REGFI_NK_REC* subkey; |
---|
1578 | |
---|
1579 | i->cur_subkey++; |
---|
1580 | subkey = regfi_iterator_cur_subkey(i); |
---|
1581 | |
---|
1582 | if(subkey == NULL) |
---|
1583 | i->cur_subkey--; |
---|
1584 | |
---|
1585 | return subkey; |
---|
1586 | } |
---|
1587 | |
---|
1588 | |
---|
1589 | /****************************************************************************** |
---|
1590 | *****************************************************************************/ |
---|
1591 | bool regfi_iterator_find_value(REGFI_ITERATOR* i, const char* value_name) |
---|
1592 | { |
---|
1593 | const REGFI_VK_REC* cur; |
---|
1594 | bool found = false; |
---|
1595 | |
---|
1596 | /* XXX: cur->valuename can be NULL in the registry. |
---|
1597 | * Should we allow for a way to search for that? |
---|
1598 | */ |
---|
1599 | if(value_name == NULL) |
---|
1600 | return false; |
---|
1601 | |
---|
1602 | cur = regfi_iterator_first_value(i); |
---|
1603 | while((cur != NULL) && (found == false)) |
---|
1604 | { |
---|
1605 | if((cur->valuename != NULL) |
---|
1606 | && (strcasecmp(cur->valuename, value_name) == 0)) |
---|
1607 | found = true; |
---|
1608 | else |
---|
1609 | cur = regfi_iterator_next_value(i); |
---|
1610 | } |
---|
1611 | |
---|
1612 | return found; |
---|
1613 | } |
---|
1614 | |
---|
1615 | |
---|
1616 | /****************************************************************************** |
---|
1617 | *****************************************************************************/ |
---|
1618 | const REGFI_VK_REC* regfi_iterator_first_value(REGFI_ITERATOR* i) |
---|
1619 | { |
---|
1620 | i->cur_value = 0; |
---|
1621 | return regfi_iterator_cur_value(i); |
---|
1622 | } |
---|
1623 | |
---|
1624 | |
---|
1625 | /****************************************************************************** |
---|
1626 | *****************************************************************************/ |
---|
1627 | const REGFI_VK_REC* regfi_iterator_cur_value(REGFI_ITERATOR* i) |
---|
1628 | { |
---|
1629 | REGFI_VK_REC* ret_val = NULL; |
---|
1630 | if(i->cur_value < i->cur_key->num_values) |
---|
1631 | ret_val = i->cur_key->values[i->cur_value]; |
---|
1632 | |
---|
1633 | return ret_val; |
---|
1634 | } |
---|
1635 | |
---|
1636 | |
---|
1637 | /****************************************************************************** |
---|
1638 | *****************************************************************************/ |
---|
1639 | const REGFI_VK_REC* regfi_iterator_next_value(REGFI_ITERATOR* i) |
---|
1640 | { |
---|
1641 | const REGFI_VK_REC* ret_val; |
---|
1642 | |
---|
1643 | i->cur_value++; |
---|
1644 | ret_val = regfi_iterator_cur_value(i); |
---|
1645 | if(ret_val == NULL) |
---|
1646 | i->cur_value--; |
---|
1647 | |
---|
1648 | return ret_val; |
---|
1649 | } |
---|
1650 | |
---|
1651 | |
---|
1652 | |
---|
1653 | /******************************************************************* |
---|
1654 | * Computes the checksum of the registry file header. |
---|
1655 | * buffer must be at least the size of an regf header (4096 bytes). |
---|
1656 | *******************************************************************/ |
---|
1657 | static uint32 regfi_compute_header_checksum(uint8* buffer) |
---|
1658 | { |
---|
1659 | uint32 checksum, x; |
---|
1660 | int i; |
---|
1661 | |
---|
1662 | /* XOR of all bytes 0x0000 - 0x01FB */ |
---|
1663 | |
---|
1664 | checksum = x = 0; |
---|
1665 | |
---|
1666 | for ( i=0; i<0x01FB; i+=4 ) { |
---|
1667 | x = IVAL(buffer, i ); |
---|
1668 | checksum ^= x; |
---|
1669 | } |
---|
1670 | |
---|
1671 | return checksum; |
---|
1672 | } |
---|
1673 | |
---|
1674 | |
---|
1675 | /******************************************************************* |
---|
1676 | * XXX: Add way to return more detailed error information. |
---|
1677 | *******************************************************************/ |
---|
1678 | REGFI_FILE* regfi_parse_regf(int fd, bool strict) |
---|
1679 | { |
---|
1680 | uint8 file_header[REGFI_REGF_SIZE]; |
---|
1681 | uint32 length; |
---|
1682 | REGFI_FILE* ret_val; |
---|
1683 | |
---|
1684 | ret_val = (REGFI_FILE*)zalloc(sizeof(REGFI_FILE)); |
---|
1685 | if(ret_val == NULL) |
---|
1686 | return NULL; |
---|
1687 | |
---|
1688 | ret_val->fd = fd; |
---|
1689 | |
---|
1690 | length = REGFI_REGF_SIZE; |
---|
1691 | if((regfi_read(fd, file_header, &length)) != 0 |
---|
1692 | || length != REGFI_REGF_SIZE) |
---|
1693 | { |
---|
1694 | free(ret_val); |
---|
1695 | return NULL; |
---|
1696 | } |
---|
1697 | |
---|
1698 | ret_val->checksum = IVAL(file_header, 0x1FC); |
---|
1699 | ret_val->computed_checksum = regfi_compute_header_checksum(file_header); |
---|
1700 | if (strict && (ret_val->checksum != ret_val->computed_checksum)) |
---|
1701 | { |
---|
1702 | free(ret_val); |
---|
1703 | return NULL; |
---|
1704 | } |
---|
1705 | |
---|
1706 | memcpy(ret_val->magic, file_header, REGFI_REGF_MAGIC_SIZE); |
---|
1707 | if(strict && (memcmp(ret_val->magic, "regf", REGFI_REGF_MAGIC_SIZE) != 0)) |
---|
1708 | { |
---|
1709 | free(ret_val); |
---|
1710 | return NULL; |
---|
1711 | } |
---|
1712 | |
---|
1713 | ret_val->unknown1 = IVAL(file_header, 0x4); |
---|
1714 | ret_val->unknown2 = IVAL(file_header, 0x8); |
---|
1715 | |
---|
1716 | ret_val->mtime.low = IVAL(file_header, 0xC); |
---|
1717 | ret_val->mtime.high = IVAL(file_header, 0x10); |
---|
1718 | |
---|
1719 | ret_val->unknown3 = IVAL(file_header, 0x14); |
---|
1720 | ret_val->unknown4 = IVAL(file_header, 0x18); |
---|
1721 | ret_val->unknown5 = IVAL(file_header, 0x1C); |
---|
1722 | ret_val->unknown6 = IVAL(file_header, 0x20); |
---|
1723 | |
---|
1724 | ret_val->data_offset = IVAL(file_header, 0x24); |
---|
1725 | ret_val->last_block = IVAL(file_header, 0x28); |
---|
1726 | |
---|
1727 | ret_val->unknown7 = IVAL(file_header, 0x2C); |
---|
1728 | |
---|
1729 | return ret_val; |
---|
1730 | } |
---|
1731 | |
---|
1732 | |
---|
1733 | |
---|
1734 | /******************************************************************* |
---|
1735 | * Given real file offset, read and parse the hbin at that location |
---|
1736 | * along with it's associated cells. |
---|
1737 | *******************************************************************/ |
---|
1738 | /* XXX: Need a way to return types of errors. |
---|
1739 | */ |
---|
1740 | REGFI_HBIN* regfi_parse_hbin(REGFI_FILE* file, uint32 offset, bool strict) |
---|
1741 | { |
---|
1742 | REGFI_HBIN *hbin; |
---|
1743 | uint8 hbin_header[REGFI_HBIN_HEADER_SIZE]; |
---|
1744 | uint32 length; |
---|
1745 | |
---|
1746 | if(offset >= file->file_length) |
---|
1747 | return NULL; |
---|
1748 | |
---|
1749 | if(lseek(file->fd, offset, SEEK_SET) == -1) |
---|
1750 | { |
---|
1751 | regfi_add_message(file, REGFI_MSG_ERROR, "Seek failed" |
---|
1752 | " while parsing hbin at offset 0x%.8X.", offset); |
---|
1753 | return NULL; |
---|
1754 | } |
---|
1755 | |
---|
1756 | length = REGFI_HBIN_HEADER_SIZE; |
---|
1757 | if((regfi_read(file->fd, hbin_header, &length) != 0) |
---|
1758 | || length != REGFI_HBIN_HEADER_SIZE) |
---|
1759 | return NULL; |
---|
1760 | |
---|
1761 | if(lseek(file->fd, offset, SEEK_SET) == -1) |
---|
1762 | { |
---|
1763 | regfi_add_message(file, REGFI_MSG_ERROR, "Seek failed" |
---|
1764 | " while parsing hbin at offset 0x%.8X.", offset); |
---|
1765 | return NULL; |
---|
1766 | } |
---|
1767 | |
---|
1768 | if(!(hbin = (REGFI_HBIN*)zalloc(sizeof(REGFI_HBIN)))) |
---|
1769 | return NULL; |
---|
1770 | hbin->file_off = offset; |
---|
1771 | |
---|
1772 | memcpy(hbin->magic, hbin_header, 4); |
---|
1773 | if(strict && (memcmp(hbin->magic, "hbin", 4) != 0)) |
---|
1774 | { |
---|
1775 | regfi_add_message(file, REGFI_MSG_INFO, "Magic number mismatch " |
---|
1776 | "(%.2X %.2X %.2X %.2X) while parsing hbin at offset" |
---|
1777 | " 0x%.8X.", hbin->magic[0], hbin->magic[1], |
---|
1778 | hbin->magic[2], hbin->magic[3], offset); |
---|
1779 | free(hbin); |
---|
1780 | return NULL; |
---|
1781 | } |
---|
1782 | |
---|
1783 | hbin->first_hbin_off = IVAL(hbin_header, 0x4); |
---|
1784 | hbin->block_size = IVAL(hbin_header, 0x8); |
---|
1785 | /* this should be the same thing as hbin->block_size but just in case */ |
---|
1786 | hbin->next_block = IVAL(hbin_header, 0x1C); |
---|
1787 | |
---|
1788 | |
---|
1789 | /* Ensure the block size is a multiple of 0x1000 and doesn't run off |
---|
1790 | * the end of the file. |
---|
1791 | */ |
---|
1792 | /* XXX: This may need to be relaxed for dealing with |
---|
1793 | * partial or corrupt files. |
---|
1794 | */ |
---|
1795 | if((offset + hbin->block_size > file->file_length) |
---|
1796 | || (hbin->block_size & 0xFFFFF000) != hbin->block_size) |
---|
1797 | { |
---|
1798 | regfi_add_message(file, REGFI_MSG_ERROR, "The hbin offset is not aligned" |
---|
1799 | " or runs off the end of the file" |
---|
1800 | " while parsing hbin at offset 0x%.8X.", offset); |
---|
1801 | free(hbin); |
---|
1802 | return NULL; |
---|
1803 | } |
---|
1804 | |
---|
1805 | return hbin; |
---|
1806 | } |
---|
1807 | |
---|
1808 | |
---|
1809 | /******************************************************************* |
---|
1810 | *******************************************************************/ |
---|
1811 | REGFI_NK_REC* regfi_parse_nk(REGFI_FILE* file, uint32 offset, |
---|
1812 | uint32 max_size, bool strict) |
---|
1813 | { |
---|
1814 | uint8 nk_header[REGFI_NK_MIN_LENGTH]; |
---|
1815 | REGFI_HBIN *hbin; |
---|
1816 | REGFI_NK_REC* ret_val; |
---|
1817 | uint32 length,cell_length; |
---|
1818 | uint32 class_offset, class_maxsize; |
---|
1819 | bool unalloc = false; |
---|
1820 | |
---|
1821 | if(!regfi_parse_cell(file->fd, offset, nk_header, REGFI_NK_MIN_LENGTH, |
---|
1822 | &cell_length, &unalloc)) |
---|
1823 | { |
---|
1824 | regfi_add_message(file, REGFI_MSG_WARN, "Could not parse cell header" |
---|
1825 | " while parsing NK record at offset 0x%.8X.", offset); |
---|
1826 | return NULL; |
---|
1827 | } |
---|
1828 | |
---|
1829 | /* A bit of validation before bothering to allocate memory */ |
---|
1830 | if((nk_header[0x0] != 'n') || (nk_header[0x1] != 'k')) |
---|
1831 | { |
---|
1832 | regfi_add_message(file, REGFI_MSG_WARN, "Magic number mismatch in parsing" |
---|
1833 | " NK record at offset 0x%.8X.", offset); |
---|
1834 | return NULL; |
---|
1835 | } |
---|
1836 | |
---|
1837 | ret_val = (REGFI_NK_REC*)zalloc(sizeof(REGFI_NK_REC)); |
---|
1838 | if(ret_val == NULL) |
---|
1839 | { |
---|
1840 | regfi_add_message(file, REGFI_MSG_ERROR, "Failed to allocate memory while" |
---|
1841 | " parsing NK record at offset 0x%.8X.", offset); |
---|
1842 | return NULL; |
---|
1843 | } |
---|
1844 | |
---|
1845 | ret_val->offset = offset; |
---|
1846 | ret_val->cell_size = cell_length; |
---|
1847 | |
---|
1848 | if(ret_val->cell_size > max_size) |
---|
1849 | ret_val->cell_size = max_size & 0xFFFFFFF8; |
---|
1850 | if((ret_val->cell_size < REGFI_NK_MIN_LENGTH) |
---|
1851 | || (strict && ret_val->cell_size != (ret_val->cell_size & 0xFFFFFFF8))) |
---|
1852 | { |
---|
1853 | regfi_add_message(file, REGFI_MSG_WARN, "A length check failed while" |
---|
1854 | " parsing NK record at offset 0x%.8X.", offset); |
---|
1855 | free(ret_val); |
---|
1856 | return NULL; |
---|
1857 | } |
---|
1858 | |
---|
1859 | ret_val->magic[0] = nk_header[0x0]; |
---|
1860 | ret_val->magic[1] = nk_header[0x1]; |
---|
1861 | ret_val->key_type = SVAL(nk_header, 0x2); |
---|
1862 | if((ret_val->key_type != REGFI_NK_TYPE_NORMALKEY) |
---|
1863 | && (ret_val->key_type != REGFI_NK_TYPE_ROOTKEY1) |
---|
1864 | && (ret_val->key_type != REGFI_NK_TYPE_ROOTKEY2) |
---|
1865 | && (ret_val->key_type != REGFI_NK_TYPE_LINKKEY) |
---|
1866 | && (ret_val->key_type != REGFI_NK_TYPE_UNKNOWN1) |
---|
1867 | && (ret_val->key_type != REGFI_NK_TYPE_UNKNOWN2) |
---|
1868 | && (ret_val->key_type != REGFI_NK_TYPE_UNKNOWN3)) |
---|
1869 | { |
---|
1870 | regfi_add_message(file, REGFI_MSG_WARN, "Unknown key type (0x%.4X) while" |
---|
1871 | " parsing NK record at offset 0x%.8X.", |
---|
1872 | ret_val->key_type, offset); |
---|
1873 | } |
---|
1874 | |
---|
1875 | ret_val->mtime.low = IVAL(nk_header, 0x4); |
---|
1876 | ret_val->mtime.high = IVAL(nk_header, 0x8); |
---|
1877 | /* If the key is unallocated and the MTIME is earlier than Jan 1, 1990 |
---|
1878 | * or later than Jan 1, 2290, we consider this a bad key. This helps |
---|
1879 | * weed out some false positives during deleted data recovery. |
---|
1880 | */ |
---|
1881 | if(unalloc |
---|
1882 | && ((ret_val->mtime.high < REGFI_MTIME_MIN_HIGH |
---|
1883 | && ret_val->mtime.low < REGFI_MTIME_MIN_LOW) |
---|
1884 | || (ret_val->mtime.high > REGFI_MTIME_MAX_HIGH |
---|
1885 | && ret_val->mtime.low > REGFI_MTIME_MAX_LOW))) |
---|
1886 | return NULL; |
---|
1887 | |
---|
1888 | ret_val->unknown1 = IVAL(nk_header, 0xC); |
---|
1889 | ret_val->parent_off = IVAL(nk_header, 0x10); |
---|
1890 | ret_val->num_subkeys = IVAL(nk_header, 0x14); |
---|
1891 | ret_val->unknown2 = IVAL(nk_header, 0x18); |
---|
1892 | ret_val->subkeys_off = IVAL(nk_header, 0x1C); |
---|
1893 | ret_val->unknown3 = IVAL(nk_header, 0x20); |
---|
1894 | ret_val->num_values = IVAL(nk_header, 0x24); |
---|
1895 | ret_val->values_off = IVAL(nk_header, 0x28); |
---|
1896 | ret_val->sk_off = IVAL(nk_header, 0x2C); |
---|
1897 | ret_val->classname_off = IVAL(nk_header, 0x30); |
---|
1898 | |
---|
1899 | ret_val->max_bytes_subkeyname = IVAL(nk_header, 0x34); |
---|
1900 | ret_val->max_bytes_subkeyclassname = IVAL(nk_header, 0x38); |
---|
1901 | ret_val->max_bytes_valuename = IVAL(nk_header, 0x3C); |
---|
1902 | ret_val->max_bytes_value = IVAL(nk_header, 0x40); |
---|
1903 | ret_val->unk_index = IVAL(nk_header, 0x44); |
---|
1904 | |
---|
1905 | ret_val->name_length = SVAL(nk_header, 0x48); |
---|
1906 | ret_val->classname_length = SVAL(nk_header, 0x4A); |
---|
1907 | |
---|
1908 | |
---|
1909 | if(ret_val->name_length + REGFI_NK_MIN_LENGTH > ret_val->cell_size) |
---|
1910 | { |
---|
1911 | if(strict) |
---|
1912 | { |
---|
1913 | regfi_add_message(file, REGFI_MSG_ERROR, "Contents too large for cell" |
---|
1914 | " while parsing NK record at offset 0x%.8X.", offset); |
---|
1915 | free(ret_val); |
---|
1916 | return NULL; |
---|
1917 | } |
---|
1918 | else |
---|
1919 | ret_val->name_length = ret_val->cell_size - REGFI_NK_MIN_LENGTH; |
---|
1920 | } |
---|
1921 | else if (unalloc) |
---|
1922 | { /* Truncate cell_size if it's much larger than the apparent total record length. */ |
---|
1923 | /* Round up to the next multiple of 8 */ |
---|
1924 | length = (ret_val->name_length + REGFI_NK_MIN_LENGTH) & 0xFFFFFFF8; |
---|
1925 | if(length < ret_val->name_length + REGFI_NK_MIN_LENGTH) |
---|
1926 | length+=8; |
---|
1927 | |
---|
1928 | /* If cell_size is still greater, truncate. */ |
---|
1929 | if(length < ret_val->cell_size) |
---|
1930 | ret_val->cell_size = length; |
---|
1931 | } |
---|
1932 | |
---|
1933 | ret_val->keyname = (char*)zalloc(sizeof(char)*(ret_val->name_length+1)); |
---|
1934 | if(ret_val->keyname == NULL) |
---|
1935 | { |
---|
1936 | free(ret_val); |
---|
1937 | return NULL; |
---|
1938 | } |
---|
1939 | |
---|
1940 | /* Don't need to seek, should be at the right offset */ |
---|
1941 | length = ret_val->name_length; |
---|
1942 | if((regfi_read(file->fd, (uint8*)ret_val->keyname, &length) != 0) |
---|
1943 | || length != ret_val->name_length) |
---|
1944 | { |
---|
1945 | regfi_add_message(file, REGFI_MSG_ERROR, "Failed to read key name" |
---|
1946 | " while parsing NK record at offset 0x%.8X.", offset); |
---|
1947 | free(ret_val->keyname); |
---|
1948 | free(ret_val); |
---|
1949 | return NULL; |
---|
1950 | } |
---|
1951 | ret_val->keyname[ret_val->name_length] = '\0'; |
---|
1952 | |
---|
1953 | if(ret_val->classname_off != REGFI_OFFSET_NONE) |
---|
1954 | { |
---|
1955 | hbin = regfi_lookup_hbin(file, ret_val->classname_off); |
---|
1956 | if(hbin) |
---|
1957 | { |
---|
1958 | class_offset = ret_val->classname_off+REGFI_REGF_SIZE; |
---|
1959 | class_maxsize = hbin->block_size + hbin->file_off - class_offset; |
---|
1960 | ret_val->classname |
---|
1961 | = regfi_parse_classname(file, class_offset, &ret_val->classname_length, |
---|
1962 | class_maxsize, strict); |
---|
1963 | } |
---|
1964 | else |
---|
1965 | { |
---|
1966 | ret_val->classname = NULL; |
---|
1967 | regfi_add_message(file, REGFI_MSG_WARN, "Could not find hbin for class" |
---|
1968 | " name while parsing NK record at offset 0x%.8X.", |
---|
1969 | offset); |
---|
1970 | } |
---|
1971 | |
---|
1972 | if(ret_val->classname == NULL) |
---|
1973 | { |
---|
1974 | regfi_add_message(file, REGFI_MSG_WARN, "Could not parse class" |
---|
1975 | " name while parsing NK record at offset 0x%.8X.", |
---|
1976 | offset); |
---|
1977 | return NULL; |
---|
1978 | } |
---|
1979 | } |
---|
1980 | |
---|
1981 | return ret_val; |
---|
1982 | } |
---|
1983 | |
---|
1984 | |
---|
1985 | char* regfi_parse_classname(REGFI_FILE* file, uint32 offset, |
---|
1986 | uint16* name_length, uint32 max_size, bool strict) |
---|
1987 | { |
---|
1988 | char* ret_val = NULL; |
---|
1989 | uint32 length; |
---|
1990 | uint32 cell_length; |
---|
1991 | bool unalloc = false; |
---|
1992 | |
---|
1993 | if(*name_length > 0 && offset != REGFI_OFFSET_NONE |
---|
1994 | && offset == (offset & 0xFFFFFFF8)) |
---|
1995 | { |
---|
1996 | if(!regfi_parse_cell(file->fd, offset, NULL, 0, &cell_length, &unalloc)) |
---|
1997 | { |
---|
1998 | regfi_add_message(file, REGFI_MSG_WARN, "Could not parse cell header" |
---|
1999 | " while parsing class name at offset 0x%.8X.", offset); |
---|
2000 | return NULL; |
---|
2001 | } |
---|
2002 | |
---|
2003 | if((cell_length & 0xFFFFFFF8) != cell_length) |
---|
2004 | { |
---|
2005 | regfi_add_message(file, REGFI_MSG_ERROR, "Cell length not a multiple of 8" |
---|
2006 | " while parsing class name at offset 0x%.8X.", offset); |
---|
2007 | return NULL; |
---|
2008 | } |
---|
2009 | |
---|
2010 | if(cell_length > max_size) |
---|
2011 | { |
---|
2012 | regfi_add_message(file, REGFI_MSG_WARN, "Cell stretches past hbin " |
---|
2013 | "boundary while parsing class name at offset 0x%.8X.", |
---|
2014 | offset); |
---|
2015 | if(strict) |
---|
2016 | return NULL; |
---|
2017 | cell_length = max_size; |
---|
2018 | } |
---|
2019 | |
---|
2020 | if((cell_length - 4) < *name_length) |
---|
2021 | { |
---|
2022 | regfi_add_message(file, REGFI_MSG_WARN, "Class name is larger than" |
---|
2023 | " cell_length while parsing class name at offset" |
---|
2024 | " 0x%.8X.", offset); |
---|
2025 | if(strict) |
---|
2026 | return NULL; |
---|
2027 | *name_length = cell_length - 4; |
---|
2028 | } |
---|
2029 | |
---|
2030 | ret_val = (char*)zalloc(*name_length); |
---|
2031 | if(ret_val != NULL) |
---|
2032 | { |
---|
2033 | length = *name_length; |
---|
2034 | if((regfi_read(file->fd, (uint8*)ret_val, &length) != 0) |
---|
2035 | || length != *name_length) |
---|
2036 | { |
---|
2037 | regfi_add_message(file, REGFI_MSG_ERROR, "Could not read class name" |
---|
2038 | " while parsing class name at offset 0x%.8X.", offset); |
---|
2039 | free(ret_val); |
---|
2040 | return NULL; |
---|
2041 | } |
---|
2042 | } |
---|
2043 | } |
---|
2044 | |
---|
2045 | return ret_val; |
---|
2046 | } |
---|
2047 | |
---|
2048 | |
---|
2049 | /******************************************************************* |
---|
2050 | *******************************************************************/ |
---|
2051 | REGFI_VK_REC* regfi_parse_vk(REGFI_FILE* file, uint32 offset, |
---|
2052 | uint32 max_size, bool strict) |
---|
2053 | { |
---|
2054 | REGFI_VK_REC* ret_val; |
---|
2055 | REGFI_HBIN *hbin; |
---|
2056 | uint8 vk_header[REGFI_VK_MIN_LENGTH]; |
---|
2057 | uint32 raw_data_size, length, cell_length; |
---|
2058 | uint32 data_offset, data_maxsize; |
---|
2059 | bool unalloc = false; |
---|
2060 | |
---|
2061 | if(!regfi_parse_cell(file->fd, offset, vk_header, REGFI_VK_MIN_LENGTH, |
---|
2062 | &cell_length, &unalloc)) |
---|
2063 | { |
---|
2064 | regfi_add_message(file, REGFI_MSG_WARN, "Could not parse cell header" |
---|
2065 | " while parsing VK record at offset 0x%.8X.", offset); |
---|
2066 | return NULL; |
---|
2067 | } |
---|
2068 | |
---|
2069 | ret_val = (REGFI_VK_REC*)zalloc(sizeof(REGFI_VK_REC)); |
---|
2070 | if(ret_val == NULL) |
---|
2071 | return NULL; |
---|
2072 | |
---|
2073 | ret_val->offset = offset; |
---|
2074 | ret_val->cell_size = cell_length; |
---|
2075 | |
---|
2076 | if(ret_val->cell_size > max_size) |
---|
2077 | ret_val->cell_size = max_size & 0xFFFFFFF8; |
---|
2078 | if((ret_val->cell_size < REGFI_VK_MIN_LENGTH) |
---|
2079 | || ret_val->cell_size != (ret_val->cell_size & 0xFFFFFFF8)) |
---|
2080 | { |
---|
2081 | regfi_add_message(file, REGFI_MSG_WARN, "Invalid cell size encountered" |
---|
2082 | " while parsing VK record at offset 0x%.8X.", offset); |
---|
2083 | free(ret_val); |
---|
2084 | return NULL; |
---|
2085 | } |
---|
2086 | |
---|
2087 | ret_val->magic[0] = vk_header[0x0]; |
---|
2088 | ret_val->magic[1] = vk_header[0x1]; |
---|
2089 | if((ret_val->magic[0] != 'v') || (ret_val->magic[1] != 'k')) |
---|
2090 | { |
---|
2091 | /* XXX: This does not account for deleted keys under Win2K which |
---|
2092 | * often have this (and the name length) overwritten with |
---|
2093 | * 0xFFFF. |
---|
2094 | */ |
---|
2095 | regfi_add_message(file, REGFI_MSG_WARN, "Magic number mismatch" |
---|
2096 | " while parsing VK record at offset 0x%.8X.", offset); |
---|
2097 | free(ret_val); |
---|
2098 | return NULL; |
---|
2099 | } |
---|
2100 | |
---|
2101 | ret_val->name_length = SVAL(vk_header, 0x2); |
---|
2102 | raw_data_size = IVAL(vk_header, 0x4); |
---|
2103 | ret_val->data_size = raw_data_size & ~REGFI_VK_DATA_IN_OFFSET; |
---|
2104 | ret_val->data_in_offset = (bool)(raw_data_size & REGFI_VK_DATA_IN_OFFSET); |
---|
2105 | ret_val->data_off = IVAL(vk_header, 0x8); |
---|
2106 | ret_val->type = IVAL(vk_header, 0xC); |
---|
2107 | ret_val->flag = SVAL(vk_header, 0x10); |
---|
2108 | ret_val->unknown1 = SVAL(vk_header, 0x12); |
---|
2109 | |
---|
2110 | if(ret_val->flag & REGFI_VK_FLAG_NAME_PRESENT) |
---|
2111 | { |
---|
2112 | if(ret_val->name_length + REGFI_VK_MIN_LENGTH + 4 > ret_val->cell_size) |
---|
2113 | { |
---|
2114 | regfi_add_message(file, REGFI_MSG_WARN, "Name too long for remaining cell" |
---|
2115 | " space while parsing VK record at offset 0x%.8X.", |
---|
2116 | offset); |
---|
2117 | if(strict) |
---|
2118 | { |
---|
2119 | free(ret_val); |
---|
2120 | return NULL; |
---|
2121 | } |
---|
2122 | else |
---|
2123 | ret_val->name_length = ret_val->cell_size - REGFI_VK_MIN_LENGTH - 4; |
---|
2124 | } |
---|
2125 | |
---|
2126 | /* Round up to the next multiple of 8 */ |
---|
2127 | cell_length = (ret_val->name_length + REGFI_VK_MIN_LENGTH + 4) & 0xFFFFFFF8; |
---|
2128 | if(cell_length < ret_val->name_length + REGFI_VK_MIN_LENGTH + 4) |
---|
2129 | cell_length+=8; |
---|
2130 | |
---|
2131 | ret_val->valuename = (char*)zalloc(sizeof(char)*(ret_val->name_length+1)); |
---|
2132 | if(ret_val->valuename == NULL) |
---|
2133 | { |
---|
2134 | free(ret_val); |
---|
2135 | return NULL; |
---|
2136 | } |
---|
2137 | |
---|
2138 | length = ret_val->name_length; |
---|
2139 | if((regfi_read(file->fd, (uint8*)ret_val->valuename, &length) != 0) |
---|
2140 | || length != ret_val->name_length) |
---|
2141 | { |
---|
2142 | regfi_add_message(file, REGFI_MSG_ERROR, "Could not read value name" |
---|
2143 | " while parsing VK record at offset 0x%.8X.", offset); |
---|
2144 | free(ret_val->valuename); |
---|
2145 | free(ret_val); |
---|
2146 | return NULL; |
---|
2147 | } |
---|
2148 | ret_val->valuename[ret_val->name_length] = '\0'; |
---|
2149 | |
---|
2150 | } |
---|
2151 | else |
---|
2152 | cell_length = REGFI_VK_MIN_LENGTH + 4; |
---|
2153 | |
---|
2154 | if(unalloc) |
---|
2155 | { |
---|
2156 | /* If cell_size is still greater, truncate. */ |
---|
2157 | if(cell_length < ret_val->cell_size) |
---|
2158 | ret_val->cell_size = cell_length; |
---|
2159 | } |
---|
2160 | |
---|
2161 | if(ret_val->data_size == 0) |
---|
2162 | ret_val->data = NULL; |
---|
2163 | else |
---|
2164 | { |
---|
2165 | if(ret_val->data_in_offset) |
---|
2166 | { |
---|
2167 | ret_val->data = regfi_parse_data(file, ret_val->data_off, |
---|
2168 | raw_data_size, 4, strict); |
---|
2169 | } |
---|
2170 | else |
---|
2171 | { |
---|
2172 | hbin = regfi_lookup_hbin(file, ret_val->data_off); |
---|
2173 | if(hbin) |
---|
2174 | { |
---|
2175 | data_offset = ret_val->data_off+REGFI_REGF_SIZE; |
---|
2176 | data_maxsize = hbin->block_size + hbin->file_off - data_offset; |
---|
2177 | ret_val->data = regfi_parse_data(file, data_offset, raw_data_size, |
---|
2178 | data_maxsize, strict); |
---|
2179 | |
---|
2180 | } |
---|
2181 | else |
---|
2182 | { |
---|
2183 | regfi_add_message(file, REGFI_MSG_WARN, "Could not find hbin for data" |
---|
2184 | " while parsing VK record at offset 0x%.8X.", offset); |
---|
2185 | ret_val->data = NULL; |
---|
2186 | } |
---|
2187 | } |
---|
2188 | |
---|
2189 | if(ret_val->data == NULL) |
---|
2190 | { |
---|
2191 | regfi_add_message(file, REGFI_MSG_WARN, "Could not parse data record" |
---|
2192 | " while parsing VK record at offset 0x%.8X.", offset); |
---|
2193 | } |
---|
2194 | } |
---|
2195 | |
---|
2196 | return ret_val; |
---|
2197 | } |
---|
2198 | |
---|
2199 | |
---|
2200 | uint8* regfi_parse_data(REGFI_FILE* file, uint32 offset, uint32 length, |
---|
2201 | uint32 max_size, bool strict) |
---|
2202 | { |
---|
2203 | uint8* ret_val; |
---|
2204 | uint32 read_length, cell_length; |
---|
2205 | uint8 i; |
---|
2206 | bool unalloc; |
---|
2207 | |
---|
2208 | /* The data is typically stored in the offset if the size <= 4 */ |
---|
2209 | if (length & REGFI_VK_DATA_IN_OFFSET) |
---|
2210 | { |
---|
2211 | length = length & ~REGFI_VK_DATA_IN_OFFSET; |
---|
2212 | if(length > 4) |
---|
2213 | { |
---|
2214 | regfi_add_message(file, REGFI_MSG_ERROR, "Data in offset but length > 4" |
---|
2215 | " while parsing data record at offset 0x%.8X.", |
---|
2216 | offset); |
---|
2217 | return NULL; |
---|
2218 | } |
---|
2219 | |
---|
2220 | if((ret_val = (uint8*)zalloc(sizeof(uint8)*length)) == NULL) |
---|
2221 | return NULL; |
---|
2222 | |
---|
2223 | for(i = 0; i < length; i++) |
---|
2224 | ret_val[i] = (uint8)((offset >> i*8) & 0xFF); |
---|
2225 | } |
---|
2226 | else |
---|
2227 | { |
---|
2228 | if(!regfi_parse_cell(file->fd, offset, NULL, 0, |
---|
2229 | &cell_length, &unalloc)) |
---|
2230 | { |
---|
2231 | regfi_add_message(file, REGFI_MSG_WARN, "Could not parse cell while" |
---|
2232 | " parsing data record at offset 0x%.8X.", offset); |
---|
2233 | return NULL; |
---|
2234 | } |
---|
2235 | |
---|
2236 | if((cell_length & 0xFFFFFFF8) != cell_length) |
---|
2237 | { |
---|
2238 | regfi_add_message(file, REGFI_MSG_WARN, "Cell length not multiple of 8" |
---|
2239 | " while parsing data record at offset 0x%.8X.", |
---|
2240 | offset); |
---|
2241 | return NULL; |
---|
2242 | } |
---|
2243 | |
---|
2244 | if(cell_length > max_size) |
---|
2245 | { |
---|
2246 | regfi_add_message(file, REGFI_MSG_WARN, "Cell extends past hbin boundary" |
---|
2247 | " while parsing data record at offset 0x%.8X.", |
---|
2248 | offset); |
---|
2249 | if(strict) |
---|
2250 | return NULL; |
---|
2251 | else |
---|
2252 | cell_length = max_size; |
---|
2253 | } |
---|
2254 | |
---|
2255 | if(cell_length - 4 < length) |
---|
2256 | { |
---|
2257 | /* XXX: This strict condition has been triggered in multiple registries. |
---|
2258 | * Not sure the cause, but the data length values are very large, |
---|
2259 | * such as 53392. |
---|
2260 | */ |
---|
2261 | regfi_add_message(file, REGFI_MSG_WARN, "Data length (0x%.8X) larger than" |
---|
2262 | " remaining cell length (0x%.8X)" |
---|
2263 | " while parsing data record at offset 0x%.8X.", |
---|
2264 | length, cell_length - 4, offset); |
---|
2265 | if(strict) |
---|
2266 | return NULL; |
---|
2267 | else |
---|
2268 | length = cell_length - 4; |
---|
2269 | } |
---|
2270 | |
---|
2271 | if((ret_val = (uint8*)zalloc(sizeof(uint8)*length)) == NULL) |
---|
2272 | return NULL; |
---|
2273 | |
---|
2274 | read_length = length; |
---|
2275 | if((regfi_read(file->fd, ret_val, &read_length) != 0) |
---|
2276 | || read_length != length) |
---|
2277 | { |
---|
2278 | regfi_add_message(file, REGFI_MSG_ERROR, "Could not read data block while" |
---|
2279 | " parsing data record at offset 0x%.8X.", offset); |
---|
2280 | free(ret_val); |
---|
2281 | return NULL; |
---|
2282 | } |
---|
2283 | } |
---|
2284 | |
---|
2285 | return ret_val; |
---|
2286 | } |
---|
2287 | |
---|
2288 | |
---|
2289 | range_list* regfi_parse_unalloc_cells(REGFI_FILE* file) |
---|
2290 | { |
---|
2291 | range_list* ret_val; |
---|
2292 | REGFI_HBIN* hbin; |
---|
2293 | const range_list_element* hbins_elem; |
---|
2294 | uint32 i, num_hbins, curr_off, cell_len; |
---|
2295 | bool is_unalloc; |
---|
2296 | |
---|
2297 | ret_val = range_list_new(); |
---|
2298 | if(ret_val == NULL) |
---|
2299 | return NULL; |
---|
2300 | |
---|
2301 | num_hbins = range_list_size(file->hbins); |
---|
2302 | for(i=0; i<num_hbins; i++) |
---|
2303 | { |
---|
2304 | hbins_elem = range_list_get(file->hbins, i); |
---|
2305 | if(hbins_elem == NULL) |
---|
2306 | break; |
---|
2307 | hbin = (REGFI_HBIN*)hbins_elem->data; |
---|
2308 | |
---|
2309 | curr_off = REGFI_HBIN_HEADER_SIZE; |
---|
2310 | while(curr_off < hbin->block_size) |
---|
2311 | { |
---|
2312 | if(!regfi_parse_cell(file->fd, hbin->file_off+curr_off, NULL, 0, |
---|
2313 | &cell_len, &is_unalloc)) |
---|
2314 | break; |
---|
2315 | |
---|
2316 | if((cell_len == 0) || ((cell_len & 0xFFFFFFF8) != cell_len)) |
---|
2317 | { |
---|
2318 | regfi_add_message(file, REGFI_MSG_ERROR, "Bad cell length encountered" |
---|
2319 | " while parsing unallocated cells at offset 0x%.8X.", |
---|
2320 | hbin->file_off+curr_off); |
---|
2321 | break; |
---|
2322 | } |
---|
2323 | |
---|
2324 | /* for some reason the record_size of the last record in |
---|
2325 | an hbin block can extend past the end of the block |
---|
2326 | even though the record fits within the remaining |
---|
2327 | space....aaarrrgggghhhhhh */ |
---|
2328 | if(curr_off + cell_len >= hbin->block_size) |
---|
2329 | cell_len = hbin->block_size - curr_off; |
---|
2330 | |
---|
2331 | if(is_unalloc) |
---|
2332 | range_list_add(ret_val, hbin->file_off+curr_off, |
---|
2333 | cell_len, NULL); |
---|
2334 | |
---|
2335 | curr_off = curr_off+cell_len; |
---|
2336 | } |
---|
2337 | } |
---|
2338 | |
---|
2339 | return ret_val; |
---|
2340 | } |
---|