Skip to contentSkip to conversation

Fix misuse of ctype.h functions

PATCH In review Markus Armbruster 4 participants 15 messages May 14, 2019 – May 22, 2019

lore
Share & tools
lore
#0 lore raw

v2 cover Fix misuse of ctype.h functions

v2:
* PATCH 1: Use g_ascii_isspace(), adapt commit message [Philippe]
* PATCH 3: Add comment pointing to the GDB manual [Philippe]
* PATCH 5: Improve commit message [Thomas]

Markus Armbruster (6):
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() pc-bios/s390-ccw: Clean up harmless misuse of isdigit()
cutils: Simplify how parse_uint() checks for whitespace

 gdbstub.c                 | 20 ++++++++++++--------
 pc-bios/s390-ccw/libc.c   |  2 +-
 pc-bios/s390-ccw/menu.c   |  2 +-
 qemu-bridge-helper.c      |  6 +++---
 tests/vhost-user-bridge.c |  2 +-
 util/cutils.c             |  2 +-
 6 files changed, 19 insertions(+), 15 deletions(-)
Signature · 1 line
2.17.2
#1 lore raw

PATCH v2 1/6 qemu-bridge-helper: Fix misuse of isspace()

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.

---
 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
#2 lore raw

PATCH v2 2/6 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.

---
 tests/vhost-user-bridge.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tests/vhost-user-bridge.c +1 -1
index 0033b61f2e..d70b107ebc 100644
@@ -645,7 +645,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
Reviewed-by on v2 2/6
#3 lore raw

Re: [Qemu-devel] [PATCH v2 2/6] tests/vhost-user-bridge: Fix misuse of isdigit()

Quoted 1 line · Markus Armbruster

vubr_set_host() passes char values to isdigit(). Undefined behavior

"happens"?

Quoted 15 lines

when the value is negative.

Fix by using qemu_isdigit() instead.

---
 tests/vhost-user-bridge.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tests/vhost-user-bridge.c +0 -0
index 0033b61f2e..d70b107ebc 100644

tests/vhost-user-bridge.c vubr_host_notifier_setup(VubrDev *dev) · line 651

Quoted 8 lines
tests/vhost-user-bridge.c +1 -1
@@ -645,7 +645,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);
Reviewed-by on v2 2/6
#4 lore raw

Re: [Qemu-devel] [PATCH v2 2/6] tests/vhost-user-bridge: Fix misuse of isdigit()

In reply to Markus Armbruster
Quoted 15 lines · Markus Armbruster

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

Fix by using qemu_isdigit() instead.

---
 tests/vhost-user-bridge.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tests/vhost-user-bridge.c +0 -0
index 0033b61f2e..d70b107ebc 100644

tests/vhost-user-bridge.c vubr_host_notifier_setup(VubrDev *dev) · line 651

Quoted 8 lines
tests/vhost-user-bridge.c +1 -1
@@ -645,7 +645,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);
#5 lore raw

Re: [Qemu-devel] [PATCH v2 2/6] tests/vhost-user-bridge: Fix misuse of isdigit()

Quoted 11 lines · Thomas Huth
Quoted 10 lines · Markus Armbruster

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

Fix by using qemu_isdigit() instead.

---
 tests/vhost-user-bridge.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 15 lines

tests/vhost-user-bridge.c vubr_host_notifier_setup(VubrDev *dev) · line 651

Quoted 13 lines
tests/vhost-user-bridge.c +1 -1
index 0033b61f2e..d70b107ebc 100644
@@ -645,7 +645,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);

By the way, how do you compile / run this test? The original commit message say one should compile it with "make tests/vhost-user-bridge" but that does not work for me:

$ make tests/vhost-user-bridge
cc     tests/vhost-user-bridge.c   -o tests/vhost-user-bridge
tests/vhost-user-bridge.c:32:24: fatal error: qemu/osdep.h: No such file
or directory

Thomas

#6 lore raw

Re: [Qemu-devel] [PATCH v2 2/6] tests/vhost-user-bridge: Fix misuse of isdigit()

Quoted 12 lines · Thomas Huth
Quoted 11 lines · Thomas Huth
Quoted 10 lines · Markus Armbruster

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

Fix by using qemu_isdigit() instead.

---
 tests/vhost-user-bridge.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 4 lines
Quoted 4 lines

tests/vhost-user-bridge.c

Quoted 4 lines
tests/vhost-user-bridge.c +0 -0
index 0033b61f2e..d70b107ebc 100644

Squashing in

    @@ -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"
Quoted 12 lines
Quoted 11 lines

tests/vhost-user-bridge.c vubr_host_notifier_setup(VubrDev *dev) · line 651

Quoted 9 lines
tests/vhost-user-bridge.c +1 -1
@@ -645,7 +645,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);
Quoted 8 lines

By the way, how do you compile / run this test? The original commit message say one should compile it with "make tests/vhost-user-bridge" but that does not work for me:

$ make tests/vhost-user-bridge
cc     tests/vhost-user-bridge.c   -o tests/vhost-user-bridge
tests/vhost-user-bridge.c:32:24: fatal error: qemu/osdep.h: No such file
or directory

With that fixup, it compiles for me.

Thanks for your question! I blindly assumed "make check" actually compiled this.

#7 lore raw

PATCH v2 3/6 gdbstub: Reject invalid RLE repeat counts

In reply to Markus Armbruster

"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.

---
 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
#8 lore raw

PATCH v2 4/6 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)

---
 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
#9 lore raw

PATCH v2 5/6 pc-bios/s390-ccw: Clean up harmless misuse of isdigit()

In reply to Markus Armbruster

atoui() and get_index() pass char values to isdigit(). With a standard isdigit(), we'd get undefined behavior when the value is negative. Can't happen as char is unsigned on s390x. Even if it could, we're actually using isdigit() from pc-bios/s390-ccw/libc.h here, which works fine for negative values. Clean up anyway, just to avoid setting a bad example.

Cc Christian Borntraeger <[email protected]>
Cc Thomas Huth <[email protected]>
Cc Cornelia Huck <[email protected]>
---
 pc-bios/s390-ccw/libc.c | 2 +-
 pc-bios/s390-ccw/menu.c | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
pc-bios/s390-ccw/libc.c +1 -1
index a786566c4c..3187923950 100644
@@ -38,7 +38,7 @@ uint64_t atoui(const char *str)
     }
 
     while (*str) {
-        if (!isdigit(*str)) {
+        if (!isdigit(*(unsigned char *)str)) {
             break;
         }
         val = val * 10 + *str - '0';
pc-bios/s390-ccw/menu.c +1 -1
index 82a4ae6315..ce3815b201 100644
@@ -134,7 +134,7 @@ static int get_index(void)
 
     /* Check for erroneous input */
     for (i = 0; i < len; i++) {
-        if (!isdigit(buf[i])) {
+        if (!isdigit((unsigned char)buf[i])) {
             return -1;
         }
     }
Signature · 1 line
2.17.2
Acked-by on v2 5/6
#10 lore raw

Re: [Qemu-devel] [PATCH v2 5/6] pc-bios/s390-ccw: Clean up harmless misuse of isdigit()

Quoted 8 lines · Markus Armbruster

atoui() and get_index() pass char values to isdigit(). With a standard isdigit(), we'd get undefined behavior when the value is negative. Can't happen as char is unsigned on s390x. Even if it could, we're actually using isdigit() from pc-bios/s390-ccw/libc.h here, which works fine for negative values. Clean up anyway, just to avoid setting a bad example.

Cc Christian Borntraeger <[email protected]>
Quoted 36 lines
---
 pc-bios/s390-ccw/libc.c | 2 +-
 pc-bios/s390-ccw/menu.c | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
pc-bios/s390-ccw/libc.c +1 -1
index a786566c4c..3187923950 100644
@@ -38,7 +38,7 @@ uint64_t atoui(const char *str)
     }
 
     while (*str) {
-        if (!isdigit(*str)) {
+        if (!isdigit(*(unsigned char *)str)) {
             break;
         }
         val = val * 10 + *str - '0';
pc-bios/s390-ccw/menu.c +1 -1
index 82a4ae6315..ce3815b201 100644
@@ -134,7 +134,7 @@ static int get_index(void)
 
     /* Check for erroneous input */
     for (i = 0; i < len; i++) {
-        if (!isdigit(buf[i])) {
+        if (!isdigit((unsigned char)buf[i])) {
             return -1;
         }
     }
#11 lore raw

Re: [Qemu-devel] [PATCH v2 5/6] pc-bios/s390-ccw: Clean up harmless misuse of isdigit()

Quoted 10 lines · Christian Borntraeger
Quoted 7 lines · Markus Armbruster

atoui() and get_index() pass char values to isdigit(). With a standard isdigit(), we'd get undefined behavior when the value is negative. Can't happen as char is unsigned on s390x. Even if it could, we're actually using isdigit() from pc-bios/s390-ccw/libc.h here, which works fine for negative values. Clean up anyway, just to avoid setting a bad example.

Quoted 2 lines
Quoted 1 line
Cc Christian Borntraeger <[email protected]>

FYI, this patch is already queued in Cornelia's s390-next tree (and it should also go via her tree, since you need to rebuild the bios binary afterwards).

Thomas

#12 lore raw

PATCH v2 6/6 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.

---
 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
Reviewed-by on v2 6/6
#13 lore raw

Re: [Qemu-devel] [PATCH v2 6/6] cutils: Simplify how parse_uint() checks for whitespace

Quoted 13 lines · Markus Armbruster

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

---
 util/cutils.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
util/cutils.c +0 -0
index d682c90901..9aacc422ca 100644

util/cutils.c int parse_uint(const char *s, unsigned long long *value, char **endptr, · line 689

Quoted 8 lines
util/cutils.c +1 -1
@@ -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 == '-') {