From nobody Sun May 19 00:42:44 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 170.10.129.124 as permitted sender) client-ip=170.10.129.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1670518798; cv=none; d=zohomail.com; s=zohoarc; b=Ag3MM9FF6lr/8hP4b0e2HcLwmNqIoyX0modrCfx3DwyC1ztH63Y89cQMfHBQwtpSSTmf/PmCBT6QQwNNVtIGOcY/yEvkP+lWURnLcB5iYjfizUmTZPzTKZ+1ACaNGPcBihoanf8G0OsEaBsVRFrkrmEPQ0Dxp3n+45c/iHctiv4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1670518798; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=avbUR3J/2ZUHGONOgM4BcitHGJGcj3ICQhrYZ/50QE4=; b=hh1pU23vJkt8ySEsVcYdp1ddopOWtEjutIMJHsHtHNckipmtRbSVLO+FsOpdtmL0t9SeNQJbccNdVkXMCY3Gj/oHA3n+g9dyUlEhqzqIK8G7tSJFH2QWFvWjd4ilQRiint2oll4yyxX4obEdYAMTWO5fwb8IriRTI/WdZa959yE= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by mx.zohomail.com with SMTPS id 1670518798739494.25518716883767; Thu, 8 Dec 2022 08:59:58 -0800 (PST) Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-147-LCX4mBeCNuSbkdCSvRhtnA-1; Thu, 08 Dec 2022 11:59:52 -0500 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id C252D38164D9; Thu, 8 Dec 2022 16:59:48 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com [10.30.29.100]) by smtp.corp.redhat.com (Postfix) with ESMTP id AD23E492B04; Thu, 8 Dec 2022 16:59:48 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (localhost [IPv6:::1]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id C06FA194704B; Thu, 8 Dec 2022 16:59:47 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id 4FA461946A41 for ; Thu, 8 Dec 2022 16:59:46 +0000 (UTC) Received: by smtp.corp.redhat.com (Postfix) id 24ABB42222; Thu, 8 Dec 2022 16:59:46 +0000 (UTC) Received: from maggie.redhat.com (unknown [10.43.2.39]) by smtp.corp.redhat.com (Postfix) with ESMTP id BCFCA17582 for ; Thu, 8 Dec 2022 16:59:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1670518797; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=avbUR3J/2ZUHGONOgM4BcitHGJGcj3ICQhrYZ/50QE4=; b=basP3FakjxHU6Dhy7OtuGe4dB/mXBAlw73epX/r+ln1A62FFEPLMiGA8HGckrGXnWEiI6R pH+wzVlSVQSP8xebhPa9Uo9e6iSkVDm+1w1Hx/eFJGDDWM48dhqsUZUDh+96bwBHt2lmir 0ikkhM/eX7AZFG4R7Oxc/69b0atnggI= X-MC-Unique: LCX4mBeCNuSbkdCSvRhtnA-1 X-Original-To: libvir-list@listman.corp.redhat.com From: Michal Privoznik To: libvir-list@redhat.com Subject: [PATCH v2 1/2] virnetdev: Make virNetDevGenerateName() return 1 if no name was generated Date: Thu, 8 Dec 2022 17:59:42 +0100 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.5 X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libvir-list-bounces@redhat.com Sender: "libvir-list" X-Scanned-By: MIMEDefang 3.1 on 10.11.54.10 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1670518799844100003 Content-Type: text/plain; charset="utf-8"; x-default="true" A caller might be interested in the case when @ifname was already set and it wasn't a template. In such case the virNetDevGenerateName() does not touch the @ifname at all and returns 0 to indicate success. Make it return 1 to distinguish this case from the other case, in which a new name was generated. Signed-off-by: Michal Privoznik Reviewed-by: Laine Stump --- src/util/virnetdev.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/util/virnetdev.c b/src/util/virnetdev.c index 66cfc5d781..82dbb486f2 100644 --- a/src/util/virnetdev.c +++ b/src/util/virnetdev.c @@ -3595,7 +3595,9 @@ virNetDevReserveName(const char *name) * Note: if string pointed by @ifname is NOT a template or NULL, leave * it unchanged and return it directly. * - * Returns 0 on success, -1 on failure. + * Returns: 1 if @ifname already contains a valid name, + * 0 on success (@ifname was generated), + * -1 on failure. */ int virNetDevGenerateName(char **ifname, virNetDevGenNameType type) @@ -3609,7 +3611,7 @@ virNetDevGenerateName(char **ifname, virNetDevGenName= Type type) if (*ifname && (strchr(*ifname, '%') !=3D strrchr(*ifname, '%') || strstr(*ifname, "%d") =3D=3D NULL)) { - return 0; + return 1; } =20 if (maxIDd <=3D (double)INT_MAX) --=20 2.37.4 From nobody Sun May 19 00:42:44 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) client-ip=170.10.133.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1670518802; cv=none; d=zohomail.com; s=zohoarc; b=Wk21dYJ6xJo4mAe6hRHP1U71/YCEuXuqdgXEvlQiUX+uRuXuCLDOn5WRIaqe+jf26ZtWmetxMCVONi/focv56MSuaTIR1f/WPFUzKvJrJeyiCZXOsOs9hkvPKmLCzNiLQH0gdM61o0BmmMXcWhZCnM8rM81rBk5aYroiJQEHQX4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1670518802; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=TMfv6sxKH2uFyxM2LENfjEHGSTS7O1RH8adbWUuBzCc=; b=QzzYy6xs0GB+0xY4J+0KUSOshkO3RlfrFsWw01OHT1v+HYeZc24cetG8ea+qel+usF0MoV8iFEMyPZNSFZmMGOW9LWS0ZeDQnkYKNjrVUzvNd08mIBaaF8vP8V3hrJ1CQaFbJu2d9OuiGIZrM7JSZr20omi9NkOI3b5jSeITtxA= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by mx.zohomail.com with SMTPS id 1670518802263445.53507138008206; Thu, 8 Dec 2022 09:00:02 -0800 (PST) Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-524-TPruDzD8P3WzFyUrTTV48g-1; Thu, 08 Dec 2022 11:59:57 -0500 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 395331C0515C; Thu, 8 Dec 2022 16:59:55 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com [10.30.29.100]) by smtp.corp.redhat.com (Postfix) with ESMTP id 231DF1121331; Thu, 8 Dec 2022 16:59:55 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (localhost [IPv6:::1]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id 0B89B194705A; Thu, 8 Dec 2022 16:59:48 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id C44101946A41 for ; Thu, 8 Dec 2022 16:59:46 +0000 (UTC) Received: by smtp.corp.redhat.com (Postfix) id BA0DC42222; Thu, 8 Dec 2022 16:59:46 +0000 (UTC) Received: from maggie.redhat.com (unknown [10.43.2.39]) by smtp.corp.redhat.com (Postfix) with ESMTP id 5DAE917582 for ; Thu, 8 Dec 2022 16:59:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1670518800; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=TMfv6sxKH2uFyxM2LENfjEHGSTS7O1RH8adbWUuBzCc=; b=BunmZvZOvm3bm02FbjQGb3M079eFFxjHKgco+jMTtcgLMr2yz4830I49q4VoUz3xvhFA1f SR8pKFdnocFb1If37TRwjfrMBpo5vYxvG5hGdOoahQZJueTtK734+WLJAMbL1rzuqTOuP7 QqoPNfBHSRKyxeWkD6bcYkayhpR4du8= X-MC-Unique: TPruDzD8P3WzFyUrTTV48g-1 X-Original-To: libvir-list@listman.corp.redhat.com From: Michal Privoznik To: libvir-list@redhat.com Subject: [PATCH v2 2/2] virnetdevtap.c: Disallow pre-existing TAP devices Date: Thu, 8 Dec 2022 17:59:43 +0100 Message-Id: <228b28f6fe4e19b8ff4142fd135e787791aa266a.1670518675.git.mprivozn@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.5 X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libvir-list-bounces@redhat.com Sender: "libvir-list" X-Scanned-By: MIMEDefang 3.1 on 10.11.54.3 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1670518803924100001 Content-Type: text/plain; charset="utf-8"; x-default="true" When starting a guest with which has the target device name set (i.e. not generated by us), it may happen that the TAP device already exists. This then may lead to all sorts of problems. For instance: for the TAP device is plugged into the network's bridge, but since the TAP device is persistent it remains plugged there even after the guest is shut off. We don't have a code that unplugs TAP devices from the bridge because TAP devices we create are transient, i.e. are removed automatically when QEMU closes their FD. The only exception is with where we specifically want to let users use pre-created TAP device and basically not touch it at all. There's another reason for denying to use a pre-created TAP devices: if we ever have bug in TAP name generation, we may re-use a TAP device from another domain. Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=3D2144738 Signed-off-by: Michal Privoznik Reviewed-by: Laine Stump --- src/qemu/qemu_interface.c | 2 ++ src/util/virnetdevtap.c | 23 +++++++++++++++++++++-- src/util/virnetdevtap.h | 2 ++ 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/qemu/qemu_interface.c b/src/qemu/qemu_interface.c index 4cc76e07a5..264d5e060c 100644 --- a/src/qemu/qemu_interface.c +++ b/src/qemu/qemu_interface.c @@ -461,6 +461,8 @@ qemuInterfaceEthernetConnect(virDomainDef *def, if (!net->ifname) template_ifname =3D true; =20 + tap_create_flags |=3D VIR_NETDEV_TAP_CREATE_ALLOW_EXISTING; + if (virNetDevTapCreate(&net->ifname, tunpath, tapfd, tapfdSize, tap_create_flags) < 0) { goto cleanup; diff --git a/src/util/virnetdevtap.c b/src/util/virnetdevtap.c index 112a1e8b99..a4ead0ae93 100644 --- a/src/util/virnetdevtap.c +++ b/src/util/virnetdevtap.c @@ -148,12 +148,15 @@ virNetDevTapGetRealDeviceName(char *ifname G_GNUC_UNU= SED) * @tunpath: path to the tun device (if NULL, /dev/net/tun is used) * @tapfds: array of file descriptors return value for the new tap device * @tapfdSize: number of file descriptors in @tapfd - * @flags: OR of virNetDevTapCreateFlags. Only one flag is recognized: + * @flags: OR of virNetDevTapCreateFlags. Only the following flags are + * recognized: * * VIR_NETDEV_TAP_CREATE_VNET_HDR * - Enable IFF_VNET_HDR on the tap device * VIR_NETDEV_TAP_CREATE_PERSIST * - The device will persist after the file descriptor is closed + * VIR_NETDEV_TAP_CREATE_ALLOW_EXISTING + * - The device creation fails if @ifname already exists * * Creates a tap interface. The caller must use virNetDevTapDelete to * remove a persistent TAP device when it is no longer needed. In case @@ -170,6 +173,7 @@ int virNetDevTapCreate(char **ifname, { size_t i =3D 0; struct ifreq ifr; + int rc; int ret =3D -1; int fd =3D -1; =20 @@ -179,9 +183,24 @@ int virNetDevTapCreate(char **ifname, * can lead to race conditions). if ifname is just a * user-provided name, virNetDevGenerateName leaves it * unchanged. */ - if (virNetDevGenerateName(ifname, VIR_NET_DEV_GEN_NAME_VNET) < 0) + rc =3D virNetDevGenerateName(ifname, VIR_NET_DEV_GEN_NAME_VNET); + if (rc < 0) return -1; =20 + if (rc > 0 && + !(flags & VIR_NETDEV_TAP_CREATE_ALLOW_EXISTING)) { + rc =3D virNetDevExists(*ifname); + + if (rc < 0) { + return -1; + } else if (rc > 0) { + virReportError(VIR_ERR_OPERATION_INVALID, + _("The %s interface already exists"), + *ifname); + return -1; + } + } + if (!tunpath) tunpath =3D "/dev/net/tun"; =20 diff --git a/src/util/virnetdevtap.h b/src/util/virnetdevtap.h index 197ea10f94..c9d29c0384 100644 --- a/src/util/virnetdevtap.h +++ b/src/util/virnetdevtap.h @@ -56,6 +56,8 @@ typedef enum { VIR_NETDEV_TAP_CREATE_USE_MAC_FOR_BRIDGE =3D 1 << 2, /* The device will persist after the file descriptor is closed */ VIR_NETDEV_TAP_CREATE_PERSIST =3D 1 << 3, + /* The device is allowed to exist before creation */ + VIR_NETDEV_TAP_CREATE_ALLOW_EXISTING =3D 1 << 4, } virNetDevTapCreateFlags; =20 int --=20 2.37.4