diff --git a/fs/smb/server/mgmt/user_config.h b/fs/smb/server/mgmt/user_config.h index cc460b4ff7d3..e116b2ee9917 100644 --- a/fs/smb/server/mgmt/user_config.h +++ b/fs/smb/server/mgmt/user_config.h @@ -67,4 +67,9 @@ struct ksmbd_user *ksmbd_alloc_user(struct ksmbd_login_response *resp, void ksmbd_free_user(struct ksmbd_user *user); bool ksmbd_anonymous_user(struct ksmbd_user *user); bool ksmbd_compare_user(struct ksmbd_user *u1, struct ksmbd_user *u2); + +static inline bool ksmbd_user_authenticated(struct ksmbd_user *user) +{ + return user && !user_guest(user) && !ksmbd_anonymous_user(user); +} #endif /* __USER_CONFIG_MANAGEMENT_H__ */ diff --git a/fs/smb/server/smb2pdu.c b/fs/smb/server/smb2pdu.c index db10c099219d..fe17d4061237 100644 --- a/fs/smb/server/smb2pdu.c +++ b/fs/smb/server/smb2pdu.c @@ -2365,6 +2365,17 @@ static int ntlm_authenticate(struct ksmbd_work *work, return -EKEYREJECTED; } ksmbd_free_user(user); + } else if (sess->user) { + /* + * Reauthentication of an expired session: as for a valid + * session, and as krb5_authenticate() does, it must be by the + * same user. Replacing sess->user would also leak the old one. + */ + if (!ksmbd_compare_user(sess->user, user)) { + ksmbd_free_user(user); + return -EKEYREJECTED; + } + ksmbd_free_user(user); } else { sess->user = user; } @@ -3541,7 +3552,8 @@ next: static noinline int smb2_set_stream_name_xattr(const struct path *path, struct ksmbd_file *fp, - char *stream_name, int s_type) + char *stream_name, int s_type, + bool *created) { const struct mnt_idmap *idmap = mnt_idmap(path->mnt); size_t xattr_stream_size; @@ -3605,6 +3617,8 @@ static noinline int smb2_set_stream_name_xattr(const struct path *path, rc = ksmbd_vfs_setxattr(idmap, path, xattr_stream_name, NULL, 0, 0, false); if (rc < 0) pr_err("Failed to store XATTR stream name :%d\n", rc); + else + *created = true; return 0; } @@ -4335,7 +4349,8 @@ static int smb2_handle_app_instance_id(struct ksmbd_work *work, /* Authorize the referenced open even if its name changes meanwhile. */ old_path = &old_fp->filp->f_path; rc = smb_check_perm_dacl(conn, old_path, &maximal_access, 0, - work->sess->user->uid, false); + work->sess->user->uid, + ksmbd_user_authenticated(work->sess->user), false); if (rc) { /* Access denial prevents takeover, not the CREATE itself. */ ksmbd_put_durable_fd(old_fp); @@ -4394,6 +4409,7 @@ int smb2_open(struct ksmbd_work *work) char *stream_name = NULL; bool file_present = false, created = false, already_permitted = false; bool app_instance_id_checked = false, app_instance_closed; + bool stream_created = false; int share_ret, need_truncate = 0; u64 time, alloc_size = 0; umode_t posix_mode = 0; @@ -4856,14 +4872,17 @@ resolve_path: if (file_present && !(req->CreateOptions & FILE_DELETE_ON_CLOSE_LE)) { rc = smb_check_perm_dacl(conn, &path, &daccess, req->DesiredAccess, - sess->user->uid, false); + sess->user->uid, + ksmbd_user_authenticated(sess->user), false); if (rc) goto err_out; if (maximal_access_ctxt) { maximal_access = FILE_MAXIMAL_ACCESS_LE; rc = smb_check_perm_dacl(conn, &path, &maximal_access, - 0, sess->user->uid, false); + 0, sess->user->uid, + ksmbd_user_authenticated(sess->user), + false); if (rc) goto err_out; @@ -4961,16 +4980,32 @@ resolve_path: if ((daccess & FILE_DELETE_LE) || (req->CreateOptions & FILE_DELETE_ON_CLOSE_LE)) { - rc = inode_permission(idmap, - d_inode(path.dentry->d_parent), - MAY_EXEC | MAY_WRITE); + struct dentry *parent = dget_parent(path.dentry); + struct inode *parent_inode = d_inode(parent); + + if (parent_inode) + rc = inode_permission(idmap, parent_inode, + MAY_EXEC | MAY_WRITE); + else + rc = -ENOENT; + dput(parent); if (rc) goto err_out; } } } - rc = ksmbd_query_inode_status(path.dentry->d_parent); + { + struct dentry *parent = dget_parent(path.dentry); + + if (!d_inode(parent)) { + dput(parent); + rc = -ENOENT; + goto err_out; + } + rc = ksmbd_query_inode_status(parent); + dput(parent); + } if (rc == KSMBD_INODE_STATUS_PENDING_DELETE) { rc = -EBUSY; goto err_out; @@ -5053,10 +5088,17 @@ resolve_path: if (created) { int posix_acl_rc; struct inode *inode = d_inode(path.dentry); + struct inode *parent_inode; + struct dentry *parent; - posix_acl_rc = ksmbd_vfs_inherit_posix_acl(idmap, - &path, - d_inode(path.dentry->d_parent)); + parent = dget_parent(path.dentry); + parent_inode = d_inode(parent); + if (parent_inode) + posix_acl_rc = ksmbd_vfs_inherit_posix_acl(idmap, &path, + parent_inode); + else + posix_acl_rc = -ENOENT; + dput(parent); if (posix_acl_rc) ksmbd_debug(SMB, "inherit posix acl failed : %d\n", posix_acl_rc); @@ -5138,7 +5180,8 @@ resolve_path: rc = smb2_set_stream_name_xattr(&path, fp, stream_name, - s_type); + s_type, + &stream_created); if (rc) goto err_out; file_info = FILE_CREATED; @@ -5146,13 +5189,25 @@ resolve_path: fp->attrib_only = !(req->DesiredAccess & ~(FILE_READ_ATTRIBUTES_LE | FILE_WRITE_ATTRIBUTES_LE | FILE_SYNCHRONIZE_LE)); + need_truncate = !S_ISDIR(file_inode(filp)->i_mode) && + open_flags & O_TRUNC && !fp->attrib_only && !stream_name; + /* + * Count the FILE_WRITE_DATA that MS-FSA 2.1.5.1.2 grants an overwrite, + * supersede included, or a new named stream on an existing file. + * An attribute-only open of the file is not truncated, and the + * synthesized AFP_AfpInfo stream is not created by the client. + */ + fp->implied_write = need_truncate || + (stream_name && + (open_flags & O_TRUNC || + (file_present && stream_created))); fp->is_posix_ctxt = posix_ctxt; fp->posix_append = posix_ctxt && !!(open_flags & O_APPEND); /* fp should be searchable through ksmbd_inode.m_fp_list - * after daccess, saccess, attrib_only, and stream are - * initialized. + * after daccess, saccess, attrib_only, implied_write, and + * stream are initialized. */ down_write(&fp->f_ci->m_lock); list_add(&fp->node, &fp->f_ci->m_fp_list); @@ -5173,11 +5228,8 @@ resolve_path: if (file_present || created) path_put(&path); - if (!S_ISDIR(file_inode(filp)->i_mode) && open_flags & O_TRUNC && - !fp->attrib_only && !stream_name) { + if (need_truncate) smb_break_all_oplock(work, fp); - need_truncate = 1; - } share_ret = ksmbd_smb_check_shared_mode(fp->filp, fp); if (!test_share_config_flag(work->tcon->share_conf, KSMBD_SHARE_FLAG_OPLOCKS) || @@ -6333,6 +6385,7 @@ static int process_query_dir_entries(struct smb2_query_dir_private *priv) rc = smb_check_perm_dacl(priv->work->conn, &path, &daccess, daccess, priv->work->sess->user->uid, + ksmbd_user_authenticated(priv->work->sess->user), true); if (rc) { dput(dent); diff --git a/fs/smb/server/smb_common.c b/fs/smb/server/smb_common.c index f9fb3741ada3..e7a987af64ba 100644 --- a/fs/smb/server/smb_common.c +++ b/fs/smb/server/smb_common.c @@ -695,10 +695,24 @@ static void smb_shared_mode_error(int error, struct ksmbd_file *prev_fp, prev_fp->saccess, curr_fp->daccess); } +/* + * Sharing checks use fp->daccess plus the FILE_WRITE_DATA that MS-FSA + * grants an open that overwrites data or creates a named stream. + * fp->daccess keeps the open's own access. + */ +__le32 ksmbd_sharing_daccess(const struct ksmbd_file *fp) +{ + if (fp->implied_write) + return fp->daccess | FILE_WRITE_DATA_LE; + return fp->daccess; +} + int ksmbd_smb_check_shared_mode(struct file *filp, struct ksmbd_file *curr_fp) { int rc = 0; struct ksmbd_file *prev_fp; + __le32 curr_access = ksmbd_sharing_daccess(curr_fp); + __le32 access; /* * Lookup fp in master fp list, and check desired access and @@ -719,6 +733,24 @@ int ksmbd_smb_check_shared_mode(struct file *filp, struct ksmbd_file *curr_fp) if (prev_fp->attrib_only != curr_fp->attrib_only) continue; + /* + * MS-FSA checks sharing per stream, so a named stream open's + * implied write is not a write against the primary stream. + * It still decides whether the pair is checked. + */ + access = curr_access; + if (ksmbd_stream_fd(curr_fp) && !ksmbd_stream_fd(prev_fp)) + access = curr_fp->daccess; + + /* + * Two opens conflict only if both hold a right in + * FILE_SHARE_CHECKED_ACCESS_LE (MS-FSA 2.1.5.1.2.1 and + * 2.1.5.1.2.2) + */ + if (!(ksmbd_sharing_daccess(prev_fp) & FILE_SHARE_CHECKED_ACCESS_LE) || + !(curr_access & FILE_SHARE_CHECKED_ACCESS_LE)) + continue; + if (!(prev_fp->saccess & FILE_SHARE_DELETE_LE) && curr_fp->daccess & FILE_DELETE_LE) { smb_shared_mode_error(SHARE_DELETE_ERROR, @@ -745,7 +777,7 @@ int ksmbd_smb_check_shared_mode(struct file *filp, struct ksmbd_file *curr_fp) } if (!(prev_fp->saccess & FILE_SHARE_WRITE_LE) && - curr_fp->daccess & (FILE_WRITE_DATA_LE | FILE_APPEND_DATA_LE)) { + access & (FILE_WRITE_DATA_LE | FILE_APPEND_DATA_LE)) { smb_shared_mode_error(SHARE_WRITE_ERROR, prev_fp, curr_fp); diff --git a/fs/smb/server/smb_common.h b/fs/smb/server/smb_common.h index a9cc09e31721..d3d19f1a23ea 100644 --- a/fs/smb/server/smb_common.h +++ b/fs/smb/server/smb_common.h @@ -69,6 +69,10 @@ FILE_EXECUTE | FILE_DELETE_CHILD | \ FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES) +/* Rights that make an open take part in sharing checks */ +#define FILE_SHARE_CHECKED_ACCESS_LE (FILE_EXECUTE_LE | FILE_READ_DATA_LE | \ + FILE_WRITE_DATA_LE | FILE_APPEND_DATA_LE | FILE_DELETE_LE) + #define SMB_COM_NEGOTIATE 0x72 /* See MS-CIFS 2.2.2.1 */ /* See MS-CIFS 2.2.3.1 */ @@ -284,6 +288,7 @@ int ksmbd_extract_shortname(struct ksmbd_conn *conn, int ksmbd_smb_negotiate_common(struct ksmbd_work *work, unsigned int command); +__le32 ksmbd_sharing_daccess(const struct ksmbd_file *fp); int ksmbd_smb_check_shared_mode(struct file *filp, struct ksmbd_file *curr_fp); int __ksmbd_override_fsids(struct ksmbd_work *work, struct ksmbd_share_config *share); diff --git a/fs/smb/server/smbacl.c b/fs/smb/server/smbacl.c index ab76c5c9e0ea..6b5f22d6280c 100644 --- a/fs/smb/server/smbacl.c +++ b/fs/smb/server/smbacl.c @@ -7,6 +7,7 @@ */ #include +#include #include #include #include @@ -1207,7 +1208,7 @@ int smb_inherit_dacl(struct ksmbd_conn *conn, struct smb_acl *parent_pdacl; struct smb_ntsd *parent_pntsd = NULL; struct smb_sid owner_sid, group_sid; - struct dentry *parent = path->dentry->d_parent; + struct dentry *parent; const struct mnt_idmap *idmap = mnt_idmap(path->mnt); int inherited_flags = 0, flags = 0, i, nt_size = 0, pdacl_size; int rc = 0, pntsd_type, ppntsd_size, acl_len, aces_size; @@ -1217,8 +1218,14 @@ int smb_inherit_dacl(struct ksmbd_conn *conn, char *aces_base; bool is_dir = S_ISDIR(d_inode(path->dentry)->i_mode); + parent = dget_parent(path->dentry); + if (!d_inode(parent)) { + dput(parent); + return -ENOENT; + } ppntsd_size = ksmbd_vfs_get_sd_xattr(conn, idmap, parent, &parent_pntsd); + dput(parent); if (ppntsd_size <= 0) return -ENOENT; @@ -1459,9 +1466,49 @@ bool smb_inherit_flags(int flags, bool is_dir) return false; } +/* Whether the caller's credentials hold the group of an S-1-22-2- SID */ +static bool smb_sid_in_group(const struct smb_sid *sid) +{ + gid_t gid; + + if (sid->num_subauth != sid_unix_groups.num_subauth + 1 || + compare_sids(sid, &sid_unix_groups)) + return false; + gid = le32_to_cpu(sid->sub_auth[sid->num_subauth - 1]); + /* + * ksmbd_override_fsids() builds the caller's credentials with + * make_kgid(&init_user_ns, ...), so the group membership test must + * compare in that space. Applying the mount idmap here (as when + * resolving a SID to an on-disk gid) would mismatch on idmapped + * mounts. + */ + return in_group_p(make_kgid(&init_user_ns, gid)); +} + +/* Whether two SIDs are the same SID */ +static bool smb_sid_equal(const struct smb_sid *a, const struct smb_sid *b) +{ + return a->num_subauth == b->num_subauth && !compare_sids(a, b); +} + +/* + * compare_sids() compares only the sub-authorities both SIDs have, so it + * also matches a shorter or longer SID, such as S-1-3 or S-1-3-4-777 + */ +static bool smb_sid_is_owner_rights(const struct smb_sid *sid) +{ + return sid->num_subauth == sid_owner_rights.num_subauth && + !compare_sids(sid, &sid_owner_rights); +} + +static unsigned int smb_parent_derived_rights(struct ksmbd_conn *conn, + const struct path *path, int uid, + bool authenticated, + unsigned int want); + int smb_check_perm_dacl(struct ksmbd_conn *conn, const struct path *path, __le32 *pdaccess, __le32 raw_daccess, int uid, - bool strict) + bool authenticated, bool strict) { const struct mnt_idmap *idmap = mnt_idmap(path->mnt); struct smb_ntsd *pntsd = NULL; @@ -1475,13 +1522,23 @@ int smb_check_perm_dacl(struct ksmbd_conn *conn, const struct path *path, int granted = requested; struct smb_ace *ace; int i, found = 0; - unsigned int access_bits = 0, denied = 0; + unsigned int access_bits = 0, denied = 0, grp_granted = 0; + unsigned int grp_allowed = 0, grp_denied = 0, bits; struct smb_ace *others_ace = NULL; - bool or_ace = false; + bool or_ace = false, obj_deny = false, au_ace = false, member, counted; + bool grp_ace = false; struct posix_acl_entry *pa_entry; unsigned int sid_type = SIDOWNER; unsigned short ace_size; bool is_owner, owner_rights = false; + bool maximal = *pdaccess & FILE_MAXIMAL_ACCESS_LE; + /* + * MAXIMUM_ALLOWED requests that are not strict match every session + * against Authenticated Users and do not count ACEs for the + * caller's groups. Strict ones match SIDs as explicit requests do. + */ + bool open_max = maximal && !strict; + bool authusers = authenticated || open_max; vfsuid_t vfsuid; ksmbd_debug(SMB, "check permission using windows acl\n"); @@ -1515,14 +1572,17 @@ int smb_check_perm_dacl(struct ksmbd_conn *conn, const struct path *path, /* * An empty (present, zero-ACE) DACL grants no access to * anyone except the object owner's implicit READ_CONTROL - * and WRITE_DAC (MS-DTYP 2.4.5). Deny every other caller, - * deny the owner any access beyond those two bits, and do - * not let trailing bytes after a zero-ACE DACL become an - * implicit grant. + * and WRITE_DAC (MS-DTYP 2.5.3.2), besides what the parent + * grants. Deny every other right, and do not let trailing + * bytes after a zero-ACE DACL become an implicit grant. */ if (is_owner) allowed = READ_CONTROL | WRITE_DAC; - if (*pdaccess & FILE_MAXIMAL_ACCESS_LE) + if (!strict) + allowed |= smb_parent_derived_rights(conn, path, uid, + authenticated, + requested & ~allowed); + if (maximal) requested |= allowed; if (!allowed || requested & ~allowed) rc = -EACCES; @@ -1531,62 +1591,116 @@ int smb_check_perm_dacl(struct ksmbd_conn *conn, const struct path *path, goto err_out; } - if (*pdaccess & FILE_MAXIMAL_ACCESS_LE) { - ace = (struct smb_ace *)((char *)pdacl + sizeof(struct smb_acl)); - aces_size = pdacl_size - sizeof(struct smb_acl); - for (i = 0; i < le16_to_cpu(pdacl->num_aces); i++) { - if (aces_size < offsetof(struct smb_ace, sid) + - CIFS_SID_BASE_SIZE) - break; - ace_size = le16_to_cpu(ace->size); - if (ace_size > aces_size || - ace_size < offsetof(struct smb_ace, sid) + - CIFS_SID_BASE_SIZE) - break; - aces_size -= ace_size; + /* + * Evaluate the DACL in order (MS-DTYP 2.5.3.2). access_bits collects + * the result for the ACEs that apply to the caller, without its groups + * for a MAXIMUM_ALLOWED request that is not strict. grp_granted + * collects what ACEs for the caller's groups, or OWNER RIGHTS for the + * owner, grant. It is used when the SID lookup and POSIX ACL fallback + * below do not decide. + */ + ace = (struct smb_ace *)((char *)pdacl + sizeof(struct smb_acl)); + aces_size = pdacl_size - sizeof(struct smb_acl); + for (i = 0; i < le16_to_cpu(pdacl->num_aces); i++) { + if (aces_size < offsetof(struct smb_ace, sid) + + CIFS_SID_BASE_SIZE) + break; + ace_size = le16_to_cpu(ace->size); + if (ace_size > aces_size || + ace_size < offsetof(struct smb_ace, sid) + + CIFS_SID_BASE_SIZE) + break; + aces_size -= ace_size; - if (ace->sid.num_subauth > SID_MAX_SUB_AUTHORITIES || - ace_size < offsetof(struct smb_ace, sid) + - CIFS_SID_BASE_SIZE + - sizeof(__le32) * ace->sid.num_subauth) - break; + if (ace->sid.num_subauth > SID_MAX_SUB_AUTHORITIES || + ace_size < offsetof(struct smb_ace, sid) + + CIFS_SID_BASE_SIZE + + sizeof(__le32) * ace->sid.num_subauth) + break; - if (!compare_sids(&sid_owner_rights, &ace->sid)) { - owner_rights = true; - if (!is_owner) - goto next_ace; - } + if ((ace->type == ACCESS_DENIED_OBJECT_ACE_TYPE || + ace->type == ACCESS_DENIED_CALLBACK_OBJECT_ACE_TYPE) && + !(ace->flags & INHERIT_ONLY_ACE)) + obj_deny = true; - if (ace->flags & INHERIT_ONLY_ACE || - (compare_sids(&sid, &ace->sid) && - compare_sids(&sid_unix_NFS_mode, &ace->sid) && - compare_sids(&sid_everyone, &ace->sid) && - compare_sids(&sid_authusers, &ace->sid) && - compare_sids(&sid_owner_rights, &ace->sid))) + if (!compare_sids(&sid_owner_rights, &ace->sid)) { + owner_rights = true; + if (!is_owner) goto next_ace; - - switch (ace->type) { - case ACCESS_ALLOWED_ACE_TYPE: - access_bits |= le32_to_cpu(ace->access_req); - break; - case ACCESS_DENIED_ACE_TYPE: - case ACCESS_DENIED_CALLBACK_ACE_TYPE: - denied |= ~access_bits & - le32_to_cpu(ace->access_req); - break; - } -next_ace: - ace = (struct smb_ace *)((char *)ace + le16_to_cpu(ace->size)); } + + member = smb_sid_in_group(&ace->sid); + /* The grp_ masks count groups and OWNER RIGHTS only */ + counted = member || + (is_owner && smb_sid_is_owner_rights(&ace->sid)); + if (ace->flags & INHERIT_ONLY_ACE || + (compare_sids(&sid, &ace->sid) && + compare_sids(&sid_unix_NFS_mode, &ace->sid) && + compare_sids(&sid_everyone, &ace->sid) && + (!authusers || + compare_sids(&sid_authusers, &ace->sid)) && + compare_sids(&sid_owner_rights, &ace->sid) && + !member)) + goto next_ace; + /* Other requests count only SIDs that match exactly */ + if (!open_max && !member && + !smb_sid_equal(&sid, &ace->sid) && + !smb_sid_equal(&sid_everyone, &ace->sid) && + !(authusers && smb_sid_equal(&sid_authusers, &ace->sid)) && + !smb_sid_is_owner_rights(&ace->sid)) + goto next_ace; + + bits = le32_to_cpu(ace->access_req); + switch (ace->type) { + case ACCESS_ALLOWED_ACE_TYPE: + grp_ace |= counted; + if (counted) + grp_allowed |= bits; + /* An open's MAXIMUM_ALLOWED takes no group grants */ + if (open_max && member) + break; + if (counted) + grp_granted |= bits; + access_bits |= bits; + break; + case ACCESS_DENIED_ACE_TYPE: + case ACCESS_DENIED_CALLBACK_ACE_TYPE: + grp_ace |= counted; + if (counted) + grp_denied |= ~grp_allowed & bits; + /* nor group denials */ + if (!(open_max && member)) + denied |= ~access_bits & bits; + break; + } +next_ace: + ace = (struct smb_ace *)((char *)ace + le16_to_cpu(ace->size)); + } + + /* Keep only file rights, not MAXIMUM_ALLOWED or generic bits */ + grp_granted &= GENERIC_ALL_FLAGS & ~grp_denied; + if (maximal) { if (is_owner && !owner_rights) access_bits |= READ_CONTROL | WRITE_DAC | FILE_READ_ATTRIBUTES | DELETE; access_bits &= ~denied; + /* An object deny ACE could deny the parent's rights */ + if (!open_max && obj_deny) + access_bits = 0; if ((raw_daccess & FILE_GENERIC_EXECUTE_LE) && S_ISREG(d_inode(path->dentry)->i_mode) && (access_bits & GENERIC_READ_FLAGS) == GENERIC_READ_FLAGS) access_bits |= FILE_EXECUTE; granted = requested | access_bits; + } else if (!obj_deny) { + /* An explicit request takes the ordered result */ + access_bits &= ~denied; + } else { + /* + * ksmbd does not parse object deny ACEs, so with one present an + * explicit request keeps the answer of the ACE found below + */ + access_bits = 0; } ace = (struct smb_ace *)((char *)pdacl + sizeof(struct smb_acl)); @@ -1616,15 +1730,26 @@ next_ace: !(ace->flags & INHERIT_ONLY_ACE)) or_ace = true; if (!compare_sids(&sid_everyone, &ace->sid) || - !compare_sids(&sid_authusers, &ace->sid)) + (authusers && + !compare_sids(&sid_authusers, &ace->sid))) others_ace = ace; + if (!compare_sids(&sid_authusers, &ace->sid)) + au_ace = true; ace = (struct smb_ace *)((char *)ace + le16_to_cpu(ace->size)); } if (IS_ENABLED(CONFIG_FS_POSIX_ACL)) { posix_acls = get_inode_acl(d_inode(path->dentry), ACL_TYPE_ACCESS); - if (!IS_ERR_OR_NULL(posix_acls) && !found && !others_ace) { + /* + * An Authenticated Users ACE keeps the POSIX ACL from deciding, + * also for sessions that it does not match. For a strict + * MAXIMUM_ALLOWED request, ACEs for the caller's groups or + * OWNER RIGHTS decide before the POSIX ACL, unless the DACL has + * an object deny ACE, which ksmbd does not parse. + */ + if (!IS_ERR_OR_NULL(posix_acls) && !found && !others_ace && + !au_ace && !(strict && maximal && grp_ace && !obj_deny)) { unsigned int id = -1; pa_entry = posix_acls->a_entries; @@ -1646,6 +1771,15 @@ next_ace: if (!access_bits) access_bits = SET_MINIMUM_RIGHTS; + /* + * Only ACEs for the caller's groups, + * or OWNER RIGHTS for the owner, apply + * here. The POSIX ACL entry does not + * grant an explicitly requested right + * that they deny first in DACL order. + */ + access_bits &= ~(grp_denied & + requested); posix_acl_release(posix_acls); goto check_access_bits; } @@ -1668,7 +1802,19 @@ next_ace: */ if (is_owner && !or_ace) allowed = READ_CONTROL | WRITE_DAC; - if (*pdaccess & FILE_MAXIMAL_ACCESS_LE) + /* + * Add what ACEs for the caller's groups, or OWNER + * RIGHTS for the owner, grant in DACL order, unless an + * object deny ACE, which ksmbd does not parse, could + * apply. + */ + if (!obj_deny) + allowed |= grp_granted; + if (!strict) + allowed |= smb_parent_derived_rights(conn, path, uid, + authenticated, + requested & ~allowed); + if (maximal) requested |= allowed; if (!allowed || requested & ~allowed) { ksmbd_debug(SMB, "Can't find corresponding sid\n"); @@ -1680,7 +1826,7 @@ next_ace: } } - if (!(*pdaccess & FILE_MAXIMAL_ACCESS_LE)) { + if (!maximal && obj_deny) { switch (ace->type) { case ACCESS_ALLOWED_ACE_TYPE: access_bits = le32_to_cpu(ace->access_req); @@ -1714,6 +1860,45 @@ err_out: } EXPORT_SYMBOL_IF_KUNIT(smb_check_perm_dacl); +/* + * Return the rights in @want that @path gets from its parent directory: + * DELETE if the parent's DACL grants FILE_DELETE_CHILD, and + * FILE_READ_ATTRIBUTES if it grants FILE_LIST_DIRECTORY + * (MS-FSA 2.1.5.1.2.1). A parent with no DACL, or with one that ksmbd + * cannot read, gives nothing. Callers pass only rights that the request + * names. + */ +static unsigned int smb_parent_derived_rights(struct ksmbd_conn *conn, + const struct path *path, int uid, + bool authenticated, + unsigned int want) +{ + struct path parent = { .mnt = path->mnt }; + __le32 granted = FILE_MAXIMAL_ACCESS_LE; + unsigned int rights = 0; + int rc; + + if (!(want & (DELETE | FILE_READ_ATTRIBUTES))) + return 0; + + /* The root of the mount, or a disconnected dentry, has no parent */ + if (path->dentry == path->mnt->mnt_root || IS_ROOT(path->dentry)) + return 0; + + parent.dentry = dget_parent(path->dentry); + /* Strict, so the parent's own parent is not consulted */ + rc = smb_check_perm_dacl(conn, &parent, &granted, 0, uid, + authenticated, true); + dput(parent.dentry); + if (rc || granted == FILE_MAXIMAL_ACCESS_LE) + return 0; + if (granted & FILE_DELETE_CHILD_LE) + rights |= DELETE; + if (granted & FILE_LIST_DIRECTORY_LE) + rights |= FILE_READ_ATTRIBUTES; + return rights & want; +} + int set_info_sec(struct ksmbd_conn *conn, struct ksmbd_tree_connect *tcon, const struct path *path, struct smb_ntsd *pntsd, int ntsd_len, bool type_check, bool get_write, bool posix_open, diff --git a/fs/smb/server/smbacl.h b/fs/smb/server/smbacl.h index 3342cebab333..bbd35948a379 100644 --- a/fs/smb/server/smbacl.h +++ b/fs/smb/server/smbacl.h @@ -97,7 +97,7 @@ int smb_inherit_dacl(struct ksmbd_conn *conn, const struct path *path, unsigned int uid, unsigned int gid); int smb_check_perm_dacl(struct ksmbd_conn *conn, const struct path *path, __le32 *pdaccess, __le32 raw_daccess, int uid, - bool strict); + bool authenticated, bool strict); int set_info_sec(struct ksmbd_conn *conn, struct ksmbd_tree_connect *tcon, const struct path *path, struct smb_ntsd *pntsd, int ntsd_len, bool type_check, bool get_write, bool posix_open, diff --git a/fs/smb/server/tests/smbacl_kunit.c b/fs/smb/server/tests/smbacl_kunit.c index 33496b4d31a3..bf28439a7696 100644 --- a/fs/smb/server/tests/smbacl_kunit.c +++ b/fs/smb/server/tests/smbacl_kunit.c @@ -229,7 +229,7 @@ static void ksmbd_smb_check_perm_dacl_boundary_test(struct kunit *test) goto out; rc = smb_check_perm_dacl(NULL, &file->f_path, &daccess, - cpu_to_le32(FILE_READ_DATA), 0, false); + cpu_to_le32(FILE_READ_DATA), 0, true, false); /* * The post-boundary ACE (ACE #2, beyond pdacl->size) grants @@ -271,7 +271,7 @@ ksmbd_smb_check_perm_dacl_maximal_boundary_test(struct kunit *test) goto out; rc = smb_check_perm_dacl(NULL, &file->f_path, &daccess, - FILE_MAXIMAL_ACCESS_LE, 0, false); + FILE_MAXIMAL_ACCESS_LE, 0, true, false); KUNIT_EXPECT_EQ(test, 0, rc); if (rc) goto out; diff --git a/fs/smb/server/vfs.c b/fs/smb/server/vfs.c index 1f2bc76f0d09..cb3f4f5fe9cf 100644 --- a/fs/smb/server/vfs.c +++ b/fs/smb/server/vfs.c @@ -367,6 +367,10 @@ int ksmbd_vfs_read(struct ksmbd_work *work, struct ksmbd_file *fp, size_t count, if (ksmbd_stream_fd(fp)) return ksmbd_vfs_stream_read(fp, rbuf, pos, count); + /* The access granted on open can include FILE_EXECUTE for a write-only file */ + if (!(filp->f_mode & FMODE_READ)) + return -EACCES; + if (!fp->is_posix_ctxt) { int ret; diff --git a/fs/smb/server/vfs_cache.c b/fs/smb/server/vfs_cache.c index f9079c529d07..c846023e5ada 100644 --- a/fs/smb/server/vfs_cache.c +++ b/fs/smb/server/vfs_cache.c @@ -420,7 +420,12 @@ bool ksmbd_has_stream_without_delete_share(struct ksmbd_file *fp) if (file_inode(fp->filp) != file_inode(prev_fp->filp)) continue; - if (!(prev_fp->saccess & FILE_SHARE_DELETE_LE)) { + /* + * A stream open with no right in FILE_SHARE_CHECKED_ACCESS_LE, + * implied writes included, never conflicts. + */ + if (!(prev_fp->saccess & FILE_SHARE_DELETE_LE) && + ksmbd_sharing_daccess(prev_fp) & FILE_SHARE_CHECKED_ACCESS_LE) { ret = true; break; } diff --git a/fs/smb/server/vfs_cache.h b/fs/smb/server/vfs_cache.h index a0239ec178d8..57c81287efe6 100644 --- a/fs/smb/server/vfs_cache.h +++ b/fs/smb/server/vfs_cache.h @@ -131,6 +131,8 @@ struct ksmbd_file { bool is_nt_open; bool attrib_only; + /* writes data on open, see ksmbd_sharing_daccess() */ + bool implied_write; bool allocation_size_set; char client_guid[16];