Skip to contentSkip to conversation

ksmbd: test smb_check_perm_dacl() DACL walk boundary

PATCH In review Hang Nan 2 participants 4 messages Aug 18 – Aug 19 1 message not in the archive

Subsystems (2)
lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

Hang Nan

author
#1 lore raw

PATCH v3 1/3 ksmbd: add KUnit test for the DACL walk boundary

smb_check_perm_dacl() must stop walking ACEs at the DACL declared size instead of using the enclosing security descriptor length.

Add the ksmbd KUnit test configuration and a semantic harness that verifies a crafted access-granting ACE beyond the declared DACL size is ignored.

---
 fs/smb/server/Kconfig       |  15 ++++
 fs/smb/server/Makefile      |   1 +
 fs/smb/server/smbacl_test.c | 170 ++++++++++++++++++++++++++++++++++++
 3 files changed, 186 insertions(+)
 create mode 100644 fs/smb/server/smbacl_test.c
fs/smb/server/Kconfig +15 -0
index 08d8b7a965a6..463fa19819b2 100644
@@ -72,3 +72,18 @@ config SMB_SERVER_KERBEROS5
 	bool "Support for Kerberos 5"
 	depends on SMB_SERVER
 	default y
+
+config SMB_SERVER_KUNIT_TESTS
+	tristate "KUnit tests for SMB3 server helpers" if !KUNIT_ALL_TESTS
+	depends on SMB_SERVER && SMB_KUNIT_TESTS && TMPFS_XATTR
+	default SMB_KUNIT_TESTS
+
+	help
+	  KUnit tests for ksmbd server helpers such as the DACL access
+	  check in smb_check_perm_dacl().  The tests use crafted security
+	  descriptors stored in tmpfs NTACL xattrs to exercise the real
+	  permission-checking path.  This option is only useful for kernel
+	  developers; enable it together with CONFIG_SMB_KUNIT_TESTS.
+
+	  For more information on KUnit and unit tests in the kernel,
+	  please read Documentation/dev-tools/kunit/index.rst.
fs/smb/server/Makefile +1 -0
index a3e9306055e8..62228679fc9a 100644
@@ -19,3 +19,4 @@ $(obj)/ksmbd_spnego_negtokentarg.asn1.o: $(obj)/ksmbd_spnego_negtokentarg.asn1.c
 
 ksmbd-$(CONFIG_SMB_SERVER_SMBDIRECT) += transport_rdma.o
 ksmbd-$(CONFIG_PROC_FS) += proc.o
+obj-$(CONFIG_SMB_SERVER_KUNIT_TESTS) += smbacl_test.o
fs/smb/server/smbacl_test.c new file +170 -0

170 lines not on this page · raw message

Signature · 1 line
2.55.0
maintainer
#2 lore raw

Re: [PATCH v3 1/3] ksmbd: add KUnit test for the DACL walk boundary

Quoted 10 lines
fs/smb/server/Kconfig +2 -0
index 08d8b7a965a6..463fa19819b2 100644
@@ -72,3 +72,18 @@ config SMB_SERVER_KERBEROS5
        bool "Support for Kerberos 5"
        depends on SMB_SERVER
        default y
+
+config SMB_SERVER_KUNIT_TESTS

fs/smb/server/Kconfig config SMB_SERVER_KERBEROS5 · line 86

Quoted 10 lines
fs/smb/server/Kconfig +10 -0
+       tristate "KUnit tests for SMB3 server helpers" if !KUNIT_ALL_TESTS
+       depends on SMB_SERVER && SMB_KUNIT_TESTS && TMPFS_XATTR
+       default SMB_KUNIT_TESTS
+
+       help
+         KUnit tests for ksmbd server helpers such as the DACL access
+         check in smb_check_perm_dacl().  The tests use crafted security
+         descriptors stored in tmpfs NTACL xattrs to exercise the real
+         permission-checking path.  This option is only useful for kernel
+         developers; enable it together with CONFIG_SMB_KUNIT_TESTS.

The help text is currently tied to this particular test and its tmpfs NTACL implementation. Since SMB_SERVER_KUNIT_TESTS may cover more server tests in the future, please keep the description more generic. Also, SMB_KUNIT_TESTS is already listed in the dependency, so it does not need to be mentioned separately in the help text.

fs/smb/server/smbacl_test.c

Quoted 13 lines
fs/smb/server/Kconfig +3 -0
+
+         For more information on KUnit and unit tests in the kernel,
+         please read Documentation/dev-tools/kunit/index.rst.
fs/smb/server/Makefile +1 -0
index a3e9306055e8..62228679fc9a 100644
@@ -19,3 +19,4 @@ $(obj)/ksmbd_spnego_negtokentarg.asn1.o: $(obj)/ksmbd_spnego_negtokentarg.asn1.c
 
 ksmbd-$(CONFIG_SMB_SERVER_SMBDIRECT) += transport_rdma.o
 ksmbd-$(CONFIG_PROC_FS) += proc.o
+obj-$(CONFIG_SMB_SERVER_KUNIT_TESTS) += smbacl_test.o
fs/smb/server/smbacl_test.c +0 -0

Also, could the test be moved under fs/smb/server/tests/, for example:

fs/smb/server/tests/smbacl_kunit.c

with a tests/Kconfig and tests/Makefile. This follows the usual KUnit layout and will make it easier to add more server-side tests later. These are not functional issues, but it seems worthwhile to get the layout right while adding the first test.
Thanks!

author
#3 lore raw

PATCH v3 2/3 ksmbd: test smb_check_perm_dacl() DACL walk boundary

In reply to a message not in the archive

Drive smb_check_perm_dacl() through ksmbd's NTACL xattr path with a crafted descriptor whose second ACE is beyond the declared DACL size.

Verify that the out-of-boundary ACE is not selected and access remains denied.

---
 fs/smb/server/smbacl.c      |  2 +
 fs/smb/server/smbacl_test.c | 90 +++++++++++++++++++++++++++++++++++++
 fs/smb/server/vfs.c         |  2 +
 3 files changed, 94 insertions(+)
fs/smb/server/smbacl.c +2 -0
index 8ad2e5a5cca8..5b2d46c09da7 100644
@@ -7,6 +7,7 @@
  */
 
 #include <linux/fs.h>
+#include <kunit/visibility.h>
 #include <linux/slab.h>
 #include <linux/string.h>
 #include <linux/mnt_idmapping.h>
@@ -1652,6 +1653,7 @@ int smb_check_perm_dacl(struct ksmbd_conn *conn, const struct path *path,
 	kfree(pntsd);
 	return rc;
 }
+EXPORT_SYMBOL_IF_KUNIT(smb_check_perm_dacl);
 
 int set_info_sec(struct ksmbd_conn *conn, struct ksmbd_tree_connect *tcon,
 		 const struct path *path, struct smb_ntsd *pntsd, int ntsd_len,
fs/smb/server/smbacl_test.c +90 -0

114 lines not on this page · raw message

fs/smb/server/vfs.c +2 -0
index 286536f75144..536300f9eb8f 100644
@@ -5,6 +5,7 @@
  */
 
 #include <crypto/sha2.h>
+#include <kunit/visibility.h>
 #include <linux/kernel.h>
 #include <linux/fs.h>
 #include <linux/filelock.h>
@@ -1668,6 +1669,7 @@ int ksmbd_vfs_set_sd_xattr(struct ksmbd_conn *conn,
 	kfree(def_smb_acl);
 	return rc;
 }
+EXPORT_SYMBOL_IF_KUNIT(ksmbd_vfs_set_sd_xattr);
 
 int ksmbd_vfs_get_sd_xattr(struct ksmbd_conn *conn,
 			   struct mnt_idmap *idmap,
Signature · 1 line
2.55.0
author
#4 lore raw

PATCH v3 3/3 ksmbd: test maximal-access DACL walk boundary

In reply to a message not in the archive

Add a maximal-access variant of the smb_check_perm_dacl() boundary test. The in-boundary ACE grants read access, while a trailing ACE beyond the declared DACL size grants write access.

Verify that maximal-access calculation includes the in-boundary permission and ignores the trailing permission.

---
 fs/smb/server/smbacl_test.c | 47 ++++++++++++++++++++++++++++++++++---
 1 file changed, 44 insertions(+), 3 deletions(-)
fs/smb/server/smbacl_test.c +44 -3
index d1703b83aeff..f1f7722bb5d5 100644
@@ -12,10 +12,11 @@
  *   sits beyond struct smb_acl::size; stopping at the declared DACL
  *   size (the fixed behaviour) rejects it.
  *
- * - ksmbd_smb_check_perm_dacl_boundary: drives the real
+ * - ksmbd_smb_check_perm_dacl_boundary and
+ *   ksmbd_smb_check_perm_dacl_maximal_boundary: drive the real
  *   smb_check_perm_dacl() with a descriptor stored through ksmbd's own
- *   NTACL xattr path on a tmpfs file, and asserts that a post-boundary
- *   ACE is not selected for a regular access check.
+ *   NTACL xattr path on a tmpfs file, and assert that a post-boundary
+ *   ACE is not selected for either a regular or maximal access check.
  */
 
 #include <kunit/test.h>
@@ -242,9 +243,49 @@ static void ksmbd_smb_check_perm_dacl_boundary_test(struct kunit *test)
 	fput(file);
 }
 
+static void
+ksmbd_smb_check_perm_dacl_maximal_boundary_test(struct kunit *test)
+{
+	struct file *file;
+	struct smb_ntsd *pntsd;
+	__le32 daccess = FILE_MAXIMAL_ACCESS_LE;
+	int ntsd_size, rc;
+
+	/*
+	 * The in-boundary ACE grants read access.  The trailing ACE grants
+	 * write access, which must not be included in the maximal access mask.
+	 */
+	pntsd = build_boundary_ntsd(test, &test_owner_sid, FILE_READ_DATA,
+				     FILE_WRITE_DATA, &ntsd_size);
+	KUNIT_ASSERT_NOT_NULL(test, pntsd);
+
+	file = shmem_file_setup("ksmbd-kunit-dacl-maximal", 0,
+				mk_vma_flags(VMA_NORESERVE_BIT));
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, file);
+
+	rc = ksmbd_vfs_set_sd_xattr(NULL, mnt_idmap(file->f_path.mnt),
+				    &file->f_path, pntsd, ntsd_size,
+				    false);
+	KUNIT_EXPECT_EQ(test, 0, rc);
+	if (rc)
+		goto out;
+
+	rc = smb_check_perm_dacl(NULL, &file->f_path, &daccess,
+				 FILE_MAXIMAL_ACCESS_LE, 0, false);
+	KUNIT_EXPECT_EQ(test, 0, rc);
+	if (rc)
+		goto out;
+
+	KUNIT_EXPECT_TRUE(test, le32_to_cpu(daccess) & FILE_READ_DATA);
+	KUNIT_EXPECT_FALSE(test, le32_to_cpu(daccess) & FILE_WRITE_DATA);
+out:
+	fput(file);
+}
+
 static struct kunit_case ksmbd_smbacl_test_cases[] = {
 	KUNIT_CASE(ksmbd_dacl_walk_must_stop_at_declared_size),
 	KUNIT_CASE(ksmbd_smb_check_perm_dacl_boundary_test),
+	KUNIT_CASE(ksmbd_smb_check_perm_dacl_maximal_boundary_test),
 	{}
 };
 
Signature · 1 line
2.55.0