Skip to contentSkip to conversation

ksmbd: fail a non-directory delete-on-close open of a directory

PATCH New Ian Bridges 1 participant 1 message Nov 27

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ksmbd: fail a non-directory delete-on-close open of a directory

smb2_open() fails an open with FILE_NON_DIRECTORY_FILE when the target is a directory, unless FILE_DELETE_ON_CLOSE is also set. With both options the directory opens and is marked delete-on-close. An empty directory is then removed at close, although the client asked for a non-directory. A directory that is not empty stays, and the close still succeeds.

MS-FSA requires STATUS_FILE_IS_A_DIRECTORY for a non-directory open of the unnamed data stream of a directory, and delete-on-close makes no difference. Fail that open whatever the other options are. A delete-on-close open of a named stream of a directory still succeeds and removes the stream at close.

Fixes e2f34481b24d ("cifsd: add server-side procedures for SMB3")

---

Tested on ksmbd-for-next 3c31349456c5 with KASAN. An open of a directory with FILE_NON_DIRECTORY_FILE and FILE_DELETE_ON_CLOSE now fails with STATUS_FILE_IS_A_DIRECTORY, and the directory stays. Before, an empty directory was removed at close. A directory that was not empty stayed, and the close returned STATUS_SUCCESS. A delete-on-close open of a named stream of a directory still removes the stream.

smbtorture first tries to remove a test directory with such an open. On the base that open succeeded, so later tests in the same run found the old directory and failed. Those tests now pass, and each of them gives the same result on the base when run alone. The other smbtorture results are the same as on the base.

This patch was developed with LLM assistance.

 fs/smb/server/smb2pdu.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
fs/smb/server/smb2pdu.c +2 -1
index 5a7842264b98..28fb77bcd3dc 100644
@@ -4976,7 +4976,8 @@ int smb2_open(struct ksmbd_work *work)
 
 	if (file_present && req->CreateOptions & FILE_NON_DIRECTORY_FILE_LE &&
 	    S_ISDIR(d_inode(path.dentry)->i_mode) &&
-	    !(req->CreateOptions & FILE_DELETE_ON_CLOSE_LE)) {
+	    (!stream_name ||
+	     !(req->CreateOptions & FILE_DELETE_ON_CLOSE_LE))) {
 		ksmbd_debug(SMB, "open() argument is a directory: %s, %x\n",
 			    name, le32_to_cpu(req->CreateOptions));
 		rsp->hdr.Status = STATUS_FILE_IS_A_DIRECTORY;
Signature · 1 line
2.47.3