/* * set an chinese word into bitmap * normarlly called mannny times on process initialize * parameters: * bm :bitmap which ts set into * ts :an chinese word, e.g. 天气 */ void word_set(char *bm, char *ts) { unsigned int pos = 0; if (!ts || strlen(ts) > MAX_PHRASE_S_LEN) return; BITMAP_POS(ts, strlen(ts), pos); //char *s = ts; int len = strlen(ts); if (BITMAP_GET(bm, pos)) { //printf("%x seted\n", pos); } //if (pos == 0x24e1373a) printf("%s seted\n", ts); BITMAP_SET(bm, pos); }
/* Allocate a single block, first allocating an hbin (page) at the end * of the current file if necessary. NB. To keep the implementation * simple and more likely to be correct, we do not reuse existing free * blocks. * * seg_len is the size of the block (this INCLUDES the block header). * The header of the block is initialized to -seg_len (negative to * indicate used). id[2] is the block ID (type), eg. "nk" for nk- * record. The block bitmap is updated to show this block as valid. * The rest of the contents of the block will be zero. * * **NB** Because allocate_block may reallocate the memory, all * pointers into the memory become potentially invalid. I really * love writing in C, can't you tell? * * Returns: * > 0 : offset of new block * 0 : error (errno set) */ static size_t allocate_block (hive_h *h, size_t seg_len, const char id[2]) { CHECK_WRITABLE (0); if (seg_len < 4) { /* The caller probably forgot to include the header. Note that * value lists have no ID field, so seg_len == 4 would be possible * for them, albeit unusual. */ SET_ERRNO (ERANGE, "refusing too small allocation (%zu)", seg_len); return 0; } /* Refuse really large allocations. */ if (seg_len > HIVEX_MAX_ALLOCATION) { SET_ERRNO (ERANGE, "refusing too large allocation (%zu)", seg_len); return 0; } /* Round up allocation to multiple of 8 bytes. All blocks must be * on an 8 byte boundary. */ seg_len = (seg_len + 7) & ~7; /* Allocate a new page if necessary. */ if (h->endblocks == 0 || h->endblocks + seg_len > h->endpages) { size_t newendblocks = allocate_page (h, seg_len); if (newendblocks == 0) return 0; h->endblocks = newendblocks; } size_t offset = h->endblocks; DEBUG (2, "new block at 0x%zx, size %zu", offset, seg_len); struct ntreg_hbin_block *blockhdr = (struct ntreg_hbin_block *) ((char *) h->addr + offset); memset (blockhdr, 0, seg_len); blockhdr->seg_len = htole32 (- (int32_t) seg_len); if (id[0] && id[1] && seg_len >= sizeof (struct ntreg_hbin_block)) { blockhdr->id[0] = id[0]; blockhdr->id[1] = id[1]; } BITMAP_SET (h->bitmap, offset); h->endblocks += seg_len; /* If there is space after the last block in the last page, then we * have to put a dummy free block header here to mark the rest of * the page as free. */ ssize_t rem = h->endpages - h->endblocks; if (rem > 0) { DEBUG (2, "marking remainder of page free" " starting at 0x%zx, size %zd", h->endblocks, rem); assert (rem >= 4); blockhdr = (struct ntreg_hbin_block *) ((char *) h->addr + h->endblocks); blockhdr->seg_len = htole32 ((int32_t) rem); } return offset; }
hive_h * hivex_open (const char *filename, int flags) { hive_h *h = NULL; assert (sizeof (struct ntreg_header) == 0x1000); assert (offsetof (struct ntreg_header, csum) == 0x1fc); h = calloc (1, sizeof *h); if (h == NULL) goto error; h->msglvl = flags & HIVEX_OPEN_MSGLVL_MASK; const char *debug = getenv ("HIVEX_DEBUG"); if (debug && STREQ (debug, "1")) h->msglvl = 2; DEBUG (2, "created handle %p", h); h->writable = !!(flags & HIVEX_OPEN_WRITE); h->unsafe = !!(flags & HIVEX_OPEN_UNSAFE); h->filename = strdup (filename); if (h->filename == NULL) goto error; #ifdef O_CLOEXEC h->fd = open (filename, O_RDONLY | O_CLOEXEC | O_BINARY); #else h->fd = open (filename, O_RDONLY | O_BINARY); #endif if (h->fd == -1) goto error; #ifndef O_CLOEXEC fcntl (h->fd, F_SETFD, FD_CLOEXEC); #endif struct stat statbuf; if (fstat (h->fd, &statbuf) == -1) goto error; h->size = statbuf.st_size; if (h->size < 0x2000) { SET_ERRNO (EINVAL, "%s: file is too small to be a Windows NT Registry hive file", filename); goto error; } if (!h->writable) { h->addr = mmap (NULL, h->size, PROT_READ, MAP_SHARED, h->fd, 0); if (h->addr == MAP_FAILED) goto error; DEBUG (2, "mapped file at %p", h->addr); } else { h->addr = malloc (h->size); if (h->addr == NULL) goto error; if (full_read (h->fd, h->addr, h->size) < h->size) goto error; /* We don't need the file descriptor along this path, since we * have read all the data. */ if (close (h->fd) == -1) goto error; h->fd = -1; } /* Check header. */ if (h->hdr->magic[0] != 'r' || h->hdr->magic[1] != 'e' || h->hdr->magic[2] != 'g' || h->hdr->magic[3] != 'f') { SET_ERRNO (ENOTSUP, "%s: not a Windows NT Registry hive file", filename); goto error; } /* Check major version. */ uint32_t major_ver = le32toh (h->hdr->major_ver); if (major_ver != 1) { SET_ERRNO (ENOTSUP, "%s: hive file major version %" PRIu32 " (expected 1)", filename, major_ver); goto error; } h->bitmap = calloc (1 + h->size / 32, 1); if (h->bitmap == NULL) goto error; /* Header checksum. */ uint32_t sum = header_checksum (h); if (sum != le32toh (h->hdr->csum)) { SET_ERRNO (EINVAL, "%s: bad checksum in hive header", filename); goto error; } for (int t=0; t<nr_recode_types; t++) { gl_lock_init (h->iconv_cache[t].mutex); h->iconv_cache[t].handle = NULL; } /* Last modified time. */ h->last_modified = le64toh ((int64_t) h->hdr->last_modified); if (h->msglvl >= 2) { char *name = _hivex_recode (h, utf16le_to_utf8, h->hdr->name, 64, NULL); fprintf (stderr, "hivex_open: header fields:\n" " file version %" PRIu32 ".%" PRIu32 "\n" " sequence nos %" PRIu32 " %" PRIu32 "\n" " (sequences nos should match if hive was synched at shutdown)\n" " last modified %" PRIi64 "\n" " (Windows filetime, x 100 ns since 1601-01-01)\n" " original file name %s\n" " (only 32 chars are stored, name is probably truncated)\n" " root offset 0x%x + 0x1000\n" " end of last page 0x%x + 0x1000 (total file size 0x%zx)\n" " checksum 0x%x (calculated 0x%x)\n", major_ver, le32toh (h->hdr->minor_ver), le32toh (h->hdr->sequence1), le32toh (h->hdr->sequence2), h->last_modified, name ? name : "(conversion failed)", le32toh (h->hdr->offset), le32toh (h->hdr->blocks), h->size, le32toh (h->hdr->csum), sum); free (name); } h->rootoffs = le32toh (h->hdr->offset) + 0x1000; h->endpages = le32toh (h->hdr->blocks) + 0x1000; DEBUG (2, "root offset = 0x%zx", h->rootoffs); /* We'll set this flag when we see a block with the root offset (ie. * the root block). */ int seen_root_block = 0, bad_root_block = 0; /* Collect some stats. */ size_t pages = 0; /* Number of hbin pages read. */ size_t smallest_page = SIZE_MAX, largest_page = 0; size_t blocks = 0; /* Total number of blocks found. */ size_t smallest_block = SIZE_MAX, largest_block = 0, blocks_bytes = 0; size_t used_blocks = 0; /* Total number of used blocks found. */ size_t used_size = 0; /* Total size (bytes) of used blocks. */ /* Read the pages and blocks. The aim here is to be robust against * corrupt or malicious registries. So we make sure the loops * always make forward progress. We add the address of each block * we read to a hash table so pointers will only reference the start * of valid blocks. */ size_t off; struct ntreg_hbin_page *page; for (off = 0x1000; off < h->size; off += le32toh (page->page_size)) { if (off >= h->endpages) break; page = (struct ntreg_hbin_page *) ((char *) h->addr + off); if (page->magic[0] != 'h' || page->magic[1] != 'b' || page->magic[2] != 'i' || page->magic[3] != 'n') { if (!h->unsafe) { SET_ERRNO (ENOTSUP, "%s: trailing garbage at end of file " "(at 0x%zx, after %zu pages)", filename, off, pages); goto error; } DEBUG (2, "page not found at expected offset 0x%zx, " "seeking until one is found or EOF is reached", off); int found = 0; while (off < h->size) { off += 0x1000; if (off >= h->endpages) break; page = (struct ntreg_hbin_page *) ((char *) h->addr + off); if (page->magic[0] == 'h' && page->magic[1] == 'b' && page->magic[2] == 'i' && page->magic[3] == 'n') { DEBUG (2, "found next page by seeking at 0x%zx", off); found = 1; break; } } if (!found) { DEBUG (2, "page not found and end of pages section reached"); break; } } size_t page_size = le32toh (page->page_size); DEBUG (2, "page at 0x%zx, size %zu", off, page_size); pages++; if (page_size < smallest_page) smallest_page = page_size; if (page_size > largest_page) largest_page = page_size; if (page_size <= sizeof (struct ntreg_hbin_page) || (page_size & 0x0fff) != 0) { SET_ERRNO (ENOTSUP, "%s: page size %zu at 0x%zx, bad registry", filename, page_size, off); goto error; } if (off + page_size > h->size) { SET_ERRNO (ENOTSUP, "%s: page size %zu at 0x%zx extends beyond end of file, bad registry", filename, page_size, off); goto error; } size_t page_offset = le32toh(page->offset_first) + 0x1000; if (page_offset != off) { SET_ERRNO (ENOTSUP, "%s: declared page offset (0x%zx) does not match computed " "offset (0x%zx), bad registry", filename, page_offset, off); goto error; } /* Read the blocks in this page. */ size_t blkoff; struct ntreg_hbin_block *block; size_t seg_len; for (blkoff = off + 0x20; blkoff < off + page_size; blkoff += seg_len) { blocks++; int is_root = blkoff == h->rootoffs; if (is_root) seen_root_block = 1; block = (struct ntreg_hbin_block *) ((char *) h->addr + blkoff); int used; seg_len = block_len (h, blkoff, &used); /* https://gcc.gnu.org/bugzilla/show_bug.cgi?id=78665 */ #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wstrict-overflow" if (seg_len <= 4 || (seg_len & 3) != 0) { #pragma GCC diagnostic pop if (is_root || !h->unsafe) { SET_ERRNO (ENOTSUP, "%s, the block at 0x%zx has invalid size %" PRIu32 ", bad registry", filename, blkoff, le32toh (block->seg_len)); goto error; } else { DEBUG (2, "%s: block at 0x%zx has invalid size %" PRIu32 ", skipping", filename, blkoff, le32toh (block->seg_len)); break; } } if (h->msglvl >= 2) { unsigned char *id = (unsigned char *) block->id; int id0 = id[0], id1 = id[1]; fprintf (stderr, "%s: %s: " "%s block id %d,%d (%c%c) at 0x%zx size %zu%s\n", "hivex", __func__, used ? "used" : "free", id0, id1, c_isprint (id0) ? id0 : '.', c_isprint (id1) ? id1 : '.', blkoff, seg_len, is_root ? " (root)" : ""); } blocks_bytes += seg_len; if (seg_len < smallest_block) smallest_block = seg_len; if (seg_len > largest_block) largest_block = seg_len; if (is_root && !used) bad_root_block = 1; if (used) { used_blocks++; used_size += seg_len; /* Root block must be an nk-block. */ if (is_root && (block->id[0] != 'n' || block->id[1] != 'k')) bad_root_block = 1; /* Note this blkoff is a valid address. */ BITMAP_SET (h->bitmap, blkoff); } } } if (!seen_root_block) { SET_ERRNO (ENOTSUP, "%s: no root block found", filename); goto error; } if (bad_root_block) { SET_ERRNO (ENOTSUP, "%s: bad root block (free or not nk)", filename); goto error; } DEBUG (1, "successfully read Windows Registry hive file:\n" " pages: %zu [sml: %zu, lge: %zu]\n" " blocks: %zu [sml: %zu, avg: %zu, lge: %zu]\n" " blocks used: %zu\n" " bytes used: %zu", pages, smallest_page, largest_page, blocks, smallest_block, blocks_bytes / blocks, largest_block, used_blocks, used_size); return h; error:; int err = errno; if (h) { free (h->bitmap); if (h->addr && h->size && h->addr != MAP_FAILED) { if (!h->writable) munmap (h->addr, h->size); else free (h->addr); } if (h->fd >= 0) close (h->fd); free (h->filename); free (h); } errno = err; return NULL; }
/*------------------------------------------------------------------------- * * Preform initialization of guests and guest CPUs * * Should be called on BSP only while all APs are stopped * * Return TRUE for success * *------------------------------------------------------------------------- */ boolean_t initialize_all_guests(uint32_t number_of_host_processors, const mon_memory_layout_t *mon_memory_layout, const mon_guest_startup_t * primary_guest_startup_state, uint32_t number_of_secondary_guests, const mon_guest_startup_t * secondary_guests_startup_state_array, const mon_application_params_struct_t * application_params) { guest_handle_t primary_guest; gpm_handle_t primary_guest_startup_gpm; boolean_t ok = FALSE; /* guest_handle_t cur_guest; */ guest_cpu_handle_t gcpu; guest_gcpu_econtext_t gcpu_context; MON_ASSERT(hw_cpu_id() == 0); MON_ASSERT(number_of_host_processors > 0); MON_ASSERT(mon_memory_layout); MON_ASSERT(primary_guest_startup_state); if (number_of_secondary_guests > 0) { MON_LOG(mask_anonymous, level_trace, "initialize_all_guests ASSERT: Secondary guests are" " yet not implemented\n"); MON_ASSERT(secondary_guests_startup_state_array); /* init guests and allocate memory for them */ /* shutdown temporary layout object */ MON_DEADLOOP(); return FALSE; } /* first init primary guest */ MON_LOG(mask_anonymous, level_trace, "Init primary guest\n"); /* BUGBUG: This is a workaround until loader will not do this!!! */ BITMAP_SET(((mon_guest_startup_t *)primary_guest_startup_state)->flags, MON_GUEST_FLAG_REAL_BIOS_ACCESS_ENABLE | MON_GUEST_FLAG_LAUNCH_IMMEDIATELY); /* TODO: Uses global policym but should be part of mon_guest_startup_t * structure. */ primary_guest = init_single_guest(number_of_host_processors, primary_guest_startup_state, NULL); if (!primary_guest) { MON_LOG(mask_anonymous, level_trace, "initialize_all_guests: Cannot init primary guest\n"); MON_DEADLOOP(); return FALSE; } guest_set_primary(primary_guest); primary_guest_startup_gpm = mon_guest_get_startup_gpm(primary_guest); /* init memory layout in the startup gpm */ ok = init_memory_layout(mon_memory_layout, primary_guest_startup_gpm, number_of_secondary_guests > 0, application_params); /* Set active_gpm to startup gpm */ for (gcpu = mon_guest_gcpu_first(primary_guest, &gcpu_context); gcpu; gcpu = mon_guest_gcpu_next(&gcpu_context)) mon_gcpu_set_current_gpm(gcpu, primary_guest_startup_gpm); MON_LOG(mask_anonymous, level_trace, "Primary guest initialized successfully\n"); return TRUE; }