Skip to contentSkip to conversation

xenconsole: add option to avoid escape sequences in log

PATCH Superseded Marek Marczykowski-Górecki 3 participants 5 messages Jul 21, 2018 – Jul 26, 2018

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xenconsole: add option to avoid escape sequences in log

Add --replace-escape, -e option to xenconsoled, which replaces ESC with '.' in console output written to log file. This makes it slightly safer to do tail -f on a console output of untrusted guest.
The pty output is unaffected by this option.

---
Is there any more documentation to be updated? I didn't found xenconsoled man page or such.

---
 tools/console/daemon/io.c   | 18 ++++++++++++++++++
 tools/console/daemon/main.c |  9 +++++++--
 2 files changed, 25 insertions(+), 2 deletions(-)
tools/console/daemon/io.c +18 -0
index e22009aa39..c8280e0b7c 100644
@@ -68,6 +68,7 @@ extern int log_time_hv;
 extern int log_time_guest;
 extern char *log_dir;
 extern int discard_overflowed_data;
+extern int replace_escape;
 
 static int log_time_hv_needts = 1;
 static int log_time_guest_needts = 1;
@@ -227,8 +228,25 @@ static inline int console_iter_int_arg3(struct domain *d,
 	return ret;
 }
 
+static void do_replace_escape(const char *buf, char *dest, int len) {
+	int i;
+
+	memcpy(dest, buf, len);
+	for (i = 0; i < len; i++) {
+		if (dest[i] == '\033')
+			dest[i] = '.';
+	}
+}
+
 static int write_all(int fd, const char* buf, size_t len)
 {
+	char buf_replaced[len];
+
+	if (replace_escape) {
+		do_replace_escape(buf, buf_replaced, len);
+		buf = buf_replaced;
+	}
+
 	while (len) {
 		ssize_t ret = write(fd, buf, len);
 		if (ret == -1 && errno == EINTR)
tools/console/daemon/main.c +7 -2
index 806d2fd611..b716e38c27 100644
@@ -40,6 +40,7 @@ int log_time_hv = 0;
 int log_time_guest = 0;
 char *log_dir = NULL;
 int discard_overflowed_data = 1;
+int replace_escape = 0;
 
 static void handle_hup(int sig)
 {
@@ -48,7 +49,7 @@ static void handle_hup(int sig)
 
 static void usage(char *name)
 {
-	printf("Usage: %s [-h] [-V] [-v] [-i] [--log=none|guest|hv|all] [--log-dir=DIR] [--pid-file=PATH] [-t, --timestamp=none|guest|hv|all] [-o, --overflow-data=discard|keep]\n", name);
+	printf("Usage: %s [-h] [-V] [-v] [-i] [--log=none|guest|hv|all] [--log-dir=DIR] [--pid-file=PATH] [-t, --timestamp=none|guest|hv|all] [-o, --overflow-data=discard|keep] [-e, --replace-escape]\n", name);
 }
 
 static void version(char *name)
@@ -93,7 +94,7 @@ static void increase_fd_limit(void)
 
 int main(int argc, char **argv)
 {
-	const char *sopts = "hVvit:o:";
+	const char *sopts = "hVvit:o:e";
 	struct option lopts[] = {
 		{ "help", 0, 0, 'h' },
 		{ "version", 0, 0, 'V' },
@@ -104,6 +105,7 @@ int main(int argc, char **argv)
 		{ "pid-file", 1, 0, 'p' },
 		{ "timestamp", 1, 0, 't' },
 		{ "overflow-data", 1, 0, 'o'},
+		{ "replace-escape", 0, 0, 'e'},
 		{ 0 },
 	};
 	bool is_interactive = false;
@@ -168,6 +170,9 @@ int main(int argc, char **argv)
 				discard_overflowed_data = 1;
 			}
 			break;
+		case 'e':
+			replace_escape = 1;
+			break;
 		case '?':
 			fprintf(stderr,
 				"Try `%s --help' for more information\n",
Signature · 7 lines
2.17.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#1 lore raw
Quoted 4 lines · Marek Marczykowski-Górecki

Add --replace-escape, -e option to xenconsoled, which replaces ESC with '.' in console output written to log file. This makes it slightly safer to do tail -f on a console output of untrusted guest.
The pty output is unaffected by this option.

I would rather only have the long option, not the short one.

Quoted 5 lines

---
Is there any more documentation to be updated? I didn't found xenconsoled man page or such.

No. But you're welcome to expand the help string in usage() to be more verbose. :-)

Quoted 18 lines
---
 tools/console/daemon/io.c   | 18 ++++++++++++++++++
 tools/console/daemon/main.c |  9 +++++++--
 2 files changed, 25 insertions(+), 2 deletions(-)
tools/console/daemon/io.c +1 -0
index e22009aa39..c8280e0b7c 100644
@@ -68,6 +68,7 @@ extern int log_time_hv;
 extern int log_time_guest;
 extern char *log_dir;
 extern int discard_overflowed_data;
+extern int replace_escape;
 
 static int log_time_hv_needts = 1;
 static int log_time_guest_needts = 1;

tools/console/daemon/io.c static inline int console_iter_int_arg3(struct domain *d, · line 231

Quoted 4 lines
tools/console/daemon/io.c +1 -0
@@ -227,8 +228,25 @@ static inline int console_iter_int_arg3(struct domain *d,
 	return ret;
 }
 
+static void do_replace_escape(const char *buf, char *dest, int len) {

Coding style - { should be on a new line.

tools/console/daemon/io.c static inline int console_iter_int_arg3(struct domain *d, · line 238

Quoted 7 lines
tools/console/daemon/io.c +7 -0
+	int i;
+
+	memcpy(dest, buf, len);
+	for (i = 0; i < len; i++) {
+		if (dest[i] == '\033')
+			dest[i] = '.';
+	}

This could be made more efficient by using:

        for (i = 0; i < len; i++) {
             if (src[i] == '\033')
                 dst[i] = '.';
             else
                 dst[i] = src[i];
	}

You need to go over the array once anyway, you can do without that memcpy.

Wei.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#2 lore raw
Quoted 52 lines · Wei Liu
Quoted 4 lines · Marek Marczykowski-Górecki

Add --replace-escape, -e option to xenconsoled, which replaces ESC with '.' in console output written to log file. This makes it slightly safer to do tail -f on a console output of untrusted guest.
The pty output is unaffected by this option.

I would rather only have the long option, not the short one.

Quoted 5 lines

---
Is there any more documentation to be updated? I didn't found xenconsoled man page or such.

No. But you're welcome to expand the help string in usage() to be more verbose. :-)

Quoted 18 lines
---
 tools/console/daemon/io.c   | 18 ++++++++++++++++++
 tools/console/daemon/main.c |  9 +++++++--
 2 files changed, 25 insertions(+), 2 deletions(-)
tools/console/daemon/io.c +1 -0
index e22009aa39..c8280e0b7c 100644
@@ -68,6 +68,7 @@ extern int log_time_hv;
 extern int log_time_guest;
 extern char *log_dir;
 extern int discard_overflowed_data;
+extern int replace_escape;
 
 static int log_time_hv_needts = 1;
 static int log_time_guest_needts = 1;

tools/console/daemon/io.c static inline int console_iter_int_arg3(struct domain *d, · line 231

Quoted 4 lines
tools/console/daemon/io.c +1 -0
@@ -227,8 +228,25 @@ static inline int console_iter_int_arg3(struct domain *d,
 	return ret;
 }
 
+static void do_replace_escape(const char *buf, char *dest, int len) {

Coding style - { should be on a new line.

tools/console/daemon/io.c static inline int console_iter_int_arg3(struct domain *d, · line 238

Quoted 7 lines
tools/console/daemon/io.c +7 -0
+	int i;
+
+	memcpy(dest, buf, len);
+	for (i = 0; i < len; i++) {
+		if (dest[i] == '\033')
+			dest[i] = '.';
+	}

This could be made more efficient by using:

Quoted 5 lines
        for (i = 0; i < len; i++) {
             if (src[i] == '\033')
                 dst[i] = '.';
             else
                 dst[i] = src[i];

Oh this can even be written in a shorter form:

  dst[i] = src[i] != '\033' ?: '.';

Wei.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#3 lore raw
Quoted 11 lines · Wei Liu
Quoted 1 line · Wei Liu

On Sat, Jul 21, 2018 at 02:14:12AM +0200, Marek Marczykowski-Górecki

Quoted 8 lines · wrote:
Quoted 7 lines
tools/console/daemon/io.c +6 -0
+
+   memcpy(dest, buf, len);
+   for (i = 0; i < len; i++) {
+           if (dest[i] == '\033')
+                   dest[i] = '.';
+   }
Quoted 8 lines
Quoted 7 lines

This could be made more efficient by using:

        for (i = 0; i < len; i++) {
             if (src[i] == '\033')
                 dst[i] = '.';
             else
                 dst[i] = src[i];

The above code doesn't write the value that was checked into the destination buffer; instead it does a second copy from the source buffer. That is a problematic code pattern that we shouldn't really encourage.

Quoted 6 lines

Oh this can even be written in a shorter form:

  dst[i] = src[i] != '\033' ?: '.';

That may be shorter but it is harder to understand, whereas Marek's original code is clear and well structured.

One nit with the proposed patch is the introduction of the dynamically size array on the stack, with input to the function determining its size:

Quoted 3 lines

static int write_all(int fd, const char* buf, size_t len) {
+ char buf_replaced[len];

That might be just fine for userspace code, but would it be better replaced with a fixed size buffer and a loop instead?

Christopher

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#4 lore raw
Quoted 22 lines · Christopher Clark
Quoted 11 lines · Wei Liu
Quoted 1 line · Wei Liu

On Sat, Jul 21, 2018 at 02:14:12AM +0200, Marek Marczykowski-Górecki

Quoted 8 lines · wrote:
Quoted 7 lines
tools/console/daemon/io.c +6 -0
+
+   memcpy(dest, buf, len);
+   for (i = 0; i < len; i++) {
+           if (dest[i] == '\033')
+                   dest[i] = '.';
+   }
Quoted 8 lines
Quoted 7 lines

This could be made more efficient by using:

        for (i = 0; i < len; i++) {
             if (src[i] == '\033')
                 dst[i] = '.';
             else
                 dst[i] = src[i];
Quoted 3 lines

The above code doesn't write the value that was checked into the destination buffer; instead it does a second copy from the source buffer. That is a problematic code pattern that we shouldn't really encourage.

Because you worry about TOCTOU attack? But both src and dst are under xenconsoled's control.

Wei.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel