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