Skip to contentSkip to conversation

ksmbd: refuse to open an append-only file for writing without O_APPEND

PATCH Superseded Hungyu Lin 3 participants 3 messages Nov 20

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Hungyu Lin

author
#0 lore raw

PATCH 1/1 ksmbd: refuse to open an append-only file for writing without O_APPEND

smb2_open() opens the file with dentry_open(), which does not go through may_open(). inode_permission() refuses write access to an immutable inode, but nothing looks at S_APPEND. A client that has write permission on an append-only file can open it for writing and overwrite the data that is already there, which open(2) on the server and nfsd do not allow.

Refuse an open that asks for FILE_WRITE_DATA or FILE_APPEND_DATA on an append-only file like may_open() does, unless the file is opened with O_APPEND. Leave these two rights out of the maximal access of such a file as well, so that an open with MAXIMUM_ALLOWED still gets a handle for reading.

Fixes e2f34481b24d ("cifsd: add server-side procedures for SMB3")
---
 fs/smb/server/smb2pdu.c | 7 +++++++
 fs/smb/server/vfs.c     | 3 +++
 2 files changed, 10 insertions(+)
fs/smb/server/smb2pdu.c +7 -0
index 6bd63c5e7da3..e83e1cc3ba4f 100644
@@ -5122,6 +5122,13 @@ int smb2_open(struct ksmbd_work *work)
 		goto err_out;
 	}
 
+	if (IS_APPEND(d_inode(path.dentry)) && !(open_flags & O_APPEND) &&
+	    (open_flags & O_ACCMODE) != O_RDONLY &&
+	    daccess & (FILE_WRITE_DATA_LE | FILE_APPEND_DATA_LE)) {
+		rc = -EACCES;
+		goto err_out;
+	}
+
 	rc = 0;
 	filp = dentry_open(&path, open_flags, current_cred());
 	if (IS_ERR(filp)) {
fs/smb/server/vfs.c +3 -0
index 39bbe6aad629..e3169790619b 100644
@@ -136,6 +136,9 @@ void ksmbd_vfs_query_maximal_access(const struct mnt_idmap *idmap,
 				FILE_WRITE_EA | FILE_WRITE_ATTRIBUTES |
 				FILE_DELETE_CHILD);
 
+	if (IS_APPEND(d_inode(dentry)) && !S_ISDIR(d_inode(dentry)->i_mode))
+		*daccess &= ~(FILE_WRITE_DATA_LE | FILE_APPEND_DATA_LE);
+
 	if (!inode_permission(idmap, d_inode(dentry), MAY_OPEN | MAY_READ))
 		*daccess |= FILE_READ_DATA_LE | FILE_READ_EA_LE;
 
Signature · 1 line
2.43.0
maintainer
#1 lore raw
Quoted 14 lines · Hungyu Lin

smb2_open() opens the file with dentry_open(), which does not go through may_open(). inode_permission() refuses write access to an immutable inode, but nothing looks at S_APPEND. A client that has write permission on an append-only file can open it for writing and overwrite the data that is already there, which open(2) on the server and nfsd do not allow.

Refuse an open that asks for FILE_WRITE_DATA or FILE_APPEND_DATA on an append-only file like may_open() does, unless the file is opened with O_APPEND. Leave these two rights out of the maximal access of such a file as well, so that an open with MAXIMUM_ALLOWED still gets a handle for reading.

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

Can you check Sashiko's review comments ?

https://sashiko.dev/#/patchset/20261120071553.28739-1-dennylin0707%40gmail.com

Thanks.

#2 lore raw
In reply to Hungyu Lin

Hi,

fs/smb/server/vfs.c void ksmbd_vfs_query_maximal_access(const struct mnt_idmap *idmap, · line 140

Quoted 2 lines
fs/smb/server/vfs.c +2 -0
+	if (IS_APPEND(d_inode(dentry)) && !S_ISDIR(d_inode(dentry)->i_mode))
+		*daccess &= ~(FILE_WRITE_DATA_LE | FILE_APPEND_DATA_LE);

On an append-only file without a stored NT DACL, a POSIX CREATE request for MAXIMUM_ALLOWED | FILE_APPEND_DATA still opens the file with O_APPEND. smb2_open() sets that flag from the original request, but this hunk removes FILE_APPEND_DATA from the handle's granted access. smb2_write() then refuses append writes with STATUS_ACCESS_DENIED.

Without the patch, the same request can append with an append write (offset U64_MAX), while writes at an explicit offset are refused. With the patch, the open still succeeds, but every write on the handle fails.

I think this is a niche case. I had to write a custom SMB client to reproduce it, but it does represent a behavioral change introduced by the patch.

Thanks,
Ian