From patchwork Tue Jan 21 12:24:14 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: "Dr. David Alan Gilbert" X-Patchwork-Id: 1226579 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=KnFUal77; dkim-atps=neutral Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4829K33kXyz9sP3 for ; Wed, 22 Jan 2020 01:04:55 +1100 (AEDT) Received: from localhost ([::1]:54570 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1itu9E-0005zJ-HG for incoming@patchwork.ozlabs.org; Tue, 21 Jan 2020 09:04:52 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:52716) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1itshR-00038s-6q for qemu-devel@nongnu.org; Tue, 21 Jan 2020 07:32:09 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1itshN-0004je-3f for qemu-devel@nongnu.org; Tue, 21 Jan 2020 07:32:04 -0500 Received: from us-smtp-delivery-1.mimecast.com ([207.211.31.120]:48763 helo=us-smtp-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1itshL-0004ip-VQ for qemu-devel@nongnu.org; Tue, 21 Jan 2020 07:32:01 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1579609918; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ihRNPrnuZU8lVi/S//EW9qsyv7mGeMFrzUihizQGKS8=; b=KnFUal77+jf80EzqT0DQHqg8s0VMZIu8MKXU3d/yOtjkJQ9NOBepeJfEnN8YXasf1z1gO4 LCymoFZ6hgtQeUW0J/8cAM5xkcS92ozFLgtzs/QbMcQ8zvnGQUNISo8j3TBj6k1qNqxfuG SFlNKJ1lCR0fnxSfBZD9JCH3lA+rJyM= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-191-0-Csdl8NOzaJwTTJqZ8oHw-1; Tue, 21 Jan 2020 07:31:57 -0500 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 783C2800D48; Tue, 21 Jan 2020 12:31:56 +0000 (UTC) Received: from dgilbert-t580.localhost (unknown [10.36.118.32]) by smtp.corp.redhat.com (Postfix) with ESMTP id C019D60BE0; Tue, 21 Jan 2020 12:31:54 +0000 (UTC) From: "Dr. David Alan Gilbert (git)" To: qemu-devel@nongnu.org, stefanha@redhat.com, vgoyal@redhat.com, berrange@redhat.com, slp@redhat.com, philmd@redhat.com Subject: [PATCH v2 090/109] virtiofsd: prevent races with lo_dirp_put() Date: Tue, 21 Jan 2020 12:24:14 +0000 Message-Id: <20200121122433.50803-91-dgilbert@redhat.com> In-Reply-To: <20200121122433.50803-1-dgilbert@redhat.com> References: <20200121122433.50803-1-dgilbert@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-MC-Unique: 0-Csdl8NOzaJwTTJqZ8oHw-1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 207.211.31.120 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: m.mizuma@jp.fujitsu.com, misono.tomohiro@jp.fujitsu.com Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" From: Stefan Hajnoczi Introduce lo_dirp_put() so that FUSE_RELEASEDIR does not cause use-after-free races with other threads that are accessing lo_dirp. Also make lo_releasedir() atomic to prevent FUSE_RELEASEDIR racing with itself. This prevents double-frees. Signed-off-by: Stefan Hajnoczi Reviewed-by: Philippe Mathieu-Daudé --- tools/virtiofsd/passthrough_ll.c | 41 +++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 6 deletions(-) diff --git a/tools/virtiofsd/passthrough_ll.c b/tools/virtiofsd/passthrough_ll.c index c52f7c2c32..e40268fce5 100644 --- a/tools/virtiofsd/passthrough_ll.c +++ b/tools/virtiofsd/passthrough_ll.c @@ -1284,11 +1284,28 @@ static void lo_readlink(fuse_req_t req, fuse_ino_t ino) } struct lo_dirp { + gint refcount; DIR *dp; struct dirent *entry; off_t offset; }; +static void lo_dirp_put(struct lo_dirp **dp) +{ + struct lo_dirp *d = *dp; + + if (!d) { + return; + } + *dp = NULL; + + if (g_atomic_int_dec_and_test(&d->refcount)) { + closedir(d->dp); + free(d); + } +} + +/* Call lo_dirp_put() on the return value when no longer needed */ static struct lo_dirp *lo_dirp(fuse_req_t req, struct fuse_file_info *fi) { struct lo_data *lo = lo_data(req); @@ -1296,6 +1313,9 @@ static struct lo_dirp *lo_dirp(fuse_req_t req, struct fuse_file_info *fi) pthread_mutex_lock(&lo->mutex); elem = lo_map_get(&lo->dirp_map, fi->fh); + if (elem) { + g_atomic_int_inc(&elem->dirp->refcount); + } pthread_mutex_unlock(&lo->mutex); if (!elem) { return NULL; @@ -1331,6 +1351,7 @@ static void lo_opendir(fuse_req_t req, fuse_ino_t ino, d->offset = 0; d->entry = NULL; + g_atomic_int_set(&d->refcount, 1); /* paired with lo_releasedir() */ pthread_mutex_lock(&lo->mutex); fh = lo_add_dirp_mapping(req, d); pthread_mutex_unlock(&lo->mutex); @@ -1364,7 +1385,7 @@ static void lo_do_readdir(fuse_req_t req, fuse_ino_t ino, size_t size, off_t offset, struct fuse_file_info *fi, int plus) { struct lo_data *lo = lo_data(req); - struct lo_dirp *d; + struct lo_dirp *d = NULL; struct lo_inode *dinode; char *buf = NULL; char *p; @@ -1454,6 +1475,8 @@ static void lo_do_readdir(fuse_req_t req, fuse_ino_t ino, size_t size, err = 0; error: + lo_dirp_put(&d); + /* * If there's an error, we can only signal it if we haven't stored * any entries yet - otherwise we'd end up with wrong lookup @@ -1484,22 +1507,25 @@ static void lo_releasedir(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi) { struct lo_data *lo = lo_data(req); + struct lo_map_elem *elem; struct lo_dirp *d; (void)ino; - d = lo_dirp(req, fi); - if (!d) { + pthread_mutex_lock(&lo->mutex); + elem = lo_map_get(&lo->dirp_map, fi->fh); + if (!elem) { + pthread_mutex_unlock(&lo->mutex); fuse_reply_err(req, EBADF); return; } - pthread_mutex_lock(&lo->mutex); + d = elem->dirp; lo_map_remove(&lo->dirp_map, fi->fh); pthread_mutex_unlock(&lo->mutex); - closedir(d->dp); - free(d); + lo_dirp_put(&d); /* paired with lo_opendir() */ + fuse_reply_err(req, 0); } @@ -1710,6 +1736,9 @@ static void lo_fsyncdir(fuse_req_t req, fuse_ino_t ino, int datasync, } else { res = fsync(fd); } + + lo_dirp_put(&d); + fuse_reply_err(req, res == -1 ? errno : 0); }