Commit 6f7cb26e authored by Rusty Russell's avatar Rusty Russell

tdb2: careful on wrap.

It's much harder to wrap a 64-bit tdb2 than a 32-bit tdb1, but we should still
take care against bugs.

Also, we should *not* cast the length to a size_t when comparing it to
the stat result, in case size_t is 32 bit.
parent a3606bbd
...@@ -497,8 +497,8 @@ static enum TDB_ERROR check_free(struct tdb_context *tdb, ...@@ -497,8 +497,8 @@ static enum TDB_ERROR check_free(struct tdb_context *tdb,
} }
ecode = tdb->tdb2.io->oob(tdb, off ecode = tdb->tdb2.io->oob(tdb, off,
+ frec_len(frec) frec_len(frec)
+ sizeof(struct tdb_used_record), + sizeof(struct tdb_used_record),
false); false);
if (ecode != TDB_SUCCESS) { if (ecode != TDB_SUCCESS) {
......
...@@ -898,7 +898,7 @@ static enum TDB_ERROR tdb_expand(struct tdb_context *tdb, tdb_len_t size) ...@@ -898,7 +898,7 @@ static enum TDB_ERROR tdb_expand(struct tdb_context *tdb, tdb_len_t size)
/* Someone else may have expanded the file, so retry. */ /* Someone else may have expanded the file, so retry. */
old_size = tdb->file->map_size; old_size = tdb->file->map_size;
tdb->tdb2.io->oob(tdb, tdb->file->map_size + 1, true); tdb->tdb2.io->oob(tdb, tdb->file->map_size, 1, true);
if (tdb->file->map_size != old_size) { if (tdb->file->map_size != old_size) {
tdb_unlock_expand(tdb, F_WRLCK); tdb_unlock_expand(tdb, F_WRLCK);
return TDB_SUCCESS; return TDB_SUCCESS;
......
...@@ -81,8 +81,8 @@ void tdb_mmap(struct tdb_context *tdb) ...@@ -81,8 +81,8 @@ void tdb_mmap(struct tdb_context *tdb)
If probe is true, len being too large isn't a failure. If probe is true, len being too large isn't a failure.
*/ */
static enum TDB_ERROR tdb_oob(struct tdb_context *tdb, tdb_off_t len, static enum TDB_ERROR tdb_oob(struct tdb_context *tdb,
bool probe) tdb_off_t off, tdb_len_t len, bool probe)
{ {
struct stat st; struct stat st;
enum TDB_ERROR ecode; enum TDB_ERROR ecode;
...@@ -92,7 +92,16 @@ static enum TDB_ERROR tdb_oob(struct tdb_context *tdb, tdb_off_t len, ...@@ -92,7 +92,16 @@ static enum TDB_ERROR tdb_oob(struct tdb_context *tdb, tdb_off_t len,
|| (tdb->flags & TDB_NOLOCK) || (tdb->flags & TDB_NOLOCK)
|| tdb_has_expansion_lock(tdb)); || tdb_has_expansion_lock(tdb));
if (len <= tdb->file->map_size) if (len + off < len) {
if (probe)
return TDB_SUCCESS;
return tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
"tdb_oob off %llu len %llu wrap\n",
(long long)off, (long long)len);
}
if (len + off <= tdb->file->map_size)
return TDB_SUCCESS; return TDB_SUCCESS;
if (tdb->flags & TDB_INTERNAL) { if (tdb->flags & TDB_INTERNAL) {
if (probe) if (probe)
...@@ -101,7 +110,7 @@ static enum TDB_ERROR tdb_oob(struct tdb_context *tdb, tdb_off_t len, ...@@ -101,7 +110,7 @@ static enum TDB_ERROR tdb_oob(struct tdb_context *tdb, tdb_off_t len,
tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR, tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
"tdb_oob len %lld beyond internal" "tdb_oob len %lld beyond internal"
" malloc size %lld", " malloc size %lld",
(long long)len, (long long)(off + len),
(long long)tdb->file->map_size); (long long)tdb->file->map_size);
return TDB_ERR_IO; return TDB_ERR_IO;
} }
...@@ -120,13 +129,13 @@ static enum TDB_ERROR tdb_oob(struct tdb_context *tdb, tdb_off_t len, ...@@ -120,13 +129,13 @@ static enum TDB_ERROR tdb_oob(struct tdb_context *tdb, tdb_off_t len,
tdb_unlock_expand(tdb, F_RDLCK); tdb_unlock_expand(tdb, F_RDLCK);
if (st.st_size < (size_t)len) { if (st.st_size < off + len) {
if (probe) if (probe)
return TDB_SUCCESS; return TDB_SUCCESS;
tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR, tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
"tdb_oob len %zu beyond eof at %zu", "tdb_oob len %llu beyond eof at %zu",
(size_t)len, st.st_size); (long long)(off + len), st.st_size);
return TDB_ERR_IO; return TDB_ERR_IO;
} }
...@@ -253,7 +262,7 @@ static enum TDB_ERROR tdb_write(struct tdb_context *tdb, tdb_off_t off, ...@@ -253,7 +262,7 @@ static enum TDB_ERROR tdb_write(struct tdb_context *tdb, tdb_off_t off,
"Write to read-only database"); "Write to read-only database");
} }
ecode = tdb->tdb2.io->oob(tdb, off + len, false); ecode = tdb->tdb2.io->oob(tdb, off, len, false);
if (ecode != TDB_SUCCESS) { if (ecode != TDB_SUCCESS) {
return ecode; return ecode;
} }
...@@ -283,7 +292,7 @@ static enum TDB_ERROR tdb_read(struct tdb_context *tdb, tdb_off_t off, ...@@ -283,7 +292,7 @@ static enum TDB_ERROR tdb_read(struct tdb_context *tdb, tdb_off_t off,
{ {
enum TDB_ERROR ecode; enum TDB_ERROR ecode;
ecode = tdb->tdb2.io->oob(tdb, off + len, false); ecode = tdb->tdb2.io->oob(tdb, off, len, false);
if (ecode != TDB_SUCCESS) { if (ecode != TDB_SUCCESS) {
return ecode; return ecode;
} }
...@@ -574,7 +583,7 @@ static void *tdb_direct(struct tdb_context *tdb, tdb_off_t off, size_t len, ...@@ -574,7 +583,7 @@ static void *tdb_direct(struct tdb_context *tdb, tdb_off_t off, size_t len,
if (unlikely(!tdb->file->map_ptr)) if (unlikely(!tdb->file->map_ptr))
return NULL; return NULL;
ecode = tdb_oob(tdb, off + len, false); ecode = tdb_oob(tdb, off, len, false);
if (unlikely(ecode != TDB_SUCCESS)) if (unlikely(ecode != TDB_SUCCESS))
return TDB_ERR_PTR(ecode); return TDB_ERR_PTR(ecode);
return (char *)tdb->file->map_ptr + off; return (char *)tdb->file->map_ptr + off;
......
...@@ -747,7 +747,7 @@ finished: ...@@ -747,7 +747,7 @@ finished:
if (tdb->flags & TDB_VERSION1) { if (tdb->flags & TDB_VERSION1) {
ecode = tdb1_probe_length(tdb); ecode = tdb1_probe_length(tdb);
} else { } else {
ecode = tdb->tdb2.io->oob(tdb, tdb->file->map_size + 1, true); ecode = tdb->tdb2.io->oob(tdb, tdb->file->map_size, 1, true);
} }
if (unlikely(ecode != TDB_SUCCESS)) if (unlikely(ecode != TDB_SUCCESS))
goto fail; goto fail;
......
...@@ -347,7 +347,7 @@ struct tdb_methods { ...@@ -347,7 +347,7 @@ struct tdb_methods {
tdb_len_t); tdb_len_t);
enum TDB_ERROR (*twrite)(struct tdb_context *, tdb_off_t, const void *, enum TDB_ERROR (*twrite)(struct tdb_context *, tdb_off_t, const void *,
tdb_len_t); tdb_len_t);
enum TDB_ERROR (*oob)(struct tdb_context *, tdb_off_t, bool); enum TDB_ERROR (*oob)(struct tdb_context *, tdb_off_t, tdb_len_t, bool);
enum TDB_ERROR (*expand_file)(struct tdb_context *, tdb_len_t); enum TDB_ERROR (*expand_file)(struct tdb_context *, tdb_len_t);
void *(*direct)(struct tdb_context *, tdb_off_t, size_t, bool); void *(*direct)(struct tdb_context *, tdb_off_t, size_t, bool);
}; };
......
...@@ -345,16 +345,16 @@ static void transaction_write_existing(struct tdb_context *tdb, tdb_off_t off, ...@@ -345,16 +345,16 @@ static void transaction_write_existing(struct tdb_context *tdb, tdb_off_t off,
/* /*
out of bounds check during a transaction out of bounds check during a transaction
*/ */
static enum TDB_ERROR transaction_oob(struct tdb_context *tdb, tdb_off_t len, static enum TDB_ERROR transaction_oob(struct tdb_context *tdb,
bool probe) tdb_off_t off, tdb_len_t len, bool probe)
{ {
if (len <= tdb->file->map_size || probe) { if ((off + len >= off && off + len <= tdb->file->map_size) || probe) {
return TDB_SUCCESS; return TDB_SUCCESS;
} }
tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR, tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
"tdb_oob len %lld beyond transaction size %lld", "tdb_oob len %lld beyond transaction size %lld",
(long long)len, (long long)(off + len),
(long long)tdb->file->map_size); (long long)tdb->file->map_size);
return TDB_ERR_IO; return TDB_ERR_IO;
} }
...@@ -601,7 +601,7 @@ enum TDB_ERROR tdb_transaction_start(struct tdb_context *tdb) ...@@ -601,7 +601,7 @@ enum TDB_ERROR tdb_transaction_start(struct tdb_context *tdb)
/* make sure we know about any file expansions already done by /* make sure we know about any file expansions already done by
anyone else */ anyone else */
tdb->tdb2.io->oob(tdb, tdb->file->map_size + 1, true); tdb->tdb2.io->oob(tdb, tdb->file->map_size, 1, true);
tdb->tdb2.transaction->old_map_size = tdb->file->map_size; tdb->tdb2.transaction->old_map_size = tdb->file->map_size;
/* finally hook the io methods, replacing them with /* finally hook the io methods, replacing them with
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment