1 | /* |
---|
2 | * Copyright (C) 2005-2011,2015 Timothy D. Morgan |
---|
3 | * Copyright (C) 2005 Gerald (Jerry) Carter |
---|
4 | * |
---|
5 | * This program is free software; you can redistribute it and/or modify |
---|
6 | * it under the terms of the GNU General Public License as published by |
---|
7 | * the Free Software Foundation; version 3 of the License. |
---|
8 | * |
---|
9 | * This program is distributed in the hope that it will be useful, |
---|
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
12 | * GNU General Public License for more details. |
---|
13 | * |
---|
14 | * You should have received a copy of the GNU General Public License |
---|
15 | * along with this program; if not, write to the Free Software |
---|
16 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
17 | * |
---|
18 | * $Id: regfi.c 291 2016-11-06 18:44:16Z tim $ |
---|
19 | */ |
---|
20 | |
---|
21 | /** |
---|
22 | * @file |
---|
23 | * |
---|
24 | * Windows NT (and later) read-only registry library |
---|
25 | * |
---|
26 | * See @ref regfi.h for more information. |
---|
27 | * |
---|
28 | * Branched from Samba project Subversion repository, version #7470: |
---|
29 | * http://viewcvs.samba.org/cgi-bin/viewcvs.cgi/trunk/source/registry/regfio.c?rev=7470&view=auto |
---|
30 | * |
---|
31 | * Since then, it has been heavily rewritten, simplified, and improved. |
---|
32 | */ |
---|
33 | |
---|
34 | #include "regfi.h" |
---|
35 | |
---|
36 | /* Library version can be overridden at build time */ |
---|
37 | #ifndef REGFI_VERSION |
---|
38 | #define REGFI_VERSION "trunk" |
---|
39 | #endif |
---|
40 | |
---|
41 | |
---|
42 | /* Registry types mapping */ |
---|
43 | const unsigned int regfi_num_reg_types = 12; |
---|
44 | static const char* regfi_type_names[] = |
---|
45 | {"NONE", "SZ", "EXPAND_SZ", "BINARY", "DWORD", "DWORD_BE", "LINK", |
---|
46 | "MULTI_SZ", "RSRC_LIST", "RSRC_DESC", "RSRC_REQ_LIST", "QWORD"}; |
---|
47 | |
---|
48 | const char* regfi_encoding_names[] = |
---|
49 | {"US-ASCII", "UTF-8", "UTF-16LE"}; |
---|
50 | |
---|
51 | |
---|
52 | /* Ensures regfi_init runs only once */ |
---|
53 | static pthread_once_t regfi_init_once = PTHREAD_ONCE_INIT; |
---|
54 | |
---|
55 | |
---|
56 | /****************************************************************************** |
---|
57 | ******************************************************************************/ |
---|
58 | const char* regfi_version() |
---|
59 | { |
---|
60 | return REGFI_VERSION; |
---|
61 | } |
---|
62 | |
---|
63 | |
---|
64 | /****************************************************************************** |
---|
65 | ******************************************************************************/ |
---|
66 | void regfi_log_free(void* ptr) |
---|
67 | { |
---|
68 | REGFI_LOG* log_info = (REGFI_LOG*)ptr; |
---|
69 | |
---|
70 | if(log_info->messages != NULL) |
---|
71 | free(log_info->messages); |
---|
72 | |
---|
73 | talloc_free(log_info); |
---|
74 | } |
---|
75 | |
---|
76 | |
---|
77 | /****************************************************************************** |
---|
78 | ******************************************************************************/ |
---|
79 | void regfi_init() |
---|
80 | { |
---|
81 | int err; |
---|
82 | if((err = pthread_key_create(®fi_log_key, regfi_log_free)) != 0) |
---|
83 | fprintf(stderr, "ERROR: key_create: %s\n", strerror(err)); |
---|
84 | errno = err; |
---|
85 | } |
---|
86 | |
---|
87 | |
---|
88 | /****************************************************************************** |
---|
89 | ******************************************************************************/ |
---|
90 | REGFI_LOG* regfi_log_new() |
---|
91 | { |
---|
92 | int err; |
---|
93 | REGFI_LOG* log_info = talloc(NULL, REGFI_LOG); |
---|
94 | if(log_info == NULL) |
---|
95 | return NULL; |
---|
96 | |
---|
97 | log_info->msg_mask = REGFI_DEFAULT_LOG_MASK; |
---|
98 | log_info->messages = NULL; |
---|
99 | |
---|
100 | pthread_once(®fi_init_once, regfi_init); |
---|
101 | |
---|
102 | if((err = pthread_setspecific(regfi_log_key, log_info)) != 0) |
---|
103 | { |
---|
104 | fprintf(stderr, "ERROR: setspecific: %s\n", strerror(err)); |
---|
105 | goto fail; |
---|
106 | } |
---|
107 | |
---|
108 | return log_info; |
---|
109 | |
---|
110 | fail: |
---|
111 | talloc_free(log_info); |
---|
112 | errno = err; |
---|
113 | return NULL; |
---|
114 | } |
---|
115 | |
---|
116 | |
---|
117 | /****************************************************************************** |
---|
118 | ******************************************************************************/ |
---|
119 | void regfi_log_add(uint16_t msg_type, const char* fmt, ...) |
---|
120 | { |
---|
121 | /* XXX: Switch internal storage over to a linked list or stack. |
---|
122 | * Then add a regfi_log_get function that returns the list in some |
---|
123 | * convenient, user-friendly data structure. regfi_log_get_str should |
---|
124 | * stick around and will simply smush the list into a big string when |
---|
125 | * it's called, rather than having messages smushed when they're first |
---|
126 | * written to the log. |
---|
127 | */ |
---|
128 | uint32_t buf_size, buf_used; |
---|
129 | char* new_msg; |
---|
130 | REGFI_LOG* log_info; |
---|
131 | va_list args; |
---|
132 | |
---|
133 | log_info = (REGFI_LOG*)pthread_getspecific(regfi_log_key); |
---|
134 | if(log_info == NULL && (log_info = regfi_log_new()) == NULL) |
---|
135 | return; |
---|
136 | |
---|
137 | if((log_info->msg_mask & msg_type) == 0) |
---|
138 | return; |
---|
139 | |
---|
140 | if(log_info->messages == NULL) |
---|
141 | buf_used = 0; |
---|
142 | else |
---|
143 | buf_used = strlen(log_info->messages); |
---|
144 | |
---|
145 | buf_size = buf_used+strlen(fmt)+160; |
---|
146 | new_msg = realloc(log_info->messages, buf_size); |
---|
147 | if(new_msg == NULL) |
---|
148 | /* XXX: should we report this? */ |
---|
149 | return; |
---|
150 | |
---|
151 | switch (msg_type) |
---|
152 | { |
---|
153 | case REGFI_LOG_INFO: |
---|
154 | strcpy(new_msg+buf_used, "INFO: "); |
---|
155 | buf_used += 6; |
---|
156 | break; |
---|
157 | case REGFI_LOG_WARN: |
---|
158 | strcpy(new_msg+buf_used, "WARN: "); |
---|
159 | buf_used += 6; |
---|
160 | break; |
---|
161 | case REGFI_LOG_ERROR: |
---|
162 | strcpy(new_msg+buf_used, "ERROR: "); |
---|
163 | buf_used += 7; |
---|
164 | break; |
---|
165 | } |
---|
166 | |
---|
167 | va_start(args, fmt); |
---|
168 | vsnprintf(new_msg+buf_used, buf_size-buf_used, fmt, args); |
---|
169 | va_end(args); |
---|
170 | strncat(new_msg, "\n", buf_size-1); |
---|
171 | |
---|
172 | log_info->messages = new_msg; |
---|
173 | } |
---|
174 | |
---|
175 | |
---|
176 | /****************************************************************************** |
---|
177 | ******************************************************************************/ |
---|
178 | char* regfi_log_get_str() |
---|
179 | { |
---|
180 | char* ret_val; |
---|
181 | REGFI_LOG* log_info = (REGFI_LOG*)pthread_getspecific(regfi_log_key); |
---|
182 | if(log_info == NULL && (log_info = regfi_log_new()) == NULL) |
---|
183 | return NULL; |
---|
184 | |
---|
185 | ret_val = log_info->messages; |
---|
186 | log_info->messages = NULL; |
---|
187 | |
---|
188 | return ret_val; |
---|
189 | } |
---|
190 | |
---|
191 | |
---|
192 | /****************************************************************************** |
---|
193 | ******************************************************************************/ |
---|
194 | bool regfi_log_set_mask(uint16_t msg_mask) |
---|
195 | { |
---|
196 | REGFI_LOG* log_info = (REGFI_LOG*)pthread_getspecific(regfi_log_key); |
---|
197 | if(log_info == NULL && (log_info = regfi_log_new()) == NULL) |
---|
198 | { |
---|
199 | return false; |
---|
200 | } |
---|
201 | |
---|
202 | log_info->msg_mask = msg_mask; |
---|
203 | return true; |
---|
204 | } |
---|
205 | |
---|
206 | |
---|
207 | /****************************************************************************** |
---|
208 | * Returns NULL for an invalid e |
---|
209 | *****************************************************************************/ |
---|
210 | static const char* regfi_encoding_int2str(REGFI_ENCODING e) |
---|
211 | { |
---|
212 | if(e < REGFI_NUM_ENCODINGS) |
---|
213 | return regfi_encoding_names[e]; |
---|
214 | |
---|
215 | return NULL; |
---|
216 | } |
---|
217 | |
---|
218 | |
---|
219 | /****************************************************************************** |
---|
220 | * Returns NULL for an invalid val |
---|
221 | *****************************************************************************/ |
---|
222 | const char* regfi_type_val2str(unsigned int val) |
---|
223 | { |
---|
224 | if(val == REG_KEY) |
---|
225 | return "KEY"; |
---|
226 | |
---|
227 | if(val >= regfi_num_reg_types) |
---|
228 | return NULL; |
---|
229 | |
---|
230 | return regfi_type_names[val]; |
---|
231 | } |
---|
232 | |
---|
233 | |
---|
234 | /****************************************************************************** |
---|
235 | * Returns -1 on error |
---|
236 | *****************************************************************************/ |
---|
237 | int regfi_type_str2val(const char* str) |
---|
238 | { |
---|
239 | int i; |
---|
240 | |
---|
241 | if(strcmp("KEY", str) == 0) |
---|
242 | return REG_KEY; |
---|
243 | |
---|
244 | for(i=0; i < regfi_num_reg_types; i++) |
---|
245 | if (strcmp(regfi_type_names[i], str) == 0) |
---|
246 | return i; |
---|
247 | |
---|
248 | if(strcmp("DWORD_LE", str) == 0) |
---|
249 | return REG_DWORD_LE; |
---|
250 | |
---|
251 | return -1; |
---|
252 | } |
---|
253 | |
---|
254 | |
---|
255 | /* Security descriptor formatting functions */ |
---|
256 | |
---|
257 | const char* regfi_ace_type2str(uint8_t type) |
---|
258 | { |
---|
259 | static const char* map[7] |
---|
260 | = {"ALLOW", "DENY", "AUDIT", "ALARM", |
---|
261 | "ALLOW CPD", "OBJ ALLOW", "OBJ DENY"}; |
---|
262 | if(type < 7) |
---|
263 | return map[type]; |
---|
264 | else |
---|
265 | /* XXX: would be nice to return the unknown integer value. |
---|
266 | * However, as it is a const string, it can't be free()ed later on, |
---|
267 | * so that would need to change. |
---|
268 | */ |
---|
269 | return "UNKNOWN"; |
---|
270 | } |
---|
271 | |
---|
272 | |
---|
273 | /* XXX: need a better reference on the meaning of each flag. */ |
---|
274 | /* For more info, see: |
---|
275 | * http://msdn2.microsoft.com/en-us/library/aa772242.aspx |
---|
276 | */ |
---|
277 | char* regfi_ace_flags2str(uint8_t flags) |
---|
278 | { |
---|
279 | static const char* flag_map[32] = |
---|
280 | { "OI", /* Object Inherit */ |
---|
281 | "CI", /* Container Inherit */ |
---|
282 | "NP", /* Non-Propagate */ |
---|
283 | "IO", /* Inherit Only */ |
---|
284 | "IA", /* Inherited ACE */ |
---|
285 | NULL, |
---|
286 | NULL, |
---|
287 | NULL, |
---|
288 | }; |
---|
289 | |
---|
290 | char* ret_val = malloc(35*sizeof(char)); |
---|
291 | char* fo = ret_val; |
---|
292 | uint32_t i; |
---|
293 | uint8_t f; |
---|
294 | |
---|
295 | if(ret_val == NULL) |
---|
296 | return NULL; |
---|
297 | |
---|
298 | fo[0] = '\0'; |
---|
299 | if (!flags) |
---|
300 | return ret_val; |
---|
301 | |
---|
302 | for(i=0; i < 8; i++) |
---|
303 | { |
---|
304 | f = (1<<i); |
---|
305 | if((flags & f) && (flag_map[i] != NULL)) |
---|
306 | { |
---|
307 | strcpy(fo, flag_map[i]); |
---|
308 | fo += strlen(flag_map[i]); |
---|
309 | *(fo++) = ' '; |
---|
310 | flags ^= f; |
---|
311 | } |
---|
312 | } |
---|
313 | |
---|
314 | /* Any remaining unknown flags are added at the end in hex. */ |
---|
315 | if(flags != 0) |
---|
316 | sprintf(fo, "0x%.2X ", flags); |
---|
317 | |
---|
318 | /* Chop off the last space if we've written anything to ret_val */ |
---|
319 | if(fo != ret_val) |
---|
320 | fo[-1] = '\0'; |
---|
321 | |
---|
322 | return ret_val; |
---|
323 | } |
---|
324 | |
---|
325 | |
---|
326 | char* regfi_ace_perms2str(uint32_t perms) |
---|
327 | { |
---|
328 | uint32_t i, p; |
---|
329 | /* This is more than is needed by a fair margin. */ |
---|
330 | char* ret_val = malloc(350*sizeof(char)); |
---|
331 | char* r = ret_val; |
---|
332 | |
---|
333 | /* Each represents one of 32 permissions bits. NULL is for undefined/reserved bits. |
---|
334 | * For more information, see: |
---|
335 | * http://msdn2.microsoft.com/en-gb/library/aa374892.aspx |
---|
336 | * http://msdn2.microsoft.com/en-gb/library/ms724878.aspx |
---|
337 | */ |
---|
338 | static const char* perm_map[32] = |
---|
339 | {/* object-specific permissions (registry keys, in this case) */ |
---|
340 | "QRY_VAL", /* KEY_QUERY_VALUE */ |
---|
341 | "SET_VAL", /* KEY_SET_VALUE */ |
---|
342 | "CREATE_KEY", /* KEY_CREATE_SUB_KEY */ |
---|
343 | "ENUM_KEYS", /* KEY_ENUMERATE_SUB_KEYS */ |
---|
344 | "NOTIFY", /* KEY_NOTIFY */ |
---|
345 | "CREATE_LNK", /* KEY_CREATE_LINK - Reserved for system use. */ |
---|
346 | NULL, |
---|
347 | NULL, |
---|
348 | "WOW64_64", /* KEY_WOW64_64KEY */ |
---|
349 | "WOW64_32", /* KEY_WOW64_32KEY */ |
---|
350 | NULL, |
---|
351 | NULL, |
---|
352 | NULL, |
---|
353 | NULL, |
---|
354 | NULL, |
---|
355 | NULL, |
---|
356 | /* standard access rights */ |
---|
357 | "DELETE", /* DELETE */ |
---|
358 | "R_CONT", /* READ_CONTROL */ |
---|
359 | "W_DAC", /* WRITE_DAC */ |
---|
360 | "W_OWNER", /* WRITE_OWNER */ |
---|
361 | "SYNC", /* SYNCHRONIZE - Shouldn't be set in registries */ |
---|
362 | NULL, |
---|
363 | NULL, |
---|
364 | NULL, |
---|
365 | /* other generic */ |
---|
366 | "SYS_SEC", /* ACCESS_SYSTEM_SECURITY */ |
---|
367 | "MAX_ALLWD", /* MAXIMUM_ALLOWED */ |
---|
368 | NULL, |
---|
369 | NULL, |
---|
370 | "GEN_A", /* GENERIC_ALL */ |
---|
371 | "GEN_X", /* GENERIC_EXECUTE */ |
---|
372 | "GEN_W", /* GENERIC_WRITE */ |
---|
373 | "GEN_R", /* GENERIC_READ */ |
---|
374 | }; |
---|
375 | |
---|
376 | |
---|
377 | if(ret_val == NULL) |
---|
378 | return NULL; |
---|
379 | |
---|
380 | r[0] = '\0'; |
---|
381 | for(i=0; i < 32; i++) |
---|
382 | { |
---|
383 | p = (1<<i); |
---|
384 | if((perms & p) && (perm_map[i] != NULL)) |
---|
385 | { |
---|
386 | strcpy(r, perm_map[i]); |
---|
387 | r += strlen(perm_map[i]); |
---|
388 | *(r++) = ' '; |
---|
389 | perms ^= p; |
---|
390 | } |
---|
391 | } |
---|
392 | |
---|
393 | /* Any remaining unknown permission bits are added at the end in hex. */ |
---|
394 | if(perms != 0) |
---|
395 | sprintf(r, "0x%.8X ", perms); |
---|
396 | |
---|
397 | /* Chop off the last space if we've written anything to ret_val */ |
---|
398 | if(r != ret_val) |
---|
399 | r[-1] = '\0'; |
---|
400 | |
---|
401 | return ret_val; |
---|
402 | } |
---|
403 | |
---|
404 | |
---|
405 | char* regfi_get_acl(WINSEC_ACL* acl) |
---|
406 | { |
---|
407 | uint32_t i, extra, size = 0; |
---|
408 | const char* type_str; |
---|
409 | char* flags_str; |
---|
410 | char* perms_str; |
---|
411 | char* sid_str; |
---|
412 | char* ace_delim = ""; |
---|
413 | char* ret_val = NULL; |
---|
414 | char* tmp_val = NULL; |
---|
415 | bool failed = false; |
---|
416 | char field_delim = ':'; |
---|
417 | |
---|
418 | for (i = 0; i < acl->num_aces && !failed; i++) |
---|
419 | { |
---|
420 | sid_str = winsec_sid2str(acl->aces[i]->trustee); |
---|
421 | type_str = regfi_ace_type2str(acl->aces[i]->type); |
---|
422 | perms_str = regfi_ace_perms2str(acl->aces[i]->access_mask); |
---|
423 | flags_str = regfi_ace_flags2str(acl->aces[i]->flags); |
---|
424 | |
---|
425 | if(flags_str != NULL && perms_str != NULL |
---|
426 | && type_str != NULL && sid_str != NULL) |
---|
427 | { |
---|
428 | /* XXX: this is slow */ |
---|
429 | extra = strlen(sid_str) + strlen(type_str) |
---|
430 | + strlen(perms_str) + strlen(flags_str) + 5; |
---|
431 | tmp_val = realloc(ret_val, size+extra); |
---|
432 | |
---|
433 | if(tmp_val == NULL) |
---|
434 | { |
---|
435 | free(ret_val); |
---|
436 | ret_val = NULL; |
---|
437 | failed = true; |
---|
438 | } |
---|
439 | else |
---|
440 | { |
---|
441 | ret_val = tmp_val; |
---|
442 | size += sprintf(ret_val+size, "%s%s%c%s%c%s%c%s", |
---|
443 | ace_delim,sid_str, |
---|
444 | field_delim,type_str, |
---|
445 | field_delim,perms_str, |
---|
446 | field_delim,flags_str); |
---|
447 | ace_delim = "|"; |
---|
448 | } |
---|
449 | } |
---|
450 | else |
---|
451 | failed = true; |
---|
452 | |
---|
453 | if(sid_str != NULL) |
---|
454 | free(sid_str); |
---|
455 | if(sid_str != NULL) |
---|
456 | free(perms_str); |
---|
457 | if(sid_str != NULL) |
---|
458 | free(flags_str); |
---|
459 | } |
---|
460 | |
---|
461 | return ret_val; |
---|
462 | } |
---|
463 | |
---|
464 | |
---|
465 | char* regfi_get_sacl(WINSEC_DESC *sec_desc) |
---|
466 | { |
---|
467 | if (sec_desc->sacl) |
---|
468 | return regfi_get_acl(sec_desc->sacl); |
---|
469 | else |
---|
470 | return NULL; |
---|
471 | } |
---|
472 | |
---|
473 | |
---|
474 | char* regfi_get_dacl(WINSEC_DESC *sec_desc) |
---|
475 | { |
---|
476 | if (sec_desc->dacl) |
---|
477 | return regfi_get_acl(sec_desc->dacl); |
---|
478 | else |
---|
479 | return NULL; |
---|
480 | } |
---|
481 | |
---|
482 | |
---|
483 | char* regfi_get_owner(WINSEC_DESC *sec_desc) |
---|
484 | { |
---|
485 | return winsec_sid2str(sec_desc->owner_sid); |
---|
486 | } |
---|
487 | |
---|
488 | |
---|
489 | char* regfi_get_group(WINSEC_DESC *sec_desc) |
---|
490 | { |
---|
491 | return winsec_sid2str(sec_desc->grp_sid); |
---|
492 | } |
---|
493 | |
---|
494 | |
---|
495 | bool regfi_read_lock(REGFI_FILE* file, pthread_rwlock_t* lock, const char* context) |
---|
496 | { |
---|
497 | int lock_ret = pthread_rwlock_rdlock(lock); |
---|
498 | if(lock_ret != 0) |
---|
499 | { |
---|
500 | regfi_log_add(REGFI_LOG_ERROR, "Error obtaining read lock in" |
---|
501 | "%s due to: %s\n", context, strerror(lock_ret)); |
---|
502 | return false; |
---|
503 | } |
---|
504 | |
---|
505 | return true; |
---|
506 | } |
---|
507 | |
---|
508 | |
---|
509 | bool regfi_write_lock(REGFI_FILE* file, pthread_rwlock_t* lock, const char* context) |
---|
510 | { |
---|
511 | int lock_ret = pthread_rwlock_wrlock(lock); |
---|
512 | if(lock_ret != 0) |
---|
513 | { |
---|
514 | regfi_log_add(REGFI_LOG_ERROR, "Error obtaining write lock in" |
---|
515 | "%s due to: %s\n", context, strerror(lock_ret)); |
---|
516 | return false; |
---|
517 | } |
---|
518 | |
---|
519 | return true; |
---|
520 | } |
---|
521 | |
---|
522 | |
---|
523 | bool regfi_rw_unlock(REGFI_FILE* file, pthread_rwlock_t* lock, const char* context) |
---|
524 | { |
---|
525 | int lock_ret = pthread_rwlock_unlock(lock); |
---|
526 | if(lock_ret != 0) |
---|
527 | { |
---|
528 | regfi_log_add(REGFI_LOG_ERROR, "Error releasing lock in" |
---|
529 | "%s due to: %s\n", context, strerror(lock_ret)); |
---|
530 | return false; |
---|
531 | } |
---|
532 | |
---|
533 | return true; |
---|
534 | } |
---|
535 | |
---|
536 | |
---|
537 | bool regfi_lock(REGFI_FILE* file, pthread_mutex_t* lock, const char* context) |
---|
538 | { |
---|
539 | int lock_ret = pthread_mutex_lock(lock); |
---|
540 | if(lock_ret != 0) |
---|
541 | { |
---|
542 | regfi_log_add(REGFI_LOG_ERROR, "Error obtaining mutex lock in" |
---|
543 | "%s due to: %s\n", context, strerror(lock_ret)); |
---|
544 | return false; |
---|
545 | } |
---|
546 | |
---|
547 | return true; |
---|
548 | } |
---|
549 | |
---|
550 | |
---|
551 | bool regfi_unlock(REGFI_FILE* file, pthread_mutex_t* lock, const char* context) |
---|
552 | { |
---|
553 | int lock_ret = pthread_mutex_unlock(lock); |
---|
554 | if(lock_ret != 0) |
---|
555 | { |
---|
556 | regfi_log_add(REGFI_LOG_ERROR, "Error releasing mutex lock in" |
---|
557 | "%s due to: %s\n", context, strerror(lock_ret)); |
---|
558 | return false; |
---|
559 | } |
---|
560 | |
---|
561 | return true; |
---|
562 | } |
---|
563 | |
---|
564 | |
---|
565 | int64_t regfi_raw_seek(REGFI_RAW_FILE* self, uint64_t offset, int whence) |
---|
566 | { |
---|
567 | if(sizeof(off_t) == 4 && offset > 2147483647) |
---|
568 | { |
---|
569 | errno = EOVERFLOW; |
---|
570 | return -1; |
---|
571 | } |
---|
572 | return lseek(*(int*)self->state, offset, whence); |
---|
573 | } |
---|
574 | |
---|
575 | ssize_t regfi_raw_read(REGFI_RAW_FILE* self, void* buf, size_t count) |
---|
576 | { |
---|
577 | return read(*(int*)self->state, buf, count); |
---|
578 | } |
---|
579 | |
---|
580 | |
---|
581 | /***************************************************************************** |
---|
582 | * Convenience function to wrap up the ugly callback stuff |
---|
583 | *****************************************************************************/ |
---|
584 | uint64_t regfi_seek(REGFI_RAW_FILE* file_cb, uint64_t offset, int whence) |
---|
585 | { |
---|
586 | return file_cb->seek(file_cb, offset, whence); |
---|
587 | } |
---|
588 | |
---|
589 | |
---|
590 | /***************************************************************************** |
---|
591 | * This function is just like read(2), except that it continues to |
---|
592 | * re-try reading from the file descriptor if EINTR or EAGAIN is received. |
---|
593 | * regfi_read will attempt to read length bytes from the file and write them to |
---|
594 | * buf. |
---|
595 | * |
---|
596 | * On success, 0 is returned. Upon failure, an errno code is returned. |
---|
597 | * |
---|
598 | * The number of bytes successfully read is returned through the length |
---|
599 | * parameter by reference. If both the return value and length parameter are |
---|
600 | * returned as 0, then EOF was encountered immediately |
---|
601 | *****************************************************************************/ |
---|
602 | uint32_t regfi_read(REGFI_RAW_FILE* file_cb, uint8_t* buf, uint32_t* length) |
---|
603 | { |
---|
604 | uint32_t rsize = 0; |
---|
605 | uint32_t rret = 0; |
---|
606 | |
---|
607 | do |
---|
608 | { |
---|
609 | rret = file_cb->read(file_cb, |
---|
610 | buf + rsize, |
---|
611 | *length - rsize); |
---|
612 | if(rret > 0) |
---|
613 | rsize += rret; |
---|
614 | }while(*length - rsize > 0 |
---|
615 | && (rret > 0 || (rret == -1 && (errno == EAGAIN || errno == EINTR)))); |
---|
616 | |
---|
617 | *length = rsize; |
---|
618 | if (rret == -1 && errno != EINTR && errno != EAGAIN) |
---|
619 | return errno; |
---|
620 | |
---|
621 | return 0; |
---|
622 | } |
---|
623 | |
---|
624 | |
---|
625 | /***************************************************************************** |
---|
626 | * |
---|
627 | *****************************************************************************/ |
---|
628 | bool regfi_parse_cell(REGFI_RAW_FILE* file_cb, uint32_t offset, uint8_t* hdr, |
---|
629 | uint32_t hdr_len, uint32_t* cell_length, bool* unalloc) |
---|
630 | { |
---|
631 | uint32_t length; |
---|
632 | int32_t raw_length; |
---|
633 | uint8_t tmp[4]; |
---|
634 | |
---|
635 | if(regfi_seek(file_cb, offset, SEEK_SET) == -1) |
---|
636 | return false; |
---|
637 | |
---|
638 | length = 4; |
---|
639 | if((regfi_read(file_cb, tmp, &length) != 0) || length != 4) |
---|
640 | return false; |
---|
641 | raw_length = IVALS(tmp, 0); |
---|
642 | |
---|
643 | if(raw_length < 0) |
---|
644 | { |
---|
645 | (*cell_length) = raw_length*(-1); |
---|
646 | (*unalloc) = false; |
---|
647 | } |
---|
648 | else |
---|
649 | { |
---|
650 | (*cell_length) = raw_length; |
---|
651 | (*unalloc) = true; |
---|
652 | } |
---|
653 | |
---|
654 | if(*cell_length - 4 < hdr_len) |
---|
655 | return false; |
---|
656 | |
---|
657 | if(hdr_len > 0) |
---|
658 | { |
---|
659 | length = hdr_len; |
---|
660 | if((regfi_read(file_cb, hdr, &length) != 0) || length != hdr_len) |
---|
661 | return false; |
---|
662 | } |
---|
663 | |
---|
664 | return true; |
---|
665 | } |
---|
666 | |
---|
667 | |
---|
668 | /****************************************************************************** |
---|
669 | * Given an offset and an hbin, is the offset within that hbin? |
---|
670 | * The offset is a virtual file offset. |
---|
671 | ******************************************************************************/ |
---|
672 | static bool regfi_offset_in_hbin(const REGFI_HBIN* hbin, uint32_t voffset) |
---|
673 | { |
---|
674 | if(!hbin) |
---|
675 | return false; |
---|
676 | |
---|
677 | if((voffset > hbin->first_hbin_off) |
---|
678 | && (voffset < (hbin->first_hbin_off + hbin->block_size))) |
---|
679 | return true; |
---|
680 | |
---|
681 | return false; |
---|
682 | } |
---|
683 | |
---|
684 | |
---|
685 | |
---|
686 | /****************************************************************************** |
---|
687 | * Provide a physical offset and receive the correpsonding HBIN |
---|
688 | * block for it. NULL if one doesn't exist. |
---|
689 | ******************************************************************************/ |
---|
690 | const REGFI_HBIN* regfi_lookup_hbin(REGFI_FILE* file, uint32_t offset) |
---|
691 | { |
---|
692 | return (const REGFI_HBIN*)range_list_find_data(file->hbins, offset); |
---|
693 | } |
---|
694 | |
---|
695 | |
---|
696 | /****************************************************************************** |
---|
697 | * Calculate the largest possible cell size given a physical offset. |
---|
698 | * Largest size is based on the HBIN the offset is currently a member of. |
---|
699 | * Returns negative values on error. |
---|
700 | * (Since cells can only be ~2^31 in size, this works out.) |
---|
701 | ******************************************************************************/ |
---|
702 | int32_t regfi_calc_maxsize(REGFI_FILE* file, uint32_t offset) |
---|
703 | { |
---|
704 | const REGFI_HBIN* hbin = regfi_lookup_hbin(file, offset); |
---|
705 | if(hbin == NULL) |
---|
706 | return -1; |
---|
707 | |
---|
708 | return (hbin->block_size + hbin->file_off) - offset; |
---|
709 | } |
---|
710 | |
---|
711 | |
---|
712 | /****************************************************************************** |
---|
713 | ******************************************************************************/ |
---|
714 | REGFI_SUBKEY_LIST* regfi_load_subkeylist(REGFI_FILE* file, uint32_t offset, |
---|
715 | uint32_t num_keys, uint32_t max_size, |
---|
716 | bool strict) |
---|
717 | { |
---|
718 | REGFI_SUBKEY_LIST* ret_val; |
---|
719 | |
---|
720 | ret_val = regfi_load_subkeylist_aux(file, offset, max_size, strict, |
---|
721 | REGFI_MAX_SUBKEY_DEPTH); |
---|
722 | if(ret_val == NULL) |
---|
723 | { |
---|
724 | regfi_log_add(REGFI_LOG_WARN, "Failed to load subkey list at" |
---|
725 | " offset 0x%.8X.", offset); |
---|
726 | return NULL; |
---|
727 | } |
---|
728 | |
---|
729 | if(num_keys != ret_val->num_keys) |
---|
730 | { |
---|
731 | /* Not sure which should be authoritative, the number from the |
---|
732 | * NK record, or the number in the subkey list. Just emit a warning for |
---|
733 | * now if they don't match. |
---|
734 | */ |
---|
735 | regfi_log_add(REGFI_LOG_WARN, "Number of subkeys listed in parent" |
---|
736 | " (%d) did not match number found in subkey list/tree (%d)" |
---|
737 | " while parsing subkey list/tree at offset 0x%.8X.", |
---|
738 | num_keys, ret_val->num_keys, offset); |
---|
739 | } |
---|
740 | |
---|
741 | return ret_val; |
---|
742 | } |
---|
743 | |
---|
744 | |
---|
745 | /****************************************************************************** |
---|
746 | ******************************************************************************/ |
---|
747 | REGFI_SUBKEY_LIST* regfi_load_subkeylist_aux(REGFI_FILE* file, uint32_t offset, |
---|
748 | uint32_t max_size, bool strict, |
---|
749 | uint8_t depth_left) |
---|
750 | { |
---|
751 | REGFI_SUBKEY_LIST* ret_val; |
---|
752 | REGFI_SUBKEY_LIST** sublists; |
---|
753 | uint32_t i, num_sublists, off; |
---|
754 | int32_t sublist_maxsize; |
---|
755 | |
---|
756 | if(depth_left == 0) |
---|
757 | { |
---|
758 | regfi_log_add(REGFI_LOG_WARN, "Maximum depth reached" |
---|
759 | " while parsing subkey list/tree at offset 0x%.8X.", |
---|
760 | offset); |
---|
761 | return NULL; |
---|
762 | } |
---|
763 | |
---|
764 | ret_val = regfi_parse_subkeylist(file, offset, max_size, strict); |
---|
765 | if(ret_val == NULL) |
---|
766 | return NULL; |
---|
767 | |
---|
768 | if(ret_val->recursive_type) |
---|
769 | { |
---|
770 | num_sublists = ret_val->num_children; |
---|
771 | sublists = (REGFI_SUBKEY_LIST**)malloc(num_sublists |
---|
772 | * sizeof(REGFI_SUBKEY_LIST*)); |
---|
773 | for(i=0; i < num_sublists; i++) |
---|
774 | { |
---|
775 | off = ret_val->elements[i].offset + REGFI_REGF_SIZE; |
---|
776 | |
---|
777 | sublist_maxsize = regfi_calc_maxsize(file, off); |
---|
778 | if(sublist_maxsize < 0) |
---|
779 | sublists[i] = NULL; |
---|
780 | else |
---|
781 | sublists[i] = regfi_load_subkeylist_aux(file, off, sublist_maxsize, |
---|
782 | strict, depth_left-1); |
---|
783 | } |
---|
784 | talloc_free(ret_val); |
---|
785 | |
---|
786 | return regfi_merge_subkeylists(num_sublists, sublists, strict); |
---|
787 | } |
---|
788 | |
---|
789 | return ret_val; |
---|
790 | } |
---|
791 | |
---|
792 | |
---|
793 | /****************************************************************************** |
---|
794 | ******************************************************************************/ |
---|
795 | REGFI_SUBKEY_LIST* regfi_parse_subkeylist(REGFI_FILE* file, uint32_t offset, |
---|
796 | uint32_t max_size, bool strict) |
---|
797 | { |
---|
798 | REGFI_SUBKEY_LIST* ret_val = NULL; |
---|
799 | uint32_t i, cell_length, length, elem_size, read_len; |
---|
800 | uint8_t* elements = NULL; |
---|
801 | uint8_t buf[REGFI_SUBKEY_LIST_MIN_LEN]; |
---|
802 | bool unalloc; |
---|
803 | bool recursive_type; |
---|
804 | |
---|
805 | if(!regfi_lock(file, &file->cb_lock, "regfi_parse_subkeylist")) |
---|
806 | goto fail; |
---|
807 | |
---|
808 | if(!regfi_parse_cell(file->cb, offset, buf, REGFI_SUBKEY_LIST_MIN_LEN, |
---|
809 | &cell_length, &unalloc)) |
---|
810 | { |
---|
811 | regfi_log_add(REGFI_LOG_WARN, "Could not parse cell while " |
---|
812 | "parsing subkey-list at offset 0x%.8X.", offset); |
---|
813 | goto fail_locked; |
---|
814 | } |
---|
815 | |
---|
816 | if(cell_length > max_size) |
---|
817 | { |
---|
818 | regfi_log_add(REGFI_LOG_WARN, "Cell size longer than max_size" |
---|
819 | " while parsing subkey-list at offset 0x%.8X.", offset); |
---|
820 | if(strict) |
---|
821 | goto fail_locked; |
---|
822 | cell_length = max_size & 0xFFFFFFF8; |
---|
823 | } |
---|
824 | |
---|
825 | recursive_type = false; |
---|
826 | if(buf[0] == 'r' && buf[1] == 'i') |
---|
827 | { |
---|
828 | recursive_type = true; |
---|
829 | elem_size = sizeof(uint32_t); |
---|
830 | } |
---|
831 | else if(buf[0] == 'l' && buf[1] == 'i') |
---|
832 | { |
---|
833 | elem_size = sizeof(uint32_t); |
---|
834 | } |
---|
835 | else if((buf[0] == 'l') && (buf[1] == 'f' || buf[1] == 'h')) |
---|
836 | elem_size = sizeof(REGFI_SUBKEY_LIST_ELEM); |
---|
837 | else |
---|
838 | { |
---|
839 | regfi_log_add(REGFI_LOG_ERROR, "Unknown magic number" |
---|
840 | " (0x%.2X, 0x%.2X) encountered while parsing" |
---|
841 | " subkey-list at offset 0x%.8X.", buf[0], buf[1], offset); |
---|
842 | goto fail_locked; |
---|
843 | } |
---|
844 | |
---|
845 | ret_val = talloc(NULL, REGFI_SUBKEY_LIST); |
---|
846 | if(ret_val == NULL) |
---|
847 | goto fail_locked; |
---|
848 | |
---|
849 | ret_val->offset = offset; |
---|
850 | ret_val->cell_size = cell_length; |
---|
851 | ret_val->magic[0] = buf[0]; |
---|
852 | ret_val->magic[1] = buf[1]; |
---|
853 | ret_val->recursive_type = recursive_type; |
---|
854 | ret_val->num_children = SVAL(buf, 0x2); |
---|
855 | |
---|
856 | if(!recursive_type) |
---|
857 | ret_val->num_keys = ret_val->num_children; |
---|
858 | |
---|
859 | length = elem_size*ret_val->num_children; |
---|
860 | if(cell_length - REGFI_SUBKEY_LIST_MIN_LEN - sizeof(uint32_t) < length) |
---|
861 | { |
---|
862 | regfi_log_add(REGFI_LOG_WARN, "Number of elements too large for" |
---|
863 | " cell while parsing subkey-list at offset 0x%.8X.", |
---|
864 | offset); |
---|
865 | if(strict) |
---|
866 | goto fail_locked; |
---|
867 | length = cell_length - REGFI_SUBKEY_LIST_MIN_LEN - sizeof(uint32_t); |
---|
868 | } |
---|
869 | |
---|
870 | ret_val->elements = talloc_array(ret_val, REGFI_SUBKEY_LIST_ELEM, |
---|
871 | ret_val->num_children); |
---|
872 | if(ret_val->elements == NULL) |
---|
873 | goto fail_locked; |
---|
874 | |
---|
875 | elements = (uint8_t*)malloc(length); |
---|
876 | if(elements == NULL) |
---|
877 | goto fail_locked; |
---|
878 | |
---|
879 | read_len = length; |
---|
880 | if(regfi_read(file->cb, elements, &read_len) != 0 || read_len!=length) |
---|
881 | goto fail_locked; |
---|
882 | |
---|
883 | if(!regfi_unlock(file, &file->cb_lock, "regfi_parse_subkeylist")) |
---|
884 | goto fail; |
---|
885 | |
---|
886 | if(elem_size == sizeof(uint32_t)) |
---|
887 | { |
---|
888 | for (i=0; i < ret_val->num_children; i++) |
---|
889 | { |
---|
890 | ret_val->elements[i].offset = IVAL(elements, i*elem_size); |
---|
891 | ret_val->elements[i].hash = 0; |
---|
892 | } |
---|
893 | } |
---|
894 | else |
---|
895 | { |
---|
896 | for (i=0; i < ret_val->num_children; i++) |
---|
897 | { |
---|
898 | ret_val->elements[i].offset = IVAL(elements, i*elem_size); |
---|
899 | ret_val->elements[i].hash = IVAL(elements, i*elem_size+4); |
---|
900 | } |
---|
901 | } |
---|
902 | free(elements); |
---|
903 | |
---|
904 | return ret_val; |
---|
905 | |
---|
906 | fail_locked: |
---|
907 | regfi_unlock(file, &file->cb_lock, "regfi_parse_subkeylist"); |
---|
908 | fail: |
---|
909 | if(elements != NULL) |
---|
910 | free(elements); |
---|
911 | talloc_free(ret_val); |
---|
912 | return NULL; |
---|
913 | } |
---|
914 | |
---|
915 | |
---|
916 | /******************************************************************* |
---|
917 | *******************************************************************/ |
---|
918 | REGFI_SUBKEY_LIST* regfi_merge_subkeylists(uint16_t num_lists, |
---|
919 | REGFI_SUBKEY_LIST** lists, |
---|
920 | bool strict) |
---|
921 | { |
---|
922 | uint32_t i,j,k; |
---|
923 | REGFI_SUBKEY_LIST* ret_val; |
---|
924 | |
---|
925 | if(lists == NULL) |
---|
926 | return NULL; |
---|
927 | ret_val = talloc(NULL, REGFI_SUBKEY_LIST); |
---|
928 | |
---|
929 | if(ret_val == NULL) |
---|
930 | return NULL; |
---|
931 | |
---|
932 | /* Obtain total number of elements */ |
---|
933 | ret_val->num_keys = 0; |
---|
934 | for(i=0; i < num_lists; i++) |
---|
935 | { |
---|
936 | if(lists[i] != NULL) |
---|
937 | ret_val->num_keys += lists[i]->num_children; |
---|
938 | } |
---|
939 | ret_val->num_children = ret_val->num_keys; |
---|
940 | |
---|
941 | if(ret_val->num_keys > 0) |
---|
942 | { |
---|
943 | ret_val->elements = talloc_array(ret_val, REGFI_SUBKEY_LIST_ELEM, |
---|
944 | ret_val->num_keys); |
---|
945 | k=0; |
---|
946 | |
---|
947 | if(ret_val->elements != NULL) |
---|
948 | { |
---|
949 | for(i=0; i < num_lists; i++) |
---|
950 | { |
---|
951 | if(lists[i] != NULL) |
---|
952 | { |
---|
953 | for(j=0; j < lists[i]->num_keys; j++) |
---|
954 | { |
---|
955 | ret_val->elements[k].hash = lists[i]->elements[j].hash; |
---|
956 | ret_val->elements[k++].offset = lists[i]->elements[j].offset; |
---|
957 | } |
---|
958 | } |
---|
959 | } |
---|
960 | } |
---|
961 | } |
---|
962 | |
---|
963 | for(i=0; i < num_lists; i++) |
---|
964 | talloc_free(lists[i]); |
---|
965 | free(lists); |
---|
966 | |
---|
967 | return ret_val; |
---|
968 | } |
---|
969 | |
---|
970 | |
---|
971 | /****************************************************************************** |
---|
972 | * |
---|
973 | ******************************************************************************/ |
---|
974 | REGFI_SK* regfi_parse_sk(REGFI_FILE* file, uint32_t offset, uint32_t max_size, |
---|
975 | bool strict) |
---|
976 | { |
---|
977 | REGFI_SK* ret_val = NULL; |
---|
978 | uint8_t* sec_desc_buf = NULL; |
---|
979 | uint32_t cell_length, length; |
---|
980 | uint8_t sk_header[REGFI_SK_MIN_LENGTH]; |
---|
981 | bool unalloc = false; |
---|
982 | |
---|
983 | if(!regfi_lock(file, &file->cb_lock, "regfi_parse_sk")) |
---|
984 | goto fail; |
---|
985 | |
---|
986 | if(!regfi_parse_cell(file->cb, offset, sk_header, REGFI_SK_MIN_LENGTH, |
---|
987 | &cell_length, &unalloc)) |
---|
988 | { |
---|
989 | regfi_log_add(REGFI_LOG_WARN, "Could not parse SK record cell" |
---|
990 | " at offset 0x%.8X.", offset); |
---|
991 | goto fail_locked; |
---|
992 | } |
---|
993 | |
---|
994 | if(sk_header[0] != 's' || sk_header[1] != 'k') |
---|
995 | { |
---|
996 | regfi_log_add(REGFI_LOG_WARN, "Magic number mismatch in parsing" |
---|
997 | " SK record at offset 0x%.8X.", offset); |
---|
998 | goto fail_locked; |
---|
999 | } |
---|
1000 | |
---|
1001 | ret_val = talloc(NULL, REGFI_SK); |
---|
1002 | if(ret_val == NULL) |
---|
1003 | goto fail_locked; |
---|
1004 | |
---|
1005 | ret_val->offset = offset; |
---|
1006 | /* XXX: Is there a way to be more conservative (shorter) with |
---|
1007 | * cell length when cell is unallocated? |
---|
1008 | */ |
---|
1009 | ret_val->cell_size = cell_length; |
---|
1010 | |
---|
1011 | if(ret_val->cell_size > max_size) |
---|
1012 | ret_val->cell_size = max_size & 0xFFFFFFF8; |
---|
1013 | if((ret_val->cell_size < REGFI_SK_MIN_LENGTH) |
---|
1014 | || (strict && (ret_val->cell_size & 0x00000007) != 0)) |
---|
1015 | { |
---|
1016 | regfi_log_add(REGFI_LOG_WARN, "Invalid cell size found while" |
---|
1017 | " parsing SK record at offset 0x%.8X.", offset); |
---|
1018 | goto fail_locked; |
---|
1019 | } |
---|
1020 | |
---|
1021 | ret_val->magic[0] = sk_header[0]; |
---|
1022 | ret_val->magic[1] = sk_header[1]; |
---|
1023 | |
---|
1024 | ret_val->unknown_tag = SVAL(sk_header, 0x2); |
---|
1025 | ret_val->prev_sk_off = IVAL(sk_header, 0x4); |
---|
1026 | ret_val->next_sk_off = IVAL(sk_header, 0x8); |
---|
1027 | ret_val->ref_count = IVAL(sk_header, 0xC); |
---|
1028 | ret_val->desc_size = IVAL(sk_header, 0x10); |
---|
1029 | |
---|
1030 | if((ret_val->prev_sk_off & 0x00000007) != 0 |
---|
1031 | || (ret_val->next_sk_off & 0x00000007) != 0) |
---|
1032 | { |
---|
1033 | regfi_log_add(REGFI_LOG_WARN, "SK record's next/previous offsets" |
---|
1034 | " are not a multiple of 8 while parsing SK record at" |
---|
1035 | " offset 0x%.8X.", offset); |
---|
1036 | goto fail_locked; |
---|
1037 | } |
---|
1038 | |
---|
1039 | if(ret_val->desc_size + REGFI_SK_MIN_LENGTH > ret_val->cell_size) |
---|
1040 | { |
---|
1041 | regfi_log_add(REGFI_LOG_WARN, "Security descriptor too large for" |
---|
1042 | " cell while parsing SK record at offset 0x%.8X.", |
---|
1043 | offset); |
---|
1044 | goto fail_locked; |
---|
1045 | } |
---|
1046 | |
---|
1047 | sec_desc_buf = (uint8_t*)malloc(ret_val->desc_size); |
---|
1048 | if(sec_desc_buf == NULL) |
---|
1049 | goto fail_locked; |
---|
1050 | |
---|
1051 | length = ret_val->desc_size; |
---|
1052 | if(regfi_read(file->cb, sec_desc_buf, &length) != 0 |
---|
1053 | || length != ret_val->desc_size) |
---|
1054 | { |
---|
1055 | regfi_log_add(REGFI_LOG_ERROR, "Failed to read security" |
---|
1056 | " descriptor while parsing SK record at offset 0x%.8X.", |
---|
1057 | offset); |
---|
1058 | goto fail_locked; |
---|
1059 | } |
---|
1060 | |
---|
1061 | if(!regfi_unlock(file, &file->cb_lock, "regfi_parse_sk")) |
---|
1062 | goto fail; |
---|
1063 | |
---|
1064 | if(!(ret_val->sec_desc = winsec_parse_desc(ret_val, sec_desc_buf, |
---|
1065 | ret_val->desc_size))) |
---|
1066 | { |
---|
1067 | regfi_log_add(REGFI_LOG_ERROR, "Failed to parse security" |
---|
1068 | " descriptor while parsing SK record at offset 0x%.8X.", |
---|
1069 | offset); |
---|
1070 | goto fail; |
---|
1071 | } |
---|
1072 | |
---|
1073 | free(sec_desc_buf); |
---|
1074 | return ret_val; |
---|
1075 | |
---|
1076 | fail_locked: |
---|
1077 | regfi_unlock(file, &file->cb_lock, "regfi_parse_sk"); |
---|
1078 | fail: |
---|
1079 | if(sec_desc_buf != NULL) |
---|
1080 | free(sec_desc_buf); |
---|
1081 | talloc_free(ret_val); |
---|
1082 | return NULL; |
---|
1083 | } |
---|
1084 | |
---|
1085 | |
---|
1086 | REGFI_VALUE_LIST* regfi_parse_valuelist(REGFI_FILE* file, uint32_t offset, |
---|
1087 | uint32_t num_values, bool strict) |
---|
1088 | { |
---|
1089 | REGFI_VALUE_LIST* ret_val = NULL; |
---|
1090 | uint32_t i, cell_length, length, read_len; |
---|
1091 | bool unalloc; |
---|
1092 | |
---|
1093 | if(!regfi_lock(file, &file->cb_lock, "regfi_parse_valuelist")) |
---|
1094 | goto fail; |
---|
1095 | |
---|
1096 | if(!regfi_parse_cell(file->cb, offset, NULL, 0, &cell_length, &unalloc)) |
---|
1097 | { |
---|
1098 | regfi_log_add(REGFI_LOG_ERROR, "Failed to read cell header" |
---|
1099 | " while parsing value list at offset 0x%.8X.", offset); |
---|
1100 | goto fail_locked; |
---|
1101 | } |
---|
1102 | |
---|
1103 | if((cell_length & 0x00000007) != 0) |
---|
1104 | { |
---|
1105 | regfi_log_add(REGFI_LOG_WARN, "Cell length not a multiple of 8" |
---|
1106 | " while parsing value list at offset 0x%.8X.", offset); |
---|
1107 | if(strict) |
---|
1108 | goto fail_locked; |
---|
1109 | cell_length = cell_length & 0xFFFFFFF8; |
---|
1110 | } |
---|
1111 | |
---|
1112 | if((num_values * sizeof(uint32_t)) > cell_length-sizeof(uint32_t)) |
---|
1113 | { |
---|
1114 | regfi_log_add(REGFI_LOG_WARN, "Too many values found" |
---|
1115 | " while parsing value list at offset 0x%.8X.", offset); |
---|
1116 | if(strict) |
---|
1117 | goto fail_locked; |
---|
1118 | num_values = cell_length/sizeof(uint32_t) - sizeof(uint32_t); |
---|
1119 | } |
---|
1120 | |
---|
1121 | read_len = num_values*sizeof(uint32_t); |
---|
1122 | ret_val = talloc(NULL, REGFI_VALUE_LIST); |
---|
1123 | if(ret_val == NULL) |
---|
1124 | goto fail_locked; |
---|
1125 | |
---|
1126 | ret_val->elements = (REGFI_VALUE_LIST_ELEM*)talloc_size(ret_val, read_len); |
---|
1127 | if(ret_val->elements == NULL) |
---|
1128 | goto fail_locked; |
---|
1129 | |
---|
1130 | ret_val->offset = offset; |
---|
1131 | ret_val->cell_size = cell_length; |
---|
1132 | ret_val->num_values = num_values; |
---|
1133 | |
---|
1134 | length = read_len; |
---|
1135 | if((regfi_read(file->cb, (uint8_t*)ret_val->elements, &length) != 0) |
---|
1136 | || length != read_len) |
---|
1137 | { |
---|
1138 | regfi_log_add(REGFI_LOG_ERROR, "Failed to read value pointers" |
---|
1139 | " while parsing value list at offset 0x%.8X.", offset); |
---|
1140 | goto fail_locked; |
---|
1141 | } |
---|
1142 | |
---|
1143 | if(!regfi_unlock(file, &file->cb_lock, "regfi_parse_valuelist")) |
---|
1144 | goto fail; |
---|
1145 | |
---|
1146 | for(i=0; i < num_values; i++) |
---|
1147 | { |
---|
1148 | /* Fix endianness */ |
---|
1149 | ret_val->elements[i] = IVAL(&ret_val->elements[i], 0); |
---|
1150 | |
---|
1151 | /* Validate the first num_values values to ensure they make sense */ |
---|
1152 | if(strict) |
---|
1153 | { |
---|
1154 | /* XXX: Need to revisit this file length check when we start dealing |
---|
1155 | * with partial files. */ |
---|
1156 | if((ret_val->elements[i] + REGFI_REGF_SIZE > file->file_length) |
---|
1157 | || ((ret_val->elements[i] & 0x00000007) != 0)) |
---|
1158 | { |
---|
1159 | regfi_log_add(REGFI_LOG_WARN, "Invalid value pointer" |
---|
1160 | " (0x%.8X) found while parsing value list at offset" |
---|
1161 | " 0x%.8X.", ret_val->elements[i], offset); |
---|
1162 | goto fail; |
---|
1163 | } |
---|
1164 | } |
---|
1165 | } |
---|
1166 | |
---|
1167 | return ret_val; |
---|
1168 | |
---|
1169 | fail_locked: |
---|
1170 | regfi_unlock(file, &file->cb_lock, "regfi_parse_valuelist"); |
---|
1171 | fail: |
---|
1172 | talloc_free(ret_val); |
---|
1173 | return NULL; |
---|
1174 | } |
---|
1175 | |
---|
1176 | /* XXX: should give this boolean return type to indicate errors */ |
---|
1177 | void regfi_interpret_valuename(REGFI_FILE* file, REGFI_VK* vk, bool strict) |
---|
1178 | { |
---|
1179 | /* XXX: Registry value names are supposedly limited to 16383 characters |
---|
1180 | * according to: |
---|
1181 | * http://msdn.microsoft.com/en-us/library/ms724872%28VS.85%29.aspx |
---|
1182 | * Might want to emit a warning if this is exceeded. |
---|
1183 | * It is expected that "characters" could be variable width. |
---|
1184 | * Also, it may be useful to use this information to limit false positives |
---|
1185 | * when recovering deleted VK records. |
---|
1186 | */ |
---|
1187 | REGFI_BUFFER tmp_buf; |
---|
1188 | REGFI_ENCODING from_encoding = (vk->flags & REGFI_VK_FLAG_ASCIINAME) |
---|
1189 | ? REGFI_ENCODING_ASCII : REGFI_ENCODING_UTF16LE; |
---|
1190 | |
---|
1191 | if(vk->name_length == 0) |
---|
1192 | return; |
---|
1193 | |
---|
1194 | if(from_encoding == file->string_encoding) |
---|
1195 | { |
---|
1196 | vk->name_raw[vk->name_length] = '\0'; |
---|
1197 | vk->name = (char*)vk->name_raw; |
---|
1198 | } |
---|
1199 | else |
---|
1200 | { |
---|
1201 | tmp_buf = regfi_conv_charset(regfi_encoding_int2str(from_encoding), |
---|
1202 | regfi_encoding_int2str(file->string_encoding), |
---|
1203 | vk->name_raw, vk->name_length); |
---|
1204 | if(tmp_buf.buf == NULL) |
---|
1205 | { |
---|
1206 | regfi_log_add(REGFI_LOG_WARN, "Error occurred while converting" |
---|
1207 | " value name to encoding %s. VK offset: 0x%.8X." |
---|
1208 | " Error message: %s", |
---|
1209 | regfi_encoding_int2str(file->string_encoding), |
---|
1210 | vk->offset, strerror(errno)); |
---|
1211 | vk->name = NULL; |
---|
1212 | } |
---|
1213 | else |
---|
1214 | { |
---|
1215 | vk->name = (char*)tmp_buf.buf; |
---|
1216 | talloc_reparent(NULL, vk, vk->name); |
---|
1217 | } |
---|
1218 | } |
---|
1219 | } |
---|
1220 | |
---|
1221 | |
---|
1222 | /****************************************************************************** |
---|
1223 | ******************************************************************************/ |
---|
1224 | REGFI_VK* regfi_load_value(REGFI_FILE* file, uint32_t offset, bool strict) |
---|
1225 | { |
---|
1226 | REGFI_VK* ret_val = NULL; |
---|
1227 | int32_t max_size; |
---|
1228 | |
---|
1229 | max_size = regfi_calc_maxsize(file, offset); |
---|
1230 | if(max_size < 0) |
---|
1231 | return NULL; |
---|
1232 | |
---|
1233 | ret_val = regfi_parse_vk(file, offset, max_size, strict); |
---|
1234 | if(ret_val == NULL) |
---|
1235 | return NULL; |
---|
1236 | |
---|
1237 | regfi_interpret_valuename(file, ret_val, strict); |
---|
1238 | |
---|
1239 | return ret_val; |
---|
1240 | } |
---|
1241 | |
---|
1242 | |
---|
1243 | /****************************************************************************** |
---|
1244 | * If !strict, the list may contain NULLs, VK records may point to NULL. |
---|
1245 | ******************************************************************************/ |
---|
1246 | REGFI_VALUE_LIST* regfi_load_valuelist(REGFI_FILE* file, uint32_t offset, |
---|
1247 | uint32_t num_values, uint32_t max_size, |
---|
1248 | bool strict) |
---|
1249 | { |
---|
1250 | uint32_t usable_num_values; |
---|
1251 | |
---|
1252 | if((num_values+1) * sizeof(uint32_t) > max_size) |
---|
1253 | { |
---|
1254 | regfi_log_add(REGFI_LOG_WARN, "Number of values indicated by" |
---|
1255 | " parent key (%d) would cause cell to straddle HBIN" |
---|
1256 | " boundary while loading value list at offset" |
---|
1257 | " 0x%.8X.", num_values, offset); |
---|
1258 | if(strict) |
---|
1259 | return NULL; |
---|
1260 | usable_num_values = max_size/sizeof(uint32_t) - sizeof(uint32_t); |
---|
1261 | } |
---|
1262 | else |
---|
1263 | usable_num_values = num_values; |
---|
1264 | |
---|
1265 | return regfi_parse_valuelist(file, offset, usable_num_values, strict); |
---|
1266 | } |
---|
1267 | |
---|
1268 | |
---|
1269 | /* XXX: should give this boolean return type to indicate errors */ |
---|
1270 | void regfi_interpret_keyname(REGFI_FILE* file, REGFI_NK* nk, bool strict) |
---|
1271 | { |
---|
1272 | /* XXX: Registry key names are supposedly limited to 255 characters according to: |
---|
1273 | * http://msdn.microsoft.com/en-us/library/ms724872%28VS.85%29.aspx |
---|
1274 | * Might want to emit a warning if this is exceeded. |
---|
1275 | * It is expected that "characters" could be variable width. |
---|
1276 | * Also, it may be useful to use this information to limit false positives |
---|
1277 | * when recovering deleted NK records. |
---|
1278 | */ |
---|
1279 | REGFI_BUFFER tmp_buf; |
---|
1280 | REGFI_ENCODING from_encoding = (nk->flags & REGFI_NK_FLAG_ASCIINAME) |
---|
1281 | ? REGFI_ENCODING_ASCII : REGFI_ENCODING_UTF16LE; |
---|
1282 | |
---|
1283 | if(nk->name_length == 0) |
---|
1284 | return; |
---|
1285 | |
---|
1286 | if(from_encoding == file->string_encoding) |
---|
1287 | { |
---|
1288 | nk->name_raw[nk->name_length] = '\0'; |
---|
1289 | nk->name = (char*)nk->name_raw; |
---|
1290 | } |
---|
1291 | else |
---|
1292 | { |
---|
1293 | tmp_buf = regfi_conv_charset(regfi_encoding_int2str(from_encoding), |
---|
1294 | regfi_encoding_int2str(file->string_encoding), |
---|
1295 | nk->name_raw, nk->name_length); |
---|
1296 | if(tmp_buf.buf == NULL) |
---|
1297 | { |
---|
1298 | regfi_log_add(REGFI_LOG_WARN, "Error occurred while converting" |
---|
1299 | " key name to encoding %s. NK offset: 0x%.8X." |
---|
1300 | " Error message: %s", |
---|
1301 | regfi_encoding_int2str(file->string_encoding), |
---|
1302 | nk->offset, strerror(errno)); |
---|
1303 | nk->name = NULL; |
---|
1304 | } |
---|
1305 | else |
---|
1306 | { |
---|
1307 | nk->name = (char*)tmp_buf.buf; |
---|
1308 | talloc_reparent(NULL, nk, nk->name); |
---|
1309 | } |
---|
1310 | } |
---|
1311 | } |
---|
1312 | |
---|
1313 | |
---|
1314 | /****************************************************************************** |
---|
1315 | * |
---|
1316 | ******************************************************************************/ |
---|
1317 | REGFI_NK* regfi_load_key(REGFI_FILE* file, uint32_t offset, bool strict) |
---|
1318 | { |
---|
1319 | REGFI_NK* nk; |
---|
1320 | uint32_t off; |
---|
1321 | int32_t max_size; |
---|
1322 | |
---|
1323 | if(file->nk_cache != NULL) |
---|
1324 | { |
---|
1325 | /* First, check to see if we have this key in our cache */ |
---|
1326 | if(!regfi_lock(file, &file->mem_lock, "regfi_load_nk")) |
---|
1327 | return NULL; |
---|
1328 | regfi_lock(file, &file->nk_lock, "regfi_load_nk"); |
---|
1329 | |
---|
1330 | nk = (REGFI_NK*)lru_cache_find(file->nk_cache, &offset, 4); |
---|
1331 | if(nk != NULL) |
---|
1332 | nk = talloc_reference(NULL, nk); |
---|
1333 | |
---|
1334 | regfi_unlock(file, &file->nk_lock, "regfi_load_nk"); |
---|
1335 | regfi_unlock(file, &file->mem_lock, "regfi_load_nk"); |
---|
1336 | if(nk != NULL) |
---|
1337 | return nk; |
---|
1338 | } |
---|
1339 | |
---|
1340 | /* Not cached currently, proceed with loading it */ |
---|
1341 | max_size = regfi_calc_maxsize(file, offset); |
---|
1342 | if (max_size < 0) |
---|
1343 | return NULL; |
---|
1344 | |
---|
1345 | /* get the initial nk record */ |
---|
1346 | if((nk = regfi_parse_nk(file, offset, max_size, true)) == NULL) |
---|
1347 | { |
---|
1348 | regfi_log_add(REGFI_LOG_ERROR, "Could not load NK record at" |
---|
1349 | " offset 0x%.8X.", offset); |
---|
1350 | return NULL; |
---|
1351 | } |
---|
1352 | |
---|
1353 | regfi_interpret_keyname(file, nk, strict); |
---|
1354 | |
---|
1355 | /* get value list */ |
---|
1356 | if(nk->num_values && (nk->values_off!=REGFI_OFFSET_NONE)) |
---|
1357 | { |
---|
1358 | off = nk->values_off + REGFI_REGF_SIZE; |
---|
1359 | max_size = regfi_calc_maxsize(file, off); |
---|
1360 | if(max_size < 0) |
---|
1361 | { |
---|
1362 | if(strict) |
---|
1363 | { |
---|
1364 | talloc_free(nk); |
---|
1365 | return NULL; |
---|
1366 | } |
---|
1367 | else |
---|
1368 | nk->values = NULL; |
---|
1369 | } |
---|
1370 | else |
---|
1371 | { |
---|
1372 | nk->values = regfi_load_valuelist(file, off, nk->num_values, |
---|
1373 | max_size, true); |
---|
1374 | if(nk->values == NULL) |
---|
1375 | { |
---|
1376 | regfi_log_add(REGFI_LOG_WARN, "Could not load value list" |
---|
1377 | " for NK record at offset 0x%.8X.", offset); |
---|
1378 | if(strict) |
---|
1379 | { |
---|
1380 | talloc_free(nk); |
---|
1381 | return NULL; |
---|
1382 | } |
---|
1383 | } |
---|
1384 | talloc_reparent(NULL, nk, nk->values); |
---|
1385 | } |
---|
1386 | } |
---|
1387 | |
---|
1388 | /* now get subkey list */ |
---|
1389 | if(nk->num_subkeys && (nk->subkeys_off != REGFI_OFFSET_NONE)) |
---|
1390 | { |
---|
1391 | off = nk->subkeys_off + REGFI_REGF_SIZE; |
---|
1392 | max_size = regfi_calc_maxsize(file, off); |
---|
1393 | if(max_size < 0) |
---|
1394 | { |
---|
1395 | if(strict) |
---|
1396 | { |
---|
1397 | talloc_free(nk); |
---|
1398 | return NULL; |
---|
1399 | } |
---|
1400 | else |
---|
1401 | nk->subkeys = NULL; |
---|
1402 | } |
---|
1403 | else |
---|
1404 | { |
---|
1405 | nk->subkeys = regfi_load_subkeylist(file, off, nk->num_subkeys, |
---|
1406 | max_size, true); |
---|
1407 | |
---|
1408 | if(nk->subkeys == NULL) |
---|
1409 | { |
---|
1410 | regfi_log_add(REGFI_LOG_WARN, "Could not load subkey list" |
---|
1411 | " while parsing NK record at offset 0x%.8X.", offset); |
---|
1412 | nk->num_subkeys = 0; |
---|
1413 | } |
---|
1414 | talloc_reparent(NULL, nk, nk->subkeys); |
---|
1415 | } |
---|
1416 | } |
---|
1417 | |
---|
1418 | if(file->nk_cache != NULL) |
---|
1419 | { |
---|
1420 | /* All is well, so let us cache this key for later */ |
---|
1421 | if(!regfi_lock(file, &file->mem_lock, "regfi_load_nk")) |
---|
1422 | return NULL; |
---|
1423 | regfi_lock(file, &file->nk_lock, "regfi_load_nk"); |
---|
1424 | |
---|
1425 | lru_cache_update(file->nk_cache, &offset, 4, nk); |
---|
1426 | |
---|
1427 | regfi_unlock(file, &file->nk_lock, "regfi_load_nk"); |
---|
1428 | regfi_unlock(file, &file->mem_lock, "regfi_load_nk"); |
---|
1429 | } |
---|
1430 | |
---|
1431 | return nk; |
---|
1432 | } |
---|
1433 | |
---|
1434 | |
---|
1435 | /****************************************************************************** |
---|
1436 | ******************************************************************************/ |
---|
1437 | const REGFI_SK* regfi_load_sk(REGFI_FILE* file, uint32_t offset, bool strict) |
---|
1438 | { |
---|
1439 | REGFI_SK* ret_val = NULL; |
---|
1440 | int32_t max_size; |
---|
1441 | uint32_t* failure_ptr = NULL; |
---|
1442 | |
---|
1443 | max_size = regfi_calc_maxsize(file, offset); |
---|
1444 | if(max_size < 0) |
---|
1445 | return NULL; |
---|
1446 | |
---|
1447 | if(file->sk_cache == NULL) |
---|
1448 | return regfi_parse_sk(file, offset, max_size, strict); |
---|
1449 | if(!regfi_lock(file, &file->mem_lock, "regfi_load_sk")) |
---|
1450 | return NULL; |
---|
1451 | regfi_lock(file, &file->sk_lock, "regfi_load_sk"); |
---|
1452 | |
---|
1453 | /* First look if we have already parsed it */ |
---|
1454 | ret_val = (REGFI_SK*)lru_cache_find(file->sk_cache, &offset, 4); |
---|
1455 | |
---|
1456 | /* Bail out if we have previously cached a parse failure at this offset. */ |
---|
1457 | if(ret_val && *(uint32_t*)ret_val == REGFI_OFFSET_NONE) |
---|
1458 | { |
---|
1459 | ret_val = NULL; |
---|
1460 | goto unlock; |
---|
1461 | } |
---|
1462 | |
---|
1463 | if(ret_val == NULL) |
---|
1464 | { |
---|
1465 | ret_val = regfi_parse_sk(file, offset, max_size, strict); |
---|
1466 | if(ret_val == NULL) |
---|
1467 | { /* Cache the parse failure and bail out. */ |
---|
1468 | failure_ptr = talloc(NULL, uint32_t); |
---|
1469 | if(failure_ptr == NULL) |
---|
1470 | goto unlock; |
---|
1471 | |
---|
1472 | *failure_ptr = REGFI_OFFSET_NONE; |
---|
1473 | lru_cache_update(file->sk_cache, &offset, 4, failure_ptr); |
---|
1474 | |
---|
1475 | /* Let the cache be the only owner of this */ |
---|
1476 | talloc_unlink(NULL, failure_ptr); |
---|
1477 | } |
---|
1478 | else |
---|
1479 | lru_cache_update(file->sk_cache, &offset, 4, ret_val); |
---|
1480 | } |
---|
1481 | else |
---|
1482 | ret_val = talloc_reference(NULL, ret_val); |
---|
1483 | |
---|
1484 | unlock: |
---|
1485 | regfi_unlock(file, &file->sk_lock, "regfi_load_sk"); |
---|
1486 | regfi_unlock(file, &file->mem_lock, "regfi_load_sk"); |
---|
1487 | |
---|
1488 | return ret_val; |
---|
1489 | } |
---|
1490 | |
---|
1491 | |
---|
1492 | |
---|
1493 | /****************************************************************************** |
---|
1494 | ******************************************************************************/ |
---|
1495 | REGFI_NK* regfi_find_root_nk(REGFI_FILE* file, const REGFI_HBIN* hbin) |
---|
1496 | { |
---|
1497 | REGFI_NK* nk = NULL; |
---|
1498 | uint32_t cell_length; |
---|
1499 | uint32_t cur_offset = hbin->file_off+REGFI_HBIN_HEADER_SIZE; |
---|
1500 | uint32_t hbin_end = hbin->file_off+hbin->block_size; |
---|
1501 | bool unalloc; |
---|
1502 | |
---|
1503 | while(cur_offset < hbin_end) |
---|
1504 | { |
---|
1505 | |
---|
1506 | if(!regfi_lock(file, &file->cb_lock, "regfi_find_root_nk")) |
---|
1507 | return NULL; |
---|
1508 | |
---|
1509 | if(!regfi_parse_cell(file->cb, cur_offset, NULL, 0, &cell_length, &unalloc)) |
---|
1510 | { |
---|
1511 | regfi_log_add(REGFI_LOG_WARN, "Could not parse cell at offset" |
---|
1512 | " 0x%.8X while searching for root key.", cur_offset); |
---|
1513 | goto error_locked; |
---|
1514 | } |
---|
1515 | |
---|
1516 | if(!regfi_unlock(file, &file->cb_lock, "regfi_find_root_nk")) |
---|
1517 | return NULL; |
---|
1518 | |
---|
1519 | if(cell_length == 0) |
---|
1520 | break; |
---|
1521 | |
---|
1522 | if(!unalloc) |
---|
1523 | { |
---|
1524 | nk = regfi_load_key(file, cur_offset, true); |
---|
1525 | if(nk != NULL) |
---|
1526 | { |
---|
1527 | if(nk->flags & REGFI_NK_FLAG_ROOT) |
---|
1528 | return nk; |
---|
1529 | } |
---|
1530 | } |
---|
1531 | |
---|
1532 | cur_offset += cell_length; |
---|
1533 | } |
---|
1534 | |
---|
1535 | return NULL; |
---|
1536 | |
---|
1537 | error_locked: |
---|
1538 | regfi_unlock(file, &file->cb_lock, "regfi_find_root_nk"); |
---|
1539 | return NULL; |
---|
1540 | } |
---|
1541 | |
---|
1542 | |
---|
1543 | |
---|
1544 | /****************************************************************************** |
---|
1545 | ******************************************************************************/ |
---|
1546 | REGFI_FILE* regfi_alloc(int fd, REGFI_ENCODING output_encoding) |
---|
1547 | { |
---|
1548 | REGFI_FILE* ret_val; |
---|
1549 | REGFI_RAW_FILE* file_cb = talloc(NULL, REGFI_RAW_FILE); |
---|
1550 | if(file_cb == NULL) |
---|
1551 | return NULL; |
---|
1552 | |
---|
1553 | file_cb->state = (void*)talloc(file_cb, int); |
---|
1554 | if(file_cb->state == NULL) |
---|
1555 | goto fail; |
---|
1556 | *(int*)file_cb->state = fd; |
---|
1557 | |
---|
1558 | file_cb->cur_off = 0; |
---|
1559 | file_cb->size = 0; |
---|
1560 | file_cb->read = ®fi_raw_read; |
---|
1561 | file_cb->seek = ®fi_raw_seek; |
---|
1562 | |
---|
1563 | ret_val = regfi_alloc_cb(file_cb, output_encoding); |
---|
1564 | if(ret_val == NULL) |
---|
1565 | goto fail; |
---|
1566 | |
---|
1567 | /* In this case, we want file_cb to be freed when ret_val is */ |
---|
1568 | talloc_reparent(NULL, ret_val, file_cb); |
---|
1569 | return ret_val; |
---|
1570 | |
---|
1571 | fail: |
---|
1572 | talloc_free(file_cb); |
---|
1573 | return NULL; |
---|
1574 | } |
---|
1575 | |
---|
1576 | |
---|
1577 | /****************************************************************************** |
---|
1578 | ******************************************************************************/ |
---|
1579 | static int regfi_free_cb(void* f) |
---|
1580 | { |
---|
1581 | REGFI_FILE* file = (REGFI_FILE*)f; |
---|
1582 | |
---|
1583 | pthread_mutex_destroy(&file->cb_lock); |
---|
1584 | pthread_rwlock_destroy(&file->hbins_lock); |
---|
1585 | pthread_mutex_destroy(&file->sk_lock); |
---|
1586 | pthread_mutex_destroy(&file->nk_lock); |
---|
1587 | pthread_mutex_destroy(&file->mem_lock); |
---|
1588 | |
---|
1589 | return 0; |
---|
1590 | } |
---|
1591 | |
---|
1592 | |
---|
1593 | /****************************************************************************** |
---|
1594 | ******************************************************************************/ |
---|
1595 | REGFI_FILE* regfi_alloc_cb(REGFI_RAW_FILE* file_cb, |
---|
1596 | REGFI_ENCODING output_encoding) |
---|
1597 | { |
---|
1598 | REGFI_FILE* rb; |
---|
1599 | REGFI_HBIN* hbin = NULL; |
---|
1600 | uint32_t hbin_off, cache_secret; |
---|
1601 | int64_t file_length; |
---|
1602 | bool rla; |
---|
1603 | |
---|
1604 | /* Determine file length. Must be at least big enough for the header |
---|
1605 | * and one hbin. |
---|
1606 | */ |
---|
1607 | file_length = regfi_seek(file_cb, 0, SEEK_END); |
---|
1608 | if(file_length < REGFI_REGF_SIZE+REGFI_HBIN_ALLOC) |
---|
1609 | { |
---|
1610 | regfi_log_add(REGFI_LOG_ERROR, "File length (%d) too short to contain a" |
---|
1611 | " header and at least one HBIN.", file_length); |
---|
1612 | return NULL; |
---|
1613 | } |
---|
1614 | regfi_seek(file_cb, 0, SEEK_SET); |
---|
1615 | |
---|
1616 | if(output_encoding != REGFI_ENCODING_UTF8 |
---|
1617 | && output_encoding != REGFI_ENCODING_ASCII) |
---|
1618 | { |
---|
1619 | regfi_log_add(REGFI_LOG_ERROR, "Invalid output_encoding supplied" |
---|
1620 | " in creation of regfi iterator."); |
---|
1621 | return NULL; |
---|
1622 | } |
---|
1623 | |
---|
1624 | /* Read file header */ |
---|
1625 | if ((rb = regfi_parse_regf(file_cb, false)) == NULL) |
---|
1626 | { |
---|
1627 | regfi_log_add(REGFI_LOG_ERROR, "Failed to read REGF block."); |
---|
1628 | return NULL; |
---|
1629 | } |
---|
1630 | rb->file_length = file_length; |
---|
1631 | rb->cb = file_cb; |
---|
1632 | rb->string_encoding = output_encoding; |
---|
1633 | |
---|
1634 | if(pthread_mutex_init(&rb->cb_lock, NULL) != 0) |
---|
1635 | { |
---|
1636 | regfi_log_add(REGFI_LOG_ERROR, "Failed to create cb_lock mutex."); |
---|
1637 | goto fail; |
---|
1638 | } |
---|
1639 | |
---|
1640 | if(pthread_rwlock_init(&rb->hbins_lock, NULL) != 0) |
---|
1641 | { |
---|
1642 | regfi_log_add(REGFI_LOG_ERROR, "Failed to create hbins_lock rwlock."); |
---|
1643 | goto fail; |
---|
1644 | } |
---|
1645 | |
---|
1646 | if(pthread_mutex_init(&rb->sk_lock, NULL) != 0) |
---|
1647 | { |
---|
1648 | regfi_log_add(REGFI_LOG_ERROR, "Failed to create sk_lock mutex."); |
---|
1649 | goto fail; |
---|
1650 | } |
---|
1651 | |
---|
1652 | if(pthread_mutex_init(&rb->nk_lock, NULL) != 0) |
---|
1653 | { |
---|
1654 | regfi_log_add(REGFI_LOG_ERROR, "Failed to create nk_lock mutex."); |
---|
1655 | goto fail; |
---|
1656 | } |
---|
1657 | |
---|
1658 | if(pthread_mutex_init(&rb->mem_lock, NULL) != 0) |
---|
1659 | { |
---|
1660 | regfi_log_add(REGFI_LOG_ERROR, "Failed to create mem_lock mutex."); |
---|
1661 | goto fail; |
---|
1662 | } |
---|
1663 | |
---|
1664 | rb->hbins = range_list_new(); |
---|
1665 | if(rb->hbins == NULL) |
---|
1666 | { |
---|
1667 | regfi_log_add(REGFI_LOG_ERROR, "Failed to create HBIN range_list."); |
---|
1668 | goto fail; |
---|
1669 | } |
---|
1670 | talloc_reparent(NULL, rb, rb->hbins); |
---|
1671 | |
---|
1672 | rla = true; |
---|
1673 | hbin_off = REGFI_REGF_SIZE; |
---|
1674 | hbin = regfi_parse_hbin(rb, hbin_off, true); |
---|
1675 | while(hbin && rla) |
---|
1676 | { |
---|
1677 | rla = range_list_add(rb->hbins, hbin->file_off, hbin->block_size, hbin); |
---|
1678 | if(rla) |
---|
1679 | talloc_reparent(NULL, rb->hbins, hbin); |
---|
1680 | |
---|
1681 | hbin_off = hbin->file_off + hbin->block_size; |
---|
1682 | hbin = regfi_parse_hbin(rb, hbin_off, true); |
---|
1683 | } |
---|
1684 | |
---|
1685 | /* This secret isn't very secret, but we don't need a good one. This |
---|
1686 | * secret is just designed to prevent someone from trying to blow our |
---|
1687 | * caching and make things slow. |
---|
1688 | */ |
---|
1689 | cache_secret = 0x15DEAD05^time(NULL)^(getpid()<<16); |
---|
1690 | |
---|
1691 | rb->sk_cache = NULL; |
---|
1692 | if(REGFI_CACHE_SK_MAX > 0) |
---|
1693 | rb->sk_cache = lru_cache_create_ctx(rb, REGFI_CACHE_SK_MAX, |
---|
1694 | cache_secret, true); |
---|
1695 | |
---|
1696 | rb->nk_cache = NULL; |
---|
1697 | if(REGFI_CACHE_NK_MAX > 0) |
---|
1698 | rb->nk_cache = lru_cache_create_ctx(rb, REGFI_CACHE_NK_MAX, |
---|
1699 | cache_secret, true); |
---|
1700 | |
---|
1701 | /* success */ |
---|
1702 | talloc_set_destructor((void*)rb, regfi_free_cb); |
---|
1703 | return rb; |
---|
1704 | |
---|
1705 | fail: |
---|
1706 | pthread_mutex_destroy(&rb->cb_lock); |
---|
1707 | pthread_rwlock_destroy(&rb->hbins_lock); |
---|
1708 | pthread_mutex_destroy(&rb->sk_lock); |
---|
1709 | pthread_mutex_destroy(&rb->nk_lock); |
---|
1710 | pthread_mutex_destroy(&rb->mem_lock); |
---|
1711 | |
---|
1712 | range_list_free(rb->hbins); |
---|
1713 | talloc_free(rb); |
---|
1714 | return NULL; |
---|
1715 | } |
---|
1716 | |
---|
1717 | |
---|
1718 | /****************************************************************************** |
---|
1719 | ******************************************************************************/ |
---|
1720 | void regfi_free(REGFI_FILE* file) |
---|
1721 | { |
---|
1722 | /* Callback handles cleanup side effects */ |
---|
1723 | talloc_free(file); |
---|
1724 | } |
---|
1725 | |
---|
1726 | |
---|
1727 | /****************************************************************************** |
---|
1728 | * First checks the offset given by the file header, then checks the |
---|
1729 | * rest of the file if that fails. |
---|
1730 | ******************************************************************************/ |
---|
1731 | const REGFI_NK* regfi_get_rootkey(REGFI_FILE* file) |
---|
1732 | { |
---|
1733 | REGFI_NK* nk = NULL; |
---|
1734 | REGFI_HBIN* hbin; |
---|
1735 | uint32_t root_offset, i, num_hbins; |
---|
1736 | |
---|
1737 | if(!file) |
---|
1738 | return NULL; |
---|
1739 | |
---|
1740 | root_offset = file->root_cell+REGFI_REGF_SIZE; |
---|
1741 | nk = regfi_load_key(file, root_offset, true); |
---|
1742 | if(nk != NULL) |
---|
1743 | { |
---|
1744 | if(nk->flags & REGFI_NK_FLAG_ROOT) |
---|
1745 | return nk; |
---|
1746 | } |
---|
1747 | |
---|
1748 | regfi_log_add(REGFI_LOG_WARN, "File header indicated root key at" |
---|
1749 | " location 0x%.8X, but no root key found." |
---|
1750 | " Searching rest of file...", root_offset); |
---|
1751 | |
---|
1752 | /* If the file header gives bad info, scan through the file one HBIN |
---|
1753 | * block at a time looking for an NK record with a root key type. |
---|
1754 | */ |
---|
1755 | |
---|
1756 | if(!regfi_read_lock(file, &file->hbins_lock, "regfi_get_rootkey")) |
---|
1757 | return NULL; |
---|
1758 | |
---|
1759 | num_hbins = range_list_size(file->hbins); |
---|
1760 | for(i=0; i < num_hbins && nk == NULL; i++) |
---|
1761 | { |
---|
1762 | hbin = (REGFI_HBIN*)range_list_get(file->hbins, i)->data; |
---|
1763 | nk = regfi_find_root_nk(file, hbin); |
---|
1764 | } |
---|
1765 | |
---|
1766 | if(!regfi_rw_unlock(file, &file->hbins_lock, "regfi_get_rootkey")) |
---|
1767 | return NULL; |
---|
1768 | |
---|
1769 | return nk; |
---|
1770 | } |
---|
1771 | |
---|
1772 | |
---|
1773 | /****************************************************************************** |
---|
1774 | *****************************************************************************/ |
---|
1775 | void regfi_free_record(REGFI_FILE* file, const void* record) |
---|
1776 | { |
---|
1777 | if(!regfi_lock(file, &file->mem_lock, "regfi_free_record")) |
---|
1778 | return; |
---|
1779 | |
---|
1780 | talloc_unlink(NULL, (void*)record); |
---|
1781 | |
---|
1782 | regfi_unlock(file, &file->mem_lock, "regfi_free_record"); |
---|
1783 | } |
---|
1784 | |
---|
1785 | |
---|
1786 | /****************************************************************************** |
---|
1787 | *****************************************************************************/ |
---|
1788 | const void* regfi_reference_record(REGFI_FILE* file, const void* record) |
---|
1789 | { |
---|
1790 | const void* ret_val = NULL; |
---|
1791 | |
---|
1792 | if(!regfi_lock(file, &file->mem_lock, "regfi_reference_record")) |
---|
1793 | return ret_val; |
---|
1794 | |
---|
1795 | ret_val = talloc_reference(NULL, record); |
---|
1796 | |
---|
1797 | regfi_unlock(file, &file->mem_lock, "regfi_reference_record"); |
---|
1798 | return ret_val; |
---|
1799 | } |
---|
1800 | |
---|
1801 | |
---|
1802 | /****************************************************************************** |
---|
1803 | *****************************************************************************/ |
---|
1804 | uint32_t regfi_fetch_num_subkeys(const REGFI_NK* key) |
---|
1805 | { |
---|
1806 | uint32_t num_in_list = 0; |
---|
1807 | if(key == NULL) |
---|
1808 | return 0; |
---|
1809 | |
---|
1810 | if(key->subkeys != NULL) |
---|
1811 | num_in_list = key->subkeys->num_keys; |
---|
1812 | |
---|
1813 | if(num_in_list != key->num_subkeys) |
---|
1814 | { |
---|
1815 | regfi_log_add(REGFI_LOG_INFO, "Key at offset 0x%.8X contains %d keys in its" |
---|
1816 | " subkey list but reports %d should be available.", |
---|
1817 | key->offset, num_in_list, key->num_subkeys); |
---|
1818 | return (num_in_list < key->num_subkeys)?num_in_list:key->num_subkeys; |
---|
1819 | } |
---|
1820 | |
---|
1821 | return num_in_list; |
---|
1822 | } |
---|
1823 | |
---|
1824 | |
---|
1825 | /****************************************************************************** |
---|
1826 | *****************************************************************************/ |
---|
1827 | uint32_t regfi_fetch_num_values(const REGFI_NK* key) |
---|
1828 | { |
---|
1829 | uint32_t num_in_list = 0; |
---|
1830 | if(key == NULL) |
---|
1831 | return 0; |
---|
1832 | |
---|
1833 | if(key->values != NULL) |
---|
1834 | num_in_list = key->values->num_values; |
---|
1835 | |
---|
1836 | if(num_in_list != key->num_values) |
---|
1837 | { |
---|
1838 | regfi_log_add(REGFI_LOG_INFO, "Key at offset 0x%.8X contains %d values in" |
---|
1839 | " its value list but reports %d should be available.", |
---|
1840 | key->offset, num_in_list, key->num_values); |
---|
1841 | return (num_in_list < key->num_values)?num_in_list:key->num_values; |
---|
1842 | } |
---|
1843 | |
---|
1844 | return num_in_list; |
---|
1845 | } |
---|
1846 | |
---|
1847 | |
---|
1848 | /****************************************************************************** |
---|
1849 | *****************************************************************************/ |
---|
1850 | REGFI_ITERATOR* regfi_iterator_new(REGFI_FILE* file) |
---|
1851 | { |
---|
1852 | REGFI_NK* root; |
---|
1853 | REGFI_ITERATOR* ret_val; |
---|
1854 | |
---|
1855 | ret_val = talloc(NULL, REGFI_ITERATOR); |
---|
1856 | if(ret_val == NULL) |
---|
1857 | return NULL; |
---|
1858 | |
---|
1859 | ret_val->cur = talloc(ret_val, REGFI_ITER_POSITION); |
---|
1860 | if(ret_val->cur == NULL) |
---|
1861 | { |
---|
1862 | talloc_free(ret_val); |
---|
1863 | return NULL; |
---|
1864 | } |
---|
1865 | |
---|
1866 | ret_val->key_positions = void_stack_new(REGFI_MAX_DEPTH); |
---|
1867 | if(ret_val->key_positions == NULL) |
---|
1868 | { |
---|
1869 | talloc_free(ret_val); |
---|
1870 | return NULL; |
---|
1871 | } |
---|
1872 | talloc_reparent(NULL, ret_val, ret_val->key_positions); |
---|
1873 | |
---|
1874 | root = (REGFI_NK*)regfi_get_rootkey(file); |
---|
1875 | if(root == NULL) |
---|
1876 | { |
---|
1877 | talloc_free(ret_val); |
---|
1878 | return NULL; |
---|
1879 | } |
---|
1880 | |
---|
1881 | ret_val->cur->offset = root->offset; |
---|
1882 | if(root->subkeys_off == REGFI_OFFSET_NONE) |
---|
1883 | ret_val->cur->num_subkeys = 0; |
---|
1884 | else |
---|
1885 | ret_val->cur->num_subkeys = regfi_fetch_num_subkeys(root); |
---|
1886 | |
---|
1887 | if(root->values_off == REGFI_OFFSET_NONE) |
---|
1888 | ret_val->cur->num_values = 0; |
---|
1889 | else |
---|
1890 | ret_val->cur->num_values = regfi_fetch_num_values(root); |
---|
1891 | |
---|
1892 | ret_val->cur->cur_subkey = 0; |
---|
1893 | ret_val->cur->cur_value = 0; |
---|
1894 | ret_val->f = file; |
---|
1895 | |
---|
1896 | regfi_free_record(ret_val->f, root); |
---|
1897 | return ret_val; |
---|
1898 | } |
---|
1899 | |
---|
1900 | |
---|
1901 | /****************************************************************************** |
---|
1902 | *****************************************************************************/ |
---|
1903 | void regfi_iterator_free(REGFI_ITERATOR* i) |
---|
1904 | { |
---|
1905 | talloc_unlink(NULL, i); |
---|
1906 | } |
---|
1907 | |
---|
1908 | |
---|
1909 | /****************************************************************************** |
---|
1910 | *****************************************************************************/ |
---|
1911 | /* XXX: some way of indicating reason for failure should be added. */ |
---|
1912 | bool regfi_iterator_down(REGFI_ITERATOR* i) |
---|
1913 | { |
---|
1914 | REGFI_NK* subkey; |
---|
1915 | REGFI_ITER_POSITION* pos = talloc(i, REGFI_ITER_POSITION); |
---|
1916 | if(pos == NULL) |
---|
1917 | return false; |
---|
1918 | |
---|
1919 | subkey = (REGFI_NK*)regfi_iterator_cur_subkey(i); |
---|
1920 | if(subkey == NULL) |
---|
1921 | { |
---|
1922 | talloc_free(pos); |
---|
1923 | return false; |
---|
1924 | } |
---|
1925 | |
---|
1926 | if(!void_stack_push(i->key_positions, i->cur)) |
---|
1927 | { |
---|
1928 | talloc_free(pos); |
---|
1929 | regfi_free_record(i->f, subkey); |
---|
1930 | return false; |
---|
1931 | } |
---|
1932 | |
---|
1933 | pos->offset = subkey->offset; |
---|
1934 | if(subkey->subkeys_off == REGFI_OFFSET_NONE) |
---|
1935 | pos->num_subkeys = 0; |
---|
1936 | else |
---|
1937 | pos->num_subkeys = regfi_fetch_num_subkeys(subkey); |
---|
1938 | |
---|
1939 | if(subkey->values_off == REGFI_OFFSET_NONE) |
---|
1940 | pos->num_values = 0; |
---|
1941 | else |
---|
1942 | pos->num_values = regfi_fetch_num_values(subkey); |
---|
1943 | |
---|
1944 | pos->cur_subkey = 0; |
---|
1945 | pos->cur_value = 0; |
---|
1946 | i->cur = pos; |
---|
1947 | |
---|
1948 | regfi_free_record(i->f, subkey); |
---|
1949 | return true; |
---|
1950 | } |
---|
1951 | |
---|
1952 | |
---|
1953 | /****************************************************************************** |
---|
1954 | *****************************************************************************/ |
---|
1955 | bool regfi_iterator_up(REGFI_ITERATOR* i) |
---|
1956 | { |
---|
1957 | REGFI_ITER_POSITION* pos; |
---|
1958 | |
---|
1959 | pos = (REGFI_ITER_POSITION*)void_stack_pop(i->key_positions); |
---|
1960 | if(pos == NULL) |
---|
1961 | return false; |
---|
1962 | |
---|
1963 | if(!regfi_lock(i->f, &i->f->mem_lock, "regfi_iterator_up")) |
---|
1964 | return false; |
---|
1965 | |
---|
1966 | talloc_unlink(i, i->cur); |
---|
1967 | |
---|
1968 | regfi_unlock(i->f, &i->f->mem_lock, "regfi_iterator_up"); |
---|
1969 | |
---|
1970 | i->cur = pos; |
---|
1971 | return true; |
---|
1972 | } |
---|
1973 | |
---|
1974 | |
---|
1975 | /****************************************************************************** |
---|
1976 | *****************************************************************************/ |
---|
1977 | bool regfi_iterator_to_root(REGFI_ITERATOR* i) |
---|
1978 | { |
---|
1979 | while(regfi_iterator_up(i)) |
---|
1980 | continue; |
---|
1981 | |
---|
1982 | return true; |
---|
1983 | } |
---|
1984 | |
---|
1985 | |
---|
1986 | /****************************************************************************** |
---|
1987 | *****************************************************************************/ |
---|
1988 | bool regfi_iterator_find_subkey(REGFI_ITERATOR* i, const char* name) |
---|
1989 | { |
---|
1990 | const REGFI_NK* cur_key; |
---|
1991 | uint32_t new_index; |
---|
1992 | bool ret_val = false; |
---|
1993 | |
---|
1994 | cur_key = regfi_iterator_cur_key(i); |
---|
1995 | if(cur_key == NULL) |
---|
1996 | { |
---|
1997 | regfi_log_add(REGFI_LOG_ERROR, "Current key invalid in find_subkey."); |
---|
1998 | return ret_val; |
---|
1999 | } |
---|
2000 | |
---|
2001 | if(regfi_find_subkey(i->f, cur_key, name, &new_index)) |
---|
2002 | { |
---|
2003 | i->cur->cur_subkey = new_index; |
---|
2004 | ret_val = true; |
---|
2005 | } |
---|
2006 | |
---|
2007 | regfi_free_record(i->f, cur_key); |
---|
2008 | return ret_val; |
---|
2009 | } |
---|
2010 | |
---|
2011 | |
---|
2012 | /****************************************************************************** |
---|
2013 | *****************************************************************************/ |
---|
2014 | bool regfi_iterator_descend(REGFI_ITERATOR* i, const char** path) |
---|
2015 | { |
---|
2016 | uint32_t x; |
---|
2017 | if(path == NULL) |
---|
2018 | return false; |
---|
2019 | |
---|
2020 | for(x=0; |
---|
2021 | ((path[x] != NULL) && regfi_iterator_find_subkey(i, path[x]) |
---|
2022 | && regfi_iterator_down(i)); |
---|
2023 | x++) |
---|
2024 | { continue; } |
---|
2025 | |
---|
2026 | if(path[x] == NULL) |
---|
2027 | { |
---|
2028 | return true; |
---|
2029 | } |
---|
2030 | |
---|
2031 | /* XXX: is this the right number of times? */ |
---|
2032 | for(; x > 0; x--) |
---|
2033 | regfi_iterator_up(i); |
---|
2034 | |
---|
2035 | return false; |
---|
2036 | } |
---|
2037 | |
---|
2038 | |
---|
2039 | /****************************************************************************** |
---|
2040 | *****************************************************************************/ |
---|
2041 | const REGFI_NK* regfi_iterator_cur_key(REGFI_ITERATOR* i) |
---|
2042 | { |
---|
2043 | const REGFI_NK* ret_val = NULL; |
---|
2044 | |
---|
2045 | ret_val = regfi_load_key(i->f, i->cur->offset, true); |
---|
2046 | return ret_val; |
---|
2047 | } |
---|
2048 | |
---|
2049 | |
---|
2050 | /****************************************************************************** |
---|
2051 | *****************************************************************************/ |
---|
2052 | const REGFI_SK* regfi_fetch_sk(REGFI_FILE* file, const REGFI_NK* key) |
---|
2053 | { |
---|
2054 | if(key == NULL || key->sk_off == REGFI_OFFSET_NONE) |
---|
2055 | return NULL; |
---|
2056 | |
---|
2057 | /*lru_cache_print(file->sk_cache);*/ |
---|
2058 | return regfi_load_sk(file, key->sk_off + REGFI_REGF_SIZE, true); |
---|
2059 | } |
---|
2060 | |
---|
2061 | |
---|
2062 | /****************************************************************************** |
---|
2063 | *****************************************************************************/ |
---|
2064 | const REGFI_SK* regfi_next_sk(REGFI_FILE* file, const REGFI_SK* sk) |
---|
2065 | { |
---|
2066 | if(sk == NULL || sk->next_sk_off == REGFI_OFFSET_NONE) |
---|
2067 | return NULL; |
---|
2068 | |
---|
2069 | return regfi_load_sk(file, sk->next_sk_off + REGFI_REGF_SIZE, true); |
---|
2070 | } |
---|
2071 | |
---|
2072 | |
---|
2073 | /****************************************************************************** |
---|
2074 | *****************************************************************************/ |
---|
2075 | const REGFI_SK* regfi_prev_sk(REGFI_FILE* file, const REGFI_SK* sk) |
---|
2076 | { |
---|
2077 | if(sk == NULL || sk->prev_sk_off == REGFI_OFFSET_NONE) |
---|
2078 | return NULL; |
---|
2079 | |
---|
2080 | return regfi_load_sk(file, sk->prev_sk_off + REGFI_REGF_SIZE, true); |
---|
2081 | } |
---|
2082 | |
---|
2083 | |
---|
2084 | /****************************************************************************** |
---|
2085 | *****************************************************************************/ |
---|
2086 | bool regfi_iterator_first_subkey(REGFI_ITERATOR* i) |
---|
2087 | { |
---|
2088 | i->cur->cur_subkey = 0; |
---|
2089 | return (i->cur->cur_subkey < i->cur->num_subkeys); |
---|
2090 | } |
---|
2091 | |
---|
2092 | |
---|
2093 | /****************************************************************************** |
---|
2094 | *****************************************************************************/ |
---|
2095 | const REGFI_NK* regfi_iterator_cur_subkey(REGFI_ITERATOR* i) |
---|
2096 | { |
---|
2097 | const REGFI_NK* cur_key; |
---|
2098 | const REGFI_NK* ret_val; |
---|
2099 | |
---|
2100 | cur_key = regfi_iterator_cur_key(i); |
---|
2101 | if(cur_key == NULL) |
---|
2102 | { |
---|
2103 | regfi_log_add(REGFI_LOG_ERROR, "Current key invalid in cur_subkey."); |
---|
2104 | return NULL; |
---|
2105 | } |
---|
2106 | |
---|
2107 | ret_val = regfi_get_subkey(i->f, cur_key, i->cur->cur_subkey); |
---|
2108 | |
---|
2109 | regfi_free_record(i->f, cur_key); |
---|
2110 | return ret_val; |
---|
2111 | } |
---|
2112 | |
---|
2113 | |
---|
2114 | /****************************************************************************** |
---|
2115 | *****************************************************************************/ |
---|
2116 | bool regfi_iterator_next_subkey(REGFI_ITERATOR* i) |
---|
2117 | { |
---|
2118 | i->cur->cur_subkey++; |
---|
2119 | return (i->cur->cur_subkey < i->cur->num_subkeys); |
---|
2120 | } |
---|
2121 | |
---|
2122 | |
---|
2123 | /****************************************************************************** |
---|
2124 | *****************************************************************************/ |
---|
2125 | bool regfi_iterator_find_value(REGFI_ITERATOR* i, const char* name) |
---|
2126 | { |
---|
2127 | const REGFI_NK* cur_key; |
---|
2128 | uint32_t new_index; |
---|
2129 | bool ret_val = false; |
---|
2130 | |
---|
2131 | cur_key = regfi_iterator_cur_key(i); |
---|
2132 | if(cur_key == NULL) |
---|
2133 | { |
---|
2134 | regfi_log_add(REGFI_LOG_ERROR, "Current key invalid in find_value."); |
---|
2135 | return ret_val; |
---|
2136 | } |
---|
2137 | |
---|
2138 | if(regfi_find_value(i->f, cur_key, name, &new_index)) |
---|
2139 | { |
---|
2140 | i->cur->cur_value = new_index; |
---|
2141 | ret_val = true; |
---|
2142 | } |
---|
2143 | |
---|
2144 | regfi_free_record(i->f, cur_key); |
---|
2145 | return ret_val; |
---|
2146 | } |
---|
2147 | |
---|
2148 | |
---|
2149 | /****************************************************************************** |
---|
2150 | *****************************************************************************/ |
---|
2151 | bool regfi_iterator_first_value(REGFI_ITERATOR* i) |
---|
2152 | { |
---|
2153 | i->cur->cur_value = 0; |
---|
2154 | return (i->cur->cur_value < i->cur->num_values); |
---|
2155 | } |
---|
2156 | |
---|
2157 | |
---|
2158 | /****************************************************************************** |
---|
2159 | *****************************************************************************/ |
---|
2160 | const REGFI_VK* regfi_iterator_cur_value(REGFI_ITERATOR* i) |
---|
2161 | { |
---|
2162 | const REGFI_NK* cur_key; |
---|
2163 | const REGFI_VK* ret_val = NULL; |
---|
2164 | |
---|
2165 | cur_key = regfi_iterator_cur_key(i); |
---|
2166 | if(cur_key == NULL) |
---|
2167 | { |
---|
2168 | regfi_log_add(REGFI_LOG_ERROR, "Current key invalid in cur_value."); |
---|
2169 | return ret_val; |
---|
2170 | } |
---|
2171 | |
---|
2172 | ret_val = regfi_get_value(i->f, cur_key, i->cur->cur_value); |
---|
2173 | |
---|
2174 | regfi_free_record(i->f, cur_key); |
---|
2175 | return ret_val; |
---|
2176 | } |
---|
2177 | |
---|
2178 | |
---|
2179 | /****************************************************************************** |
---|
2180 | *****************************************************************************/ |
---|
2181 | bool regfi_iterator_next_value(REGFI_ITERATOR* i) |
---|
2182 | { |
---|
2183 | i->cur->cur_value++; |
---|
2184 | return (i->cur->cur_value < i->cur->num_values); |
---|
2185 | } |
---|
2186 | |
---|
2187 | |
---|
2188 | |
---|
2189 | |
---|
2190 | /****************************************************************************** |
---|
2191 | *****************************************************************************/ |
---|
2192 | const REGFI_NK** regfi_iterator_ancestry(REGFI_ITERATOR* i) |
---|
2193 | { |
---|
2194 | REGFI_NK** ret_val; |
---|
2195 | void_stack_iterator* iter; |
---|
2196 | const REGFI_ITER_POSITION* cur; |
---|
2197 | uint16_t k, num_keys; |
---|
2198 | |
---|
2199 | num_keys = void_stack_size(i->key_positions)+1; |
---|
2200 | ret_val = talloc_array(NULL, REGFI_NK*, num_keys+1); |
---|
2201 | if(ret_val == NULL) |
---|
2202 | return NULL; |
---|
2203 | |
---|
2204 | iter = void_stack_iterator_new(i->key_positions); |
---|
2205 | if (iter == NULL) |
---|
2206 | { |
---|
2207 | talloc_free(ret_val); |
---|
2208 | return NULL; |
---|
2209 | } |
---|
2210 | |
---|
2211 | k=0; |
---|
2212 | for(cur=void_stack_iterator_next(iter); |
---|
2213 | cur != NULL; cur=void_stack_iterator_next(iter)) |
---|
2214 | { |
---|
2215 | ret_val[k++] = regfi_load_key(i->f, cur->offset, true); |
---|
2216 | } |
---|
2217 | ret_val[k] = regfi_load_key(i->f, i->cur->offset, true); |
---|
2218 | void_stack_iterator_free(iter); |
---|
2219 | |
---|
2220 | if(!regfi_lock(i->f, &i->f->mem_lock, "regfi_iterator_ancestry")) |
---|
2221 | { |
---|
2222 | talloc_free(ret_val); |
---|
2223 | return NULL; |
---|
2224 | } |
---|
2225 | |
---|
2226 | for(k=0; k<num_keys; k++) |
---|
2227 | talloc_reparent(NULL, ret_val, ret_val[k]); |
---|
2228 | |
---|
2229 | regfi_unlock(i->f, &i->f->mem_lock, "regfi_iterator_ancestry"); |
---|
2230 | |
---|
2231 | ret_val[k] = NULL; |
---|
2232 | return (const REGFI_NK**)ret_val; |
---|
2233 | } |
---|
2234 | |
---|
2235 | |
---|
2236 | /****************************************************************************** |
---|
2237 | *****************************************************************************/ |
---|
2238 | const REGFI_CLASSNAME* regfi_fetch_classname(REGFI_FILE* file, |
---|
2239 | const REGFI_NK* key) |
---|
2240 | { |
---|
2241 | REGFI_CLASSNAME* ret_val; |
---|
2242 | uint8_t* raw; |
---|
2243 | REGFI_BUFFER tmp_buf; |
---|
2244 | uint32_t offset; |
---|
2245 | int32_t max_size; |
---|
2246 | uint16_t parse_length; |
---|
2247 | |
---|
2248 | if(key->classname_off == REGFI_OFFSET_NONE || key->classname_length == 0) |
---|
2249 | return NULL; |
---|
2250 | |
---|
2251 | offset = key->classname_off + REGFI_REGF_SIZE; |
---|
2252 | max_size = regfi_calc_maxsize(file, offset); |
---|
2253 | if(max_size <= 0) |
---|
2254 | return NULL; |
---|
2255 | |
---|
2256 | parse_length = key->classname_length; |
---|
2257 | raw = regfi_parse_classname(file, offset, &parse_length, max_size, true); |
---|
2258 | |
---|
2259 | if(raw == NULL) |
---|
2260 | { |
---|
2261 | regfi_log_add(REGFI_LOG_WARN, "Could not parse class" |
---|
2262 | " name at offset 0x%.8X for key record at offset 0x%.8X.", |
---|
2263 | offset, key->offset); |
---|
2264 | return NULL; |
---|
2265 | } |
---|
2266 | |
---|
2267 | ret_val = talloc(NULL, REGFI_CLASSNAME); |
---|
2268 | if(ret_val == NULL) |
---|
2269 | return NULL; |
---|
2270 | |
---|
2271 | ret_val->offset = offset; |
---|
2272 | ret_val->raw = raw; |
---|
2273 | ret_val->size = parse_length; |
---|
2274 | talloc_reparent(NULL, ret_val, raw); |
---|
2275 | |
---|
2276 | tmp_buf = regfi_conv_charset(regfi_encoding_int2str(REGFI_ENCODING_UTF16LE), |
---|
2277 | regfi_encoding_int2str(file->string_encoding), |
---|
2278 | raw, parse_length); |
---|
2279 | if(tmp_buf.buf == NULL) |
---|
2280 | { |
---|
2281 | regfi_log_add(REGFI_LOG_WARN, "Error occurred while" |
---|
2282 | " converting classname to charset %s. Error message: %s", |
---|
2283 | file->string_encoding, strerror(errno)); |
---|
2284 | ret_val->interpreted = NULL; |
---|
2285 | } |
---|
2286 | else |
---|
2287 | { |
---|
2288 | ret_val->interpreted = (char*)tmp_buf.buf; |
---|
2289 | talloc_reparent(NULL, ret_val, tmp_buf.buf); |
---|
2290 | } |
---|
2291 | |
---|
2292 | return ret_val; |
---|
2293 | } |
---|
2294 | |
---|
2295 | |
---|
2296 | /****************************************************************************** |
---|
2297 | *****************************************************************************/ |
---|
2298 | const REGFI_DATA* regfi_fetch_data(REGFI_FILE* file, |
---|
2299 | const REGFI_VK* value) |
---|
2300 | { |
---|
2301 | REGFI_DATA* ret_val = NULL; |
---|
2302 | REGFI_BUFFER raw_data; |
---|
2303 | |
---|
2304 | if(value->data_size != 0) |
---|
2305 | { |
---|
2306 | raw_data = regfi_load_data(file, value->data_off, value->data_size, |
---|
2307 | value->data_in_offset, true); |
---|
2308 | if(raw_data.buf == NULL) |
---|
2309 | { |
---|
2310 | regfi_log_add(REGFI_LOG_WARN, "Could not parse data record" |
---|
2311 | " while parsing VK record at offset 0x%.8X.", |
---|
2312 | value->offset); |
---|
2313 | } |
---|
2314 | else |
---|
2315 | { |
---|
2316 | ret_val = regfi_buffer_to_data(raw_data); |
---|
2317 | |
---|
2318 | if(ret_val == NULL) |
---|
2319 | { |
---|
2320 | regfi_log_add(REGFI_LOG_WARN, "Error occurred in converting" |
---|
2321 | " data buffer to data structure while interpreting " |
---|
2322 | "data for VK record at offset 0x%.8X.", |
---|
2323 | value->offset); |
---|
2324 | talloc_free(raw_data.buf); |
---|
2325 | return NULL; |
---|
2326 | } |
---|
2327 | |
---|
2328 | if(!regfi_interpret_data(file, value->type, ret_val)) |
---|
2329 | { |
---|
2330 | regfi_log_add(REGFI_LOG_INFO, "Error occurred while" |
---|
2331 | " interpreting data for VK record at offset 0x%.8X.", |
---|
2332 | value->offset); |
---|
2333 | } |
---|
2334 | } |
---|
2335 | } |
---|
2336 | |
---|
2337 | return ret_val; |
---|
2338 | } |
---|
2339 | |
---|
2340 | |
---|
2341 | |
---|
2342 | /****************************************************************************** |
---|
2343 | *****************************************************************************/ |
---|
2344 | bool regfi_find_subkey(REGFI_FILE* file, const REGFI_NK* key, |
---|
2345 | const char* name, uint32_t* index) |
---|
2346 | { |
---|
2347 | const REGFI_NK* cur; |
---|
2348 | uint32_t i; |
---|
2349 | uint32_t num_subkeys = regfi_fetch_num_subkeys(key); |
---|
2350 | bool found = false; |
---|
2351 | |
---|
2352 | /* XXX: should we allow "(default)" subkey names? |
---|
2353 | * Do realistically they exist? |
---|
2354 | */ |
---|
2355 | if(name == NULL) |
---|
2356 | return false; |
---|
2357 | |
---|
2358 | /* XXX: Should lazily build a hash table in memory to index where keys are when |
---|
2359 | * there are a large number of subkeys. Attach this to cached keys to |
---|
2360 | * bound the extra amount of memory used. |
---|
2361 | */ |
---|
2362 | for(i=0; (i < num_subkeys) && (found == false); i++) |
---|
2363 | { |
---|
2364 | cur = regfi_get_subkey(file, key, i); |
---|
2365 | if(cur == NULL) |
---|
2366 | return false; |
---|
2367 | |
---|
2368 | /* A NULL name signifies the "(default)" value for a key */ |
---|
2369 | if(cur->name != NULL && (strcasecmp(cur->name, name) == 0)) |
---|
2370 | { |
---|
2371 | found = true; |
---|
2372 | *index = i; |
---|
2373 | } |
---|
2374 | |
---|
2375 | regfi_free_record(file, cur); |
---|
2376 | } |
---|
2377 | |
---|
2378 | return found; |
---|
2379 | } |
---|
2380 | |
---|
2381 | |
---|
2382 | |
---|
2383 | /****************************************************************************** |
---|
2384 | *****************************************************************************/ |
---|
2385 | bool regfi_find_value(REGFI_FILE* file, const REGFI_NK* key, |
---|
2386 | const char* name, uint32_t* index) |
---|
2387 | { |
---|
2388 | const REGFI_VK* cur; |
---|
2389 | uint32_t i; |
---|
2390 | uint32_t num_values = regfi_fetch_num_values(key); |
---|
2391 | bool found = false; |
---|
2392 | |
---|
2393 | /* XXX: Should lazily build a hash table in memory to index where values are when |
---|
2394 | * there are a large number of them. Attach this to cached keys to |
---|
2395 | * bound the extra amount of memory used. |
---|
2396 | */ |
---|
2397 | for(i=0; (i < num_values) && (found == false); i++) |
---|
2398 | { |
---|
2399 | cur = regfi_get_value(file, key, i); |
---|
2400 | if(cur == NULL) |
---|
2401 | return false; |
---|
2402 | |
---|
2403 | /* A NULL name signifies the "(default)" value for a key */ |
---|
2404 | if(((name == NULL) && (cur->name == NULL)) |
---|
2405 | || ((name != NULL) && (cur->name != NULL) |
---|
2406 | && (strcasecmp(cur->name, name) == 0))) |
---|
2407 | { |
---|
2408 | found = true; |
---|
2409 | *index = i; |
---|
2410 | } |
---|
2411 | |
---|
2412 | regfi_free_record(file, cur); |
---|
2413 | } |
---|
2414 | |
---|
2415 | return found; |
---|
2416 | } |
---|
2417 | |
---|
2418 | |
---|
2419 | |
---|
2420 | /****************************************************************************** |
---|
2421 | *****************************************************************************/ |
---|
2422 | const REGFI_NK* regfi_get_subkey(REGFI_FILE* file, const REGFI_NK* key, |
---|
2423 | uint32_t index) |
---|
2424 | { |
---|
2425 | if(index < regfi_fetch_num_subkeys(key)) |
---|
2426 | { |
---|
2427 | return regfi_load_key(file, |
---|
2428 | key->subkeys->elements[index].offset+REGFI_REGF_SIZE, |
---|
2429 | true); |
---|
2430 | } |
---|
2431 | |
---|
2432 | return NULL; |
---|
2433 | } |
---|
2434 | |
---|
2435 | |
---|
2436 | /****************************************************************************** |
---|
2437 | *****************************************************************************/ |
---|
2438 | const REGFI_VK* regfi_get_value(REGFI_FILE* file, const REGFI_NK* key, |
---|
2439 | uint32_t index) |
---|
2440 | { |
---|
2441 | if(index < regfi_fetch_num_values(key)) |
---|
2442 | { |
---|
2443 | return regfi_load_value(file, |
---|
2444 | key->values->elements[index]+REGFI_REGF_SIZE, |
---|
2445 | true); |
---|
2446 | } |
---|
2447 | |
---|
2448 | return NULL; |
---|
2449 | } |
---|
2450 | |
---|
2451 | |
---|
2452 | |
---|
2453 | /****************************************************************************** |
---|
2454 | *****************************************************************************/ |
---|
2455 | const REGFI_NK* regfi_get_parentkey(REGFI_FILE* file, const REGFI_NK* key) |
---|
2456 | { |
---|
2457 | if(key != NULL && key->parent_off != REGFI_OFFSET_NONE) |
---|
2458 | return regfi_load_key(file, |
---|
2459 | key->parent_off+REGFI_REGF_SIZE, true); |
---|
2460 | |
---|
2461 | return NULL; |
---|
2462 | } |
---|
2463 | |
---|
2464 | |
---|
2465 | |
---|
2466 | /****************************************************************************** |
---|
2467 | *****************************************************************************/ |
---|
2468 | REGFI_DATA* regfi_buffer_to_data(REGFI_BUFFER raw_data) |
---|
2469 | { |
---|
2470 | REGFI_DATA* ret_val; |
---|
2471 | |
---|
2472 | if(raw_data.buf == NULL) |
---|
2473 | return NULL; |
---|
2474 | |
---|
2475 | ret_val = talloc(NULL, REGFI_DATA); |
---|
2476 | if(ret_val == NULL) |
---|
2477 | return NULL; |
---|
2478 | |
---|
2479 | talloc_reparent(NULL, ret_val, raw_data.buf); |
---|
2480 | ret_val->raw = raw_data.buf; |
---|
2481 | ret_val->size = raw_data.len; |
---|
2482 | ret_val->interpreted_size = 0; |
---|
2483 | ret_val->interpreted.qword = 0; |
---|
2484 | |
---|
2485 | return ret_val; |
---|
2486 | } |
---|
2487 | |
---|
2488 | |
---|
2489 | /****************************************************************************** |
---|
2490 | *****************************************************************************/ |
---|
2491 | bool regfi_interpret_data(REGFI_FILE* file, uint32_t type, REGFI_DATA* data) |
---|
2492 | { |
---|
2493 | REGFI_BUFFER tmp_buf; |
---|
2494 | uint8_t** tmp_array; |
---|
2495 | uint32_t i, j; |
---|
2496 | |
---|
2497 | if(data == NULL) |
---|
2498 | return false; |
---|
2499 | |
---|
2500 | switch (type) |
---|
2501 | { |
---|
2502 | case REG_SZ: |
---|
2503 | case REG_EXPAND_SZ: |
---|
2504 | /* REG_LINK is a symbolic link, stored as a unicode string. */ |
---|
2505 | case REG_LINK: |
---|
2506 | tmp_buf = regfi_conv_charset(regfi_encoding_int2str(REGFI_ENCODING_UTF16LE), |
---|
2507 | regfi_encoding_int2str(file->string_encoding), |
---|
2508 | data->raw, data->size); |
---|
2509 | if(tmp_buf.buf == NULL) |
---|
2510 | { |
---|
2511 | regfi_log_add(REGFI_LOG_INFO, "Error occurred while" |
---|
2512 | " converting data of type %d to string encoding %d." |
---|
2513 | " Error message: %s", |
---|
2514 | type, file->string_encoding, strerror(errno)); |
---|
2515 | data->interpreted.string = NULL; |
---|
2516 | data->interpreted_size = 0; |
---|
2517 | return false; |
---|
2518 | } |
---|
2519 | |
---|
2520 | data->interpreted.string = tmp_buf.buf; |
---|
2521 | data->interpreted_size = tmp_buf.len; |
---|
2522 | talloc_reparent(NULL, data, tmp_buf.buf); |
---|
2523 | break; |
---|
2524 | |
---|
2525 | case REG_DWORD: |
---|
2526 | if(data->size < 4) |
---|
2527 | { |
---|
2528 | data->interpreted.dword = 0; |
---|
2529 | data->interpreted_size = 0; |
---|
2530 | return false; |
---|
2531 | } |
---|
2532 | data->interpreted.dword = IVAL(data->raw, 0); |
---|
2533 | data->interpreted_size = 4; |
---|
2534 | break; |
---|
2535 | |
---|
2536 | case REG_DWORD_BE: |
---|
2537 | if(data->size < 4) |
---|
2538 | { |
---|
2539 | data->interpreted.dword_be = 0; |
---|
2540 | data->interpreted_size = 0; |
---|
2541 | return false; |
---|
2542 | } |
---|
2543 | data->interpreted.dword_be = RIVAL(data->raw, 0); |
---|
2544 | data->interpreted_size = 4; |
---|
2545 | break; |
---|
2546 | |
---|
2547 | case REG_QWORD: |
---|
2548 | if(data->size < 8) |
---|
2549 | { |
---|
2550 | data->interpreted.qword = 0; |
---|
2551 | data->interpreted_size = 0; |
---|
2552 | return false; |
---|
2553 | } |
---|
2554 | data->interpreted.qword = |
---|
2555 | (uint64_t)IVAL(data->raw, 0) + (((uint64_t)IVAL(data->raw, 4))<<32); |
---|
2556 | data->interpreted_size = 8; |
---|
2557 | break; |
---|
2558 | |
---|
2559 | case REG_MULTI_SZ: |
---|
2560 | /* Attempt to convert entire string from UTF-16LE to output encoding, |
---|
2561 | * then parse and quote fields individually. |
---|
2562 | */ |
---|
2563 | tmp_buf = regfi_conv_charset(regfi_encoding_int2str(REGFI_ENCODING_UTF16LE), |
---|
2564 | regfi_encoding_int2str(file->string_encoding), |
---|
2565 | data->raw, data->size); |
---|
2566 | if(tmp_buf.buf == NULL) |
---|
2567 | { |
---|
2568 | regfi_log_add(REGFI_LOG_INFO, "Error occurred while" |
---|
2569 | " converting data of type %d to string encoding %d." |
---|
2570 | " Error message: %s", |
---|
2571 | type, file->string_encoding, strerror(errno)); |
---|
2572 | data->interpreted.multiple_string = NULL; |
---|
2573 | data->interpreted_size = 0; |
---|
2574 | return false; |
---|
2575 | } |
---|
2576 | |
---|
2577 | tmp_array = talloc_array(NULL, uint8_t*, tmp_buf.len+1); |
---|
2578 | if(tmp_array == NULL) |
---|
2579 | { |
---|
2580 | talloc_free(tmp_buf.buf); |
---|
2581 | data->interpreted.string = NULL; |
---|
2582 | data->interpreted_size = 0; |
---|
2583 | return false; |
---|
2584 | } |
---|
2585 | |
---|
2586 | tmp_array[0] = tmp_buf.buf; |
---|
2587 | for(i=0,j=1; i < tmp_buf.len && j < tmp_buf.len; i++) |
---|
2588 | { |
---|
2589 | if(tmp_buf.buf[i] == '\0' && (i+1 < tmp_buf.len) |
---|
2590 | && tmp_buf.buf[i+1] != '\0') |
---|
2591 | tmp_array[j++] = tmp_buf.buf+i+1; |
---|
2592 | } |
---|
2593 | tmp_array[j] = NULL; |
---|
2594 | tmp_array = talloc_realloc(NULL, tmp_array, uint8_t*, j+1); |
---|
2595 | data->interpreted.multiple_string = tmp_array; |
---|
2596 | /* XXX: how meaningful is this? should we store number of strings instead? */ |
---|
2597 | data->interpreted_size = tmp_buf.len; |
---|
2598 | talloc_reparent(NULL, tmp_array, tmp_buf.buf); |
---|
2599 | talloc_reparent(NULL, data, tmp_array); |
---|
2600 | break; |
---|
2601 | |
---|
2602 | /* XXX: Dont know how to interpret these yet, just treat as binary */ |
---|
2603 | case REG_NONE: |
---|
2604 | data->interpreted.none = data->raw; |
---|
2605 | data->interpreted_size = data->size; |
---|
2606 | break; |
---|
2607 | |
---|
2608 | case REG_RESOURCE_LIST: |
---|
2609 | data->interpreted.resource_list = data->raw; |
---|
2610 | data->interpreted_size = data->size; |
---|
2611 | break; |
---|
2612 | |
---|
2613 | case REG_FULL_RESOURCE_DESCRIPTOR: |
---|
2614 | data->interpreted.full_resource_descriptor = data->raw; |
---|
2615 | data->interpreted_size = data->size; |
---|
2616 | break; |
---|
2617 | |
---|
2618 | case REG_RESOURCE_REQUIREMENTS_LIST: |
---|
2619 | data->interpreted.resource_requirements_list = data->raw; |
---|
2620 | data->interpreted_size = data->size; |
---|
2621 | break; |
---|
2622 | |
---|
2623 | case REG_BINARY: |
---|
2624 | data->interpreted.binary = data->raw; |
---|
2625 | data->interpreted_size = data->size; |
---|
2626 | break; |
---|
2627 | |
---|
2628 | default: |
---|
2629 | data->interpreted.qword = 0; |
---|
2630 | data->interpreted_size = 0; |
---|
2631 | return false; |
---|
2632 | } |
---|
2633 | |
---|
2634 | data->type = type; |
---|
2635 | return true; |
---|
2636 | } |
---|
2637 | |
---|
2638 | |
---|
2639 | /****************************************************************************** |
---|
2640 | * Convert string from input_charset to output_charset. |
---|
2641 | * On error, returns a NULL buf attribute and sets the errno. |
---|
2642 | *****************************************************************************/ |
---|
2643 | REGFI_BUFFER regfi_conv_charset(const char* input_charset, const char* output_charset, |
---|
2644 | uint8_t* input, uint32_t input_len) |
---|
2645 | { |
---|
2646 | iconv_t conv_desc; |
---|
2647 | char* inbuf = (char*)input; |
---|
2648 | char* outbuf; |
---|
2649 | char* retbuf; |
---|
2650 | size_t allocated = (size_t)input_len; |
---|
2651 | size_t in_left = (size_t)input_len; |
---|
2652 | size_t out_left = (size_t)allocated-1; |
---|
2653 | REGFI_BUFFER ret_val; |
---|
2654 | int ret; |
---|
2655 | |
---|
2656 | ret_val.buf = NULL; |
---|
2657 | ret_val.len = 0; |
---|
2658 | retbuf = talloc_array(NULL, char, allocated); |
---|
2659 | outbuf = retbuf; |
---|
2660 | if(outbuf == NULL) |
---|
2661 | { |
---|
2662 | errno = ENOMEM; |
---|
2663 | return ret_val; |
---|
2664 | } |
---|
2665 | |
---|
2666 | /* Set up conversion descriptor. */ |
---|
2667 | /* XXX: Consider creating a couple of conversion descriptors earlier, |
---|
2668 | * storing them on an iterator so they don't have to be recreated |
---|
2669 | * each time. |
---|
2670 | */ |
---|
2671 | conv_desc = iconv_open(output_charset, input_charset); |
---|
2672 | |
---|
2673 | ret = 0; |
---|
2674 | do |
---|
2675 | { |
---|
2676 | if(ret == -1) |
---|
2677 | { |
---|
2678 | retbuf = talloc_realloc(NULL, retbuf, char, allocated+(in_left*2)); |
---|
2679 | if(retbuf == NULL) |
---|
2680 | { |
---|
2681 | errno = ENOMEM; |
---|
2682 | return ret_val; |
---|
2683 | } |
---|
2684 | outbuf = retbuf+(allocated-1-out_left); |
---|
2685 | out_left += in_left*2; |
---|
2686 | allocated += in_left*2; |
---|
2687 | } |
---|
2688 | ret = iconv(conv_desc, &inbuf, &in_left, &outbuf, &out_left); |
---|
2689 | |
---|
2690 | } while(ret == -1 && errno == E2BIG); |
---|
2691 | |
---|
2692 | if(ret == -1) |
---|
2693 | { |
---|
2694 | iconv_close(conv_desc); |
---|
2695 | return ret_val; |
---|
2696 | } |
---|
2697 | |
---|
2698 | /* Save memory */ |
---|
2699 | if(out_left > 0) |
---|
2700 | { |
---|
2701 | retbuf = talloc_realloc(NULL, retbuf, char, allocated-out_left); |
---|
2702 | if(retbuf == NULL) |
---|
2703 | { |
---|
2704 | errno = ENOMEM; |
---|
2705 | return ret_val; |
---|
2706 | } |
---|
2707 | allocated -= out_left; |
---|
2708 | } |
---|
2709 | retbuf[allocated-1] = '\0'; |
---|
2710 | iconv_close(conv_desc); |
---|
2711 | |
---|
2712 | ret_val.buf = (uint8_t*)retbuf; |
---|
2713 | ret_val.len = allocated-1; |
---|
2714 | return ret_val; |
---|
2715 | } |
---|
2716 | |
---|
2717 | |
---|
2718 | /******************************************************************* |
---|
2719 | * Computes the checksum of the registry file header. |
---|
2720 | * buffer must be at least the size of a regf header (4096 bytes). |
---|
2721 | *******************************************************************/ |
---|
2722 | static uint32_t regfi_compute_header_checksum(uint8_t* buffer) |
---|
2723 | { |
---|
2724 | uint32_t checksum, x; |
---|
2725 | int i; |
---|
2726 | |
---|
2727 | /* XOR of all bytes 0x0000 - 0x01FB */ |
---|
2728 | |
---|
2729 | checksum = x = 0; |
---|
2730 | |
---|
2731 | for ( i=0; i<0x01FB; i+=4 ) { |
---|
2732 | x = IVAL(buffer, i ); |
---|
2733 | checksum ^= x; |
---|
2734 | } |
---|
2735 | |
---|
2736 | return checksum; |
---|
2737 | } |
---|
2738 | |
---|
2739 | |
---|
2740 | /******************************************************************* |
---|
2741 | *******************************************************************/ |
---|
2742 | REGFI_FILE* regfi_parse_regf(REGFI_RAW_FILE* file_cb, bool strict) |
---|
2743 | { |
---|
2744 | uint8_t file_header[REGFI_REGF_SIZE]; |
---|
2745 | uint32_t length; |
---|
2746 | REGFI_FILE* ret_val; |
---|
2747 | |
---|
2748 | ret_val = talloc(NULL, REGFI_FILE); |
---|
2749 | if(ret_val == NULL) |
---|
2750 | return NULL; |
---|
2751 | |
---|
2752 | ret_val->sk_cache = NULL; |
---|
2753 | ret_val->hbins = NULL; |
---|
2754 | |
---|
2755 | length = REGFI_REGF_SIZE; |
---|
2756 | if((regfi_read(file_cb, file_header, &length)) != 0 |
---|
2757 | || length != REGFI_REGF_SIZE) |
---|
2758 | { |
---|
2759 | regfi_log_add(REGFI_LOG_WARN, "Read failed while parsing REGF structure."); |
---|
2760 | goto fail; |
---|
2761 | } |
---|
2762 | |
---|
2763 | ret_val->checksum = IVAL(file_header, 0x1FC); |
---|
2764 | ret_val->computed_checksum = regfi_compute_header_checksum(file_header); |
---|
2765 | if (strict && (ret_val->checksum != ret_val->computed_checksum)) |
---|
2766 | { |
---|
2767 | regfi_log_add(REGFI_LOG_WARN, "Stored header checksum (%.8X) did not equal" |
---|
2768 | " computed checksum (%.8X).", |
---|
2769 | ret_val->checksum, ret_val->computed_checksum); |
---|
2770 | if(strict) |
---|
2771 | goto fail; |
---|
2772 | } |
---|
2773 | |
---|
2774 | memcpy(ret_val->magic, file_header, REGFI_REGF_MAGIC_SIZE); |
---|
2775 | if(memcmp(ret_val->magic, "regf", REGFI_REGF_MAGIC_SIZE) != 0) |
---|
2776 | { |
---|
2777 | regfi_log_add(REGFI_LOG_ERROR, "Magic number mismatch " |
---|
2778 | "(%.2X %.2X %.2X %.2X) while parsing hive header", |
---|
2779 | ret_val->magic[0], ret_val->magic[1], |
---|
2780 | ret_val->magic[2], ret_val->magic[3]); |
---|
2781 | goto fail; |
---|
2782 | } |
---|
2783 | |
---|
2784 | ret_val->sequence1 = IVAL(file_header, 0x4); |
---|
2785 | ret_val->sequence2 = IVAL(file_header, 0x8); |
---|
2786 | ret_val->mtime = ((uint64_t)IVAL(file_header, 0x10)) << 32; |
---|
2787 | ret_val->mtime |= IVAL(file_header, 0xC); |
---|
2788 | ret_val->major_version = IVAL(file_header, 0x14); |
---|
2789 | ret_val->minor_version = IVAL(file_header, 0x18); |
---|
2790 | ret_val->type = IVAL(file_header, 0x1C); |
---|
2791 | ret_val->format = IVAL(file_header, 0x20); |
---|
2792 | ret_val->root_cell = IVAL(file_header, 0x24); |
---|
2793 | ret_val->last_block = IVAL(file_header, 0x28); |
---|
2794 | ret_val->cluster = IVAL(file_header, 0x2C); |
---|
2795 | |
---|
2796 | memcpy(ret_val->file_name, file_header+0x30, REGFI_REGF_NAME_SIZE); |
---|
2797 | |
---|
2798 | ret_val->rm_id = winsec_parse_uuid(ret_val, file_header+0x70, 16); |
---|
2799 | if(ret_val->rm_id == NULL) |
---|
2800 | regfi_log_add(REGFI_LOG_WARN, "Hive header's rm_id failed to parse."); |
---|
2801 | |
---|
2802 | ret_val->log_id = winsec_parse_uuid(ret_val, file_header+0x80, 16); |
---|
2803 | if(ret_val->log_id == NULL) |
---|
2804 | regfi_log_add(REGFI_LOG_WARN, "Hive header's log_id failed to parse."); |
---|
2805 | |
---|
2806 | ret_val->flags = IVAL(file_header, 0x90); |
---|
2807 | |
---|
2808 | ret_val->tm_id = winsec_parse_uuid(ret_val, file_header+0x94, 16); |
---|
2809 | if(ret_val->tm_id == NULL) |
---|
2810 | regfi_log_add(REGFI_LOG_WARN, "Hive header's tm_id failed to parse."); |
---|
2811 | |
---|
2812 | ret_val->guid_signature = IVAL(file_header, 0xa4); |
---|
2813 | |
---|
2814 | memcpy(ret_val->reserved1, file_header+0xa8, REGFI_REGF_RESERVED1_SIZE); |
---|
2815 | memcpy(ret_val->reserved2, file_header+0x200, REGFI_REGF_RESERVED2_SIZE); |
---|
2816 | |
---|
2817 | ret_val->thaw_tm_id = winsec_parse_uuid(ret_val, file_header+0xFC8, 16); |
---|
2818 | ret_val->thaw_rm_id = winsec_parse_uuid(ret_val, file_header+0xFD8, 16); |
---|
2819 | ret_val->thaw_log_id = winsec_parse_uuid(ret_val, file_header+0xFE8, 16); |
---|
2820 | ret_val->boot_type = IVAL(file_header, 0xFF8); |
---|
2821 | ret_val->boot_recover = IVAL(file_header, 0xFFC); |
---|
2822 | |
---|
2823 | return ret_val; |
---|
2824 | |
---|
2825 | fail: |
---|
2826 | talloc_free(ret_val); |
---|
2827 | return NULL; |
---|
2828 | } |
---|
2829 | |
---|
2830 | |
---|
2831 | |
---|
2832 | /****************************************************************************** |
---|
2833 | * Given real file offset, read and parse the hbin at that location |
---|
2834 | * along with it's associated cells. |
---|
2835 | ******************************************************************************/ |
---|
2836 | REGFI_HBIN* regfi_parse_hbin(REGFI_FILE* file, uint32_t offset, bool strict) |
---|
2837 | { |
---|
2838 | REGFI_HBIN* hbin = NULL; |
---|
2839 | uint8_t hbin_header[REGFI_HBIN_HEADER_SIZE]; |
---|
2840 | uint32_t length; |
---|
2841 | |
---|
2842 | if(offset >= file->file_length) |
---|
2843 | goto fail; |
---|
2844 | |
---|
2845 | if(!regfi_lock(file, &file->cb_lock, "regfi_parse_hbin")) |
---|
2846 | goto fail; |
---|
2847 | |
---|
2848 | if(regfi_seek(file->cb, offset, SEEK_SET) == -1) |
---|
2849 | { |
---|
2850 | regfi_log_add(REGFI_LOG_ERROR, "Seek failed" |
---|
2851 | " while parsing hbin at offset 0x%.8X.", offset); |
---|
2852 | goto fail_locked; |
---|
2853 | } |
---|
2854 | |
---|
2855 | length = REGFI_HBIN_HEADER_SIZE; |
---|
2856 | if((regfi_read(file->cb, hbin_header, &length) != 0) |
---|
2857 | || length != REGFI_HBIN_HEADER_SIZE) |
---|
2858 | { |
---|
2859 | regfi_log_add(REGFI_LOG_ERROR, "Read failed" |
---|
2860 | " while parsing hbin at offset 0x%.8X.", offset); |
---|
2861 | goto fail_locked; |
---|
2862 | } |
---|
2863 | |
---|
2864 | if(!regfi_unlock(file, &file->cb_lock, "regfi_parse_hbin")) |
---|
2865 | goto fail; |
---|
2866 | |
---|
2867 | hbin = talloc(NULL, REGFI_HBIN); |
---|
2868 | if(hbin == NULL) |
---|
2869 | goto fail; |
---|
2870 | hbin->file_off = offset; |
---|
2871 | |
---|
2872 | memcpy(hbin->magic, hbin_header, 4); |
---|
2873 | if(strict && (memcmp(hbin->magic, "hbin", 4) != 0)) |
---|
2874 | { |
---|
2875 | /* This always seems to happen at the end of a file, so we make it an INFO |
---|
2876 | * message, rather than something more serious. |
---|
2877 | */ |
---|
2878 | regfi_log_add(REGFI_LOG_INFO, "Magic number mismatch " |
---|
2879 | "(%.2X %.2X %.2X %.2X) while parsing hbin at offset" |
---|
2880 | " 0x%.8X.", hbin->magic[0], hbin->magic[1], |
---|
2881 | hbin->magic[2], hbin->magic[3], offset); |
---|
2882 | goto fail; |
---|
2883 | } |
---|
2884 | |
---|
2885 | hbin->first_hbin_off = IVAL(hbin_header, 0x4); |
---|
2886 | hbin->block_size = IVAL(hbin_header, 0x8); |
---|
2887 | /* this should be the same thing as hbin->block_size, but just in case */ |
---|
2888 | hbin->next_block = IVAL(hbin_header, 0x1C); |
---|
2889 | |
---|
2890 | |
---|
2891 | /* Ensure the block size is a multiple of 0x1000 and doesn't run off |
---|
2892 | * the end of the file. |
---|
2893 | */ |
---|
2894 | /* XXX: This may need to be relaxed for dealing with |
---|
2895 | * partial or corrupt files. |
---|
2896 | */ |
---|
2897 | if((offset + hbin->block_size > file->file_length) |
---|
2898 | || (hbin->block_size & 0xFFFFF000) != hbin->block_size) |
---|
2899 | { |
---|
2900 | regfi_log_add(REGFI_LOG_ERROR, "The hbin offset is not aligned" |
---|
2901 | " or runs off the end of the file" |
---|
2902 | " while parsing hbin at offset 0x%.8X.", offset); |
---|
2903 | goto fail; |
---|
2904 | } |
---|
2905 | |
---|
2906 | return hbin; |
---|
2907 | |
---|
2908 | fail_locked: |
---|
2909 | regfi_unlock(file, &file->cb_lock, "regfi_parse_hbin"); |
---|
2910 | fail: |
---|
2911 | talloc_free(hbin); |
---|
2912 | return NULL; |
---|
2913 | } |
---|
2914 | |
---|
2915 | |
---|
2916 | /******************************************************************* |
---|
2917 | *******************************************************************/ |
---|
2918 | REGFI_NK* regfi_parse_nk(REGFI_FILE* file, uint32_t offset, |
---|
2919 | uint32_t max_size, bool strict) |
---|
2920 | { |
---|
2921 | uint8_t nk_header[REGFI_NK_MIN_LENGTH]; |
---|
2922 | REGFI_NK* ret_val; |
---|
2923 | uint32_t length,cell_length; |
---|
2924 | bool unalloc = false; |
---|
2925 | |
---|
2926 | ret_val = talloc(NULL, REGFI_NK); |
---|
2927 | if(ret_val == NULL) |
---|
2928 | { |
---|
2929 | regfi_log_add(REGFI_LOG_ERROR, "Failed to allocate memory while" |
---|
2930 | " parsing NK record at offset 0x%.8X.", offset); |
---|
2931 | goto fail; |
---|
2932 | } |
---|
2933 | |
---|
2934 | if(!regfi_lock(file, &file->cb_lock, "regfi_parse_nk")) |
---|
2935 | goto fail; |
---|
2936 | |
---|
2937 | if(!regfi_parse_cell(file->cb, offset, nk_header, REGFI_NK_MIN_LENGTH, |
---|
2938 | &cell_length, &unalloc)) |
---|
2939 | { |
---|
2940 | regfi_log_add(REGFI_LOG_WARN, "Could not parse cell header" |
---|
2941 | " while parsing NK record at offset 0x%.8X.", offset); |
---|
2942 | goto fail_locked; |
---|
2943 | } |
---|
2944 | |
---|
2945 | if((nk_header[0x0] != 'n') || (nk_header[0x1] != 'k')) |
---|
2946 | { |
---|
2947 | regfi_log_add(REGFI_LOG_WARN, "Magic number mismatch in parsing" |
---|
2948 | " NK record at offset 0x%.8X.", offset); |
---|
2949 | goto fail_locked; |
---|
2950 | } |
---|
2951 | |
---|
2952 | ret_val->values = NULL; |
---|
2953 | ret_val->subkeys = NULL; |
---|
2954 | ret_val->offset = offset; |
---|
2955 | ret_val->cell_size = cell_length; |
---|
2956 | |
---|
2957 | if(ret_val->cell_size > max_size) |
---|
2958 | ret_val->cell_size = max_size & 0xFFFFFFF8; |
---|
2959 | if((ret_val->cell_size < REGFI_NK_MIN_LENGTH) |
---|
2960 | || (strict && (ret_val->cell_size & 0x00000007) != 0)) |
---|
2961 | { |
---|
2962 | regfi_log_add(REGFI_LOG_WARN, "A length check failed while" |
---|
2963 | " parsing NK record at offset 0x%.8X.", offset); |
---|
2964 | goto fail_locked; |
---|
2965 | } |
---|
2966 | |
---|
2967 | ret_val->magic[0] = nk_header[0x0]; |
---|
2968 | ret_val->magic[1] = nk_header[0x1]; |
---|
2969 | ret_val->flags = SVAL(nk_header, 0x2); |
---|
2970 | |
---|
2971 | if((ret_val->flags & ~REGFI_NK_KNOWN_FLAGS) != 0) |
---|
2972 | { |
---|
2973 | regfi_log_add(REGFI_LOG_WARN, "Unknown key flags (0x%.4X) while" |
---|
2974 | " parsing NK record at offset 0x%.8X.", |
---|
2975 | (ret_val->flags & ~REGFI_NK_KNOWN_FLAGS), offset); |
---|
2976 | } |
---|
2977 | |
---|
2978 | ret_val->mtime = ((uint64_t)IVAL(nk_header, 0x8)) << 32; |
---|
2979 | ret_val->mtime |= IVAL(nk_header, 0x4); |
---|
2980 | /* If the key is unallocated and the MTIME is earlier than Jan 1, 1990 |
---|
2981 | * or later than Jan 1, 2290, we consider this a bad key. This helps |
---|
2982 | * weed out some false positives during deleted data recovery. |
---|
2983 | */ |
---|
2984 | if(unalloc |
---|
2985 | && (ret_val->mtime < REGFI_MTIME_MIN |
---|
2986 | || ret_val->mtime > REGFI_MTIME_MAX)) |
---|
2987 | { goto fail_locked; } |
---|
2988 | |
---|
2989 | ret_val->unknown1 = IVAL(nk_header, 0xC); |
---|
2990 | ret_val->parent_off = IVAL(nk_header, 0x10); |
---|
2991 | ret_val->num_subkeys = IVAL(nk_header, 0x14); |
---|
2992 | ret_val->unknown2 = IVAL(nk_header, 0x18); |
---|
2993 | ret_val->subkeys_off = IVAL(nk_header, 0x1C); |
---|
2994 | ret_val->unknown3 = IVAL(nk_header, 0x20); |
---|
2995 | ret_val->num_values = IVAL(nk_header, 0x24); |
---|
2996 | ret_val->values_off = IVAL(nk_header, 0x28); |
---|
2997 | ret_val->sk_off = IVAL(nk_header, 0x2C); |
---|
2998 | ret_val->classname_off = IVAL(nk_header, 0x30); |
---|
2999 | |
---|
3000 | ret_val->max_bytes_subkeyname = IVAL(nk_header, 0x34); |
---|
3001 | ret_val->max_bytes_subkeyclassname = IVAL(nk_header, 0x38); |
---|
3002 | ret_val->max_bytes_valuename = IVAL(nk_header, 0x3C); |
---|
3003 | ret_val->max_bytes_value = IVAL(nk_header, 0x40); |
---|
3004 | ret_val->unk_index = IVAL(nk_header, 0x44); |
---|
3005 | |
---|
3006 | ret_val->name_length = SVAL(nk_header, 0x48); |
---|
3007 | ret_val->classname_length = SVAL(nk_header, 0x4A); |
---|
3008 | ret_val->name = NULL; |
---|
3009 | |
---|
3010 | if(ret_val->name_length + REGFI_NK_MIN_LENGTH > ret_val->cell_size) |
---|
3011 | { |
---|
3012 | if(strict) |
---|
3013 | { |
---|
3014 | regfi_log_add(REGFI_LOG_ERROR, "Contents too large for cell" |
---|
3015 | " while parsing NK record at offset 0x%.8X.", offset); |
---|
3016 | goto fail_locked; |
---|
3017 | } |
---|
3018 | else |
---|
3019 | ret_val->name_length = ret_val->cell_size - REGFI_NK_MIN_LENGTH; |
---|
3020 | } |
---|
3021 | else if (unalloc) |
---|
3022 | { /* Truncate cell_size if it's much larger than the apparent total record length. */ |
---|
3023 | /* Round up to the next multiple of 8 */ |
---|
3024 | length = (ret_val->name_length + REGFI_NK_MIN_LENGTH) & 0xFFFFFFF8; |
---|
3025 | if(length < ret_val->name_length + REGFI_NK_MIN_LENGTH) |
---|
3026 | length+=8; |
---|
3027 | |
---|
3028 | /* If cell_size is still greater, truncate. */ |
---|
3029 | if(length < ret_val->cell_size) |
---|
3030 | ret_val->cell_size = length; |
---|
3031 | } |
---|
3032 | |
---|
3033 | /* +1 to length in case we decided to use this directly as a string later */ |
---|
3034 | ret_val->name_raw = talloc_array(ret_val, uint8_t, ret_val->name_length+1); |
---|
3035 | if(ret_val->name_raw == NULL) |
---|
3036 | goto fail_locked; |
---|
3037 | |
---|
3038 | /* Don't need to seek, should be at the right offset */ |
---|
3039 | length = ret_val->name_length; |
---|
3040 | if((regfi_read(file->cb, (uint8_t*)ret_val->name_raw, &length) != 0) |
---|
3041 | || length != ret_val->name_length) |
---|
3042 | { |
---|
3043 | regfi_log_add(REGFI_LOG_ERROR, "Failed to read key name" |
---|
3044 | " while parsing NK record at offset 0x%.8X.", offset); |
---|
3045 | goto fail_locked; |
---|
3046 | } |
---|
3047 | |
---|
3048 | if(!regfi_unlock(file, &file->cb_lock, "regfi_parse_nk")) |
---|
3049 | goto fail; |
---|
3050 | |
---|
3051 | return ret_val; |
---|
3052 | |
---|
3053 | fail_locked: |
---|
3054 | regfi_unlock(file, &file->cb_lock, "regfi_parse_nk"); |
---|
3055 | fail: |
---|
3056 | talloc_free(ret_val); |
---|
3057 | return NULL; |
---|
3058 | } |
---|
3059 | |
---|
3060 | |
---|
3061 | uint8_t* regfi_parse_classname(REGFI_FILE* file, uint32_t offset, |
---|
3062 | uint16_t* name_length, uint32_t max_size, bool strict) |
---|
3063 | { |
---|
3064 | uint8_t* ret_val = NULL; |
---|
3065 | uint32_t length; |
---|
3066 | uint32_t cell_length; |
---|
3067 | bool unalloc = false; |
---|
3068 | |
---|
3069 | if(*name_length <= 0 || offset == REGFI_OFFSET_NONE |
---|
3070 | || (offset & 0x00000007) != 0) |
---|
3071 | { goto fail; } |
---|
3072 | |
---|
3073 | if(!regfi_lock(file, &file->cb_lock, "regfi_parse_classname")) |
---|
3074 | goto fail; |
---|
3075 | |
---|
3076 | if(!regfi_parse_cell(file->cb, offset, NULL, 0, &cell_length, &unalloc)) |
---|
3077 | { |
---|
3078 | regfi_log_add(REGFI_LOG_WARN, "Could not parse cell header" |
---|
3079 | " while parsing class name at offset 0x%.8X.", offset); |
---|
3080 | goto fail_locked; |
---|
3081 | } |
---|
3082 | |
---|
3083 | if((cell_length & 0x0000007) != 0) |
---|
3084 | { |
---|
3085 | regfi_log_add(REGFI_LOG_ERROR, "Cell length not a multiple of 8" |
---|
3086 | " while parsing class name at offset 0x%.8X.", offset); |
---|
3087 | goto fail_locked; |
---|
3088 | } |
---|
3089 | |
---|
3090 | if(cell_length > max_size) |
---|
3091 | { |
---|
3092 | regfi_log_add(REGFI_LOG_WARN, "Cell stretches past hbin " |
---|
3093 | "boundary while parsing class name at offset 0x%.8X.", |
---|
3094 | offset); |
---|
3095 | if(strict) |
---|
3096 | goto fail_locked; |
---|
3097 | cell_length = max_size; |
---|
3098 | } |
---|
3099 | |
---|
3100 | if((cell_length - 4) < *name_length) |
---|
3101 | { |
---|
3102 | regfi_log_add(REGFI_LOG_WARN, "Class name is larger than" |
---|
3103 | " cell_length while parsing class name at offset" |
---|
3104 | " 0x%.8X.", offset); |
---|
3105 | if(strict) |
---|
3106 | goto fail_locked; |
---|
3107 | *name_length = cell_length - 4; |
---|
3108 | } |
---|
3109 | |
---|
3110 | ret_val = talloc_array(NULL, uint8_t, *name_length); |
---|
3111 | if(ret_val != NULL) |
---|
3112 | { |
---|
3113 | length = *name_length; |
---|
3114 | if((regfi_read(file->cb, ret_val, &length) != 0) |
---|
3115 | || length != *name_length) |
---|
3116 | { |
---|
3117 | regfi_log_add(REGFI_LOG_ERROR, "Could not read class name" |
---|
3118 | " while parsing class name at offset 0x%.8X.", offset); |
---|
3119 | goto fail_locked; |
---|
3120 | } |
---|
3121 | } |
---|
3122 | |
---|
3123 | if(!regfi_unlock(file, &file->cb_lock, "regfi_parse_classname")) |
---|
3124 | goto fail; |
---|
3125 | |
---|
3126 | return ret_val; |
---|
3127 | |
---|
3128 | fail_locked: |
---|
3129 | regfi_unlock(file, &file->cb_lock, "regfi_parse_classname"); |
---|
3130 | fail: |
---|
3131 | talloc_free(ret_val); |
---|
3132 | return NULL; |
---|
3133 | } |
---|
3134 | |
---|
3135 | |
---|
3136 | /****************************************************************************** |
---|
3137 | *******************************************************************************/ |
---|
3138 | REGFI_VK* regfi_parse_vk(REGFI_FILE* file, uint32_t offset, |
---|
3139 | uint32_t max_size, bool strict) |
---|
3140 | { |
---|
3141 | REGFI_VK* ret_val; |
---|
3142 | uint8_t vk_header[REGFI_VK_MIN_LENGTH]; |
---|
3143 | uint32_t raw_data_size, length, cell_length; |
---|
3144 | bool unalloc = false; |
---|
3145 | |
---|
3146 | ret_val = talloc(NULL, REGFI_VK); |
---|
3147 | if(ret_val == NULL) |
---|
3148 | goto fail; |
---|
3149 | |
---|
3150 | if(!regfi_lock(file, &file->cb_lock, "regfi_parse_nk")) |
---|
3151 | goto fail; |
---|
3152 | |
---|
3153 | if(!regfi_parse_cell(file->cb, offset, vk_header, REGFI_VK_MIN_LENGTH, |
---|
3154 | &cell_length, &unalloc)) |
---|
3155 | { |
---|
3156 | regfi_log_add(REGFI_LOG_WARN, "Could not parse cell header" |
---|
3157 | " while parsing VK record at offset 0x%.8X.", offset); |
---|
3158 | goto fail_locked; |
---|
3159 | } |
---|
3160 | |
---|
3161 | ret_val->offset = offset; |
---|
3162 | ret_val->cell_size = cell_length; |
---|
3163 | ret_val->name = NULL; |
---|
3164 | ret_val->name_raw = NULL; |
---|
3165 | |
---|
3166 | if(ret_val->cell_size > max_size) |
---|
3167 | ret_val->cell_size = max_size & 0xFFFFFFF8; |
---|
3168 | if((ret_val->cell_size < REGFI_VK_MIN_LENGTH) |
---|
3169 | || (ret_val->cell_size & 0x00000007) != 0) |
---|
3170 | { |
---|
3171 | regfi_log_add(REGFI_LOG_WARN, "Invalid cell size encountered" |
---|
3172 | " while parsing VK record at offset 0x%.8X.", offset); |
---|
3173 | goto fail_locked; |
---|
3174 | } |
---|
3175 | |
---|
3176 | ret_val->magic[0] = vk_header[0x0]; |
---|
3177 | ret_val->magic[1] = vk_header[0x1]; |
---|
3178 | if((ret_val->magic[0] != 'v') || (ret_val->magic[1] != 'k')) |
---|
3179 | { |
---|
3180 | /* XXX: This does not account for deleted keys under Win2K which |
---|
3181 | * often have this (and the name length) overwritten with |
---|
3182 | * 0xFFFF. |
---|
3183 | */ |
---|
3184 | regfi_log_add(REGFI_LOG_WARN, "Magic number mismatch" |
---|
3185 | " while parsing VK record at offset 0x%.8X.", offset); |
---|
3186 | goto fail_locked; |
---|
3187 | } |
---|
3188 | |
---|
3189 | ret_val->name_length = SVAL(vk_header, 0x2); |
---|
3190 | raw_data_size = IVAL(vk_header, 0x4); |
---|
3191 | ret_val->data_size = raw_data_size & ~REGFI_VK_DATA_IN_OFFSET; |
---|
3192 | /* The data is typically stored in the offset if the size <= 4, |
---|
3193 | * in which case this flag is set. |
---|
3194 | */ |
---|
3195 | ret_val->data_in_offset = (bool)(raw_data_size & REGFI_VK_DATA_IN_OFFSET); |
---|
3196 | ret_val->data_off = IVAL(vk_header, 0x8); |
---|
3197 | ret_val->type = IVAL(vk_header, 0xC); |
---|
3198 | ret_val->flags = SVAL(vk_header, 0x10); |
---|
3199 | ret_val->unknown1 = SVAL(vk_header, 0x12); |
---|
3200 | |
---|
3201 | if(ret_val->name_length > 0) |
---|
3202 | { |
---|
3203 | if(ret_val->name_length + REGFI_VK_MIN_LENGTH + 4 > ret_val->cell_size) |
---|
3204 | { |
---|
3205 | regfi_log_add(REGFI_LOG_WARN, "Name too long for remaining cell" |
---|
3206 | " space while parsing VK record at offset 0x%.8X.", |
---|
3207 | offset); |
---|
3208 | if(strict) |
---|
3209 | goto fail_locked; |
---|
3210 | else |
---|
3211 | ret_val->name_length = ret_val->cell_size - REGFI_VK_MIN_LENGTH - 4; |
---|
3212 | } |
---|
3213 | |
---|
3214 | /* Round up to the next multiple of 8 */ |
---|
3215 | cell_length = (ret_val->name_length + REGFI_VK_MIN_LENGTH + 4) & 0xFFFFFFF8; |
---|
3216 | if(cell_length < ret_val->name_length + REGFI_VK_MIN_LENGTH + 4) |
---|
3217 | cell_length+=8; |
---|
3218 | |
---|
3219 | /* +1 to length in case we decided to use this directly as a string later */ |
---|
3220 | ret_val->name_raw = talloc_array(ret_val, uint8_t, ret_val->name_length+1); |
---|
3221 | if(ret_val->name_raw == NULL) |
---|
3222 | goto fail_locked; |
---|
3223 | |
---|
3224 | length = ret_val->name_length; |
---|
3225 | if((regfi_read(file->cb, (uint8_t*)ret_val->name_raw, &length) != 0) |
---|
3226 | || length != ret_val->name_length) |
---|
3227 | { |
---|
3228 | regfi_log_add(REGFI_LOG_ERROR, "Could not read value name" |
---|
3229 | " while parsing VK record at offset 0x%.8X.", offset); |
---|
3230 | goto fail_locked; |
---|
3231 | } |
---|
3232 | } |
---|
3233 | else |
---|
3234 | cell_length = REGFI_VK_MIN_LENGTH + 4; |
---|
3235 | |
---|
3236 | if(!regfi_unlock(file, &file->cb_lock, "regfi_parse_nk")) |
---|
3237 | goto fail; |
---|
3238 | |
---|
3239 | if(unalloc) |
---|
3240 | { |
---|
3241 | /* If cell_size is still greater, truncate. */ |
---|
3242 | if(cell_length < ret_val->cell_size) |
---|
3243 | ret_val->cell_size = cell_length; |
---|
3244 | } |
---|
3245 | |
---|
3246 | return ret_val; |
---|
3247 | |
---|
3248 | fail_locked: |
---|
3249 | regfi_unlock(file, &file->cb_lock, "regfi_parse_vk"); |
---|
3250 | fail: |
---|
3251 | talloc_free(ret_val); |
---|
3252 | return NULL; |
---|
3253 | } |
---|
3254 | |
---|
3255 | |
---|
3256 | /****************************************************************************** |
---|
3257 | * |
---|
3258 | ******************************************************************************/ |
---|
3259 | REGFI_BUFFER regfi_load_data(REGFI_FILE* file, uint32_t voffset, |
---|
3260 | uint32_t length, bool data_in_offset, |
---|
3261 | bool strict) |
---|
3262 | { |
---|
3263 | REGFI_BUFFER ret_val; |
---|
3264 | uint32_t cell_length, offset; |
---|
3265 | int32_t max_size; |
---|
3266 | bool unalloc; |
---|
3267 | |
---|
3268 | /* Microsoft's documentation indicates that "available memory" is |
---|
3269 | * the limit on value sizes for the more recent registry format version. |
---|
3270 | * This is not only annoying, but it's probably also incorrect, since clearly |
---|
3271 | * value data sizes are limited to 2^31 (high bit used as a flag) and even |
---|
3272 | * with big data records, the apparent max size is: |
---|
3273 | * 16344 * 2^16 = 1071104040 (~1GB). |
---|
3274 | * |
---|
3275 | * We choose to limit it to 1M which was the limit in older versions and |
---|
3276 | * should rarely be exceeded unless the file is corrupt or malicious. |
---|
3277 | * For more info, see: |
---|
3278 | * http://msdn.microsoft.com/en-us/library/ms724872%28VS.85%29.aspx |
---|
3279 | */ |
---|
3280 | /* XXX: add way to skip this check at user discression. */ |
---|
3281 | if(length > REGFI_VK_MAX_DATA_LENGTH) |
---|
3282 | { |
---|
3283 | regfi_log_add(REGFI_LOG_WARN, "Value data size %d larger than " |
---|
3284 | "%d, truncating...", length, REGFI_VK_MAX_DATA_LENGTH); |
---|
3285 | length = REGFI_VK_MAX_DATA_LENGTH; |
---|
3286 | } |
---|
3287 | |
---|
3288 | if(data_in_offset) |
---|
3289 | return regfi_parse_little_data(file, voffset, length, strict); |
---|
3290 | else |
---|
3291 | { |
---|
3292 | offset = voffset + REGFI_REGF_SIZE; |
---|
3293 | max_size = regfi_calc_maxsize(file, offset); |
---|
3294 | if(max_size < 0) |
---|
3295 | { |
---|
3296 | regfi_log_add(REGFI_LOG_WARN, "Could not find HBIN for data" |
---|
3297 | " at offset 0x%.8X.", offset); |
---|
3298 | goto fail; |
---|
3299 | } |
---|
3300 | |
---|
3301 | if(!regfi_lock(file, &file->cb_lock, "regfi_load_data")) |
---|
3302 | goto fail; |
---|
3303 | |
---|
3304 | if(!regfi_parse_cell(file->cb, offset, NULL, 0, |
---|
3305 | &cell_length, &unalloc)) |
---|
3306 | { |
---|
3307 | regfi_log_add(REGFI_LOG_WARN, "Could not parse cell while" |
---|
3308 | " parsing data record at offset 0x%.8X.", offset); |
---|
3309 | goto fail_locked; |
---|
3310 | } |
---|
3311 | |
---|
3312 | if(!regfi_unlock(file, &file->cb_lock, "regfi_load_data")) |
---|
3313 | goto fail; |
---|
3314 | |
---|
3315 | if((cell_length & 0x00000007) != 0) |
---|
3316 | { |
---|
3317 | regfi_log_add(REGFI_LOG_WARN, "Cell length not multiple of 8" |
---|
3318 | " while parsing data record at offset 0x%.8X.", |
---|
3319 | offset); |
---|
3320 | goto fail; |
---|
3321 | } |
---|
3322 | |
---|
3323 | if(cell_length > max_size) |
---|
3324 | { |
---|
3325 | regfi_log_add(REGFI_LOG_WARN, "Cell extends past HBIN boundary" |
---|
3326 | " while parsing data record at offset 0x%.8X.", |
---|
3327 | offset); |
---|
3328 | goto fail; |
---|
3329 | } |
---|
3330 | |
---|
3331 | if(cell_length - 4 < length) |
---|
3332 | { |
---|
3333 | /* XXX: All big data records thus far have been 16 bytes long. |
---|
3334 | * Should we check for this precise size instead of just |
---|
3335 | * relying upon the above check? |
---|
3336 | */ |
---|
3337 | if (file->major_version >= 1 && file->minor_version >= 5) |
---|
3338 | { |
---|
3339 | /* Attempt to parse a big data record */ |
---|
3340 | return regfi_load_big_data(file, offset, length, cell_length, |
---|
3341 | NULL, strict); |
---|
3342 | } |
---|
3343 | else |
---|
3344 | { |
---|
3345 | regfi_log_add(REGFI_LOG_WARN, "Data length (0x%.8X) larger than" |
---|
3346 | " remaining cell length (0x%.8X)" |
---|
3347 | " while parsing data record at offset 0x%.8X.", |
---|
3348 | length, cell_length - 4, offset); |
---|
3349 | if(strict) |
---|
3350 | goto fail; |
---|
3351 | else |
---|
3352 | length = cell_length - 4; |
---|
3353 | } |
---|
3354 | } |
---|
3355 | |
---|
3356 | ret_val = regfi_parse_data(file, offset, length, strict); |
---|
3357 | } |
---|
3358 | |
---|
3359 | return ret_val; |
---|
3360 | |
---|
3361 | fail_locked: |
---|
3362 | regfi_unlock(file, &file->cb_lock, "regfi_load_data"); |
---|
3363 | fail: |
---|
3364 | ret_val.buf = NULL; |
---|
3365 | ret_val.len = 0; |
---|
3366 | return ret_val; |
---|
3367 | } |
---|
3368 | |
---|
3369 | |
---|
3370 | /****************************************************************************** |
---|
3371 | * Parses the common case data records stored in a single cell. |
---|
3372 | ******************************************************************************/ |
---|
3373 | REGFI_BUFFER regfi_parse_data(REGFI_FILE* file, uint32_t offset, |
---|
3374 | uint32_t length, bool strict) |
---|
3375 | { |
---|
3376 | REGFI_BUFFER ret_val; |
---|
3377 | uint32_t read_length; |
---|
3378 | |
---|
3379 | ret_val.buf = NULL; |
---|
3380 | ret_val.len = 0; |
---|
3381 | |
---|
3382 | if((ret_val.buf = talloc_array(NULL, uint8_t, length)) == NULL) |
---|
3383 | goto fail; |
---|
3384 | ret_val.len = length; |
---|
3385 | |
---|
3386 | if(!regfi_lock(file, &file->cb_lock, "regfi_parse_data")) |
---|
3387 | goto fail; |
---|
3388 | |
---|
3389 | if(regfi_seek(file->cb, offset+4, SEEK_SET) == -1) |
---|
3390 | { |
---|
3391 | regfi_log_add(REGFI_LOG_WARN, "Could not seek while " |
---|
3392 | "reading data at offset 0x%.8X.", offset); |
---|
3393 | goto fail_locked; |
---|
3394 | } |
---|
3395 | |
---|
3396 | read_length = length; |
---|
3397 | if((regfi_read(file->cb, ret_val.buf, &read_length) != 0) |
---|
3398 | || read_length != length) |
---|
3399 | { |
---|
3400 | regfi_log_add(REGFI_LOG_ERROR, "Could not read data block while" |
---|
3401 | " parsing data record at offset 0x%.8X.", offset); |
---|
3402 | goto fail_locked; |
---|
3403 | } |
---|
3404 | |
---|
3405 | if(!regfi_unlock(file, &file->cb_lock, "regfi_parse_data")) |
---|
3406 | goto fail; |
---|
3407 | |
---|
3408 | return ret_val; |
---|
3409 | |
---|
3410 | fail_locked: |
---|
3411 | regfi_unlock(file, &file->cb_lock, "regfi_parse_data"); |
---|
3412 | fail: |
---|
3413 | talloc_free(ret_val.buf); |
---|
3414 | ret_val.buf = NULL; |
---|
3415 | ret_val.buf = 0; |
---|
3416 | return ret_val; |
---|
3417 | } |
---|
3418 | |
---|
3419 | |
---|
3420 | |
---|
3421 | /****************************************************************************** |
---|
3422 | * |
---|
3423 | ******************************************************************************/ |
---|
3424 | REGFI_BUFFER regfi_parse_little_data(REGFI_FILE* file, uint32_t voffset, |
---|
3425 | uint32_t length, bool strict) |
---|
3426 | { |
---|
3427 | uint8_t i; |
---|
3428 | REGFI_BUFFER ret_val; |
---|
3429 | |
---|
3430 | ret_val.buf = NULL; |
---|
3431 | ret_val.len = 0; |
---|
3432 | |
---|
3433 | if(length > 4) |
---|
3434 | { |
---|
3435 | regfi_log_add(REGFI_LOG_ERROR, "Data in offset but length > 4" |
---|
3436 | " while parsing data record. (voffset=0x%.8X, length=%d)", |
---|
3437 | voffset, length); |
---|
3438 | return ret_val; |
---|
3439 | } |
---|
3440 | |
---|
3441 | if((ret_val.buf = talloc_array(NULL, uint8_t, length)) == NULL) |
---|
3442 | return ret_val; |
---|
3443 | ret_val.len = length; |
---|
3444 | |
---|
3445 | for(i = 0; i < length; i++) |
---|
3446 | ret_val.buf[i] = (uint8_t)((voffset >> i*8) & 0xFF); |
---|
3447 | |
---|
3448 | return ret_val; |
---|
3449 | } |
---|
3450 | |
---|
3451 | /****************************************************************************** |
---|
3452 | *******************************************************************************/ |
---|
3453 | REGFI_BUFFER regfi_parse_big_data_header(REGFI_FILE* file, uint32_t offset, |
---|
3454 | uint32_t max_size, bool strict) |
---|
3455 | { |
---|
3456 | REGFI_BUFFER ret_val; |
---|
3457 | uint32_t cell_length; |
---|
3458 | bool unalloc; |
---|
3459 | |
---|
3460 | /* XXX: do something with unalloc? */ |
---|
3461 | ret_val.buf = (uint8_t*)talloc_array(NULL, uint8_t, REGFI_BIG_DATA_MIN_LENGTH); |
---|
3462 | if(ret_val.buf == NULL) |
---|
3463 | goto fail; |
---|
3464 | |
---|
3465 | if(REGFI_BIG_DATA_MIN_LENGTH > max_size) |
---|
3466 | { |
---|
3467 | regfi_log_add(REGFI_LOG_WARN, "Big data header exceeded max_size " |
---|
3468 | "while parsing big data header at offset 0x%.8X.",offset); |
---|
3469 | goto fail; |
---|
3470 | } |
---|
3471 | |
---|
3472 | if(!regfi_lock(file, &file->cb_lock, "regfi_parse_big_data_header")) |
---|
3473 | goto fail; |
---|
3474 | |
---|
3475 | |
---|
3476 | if(!regfi_parse_cell(file->cb, offset, ret_val.buf, REGFI_BIG_DATA_MIN_LENGTH, |
---|
3477 | &cell_length, &unalloc)) |
---|
3478 | { |
---|
3479 | regfi_log_add(REGFI_LOG_WARN, "Could not parse cell while" |
---|
3480 | " parsing big data header at offset 0x%.8X.", offset); |
---|
3481 | goto fail_locked; |
---|
3482 | } |
---|
3483 | |
---|
3484 | if(!regfi_unlock(file, &file->cb_lock, "regfi_parse_big_data_header")) |
---|
3485 | goto fail; |
---|
3486 | |
---|
3487 | if((ret_val.buf[0] != 'd') || (ret_val.buf[1] != 'b')) |
---|
3488 | { |
---|
3489 | regfi_log_add(REGFI_LOG_WARN, "Unknown magic number" |
---|
3490 | " (0x%.2X, 0x%.2X) encountered while parsing" |
---|
3491 | " big data header at offset 0x%.8X.", |
---|
3492 | ret_val.buf[0], ret_val.buf[1], offset); |
---|
3493 | goto fail; |
---|
3494 | } |
---|
3495 | |
---|
3496 | ret_val.len = REGFI_BIG_DATA_MIN_LENGTH; |
---|
3497 | return ret_val; |
---|
3498 | |
---|
3499 | fail_locked: |
---|
3500 | regfi_unlock(file, &file->cb_lock, "regfi_parse_big_data_header"); |
---|
3501 | fail: |
---|
3502 | talloc_free(ret_val.buf); |
---|
3503 | ret_val.buf = NULL; |
---|
3504 | ret_val.len = 0; |
---|
3505 | return ret_val; |
---|
3506 | } |
---|
3507 | |
---|
3508 | |
---|
3509 | |
---|
3510 | /****************************************************************************** |
---|
3511 | * |
---|
3512 | ******************************************************************************/ |
---|
3513 | uint32_t* regfi_parse_big_data_indirect(REGFI_FILE* file, uint32_t offset, |
---|
3514 | uint16_t num_chunks, bool strict) |
---|
3515 | { |
---|
3516 | uint32_t* ret_val; |
---|
3517 | uint32_t indirect_length; |
---|
3518 | int32_t max_size; |
---|
3519 | uint16_t i; |
---|
3520 | bool unalloc; |
---|
3521 | |
---|
3522 | /* XXX: do something with unalloc? */ |
---|
3523 | max_size = regfi_calc_maxsize(file, offset); |
---|
3524 | if((max_size < 0) || (num_chunks*sizeof(uint32_t) + 4 > max_size)) |
---|
3525 | return NULL; |
---|
3526 | |
---|
3527 | ret_val = (uint32_t*)talloc_array(NULL, uint32_t, num_chunks); |
---|
3528 | if(ret_val == NULL) |
---|
3529 | goto fail; |
---|
3530 | |
---|
3531 | if(!regfi_lock(file, &file->cb_lock, "regfi_parse_big_data_indirect")) |
---|
3532 | goto fail; |
---|
3533 | |
---|
3534 | if(!regfi_parse_cell(file->cb, offset, (uint8_t*)ret_val, |
---|
3535 | num_chunks*sizeof(uint32_t), |
---|
3536 | &indirect_length, &unalloc)) |
---|
3537 | { |
---|
3538 | regfi_log_add(REGFI_LOG_WARN, "Could not parse cell while" |
---|
3539 | " parsing big data indirect record at offset 0x%.8X.", |
---|
3540 | offset); |
---|
3541 | goto fail_locked; |
---|
3542 | } |
---|
3543 | |
---|
3544 | if(!regfi_unlock(file, &file->cb_lock, "regfi_parse_big_data_indirect")) |
---|
3545 | goto fail; |
---|
3546 | |
---|
3547 | /* Convert pointers to proper endianess, verify they are aligned. */ |
---|
3548 | for(i=0; i<num_chunks; i++) |
---|
3549 | { |
---|
3550 | ret_val[i] = IVAL(ret_val, i*sizeof(uint32_t)); |
---|
3551 | if((ret_val[i] & 0x00000007) != 0) |
---|
3552 | goto fail; |
---|
3553 | } |
---|
3554 | |
---|
3555 | return ret_val; |
---|
3556 | |
---|
3557 | fail_locked: |
---|
3558 | regfi_unlock(file, &file->cb_lock, "regfi_parse_big_data_indirect"); |
---|
3559 | fail: |
---|
3560 | talloc_free(ret_val); |
---|
3561 | return NULL; |
---|
3562 | } |
---|
3563 | |
---|
3564 | |
---|
3565 | /****************************************************************************** |
---|
3566 | * Arguments: |
---|
3567 | * file -- |
---|
3568 | * offsets -- list of virtual offsets. |
---|
3569 | * num_chunks -- |
---|
3570 | * strict -- |
---|
3571 | * |
---|
3572 | * Returns: |
---|
3573 | * A range_list with physical offsets and complete lengths |
---|
3574 | * (including cell headers) of associated cells. |
---|
3575 | * No data in range_list elements. |
---|
3576 | ******************************************************************************/ |
---|
3577 | range_list* regfi_parse_big_data_cells(REGFI_FILE* file, uint32_t* offsets, |
---|
3578 | uint16_t num_chunks, bool strict) |
---|
3579 | { |
---|
3580 | uint32_t cell_length, chunk_offset; |
---|
3581 | range_list* ret_val; |
---|
3582 | uint16_t i; |
---|
3583 | bool unalloc; |
---|
3584 | |
---|
3585 | /* XXX: do something with unalloc? */ |
---|
3586 | ret_val = range_list_new(); |
---|
3587 | if(ret_val == NULL) |
---|
3588 | goto fail; |
---|
3589 | |
---|
3590 | for(i=0; i<num_chunks; i++) |
---|
3591 | { |
---|
3592 | if(!regfi_lock(file, &file->cb_lock, "regfi_parse_big_data_cells")) |
---|
3593 | goto fail; |
---|
3594 | |
---|
3595 | chunk_offset = offsets[i]+REGFI_REGF_SIZE; |
---|
3596 | if(!regfi_parse_cell(file->cb, chunk_offset, NULL, 0, |
---|
3597 | &cell_length, &unalloc)) |
---|
3598 | { |
---|
3599 | regfi_log_add(REGFI_LOG_WARN, "Could not parse cell while" |
---|
3600 | " parsing big data chunk at offset 0x%.8X.", |
---|
3601 | chunk_offset); |
---|
3602 | goto fail_locked; |
---|
3603 | } |
---|
3604 | |
---|
3605 | if(!regfi_unlock(file, &file->cb_lock, "regfi_parse_big_data_cells")) |
---|
3606 | goto fail; |
---|
3607 | |
---|
3608 | if(!range_list_add(ret_val, chunk_offset, cell_length, NULL)) |
---|
3609 | goto fail; |
---|
3610 | } |
---|
3611 | |
---|
3612 | return ret_val; |
---|
3613 | |
---|
3614 | fail_locked: |
---|
3615 | regfi_unlock(file, &file->cb_lock, "regfi_parse_big_data_cells"); |
---|
3616 | fail: |
---|
3617 | if(ret_val != NULL) |
---|
3618 | range_list_free(ret_val); |
---|
3619 | return NULL; |
---|
3620 | } |
---|
3621 | |
---|
3622 | |
---|
3623 | /****************************************************************************** |
---|
3624 | *******************************************************************************/ |
---|
3625 | REGFI_BUFFER regfi_load_big_data(REGFI_FILE* file, |
---|
3626 | uint32_t offset, uint32_t data_length, |
---|
3627 | uint32_t cell_length, range_list* used_ranges, |
---|
3628 | bool strict) |
---|
3629 | { |
---|
3630 | REGFI_BUFFER ret_val; |
---|
3631 | uint16_t num_chunks, i; |
---|
3632 | uint32_t read_length, data_left, tmp_len, indirect_offset; |
---|
3633 | uint32_t* indirect_ptrs = NULL; |
---|
3634 | REGFI_BUFFER bd_header; |
---|
3635 | range_list* bd_cells = NULL; |
---|
3636 | const range_list_element* cell_info; |
---|
3637 | |
---|
3638 | ret_val.buf = NULL; |
---|
3639 | |
---|
3640 | /* XXX: Add better error/warning messages */ |
---|
3641 | |
---|
3642 | bd_header = regfi_parse_big_data_header(file, offset, cell_length, strict); |
---|
3643 | if(bd_header.buf == NULL) |
---|
3644 | goto fail; |
---|
3645 | |
---|
3646 | /* Keep track of used space for use by reglookup-recover */ |
---|
3647 | if(used_ranges != NULL) |
---|
3648 | if(!range_list_add(used_ranges, offset, cell_length, NULL)) |
---|
3649 | goto fail; |
---|
3650 | |
---|
3651 | num_chunks = SVAL(bd_header.buf, 0x2); |
---|
3652 | indirect_offset = IVAL(bd_header.buf, 0x4) + REGFI_REGF_SIZE; |
---|
3653 | talloc_free(bd_header.buf); |
---|
3654 | |
---|
3655 | indirect_ptrs = regfi_parse_big_data_indirect(file, indirect_offset, |
---|
3656 | num_chunks, strict); |
---|
3657 | if(indirect_ptrs == NULL) |
---|
3658 | goto fail; |
---|
3659 | |
---|
3660 | if(used_ranges != NULL) |
---|
3661 | if(!range_list_add(used_ranges, indirect_offset, num_chunks*4+4, NULL)) |
---|
3662 | goto fail; |
---|
3663 | |
---|
3664 | if((ret_val.buf = talloc_array(NULL, uint8_t, data_length)) == NULL) |
---|
3665 | goto fail; |
---|
3666 | data_left = data_length; |
---|
3667 | |
---|
3668 | bd_cells = regfi_parse_big_data_cells(file, indirect_ptrs, num_chunks, strict); |
---|
3669 | if(bd_cells == NULL) |
---|
3670 | goto fail; |
---|
3671 | |
---|
3672 | talloc_free(indirect_ptrs); |
---|
3673 | indirect_ptrs = NULL; |
---|
3674 | |
---|
3675 | for(i=0; (i<num_chunks) && (data_left>0); i++) |
---|
3676 | { |
---|
3677 | cell_info = range_list_get(bd_cells, i); |
---|
3678 | if(cell_info == NULL) |
---|
3679 | goto fail; |
---|
3680 | |
---|
3681 | /* XXX: This should be "cell_info->length-4" to account for the 4 byte cell |
---|
3682 | * length. However, it has been observed that some (all?) chunks |
---|
3683 | * have an additional 4 bytes of 0 at the end of their cells that |
---|
3684 | * isn't part of the data, so we're trimming that off too. |
---|
3685 | * Perhaps it's just an 8 byte alignment requirement... |
---|
3686 | */ |
---|
3687 | if(cell_info->length - 8 >= data_left) |
---|
3688 | { |
---|
3689 | if(i+1 != num_chunks) |
---|
3690 | { |
---|
3691 | regfi_log_add(REGFI_LOG_WARN, "Left over chunks detected " |
---|
3692 | "while constructing big data at offset 0x%.8X " |
---|
3693 | "(chunk offset 0x%.8X).", offset, cell_info->offset); |
---|
3694 | } |
---|
3695 | read_length = data_left; |
---|
3696 | } |
---|
3697 | else |
---|
3698 | read_length = cell_info->length - 8; |
---|
3699 | |
---|
3700 | |
---|
3701 | if(read_length > regfi_calc_maxsize(file, cell_info->offset)) |
---|
3702 | { |
---|
3703 | regfi_log_add(REGFI_LOG_WARN, "A chunk exceeded the maxsize " |
---|
3704 | "while constructing big data at offset 0x%.8X " |
---|
3705 | "(chunk offset 0x%.8X).", offset, cell_info->offset); |
---|
3706 | goto fail; |
---|
3707 | } |
---|
3708 | |
---|
3709 | if(!regfi_lock(file, &file->cb_lock, "regfi_load_big_data")) |
---|
3710 | goto fail; |
---|
3711 | |
---|
3712 | if(regfi_seek(file->cb, cell_info->offset+sizeof(uint32_t), SEEK_SET) == -1) |
---|
3713 | { |
---|
3714 | regfi_log_add(REGFI_LOG_WARN, "Could not seek to chunk while " |
---|
3715 | "constructing big data at offset 0x%.8X " |
---|
3716 | "(chunk offset 0x%.8X).", offset, cell_info->offset); |
---|
3717 | goto fail_locked; |
---|
3718 | } |
---|
3719 | |
---|
3720 | tmp_len = read_length; |
---|
3721 | if(regfi_read(file->cb, ret_val.buf+(data_length-data_left), |
---|
3722 | &read_length) != 0 || (read_length != tmp_len)) |
---|
3723 | { |
---|
3724 | regfi_log_add(REGFI_LOG_WARN, "Could not read data chunk while" |
---|
3725 | " constructing big data at offset 0x%.8X" |
---|
3726 | " (chunk offset 0x%.8X).", offset, cell_info->offset); |
---|
3727 | goto fail_locked; |
---|
3728 | } |
---|
3729 | |
---|
3730 | if(!regfi_unlock(file, &file->cb_lock, "regfi_load_big_data")) |
---|
3731 | goto fail; |
---|
3732 | |
---|
3733 | if(used_ranges != NULL) |
---|
3734 | if(!range_list_add(used_ranges, cell_info->offset,cell_info->length,NULL)) |
---|
3735 | goto fail; |
---|
3736 | |
---|
3737 | data_left -= read_length; |
---|
3738 | } |
---|
3739 | range_list_free(bd_cells); |
---|
3740 | |
---|
3741 | ret_val.len = data_length-data_left; |
---|
3742 | return ret_val; |
---|
3743 | |
---|
3744 | fail_locked: |
---|
3745 | regfi_unlock(file, &file->cb_lock, "regfi_load_big_data"); |
---|
3746 | fail: |
---|
3747 | talloc_free(ret_val.buf); |
---|
3748 | talloc_free(indirect_ptrs); |
---|
3749 | if(bd_cells != NULL) |
---|
3750 | range_list_free(bd_cells); |
---|
3751 | ret_val.buf = NULL; |
---|
3752 | ret_val.len = 0; |
---|
3753 | return ret_val; |
---|
3754 | } |
---|
3755 | |
---|
3756 | |
---|
3757 | range_list* regfi_parse_unalloc_cells(REGFI_FILE* file) |
---|
3758 | { |
---|
3759 | range_list* ret_val; |
---|
3760 | REGFI_HBIN* hbin; |
---|
3761 | const range_list_element* hbins_elem; |
---|
3762 | uint32_t i, num_hbins, curr_off, cell_len; |
---|
3763 | bool is_unalloc; |
---|
3764 | |
---|
3765 | ret_val = range_list_new(); |
---|
3766 | if(ret_val == NULL) |
---|
3767 | return NULL; |
---|
3768 | |
---|
3769 | if(!regfi_read_lock(file, &file->hbins_lock, "regfi_parse_unalloc_cells")) |
---|
3770 | { |
---|
3771 | range_list_free(ret_val); |
---|
3772 | return NULL; |
---|
3773 | } |
---|
3774 | |
---|
3775 | num_hbins = range_list_size(file->hbins); |
---|
3776 | for(i=0; i<num_hbins; i++) |
---|
3777 | { |
---|
3778 | hbins_elem = range_list_get(file->hbins, i); |
---|
3779 | if(hbins_elem == NULL) |
---|
3780 | break; |
---|
3781 | hbin = (REGFI_HBIN*)hbins_elem->data; |
---|
3782 | |
---|
3783 | curr_off = REGFI_HBIN_HEADER_SIZE; |
---|
3784 | while(curr_off < hbin->block_size) |
---|
3785 | { |
---|
3786 | if(!regfi_lock(file, &file->cb_lock, "regfi_parse_unalloc_cells")) |
---|
3787 | break; |
---|
3788 | |
---|
3789 | if(!regfi_parse_cell(file->cb, hbin->file_off+curr_off, NULL, 0, |
---|
3790 | &cell_len, &is_unalloc)) |
---|
3791 | { |
---|
3792 | regfi_unlock(file, &file->cb_lock, "regfi_parse_unalloc_cells"); |
---|
3793 | break; |
---|
3794 | } |
---|
3795 | |
---|
3796 | if(!regfi_unlock(file, &file->cb_lock, "regfi_parse_unalloc_cells")) |
---|
3797 | break; |
---|
3798 | |
---|
3799 | if((cell_len == 0) || ((cell_len & 0x00000007) != 0)) |
---|
3800 | { |
---|
3801 | regfi_log_add(REGFI_LOG_ERROR, "Bad cell length encountered" |
---|
3802 | " while parsing unallocated cells at offset 0x%.8X.", |
---|
3803 | hbin->file_off+curr_off); |
---|
3804 | break; |
---|
3805 | } |
---|
3806 | |
---|
3807 | /* for some reason the record_size of the last record in |
---|
3808 | an hbin block can extend past the end of the block |
---|
3809 | even though the record fits within the remaining |
---|
3810 | space....aaarrrgggghhhhhh */ |
---|
3811 | if(curr_off + cell_len >= hbin->block_size) |
---|
3812 | cell_len = hbin->block_size - curr_off; |
---|
3813 | |
---|
3814 | if(is_unalloc) |
---|
3815 | range_list_add(ret_val, hbin->file_off+curr_off, |
---|
3816 | cell_len, NULL); |
---|
3817 | |
---|
3818 | curr_off = curr_off+cell_len; |
---|
3819 | } |
---|
3820 | } |
---|
3821 | |
---|
3822 | if(!regfi_rw_unlock(file, &file->hbins_lock, "regfi_parse_unalloc_cells")) |
---|
3823 | { |
---|
3824 | range_list_free(ret_val); |
---|
3825 | return NULL; |
---|
3826 | } |
---|
3827 | |
---|
3828 | return ret_val; |
---|
3829 | } |
---|
3830 | |
---|
3831 | |
---|
3832 | /* From lib/time.c */ |
---|
3833 | |
---|
3834 | /**************************************************************************** |
---|
3835 | Returns an 8 byte filetime from a time_t |
---|
3836 | This takes real GMT as input and converts to kludge-GMT |
---|
3837 | ****************************************************************************/ |
---|
3838 | REGFI_NTTIME regfi_unix2nt_time(time_t t) |
---|
3839 | { |
---|
3840 | double d; |
---|
3841 | |
---|
3842 | if (t==0) |
---|
3843 | return 0L; |
---|
3844 | |
---|
3845 | if (t == TIME_T_MAX) |
---|
3846 | return 0x7fffffffffffffffL; |
---|
3847 | |
---|
3848 | if (t == -1) |
---|
3849 | return 0xffffffffffffffffL; |
---|
3850 | |
---|
3851 | /* this converts GMT to kludge-GMT */ |
---|
3852 | /* XXX: This was removed due to difficult dependency requirements. |
---|
3853 | * So far, times appear to be correct without this adjustment, but |
---|
3854 | * that may be proven wrong with adequate testing. |
---|
3855 | */ |
---|
3856 | /* t -= TimeDiff(t) - get_serverzone(); */ |
---|
3857 | |
---|
3858 | d = (double)(t) + REGFI_TIME_FIXUP; |
---|
3859 | d *= 1.0e7; |
---|
3860 | /* |
---|
3861 | nt->high = (uint32_t)(d * (1.0/c)); |
---|
3862 | nt->low = (uint32_t)(d - ((double)nt->high) * c); |
---|
3863 | */ |
---|
3864 | |
---|
3865 | return (REGFI_NTTIME) d; |
---|
3866 | } |
---|
3867 | |
---|
3868 | |
---|
3869 | /**************************************************************************** |
---|
3870 | Interpret an 8 byte "filetime" structure to a time_t |
---|
3871 | It's originally in "100ns units since jan 1st 1601" |
---|
3872 | |
---|
3873 | An 8 byte value of 0xffffffffffffffff will be returned as (time_t)0. |
---|
3874 | |
---|
3875 | It appears to be kludge-GMT (at least for file listings). This means |
---|
3876 | its the GMT you get by taking a localtime and adding the |
---|
3877 | serverzone. This is NOT the same as GMT in some cases. This routine |
---|
3878 | converts this to real GMT. |
---|
3879 | ****************************************************************************/ |
---|
3880 | double regfi_nt2unix_time(REGFI_NTTIME nt) |
---|
3881 | { |
---|
3882 | double ret_val; |
---|
3883 | |
---|
3884 | if (nt == 0 || nt == 0xffffffffffffffffL) |
---|
3885 | return 0; |
---|
3886 | |
---|
3887 | ret_val = (double)(nt) * 1.0e-7; |
---|
3888 | |
---|
3889 | /* now adjust by 369 years to make the secs since 1970 */ |
---|
3890 | ret_val -= REGFI_TIME_FIXUP; |
---|
3891 | |
---|
3892 | /* this takes us from kludge-GMT to real GMT */ |
---|
3893 | /* XXX: This was removed due to difficult dependency requirements. |
---|
3894 | * So far, times appear to be correct without this adjustment, but |
---|
3895 | * that may be proven wrong with adequate testing. |
---|
3896 | */ |
---|
3897 | /* |
---|
3898 | ret -= get_serverzone(); |
---|
3899 | ret += LocTimeDiff(ret); |
---|
3900 | */ |
---|
3901 | |
---|
3902 | return ret_val; |
---|
3903 | } |
---|
3904 | |
---|
3905 | /* End of stuff from lib/time.c */ |
---|