[libvirt] [PATCH v2 0/3] introduce virBufferEscapeN and fix escape bug

I've dropped the STRCAT patches in v2. The whole purpose of the STRCAT was to avoid calling *strcspn* several times but it was probably a bad call. Now we call that function for each pair but if no escaping is required we at least don't allocate the structure for that pair and later in the *virBufferEscapeN* we only iterate over the pairs that will escape at least one character. Pavel Hrdina (3): util: virbuffer: introduce virBufferEscapeN util: virqemu: introduce virQEMUBuildBufferEscape qemu: properly escape socket path for graphics src/libvirt_private.syms | 2 + src/qemu/qemu_command.c | 6 +- src/util/virbuffer.c | 101 +++++++++++++++++++++ src/util/virbuffer.h | 2 + src/util/virqemu.c | 17 ++++ src/util/virqemu.h | 1 + .../qemuxml2argvdata/qemuxml2argv-name-escape.args | 5 +- .../qemuxml2argvdata/qemuxml2argv-name-escape.xml | 7 +- tests/qemuxml2argvtest.c | 3 +- tests/virbuftest.c | 41 +++++++++ 10 files changed, 179 insertions(+), 6 deletions(-) -- 2.11.1

Signed-off-by: Pavel Hrdina <phrdina@redhat.com> --- src/libvirt_private.syms | 1 + src/util/virbuffer.c | 101 +++++++++++++++++++++++++++++++++++++++++++++++ src/util/virbuffer.h | 2 + tests/virbuftest.c | 41 +++++++++++++++++++ 4 files changed, 145 insertions(+) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 07a35333b1..28e595fe58 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -1286,6 +1286,7 @@ virBufferContentAndReset; virBufferCurrentContent; virBufferError; virBufferEscape; +virBufferEscapeN; virBufferEscapeSexpr; virBufferEscapeShell; virBufferEscapeString; diff --git a/src/util/virbuffer.c b/src/util/virbuffer.c index d582e7dbec..bd4ae9738c 100644 --- a/src/util/virbuffer.c +++ b/src/util/virbuffer.c @@ -33,6 +33,7 @@ #include "virbuffer.h" #include "viralloc.h" #include "virerror.h" +#include "virstring.h" /* If adding more fields, ensure to edit buf.h to match @@ -588,6 +589,106 @@ virBufferEscape(virBufferPtr buf, char escape, const char *toescape, VIR_FREE(escaped); } + +struct _virBufferEscapePair { + char escape; + char *toescape; +}; + + +/** + * virBufferEscapeN: + * @buf: the buffer to append to + * @format: a printf like format string but with only one %s parameter + * @str: the string argument which needs to be escaped + * @...: the variable list of arguments composed + * + * The variable list of arguments @... must be composed of + * 'char escape, char *toescape' pairs followed by NULL. + * + * This has the same functionality as virBufferEscape with the extension + * that allows to specify multiple pairs of chars that needs to be escaped. + */ +void +virBufferEscapeN(virBufferPtr buf, + const char *format, + const char *str, + ...) +{ + int len; + size_t i; + char escape; + char *toescape; + char *escaped = NULL; + char *out; + const char *cur; + struct _virBufferEscapePair escapeItem; + struct _virBufferEscapePair *escapeList = NULL; + size_t nescapeList = 0; + va_list ap; + + if ((format == NULL) || (buf == NULL) || (str == NULL)) + return; + + if (buf->error) + return; + + len = strlen(str); + + va_start(ap, str); + + while ((escape = va_arg(ap, int))) { + if (!(toescape = va_arg(ap, char *))) { + virBufferSetError(buf, errno); + goto cleanup; + } + + if (strcspn(str, toescape) == len) + continue; + + escapeItem.escape = escape; + escapeItem.toescape = toescape; + + if (VIR_APPEND_ELEMENT_QUIET(escapeList, nescapeList, escapeItem) < 0) { + virBufferSetError(buf, errno); + goto cleanup; + } + } + + if (nescapeList == 0) { + virBufferAsprintf(buf, format, str); + goto cleanup; + } + + if (xalloc_oversized(2, len) || + VIR_ALLOC_N_QUIET(escaped, 2 * len + 1) < 0) { + virBufferSetError(buf, errno); + goto cleanup; + } + + cur = str; + out = escaped; + while (*cur != 0) { + for (i = 0; i < nescapeList; i++) { + if (strchr(escapeList[i].toescape, *cur)) { + *out++ = escapeList[i].escape; + break; + } + } + *out++ = *cur; + cur++; + } + *out = 0; + + virBufferAsprintf(buf, format, escaped); + + cleanup: + va_end(ap); + VIR_FREE(escapeList); + VIR_FREE(escaped); +} + + /** * virBufferURIEncodeString: * @buf: the buffer to append to diff --git a/src/util/virbuffer.h b/src/util/virbuffer.h index 144a1ba06e..94f14b5b16 100644 --- a/src/util/virbuffer.h +++ b/src/util/virbuffer.h @@ -82,6 +82,8 @@ void virBufferStrcat(virBufferPtr buf, ...) ATTRIBUTE_SENTINEL; void virBufferEscape(virBufferPtr buf, char escape, const char *toescape, const char *format, const char *str); +void virBufferEscapeN(virBufferPtr buf, const char *format, + const char *str, ...); void virBufferEscapeString(virBufferPtr buf, const char *format, const char *str); void virBufferEscapeSexpr(virBufferPtr buf, const char *format, diff --git a/tests/virbuftest.c b/tests/virbuftest.c index 22407ab6a8..34160e6b28 100644 --- a/tests/virbuftest.c +++ b/tests/virbuftest.c @@ -376,6 +376,35 @@ testBufEscapeStr(const void *opaque ATTRIBUTE_UNUSED) static int +testBufEscapeN(const void *opaque) +{ + const struct testBufAddStrData *data = opaque; + virBuffer buf = VIR_BUFFER_INITIALIZER; + char *actual; + int ret = -1; + + virBufferEscapeN(&buf, "%s", data->data, '\\', "=", ',', ",", NULL); + + if (!(actual = virBufferContentAndReset(&buf))) { + VIR_TEST_DEBUG("testBufEscapeN: buf is empty"); + goto cleanup; + } + + if (STRNEQ_NULLABLE(actual, data->expect)) { + VIR_TEST_DEBUG("testBufEscapeN: Strings don't match:\n"); + virTestDifference(stderr, data->expect, actual); + goto cleanup; + } + + ret = 0; + + cleanup: + VIR_FREE(actual); + return ret; +} + + +static int mymain(void) { int ret = 0; @@ -422,6 +451,18 @@ mymain(void) DO_TEST_ESCAPE("\x01\x01\x02\x03\x05\x08", "<c>\n <el></el>\n</c>"); +#define DO_TEST_ESCAPEN(data, expect) \ + do { \ + struct testBufAddStrData info = { data, expect }; \ + if (virTestRun("Buf: EscapeN", testBufEscapeN, &info) < 0) \ + ret = -1; \ + } while (0) + + DO_TEST_ESCAPEN("noescape", "noescape"); + DO_TEST_ESCAPEN("comma,escape", "comma,,escape"); + DO_TEST_ESCAPEN("equal=escape", "equal\\=escape"); + DO_TEST_ESCAPEN("comma,equal=escape", "comma,,equal\\=escape"); + return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE; } -- 2.11.1

On Thu, Feb 23, 2017 at 09:36:16PM +0100, Pavel Hrdina wrote:
+/** + * virBufferEscapeN: + * @buf: the buffer to append to + * @format: a printf like format string but with only one %s parameter + * @str: the string argument which needs to be escaped
+ * @...: the variable list of arguments composed
s/arguments composed/escape pairs/ maybe?
+ * + * The variable list of arguments @... must be composed of + * 'char escape, char *toescape' pairs followed by NULL. + * + * This has the same functionality as virBufferEscape with the extension + * that allows to specify multiple pairs of chars that needs to be escaped. + */ +void +virBufferEscapeN(virBufferPtr buf, + const char *format, + const char *str, + ...) +{ + int len; + size_t i; + char escape; + char *toescape; + char *escaped = NULL; + char *out; + const char *cur; + struct _virBufferEscapePair escapeItem; + struct _virBufferEscapePair *escapeList = NULL; + size_t nescapeList = 0; + va_list ap; + + if ((format == NULL) || (buf == NULL) || (str == NULL)) + return; + + if (buf->error) + return; + + len = strlen(str); + + va_start(ap, str); +
+ while ((escape = va_arg(ap, int))) { + if (!(toescape = va_arg(ap, char *))) {
You can either assign directly to escapeItem.{to,}escape here, or declare toescape and escapeItem inside the while loop to reduce the number of function-wide variables.
+ virBufferSetError(buf, errno); + goto cleanup; + }
Jan

This will eventually replace virQEMUBuildBufferEscapeComma, however it's not possible right now. Some parts of the code that uses the old function needs to be refactored. Signed-off-by: Pavel Hrdina <phrdina@redhat.com> --- src/libvirt_private.syms | 1 + src/util/virqemu.c | 17 +++++++++++++++++ src/util/virqemu.h | 1 + 3 files changed, 19 insertions(+) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 28e595fe58..dffc1f2a24 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -2298,6 +2298,7 @@ virProcessWait; # util/virqemu.h +virQEMUBuildBufferEscape; virQEMUBuildBufferEscapeComma; virQEMUBuildCommandLineJSON; virQEMUBuildCommandLineJSONArrayBitmap; diff --git a/src/util/virqemu.c b/src/util/virqemu.c index 2e9e65f9ef..f10b356781 100644 --- a/src/util/virqemu.c +++ b/src/util/virqemu.c @@ -300,6 +300,23 @@ virQEMUBuildBufferEscapeComma(virBufferPtr buf, const char *str) /** + * virQEMUBuildBufferEscape: + * @buf: buffer to append the escaped string + * @str: the string to escape + * + * Some characters passed as values on the QEMU command line must be escaped. + * + * - ',' must by escaped by ',' + * - '=' must by escaped by '\' + */ +void +virQEMUBuildBufferEscape(virBufferPtr buf, const char *str) +{ + virBufferEscapeN(buf, "%s", str, ',', ",", '\\', "=", NULL); +} + + +/** * virQEMUBuildLuksOpts: * @buf: buffer to build the string into * @enc: pointer to encryption info diff --git a/src/util/virqemu.h b/src/util/virqemu.h index 539d62ab14..10aeb67f4e 100644 --- a/src/util/virqemu.h +++ b/src/util/virqemu.h @@ -50,6 +50,7 @@ char *virQEMUBuildObjectCommandlineFromJSON(const char *type, char *virQEMUBuildDriveCommandlineFromJSON(virJSONValuePtr src); void virQEMUBuildBufferEscapeComma(virBufferPtr buf, const char *str); +void virQEMUBuildBufferEscape(virBufferPtr buf, const char *str); void virQEMUBuildLuksOpts(virBufferPtr buf, virStorageEncryptionInfoDefPtr enc, const char *alias) -- 2.11.1

Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1352529 Signed-off-by: Pavel Hrdina <phrdina@redhat.com> --- src/qemu/qemu_command.c | 6 ++++-- tests/qemuxml2argvdata/qemuxml2argv-name-escape.args | 5 +++-- tests/qemuxml2argvdata/qemuxml2argv-name-escape.xml | 7 ++++++- tests/qemuxml2argvtest.c | 3 ++- 4 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/qemu/qemu_command.c b/src/qemu/qemu_command.c index d5da533e50..41eecfd187 100644 --- a/src/qemu/qemu_command.c +++ b/src/qemu/qemu_command.c @@ -7495,7 +7495,7 @@ qemuBuildGraphicsVNCCommandLine(virQEMUDriverConfigPtr cfg, switch (glisten->type) { case VIR_DOMAIN_GRAPHICS_LISTEN_TYPE_SOCKET: virBufferAddLit(&opt, "unix:"); - virQEMUBuildBufferEscapeComma(&opt, glisten->socket); + virQEMUBuildBufferEscape(&opt, glisten->socket); break; case VIR_DOMAIN_GRAPHICS_LISTEN_TYPE_ADDRESS: @@ -7627,7 +7627,9 @@ qemuBuildGraphicsSPICECommandLine(virQEMUDriverConfigPtr cfg, goto error; } - virBufferAsprintf(&opt, "unix,addr=%s,", glisten->socket); + virBufferAddLit(&opt, "unix,addr="); + virQEMUBuildBufferEscape(&opt, glisten->socket); + virBufferAddLit(&opt, ","); hasInsecure = true; break; diff --git a/tests/qemuxml2argvdata/qemuxml2argv-name-escape.args b/tests/qemuxml2argvdata/qemuxml2argv-name-escape.args index 9ae50bd455..deb37e09e6 100644 --- a/tests/qemuxml2argvdata/qemuxml2argv-name-escape.args +++ b/tests/qemuxml2argvdata/qemuxml2argv-name-escape.args @@ -3,7 +3,7 @@ PATH=/bin \ HOME=/home/test \ USER=test \ LOGNAME=test \ -QEMU_AUDIO_DRV=none \ +QEMU_AUDIO_DRV=spice \ /usr/bin/qemu \ -name guest=foo=1,,bar=2,debug-threads=on \ -S \ @@ -20,6 +20,7 @@ bar=2/monitor.sock,server,nowait \ -no-acpi \ -boot c \ -usb \ --vnc unix:/tmp/bar,,foo.sock \ +-vnc 'unix:/tmp/lib/domain--1-foo\=1,,bar\=2/vnc.sock' \ +-spice 'unix,addr=/tmp/lib/domain--1-foo\=1,,bar\=2/spice.sock' \ -vga cirrus \ -device virtio-balloon-pci,id=balloon0,bus=pci.0,addr=0x3 diff --git a/tests/qemuxml2argvdata/qemuxml2argv-name-escape.xml b/tests/qemuxml2argvdata/qemuxml2argv-name-escape.xml index 5e8c7476fe..604e1453f2 100644 --- a/tests/qemuxml2argvdata/qemuxml2argv-name-escape.xml +++ b/tests/qemuxml2argvdata/qemuxml2argv-name-escape.xml @@ -14,6 +14,11 @@ <on_crash>destroy</on_crash> <devices> <emulator>/usr/bin/qemu</emulator> - <graphics type='vnc' socket='/tmp/bar,foo.sock'/> + <graphics type='vnc'> + <listen type='socket'/> + </graphics> + <graphics type='spice'> + <listen type='socket'/> + </graphics> </devices> </domain> diff --git a/tests/qemuxml2argvtest.c b/tests/qemuxml2argvtest.c index f55b04b057..f3b5648b5c 100644 --- a/tests/qemuxml2argvtest.c +++ b/tests/qemuxml2argvtest.c @@ -2455,7 +2455,8 @@ mymain(void) DO_TEST("name-escape", QEMU_CAPS_NAME_DEBUG_THREADS, QEMU_CAPS_OBJECT_SECRET, QEMU_CAPS_CHARDEV, QEMU_CAPS_VNC, - QEMU_CAPS_NAME_GUEST, QEMU_CAPS_DEVICE_CIRRUS_VGA); + QEMU_CAPS_NAME_GUEST, QEMU_CAPS_DEVICE_CIRRUS_VGA, + QEMU_CAPS_SPICE, QEMU_CAPS_SPICE_UNIX); DO_TEST("debug-threads", QEMU_CAPS_NAME_DEBUG_THREADS); DO_TEST("master-key", QEMU_CAPS_OBJECT_SECRET); -- 2.11.1

On Thu, Feb 23, 2017 at 09:36:15PM +0100, Pavel Hrdina wrote:
I've dropped the STRCAT patches in v2. The whole purpose of the STRCAT was to avoid calling *strcspn* several times but it was probably a bad call. Now we call that function for each pair but if no escaping is required we at least don't allocate the structure for that pair and later in the *virBufferEscapeN* we only iterate over the pairs that will escape at least one character.
Pavel Hrdina (3): util: virbuffer: introduce virBufferEscapeN util: virqemu: introduce virQEMUBuildBufferEscape qemu: properly escape socket path for graphics
src/libvirt_private.syms | 2 + src/qemu/qemu_command.c | 6 +- src/util/virbuffer.c | 101 +++++++++++++++++++++ src/util/virbuffer.h | 2 + src/util/virqemu.c | 17 ++++ src/util/virqemu.h | 1 + .../qemuxml2argvdata/qemuxml2argv-name-escape.args | 5 +- .../qemuxml2argvdata/qemuxml2argv-name-escape.xml | 7 +- tests/qemuxml2argvtest.c | 3 +- tests/virbuftest.c | 41 +++++++++ 10 files changed, 179 insertions(+), 6 deletions(-)
ACK series Jan

On Fri, Feb 24, 2017 at 11:53:29AM +0100, Ján Tomko wrote:
On Thu, Feb 23, 2017 at 09:36:15PM +0100, Pavel Hrdina wrote:
I've dropped the STRCAT patches in v2. The whole purpose of the STRCAT was to avoid calling *strcspn* several times but it was probably a bad call. Now we call that function for each pair but if no escaping is required we at least don't allocate the structure for that pair and later in the *virBufferEscapeN* we only iterate over the pairs that will escape at least one character.
Pavel Hrdina (3): util: virbuffer: introduce virBufferEscapeN util: virqemu: introduce virQEMUBuildBufferEscape qemu: properly escape socket path for graphics
src/libvirt_private.syms | 2 + src/qemu/qemu_command.c | 6 +- src/util/virbuffer.c | 101 +++++++++++++++++++++ src/util/virbuffer.h | 2 + src/util/virqemu.c | 17 ++++ src/util/virqemu.h | 1 + .../qemuxml2argvdata/qemuxml2argv-name-escape.args | 5 +- .../qemuxml2argvdata/qemuxml2argv-name-escape.xml | 7 +- tests/qemuxml2argvtest.c | 3 +- tests/virbuftest.c | 41 +++++++++ 10 files changed, 179 insertions(+), 6 deletions(-)
ACK series
Jan
Thanks, I've fixed the issues in patch 01 and pushed it. Pavel
-- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list
participants (2)
-
Ján Tomko
-
Pavel Hrdina