git: b4fb947923b5 - main - bhyve: Escape binary data sent in reply packets

From: Mark Johnston <markj_at_FreeBSD.org>
Date: Wed, 31 Jul 2024 23:44:39 UTC
The branch main has been updated by markj:

URL: https://cgit.FreeBSD.org/src/commit/?id=b4fb947923b566a3a8a6ad8e5f8ea8dec0c68ee4

commit b4fb947923b566a3a8a6ad8e5f8ea8dec0c68ee4
Author:     Mark Johnston <markj@FreeBSD.org>
AuthorDate: 2024-07-31 22:41:13 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2024-07-31 22:41:13 +0000

    bhyve: Escape binary data sent in reply packets
    
    Per https://sourceware.org/gdb/current/onlinedocs/gdb.html/Overview.html#Binary-Data
    certain bytes must be escaped.  The XML register definitions we have so far do
    not run afoul of that rule, but the stub should handle them anyway.
    
    Reviewed by:    jhb
    MFC after:      2 weeks
    Sponsored by:   Innovate UK
    Differential Revision:  https://reviews.freebsd.org/D46194
---
 usr.sbin/bhyve/gdb.c | 26 ++++++++++++++++++++++++--
 1 file changed, 24 insertions(+), 2 deletions(-)

diff --git a/usr.sbin/bhyve/gdb.c b/usr.sbin/bhyve/gdb.c
index a13166e1e2c8..7c04b0017c80 100644
--- a/usr.sbin/bhyve/gdb.c
+++ b/usr.sbin/bhyve/gdb.c
@@ -651,6 +651,28 @@ append_packet_data(const uint8_t *data, size_t len)
 	}
 }
 
+static void
+append_binary_data(const uint8_t *data, size_t len)
+{
+	uint8_t buf[2];
+
+	for (; len > 0; data++, len--) {
+		switch (*data) {
+		case '}':
+		case '#':
+		case '$':
+		case '*':
+			buf[0] = 0x7d;
+			buf[1] = *data ^ 0x20;
+			append_packet_data(buf, 2);
+			break;
+		default:
+			append_packet_data(data, 1);
+			break;
+		}
+	}
+}
+
 static void
 append_string(const char *str)
 {
@@ -1801,10 +1823,10 @@ gdb_query(const uint8_t *data, size_t len)
 			append_char('l');
 		} else if (doff + dlen >= xmllen) {
 			append_char('l');
-			append_packet_data(xml + doff, xmllen - doff);
+			append_binary_data(xml + doff, xmllen - doff);
 		} else {
 			append_char('m');
-			append_packet_data(xml + doff, dlen);
+			append_binary_data(xml + doff, dlen);
 		}
 		finish_packet();
 		(void)munmap(__DECONST(void *, xml), xmllen);