summaryrefslogtreecommitdiffabout
path: root/src
authorSergey Poznyakoff <gray@gnu.org>2018-05-16 19:02:53 (GMT)
committer Sergey Poznyakoff <gray@gnu.org>2018-05-16 19:02:53 (GMT)
commit48733c9aca3d12fc07a791d24fb3e6890de9c01f (patch) (side-by-side diff)
tree95d483c9738e83ea30cedd8b75ea93cdd7876bff /src
parentbbfd14e24d0c288bff351a9ed4cdac217e6cbe4d (diff)
downloadgdbm-48733c9aca3d12fc07a791d24fb3e6890de9c01f.tar.gz
gdbm-48733c9aca3d12fc07a791d24fb3e6890de9c01f.tar.bz2
Improve database file safety checks.
* src/bucket.c (_gdbm_get_bucket): Verify bucket consistency. * src/gdbm.h.in (GDBM_BAD_BUCKET, GDBM_WRONG_ARCH): New error codes. * src/gdbmerrno.c (gdbm_errlist): New error codes. * src/gdbmopen.c (gdbm_fd_open): Improve header consistency checks.
Diffstat (limited to 'src') (more/less context) (ignore whitespace changes)
-rw-r--r--src/bucket.c14
-rw-r--r--src/gdbm.h.in6
-rw-r--r--src/gdbmerrno.c4
-rw-r--r--src/gdbmopen.c76
4 files changed, 75 insertions, 25 deletions
diff --git a/src/bucket.c b/src/bucket.c
index 6d12589..6def504 100644
--- a/src/bucket.c
+++ b/src/bucket.c
@@ -72,6 +72,8 @@ _gdbm_get_bucket (GDBM_FILE dbf, int dir_index)
/* Is that one is not already current, we must find it. */
if (dbf->cache_entry->ca_adr != bucket_adr)
{
+ size_t max_bucket_elts;
+
/* Look in the cache. */
for (index = 0; index < dbf->cache_size; index++)
{
@@ -117,6 +119,18 @@ _gdbm_get_bucket (GDBM_FILE dbf, int dir_index)
_gdbm_fatal (dbf, gdbm_db_strerror (dbf));
return -1;
}
+ /* Validate the bucket */
+ max_bucket_elts =
+ (dbf->header->bucket_size - sizeof(hash_bucket))
+ / sizeof(bucket_element) + 1;
+ if (!(dbf->bucket->count >= 0
+ && dbf->bucket->av_count >= 0
+ && dbf->bucket->count <= max_bucket_elts
+ && dbf->bucket->av_count <= max_bucket_elts))
+ {
+ GDBM_SET_ERRNO (dbf, GDBM_BAD_BUCKET, TRUE);
+ return -1;
+ }
}
return 0;
diff --git a/src/gdbm.h.in b/src/gdbm.h.in
index 8dd61b0..d6a8306 100644
--- a/src/gdbm.h.in
+++ b/src/gdbm.h.in
@@ -219,9 +219,11 @@ extern int gdbm_copy_meta (GDBM_FILE dst, GDBM_FILE src);
# define GDBM_NEED_RECOVERY 29
# define GDBM_BACKUP_FAILED 30
# define GDBM_DIR_OVERFLOW 31
-
+# define GDBM_BAD_BUCKET 32
+# define GDBM_WRONG_ARCH 33
+
# define _GDBM_MIN_ERRNO 0
-# define _GDBM_MAX_ERRNO GDBM_DIR_OVERFLOW
+# define _GDBM_MAX_ERRNO GDBM_WRONG_ARCH
/* This one was never used and will be removed in the future */
# define GDBM_UNKNOWN_UPDATE GDBM_UNKNOWN_ERROR
diff --git a/src/gdbmerrno.c b/src/gdbmerrno.c
index 1962d1a..0c8c8e5 100644
--- a/src/gdbmerrno.c
+++ b/src/gdbmerrno.c
@@ -131,7 +131,9 @@ const char * const gdbm_errlist[_GDBM_MAX_ERRNO+1] = {
[GDBM_ERR_FILE_MODE] = N_("Failed to restore file mode"),
[GDBM_NEED_RECOVERY] = N_("Database needs recovery"),
[GDBM_BACKUP_FAILED] = N_("Failed to create backup copy"),
- [GDBM_DIR_OVERFLOW] = N_("Bucket directory overflow")
+ [GDBM_DIR_OVERFLOW] = N_("Bucket directory overflow"),
+ [GDBM_BAD_BUCKET] = N_("Malformed bucket header"),
+ [GDBM_WRONG_OFF_T] = N_("File header assumes wrong off_t size")
};
const char *
diff --git a/src/gdbmopen.c b/src/gdbmopen.c
index db56440..669f8fb 100644
--- a/src/gdbmopen.c
+++ b/src/gdbmopen.c
@@ -49,6 +49,52 @@ compute_directory_size (GDBM_FILE dbf, blksize_t block_size,
*ret_dir_bits = dir_bits;
}
+static int
+validate_header (gdbm_file_header const *hdr, struct stat const *st)
+{
+ /* Is the magic number good? */
+ if (hdr->header_magic != GDBM_MAGIC)
+ {
+ switch (hdr->header_magic)
+ {
+ case GDBM_OMAGIC:
+ /* OK */
+ break;
+
+ case GDBM_OMAGIC_SWAP:
+ case GDBM_MAGIC32_SWAP:
+ case GDBM_MAGIC64_SWAP:
+ return GDBM_BYTE_SWAPPED;
+
+ case GDBM_MAGIC32:
+ case GDBM_MAGIC64:
+ return GDBM_WRONG_OFF_T;
+
+ default:
+ return GDBM_BAD_MAGIC_NUMBER;
+ }
+ }
+
+ if (!(hdr->block_size > sizeof (gdbm_file_header)
+ && hdr->block_size - sizeof (gdbm_file_header) >=
+ sizeof(hdr->avail.av_table[0])))
+ {
+ return GDBM_BLOCK_SIZE_ERROR;
+ }
+
+ /* Make sure dir and dir + dir_size fall within the file boundary */
+ if (!(hdr->dir > 0
+ && hdr->dir < st->st_size
+ && hdr->dir_size > 0
+ && hdr->dir + hdr->dir_size < st->st_size))
+ return GDBM_BAD_FILE_OFFSET;
+
+ if (!(hdr->bucket_size > sizeof(hash_bucket)))
+ return GDBM_BAD_MAGIC_NUMBER;
+
+ return 0;
+}
+
GDBM_FILE
gdbm_fd_open (int fd, const char *file_name, int block_size,
int flags, void (*fatal_func) (const char *))
@@ -323,7 +369,8 @@ gdbm_fd_open (int fd, const char *file_name, int block_size,
header and initialize the hash directory. */
gdbm_file_header partial_header; /* For the first part of it. */
-
+ int rc;
+
/* Read the partial file header. */
if (_gdbm_full_read (dbf, &partial_header, sizeof (gdbm_file_header)))
{
@@ -336,33 +383,17 @@ gdbm_fd_open (int fd, const char *file_name, int block_size,
return NULL;
}
- /* Is the magic number good? */
- if (partial_header.header_magic != GDBM_MAGIC
- && partial_header.header_magic != GDBM_OMAGIC)
+ /* Is the header valid? */
+ rc = validate_header (&partial_header, &file_stat);
+ if (rc != GDBM_NO_ERROR)
{
if (!(flags & GDBM_CLOERROR))
dbf->desc = -1;
gdbm_close (dbf);
- switch (partial_header.header_magic)
- {
- case GDBM_OMAGIC_SWAP:
- case GDBM_MAGIC32_SWAP:
- case GDBM_MAGIC64_SWAP:
- GDBM_SET_ERRNO2 (NULL, GDBM_BYTE_SWAPPED, FALSE,
- GDBM_DEBUG_OPEN);
- break;
- case GDBM_MAGIC32:
- case GDBM_MAGIC64:
- GDBM_SET_ERRNO2 (NULL, GDBM_BAD_FILE_OFFSET, FALSE,
- GDBM_DEBUG_OPEN);
- break;
- default:
- GDBM_SET_ERRNO2 (NULL, GDBM_BAD_MAGIC_NUMBER, FALSE,
- GDBM_DEBUG_OPEN);
- }
+ GDBM_SET_ERRNO2 (NULL, rc, FALSE, GDBM_DEBUG_OPEN);
return NULL;
}
-
+
/* It is a good database, read the entire header. */
dbf->header = malloc (partial_header.block_size);
if (dbf->header == NULL)
@@ -373,6 +404,7 @@ gdbm_fd_open (int fd, const char *file_name, int block_size,
GDBM_SET_ERRNO2 (NULL, GDBM_MALLOC_ERROR, FALSE, GDBM_DEBUG_OPEN);
return NULL;
}
+
memcpy (dbf->header, &partial_header, sizeof (gdbm_file_header));
if (_gdbm_full_read (dbf, &dbf->header->avail.av_table[1],
dbf->header->block_size - sizeof (gdbm_file_header)))

Return to:

Send suggestions and report system problems to the System administrator.