Skip to content

Commit 8e47d29

Browse files
mjbommargregkh
authored andcommitted
smb: client: validate the whole DACL before rewriting it in cifsacl
[ Upstream commit 0a8cf16 ] build_sec_desc() and id_mode_to_cifs_acl() derive a DACL pointer from a server-supplied dacloffset and then use the incoming ACL to rebuild the chmod/chown security descriptor. The original fix only checked that the struct smb_acl header fits before reading dacl_ptr->size or dacl_ptr->num_aces. That avoids the immediate header-field OOB read, but the rewrite helpers still walk ACEs based on pdacl->num_aces with no structural validation of the incoming DACL body. A malicious server can return a truncated DACL that still contains a header, claims one or more ACEs, and then drive replace_sids_and_copy_aces() or set_chmod_dacl() past the validated extent while they compare or copy attacker-controlled ACEs. Factor the DACL structural checks into validate_dacl(), extend them to validate each ACE against the DACL bounds, and use the shared validator before the chmod/chown rebuild paths. parse_dacl() reuses the same validator so the read-side parser and write-side rewrite paths agree on what constitutes a well-formed incoming DACL. Fixes: bc3e9dd ("cifs: Change SIDs in ACEs while transferring file ownership.") Cc: stable@vger.kernel.org Assisted-by: Claude:claude-opus-4-6 Assisted-by: Codex:gpt-5-4 Signed-off-by: Michael Bommarito <michael.bommarito@gmail.com> Signed-off-by: Steve French <stfrench@microsoft.com> [ no kmalloc_objs ] Signed-off-by: Sasha Levin <sashal@kernel.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 325d4ac commit 8e47d29

1 file changed

Lines changed: 85 additions & 31 deletions

File tree

fs/smb/client/cifsacl.c

Lines changed: 85 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -758,14 +758,85 @@ static void dump_ace(struct smb_ace *pace, char *end_of_acl)
758758
}
759759
#endif
760760

761+
static int validate_dacl(struct smb_acl *pdacl, char *end_of_acl)
762+
{
763+
int i, ace_hdr_size, ace_size, min_ace_size;
764+
u16 dacl_size, num_aces;
765+
char *acl_base, *end_of_dacl;
766+
struct smb_ace *pace;
767+
768+
if (!pdacl)
769+
return 0;
770+
771+
if (end_of_acl < (char *)pdacl + sizeof(struct smb_acl)) {
772+
cifs_dbg(VFS, "ACL too small to parse DACL\n");
773+
return -EINVAL;
774+
}
775+
776+
dacl_size = le16_to_cpu(pdacl->size);
777+
if (dacl_size < sizeof(struct smb_acl) ||
778+
end_of_acl < (char *)pdacl + dacl_size) {
779+
cifs_dbg(VFS, "ACL too small to parse DACL\n");
780+
return -EINVAL;
781+
}
782+
783+
num_aces = le16_to_cpu(pdacl->num_aces);
784+
if (!num_aces)
785+
return 0;
786+
787+
ace_hdr_size = offsetof(struct smb_ace, sid) +
788+
offsetof(struct smb_sid, sub_auth);
789+
min_ace_size = ace_hdr_size + sizeof(__le32);
790+
if (num_aces > (dacl_size - sizeof(struct smb_acl)) / min_ace_size) {
791+
cifs_dbg(VFS, "ACL too small to parse DACL\n");
792+
return -EINVAL;
793+
}
794+
795+
end_of_dacl = (char *)pdacl + dacl_size;
796+
acl_base = (char *)pdacl;
797+
ace_size = sizeof(struct smb_acl);
798+
799+
for (i = 0; i < num_aces; ++i) {
800+
if (end_of_dacl - acl_base < ace_size) {
801+
cifs_dbg(VFS, "ACL too small to parse ACE\n");
802+
return -EINVAL;
803+
}
804+
805+
pace = (struct smb_ace *)(acl_base + ace_size);
806+
acl_base = (char *)pace;
807+
808+
if (end_of_dacl - acl_base < ace_hdr_size ||
809+
pace->sid.num_subauth == 0 ||
810+
pace->sid.num_subauth > SID_MAX_SUB_AUTHORITIES) {
811+
cifs_dbg(VFS, "ACL too small to parse ACE\n");
812+
return -EINVAL;
813+
}
814+
815+
ace_size = ace_hdr_size + sizeof(__le32) * pace->sid.num_subauth;
816+
if (end_of_dacl - acl_base < ace_size ||
817+
le16_to_cpu(pace->size) < ace_size) {
818+
cifs_dbg(VFS, "ACL too small to parse ACE\n");
819+
return -EINVAL;
820+
}
821+
822+
ace_size = le16_to_cpu(pace->size);
823+
if (end_of_dacl - acl_base < ace_size) {
824+
cifs_dbg(VFS, "ACL too small to parse ACE\n");
825+
return -EINVAL;
826+
}
827+
}
828+
829+
return 0;
830+
}
831+
761832
static void parse_dacl(struct smb_acl *pdacl, char *end_of_acl,
762833
struct smb_sid *pownersid, struct smb_sid *pgrpsid,
763834
struct cifs_fattr *fattr, bool mode_from_special_sid)
764835
{
765836
int i;
766837
u16 num_aces = 0;
767838
int acl_size;
768-
char *acl_base;
839+
char *acl_base, *end_of_dacl;
769840
struct smb_ace **ppace;
770841

771842
/* BB need to add parm so we can store the SID BB */
@@ -777,12 +848,8 @@ static void parse_dacl(struct smb_acl *pdacl, char *end_of_acl,
777848
return;
778849
}
779850

780-
/* validate that we do not go past end of acl */
781-
if (end_of_acl < (char *)pdacl + sizeof(struct smb_acl) ||
782-
end_of_acl < (char *)pdacl + le16_to_cpu(pdacl->size)) {
783-
cifs_dbg(VFS, "ACL too small to parse DACL\n");
851+
if (validate_dacl(pdacl, end_of_acl))
784852
return;
785-
}
786853

787854
cifs_dbg(NOISY, "DACL revision %d size %d num aces %d\n",
788855
le16_to_cpu(pdacl->revision), le16_to_cpu(pdacl->size),
@@ -793,43 +860,24 @@ static void parse_dacl(struct smb_acl *pdacl, char *end_of_acl,
793860
user/group/other have no permissions */
794861
fattr->cf_mode &= ~(0777);
795862

863+
end_of_dacl = (char *)pdacl + le16_to_cpu(pdacl->size);
796864
acl_base = (char *)pdacl;
797865
acl_size = sizeof(struct smb_acl);
798866

799867
num_aces = le16_to_cpu(pdacl->num_aces);
800868
if (num_aces > 0) {
801869
umode_t denied_mode = 0;
802870

803-
if (num_aces > (le16_to_cpu(pdacl->size) - sizeof(struct smb_acl)) /
804-
(offsetof(struct smb_ace, sid) +
805-
offsetof(struct smb_sid, sub_auth) + sizeof(__le16)))
806-
return;
807-
808871
ppace = kmalloc_array(num_aces, sizeof(struct smb_ace *),
809872
GFP_KERNEL);
810873
if (!ppace)
811874
return;
812875

813876
for (i = 0; i < num_aces; ++i) {
814-
if (end_of_acl - acl_base < acl_size)
815-
break;
816-
817877
ppace[i] = (struct smb_ace *) (acl_base + acl_size);
818-
acl_base = (char *)ppace[i];
819-
acl_size = offsetof(struct smb_ace, sid) +
820-
offsetof(struct smb_sid, sub_auth);
821-
822-
if (end_of_acl - acl_base < acl_size ||
823-
ppace[i]->sid.num_subauth == 0 ||
824-
ppace[i]->sid.num_subauth > SID_MAX_SUB_AUTHORITIES ||
825-
(end_of_acl - acl_base <
826-
acl_size + sizeof(__le32) * ppace[i]->sid.num_subauth) ||
827-
(le16_to_cpu(ppace[i]->size) <
828-
acl_size + sizeof(__le32) * ppace[i]->sid.num_subauth))
829-
break;
830878

831879
#ifdef CONFIG_CIFS_DEBUG2
832-
dump_ace(ppace[i], end_of_acl);
880+
dump_ace(ppace[i], end_of_dacl);
833881
#endif
834882
if (mode_from_special_sid &&
835883
ppace[i]->sid.num_subauth >= 3 &&
@@ -872,6 +920,7 @@ static void parse_dacl(struct smb_acl *pdacl, char *end_of_acl,
872920
(void *)ppace[i],
873921
sizeof(struct smb_ace)); */
874922

923+
acl_base = (char *)ppace[i];
875924
acl_size = le16_to_cpu(ppace[i]->size);
876925
}
877926

@@ -1317,10 +1366,9 @@ static int build_sec_desc(struct smb_ntsd *pntsd, struct smb_ntsd *pnntsd,
13171366
}
13181367

13191368
dacl_ptr = (struct smb_acl *)((char *)pntsd + dacloffset);
1320-
if (end_of_acl < (char *)dacl_ptr + le16_to_cpu(dacl_ptr->size)) {
1321-
cifs_dbg(VFS, "Server returned illegal ACL size\n");
1322-
return -EINVAL;
1323-
}
1369+
rc = validate_dacl(dacl_ptr, end_of_acl);
1370+
if (rc)
1371+
return rc;
13241372
}
13251373

13261374
owner_sid_ptr = (struct smb_sid *)((char *)pntsd +
@@ -1697,6 +1745,12 @@ id_mode_to_cifs_acl(struct inode *inode, const char *path, __u64 *pnmode,
16971745
}
16981746

16991747
dacl_ptr = (struct smb_acl *)((char *)pntsd + dacloffset);
1748+
rc = validate_dacl(dacl_ptr, (char *)pntsd + secdesclen);
1749+
if (rc) {
1750+
kfree(pntsd);
1751+
cifs_put_tlink(tlink);
1752+
return rc;
1753+
}
17001754
if (mode_from_sid)
17011755
nsecdesclen +=
17021756
le16_to_cpu(dacl_ptr->num_aces) * sizeof(struct smb_ace);

0 commit comments

Comments
 (0)