From 21366649410c29904a463b57e7d8688ce6e11381 Mon Sep 17 00:00:00 2001 From: Andrew Bartlett Date: Fri, 4 May 2018 22:22:26 +1200 Subject: [PATCH] ldb_tdb: Allow use of a TDB for ldb_tdb after as fork() Otherwise we rely on the caller doing tdb_reopen_all() which should not be their job. Signed-off-by: Andrew Bartlett Reviewed-by: Garming Sam --- lib/ldb/ldb_tdb/ldb_tdb_wrap.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/lib/ldb/ldb_tdb/ldb_tdb_wrap.c b/lib/ldb/ldb_tdb/ldb_tdb_wrap.c index eb168098a75..bc702a22584 100644 --- a/lib/ldb/ldb_tdb/ldb_tdb_wrap.c +++ b/lib/ldb/ldb_tdb/ldb_tdb_wrap.c @@ -74,6 +74,7 @@ struct ltdb_wrap { struct tdb_context *tdb; dev_t device; ino_t inode; + pid_t pid; }; static struct ltdb_wrap *tdb_list; @@ -105,9 +106,25 @@ struct tdb_context *ltdb_wrap_open(TALLOC_CTX *mem_ctx, if (stat(path, &st) == 0) { for (w=tdb_list;w;w=w->next) { if (st.st_dev == w->device && st.st_ino == w->inode) { + pid_t pid = getpid(); + int ret; if (!talloc_reference(mem_ctx, w)) { return NULL; } + if (w->pid != pid) { + ret = tdb_reopen(w->tdb); + if (ret != 0) { + /* + * Avoid use-after-free: + * on fail the TDB + * is closed! + */ + DLIST_REMOVE(tdb_list, + w); + return NULL; + } + w->pid = pid; + } return w->tdb; } } @@ -135,6 +152,7 @@ struct tdb_context *ltdb_wrap_open(TALLOC_CTX *mem_ctx, w->device = st.st_dev; w->inode = st.st_ino; + w->pid = getpid(); talloc_set_destructor(w, ltdb_wrap_destructor); -- 2.34.1