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