tdb: fix tdb_check() on other-endian tdbs.
[ccan] / ccan / tdb / check.c
index cd2b9d9ec57cda07ca1e1f747840d7b2c020e10b..53fb286613a3e829e8f2aa159109124c3e182503 100644 (file)
@@ -1,14 +1,14 @@
- /* 
+ /*
    Unix SMB/CIFS implementation.
 
    trivial database library
 
    Copyright (C) Rusty Russell            2009
-   
+
      ** NOTE! The following LGPL license applies to the tdb
      ** library. This does NOT imply that all of Samba is released
      ** under the LGPL
-   
+
    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
@@ -30,7 +30,7 @@ static bool tdb_check_header(struct tdb_context *tdb, tdb_off_t *recovery)
 {
        struct tdb_header hdr;
 
-       if (tdb->methods->tdb_read(tdb, 0, &hdr, sizeof(hdr), DOCONV()) == -1)
+       if (tdb->methods->tdb_read(tdb, 0, &hdr, sizeof(hdr), 0) == -1)
                return false;
        if (strcmp(hdr.magic_food, TDB_MAGIC_FOOD) != 0)
                goto corrupt;
@@ -39,7 +39,7 @@ static bool tdb_check_header(struct tdb_context *tdb, tdb_off_t *recovery)
        if (hdr.version != TDB_VERSION)
                goto corrupt;
 
-       if (hdr.rwlocks != 0)
+       if (hdr.hashcheck != hashcheck(tdb))
                goto corrupt;
 
        if (hdr.hash_size == 0)
@@ -57,42 +57,66 @@ static bool tdb_check_header(struct tdb_context *tdb, tdb_off_t *recovery)
 
 corrupt:
        tdb->ecode = TDB_ERR_CORRUPT;
+       TDB_LOG((tdb, TDB_DEBUG_ERROR, "Header is corrupt\n"));
        return false;
 }
 
 /* Generic record header check. */
 static bool tdb_check_record(struct tdb_context *tdb,
                             tdb_off_t off,
-                            const struct list_struct *rec)
+                            const struct tdb_record *rec)
 {
        tdb_off_t tailer;
 
        /* Check rec->next: 0 or points to record offset, aligned. */
-       if (rec->next > 0 && rec->next < TDB_DATA_START(tdb->header.hash_size))
+       if (rec->next > 0 && rec->next < TDB_DATA_START(tdb->header.hash_size)){
+               TDB_LOG((tdb, TDB_DEBUG_ERROR,
+                        "Record offset %d too small next %d\n",
+                        off, rec->next));
                goto corrupt;
-       if (rec->next + sizeof(*rec) < rec->next)
+       }
+       if (rec->next + sizeof(*rec) < rec->next) {
+               TDB_LOG((tdb, TDB_DEBUG_ERROR,
+                        "Record offset %d too large next %d\n",
+                        off, rec->next));
                goto corrupt;
-       if ((rec->next % TDB_ALIGNMENT) != 0)
+       }
+       if ((rec->next % TDB_ALIGNMENT) != 0) {
+               TDB_LOG((tdb, TDB_DEBUG_ERROR,
+                        "Record offset %d misaligned next %d\n",
+                        off, rec->next));
                goto corrupt;
-       if (tdb->methods->tdb_oob(tdb, rec->next+sizeof(*rec), 1))
+       }
+       if (tdb->methods->tdb_oob(tdb, rec->next+sizeof(*rec), 0))
                goto corrupt;
 
        /* Check rec_len: similar to rec->next, implies next record. */
-       if ((rec->rec_len % TDB_ALIGNMENT) != 0)
+       if ((rec->rec_len % TDB_ALIGNMENT) != 0) {
+               TDB_LOG((tdb, TDB_DEBUG_ERROR,
+                        "Record offset %d misaligned length %d\n",
+                        off, rec->rec_len));
                goto corrupt;
+       }
        /* Must fit tailer. */
-       if (rec->rec_len < sizeof(tailer))
+       if (rec->rec_len < sizeof(tailer)) {
+               TDB_LOG((tdb, TDB_DEBUG_ERROR,
+                        "Record offset %d too short length %d\n",
+                        off, rec->rec_len));
                goto corrupt;
+       }
        /* OOB allows "right at the end" access, so this works for last rec. */
-       if (tdb->methods->tdb_oob(tdb, off+sizeof(*rec)+rec->rec_len, 1))
+       if (tdb->methods->tdb_oob(tdb, off+sizeof(*rec)+rec->rec_len, 0))
                goto corrupt;
 
        /* Check tailer. */
        if (tdb_ofs_read(tdb, off+sizeof(*rec)+rec->rec_len-sizeof(tailer),
                         &tailer) == -1)
                goto corrupt;
-       if (tailer != sizeof(*rec) + rec->rec_len)
+       if (tailer != sizeof(*rec) + rec->rec_len) {
+               TDB_LOG((tdb, TDB_DEBUG_ERROR,
+                        "Record offset %d invalid tailer\n", off));
                goto corrupt;
+       }
 
        return true;
 
@@ -205,10 +229,10 @@ static void record_offset(unsigned char bits[], tdb_off_t off)
 /* Check that an in-use record is valid. */
 static bool tdb_check_used_record(struct tdb_context *tdb,
                                  tdb_off_t off,
-                                 const struct list_struct *rec,
+                                 const struct tdb_record *rec,
                                  unsigned char **hashes,
                                  int (*check)(TDB_DATA, TDB_DATA, void *),
-                                 void *private)
+                                 void *private_data)
 {
        TDB_DATA key, data;
 
@@ -216,15 +240,21 @@ static bool tdb_check_used_record(struct tdb_context *tdb,
                return false;
 
        /* key + data + tailer must fit in record */
-       if (rec->key_len + rec->data_len + sizeof(tdb_off_t) > rec->rec_len)
+       if (rec->key_len + rec->data_len + sizeof(tdb_off_t) > rec->rec_len) {
+               TDB_LOG((tdb, TDB_DEBUG_ERROR,
+                        "Record offset %d too short for contents\n", off));
                return false;
+       }
 
        key = get_bytes(tdb, off + sizeof(*rec), rec->key_len);
        if (!key.dptr)
                return false;
 
-       if (tdb->hash_fn(&key) != rec->full_hash)
+       if (tdb->hash_fn(&key) != rec->full_hash) {
+               TDB_LOG((tdb, TDB_DEBUG_ERROR,
+                        "Record offset %d has incorrect hash\n", off));
                goto fail_put_key;
+       }
 
        /* Mark this offset as a known value for this hash bucket. */
        record_offset(hashes[BUCKET(rec->full_hash)+1], off);
@@ -240,7 +270,7 @@ static bool tdb_check_used_record(struct tdb_context *tdb,
                if (!data.dptr)
                        goto fail_put_key;
 
-               if (check(key, data, private) == -1)
+               if (check(key, data, private_data) == -1)
                        goto fail_put_data;
                put_bytes(tdb, data);
        }
@@ -258,7 +288,7 @@ fail_put_key:
 /* Check that an unused record is valid. */
 static bool tdb_check_free_record(struct tdb_context *tdb,
                                  tdb_off_t off,
-                                 const struct list_struct *rec,
+                                 const struct tdb_record *rec,
                                  unsigned char **hashes)
 {
        if (!tdb_check_record(tdb, off, rec))
@@ -272,18 +302,42 @@ static bool tdb_check_free_record(struct tdb_context *tdb,
        return true;
 }
 
+/* Slow, but should be very rare. */
+static size_t dead_space(struct tdb_context *tdb, tdb_off_t off)
+{
+       size_t len;
+
+       for (len = 0; off + len < tdb->map_size; len++) {
+               char c;
+               if (tdb->methods->tdb_read(tdb, off, &c, 1, 0))
+                       return 0;
+               if (c != 0 && c != 0x42)
+                       break;
+       }
+       return len;
+}
+
 int tdb_check(struct tdb_context *tdb,
-             int (*check)(TDB_DATA key, TDB_DATA data, void *private),
-             void *private)
+             int (*check)(TDB_DATA key, TDB_DATA data, void *private_data),
+             void *private_data)
 {
        unsigned int h;
        unsigned char **hashes;
        tdb_off_t off, recovery_start;
-       struct list_struct rec;
+       struct tdb_record rec;
        bool found_recovery = false;
-
-       if (tdb_lockall(tdb) == -1)
-               return -1;
+       tdb_len_t dead;
+       bool locked;
+
+       /* Read-only databases use no locking at all: it's best-effort.
+        * We may have a write lock already, so skip that case too. */
+       if (tdb->read_only || tdb->allrecord_lock.count != 0) {
+               locked = false;
+       } else {
+               if (tdb_lockall_read(tdb) == -1)
+                       return -1;
+               locked = true;
+       }
 
        /* Make sure we know true size of the underlying file. */
        tdb->methods->tdb_oob(tdb, tdb->map_size + 1, 1);
@@ -295,11 +349,13 @@ int tdb_check(struct tdb_context *tdb,
        /* We should have the whole header, too. */
        if (tdb->map_size < TDB_DATA_START(tdb->header.hash_size)) {
                tdb->ecode = TDB_ERR_CORRUPT;
+               TDB_LOG((tdb, TDB_DEBUG_ERROR, "File too short for hashes\n"));
                goto unlock;
        }
 
        /* One big malloc: pointers then bit arrays. */
-       hashes = calloc(1, sizeof(hashes[0]) * (1+tdb->header.hash_size)
+       hashes = (unsigned char **)calloc(
+                       1, sizeof(hashes[0]) * (1+tdb->header.hash_size)
                        + BITMAP_BITS / CHAR_BIT * (1+tdb->header.hash_size));
        if (!hashes) {
                tdb->ecode = TDB_ERR_OOM;
@@ -331,20 +387,44 @@ int tdb_check(struct tdb_context *tdb,
                case TDB_MAGIC:
                case TDB_DEAD_MAGIC:
                        if (!tdb_check_used_record(tdb, off, &rec, hashes,
-                                                  check, private))
+                                                  check, private_data))
                                goto free;
                        break;
                case TDB_FREE_MAGIC:
                        if (!tdb_check_free_record(tdb, off, &rec, hashes))
                                goto free;
                        break;
+               /* If we crash after ftruncate, we can get zeroes or fill. */
+               case TDB_RECOVERY_INVALID_MAGIC:
+               case 0x42424242:
+                       if (recovery_start == off) {
+                               found_recovery = true;
+                               break;
+                       }
+                       dead = dead_space(tdb, off);
+                       if (dead < sizeof(rec))
+                               goto corrupt;
+
+                       TDB_LOG((tdb, TDB_DEBUG_ERROR,
+                                "Dead space at %d-%d (of %u)\n",
+                                off, off + dead, tdb->map_size));
+                       rec.rec_len = dead - sizeof(rec);
+                       break;
                case TDB_RECOVERY_MAGIC:
-                       if (recovery_start != off)
+                       if (recovery_start != off) {
+                               TDB_LOG((tdb, TDB_DEBUG_ERROR,
+                                        "Unexpected recovery record at offset %d\n",
+                                        off));
                                goto free;
+                       }
                        found_recovery = true;
                        break;
-               default:
+               default: ;
+               corrupt:
                        tdb->ecode = TDB_ERR_CORRUPT;
+                       TDB_LOG((tdb, TDB_DEBUG_ERROR,
+                                "Bad magic 0x%x at offset %d\n",
+                                rec.magic, off));
                        goto free;
                }
        }
@@ -356,28 +436,32 @@ int tdb_check(struct tdb_context *tdb,
                for (i = 0; i < BITMAP_BITS / CHAR_BIT; i++) {
                        if (hashes[h][i] != 0) {
                                tdb->ecode = TDB_ERR_CORRUPT;
+                               TDB_LOG((tdb, TDB_DEBUG_ERROR,
+                                        "Hashes do not match records\n"));
                                goto free;
                        }
                }
        }
 
        /* We must have found recovery area if there was one. */
-       if (recovery_start != 0 && !found_recovery)
+       if (recovery_start != 0 && !found_recovery) {
+               TDB_LOG((tdb, TDB_DEBUG_ERROR,
+                        "Expected a recovery area at %u\n",
+                        recovery_start));
                goto free;
+       }
 
        free(hashes);
-       tdb_unlockall(tdb);
+       if (locked) {
+               tdb_unlockall_read(tdb);
+       }
        return 0;
 
 free:
        free(hashes);
 unlock:
-       tdb_unlockall(tdb);
+       if (locked) {
+               tdb_unlockall_read(tdb);
+       }
        return -1;
 }
-
-               
-
-       
-
-