8ff60a
From 222b16ac61329dc819ab5b9ccd3276c5a1a01c8f Mon Sep 17 00:00:00 2001
8ff60a
From: Stefan Metzmacher <metze@samba.org>
8ff60a
Date: Thu, 2 Jul 2020 14:32:34 +0200
8ff60a
Subject: [PATCH 1/3] s4:torture/smb2: add smb2.delete-on-close-perms.BUG14427
8ff60a
8ff60a
BUG: https://bugzilla.samba.org/show_bug.cgi?id=14427
8ff60a
8ff60a
Signed-off-by: Stefan Metzmacher <metze@samba.org>
8ff60a
Reviewed-by: Ralph Boehme <slow@samba.org>
8ff60a
(cherry picked from commit bcba4bb210d9482be4c2c8dadfb5cc185046cbaa)
8ff60a
---
8ff60a
 selftest/knownfail.d/bug14427          |  1 +
8ff60a
 source4/torture/smb2/delete-on-close.c | 43 +++++++++++++++++++++++++-
8ff60a
 2 files changed, 43 insertions(+), 1 deletion(-)
8ff60a
 create mode 100644 selftest/knownfail.d/bug14427
8ff60a
8ff60a
diff --git a/selftest/knownfail.d/bug14427 b/selftest/knownfail.d/bug14427
8ff60a
new file mode 100644
8ff60a
index 00000000000..e136465ef87
8ff60a
--- /dev/null
8ff60a
+++ b/selftest/knownfail.d/bug14427
8ff60a
@@ -0,0 +1 @@
8ff60a
+^samba3.smb2.delete-on-close-perms.BUG14427
8ff60a
diff --git a/source4/torture/smb2/delete-on-close.c b/source4/torture/smb2/delete-on-close.c
8ff60a
index 3c495750f43..05242876dcb 100644
8ff60a
--- a/source4/torture/smb2/delete-on-close.c
8ff60a
+++ b/source4/torture/smb2/delete-on-close.c
8ff60a
@@ -698,6 +698,46 @@ static bool test_doc_read_only(struct torture_context *tctx,
8ff60a
 	return ret;
8ff60a
 }
8ff60a
 
8ff60a
+/*
8ff60a
+ * This is a regression test for
8ff60a
+ * https://bugzilla.samba.org/show_bug.cgi?id=14427
8ff60a
+ *
8ff60a
+ * It's not really a delete-on-close specific test.
8ff60a
+ */
8ff60a
+static bool test_doc_bug14427(struct torture_context *tctx, struct smb2_tree *tree1)
8ff60a
+{
8ff60a
+	struct smb2_tree *tree2 = NULL;
8ff60a
+	NTSTATUS status;
8ff60a
+	char fname[256];
8ff60a
+	bool ret = false;
8ff60a
+	bool ok;
8ff60a
+
8ff60a
+	/* Add some random component to the file name. */
8ff60a
+	snprintf(fname, sizeof(fname), "doc_bug14427_%s.dat",
8ff60a
+		 generate_random_str(tctx, 8));
8ff60a
+
8ff60a
+	ok = torture_smb2_tree_connect(tctx, tree1->session, tctx, &tree2);
8ff60a
+	torture_assert_goto(tctx, ok, ret, done,
8ff60a
+		"torture_smb2_tree_connect() failed.\n");
8ff60a
+
8ff60a
+	status = torture_setup_simple_file(tctx, tree1, fname);
8ff60a
+	torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
8ff60a
+		"torture_setup_simple_file() failed on tree1.\n");
8ff60a
+
8ff60a
+	status = smb2_util_unlink(tree2, fname);
8ff60a
+	torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
8ff60a
+		"smb2_util_unlink() failed on tree2.\n");
8ff60a
+	TALLOC_FREE(tree2);
8ff60a
+	ret = true;
8ff60a
+done:
8ff60a
+	if (tree2 != NULL) {
8ff60a
+		TALLOC_FREE(tree2);
8ff60a
+		smb2_util_unlink(tree1, fname);
8ff60a
+	}
8ff60a
+
8ff60a
+	TALLOC_FREE(tree1);
8ff60a
+	return ret;
8ff60a
+}
8ff60a
 
8ff60a
 /*
8ff60a
  *  Extreme testing of Delete On Close and permissions
8ff60a
@@ -713,7 +753,8 @@ struct torture_suite *torture_smb2_doc_init(TALLOC_CTX *ctx)
8ff60a
 	torture_suite_add_1smb2_test(suite, "CREATE_IF", test_doc_create_if);
8ff60a
 	torture_suite_add_1smb2_test(suite, "CREATE_IF Existing", test_doc_create_if_exist);
8ff60a
 	torture_suite_add_1smb2_test(suite, "FIND_and_set_DOC", test_doc_find_and_set_doc);
8ff60a
-	torture_suite_add_1smb2_test(suite,  "READONLY", test_doc_read_only);
8ff60a
+	torture_suite_add_1smb2_test(suite, "READONLY", test_doc_read_only);
8ff60a
+	torture_suite_add_1smb2_test(suite, "BUG14427", test_doc_bug14427);
8ff60a
 
8ff60a
 	suite->description = talloc_strdup(suite, "SMB2-Delete-on-Close-Perms tests");
8ff60a
 
8ff60a
-- 
8ff60a
2.26.2
8ff60a
8ff60a
8ff60a
From a6005fb5155a7c7886b179e7672b198a55e69380 Mon Sep 17 00:00:00 2001
8ff60a
From: Stefan Metzmacher <metze@samba.org>
8ff60a
Date: Thu, 2 Jul 2020 12:06:28 +0200
8ff60a
Subject: [PATCH 2/3] s3:smbd: reformat if statement for caching in vfs_ChDir()
8ff60a
8ff60a
BUG: https://bugzilla.samba.org/show_bug.cgi?id=14427
8ff60a
8ff60a
Signed-off-by: Stefan Metzmacher <metze@samba.org>
8ff60a
Reviewed-by: Ralph Boehme <slow@samba.org>
8ff60a
(cherry picked from commit b2b5ae090ee8796609eb0b5794bc4e62c24414ef)
8ff60a
---
8ff60a
 source3/smbd/vfs.c | 5 +++--
8ff60a
 1 file changed, 3 insertions(+), 2 deletions(-)
8ff60a
8ff60a
diff --git a/source3/smbd/vfs.c b/source3/smbd/vfs.c
8ff60a
index 96067e45005..7c8f99bbd41 100644
8ff60a
--- a/source3/smbd/vfs.c
8ff60a
+++ b/source3/smbd/vfs.c
8ff60a
@@ -879,8 +879,9 @@ int vfs_ChDir(connection_struct *conn, const struct smb_filename *smb_fname)
8ff60a
 		return 0;
8ff60a
 	}
8ff60a
 
8ff60a
-	if (*smb_fname->base_name == '/' &&
8ff60a
-			strcsequal(LastDir,smb_fname->base_name)) {
8ff60a
+	if (smb_fname->base_name[0] == '/' &&
8ff60a
+	    strcsequal(LastDir,smb_fname->base_name))
8ff60a
+	{
8ff60a
 		return 0;
8ff60a
 	}
8ff60a
 
8ff60a
-- 
8ff60a
2.26.2
8ff60a
8ff60a
8ff60a
From 735fd5fe21b4c365946806e79df668cec22b3210 Mon Sep 17 00:00:00 2001
8ff60a
From: Stefan Metzmacher <metze@samba.org>
8ff60a
Date: Wed, 1 Jul 2020 09:38:58 +0200
8ff60a
Subject: [PATCH 3/3] s3:smbd: make sure vfs_ChDir() always sets
8ff60a
 conn->cwd_fsp->fh->fd = AT_FDCWD
8ff60a
8ff60a
This is what all consumers of conn->cwd_fsp->fh->fd expect!
8ff60a
8ff60a
BUG: https://bugzilla.samba.org/show_bug.cgi?id=14427
8ff60a
8ff60a
Signed-off-by: Stefan Metzmacher <metze@samba.org>
8ff60a
Reviewed-by: Ralph Boehme <slow@samba.org>
8ff60a
(cherry picked from commit f3f330f61db983f6d213a097d9a4d91b1057ecb1)
8ff60a
---
8ff60a
 selftest/knownfail.d/bug14427 |  1 -
8ff60a
 source3/smbd/vfs.c            | 35 +++++++++++++++++++++++++++++++++++
8ff60a
 2 files changed, 35 insertions(+), 1 deletion(-)
8ff60a
 delete mode 100644 selftest/knownfail.d/bug14427
8ff60a
8ff60a
diff --git a/selftest/knownfail.d/bug14427 b/selftest/knownfail.d/bug14427
8ff60a
deleted file mode 100644
8ff60a
index e136465ef87..00000000000
8ff60a
--- a/selftest/knownfail.d/bug14427
8ff60a
+++ /dev/null
8ff60a
@@ -1 +0,0 @@
8ff60a
-^samba3.smb2.delete-on-close-perms.BUG14427
8ff60a
diff --git a/source3/smbd/vfs.c b/source3/smbd/vfs.c
8ff60a
index 7c8f99bbd41..411999c3856 100644
8ff60a
--- a/source3/smbd/vfs.c
8ff60a
+++ b/source3/smbd/vfs.c
8ff60a
@@ -876,12 +876,47 @@ int vfs_ChDir(connection_struct *conn, const struct smb_filename *smb_fname)
8ff60a
 	}
8ff60a
 
8ff60a
 	if (ISDOT(smb_fname->base_name)) {
8ff60a
+		/*
8ff60a
+		 * passing a '.' is a noop,
8ff60a
+		 * and we only expect this after
8ff60a
+		 * everything is initialized.
8ff60a
+		 *
8ff60a
+		 * So the first vfs_ChDir() on a given
8ff60a
+		 * connection_struct must not be '.'.
8ff60a
+		 *
8ff60a
+		 * Note: conn_new() sets
8ff60a
+		 * conn->cwd_fsp->fh->fd = -1
8ff60a
+		 * and vfs_ChDir() leaves with
8ff60a
+		 * conn->cwd_fsp->fh->fd = AT_FDCWD
8ff60a
+		 * on success!
8ff60a
+		 */
8ff60a
+		if (conn->cwd_fsp->fh->fd != AT_FDCWD) {
8ff60a
+			/*
8ff60a
+			 * This should never happen and
8ff60a
+			 * we might change this to
8ff60a
+			 * SMB_ASSERT() in future.
8ff60a
+			 */
8ff60a
+			DBG_ERR("Called with '.' as first operation!\n");
8ff60a
+			log_stack_trace();
8ff60a
+			errno = EINVAL;
8ff60a
+			return -1;
8ff60a
+		}
8ff60a
 		return 0;
8ff60a
 	}
8ff60a
 
8ff60a
 	if (smb_fname->base_name[0] == '/' &&
8ff60a
 	    strcsequal(LastDir,smb_fname->base_name))
8ff60a
 	{
8ff60a
+		/*
8ff60a
+		 * conn->cwd_fsp->fsp_name and the kernel
8ff60a
+		 * are already correct, but conn->cwd_fsp->fh->fd
8ff60a
+		 * might still be -1 as initialized in conn_new().
8ff60a
+		 *
8ff60a
+		 * This can happen when a client made a 2nd
8ff60a
+		 * tree connect to a share with the same underlying
8ff60a
+		 * path (may or may not the same share).
8ff60a
+		 */
8ff60a
+		conn->cwd_fsp->fh->fd = AT_FDCWD;
8ff60a
 		return 0;
8ff60a
 	}
8ff60a
 
8ff60a
-- 
8ff60a
2.26.2
8ff60a