ksmbd: refuse to open an append-only file for writing without O_APPEND
Subsystems (2)
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.
--- fs/smb/server/smb2pdu.c | 7 +++++++ fs/smb/server/vfs.c | 3 +++ 2 files changed, 10 insertions(+)
fs/smb/server/smb2pdu.c +7 -0
@@ -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
@@ -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
Namjae Jeon
maintainerQuoted 14 lines · Hungyu Lin
smb2_open()opens the file withdentry_open(), which does not go throughmay_open().inode_permission()refuses write access to an immutable inode, but nothing looks atS_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_DATAorFILE_APPEND_DATAon an append-only file likemay_open()does, unless the file is opened withO_APPEND. Leave these two rights out of the maximal access of such a file as well, so that an open withMAXIMUM_ALLOWEDstill gets a handle for reading.
Quoted 3 lines
Fixes e2f34481b24d ("cifsd: add server-side procedures for SMB3")Signed-off-by Hungyu Lin <[email protected]>
Can you check Sashiko's review comments ?
https://sashiko.dev/#/patchset/20261120071553.28739-1-dennylin0707%40gmail.com
Thanks.
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