a3a04f
From a57290580b7fcffea9b76991f2dd49ad480d3b64 Mon Sep 17 00:00:00 2001
a3a04f
From: Stefan Metzmacher <metze@samba.org>
a3a04f
Date: Wed, 15 Mar 2017 17:04:30 +0000
a3a04f
Subject: [PATCH 1/2] libcli/smb: Fix alignment problems of
a3a04f
 smb_bytes_pull_str()
a3a04f
a3a04f
This function needs to get the whole smb buffer in order to get
a3a04f
the alignment for unicode correct.
a3a04f
a3a04f
BUG: https://bugzilla.samba.org/show_bug.cgi?id=12824
a3a04f
a3a04f
Signed-off-by: Stefan Metzmacher <metze@samba.org>
a3a04f
Reviewed-by: Jeremy Allison <jra@samba.org>
a3a04f
Reviewed-by: Andreas Schneider <asn@samba.org>
a3a04f
(cherry picked from commit e60e77a8afd095bfdb3d678aa48570ca159d9b24)
a3a04f
---
a3a04f
 libcli/smb/smb1cli_session.c | 28 +++++++++++++-------------
a3a04f
 libcli/smb/smb_util.h        |  3 ++-
a3a04f
 libcli/smb/util.c            | 47 +++++++++++++++++++++++++++++---------------
a3a04f
 3 files changed, 47 insertions(+), 31 deletions(-)
a3a04f
a3a04f
diff --git a/libcli/smb/smb1cli_session.c b/libcli/smb/smb1cli_session.c
a3a04f
index 9d92aa6aed4..11614df0ae4 100644
a3a04f
--- a/libcli/smb/smb1cli_session.c
a3a04f
+++ b/libcli/smb/smb1cli_session.c
a3a04f
@@ -210,16 +210,16 @@ static void smb1cli_session_setup_lm21_done(struct tevent_req *subreq)
a3a04f
 	p = bytes;
a3a04f
 
a3a04f
 	status = smb_bytes_pull_str(state, &state->out_native_os,
a3a04f
-				    use_unicode, p,
a3a04f
-				    bytes+num_bytes-p, &ret;;
a3a04f
+				    use_unicode, bytes, num_bytes,
a3a04f
+				    p, &ret;;
a3a04f
 	if (tevent_req_nterror(req, status)) {
a3a04f
 		return;
a3a04f
 	}
a3a04f
 	p += ret;
a3a04f
 
a3a04f
 	status = smb_bytes_pull_str(state, &state->out_native_lm,
a3a04f
-				    use_unicode, p,
a3a04f
-				    bytes+num_bytes-p, &ret;;
a3a04f
+				    use_unicode, bytes, num_bytes,
a3a04f
+				    p, &ret;;
a3a04f
 	if (tevent_req_nterror(req, status)) {
a3a04f
 		return;
a3a04f
 	}
a3a04f
@@ -493,24 +493,24 @@ static void smb1cli_session_setup_nt1_done(struct tevent_req *subreq)
a3a04f
 	p = bytes;
a3a04f
 
a3a04f
 	status = smb_bytes_pull_str(state, &state->out_native_os,
a3a04f
-				    use_unicode, p,
a3a04f
-				    bytes+num_bytes-p, &ret;;
a3a04f
+				    use_unicode, bytes, num_bytes,
a3a04f
+				    p, &ret;;
a3a04f
 	if (tevent_req_nterror(req, status)) {
a3a04f
 		return;
a3a04f
 	}
a3a04f
 	p += ret;
a3a04f
 
a3a04f
 	status = smb_bytes_pull_str(state, &state->out_native_lm,
a3a04f
-				    use_unicode, p,
a3a04f
-				    bytes+num_bytes-p, &ret;;
a3a04f
+				    use_unicode, bytes, num_bytes,
a3a04f
+				    p, &ret;;
a3a04f
 	if (tevent_req_nterror(req, status)) {
a3a04f
 		return;
a3a04f
 	}
a3a04f
 	p += ret;
a3a04f
 
a3a04f
 	status = smb_bytes_pull_str(state, &state->out_primary_domain,
a3a04f
-				    use_unicode, p,
a3a04f
-				    bytes+num_bytes-p, &ret;;
a3a04f
+				    use_unicode, bytes, num_bytes,
a3a04f
+				    p, &ret;;
a3a04f
 	if (tevent_req_nterror(req, status)) {
a3a04f
 		return;
a3a04f
 	}
a3a04f
@@ -754,16 +754,16 @@ static void smb1cli_session_setup_ext_done(struct tevent_req *subreq)
a3a04f
 	p += out_security_blob_length;
a3a04f
 
a3a04f
 	status = smb_bytes_pull_str(state, &state->out_native_os,
a3a04f
-				    use_unicode, p,
a3a04f
-				    bytes+num_bytes-p, &ret;;
a3a04f
+				    use_unicode, bytes, num_bytes,
a3a04f
+				    p, &ret;;
a3a04f
 	if (tevent_req_nterror(req, status)) {
a3a04f
 		return;
a3a04f
 	}
a3a04f
 	p += ret;
a3a04f
 
a3a04f
 	status = smb_bytes_pull_str(state, &state->out_native_lm,
a3a04f
-				    use_unicode, p,
a3a04f
-				    bytes+num_bytes-p, &ret;;
a3a04f
+				    use_unicode, bytes, num_bytes,
a3a04f
+				    p, &ret;;
a3a04f
 	if (tevent_req_nterror(req, status)) {
a3a04f
 		return;
a3a04f
 	}
a3a04f
diff --git a/libcli/smb/smb_util.h b/libcli/smb/smb_util.h
a3a04f
index 7e6f0a4ebc4..2884786339d 100644
a3a04f
--- a/libcli/smb/smb_util.h
a3a04f
+++ b/libcli/smb/smb_util.h
a3a04f
@@ -38,4 +38,5 @@ uint8_t *trans2_bytes_push_bytes(uint8_t *buf,
a3a04f
 				 const uint8_t *bytes, size_t num_bytes);
a3a04f
 NTSTATUS smb_bytes_pull_str(TALLOC_CTX *mem_ctx, char **_str, bool ucs2,
a3a04f
 			    const uint8_t *buf, size_t buf_len,
a3a04f
-			    size_t *pbuf_consumed);
a3a04f
+			    const uint8_t *position,
a3a04f
+			    size_t *_consumed);
a3a04f
diff --git a/libcli/smb/util.c b/libcli/smb/util.c
a3a04f
index ef8c9fafa35..7ef909c6077 100644
a3a04f
--- a/libcli/smb/util.c
a3a04f
+++ b/libcli/smb/util.c
a3a04f
@@ -319,29 +319,43 @@ uint8_t *trans2_bytes_push_bytes(uint8_t *buf,
a3a04f
 static NTSTATUS internal_bytes_pull_str(TALLOC_CTX *mem_ctx, char **_str,
a3a04f
 					bool ucs2, bool align_odd,
a3a04f
 					const uint8_t *buf, size_t buf_len,
a3a04f
-					size_t *pbuf_consumed)
a3a04f
+					const uint8_t *position,
a3a04f
+					size_t *p_consumed)
a3a04f
 {
a3a04f
 	size_t pad = 0;
a3a04f
+	size_t offset;
a3a04f
 	char *str = NULL;
a3a04f
 	size_t str_len = 0;
a3a04f
 	bool ok;
a3a04f
 
a3a04f
 	*_str = NULL;
a3a04f
-	if (pbuf_consumed != NULL) {
a3a04f
-		*pbuf_consumed = 0;
a3a04f
+	if (p_consumed != NULL) {
a3a04f
+		*p_consumed = 0;
a3a04f
+	}
a3a04f
+
a3a04f
+	if (position < buf) {
a3a04f
+		return NT_STATUS_INTERNAL_ERROR;
a3a04f
+	}
a3a04f
+
a3a04f
+	offset = PTR_DIFF(position, buf);
a3a04f
+	if (offset > buf_len) {
a3a04f
+		return NT_STATUS_BUFFER_TOO_SMALL;
a3a04f
 	}
a3a04f
 
a3a04f
 	if (ucs2 &&
a3a04f
-	    ((align_odd && (buf_len % 2 == 0)) ||
a3a04f
-	     (!align_odd && (buf_len % 2 == 1)))) {
a3a04f
-		if (buf_len < 1) {
a3a04f
-			return NT_STATUS_BUFFER_TOO_SMALL;
a3a04f
-		}
a3a04f
-		pad = 1;
a3a04f
-		buf_len -= pad;
a3a04f
-		buf += pad;
a3a04f
+	    ((align_odd && (offset % 2 == 0)) ||
a3a04f
+	     (!align_odd && (offset % 2 == 1)))) {
a3a04f
+		pad += 1;
a3a04f
+		offset += 1;
a3a04f
+	}
a3a04f
+
a3a04f
+	if (offset > buf_len) {
a3a04f
+		return NT_STATUS_BUFFER_TOO_SMALL;
a3a04f
 	}
a3a04f
 
a3a04f
+	buf_len -= offset;
a3a04f
+	buf += offset;
a3a04f
+
a3a04f
 	if (ucs2) {
a3a04f
 		buf_len = utf16_len_n(buf, buf_len);
a3a04f
 	} else {
a3a04f
@@ -361,17 +375,18 @@ static NTSTATUS internal_bytes_pull_str(TALLOC_CTX *mem_ctx, char **_str,
a3a04f
 		return map_nt_error_from_unix_common(errno);
a3a04f
 	}
a3a04f
 
a3a04f
-	if (pbuf_consumed != NULL) {
a3a04f
-		*pbuf_consumed = buf_len + pad;
a3a04f
+	if (p_consumed != NULL) {
a3a04f
+		*p_consumed = buf_len + pad;
a3a04f
 	}
a3a04f
 	*_str = str;
a3a04f
-	return NT_STATUS_OK;;
a3a04f
+	return NT_STATUS_OK;
a3a04f
 }
a3a04f
 
a3a04f
 NTSTATUS smb_bytes_pull_str(TALLOC_CTX *mem_ctx, char **_str, bool ucs2,
a3a04f
 			    const uint8_t *buf, size_t buf_len,
a3a04f
-			    size_t *_buf_consumed)
a3a04f
+			    const uint8_t *position,
a3a04f
+			    size_t *_consumed)
a3a04f
 {
a3a04f
 	return internal_bytes_pull_str(mem_ctx, _str, ucs2, true,
a3a04f
-				       buf, buf_len, _buf_consumed);
a3a04f
+				       buf, buf_len, position, _consumed);
a3a04f
 }
a3a04f
-- 
a3a04f
2.13.1
a3a04f
a3a04f
a3a04f
From 460941fe916d787057437412eef64c0ffdd1f65d Mon Sep 17 00:00:00 2001
a3a04f
From: Stefan Metzmacher <metze@samba.org>
a3a04f
Date: Wed, 15 Mar 2017 17:04:44 +0000
a3a04f
Subject: [PATCH 2/2] s3:libsmb: add cli_state_update_after_sesssetup() helper
a3a04f
 function
a3a04f
a3a04f
This function updates cli->server_{os,type,domain} to valid values
a3a04f
after a session setup.
a3a04f
a3a04f
BUG: https://bugzilla.samba.org/show_bug.cgi?id=12779
a3a04f
a3a04f
Signed-off-by: Stefan Metzmacher <metze@samba.org>
a3a04f
Reviewed-by: Andreas Schneider <asn@samba.org>
a3a04f
(cherry picked from commit e0069bd2a4820eca17c59d91bd1853f2f053a7a3)
a3a04f
---
a3a04f
 source3/libsmb/cliconnect.c | 74 +++++++++++++++++++++++++++++++--------------
a3a04f
 1 file changed, 52 insertions(+), 22 deletions(-)
a3a04f
a3a04f
diff --git a/source3/libsmb/cliconnect.c b/source3/libsmb/cliconnect.c
a3a04f
index a2362ceb863..ef03da17eec 100644
a3a04f
--- a/source3/libsmb/cliconnect.c
a3a04f
+++ b/source3/libsmb/cliconnect.c
a3a04f
@@ -372,6 +372,38 @@ NTSTATUS cli_session_creds_prepare_krb5(struct cli_state *cli,
a3a04f
 	return NT_STATUS_OK;
a3a04f
 }
a3a04f
 
a3a04f
+static NTSTATUS cli_state_update_after_sesssetup(struct cli_state *cli,
a3a04f
+						 const char *native_os,
a3a04f
+						 const char *native_lm,
a3a04f
+						 const char *primary_domain)
a3a04f
+{
a3a04f
+#define _VALID_STR(p) ((p) != NULL && (p)[0] != '\0')
a3a04f
+
a3a04f
+	if (!_VALID_STR(cli->server_os) && _VALID_STR(native_os)) {
a3a04f
+		cli->server_os = talloc_strdup(cli, native_os);
a3a04f
+		if (cli->server_os == NULL) {
a3a04f
+			return NT_STATUS_NO_MEMORY;
a3a04f
+		}
a3a04f
+	}
a3a04f
+
a3a04f
+	if (!_VALID_STR(cli->server_type) && _VALID_STR(native_lm)) {
a3a04f
+		cli->server_type = talloc_strdup(cli, native_lm);
a3a04f
+		if (cli->server_type == NULL) {
a3a04f
+			return NT_STATUS_NO_MEMORY;
a3a04f
+		}
a3a04f
+	}
a3a04f
+
a3a04f
+	if (!_VALID_STR(cli->server_domain) && _VALID_STR(primary_domain)) {
a3a04f
+		cli->server_domain = talloc_strdup(cli, primary_domain);
a3a04f
+		if (cli->server_domain == NULL) {
a3a04f
+			return NT_STATUS_NO_MEMORY;
a3a04f
+		}
a3a04f
+	}
a3a04f
+
a3a04f
+#undef _VALID_STRING
a3a04f
+	return NT_STATUS_OK;
a3a04f
+}
a3a04f
+
a3a04f
 /********************************************************
a3a04f
  Utility function to ensure we always return at least
a3a04f
  a valid char * pointer to an empty string for the
a3a04f
@@ -762,7 +794,6 @@ static void cli_sesssetup_blob_done(struct tevent_req *subreq)
a3a04f
 		subreq, struct tevent_req);
a3a04f
 	struct cli_sesssetup_blob_state *state = tevent_req_data(
a3a04f
 		req, struct cli_sesssetup_blob_state);
a3a04f
-	struct cli_state *cli = state->cli;
a3a04f
 	NTSTATUS status;
a3a04f
 
a3a04f
 	if (smbXcli_conn_protocol(state->cli->conn) >= PROTOCOL_SMB2_02) {
a3a04f
@@ -784,15 +815,16 @@ static void cli_sesssetup_blob_done(struct tevent_req *subreq)
a3a04f
 		return;
a3a04f
 	}
a3a04f
 
a3a04f
-	if (cli->server_os == NULL) {
a3a04f
-		cli->server_os = talloc_move(cli, &state->out_native_os);
a3a04f
-	}
a3a04f
-	if (cli->server_type == NULL) {
a3a04f
-		cli->server_type = talloc_move(cli, &state->out_native_lm);
a3a04f
-	}
a3a04f
-
a3a04f
 	state->status = status;
a3a04f
 
a3a04f
+	status = cli_state_update_after_sesssetup(state->cli,
a3a04f
+						  state->out_native_os,
a3a04f
+						  state->out_native_lm,
a3a04f
+						  NULL);
a3a04f
+	if (tevent_req_nterror(req, status)) {
a3a04f
+		return;
a3a04f
+	}
a3a04f
+
a3a04f
 	if (state->blob.length != 0) {
a3a04f
 		/*
a3a04f
 		 * More to send
a3a04f
@@ -1667,14 +1699,12 @@ static void cli_session_setup_creds_done_nt1(struct tevent_req *subreq)
a3a04f
 		return;
a3a04f
 	}
a3a04f
 
a3a04f
-	if (cli->server_os == NULL) {
a3a04f
-		cli->server_os = talloc_move(cli, &state->out_native_os);
a3a04f
-	}
a3a04f
-	if (cli->server_type == NULL) {
a3a04f
-		cli->server_type = talloc_move(cli, &state->out_native_lm);
a3a04f
-	}
a3a04f
-	if (cli->server_domain == NULL) {
a3a04f
-		cli->server_domain = talloc_move(cli, &state->out_primary_domain);
a3a04f
+	status = cli_state_update_after_sesssetup(state->cli,
a3a04f
+						  state->out_native_os,
a3a04f
+						  state->out_native_lm,
a3a04f
+						  state->out_primary_domain);
a3a04f
+	if (tevent_req_nterror(req, status)) {
a3a04f
+		return;
a3a04f
 	}
a3a04f
 
a3a04f
 	ok = smb1cli_conn_activate_signing(cli->conn,
a3a04f
@@ -1707,7 +1737,6 @@ static void cli_session_setup_creds_done_lm21(struct tevent_req *subreq)
a3a04f
 		subreq, struct tevent_req);
a3a04f
 	struct cli_session_setup_creds_state *state = tevent_req_data(
a3a04f
 		req, struct cli_session_setup_creds_state);
a3a04f
-	struct cli_state *cli = state->cli;
a3a04f
 	NTSTATUS status;
a3a04f
 
a3a04f
 	status = smb1cli_session_setup_lm21_recv(subreq, state,
a3a04f
@@ -1720,11 +1749,12 @@ static void cli_session_setup_creds_done_lm21(struct tevent_req *subreq)
a3a04f
 		return;
a3a04f
 	}
a3a04f
 
a3a04f
-	if (cli->server_os == NULL) {
a3a04f
-		cli->server_os = talloc_move(cli, &state->out_native_os);
a3a04f
-	}
a3a04f
-	if (cli->server_type == NULL) {
a3a04f
-		cli->server_type = talloc_move(cli, &state->out_native_lm);
a3a04f
+	status = cli_state_update_after_sesssetup(state->cli,
a3a04f
+						  state->out_native_os,
a3a04f
+						  state->out_native_lm,
a3a04f
+						  NULL);
a3a04f
+	if (tevent_req_nterror(req, status)) {
a3a04f
+		return;
a3a04f
 	}
a3a04f
 
a3a04f
 	tevent_req_done(req);
a3a04f
-- 
a3a04f
2.13.1
a3a04f