b678c9
From 364275d1ae8c55242497e7c8804fb28aa3b73465 Mon Sep 17 00:00:00 2001
b678c9
From: Jeremy Allison <jra@samba.org>
b678c9
Date: Fri, 8 Sep 2017 10:13:14 -0700
b678c9
Subject: [PATCH] CVE-2017-12163: s3:smbd: Prevent client short SMB1 write from
b678c9
 writing server memory to file.
b678c9
b678c9
BUG: https://bugzilla.samba.org/show_bug.cgi?id=13020
b678c9
b678c9
Signed-off-by: Jeremy Allison <jra@samba.org>
b678c9
Signed-off-by: Stefan Metzmacher <metze@samba.org>
b678c9
---
b678c9
 source3/smbd/reply.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++
b678c9
 1 file changed, 50 insertions(+)
b678c9
b678c9
diff --git a/source3/smbd/reply.c b/source3/smbd/reply.c
b678c9
index 317143f..7b07078 100644
b678c9
--- a/source3/smbd/reply.c
b678c9
+++ b/source3/smbd/reply.c
b678c9
@@ -4474,6 +4474,9 @@ void reply_writebraw(struct smb_request *req)
b678c9
 	}
b678c9
 
b678c9
 	/* Ensure we don't write bytes past the end of this packet. */
b678c9
+	/*
b678c9
+	 * This already protects us against CVE-2017-12163.
b678c9
+	 */
b678c9
 	if (data + numtowrite > smb_base(req->inbuf) + smb_len(req->inbuf)) {
b678c9
 		reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
b678c9
 		error_to_writebrawerr(req);
b678c9
@@ -4574,6 +4577,11 @@ void reply_writebraw(struct smb_request *req)
b678c9
 			exit_server_cleanly("secondary writebraw failed");
b678c9
 		}
b678c9
 
b678c9
+		/*
b678c9
+		 * We are not vulnerable to CVE-2017-12163
b678c9
+		 * here as we are guarenteed to have numtowrite
b678c9
+		 * bytes available - we just read from the client.
b678c9
+		 */
b678c9
 		nwritten = write_file(req,fsp,buf+4,startpos+nwritten,numtowrite);
b678c9
 		if (nwritten == -1) {
b678c9
 			TALLOC_FREE(buf);
b678c9
@@ -4647,6 +4655,7 @@ void reply_writeunlock(struct smb_request *req)
b678c9
 	connection_struct *conn = req->conn;
b678c9
 	ssize_t nwritten = -1;
b678c9
 	size_t numtowrite;
b678c9
+	size_t remaining;
b678c9
 	off_t startpos;
b678c9
 	const char *data;
b678c9
 	NTSTATUS status = NT_STATUS_OK;
b678c9
@@ -4679,6 +4688,17 @@ void reply_writeunlock(struct smb_request *req)
b678c9
 	startpos = IVAL_TO_SMB_OFF_T(req->vwv+2, 0);
b678c9
 	data = (const char *)req->buf + 3;
b678c9
 
b678c9
+	/*
b678c9
+	 * Ensure client isn't asking us to write more than
b678c9
+	 * they sent. CVE-2017-12163.
b678c9
+	 */
b678c9
+	remaining = smbreq_bufrem(req, data);
b678c9
+	if (numtowrite > remaining) {
b678c9
+		reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
b678c9
+		END_PROFILE(SMBwriteunlock);
b678c9
+		return;
b678c9
+	}
b678c9
+
b678c9
 	if (!fsp->print_file && numtowrite > 0) {
b678c9
 		init_strict_lock_struct(fsp, (uint64_t)req->smbpid,
b678c9
 		    (uint64_t)startpos, (uint64_t)numtowrite, WRITE_LOCK,
b678c9
@@ -4756,6 +4776,7 @@ void reply_write(struct smb_request *req)
b678c9
 {
b678c9
 	connection_struct *conn = req->conn;
b678c9
 	size_t numtowrite;
b678c9
+	size_t remaining;
b678c9
 	ssize_t nwritten = -1;
b678c9
 	off_t startpos;
b678c9
 	const char *data;
b678c9
@@ -4796,6 +4817,17 @@ void reply_write(struct smb_request *req)
b678c9
 	startpos = IVAL_TO_SMB_OFF_T(req->vwv+2, 0);
b678c9
 	data = (const char *)req->buf + 3;
b678c9
 
b678c9
+	/*
b678c9
+	 * Ensure client isn't asking us to write more than
b678c9
+	 * they sent. CVE-2017-12163.
b678c9
+	 */
b678c9
+	remaining = smbreq_bufrem(req, data);
b678c9
+	if (numtowrite > remaining) {
b678c9
+		reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
b678c9
+		END_PROFILE(SMBwrite);
b678c9
+		return;
b678c9
+	}
b678c9
+
b678c9
 	if (!fsp->print_file) {
b678c9
 		init_strict_lock_struct(fsp, (uint64_t)req->smbpid,
b678c9
 			(uint64_t)startpos, (uint64_t)numtowrite, WRITE_LOCK,
b678c9
@@ -5018,6 +5050,9 @@ void reply_write_and_X(struct smb_request *req)
b678c9
 			goto out;
b678c9
 		}
b678c9
 	} else {
b678c9
+		/*
b678c9
+		 * This already protects us against CVE-2017-12163.
b678c9
+		 */
b678c9
 		if (smb_doff > smblen || smb_doff + numtowrite < numtowrite ||
b678c9
 				smb_doff + numtowrite > smblen) {
b678c9
 			reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
b678c9
@@ -5444,6 +5479,7 @@ void reply_writeclose(struct smb_request *req)
b678c9
 {
b678c9
 	connection_struct *conn = req->conn;
b678c9
 	size_t numtowrite;
b678c9
+	size_t remaining;
b678c9
 	ssize_t nwritten = -1;
b678c9
 	NTSTATUS close_status = NT_STATUS_OK;
b678c9
 	off_t startpos;
b678c9
@@ -5477,6 +5513,17 @@ void reply_writeclose(struct smb_request *req)
b678c9
 	mtime = convert_time_t_to_timespec(srv_make_unix_date3(req->vwv+4));
b678c9
 	data = (const char *)req->buf + 1;
b678c9
 
b678c9
+	/*
b678c9
+	 * Ensure client isn't asking us to write more than
b678c9
+	 * they sent. CVE-2017-12163.
b678c9
+	 */
b678c9
+	remaining = smbreq_bufrem(req, data);
b678c9
+	if (numtowrite > remaining) {
b678c9
+		reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
b678c9
+		END_PROFILE(SMBwriteclose);
b678c9
+		return;
b678c9
+	}
b678c9
+
b678c9
 	if (fsp->print_file == NULL) {
b678c9
 		init_strict_lock_struct(fsp, (uint64_t)req->smbpid,
b678c9
 		    (uint64_t)startpos, (uint64_t)numtowrite, WRITE_LOCK,
b678c9
@@ -6069,6 +6116,9 @@ void reply_printwrite(struct smb_request *req)
b678c9
 
b678c9
 	numtowrite = SVAL(req->buf, 1);
b678c9
 
b678c9
+	/*
b678c9
+	 * This already protects us against CVE-2017-12163.
b678c9
+	 */
b678c9
 	if (req->buflen < numtowrite + 3) {
b678c9
 		reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
b678c9
 		END_PROFILE(SMBsplwr);
b678c9
-- 
b678c9
1.9.1
b678c9