801d0a
From 5e94fe726e9af81374c697ce603b3728ccaaebf3 Mon Sep 17 00:00:00 2001
801d0a
From: Jeremy Allison <jra@samba.org>
801d0a
Date: Fri, 12 Jul 2019 12:10:35 -0700
801d0a
Subject: [PATCH 1/6] CVE-2019-10197: smbd: separate out impersonation debug
801d0a
 info into a new function.
801d0a
801d0a
Will be called on elsewhere on successful impersonation.
801d0a
801d0a
BUG: https://bugzilla.samba.org/show_bug.cgi?id=14035
801d0a
801d0a
Signed-off-by: Jeremy Allison <jra@samba.org>
801d0a
Reviewed-by: Ralph Boehme <slow@samba.org>
801d0a
Reviewed-by: Stefan Metzmacher <metze@samba.org>
801d0a
---
801d0a
 source3/smbd/uid.c | 37 +++++++++++++++++++++++--------------
801d0a
 1 file changed, 23 insertions(+), 14 deletions(-)
801d0a
801d0a
diff --git a/source3/smbd/uid.c b/source3/smbd/uid.c
801d0a
index a4bcb747d37e..ce8e8d92131c 100644
801d0a
--- a/source3/smbd/uid.c
801d0a
+++ b/source3/smbd/uid.c
801d0a
@@ -279,6 +279,28 @@ static bool check_user_ok(connection_struct *conn,
801d0a
 	return(True);
801d0a
 }
801d0a
 
801d0a
+static void print_impersonation_info(connection_struct *conn)
801d0a
+{
801d0a
+	struct smb_filename *cwdfname = NULL;
801d0a
+
801d0a
+	if (!CHECK_DEBUGLVL(DBGLVL_INFO)) {
801d0a
+		return;
801d0a
+	}
801d0a
+
801d0a
+	cwdfname = vfs_GetWd(talloc_tos(), conn);
801d0a
+	if (cwdfname == NULL) {
801d0a
+		return;
801d0a
+	}
801d0a
+
801d0a
+	DBG_INFO("Impersonated user: uid=(%d,%d), gid=(%d,%d), cwd=[%s]\n",
801d0a
+		 (int)getuid(),
801d0a
+		 (int)geteuid(),
801d0a
+		 (int)getgid(),
801d0a
+		 (int)getegid(),
801d0a
+		 cwdfname->base_name);
801d0a
+	TALLOC_FREE(cwdfname);
801d0a
+}
801d0a
+
801d0a
 /****************************************************************************
801d0a
  Become the user of a connection number without changing the security context
801d0a
  stack, but modify the current_user entries.
801d0a
@@ -415,20 +437,7 @@ static bool change_to_user_internal(connection_struct *conn,
801d0a
 		current_user.done_chdir = true;
801d0a
 	}
801d0a
 
801d0a
-	if (CHECK_DEBUGLVL(DBGLVL_INFO)) {
801d0a
-		struct smb_filename *cwdfname = vfs_GetWd(talloc_tos(), conn);
801d0a
-		if (cwdfname == NULL) {
801d0a
-			return false;
801d0a
-		}
801d0a
-		DBG_INFO("Impersonated user: uid=(%d,%d), gid=(%d,%d), cwd=[%s]\n",
801d0a
-			 (int)getuid(),
801d0a
-			 (int)geteuid(),
801d0a
-			 (int)getgid(),
801d0a
-			 (int)getegid(),
801d0a
-			 cwdfname->base_name);
801d0a
-		TALLOC_FREE(cwdfname);
801d0a
-	}
801d0a
-
801d0a
+	print_impersonation_info(conn);
801d0a
 	return true;
801d0a
 }
801d0a
 
801d0a
-- 
801d0a
2.17.1
801d0a
801d0a
801d0a
From b4cd0dcbc38ae61cfb075e5f659384df889e99f7 Mon Sep 17 00:00:00 2001
801d0a
From: Stefan Metzmacher <metze@samba.org>
801d0a
Date: Thu, 11 Jul 2019 17:01:29 +0200
801d0a
Subject: [PATCH 2/6] CVE-2019-10197: smbd: make sure that
801d0a
 change_to_user_internal() always resets current_user.done_chdir
801d0a
801d0a
We should not leave current_user.done_chdir as true if we didn't call
801d0a
chdir_current_service() with success.
801d0a
801d0a
This caused problems in when calling vfs_ChDir() in pop_conn_ctx() when
801d0a
chdir_current_service() worked once on one share but later failed on another
801d0a
share.
801d0a
801d0a
BUG: https://bugzilla.samba.org/show_bug.cgi?id=14035
801d0a
801d0a
Signed-off-by: Stefan Metzmacher <metze@samba.org>
801d0a
Reviewed-by: Ralph Boehme <slow@samba.org>
801d0a
---
801d0a
 source3/smbd/uid.c | 1 +
801d0a
 1 file changed, 1 insertion(+)
801d0a
801d0a
diff --git a/source3/smbd/uid.c b/source3/smbd/uid.c
801d0a
index ce8e8d92131c..77a81f602988 100644
801d0a
--- a/source3/smbd/uid.c
801d0a
+++ b/source3/smbd/uid.c
801d0a
@@ -427,6 +427,7 @@ static bool change_to_user_internal(connection_struct *conn,
801d0a
 	current_user.conn = conn;
801d0a
 	current_user.vuid = vuid;
801d0a
 	current_user.need_chdir = conn->tcon_done;
801d0a
+	current_user.done_chdir = false;
801d0a
 
801d0a
 	if (current_user.need_chdir) {
801d0a
 		ok = chdir_current_service(conn);
801d0a
-- 
801d0a
2.17.1
801d0a
801d0a
801d0a
From b1496ce793129302c9959ebc6330219c6a3143f0 Mon Sep 17 00:00:00 2001
801d0a
From: Stefan Metzmacher <metze@samba.org>
801d0a
Date: Tue, 18 Jun 2019 14:04:08 +0200
801d0a
Subject: [PATCH 3/6] CVE-2019-10197: smbd: make sure we reset
801d0a
 current_user.{need,done}_chdir in become_root()
801d0a
801d0a
BUG: https://bugzilla.samba.org/show_bug.cgi?id=14035
801d0a
801d0a
Signed-off-by: Stefan Metzmacher <metze@samba.org>
801d0a
---
801d0a
 source3/smbd/uid.c | 3 +++
801d0a
 1 file changed, 3 insertions(+)
801d0a
801d0a
diff --git a/source3/smbd/uid.c b/source3/smbd/uid.c
801d0a
index 77a81f602988..50868ba8572a 100644
801d0a
--- a/source3/smbd/uid.c
801d0a
+++ b/source3/smbd/uid.c
801d0a
@@ -624,6 +624,9 @@ void smbd_become_root(void)
801d0a
 	}
801d0a
 	push_conn_ctx();
801d0a
 	set_root_sec_ctx();
801d0a
+
801d0a
+	current_user.need_chdir = false;
801d0a
+	current_user.done_chdir = false;
801d0a
 }
801d0a
 
801d0a
 /* Unbecome the root user */
801d0a
-- 
801d0a
2.17.1
801d0a
801d0a
801d0a
From 03a0719d6d5c1a81b44bc3cedc76563a1eb04491 Mon Sep 17 00:00:00 2001
801d0a
From: Stefan Metzmacher <metze@samba.org>
801d0a
Date: Tue, 30 Jul 2019 17:16:59 +0200
801d0a
Subject: [PATCH 4/6] CVE-2019-10197: selftest: make fsrvp_share its own
801d0a
 independent subdirectory
801d0a
801d0a
The next patch will otherwise break the fsrvp related tests.
801d0a
801d0a
BUG: https://bugzilla.samba.org/show_bug.cgi?id=14035
801d0a
801d0a
Signed-off-by: Stefan Metzmacher <metze@samba.org>
801d0a
---
801d0a
 selftest/target/Samba3.pm | 7 +++++--
801d0a
 1 file changed, 5 insertions(+), 2 deletions(-)
801d0a
801d0a
diff --git a/selftest/target/Samba3.pm b/selftest/target/Samba3.pm
801d0a
index 9d88253c9fe7..f7eb314138a0 100755
801d0a
--- a/selftest/target/Samba3.pm
801d0a
+++ b/selftest/target/Samba3.pm
801d0a
@@ -1540,6 +1540,9 @@ sub provision($$$$$$$$$)
801d0a
 	my $widelinks_linkdir="$shrdir/widelinks_foo";
801d0a
 	push(@dirs,$widelinks_linkdir);
801d0a
 
801d0a
+	my $fsrvp_shrdir="$shrdir/fsrvp";
801d0a
+	push(@dirs,$fsrvp_shrdir);
801d0a
+
801d0a
 	my $shadow_tstdir="$shrdir/shadow";
801d0a
 	push(@dirs,$shadow_tstdir);
801d0a
 	my $shadow_mntdir="$shadow_tstdir/mount";
801d0a
@@ -2083,14 +2086,14 @@ sub provision($$$$$$$$$)
801d0a
 	guest ok = yes
801d0a
 
801d0a
 [fsrvp_share]
801d0a
-	path = $shrdir
801d0a
+	path = $fsrvp_shrdir
801d0a
 	comment = fake shapshots using rsync
801d0a
 	vfs objects = shell_snap shadow_copy2
801d0a
 	shell_snap:check path command = $fake_snap_pl --check
801d0a
 	shell_snap:create command = $fake_snap_pl --create
801d0a
 	shell_snap:delete command = $fake_snap_pl --delete
801d0a
 	# a relative path here fails, the snapshot dir is no longer found
801d0a
-	shadow:snapdir = $shrdir/.snapshots
801d0a
+	shadow:snapdir = $fsrvp_shrdir/.snapshots
801d0a
 
801d0a
 [shadow1]
801d0a
 	path = $shadow_shrdir
801d0a
-- 
801d0a
2.17.1
801d0a
801d0a
801d0a
From 409447f3258b87745a2248570278b1c6da8991f4 Mon Sep 17 00:00:00 2001
801d0a
From: Stefan Metzmacher <metze@samba.org>
801d0a
Date: Tue, 16 Jul 2019 15:40:38 +0200
801d0a
Subject: [PATCH 5/6] CVE-2019-10197: test_smbclient_s3.sh: add regression test
801d0a
 for the no permission on share root problem
801d0a
801d0a
BUG: https://bugzilla.samba.org/show_bug.cgi?id=14035
801d0a
801d0a
Signed-off-by: Stefan Metzmacher <metze@samba.org>
801d0a
---
801d0a
 selftest/knownfail.d/CVE-2019-10197       |  1 +
801d0a
 selftest/target/Samba3.pm                 | 12 +++++++++
801d0a
 source3/script/tests/test_smbclient_s3.sh | 30 +++++++++++++++++++++++
801d0a
 3 files changed, 43 insertions(+)
801d0a
 create mode 100644 selftest/knownfail.d/CVE-2019-10197
801d0a
801d0a
diff --git a/selftest/knownfail.d/CVE-2019-10197 b/selftest/knownfail.d/CVE-2019-10197
801d0a
new file mode 100644
801d0a
index 000000000000..f7056bbf3ad4
801d0a
--- /dev/null
801d0a
+++ b/selftest/knownfail.d/CVE-2019-10197
801d0a
@@ -0,0 +1 @@
801d0a
+^samba3.blackbox.smbclient_s3.*.noperm.share.regression
801d0a
diff --git a/selftest/target/Samba3.pm b/selftest/target/Samba3.pm
801d0a
index f7eb314138a0..2f491441815f 100755
801d0a
--- a/selftest/target/Samba3.pm
801d0a
+++ b/selftest/target/Samba3.pm
801d0a
@@ -1516,6 +1516,9 @@ sub provision($$$$$$$$$)
801d0a
 	my $ro_shrdir="$shrdir/root-tmp";
801d0a
 	push(@dirs,$ro_shrdir);
801d0a
 
801d0a
+	my $noperm_shrdir="$shrdir/noperm-tmp";
801d0a
+	push(@dirs,$noperm_shrdir);
801d0a
+
801d0a
 	my $msdfs_shrdir="$shrdir/msdfsshare";
801d0a
 	push(@dirs,$msdfs_shrdir);
801d0a
 
801d0a
@@ -1586,6 +1589,11 @@ sub provision($$$$$$$$$)
801d0a
 	chmod 0755, $piddir;
801d0a
 
801d0a
 
801d0a
+	##
801d0a
+	## Create a directory without permissions to enter
801d0a
+	##
801d0a
+	chmod 0000, $noperm_shrdir;
801d0a
+
801d0a
 	##
801d0a
 	## create ro and msdfs share layout
801d0a
 	##
801d0a
@@ -1902,6 +1910,10 @@ sub provision($$$$$$$$$)
801d0a
 [ro-tmp]
801d0a
 	path = $ro_shrdir
801d0a
 	guest ok = yes
801d0a
+[noperm]
801d0a
+	path = $noperm_shrdir
801d0a
+	wide links = yes
801d0a
+	guest ok = yes
801d0a
 [write-list-tmp]
801d0a
 	path = $shrdir
801d0a
         read only = yes
801d0a
diff --git a/source3/script/tests/test_smbclient_s3.sh b/source3/script/tests/test_smbclient_s3.sh
801d0a
index bf033ccd2fbf..0bae1d78fac9 100755
801d0a
--- a/source3/script/tests/test_smbclient_s3.sh
801d0a
+++ b/source3/script/tests/test_smbclient_s3.sh
801d0a
@@ -1329,6 +1329,32 @@ EOF
801d0a
     fi
801d0a
 }
801d0a
 
801d0a
+#
801d0a
+# Regression test for CVE-2019-10197
801d0a
+# we should always get ACCESS_DENIED
801d0a
+#
801d0a
+test_noperm_share_regression()
801d0a
+{
801d0a
+    cmd='$SMBCLIENT -U$USERNAME%$PASSWORD //$SERVER/noperm -I $SERVER_IP $LOCAL_ADDARGS -c "ls;ls"  2>&1'
801d0a
+    eval echo "$cmd"
801d0a
+    out=`eval $cmd`
801d0a
+    ret=$?
801d0a
+    if [ $ret -eq 0 ] ; then
801d0a
+       echo "$out"
801d0a
+       echo "failed accessing no perm share should not work"
801d0a
+       return 1
801d0a
+    fi
801d0a
+
801d0a
+    num=`echo "$out" | grep 'NT_STATUS_ACCESS_DENIED' | wc -l`
801d0a
+    if [ "$num" -ne "2" ] ; then
801d0a
+       echo "$out"
801d0a
+       echo "failed num[$num] - two NT_STATUS_ACCESS_DENIED lines expected"
801d0a
+       return 1
801d0a
+    fi
801d0a
+
801d0a
+    return 0
801d0a
+}
801d0a
+
801d0a
 # Test smbclient deltree command
801d0a
 test_deltree()
801d0a
 {
801d0a
@@ -1857,6 +1883,10 @@ testit "follow local symlinks" \
801d0a
     test_local_symlinks || \
801d0a
     failed=`expr $failed + 1`
801d0a
 
801d0a
+testit "noperm share regression" \
801d0a
+    test_noperm_share_regression || \
801d0a
+    failed=`expr $failed + 1`
801d0a
+
801d0a
 testit "smbclient deltree command" \
801d0a
     test_deltree || \
801d0a
     failed=`expr $failed + 1`
801d0a
-- 
801d0a
2.17.1
801d0a
801d0a
801d0a
From 501e034aa5b6ba50bf14e41c59674fbbc28a2e9c Mon Sep 17 00:00:00 2001
801d0a
From: Stefan Metzmacher <metze@samba.org>
801d0a
Date: Thu, 11 Jul 2019 17:02:15 +0200
801d0a
Subject: [PATCH 6/6] CVE-2019-10197: smbd: split change_to_user_impersonate()
801d0a
 out of change_to_user_internal()
801d0a
801d0a
This makes sure we always call chdir_current_service() even
801d0a
when we still impersonated the user. Which is important
801d0a
in order to run the SMB* request within the correct working directory
801d0a
and only if the user has permissions to enter that directory.
801d0a
801d0a
It makes sure we always update conn->lastused_count
801d0a
in chdir_current_service() for each request.
801d0a
801d0a
Note that vfs_ChDir() (called from chdir_current_service())
801d0a
maintains its own cache and avoids calling SMB_VFS_CHDIR()
801d0a
if possible.
801d0a
801d0a
It means we still avoid syscalls if we get a multiple requests
801d0a
for the same session/tcon tuple.
801d0a
801d0a
BUG: https://bugzilla.samba.org/show_bug.cgi?id=14035
801d0a
801d0a
Signed-off-by: Stefan Metzmacher <metze@samba.org>
801d0a
Reviewed-by: Ralph Boehme <slow@samba.org>
801d0a
---
801d0a
 selftest/knownfail.d/CVE-2019-10197 |  1 -
801d0a
 source3/smbd/uid.c                  | 21 +++++++++++++++++----
801d0a
 2 files changed, 17 insertions(+), 5 deletions(-)
801d0a
 delete mode 100644 selftest/knownfail.d/CVE-2019-10197
801d0a
801d0a
diff --git a/selftest/knownfail.d/CVE-2019-10197 b/selftest/knownfail.d/CVE-2019-10197
801d0a
deleted file mode 100644
801d0a
index f7056bbf3ad4..000000000000
801d0a
--- a/selftest/knownfail.d/CVE-2019-10197
801d0a
+++ /dev/null
801d0a
@@ -1 +0,0 @@
801d0a
-^samba3.blackbox.smbclient_s3.*.noperm.share.regression
801d0a
diff --git a/source3/smbd/uid.c b/source3/smbd/uid.c
801d0a
index 50868ba8572a..5c39baade5cf 100644
801d0a
--- a/source3/smbd/uid.c
801d0a
+++ b/source3/smbd/uid.c
801d0a
@@ -306,9 +306,9 @@ static void print_impersonation_info(connection_struct *conn)
801d0a
  stack, but modify the current_user entries.
801d0a
 ****************************************************************************/
801d0a
 
801d0a
-static bool change_to_user_internal(connection_struct *conn,
801d0a
-				    const struct auth_session_info *session_info,
801d0a
-				    uint64_t vuid)
801d0a
+static bool change_to_user_impersonate(connection_struct *conn,
801d0a
+				       const struct auth_session_info *session_info,
801d0a
+				       uint64_t vuid)
801d0a
 {
801d0a
 	int snum;
801d0a
 	gid_t gid;
801d0a
@@ -321,7 +321,6 @@ static bool change_to_user_internal(connection_struct *conn,
801d0a
 
801d0a
 	if ((current_user.conn == conn) &&
801d0a
 	    (current_user.vuid == vuid) &&
801d0a
-	    (current_user.need_chdir == conn->tcon_done) &&
801d0a
 	    (current_user.ut.uid == session_info->unix_token->uid))
801d0a
 	{
801d0a
 		DBG_INFO("Skipping user change - already user\n");
801d0a
@@ -426,6 +425,20 @@ static bool change_to_user_internal(connection_struct *conn,
801d0a
 
801d0a
 	current_user.conn = conn;
801d0a
 	current_user.vuid = vuid;
801d0a
+	return true;
801d0a
+}
801d0a
+
801d0a
+static bool change_to_user_internal(connection_struct *conn,
801d0a
+				    const struct auth_session_info *session_info,
801d0a
+				    uint64_t vuid)
801d0a
+{
801d0a
+	bool ok;
801d0a
+
801d0a
+	ok = change_to_user_impersonate(conn, session_info, vuid);
801d0a
+	if (!ok) {
801d0a
+		return false;
801d0a
+	}
801d0a
+
801d0a
 	current_user.need_chdir = conn->tcon_done;
801d0a
 	current_user.done_chdir = false;
801d0a
 
801d0a
-- 
801d0a
2.17.1
801d0a