Blame 0102-virtiofsd-introduce-inode-refcount-to-prevent-use-af.patch

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