source: trunk/lib/regfi.c @ 143

Last change on this file since 143 was 143, checked in by tim, 15 years ago

fixed a null pointer exception

removed some dependencies on less portable items

altered Makefiles to allow for MinGW cross compiling

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