yeahuh / rpms / qemu-kvm

Forked from rpms/qemu-kvm 2 years ago
Clone

Blame SOURCES/kvm-virtiofsd-introduce-inode-refcount-to-prevent-use-af.patch

ddf19c
From da6ee5c24397d2ca93dfaf275fdd9dafc922da15 Mon Sep 17 00:00:00 2001
ddf19c
From: "Dr. David Alan Gilbert" <dgilbert@redhat.com>
ddf19c
Date: Mon, 27 Jan 2020 19:02:11 +0100
ddf19c
Subject: [PATCH 100/116] virtiofsd: introduce inode refcount to prevent
ddf19c
 use-after-free
ddf19c
MIME-Version: 1.0
ddf19c
Content-Type: text/plain; charset=UTF-8
ddf19c
Content-Transfer-Encoding: 8bit
ddf19c
ddf19c
RH-Author: Dr. David Alan Gilbert <dgilbert@redhat.com>
ddf19c
Message-id: <20200127190227.40942-97-dgilbert@redhat.com>
ddf19c
Patchwork-id: 93550
ddf19c
O-Subject: [RHEL-AV-8.2 qemu-kvm PATCH 096/112] virtiofsd: introduce inode refcount to prevent use-after-free
ddf19c
Bugzilla: 1694164
ddf19c
RH-Acked-by: Philippe Mathieu-Daudé <philmd@redhat.com>
ddf19c
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
ddf19c
RH-Acked-by: Sergio Lopez Pascual <slp@redhat.com>
ddf19c
ddf19c
From: Stefan Hajnoczi <stefanha@redhat.com>
ddf19c
ddf19c
If thread A is using an inode it must not be deleted by thread B when
ddf19c
processing a FUSE_FORGET request.
ddf19c
ddf19c
The FUSE protocol itself already has a counter called nlookup that is
ddf19c
used in FUSE_FORGET messages.  We cannot trust this counter since the
ddf19c
untrusted client can manipulate it via FUSE_FORGET messages.
ddf19c
ddf19c
Introduce a new refcount to keep inodes alive for the required lifespan.
ddf19c
lo_inode_put() must be called to release a reference.  FUSE's nlookup
ddf19c
counter holds exactly one reference so that the inode stays alive as
ddf19c
long as the client still wants to remember it.
ddf19c
ddf19c
Note that the lo_inode->is_symlink field is moved to avoid creating a
ddf19c
hole in the struct due to struct field alignment.
ddf19c
ddf19c
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
ddf19c
Reviewed-by: Misono Tomohiro <misono.tomohiro@jp.fujitsu.com>
ddf19c
Reviewed-by: Sergio Lopez <slp@redhat.com>
ddf19c
Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
ddf19c
(cherry picked from commit c241aa9457d88c6a0d027f48fadfed131646bce3)
ddf19c
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
ddf19c
---
ddf19c
 tools/virtiofsd/passthrough_ll.c | 169 +++++++++++++++++++++++++++++++++------
ddf19c
 1 file changed, 146 insertions(+), 23 deletions(-)
ddf19c
ddf19c
diff --git a/tools/virtiofsd/passthrough_ll.c b/tools/virtiofsd/passthrough_ll.c
ddf19c
index e3a6d6b..ab16135 100644
ddf19c
--- a/tools/virtiofsd/passthrough_ll.c
ddf19c
+++ b/tools/virtiofsd/passthrough_ll.c
ddf19c
@@ -97,7 +97,13 @@ struct lo_key {
ddf19c
 
ddf19c
 struct lo_inode {
ddf19c
     int fd;
ddf19c
-    bool is_symlink;
ddf19c
+
ddf19c
+    /*
ddf19c
+     * Atomic reference count for this object.  The nlookup field holds a
ddf19c
+     * reference and release it when nlookup reaches 0.
ddf19c
+     */
ddf19c
+    gint refcount;
ddf19c
+
ddf19c
     struct lo_key key;
ddf19c
 
ddf19c
     /*
ddf19c
@@ -116,6 +122,8 @@ struct lo_inode {
ddf19c
     fuse_ino_t fuse_ino;
ddf19c
     pthread_mutex_t plock_mutex;
ddf19c
     GHashTable *posix_locks; /* protected by lo_inode->plock_mutex */
ddf19c
+
ddf19c
+    bool is_symlink;
ddf19c
 };
ddf19c
 
ddf19c
 struct lo_cred {
ddf19c
@@ -471,6 +479,23 @@ static ssize_t lo_add_inode_mapping(fuse_req_t req, struct lo_inode *inode)
ddf19c
     return elem - lo_data(req)->ino_map.elems;
ddf19c
 }
ddf19c
 
ddf19c
+static void lo_inode_put(struct lo_data *lo, struct lo_inode **inodep)
ddf19c
+{
ddf19c
+    struct lo_inode *inode = *inodep;
ddf19c
+
ddf19c
+    if (!inode) {
ddf19c
+        return;
ddf19c
+    }
ddf19c
+
ddf19c
+    *inodep = NULL;
ddf19c
+
ddf19c
+    if (g_atomic_int_dec_and_test(&inode->refcount)) {
ddf19c
+        close(inode->fd);
ddf19c
+        free(inode);
ddf19c
+    }
ddf19c
+}
ddf19c
+
ddf19c
+/* Caller must release refcount using lo_inode_put() */
ddf19c
 static struct lo_inode *lo_inode(fuse_req_t req, fuse_ino_t ino)
ddf19c
 {
ddf19c
     struct lo_data *lo = lo_data(req);
ddf19c
@@ -478,6 +503,9 @@ static struct lo_inode *lo_inode(fuse_req_t req, fuse_ino_t ino)
ddf19c
 
ddf19c
     pthread_mutex_lock(&lo->mutex);
ddf19c
     elem = lo_map_get(&lo->ino_map, ino);
ddf19c
+    if (elem) {
ddf19c
+        g_atomic_int_inc(&elem->inode->refcount);
ddf19c
+    }
ddf19c
     pthread_mutex_unlock(&lo->mutex);
ddf19c
 
ddf19c
     if (!elem) {
ddf19c
@@ -487,10 +515,23 @@ static struct lo_inode *lo_inode(fuse_req_t req, fuse_ino_t ino)
ddf19c
     return elem->inode;
ddf19c
 }
ddf19c
 
ddf19c
+/*
ddf19c
+ * TODO Remove this helper and force callers to hold an inode refcount until
ddf19c
+ * they are done with the fd.  This will be done in a later patch to make
ddf19c
+ * review easier.
ddf19c
+ */
ddf19c
 static int lo_fd(fuse_req_t req, fuse_ino_t ino)
ddf19c
 {
ddf19c
     struct lo_inode *inode = lo_inode(req, ino);
ddf19c
-    return inode ? inode->fd : -1;
ddf19c
+    int fd;
ddf19c
+
ddf19c
+    if (!inode) {
ddf19c
+        return -1;
ddf19c
+    }
ddf19c
+
ddf19c
+    fd = inode->fd;
ddf19c
+    lo_inode_put(lo_data(req), &inode;;
ddf19c
+    return fd;
ddf19c
 }
ddf19c
 
ddf19c
 static void lo_init(void *userdata, struct fuse_conn_info *conn)
ddf19c
@@ -545,6 +586,10 @@ static void lo_getattr(fuse_req_t req, fuse_ino_t ino,
ddf19c
     fuse_reply_attr(req, &buf, lo->timeout);
ddf19c
 }
ddf19c
 
ddf19c
+/*
ddf19c
+ * Increments parent->nlookup and caller must release refcount using
ddf19c
+ * lo_inode_put(&parent).
ddf19c
+ */
ddf19c
 static int lo_parent_and_name(struct lo_data *lo, struct lo_inode *inode,
ddf19c
                               char path[PATH_MAX], struct lo_inode **parent)
ddf19c
 {
ddf19c
@@ -582,6 +627,7 @@ retry:
ddf19c
         p = &lo->root;
ddf19c
         pthread_mutex_lock(&lo->mutex);
ddf19c
         p->nlookup++;
ddf19c
+        g_atomic_int_inc(&p->refcount);
ddf19c
         pthread_mutex_unlock(&lo->mutex);
ddf19c
     } else {
ddf19c
         *last = '\0';
ddf19c
@@ -625,6 +671,7 @@ retry:
ddf19c
 
ddf19c
 fail_unref:
ddf19c
     unref_inode_lolocked(lo, p, 1);
ddf19c
+    lo_inode_put(lo, &p);
ddf19c
 fail:
ddf19c
     if (retries) {
ddf19c
         retries--;
ddf19c
@@ -663,6 +710,7 @@ fallback:
ddf19c
     if (res != -1) {
ddf19c
         res = utimensat(parent->fd, path, tv, AT_SYMLINK_NOFOLLOW);
ddf19c
         unref_inode_lolocked(lo, parent, 1);
ddf19c
+        lo_inode_put(lo, &parent);
ddf19c
     }
ddf19c
 
ddf19c
     return res;
ddf19c
@@ -780,11 +828,13 @@ static void lo_setattr(fuse_req_t req, fuse_ino_t ino, struct stat *attr,
ddf19c
             goto out_err;
ddf19c
         }
ddf19c
     }
ddf19c
+    lo_inode_put(lo, &inode;;
ddf19c
 
ddf19c
     return lo_getattr(req, ino, fi);
ddf19c
 
ddf19c
 out_err:
ddf19c
     saverr = errno;
ddf19c
+    lo_inode_put(lo, &inode;;
ddf19c
     fuse_reply_err(req, saverr);
ddf19c
 }
ddf19c
 
ddf19c
@@ -801,6 +851,7 @@ static struct lo_inode *lo_find(struct lo_data *lo, struct stat *st)
ddf19c
     if (p) {
ddf19c
         assert(p->nlookup > 0);
ddf19c
         p->nlookup++;
ddf19c
+        g_atomic_int_inc(&p->refcount);
ddf19c
     }
ddf19c
     pthread_mutex_unlock(&lo->mutex);
ddf19c
 
ddf19c
@@ -820,6 +871,10 @@ static void posix_locks_value_destroy(gpointer data)
ddf19c
     free(plock);
ddf19c
 }
ddf19c
 
ddf19c
+/*
ddf19c
+ * Increments nlookup and caller must release refcount using
ddf19c
+ * lo_inode_put(&parent).
ddf19c
+ */
ddf19c
 static int lo_do_lookup(fuse_req_t req, fuse_ino_t parent, const char *name,
ddf19c
                         struct fuse_entry_param *e)
ddf19c
 {
ddf19c
@@ -827,7 +882,8 @@ static int lo_do_lookup(fuse_req_t req, fuse_ino_t parent, const char *name,
ddf19c
     int res;
ddf19c
     int saverr;
ddf19c
     struct lo_data *lo = lo_data(req);
ddf19c
-    struct lo_inode *inode, *dir = lo_inode(req, parent);
ddf19c
+    struct lo_inode *inode = NULL;
ddf19c
+    struct lo_inode *dir = lo_inode(req, parent);
ddf19c
 
ddf19c
     /*
ddf19c
      * name_to_handle_at() and open_by_handle_at() can reach here with fuse
ddf19c
@@ -868,6 +924,13 @@ static int lo_do_lookup(fuse_req_t req, fuse_ino_t parent, const char *name,
ddf19c
         }
ddf19c
 
ddf19c
         inode->is_symlink = S_ISLNK(e->attr.st_mode);
ddf19c
+
ddf19c
+        /*
ddf19c
+         * One for the caller and one for nlookup (released in
ddf19c
+         * unref_inode_lolocked())
ddf19c
+         */
ddf19c
+        g_atomic_int_set(&inode->refcount, 2);
ddf19c
+
ddf19c
         inode->nlookup = 1;
ddf19c
         inode->fd = newfd;
ddf19c
         newfd = -1;
ddf19c
@@ -883,6 +946,8 @@ static int lo_do_lookup(fuse_req_t req, fuse_ino_t parent, const char *name,
ddf19c
         pthread_mutex_unlock(&lo->mutex);
ddf19c
     }
ddf19c
     e->ino = inode->fuse_ino;
ddf19c
+    lo_inode_put(lo, &inode;;
ddf19c
+    lo_inode_put(lo, &dir;;
ddf19c
 
ddf19c
     fuse_log(FUSE_LOG_DEBUG, "  %lli/%s -> %lli\n", (unsigned long long)parent,
ddf19c
              name, (unsigned long long)e->ino);
ddf19c
@@ -894,6 +959,8 @@ out_err:
ddf19c
     if (newfd != -1) {
ddf19c
         close(newfd);
ddf19c
     }
ddf19c
+    lo_inode_put(lo, &inode;;
ddf19c
+    lo_inode_put(lo, &dir;;
ddf19c
     return saverr;
ddf19c
 }
ddf19c
 
ddf19c
@@ -991,6 +1058,7 @@ static void lo_mknod_symlink(fuse_req_t req, fuse_ino_t parent,
ddf19c
 {
ddf19c
     int res;
ddf19c
     int saverr;
ddf19c
+    struct lo_data *lo = lo_data(req);
ddf19c
     struct lo_inode *dir;
ddf19c
     struct fuse_entry_param e;
ddf19c
     struct lo_cred old = {};
ddf19c
@@ -1032,9 +1100,11 @@ static void lo_mknod_symlink(fuse_req_t req, fuse_ino_t parent,
ddf19c
              name, (unsigned long long)e.ino);
ddf19c
 
ddf19c
     fuse_reply_entry(req, &e);
ddf19c
+    lo_inode_put(lo, &dir;;
ddf19c
     return;
ddf19c
 
ddf19c
 out:
ddf19c
+    lo_inode_put(lo, &dir;;
ddf19c
     fuse_reply_err(req, saverr);
ddf19c
 }
ddf19c
 
ddf19c
@@ -1085,6 +1155,7 @@ fallback:
ddf19c
     if (res != -1) {
ddf19c
         res = linkat(parent->fd, path, dfd, name, 0);
ddf19c
         unref_inode_lolocked(lo, parent, 1);
ddf19c
+        lo_inode_put(lo, &parent);
ddf19c
     }
ddf19c
 
ddf19c
     return res;
ddf19c
@@ -1095,6 +1166,7 @@ static void lo_link(fuse_req_t req, fuse_ino_t ino, fuse_ino_t parent,
ddf19c
 {
ddf19c
     int res;
ddf19c
     struct lo_data *lo = lo_data(req);
ddf19c
+    struct lo_inode *parent_inode;
ddf19c
     struct lo_inode *inode;
ddf19c
     struct fuse_entry_param e;
ddf19c
     int saverr;
ddf19c
@@ -1104,17 +1176,18 @@ static void lo_link(fuse_req_t req, fuse_ino_t ino, fuse_ino_t parent,
ddf19c
         return;
ddf19c
     }
ddf19c
 
ddf19c
+    parent_inode = lo_inode(req, parent);
ddf19c
     inode = lo_inode(req, ino);
ddf19c
-    if (!inode) {
ddf19c
-        fuse_reply_err(req, EBADF);
ddf19c
-        return;
ddf19c
+    if (!parent_inode || !inode) {
ddf19c
+        errno = EBADF;
ddf19c
+        goto out_err;
ddf19c
     }
ddf19c
 
ddf19c
     memset(&e, 0, sizeof(struct fuse_entry_param));
ddf19c
     e.attr_timeout = lo->timeout;
ddf19c
     e.entry_timeout = lo->timeout;
ddf19c
 
ddf19c
-    res = linkat_empty_nofollow(lo, inode, lo_fd(req, parent), name);
ddf19c
+    res = linkat_empty_nofollow(lo, inode, parent_inode->fd, name);
ddf19c
     if (res == -1) {
ddf19c
         goto out_err;
ddf19c
     }
ddf19c
@@ -1133,13 +1206,18 @@ static void lo_link(fuse_req_t req, fuse_ino_t ino, fuse_ino_t parent,
ddf19c
              name, (unsigned long long)e.ino);
ddf19c
 
ddf19c
     fuse_reply_entry(req, &e);
ddf19c
+    lo_inode_put(lo, &parent_inode);
ddf19c
+    lo_inode_put(lo, &inode;;
ddf19c
     return;
ddf19c
 
ddf19c
 out_err:
ddf19c
     saverr = errno;
ddf19c
+    lo_inode_put(lo, &parent_inode);
ddf19c
+    lo_inode_put(lo, &inode;;
ddf19c
     fuse_reply_err(req, saverr);
ddf19c
 }
ddf19c
 
ddf19c
+/* Increments nlookup and caller must release refcount using lo_inode_put() */
ddf19c
 static struct lo_inode *lookup_name(fuse_req_t req, fuse_ino_t parent,
ddf19c
                                     const char *name)
ddf19c
 {
ddf19c
@@ -1176,6 +1254,7 @@ static void lo_rmdir(fuse_req_t req, fuse_ino_t parent, const char *name)
ddf19c
 
ddf19c
     fuse_reply_err(req, res == -1 ? errno : 0);
ddf19c
     unref_inode_lolocked(lo, inode, 1);
ddf19c
+    lo_inode_put(lo, &inode;;
ddf19c
 }
ddf19c
 
ddf19c
 static void lo_rename(fuse_req_t req, fuse_ino_t parent, const char *name,
ddf19c
@@ -1183,8 +1262,10 @@ static void lo_rename(fuse_req_t req, fuse_ino_t parent, const char *name,
ddf19c
                       unsigned int flags)
ddf19c
 {
ddf19c
     int res;
ddf19c
-    struct lo_inode *oldinode;
ddf19c
-    struct lo_inode *newinode;
ddf19c
+    struct lo_inode *parent_inode;
ddf19c
+    struct lo_inode *newparent_inode;
ddf19c
+    struct lo_inode *oldinode = NULL;
ddf19c
+    struct lo_inode *newinode = NULL;
ddf19c
     struct lo_data *lo = lo_data(req);
ddf19c
 
ddf19c
     if (!is_safe_path_component(name) || !is_safe_path_component(newname)) {
ddf19c
@@ -1192,6 +1273,13 @@ static void lo_rename(fuse_req_t req, fuse_ino_t parent, const char *name,
ddf19c
         return;
ddf19c
     }
ddf19c
 
ddf19c
+    parent_inode = lo_inode(req, parent);
ddf19c
+    newparent_inode = lo_inode(req, newparent);
ddf19c
+    if (!parent_inode || !newparent_inode) {
ddf19c
+        fuse_reply_err(req, EBADF);
ddf19c
+        goto out;
ddf19c
+    }
ddf19c
+
ddf19c
     oldinode = lookup_name(req, parent, name);
ddf19c
     newinode = lookup_name(req, newparent, newname);
ddf19c
 
ddf19c
@@ -1204,8 +1292,8 @@ static void lo_rename(fuse_req_t req, fuse_ino_t parent, const char *name,
ddf19c
 #ifndef SYS_renameat2
ddf19c
         fuse_reply_err(req, EINVAL);
ddf19c
 #else
ddf19c
-        res = syscall(SYS_renameat2, lo_fd(req, parent), name,
ddf19c
-                       lo_fd(req, newparent), newname, flags);
ddf19c
+        res = syscall(SYS_renameat2, parent_inode->fd, name,
ddf19c
+                        newparent_inode->fd, newname, flags);
ddf19c
         if (res == -1 && errno == ENOSYS) {
ddf19c
             fuse_reply_err(req, EINVAL);
ddf19c
         } else {
ddf19c
@@ -1215,12 +1303,16 @@ static void lo_rename(fuse_req_t req, fuse_ino_t parent, const char *name,
ddf19c
         goto out;
ddf19c
     }
ddf19c
 
ddf19c
-    res = renameat(lo_fd(req, parent), name, lo_fd(req, newparent), newname);
ddf19c
+    res = renameat(parent_inode->fd, name, newparent_inode->fd, newname);
ddf19c
 
ddf19c
     fuse_reply_err(req, res == -1 ? errno : 0);
ddf19c
 out:
ddf19c
     unref_inode_lolocked(lo, oldinode, 1);
ddf19c
     unref_inode_lolocked(lo, newinode, 1);
ddf19c
+    lo_inode_put(lo, &oldinode);
ddf19c
+    lo_inode_put(lo, &newinode);
ddf19c
+    lo_inode_put(lo, &parent_inode);
ddf19c
+    lo_inode_put(lo, &newparent_inode);
ddf19c
 }
ddf19c
 
ddf19c
 static void lo_unlink(fuse_req_t req, fuse_ino_t parent, const char *name)
ddf19c
@@ -1244,6 +1336,7 @@ static void lo_unlink(fuse_req_t req, fuse_ino_t parent, const char *name)
ddf19c
 
ddf19c
     fuse_reply_err(req, res == -1 ? errno : 0);
ddf19c
     unref_inode_lolocked(lo, inode, 1);
ddf19c
+    lo_inode_put(lo, &inode;;
ddf19c
 }
ddf19c
 
ddf19c
 static void unref_inode_lolocked(struct lo_data *lo, struct lo_inode *inode,
ddf19c
@@ -1265,8 +1358,9 @@ static void unref_inode_lolocked(struct lo_data *lo, struct lo_inode *inode,
ddf19c
         g_hash_table_destroy(inode->posix_locks);
ddf19c
         pthread_mutex_destroy(&inode->plock_mutex);
ddf19c
         pthread_mutex_unlock(&lo->mutex);
ddf19c
-        close(inode->fd);
ddf19c
-        free(inode);
ddf19c
+
ddf19c
+        /* Drop our refcount from lo_do_lookup() */
ddf19c
+        lo_inode_put(lo, &inode;;
ddf19c
     } else {
ddf19c
         pthread_mutex_unlock(&lo->mutex);
ddf19c
     }
ddf19c
@@ -1280,6 +1374,7 @@ static int unref_all_inodes_cb(gpointer key, gpointer value, gpointer user_data)
ddf19c
     inode->nlookup = 0;
ddf19c
     lo_map_remove(&lo->ino_map, inode->fuse_ino);
ddf19c
     close(inode->fd);
ddf19c
+    lo_inode_put(lo, &inode;; /* Drop our refcount from lo_do_lookup() */
ddf19c
 
ddf19c
     return TRUE;
ddf19c
 }
ddf19c
@@ -1306,6 +1401,7 @@ static void lo_forget_one(fuse_req_t req, fuse_ino_t ino, uint64_t nlookup)
ddf19c
              (unsigned long long)nlookup);
ddf19c
 
ddf19c
     unref_inode_lolocked(lo, inode, nlookup);
ddf19c
+    lo_inode_put(lo, &inode;;
ddf19c
 }
ddf19c
 
ddf19c
 static void lo_forget(fuse_req_t req, fuse_ino_t ino, uint64_t nlookup)
ddf19c
@@ -1537,6 +1633,7 @@ static void lo_do_readdir(fuse_req_t req, fuse_ino_t ino, size_t size,
ddf19c
     err = 0;
ddf19c
 error:
ddf19c
     lo_dirp_put(&d);
ddf19c
+    lo_inode_put(lo, &dinode);
ddf19c
 
ddf19c
     /*
ddf19c
      * If there's an error, we can only signal it if we haven't stored
ddf19c
@@ -1595,6 +1692,7 @@ static void lo_create(fuse_req_t req, fuse_ino_t parent, const char *name,
ddf19c
 {
ddf19c
     int fd;
ddf19c
     struct lo_data *lo = lo_data(req);
ddf19c
+    struct lo_inode *parent_inode;
ddf19c
     struct fuse_entry_param e;
ddf19c
     int err;
ddf19c
     struct lo_cred old = {};
ddf19c
@@ -1607,12 +1705,18 @@ static void lo_create(fuse_req_t req, fuse_ino_t parent, const char *name,
ddf19c
         return;
ddf19c
     }
ddf19c
 
ddf19c
+    parent_inode = lo_inode(req, parent);
ddf19c
+    if (!parent_inode) {
ddf19c
+        fuse_reply_err(req, EBADF);
ddf19c
+        return;
ddf19c
+    }
ddf19c
+
ddf19c
     err = lo_change_cred(req, &old;;
ddf19c
     if (err) {
ddf19c
         goto out;
ddf19c
     }
ddf19c
 
ddf19c
-    fd = openat(lo_fd(req, parent), name, (fi->flags | O_CREAT) & ~O_NOFOLLOW,
ddf19c
+    fd = openat(parent_inode->fd, name, (fi->flags | O_CREAT) & ~O_NOFOLLOW,
ddf19c
                 mode);
ddf19c
     err = fd == -1 ? errno : 0;
ddf19c
     lo_restore_cred(&old;;
ddf19c
@@ -1625,8 +1729,8 @@ static void lo_create(fuse_req_t req, fuse_ino_t parent, const char *name,
ddf19c
         pthread_mutex_unlock(&lo->mutex);
ddf19c
         if (fh == -1) {
ddf19c
             close(fd);
ddf19c
-            fuse_reply_err(req, ENOMEM);
ddf19c
-            return;
ddf19c
+            err = ENOMEM;
ddf19c
+            goto out;
ddf19c
         }
ddf19c
 
ddf19c
         fi->fh = fh;
ddf19c
@@ -1639,6 +1743,8 @@ static void lo_create(fuse_req_t req, fuse_ino_t parent, const char *name,
ddf19c
     }
ddf19c
 
ddf19c
 out:
ddf19c
+    lo_inode_put(lo, &parent_inode);
ddf19c
+
ddf19c
     if (err) {
ddf19c
         fuse_reply_err(req, err);
ddf19c
     } else {
ddf19c
@@ -1712,16 +1818,18 @@ static void lo_getlk(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi,
ddf19c
     plock =
ddf19c
         lookup_create_plock_ctx(lo, inode, fi->lock_owner, lock->l_pid, &ret;;
ddf19c
     if (!plock) {
ddf19c
-        pthread_mutex_unlock(&inode->plock_mutex);
ddf19c
-        fuse_reply_err(req, ret);
ddf19c
-        return;
ddf19c
+        saverr = ret;
ddf19c
+        goto out;
ddf19c
     }
ddf19c
 
ddf19c
     ret = fcntl(plock->fd, F_OFD_GETLK, lock);
ddf19c
     if (ret == -1) {
ddf19c
         saverr = errno;
ddf19c
     }
ddf19c
+
ddf19c
+out:
ddf19c
     pthread_mutex_unlock(&inode->plock_mutex);
ddf19c
+    lo_inode_put(lo, &inode;;
ddf19c
 
ddf19c
     if (saverr) {
ddf19c
         fuse_reply_err(req, saverr);
ddf19c
@@ -1761,9 +1869,8 @@ static void lo_setlk(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi,
ddf19c
         lookup_create_plock_ctx(lo, inode, fi->lock_owner, lock->l_pid, &ret;;
ddf19c
 
ddf19c
     if (!plock) {
ddf19c
-        pthread_mutex_unlock(&inode->plock_mutex);
ddf19c
-        fuse_reply_err(req, ret);
ddf19c
-        return;
ddf19c
+        saverr = ret;
ddf19c
+        goto out;
ddf19c
     }
ddf19c
 
ddf19c
     /* TODO: Is it alright to modify flock? */
ddf19c
@@ -1772,7 +1879,11 @@ static void lo_setlk(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi,
ddf19c
     if (ret == -1) {
ddf19c
         saverr = errno;
ddf19c
     }
ddf19c
+
ddf19c
+out:
ddf19c
     pthread_mutex_unlock(&inode->plock_mutex);
ddf19c
+    lo_inode_put(lo, &inode;;
ddf19c
+
ddf19c
     fuse_reply_err(req, saverr);
ddf19c
 }
ddf19c
 
ddf19c
@@ -1898,6 +2009,7 @@ static void lo_flush(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi)
ddf19c
     pthread_mutex_unlock(&inode->plock_mutex);
ddf19c
 
ddf19c
     res = close(dup(lo_fi_fd(req, fi)));
ddf19c
+    lo_inode_put(lo_data(req), &inode;;
ddf19c
     fuse_reply_err(req, res == -1 ? errno : 0);
ddf19c
 }
ddf19c
 
ddf19c
@@ -2115,11 +2227,14 @@ out_free:
ddf19c
     if (fd >= 0) {
ddf19c
         close(fd);
ddf19c
     }
ddf19c
+
ddf19c
+    lo_inode_put(lo, &inode;;
ddf19c
     return;
ddf19c
 
ddf19c
 out_err:
ddf19c
     saverr = errno;
ddf19c
 out:
ddf19c
+    lo_inode_put(lo, &inode;;
ddf19c
     fuse_reply_err(req, saverr);
ddf19c
     goto out_free;
ddf19c
 }
ddf19c
@@ -2190,11 +2305,14 @@ out_free:
ddf19c
     if (fd >= 0) {
ddf19c
         close(fd);
ddf19c
     }
ddf19c
+
ddf19c
+    lo_inode_put(lo, &inode;;
ddf19c
     return;
ddf19c
 
ddf19c
 out_err:
ddf19c
     saverr = errno;
ddf19c
 out:
ddf19c
+    lo_inode_put(lo, &inode;;
ddf19c
     fuse_reply_err(req, saverr);
ddf19c
     goto out_free;
ddf19c
 }
ddf19c
@@ -2243,6 +2361,8 @@ out:
ddf19c
     if (fd >= 0) {
ddf19c
         close(fd);
ddf19c
     }
ddf19c
+
ddf19c
+    lo_inode_put(lo, &inode;;
ddf19c
     fuse_reply_err(req, saverr);
ddf19c
 }
ddf19c
 
ddf19c
@@ -2289,6 +2409,8 @@ out:
ddf19c
     if (fd >= 0) {
ddf19c
         close(fd);
ddf19c
     }
ddf19c
+
ddf19c
+    lo_inode_put(lo, &inode;;
ddf19c
     fuse_reply_err(req, saverr);
ddf19c
 }
ddf19c
 
ddf19c
@@ -2671,6 +2793,7 @@ static void setup_root(struct lo_data *lo, struct lo_inode *root)
ddf19c
     root->key.ino = stat.st_ino;
ddf19c
     root->key.dev = stat.st_dev;
ddf19c
     root->nlookup = 2;
ddf19c
+    g_atomic_int_set(&root->refcount, 2);
ddf19c
 }
ddf19c
 
ddf19c
 static guint lo_key_hash(gconstpointer key)
ddf19c
-- 
ddf19c
1.8.3.1
ddf19c