From nobody Fri Jan 9 08:53:47 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) client-ip=8.43.85.245; envelope-from=devel-bounces@lists.libvirt.org; helo=lists.libvirt.org; Received-SPF: pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) client-ip=8.43.85.245; envelope-from=devel-bounces@lists.libvirt.org; helo=lists.libvirt.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) smtp.mailfrom=devel-bounces@lists.libvirt.org; dmarc=pass(p=reject dis=none) header.from=lists.libvirt.org ARC-Seal: i=1; a=rsa-sha256; t=1766070412; cv=none; d=zohomail.com; s=zohoarc; b=M/lk+NwgR2GdB8v+QZ6ppXv3C3bPKYI18pJior2/GneLDM1ioLdIocvDzLAgYt2v4X/LzsiTfzhbDObYMdFs9/D6qd4ySjdXTnD3E5fR9teP+ejzs9N4zM+PPTd60AwcjrKYDjbVEzNElhnyCpSrDhZCBigPOIINRZG/fbHyeQk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1766070412; h=Content-Type:Content-Transfer-Encoding:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Owner:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:References:Subject:Subject:To:To:Message-Id:Cc; bh=yh3tUOW9Hru46ShsnaTygoDXPlIoZTPJkLrco7Ar4DU=; b=Y9NFv+j6gmMND7ws8Q6iSes2GmK+3wskr7f7iKusF6BKRx/uvNt2laGvcXY1yc/1sqFZErF6NmwUgq4fdKDrU0v7253MDPrDKkpUewTudPcR3Kcb/+Td6OSWm/0ZJ3NJnuypvHd/ztFIDvkJBSAhInkjDwqbJ17Bk6kTS8qYEY4= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) smtp.mailfrom=devel-bounces@lists.libvirt.org; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists.libvirt.org (lists.libvirt.org [8.43.85.245]) by mx.zohomail.com with SMTPS id 17660704119941015.4393628295811; Thu, 18 Dec 2025 07:06:51 -0800 (PST) Received: by lists.libvirt.org (Postfix, from userid 993) id F1EEA3F936; Thu, 18 Dec 2025 10:06:50 -0500 (EST) Received: from [172.19.199.83] (lists.libvirt.org [8.43.85.245]) by lists.libvirt.org (Postfix) with ESMTP id 5B44D418F5; Thu, 18 Dec 2025 10:05:50 -0500 (EST) Received: by lists.libvirt.org (Postfix, from userid 993) id BDECF4183C; Thu, 18 Dec 2025 09:53:40 -0500 (EST) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (3072 bits) server-digest SHA256) (No client certificate requested) by lists.libvirt.org (Postfix) with ESMTPS id CFD0041823 for ; Thu, 18 Dec 2025 09:53:38 -0500 (EST) Received: from mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-271-ImMnrFDCMeWn8N39Wj4yFg-1; Thu, 18 Dec 2025 09:53:35 -0500 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (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-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id EA30D180066C for ; Thu, 18 Dec 2025 14:53:34 +0000 (UTC) Received: from moe (unknown [10.43.3.236]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 538EF19560B4 for ; Thu, 18 Dec 2025 14:53:34 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on lists.libvirt.org X-Spam-Level: X-Spam-Status: No, score=-5.0 required=5.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED,RCVD_IN_VALIDITY_RPBL_BLOCKED, RCVD_IN_VALIDITY_SAFE_BLOCKED,SPF_PASS autolearn=unavailable autolearn_force=no version=4.0.1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1766069618; h=from:from: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; bh=yh3tUOW9Hru46ShsnaTygoDXPlIoZTPJkLrco7Ar4DU=; b=IoK1Jtl5xBE9tKkm3kYO4TuL4zkr16jmvBjvU1G7cJPuwIzwg/9qG8uagUAT/RbzFGZtM1 zsHbAkx/Os/yLmaGYEahNnjcGdoLrDY9n+Je7O1AEAm0quA8KhcdLNPf34C5yc7DXec9qB jVgJ71Gx9DZ5LzBLO4DaQ/ix5lel0Iw= X-MC-Unique: ImMnrFDCMeWn8N39Wj4yFg-1 X-Mimecast-MFC-AGG-ID: ImMnrFDCMeWn8N39Wj4yFg_1766069615 To: devel@lists.libvirt.org Subject: [PATCH v2 1/2] util: Introduce virStringEscapeDBus() Date: Thu, 18 Dec 2025 15:53:29 +0100 Message-ID: <2cd35ac018482f42e7494c9e529b7dab509e5ea7.1766068973.git.mprivozn@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.12 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: 7AlsJWWSLOUGleuD4OQKFzxfhWezA77MfzxwNHaTFGY_1766069615 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Message-ID-Hash: KSFE7JVRAIUR52WWYQHZCWN56MHZYIJD X-Message-ID-Hash: KSFE7JVRAIUR52WWYQHZCWN56MHZYIJD X-MailFrom: mprivozn@redhat.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; header-match-devel.lists.libvirt.org-0; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header X-Mailman-Version: 3.3.10 Precedence: list List-Id: Development discussions about the libvirt library & tools Archived-At: List-Archive: <> List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: From: Michal Privoznik via Devel Reply-To: Michal Privoznik X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1766070412971158500 Content-Type: text/plain; charset="utf-8"; x-default="true" From: Michal Privoznik DBus allows only some characters to be specified verbatim [1]. Everything else must be specified via '%NN' syntax where NN is hex value of the escaped character. Introduce virStringEscapeDBus() helper to handle string escaping. 1: https://gitlab.freedesktop.org/dbus/dbus/-/blob/2dee5236088bcf690ba92b74= 3a584720b8cb6f55/dbus/dbus-address.c#L288 Signed-off-by: Michal Privoznik --- src/libvirt_private.syms | 1 + src/util/virstring.c | 64 ++++++++++++++++++++++++++++++++++++++++ src/util/virstring.h | 1 + tests/virstringtest.c | 43 +++++++++++++++++++++++++++ 4 files changed, 109 insertions(+) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 4e57e4a8f6..ef8ddd0330 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -3478,6 +3478,7 @@ virSkipSpacesBackwards; virSkipToDigit; virStrcpy; virStringBufferIsPrintable; +virStringEscapeDBus; virStringFilterChars; virStringFormatHex; virStringHasCaseSuffix; diff --git a/src/util/virstring.c b/src/util/virstring.c index e001d76bf1..0729002d12 100644 --- a/src/util/virstring.c +++ b/src/util/virstring.c @@ -1118,3 +1118,67 @@ virStringFormatHex(const unsigned char *buf, size_t = len) =20 return g_steal_pointer(&hex); } + + +/** + * virStringEscapeDBus: + * @str: string to escape + * + * Produces new string that's safe to pass to DBUS. Specifically, + * alphanumerical characters, digits, '-', '_', '/', '\\', '*' and '.' are + * kept. Everything else is escaped using "%NN", where NN is value of the + * character in hex. + * + * Returns: newly allocated string. Caller must free. + */ +char * +virStringEscapeDBus(const char *str) +{ + size_t len; + size_t i; + size_t j =3D 0; + size_t alloc =3D 1; + char *ret; + + if (!str) + return NULL; + + len =3D strlen(str); + alloc =3D len * 1.25; /* assume some escaping */ + ret =3D g_new0(char, alloc + 1); + + for (i =3D 0; str[i]; i++) { + const char c =3D str[i]; + + if ((c >=3D 'a' && c <=3D 'z') || + (c >=3D 'A' && c <=3D 'Z') || + (c >=3D '0' && c <=3D '9') || + (c =3D=3D '-') || + (c =3D=3D '_') || + (c =3D=3D '/') || + (c =3D=3D '\\') || + (c =3D=3D '*') || + (c =3D=3D '.')) { + if (j >=3D alloc) { + ret =3D g_renew(char, ret, alloc + 11); + alloc +=3D 10; + } + + ret[j++] =3D c; + } else { + static const char hextable[] =3D "0123456789ABCDEF"; + + if (j + 3 >=3D alloc) { + ret =3D g_renew(char, ret, alloc + 11); + alloc +=3D 10; + } + + ret[j++] =3D '%'; + ret[j++] =3D hextable[(c >> 4) & 15]; + ret[j++] =3D hextable[c & 15]; + } + } + + ret[j++] =3D '\0'; + return ret; +} diff --git a/src/util/virstring.h b/src/util/virstring.h index 8c2208ece8..aadfc37e41 100644 --- a/src/util/virstring.h +++ b/src/util/virstring.h @@ -141,3 +141,4 @@ int virStringParseVersion(unsigned long long *version, =20 void virStringListRemoveDuplicates(char ***list); char *virStringFormatHex(const unsigned char *buf, size_t len); +char *virStringEscapeDBus(const char *str); diff --git a/tests/virstringtest.c b/tests/virstringtest.c index 0792155cc3..50b0e2cc5c 100644 --- a/tests/virstringtest.c +++ b/tests/virstringtest.c @@ -486,6 +486,29 @@ static int testFilterChars(const void *args) return 0; } =20 + +struct testDBusData { + const char *string; + const char *expected; +}; + + +static int +testDBusEscape(const void *args) +{ + const struct testDBusData *data =3D args; + g_autofree char *res =3D virStringEscapeDBus(data->string); + + if (STRNEQ_NULLABLE(res, data->expected)) { + fprintf(stderr, "%s: returned '%s', expected '%s'\n", + __FUNCTION__, NULLSTR(res), NULLSTR(data->expected)); + return -1; + } + + return 0; +} + + static int mymain(void) { @@ -767,6 +790,26 @@ mymain(void) TEST_FILTER_CHARS(NULL, NULL, NULL); TEST_FILTER_CHARS("hello 123 hello", "helo", "hellohello"); =20 +#define TEST_DBUS_ESCAPE(str, exp) \ + do { \ + struct testDBusData dbusData =3D { \ + .string =3D str, \ + .expected =3D exp, \ + }; \ + if (virTestRun("DBus escape " #str, \ + testDBusEscape, &dbusData) < 0) \ + ret =3D -1; \ + } while (0) + + TEST_DBUS_ESCAPE(NULL, NULL); + TEST_DBUS_ESCAPE("", ""); + TEST_DBUS_ESCAPE("-_/\\*.", "-_/\\*."); + TEST_DBUS_ESCAPE("abcdefghijklmnopqrstuvwxyz", + "abcdefghijklmnopqrstuvwxyz"); + TEST_DBUS_ESCAPE("/some/~/path/with space/-_\\*./#$", + "/some/%7E/path/with%20space/-_\\*./%23%24"); + + return ret =3D=3D 0 ? EXIT_SUCCESS : EXIT_FAILURE; } =20 --=20 2.51.2