ksmbd: follow-ups to the security descriptor codec
Subsystems (2)
cover ksmbd: follow-ups to the security descriptor codec
The codec series [1] added a decoder and an encoder for security descriptors (SDs) and replaced ksmbd's walks of raw SD bytes with them. It changed as few of the callers' decisions as it could.
The issues in this series were found during the work on the codec. They were held out of that series to reduce its changes in behavior. Now that the codec is merged, they are submitted here as four hardening changes. Each patch touches only smbacl.c, and its message describes the change.
Tests
=====
Tested on ksmbd-for-next f9208b7a4919, on x86_64 only.
- Each patch builds with W=1 and no warnings (GCC 14, with KASAN), and sparse reports nothing in fs/smb/server.
- KUnit under UML passes at each patch with GCC 14, and on the base and at the last patch with clang 22. The series adds no KUnit case.
- checkpatch --strict reports nothing.
- A script with a test for each patch was run over SMB under five modules, the base and each patch, on a KASAN kernel booted with kasan_multi_shot. Each test changed its result at its own patch and at no other. The test of patch 3 writes the stored SD into the xattr, since SET_INFO refuses the ACE that it needs.
- smbtorture smb2.acls, smb2.setinfo, smb2.getinfo, smb2.create, smb2.sdread, smb2.maximum_allowed, smb2.winattr, smb2.delete-on-close-perms, smb2.dosmode, and smb2.rename give the same results on the base and with the series. There is no KASAN report or warning, the kernel stays untainted, and kmemleak reports nothing.
- Out of tree, the functions that patches 1 and 2 change were run in userspace under ASan and UBSan against upstream's functions from before the codec series, compiled verbatim. That was about 1.2 million comparisons over generated and hand-written SDs for the QUERY_INFO reply, SET_INFO, and inheritance. Every difference is one that this series or the codec series lists. The comparison does not run the access check, and its store never fails, so patches 3 and 4 rest on the tests over SMB.
This series was developed with LLM assistance.
[1] https://lore.kernel.org/linux-cifs/[email protected]/
Ian Bridges (4):
ksmbd: match SIDs exactly when a DACL is mapped to POSIX
ksmbd: replace only the CREATOR SIDs in inheritance
ksmbd: skip SIDs with no sub-authority in the access check
ksmbd: build the SD from the inode if an inherited SD is not stored
fs/smb/server/smbacl.c | 42 +++++++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 19 deletions(-)
base-commit: f9208b7a49196e36c0434637ab83e001f5800a1d
Signature · 1 line
2.47.3
PATCH 1/4 ksmbd: match SIDs exactly when a DACL is mapped to POSIX
parse_dacl() maps the ACEs of a DACL from a client to the mode and the POSIX ACLs of the file. It compares the SID of each ACE with the owner, the group, and Everyone. It uses compare_sids(), which compares only the sub-authorities that both SIDs have. So a shorter or longer SID matches as well. An ACE for S-1-1-0-5 sets the mode bits for others. If the SD names a user of the server's domain as its owner, an ACE for the domain SID is mapped as the owner's ACE.
Use smb_sid_equal() instead, which also compares the number of sub-authorities. Do the same for CREATOR OWNER, CREATOR GROUP, and Authenticated Users. parse_dacl() skips the ACEs for those three. An ACE that no longer matches is handled like an ACE for any other SID.
The NFS mode ACE has the SID S-1-5-88-3-<mode>. Take the mode only from a SID with three sub-authorities. Before, a longer SID that starts with S-1-5-88-3 was read as a mode ACE too.
Move smb_sid_equal() above parse_dacl(), which now calls it.
--- fs/smb/server/smbacl.c | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-)
fs/smb/server/smbacl.c +13 -13
@@ -376,6 +376,12 @@ void free_acl_state(struct posix_acl_state *state)kfree (state->groups); } +/* 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); +} + static int parse_dacl(const struct mnt_idmap *idmap, struct ksmbd_sd *sd, const struct smb_sid *pownersid,
@@ -423,13 +429,13 @@ static int parse_dacl(const struct mnt_idmap *idmap,continue ;if (ace->type ==ACCESS_ALLOWED && - ace->sid->num_subauth >=3 && + ace->sid->num_subauth ==3 && !(compare_sids (ace->sid, &sid_unix_NFS_mode))) { fattr->cf_mode =le32_to_cpu (ace->sid->sub_auth[2 ]); fattr->cf_posix_mode = true; nfs_mode_found = true; - }else if (!compare_sids (ace->sid, pownersid)) { + }else if (pownersid &&smb_sid_equal (ace->sid, pownersid)) { acl_mode =access_flags_to_mode (fattr,cpu_to_le32 (ace->mask), ace->type);
@@ -440,7 +446,7 @@ static int parse_dacl(const struct mnt_idmap *idmap, mode |= acl_mode; } owner_found = true; - }else if (!compare_sids (ace->sid, pgrpsid) || + }else if ((pgrpsid &&smb_sid_equal (ace->sid, pgrpsid)) || ace->sid->sub_auth[ace->sid->num_subauth -1 ] ==DOMAIN_USER_RID_LE ) { acl_mode = access_flags_to_mode(fattr,
@@ -452,7 +458,7 @@ static int parse_dacl(const struct mnt_idmap *idmap, mode |= acl_mode; } group_found = true; - }else if (!compare_sids (ace->sid, &sid_everyone)) { + }else if (smb_sid_equal (ace->sid, &sid_everyone)) { acl_mode =access_flags_to_mode (fattr,cpu_to_le32 (ace->mask), ace->type);
@@ -462,11 +468,11 @@ static int parse_dacl(const struct mnt_idmap *idmap, mode |= acl_mode; } others_found = true; - }else if (!compare_sids (ace->sid, &creator_owner)) { + }else if (smb_sid_equal (ace->sid, &creator_owner)) {continue ; - }else if (!compare_sids (ace->sid, &creator_group)) { + }else if (smb_sid_equal (ace->sid, &creator_group)) {continue ; - }else if (!compare_sids (ace->sid, &sid_authusers)) { + }else if (smb_sid_equal (ace->sid, &sid_authusers)) {continue ; }else {struct smb_fattr temp_fattr;
@@ -1083,12 +1089,6 @@ static bool smb_sid_in_group(const struct smb_sid *sid)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
Signature · 1 line
2.47.3
PATCH 2/4 ksmbd: replace only the CREATOR SIDs in inheritance
In reply to Ian Bridgessmb_inherit_dacl() replaces CREATOR OWNER (S-1-3-0) and CREATOR GROUP (S-1-3-1) in an inherited ACE. It puts in the SID of the user who creates the file and the SID of that user's group. It finds the two CREATOR SIDs with compare_sids(), which compares only the sub-authorities that both SIDs have. So an ACE for a longer SID, such as S-1-3-0-9, is replaced as well.
Use smb_sid_equal() instead, which also compares the number of sub-authorities. Only CREATOR OWNER and CREATOR GROUP are replaced. An ACE for any other SID is inherited with its SID.
--- fs/smb/server/smbacl.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
fs/smb/server/smbacl.c +2 -2
@@ -1008,11 +1008,11 @@ int smb_inherit_dacl(struct ksmbd_conn *conn, flags =0 ; } -if (!compare_sids (&creator_owner, parent_aces->sid)) { +if (smb_sid_equal (&creator_owner, parent_aces->sid)) { creator = &creator_owner;id_to_sid (uid,SIDOWNER , &owner_sid); psid = &owner_sid; - }else if (!compare_sids (&creator_group, parent_aces->sid)) { + }else if (smb_sid_equal (&creator_group, parent_aces->sid)) { creator = &creator_group;id_to_sid (gid,SIDUNIX_GROUP , &group_sid); psid = &group_sid;
Signature · 1 line
2.47.3
PATCH 3/4 ksmbd: skip SIDs with no sub-authority in the access check
In reply to Ian Bridgessmb_check_perm_dacl() finds the ACEs for the user who makes a request with compare_sids(), which compares only the sub-authorities that both SIDs have. A SID with no sub-authority has none to compare, so it matches every SID of its authority. An ACE for S-1-5 matches every user of the server's domain, Authenticated Users, and the NFS mode SID. An ACE for S-1-1 matches Everyone, and an ACE for S-1-3 matches OWNER RIGHTS.
ksmbd gives every user a SID with at least one sub-authority. Everyone, Authenticated Users, OWNER RIGHTS, and the NFS mode SID have one too. So an ACE whose SID has no sub-authority is for none of them. Skip such an ACE in both loops over the DACL, as an object ACE is skipped.
This changes what the check decides for a stored SD that holds such an ACE. The ACE no longer applies to any user. A request that was granted because of the ACE can be denied. A MAXIMUM_ALLOWED open no longer gets rights from an allow ACE with such a SID, and it no longer loses rights to a deny ACE with one.
SET_INFO refuses an ACE with such a SID, so a client cannot set one today.
---
Notes:
Tested over SMB with two stored DACLs.
- The DACL has one ACE, which allows read and write to S-1-5. Before, the owner and another user could each open the file for READ_CONTROL, WRITE_DAC, and DELETE. A MAXIMUM_ALLOWED open gave each of them READ_DATA and WRITE_DATA. Now the owner can open the file for READ_CONTROL and WRITE_DAC, and a MAXIMUM_ALLOWED open gives the owner those two rights. The other user is denied.
- The DACL denies WRITE_DATA to S-1-5 and then allows everything to Everyone. Before, a MAXIMUM_ALLOWED open was granted everything but WRITE_DATA. Now it is granted WRITE_DATA too. An open that asks for WRITE_DATA itself is granted before and after.
fs/smb/server/smbacl.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
fs/smb/server/smbacl.c +3 -3
@@ -1193,8 +1193,8 @@ int smb_check_perm_dacl(struct ksmbd_conn *conn, const struct path *path, ace->type == ACCESS_DENIED_CALLBACK_OBJECT_ACE_TYPE) && !(ace->flags & INHERIT_ONLY_ACE)) obj_deny = true; -/* ksmbd evaluates no object ACE, so one matches no one */ -if (ace->obj_len) +/* An object ACE or a SID with no sub-authority matches no one */ +if (ace->obj_len || !ace->sid->num_subauth)continue ;if (!compare_sids (&sid_owner_rights, ace->sid)) {
@@ -1277,7 +1277,7 @@ int smb_check_perm_dacl(struct ksmbd_conn *conn, const struct path *path,for (i =0 ; i < sd->num_aces; i++) { ace = &sd->aces[i]; -if (ace->obj_len) +if (ace->obj_len || !ace->sid->num_subauth)continue ;if (!compare_sids (&sid, ace->sid) || !compare_sids (&sid_unix_NFS_mode, ace->sid)) {
Signature · 1 line
2.47.3
PATCH 4/4 ksmbd: build the SD from the inode if an inherited SD is not stored
In reply to Ian Bridgessmb_inherit_dacl() ignores what ksmbd_vfs_set_sd_xattr() returns and reports success. smb2_open() then takes the inheritance as done. A new file whose inherited SD could not be stored is left with no stored SD. The store fails, for example, with -ENOSPC when the filesystem has no room for an xattr of that size.
Return the error of the store. smb2_open() then runs its fallback, as it does when the parent has no stored SD.
--- fs/smb/server/smbacl.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
fs/smb/server/smbacl.c +5 -1
@@ -1047,7 +1047,11 @@ int smb_inherit_dacl(struct ksmbd_conn *conn,if (rc) goto free_aces_base; -ksmbd_vfs_set_sd_xattr (conn, idmap, path, pntsd, pntsd_size); +/* +* If the filesystem refuses this SD, the caller tries the SD +* built from the inode, which may fit. +*/ + rc =ksmbd_vfs_set_sd_xattr (conn, idmap, path, pntsd, pntsd_size);kvfree (pntsd); } free_aces_base:
Signature · 1 line
2.47.3