[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 157 2009-11-23 00:47:22Z 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 | |
---|
[157] | 473 | /****************************************************************************** |
---|
[106] | 474 | * Given an offset and an hbin, is the offset within that hbin? |
---|
| 475 | * The offset is a virtual file offset. |
---|
[157] | 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 | |
---|
[157] | 491 | /****************************************************************************** |
---|
| 492 | * Provide a physical offset and receive the correpsonding HBIN |
---|
[106] | 493 | * block for it. NULL if one doesn't exist. |
---|
[157] | 494 | ******************************************************************************/ |
---|
| 495 | const REGFI_HBIN* regfi_lookup_hbin(REGFI_FILE* file, uint32 offset) |
---|
[30] | 496 | { |
---|
[157] | 497 | return (const REGFI_HBIN*)range_list_find_data(file->hbins, offset); |
---|
[30] | 498 | } |
---|
| 499 | |
---|
| 500 | |
---|
[157] | 501 | /****************************************************************************** |
---|
| 502 | * Calculate the largest possible cell size given a physical offset. |
---|
| 503 | * Largest size is based on the HBIN the offset is currently a member of. |
---|
| 504 | * Returns negative values on error. |
---|
| 505 | * (Since cells can only be ~2^31 in size, this works out.) |
---|
| 506 | ******************************************************************************/ |
---|
| 507 | int32 regfi_calc_maxsize(REGFI_FILE* file, uint32 offset) |
---|
| 508 | { |
---|
| 509 | const REGFI_HBIN* hbin = regfi_lookup_hbin(file, offset); |
---|
| 510 | if(hbin == NULL) |
---|
| 511 | return -1; |
---|
[139] | 512 | |
---|
[157] | 513 | return (hbin->block_size + hbin->file_off) - offset; |
---|
| 514 | } |
---|
| 515 | |
---|
| 516 | |
---|
[139] | 517 | /****************************************************************************** |
---|
| 518 | ******************************************************************************/ |
---|
| 519 | REGFI_SUBKEY_LIST* regfi_load_subkeylist(REGFI_FILE* file, uint32 offset, |
---|
| 520 | uint32 num_keys, uint32 max_size, |
---|
| 521 | bool strict) |
---|
[127] | 522 | { |
---|
[135] | 523 | REGFI_SUBKEY_LIST* ret_val; |
---|
[134] | 524 | |
---|
[139] | 525 | ret_val = regfi_load_subkeylist_aux(file, offset, max_size, strict, |
---|
| 526 | REGFI_MAX_SUBKEY_DEPTH); |
---|
[143] | 527 | if(ret_val == NULL) |
---|
| 528 | { |
---|
| 529 | regfi_add_message(file, REGFI_MSG_WARN, "Failed to load subkey list at" |
---|
| 530 | " offset 0x%.8X.", offset); |
---|
| 531 | return NULL; |
---|
| 532 | } |
---|
[139] | 533 | |
---|
| 534 | if(num_keys != ret_val->num_keys) |
---|
| 535 | { |
---|
| 536 | /* Not sure which should be authoritative, the number from the |
---|
| 537 | * NK record, or the number in the subkey list. Just emit a warning for |
---|
| 538 | * now if they don't match. |
---|
| 539 | */ |
---|
| 540 | regfi_add_message(file, REGFI_MSG_WARN, "Number of subkeys listed in parent" |
---|
| 541 | " (%d) did not match number found in subkey list/tree (%d)" |
---|
| 542 | " while parsing subkey list/tree at offset 0x%.8X.", |
---|
| 543 | num_keys, ret_val->num_keys, offset); |
---|
| 544 | } |
---|
| 545 | |
---|
| 546 | return ret_val; |
---|
| 547 | } |
---|
| 548 | |
---|
| 549 | |
---|
| 550 | /****************************************************************************** |
---|
| 551 | ******************************************************************************/ |
---|
| 552 | REGFI_SUBKEY_LIST* regfi_load_subkeylist_aux(REGFI_FILE* file, uint32 offset, |
---|
| 553 | uint32 max_size, bool strict, |
---|
| 554 | uint8 depth_left) |
---|
| 555 | { |
---|
| 556 | REGFI_SUBKEY_LIST* ret_val; |
---|
| 557 | REGFI_SUBKEY_LIST** sublists; |
---|
[157] | 558 | uint32 i, num_sublists, off; |
---|
| 559 | int32 sublist_maxsize; |
---|
[139] | 560 | |
---|
| 561 | if(depth_left == 0) |
---|
| 562 | { |
---|
| 563 | regfi_add_message(file, REGFI_MSG_WARN, "Maximum depth reached" |
---|
| 564 | " while parsing subkey list/tree at offset 0x%.8X.", |
---|
| 565 | offset); |
---|
[127] | 566 | return NULL; |
---|
[139] | 567 | } |
---|
[134] | 568 | |
---|
[139] | 569 | ret_val = regfi_parse_subkeylist(file, offset, max_size, strict); |
---|
[134] | 570 | if(ret_val == NULL) |
---|
| 571 | return NULL; |
---|
[139] | 572 | |
---|
| 573 | if(ret_val->recursive_type) |
---|
[127] | 574 | { |
---|
[139] | 575 | num_sublists = ret_val->num_children; |
---|
[150] | 576 | sublists = (REGFI_SUBKEY_LIST**)malloc(num_sublists |
---|
[139] | 577 | * sizeof(REGFI_SUBKEY_LIST*)); |
---|
| 578 | for(i=0; i < num_sublists; i++) |
---|
[127] | 579 | { |
---|
[139] | 580 | off = ret_val->elements[i].offset + REGFI_REGF_SIZE; |
---|
[157] | 581 | |
---|
| 582 | sublist_maxsize = regfi_calc_maxsize(file, off); |
---|
| 583 | if(sublist_maxsize < 0) |
---|
[139] | 584 | sublists[i] = NULL; |
---|
| 585 | else |
---|
[157] | 586 | sublists[i] = regfi_load_subkeylist_aux(file, off, sublist_maxsize, |
---|
| 587 | strict, depth_left-1); |
---|
[127] | 588 | } |
---|
[150] | 589 | talloc_free(ret_val); |
---|
[134] | 590 | |
---|
[139] | 591 | return regfi_merge_subkeylists(num_sublists, sublists, strict); |
---|
[127] | 592 | } |
---|
[30] | 593 | |
---|
[127] | 594 | return ret_val; |
---|
| 595 | } |
---|
| 596 | |
---|
| 597 | |
---|
[139] | 598 | /****************************************************************************** |
---|
| 599 | ******************************************************************************/ |
---|
| 600 | REGFI_SUBKEY_LIST* regfi_parse_subkeylist(REGFI_FILE* file, uint32 offset, |
---|
| 601 | uint32 max_size, bool strict) |
---|
[30] | 602 | { |
---|
[135] | 603 | REGFI_SUBKEY_LIST* ret_val; |
---|
[150] | 604 | uint32 i, cell_length, length, elem_size, read_len; |
---|
| 605 | uint8* elements = NULL; |
---|
[127] | 606 | uint8 buf[REGFI_SUBKEY_LIST_MIN_LEN]; |
---|
[104] | 607 | bool unalloc; |
---|
[139] | 608 | bool recursive_type; |
---|
[30] | 609 | |
---|
[127] | 610 | if(!regfi_parse_cell(file->fd, offset, buf, REGFI_SUBKEY_LIST_MIN_LEN, |
---|
[104] | 611 | &cell_length, &unalloc)) |
---|
[139] | 612 | { |
---|
| 613 | regfi_add_message(file, REGFI_MSG_WARN, "Could not parse cell while " |
---|
| 614 | "parsing subkey-list at offset 0x%.8X.", offset); |
---|
[104] | 615 | return NULL; |
---|
[139] | 616 | } |
---|
[30] | 617 | |
---|
[116] | 618 | if(cell_length > max_size) |
---|
| 619 | { |
---|
[139] | 620 | regfi_add_message(file, REGFI_MSG_WARN, "Cell size longer than max_size" |
---|
| 621 | " while parsing subkey-list at offset 0x%.8X.", offset); |
---|
[116] | 622 | if(strict) |
---|
| 623 | return NULL; |
---|
| 624 | cell_length = max_size & 0xFFFFFFF8; |
---|
| 625 | } |
---|
[30] | 626 | |
---|
[139] | 627 | recursive_type = false; |
---|
[127] | 628 | if(buf[0] == 'r' && buf[1] == 'i') |
---|
[104] | 629 | { |
---|
[139] | 630 | recursive_type = true; |
---|
| 631 | elem_size = sizeof(uint32); |
---|
[104] | 632 | } |
---|
[139] | 633 | else if(buf[0] == 'l' && buf[1] == 'i') |
---|
[134] | 634 | elem_size = sizeof(uint32); |
---|
| 635 | else if((buf[0] == 'l') && (buf[1] == 'f' || buf[1] == 'h')) |
---|
[135] | 636 | elem_size = sizeof(REGFI_SUBKEY_LIST_ELEM); |
---|
[134] | 637 | else |
---|
| 638 | { |
---|
[139] | 639 | regfi_add_message(file, REGFI_MSG_ERROR, "Unknown magic number" |
---|
| 640 | " (0x%.2X, 0x%.2X) encountered while parsing" |
---|
| 641 | " subkey-list at offset 0x%.8X.", buf[0], buf[1], offset); |
---|
[134] | 642 | return NULL; |
---|
| 643 | } |
---|
| 644 | |
---|
[150] | 645 | ret_val = talloc(NULL, REGFI_SUBKEY_LIST); |
---|
[127] | 646 | if(ret_val == NULL) |
---|
| 647 | return NULL; |
---|
| 648 | |
---|
| 649 | ret_val->offset = offset; |
---|
| 650 | ret_val->cell_size = cell_length; |
---|
[104] | 651 | ret_val->magic[0] = buf[0]; |
---|
| 652 | ret_val->magic[1] = buf[1]; |
---|
[139] | 653 | ret_val->recursive_type = recursive_type; |
---|
| 654 | ret_val->num_children = SVAL(buf, 0x2); |
---|
[101] | 655 | |
---|
[139] | 656 | if(!recursive_type) |
---|
| 657 | ret_val->num_keys = ret_val->num_children; |
---|
[101] | 658 | |
---|
[139] | 659 | length = elem_size*ret_val->num_children; |
---|
| 660 | if(cell_length - REGFI_SUBKEY_LIST_MIN_LEN - sizeof(uint32) < length) |
---|
[134] | 661 | { |
---|
[139] | 662 | regfi_add_message(file, REGFI_MSG_WARN, "Number of elements too large for" |
---|
| 663 | " cell while parsing subkey-list at offset 0x%.8X.", |
---|
| 664 | offset); |
---|
| 665 | if(strict) |
---|
[150] | 666 | goto fail; |
---|
[139] | 667 | length = cell_length - REGFI_SUBKEY_LIST_MIN_LEN - sizeof(uint32); |
---|
[134] | 668 | } |
---|
[30] | 669 | |
---|
[150] | 670 | ret_val->elements = talloc_array(ret_val, REGFI_SUBKEY_LIST_ELEM, |
---|
| 671 | ret_val->num_children); |
---|
[127] | 672 | if(ret_val->elements == NULL) |
---|
[150] | 673 | goto fail; |
---|
[30] | 674 | |
---|
[150] | 675 | elements = (uint8*)malloc(length); |
---|
[139] | 676 | if(elements == NULL) |
---|
[150] | 677 | goto fail; |
---|
[30] | 678 | |
---|
[150] | 679 | read_len = length; |
---|
| 680 | if(regfi_read(file->fd, elements, &read_len) != 0 || read_len != length) |
---|
| 681 | goto fail; |
---|
[30] | 682 | |
---|
[139] | 683 | if(elem_size == sizeof(uint32)) |
---|
[104] | 684 | { |
---|
[139] | 685 | for (i=0; i < ret_val->num_children; i++) |
---|
[134] | 686 | { |
---|
[139] | 687 | ret_val->elements[i].offset = IVAL(elements, i*elem_size); |
---|
[134] | 688 | ret_val->elements[i].hash = 0; |
---|
| 689 | } |
---|
[104] | 690 | } |
---|
[134] | 691 | else |
---|
| 692 | { |
---|
[139] | 693 | for (i=0; i < ret_val->num_children; i++) |
---|
[134] | 694 | { |
---|
[139] | 695 | ret_val->elements[i].offset = IVAL(elements, i*elem_size); |
---|
| 696 | ret_val->elements[i].hash = IVAL(elements, i*elem_size+4); |
---|
[134] | 697 | } |
---|
| 698 | } |
---|
[139] | 699 | free(elements); |
---|
[30] | 700 | |
---|
[104] | 701 | return ret_val; |
---|
[150] | 702 | |
---|
| 703 | fail: |
---|
| 704 | if(elements != NULL) |
---|
| 705 | free(elements); |
---|
| 706 | talloc_free(ret_val); |
---|
| 707 | return NULL; |
---|
[30] | 708 | } |
---|
| 709 | |
---|
| 710 | |
---|
[139] | 711 | /******************************************************************* |
---|
| 712 | *******************************************************************/ |
---|
| 713 | REGFI_SUBKEY_LIST* regfi_merge_subkeylists(uint16 num_lists, |
---|
| 714 | REGFI_SUBKEY_LIST** lists, |
---|
| 715 | bool strict) |
---|
| 716 | { |
---|
| 717 | uint32 i,j,k; |
---|
| 718 | REGFI_SUBKEY_LIST* ret_val; |
---|
[102] | 719 | |
---|
[139] | 720 | if(lists == NULL) |
---|
| 721 | return NULL; |
---|
[150] | 722 | ret_val = talloc(NULL, REGFI_SUBKEY_LIST); |
---|
[139] | 723 | |
---|
| 724 | if(ret_val == NULL) |
---|
| 725 | return NULL; |
---|
| 726 | |
---|
| 727 | /* Obtain total number of elements */ |
---|
| 728 | ret_val->num_keys = 0; |
---|
| 729 | for(i=0; i < num_lists; i++) |
---|
| 730 | { |
---|
| 731 | if(lists[i] != NULL) |
---|
| 732 | ret_val->num_keys += lists[i]->num_children; |
---|
| 733 | } |
---|
| 734 | ret_val->num_children = ret_val->num_keys; |
---|
| 735 | |
---|
| 736 | if(ret_val->num_keys > 0) |
---|
| 737 | { |
---|
[150] | 738 | ret_val->elements = talloc_array(ret_val, REGFI_SUBKEY_LIST_ELEM, |
---|
| 739 | ret_val->num_keys); |
---|
[139] | 740 | k=0; |
---|
| 741 | |
---|
| 742 | if(ret_val->elements != NULL) |
---|
| 743 | { |
---|
| 744 | for(i=0; i < num_lists; i++) |
---|
| 745 | { |
---|
| 746 | if(lists[i] != NULL) |
---|
| 747 | { |
---|
| 748 | for(j=0; j < lists[i]->num_keys; j++) |
---|
| 749 | { |
---|
[150] | 750 | ret_val->elements[k].hash = lists[i]->elements[j].hash; |
---|
| 751 | ret_val->elements[k++].offset = lists[i]->elements[j].offset; |
---|
[139] | 752 | } |
---|
| 753 | } |
---|
| 754 | } |
---|
| 755 | } |
---|
| 756 | } |
---|
| 757 | |
---|
| 758 | for(i=0; i < num_lists; i++) |
---|
| 759 | regfi_subkeylist_free(lists[i]); |
---|
| 760 | free(lists); |
---|
| 761 | |
---|
| 762 | return ret_val; |
---|
| 763 | } |
---|
| 764 | |
---|
| 765 | |
---|
[147] | 766 | /****************************************************************************** |
---|
| 767 | * |
---|
| 768 | ******************************************************************************/ |
---|
| 769 | REGFI_SK_REC* regfi_parse_sk(REGFI_FILE* file, uint32 offset, uint32 max_size, |
---|
| 770 | bool strict) |
---|
[30] | 771 | { |
---|
[135] | 772 | REGFI_SK_REC* ret_val; |
---|
[147] | 773 | uint8* sec_desc_buf = NULL; |
---|
[102] | 774 | uint32 cell_length, length; |
---|
| 775 | uint8 sk_header[REGFI_SK_MIN_LENGTH]; |
---|
| 776 | bool unalloc = false; |
---|
[30] | 777 | |
---|
[102] | 778 | if(!regfi_parse_cell(file->fd, offset, sk_header, REGFI_SK_MIN_LENGTH, |
---|
| 779 | &cell_length, &unalloc)) |
---|
[137] | 780 | { |
---|
[138] | 781 | regfi_add_message(file, REGFI_MSG_WARN, "Could not parse SK record cell" |
---|
[137] | 782 | " at offset 0x%.8X.", offset); |
---|
[102] | 783 | return NULL; |
---|
[137] | 784 | } |
---|
[102] | 785 | |
---|
| 786 | if(sk_header[0] != 's' || sk_header[1] != 'k') |
---|
[137] | 787 | { |
---|
[138] | 788 | regfi_add_message(file, REGFI_MSG_WARN, "Magic number mismatch in parsing" |
---|
| 789 | " SK record at offset 0x%.8X.", offset); |
---|
[102] | 790 | return NULL; |
---|
[137] | 791 | } |
---|
| 792 | |
---|
[147] | 793 | ret_val = talloc(NULL, REGFI_SK_REC); |
---|
[102] | 794 | if(ret_val == NULL) |
---|
| 795 | return NULL; |
---|
[30] | 796 | |
---|
[102] | 797 | ret_val->offset = offset; |
---|
[116] | 798 | /* XXX: Is there a way to be more conservative (shorter) with |
---|
| 799 | * cell length when cell is unallocated? |
---|
[111] | 800 | */ |
---|
[102] | 801 | ret_val->cell_size = cell_length; |
---|
[30] | 802 | |
---|
[102] | 803 | if(ret_val->cell_size > max_size) |
---|
| 804 | ret_val->cell_size = max_size & 0xFFFFFFF8; |
---|
| 805 | if((ret_val->cell_size < REGFI_SK_MIN_LENGTH) |
---|
[157] | 806 | || (strict && (ret_val->cell_size & 0x00000007) != 0)) |
---|
[102] | 807 | { |
---|
[138] | 808 | regfi_add_message(file, REGFI_MSG_WARN, "Invalid cell size found while" |
---|
| 809 | " parsing SK record at offset 0x%.8X.", offset); |
---|
[147] | 810 | goto fail; |
---|
[102] | 811 | } |
---|
[30] | 812 | |
---|
[102] | 813 | ret_val->magic[0] = sk_header[0]; |
---|
| 814 | ret_val->magic[1] = sk_header[1]; |
---|
[30] | 815 | |
---|
[102] | 816 | ret_val->unknown_tag = SVAL(sk_header, 0x2); |
---|
| 817 | ret_val->prev_sk_off = IVAL(sk_header, 0x4); |
---|
| 818 | ret_val->next_sk_off = IVAL(sk_header, 0x8); |
---|
| 819 | ret_val->ref_count = IVAL(sk_header, 0xC); |
---|
| 820 | ret_val->desc_size = IVAL(sk_header, 0x10); |
---|
[30] | 821 | |
---|
[157] | 822 | if((ret_val->prev_sk_off & 0x00000007) != 0 |
---|
| 823 | || (ret_val->next_sk_off & 0x00000007) != 0) |
---|
[140] | 824 | { |
---|
| 825 | regfi_add_message(file, REGFI_MSG_WARN, "SK record's next/previous offsets" |
---|
| 826 | " are not a multiple of 8 while parsing SK record at" |
---|
| 827 | " offset 0x%.8X.", offset); |
---|
[147] | 828 | goto fail; |
---|
[140] | 829 | } |
---|
| 830 | |
---|
[102] | 831 | if(ret_val->desc_size + REGFI_SK_MIN_LENGTH > ret_val->cell_size) |
---|
| 832 | { |
---|
[140] | 833 | regfi_add_message(file, REGFI_MSG_WARN, "Security descriptor too large for" |
---|
[138] | 834 | " cell while parsing SK record at offset 0x%.8X.", |
---|
| 835 | offset); |
---|
[147] | 836 | goto fail; |
---|
[102] | 837 | } |
---|
[30] | 838 | |
---|
[147] | 839 | sec_desc_buf = (uint8*)malloc(ret_val->desc_size); |
---|
| 840 | if(sec_desc_buf == NULL) |
---|
| 841 | goto fail; |
---|
[102] | 842 | |
---|
[134] | 843 | length = ret_val->desc_size; |
---|
| 844 | if(regfi_read(file->fd, sec_desc_buf, &length) != 0 |
---|
| 845 | || length != ret_val->desc_size) |
---|
| 846 | { |
---|
[138] | 847 | regfi_add_message(file, REGFI_MSG_ERROR, "Failed to read security" |
---|
| 848 | " descriptor while parsing SK record at offset 0x%.8X.", |
---|
| 849 | offset); |
---|
[147] | 850 | goto fail; |
---|
[134] | 851 | } |
---|
[102] | 852 | |
---|
[147] | 853 | if(!(ret_val->sec_desc = winsec_parse_desc(ret_val, sec_desc_buf, |
---|
| 854 | ret_val->desc_size))) |
---|
[134] | 855 | { |
---|
[138] | 856 | regfi_add_message(file, REGFI_MSG_ERROR, "Failed to parse security" |
---|
| 857 | " descriptor while parsing SK record at offset 0x%.8X.", |
---|
| 858 | offset); |
---|
[147] | 859 | goto fail; |
---|
[134] | 860 | } |
---|
[147] | 861 | |
---|
[134] | 862 | free(sec_desc_buf); |
---|
[147] | 863 | return ret_val; |
---|
[134] | 864 | |
---|
[147] | 865 | fail: |
---|
| 866 | if(sec_desc_buf != NULL) |
---|
| 867 | free(sec_desc_buf); |
---|
| 868 | talloc_free(ret_val); |
---|
| 869 | return NULL; |
---|
[30] | 870 | } |
---|
| 871 | |
---|
| 872 | |
---|
[145] | 873 | REGFI_VALUE_LIST* regfi_parse_valuelist(REGFI_FILE* file, uint32 offset, |
---|
| 874 | uint32 num_values, bool strict) |
---|
[111] | 875 | { |
---|
[145] | 876 | REGFI_VALUE_LIST* ret_val; |
---|
[111] | 877 | uint32 i, cell_length, length, read_len; |
---|
| 878 | bool unalloc; |
---|
[30] | 879 | |
---|
[111] | 880 | if(!regfi_parse_cell(file->fd, offset, NULL, 0, &cell_length, &unalloc)) |
---|
[137] | 881 | { |
---|
[138] | 882 | regfi_add_message(file, REGFI_MSG_ERROR, "Failed to read cell header" |
---|
[137] | 883 | " while parsing value list at offset 0x%.8X.", offset); |
---|
[111] | 884 | return NULL; |
---|
[137] | 885 | } |
---|
[111] | 886 | |
---|
[157] | 887 | if((cell_length & 0x00000007) != 0) |
---|
[111] | 888 | { |
---|
[145] | 889 | regfi_add_message(file, REGFI_MSG_WARN, "Cell length not a multiple of 8" |
---|
| 890 | " while parsing value list at offset 0x%.8X.", offset); |
---|
[111] | 891 | if(strict) |
---|
| 892 | return NULL; |
---|
| 893 | cell_length = cell_length & 0xFFFFFFF8; |
---|
| 894 | } |
---|
[145] | 895 | |
---|
[111] | 896 | if((num_values * sizeof(uint32)) > cell_length-sizeof(uint32)) |
---|
[137] | 897 | { |
---|
[140] | 898 | regfi_add_message(file, REGFI_MSG_WARN, "Too many values found" |
---|
[137] | 899 | " while parsing value list at offset 0x%.8X.", offset); |
---|
[145] | 900 | if(strict) |
---|
| 901 | return NULL; |
---|
| 902 | num_values = cell_length/sizeof(uint32) - sizeof(uint32); |
---|
[137] | 903 | } |
---|
[111] | 904 | |
---|
| 905 | read_len = num_values*sizeof(uint32); |
---|
[150] | 906 | ret_val = talloc(NULL, REGFI_VALUE_LIST); |
---|
[111] | 907 | if(ret_val == NULL) |
---|
| 908 | return NULL; |
---|
| 909 | |
---|
[150] | 910 | ret_val->elements = (REGFI_VALUE_LIST_ELEM*)talloc_size(ret_val, read_len); |
---|
[145] | 911 | if(ret_val->elements == NULL) |
---|
| 912 | { |
---|
[150] | 913 | talloc_free(ret_val); |
---|
[145] | 914 | return NULL; |
---|
| 915 | } |
---|
| 916 | ret_val->num_values = num_values; |
---|
| 917 | |
---|
[111] | 918 | length = read_len; |
---|
[145] | 919 | if((regfi_read(file->fd, (uint8*)ret_val->elements, &length) != 0) |
---|
| 920 | || length != read_len) |
---|
[111] | 921 | { |
---|
[138] | 922 | regfi_add_message(file, REGFI_MSG_ERROR, "Failed to read value pointers" |
---|
[137] | 923 | " while parsing value list at offset 0x%.8X.", offset); |
---|
[150] | 924 | talloc_free(ret_val); |
---|
[111] | 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) |
---|
[157] | 939 | || ((ret_val->elements[i] & 0x00000007) != 0)) |
---|
[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); |
---|
[150] | 944 | talloc_free(ret_val); |
---|
[111] | 945 | return NULL; |
---|
| 946 | } |
---|
| 947 | } |
---|
| 948 | } |
---|
| 949 | |
---|
| 950 | return ret_val; |
---|
| 951 | } |
---|
| 952 | |
---|
| 953 | |
---|
| 954 | |
---|
[103] | 955 | /****************************************************************************** |
---|
| 956 | ******************************************************************************/ |
---|
[150] | 957 | REGFI_VK_REC* regfi_load_value(REGFI_FILE* file, uint32 offset, bool strict) |
---|
[30] | 958 | { |
---|
[145] | 959 | REGFI_VK_REC* ret_val = NULL; |
---|
[151] | 960 | REGFI_BUFFER data; |
---|
[157] | 961 | int32 max_size; |
---|
[30] | 962 | |
---|
[157] | 963 | max_size = regfi_calc_maxsize(file, offset); |
---|
| 964 | if(max_size < 0) |
---|
[103] | 965 | return NULL; |
---|
[145] | 966 | |
---|
[157] | 967 | ret_val = regfi_parse_vk(file, offset, max_size, strict); |
---|
[103] | 968 | if(ret_val == NULL) |
---|
| 969 | return NULL; |
---|
[145] | 970 | |
---|
| 971 | if(ret_val->data_size == 0) |
---|
| 972 | ret_val->data = NULL; |
---|
| 973 | else |
---|
[32] | 974 | { |
---|
[157] | 975 | data = regfi_load_data(file, ret_val->data_off, ret_val->data_size, |
---|
| 976 | ret_val->data_in_offset, strict); |
---|
| 977 | ret_val->data = data.buf; |
---|
| 978 | ret_val->data_size = data.len; |
---|
[151] | 979 | |
---|
[145] | 980 | if(ret_val->data == NULL) |
---|
| 981 | { |
---|
| 982 | regfi_add_message(file, REGFI_MSG_WARN, "Could not parse data record" |
---|
[151] | 983 | " while parsing VK record at offset 0x%.8X.", |
---|
[152] | 984 | ret_val->offset, ret_val->valuename); |
---|
[145] | 985 | } |
---|
[150] | 986 | else |
---|
| 987 | talloc_steal(ret_val, ret_val->data); |
---|
[31] | 988 | } |
---|
[30] | 989 | |
---|
[103] | 990 | return ret_val; |
---|
[30] | 991 | } |
---|
| 992 | |
---|
| 993 | |
---|
[145] | 994 | /****************************************************************************** |
---|
| 995 | * If !strict, the list may contain NULLs, VK records may point to NULL. |
---|
| 996 | ******************************************************************************/ |
---|
| 997 | REGFI_VALUE_LIST* regfi_load_valuelist(REGFI_FILE* file, uint32 offset, |
---|
[146] | 998 | uint32 num_values, uint32 max_size, |
---|
[145] | 999 | bool strict) |
---|
| 1000 | { |
---|
| 1001 | uint32 usable_num_values; |
---|
[30] | 1002 | |
---|
[145] | 1003 | if((num_values+1) * sizeof(uint32) > max_size) |
---|
| 1004 | { |
---|
| 1005 | regfi_add_message(file, REGFI_MSG_WARN, "Number of values indicated by" |
---|
| 1006 | " parent key (%d) would cause cell to straddle HBIN" |
---|
| 1007 | " boundary while loading value list at offset" |
---|
| 1008 | " 0x%.8X.", num_values, offset); |
---|
| 1009 | if(strict) |
---|
| 1010 | return NULL; |
---|
| 1011 | usable_num_values = max_size/sizeof(uint32) - sizeof(uint32); |
---|
| 1012 | } |
---|
| 1013 | else |
---|
| 1014 | usable_num_values = num_values; |
---|
| 1015 | |
---|
| 1016 | return regfi_parse_valuelist(file, offset, usable_num_values, strict); |
---|
| 1017 | } |
---|
| 1018 | |
---|
| 1019 | |
---|
| 1020 | |
---|
[146] | 1021 | /****************************************************************************** |
---|
| 1022 | * |
---|
| 1023 | ******************************************************************************/ |
---|
[135] | 1024 | REGFI_NK_REC* regfi_load_key(REGFI_FILE* file, uint32 offset, bool strict) |
---|
[30] | 1025 | { |
---|
[135] | 1026 | REGFI_NK_REC* nk; |
---|
[157] | 1027 | uint32 off; |
---|
| 1028 | int32 max_size; |
---|
[99] | 1029 | |
---|
[157] | 1030 | max_size = regfi_calc_maxsize(file, offset); |
---|
| 1031 | if (max_size < 0) |
---|
[105] | 1032 | return NULL; |
---|
[30] | 1033 | |
---|
[31] | 1034 | /* get the initial nk record */ |
---|
[157] | 1035 | if((nk = regfi_parse_nk(file, offset, max_size, true)) == NULL) |
---|
[135] | 1036 | { |
---|
[138] | 1037 | regfi_add_message(file, REGFI_MSG_ERROR, "Could not load NK record at" |
---|
[137] | 1038 | " offset 0x%.8X.", offset); |
---|
[99] | 1039 | return NULL; |
---|
[135] | 1040 | } |
---|
[30] | 1041 | |
---|
[146] | 1042 | /* get value list */ |
---|
[135] | 1043 | if(nk->num_values && (nk->values_off!=REGFI_OFFSET_NONE)) |
---|
[32] | 1044 | { |
---|
[157] | 1045 | off = nk->values_off + REGFI_REGF_SIZE; |
---|
| 1046 | max_size = regfi_calc_maxsize(file, off); |
---|
| 1047 | if(max_size < 0) |
---|
[32] | 1048 | { |
---|
[105] | 1049 | if(strict) |
---|
[32] | 1050 | { |
---|
[150] | 1051 | regfi_free_key(nk); |
---|
[99] | 1052 | return NULL; |
---|
[31] | 1053 | } |
---|
[105] | 1054 | else |
---|
| 1055 | nk->values = NULL; |
---|
[133] | 1056 | |
---|
[31] | 1057 | } |
---|
[105] | 1058 | else |
---|
[103] | 1059 | { |
---|
[157] | 1060 | nk->values = regfi_load_valuelist(file, off, nk->num_values, |
---|
| 1061 | max_size, true); |
---|
[145] | 1062 | if(nk->values == NULL) |
---|
[105] | 1063 | { |
---|
[145] | 1064 | regfi_add_message(file, REGFI_MSG_WARN, "Could not load value list" |
---|
| 1065 | " for NK record at offset 0x%.8X.", offset); |
---|
| 1066 | if(strict) |
---|
| 1067 | { |
---|
[150] | 1068 | regfi_free_key(nk); |
---|
[145] | 1069 | return NULL; |
---|
| 1070 | } |
---|
[105] | 1071 | } |
---|
[150] | 1072 | talloc_steal(nk, nk->values); |
---|
[103] | 1073 | } |
---|
[31] | 1074 | } |
---|
[105] | 1075 | |
---|
[146] | 1076 | /* now get subkey list */ |
---|
[135] | 1077 | if(nk->num_subkeys && (nk->subkeys_off != REGFI_OFFSET_NONE)) |
---|
[32] | 1078 | { |
---|
[157] | 1079 | off = nk->subkeys_off + REGFI_REGF_SIZE; |
---|
| 1080 | max_size = regfi_calc_maxsize(file, off); |
---|
| 1081 | if(max_size < 0) |
---|
[32] | 1082 | { |
---|
[105] | 1083 | if(strict) |
---|
[32] | 1084 | { |
---|
[150] | 1085 | regfi_free_key(nk); |
---|
[99] | 1086 | return NULL; |
---|
[31] | 1087 | } |
---|
[105] | 1088 | else |
---|
| 1089 | nk->subkeys = NULL; |
---|
[31] | 1090 | } |
---|
[105] | 1091 | else |
---|
[104] | 1092 | { |
---|
[134] | 1093 | nk->subkeys = regfi_load_subkeylist(file, off, nk->num_subkeys, |
---|
[157] | 1094 | max_size, true); |
---|
[134] | 1095 | |
---|
[105] | 1096 | if(nk->subkeys == NULL) |
---|
| 1097 | { |
---|
[140] | 1098 | regfi_add_message(file, REGFI_MSG_WARN, "Could not load subkey list" |
---|
| 1099 | " while parsing NK record at offset 0x%.8X.", offset); |
---|
[105] | 1100 | nk->num_subkeys = 0; |
---|
| 1101 | } |
---|
[150] | 1102 | talloc_steal(nk, nk->subkeys); |
---|
[104] | 1103 | } |
---|
[31] | 1104 | } |
---|
[30] | 1105 | |
---|
[99] | 1106 | return nk; |
---|
[30] | 1107 | } |
---|
| 1108 | |
---|
[32] | 1109 | |
---|
[102] | 1110 | /****************************************************************************** |
---|
| 1111 | ******************************************************************************/ |
---|
[146] | 1112 | const REGFI_SK_REC* regfi_load_sk(REGFI_FILE* file, uint32 offset, bool strict) |
---|
| 1113 | { |
---|
| 1114 | REGFI_SK_REC* ret_val = NULL; |
---|
[157] | 1115 | int32 max_size; |
---|
[147] | 1116 | void* failure_ptr = NULL; |
---|
| 1117 | |
---|
[146] | 1118 | /* First look if we have already parsed it */ |
---|
| 1119 | ret_val = (REGFI_SK_REC*)lru_cache_find(file->sk_cache, &offset, 4); |
---|
| 1120 | |
---|
| 1121 | /* Bail out if we have previously cached a parse failure at this offset. */ |
---|
| 1122 | if(ret_val == (void*)REGFI_OFFSET_NONE) |
---|
| 1123 | return NULL; |
---|
| 1124 | |
---|
| 1125 | if(ret_val == NULL) |
---|
| 1126 | { |
---|
[157] | 1127 | max_size = regfi_calc_maxsize(file, offset); |
---|
| 1128 | if(max_size < 0) |
---|
[146] | 1129 | return NULL; |
---|
| 1130 | |
---|
[157] | 1131 | ret_val = regfi_parse_sk(file, offset, max_size, strict); |
---|
[146] | 1132 | if(ret_val == NULL) |
---|
| 1133 | { /* Cache the parse failure and bail out. */ |
---|
[147] | 1134 | failure_ptr = talloc(NULL, uint32_t); |
---|
| 1135 | if(failure_ptr == NULL) |
---|
| 1136 | return NULL; |
---|
| 1137 | *(uint32_t*)failure_ptr = REGFI_OFFSET_NONE; |
---|
| 1138 | lru_cache_update(file->sk_cache, &offset, 4, failure_ptr); |
---|
[146] | 1139 | return NULL; |
---|
| 1140 | } |
---|
| 1141 | |
---|
| 1142 | lru_cache_update(file->sk_cache, &offset, 4, ret_val); |
---|
| 1143 | } |
---|
| 1144 | |
---|
| 1145 | return ret_val; |
---|
| 1146 | } |
---|
| 1147 | |
---|
| 1148 | |
---|
| 1149 | |
---|
| 1150 | /****************************************************************************** |
---|
| 1151 | ******************************************************************************/ |
---|
| 1152 | static bool regfi_find_root_nk(REGFI_FILE* file, uint32 offset,uint32 hbin_size, |
---|
[102] | 1153 | uint32* root_offset) |
---|
[30] | 1154 | { |
---|
[102] | 1155 | uint8 tmp[4]; |
---|
| 1156 | int32 record_size; |
---|
| 1157 | uint32 length, hbin_offset = 0; |
---|
[135] | 1158 | REGFI_NK_REC* nk = NULL; |
---|
[31] | 1159 | bool found = false; |
---|
[30] | 1160 | |
---|
[102] | 1161 | for(record_size=0; !found && (hbin_offset < hbin_size); ) |
---|
[32] | 1162 | { |
---|
[102] | 1163 | if(lseek(file->fd, offset+hbin_offset, SEEK_SET) == -1) |
---|
[31] | 1164 | return false; |
---|
[102] | 1165 | |
---|
| 1166 | length = 4; |
---|
| 1167 | if((regfi_read(file->fd, tmp, &length) != 0) || length != 4) |
---|
[31] | 1168 | return false; |
---|
[102] | 1169 | record_size = IVALS(tmp, 0); |
---|
[30] | 1170 | |
---|
[102] | 1171 | if(record_size < 0) |
---|
| 1172 | { |
---|
| 1173 | record_size = record_size*(-1); |
---|
| 1174 | nk = regfi_parse_nk(file, offset+hbin_offset, hbin_size-hbin_offset, true); |
---|
| 1175 | if(nk != NULL) |
---|
| 1176 | { |
---|
[152] | 1177 | if(nk->key_type & REGFI_NK_FLAG_ROOT) |
---|
[102] | 1178 | { |
---|
| 1179 | found = true; |
---|
| 1180 | *root_offset = nk->offset; |
---|
| 1181 | } |
---|
[150] | 1182 | regfi_free_key(nk); |
---|
[102] | 1183 | } |
---|
[31] | 1184 | } |
---|
[30] | 1185 | |
---|
[102] | 1186 | hbin_offset += record_size; |
---|
[31] | 1187 | } |
---|
[32] | 1188 | |
---|
[102] | 1189 | return found; |
---|
[30] | 1190 | } |
---|
| 1191 | |
---|
| 1192 | |
---|
| 1193 | /******************************************************************* |
---|
[97] | 1194 | * Open the registry file and then read in the REGF block to get the |
---|
| 1195 | * first hbin offset. |
---|
| 1196 | *******************************************************************/ |
---|
[135] | 1197 | REGFI_FILE* regfi_open(const char* filename) |
---|
[30] | 1198 | { |
---|
[137] | 1199 | struct stat sbuf; |
---|
[135] | 1200 | REGFI_FILE* rb; |
---|
| 1201 | REGFI_HBIN* hbin = NULL; |
---|
[146] | 1202 | uint32 hbin_off, file_length, cache_secret; |
---|
[97] | 1203 | int fd; |
---|
[110] | 1204 | bool rla; |
---|
[30] | 1205 | |
---|
[97] | 1206 | /* open an existing file */ |
---|
[143] | 1207 | if ((fd = open(filename, REGFI_OPEN_FLAGS)) == -1) |
---|
[97] | 1208 | { |
---|
[143] | 1209 | /* fprintf(stderr, "regfi_open: failure to open %s (%s)\n", filename, strerror(errno));*/ |
---|
[31] | 1210 | return NULL; |
---|
| 1211 | } |
---|
[99] | 1212 | |
---|
[137] | 1213 | /* Determine file length. Must be at least big enough |
---|
| 1214 | * for the header and one hbin. |
---|
| 1215 | */ |
---|
| 1216 | if (fstat(fd, &sbuf) == -1) |
---|
| 1217 | return NULL; |
---|
| 1218 | file_length = sbuf.st_size; |
---|
| 1219 | if(file_length < REGFI_REGF_SIZE+REGFI_HBIN_ALLOC) |
---|
| 1220 | return NULL; |
---|
| 1221 | |
---|
[31] | 1222 | /* read in an existing file */ |
---|
[97] | 1223 | if ((rb = regfi_parse_regf(fd, true)) == NULL) |
---|
| 1224 | { |
---|
[143] | 1225 | /* fprintf(stderr, "regfi_open: Failed to read initial REGF block\n"); */ |
---|
[97] | 1226 | close(fd); |
---|
[31] | 1227 | return NULL; |
---|
| 1228 | } |
---|
[137] | 1229 | rb->file_length = file_length; |
---|
| 1230 | |
---|
[99] | 1231 | rb->hbins = range_list_new(); |
---|
[110] | 1232 | if(rb->hbins == NULL) |
---|
[99] | 1233 | { |
---|
[143] | 1234 | /* fprintf(stderr, "regfi_open: Failed to create HBIN list.\n"); */ |
---|
[99] | 1235 | close(fd); |
---|
[150] | 1236 | talloc_free(rb); |
---|
[99] | 1237 | return NULL; |
---|
| 1238 | } |
---|
[150] | 1239 | talloc_steal(rb, rb->hbins); |
---|
| 1240 | |
---|
[106] | 1241 | rla = true; |
---|
[135] | 1242 | hbin_off = REGFI_REGF_SIZE; |
---|
[110] | 1243 | hbin = regfi_parse_hbin(rb, hbin_off, true); |
---|
[106] | 1244 | while(hbin && rla) |
---|
| 1245 | { |
---|
[137] | 1246 | rla = range_list_add(rb->hbins, hbin->file_off, hbin->block_size, hbin); |
---|
[148] | 1247 | if(rla) |
---|
| 1248 | talloc_steal(rb->hbins, hbin); |
---|
[106] | 1249 | hbin_off = hbin->file_off + hbin->block_size; |
---|
[110] | 1250 | hbin = regfi_parse_hbin(rb, hbin_off, true); |
---|
[106] | 1251 | } |
---|
| 1252 | |
---|
[146] | 1253 | /* This secret isn't very secret, but we don't need a good one. This |
---|
| 1254 | * secret is just designed to prevent someone from trying to blow our |
---|
| 1255 | * caching and make things slow. |
---|
| 1256 | */ |
---|
| 1257 | cache_secret = 0x15DEAD05^time(NULL)^(getpid()<<16); |
---|
| 1258 | |
---|
| 1259 | /* Cache an unlimited number of SK records. Typically there are very few. */ |
---|
[150] | 1260 | rb->sk_cache = lru_cache_create_ctx(rb, 0, cache_secret, true); |
---|
[146] | 1261 | |
---|
[138] | 1262 | /* Default message mask */ |
---|
| 1263 | rb->msg_mask = REGFI_MSG_ERROR|REGFI_MSG_WARN; |
---|
| 1264 | |
---|
[31] | 1265 | /* success */ |
---|
| 1266 | return rb; |
---|
[30] | 1267 | } |
---|
| 1268 | |
---|
| 1269 | |
---|
[148] | 1270 | /****************************************************************************** |
---|
| 1271 | ******************************************************************************/ |
---|
[146] | 1272 | int regfi_close(REGFI_FILE *file) |
---|
[30] | 1273 | { |
---|
[31] | 1274 | int fd; |
---|
[30] | 1275 | |
---|
[31] | 1276 | /* nothing to do if there is no open file */ |
---|
[99] | 1277 | if ((file == NULL) || (file->fd == -1)) |
---|
| 1278 | return 0; |
---|
[30] | 1279 | |
---|
[31] | 1280 | fd = file->fd; |
---|
| 1281 | file->fd = -1; |
---|
[148] | 1282 | |
---|
[99] | 1283 | range_list_free(file->hbins); |
---|
[106] | 1284 | |
---|
[146] | 1285 | if(file->sk_cache != NULL) |
---|
| 1286 | lru_cache_destroy(file->sk_cache); |
---|
[148] | 1287 | |
---|
[150] | 1288 | talloc_free(file); |
---|
[106] | 1289 | return close(fd); |
---|
[30] | 1290 | } |
---|
| 1291 | |
---|
| 1292 | |
---|
[80] | 1293 | /****************************************************************************** |
---|
| 1294 | * There should be only *one* root key in the registry file based |
---|
| 1295 | * on my experience. --jerry |
---|
[148] | 1296 | ******************************************************************************/ |
---|
[135] | 1297 | REGFI_NK_REC* regfi_rootkey(REGFI_FILE *file) |
---|
[30] | 1298 | { |
---|
[135] | 1299 | REGFI_NK_REC* nk = NULL; |
---|
[146] | 1300 | REGFI_HBIN* hbin; |
---|
| 1301 | uint32 root_offset, i, num_hbins; |
---|
[99] | 1302 | |
---|
| 1303 | if(!file) |
---|
[31] | 1304 | return NULL; |
---|
[99] | 1305 | |
---|
[102] | 1306 | /* Scan through the file one HBIN block at a time looking |
---|
[146] | 1307 | * for an NK record with a root key type. |
---|
| 1308 | * This is typically the first NK record in the first HBIN |
---|
| 1309 | * block (but we're not assuming that generally). |
---|
| 1310 | */ |
---|
[107] | 1311 | num_hbins = range_list_size(file->hbins); |
---|
| 1312 | for(i=0; i < num_hbins; i++) |
---|
[99] | 1313 | { |
---|
[135] | 1314 | hbin = (REGFI_HBIN*)range_list_get(file->hbins, i)->data; |
---|
| 1315 | if(regfi_find_root_nk(file, hbin->file_off+REGFI_HBIN_HEADER_SIZE, |
---|
| 1316 | hbin->block_size-REGFI_HBIN_HEADER_SIZE, &root_offset)) |
---|
[99] | 1317 | { |
---|
[105] | 1318 | nk = regfi_load_key(file, root_offset, true); |
---|
[102] | 1319 | break; |
---|
[31] | 1320 | } |
---|
| 1321 | } |
---|
[30] | 1322 | |
---|
[80] | 1323 | return nk; |
---|
[30] | 1324 | } |
---|
| 1325 | |
---|
| 1326 | |
---|
[80] | 1327 | /****************************************************************************** |
---|
| 1328 | *****************************************************************************/ |
---|
[150] | 1329 | void regfi_free_key(REGFI_NK_REC* nk) |
---|
[30] | 1330 | { |
---|
[127] | 1331 | regfi_subkeylist_free(nk->subkeys); |
---|
[150] | 1332 | talloc_free(nk); |
---|
| 1333 | } |
---|
[127] | 1334 | |
---|
[80] | 1335 | |
---|
[150] | 1336 | /****************************************************************************** |
---|
| 1337 | *****************************************************************************/ |
---|
| 1338 | void regfi_free_value(REGFI_VK_REC* vk) |
---|
| 1339 | { |
---|
| 1340 | talloc_free(vk); |
---|
[80] | 1341 | } |
---|
| 1342 | |
---|
| 1343 | |
---|
| 1344 | /****************************************************************************** |
---|
| 1345 | *****************************************************************************/ |
---|
[135] | 1346 | void regfi_subkeylist_free(REGFI_SUBKEY_LIST* list) |
---|
[127] | 1347 | { |
---|
| 1348 | if(list != NULL) |
---|
| 1349 | { |
---|
[150] | 1350 | talloc_free(list); |
---|
[127] | 1351 | } |
---|
| 1352 | } |
---|
| 1353 | |
---|
| 1354 | |
---|
| 1355 | /****************************************************************************** |
---|
| 1356 | *****************************************************************************/ |
---|
[135] | 1357 | REGFI_ITERATOR* regfi_iterator_new(REGFI_FILE* fh) |
---|
[80] | 1358 | { |
---|
[135] | 1359 | REGFI_NK_REC* root; |
---|
[150] | 1360 | REGFI_ITERATOR* ret_val = talloc(NULL, REGFI_ITERATOR); |
---|
[80] | 1361 | if(ret_val == NULL) |
---|
| 1362 | return NULL; |
---|
| 1363 | |
---|
[81] | 1364 | root = regfi_rootkey(fh); |
---|
[80] | 1365 | if(root == NULL) |
---|
| 1366 | { |
---|
[150] | 1367 | talloc_free(ret_val); |
---|
[80] | 1368 | return NULL; |
---|
| 1369 | } |
---|
| 1370 | |
---|
[135] | 1371 | ret_val->key_positions = void_stack_new(REGFI_MAX_DEPTH); |
---|
[80] | 1372 | if(ret_val->key_positions == NULL) |
---|
| 1373 | { |
---|
[150] | 1374 | talloc_free(ret_val); |
---|
[80] | 1375 | return NULL; |
---|
| 1376 | } |
---|
[150] | 1377 | talloc_steal(ret_val, ret_val->key_positions); |
---|
[80] | 1378 | |
---|
| 1379 | ret_val->f = fh; |
---|
| 1380 | ret_val->cur_key = root; |
---|
| 1381 | ret_val->cur_subkey = 0; |
---|
| 1382 | ret_val->cur_value = 0; |
---|
| 1383 | |
---|
| 1384 | return ret_val; |
---|
| 1385 | } |
---|
| 1386 | |
---|
| 1387 | |
---|
| 1388 | /****************************************************************************** |
---|
| 1389 | *****************************************************************************/ |
---|
| 1390 | void regfi_iterator_free(REGFI_ITERATOR* i) |
---|
| 1391 | { |
---|
[150] | 1392 | talloc_free(i); |
---|
[80] | 1393 | } |
---|
| 1394 | |
---|
| 1395 | |
---|
| 1396 | |
---|
| 1397 | /****************************************************************************** |
---|
| 1398 | *****************************************************************************/ |
---|
| 1399 | /* XXX: some way of indicating reason for failure should be added. */ |
---|
| 1400 | bool regfi_iterator_down(REGFI_ITERATOR* i) |
---|
| 1401 | { |
---|
[135] | 1402 | REGFI_NK_REC* subkey; |
---|
[80] | 1403 | REGFI_ITER_POSITION* pos; |
---|
| 1404 | |
---|
[150] | 1405 | pos = talloc(i->key_positions, REGFI_ITER_POSITION); |
---|
[80] | 1406 | if(pos == NULL) |
---|
| 1407 | return false; |
---|
| 1408 | |
---|
[135] | 1409 | subkey = (REGFI_NK_REC*)regfi_iterator_cur_subkey(i); |
---|
[80] | 1410 | if(subkey == NULL) |
---|
| 1411 | { |
---|
[150] | 1412 | talloc_free(pos); |
---|
[80] | 1413 | return false; |
---|
| 1414 | } |
---|
| 1415 | |
---|
| 1416 | pos->nk = i->cur_key; |
---|
| 1417 | pos->cur_subkey = i->cur_subkey; |
---|
| 1418 | if(!void_stack_push(i->key_positions, pos)) |
---|
| 1419 | { |
---|
[150] | 1420 | talloc_free(pos); |
---|
| 1421 | regfi_free_key(subkey); |
---|
[80] | 1422 | return false; |
---|
| 1423 | } |
---|
[150] | 1424 | talloc_steal(i, subkey); |
---|
[80] | 1425 | |
---|
| 1426 | i->cur_key = subkey; |
---|
| 1427 | i->cur_subkey = 0; |
---|
| 1428 | i->cur_value = 0; |
---|
| 1429 | |
---|
| 1430 | return true; |
---|
| 1431 | } |
---|
| 1432 | |
---|
| 1433 | |
---|
| 1434 | /****************************************************************************** |
---|
| 1435 | *****************************************************************************/ |
---|
| 1436 | bool regfi_iterator_up(REGFI_ITERATOR* i) |
---|
| 1437 | { |
---|
| 1438 | REGFI_ITER_POSITION* pos; |
---|
| 1439 | |
---|
| 1440 | pos = (REGFI_ITER_POSITION*)void_stack_pop(i->key_positions); |
---|
| 1441 | if(pos == NULL) |
---|
| 1442 | return false; |
---|
| 1443 | |
---|
[150] | 1444 | regfi_free_key(i->cur_key); |
---|
[80] | 1445 | i->cur_key = pos->nk; |
---|
| 1446 | i->cur_subkey = pos->cur_subkey; |
---|
| 1447 | i->cur_value = 0; |
---|
[150] | 1448 | talloc_free(pos); |
---|
[80] | 1449 | |
---|
| 1450 | return true; |
---|
| 1451 | } |
---|
| 1452 | |
---|
| 1453 | |
---|
| 1454 | /****************************************************************************** |
---|
| 1455 | *****************************************************************************/ |
---|
| 1456 | bool regfi_iterator_to_root(REGFI_ITERATOR* i) |
---|
| 1457 | { |
---|
| 1458 | while(regfi_iterator_up(i)) |
---|
| 1459 | continue; |
---|
| 1460 | |
---|
| 1461 | return true; |
---|
| 1462 | } |
---|
| 1463 | |
---|
| 1464 | |
---|
| 1465 | /****************************************************************************** |
---|
| 1466 | *****************************************************************************/ |
---|
| 1467 | bool regfi_iterator_find_subkey(REGFI_ITERATOR* i, const char* subkey_name) |
---|
| 1468 | { |
---|
[135] | 1469 | REGFI_NK_REC* subkey; |
---|
[80] | 1470 | bool found = false; |
---|
| 1471 | uint32 old_subkey = i->cur_subkey; |
---|
[133] | 1472 | |
---|
[80] | 1473 | if(subkey_name == NULL) |
---|
| 1474 | return false; |
---|
| 1475 | |
---|
| 1476 | /* XXX: this alloc/free of each sub key might be a bit excessive */ |
---|
[135] | 1477 | subkey = (REGFI_NK_REC*)regfi_iterator_first_subkey(i); |
---|
[80] | 1478 | while((subkey != NULL) && (found == false)) |
---|
| 1479 | { |
---|
| 1480 | if(subkey->keyname != NULL |
---|
| 1481 | && strcasecmp(subkey->keyname, subkey_name) == 0) |
---|
| 1482 | found = true; |
---|
[82] | 1483 | else |
---|
| 1484 | { |
---|
[150] | 1485 | regfi_free_key(subkey); |
---|
[135] | 1486 | subkey = (REGFI_NK_REC*)regfi_iterator_next_subkey(i); |
---|
[82] | 1487 | } |
---|
[80] | 1488 | } |
---|
| 1489 | |
---|
| 1490 | if(found == false) |
---|
| 1491 | { |
---|
| 1492 | i->cur_subkey = old_subkey; |
---|
| 1493 | return false; |
---|
| 1494 | } |
---|
| 1495 | |
---|
[150] | 1496 | regfi_free_key(subkey); |
---|
[80] | 1497 | return true; |
---|
| 1498 | } |
---|
| 1499 | |
---|
| 1500 | |
---|
| 1501 | /****************************************************************************** |
---|
| 1502 | *****************************************************************************/ |
---|
| 1503 | bool regfi_iterator_walk_path(REGFI_ITERATOR* i, const char** path) |
---|
| 1504 | { |
---|
| 1505 | uint32 x; |
---|
| 1506 | if(path == NULL) |
---|
| 1507 | return false; |
---|
| 1508 | |
---|
| 1509 | for(x=0; |
---|
| 1510 | ((path[x] != NULL) && regfi_iterator_find_subkey(i, path[x]) |
---|
| 1511 | && regfi_iterator_down(i)); |
---|
| 1512 | x++) |
---|
| 1513 | { continue; } |
---|
| 1514 | |
---|
| 1515 | if(path[x] == NULL) |
---|
| 1516 | return true; |
---|
| 1517 | |
---|
| 1518 | /* XXX: is this the right number of times? */ |
---|
| 1519 | for(; x > 0; x--) |
---|
| 1520 | regfi_iterator_up(i); |
---|
| 1521 | |
---|
| 1522 | return false; |
---|
| 1523 | } |
---|
| 1524 | |
---|
| 1525 | |
---|
| 1526 | /****************************************************************************** |
---|
| 1527 | *****************************************************************************/ |
---|
[135] | 1528 | const REGFI_NK_REC* regfi_iterator_cur_key(REGFI_ITERATOR* i) |
---|
[80] | 1529 | { |
---|
| 1530 | return i->cur_key; |
---|
| 1531 | } |
---|
| 1532 | |
---|
| 1533 | |
---|
| 1534 | /****************************************************************************** |
---|
| 1535 | *****************************************************************************/ |
---|
[135] | 1536 | const REGFI_SK_REC* regfi_iterator_cur_sk(REGFI_ITERATOR* i) |
---|
[109] | 1537 | { |
---|
[146] | 1538 | if(i->cur_key == NULL || i->cur_key->sk_off == REGFI_OFFSET_NONE) |
---|
[109] | 1539 | return NULL; |
---|
| 1540 | |
---|
[146] | 1541 | return regfi_load_sk(i->f, i->cur_key->sk_off + REGFI_REGF_SIZE, true); |
---|
[109] | 1542 | } |
---|
| 1543 | |
---|
| 1544 | |
---|
| 1545 | /****************************************************************************** |
---|
| 1546 | *****************************************************************************/ |
---|
[150] | 1547 | REGFI_NK_REC* regfi_iterator_first_subkey(REGFI_ITERATOR* i) |
---|
[80] | 1548 | { |
---|
| 1549 | i->cur_subkey = 0; |
---|
| 1550 | return regfi_iterator_cur_subkey(i); |
---|
| 1551 | } |
---|
| 1552 | |
---|
| 1553 | |
---|
| 1554 | /****************************************************************************** |
---|
| 1555 | *****************************************************************************/ |
---|
[150] | 1556 | REGFI_NK_REC* regfi_iterator_cur_subkey(REGFI_ITERATOR* i) |
---|
[80] | 1557 | { |
---|
| 1558 | uint32 nk_offset; |
---|
| 1559 | |
---|
[31] | 1560 | /* see if there is anything left to report */ |
---|
[135] | 1561 | if (!(i->cur_key) || (i->cur_key->subkeys_off==REGFI_OFFSET_NONE) |
---|
[80] | 1562 | || (i->cur_subkey >= i->cur_key->num_subkeys)) |
---|
[31] | 1563 | return NULL; |
---|
[30] | 1564 | |
---|
[139] | 1565 | nk_offset = i->cur_key->subkeys->elements[i->cur_subkey].offset; |
---|
[133] | 1566 | |
---|
[135] | 1567 | return regfi_load_key(i->f, nk_offset+REGFI_REGF_SIZE, true); |
---|
[30] | 1568 | } |
---|
[80] | 1569 | |
---|
| 1570 | |
---|
| 1571 | /****************************************************************************** |
---|
| 1572 | *****************************************************************************/ |
---|
| 1573 | /* XXX: some way of indicating reason for failure should be added. */ |
---|
[150] | 1574 | REGFI_NK_REC* regfi_iterator_next_subkey(REGFI_ITERATOR* i) |
---|
[80] | 1575 | { |
---|
[150] | 1576 | REGFI_NK_REC* subkey; |
---|
[80] | 1577 | |
---|
| 1578 | i->cur_subkey++; |
---|
| 1579 | subkey = regfi_iterator_cur_subkey(i); |
---|
| 1580 | |
---|
| 1581 | if(subkey == NULL) |
---|
| 1582 | i->cur_subkey--; |
---|
| 1583 | |
---|
| 1584 | return subkey; |
---|
| 1585 | } |
---|
| 1586 | |
---|
| 1587 | |
---|
| 1588 | /****************************************************************************** |
---|
| 1589 | *****************************************************************************/ |
---|
| 1590 | bool regfi_iterator_find_value(REGFI_ITERATOR* i, const char* value_name) |
---|
| 1591 | { |
---|
[150] | 1592 | REGFI_VK_REC* cur; |
---|
[80] | 1593 | bool found = false; |
---|
| 1594 | |
---|
| 1595 | /* XXX: cur->valuename can be NULL in the registry. |
---|
| 1596 | * Should we allow for a way to search for that? |
---|
| 1597 | */ |
---|
| 1598 | if(value_name == NULL) |
---|
| 1599 | return false; |
---|
| 1600 | |
---|
| 1601 | cur = regfi_iterator_first_value(i); |
---|
| 1602 | while((cur != NULL) && (found == false)) |
---|
| 1603 | { |
---|
| 1604 | if((cur->valuename != NULL) |
---|
| 1605 | && (strcasecmp(cur->valuename, value_name) == 0)) |
---|
| 1606 | found = true; |
---|
[95] | 1607 | else |
---|
[150] | 1608 | { |
---|
| 1609 | regfi_free_value(cur); |
---|
[95] | 1610 | cur = regfi_iterator_next_value(i); |
---|
[150] | 1611 | } |
---|
[80] | 1612 | } |
---|
| 1613 | |
---|
[94] | 1614 | return found; |
---|
[80] | 1615 | } |
---|
| 1616 | |
---|
| 1617 | |
---|
| 1618 | /****************************************************************************** |
---|
| 1619 | *****************************************************************************/ |
---|
[150] | 1620 | REGFI_VK_REC* regfi_iterator_first_value(REGFI_ITERATOR* i) |
---|
[80] | 1621 | { |
---|
| 1622 | i->cur_value = 0; |
---|
| 1623 | return regfi_iterator_cur_value(i); |
---|
| 1624 | } |
---|
| 1625 | |
---|
| 1626 | |
---|
| 1627 | /****************************************************************************** |
---|
| 1628 | *****************************************************************************/ |
---|
[150] | 1629 | REGFI_VK_REC* regfi_iterator_cur_value(REGFI_ITERATOR* i) |
---|
[80] | 1630 | { |
---|
[150] | 1631 | REGFI_VK_REC* ret_val = NULL; |
---|
[145] | 1632 | uint32 voffset; |
---|
[80] | 1633 | |
---|
[145] | 1634 | if(i->cur_key->values != NULL && i->cur_key->values->elements != NULL) |
---|
| 1635 | { |
---|
| 1636 | if(i->cur_value < i->cur_key->values->num_values) |
---|
| 1637 | { |
---|
| 1638 | voffset = i->cur_key->values->elements[i->cur_value]; |
---|
| 1639 | ret_val = regfi_load_value(i->f, voffset+REGFI_REGF_SIZE, true); |
---|
| 1640 | } |
---|
| 1641 | } |
---|
| 1642 | |
---|
[80] | 1643 | return ret_val; |
---|
| 1644 | } |
---|
| 1645 | |
---|
| 1646 | |
---|
| 1647 | /****************************************************************************** |
---|
| 1648 | *****************************************************************************/ |
---|
[150] | 1649 | REGFI_VK_REC* regfi_iterator_next_value(REGFI_ITERATOR* i) |
---|
[80] | 1650 | { |
---|
[150] | 1651 | REGFI_VK_REC* ret_val; |
---|
[80] | 1652 | |
---|
| 1653 | i->cur_value++; |
---|
| 1654 | ret_val = regfi_iterator_cur_value(i); |
---|
| 1655 | if(ret_val == NULL) |
---|
| 1656 | i->cur_value--; |
---|
| 1657 | |
---|
| 1658 | return ret_val; |
---|
| 1659 | } |
---|
[97] | 1660 | |
---|
| 1661 | |
---|
| 1662 | /******************************************************************* |
---|
| 1663 | * Computes the checksum of the registry file header. |
---|
| 1664 | * buffer must be at least the size of an regf header (4096 bytes). |
---|
| 1665 | *******************************************************************/ |
---|
| 1666 | static uint32 regfi_compute_header_checksum(uint8* buffer) |
---|
| 1667 | { |
---|
| 1668 | uint32 checksum, x; |
---|
| 1669 | int i; |
---|
| 1670 | |
---|
| 1671 | /* XOR of all bytes 0x0000 - 0x01FB */ |
---|
| 1672 | |
---|
| 1673 | checksum = x = 0; |
---|
| 1674 | |
---|
| 1675 | for ( i=0; i<0x01FB; i+=4 ) { |
---|
| 1676 | x = IVAL(buffer, i ); |
---|
| 1677 | checksum ^= x; |
---|
| 1678 | } |
---|
| 1679 | |
---|
| 1680 | return checksum; |
---|
| 1681 | } |
---|
| 1682 | |
---|
| 1683 | |
---|
| 1684 | /******************************************************************* |
---|
[116] | 1685 | * XXX: Add way to return more detailed error information. |
---|
[97] | 1686 | *******************************************************************/ |
---|
[135] | 1687 | REGFI_FILE* regfi_parse_regf(int fd, bool strict) |
---|
[97] | 1688 | { |
---|
[135] | 1689 | uint8 file_header[REGFI_REGF_SIZE]; |
---|
[102] | 1690 | uint32 length; |
---|
[135] | 1691 | REGFI_FILE* ret_val; |
---|
[97] | 1692 | |
---|
[150] | 1693 | ret_val = talloc(NULL, REGFI_FILE); |
---|
[97] | 1694 | if(ret_val == NULL) |
---|
| 1695 | return NULL; |
---|
| 1696 | |
---|
| 1697 | ret_val->fd = fd; |
---|
[150] | 1698 | ret_val->sk_cache = NULL; |
---|
| 1699 | ret_val->last_message = NULL; |
---|
| 1700 | ret_val->hbins = NULL; |
---|
| 1701 | |
---|
[135] | 1702 | length = REGFI_REGF_SIZE; |
---|
[150] | 1703 | if((regfi_read(fd, file_header, &length)) != 0 || length != REGFI_REGF_SIZE) |
---|
| 1704 | goto fail; |
---|
| 1705 | |
---|
[97] | 1706 | ret_val->checksum = IVAL(file_header, 0x1FC); |
---|
| 1707 | ret_val->computed_checksum = regfi_compute_header_checksum(file_header); |
---|
| 1708 | if (strict && (ret_val->checksum != ret_val->computed_checksum)) |
---|
[150] | 1709 | goto fail; |
---|
[97] | 1710 | |
---|
[135] | 1711 | memcpy(ret_val->magic, file_header, REGFI_REGF_MAGIC_SIZE); |
---|
[150] | 1712 | if(memcmp(ret_val->magic, "regf", REGFI_REGF_MAGIC_SIZE) != 0) |
---|
[97] | 1713 | { |
---|
[150] | 1714 | if(strict) |
---|
| 1715 | goto fail; |
---|
| 1716 | regfi_add_message(ret_val, REGFI_MSG_WARN, "Magic number mismatch " |
---|
| 1717 | "(%.2X %.2X %.2X %.2X) while parsing hive header", |
---|
[151] | 1718 | ret_val->magic[0], ret_val->magic[1], |
---|
[150] | 1719 | ret_val->magic[2], ret_val->magic[3]); |
---|
[97] | 1720 | } |
---|
[151] | 1721 | ret_val->sequence1 = IVAL(file_header, 0x4); |
---|
| 1722 | ret_val->sequence2 = IVAL(file_header, 0x8); |
---|
[97] | 1723 | ret_val->mtime.low = IVAL(file_header, 0xC); |
---|
| 1724 | ret_val->mtime.high = IVAL(file_header, 0x10); |
---|
[151] | 1725 | ret_val->major_version = IVAL(file_header, 0x14); |
---|
| 1726 | ret_val->minor_version = IVAL(file_header, 0x18); |
---|
| 1727 | ret_val->type = IVAL(file_header, 0x1C); |
---|
| 1728 | ret_val->format = IVAL(file_header, 0x20); |
---|
| 1729 | ret_val->root_cell = IVAL(file_header, 0x24); |
---|
[97] | 1730 | ret_val->last_block = IVAL(file_header, 0x28); |
---|
| 1731 | |
---|
[151] | 1732 | ret_val->cluster = IVAL(file_header, 0x2C); |
---|
[97] | 1733 | |
---|
[151] | 1734 | memcpy(ret_val->file_name, file_header+0x30, REGFI_REGF_NAME_SIZE); |
---|
| 1735 | |
---|
| 1736 | /* XXX: Should we add a warning if these uuid parsers fail? Can they? */ |
---|
| 1737 | ret_val->rm_id = winsec_parse_uuid(ret_val, file_header+0x70, 16); |
---|
| 1738 | ret_val->log_id = winsec_parse_uuid(ret_val, file_header+0x80, 16); |
---|
| 1739 | ret_val->flags = IVAL(file_header, 0x90); |
---|
| 1740 | ret_val->tm_id = winsec_parse_uuid(ret_val, file_header+0x94, 16); |
---|
| 1741 | ret_val->guid_signature = IVAL(file_header, 0xa4); |
---|
| 1742 | |
---|
| 1743 | memcpy(ret_val->reserved1, file_header+0xa8, REGFI_REGF_RESERVED1_SIZE); |
---|
| 1744 | memcpy(ret_val->reserved2, file_header+0x200, REGFI_REGF_RESERVED2_SIZE); |
---|
| 1745 | |
---|
| 1746 | ret_val->thaw_tm_id = winsec_parse_uuid(ret_val, file_header+0xFC8, 16); |
---|
| 1747 | ret_val->thaw_rm_id = winsec_parse_uuid(ret_val, file_header+0xFD8, 16); |
---|
| 1748 | ret_val->thaw_log_id = winsec_parse_uuid(ret_val, file_header+0xFE8, 16); |
---|
[152] | 1749 | ret_val->boot_type = IVAL(file_header, 0xFF8); |
---|
| 1750 | ret_val->boot_recover = IVAL(file_header, 0xFFC); |
---|
[151] | 1751 | |
---|
[97] | 1752 | return ret_val; |
---|
[150] | 1753 | |
---|
| 1754 | fail: |
---|
| 1755 | talloc_free(ret_val); |
---|
| 1756 | return NULL; |
---|
[97] | 1757 | } |
---|
| 1758 | |
---|
| 1759 | |
---|
| 1760 | |
---|
[148] | 1761 | /****************************************************************************** |
---|
[97] | 1762 | * Given real file offset, read and parse the hbin at that location |
---|
[110] | 1763 | * along with it's associated cells. |
---|
[148] | 1764 | ******************************************************************************/ |
---|
[135] | 1765 | REGFI_HBIN* regfi_parse_hbin(REGFI_FILE* file, uint32 offset, bool strict) |
---|
[97] | 1766 | { |
---|
[135] | 1767 | REGFI_HBIN *hbin; |
---|
| 1768 | uint8 hbin_header[REGFI_HBIN_HEADER_SIZE]; |
---|
[110] | 1769 | uint32 length; |
---|
[99] | 1770 | |
---|
| 1771 | if(offset >= file->file_length) |
---|
| 1772 | return NULL; |
---|
[97] | 1773 | |
---|
| 1774 | if(lseek(file->fd, offset, SEEK_SET) == -1) |
---|
[137] | 1775 | { |
---|
[138] | 1776 | regfi_add_message(file, REGFI_MSG_ERROR, "Seek failed" |
---|
[137] | 1777 | " while parsing hbin at offset 0x%.8X.", offset); |
---|
[97] | 1778 | return NULL; |
---|
[137] | 1779 | } |
---|
[97] | 1780 | |
---|
[135] | 1781 | length = REGFI_HBIN_HEADER_SIZE; |
---|
[97] | 1782 | if((regfi_read(file->fd, hbin_header, &length) != 0) |
---|
[135] | 1783 | || length != REGFI_HBIN_HEADER_SIZE) |
---|
[97] | 1784 | return NULL; |
---|
| 1785 | |
---|
| 1786 | if(lseek(file->fd, offset, SEEK_SET) == -1) |
---|
[137] | 1787 | { |
---|
[138] | 1788 | regfi_add_message(file, REGFI_MSG_ERROR, "Seek failed" |
---|
[137] | 1789 | " while parsing hbin at offset 0x%.8X.", offset); |
---|
[97] | 1790 | return NULL; |
---|
[137] | 1791 | } |
---|
[97] | 1792 | |
---|
[148] | 1793 | hbin = talloc(NULL, REGFI_HBIN); |
---|
| 1794 | if(hbin == NULL) |
---|
[99] | 1795 | return NULL; |
---|
| 1796 | hbin->file_off = offset; |
---|
| 1797 | |
---|
[97] | 1798 | memcpy(hbin->magic, hbin_header, 4); |
---|
| 1799 | if(strict && (memcmp(hbin->magic, "hbin", 4) != 0)) |
---|
[99] | 1800 | { |
---|
[138] | 1801 | regfi_add_message(file, REGFI_MSG_INFO, "Magic number mismatch " |
---|
| 1802 | "(%.2X %.2X %.2X %.2X) while parsing hbin at offset" |
---|
| 1803 | " 0x%.8X.", hbin->magic[0], hbin->magic[1], |
---|
| 1804 | hbin->magic[2], hbin->magic[3], offset); |
---|
[148] | 1805 | talloc_free(hbin); |
---|
[97] | 1806 | return NULL; |
---|
[99] | 1807 | } |
---|
[97] | 1808 | |
---|
| 1809 | hbin->first_hbin_off = IVAL(hbin_header, 0x4); |
---|
| 1810 | hbin->block_size = IVAL(hbin_header, 0x8); |
---|
| 1811 | /* this should be the same thing as hbin->block_size but just in case */ |
---|
| 1812 | hbin->next_block = IVAL(hbin_header, 0x1C); |
---|
| 1813 | |
---|
| 1814 | |
---|
| 1815 | /* Ensure the block size is a multiple of 0x1000 and doesn't run off |
---|
| 1816 | * the end of the file. |
---|
| 1817 | */ |
---|
[116] | 1818 | /* XXX: This may need to be relaxed for dealing with |
---|
| 1819 | * partial or corrupt files. |
---|
| 1820 | */ |
---|
[97] | 1821 | if((offset + hbin->block_size > file->file_length) |
---|
| 1822 | || (hbin->block_size & 0xFFFFF000) != hbin->block_size) |
---|
[99] | 1823 | { |
---|
[138] | 1824 | regfi_add_message(file, REGFI_MSG_ERROR, "The hbin offset is not aligned" |
---|
[137] | 1825 | " or runs off the end of the file" |
---|
| 1826 | " while parsing hbin at offset 0x%.8X.", offset); |
---|
[148] | 1827 | talloc_free(hbin); |
---|
[97] | 1828 | return NULL; |
---|
[99] | 1829 | } |
---|
[97] | 1830 | |
---|
| 1831 | return hbin; |
---|
| 1832 | } |
---|
| 1833 | |
---|
| 1834 | |
---|
[126] | 1835 | /******************************************************************* |
---|
| 1836 | *******************************************************************/ |
---|
[135] | 1837 | REGFI_NK_REC* regfi_parse_nk(REGFI_FILE* file, uint32 offset, |
---|
[99] | 1838 | uint32 max_size, bool strict) |
---|
| 1839 | { |
---|
| 1840 | uint8 nk_header[REGFI_NK_MIN_LENGTH]; |
---|
[135] | 1841 | REGFI_NK_REC* ret_val; |
---|
[131] | 1842 | uint32 length,cell_length; |
---|
[157] | 1843 | uint32 class_offset; |
---|
| 1844 | int32 class_maxsize; |
---|
[101] | 1845 | bool unalloc = false; |
---|
[99] | 1846 | |
---|
[101] | 1847 | if(!regfi_parse_cell(file->fd, offset, nk_header, REGFI_NK_MIN_LENGTH, |
---|
| 1848 | &cell_length, &unalloc)) |
---|
[137] | 1849 | { |
---|
[138] | 1850 | regfi_add_message(file, REGFI_MSG_WARN, "Could not parse cell header" |
---|
[137] | 1851 | " while parsing NK record at offset 0x%.8X.", offset); |
---|
| 1852 | return NULL; |
---|
| 1853 | } |
---|
| 1854 | |
---|
[99] | 1855 | /* A bit of validation before bothering to allocate memory */ |
---|
[101] | 1856 | if((nk_header[0x0] != 'n') || (nk_header[0x1] != 'k')) |
---|
[135] | 1857 | { |
---|
[138] | 1858 | regfi_add_message(file, REGFI_MSG_WARN, "Magic number mismatch in parsing" |
---|
| 1859 | " NK record at offset 0x%.8X.", offset); |
---|
[99] | 1860 | return NULL; |
---|
[135] | 1861 | } |
---|
[99] | 1862 | |
---|
[150] | 1863 | ret_val = talloc(NULL, REGFI_NK_REC); |
---|
[99] | 1864 | if(ret_val == NULL) |
---|
[135] | 1865 | { |
---|
[138] | 1866 | regfi_add_message(file, REGFI_MSG_ERROR, "Failed to allocate memory while" |
---|
[137] | 1867 | " parsing NK record at offset 0x%.8X.", offset); |
---|
[99] | 1868 | return NULL; |
---|
[135] | 1869 | } |
---|
[99] | 1870 | |
---|
[150] | 1871 | ret_val->values = NULL; |
---|
| 1872 | ret_val->subkeys = NULL; |
---|
[99] | 1873 | ret_val->offset = offset; |
---|
[101] | 1874 | ret_val->cell_size = cell_length; |
---|
| 1875 | |
---|
[99] | 1876 | if(ret_val->cell_size > max_size) |
---|
| 1877 | ret_val->cell_size = max_size & 0xFFFFFFF8; |
---|
| 1878 | if((ret_val->cell_size < REGFI_NK_MIN_LENGTH) |
---|
[157] | 1879 | || (strict && (ret_val->cell_size & 0x00000007) != 0)) |
---|
[99] | 1880 | { |
---|
[140] | 1881 | regfi_add_message(file, REGFI_MSG_WARN, "A length check failed while" |
---|
[138] | 1882 | " parsing NK record at offset 0x%.8X.", offset); |
---|
[150] | 1883 | talloc_free(ret_val); |
---|
[99] | 1884 | return NULL; |
---|
| 1885 | } |
---|
| 1886 | |
---|
[101] | 1887 | ret_val->magic[0] = nk_header[0x0]; |
---|
| 1888 | ret_val->magic[1] = nk_header[0x1]; |
---|
| 1889 | ret_val->key_type = SVAL(nk_header, 0x2); |
---|
[152] | 1890 | |
---|
| 1891 | if((ret_val->key_type & ~REGFI_NK_KNOWN_FLAGS) != 0) |
---|
[99] | 1892 | { |
---|
[152] | 1893 | regfi_add_message(file, REGFI_MSG_WARN, "Unknown key flags (0x%.4X) while" |
---|
[138] | 1894 | " parsing NK record at offset 0x%.8X.", |
---|
[152] | 1895 | (ret_val->key_type & ~REGFI_NK_KNOWN_FLAGS), offset); |
---|
[99] | 1896 | } |
---|
[101] | 1897 | |
---|
| 1898 | ret_val->mtime.low = IVAL(nk_header, 0x4); |
---|
| 1899 | ret_val->mtime.high = IVAL(nk_header, 0x8); |
---|
[116] | 1900 | /* If the key is unallocated and the MTIME is earlier than Jan 1, 1990 |
---|
| 1901 | * or later than Jan 1, 2290, we consider this a bad key. This helps |
---|
| 1902 | * weed out some false positives during deleted data recovery. |
---|
| 1903 | */ |
---|
| 1904 | if(unalloc |
---|
| 1905 | && ((ret_val->mtime.high < REGFI_MTIME_MIN_HIGH |
---|
| 1906 | && ret_val->mtime.low < REGFI_MTIME_MIN_LOW) |
---|
| 1907 | || (ret_val->mtime.high > REGFI_MTIME_MAX_HIGH |
---|
| 1908 | && ret_val->mtime.low > REGFI_MTIME_MAX_LOW))) |
---|
| 1909 | return NULL; |
---|
| 1910 | |
---|
[101] | 1911 | ret_val->unknown1 = IVAL(nk_header, 0xC); |
---|
| 1912 | ret_val->parent_off = IVAL(nk_header, 0x10); |
---|
| 1913 | ret_val->num_subkeys = IVAL(nk_header, 0x14); |
---|
| 1914 | ret_val->unknown2 = IVAL(nk_header, 0x18); |
---|
| 1915 | ret_val->subkeys_off = IVAL(nk_header, 0x1C); |
---|
| 1916 | ret_val->unknown3 = IVAL(nk_header, 0x20); |
---|
| 1917 | ret_val->num_values = IVAL(nk_header, 0x24); |
---|
| 1918 | ret_val->values_off = IVAL(nk_header, 0x28); |
---|
| 1919 | ret_val->sk_off = IVAL(nk_header, 0x2C); |
---|
| 1920 | ret_val->classname_off = IVAL(nk_header, 0x30); |
---|
[99] | 1921 | |
---|
[101] | 1922 | ret_val->max_bytes_subkeyname = IVAL(nk_header, 0x34); |
---|
| 1923 | ret_val->max_bytes_subkeyclassname = IVAL(nk_header, 0x38); |
---|
| 1924 | ret_val->max_bytes_valuename = IVAL(nk_header, 0x3C); |
---|
| 1925 | ret_val->max_bytes_value = IVAL(nk_header, 0x40); |
---|
| 1926 | ret_val->unk_index = IVAL(nk_header, 0x44); |
---|
[99] | 1927 | |
---|
[101] | 1928 | ret_val->name_length = SVAL(nk_header, 0x48); |
---|
| 1929 | ret_val->classname_length = SVAL(nk_header, 0x4A); |
---|
[99] | 1930 | |
---|
| 1931 | if(ret_val->name_length + REGFI_NK_MIN_LENGTH > ret_val->cell_size) |
---|
[101] | 1932 | { |
---|
| 1933 | if(strict) |
---|
| 1934 | { |
---|
[138] | 1935 | regfi_add_message(file, REGFI_MSG_ERROR, "Contents too large for cell" |
---|
[137] | 1936 | " while parsing NK record at offset 0x%.8X.", offset); |
---|
[150] | 1937 | talloc_free(ret_val); |
---|
[101] | 1938 | return NULL; |
---|
| 1939 | } |
---|
| 1940 | else |
---|
| 1941 | ret_val->name_length = ret_val->cell_size - REGFI_NK_MIN_LENGTH; |
---|
| 1942 | } |
---|
| 1943 | else if (unalloc) |
---|
| 1944 | { /* Truncate cell_size if it's much larger than the apparent total record length. */ |
---|
| 1945 | /* Round up to the next multiple of 8 */ |
---|
| 1946 | length = (ret_val->name_length + REGFI_NK_MIN_LENGTH) & 0xFFFFFFF8; |
---|
| 1947 | if(length < ret_val->name_length + REGFI_NK_MIN_LENGTH) |
---|
| 1948 | length+=8; |
---|
[99] | 1949 | |
---|
[101] | 1950 | /* If cell_size is still greater, truncate. */ |
---|
| 1951 | if(length < ret_val->cell_size) |
---|
| 1952 | ret_val->cell_size = length; |
---|
| 1953 | } |
---|
| 1954 | |
---|
[150] | 1955 | ret_val->keyname = talloc_array(ret_val, char, ret_val->name_length+1); |
---|
[99] | 1956 | if(ret_val->keyname == NULL) |
---|
| 1957 | { |
---|
[150] | 1958 | talloc_free(ret_val); |
---|
[99] | 1959 | return NULL; |
---|
| 1960 | } |
---|
| 1961 | |
---|
| 1962 | /* Don't need to seek, should be at the right offset */ |
---|
| 1963 | length = ret_val->name_length; |
---|
[101] | 1964 | if((regfi_read(file->fd, (uint8*)ret_val->keyname, &length) != 0) |
---|
[99] | 1965 | || length != ret_val->name_length) |
---|
| 1966 | { |
---|
[138] | 1967 | regfi_add_message(file, REGFI_MSG_ERROR, "Failed to read key name" |
---|
[137] | 1968 | " while parsing NK record at offset 0x%.8X.", offset); |
---|
[150] | 1969 | talloc_free(ret_val); |
---|
[99] | 1970 | return NULL; |
---|
| 1971 | } |
---|
| 1972 | ret_val->keyname[ret_val->name_length] = '\0'; |
---|
| 1973 | |
---|
[150] | 1974 | /* XXX: This linking should be moved up to regfi_load_key */ |
---|
[135] | 1975 | if(ret_val->classname_off != REGFI_OFFSET_NONE) |
---|
[126] | 1976 | { |
---|
[157] | 1977 | class_offset = ret_val->classname_off + REGFI_REGF_SIZE; |
---|
| 1978 | class_maxsize = regfi_calc_maxsize(file, class_offset); |
---|
| 1979 | if(class_maxsize > 0) |
---|
[131] | 1980 | { |
---|
| 1981 | ret_val->classname |
---|
| 1982 | = regfi_parse_classname(file, class_offset, &ret_val->classname_length, |
---|
| 1983 | class_maxsize, strict); |
---|
| 1984 | } |
---|
| 1985 | else |
---|
[137] | 1986 | { |
---|
[131] | 1987 | ret_val->classname = NULL; |
---|
[138] | 1988 | regfi_add_message(file, REGFI_MSG_WARN, "Could not find hbin for class" |
---|
| 1989 | " name while parsing NK record at offset 0x%.8X.", |
---|
| 1990 | offset); |
---|
[137] | 1991 | } |
---|
[140] | 1992 | |
---|
| 1993 | if(ret_val->classname == NULL) |
---|
| 1994 | { |
---|
| 1995 | regfi_add_message(file, REGFI_MSG_WARN, "Could not parse class" |
---|
| 1996 | " name while parsing NK record at offset 0x%.8X.", |
---|
| 1997 | offset); |
---|
| 1998 | } |
---|
[150] | 1999 | else |
---|
| 2000 | talloc_steal(ret_val, ret_val->classname); |
---|
[126] | 2001 | } |
---|
[138] | 2002 | |
---|
[126] | 2003 | return ret_val; |
---|
| 2004 | } |
---|
| 2005 | |
---|
| 2006 | |
---|
[135] | 2007 | char* regfi_parse_classname(REGFI_FILE* file, uint32 offset, |
---|
[131] | 2008 | uint16* name_length, uint32 max_size, bool strict) |
---|
[126] | 2009 | { |
---|
| 2010 | char* ret_val = NULL; |
---|
| 2011 | uint32 length; |
---|
| 2012 | uint32 cell_length; |
---|
| 2013 | bool unalloc = false; |
---|
| 2014 | |
---|
[135] | 2015 | if(*name_length > 0 && offset != REGFI_OFFSET_NONE |
---|
[157] | 2016 | && (offset & 0x00000007) == 0) |
---|
[131] | 2017 | { |
---|
[126] | 2018 | if(!regfi_parse_cell(file->fd, offset, NULL, 0, &cell_length, &unalloc)) |
---|
[137] | 2019 | { |
---|
[138] | 2020 | regfi_add_message(file, REGFI_MSG_WARN, "Could not parse cell header" |
---|
[137] | 2021 | " while parsing class name at offset 0x%.8X.", offset); |
---|
[126] | 2022 | return NULL; |
---|
[137] | 2023 | } |
---|
[126] | 2024 | |
---|
[157] | 2025 | if((cell_length & 0x0000007) != 0) |
---|
[137] | 2026 | { |
---|
[138] | 2027 | regfi_add_message(file, REGFI_MSG_ERROR, "Cell length not a multiple of 8" |
---|
[137] | 2028 | " while parsing class name at offset 0x%.8X.", offset); |
---|
[131] | 2029 | return NULL; |
---|
[137] | 2030 | } |
---|
| 2031 | |
---|
[131] | 2032 | if(cell_length > max_size) |
---|
[125] | 2033 | { |
---|
[138] | 2034 | regfi_add_message(file, REGFI_MSG_WARN, "Cell stretches past hbin " |
---|
| 2035 | "boundary while parsing class name at offset 0x%.8X.", |
---|
| 2036 | offset); |
---|
[126] | 2037 | if(strict) |
---|
| 2038 | return NULL; |
---|
[131] | 2039 | cell_length = max_size; |
---|
[126] | 2040 | } |
---|
[131] | 2041 | |
---|
| 2042 | if((cell_length - 4) < *name_length) |
---|
| 2043 | { |
---|
[138] | 2044 | regfi_add_message(file, REGFI_MSG_WARN, "Class name is larger than" |
---|
| 2045 | " cell_length while parsing class name at offset" |
---|
| 2046 | " 0x%.8X.", offset); |
---|
[131] | 2047 | if(strict) |
---|
| 2048 | return NULL; |
---|
| 2049 | *name_length = cell_length - 4; |
---|
| 2050 | } |
---|
[126] | 2051 | |
---|
[150] | 2052 | ret_val = talloc_array(NULL, char, *name_length); |
---|
[126] | 2053 | if(ret_val != NULL) |
---|
| 2054 | { |
---|
| 2055 | length = *name_length; |
---|
| 2056 | if((regfi_read(file->fd, (uint8*)ret_val, &length) != 0) |
---|
| 2057 | || length != *name_length) |
---|
[125] | 2058 | { |
---|
[138] | 2059 | regfi_add_message(file, REGFI_MSG_ERROR, "Could not read class name" |
---|
[137] | 2060 | " while parsing class name at offset 0x%.8X.", offset); |
---|
[150] | 2061 | talloc_free(ret_val); |
---|
[126] | 2062 | return NULL; |
---|
[125] | 2063 | } |
---|
| 2064 | } |
---|
| 2065 | } |
---|
| 2066 | |
---|
[99] | 2067 | return ret_val; |
---|
| 2068 | } |
---|
| 2069 | |
---|
| 2070 | |
---|
[152] | 2071 | /****************************************************************************** |
---|
| 2072 | *******************************************************************************/ |
---|
[135] | 2073 | REGFI_VK_REC* regfi_parse_vk(REGFI_FILE* file, uint32 offset, |
---|
[145] | 2074 | uint32 max_size, bool strict) |
---|
[97] | 2075 | { |
---|
[135] | 2076 | REGFI_VK_REC* ret_val; |
---|
[101] | 2077 | uint8 vk_header[REGFI_VK_MIN_LENGTH]; |
---|
| 2078 | uint32 raw_data_size, length, cell_length; |
---|
| 2079 | bool unalloc = false; |
---|
[97] | 2080 | |
---|
[101] | 2081 | if(!regfi_parse_cell(file->fd, offset, vk_header, REGFI_VK_MIN_LENGTH, |
---|
| 2082 | &cell_length, &unalloc)) |
---|
[137] | 2083 | { |
---|
[138] | 2084 | regfi_add_message(file, REGFI_MSG_WARN, "Could not parse cell header" |
---|
[137] | 2085 | " while parsing VK record at offset 0x%.8X.", offset); |
---|
[101] | 2086 | return NULL; |
---|
[137] | 2087 | } |
---|
[111] | 2088 | |
---|
[150] | 2089 | ret_val = talloc(NULL, REGFI_VK_REC); |
---|
[101] | 2090 | if(ret_val == NULL) |
---|
| 2091 | return NULL; |
---|
| 2092 | |
---|
| 2093 | ret_val->offset = offset; |
---|
| 2094 | ret_val->cell_size = cell_length; |
---|
[150] | 2095 | ret_val->data = NULL; |
---|
| 2096 | ret_val->valuename = NULL; |
---|
| 2097 | |
---|
[101] | 2098 | if(ret_val->cell_size > max_size) |
---|
| 2099 | ret_val->cell_size = max_size & 0xFFFFFFF8; |
---|
| 2100 | if((ret_val->cell_size < REGFI_VK_MIN_LENGTH) |
---|
[157] | 2101 | || (ret_val->cell_size & 0x00000007) != 0) |
---|
[97] | 2102 | { |
---|
[138] | 2103 | regfi_add_message(file, REGFI_MSG_WARN, "Invalid cell size encountered" |
---|
[137] | 2104 | " while parsing VK record at offset 0x%.8X.", offset); |
---|
[150] | 2105 | talloc_free(ret_val); |
---|
[101] | 2106 | return NULL; |
---|
| 2107 | } |
---|
[97] | 2108 | |
---|
[101] | 2109 | ret_val->magic[0] = vk_header[0x0]; |
---|
| 2110 | ret_val->magic[1] = vk_header[0x1]; |
---|
| 2111 | if((ret_val->magic[0] != 'v') || (ret_val->magic[1] != 'k')) |
---|
| 2112 | { |
---|
[124] | 2113 | /* XXX: This does not account for deleted keys under Win2K which |
---|
| 2114 | * often have this (and the name length) overwritten with |
---|
| 2115 | * 0xFFFF. |
---|
| 2116 | */ |
---|
[138] | 2117 | regfi_add_message(file, REGFI_MSG_WARN, "Magic number mismatch" |
---|
[137] | 2118 | " while parsing VK record at offset 0x%.8X.", offset); |
---|
[150] | 2119 | talloc_free(ret_val); |
---|
[101] | 2120 | return NULL; |
---|
| 2121 | } |
---|
| 2122 | |
---|
| 2123 | ret_val->name_length = SVAL(vk_header, 0x2); |
---|
| 2124 | raw_data_size = IVAL(vk_header, 0x4); |
---|
[135] | 2125 | ret_val->data_size = raw_data_size & ~REGFI_VK_DATA_IN_OFFSET; |
---|
[157] | 2126 | /* The data is typically stored in the offset if the size <= 4, |
---|
| 2127 | * in which case this flag is set. |
---|
| 2128 | */ |
---|
[135] | 2129 | ret_val->data_in_offset = (bool)(raw_data_size & REGFI_VK_DATA_IN_OFFSET); |
---|
[101] | 2130 | ret_val->data_off = IVAL(vk_header, 0x8); |
---|
| 2131 | ret_val->type = IVAL(vk_header, 0xC); |
---|
| 2132 | ret_val->flag = SVAL(vk_header, 0x10); |
---|
| 2133 | ret_val->unknown1 = SVAL(vk_header, 0x12); |
---|
| 2134 | |
---|
[135] | 2135 | if(ret_val->flag & REGFI_VK_FLAG_NAME_PRESENT) |
---|
[101] | 2136 | { |
---|
[113] | 2137 | if(ret_val->name_length + REGFI_VK_MIN_LENGTH + 4 > ret_val->cell_size) |
---|
[101] | 2138 | { |
---|
[138] | 2139 | regfi_add_message(file, REGFI_MSG_WARN, "Name too long for remaining cell" |
---|
| 2140 | " space while parsing VK record at offset 0x%.8X.", |
---|
| 2141 | offset); |
---|
[101] | 2142 | if(strict) |
---|
| 2143 | { |
---|
[150] | 2144 | talloc_free(ret_val); |
---|
[101] | 2145 | return NULL; |
---|
| 2146 | } |
---|
| 2147 | else |
---|
[113] | 2148 | ret_val->name_length = ret_val->cell_size - REGFI_VK_MIN_LENGTH - 4; |
---|
[101] | 2149 | } |
---|
| 2150 | |
---|
| 2151 | /* Round up to the next multiple of 8 */ |
---|
[113] | 2152 | cell_length = (ret_val->name_length + REGFI_VK_MIN_LENGTH + 4) & 0xFFFFFFF8; |
---|
| 2153 | if(cell_length < ret_val->name_length + REGFI_VK_MIN_LENGTH + 4) |
---|
| 2154 | cell_length+=8; |
---|
[101] | 2155 | |
---|
[150] | 2156 | ret_val->valuename = talloc_array(ret_val, char, ret_val->name_length+1); |
---|
[101] | 2157 | if(ret_val->valuename == NULL) |
---|
| 2158 | { |
---|
[150] | 2159 | talloc_free(ret_val); |
---|
[101] | 2160 | return NULL; |
---|
| 2161 | } |
---|
[113] | 2162 | |
---|
[101] | 2163 | length = ret_val->name_length; |
---|
| 2164 | if((regfi_read(file->fd, (uint8*)ret_val->valuename, &length) != 0) |
---|
| 2165 | || length != ret_val->name_length) |
---|
| 2166 | { |
---|
[138] | 2167 | regfi_add_message(file, REGFI_MSG_ERROR, "Could not read value name" |
---|
[137] | 2168 | " while parsing VK record at offset 0x%.8X.", offset); |
---|
[150] | 2169 | talloc_free(ret_val); |
---|
[101] | 2170 | return NULL; |
---|
| 2171 | } |
---|
| 2172 | ret_val->valuename[ret_val->name_length] = '\0'; |
---|
[133] | 2173 | |
---|
[101] | 2174 | } |
---|
| 2175 | else |
---|
[113] | 2176 | cell_length = REGFI_VK_MIN_LENGTH + 4; |
---|
[101] | 2177 | |
---|
| 2178 | if(unalloc) |
---|
| 2179 | { |
---|
| 2180 | /* If cell_size is still greater, truncate. */ |
---|
[113] | 2181 | if(cell_length < ret_val->cell_size) |
---|
| 2182 | ret_val->cell_size = cell_length; |
---|
[101] | 2183 | } |
---|
| 2184 | |
---|
| 2185 | return ret_val; |
---|
[97] | 2186 | } |
---|
[101] | 2187 | |
---|
| 2188 | |
---|
[152] | 2189 | /****************************************************************************** |
---|
[157] | 2190 | * |
---|
| 2191 | ******************************************************************************/ |
---|
| 2192 | REGFI_BUFFER regfi_load_data(REGFI_FILE* file, uint32 voffset, |
---|
| 2193 | uint32 length, bool data_in_offset, |
---|
| 2194 | bool strict) |
---|
[101] | 2195 | { |
---|
[151] | 2196 | REGFI_BUFFER ret_val; |
---|
[157] | 2197 | uint32 cell_length, offset; |
---|
| 2198 | int32 max_size; |
---|
[101] | 2199 | bool unalloc; |
---|
[151] | 2200 | |
---|
[145] | 2201 | if(data_in_offset) |
---|
[157] | 2202 | return regfi_parse_little_data(file, voffset, length, strict); |
---|
| 2203 | else |
---|
[101] | 2204 | { |
---|
[157] | 2205 | offset = voffset + REGFI_REGF_SIZE; |
---|
| 2206 | max_size = regfi_calc_maxsize(file, offset); |
---|
| 2207 | if(max_size < 0) |
---|
[137] | 2208 | { |
---|
[157] | 2209 | regfi_add_message(file, REGFI_MSG_WARN, "Could not find HBIN for data" |
---|
| 2210 | " at offset 0x%.8X.", offset); |
---|
[151] | 2211 | goto fail; |
---|
[137] | 2212 | } |
---|
[157] | 2213 | |
---|
[101] | 2214 | if(!regfi_parse_cell(file->fd, offset, NULL, 0, |
---|
| 2215 | &cell_length, &unalloc)) |
---|
[137] | 2216 | { |
---|
[138] | 2217 | regfi_add_message(file, REGFI_MSG_WARN, "Could not parse cell while" |
---|
[137] | 2218 | " parsing data record at offset 0x%.8X.", offset); |
---|
[151] | 2219 | goto fail; |
---|
[137] | 2220 | } |
---|
[111] | 2221 | |
---|
[157] | 2222 | if((cell_length & 0x00000007) != 0) |
---|
[137] | 2223 | { |
---|
[138] | 2224 | regfi_add_message(file, REGFI_MSG_WARN, "Cell length not multiple of 8" |
---|
[137] | 2225 | " while parsing data record at offset 0x%.8X.", |
---|
| 2226 | offset); |
---|
[151] | 2227 | goto fail; |
---|
[137] | 2228 | } |
---|
[101] | 2229 | |
---|
[131] | 2230 | if(cell_length > max_size) |
---|
| 2231 | { |
---|
[145] | 2232 | regfi_add_message(file, REGFI_MSG_WARN, "Cell extends past HBIN boundary" |
---|
| 2233 | " while parsing data record at offset 0x%.8X.", |
---|
[137] | 2234 | offset); |
---|
[157] | 2235 | goto fail; |
---|
[131] | 2236 | } |
---|
| 2237 | |
---|
[101] | 2238 | if(cell_length - 4 < length) |
---|
| 2239 | { |
---|
[155] | 2240 | /* XXX: All big data records thus far have been 16 bytes long. |
---|
| 2241 | * Should we check for this precise size instead of just |
---|
| 2242 | * relying upon the above check? |
---|
| 2243 | */ |
---|
[152] | 2244 | if (file->major_version >= 1 && file->minor_version >= 5) |
---|
| 2245 | { |
---|
| 2246 | /* Attempt to parse a big data record */ |
---|
[157] | 2247 | return regfi_load_big_data(file, offset, length, cell_length, |
---|
| 2248 | NULL, strict); |
---|
[152] | 2249 | } |
---|
[101] | 2250 | else |
---|
[152] | 2251 | { |
---|
| 2252 | regfi_add_message(file, REGFI_MSG_WARN, "Data length (0x%.8X) larger than" |
---|
| 2253 | " remaining cell length (0x%.8X)" |
---|
| 2254 | " while parsing data record at offset 0x%.8X.", |
---|
| 2255 | length, cell_length - 4, offset); |
---|
| 2256 | if(strict) |
---|
| 2257 | goto fail; |
---|
| 2258 | else |
---|
| 2259 | length = cell_length - 4; |
---|
| 2260 | } |
---|
[101] | 2261 | } |
---|
| 2262 | |
---|
[157] | 2263 | ret_val = regfi_parse_data(file, offset, length, strict); |
---|
[101] | 2264 | } |
---|
| 2265 | |
---|
| 2266 | return ret_val; |
---|
[151] | 2267 | |
---|
| 2268 | fail: |
---|
| 2269 | ret_val.buf = NULL; |
---|
| 2270 | ret_val.len = 0; |
---|
| 2271 | return ret_val; |
---|
[101] | 2272 | } |
---|
[110] | 2273 | |
---|
| 2274 | |
---|
[152] | 2275 | /****************************************************************************** |
---|
[157] | 2276 | * Parses the common case data records stored in a single cell. |
---|
| 2277 | ******************************************************************************/ |
---|
| 2278 | REGFI_BUFFER regfi_parse_data(REGFI_FILE* file, uint32 offset, |
---|
| 2279 | uint32 length, bool strict) |
---|
| 2280 | { |
---|
| 2281 | REGFI_BUFFER ret_val; |
---|
| 2282 | uint32 read_length; |
---|
| 2283 | |
---|
| 2284 | ret_val.buf = NULL; |
---|
| 2285 | ret_val.len = 0; |
---|
| 2286 | |
---|
| 2287 | if(lseek(file->fd, offset+4, SEEK_SET) == -1) |
---|
| 2288 | { |
---|
| 2289 | regfi_add_message(file, REGFI_MSG_WARN, "Could not seek while " |
---|
| 2290 | "reading data at offset 0x%.8X.", offset); |
---|
| 2291 | return ret_val; |
---|
| 2292 | } |
---|
| 2293 | |
---|
| 2294 | if((ret_val.buf = talloc_array(NULL, uint8_t, length)) == NULL) |
---|
| 2295 | return ret_val; |
---|
| 2296 | ret_val.len = length; |
---|
| 2297 | |
---|
| 2298 | read_length = length; |
---|
| 2299 | if((regfi_read(file->fd, ret_val.buf, &read_length) != 0) |
---|
| 2300 | || read_length != length) |
---|
| 2301 | { |
---|
| 2302 | regfi_add_message(file, REGFI_MSG_ERROR, "Could not read data block while" |
---|
| 2303 | " parsing data record at offset 0x%.8X.", offset); |
---|
| 2304 | talloc_free(ret_val.buf); |
---|
| 2305 | ret_val.buf = NULL; |
---|
| 2306 | ret_val.buf = 0; |
---|
| 2307 | } |
---|
| 2308 | |
---|
| 2309 | return ret_val; |
---|
| 2310 | } |
---|
| 2311 | |
---|
| 2312 | |
---|
| 2313 | |
---|
| 2314 | /****************************************************************************** |
---|
| 2315 | * |
---|
| 2316 | ******************************************************************************/ |
---|
| 2317 | REGFI_BUFFER regfi_parse_little_data(REGFI_FILE* file, uint32 voffset, |
---|
| 2318 | uint32 length, bool strict) |
---|
| 2319 | { |
---|
| 2320 | REGFI_BUFFER ret_val; |
---|
| 2321 | uint8 i; |
---|
| 2322 | |
---|
| 2323 | ret_val.buf = NULL; |
---|
| 2324 | ret_val.len = 0; |
---|
| 2325 | |
---|
| 2326 | if(length > 4) |
---|
| 2327 | { |
---|
| 2328 | regfi_add_message(file, REGFI_MSG_ERROR, "Data in offset but length > 4" |
---|
| 2329 | " while parsing data record. (voffset=0x%.8X, length=%d)", |
---|
| 2330 | voffset, length); |
---|
| 2331 | return ret_val; |
---|
| 2332 | } |
---|
| 2333 | |
---|
| 2334 | if((ret_val.buf = talloc_array(NULL, uint8_t, length)) == NULL) |
---|
| 2335 | return ret_val; |
---|
| 2336 | ret_val.len = length; |
---|
| 2337 | |
---|
| 2338 | for(i = 0; i < length; i++) |
---|
| 2339 | ret_val.buf[i] = (uint8)((voffset >> i*8) & 0xFF); |
---|
| 2340 | |
---|
| 2341 | return ret_val; |
---|
| 2342 | } |
---|
| 2343 | |
---|
| 2344 | /****************************************************************************** |
---|
[152] | 2345 | *******************************************************************************/ |
---|
[157] | 2346 | REGFI_BUFFER regfi_parse_big_data_header(REGFI_FILE* file, uint32 offset, |
---|
| 2347 | uint32 max_size, bool strict) |
---|
[152] | 2348 | { |
---|
| 2349 | REGFI_BUFFER ret_val; |
---|
[157] | 2350 | uint32 cell_length; |
---|
[152] | 2351 | bool unalloc; |
---|
[157] | 2352 | |
---|
| 2353 | /* XXX: do something with unalloc? */ |
---|
| 2354 | ret_val.buf = (uint8*)talloc_array(NULL, uint8, REGFI_BIG_DATA_MIN_LENGTH); |
---|
| 2355 | if(ret_val.buf == NULL) |
---|
[152] | 2356 | goto fail; |
---|
| 2357 | |
---|
[157] | 2358 | if(REGFI_BIG_DATA_MIN_LENGTH > max_size) |
---|
| 2359 | { |
---|
| 2360 | regfi_add_message(file, REGFI_MSG_WARN, "Big data header exceeded max_size " |
---|
| 2361 | "while parsing big data header at offset 0x%.8X.",offset); |
---|
| 2362 | goto fail; |
---|
| 2363 | } |
---|
| 2364 | |
---|
| 2365 | if(!regfi_parse_cell(file->fd, offset, ret_val.buf, REGFI_BIG_DATA_MIN_LENGTH, |
---|
[152] | 2366 | &cell_length, &unalloc)) |
---|
| 2367 | { |
---|
| 2368 | regfi_add_message(file, REGFI_MSG_WARN, "Could not parse cell while" |
---|
[157] | 2369 | " parsing big data header at offset 0x%.8X.", offset); |
---|
[152] | 2370 | goto fail; |
---|
| 2371 | } |
---|
[157] | 2372 | |
---|
| 2373 | if((ret_val.buf[0] != 'd') || (ret_val.buf[1] != 'b')) |
---|
[152] | 2374 | { |
---|
| 2375 | regfi_add_message(file, REGFI_MSG_WARN, "Unknown magic number" |
---|
| 2376 | " (0x%.2X, 0x%.2X) encountered while parsing" |
---|
[157] | 2377 | " big data header at offset 0x%.8X.", |
---|
| 2378 | ret_val.buf[0], ret_val.buf[1], offset); |
---|
[152] | 2379 | goto fail; |
---|
| 2380 | } |
---|
| 2381 | |
---|
[157] | 2382 | ret_val.len = REGFI_BIG_DATA_MIN_LENGTH; |
---|
| 2383 | return ret_val; |
---|
| 2384 | |
---|
| 2385 | fail: |
---|
| 2386 | if(ret_val.buf != NULL) |
---|
| 2387 | { |
---|
| 2388 | talloc_free(ret_val.buf); |
---|
| 2389 | ret_val.buf = NULL; |
---|
| 2390 | } |
---|
| 2391 | ret_val.len = 0; |
---|
| 2392 | return ret_val; |
---|
| 2393 | } |
---|
| 2394 | |
---|
| 2395 | |
---|
| 2396 | |
---|
| 2397 | /****************************************************************************** |
---|
| 2398 | * |
---|
| 2399 | ******************************************************************************/ |
---|
| 2400 | uint32* regfi_parse_big_data_indirect(REGFI_FILE* file, uint32 offset, |
---|
| 2401 | uint16 num_chunks, bool strict) |
---|
| 2402 | { |
---|
| 2403 | uint32* ret_val; |
---|
| 2404 | uint32 indirect_length; |
---|
| 2405 | int32 max_size; |
---|
| 2406 | uint16 i; |
---|
| 2407 | bool unalloc; |
---|
| 2408 | |
---|
| 2409 | /* XXX: do something with unalloc? */ |
---|
| 2410 | |
---|
| 2411 | max_size = regfi_calc_maxsize(file, offset); |
---|
| 2412 | if((max_size < 0) || (num_chunks*sizeof(uint32) + 4 > max_size)) |
---|
| 2413 | return NULL; |
---|
| 2414 | |
---|
| 2415 | ret_val = (uint32*)talloc_array(NULL, uint32, num_chunks); |
---|
| 2416 | if(ret_val == NULL) |
---|
[152] | 2417 | goto fail; |
---|
| 2418 | |
---|
[157] | 2419 | if(!regfi_parse_cell(file->fd, offset, (uint8*)ret_val, |
---|
[152] | 2420 | num_chunks*sizeof(uint32), |
---|
| 2421 | &indirect_length, &unalloc)) |
---|
| 2422 | { |
---|
| 2423 | regfi_add_message(file, REGFI_MSG_WARN, "Could not parse cell while" |
---|
| 2424 | " parsing big data indirect record at offset 0x%.8X.", |
---|
| 2425 | offset); |
---|
| 2426 | goto fail; |
---|
| 2427 | } |
---|
[157] | 2428 | |
---|
| 2429 | /* Convert pointers to proper endianess, verify they are aligned. */ |
---|
| 2430 | for(i=0; i<num_chunks; i++) |
---|
[152] | 2431 | { |
---|
[157] | 2432 | ret_val[i] = IVAL(ret_val, i*sizeof(uint32)); |
---|
| 2433 | if((ret_val[i] & 0x00000007) != 0) |
---|
| 2434 | goto fail; |
---|
[152] | 2435 | } |
---|
[157] | 2436 | |
---|
| 2437 | return ret_val; |
---|
[152] | 2438 | |
---|
[157] | 2439 | fail: |
---|
| 2440 | if(ret_val != NULL) |
---|
| 2441 | talloc_free(ret_val); |
---|
| 2442 | return NULL; |
---|
| 2443 | } |
---|
| 2444 | |
---|
| 2445 | |
---|
| 2446 | /****************************************************************************** |
---|
| 2447 | * Arguments: |
---|
| 2448 | * file -- |
---|
| 2449 | * offsets -- list of virtual offsets. |
---|
| 2450 | * num_chunks -- |
---|
| 2451 | * strict -- |
---|
| 2452 | * |
---|
| 2453 | * Returns: |
---|
| 2454 | * A range_list with physical offsets and complete lengths |
---|
| 2455 | * (including cell headers) of associated cells. |
---|
| 2456 | * No data in range_list elements. |
---|
| 2457 | ******************************************************************************/ |
---|
| 2458 | range_list* regfi_parse_big_data_cells(REGFI_FILE* file, uint32* offsets, |
---|
| 2459 | uint16 num_chunks, bool strict) |
---|
| 2460 | { |
---|
| 2461 | uint32 cell_length, chunk_offset, data_left; |
---|
| 2462 | range_list* ret_val; |
---|
| 2463 | uint16 i; |
---|
| 2464 | bool unalloc; |
---|
| 2465 | |
---|
| 2466 | /* XXX: do something with unalloc? */ |
---|
| 2467 | ret_val = range_list_new(); |
---|
| 2468 | if(ret_val == NULL) |
---|
| 2469 | goto fail; |
---|
| 2470 | |
---|
[152] | 2471 | for(i=0; (i<num_chunks) && (data_left>0); i++) |
---|
| 2472 | { |
---|
[157] | 2473 | chunk_offset = offsets[i]+REGFI_REGF_SIZE; |
---|
| 2474 | if(!regfi_parse_cell(file->fd, chunk_offset, NULL, 0, |
---|
| 2475 | &cell_length, &unalloc)) |
---|
[152] | 2476 | { |
---|
| 2477 | regfi_add_message(file, REGFI_MSG_WARN, "Could not parse cell while" |
---|
| 2478 | " parsing big data chunk at offset 0x%.8X.", |
---|
| 2479 | chunk_offset); |
---|
[157] | 2480 | goto fail; |
---|
[152] | 2481 | } |
---|
| 2482 | |
---|
[157] | 2483 | if(!range_list_add(ret_val, chunk_offset, cell_length, NULL)) |
---|
| 2484 | goto fail; |
---|
| 2485 | } |
---|
| 2486 | |
---|
| 2487 | return ret_val; |
---|
| 2488 | |
---|
| 2489 | fail: |
---|
| 2490 | if(ret_val != NULL) |
---|
| 2491 | range_list_free(ret_val); |
---|
| 2492 | return NULL; |
---|
| 2493 | } |
---|
| 2494 | |
---|
| 2495 | |
---|
| 2496 | /****************************************************************************** |
---|
| 2497 | *******************************************************************************/ |
---|
| 2498 | REGFI_BUFFER regfi_load_big_data(REGFI_FILE* file, |
---|
| 2499 | uint32 offset, uint32 data_length, |
---|
| 2500 | uint32 cell_length, range_list* used_ranges, |
---|
| 2501 | bool strict) |
---|
| 2502 | { |
---|
| 2503 | REGFI_BUFFER ret_val; |
---|
| 2504 | uint16 num_chunks, i; |
---|
| 2505 | uint32 read_length, data_left, tmp_len, indirect_offset; |
---|
| 2506 | uint32* indirect_ptrs = NULL; |
---|
| 2507 | REGFI_BUFFER bd_header; |
---|
| 2508 | range_list* bd_cells = NULL; |
---|
| 2509 | const range_list_element* cell_info; |
---|
| 2510 | |
---|
| 2511 | ret_val.buf = NULL; |
---|
| 2512 | |
---|
| 2513 | /* XXX: Add better error/warning messages */ |
---|
| 2514 | |
---|
| 2515 | bd_header = regfi_parse_big_data_header(file, offset, cell_length, strict); |
---|
| 2516 | if(bd_header.buf == NULL) |
---|
| 2517 | goto fail; |
---|
| 2518 | |
---|
| 2519 | /* Keep track of used space for use by reglookup-recover */ |
---|
| 2520 | if(used_ranges != NULL) |
---|
| 2521 | if(!range_list_add(used_ranges, offset, cell_length, NULL)) |
---|
| 2522 | goto fail; |
---|
| 2523 | |
---|
| 2524 | num_chunks = SVAL(bd_header.buf, 0x2); |
---|
| 2525 | indirect_offset = IVAL(bd_header.buf, 0x4) + REGFI_REGF_SIZE; |
---|
| 2526 | talloc_free(bd_header.buf); |
---|
| 2527 | |
---|
| 2528 | indirect_ptrs = regfi_parse_big_data_indirect(file, indirect_offset, |
---|
| 2529 | num_chunks, strict); |
---|
| 2530 | if(indirect_ptrs == NULL) |
---|
| 2531 | goto fail; |
---|
| 2532 | |
---|
| 2533 | if(used_ranges != NULL) |
---|
| 2534 | if(!range_list_add(used_ranges, indirect_offset, num_chunks*4+4, NULL)) |
---|
| 2535 | goto fail; |
---|
| 2536 | |
---|
| 2537 | if((ret_val.buf = talloc_array(NULL, uint8_t, data_length)) == NULL) |
---|
| 2538 | goto fail; |
---|
| 2539 | data_left = data_length; |
---|
| 2540 | |
---|
| 2541 | bd_cells = regfi_parse_big_data_cells(file, indirect_ptrs, num_chunks, strict); |
---|
| 2542 | if(bd_cells == NULL) |
---|
| 2543 | goto fail; |
---|
| 2544 | |
---|
| 2545 | talloc_free(indirect_ptrs); |
---|
| 2546 | indirect_ptrs = NULL; |
---|
| 2547 | |
---|
| 2548 | for(i=0; (i<num_chunks) && (data_left>0); i++) |
---|
| 2549 | { |
---|
| 2550 | cell_info = range_list_get(bd_cells, i); |
---|
| 2551 | if(cell_info == NULL) |
---|
| 2552 | goto fail; |
---|
| 2553 | |
---|
| 2554 | /* XXX: This should be "cell_info->length-4" to account for the 4 byte cell |
---|
[154] | 2555 | * length. However, it has been observed that some (all?) chunks |
---|
| 2556 | * have an additional 4 bytes of 0 at the end of their cells that |
---|
| 2557 | * isn't part of the data, so we're trimming that off too. |
---|
[157] | 2558 | * Perhaps it's just an 8 byte alignment requirement... |
---|
[154] | 2559 | */ |
---|
[157] | 2560 | if(cell_info->length - 8 >= data_left) |
---|
| 2561 | { |
---|
| 2562 | if(i+1 != num_chunks) |
---|
| 2563 | { |
---|
| 2564 | regfi_add_message(file, REGFI_MSG_WARN, "Left over chunks detected " |
---|
| 2565 | "while constructing big data at offset 0x%.8X " |
---|
| 2566 | "(chunk offset 0x%.8X).", offset, cell_info->offset); |
---|
| 2567 | } |
---|
[152] | 2568 | read_length = data_left; |
---|
[157] | 2569 | } |
---|
[152] | 2570 | else |
---|
[157] | 2571 | read_length = cell_info->length - 8; |
---|
[152] | 2572 | |
---|
[157] | 2573 | |
---|
| 2574 | if(read_length > regfi_calc_maxsize(file, cell_info->offset)) |
---|
| 2575 | { |
---|
| 2576 | regfi_add_message(file, REGFI_MSG_WARN, "A chunk exceeded the maxsize " |
---|
| 2577 | "while constructing big data at offset 0x%.8X " |
---|
| 2578 | "(chunk offset 0x%.8X).", offset, cell_info->offset); |
---|
| 2579 | goto fail; |
---|
| 2580 | } |
---|
| 2581 | |
---|
| 2582 | if(lseek(file->fd, cell_info->offset+sizeof(uint32), SEEK_SET) == -1) |
---|
| 2583 | { |
---|
| 2584 | regfi_add_message(file, REGFI_MSG_WARN, "Could not seek to chunk while " |
---|
| 2585 | "constructing big data at offset 0x%.8X " |
---|
| 2586 | "(chunk offset 0x%.8X).", offset, cell_info->offset); |
---|
| 2587 | goto fail; |
---|
| 2588 | } |
---|
| 2589 | |
---|
| 2590 | tmp_len = read_length; |
---|
[152] | 2591 | if(regfi_read(file->fd, ret_val.buf+(data_length-data_left), |
---|
[157] | 2592 | &read_length) != 0 || (read_length != tmp_len)) |
---|
[152] | 2593 | { |
---|
| 2594 | regfi_add_message(file, REGFI_MSG_WARN, "Could not read data chunk while" |
---|
[157] | 2595 | " constructing big data at offset 0x%.8X" |
---|
| 2596 | " (chunk offset 0x%.8X).", offset, cell_info->offset); |
---|
| 2597 | goto fail; |
---|
[152] | 2598 | } |
---|
| 2599 | |
---|
[157] | 2600 | if(used_ranges != NULL) |
---|
| 2601 | if(!range_list_add(used_ranges, cell_info->offset,cell_info->length,NULL)) |
---|
| 2602 | goto fail; |
---|
| 2603 | |
---|
[152] | 2604 | data_left -= read_length; |
---|
| 2605 | } |
---|
[157] | 2606 | range_list_free(bd_cells); |
---|
| 2607 | |
---|
[152] | 2608 | ret_val.len = data_length-data_left; |
---|
| 2609 | return ret_val; |
---|
| 2610 | |
---|
| 2611 | fail: |
---|
[157] | 2612 | if(ret_val.buf != NULL) |
---|
| 2613 | talloc_free(ret_val.buf); |
---|
| 2614 | if(indirect_ptrs != NULL) |
---|
| 2615 | talloc_free(indirect_ptrs); |
---|
| 2616 | if(bd_cells != NULL) |
---|
| 2617 | range_list_free(bd_cells); |
---|
[152] | 2618 | ret_val.buf = NULL; |
---|
| 2619 | ret_val.len = 0; |
---|
| 2620 | return ret_val; |
---|
| 2621 | } |
---|
| 2622 | |
---|
| 2623 | |
---|
[135] | 2624 | range_list* regfi_parse_unalloc_cells(REGFI_FILE* file) |
---|
[110] | 2625 | { |
---|
| 2626 | range_list* ret_val; |
---|
[135] | 2627 | REGFI_HBIN* hbin; |
---|
[110] | 2628 | const range_list_element* hbins_elem; |
---|
| 2629 | uint32 i, num_hbins, curr_off, cell_len; |
---|
| 2630 | bool is_unalloc; |
---|
| 2631 | |
---|
| 2632 | ret_val = range_list_new(); |
---|
| 2633 | if(ret_val == NULL) |
---|
| 2634 | return NULL; |
---|
| 2635 | |
---|
| 2636 | num_hbins = range_list_size(file->hbins); |
---|
| 2637 | for(i=0; i<num_hbins; i++) |
---|
| 2638 | { |
---|
| 2639 | hbins_elem = range_list_get(file->hbins, i); |
---|
| 2640 | if(hbins_elem == NULL) |
---|
| 2641 | break; |
---|
[135] | 2642 | hbin = (REGFI_HBIN*)hbins_elem->data; |
---|
[110] | 2643 | |
---|
[135] | 2644 | curr_off = REGFI_HBIN_HEADER_SIZE; |
---|
[110] | 2645 | while(curr_off < hbin->block_size) |
---|
| 2646 | { |
---|
| 2647 | if(!regfi_parse_cell(file->fd, hbin->file_off+curr_off, NULL, 0, |
---|
| 2648 | &cell_len, &is_unalloc)) |
---|
| 2649 | break; |
---|
| 2650 | |
---|
[157] | 2651 | if((cell_len == 0) || ((cell_len & 0x00000007) != 0)) |
---|
[140] | 2652 | { |
---|
| 2653 | regfi_add_message(file, REGFI_MSG_ERROR, "Bad cell length encountered" |
---|
| 2654 | " while parsing unallocated cells at offset 0x%.8X.", |
---|
| 2655 | hbin->file_off+curr_off); |
---|
[110] | 2656 | break; |
---|
[140] | 2657 | } |
---|
| 2658 | |
---|
[110] | 2659 | /* for some reason the record_size of the last record in |
---|
| 2660 | an hbin block can extend past the end of the block |
---|
| 2661 | even though the record fits within the remaining |
---|
| 2662 | space....aaarrrgggghhhhhh */ |
---|
| 2663 | if(curr_off + cell_len >= hbin->block_size) |
---|
| 2664 | cell_len = hbin->block_size - curr_off; |
---|
| 2665 | |
---|
| 2666 | if(is_unalloc) |
---|
| 2667 | range_list_add(ret_val, hbin->file_off+curr_off, |
---|
| 2668 | cell_len, NULL); |
---|
| 2669 | |
---|
| 2670 | curr_off = curr_off+cell_len; |
---|
| 2671 | } |
---|
| 2672 | } |
---|
| 2673 | |
---|
| 2674 | return ret_val; |
---|
| 2675 | } |
---|