Skip to contentSkip to conversation

Miscellaneous patches for 2019-05-22

GIT PULL Applied Markus Armbruster 2 participants 8 messages May 22, 2019 – May 30, 2019

lore
Share & tools
lore
#0 lore raw

The following changes since commit a4f667b6714916683408b983cfe0a615a725775f:

Merge remote-tracking branch 'remotes/cohuck/tags/s390x-20190521-3' into staging (2019-05-21 16:30:13 +0100)

are available in the Git repository at:

git://repo.or.cz/qemu/armbru.git tags/pull-misc-2019-05-22

for you to fetch changes up to db3d11ee3f0cb851124830172f0a93c3d77a450a:

cutils: Simplify how parse_uint() checks for whitespace (2019-05-22 15:00:04 +0200)

----------------------------------------------------------------
Miscellaneous patches for 2019-05-22

----------------------------------------------------------------
Markus Armbruster (5):
qemu-bridge-helper: Fix misuse of isspace() tests/vhost-user-bridge: Fix misuse of isdigit()
gdbstub: Reject invalid RLE repeat counts
gdbstub: Fix misuse of isxdigit()
cutils: Simplify how parse_uint() checks for whitespace

 gdbstub.c                 | 20 ++++++++++++--------
 qemu-bridge-helper.c      |  6 +++---
 tests/vhost-user-bridge.c |  3 ++-
 util/cutils.c             |  2 +-
 4 files changed, 18 insertions(+), 13 deletions(-)
Signature · 1 line
2.17.2
#1 lore raw

[Qemu-devel] [PULL 3/5] gdbstub: Reject invalid RLE repeat counts

"Debugging with GDB / Appendix E GDB Remote Serial Protocol / Overview" specifies "The printable characters '#' and '$' or with a numeric value greater than 126 must not be used." gdb_read_byte() only rejects values < 32. This is wrong. Impact depends on the caller:

* gdb_handlesig() passes a char. Incorrectly accepts '#', '$' and '\127'.

* gdb_chr_receive() passes an uint8_t. Additionally accepts characters with the most-significant bit set.

Correct the validity check to match the specification.

Message-Id: <[email protected]>

---
 gdbstub.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
gdbstub.c +5 -1
index d54abd17cc..c41eb1de07 100644
@@ -2064,7 +2064,11 @@ static void gdb_read_byte(GDBState *s, int ch)
             }
             break;
         case RS_GETLINE_RLE:
-            if (ch < ' ') {
+            /*
+             * Run-length encoding is explained in "Debugging with GDB /
+             * Appendix E GDB Remote Serial Protocol / Overview".
+             */
+            if (ch < ' ' || ch == '#' || ch == '$' || ch > 126) {
                 /* invalid RLE count encoding */
                 trace_gdbstub_err_invalid_repeat((uint8_t)ch);
                 s->state = RS_GETLINE;
Signature · 1 line
2.17.2
#2 lore raw

[Qemu-devel] [PULL 1/5] qemu-bridge-helper: Fix misuse of isspace()

In reply to Markus Armbruster

parse_acl_file() passes char values to isspace(). Undefined behavior when the value is negative. Not a security issue, because the characters come from trusted $prefix/etc/qemu/bridge.conf and the files it includes.

Furthermore, isspace()'s locale-dependence means qemu-bridge-helper uses the user's locale for parsing $prefix/etc/bridge.conf. Feels wrong.

Use g_ascii_isspace() instead. This fixes the undefined behavior, and makes parsing of $prefix/etc/bridge.conf locale-independent.

Message-Id: <[email protected]>

---
 qemu-bridge-helper.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
qemu-bridge-helper.c +3 -3
index 5396fbfbb6..f9940deefd 100644
@@ -75,7 +75,7 @@ static int parse_acl_file(const char *filename, ACLList *acl_list)
         char *ptr = line;
         char *cmd, *arg, *argend;
 
-        while (isspace(*ptr)) {
+        while (g_ascii_isspace(*ptr)) {
             ptr++;
         }
 
@@ -99,12 +99,12 @@ static int parse_acl_file(const char *filename, ACLList *acl_list)
 
         *arg = 0;
         arg++;
-        while (isspace(*arg)) {
+        while (g_ascii_isspace(*arg)) {
             arg++;
         }
 
         argend = arg + strlen(arg);
-        while (arg != argend && isspace(*(argend - 1))) {
+        while (arg != argend && g_ascii_isspace(*(argend - 1))) {
             argend--;
         }
         *argend = 0;
Signature · 1 line
2.17.2
#3 lore raw

Re: [Qemu-devel] [PULL 1/5] qemu-bridge-helper: Fix misuse of isspace()

Quoted 10 lines · Markus Armbruster

parse_acl_file() passes char values to isspace(). Undefined behavior when the value is negative. Not a security issue, because the characters come from trusted $prefix/etc/qemu/bridge.conf and the files it includes.

Furthermore, isspace()'s locale-dependence means qemu-bridge-helper uses the user's locale for parsing $prefix/etc/bridge.conf. Feels wrong.

Quoted 8 lines

Use g_ascii_isspace() instead. This fixes the undefined behavior, and makes parsing of $prefix/etc/bridge.conf locale-independent.

Message-Id: <[email protected]>

---
 qemu-bridge-helper.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

Coverity complains about this change (CID 1401706) because it doesn't have enough information to know that the table lookup g_ascii_isspace does is always safe:

tainted_data: Using tainted variable
(guchar)*(argend - 1) as an index to pointer g_ascii_table.

We know this is OK because we know the table is big enough that a guchar index can't possibly overrun, but because the table is declared in the glib header file as
GLIB_VAR const guint16 * const g_ascii_table;
Coverity has no idea of its size and is being pessimistic.

I've squashed the Coverity issue as a false-positive, but I mention it here in case you thought it worth trying to write something in coverity-model.c to provide a better model of the glib function.

thanks
-- PMM

#4 lore raw

[Qemu-devel] [PULL 4/5] gdbstub: Fix misuse of isxdigit()

In reply to Markus Armbruster

gdb_read_byte() passes its @ch argument to isxdigit(). Undefined behavior when the value is negative. Two callers:

* gdb_chr_receive() passes an uint8_t value. Safe.

* gdb_handlesig() a char value. Unsafe. Not a security issue, because the characters come from the gdb client, which is trusted.

The obvious fix would be casting @ch to unsigned char. But note that gdb_read_byte() already casts @ch to uint8_t in many places. Uses of @ch without such a cast:

(1) Compare to a character constant with == or !=

(2) s->linesum += ch

(3) Store ch or ch ^ 0x20 into s->line_buf[]

(4) Check for invalid RLE count:

    ch < ' ' || ch == '#' || ch == '$' || ch > 126

(5) Pass to isxdigit()

(6) Pass to fromhex()

Change the parameter type from int to uint8_t, and drop the now redundant casts. Affects the above uses as follows:

(1) No change: the character constants are all non-negative.

(2) Effectively no change: we only ever use s->linesum & 0xff, and

    s->linesum is int.

(3) No change: s->line_buf[] is char[].

(4) No change.

(5) Avoid undefined behavior.

(6) No change: only reached when isxdigit(ch)

Message-Id: <[email protected]>

---
 gdbstub.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
gdbstub.c +7 -7
index c41eb1de07..b129df4e59 100644
@@ -1987,7 +1987,7 @@ void gdb_do_syscall(gdb_syscall_complete_cb cb, const char *fmt, ...)
     va_end(va);
 }
 
-static void gdb_read_byte(GDBState *s, int ch)
+static void gdb_read_byte(GDBState *s, uint8_t ch)
 {
     uint8_t reply;
 
@@ -2001,7 +2001,7 @@ static void gdb_read_byte(GDBState *s, int ch)
         } else if (ch == '+') {
             trace_gdbstub_io_got_ack();
         } else {
-            trace_gdbstub_io_got_unexpected((uint8_t)ch);
+            trace_gdbstub_io_got_unexpected(ch);
         }
 
         if (ch == '+' || ch == '$')
@@ -2024,7 +2024,7 @@ static void gdb_read_byte(GDBState *s, int ch)
                 s->line_sum = 0;
                 s->state = RS_GETLINE;
             } else {
-                trace_gdbstub_err_garbage((uint8_t)ch);
+                trace_gdbstub_err_garbage(ch);
             }
             break;
         case RS_GETLINE:
@@ -2070,11 +2070,11 @@ static void gdb_read_byte(GDBState *s, int ch)
              */
             if (ch < ' ' || ch == '#' || ch == '$' || ch > 126) {
                 /* invalid RLE count encoding */
-                trace_gdbstub_err_invalid_repeat((uint8_t)ch);
+                trace_gdbstub_err_invalid_repeat(ch);
                 s->state = RS_GETLINE;
             } else {
                 /* decode repeat length */
-                int repeat = (unsigned char)ch - ' ' + 3;
+                int repeat = ch - ' ' + 3;
                 if (s->line_buf_index + repeat >= sizeof(s->line_buf) - 1) {
                     /* that many repeats would overrun the command buffer */
                     trace_gdbstub_err_overrun();
@@ -2096,7 +2096,7 @@ static void gdb_read_byte(GDBState *s, int ch)
         case RS_CHKSUM1:
             /* get high hex digit of checksum */
             if (!isxdigit(ch)) {
-                trace_gdbstub_err_checksum_invalid((uint8_t)ch);
+                trace_gdbstub_err_checksum_invalid(ch);
                 s->state = RS_GETLINE;
                 break;
             }
@@ -2107,7 +2107,7 @@ static void gdb_read_byte(GDBState *s, int ch)
         case RS_CHKSUM2:
             /* get low hex digit of checksum */
             if (!isxdigit(ch)) {
-                trace_gdbstub_err_checksum_invalid((uint8_t)ch);
+                trace_gdbstub_err_checksum_invalid(ch);
                 s->state = RS_GETLINE;
                 break;
             }
Signature · 1 line
2.17.2
#5 lore raw

[Qemu-devel] [PULL 2/5] tests/vhost-user-bridge: Fix misuse of isdigit()

In reply to Markus Armbruster

vubr_set_host() passes char values to isdigit(). Undefined behavior when the value is negative.

Fix by using qemu_isdigit() instead.

Message-Id: <[email protected]>

[Missing #include "qemu-common.h" fixed]
---
 tests/vhost-user-bridge.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
tests/vhost-user-bridge.c +2 -1
index 0033b61f2e..5b771de7a3 100644
@@ -30,6 +30,7 @@
 #define _FILE_OFFSET_BITS 64
 
 #include "qemu/osdep.h"
+#include "qemu-common.h"
 #include "qemu/atomic.h"
 #include "qemu/iov.h"
 #include "standard-headers/linux/virtio_net.h"
@@ -645,7 +646,7 @@ vubr_host_notifier_setup(VubrDev *dev)
 static void
 vubr_set_host(struct sockaddr_in *saddr, const char *host)
 {
-    if (isdigit(host[0])) {
+    if (qemu_isdigit(host[0])) {
         if (!inet_aton(host, &saddr->sin_addr)) {
             fprintf(stderr, "inet_aton() failed.\n");
             exit(1);
Signature · 1 line
2.17.2
#6 lore raw

[Qemu-devel] [PULL 5/5] cutils: Simplify how parse_uint() checks for whitespace

In reply to Markus Armbruster

Use qemu_isspace() so we don't have to cast to unsigned char.

Message-Id: <[email protected]>

---
 util/cutils.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
util/cutils.c +1 -1
index d682c90901..9aacc422ca 100644
@@ -683,7 +683,7 @@ int parse_uint(const char *s, unsigned long long *value, char **endptr,
     }
 
     /* make sure we reject negative numbers: */
-    while (isspace((unsigned char)*s)) {
+    while (qemu_isspace(*s)) {
         s++;
     }
     if (*s == '-') {
Signature · 1 line
2.17.2
#7 lore raw
In reply to Markus Armbruster
Quoted 24 lines · Markus Armbruster

The following changes since commit a4f667b6714916683408b983cfe0a615a725775f:

Merge remote-tracking branch 'remotes/cohuck/tags/s390x-20190521-3' into staging (2019-05-21 16:30:13 +0100)

are available in the Git repository at:

git://repo.or.cz/qemu/armbru.git tags/pull-misc-2019-05-22

for you to fetch changes up to db3d11ee3f0cb851124830172f0a93c3d77a450a:

cutils: Simplify how parse_uint() checks for whitespace (2019-05-22 15:00:04 +0200)

----------------------------------------------------------------
Miscellaneous patches for 2019-05-22

----------------------------------------------------------------
Markus Armbruster (5):
qemu-bridge-helper: Fix misuse of isspace() tests/vhost-user-bridge: Fix misuse of isdigit()
gdbstub: Reject invalid RLE repeat counts
gdbstub: Fix misuse of isxdigit()
cutils: Simplify how parse_uint() checks for whitespace

Quoted 5 lines
 gdbstub.c                 | 20 ++++++++++++--------
 qemu-bridge-helper.c      |  6 +++---
 tests/vhost-user-bridge.c |  3 ++-
 util/cutils.c             |  2 +-
 4 files changed, 18 insertions(+), 13 deletions(-)

Applied, thanks.

Please update the changelog at https://wiki.qemu.org/ChangeLog/4.1
for any user-visible changes.

-- PMM