4bff0a
From 709214f554355158b2c3e70c7f3424997e002cee Mon Sep 17 00:00:00 2001
4bff0a
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
4bff0a
Date: Thu, 23 Aug 2018 14:48:40 +0200
4bff0a
Subject: [PATCH] bus-message: avoid wrap-around when using length read from
4bff0a
 message
4bff0a
4bff0a
We would read (-1), and then add 1 to it, call message_peek_body(..., 0, ...),
4bff0a
and when trying to make use of the data.
4bff0a
4bff0a
The fuzzer test case is just for one site, but they all look similar.
4bff0a
4bff0a
v2: fix two UINT8_MAX/UINT32_MAX mismatches founds by LGTM
4bff0a
(cherry picked from commit 902000c19830f5e5a96e8948d691b42e91ecb1e7)
4bff0a
4bff0a
Resolves: #1696224
4bff0a
---
4bff0a
 src/libsystemd/sd-bus/bus-message.c           |  24 ++++++++++++++++++
4bff0a
 ...h-603dfd98252375ac7dbced53c2ec312671939a36 | Bin 0 -> 40 bytes
4bff0a
 2 files changed, 24 insertions(+)
4bff0a
 create mode 100644 test/fuzz/fuzz-bus-message/crash-603dfd98252375ac7dbced53c2ec312671939a36
4bff0a
4bff0a
diff --git a/src/libsystemd/sd-bus/bus-message.c b/src/libsystemd/sd-bus/bus-message.c
4bff0a
index 613722a1a0..53cbd675b7 100644
4bff0a
--- a/src/libsystemd/sd-bus/bus-message.c
4bff0a
+++ b/src/libsystemd/sd-bus/bus-message.c
4bff0a
@@ -3414,6 +3414,10 @@ _public_ int sd_bus_message_read_basic(sd_bus_message *m, char type, void *p) {
4bff0a
                                 return r;
4bff0a
 
4bff0a
                         l = BUS_MESSAGE_BSWAP32(m, *(uint32_t*) q);
4bff0a
+                        if (l == UINT32_MAX)
4bff0a
+                                /* avoid overflow right below */
4bff0a
+                                return -EBADMSG;
4bff0a
+
4bff0a
                         r = message_peek_body(m, &rindex, 1, l+1, &q);
4bff0a
                         if (r < 0)
4bff0a
                                 return r;
4bff0a
@@ -3436,6 +3440,10 @@ _public_ int sd_bus_message_read_basic(sd_bus_message *m, char type, void *p) {
4bff0a
                                 return r;
4bff0a
 
4bff0a
                         l = *(uint8_t*) q;
4bff0a
+                        if (l == UINT8_MAX)
4bff0a
+                                /* avoid overflow right below */
4bff0a
+                                return -EBADMSG;
4bff0a
+
4bff0a
                         r = message_peek_body(m, &rindex, 1, l+1, &q);
4bff0a
                         if (r < 0)
4bff0a
                                 return r;
4bff0a
@@ -3701,6 +3709,10 @@ static int bus_message_enter_variant(
4bff0a
                         return r;
4bff0a
 
4bff0a
                 l = *(uint8_t*) q;
4bff0a
+                if (l == UINT8_MAX)
4bff0a
+                        /* avoid overflow right below */
4bff0a
+                        return -EBADMSG;
4bff0a
+
4bff0a
                 r = message_peek_body(m, &rindex, 1, l+1, &q);
4bff0a
                 if (r < 0)
4bff0a
                         return r;
4bff0a
@@ -4269,6 +4281,10 @@ _public_ int sd_bus_message_peek_type(sd_bus_message *m, char *type, const char
4bff0a
                                         return r;
4bff0a
 
4bff0a
                                 l = *(uint8_t*) q;
4bff0a
+                                if (l == UINT8_MAX)
4bff0a
+                                        /* avoid overflow right below */
4bff0a
+                                        return -EBADMSG;
4bff0a
+
4bff0a
                                 r = message_peek_body(m, &rindex, 1, l+1, &q);
4bff0a
                                 if (r < 0)
4bff0a
                                         return r;
4bff0a
@@ -4849,6 +4865,10 @@ static int message_peek_field_string(
4bff0a
                 if (r < 0)
4bff0a
                         return r;
4bff0a
 
4bff0a
+                if (l == UINT32_MAX)
4bff0a
+                        /* avoid overflow right below */
4bff0a
+                        return -EBADMSG;
4bff0a
+
4bff0a
                 r = message_peek_fields(m, ri, 1, l+1, &q);
4bff0a
                 if (r < 0)
4bff0a
                         return r;
4bff0a
@@ -4900,6 +4920,10 @@ static int message_peek_field_signature(
4bff0a
                         return r;
4bff0a
 
4bff0a
                 l = *(uint8_t*) q;
4bff0a
+                if (l == UINT8_MAX)
4bff0a
+                        /* avoid overflow right below */
4bff0a
+                        return -EBADMSG;
4bff0a
+
4bff0a
                 r = message_peek_fields(m, ri, 1, l+1, &q);
4bff0a
                 if (r < 0)
4bff0a
                         return r;
4bff0a
diff --git a/test/fuzz/fuzz-bus-message/crash-603dfd98252375ac7dbced53c2ec312671939a36 b/test/fuzz/fuzz-bus-message/crash-603dfd98252375ac7dbced53c2ec312671939a36
4bff0a
new file mode 100644
4bff0a
index 0000000000000000000000000000000000000000..b3fee9e07af4f925697a549bbc8ffc03a277fac0
4bff0a
GIT binary patch
4bff0a
literal 40
4bff0a
mcmc~{Vqjzdg7laF|BC@>cE)0c{}2$`*K@IKT2AZ~5ElR}@e}O;
4bff0a
4bff0a
literal 0
4bff0a
HcmV?d00001
4bff0a