smb/server: check destination is open when renaming
Subsystems (2)
[email protected]
reviewerPATCH 1/1 smb/server: check destination is open when renaming
From: ChenXiaoSong <[email protected]>
Reproducer:
1. server: systemctl start ksmbd
2. client: mount -t cifs //${server_ip}/export /mnt
3. client: touch /mnt/file1 /mnt/file2
4. client: C program: int fd = open("/mnt/file2", O_RDONLY);
5. client: C program: rename("/mnt/file1", "/mnt/file2");
6. client: C program: struct stat stbuf; fstat(fd, &stbuf); stbuf.st_nlink is 1, should be 0
Before this patch, packets captured by tcpdump were as follows:
- SetInfo Request FILE_INFO/SMB2_FILE_RENAME_INFO NewName:file2
- SetInfo Response
If the destination file of a rename operation is already open, ksmbd should return STATUS_ACCESS_DENIED to client.
After this patch, packets captured by tcpdump are as follows:
- SetInfo Request FILE_INFO/SMB2_FILE_RENAME_INFO NewName:file2
- SetInfo Response, Error: STATUS_ACCESS_DENIED
- ...
- Create Request, File: .__smbfile_silly1d; \
GetInfo Request FILE_INFO/SMB2_FILE_ALL_INFO; \
Close Request
- Create Response, Error: STATUS_OBJECT_NAME_NOT_FOUND; \
GetInfo Response, Error: STATUS_FILE_CLOSED; \
Close Response, Error: STATUS_INVALID_PARAMETER
- Create Request, File: file2; \
SetInfo Request FILE_INFO/SMB2_FILE_BASIC_INFO; \
SetInfo Request FILE_INFO/SMB2_FILE_RENAME_INFO NewName:.__smbfile_silly1d; \
SetInfo Request FILE_INFO/SMB2_FILE_DISPOSITION_INFO; \
Close Request
- Create Response, File: file2; \
SetInfo Response; \
SetInfo Response; \
SetInfo Response; \
Close Response - SetInfo Request FILE_INFO/SMB2_FILE_RENAME_INFO NewName:file2
- SetInfo Response
This patch fixes xfstests generic/035.
--- fs/smb/server/vfs.c | 10 ++++++++++ 1 file changed, 10 insertions(+)
fs/smb/server/vfs.c +10 -0
@@ -718,6 +718,16 @@ int ksmbd_vfs_rename(struct ksmbd_work *work, const struct path *old_path, goto out3; } +if (d_really_is_positive (rd.new_dentry)) { +struct ksmbd_file *target_fp; + target_fp =ksmbd_lookup_fd_inode (rd.new_dentry); +if (target_fp) { +ksmbd_fd_put (work, target_fp); + err = -EACCES ; + goto out3; + } + } + err =vfs_rename (&rd);if (err)ksmbd_debug (VFS ,"vfs_rename failed err %d\n" , err);
Signature · 1 line
2.54.0
Namjae Jeon
maintainerfs/smb/server/vfs.c int ksmbd_vfs_rename(struct ksmbd_work *work, const struct path *old_path, · line 720
Quoted 8 lines
fs/smb/server/vfs.c +0
-0@@ -718,6 +718,16 @@ int ksmbd_vfs_rename(struct ksmbd_work *work, const struct path *old_path, goto out3; }
This check is also applied when SMB3 POSIX extensions are negotiated. POSIX rename semantics allow replacing an open destination while existing file descriptors continue to reference the unlinked inode. The check should therefore at least be skipped when work->tcon->posix_extensions is set.
fs/smb/server/vfs.c int ksmbd_vfs_rename(struct ksmbd_work *work, const struct path *old_path, · line 721
Quoted 1 line
fs/smb/server/vfs.c +1
-0+if (d_really_is_positive (rd.new_dentry)) {
Although destination share-mode enforcement appears to be missing already, rejecting every open destination would also reject the valid case where all existing handles permit FILE_SHARE_DELETE.
fs/smb/server/vfs.c int ksmbd_vfs_rename(struct ksmbd_work *work, const struct path *old_path, · line 723
Quoted 2 lines
fs/smb/server/vfs.c +2
-0+struct ksmbd_file *target_fp; + target_fp =ksmbd_lookup_fd_inode (rd.new_dentry);
This can incorrectly reject a rename when the source and destination refer to the same inode, for example through hardlinks.
Quoted 13 lines
fs/smb/server/vfs.c +7
-1+if (target_fp) { +ksmbd_fd_put (work, target_fp); + err = -EACCES ; + goto out3; + } + } + err =vfs_rename (&rd);if (err)ksmbd_debug (VFS ,"vfs_rename failed err %d\n" , err); --2.54.0
ChenXiaoSong
reviewerHi Namjae,
Currently, both the client and server support only FileRenameInformation (level 10) and do not implement FileRenameInformationEx (level 65).
Level 10 does not support FILE_RENAME_POSIX_SEMANTICS.
- MS-FSCC 2.4.42 FileRenameInformation: https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-fscc/1d2673a8-8fb9-4868-920a-775ccaa30cf8
- MS-FSCC 2.4.43 FileRenameInformationEx: https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-fscc/4217551b-d2c0-42cb-9dc1-69a716cf6d0c
- MS-FSA 2.1.5.15.12 FileRenameInformation: https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-fsa/87f86c9b-6c2a-4803-84b7-131a74a434fa
Quoted 5 lines · Namjae Jeon
This check is also applied when SMB3 POSIX extensions are negotiated. POSIX rename semantics allow replacing an open destination while existing file descriptors continue to reference the unlinked inode. The check should therefore at least be skipped when
work->tcon->posix_extensionsis set.
Signature · 3 lines
ChenXiaoSong <[email protected]> Chinese Homepage: https://chenxiaosong.com English Homepage: https://chenxiaosong.com/en
Namjae Jeon
maintainerQuoted 8 lines · ChenXiaoSong
Hi Namjae,
Currently, both the client and server support only
FileRenameInformation(level 10) and do not implementFileRenameInformationEx(level 65).Level 10 does not support
FILE_RENAME_POSIX_SEMANTICS.
Okay. We can implement this in ksmbd and cifs.ko to pass generic/035 test.
Thanks for sharing it.
Quoted 20 lines
- MS-FSCC 2.4.42 FileRenameInformation:
https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-fscc/1d2673a8-8fb9-4868-920a-775ccaa30cf8
- MS-FSCC 2.4.43 FileRenameInformationEx:
https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-fscc/4217551b-d2c0-42cb-9dc1-69a716cf6d0c
- MS-FSA 2.1.5.15.12 FileRenameInformation:
https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-fsa/87f86c9b-6c2a-4803-84b7-131a74a434faQuoted 5 lines · Namjae Jeon
This check is also applied when SMB3 POSIX extensions are negotiated. POSIX rename semantics allow replacing an open destination while existing file descriptors continue to reference the unlinked inode. The check should therefore at least be skipped when
work->tcon->posix_extensionsis set.--
ChenXiaoSong <[email protected]>
Chinese Homepage: https://chenxiaosong.com
English Homepage: https://chenxiaosong.com/en
ChenXiaoSong
reviewerAccording to MS-FSA 2.1.5.15.12, level 10 (FileRenameInformation), an overwrite rename must fail with STATUS_ACCESS_DENIED if the existing target still has an open.
MS-FSA 2.1.5.15.12 FileRenameInformation: https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-fsa/87f86c9b-6c2a-4803-84b7-131a74a434fa
Quoted 3 lines · Namjae Jeon
Okay. We can implement this in ksmbd and cifs.ko to pass generic/035 test.
Thanks for sharing it.
Signature · 3 lines
ChenXiaoSong <[email protected]> Chinese Homepage: https://chenxiaosong.com English Homepage: https://chenxiaosong.com/en
ChenXiaoSong
reviewerI will send v2 soon. For now, we also need to fail with STATUS_ACCESS_DENIED if the existing target has an open handle.
Of course, I will also try to implement FileRenameInformationEx (level 65) in the future.
Quoted 9 lines · ChenXiaoSong
According to MS-FSA 2.1.5.15.12, level 10 (FileRenameInformation), an overwrite rename must fail with
STATUS_ACCESS_DENIEDif the existing target still has an open.MS-FSA 2.1.5.15.12 FileRenameInformation: https://learn.microsoft.com/ en-us/openspecs/windows_protocols/ms- fsa/87f86c9b-6c2a-4803-84b7-131a74a434fa
Quoted 3 lines · Namjae Jeon
Okay. We can implement this in ksmbd and cifs.ko to pass generic/035 test.
Thanks for sharing it.
Signature · 3 lines
ChenXiaoSong <[email protected]> Chinese Homepage: https://chenxiaosong.com English Homepage: https://chenxiaosong.com/en
For POSIX the relevant piece is POSIX-FSA 2.1.4.2 "Server Requests Closing an Open".
The important nuance being treating "POSIX opens" different from non-POSIX opens, basically:
- allow rename if there are only other POSIX opens
- fail the rename if there's one or more non-POSIX opens
For SMB3 POSIX we're not planning to use FileRenameInformationEx, as there's no need for it, the above piece of the spec nicely achieves the desired behaviour.
Cheers!
-slow
Quoted 9 lines · ChenXiaoSong
According to MS-FSA 2.1.5.15.12, level 10 (FileRenameInformation), an overwrite rename must fail with
STATUS_ACCESS_DENIEDif the existing target still has an open.MS-FSA 2.1.5.15.12 FileRenameInformation: https://learn.microsoft.com/ en-us/openspecs/windows_protocols/ms- fsa/87f86c9b-6c2a-4803-84b7-131a74a434fa
Quoted 3 lines · Namjae Jeon
Okay. We can implement this in ksmbd and cifs.ko to pass generic/035 test.
Thanks for sharing it.
ChenXiaoSong
reviewerHi Ralph,
Thanks for your sharing.
This seems to be the rule for determining whether there are any open child files or subdirectories under the old directory of rename operation.
For checking whether the new file of rename operation is open, there does not seem to be a POSIX-specific rule.
Please let me know if my understanding is incorrect.
Quoted 4 lines · Ralph Boehme
- allow rename if there are only other POSIX opens
- fail the rename if there's one or more non-POSIX opens
Signature · 3 lines
ChenXiaoSong <[email protected]> Chinese Homepage: https://chenxiaosong.com English Homepage: https://chenxiaosong.com/en
ChenXiaoSong
reviewerHi Ralph,
I am puzzled by Samba's implementation. When mounted with posix option, why does rename_internals_fsp() still check found_open = files_forall() (Link[1])? Based on your description, should it only check non-POSIX opens?
```
// samba.git
// source3/smbd/smb2_reply.c
1344 NTSTATUS rename_internals_fsp()
1352 {
...
1504 found_open = files_forall(conn->sconn,
1505 rename_check_open_fn,
1506 &check_state);
1507 if (found_open != NULL) {
1508 DBG_NOTICE("Target file open\n");
1509 status = NT_STATUS_ACCESS_DENIED;
1510 goto out;
1511 }
...
```Quoted 16 lines · ChenXiaoSong
Hi Ralph,
Thanks for your sharing.
This seems to be the rule for determining whether there are any open child files or subdirectories under the old directory of rename operation.
For checking whether the new file of rename operation is open, there does not seem to be a POSIX-specific rule.
Please let me know if my understanding is incorrect.
Quoted 4 lines · Ralph Boehme
- allow rename if there are only other POSIX opens
- fail the rename if there's one or more non-POSIX opens
Signature · 3 lines
ChenXiaoSong <[email protected]> Chinese Homepage: https://chenxiaosong.com English Homepage: https://chenxiaosong.com/en
Quoted 1 line · ChenXiaoSong
I am puzzled by Samba's implementation. When mounted with
posixoption, why doesrename_internals_fsp()still checkfound_open = files_forall()(Link[1])? Based on your description, should it only check non-POSIX opens?
yes.
I was remembering that we had fixed this 2-3 years ago, not sure why we're not ignoring the POSIX opens here, looks like a bug.
Hi!
Quoted 1 line · ChenXiaoSong
This seems to be the rule for determining whether there are any open child files or subdirectories under the old directory of rename operation.
ah, crap, that was actually for POSIX deletion semantics, sorry for the confusion! :)
Quoted 1 line
For checking whether the new file of rename operation is open, there does not seem to be a POSIX-specific rule.
hm, it seems this one is indeed missing. Good catch, thanks!
The behaviour should be:
- if there are one or more non-POSIX opens on the destination, MUST fail with STATUS_ACCESS_DENIED
- if there are only POSIX opens, the operation can proceed
ChenXiaoSong
reviewerShould we add these rules to POSIX-FSA?
Quoted 7 lines · Ralph Boehme
hm, it seems this one is indeed missing. Good catch, thanks!
The behaviour should be:
- if there are one or more non-POSIX opens on the destination, MUST fail with
STATUS_ACCESS_DENIED
- if there are only POSIX opens, the operation can proceed
Signature · 3 lines
ChenXiaoSong <[email protected]> Chinese Homepage: https://chenxiaosong.com English Homepage: https://chenxiaosong.com/en
Quoted 1 line · ChenXiaoSong
Should we add these rules to POSIX-FSA?
definitely! :)
Working on it...
ChenXiaoSong
reviewerI am very interested in contributing to smb3-posix-extensions.git. Would you be willing to let me try making changes to the protocol?
Quoted 5 lines · Ralph Boehme
Quoted 1 line · ChenXiaoSong
Should we add these rules to POSIX-FSA?
definitely! :)
Working on it...
Signature · 3 lines
ChenXiaoSong <[email protected]> Chinese Homepage: https://chenxiaosong.com English Homepage: https://chenxiaosong.com/en
Sure! Patches welcome! :)
https://gitlab.com/smb3-posix-extensions/smb3-posix-extensions
Quoted 9 lines · ChenXiaoSong
I am very interested in contributing to smb3-posix-extensions.git. Would you be willing to let me try making changes to the protocol?
Quoted 5 lines · Ralph Boehme
Quoted 1 line · ChenXiaoSong
Should we add these rules to POSIX-FSA?
definitely! :)
Working on it...
Quoted 5 lines · Namjae Jeon via samba-technical
Quoted 3 lines · ChenXiaoSong
Hi Namjae,
Quoted 8 lines
Quoted 5 lines
Currently, both the client and server support only
FileRenameInformation(level 10) and do not implementFileRenameInformationEx(level 65).Level 10 does not support
FILE_RENAME_POSIX_SEMANTICS.Okay. We can implement this in ksmbd and cifs.ko to pass generic/035 test.
Thanks for sharing it.
fwiw, this should not be used for POSIX mode, see my other mail.