From cb6f3e2202473eeccf81e34ebcdb4bc4f726548a Mon Sep 17 00:00:00 2001
From: Ralph Boehme <slow@samba.org>
Date: Fri, 26 May 2023 13:06:19 +0200
Subject: [PATCH 05/25] 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>

Conflict: NA
Reference: https://download.samba.org/pub/samba/patches/security/samba-4.16.11-security-2023-07-19.patch
---
 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 1aa750413cd..441d41160f1 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.34.1
