Skip to contentSkip to conversation

smbd: deny rename if the destination has non-POSIX opens

RFC PATCH In review ChenXiaoSong 2 participants 2 messages Aug 5

Subsystems (2)
lore
Share & tools
lore

ChenXiaoSong

author reviewer
#0 lore raw

RFC PATCH 1/1 smbd: deny rename if the destination has non-POSIX opens

From: ChenXiaoSong <[email protected]>

Check the destination share mode entries with has_other_nonposix_opens() before replacing it, and return NT_STATUS_ACCESS_DENIED if a non-POSIX open exists.

---
 source3/smbd/smb2_reply.c | 48 +++++++++------------------------------
 1 file changed, 11 insertions(+), 37 deletions(-)
source3/smbd/smb2_reply.c +11 -37
index 6bdc02c2374..c3da096205e 100644
@@ -1312,31 +1312,6 @@ static void notify_rename(struct connection_struct *conn,
 	TALLOC_FREE(parent_dir_dst);
 }
 
-struct rename_check_open_state {
-	struct files_struct *dst_fsp;
-	struct file_id fileid;
-};
-
-static struct files_struct *rename_check_open_fn(struct files_struct *fsp,
-						 void *private_data)
-{
-	struct rename_check_open_state *state = private_data;
-
-	if (fsp == state->dst_fsp) {
-		return NULL;
-	}
-
-	if (!fsp->fsp_flags.is_fsa) {
-		return NULL;
-	}
-
-	if (!file_id_equal(&fsp->file_id, &state->fileid)) {
-		return NULL;
-	}
-
-	return fsp;
-}
-
 /****************************************************************************
  Rename an open file - given an fsp.
 ****************************************************************************/
@@ -1483,11 +1458,9 @@ NTSTATUS rename_internals_fsp(connection_struct *conn,
 	}
 
 	if (VALID_STAT(smb_fname_dst->st)) {
-
-		struct rename_check_open_state check_state = {
-			.dst_fsp = smb_fname_dst->fsp,
-		};
-		struct files_struct *found_open = NULL;
+		struct file_id fileid;
+		struct share_mode_lock *dst_lck = NULL;
+		bool has_other_open = false;
 
 		if (!replace_if_exists) {
 			DBG_NOTICE("dest exists doing rename "
@@ -1498,13 +1471,14 @@ NTSTATUS rename_internals_fsp(connection_struct *conn,
 			goto out;
 		}
 
-		check_state.fileid = vfs_file_id_from_sbuf(conn,
-							   &smb_fname_dst->st);
-
-		found_open = files_forall(conn->sconn,
-					  rename_check_open_fn,
-					  &check_state);
-		if (found_open != NULL) {
+		fileid = vfs_file_id_from_sbuf(conn, &smb_fname_dst->st);
+		dst_lck = fetch_share_mode_unlocked(ctx, fileid);
+		if (dst_lck != NULL) {
+			has_other_open = has_other_nonposix_opens(
+				dst_lck, smb_fname_dst->fsp);
+			TALLOC_FREE(dst_lck);
+		}
+		if (has_other_open) {
 			DBG_NOTICE("Target file open\n");
 			status = NT_STATUS_ACCESS_DENIED;
 			goto out;
Signature · 1 line
2.54.0
#1 lore raw
Quoted 5 lines · ChenXiaoSong

From: ChenXiaoSong <[email protected]>

Check the destination share mode entries with has_other_nonposix_opens() before replacing it, and return NT_STATUS_ACCESS_DENIED if a non-POSIX open exists.

thanks for looking into this!

I'm afraid though that the issue is more subtle then just adjusting the check in that place.

In fact, back when I implemented Directory Leases, I added the full logic to check for open files and possible H-leases on them on
- the src of the rename
- iirc also the parent of the src of the rename
- the parent of the dst
- and the dst

All that logic lives in a few levels avove rename_internals_fsp() inside smbd_smb2_setinfo_lease_break_check().

The function that deals with the dst is smbd_smb2_setinfo_rename_dst_check(). That one already correctly uses has_other_nonposix_opens() while holding a lock on the locking.tdb record of the destination.

The check with files_forall() inside rename_internals_fsp() is a clutch that already check for opens in the same process, not globally. And we can't expand it to be global, as that requires taking the lock on the locking.tdb record which is prohibitted by the fact that we already have the lck on the src when rename_internals_fsp() is called. Locking two records simultaneously is not allowed in Samba, as it could lead to deadlocks.

So I guess the best we can do is remove the redundant check altogether from rename_internals_fsp() and rely on the correct logic from smbd_smb2_setinfo_lease_break_check(). We have to live with the toctou race window anyway, it just enlarges the window a bit, but only for the case of open files by the same client. Opens from other clients, are not detected anyway by the files_forall() as said above.