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

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