[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 148 2009-02-22 23:22:59Z 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; |
---|
| 562 | sublists = (REGFI_SUBKEY_LIST**)zalloc(num_sublists |
---|
| 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 | } |
---|
[139] | 577 | 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; |
---|
[139] | 592 | uint32 i, cell_length, length, elem_size; |
---|
| 593 | uint8* elements; |
---|
[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 | |
---|
[135] | 633 | ret_val = (REGFI_SUBKEY_LIST*)zalloc(sizeof(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) |
---|
| 654 | { |
---|
| 655 | free(ret_val); |
---|
| 656 | return NULL; |
---|
| 657 | } |
---|
| 658 | length = cell_length - REGFI_SUBKEY_LIST_MIN_LEN - sizeof(uint32); |
---|
[134] | 659 | } |
---|
[30] | 660 | |
---|
[134] | 661 | ret_val->elements |
---|
[139] | 662 | = (REGFI_SUBKEY_LIST_ELEM*)zalloc(ret_val->num_children |
---|
| 663 | * sizeof(REGFI_SUBKEY_LIST_ELEM)); |
---|
[127] | 664 | if(ret_val->elements == NULL) |
---|
[104] | 665 | { |
---|
| 666 | free(ret_val); |
---|
| 667 | return NULL; |
---|
[31] | 668 | } |
---|
[30] | 669 | |
---|
[139] | 670 | elements = (uint8*)zalloc(length); |
---|
| 671 | if(elements == NULL) |
---|
[104] | 672 | { |
---|
[127] | 673 | free(ret_val->elements); |
---|
[104] | 674 | free(ret_val); |
---|
| 675 | return NULL; |
---|
[31] | 676 | } |
---|
[30] | 677 | |
---|
[139] | 678 | if(regfi_read(file->fd, elements, &length) != 0 |
---|
| 679 | || length != elem_size*ret_val->num_children) |
---|
[104] | 680 | { |
---|
[127] | 681 | free(ret_val->elements); |
---|
[104] | 682 | free(ret_val); |
---|
| 683 | return NULL; |
---|
| 684 | } |
---|
[30] | 685 | |
---|
[139] | 686 | if(elem_size == sizeof(uint32)) |
---|
[104] | 687 | { |
---|
[139] | 688 | for (i=0; i < ret_val->num_children; i++) |
---|
[134] | 689 | { |
---|
[139] | 690 | ret_val->elements[i].offset = IVAL(elements, i*elem_size); |
---|
[134] | 691 | ret_val->elements[i].hash = 0; |
---|
| 692 | } |
---|
[104] | 693 | } |
---|
[134] | 694 | else |
---|
| 695 | { |
---|
[139] | 696 | for (i=0; i < ret_val->num_children; i++) |
---|
[134] | 697 | { |
---|
[139] | 698 | ret_val->elements[i].offset = IVAL(elements, i*elem_size); |
---|
| 699 | ret_val->elements[i].hash = IVAL(elements, i*elem_size+4); |
---|
[134] | 700 | } |
---|
| 701 | } |
---|
[139] | 702 | free(elements); |
---|
[30] | 703 | |
---|
[104] | 704 | return ret_val; |
---|
[30] | 705 | } |
---|
| 706 | |
---|
| 707 | |
---|
[139] | 708 | /******************************************************************* |
---|
| 709 | *******************************************************************/ |
---|
| 710 | REGFI_SUBKEY_LIST* regfi_merge_subkeylists(uint16 num_lists, |
---|
| 711 | REGFI_SUBKEY_LIST** lists, |
---|
| 712 | bool strict) |
---|
| 713 | { |
---|
| 714 | uint32 i,j,k; |
---|
| 715 | REGFI_SUBKEY_LIST* ret_val; |
---|
[102] | 716 | |
---|
[139] | 717 | if(lists == NULL) |
---|
| 718 | return NULL; |
---|
| 719 | ret_val = (REGFI_SUBKEY_LIST*)zalloc(sizeof(REGFI_SUBKEY_LIST)); |
---|
| 720 | |
---|
| 721 | if(ret_val == NULL) |
---|
| 722 | return NULL; |
---|
| 723 | |
---|
| 724 | /* Obtain total number of elements */ |
---|
| 725 | ret_val->num_keys = 0; |
---|
| 726 | for(i=0; i < num_lists; i++) |
---|
| 727 | { |
---|
| 728 | if(lists[i] != NULL) |
---|
| 729 | ret_val->num_keys += lists[i]->num_children; |
---|
| 730 | } |
---|
| 731 | ret_val->num_children = ret_val->num_keys; |
---|
| 732 | |
---|
| 733 | if(ret_val->num_keys > 0) |
---|
| 734 | { |
---|
| 735 | ret_val->elements = |
---|
| 736 | (REGFI_SUBKEY_LIST_ELEM*)zalloc(sizeof(REGFI_SUBKEY_LIST_ELEM) |
---|
| 737 | * ret_val->num_keys); |
---|
| 738 | k=0; |
---|
| 739 | |
---|
| 740 | if(ret_val->elements != NULL) |
---|
| 741 | { |
---|
| 742 | for(i=0; i < num_lists; i++) |
---|
| 743 | { |
---|
| 744 | if(lists[i] != NULL) |
---|
| 745 | { |
---|
| 746 | for(j=0; j < lists[i]->num_keys; j++) |
---|
| 747 | { |
---|
| 748 | ret_val->elements[k].hash=lists[i]->elements[j].hash; |
---|
| 749 | ret_val->elements[k++].offset=lists[i]->elements[j].offset; |
---|
| 750 | } |
---|
| 751 | } |
---|
| 752 | } |
---|
| 753 | } |
---|
| 754 | } |
---|
| 755 | |
---|
| 756 | for(i=0; i < num_lists; i++) |
---|
| 757 | regfi_subkeylist_free(lists[i]); |
---|
| 758 | free(lists); |
---|
| 759 | |
---|
| 760 | return ret_val; |
---|
| 761 | } |
---|
| 762 | |
---|
| 763 | |
---|
[147] | 764 | /****************************************************************************** |
---|
| 765 | * |
---|
| 766 | ******************************************************************************/ |
---|
| 767 | REGFI_SK_REC* regfi_parse_sk(REGFI_FILE* file, uint32 offset, uint32 max_size, |
---|
| 768 | bool strict) |
---|
[30] | 769 | { |
---|
[135] | 770 | REGFI_SK_REC* ret_val; |
---|
[147] | 771 | uint8* sec_desc_buf = NULL; |
---|
[102] | 772 | uint32 cell_length, length; |
---|
| 773 | uint8 sk_header[REGFI_SK_MIN_LENGTH]; |
---|
| 774 | bool unalloc = false; |
---|
[30] | 775 | |
---|
[102] | 776 | if(!regfi_parse_cell(file->fd, offset, sk_header, REGFI_SK_MIN_LENGTH, |
---|
| 777 | &cell_length, &unalloc)) |
---|
[137] | 778 | { |
---|
[138] | 779 | regfi_add_message(file, REGFI_MSG_WARN, "Could not parse SK record cell" |
---|
[137] | 780 | " at offset 0x%.8X.", offset); |
---|
[102] | 781 | return NULL; |
---|
[137] | 782 | } |
---|
[102] | 783 | |
---|
| 784 | if(sk_header[0] != 's' || sk_header[1] != 'k') |
---|
[137] | 785 | { |
---|
[138] | 786 | regfi_add_message(file, REGFI_MSG_WARN, "Magic number mismatch in parsing" |
---|
| 787 | " SK record at offset 0x%.8X.", offset); |
---|
[102] | 788 | return NULL; |
---|
[137] | 789 | } |
---|
| 790 | |
---|
[147] | 791 | /* ret_val = (REGFI_SK_REC*)zalloc(sizeof(REGFI_SK_REC));*/ |
---|
| 792 | ret_val = talloc(NULL, REGFI_SK_REC); |
---|
[102] | 793 | if(ret_val == NULL) |
---|
| 794 | return NULL; |
---|
[30] | 795 | |
---|
[102] | 796 | ret_val->offset = offset; |
---|
[116] | 797 | /* XXX: Is there a way to be more conservative (shorter) with |
---|
| 798 | * cell length when cell is unallocated? |
---|
[111] | 799 | */ |
---|
[102] | 800 | ret_val->cell_size = cell_length; |
---|
[30] | 801 | |
---|
[102] | 802 | if(ret_val->cell_size > max_size) |
---|
| 803 | ret_val->cell_size = max_size & 0xFFFFFFF8; |
---|
| 804 | if((ret_val->cell_size < REGFI_SK_MIN_LENGTH) |
---|
| 805 | || (strict && ret_val->cell_size != (ret_val->cell_size & 0xFFFFFFF8))) |
---|
| 806 | { |
---|
[138] | 807 | regfi_add_message(file, REGFI_MSG_WARN, "Invalid cell size found while" |
---|
| 808 | " parsing SK record at offset 0x%.8X.", offset); |
---|
[147] | 809 | goto fail; |
---|
[102] | 810 | } |
---|
[30] | 811 | |
---|
[102] | 812 | ret_val->magic[0] = sk_header[0]; |
---|
| 813 | ret_val->magic[1] = sk_header[1]; |
---|
[30] | 814 | |
---|
[102] | 815 | ret_val->unknown_tag = SVAL(sk_header, 0x2); |
---|
| 816 | ret_val->prev_sk_off = IVAL(sk_header, 0x4); |
---|
| 817 | ret_val->next_sk_off = IVAL(sk_header, 0x8); |
---|
| 818 | ret_val->ref_count = IVAL(sk_header, 0xC); |
---|
| 819 | ret_val->desc_size = IVAL(sk_header, 0x10); |
---|
[30] | 820 | |
---|
[140] | 821 | if(ret_val->prev_sk_off != (ret_val->prev_sk_off & 0xFFFFFFF8) |
---|
| 822 | || ret_val->next_sk_off != (ret_val->next_sk_off & 0xFFFFFFF8)) |
---|
| 823 | { |
---|
| 824 | regfi_add_message(file, REGFI_MSG_WARN, "SK record's next/previous offsets" |
---|
| 825 | " are not a multiple of 8 while parsing SK record at" |
---|
| 826 | " offset 0x%.8X.", offset); |
---|
[147] | 827 | goto fail; |
---|
[140] | 828 | } |
---|
| 829 | |
---|
[102] | 830 | if(ret_val->desc_size + REGFI_SK_MIN_LENGTH > ret_val->cell_size) |
---|
| 831 | { |
---|
[140] | 832 | regfi_add_message(file, REGFI_MSG_WARN, "Security descriptor too large for" |
---|
[138] | 833 | " cell while parsing SK record at offset 0x%.8X.", |
---|
| 834 | offset); |
---|
[147] | 835 | goto fail; |
---|
[102] | 836 | } |
---|
[30] | 837 | |
---|
[147] | 838 | sec_desc_buf = (uint8*)malloc(ret_val->desc_size); |
---|
| 839 | if(sec_desc_buf == NULL) |
---|
| 840 | goto fail; |
---|
[102] | 841 | |
---|
[134] | 842 | length = ret_val->desc_size; |
---|
| 843 | if(regfi_read(file->fd, sec_desc_buf, &length) != 0 |
---|
| 844 | || length != ret_val->desc_size) |
---|
| 845 | { |
---|
[138] | 846 | regfi_add_message(file, REGFI_MSG_ERROR, "Failed to read security" |
---|
| 847 | " descriptor while parsing SK record at offset 0x%.8X.", |
---|
| 848 | offset); |
---|
[147] | 849 | goto fail; |
---|
[134] | 850 | } |
---|
[102] | 851 | |
---|
[147] | 852 | if(!(ret_val->sec_desc = winsec_parse_desc(ret_val, sec_desc_buf, |
---|
| 853 | ret_val->desc_size))) |
---|
[134] | 854 | { |
---|
[138] | 855 | regfi_add_message(file, REGFI_MSG_ERROR, "Failed to parse security" |
---|
| 856 | " descriptor while parsing SK record at offset 0x%.8X.", |
---|
| 857 | offset); |
---|
[147] | 858 | goto fail; |
---|
[134] | 859 | } |
---|
[147] | 860 | |
---|
[134] | 861 | free(sec_desc_buf); |
---|
[147] | 862 | return ret_val; |
---|
[134] | 863 | |
---|
[147] | 864 | fail: |
---|
| 865 | if(sec_desc_buf != NULL) |
---|
| 866 | free(sec_desc_buf); |
---|
| 867 | talloc_free(ret_val); |
---|
| 868 | return NULL; |
---|
[30] | 869 | } |
---|
| 870 | |
---|
| 871 | |
---|
[145] | 872 | REGFI_VALUE_LIST* regfi_parse_valuelist(REGFI_FILE* file, uint32 offset, |
---|
| 873 | uint32 num_values, bool strict) |
---|
[111] | 874 | { |
---|
[145] | 875 | REGFI_VALUE_LIST* ret_val; |
---|
[111] | 876 | uint32 i, cell_length, length, read_len; |
---|
| 877 | bool unalloc; |
---|
[30] | 878 | |
---|
[111] | 879 | if(!regfi_parse_cell(file->fd, offset, NULL, 0, &cell_length, &unalloc)) |
---|
[137] | 880 | { |
---|
[138] | 881 | regfi_add_message(file, REGFI_MSG_ERROR, "Failed to read cell header" |
---|
[137] | 882 | " while parsing value list at offset 0x%.8X.", offset); |
---|
[111] | 883 | return NULL; |
---|
[137] | 884 | } |
---|
[111] | 885 | |
---|
| 886 | if(cell_length != (cell_length & 0xFFFFFFF8)) |
---|
| 887 | { |
---|
[145] | 888 | regfi_add_message(file, REGFI_MSG_WARN, "Cell length not a multiple of 8" |
---|
| 889 | " while parsing value list at offset 0x%.8X.", offset); |
---|
[111] | 890 | if(strict) |
---|
| 891 | return NULL; |
---|
| 892 | cell_length = cell_length & 0xFFFFFFF8; |
---|
| 893 | } |
---|
[145] | 894 | |
---|
[111] | 895 | if((num_values * sizeof(uint32)) > cell_length-sizeof(uint32)) |
---|
[137] | 896 | { |
---|
[140] | 897 | regfi_add_message(file, REGFI_MSG_WARN, "Too many values found" |
---|
[137] | 898 | " while parsing value list at offset 0x%.8X.", offset); |
---|
[145] | 899 | if(strict) |
---|
| 900 | return NULL; |
---|
| 901 | num_values = cell_length/sizeof(uint32) - sizeof(uint32); |
---|
[137] | 902 | } |
---|
[111] | 903 | |
---|
| 904 | read_len = num_values*sizeof(uint32); |
---|
[145] | 905 | ret_val = (REGFI_VALUE_LIST*)malloc(sizeof(REGFI_VALUE_LIST)); |
---|
[111] | 906 | if(ret_val == NULL) |
---|
| 907 | return NULL; |
---|
| 908 | |
---|
[145] | 909 | ret_val->elements = (REGFI_VALUE_LIST_ELEM*)malloc(read_len); |
---|
| 910 | if(ret_val->elements == NULL) |
---|
| 911 | { |
---|
| 912 | free(ret_val); |
---|
| 913 | return NULL; |
---|
| 914 | } |
---|
| 915 | ret_val->num_values = num_values; |
---|
| 916 | |
---|
[111] | 917 | length = read_len; |
---|
[145] | 918 | if((regfi_read(file->fd, (uint8*)ret_val->elements, &length) != 0) |
---|
| 919 | || length != read_len) |
---|
[111] | 920 | { |
---|
[138] | 921 | regfi_add_message(file, REGFI_MSG_ERROR, "Failed to read value pointers" |
---|
[137] | 922 | " while parsing value list at offset 0x%.8X.", offset); |
---|
[145] | 923 | free(ret_val->elements); |
---|
[111] | 924 | free(ret_val); |
---|
| 925 | return NULL; |
---|
| 926 | } |
---|
| 927 | |
---|
| 928 | for(i=0; i < num_values; i++) |
---|
| 929 | { |
---|
| 930 | /* Fix endianness */ |
---|
[145] | 931 | ret_val->elements[i] = IVAL(&ret_val->elements[i], 0); |
---|
[111] | 932 | |
---|
| 933 | /* Validate the first num_values values to ensure they make sense */ |
---|
| 934 | if(strict) |
---|
| 935 | { |
---|
[145] | 936 | /* XXX: Need to revisit this file length check when we start dealing |
---|
| 937 | * with partial files. */ |
---|
| 938 | if((ret_val->elements[i] + REGFI_REGF_SIZE > file->file_length) |
---|
| 939 | || ((ret_val->elements[i] & 0xFFFFFFF8) != ret_val->elements[i])) |
---|
[111] | 940 | { |
---|
[145] | 941 | regfi_add_message(file, REGFI_MSG_WARN, "Invalid value pointer" |
---|
[138] | 942 | " (0x%.8X) found while parsing value list at offset" |
---|
[145] | 943 | " 0x%.8X.", ret_val->elements[i], offset); |
---|
| 944 | free(ret_val->elements); |
---|
[111] | 945 | free(ret_val); |
---|
| 946 | return NULL; |
---|
| 947 | } |
---|
| 948 | } |
---|
| 949 | } |
---|
| 950 | |
---|
| 951 | return ret_val; |
---|
| 952 | } |
---|
| 953 | |
---|
| 954 | |
---|
| 955 | |
---|
[103] | 956 | /****************************************************************************** |
---|
| 957 | ******************************************************************************/ |
---|
[146] | 958 | REGFI_VK_REC* regfi_load_value(REGFI_FILE* file, uint32 offset, |
---|
| 959 | bool strict) |
---|
[30] | 960 | { |
---|
[145] | 961 | REGFI_VK_REC* ret_val = NULL; |
---|
[146] | 962 | const REGFI_HBIN* hbin; |
---|
[145] | 963 | uint32 data_offset, data_maxsize; |
---|
[30] | 964 | |
---|
[145] | 965 | hbin = regfi_lookup_hbin(file, offset - REGFI_REGF_SIZE); |
---|
| 966 | if(!hbin) |
---|
[103] | 967 | return NULL; |
---|
[145] | 968 | |
---|
| 969 | ret_val = regfi_parse_vk(file, offset, |
---|
| 970 | hbin->block_size + hbin->file_off - offset, strict); |
---|
[30] | 971 | |
---|
[103] | 972 | if(ret_val == NULL) |
---|
| 973 | return NULL; |
---|
[145] | 974 | |
---|
| 975 | if(ret_val->data_size == 0) |
---|
| 976 | ret_val->data = NULL; |
---|
| 977 | else |
---|
[32] | 978 | { |
---|
[145] | 979 | if(ret_val->data_in_offset) |
---|
[32] | 980 | { |
---|
[145] | 981 | ret_val->data = regfi_parse_data(file, ret_val->type, ret_val->data_off, |
---|
| 982 | ret_val->data_size, 4, |
---|
| 983 | ret_val->data_in_offset, strict); |
---|
[31] | 984 | } |
---|
[145] | 985 | else |
---|
| 986 | { |
---|
| 987 | hbin = regfi_lookup_hbin(file, ret_val->data_off); |
---|
| 988 | if(hbin) |
---|
[111] | 989 | { |
---|
[145] | 990 | data_offset = ret_val->data_off+REGFI_REGF_SIZE; |
---|
| 991 | data_maxsize = hbin->block_size + hbin->file_off - data_offset; |
---|
| 992 | ret_val->data = regfi_parse_data(file, ret_val->type, data_offset, |
---|
| 993 | ret_val->data_size, data_maxsize, |
---|
| 994 | ret_val->data_in_offset, strict); |
---|
[111] | 995 | } |
---|
[145] | 996 | else |
---|
| 997 | { |
---|
| 998 | regfi_add_message(file, REGFI_MSG_WARN, "Could not find HBIN for data" |
---|
| 999 | " while parsing VK record at offset 0x%.8X.", |
---|
| 1000 | ret_val->offset); |
---|
| 1001 | ret_val->data = NULL; |
---|
| 1002 | } |
---|
[103] | 1003 | } |
---|
[145] | 1004 | |
---|
| 1005 | if(ret_val->data == NULL) |
---|
| 1006 | { |
---|
| 1007 | regfi_add_message(file, REGFI_MSG_WARN, "Could not parse data record" |
---|
| 1008 | " while parsing VK record at offset 0x%.8X.", |
---|
| 1009 | ret_val->offset); |
---|
| 1010 | } |
---|
[31] | 1011 | } |
---|
[30] | 1012 | |
---|
[103] | 1013 | return ret_val; |
---|
[30] | 1014 | } |
---|
| 1015 | |
---|
| 1016 | |
---|
[145] | 1017 | /****************************************************************************** |
---|
| 1018 | * If !strict, the list may contain NULLs, VK records may point to NULL. |
---|
| 1019 | ******************************************************************************/ |
---|
| 1020 | REGFI_VALUE_LIST* regfi_load_valuelist(REGFI_FILE* file, uint32 offset, |
---|
[146] | 1021 | uint32 num_values, uint32 max_size, |
---|
[145] | 1022 | bool strict) |
---|
| 1023 | { |
---|
| 1024 | uint32 usable_num_values; |
---|
[30] | 1025 | |
---|
[145] | 1026 | if((num_values+1) * sizeof(uint32) > max_size) |
---|
| 1027 | { |
---|
| 1028 | regfi_add_message(file, REGFI_MSG_WARN, "Number of values indicated by" |
---|
| 1029 | " parent key (%d) would cause cell to straddle HBIN" |
---|
| 1030 | " boundary while loading value list at offset" |
---|
| 1031 | " 0x%.8X.", num_values, offset); |
---|
| 1032 | if(strict) |
---|
| 1033 | return NULL; |
---|
| 1034 | usable_num_values = max_size/sizeof(uint32) - sizeof(uint32); |
---|
| 1035 | } |
---|
| 1036 | else |
---|
| 1037 | usable_num_values = num_values; |
---|
| 1038 | |
---|
| 1039 | return regfi_parse_valuelist(file, offset, usable_num_values, strict); |
---|
| 1040 | } |
---|
| 1041 | |
---|
| 1042 | |
---|
| 1043 | |
---|
[146] | 1044 | /****************************************************************************** |
---|
| 1045 | * |
---|
| 1046 | ******************************************************************************/ |
---|
[135] | 1047 | REGFI_NK_REC* regfi_load_key(REGFI_FILE* file, uint32 offset, bool strict) |
---|
[30] | 1048 | { |
---|
[146] | 1049 | const REGFI_HBIN* hbin; |
---|
| 1050 | const REGFI_HBIN* sub_hbin; |
---|
[135] | 1051 | REGFI_NK_REC* nk; |
---|
[105] | 1052 | uint32 max_length, off; |
---|
[99] | 1053 | |
---|
[135] | 1054 | hbin = regfi_lookup_hbin(file, offset-REGFI_REGF_SIZE); |
---|
[105] | 1055 | if (hbin == NULL) |
---|
| 1056 | return NULL; |
---|
[30] | 1057 | |
---|
[31] | 1058 | /* get the initial nk record */ |
---|
[105] | 1059 | max_length = hbin->block_size + hbin->file_off - offset; |
---|
[146] | 1060 | if((nk = regfi_parse_nk(file, offset, max_length, true)) == NULL) |
---|
[135] | 1061 | { |
---|
[138] | 1062 | regfi_add_message(file, REGFI_MSG_ERROR, "Could not load NK record at" |
---|
[137] | 1063 | " offset 0x%.8X.", offset); |
---|
[99] | 1064 | return NULL; |
---|
[135] | 1065 | } |
---|
[30] | 1066 | |
---|
[146] | 1067 | /* get value list */ |
---|
[135] | 1068 | if(nk->num_values && (nk->values_off!=REGFI_OFFSET_NONE)) |
---|
[32] | 1069 | { |
---|
[31] | 1070 | sub_hbin = hbin; |
---|
[106] | 1071 | if(!regfi_offset_in_hbin(hbin, nk->values_off)) |
---|
| 1072 | sub_hbin = regfi_lookup_hbin(file, nk->values_off); |
---|
[105] | 1073 | |
---|
| 1074 | if(sub_hbin == NULL) |
---|
[32] | 1075 | { |
---|
[105] | 1076 | if(strict) |
---|
[32] | 1077 | { |
---|
[105] | 1078 | free(nk); |
---|
[99] | 1079 | return NULL; |
---|
[31] | 1080 | } |
---|
[105] | 1081 | else |
---|
| 1082 | nk->values = NULL; |
---|
[133] | 1083 | |
---|
[31] | 1084 | } |
---|
[105] | 1085 | else |
---|
[103] | 1086 | { |
---|
[135] | 1087 | off = nk->values_off + REGFI_REGF_SIZE; |
---|
[105] | 1088 | max_length = sub_hbin->block_size + sub_hbin->file_off - off; |
---|
| 1089 | nk->values = regfi_load_valuelist(file, off, nk->num_values, max_length, |
---|
| 1090 | true); |
---|
[145] | 1091 | if(nk->values == NULL) |
---|
[105] | 1092 | { |
---|
[145] | 1093 | regfi_add_message(file, REGFI_MSG_WARN, "Could not load value list" |
---|
| 1094 | " for NK record at offset 0x%.8X.", offset); |
---|
| 1095 | if(strict) |
---|
| 1096 | { |
---|
| 1097 | free(nk); |
---|
| 1098 | return NULL; |
---|
| 1099 | } |
---|
[105] | 1100 | } |
---|
[103] | 1101 | } |
---|
[31] | 1102 | } |
---|
[105] | 1103 | |
---|
[146] | 1104 | /* now get subkey list */ |
---|
[135] | 1105 | if(nk->num_subkeys && (nk->subkeys_off != REGFI_OFFSET_NONE)) |
---|
[32] | 1106 | { |
---|
[31] | 1107 | sub_hbin = hbin; |
---|
[106] | 1108 | if(!regfi_offset_in_hbin(hbin, nk->subkeys_off)) |
---|
| 1109 | sub_hbin = regfi_lookup_hbin(file, nk->subkeys_off); |
---|
[105] | 1110 | |
---|
| 1111 | if (sub_hbin == NULL) |
---|
[32] | 1112 | { |
---|
[105] | 1113 | if(strict) |
---|
[32] | 1114 | { |
---|
[116] | 1115 | regfi_key_free(nk); |
---|
[99] | 1116 | return NULL; |
---|
[31] | 1117 | } |
---|
[105] | 1118 | else |
---|
| 1119 | nk->subkeys = NULL; |
---|
[31] | 1120 | } |
---|
[105] | 1121 | else |
---|
[104] | 1122 | { |
---|
[135] | 1123 | off = nk->subkeys_off + REGFI_REGF_SIZE; |
---|
[105] | 1124 | max_length = sub_hbin->block_size + sub_hbin->file_off - off; |
---|
[134] | 1125 | nk->subkeys = regfi_load_subkeylist(file, off, nk->num_subkeys, |
---|
[127] | 1126 | max_length, true); |
---|
[134] | 1127 | |
---|
[105] | 1128 | if(nk->subkeys == NULL) |
---|
| 1129 | { |
---|
[140] | 1130 | regfi_add_message(file, REGFI_MSG_WARN, "Could not load subkey list" |
---|
| 1131 | " while parsing NK record at offset 0x%.8X.", offset); |
---|
[105] | 1132 | nk->num_subkeys = 0; |
---|
| 1133 | } |
---|
[104] | 1134 | } |
---|
[31] | 1135 | } |
---|
[30] | 1136 | |
---|
[99] | 1137 | return nk; |
---|
[30] | 1138 | } |
---|
| 1139 | |
---|
[32] | 1140 | |
---|
[102] | 1141 | /****************************************************************************** |
---|
| 1142 | ******************************************************************************/ |
---|
[146] | 1143 | const REGFI_SK_REC* regfi_load_sk(REGFI_FILE* file, uint32 offset, bool strict) |
---|
| 1144 | { |
---|
| 1145 | REGFI_SK_REC* ret_val = NULL; |
---|
| 1146 | const REGFI_HBIN* hbin; |
---|
| 1147 | uint32 max_length; |
---|
[147] | 1148 | void* failure_ptr = NULL; |
---|
| 1149 | |
---|
[146] | 1150 | /* First look if we have already parsed it */ |
---|
| 1151 | ret_val = (REGFI_SK_REC*)lru_cache_find(file->sk_cache, &offset, 4); |
---|
| 1152 | |
---|
| 1153 | /* Bail out if we have previously cached a parse failure at this offset. */ |
---|
| 1154 | if(ret_val == (void*)REGFI_OFFSET_NONE) |
---|
| 1155 | return NULL; |
---|
| 1156 | |
---|
| 1157 | if(ret_val == NULL) |
---|
| 1158 | { |
---|
| 1159 | hbin = regfi_lookup_hbin(file, offset - REGFI_REGF_SIZE); |
---|
| 1160 | if(hbin == NULL) |
---|
| 1161 | return NULL; |
---|
| 1162 | |
---|
| 1163 | max_length = hbin->block_size + hbin->file_off - offset; |
---|
| 1164 | ret_val = regfi_parse_sk(file, offset, max_length, strict); |
---|
| 1165 | if(ret_val == NULL) |
---|
| 1166 | { /* Cache the parse failure and bail out. */ |
---|
[147] | 1167 | failure_ptr = talloc(NULL, uint32_t); |
---|
| 1168 | if(failure_ptr == NULL) |
---|
| 1169 | return NULL; |
---|
| 1170 | *(uint32_t*)failure_ptr = REGFI_OFFSET_NONE; |
---|
| 1171 | lru_cache_update(file->sk_cache, &offset, 4, failure_ptr); |
---|
[146] | 1172 | return NULL; |
---|
| 1173 | } |
---|
| 1174 | |
---|
| 1175 | lru_cache_update(file->sk_cache, &offset, 4, ret_val); |
---|
| 1176 | } |
---|
| 1177 | |
---|
| 1178 | return ret_val; |
---|
| 1179 | } |
---|
| 1180 | |
---|
| 1181 | |
---|
| 1182 | |
---|
| 1183 | /****************************************************************************** |
---|
| 1184 | ******************************************************************************/ |
---|
| 1185 | static bool regfi_find_root_nk(REGFI_FILE* file, uint32 offset,uint32 hbin_size, |
---|
[102] | 1186 | uint32* root_offset) |
---|
[30] | 1187 | { |
---|
[102] | 1188 | uint8 tmp[4]; |
---|
| 1189 | int32 record_size; |
---|
| 1190 | uint32 length, hbin_offset = 0; |
---|
[135] | 1191 | REGFI_NK_REC* nk = NULL; |
---|
[31] | 1192 | bool found = false; |
---|
[30] | 1193 | |
---|
[102] | 1194 | for(record_size=0; !found && (hbin_offset < hbin_size); ) |
---|
[32] | 1195 | { |
---|
[102] | 1196 | if(lseek(file->fd, offset+hbin_offset, SEEK_SET) == -1) |
---|
[31] | 1197 | return false; |
---|
[102] | 1198 | |
---|
| 1199 | length = 4; |
---|
| 1200 | if((regfi_read(file->fd, tmp, &length) != 0) || length != 4) |
---|
[31] | 1201 | return false; |
---|
[102] | 1202 | record_size = IVALS(tmp, 0); |
---|
[30] | 1203 | |
---|
[102] | 1204 | if(record_size < 0) |
---|
| 1205 | { |
---|
| 1206 | record_size = record_size*(-1); |
---|
| 1207 | nk = regfi_parse_nk(file, offset+hbin_offset, hbin_size-hbin_offset, true); |
---|
| 1208 | if(nk != NULL) |
---|
| 1209 | { |
---|
[135] | 1210 | if((nk->key_type == REGFI_NK_TYPE_ROOTKEY1) |
---|
| 1211 | || (nk->key_type == REGFI_NK_TYPE_ROOTKEY2)) |
---|
[102] | 1212 | { |
---|
| 1213 | found = true; |
---|
| 1214 | *root_offset = nk->offset; |
---|
| 1215 | } |
---|
| 1216 | free(nk); |
---|
| 1217 | } |
---|
[31] | 1218 | } |
---|
[30] | 1219 | |
---|
[102] | 1220 | hbin_offset += record_size; |
---|
[31] | 1221 | } |
---|
[32] | 1222 | |
---|
[102] | 1223 | return found; |
---|
[30] | 1224 | } |
---|
| 1225 | |
---|
| 1226 | |
---|
| 1227 | /******************************************************************* |
---|
[97] | 1228 | * Open the registry file and then read in the REGF block to get the |
---|
| 1229 | * first hbin offset. |
---|
| 1230 | *******************************************************************/ |
---|
[135] | 1231 | REGFI_FILE* regfi_open(const char* filename) |
---|
[30] | 1232 | { |
---|
[137] | 1233 | struct stat sbuf; |
---|
[135] | 1234 | REGFI_FILE* rb; |
---|
| 1235 | REGFI_HBIN* hbin = NULL; |
---|
[146] | 1236 | uint32 hbin_off, file_length, cache_secret; |
---|
[97] | 1237 | int fd; |
---|
[110] | 1238 | bool rla; |
---|
[30] | 1239 | |
---|
[97] | 1240 | /* open an existing file */ |
---|
[143] | 1241 | if ((fd = open(filename, REGFI_OPEN_FLAGS)) == -1) |
---|
[97] | 1242 | { |
---|
[143] | 1243 | /* fprintf(stderr, "regfi_open: failure to open %s (%s)\n", filename, strerror(errno));*/ |
---|
[31] | 1244 | return NULL; |
---|
| 1245 | } |
---|
[99] | 1246 | |
---|
[137] | 1247 | /* Determine file length. Must be at least big enough |
---|
| 1248 | * for the header and one hbin. |
---|
| 1249 | */ |
---|
| 1250 | if (fstat(fd, &sbuf) == -1) |
---|
| 1251 | return NULL; |
---|
| 1252 | file_length = sbuf.st_size; |
---|
| 1253 | if(file_length < REGFI_REGF_SIZE+REGFI_HBIN_ALLOC) |
---|
| 1254 | return NULL; |
---|
| 1255 | |
---|
[31] | 1256 | /* read in an existing file */ |
---|
[97] | 1257 | if ((rb = regfi_parse_regf(fd, true)) == NULL) |
---|
| 1258 | { |
---|
[143] | 1259 | /* fprintf(stderr, "regfi_open: Failed to read initial REGF block\n"); */ |
---|
[97] | 1260 | close(fd); |
---|
[31] | 1261 | return NULL; |
---|
| 1262 | } |
---|
[137] | 1263 | rb->file_length = file_length; |
---|
| 1264 | |
---|
[99] | 1265 | rb->hbins = range_list_new(); |
---|
[110] | 1266 | if(rb->hbins == NULL) |
---|
[99] | 1267 | { |
---|
[143] | 1268 | /* fprintf(stderr, "regfi_open: Failed to create HBIN list.\n"); */ |
---|
[106] | 1269 | range_list_free(rb->hbins); |
---|
[99] | 1270 | close(fd); |
---|
| 1271 | free(rb); |
---|
| 1272 | return NULL; |
---|
| 1273 | } |
---|
[108] | 1274 | |
---|
[106] | 1275 | rla = true; |
---|
[135] | 1276 | hbin_off = REGFI_REGF_SIZE; |
---|
[110] | 1277 | hbin = regfi_parse_hbin(rb, hbin_off, true); |
---|
[106] | 1278 | while(hbin && rla) |
---|
| 1279 | { |
---|
[137] | 1280 | rla = range_list_add(rb->hbins, hbin->file_off, hbin->block_size, hbin); |
---|
[148] | 1281 | if(rla) |
---|
| 1282 | talloc_steal(rb->hbins, hbin); |
---|
[106] | 1283 | hbin_off = hbin->file_off + hbin->block_size; |
---|
[110] | 1284 | hbin = regfi_parse_hbin(rb, hbin_off, true); |
---|
[106] | 1285 | } |
---|
| 1286 | |
---|
[146] | 1287 | /* This secret isn't very secret, but we don't need a good one. This |
---|
| 1288 | * secret is just designed to prevent someone from trying to blow our |
---|
| 1289 | * caching and make things slow. |
---|
| 1290 | */ |
---|
| 1291 | cache_secret = 0x15DEAD05^time(NULL)^(getpid()<<16); |
---|
| 1292 | |
---|
| 1293 | /* Cache an unlimited number of SK records. Typically there are very few. */ |
---|
[147] | 1294 | rb->sk_cache = lru_cache_create_ctx(NULL, 0, cache_secret, true); |
---|
[146] | 1295 | |
---|
[138] | 1296 | /* Default message mask */ |
---|
| 1297 | rb->msg_mask = REGFI_MSG_ERROR|REGFI_MSG_WARN; |
---|
| 1298 | |
---|
[31] | 1299 | /* success */ |
---|
| 1300 | return rb; |
---|
[30] | 1301 | } |
---|
| 1302 | |
---|
| 1303 | |
---|
[148] | 1304 | /****************************************************************************** |
---|
| 1305 | ******************************************************************************/ |
---|
[146] | 1306 | int regfi_close(REGFI_FILE *file) |
---|
[30] | 1307 | { |
---|
[31] | 1308 | int fd; |
---|
[30] | 1309 | |
---|
[31] | 1310 | /* nothing to do if there is no open file */ |
---|
[99] | 1311 | if ((file == NULL) || (file->fd == -1)) |
---|
| 1312 | return 0; |
---|
[30] | 1313 | |
---|
[31] | 1314 | fd = file->fd; |
---|
| 1315 | file->fd = -1; |
---|
[148] | 1316 | |
---|
[99] | 1317 | range_list_free(file->hbins); |
---|
[106] | 1318 | |
---|
[146] | 1319 | if(file->sk_cache != NULL) |
---|
| 1320 | lru_cache_destroy(file->sk_cache); |
---|
[148] | 1321 | |
---|
[99] | 1322 | free(file); |
---|
[106] | 1323 | return close(fd); |
---|
[30] | 1324 | } |
---|
| 1325 | |
---|
| 1326 | |
---|
[80] | 1327 | /****************************************************************************** |
---|
| 1328 | * There should be only *one* root key in the registry file based |
---|
| 1329 | * on my experience. --jerry |
---|
[148] | 1330 | ******************************************************************************/ |
---|
[135] | 1331 | REGFI_NK_REC* regfi_rootkey(REGFI_FILE *file) |
---|
[30] | 1332 | { |
---|
[135] | 1333 | REGFI_NK_REC* nk = NULL; |
---|
[146] | 1334 | REGFI_HBIN* hbin; |
---|
| 1335 | uint32 root_offset, i, num_hbins; |
---|
[99] | 1336 | |
---|
| 1337 | if(!file) |
---|
[31] | 1338 | return NULL; |
---|
[99] | 1339 | |
---|
[102] | 1340 | /* Scan through the file one HBIN block at a time looking |
---|
[146] | 1341 | * for an NK record with a root key type. |
---|
| 1342 | * This is typically the first NK record in the first HBIN |
---|
| 1343 | * block (but we're not assuming that generally). |
---|
| 1344 | */ |
---|
[107] | 1345 | num_hbins = range_list_size(file->hbins); |
---|
| 1346 | for(i=0; i < num_hbins; i++) |
---|
[99] | 1347 | { |
---|
[135] | 1348 | hbin = (REGFI_HBIN*)range_list_get(file->hbins, i)->data; |
---|
| 1349 | if(regfi_find_root_nk(file, hbin->file_off+REGFI_HBIN_HEADER_SIZE, |
---|
| 1350 | hbin->block_size-REGFI_HBIN_HEADER_SIZE, &root_offset)) |
---|
[99] | 1351 | { |
---|
[105] | 1352 | nk = regfi_load_key(file, root_offset, true); |
---|
[102] | 1353 | break; |
---|
[31] | 1354 | } |
---|
| 1355 | } |
---|
[30] | 1356 | |
---|
[80] | 1357 | return nk; |
---|
[30] | 1358 | } |
---|
| 1359 | |
---|
| 1360 | |
---|
[80] | 1361 | /****************************************************************************** |
---|
| 1362 | *****************************************************************************/ |
---|
[135] | 1363 | void regfi_key_free(REGFI_NK_REC* nk) |
---|
[30] | 1364 | { |
---|
[135] | 1365 | if((nk->values != NULL) && (nk->values_off!=REGFI_OFFSET_NONE)) |
---|
[80] | 1366 | { |
---|
[145] | 1367 | if(nk->values->elements != NULL) |
---|
| 1368 | free(nk->values->elements); |
---|
[80] | 1369 | free(nk->values); |
---|
| 1370 | } |
---|
[30] | 1371 | |
---|
[127] | 1372 | regfi_subkeylist_free(nk->subkeys); |
---|
| 1373 | |
---|
[80] | 1374 | if(nk->keyname != NULL) |
---|
| 1375 | free(nk->keyname); |
---|
| 1376 | if(nk->classname != NULL) |
---|
| 1377 | free(nk->classname); |
---|
| 1378 | |
---|
| 1379 | /* XXX: not freeing sec_desc because these are cached. This needs to be reviewed. */ |
---|
| 1380 | free(nk); |
---|
| 1381 | } |
---|
| 1382 | |
---|
| 1383 | |
---|
| 1384 | /****************************************************************************** |
---|
| 1385 | *****************************************************************************/ |
---|
[135] | 1386 | void regfi_subkeylist_free(REGFI_SUBKEY_LIST* list) |
---|
[127] | 1387 | { |
---|
| 1388 | if(list != NULL) |
---|
| 1389 | { |
---|
| 1390 | free(list->elements); |
---|
| 1391 | free(list); |
---|
| 1392 | } |
---|
| 1393 | } |
---|
| 1394 | |
---|
| 1395 | |
---|
| 1396 | /****************************************************************************** |
---|
| 1397 | *****************************************************************************/ |
---|
[135] | 1398 | REGFI_ITERATOR* regfi_iterator_new(REGFI_FILE* fh) |
---|
[80] | 1399 | { |
---|
[135] | 1400 | REGFI_NK_REC* root; |
---|
[80] | 1401 | REGFI_ITERATOR* ret_val = (REGFI_ITERATOR*)malloc(sizeof(REGFI_ITERATOR)); |
---|
| 1402 | if(ret_val == NULL) |
---|
| 1403 | return NULL; |
---|
| 1404 | |
---|
[81] | 1405 | root = regfi_rootkey(fh); |
---|
[80] | 1406 | if(root == NULL) |
---|
| 1407 | { |
---|
| 1408 | free(ret_val); |
---|
| 1409 | return NULL; |
---|
| 1410 | } |
---|
| 1411 | |
---|
[135] | 1412 | ret_val->key_positions = void_stack_new(REGFI_MAX_DEPTH); |
---|
[80] | 1413 | if(ret_val->key_positions == NULL) |
---|
| 1414 | { |
---|
| 1415 | free(ret_val); |
---|
| 1416 | return NULL; |
---|
| 1417 | } |
---|
| 1418 | |
---|
| 1419 | ret_val->f = fh; |
---|
| 1420 | ret_val->cur_key = root; |
---|
| 1421 | ret_val->cur_subkey = 0; |
---|
| 1422 | ret_val->cur_value = 0; |
---|
| 1423 | |
---|
| 1424 | return ret_val; |
---|
| 1425 | } |
---|
| 1426 | |
---|
| 1427 | |
---|
| 1428 | /****************************************************************************** |
---|
| 1429 | *****************************************************************************/ |
---|
| 1430 | void regfi_iterator_free(REGFI_ITERATOR* i) |
---|
| 1431 | { |
---|
| 1432 | REGFI_ITER_POSITION* cur; |
---|
| 1433 | |
---|
| 1434 | if(i->cur_key != NULL) |
---|
| 1435 | regfi_key_free(i->cur_key); |
---|
| 1436 | |
---|
| 1437 | while((cur = (REGFI_ITER_POSITION*)void_stack_pop(i->key_positions)) != NULL) |
---|
| 1438 | { |
---|
| 1439 | regfi_key_free(cur->nk); |
---|
| 1440 | free(cur); |
---|
| 1441 | } |
---|
| 1442 | |
---|
[146] | 1443 | void_stack_free(i->key_positions); |
---|
[80] | 1444 | free(i); |
---|
| 1445 | } |
---|
| 1446 | |
---|
| 1447 | |
---|
| 1448 | |
---|
| 1449 | /****************************************************************************** |
---|
| 1450 | *****************************************************************************/ |
---|
| 1451 | /* XXX: some way of indicating reason for failure should be added. */ |
---|
| 1452 | bool regfi_iterator_down(REGFI_ITERATOR* i) |
---|
| 1453 | { |
---|
[135] | 1454 | REGFI_NK_REC* subkey; |
---|
[80] | 1455 | REGFI_ITER_POSITION* pos; |
---|
| 1456 | |
---|
| 1457 | pos = (REGFI_ITER_POSITION*)malloc(sizeof(REGFI_ITER_POSITION)); |
---|
| 1458 | if(pos == NULL) |
---|
| 1459 | return false; |
---|
| 1460 | |
---|
[135] | 1461 | subkey = (REGFI_NK_REC*)regfi_iterator_cur_subkey(i); |
---|
[80] | 1462 | if(subkey == NULL) |
---|
| 1463 | { |
---|
| 1464 | free(pos); |
---|
| 1465 | return false; |
---|
| 1466 | } |
---|
| 1467 | |
---|
| 1468 | pos->nk = i->cur_key; |
---|
| 1469 | pos->cur_subkey = i->cur_subkey; |
---|
| 1470 | if(!void_stack_push(i->key_positions, pos)) |
---|
| 1471 | { |
---|
| 1472 | free(pos); |
---|
| 1473 | regfi_key_free(subkey); |
---|
| 1474 | return false; |
---|
| 1475 | } |
---|
| 1476 | |
---|
| 1477 | i->cur_key = subkey; |
---|
| 1478 | i->cur_subkey = 0; |
---|
| 1479 | i->cur_value = 0; |
---|
| 1480 | |
---|
| 1481 | return true; |
---|
| 1482 | } |
---|
| 1483 | |
---|
| 1484 | |
---|
| 1485 | /****************************************************************************** |
---|
| 1486 | *****************************************************************************/ |
---|
| 1487 | bool regfi_iterator_up(REGFI_ITERATOR* i) |
---|
| 1488 | { |
---|
| 1489 | REGFI_ITER_POSITION* pos; |
---|
| 1490 | |
---|
| 1491 | pos = (REGFI_ITER_POSITION*)void_stack_pop(i->key_positions); |
---|
| 1492 | if(pos == NULL) |
---|
| 1493 | return false; |
---|
| 1494 | |
---|
| 1495 | regfi_key_free(i->cur_key); |
---|
| 1496 | i->cur_key = pos->nk; |
---|
| 1497 | i->cur_subkey = pos->cur_subkey; |
---|
| 1498 | i->cur_value = 0; |
---|
| 1499 | free(pos); |
---|
| 1500 | |
---|
| 1501 | return true; |
---|
| 1502 | } |
---|
| 1503 | |
---|
| 1504 | |
---|
| 1505 | /****************************************************************************** |
---|
| 1506 | *****************************************************************************/ |
---|
| 1507 | bool regfi_iterator_to_root(REGFI_ITERATOR* i) |
---|
| 1508 | { |
---|
| 1509 | while(regfi_iterator_up(i)) |
---|
| 1510 | continue; |
---|
| 1511 | |
---|
| 1512 | return true; |
---|
| 1513 | } |
---|
| 1514 | |
---|
| 1515 | |
---|
| 1516 | /****************************************************************************** |
---|
| 1517 | *****************************************************************************/ |
---|
| 1518 | bool regfi_iterator_find_subkey(REGFI_ITERATOR* i, const char* subkey_name) |
---|
| 1519 | { |
---|
[135] | 1520 | REGFI_NK_REC* subkey; |
---|
[80] | 1521 | bool found = false; |
---|
| 1522 | uint32 old_subkey = i->cur_subkey; |
---|
[133] | 1523 | |
---|
[80] | 1524 | if(subkey_name == NULL) |
---|
| 1525 | return false; |
---|
| 1526 | |
---|
| 1527 | /* XXX: this alloc/free of each sub key might be a bit excessive */ |
---|
[135] | 1528 | subkey = (REGFI_NK_REC*)regfi_iterator_first_subkey(i); |
---|
[80] | 1529 | while((subkey != NULL) && (found == false)) |
---|
| 1530 | { |
---|
| 1531 | if(subkey->keyname != NULL |
---|
| 1532 | && strcasecmp(subkey->keyname, subkey_name) == 0) |
---|
| 1533 | found = true; |
---|
[82] | 1534 | else |
---|
| 1535 | { |
---|
| 1536 | regfi_key_free(subkey); |
---|
[135] | 1537 | subkey = (REGFI_NK_REC*)regfi_iterator_next_subkey(i); |
---|
[82] | 1538 | } |
---|
[80] | 1539 | } |
---|
| 1540 | |
---|
| 1541 | if(found == false) |
---|
| 1542 | { |
---|
| 1543 | i->cur_subkey = old_subkey; |
---|
| 1544 | return false; |
---|
| 1545 | } |
---|
| 1546 | |
---|
[82] | 1547 | regfi_key_free(subkey); |
---|
[80] | 1548 | return true; |
---|
| 1549 | } |
---|
| 1550 | |
---|
| 1551 | |
---|
| 1552 | /****************************************************************************** |
---|
| 1553 | *****************************************************************************/ |
---|
| 1554 | bool regfi_iterator_walk_path(REGFI_ITERATOR* i, const char** path) |
---|
| 1555 | { |
---|
| 1556 | uint32 x; |
---|
| 1557 | if(path == NULL) |
---|
| 1558 | return false; |
---|
| 1559 | |
---|
| 1560 | for(x=0; |
---|
| 1561 | ((path[x] != NULL) && regfi_iterator_find_subkey(i, path[x]) |
---|
| 1562 | && regfi_iterator_down(i)); |
---|
| 1563 | x++) |
---|
| 1564 | { continue; } |
---|
| 1565 | |
---|
| 1566 | if(path[x] == NULL) |
---|
| 1567 | return true; |
---|
| 1568 | |
---|
| 1569 | /* XXX: is this the right number of times? */ |
---|
| 1570 | for(; x > 0; x--) |
---|
| 1571 | regfi_iterator_up(i); |
---|
| 1572 | |
---|
| 1573 | return false; |
---|
| 1574 | } |
---|
| 1575 | |
---|
| 1576 | |
---|
| 1577 | /****************************************************************************** |
---|
| 1578 | *****************************************************************************/ |
---|
[135] | 1579 | const REGFI_NK_REC* regfi_iterator_cur_key(REGFI_ITERATOR* i) |
---|
[80] | 1580 | { |
---|
| 1581 | return i->cur_key; |
---|
| 1582 | } |
---|
| 1583 | |
---|
| 1584 | |
---|
| 1585 | /****************************************************************************** |
---|
| 1586 | *****************************************************************************/ |
---|
[135] | 1587 | const REGFI_SK_REC* regfi_iterator_cur_sk(REGFI_ITERATOR* i) |
---|
[109] | 1588 | { |
---|
[146] | 1589 | if(i->cur_key == NULL || i->cur_key->sk_off == REGFI_OFFSET_NONE) |
---|
[109] | 1590 | return NULL; |
---|
| 1591 | |
---|
[146] | 1592 | return regfi_load_sk(i->f, i->cur_key->sk_off + REGFI_REGF_SIZE, true); |
---|
[109] | 1593 | } |
---|
| 1594 | |
---|
| 1595 | |
---|
| 1596 | /****************************************************************************** |
---|
| 1597 | *****************************************************************************/ |
---|
[135] | 1598 | const REGFI_NK_REC* regfi_iterator_first_subkey(REGFI_ITERATOR* i) |
---|
[80] | 1599 | { |
---|
| 1600 | i->cur_subkey = 0; |
---|
| 1601 | return regfi_iterator_cur_subkey(i); |
---|
| 1602 | } |
---|
| 1603 | |
---|
| 1604 | |
---|
| 1605 | /****************************************************************************** |
---|
| 1606 | *****************************************************************************/ |
---|
[135] | 1607 | const REGFI_NK_REC* regfi_iterator_cur_subkey(REGFI_ITERATOR* i) |
---|
[80] | 1608 | { |
---|
| 1609 | uint32 nk_offset; |
---|
| 1610 | |
---|
[31] | 1611 | /* see if there is anything left to report */ |
---|
[135] | 1612 | if (!(i->cur_key) || (i->cur_key->subkeys_off==REGFI_OFFSET_NONE) |
---|
[80] | 1613 | || (i->cur_subkey >= i->cur_key->num_subkeys)) |
---|
[31] | 1614 | return NULL; |
---|
[30] | 1615 | |
---|
[139] | 1616 | nk_offset = i->cur_key->subkeys->elements[i->cur_subkey].offset; |
---|
[133] | 1617 | |
---|
[135] | 1618 | return regfi_load_key(i->f, nk_offset+REGFI_REGF_SIZE, true); |
---|
[30] | 1619 | } |
---|
[80] | 1620 | |
---|
| 1621 | |
---|
| 1622 | /****************************************************************************** |
---|
| 1623 | *****************************************************************************/ |
---|
| 1624 | /* XXX: some way of indicating reason for failure should be added. */ |
---|
[135] | 1625 | const REGFI_NK_REC* regfi_iterator_next_subkey(REGFI_ITERATOR* i) |
---|
[80] | 1626 | { |
---|
[135] | 1627 | const REGFI_NK_REC* subkey; |
---|
[80] | 1628 | |
---|
| 1629 | i->cur_subkey++; |
---|
| 1630 | subkey = regfi_iterator_cur_subkey(i); |
---|
| 1631 | |
---|
| 1632 | if(subkey == NULL) |
---|
| 1633 | i->cur_subkey--; |
---|
| 1634 | |
---|
| 1635 | return subkey; |
---|
| 1636 | } |
---|
| 1637 | |
---|
| 1638 | |
---|
| 1639 | /****************************************************************************** |
---|
| 1640 | *****************************************************************************/ |
---|
| 1641 | bool regfi_iterator_find_value(REGFI_ITERATOR* i, const char* value_name) |
---|
| 1642 | { |
---|
[135] | 1643 | const REGFI_VK_REC* cur; |
---|
[80] | 1644 | bool found = false; |
---|
| 1645 | |
---|
| 1646 | /* XXX: cur->valuename can be NULL in the registry. |
---|
| 1647 | * Should we allow for a way to search for that? |
---|
| 1648 | */ |
---|
| 1649 | if(value_name == NULL) |
---|
| 1650 | return false; |
---|
| 1651 | |
---|
| 1652 | cur = regfi_iterator_first_value(i); |
---|
| 1653 | while((cur != NULL) && (found == false)) |
---|
| 1654 | { |
---|
| 1655 | if((cur->valuename != NULL) |
---|
| 1656 | && (strcasecmp(cur->valuename, value_name) == 0)) |
---|
| 1657 | found = true; |
---|
[95] | 1658 | else |
---|
| 1659 | cur = regfi_iterator_next_value(i); |
---|
[80] | 1660 | } |
---|
| 1661 | |
---|
[94] | 1662 | return found; |
---|
[80] | 1663 | } |
---|
| 1664 | |
---|
| 1665 | |
---|
| 1666 | /****************************************************************************** |
---|
| 1667 | *****************************************************************************/ |
---|
[135] | 1668 | const REGFI_VK_REC* regfi_iterator_first_value(REGFI_ITERATOR* i) |
---|
[80] | 1669 | { |
---|
| 1670 | i->cur_value = 0; |
---|
| 1671 | return regfi_iterator_cur_value(i); |
---|
| 1672 | } |
---|
| 1673 | |
---|
| 1674 | |
---|
| 1675 | /****************************************************************************** |
---|
| 1676 | *****************************************************************************/ |
---|
[135] | 1677 | const REGFI_VK_REC* regfi_iterator_cur_value(REGFI_ITERATOR* i) |
---|
[80] | 1678 | { |
---|
[146] | 1679 | const REGFI_VK_REC* ret_val = NULL; |
---|
[145] | 1680 | uint32 voffset; |
---|
[80] | 1681 | |
---|
[145] | 1682 | if(i->cur_key->values != NULL && i->cur_key->values->elements != NULL) |
---|
| 1683 | { |
---|
| 1684 | if(i->cur_value < i->cur_key->values->num_values) |
---|
| 1685 | { |
---|
| 1686 | voffset = i->cur_key->values->elements[i->cur_value]; |
---|
| 1687 | ret_val = regfi_load_value(i->f, voffset+REGFI_REGF_SIZE, true); |
---|
| 1688 | } |
---|
| 1689 | } |
---|
| 1690 | |
---|
[80] | 1691 | return ret_val; |
---|
| 1692 | } |
---|
| 1693 | |
---|
| 1694 | |
---|
| 1695 | /****************************************************************************** |
---|
| 1696 | *****************************************************************************/ |
---|
[135] | 1697 | const REGFI_VK_REC* regfi_iterator_next_value(REGFI_ITERATOR* i) |
---|
[80] | 1698 | { |
---|
[135] | 1699 | const REGFI_VK_REC* ret_val; |
---|
[80] | 1700 | |
---|
| 1701 | i->cur_value++; |
---|
| 1702 | ret_val = regfi_iterator_cur_value(i); |
---|
| 1703 | if(ret_val == NULL) |
---|
| 1704 | i->cur_value--; |
---|
| 1705 | |
---|
| 1706 | return ret_val; |
---|
| 1707 | } |
---|
[97] | 1708 | |
---|
| 1709 | |
---|
| 1710 | /******************************************************************* |
---|
| 1711 | * Computes the checksum of the registry file header. |
---|
| 1712 | * buffer must be at least the size of an regf header (4096 bytes). |
---|
| 1713 | *******************************************************************/ |
---|
| 1714 | static uint32 regfi_compute_header_checksum(uint8* buffer) |
---|
| 1715 | { |
---|
| 1716 | uint32 checksum, x; |
---|
| 1717 | int i; |
---|
| 1718 | |
---|
| 1719 | /* XOR of all bytes 0x0000 - 0x01FB */ |
---|
| 1720 | |
---|
| 1721 | checksum = x = 0; |
---|
| 1722 | |
---|
| 1723 | for ( i=0; i<0x01FB; i+=4 ) { |
---|
| 1724 | x = IVAL(buffer, i ); |
---|
| 1725 | checksum ^= x; |
---|
| 1726 | } |
---|
| 1727 | |
---|
| 1728 | return checksum; |
---|
| 1729 | } |
---|
| 1730 | |
---|
| 1731 | |
---|
| 1732 | /******************************************************************* |
---|
[116] | 1733 | * XXX: Add way to return more detailed error information. |
---|
[97] | 1734 | *******************************************************************/ |
---|
[135] | 1735 | REGFI_FILE* regfi_parse_regf(int fd, bool strict) |
---|
[97] | 1736 | { |
---|
[135] | 1737 | uint8 file_header[REGFI_REGF_SIZE]; |
---|
[102] | 1738 | uint32 length; |
---|
[135] | 1739 | REGFI_FILE* ret_val; |
---|
[97] | 1740 | |
---|
[135] | 1741 | ret_val = (REGFI_FILE*)zalloc(sizeof(REGFI_FILE)); |
---|
[97] | 1742 | if(ret_val == NULL) |
---|
| 1743 | return NULL; |
---|
| 1744 | |
---|
| 1745 | ret_val->fd = fd; |
---|
| 1746 | |
---|
[135] | 1747 | length = REGFI_REGF_SIZE; |
---|
[102] | 1748 | if((regfi_read(fd, file_header, &length)) != 0 |
---|
[135] | 1749 | || length != REGFI_REGF_SIZE) |
---|
[97] | 1750 | { |
---|
| 1751 | free(ret_val); |
---|
| 1752 | return NULL; |
---|
| 1753 | } |
---|
| 1754 | |
---|
| 1755 | ret_val->checksum = IVAL(file_header, 0x1FC); |
---|
| 1756 | ret_val->computed_checksum = regfi_compute_header_checksum(file_header); |
---|
| 1757 | if (strict && (ret_val->checksum != ret_val->computed_checksum)) |
---|
| 1758 | { |
---|
| 1759 | free(ret_val); |
---|
| 1760 | return NULL; |
---|
| 1761 | } |
---|
| 1762 | |
---|
[135] | 1763 | memcpy(ret_val->magic, file_header, REGFI_REGF_MAGIC_SIZE); |
---|
| 1764 | if(strict && (memcmp(ret_val->magic, "regf", REGFI_REGF_MAGIC_SIZE) != 0)) |
---|
[97] | 1765 | { |
---|
| 1766 | free(ret_val); |
---|
| 1767 | return NULL; |
---|
| 1768 | } |
---|
| 1769 | |
---|
| 1770 | ret_val->unknown1 = IVAL(file_header, 0x4); |
---|
| 1771 | ret_val->unknown2 = IVAL(file_header, 0x8); |
---|
| 1772 | |
---|
| 1773 | ret_val->mtime.low = IVAL(file_header, 0xC); |
---|
| 1774 | ret_val->mtime.high = IVAL(file_header, 0x10); |
---|
| 1775 | |
---|
| 1776 | ret_val->unknown3 = IVAL(file_header, 0x14); |
---|
| 1777 | ret_val->unknown4 = IVAL(file_header, 0x18); |
---|
| 1778 | ret_val->unknown5 = IVAL(file_header, 0x1C); |
---|
| 1779 | ret_val->unknown6 = IVAL(file_header, 0x20); |
---|
| 1780 | |
---|
| 1781 | ret_val->data_offset = IVAL(file_header, 0x24); |
---|
| 1782 | ret_val->last_block = IVAL(file_header, 0x28); |
---|
| 1783 | |
---|
| 1784 | ret_val->unknown7 = IVAL(file_header, 0x2C); |
---|
| 1785 | |
---|
| 1786 | return ret_val; |
---|
| 1787 | } |
---|
| 1788 | |
---|
| 1789 | |
---|
| 1790 | |
---|
[148] | 1791 | /****************************************************************************** |
---|
[97] | 1792 | * Given real file offset, read and parse the hbin at that location |
---|
[110] | 1793 | * along with it's associated cells. |
---|
[148] | 1794 | ******************************************************************************/ |
---|
[135] | 1795 | REGFI_HBIN* regfi_parse_hbin(REGFI_FILE* file, uint32 offset, bool strict) |
---|
[97] | 1796 | { |
---|
[135] | 1797 | REGFI_HBIN *hbin; |
---|
| 1798 | uint8 hbin_header[REGFI_HBIN_HEADER_SIZE]; |
---|
[110] | 1799 | uint32 length; |
---|
[99] | 1800 | |
---|
| 1801 | if(offset >= file->file_length) |
---|
| 1802 | return NULL; |
---|
[97] | 1803 | |
---|
| 1804 | if(lseek(file->fd, offset, SEEK_SET) == -1) |
---|
[137] | 1805 | { |
---|
[138] | 1806 | regfi_add_message(file, REGFI_MSG_ERROR, "Seek failed" |
---|
[137] | 1807 | " while parsing hbin at offset 0x%.8X.", offset); |
---|
[97] | 1808 | return NULL; |
---|
[137] | 1809 | } |
---|
[97] | 1810 | |
---|
[135] | 1811 | length = REGFI_HBIN_HEADER_SIZE; |
---|
[97] | 1812 | if((regfi_read(file->fd, hbin_header, &length) != 0) |
---|
[135] | 1813 | || length != REGFI_HBIN_HEADER_SIZE) |
---|
[97] | 1814 | return NULL; |
---|
| 1815 | |
---|
| 1816 | if(lseek(file->fd, offset, SEEK_SET) == -1) |
---|
[137] | 1817 | { |
---|
[138] | 1818 | regfi_add_message(file, REGFI_MSG_ERROR, "Seek failed" |
---|
[137] | 1819 | " while parsing hbin at offset 0x%.8X.", offset); |
---|
[97] | 1820 | return NULL; |
---|
[137] | 1821 | } |
---|
[97] | 1822 | |
---|
[148] | 1823 | hbin = talloc(NULL, REGFI_HBIN); |
---|
| 1824 | if(hbin == NULL) |
---|
[99] | 1825 | return NULL; |
---|
| 1826 | hbin->file_off = offset; |
---|
| 1827 | |
---|
[97] | 1828 | memcpy(hbin->magic, hbin_header, 4); |
---|
| 1829 | if(strict && (memcmp(hbin->magic, "hbin", 4) != 0)) |
---|
[99] | 1830 | { |
---|
[138] | 1831 | regfi_add_message(file, REGFI_MSG_INFO, "Magic number mismatch " |
---|
| 1832 | "(%.2X %.2X %.2X %.2X) while parsing hbin at offset" |
---|
| 1833 | " 0x%.8X.", hbin->magic[0], hbin->magic[1], |
---|
| 1834 | hbin->magic[2], hbin->magic[3], offset); |
---|
[148] | 1835 | talloc_free(hbin); |
---|
[97] | 1836 | return NULL; |
---|
[99] | 1837 | } |
---|
[97] | 1838 | |
---|
| 1839 | hbin->first_hbin_off = IVAL(hbin_header, 0x4); |
---|
| 1840 | hbin->block_size = IVAL(hbin_header, 0x8); |
---|
| 1841 | /* this should be the same thing as hbin->block_size but just in case */ |
---|
| 1842 | hbin->next_block = IVAL(hbin_header, 0x1C); |
---|
| 1843 | |
---|
| 1844 | |
---|
| 1845 | /* Ensure the block size is a multiple of 0x1000 and doesn't run off |
---|
| 1846 | * the end of the file. |
---|
| 1847 | */ |
---|
[116] | 1848 | /* XXX: This may need to be relaxed for dealing with |
---|
| 1849 | * partial or corrupt files. |
---|
| 1850 | */ |
---|
[97] | 1851 | if((offset + hbin->block_size > file->file_length) |
---|
| 1852 | || (hbin->block_size & 0xFFFFF000) != hbin->block_size) |
---|
[99] | 1853 | { |
---|
[138] | 1854 | regfi_add_message(file, REGFI_MSG_ERROR, "The hbin offset is not aligned" |
---|
[137] | 1855 | " or runs off the end of the file" |
---|
| 1856 | " while parsing hbin at offset 0x%.8X.", offset); |
---|
[148] | 1857 | talloc_free(hbin); |
---|
[97] | 1858 | return NULL; |
---|
[99] | 1859 | } |
---|
[97] | 1860 | |
---|
| 1861 | return hbin; |
---|
| 1862 | } |
---|
| 1863 | |
---|
| 1864 | |
---|
[126] | 1865 | /******************************************************************* |
---|
| 1866 | *******************************************************************/ |
---|
[135] | 1867 | REGFI_NK_REC* regfi_parse_nk(REGFI_FILE* file, uint32 offset, |
---|
[99] | 1868 | uint32 max_size, bool strict) |
---|
| 1869 | { |
---|
| 1870 | uint8 nk_header[REGFI_NK_MIN_LENGTH]; |
---|
[146] | 1871 | const REGFI_HBIN *hbin; |
---|
[135] | 1872 | REGFI_NK_REC* ret_val; |
---|
[131] | 1873 | uint32 length,cell_length; |
---|
| 1874 | uint32 class_offset, class_maxsize; |
---|
[101] | 1875 | bool unalloc = false; |
---|
[99] | 1876 | |
---|
[101] | 1877 | if(!regfi_parse_cell(file->fd, offset, nk_header, REGFI_NK_MIN_LENGTH, |
---|
| 1878 | &cell_length, &unalloc)) |
---|
[137] | 1879 | { |
---|
[138] | 1880 | regfi_add_message(file, REGFI_MSG_WARN, "Could not parse cell header" |
---|
[137] | 1881 | " while parsing NK record at offset 0x%.8X.", offset); |
---|
| 1882 | return NULL; |
---|
| 1883 | } |
---|
| 1884 | |
---|
[99] | 1885 | /* A bit of validation before bothering to allocate memory */ |
---|
[101] | 1886 | if((nk_header[0x0] != 'n') || (nk_header[0x1] != 'k')) |
---|
[135] | 1887 | { |
---|
[138] | 1888 | regfi_add_message(file, REGFI_MSG_WARN, "Magic number mismatch in parsing" |
---|
| 1889 | " NK record at offset 0x%.8X.", offset); |
---|
[99] | 1890 | return NULL; |
---|
[135] | 1891 | } |
---|
[99] | 1892 | |
---|
[135] | 1893 | ret_val = (REGFI_NK_REC*)zalloc(sizeof(REGFI_NK_REC)); |
---|
[99] | 1894 | if(ret_val == NULL) |
---|
[135] | 1895 | { |
---|
[138] | 1896 | regfi_add_message(file, REGFI_MSG_ERROR, "Failed to allocate memory while" |
---|
[137] | 1897 | " parsing NK record at offset 0x%.8X.", offset); |
---|
[99] | 1898 | return NULL; |
---|
[135] | 1899 | } |
---|
[99] | 1900 | |
---|
| 1901 | ret_val->offset = offset; |
---|
[101] | 1902 | ret_val->cell_size = cell_length; |
---|
| 1903 | |
---|
[99] | 1904 | if(ret_val->cell_size > max_size) |
---|
| 1905 | ret_val->cell_size = max_size & 0xFFFFFFF8; |
---|
| 1906 | if((ret_val->cell_size < REGFI_NK_MIN_LENGTH) |
---|
| 1907 | || (strict && ret_val->cell_size != (ret_val->cell_size & 0xFFFFFFF8))) |
---|
| 1908 | { |
---|
[140] | 1909 | regfi_add_message(file, REGFI_MSG_WARN, "A length check failed while" |
---|
[138] | 1910 | " parsing NK record at offset 0x%.8X.", offset); |
---|
[99] | 1911 | free(ret_val); |
---|
| 1912 | return NULL; |
---|
| 1913 | } |
---|
| 1914 | |
---|
[101] | 1915 | ret_val->magic[0] = nk_header[0x0]; |
---|
| 1916 | ret_val->magic[1] = nk_header[0x1]; |
---|
| 1917 | ret_val->key_type = SVAL(nk_header, 0x2); |
---|
[135] | 1918 | if((ret_val->key_type != REGFI_NK_TYPE_NORMALKEY) |
---|
| 1919 | && (ret_val->key_type != REGFI_NK_TYPE_ROOTKEY1) |
---|
| 1920 | && (ret_val->key_type != REGFI_NK_TYPE_ROOTKEY2) |
---|
| 1921 | && (ret_val->key_type != REGFI_NK_TYPE_LINKKEY) |
---|
[137] | 1922 | && (ret_val->key_type != REGFI_NK_TYPE_UNKNOWN1) |
---|
| 1923 | && (ret_val->key_type != REGFI_NK_TYPE_UNKNOWN2) |
---|
| 1924 | && (ret_val->key_type != REGFI_NK_TYPE_UNKNOWN3)) |
---|
[99] | 1925 | { |
---|
[138] | 1926 | regfi_add_message(file, REGFI_MSG_WARN, "Unknown key type (0x%.4X) while" |
---|
| 1927 | " parsing NK record at offset 0x%.8X.", |
---|
| 1928 | ret_val->key_type, offset); |
---|
[99] | 1929 | } |
---|
[101] | 1930 | |
---|
| 1931 | ret_val->mtime.low = IVAL(nk_header, 0x4); |
---|
| 1932 | ret_val->mtime.high = IVAL(nk_header, 0x8); |
---|
[116] | 1933 | /* If the key is unallocated and the MTIME is earlier than Jan 1, 1990 |
---|
| 1934 | * or later than Jan 1, 2290, we consider this a bad key. This helps |
---|
| 1935 | * weed out some false positives during deleted data recovery. |
---|
| 1936 | */ |
---|
| 1937 | if(unalloc |
---|
| 1938 | && ((ret_val->mtime.high < REGFI_MTIME_MIN_HIGH |
---|
| 1939 | && ret_val->mtime.low < REGFI_MTIME_MIN_LOW) |
---|
| 1940 | || (ret_val->mtime.high > REGFI_MTIME_MAX_HIGH |
---|
| 1941 | && ret_val->mtime.low > REGFI_MTIME_MAX_LOW))) |
---|
| 1942 | return NULL; |
---|
| 1943 | |
---|
[101] | 1944 | ret_val->unknown1 = IVAL(nk_header, 0xC); |
---|
| 1945 | ret_val->parent_off = IVAL(nk_header, 0x10); |
---|
| 1946 | ret_val->num_subkeys = IVAL(nk_header, 0x14); |
---|
| 1947 | ret_val->unknown2 = IVAL(nk_header, 0x18); |
---|
| 1948 | ret_val->subkeys_off = IVAL(nk_header, 0x1C); |
---|
| 1949 | ret_val->unknown3 = IVAL(nk_header, 0x20); |
---|
| 1950 | ret_val->num_values = IVAL(nk_header, 0x24); |
---|
| 1951 | ret_val->values_off = IVAL(nk_header, 0x28); |
---|
| 1952 | ret_val->sk_off = IVAL(nk_header, 0x2C); |
---|
| 1953 | ret_val->classname_off = IVAL(nk_header, 0x30); |
---|
[99] | 1954 | |
---|
[101] | 1955 | ret_val->max_bytes_subkeyname = IVAL(nk_header, 0x34); |
---|
| 1956 | ret_val->max_bytes_subkeyclassname = IVAL(nk_header, 0x38); |
---|
| 1957 | ret_val->max_bytes_valuename = IVAL(nk_header, 0x3C); |
---|
| 1958 | ret_val->max_bytes_value = IVAL(nk_header, 0x40); |
---|
| 1959 | ret_val->unk_index = IVAL(nk_header, 0x44); |
---|
[99] | 1960 | |
---|
[101] | 1961 | ret_val->name_length = SVAL(nk_header, 0x48); |
---|
| 1962 | ret_val->classname_length = SVAL(nk_header, 0x4A); |
---|
[99] | 1963 | |
---|
[125] | 1964 | |
---|
[99] | 1965 | if(ret_val->name_length + REGFI_NK_MIN_LENGTH > ret_val->cell_size) |
---|
[101] | 1966 | { |
---|
| 1967 | if(strict) |
---|
| 1968 | { |
---|
[138] | 1969 | regfi_add_message(file, REGFI_MSG_ERROR, "Contents too large for cell" |
---|
[137] | 1970 | " while parsing NK record at offset 0x%.8X.", offset); |
---|
[101] | 1971 | free(ret_val); |
---|
| 1972 | return NULL; |
---|
| 1973 | } |
---|
| 1974 | else |
---|
| 1975 | ret_val->name_length = ret_val->cell_size - REGFI_NK_MIN_LENGTH; |
---|
| 1976 | } |
---|
| 1977 | else if (unalloc) |
---|
| 1978 | { /* Truncate cell_size if it's much larger than the apparent total record length. */ |
---|
| 1979 | /* Round up to the next multiple of 8 */ |
---|
| 1980 | length = (ret_val->name_length + REGFI_NK_MIN_LENGTH) & 0xFFFFFFF8; |
---|
| 1981 | if(length < ret_val->name_length + REGFI_NK_MIN_LENGTH) |
---|
| 1982 | length+=8; |
---|
[99] | 1983 | |
---|
[101] | 1984 | /* If cell_size is still greater, truncate. */ |
---|
| 1985 | if(length < ret_val->cell_size) |
---|
| 1986 | ret_val->cell_size = length; |
---|
| 1987 | } |
---|
| 1988 | |
---|
[99] | 1989 | ret_val->keyname = (char*)zalloc(sizeof(char)*(ret_val->name_length+1)); |
---|
| 1990 | if(ret_val->keyname == NULL) |
---|
| 1991 | { |
---|
| 1992 | free(ret_val); |
---|
| 1993 | return NULL; |
---|
| 1994 | } |
---|
| 1995 | |
---|
| 1996 | /* Don't need to seek, should be at the right offset */ |
---|
| 1997 | length = ret_val->name_length; |
---|
[101] | 1998 | if((regfi_read(file->fd, (uint8*)ret_val->keyname, &length) != 0) |
---|
[99] | 1999 | || length != ret_val->name_length) |
---|
| 2000 | { |
---|
[138] | 2001 | regfi_add_message(file, REGFI_MSG_ERROR, "Failed to read key name" |
---|
[137] | 2002 | " while parsing NK record at offset 0x%.8X.", offset); |
---|
[99] | 2003 | free(ret_val->keyname); |
---|
| 2004 | free(ret_val); |
---|
| 2005 | return NULL; |
---|
| 2006 | } |
---|
| 2007 | ret_val->keyname[ret_val->name_length] = '\0'; |
---|
| 2008 | |
---|
[135] | 2009 | if(ret_val->classname_off != REGFI_OFFSET_NONE) |
---|
[126] | 2010 | { |
---|
[131] | 2011 | hbin = regfi_lookup_hbin(file, ret_val->classname_off); |
---|
| 2012 | if(hbin) |
---|
| 2013 | { |
---|
[135] | 2014 | class_offset = ret_val->classname_off+REGFI_REGF_SIZE; |
---|
[131] | 2015 | class_maxsize = hbin->block_size + hbin->file_off - class_offset; |
---|
| 2016 | ret_val->classname |
---|
| 2017 | = regfi_parse_classname(file, class_offset, &ret_val->classname_length, |
---|
| 2018 | class_maxsize, strict); |
---|
| 2019 | } |
---|
| 2020 | else |
---|
[137] | 2021 | { |
---|
[131] | 2022 | ret_val->classname = NULL; |
---|
[138] | 2023 | regfi_add_message(file, REGFI_MSG_WARN, "Could not find hbin for class" |
---|
| 2024 | " name while parsing NK record at offset 0x%.8X.", |
---|
| 2025 | offset); |
---|
[137] | 2026 | } |
---|
[140] | 2027 | |
---|
| 2028 | if(ret_val->classname == NULL) |
---|
| 2029 | { |
---|
| 2030 | regfi_add_message(file, REGFI_MSG_WARN, "Could not parse class" |
---|
| 2031 | " name while parsing NK record at offset 0x%.8X.", |
---|
| 2032 | offset); |
---|
| 2033 | return NULL; |
---|
| 2034 | } |
---|
[126] | 2035 | } |
---|
[138] | 2036 | |
---|
[126] | 2037 | return ret_val; |
---|
| 2038 | } |
---|
| 2039 | |
---|
| 2040 | |
---|
[135] | 2041 | char* regfi_parse_classname(REGFI_FILE* file, uint32 offset, |
---|
[131] | 2042 | uint16* name_length, uint32 max_size, bool strict) |
---|
[126] | 2043 | { |
---|
| 2044 | char* ret_val = NULL; |
---|
| 2045 | uint32 length; |
---|
| 2046 | uint32 cell_length; |
---|
| 2047 | bool unalloc = false; |
---|
| 2048 | |
---|
[135] | 2049 | if(*name_length > 0 && offset != REGFI_OFFSET_NONE |
---|
[126] | 2050 | && offset == (offset & 0xFFFFFFF8)) |
---|
[131] | 2051 | { |
---|
[126] | 2052 | if(!regfi_parse_cell(file->fd, offset, NULL, 0, &cell_length, &unalloc)) |
---|
[137] | 2053 | { |
---|
[138] | 2054 | regfi_add_message(file, REGFI_MSG_WARN, "Could not parse cell header" |
---|
[137] | 2055 | " while parsing class name at offset 0x%.8X.", offset); |
---|
[126] | 2056 | return NULL; |
---|
[137] | 2057 | } |
---|
[126] | 2058 | |
---|
[131] | 2059 | if((cell_length & 0xFFFFFFF8) != cell_length) |
---|
[137] | 2060 | { |
---|
[138] | 2061 | regfi_add_message(file, REGFI_MSG_ERROR, "Cell length not a multiple of 8" |
---|
[137] | 2062 | " while parsing class name at offset 0x%.8X.", offset); |
---|
[131] | 2063 | return NULL; |
---|
[137] | 2064 | } |
---|
| 2065 | |
---|
[131] | 2066 | if(cell_length > max_size) |
---|
[125] | 2067 | { |
---|
[138] | 2068 | regfi_add_message(file, REGFI_MSG_WARN, "Cell stretches past hbin " |
---|
| 2069 | "boundary while parsing class name at offset 0x%.8X.", |
---|
| 2070 | offset); |
---|
[126] | 2071 | if(strict) |
---|
| 2072 | return NULL; |
---|
[131] | 2073 | cell_length = max_size; |
---|
[126] | 2074 | } |
---|
[131] | 2075 | |
---|
| 2076 | if((cell_length - 4) < *name_length) |
---|
| 2077 | { |
---|
[138] | 2078 | regfi_add_message(file, REGFI_MSG_WARN, "Class name is larger than" |
---|
| 2079 | " cell_length while parsing class name at offset" |
---|
| 2080 | " 0x%.8X.", offset); |
---|
[131] | 2081 | if(strict) |
---|
| 2082 | return NULL; |
---|
| 2083 | *name_length = cell_length - 4; |
---|
| 2084 | } |
---|
[126] | 2085 | |
---|
| 2086 | ret_val = (char*)zalloc(*name_length); |
---|
| 2087 | if(ret_val != NULL) |
---|
| 2088 | { |
---|
| 2089 | length = *name_length; |
---|
| 2090 | if((regfi_read(file->fd, (uint8*)ret_val, &length) != 0) |
---|
| 2091 | || length != *name_length) |
---|
[125] | 2092 | { |
---|
[138] | 2093 | regfi_add_message(file, REGFI_MSG_ERROR, "Could not read class name" |
---|
[137] | 2094 | " while parsing class name at offset 0x%.8X.", offset); |
---|
[126] | 2095 | free(ret_val); |
---|
| 2096 | return NULL; |
---|
[125] | 2097 | } |
---|
| 2098 | } |
---|
| 2099 | } |
---|
| 2100 | |
---|
[99] | 2101 | return ret_val; |
---|
| 2102 | } |
---|
| 2103 | |
---|
| 2104 | |
---|
[101] | 2105 | /******************************************************************* |
---|
| 2106 | *******************************************************************/ |
---|
[135] | 2107 | REGFI_VK_REC* regfi_parse_vk(REGFI_FILE* file, uint32 offset, |
---|
[145] | 2108 | uint32 max_size, bool strict) |
---|
[97] | 2109 | { |
---|
[135] | 2110 | REGFI_VK_REC* ret_val; |
---|
[101] | 2111 | uint8 vk_header[REGFI_VK_MIN_LENGTH]; |
---|
| 2112 | uint32 raw_data_size, length, cell_length; |
---|
| 2113 | bool unalloc = false; |
---|
[97] | 2114 | |
---|
[101] | 2115 | if(!regfi_parse_cell(file->fd, offset, vk_header, REGFI_VK_MIN_LENGTH, |
---|
| 2116 | &cell_length, &unalloc)) |
---|
[137] | 2117 | { |
---|
[138] | 2118 | regfi_add_message(file, REGFI_MSG_WARN, "Could not parse cell header" |
---|
[137] | 2119 | " while parsing VK record at offset 0x%.8X.", offset); |
---|
[101] | 2120 | return NULL; |
---|
[137] | 2121 | } |
---|
[111] | 2122 | |
---|
[135] | 2123 | ret_val = (REGFI_VK_REC*)zalloc(sizeof(REGFI_VK_REC)); |
---|
[101] | 2124 | if(ret_val == NULL) |
---|
| 2125 | return NULL; |
---|
| 2126 | |
---|
| 2127 | ret_val->offset = offset; |
---|
| 2128 | ret_val->cell_size = cell_length; |
---|
| 2129 | |
---|
| 2130 | if(ret_val->cell_size > max_size) |
---|
| 2131 | ret_val->cell_size = max_size & 0xFFFFFFF8; |
---|
| 2132 | if((ret_val->cell_size < REGFI_VK_MIN_LENGTH) |
---|
[111] | 2133 | || ret_val->cell_size != (ret_val->cell_size & 0xFFFFFFF8)) |
---|
[97] | 2134 | { |
---|
[138] | 2135 | regfi_add_message(file, REGFI_MSG_WARN, "Invalid cell size encountered" |
---|
[137] | 2136 | " while parsing VK record at offset 0x%.8X.", offset); |
---|
[101] | 2137 | free(ret_val); |
---|
| 2138 | return NULL; |
---|
| 2139 | } |
---|
[97] | 2140 | |
---|
[101] | 2141 | ret_val->magic[0] = vk_header[0x0]; |
---|
| 2142 | ret_val->magic[1] = vk_header[0x1]; |
---|
| 2143 | if((ret_val->magic[0] != 'v') || (ret_val->magic[1] != 'k')) |
---|
| 2144 | { |
---|
[124] | 2145 | /* XXX: This does not account for deleted keys under Win2K which |
---|
| 2146 | * often have this (and the name length) overwritten with |
---|
| 2147 | * 0xFFFF. |
---|
| 2148 | */ |
---|
[138] | 2149 | regfi_add_message(file, REGFI_MSG_WARN, "Magic number mismatch" |
---|
[137] | 2150 | " while parsing VK record at offset 0x%.8X.", offset); |
---|
[101] | 2151 | free(ret_val); |
---|
| 2152 | return NULL; |
---|
| 2153 | } |
---|
| 2154 | |
---|
| 2155 | ret_val->name_length = SVAL(vk_header, 0x2); |
---|
| 2156 | raw_data_size = IVAL(vk_header, 0x4); |
---|
[135] | 2157 | ret_val->data_size = raw_data_size & ~REGFI_VK_DATA_IN_OFFSET; |
---|
| 2158 | ret_val->data_in_offset = (bool)(raw_data_size & REGFI_VK_DATA_IN_OFFSET); |
---|
[101] | 2159 | ret_val->data_off = IVAL(vk_header, 0x8); |
---|
| 2160 | ret_val->type = IVAL(vk_header, 0xC); |
---|
| 2161 | ret_val->flag = SVAL(vk_header, 0x10); |
---|
| 2162 | ret_val->unknown1 = SVAL(vk_header, 0x12); |
---|
| 2163 | |
---|
[135] | 2164 | if(ret_val->flag & REGFI_VK_FLAG_NAME_PRESENT) |
---|
[101] | 2165 | { |
---|
[113] | 2166 | if(ret_val->name_length + REGFI_VK_MIN_LENGTH + 4 > ret_val->cell_size) |
---|
[101] | 2167 | { |
---|
[138] | 2168 | regfi_add_message(file, REGFI_MSG_WARN, "Name too long for remaining cell" |
---|
| 2169 | " space while parsing VK record at offset 0x%.8X.", |
---|
| 2170 | offset); |
---|
[101] | 2171 | if(strict) |
---|
| 2172 | { |
---|
| 2173 | free(ret_val); |
---|
| 2174 | return NULL; |
---|
| 2175 | } |
---|
| 2176 | else |
---|
[113] | 2177 | ret_val->name_length = ret_val->cell_size - REGFI_VK_MIN_LENGTH - 4; |
---|
[101] | 2178 | } |
---|
| 2179 | |
---|
| 2180 | /* Round up to the next multiple of 8 */ |
---|
[113] | 2181 | cell_length = (ret_val->name_length + REGFI_VK_MIN_LENGTH + 4) & 0xFFFFFFF8; |
---|
| 2182 | if(cell_length < ret_val->name_length + REGFI_VK_MIN_LENGTH + 4) |
---|
| 2183 | cell_length+=8; |
---|
[101] | 2184 | |
---|
| 2185 | ret_val->valuename = (char*)zalloc(sizeof(char)*(ret_val->name_length+1)); |
---|
| 2186 | if(ret_val->valuename == NULL) |
---|
| 2187 | { |
---|
| 2188 | free(ret_val); |
---|
| 2189 | return NULL; |
---|
| 2190 | } |
---|
[113] | 2191 | |
---|
[101] | 2192 | length = ret_val->name_length; |
---|
| 2193 | if((regfi_read(file->fd, (uint8*)ret_val->valuename, &length) != 0) |
---|
| 2194 | || length != ret_val->name_length) |
---|
| 2195 | { |
---|
[138] | 2196 | regfi_add_message(file, REGFI_MSG_ERROR, "Could not read value name" |
---|
[137] | 2197 | " while parsing VK record at offset 0x%.8X.", offset); |
---|
[101] | 2198 | free(ret_val->valuename); |
---|
| 2199 | free(ret_val); |
---|
| 2200 | return NULL; |
---|
| 2201 | } |
---|
| 2202 | ret_val->valuename[ret_val->name_length] = '\0'; |
---|
[133] | 2203 | |
---|
[101] | 2204 | } |
---|
| 2205 | else |
---|
[113] | 2206 | cell_length = REGFI_VK_MIN_LENGTH + 4; |
---|
[101] | 2207 | |
---|
| 2208 | if(unalloc) |
---|
| 2209 | { |
---|
| 2210 | /* If cell_size is still greater, truncate. */ |
---|
[113] | 2211 | if(cell_length < ret_val->cell_size) |
---|
| 2212 | ret_val->cell_size = cell_length; |
---|
[101] | 2213 | } |
---|
| 2214 | |
---|
| 2215 | return ret_val; |
---|
[97] | 2216 | } |
---|
[101] | 2217 | |
---|
| 2218 | |
---|
[145] | 2219 | uint8* regfi_parse_data(REGFI_FILE* file, |
---|
| 2220 | uint32 data_type, uint32 offset, |
---|
| 2221 | uint32 length, uint32 max_size, |
---|
| 2222 | bool data_in_offset, bool strict) |
---|
[101] | 2223 | { |
---|
| 2224 | uint8* ret_val; |
---|
| 2225 | uint32 read_length, cell_length; |
---|
[102] | 2226 | uint8 i; |
---|
[101] | 2227 | bool unalloc; |
---|
| 2228 | |
---|
[137] | 2229 | /* The data is typically stored in the offset if the size <= 4 */ |
---|
[145] | 2230 | if(data_in_offset) |
---|
[101] | 2231 | { |
---|
| 2232 | if(length > 4) |
---|
[137] | 2233 | { |
---|
[138] | 2234 | regfi_add_message(file, REGFI_MSG_ERROR, "Data in offset but length > 4" |
---|
[137] | 2235 | " while parsing data record at offset 0x%.8X.", |
---|
| 2236 | offset); |
---|
[101] | 2237 | return NULL; |
---|
[137] | 2238 | } |
---|
[101] | 2239 | |
---|
| 2240 | if((ret_val = (uint8*)zalloc(sizeof(uint8)*length)) == NULL) |
---|
| 2241 | return NULL; |
---|
[102] | 2242 | |
---|
| 2243 | for(i = 0; i < length; i++) |
---|
| 2244 | ret_val[i] = (uint8)((offset >> i*8) & 0xFF); |
---|
[101] | 2245 | } |
---|
| 2246 | else |
---|
| 2247 | { |
---|
| 2248 | if(!regfi_parse_cell(file->fd, offset, NULL, 0, |
---|
| 2249 | &cell_length, &unalloc)) |
---|
[137] | 2250 | { |
---|
[138] | 2251 | regfi_add_message(file, REGFI_MSG_WARN, "Could not parse cell while" |
---|
[137] | 2252 | " parsing data record at offset 0x%.8X.", offset); |
---|
[101] | 2253 | return NULL; |
---|
[137] | 2254 | } |
---|
[111] | 2255 | |
---|
| 2256 | if((cell_length & 0xFFFFFFF8) != cell_length) |
---|
[137] | 2257 | { |
---|
[138] | 2258 | regfi_add_message(file, REGFI_MSG_WARN, "Cell length not multiple of 8" |
---|
[137] | 2259 | " while parsing data record at offset 0x%.8X.", |
---|
| 2260 | offset); |
---|
[101] | 2261 | return NULL; |
---|
[137] | 2262 | } |
---|
[101] | 2263 | |
---|
[131] | 2264 | if(cell_length > max_size) |
---|
| 2265 | { |
---|
[145] | 2266 | regfi_add_message(file, REGFI_MSG_WARN, "Cell extends past HBIN boundary" |
---|
| 2267 | " while parsing data record at offset 0x%.8X.", |
---|
[137] | 2268 | offset); |
---|
[131] | 2269 | if(strict) |
---|
| 2270 | return NULL; |
---|
| 2271 | else |
---|
| 2272 | cell_length = max_size; |
---|
| 2273 | } |
---|
| 2274 | |
---|
[101] | 2275 | if(cell_length - 4 < length) |
---|
| 2276 | { |
---|
[116] | 2277 | /* XXX: This strict condition has been triggered in multiple registries. |
---|
| 2278 | * Not sure the cause, but the data length values are very large, |
---|
| 2279 | * such as 53392. |
---|
[111] | 2280 | */ |
---|
[138] | 2281 | regfi_add_message(file, REGFI_MSG_WARN, "Data length (0x%.8X) larger than" |
---|
[137] | 2282 | " remaining cell length (0x%.8X)" |
---|
| 2283 | " while parsing data record at offset 0x%.8X.", |
---|
| 2284 | length, cell_length - 4, offset); |
---|
[101] | 2285 | if(strict) |
---|
| 2286 | return NULL; |
---|
| 2287 | else |
---|
| 2288 | length = cell_length - 4; |
---|
| 2289 | } |
---|
| 2290 | |
---|
| 2291 | if((ret_val = (uint8*)zalloc(sizeof(uint8)*length)) == NULL) |
---|
| 2292 | return NULL; |
---|
| 2293 | |
---|
| 2294 | read_length = length; |
---|
| 2295 | if((regfi_read(file->fd, ret_val, &read_length) != 0) |
---|
| 2296 | || read_length != length) |
---|
| 2297 | { |
---|
[138] | 2298 | regfi_add_message(file, REGFI_MSG_ERROR, "Could not read data block while" |
---|
[137] | 2299 | " parsing data record at offset 0x%.8X.", offset); |
---|
[101] | 2300 | free(ret_val); |
---|
| 2301 | return NULL; |
---|
| 2302 | } |
---|
| 2303 | } |
---|
| 2304 | |
---|
| 2305 | return ret_val; |
---|
| 2306 | } |
---|
[110] | 2307 | |
---|
| 2308 | |
---|
[135] | 2309 | range_list* regfi_parse_unalloc_cells(REGFI_FILE* file) |
---|
[110] | 2310 | { |
---|
| 2311 | range_list* ret_val; |
---|
[135] | 2312 | REGFI_HBIN* hbin; |
---|
[110] | 2313 | const range_list_element* hbins_elem; |
---|
| 2314 | uint32 i, num_hbins, curr_off, cell_len; |
---|
| 2315 | bool is_unalloc; |
---|
| 2316 | |
---|
| 2317 | ret_val = range_list_new(); |
---|
| 2318 | if(ret_val == NULL) |
---|
| 2319 | return NULL; |
---|
| 2320 | |
---|
| 2321 | num_hbins = range_list_size(file->hbins); |
---|
| 2322 | for(i=0; i<num_hbins; i++) |
---|
| 2323 | { |
---|
| 2324 | hbins_elem = range_list_get(file->hbins, i); |
---|
| 2325 | if(hbins_elem == NULL) |
---|
| 2326 | break; |
---|
[135] | 2327 | hbin = (REGFI_HBIN*)hbins_elem->data; |
---|
[110] | 2328 | |
---|
[135] | 2329 | curr_off = REGFI_HBIN_HEADER_SIZE; |
---|
[110] | 2330 | while(curr_off < hbin->block_size) |
---|
| 2331 | { |
---|
| 2332 | if(!regfi_parse_cell(file->fd, hbin->file_off+curr_off, NULL, 0, |
---|
| 2333 | &cell_len, &is_unalloc)) |
---|
| 2334 | break; |
---|
| 2335 | |
---|
[113] | 2336 | if((cell_len == 0) || ((cell_len & 0xFFFFFFF8) != cell_len)) |
---|
[140] | 2337 | { |
---|
| 2338 | regfi_add_message(file, REGFI_MSG_ERROR, "Bad cell length encountered" |
---|
| 2339 | " while parsing unallocated cells at offset 0x%.8X.", |
---|
| 2340 | hbin->file_off+curr_off); |
---|
[110] | 2341 | break; |
---|
[140] | 2342 | } |
---|
| 2343 | |
---|
[110] | 2344 | /* for some reason the record_size of the last record in |
---|
| 2345 | an hbin block can extend past the end of the block |
---|
| 2346 | even though the record fits within the remaining |
---|
| 2347 | space....aaarrrgggghhhhhh */ |
---|
| 2348 | if(curr_off + cell_len >= hbin->block_size) |
---|
| 2349 | cell_len = hbin->block_size - curr_off; |
---|
| 2350 | |
---|
| 2351 | if(is_unalloc) |
---|
| 2352 | range_list_add(ret_val, hbin->file_off+curr_off, |
---|
| 2353 | cell_len, NULL); |
---|
| 2354 | |
---|
| 2355 | curr_off = curr_off+cell_len; |
---|
| 2356 | } |
---|
| 2357 | } |
---|
| 2358 | |
---|
| 2359 | return ret_val; |
---|
| 2360 | } |
---|