ldb_tdb: Allow use of a TDB for ldb_tdb after as fork()
authorAndrew Bartlett <abartlet@samba.org>
Fri, 4 May 2018 10:22:26 +0000 (22:22 +1200)
committerAndrew Bartlett <abartlet@samba.org>
Wed, 9 May 2018 02:29:48 +0000 (04:29 +0200)
Otherwise we rely on the caller doing tdb_reopen_all() which should
not be their job.

Signed-off-by: Andrew Bartlett <abartlet@samba.org>
Reviewed-by: Garming Sam <garming@catalyst.net.nz>
lib/ldb/ldb_tdb/ldb_tdb_wrap.c

index eb168098a75a021620a553b1ee360bc7572c3fc1..bc702a22584101c46114450d1ae808f17f4a2350 100644 (file)
@@ -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);