summaryrefslogtreecommitdiffstats
path: root/hivex
diff options
context:
space:
mode:
authorRichard Jones <rjones@redhat.com>2010-01-18 11:08:56 +0000
committerRichard Jones <rjones@redhat.com>2010-02-19 15:00:31 +0000
commitdd1a57f8b76e5acd8f86d7edad07b9ea1d316de2 (patch)
treef1ce3f8f9079326bc91055b8c0f70f342de0d6e7 /hivex
parent253def9de52d744e5ecb75fe0e163276d8ab9653 (diff)
downloadhivex-dd1a57f8b76e5acd8f86d7edad07b9ea1d316de2.tar.gz
hivex-dd1a57f8b76e5acd8f86d7edad07b9ea1d316de2.tar.xz
hivex-dd1a57f8b76e5acd8f86d7edad07b9ea1d316de2.zip
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.
Diffstat (limited to 'hivex')
-rw-r--r--hivex/Makefile.am8
-rw-r--r--hivex/README3
-rw-r--r--hivex/hivex.c44
-rw-r--r--hivex/hivex.h4
-rw-r--r--hivex/hivex.pod15
5 files changed, 52 insertions, 22 deletions
diff --git a/hivex/Makefile.am b/hivex/Makefile.am
index 5624b16..312cc3c 100644
--- a/hivex/Makefile.am
+++ b/hivex/Makefile.am
@@ -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"\" \
diff --git a/hivex/README b/hivex/README
index 7bed47b..3f7f018 100644
--- a/hivex/README
+++ b/hivex/README
@@ -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
diff --git a/hivex/hivex.c b/hivex/hivex.c
index e0118af..44f2998 100644
--- a/hivex/hivex.c
+++ b/hivex/hivex.c
@@ -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);
diff --git a/hivex/hivex.h b/hivex/hivex.h
index fe5c128..56718b4 100644
--- a/hivex/hivex.h
+++ b/hivex/hivex.h
@@ -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);
diff --git a/hivex/hivex.pod b/hivex/hivex.pod
index 7e41d0c..5a58144 100644
--- a/hivex/hivex.pod
+++ b/hivex/hivex.pod
@@ -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