[RFC PATCH xfstests] cifs: new test to check directory rename with an nonposix open child

ChenXiaoSong <[email protected]>
Newsgroups org.kernel.vger.fstests,org.kernel.vger.linux-cifs
Message-ID <[email protected]>
From: ChenXiaoSong <[email protected]>

Add a test to verify that renaming a directory fails with EACCES
while a file in it is held open without a POSIX create context.

Link: https://lore.kernel.org/linux-cifs/[email protected]/
Signed-off-by: ChenXiaoSong <[email protected]>
---
 tests/cifs/002     | 76 ++++++++++++++++++++++++++++++++++++++++++++++
 tests/cifs/002.out |  2 ++
 2 files changed, 78 insertions(+)
 create mode 100755 tests/cifs/002
 create mode 100644 tests/cifs/002.out

diff --git a/tests/cifs/002 b/tests/cifs/002
new file mode 100755
index 00000000..aa011fdf
--- /dev/null
+++ b/tests/cifs/002
@@ -0,0 +1,76 @@
+#! /bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (C) 2026 KylinSoft Co., Ltd. All rights reserved.
+# Author(s): ChenXiaoSong <[email protected]>
+#
+# FS QA Test No. 002
+#
+# Regression test for a ksmbd bug where a directory could be renamed while a
+# file in it was held open without a POSIX create context.
+#
+. ./common/preamble
+_begin_fstest auto quick rename
+
+_require_scratch
+_require_test_program "rename"
+
+# TODO: This patch has not been merged into mainline yet.
+# https://lore.kernel.org/linux-cifs/[email protected]/
+# _fixed_by_kernel_commit xxxxxxxx \
+# 	"smb/server: fix posix state check for directory rename"
+
+testdir=$SCRATCH_MNT/test-$seq
+srcdir=$testdir/dir1
+dstdir=$testdir/dir2
+
+_cleanup()
+{
+	exec 73<&-
+	rm -rf "$testdir"
+	rm -f $tmp.*
+}
+
+# SMB3.1.1 is needed to negotiate POSIX extensions with the server.  Disable
+# POSIX create contexts explicitly so that the opens below use normal SMB
+# handles, regardless of the mount options in the test configuration.
+_try_scratch_mount -o vers=3.1.1,noposix >> $seqres.full 2>&1 || \
+	_notrun "mounting the scratch share with vers=3.1.1,noposix failed"
+
+# Make sure a conflicting option from the test configuration did not win.  The
+# CIFS client reports the noposix alias as nounix in /proc/mounts.
+_fs_options "$SCRATCH_DEV" | grep -Eq '(^|,)nounix(,|$)' || \
+	_notrun "the scratch share was not mounted with POSIX extensions disabled"
+
+rm -rf "$testdir"
+mkdir -p "$testdir/control" "$srcdir" || \
+	_fail "failed to create test directories"
+
+# Verify that ordinary directory renames are permitted on this share, so an
+# EACCES result below cannot be caused by a read-only share or its permissions.
+$here/src/rename "$testdir/control" "$testdir/control-renamed" \
+	>> $seqres.full 2>&1 || _fail "ordinary directory rename failed"
+
+touch "$srcdir/file" || _fail "failed to create child file"
+
+# Keep a normal SMB handle open for a child of the directory.  Renaming the
+# directory must be rejected with EACCES until that handle is closed.
+exec 73< "$srcdir/file" || _fail "failed to open child file"
+
+$here/src/rename "$srcdir" "$dstdir" > $tmp.rename 2>&1
+ret=$?
+cat $tmp.rename >> $seqres.full
+if [ $ret -eq 0 ]; then
+	_fail "directory rename with an open child unexpectedly succeeded"
+fi
+
+if ! grep -qx 'rename: Permission denied' $tmp.rename; then
+	cat $tmp.rename >> $seqres.full
+	_fail "directory rename failed with an unexpected error"
+fi
+
+exec 73<&-
+
+echo "Silence is golden"
+
+status=0
+exit
diff --git a/tests/cifs/002.out b/tests/cifs/002.out
new file mode 100644
index 00000000..61705c7c
--- /dev/null
+++ b/tests/cifs/002.out
@@ -0,0 +1,2 @@
+QA output created by 002
+Silence is golden
-- 
2.53.0
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.