tdb: be more careful on 4G files.
[mat/samba.git] / lib / tdb / common / io.c
index cc52bcfd69f5b21c539f52b32172bd1a1a6ac119..a2db3bf4bd81d45f40c42bc6c51c5f2fb31cc159 100644 (file)
@@ -6,11 +6,11 @@
    Copyright (C) Andrew Tridgell              1999-2005
    Copyright (C) Paul `Rusty' Russell             2000
    Copyright (C) Jeremy Allison                           2000-2003
-   
+
      ** 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
 /* check for an out of bounds access - if it is out of bounds then
    see if the database has been expanded by someone else and expand
    if necessary 
-   note that "len" is the minimum length needed for the db
 */
-static int tdb_oob(struct tdb_context *tdb, tdb_off_t len, int probe)
+static int tdb_oob(struct tdb_context *tdb, tdb_off_t off, tdb_len_t len,
+                  int probe)
 {
        struct stat st;
-       if (len <= tdb->map_size)
+       if (len + off < len) {
+               if (!probe) {
+                       /* Ensure ecode is set for log fn. */
+                       tdb->ecode = TDB_ERR_IO;
+                       TDB_LOG((tdb, TDB_DEBUG_FATAL,"tdb_oob off %d len %d wrap\n",
+                                (int)off, (int)len));
+               }
+               return -1;
+       }
+
+       if (off + len <= tdb->map_size)
                return 0;
        if (tdb->flags & TDB_INTERNAL) {
                if (!probe) {
                        /* Ensure ecode is set for log fn. */
                        tdb->ecode = TDB_ERR_IO;
-                       TDB_LOG((tdb, TDB_DEBUG_FATAL,"tdb_oob len %d beyond internal malloc size %d\n",
-                                (int)len, (int)tdb->map_size));
+                       TDB_LOG((tdb, TDB_DEBUG_FATAL,"tdb_oob len %u beyond internal malloc size %u\n",
+                                (int)(off + len), (int)tdb->map_size));
                }
                return -1;
        }
@@ -53,16 +63,25 @@ static int tdb_oob(struct tdb_context *tdb, tdb_off_t len, int probe)
                return -1;
        }
 
-       if (st.st_size < (size_t)len) {
+       if (st.st_size < (size_t)off + len) {
                if (!probe) {
                        /* Ensure ecode is set for log fn. */
                        tdb->ecode = TDB_ERR_IO;
-                       TDB_LOG((tdb, TDB_DEBUG_FATAL,"tdb_oob len %d beyond eof at %d\n",
-                                (int)len, (int)st.st_size));
+                       TDB_LOG((tdb, TDB_DEBUG_FATAL,"tdb_oob len %u beyond eof at %u\n",
+                                (int)(off + len), (int)st.st_size));
                }
                return -1;
        }
 
+       /* Beware >4G files! */
+       if ((tdb_off_t)st.st_size != st.st_size) {
+               /* Ensure ecode is set for log fn. */
+               tdb->ecode = TDB_ERR_IO;
+               TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_oob len %llu too large!\n",
+                        (long long)st.st_size));
+               return -1;
+       }
+
        /* Unmap, update size, remap */
        if (tdb_munmap(tdb) == -1) {
                tdb->ecode = TDB_ERR_IO;
@@ -86,7 +105,7 @@ static int tdb_write(struct tdb_context *tdb, tdb_off_t off,
                return -1;
        }
 
-       if (tdb->methods->tdb_oob(tdb, off + len, 0) != 0)
+       if (tdb->methods->tdb_oob(tdb, off, len, 0) != 0)
                return -1;
 
        if (tdb->map_ptr) {
@@ -134,7 +153,7 @@ void *tdb_convert(void *buf, uint32_t size)
 static int tdb_read(struct tdb_context *tdb, tdb_off_t off, void *buf, 
                    tdb_len_t len, int cv)
 {
-       if (tdb->methods->tdb_oob(tdb, off + len, 0) != 0) {
+       if (tdb->methods->tdb_oob(tdb, off, len, 0) != 0) {
                return -1;
        }
 
@@ -298,8 +317,8 @@ static int tdb_expand_file(struct tdb_context *tdb, tdb_off_t size, tdb_off_t ad
    file and doing the mmap again if necessary */
 int tdb_expand(struct tdb_context *tdb, tdb_off_t size)
 {
-       struct list_struct rec;
-       tdb_off_t offset, new_size;     
+       struct tdb_record rec;
+       tdb_off_t offset, new_size, top_size, map_size;
 
        if (tdb_lock(tdb, -1, F_WRLCK) == -1) {
                TDB_LOG((tdb, TDB_DEBUG_ERROR, "lock failed in tdb_expand\n"));
@@ -307,12 +326,27 @@ int tdb_expand(struct tdb_context *tdb, tdb_off_t size)
        }
 
        /* must know about any previous expansions by another process */
-       tdb->methods->tdb_oob(tdb, tdb->map_size + 1, 1);
+       tdb->methods->tdb_oob(tdb, tdb->map_size, 1, 1);
+
+       /* limit size in order to avoid using up huge amounts of memory for
+        * in memory tdbs if an oddball huge record creeps in */
+       if (size > 100 * 1024) {
+               top_size = tdb->map_size + size * 2;
+       } else {
+               top_size = tdb->map_size + size * 100;
+       }
+
+       /* always make room for at least top_size more records, and at
+          least 25% more space. if the DB is smaller than 100MiB,
+          otherwise grow it by 10% only. */
+       if (tdb->map_size > 100 * 1024 * 1024) {
+               map_size = tdb->map_size * 1.10;
+       } else {
+               map_size = tdb->map_size * 1.25;
+       }
 
-       /* always make room for at least 100 more records, and at
-           least 25% more space. Round the database up to a multiple
-           of the page size */
-       new_size = MAX(tdb->map_size + size*100, tdb->map_size * 1.25);
+       /* Round the database up to a multiple of the page size */
+       new_size = MAX(top_size, map_size);
        size = TDB_ALIGN(new_size, tdb->page_size) - tdb->map_size;
 
        if (!(tdb->flags & TDB_INTERNAL))
@@ -419,7 +453,7 @@ int tdb_parse_data(struct tdb_context *tdb, TDB_DATA key,
                 * Optimize by avoiding the malloc/memcpy/free, point the
                 * parser directly at the mmap area.
                 */
-               if (tdb->methods->tdb_oob(tdb, offset+len, 0) != 0) {
+               if (tdb->methods->tdb_oob(tdb, offsetlen, 0) != 0) {
                        return -1;
                }
                data.dptr = offset + (unsigned char *)tdb->map_ptr;
@@ -436,7 +470,7 @@ int tdb_parse_data(struct tdb_context *tdb, TDB_DATA key,
 }
 
 /* read/write a record */
-int tdb_rec_read(struct tdb_context *tdb, tdb_off_t offset, struct list_struct *rec)
+int tdb_rec_read(struct tdb_context *tdb, tdb_off_t offset, struct tdb_record *rec)
 {
        if (tdb->methods->tdb_read(tdb, offset, rec, sizeof(*rec),DOCONV()) == -1)
                return -1;
@@ -446,12 +480,12 @@ int tdb_rec_read(struct tdb_context *tdb, tdb_off_t offset, struct list_struct *
                TDB_LOG((tdb, TDB_DEBUG_FATAL,"tdb_rec_read bad magic 0x%x at offset=%d\n", rec->magic, offset));
                return -1;
        }
-       return tdb->methods->tdb_oob(tdb, rec->next+sizeof(*rec), 0);
+       return tdb->methods->tdb_oob(tdb, rec->nextsizeof(*rec), 0);
 }
 
-int tdb_rec_write(struct tdb_context *tdb, tdb_off_t offset, struct list_struct *rec)
+int tdb_rec_write(struct tdb_context *tdb, tdb_off_t offset, struct tdb_record *rec)
 {
-       struct list_struct r = *rec;
+       struct tdb_record r = *rec;
        return tdb->methods->tdb_write(tdb, offset, CONVERT(r), sizeof(r));
 }
 
@@ -461,7 +495,6 @@ static const struct tdb_methods io_methods = {
        tdb_next_hash_chain,
        tdb_oob,
        tdb_expand_file,
-       tdb_brlock
 };
 
 /*