6f2e21
From 9ccec2afdaf8af463f321eb37d3c3bb90d1d432e Mon Sep 17 00:00:00 2001
6f2e21
From: Jeremy Allison <jra@samba.org>
6f2e21
Date: Tue, 7 Jun 2022 09:40:45 -0700
6f2e21
Subject: [PATCH 1/2] CVE-2022-32742: s4: torture: Add raw.write.bad-write test.
6f2e21
6f2e21
Reproduces the test code in:
6f2e21
6f2e21
BUG: https://bugzilla.samba.org/show_bug.cgi?id=15085
6f2e21
6f2e21
Add knownfail.
6f2e21
6f2e21
Signed-off-by: Jeremy Allison <jra@samba.org>
6f2e21
Reviewed-by: David Disseldorp <ddiss@samba.org>
6f2e21
---
6f2e21
 selftest/knownfail.d/bad-write |  2 +
6f2e21
 source4/torture/raw/write.c    | 89 ++++++++++++++++++++++++++++++++++
6f2e21
 2 files changed, 91 insertions(+)
6f2e21
 create mode 100644 selftest/knownfail.d/bad-write
6f2e21
6f2e21
diff --git a/selftest/knownfail.d/bad-write b/selftest/knownfail.d/bad-write
6f2e21
new file mode 100644
6f2e21
index 00000000000..5fc16606a13
6f2e21
--- /dev/null
6f2e21
+++ b/selftest/knownfail.d/bad-write
6f2e21
@@ -0,0 +1,2 @@
6f2e21
+^samba3.raw.write.bad-write\(nt4_dc_smb1\)
6f2e21
+^samba3.raw.write.bad-write\(ad_dc_smb1\)
6f2e21
diff --git a/source4/torture/raw/write.c b/source4/torture/raw/write.c
6f2e21
index 0a2f50f425b..661485bb548 100644
6f2e21
--- a/source4/torture/raw/write.c
6f2e21
+++ b/source4/torture/raw/write.c
6f2e21
@@ -25,6 +25,7 @@
6f2e21
 #include "libcli/libcli.h"
6f2e21
 #include "torture/util.h"
6f2e21
 #include "torture/raw/proto.h"
6f2e21
+#include "libcli/raw/raw_proto.h"
6f2e21
 
6f2e21
 #define CHECK_STATUS(status, correct) do { \
6f2e21
 	if (!NT_STATUS_EQUAL(status, correct)) { \
6f2e21
@@ -694,6 +695,93 @@ done:
6f2e21
 	return ret;
6f2e21
 }
6f2e21
 
6f2e21
+/*
6f2e21
+  test a deliberately bad SMB1 write.
6f2e21
+*/
6f2e21
+static bool test_bad_write(struct torture_context *tctx,
6f2e21
+		       struct smbcli_state *cli)
6f2e21
+{
6f2e21
+	bool ret = false;
6f2e21
+	int fnum = -1;
6f2e21
+	struct smbcli_request *req = NULL;
6f2e21
+	const char *fname = BASEDIR "\\badwrite.txt";
6f2e21
+	bool ok = false;
6f2e21
+
6f2e21
+	if (!torture_setup_dir(cli, BASEDIR)) {
6f2e21
+		torture_fail(tctx, "failed to setup basedir");
6f2e21
+	}
6f2e21
+
6f2e21
+	torture_comment(tctx, "Testing RAW_BAD_WRITE\n");
6f2e21
+
6f2e21
+	fnum = smbcli_open(cli->tree, fname, O_RDWR|O_CREAT, DENY_NONE);
6f2e21
+	if (fnum == -1) {
6f2e21
+		torture_fail_goto(tctx,
6f2e21
+			done,
6f2e21
+			talloc_asprintf(tctx,
6f2e21
+				"Failed to create %s - %s\n",
6f2e21
+				fname,
6f2e21
+				smbcli_errstr(cli->tree)));
6f2e21
+	}
6f2e21
+
6f2e21
+	req = smbcli_request_setup(cli->tree,
6f2e21
+				   SMBwrite,
6f2e21
+				   5,
6f2e21
+				   0);
6f2e21
+	if (req == NULL) {
6f2e21
+		torture_fail_goto(tctx,
6f2e21
+			done,
6f2e21
+			talloc_asprintf(tctx, "talloc fail\n"));
6f2e21
+	}
6f2e21
+
6f2e21
+	SSVAL(req->out.vwv, VWV(0), fnum);
6f2e21
+	SSVAL(req->out.vwv, VWV(1), 65535); /* bad write length. */
6f2e21
+	SIVAL(req->out.vwv, VWV(2), 0); /* offset */
6f2e21
+	SSVAL(req->out.vwv, VWV(4), 0); /* remaining. */
6f2e21
+
6f2e21
+        if (!smbcli_request_send(req)) {
6f2e21
+		torture_fail_goto(tctx,
6f2e21
+			done,
6f2e21
+			talloc_asprintf(tctx, "Send failed\n"));
6f2e21
+        }
6f2e21
+
6f2e21
+        if (!smbcli_request_receive(req)) {
6f2e21
+		torture_fail_goto(tctx,
6f2e21
+			done,
6f2e21
+			talloc_asprintf(tctx, "Reveive failed\n"));
6f2e21
+	}
6f2e21
+
6f2e21
+	/*
6f2e21
+	 * Check for expected error codes.
6f2e21
+	 * ntvfs returns NT_STATUS_UNSUCCESSFUL.
6f2e21
+	 */
6f2e21
+	ok = (NT_STATUS_EQUAL(req->status, NT_STATUS_INVALID_PARAMETER) ||
6f2e21
+	     NT_STATUS_EQUAL(req->status, NT_STATUS_UNSUCCESSFUL));
6f2e21
+
6f2e21
+	if (!ok) {
6f2e21
+		torture_fail_goto(tctx,
6f2e21
+			done,
6f2e21
+			talloc_asprintf(tctx,
6f2e21
+				"Should have returned "
6f2e21
+				"NT_STATUS_INVALID_PARAMETER or "
6f2e21
+				"NT_STATUS_UNSUCCESSFUL "
6f2e21
+				"got %s\n",
6f2e21
+				nt_errstr(req->status)));
6f2e21
+        }
6f2e21
+
6f2e21
+	ret = true;
6f2e21
+
6f2e21
+done:
6f2e21
+	if (req != NULL) {
6f2e21
+		smbcli_request_destroy(req);
6f2e21
+	}
6f2e21
+	if (fnum != -1) {
6f2e21
+		smbcli_close(cli->tree, fnum);
6f2e21
+	}
6f2e21
+	smb_raw_exit(cli->session);
6f2e21
+	smbcli_deltree(cli->tree, BASEDIR);
6f2e21
+	return ret;
6f2e21
+}
6f2e21
+
6f2e21
 /*
6f2e21
    basic testing of write calls
6f2e21
 */
6f2e21
@@ -705,6 +793,7 @@ struct torture_suite *torture_raw_write(TALLOC_CTX *mem_ctx)
6f2e21
 	torture_suite_add_1smb_test(suite, "write unlock", test_writeunlock);
6f2e21
 	torture_suite_add_1smb_test(suite, "write close", test_writeclose);
6f2e21
 	torture_suite_add_1smb_test(suite, "writex", test_writex);
6f2e21
+	torture_suite_add_1smb_test(suite, "bad-write", test_bad_write);
6f2e21
 
6f2e21
 	return suite;
6f2e21
 }
6f2e21
-- 
6f2e21
2.34.1
6f2e21
6f2e21
6f2e21
From 9097c5363605e1d5f99ff5a59dc6795c612d472f Mon Sep 17 00:00:00 2001
6f2e21
From: Jeremy Allison <jra@samba.org>
6f2e21
Date: Wed, 8 Jun 2022 13:50:51 -0700
6f2e21
Subject: [PATCH 2/2] CVE-2022-32742: s3: smbd: Harden the smbreq_bufrem() macro.
6f2e21
6f2e21
Fixes the raw.write.bad-write test.
6f2e21
6f2e21
NB. We need the two (==0) changes in source3/smbd/reply.c
6f2e21
as the gcc optimizer now knows that the return from
6f2e21
smbreq_bufrem() can never be less than zero.
6f2e21
6f2e21
BUG: https://bugzilla.samba.org/show_bug.cgi?id=15085
6f2e21
6f2e21
Remove knownfail.
6f2e21
6f2e21
Signed-off-by: Jeremy Allison <jra@samba.org>
6f2e21
Reviewed-by: David Disseldorp <ddiss@samba.org>
6f2e21
6f2e21
---
6f2e21
 selftest/knownfail.d/bad-write | 2 --
6f2e21
 source3/include/smb_macros.h   | 2 +-
6f2e21
 source3/smbd/reply.c           | 4 ++--
6f2e21
 3 files changed, 3 insertions(+), 5 deletions(-)
6f2e21
 delete mode 100644 selftest/knownfail.d/bad-write
6f2e21
6f2e21
diff --git a/selftest/knownfail.d/bad-write b/selftest/knownfail.d/bad-write
6f2e21
deleted file mode 100644
6f2e21
index 5fc16606a13..00000000000
6f2e21
--- a/selftest/knownfail.d/bad-write
6f2e21
+++ /dev/null
6f2e21
@@ -1,2 +0,0 @@
6f2e21
-^samba3.raw.write.bad-write\(nt4_dc_smb1\)
6f2e21
-^samba3.raw.write.bad-write\(ad_dc_smb1\)
6f2e21
diff --git a/source3/include/smb_macros.h b/source3/include/smb_macros.h
6f2e21
index 344a997cbd2..c75b93fcc25 100644
6f2e21
--- a/source3/include/smb_macros.h
6f2e21
+++ b/source3/include/smb_macros.h
6f2e21
@@ -152,7 +152,7 @@
6f2e21
 
6f2e21
 /* the remaining number of bytes in smb buffer 'buf' from pointer 'p'. */
6f2e21
 #define smb_bufrem(buf, p) (smb_buflen(buf)-PTR_DIFF(p, smb_buf(buf)))
6f2e21
-#define smbreq_bufrem(req, p) (req->buflen - PTR_DIFF(p, req->buf))
6f2e21
+#define smbreq_bufrem(req, p) ((req)->buflen < PTR_DIFF((p), (req)->buf) ? 0 : (req)->buflen - PTR_DIFF((p), (req)->buf))
6f2e21
 
6f2e21
 
6f2e21
 /* Note that chain_size must be available as an extern int to this macro. */
6f2e21
diff --git a/source3/smbd/reply.c b/source3/smbd/reply.c
6f2e21
index d4573d3da55..e1a47a65662 100644
6f2e21
--- a/source3/smbd/reply.c
6f2e21
+++ b/source3/smbd/reply.c
6f2e21
@@ -345,7 +345,7 @@ size_t srvstr_get_path_req(TALLOC_CTX *mem_ctx, struct smb_request *req,
6f2e21
 {
6f2e21
 	ssize_t bufrem = smbreq_bufrem(req, src);
6f2e21
 
6f2e21
-	if (bufrem < 0) {
6f2e21
+	if (bufrem == 0) {
6f2e21
 		*err = NT_STATUS_INVALID_PARAMETER;
6f2e21
 		return 0;
6f2e21
 	}
6f2e21
@@ -383,7 +383,7 @@ size_t srvstr_pull_req_talloc(TALLOC_CTX *ctx, struct smb_request *req,
6f2e21
 {
6f2e21
 	ssize_t bufrem = smbreq_bufrem(req, src);
6f2e21
 
6f2e21
-	if (bufrem < 0) {
6f2e21
+	if (bufrem == 0) {
6f2e21
 		return 0;
6f2e21
 	}
6f2e21
 
6f2e21
-- 
6f2e21
2.34.1
6f2e21