From: Ian Bridges <icb@fastmail.org>
To: linkinjeon@kernel.org
Cc: senozhatsky@chromium.org, tom@talpey.com,
chenxiaosong@chenxiaosong.com, linux-cifs@vger.kernel.org,
linux-kernel@vger.kernel.org, Ian Bridges <icb@fastmail.org>
Subject: [PATCH] ksmbd: fail a rename that replaces a directory or a read-only file
Date: Mon, 30 Nov 2026 10:42:04 -0600 [thread overview]
Message-ID: <20261130164204.2102440-1-icb@fastmail.org> (raw)
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
reply other threads:[~2026-11-30 16:42 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261130164204.2102440-1-icb@fastmail.org \
--to=icb@fastmail.org \
--cc=chenxiaosong@chenxiaosong.com \
--cc=linkinjeon@kernel.org \
--cc=linux-cifs@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=senozhatsky@chromium.org \
--cc=tom@talpey.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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®