From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 67ADC45BC0; Thu, 24 Oct 2024 11:44:28 +0200 (CEST) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id CE6AE43405; Thu, 24 Oct 2024 11:44:20 +0200 (CEST) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by mails.dpdk.org (Postfix) with ESMTP id 95204433E9 for ; Thu, 24 Oct 2024 11:44:16 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1729763056; 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=gsOrHrY70T4YayD/JsrCWaE2g4QfhuI2hBnk3ZDSRYs=; b=McTZSS9ndiqXMSKfrpmOHFZF52YQrK3HvKfJR/uwfFdUJZgTaYHOrpHIPBlg/AxB+GU4bu SIB62xj/NqGvuzJkZeO0Pje5hzhzasoAsJ769IGI9nshfJ2JRzHnoNSRSedUp7JiU/2cTM JAtlD5k/77HttO3IvhBrSPOKFTbjHp8= Received: from mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-202-6NaJnor6P9On6SzYehDk_g-1; Thu, 24 Oct 2024 05:44:15 -0400 X-MC-Unique: 6NaJnor6P9On6SzYehDk_g-1 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 1583C1955D4E; Thu, 24 Oct 2024 09:44:14 +0000 (UTC) Received: from max-p1.redhat.com (unknown [10.39.208.26]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id A30543000198; Thu, 24 Oct 2024 09:44:12 +0000 (UTC) From: Maxime Coquelin To: dev@dpdk.org, david.marchand@redhat.com, chenbox@nvidia.com Cc: Maxime Coquelin Subject: [PATCH v3 2/6] vhost: fix possible TOCTOU in VDUSE dev creation Date: Thu, 24 Oct 2024 11:44:02 +0200 Message-ID: <20241024094406.3826637-3-maxime.coquelin@redhat.com> In-Reply-To: <20241024094406.3826637-1-maxime.coquelin@redhat.com> References: <20241024094406.3826637-1-maxime.coquelin@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org This patch fixes a possible TOCTOU on the VDUSE device chardev opening at device creation time. Coverity issue: 445526 Fixes: da79cc7fda76 ("vhost: add reconnection support to VDUSE") Signed-off-by: Maxime Coquelin Reviewed-by: David Marchand --- lib/vhost/vduse.c | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/lib/vhost/vduse.c b/lib/vhost/vduse.c index 4bb309e441..a98b33dddf 100644 --- a/lib/vhost/vduse.c +++ b/lib/vhost/vduse.c @@ -546,7 +546,8 @@ vduse_device_create(const char *path, bool compliant_ol_flags) else total_queues += 1; /* Includes ctrl queue */ - if (access(path, F_OK) == 0) { + dev_fd = open(path, O_RDWR); + if (dev_fd >= 0) { VHOST_CONFIG_LOG(name, INFO, "Device already exists, reconnecting..."); reconnect = true; @@ -559,7 +560,7 @@ vduse_device_create(const char *path, bool compliant_ol_flags) VHOST_CONFIG_LOG(name, ERR, "Failed to open reconnect file %s (%s)", reconnect_file, strerror(errno)); ret = -1; - goto out_ctrl_close; + goto out_dev_close; } reconnect_log = mmap(NULL, sizeof(*reconnect_log), PROT_READ | PROT_WRITE, @@ -569,7 +570,7 @@ vduse_device_create(const char *path, bool compliant_ol_flags) VHOST_CONFIG_LOG(name, ERR, "Failed to mmap reconnect file %s (%s)", reconnect_file, strerror(errno)); ret = -1; - goto out_ctrl_close; + goto out_dev_close; } if (reconnect_log->version != VHOST_RECONNECT_VERSION) { @@ -593,7 +594,7 @@ vduse_device_create(const char *path, bool compliant_ol_flags) ret = -1; goto out_log_unmap; } - } else { + } else if (errno == ENOENT) { struct vduse_dev_config *dev_config; reco_fd = open(reconnect_file, O_CREAT | O_EXCL | O_RDWR, 0600); @@ -660,34 +661,39 @@ vduse_device_create(const char *path, bool compliant_ol_flags) memcpy(&reconnect_log->config, &vnet_config, sizeof(vnet_config)); reconnect_log->nr_vrings = total_queues; - } - dev_fd = open(path, O_RDWR); - if (dev_fd < 0) { + dev_fd = open(path, O_RDWR); + if (dev_fd < 0) { + VHOST_CONFIG_LOG(name, ERR, "Failed to open newly created device %s: %s", + path, strerror(errno)); + ret = -1; + goto out_log_unmap; + } + } else { VHOST_CONFIG_LOG(name, ERR, "Failed to open device %s: %s", path, strerror(errno)); ret = -1; - goto out_dev_close; + goto out_ctrl_close; } ret = fcntl(dev_fd, F_SETFL, O_NONBLOCK); if (ret < 0) { VHOST_CONFIG_LOG(name, ERR, "Failed to set chardev as non-blocking: %s", strerror(errno)); - goto out_dev_close; + goto out_log_unmap; } vid = vhost_new_device(&vduse_backend_ops); if (vid < 0) { VHOST_CONFIG_LOG(name, ERR, "Failed to create new Vhost device"); ret = -1; - goto out_dev_close; + goto out_log_unmap; } dev = get_device(vid); if (!dev) { ret = -1; - goto out_dev_close; + goto out_dev_destroy; } strncpy(dev->ifname, path, IF_NAME_SZ - 1); @@ -766,13 +772,13 @@ vduse_device_create(const char *path, bool compliant_ol_flags) out_dev_destroy: vhost_destroy_device(vid); +out_log_unmap: + if (reconnect_log != MAP_FAILED) + munmap(reconnect_log, sizeof(*reconnect_log)); out_dev_close: if (dev_fd >= 0) close(dev_fd); ioctl(control_fd, VDUSE_DESTROY_DEV, name); -out_log_unmap: - if (reconnect_log != MAP_FAILED) - munmap(reconnect_log, sizeof(*reconnect_log)); out_ctrl_close: close(control_fd); -- 2.46.2