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