Matthew Booth
2010-Oct-26 11:02 UTC
[Libguestfs] [PATCH] Don't include control characters in BufferIn trace output
Certain API calls, pwrite-device is an example, take a BufferIn as an argument which may contain arbitrary binary data. If trace output is on, we will attempt to print this to the terminal. As well as not working properly due to embedded NUL characters, this can cause problems on the output terminal, is unlikely to be useful, and can't be entered into guestfish in any case. This patch scans BufferIn arguments if trace output is on. If it finds any control characters it will replace the entire string with '<data>' in the trace output. --- generator/generator_c.ml | 23 +++++++++++++++++++++-- 1 files changed, 21 insertions(+), 2 deletions(-) -------------- next part -------------- A non-text attachment was scrubbed... Name: 0001-Don-t-include-control-characters-in-BufferIn-trace-o.patch Type: text/x-patch Size: 1908 bytes Desc: not available URL: <http://listman.redhat.com/archives/libguestfs/attachments/20101026/6d401ccf/attachment.bin>
Richard W.M. Jones
2010-Oct-26 12:56 UTC
[Libguestfs] [PATCH] Don't include control characters in BufferIn trace output
I think the attached patch is an improvement on the concept. Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones New in Fedora 11: Fedora Windows cross-compiler. Compile Windows programs, test, and build Windows installers. Over 70 libraries supprt'd http://fedoraproject.org/wiki/MinGW http://www.annexia.org/fedora_mingw -------------- next part -------------->From 285afc5e03be92853e49f1eef34b8b6e8184614b Mon Sep 17 00:00:00 2001From: Matthew Booth <mbooth at redhat.com> Date: Tue, 26 Oct 2010 13:54:17 +0100 Subject: [PATCH] Don't include control characters in BufferIn trace output (RHBZ#646822). The example below shows what the output looks like for a large random buffer. $ guestfish -N fs -m /dev/sda1 -x -- \ touch /test : \ pwrite /test "$(dd if=/dev/urandom bs=128k count=1)" 0 [...] pwrite "/test" "\x7f\xa0/\xb3\x80\xd3\xbc\xc3\xc3.\xb1\xe0\x1b\xafC\x06\xd5;\x0ajJ[o\xc1\xdd\xae\x1f\xce\xb2\x8d\xb3\xd0\x11\xcc$%\xe6<\xc7\xc7\xe7BU*\xc4l%\xaa\xea\xe9\x1an\xda]\xc6I\x0eC\xf9;\xec\x12a\x1f\xeaRH\xb2P\xd6+\xc4\xe6\xa5bW\x99\\x9d\xc8\x9bJ\xef\x99-\x16:h5\xe2\x0f\xa2\xa08\x9bU\x0b$\x138\xcf\xd4j\x9b\x83{%\xac0\xdaa1Xx\xbd`\x8e\xdd\x82\x87\x07\x98\xd2\x9ed\x8bq\xd0\x1f5\x8f\xab\xad4z1\xda\xc4b\xc1\xbc\x0f\xaa\xea\xc1\x15(\xfd1\xc2\x0bF\xe6\x9e\xb0+/g\\xab\xb0b\xde_\xca\xf9\xad\xe1?%\x17\xad\x98\xa4e\xc1\xe0f'\x89\xe9>\xff\xadhYi\xe7\x8c]%\xef\xe0\xa1R\xe5\xd5\x03K\xefI\xdf\xad\xd3\x82\xdb\x0f\xdd\xc3\x8f"\xf1G\xea\xf9r\xdd\xff\x88\x81\xb7\xf2\x0e\x0f\x1d;:\xf2F1\xdb\xb5D\xa1^\x928\xf5\x8e)\xab\xc4\xc3H(\xd0ol\xc6\xe4\xd6\xa3L\x1c\x06\xf4"<truncated, original size 130567 bytes> 0 [...] --- generator/generator_c.ml | 4 +++- src/guestfs-internal.h | 1 + src/guestfs.c | 29 +++++++++++++++++++++++++++++ 3 files changed, 33 insertions(+), 1 deletions(-) diff --git a/generator/generator_c.ml b/generator/generator_c.ml index 0a9060b..dad3ac3 100644 --- a/generator/generator_c.ml +++ b/generator/generator_c.ml @@ -591,7 +591,6 @@ check_state (guestfs_h *g, const char *caller) | Dev_or_Path n | FileIn n | FileOut n - | BufferIn n | Key n -> (* guestfish doesn't support string escaping, so neither do we *) pr " fprintf (stderr, \" \\\"%%s\\\"\", %s);\n" n @@ -613,6 +612,9 @@ check_state (guestfs_h *g, const char *caller) pr " fprintf (stderr, \" %%d\", %s);\n" n | Int64 n -> pr " fprintf (stderr, \" %%\" PRIi64, %s);\n" n + | BufferIn n -> (* RHBZ#646822 *) + pr " fputc (' ', stderr);\n"; + pr " guestfs___print_BufferIn (stderr, %s, %s_size);\n" n n ) args; (* Optional arguments. *) diff --git a/src/guestfs-internal.h b/src/guestfs-internal.h index 212006d..a42375f 100644 --- a/src/guestfs-internal.h +++ b/src/guestfs-internal.h @@ -212,6 +212,7 @@ extern int guestfs___send_to_daemon (guestfs_h *g, const void *v_buf, size_t n); extern int guestfs___recv_from_daemon (guestfs_h *g, uint32_t *size_rtn, void **buf_rtn); extern int guestfs___accept_from_daemon (guestfs_h *g); extern int guestfs___build_appliance (guestfs_h *g, char **kernel, char **initrd, char **appliance); +extern void guestfs___print_BufferIn (FILE *out, const char *buf, size_t buf_size); #define error guestfs_error #define perrorf guestfs_perrorf diff --git a/src/guestfs.c b/src/guestfs.c index 245f10c..df13d51 100644 --- a/src/guestfs.c +++ b/src/guestfs.c @@ -61,6 +61,7 @@ #include <arpa/inet.h> #include <netinet/in.h> +#include "c-ctype.h" #include "glthread/lock.h" #include "hash.h" #include "hash-pjw.h" @@ -732,3 +733,31 @@ guestfs_get_private (guestfs_h *g, const char *key) else return NULL; } + +/* When tracing, be careful how we print BufferIn parameters which + * usually contain large amounts of binary data (RHBZ#646822). + */ +void +guestfs___print_BufferIn (FILE *out, const char *buf, size_t buf_size) +{ + size_t i; + size_t orig_size = buf_size; + + if (buf_size > 256) + buf_size = 256; + + fputc ('"', out); + + for (i = 0; i < buf_size; ++i) { + if (c_isprint (buf[i])) + fputc (buf[i], out); + else + fprintf (out, "\\x%02x", (unsigned char) buf[i]); + } + + fputc ('"', out); + + if (orig_size > buf_size) + fprintf (out, + _("<truncated, original size %zu bytes>"), orig_size); +} -- 1.7.3.1