fe3d10
From 10f485b3a27e10906aa6ee40833fca8bf81b5511 Mon Sep 17 00:00:00 2001
fe3d10
From: Stefan Metzmacher <metze@samba.org>
fe3d10
Date: Sat, 22 Jan 2022 01:08:26 +0100
fe3d10
Subject: [PATCH] dcesrv_core: wrap gensec_*() calls in [un]become_root() calls
fe3d10
fe3d10
This is important for the source3/rpc_server code as it might
fe3d10
be called embedded in smbd and may not run as root with access
fe3d10
to our private tdb/ldb files.
fe3d10
fe3d10
Note this is only really needed for 4.15 and older, as
fe3d10
we no longer run the rpc_server embedded in smbd,
fe3d10
but we better be consistent for now.
fe3d10
fe3d10
This should be able to fix the problem the printing no longer works
fe3d10
on Windows 7 with 2021-10 monthly rollup patch (KB5006743).
fe3d10
fe3d10
Windows uses NTLMSSP with privacy at the DCERPC layer on top
fe3d10
of NCACN_NP (smb).
fe3d10
fe3d10
BUG: https://bugzilla.samba.org/show_bug.cgi?id=14867
fe3d10
fe3d10
Signed-off-by: Stefan Metzmacher <metze@samba.org>
fe3d10
Reviewed-by: Andreas Schneider <asn@samba.org>
fe3d10
(cherry picked from commit 0651fa474cd68b18d8eb9bdc7c4ba5b847ba9ad9)
fe3d10
---
fe3d10
 librpc/rpc/dcesrv_auth.c         |  5 +++++
fe3d10
 librpc/rpc/dcesrv_core.c         | 18 ++++++++++++++++++
fe3d10
 librpc/rpc/dcesrv_core.h         |  2 ++
fe3d10
 source3/rpc_server/rpc_config.c  |  2 ++
fe3d10
 source4/rpc_server/service_rpc.c | 10 ++++++++++
fe3d10
 5 files changed, 37 insertions(+)
fe3d10
fe3d10
diff --git a/librpc/rpc/dcesrv_auth.c b/librpc/rpc/dcesrv_auth.c
fe3d10
index fec8df513a83..99d8e0162160 100644
fe3d10
--- a/librpc/rpc/dcesrv_auth.c
fe3d10
+++ b/librpc/rpc/dcesrv_auth.c
fe3d10
@@ -130,11 +130,13 @@ static bool dcesrv_auth_prepare_gensec(struct dcesrv_call_state *call)
fe3d10
 	auth->auth_level = call->in_auth_info.auth_level;
fe3d10
 	auth->auth_context_id = call->in_auth_info.auth_context_id;
fe3d10
 
fe3d10
+	cb->auth.become_root();
fe3d10
 	status = cb->auth.gensec_prepare(
fe3d10
 		auth,
fe3d10
 		call,
fe3d10
 		&auth->gensec_security,
fe3d10
 		cb->auth.private_data);
fe3d10
+	cb->auth.unbecome_root();
fe3d10
 	if (!NT_STATUS_IS_OK(status)) {
fe3d10
 		DEBUG(1, ("Failed to call samba_server_gensec_start %s\n",
fe3d10
 			  nt_errstr(status)));
fe3d10
@@ -329,6 +331,7 @@ bool dcesrv_auth_bind(struct dcesrv_call_state *call)
fe3d10
 NTSTATUS dcesrv_auth_complete(struct dcesrv_call_state *call, NTSTATUS status)
fe3d10
 {
fe3d10
 	struct dcesrv_auth *auth = call->auth_state;
fe3d10
+	struct dcesrv_context_callbacks *cb = call->conn->dce_ctx->callbacks;
fe3d10
 	const char *pdu = "<unknown>";
fe3d10
 
fe3d10
 	switch (call->pkt.ptype) {
fe3d10
@@ -359,9 +362,11 @@ NTSTATUS dcesrv_auth_complete(struct dcesrv_call_state *call, NTSTATUS status)
fe3d10
 		return status;
fe3d10
 	}
fe3d10
 
fe3d10
+	cb->auth.become_root();
fe3d10
 	status = gensec_session_info(auth->gensec_security,
fe3d10
 				     auth,
fe3d10
 				     &auth->session_info);
fe3d10
+	cb->auth.unbecome_root();
fe3d10
 	if (!NT_STATUS_IS_OK(status)) {
fe3d10
 		DEBUG(1, ("Failed to establish session_info: %s\n",
fe3d10
 			  nt_errstr(status)));
fe3d10
diff --git a/librpc/rpc/dcesrv_core.c b/librpc/rpc/dcesrv_core.c
fe3d10
index d16159b0b6cd..ea91fc689b4a 100644
fe3d10
--- a/librpc/rpc/dcesrv_core.c
fe3d10
+++ b/librpc/rpc/dcesrv_core.c
fe3d10
@@ -938,6 +938,7 @@ static NTSTATUS dcesrv_bind(struct dcesrv_call_state *call)
fe3d10
 	struct dcerpc_binding *ep_2nd_description = NULL;
fe3d10
 	const char *endpoint = NULL;
fe3d10
 	struct dcesrv_auth *auth = call->auth_state;
fe3d10
+	struct dcesrv_context_callbacks *cb = call->conn->dce_ctx->callbacks;
fe3d10
 	struct dcerpc_ack_ctx *ack_ctx_list = NULL;
fe3d10
 	struct dcerpc_ack_ctx *ack_features = NULL;
fe3d10
 	struct tevent_req *subreq = NULL;
fe3d10
@@ -1143,9 +1144,11 @@ static NTSTATUS dcesrv_bind(struct dcesrv_call_state *call)
fe3d10
 		return dcesrv_auth_reply(call);
fe3d10
 	}
fe3d10
 
fe3d10
+	cb->auth.become_root();
fe3d10
 	subreq = gensec_update_send(call, call->event_ctx,
fe3d10
 				    auth->gensec_security,
fe3d10
 				    call->in_auth_info.credentials);
fe3d10
+	cb->auth.unbecome_root();
fe3d10
 	if (subreq == NULL) {
fe3d10
 		return NT_STATUS_NO_MEMORY;
fe3d10
 	}
fe3d10
@@ -1160,10 +1163,13 @@ static void dcesrv_bind_done(struct tevent_req *subreq)
fe3d10
 		tevent_req_callback_data(subreq,
fe3d10
 		struct dcesrv_call_state);
fe3d10
 	struct dcesrv_connection *conn = call->conn;
fe3d10
+	struct dcesrv_context_callbacks *cb = call->conn->dce_ctx->callbacks;
fe3d10
 	NTSTATUS status;
fe3d10
 
fe3d10
+	cb->auth.become_root();
fe3d10
 	status = gensec_update_recv(subreq, call,
fe3d10
 				    &call->out_auth_info->credentials);
fe3d10
+	cb->auth.unbecome_root();
fe3d10
 	TALLOC_FREE(subreq);
fe3d10
 
fe3d10
 	status = dcesrv_auth_complete(call, status);
fe3d10
@@ -1221,6 +1227,7 @@ static NTSTATUS dcesrv_auth3(struct dcesrv_call_state *call)
fe3d10
 {
fe3d10
 	struct dcesrv_connection *conn = call->conn;
fe3d10
 	struct dcesrv_auth *auth = call->auth_state;
fe3d10
+	struct dcesrv_context_callbacks *cb = call->conn->dce_ctx->callbacks;
fe3d10
 	struct tevent_req *subreq = NULL;
fe3d10
 	NTSTATUS status;
fe3d10
 
fe3d10
@@ -1265,9 +1272,11 @@ static NTSTATUS dcesrv_auth3(struct dcesrv_call_state *call)
fe3d10
 		return NT_STATUS_OK;
fe3d10
 	}
fe3d10
 
fe3d10
+	cb->auth.become_root();
fe3d10
 	subreq = gensec_update_send(call, call->event_ctx,
fe3d10
 				    auth->gensec_security,
fe3d10
 				    call->in_auth_info.credentials);
fe3d10
+	cb->auth.unbecome_root();
fe3d10
 	if (subreq == NULL) {
fe3d10
 		return NT_STATUS_NO_MEMORY;
fe3d10
 	}
fe3d10
@@ -1283,10 +1292,13 @@ static void dcesrv_auth3_done(struct tevent_req *subreq)
fe3d10
 		struct dcesrv_call_state);
fe3d10
 	struct dcesrv_connection *conn = call->conn;
fe3d10
 	struct dcesrv_auth *auth = call->auth_state;
fe3d10
+	struct dcesrv_context_callbacks *cb = call->conn->dce_ctx->callbacks;
fe3d10
 	NTSTATUS status;
fe3d10
 
fe3d10
+	cb->auth.become_root();
fe3d10
 	status = gensec_update_recv(subreq, call,
fe3d10
 				    &call->out_auth_info->credentials);
fe3d10
+	cb->auth.unbecome_root();
fe3d10
 	TALLOC_FREE(subreq);
fe3d10
 
fe3d10
 	status = dcesrv_auth_complete(call, status);
fe3d10
@@ -1555,6 +1567,7 @@ static NTSTATUS dcesrv_alter(struct dcesrv_call_state *call)
fe3d10
 	struct ncacn_packet *pkt = &call->ack_pkt;
fe3d10
 	uint32_t extra_flags = 0;
fe3d10
 	struct dcesrv_auth *auth = call->auth_state;
fe3d10
+	struct dcesrv_context_callbacks *cb = call->conn->dce_ctx->callbacks;
fe3d10
 	struct dcerpc_ack_ctx *ack_ctx_list = NULL;
fe3d10
 	struct tevent_req *subreq = NULL;
fe3d10
 	size_t i;
fe3d10
@@ -1666,9 +1679,11 @@ static NTSTATUS dcesrv_alter(struct dcesrv_call_state *call)
fe3d10
 		return dcesrv_auth_reply(call);
fe3d10
 	}
fe3d10
 
fe3d10
+	cb->auth.become_root();
fe3d10
 	subreq = gensec_update_send(call, call->event_ctx,
fe3d10
 				    auth->gensec_security,
fe3d10
 				    call->in_auth_info.credentials);
fe3d10
+	cb->auth.unbecome_root();
fe3d10
 	if (subreq == NULL) {
fe3d10
 		return NT_STATUS_NO_MEMORY;
fe3d10
 	}
fe3d10
@@ -1683,10 +1698,13 @@ static void dcesrv_alter_done(struct tevent_req *subreq)
fe3d10
 		tevent_req_callback_data(subreq,
fe3d10
 		struct dcesrv_call_state);
fe3d10
 	struct dcesrv_connection *conn = call->conn;
fe3d10
+	struct dcesrv_context_callbacks *cb = call->conn->dce_ctx->callbacks;
fe3d10
 	NTSTATUS status;
fe3d10
 
fe3d10
+	cb->auth.become_root();
fe3d10
 	status = gensec_update_recv(subreq, call,
fe3d10
 				    &call->out_auth_info->credentials);
fe3d10
+	cb->auth.unbecome_root();
fe3d10
 	TALLOC_FREE(subreq);
fe3d10
 
fe3d10
 	status = dcesrv_auth_complete(call, status);
fe3d10
diff --git a/librpc/rpc/dcesrv_core.h b/librpc/rpc/dcesrv_core.h
fe3d10
index d8d5f9030959..0538442e0ce6 100644
fe3d10
--- a/librpc/rpc/dcesrv_core.h
fe3d10
+++ b/librpc/rpc/dcesrv_core.h
fe3d10
@@ -392,6 +392,8 @@ struct dcesrv_context_callbacks {
fe3d10
 			struct gensec_security **out,
fe3d10
 			void *private_data);
fe3d10
 		void *private_data;
fe3d10
+		void (*become_root)(void);
fe3d10
+		void (*unbecome_root)(void);
fe3d10
 	} auth;
fe3d10
 	struct {
fe3d10
 		NTSTATUS (*find)(
fe3d10
diff --git a/source3/rpc_server/rpc_config.c b/source3/rpc_server/rpc_config.c
fe3d10
index 2f1a01da1c0b..289c4f398409 100644
fe3d10
--- a/source3/rpc_server/rpc_config.c
fe3d10
+++ b/source3/rpc_server/rpc_config.c
fe3d10
@@ -31,6 +31,8 @@
fe3d10
 static struct dcesrv_context_callbacks srv_callbacks = {
fe3d10
 	.log.successful_authz = dcesrv_log_successful_authz,
fe3d10
 	.auth.gensec_prepare = dcesrv_auth_gensec_prepare,
fe3d10
+	.auth.become_root = become_root,
fe3d10
+	.auth.unbecome_root = unbecome_root,
fe3d10
 	.assoc_group.find = dcesrv_assoc_group_find,
fe3d10
 };
fe3d10
 
fe3d10
diff --git a/source4/rpc_server/service_rpc.c b/source4/rpc_server/service_rpc.c
fe3d10
index d8c6746d7815..ebb50f8a7ef3 100644
fe3d10
--- a/source4/rpc_server/service_rpc.c
fe3d10
+++ b/source4/rpc_server/service_rpc.c
fe3d10
@@ -40,9 +40,19 @@
fe3d10
 #include "../libcli/named_pipe_auth/npa_tstream.h"
fe3d10
 #include "samba/process_model.h"
fe3d10
 
fe3d10
+static void skip_become_root(void)
fe3d10
+{
fe3d10
+}
fe3d10
+
fe3d10
+static void skip_unbecome_root(void)
fe3d10
+{
fe3d10
+}
fe3d10
+
fe3d10
 static struct dcesrv_context_callbacks srv_callbacks = {
fe3d10
 	.log.successful_authz = log_successful_dcesrv_authz_event,
fe3d10
 	.auth.gensec_prepare = dcesrv_gensec_prepare,
fe3d10
+	.auth.become_root = skip_become_root,
fe3d10
+	.auth.unbecome_root = skip_unbecome_root,
fe3d10
 	.assoc_group.find = dcesrv_assoc_group_find,
fe3d10
 };
fe3d10
 
fe3d10
-- 
fe3d10
2.25.1
fe3d10