hivex: Add HIVEX_OPEN_WRITE flag to allow hive to be opened for writing.

If this flag is omitted (as in the case for all existing callers)
then the hive is still opened read-only.

We add a 'writable' flag to the hive handle, and we change the way
that the hive file (data) is stored.  The data is still mmapped if
the file is opened read-only, since that is more efficient and allows
us to handle larger hives.  However if we need to write to the file
then we have to read it all into memory, since if we had to extend the
file we need to realloc that data.

Note the manpage section L</WRITING TO HIVE FILES> comes in a later
commit.
This commit is contained in:
Richard Jones
2010-01-18 11:08:56 +00:00
parent db8a46aa11
commit 4718aeb9f0
6 changed files with 53 additions and 22 deletions

View File

@@ -60,6 +60,7 @@ modules='
arpa_inet
c-ctype
closeout
full-read
full-write
gitlog-to-changelog
gnu-make

View File

@@ -26,9 +26,9 @@ libhivex_la_SOURCES = \
hivex.h \
byte_conversions.h
libhivex_la_LDFLAGS = -version-info 0:0:0
libhivex_la_CFLAGS = \
$(WARN_CFLAGS) $(WERROR_CFLAGS)
libhivex_la_LDFLAGS = -version-info 0:0:0 $(LTLIBINTL) $(LTLIBTHREAD)
libhivex_la_CFLAGS = $(WARN_CFLAGS) $(WERROR_CFLAGS)
libhivex_la_CPPFLAGS = -I$(top_srcdir)/gnulib/lib
bin_PROGRAMS = hivexml hivexsh
bin_SCRIPTS = hivexget
@@ -36,7 +36,7 @@ bin_SCRIPTS = hivexget
hivexml_SOURCES = \
hivexml.c
hivexml_LDADD = libhivex.la $(LIBXML2_LIBS)
hivexml_LDADD = libhivex.la $(LIBXML2_LIBS) ../gnulib/lib/libgnu.la
hivexml_CFLAGS = \
-I$(top_srcdir)/src \
-DLOCALEBASEDIR=\""$(datadir)/locale"\" \

View File

@@ -5,9 +5,6 @@ Copyright (C) 2009-2010 Red Hat Inc.
This is a self-contained library for reading Windows Registry "hive"
binary files.
It is totally dedicated to reading the files and doesn't deal with
writing or modifying them in any way.
Unlike many other tools in this area, it doesn't use the textual .REG
format for output, because parsing that is as much trouble as parsing
the original binary format. Instead it makes the file available

View File

@@ -34,6 +34,12 @@
#include <sys/stat.h>
#include <assert.h>
#include "full-read.h"
#ifndef O_CLOEXEC
#define O_CLOEXEC 0
#endif
#define STREQ(a,b) (strcmp((a),(b)) == 0)
#define STRCASEEQ(a,b) (strcasecmp((a),(b)) == 0)
//#define STRNEQ(a,b) (strcmp((a),(b)) != 0)
@@ -54,8 +60,9 @@ struct hive_h {
int fd;
size_t size;
int msglvl;
int writable;
/* Memory-mapped (readonly) registry file. */
/* Registry file, memory mapped if read-only, or malloc'd if writing. */
union {
char *addr;
struct ntreg_header *hdr;
@@ -260,11 +267,12 @@ hivex_open (const char *filename, int flags)
if (h->msglvl >= 2)
fprintf (stderr, "hivex_open: created handle %p\n", h);
h->writable = !!(flags & HIVEX_OPEN_WRITE);
h->filename = strdup (filename);
if (h->filename == NULL)
goto error;
h->fd = open (filename, O_RDONLY);
h->fd = open (filename, O_RDONLY | O_CLOEXEC);
if (h->fd == -1)
goto error;
@@ -274,12 +282,21 @@ hivex_open (const char *filename, int flags)
h->size = statbuf.st_size;
h->addr = mmap (NULL, h->size, PROT_READ, MAP_SHARED, h->fd, 0);
if (h->addr == MAP_FAILED)
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;
if (h->msglvl >= 2)
fprintf (stderr, "hivex_open: mapped file at %p\n", h->addr);
if (h->msglvl >= 2)
fprintf (stderr, "hivex_open: mapped file at %p\n", 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;
}
/* Check header. */
if (h->hdr->magic[0] != 'r' ||
@@ -471,8 +488,12 @@ hivex_open (const char *filename, int flags)
int err = errno;
if (h) {
free (h->bitmap);
if (h->addr && h->size && h->addr != MAP_FAILED)
munmap (h->addr, h->size);
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);
@@ -488,7 +509,10 @@ hivex_close (hive_h *h)
int r;
free (h->bitmap);
munmap (h->addr, h->size);
if (!h->writable)
munmap (h->addr, h->size);
else
free (h->addr);
r = close (h->fd);
free (h->filename);
free (h);

View File

@@ -69,9 +69,11 @@ enum hive_type {
typedef enum hive_type hive_type;
/* Bitmask of flags passed to hivex_open. */
#define HIVEX_OPEN_VERBOSE 1
#define HIVEX_OPEN_DEBUG 2
#define HIVEX_OPEN_MSGLVL_MASK 3
#define HIVEX_OPEN_MSGLVL_MASK (HIVEX_OPEN_VERBOSE|HIVEX_OPEN_DEBUG)
#define HIVEX_OPEN_WRITE 4
extern hive_h *hivex_open (const char *filename, int flags);
extern int hivex_close (hive_h *h);

View File

@@ -13,8 +13,7 @@ hivex - Windows Registry "hive" extraction library
libhivex is a library for extracting the contents of Windows Registry
"hive" files. It is designed to be secure against buggy or malicious
registry files, and to have limited functionality (writing or
modifying these files is not in the scope of this library).
registry files.
Unlike many other tools in this area, it doesn't use the textual .REG
format for output, because parsing that is as much trouble as parsing
@@ -32,8 +31,7 @@ L<hivexget(1)>).
Opens the hive named C<filename> for reading.
Flags is an ORed list of the open flags (or C<0> if you don't
want to pass any flags). Currently the only
flags defined are:
want to pass any flags). These flags are defined:
=over 4
@@ -49,6 +47,12 @@ itself.
This is also selected if the C<HIVEX_DEBUG> environment variable
is set to 1.
=item HIVEX_OPEN_WRITE
Open the hive for writing. If omitted, the hive is read-only.
See L</WRITING TO HIVE FILES>.
=back
C<hivex_open> returns a hive handle. On error this returns NULL and
@@ -58,6 +62,9 @@ sets C<errno> to indicate the error.
Close a hive handle and free all associated resources.
Note that any uncommitted writes are I<not> committed by this call,
but instead are lost. See L</WRITING TO HIVE FILES>.
Returns 0 on success. On error this returns -1 and sets errno.
=back