ksmbd: refuse to open an append-only file for writing without O_APPEND
Subsystems (2)
PATCH v2 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 the open like may_open() does, when the file is opened for writing without O_APPEND or the create disposition asks to overwrite it. smb2_create_open_flags() also selects O_WRONLY or O_RDWR for FILE_WRITE_EA and FILE_WRITE_ATTRIBUTES, so these opens are refused too.
An open with MAXIMUM_ALLOWED is granted the write rights that the permission bits or the stored DACL allow and would be refused as well. Leave the write rights that the client did not ask for explicitly out of the access granted to such an open, so that it gets a handle for reading. The maximal access returned in the MxAc create context is not changed.
Directories are left alone: an append-only directory still takes new entries.
---
Changes in v2:
- Refuse every open for writing without O_APPEND, not only the ones that ask for FILE_WRITE_DATA or FILE_APPEND_DATA. A handle opened for FILE_WRITE_ATTRIBUTES could still reach kernel_write() through FSCTL_SET_SPARSE.
- Refuse an overwrite disposition up front. The truncating ones used to fail in vfs_truncate() after the oplocks were broken, with STATUS_SHARING_VIOLATION.
- Drop the write rights of a MAXIMUM_ALLOWED open in smb2_open() instead of ksmbd_vfs_query_maximal_access(), so that a file with a stored DACL is covered too and the MxAc reply stays as it is.
- Keep the rights the client asked for next to MAXIMUM_ALLOWED. A POSIX open for MAXIMUM_ALLOWED | FILE_APPEND_DATA can append again, as reported by Ian Bridges.
v1: https://lore.kernel.org/linux-cifs/[email protected]/
fs/smb/server/smb2pdu.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+)
fs/smb/server/smb2pdu.c +21 -0
@@ -5028,6 +5028,14 @@ int smb2_open(struct ksmbd_work *work) maximal_access = daccess; } +if (req->DesiredAccess &FILE_MAXIMAL_ACCESS_LE && file_present && +IS_APPEND (d_inode (path.dentry)) && + !S_ISDIR (d_inode (path.dentry)->i_mode)) { +__le32 asked =smb_map_generic_desired_access (req->DesiredAccess); + + daccess &= ~(FILE_WRITE_DESIRE_ACCESS_LE & ~asked); + } +/* MAXIMUM_ALLOWED may not have granted the requested DELETE */ if (req->CreateOptions &FILE_DELETE_ON_CLOSE_LE && !(daccess &FILE_DELETE_LE )) {
@@ -5122,6 +5130,19 @@ int smb2_open(struct ksmbd_work *work) goto err_out; } +if (IS_APPEND (d_inode (path.dentry)) && + !S_ISDIR (d_inode (path.dentry)->i_mode)) { +if ((open_flags &O_ACCMODE ) !=O_RDONLY && + !(open_flags &O_APPEND )) { + rc = -EACCES ; + goto err_out; + } +if (open_flags &O_TRUNC ) { + rc = -EACCES ; + goto err_out; + } + } + rc =0 ; filp =dentry_open (&path, open_flags,current_cred ());if (IS_ERR (filp)) {
Signature · 1 line
2.43.0