From 38664163fcac985d87e4274d198568e0fe88595e Mon Sep 17 00:00:00 2001
From: Ralph Boehme <slow@samba.org>
Date: Fri, 26 May 2023 13:06:19 +0200
Subject: [PATCH] CVE-2023-34966: mdssvc: harden sl_unpack_loop()

A malicious client could send a packet where subcount is zero, leading to a busy
loop because

    count -= subcount
=>  count -= 0
=>  while (count > 0)

loops forever.

BUG: https://bugzilla.samba.org/show_bug.cgi?id=15340

Signed-off-by: Ralph Boehme <slow@samba.org>

Upstream-Status: Backport [https://github.com/samba-team/samba/commit/38664163fcac985d87e4274d198568e0fe88595e]

CVE: CVE-2023-34966

Signed-off-by: Archana Polampalli <archana.polampalli@windriver.com>
---
 source3/rpc_server/mdssvc/marshalling.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/source3/rpc_server/mdssvc/marshalling.c b/source3/rpc_server/mdssvc/marshalling.c
index 9ba6ef571f2..d794ba15838 100644
--- a/source3/rpc_server/mdssvc/marshalling.c
+++ b/source3/rpc_server/mdssvc/marshalling.c
@@ -1119,7 +1119,7 @@ static ssize_t sl_unpack_loop(DALLOC_CTX *query,
			sl_nil_t nil = 0;

			subcount = tag.count;
-			if (subcount > count) {
+			if (subcount < 1 || subcount > count) {
				return -1;
			}
			for (i = 0; i < subcount; i++) {
@@ -1147,7 +1147,7 @@ static ssize_t sl_unpack_loop(DALLOC_CTX *query,

		case SQ_TYPE_INT64:
			subcount = sl_unpack_ints(query, buf, offset, bufsize, encoding);
-			if (subcount == -1 || subcount > count) {
+			if (subcount < 1 || subcount > count) {
				return -1;
			}
			offset += tag.size;
@@ -1156,7 +1156,7 @@ static ssize_t sl_unpack_loop(DALLOC_CTX *query,

		case SQ_TYPE_UUID:
			subcount = sl_unpack_uuid(query, buf, offset, bufsize, encoding);
-			if (subcount == -1 || subcount > count) {
+			if (subcount < 1 || subcount > count) {
				return -1;
			}
			offset += tag.size;
@@ -1165,7 +1165,7 @@ static ssize_t sl_unpack_loop(DALLOC_CTX *query,

		case SQ_TYPE_FLOAT:
			subcount = sl_unpack_floats(query, buf, offset, bufsize, encoding);
-			if (subcount == -1 || subcount > count) {
+			if (subcount < 1 || subcount > count) {
				return -1;
			}
			offset += tag.size;
@@ -1174,7 +1174,7 @@ static ssize_t sl_unpack_loop(DALLOC_CTX *query,

		case SQ_TYPE_DATE:
			subcount = sl_unpack_date(query, buf, offset, bufsize, encoding);
-			if (subcount == -1 || subcount > count) {
+			if (subcount < 1 || subcount > count) {
				return -1;
			}
			offset += tag.size;
--
2.40.0
