git: f4d580a54734 - stable/14 - bhyve: Escape binary data sent in reply packets

From: Mark Johnston <markj_at_FreeBSD.org>
Date: Thu, 15 Aug 2024 14:30:01 UTC
The branch stable/14 has been updated by markj:

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

commit f4d580a547341362cacc79383a24e51c6875ff9a
Author:     Mark Johnston <markj@FreeBSD.org>
AuthorDate: 2024-07-31 22:41:13 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2024-08-15 14:29:49 +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
    
    (cherry picked from commit b4fb947923b566a3a8a6ad8e5f8ea8dec0c68ee4)
---
 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 0319aaf0a9cd..abdc64a4a75a 100644
--- a/usr.sbin/bhyve/gdb.c
+++ b/usr.sbin/bhyve/gdb.c
@@ -564,6 +564,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)
 {
@@ -1675,10 +1697,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);