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