From 475d2dd9758b1c82b8f079386c08cdec8bb23b5a Mon Sep 17 00:00:00 2001
From: Luca Boccassi <bluca@debian.org>
Date: Sun, 16 Jul 2023 01:10:47 +0100
Subject: [PATCH] bus: add some minimal bounds check on signatures

CID#1491292
CID#1491291
CID#1491290
CID#1491289
CID#1491284
CID#1491281
CID#1491280
CID#1491278

(cherry picked from commit d80cc39558ec7e596d594d1aadc4df81262611f8)
(cherry picked from commit a518ea9ff1fe55c7bb5e4f391858a57d66b779d0)
(cherry picked from commit b7531639dc75261de8957b2ddac6012d8fe2559f)

Conflict:NA
Reference:https://github.com/systemd/systemd-stable/commit/475d2dd9758b1c82b8f079386c08cdec8bb23b5a
---
 src/busctl/busctl.c                 | 5 ++++-
 src/libsystemd/sd-bus/bus-message.c | 6 ++++++
 2 files changed, 10 insertions(+), 1 deletion(-)

diff --git a/src/busctl/busctl.c b/src/busctl/busctl.c
index 9e46cc2e96..c52a8d5912 100644
--- a/src/busctl/busctl.c
+++ b/src/busctl/busctl.c
@@ -1621,8 +1621,11 @@ static int message_append_cmdline(sd_bus_message *m, const char *signature, char
                         p--;
 
                         r = signature_element_length(signature, &k);
-                        if (r < 0)
+                        if (r < 0 || k < 2) {
+                                if (r >= 0 && k < 2)
+                                        r = -ERANGE;
                                 return log_error_errno(r, "Invalid struct/dict entry signature: %m");
+                        }
 
                         {
                                 char s[k-1];
diff --git a/src/libsystemd/sd-bus/bus-message.c b/src/libsystemd/sd-bus/bus-message.c
index 3262c07c6d..cbc99c1bd8 100644
--- a/src/libsystemd/sd-bus/bus-message.c
+++ b/src/libsystemd/sd-bus/bus-message.c
@@ -2009,6 +2009,8 @@ _public_ int sd_bus_message_appendv(
                         r = signature_element_length(t, &k);
                         if (r < 0)
                                 return r;
+                        if (k < 2)
+                                return -ERANGE;
 
                         {
                                 char s[k - 1];
@@ -3452,6 +3454,8 @@ _public_ int sd_bus_message_readv(
                         r = signature_element_length(t, &k);
                         if (r < 0)
                                 return r;
+                        if (k < 2)
+                                return -ERANGE;
 
                         {
                                 char s[k - 1];
@@ -3632,6 +3636,8 @@ _public_ int sd_bus_message_skip(sd_bus_message *m, const char *types) {
                 r = signature_element_length(types, &k);
                 if (r < 0)
                         return r;
+                if (k < 2)
+                        return -ERANGE;
 
                 {
                         char s[k-1];
-- 
2.33.0

