ryantimwilson / rpms / systemd

Forked from rpms/systemd a month ago
Clone
ff6046
From a2da2b45ac05ef91074e90097115e8c734ca0f64 Mon Sep 17 00:00:00 2001
ff6046
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
ff6046
Date: Mon, 9 Jul 2018 10:52:51 +0200
ff6046
Subject: [PATCH] bus-message: avoid an infinite loop on empty structures
ff6046
ff6046
The alternative would be to treat gvariant and !gvariant messages differently.
ff6046
But this is a problem because we check signatures is variuos places before we
ff6046
have an actual message, for example in sd_bus_add_object_vtable(). It seems
ff6046
better to treat things consistent (i.e. follow the lowest common denominator)
ff6046
and disallow empty structures everywhere.
ff6046
ff6046
(cherry picked from commit ec6bda56cbca9509b1abde1122645630caca877c)
ff6046
ff6046
Resolves: #1696224
ff6046
---
ff6046
 src/libsystemd/sd-bus/bus-message.c               |   3 ++-
ff6046
 src/libsystemd/sd-bus/bus-signature.c             |   6 ++++++
ff6046
 src/libsystemd/sd-bus/test-bus-gvariant.c         |   6 +++---
ff6046
 src/libsystemd/sd-bus/test-bus-marshal.c          |   6 +++---
ff6046
 src/libsystemd/sd-bus/test-bus-signature.c        |   8 ++++----
ff6046
 ...crash-26bba7182dedc8848939931d9fcefcb7922f2e56 | Bin 0 -> 157 bytes
ff6046
 ...meout-08ee8f6446a4064db064e8e0b3d220147f7d0b5b | Bin 0 -> 534 bytes
ff6046
 7 files changed, 18 insertions(+), 11 deletions(-)
ff6046
 create mode 100644 test/fuzz/fuzz-bus-message/crash-26bba7182dedc8848939931d9fcefcb7922f2e56
ff6046
 create mode 100644 test/fuzz/fuzz-bus-message/timeout-08ee8f6446a4064db064e8e0b3d220147f7d0b5b
ff6046
ff6046
diff --git a/src/libsystemd/sd-bus/bus-message.c b/src/libsystemd/sd-bus/bus-message.c
ff6046
index 7f87d018fb..1d06fcb80e 100644
ff6046
--- a/src/libsystemd/sd-bus/bus-message.c
ff6046
+++ b/src/libsystemd/sd-bus/bus-message.c
ff6046
@@ -4178,6 +4178,7 @@ _public_ int sd_bus_message_peek_type(sd_bus_message *m, char *type, const char
ff6046
 
ff6046
                         /* signature_element_length does verification internally */
ff6046
 
ff6046
+                        /* The array element must not be empty */
ff6046
                         assert(l >= 1);
ff6046
                         if (free_and_strndup(&c->peeked_signature,
ff6046
                                              c->signature + c->index + 1, l) < 0)
ff6046
@@ -4201,7 +4202,7 @@ _public_ int sd_bus_message_peek_type(sd_bus_message *m, char *type, const char
ff6046
                         if (r < 0)
ff6046
                                 return r;
ff6046
 
ff6046
-                        assert(l >= 2);
ff6046
+                        assert(l >= 3);
ff6046
                         if (free_and_strndup(&c->peeked_signature,
ff6046
                                              c->signature + c->index + 1, l - 2) < 0)
ff6046
                                 return -ENOMEM;
ff6046
diff --git a/src/libsystemd/sd-bus/bus-signature.c b/src/libsystemd/sd-bus/bus-signature.c
ff6046
index 18c91e8707..1ca37cbb5a 100644
ff6046
--- a/src/libsystemd/sd-bus/bus-signature.c
ff6046
+++ b/src/libsystemd/sd-bus/bus-signature.c
ff6046
@@ -58,6 +58,12 @@ static int signature_element_length_internal(
ff6046
                         p += t;
ff6046
                 }
ff6046
 
ff6046
+                if (p - s < 2)
ff6046
+                        /* D-Bus spec: Empty structures are not allowed; there
ff6046
+                         * must be at least one type code between the parentheses.
ff6046
+                         */
ff6046
+                        return -EINVAL;
ff6046
+
ff6046
                 *l = p - s + 1;
ff6046
                 return 0;
ff6046
         }
ff6046
diff --git a/src/libsystemd/sd-bus/test-bus-gvariant.c b/src/libsystemd/sd-bus/test-bus-gvariant.c
ff6046
index 75804f3458..e606970a3a 100644
ff6046
--- a/src/libsystemd/sd-bus/test-bus-gvariant.c
ff6046
+++ b/src/libsystemd/sd-bus/test-bus-gvariant.c
ff6046
@@ -19,7 +19,7 @@
ff6046
 
ff6046
 static void test_bus_gvariant_is_fixed_size(void) {
ff6046
         assert_se(bus_gvariant_is_fixed_size("") > 0);
ff6046
-        assert_se(bus_gvariant_is_fixed_size("()") > 0);
ff6046
+        assert_se(bus_gvariant_is_fixed_size("()") == -EINVAL);
ff6046
         assert_se(bus_gvariant_is_fixed_size("y") > 0);
ff6046
         assert_se(bus_gvariant_is_fixed_size("u") > 0);
ff6046
         assert_se(bus_gvariant_is_fixed_size("b") > 0);
ff6046
@@ -44,7 +44,7 @@ static void test_bus_gvariant_is_fixed_size(void) {
ff6046
 
ff6046
 static void test_bus_gvariant_get_size(void) {
ff6046
         assert_se(bus_gvariant_get_size("") == 0);
ff6046
-        assert_se(bus_gvariant_get_size("()") == 1);
ff6046
+        assert_se(bus_gvariant_get_size("()") == -EINVAL);
ff6046
         assert_se(bus_gvariant_get_size("y") == 1);
ff6046
         assert_se(bus_gvariant_get_size("u") == 4);
ff6046
         assert_se(bus_gvariant_get_size("b") == 1);
ff6046
@@ -76,7 +76,7 @@ static void test_bus_gvariant_get_size(void) {
ff6046
 
ff6046
 static void test_bus_gvariant_get_alignment(void) {
ff6046
         assert_se(bus_gvariant_get_alignment("") == 1);
ff6046
-        assert_se(bus_gvariant_get_alignment("()") == 1);
ff6046
+        assert_se(bus_gvariant_get_alignment("()") == -EINVAL);
ff6046
         assert_se(bus_gvariant_get_alignment("y") == 1);
ff6046
         assert_se(bus_gvariant_get_alignment("b") == 1);
ff6046
         assert_se(bus_gvariant_get_alignment("u") == 4);
ff6046
diff --git a/src/libsystemd/sd-bus/test-bus-marshal.c b/src/libsystemd/sd-bus/test-bus-marshal.c
ff6046
index f007168ca6..1743b1b491 100644
ff6046
--- a/src/libsystemd/sd-bus/test-bus-marshal.c
ff6046
+++ b/src/libsystemd/sd-bus/test-bus-marshal.c
ff6046
@@ -151,7 +151,7 @@ int main(int argc, char *argv[]) {
ff6046
         assert_se(r >= 0);
ff6046
 
ff6046
         r = sd_bus_message_append(m, "()");
ff6046
-        assert_se(r >= 0);
ff6046
+        assert_se(r == -EINVAL);
ff6046
 
ff6046
         r = sd_bus_message_append(m, "ba(ss)", 255, 3, "aaa", "1", "bbb", "2", "ccc", "3");
ff6046
         assert_se(r >= 0);
ff6046
@@ -293,7 +293,7 @@ int main(int argc, char *argv[]) {
ff6046
         assert_se(v == 10);
ff6046
 
ff6046
         r = sd_bus_message_read(m, "()");
ff6046
-        assert_se(r > 0);
ff6046
+        assert_se(r < 0);
ff6046
 
ff6046
         r = sd_bus_message_read(m, "ba(ss)", &boolean, 3, &x, &y, &a, &b, &c, &d);
ff6046
         assert_se(r > 0);
ff6046
@@ -374,7 +374,7 @@ int main(int argc, char *argv[]) {
ff6046
 
ff6046
         assert_se(sd_bus_message_verify_type(m, 'a', "{yv}") > 0);
ff6046
 
ff6046
-        r = sd_bus_message_skip(m, "a{yv}y(ty)y(yt)y()");
ff6046
+        r = sd_bus_message_skip(m, "a{yv}y(ty)y(yt)y");
ff6046
         assert_se(r >= 0);
ff6046
 
ff6046
         assert_se(sd_bus_message_verify_type(m, 'b', NULL) > 0);
ff6046
diff --git a/src/libsystemd/sd-bus/test-bus-signature.c b/src/libsystemd/sd-bus/test-bus-signature.c
ff6046
index 1ba1909198..a716cd1b35 100644
ff6046
--- a/src/libsystemd/sd-bus/test-bus-signature.c
ff6046
+++ b/src/libsystemd/sd-bus/test-bus-signature.c
ff6046
@@ -16,9 +16,9 @@ int main(int argc, char *argv[]) {
ff6046
         assert_se(signature_is_single("v", false));
ff6046
         assert_se(signature_is_single("as", false));
ff6046
         assert_se(signature_is_single("(ss)", false));
ff6046
-        assert_se(signature_is_single("()", false));
ff6046
-        assert_se(signature_is_single("(()()()()())", false));
ff6046
-        assert_se(signature_is_single("(((())))", false));
ff6046
+        assert_se(!signature_is_single("()", false));
ff6046
+        assert_se(!signature_is_single("(()()()()())", false));
ff6046
+        assert_se(!signature_is_single("(((())))", false));
ff6046
         assert_se(signature_is_single("((((s))))", false));
ff6046
         assert_se(signature_is_single("{ss}", true));
ff6046
         assert_se(signature_is_single("a{ss}", false));
ff6046
@@ -63,7 +63,7 @@ int main(int argc, char *argv[]) {
ff6046
         assert_se(signature_is_valid("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaas", false));
ff6046
         assert_se(!signature_is_valid("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaau", false));
ff6046
 
ff6046
-        assert_se(signature_is_valid("(((((((((((((((((((((((((((((((())))))))))))))))))))))))))))))))", false));
ff6046
+        assert_se(signature_is_valid("((((((((((((((((((((((((((((((((s))))))))))))))))))))))))))))))))", false));
ff6046
         assert_se(!signature_is_valid("((((((((((((((((((((((((((((((((()))))))))))))))))))))))))))))))))", false));
ff6046
 
ff6046
         assert_se(namespace_complex_pattern("", ""));
ff6046
diff --git a/test/fuzz/fuzz-bus-message/crash-26bba7182dedc8848939931d9fcefcb7922f2e56 b/test/fuzz/fuzz-bus-message/crash-26bba7182dedc8848939931d9fcefcb7922f2e56
ff6046
new file mode 100644
ff6046
index 0000000000000000000000000000000000000000..f1bf3229effc982c8b129182fe60739efe3c5013
ff6046
GIT binary patch
ff6046
literal 157
ff6046
mcmd1#|DTC5gMmSS0SHWtK_neOii>$FgGM4Akdcw0DF6TjSP;el
ff6046
ff6046
literal 0
ff6046
HcmV?d00001
ff6046
ff6046
diff --git a/test/fuzz/fuzz-bus-message/timeout-08ee8f6446a4064db064e8e0b3d220147f7d0b5b b/test/fuzz/fuzz-bus-message/timeout-08ee8f6446a4064db064e8e0b3d220147f7d0b5b
ff6046
new file mode 100644
ff6046
index 0000000000000000000000000000000000000000..c975f906eef521a3cfac5627c8b371ee55aa0e6c
ff6046
GIT binary patch
ff6046
literal 534
ff6046
zcmcJL!Ab-%42J(Y?m8o$d;nSS(q4Ae_Yi!A47)oFEOwaGU5e<<_x8`!K@h}~fslMn
ff6046
zn)c7Z!M!`6y9Pc0I2S?0hHh3l#W~|szZ;Ct$XAT}7+V?FCpm1RoiBemuU&_Ys%S@7
ff6046
zdCkYS>{AZe=OjL~Ie67zrCwgdYud(O^J==RG>!dpXFS^tlZIX@tK0h@{D4MV@hJsW
ff6046
zyR)R1zXEs6tHM*H04&I}2-7)y>9oE<hN&+5v>VCxw(Vn{LBxXmJ)=frMcRdZlJ-~v
ff6046
b#4gh=OPF@NW^U~wGO=l?@b9nvy
ff6046
ff6046
literal 0
ff6046
HcmV?d00001
ff6046