* [PATCH] ksmbd: fail a rename that replaces a directory or a read-only file
@ 2026-11-30 16:42 Ian Bridges
0 siblings, 0 replies; only message in thread
From: Ian Bridges @ 2026-11-30 16:42 UTC (permalink / raw)
To: linkinjeon
Cc: senozhatsky, tom, chenxiaosong, linux-cifs, linux-kernel, Ian Bridges
A rename with ReplaceIfExists onto an existing read-only file
succeeds, and the read-only file is replaced. A rename of a directory
onto an existing empty directory also succeeds, and the directory is
replaced.
Fail a rename that would replace a directory or a file with the stored
read-only attribute with STATUS_ACCESS_DENIED, as MS-FSA does. A rename
of a file onto a directory, which failed with STATUS_INVALID_INFO_CLASS,
and a rename onto a non-empty directory, which failed with
STATUS_DIRECTORY_NOT_EMPTY, now also fail with STATUS_ACCESS_DENIED.
A POSIX rename keeps the POSIX behavior. On a share that does not store
DOS attributes, the read-only attribute is not checked.
Signed-off-by: Ian Bridges <icb@fastmail.org>
---
Tested on ksmbd-for-next 87ff512becd4, on x86_64 with KASAN. A script
sent each changed rename and the ones that are not meant to change,
POSIX renames among them, to the base and to the patched server. Each
changed rename got STATUS_ACCESS_DENIED and left both names as they
were. The others gave the same results as on the base. smbtorture gives
the same results as on the base, and KASAN reported nothing.
The check reads the stored attributes of the target with the caller's
credentials. If the caller cannot read the target file, that read fails
and the read-only attribute is not checked. The rename then replaces
the target, as on the base. ksmbd also reports such a file to that
caller without the read-only attribute.
This patch was developed with LLM assistance.
fs/smb/server/vfs.c | 27 +++++++++++++++++++++++++++
1 file changed, 27 insertions(+)
diff --git a/fs/smb/server/vfs.c b/fs/smb/server/vfs.c
index 35d449ee3dae..bcabb6515f0f 100644
--- a/fs/smb/server/vfs.c
+++ b/fs/smb/server/vfs.c
@@ -762,6 +762,19 @@ int ksmbd_vfs_check_rename_share(struct ksmbd_work *work,
return err;
}
+/* Whether the DOS attributes stored for a file include read-only */
+static bool ksmbd_vfs_is_readonly(struct ksmbd_work *work,
+ const struct mnt_idmap *idmap,
+ struct dentry *dentry)
+{
+ struct xattr_dos_attrib da;
+
+ return test_share_config_flag(work->tcon->share_conf,
+ KSMBD_SHARE_FLAG_STORE_DOS_ATTRS) &&
+ ksmbd_vfs_get_dos_attrib_xattr(idmap, dentry, &da) > 0 &&
+ da.attr & FILE_ATTRIBUTE_READONLY;
+}
+
int ksmbd_vfs_rename(struct ksmbd_work *work, struct ksmbd_file *old_fp,
char *newname, int flags)
{
@@ -820,6 +833,20 @@ int ksmbd_vfs_rename(struct ksmbd_work *work, struct ksmbd_file *old_fp,
goto out3;
}
+ /*
+ * An overwrite rename does not replace a directory or a read-only
+ * file. A POSIX rename follows the POSIX rules instead.
+ */
+ if (!old_fp->is_posix_ctxt &&
+ !(flags & (RENAME_NOREPLACE | RENAME_EXCHANGE)) &&
+ d_inode(rd.new_dentry) &&
+ d_inode(rd.new_dentry) != d_inode(old_child) &&
+ (d_is_dir(rd.new_dentry) ||
+ ksmbd_vfs_is_readonly(work, rd.mnt_idmap, rd.new_dentry))) {
+ err = -EACCES;
+ goto out3;
+ }
+
err = ksmbd_vfs_check_rename_share(work, old_path);
if (err)
goto out3;
base-commit: 87ff512becd45a08c44fd47a9f03e607af9ef6bd
--
2.47.3
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2026-11-30 16:42 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-30 16:42 [PATCH] ksmbd: fail a rename that replaces a directory or a read-only file Ian Bridges
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®