Skip to content

Commit 8f05411

Browse files
namjaejeonSteve French
authored and
Steve French
committed
ksmbd: fix heap-based overflow in set_ntacl_dacl()
The testcase use SMB2_SET_INFO_HE command to set a malformed file attribute under the label `security.NTACL`. SMB2_QUERY_INFO_HE command in testcase trigger the following overflow. [ 4712.003781] ================================================================== [ 4712.003790] BUG: KASAN: slab-out-of-bounds in build_sec_desc+0x842/0x1dd0 [ksmbd] [ 4712.003807] Write of size 1060 at addr ffff88801e34c068 by task kworker/0:0/4190 [ 4712.003813] CPU: 0 PID: 4190 Comm: kworker/0:0 Not tainted 5.19.0-rc5 #1 [ 4712.003850] Workqueue: ksmbd-io handle_ksmbd_work [ksmbd] [ 4712.003867] Call Trace: [ 4712.003870] <TASK> [ 4712.003873] dump_stack_lvl+0x49/0x5f [ 4712.003935] print_report.cold+0x5e/0x5cf [ 4712.003972] ? ksmbd_vfs_get_sd_xattr+0x16d/0x500 [ksmbd] [ 4712.003984] ? cmp_map_id+0x200/0x200 [ 4712.003988] ? build_sec_desc+0x842/0x1dd0 [ksmbd] [ 4712.004000] kasan_report+0xaa/0x120 [ 4712.004045] ? build_sec_desc+0x842/0x1dd0 [ksmbd] [ 4712.004056] kasan_check_range+0x100/0x1e0 [ 4712.004060] memcpy+0x3c/0x60 [ 4712.004064] build_sec_desc+0x842/0x1dd0 [ksmbd] [ 4712.004076] ? parse_sec_desc+0x580/0x580 [ksmbd] [ 4712.004088] ? ksmbd_acls_fattr+0x281/0x410 [ksmbd] [ 4712.004099] smb2_query_info+0xa8f/0x6110 [ksmbd] [ 4712.004111] ? psi_group_change+0x856/0xd70 [ 4712.004148] ? update_load_avg+0x1c3/0x1af0 [ 4712.004152] ? asym_cpu_capacity_scan+0x5d0/0x5d0 [ 4712.004157] ? xas_load+0x23/0x300 [ 4712.004162] ? smb2_query_dir+0x1530/0x1530 [ksmbd] [ 4712.004173] ? _raw_spin_lock_bh+0xe0/0xe0 [ 4712.004179] handle_ksmbd_work+0x30e/0x1020 [ksmbd] [ 4712.004192] process_one_work+0x778/0x11c0 [ 4712.004227] ? _raw_spin_lock_irq+0x8e/0xe0 [ 4712.004231] worker_thread+0x544/0x1180 [ 4712.004234] ? __cpuidle_text_end+0x4/0x4 [ 4712.004239] kthread+0x282/0x320 [ 4712.004243] ? process_one_work+0x11c0/0x11c0 [ 4712.004246] ? kthread_complete_and_exit+0x30/0x30 [ 4712.004282] ret_from_fork+0x1f/0x30 This patch add the buffer validation for security descriptor that is stored by malformed SMB2_SET_INFO_HE command. and allocate large response buffer about SMB2_O_INFO_SECURITY file info class. Fixes: e2f3448 ("cifsd: add server-side procedures for SMB3") Cc: stable@vger.kernel.org Reported-by: zdi-disclosures@trendmicro.com # ZDI-CAN-17771 Reviewed-by: Hyunchul Lee <hyc.lee@gmail.com> Signed-off-by: Namjae Jeon <linkinjeon@kernel.org> Signed-off-by: Steve French <stfrench@microsoft.com>
1 parent 824d4f6 commit 8f05411

File tree

4 files changed

+119
-57
lines changed

4 files changed

+119
-57
lines changed

fs/ksmbd/smb2pdu.c

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -535,9 +535,10 @@ int smb2_allocate_rsp_buf(struct ksmbd_work *work)
535535
struct smb2_query_info_req *req;
536536

537537
req = smb2_get_msg(work->request_buf);
538-
if (req->InfoType == SMB2_O_INFO_FILE &&
539-
(req->FileInfoClass == FILE_FULL_EA_INFORMATION ||
540-
req->FileInfoClass == FILE_ALL_INFORMATION))
538+
if ((req->InfoType == SMB2_O_INFO_FILE &&
539+
(req->FileInfoClass == FILE_FULL_EA_INFORMATION ||
540+
req->FileInfoClass == FILE_ALL_INFORMATION)) ||
541+
req->InfoType == SMB2_O_INFO_SECURITY)
541542
sz = large_sz;
542543
}
543544

@@ -2988,7 +2989,7 @@ int smb2_open(struct ksmbd_work *work)
29882989
goto err_out;
29892990

29902991
rc = build_sec_desc(user_ns,
2991-
pntsd, NULL,
2992+
pntsd, NULL, 0,
29922993
OWNER_SECINFO |
29932994
GROUP_SECINFO |
29942995
DACL_SECINFO,
@@ -3833,6 +3834,15 @@ static int verify_info_level(int info_level)
38333834
return 0;
38343835
}
38353836

3837+
static int smb2_resp_buf_len(struct ksmbd_work *work, unsigned short hdr2_len)
3838+
{
3839+
int free_len;
3840+
3841+
free_len = (int)(work->response_sz -
3842+
(get_rfc1002_len(work->response_buf) + 4)) - hdr2_len;
3843+
return free_len;
3844+
}
3845+
38363846
static int smb2_calc_max_out_buf_len(struct ksmbd_work *work,
38373847
unsigned short hdr2_len,
38383848
unsigned int out_buf_len)
@@ -3842,9 +3852,7 @@ static int smb2_calc_max_out_buf_len(struct ksmbd_work *work,
38423852
if (out_buf_len > work->conn->vals->max_trans_size)
38433853
return -EINVAL;
38443854

3845-
free_len = (int)(work->response_sz -
3846-
(get_rfc1002_len(work->response_buf) + 4)) -
3847-
hdr2_len;
3855+
free_len = smb2_resp_buf_len(work, hdr2_len);
38483856
if (free_len < 0)
38493857
return -EINVAL;
38503858

@@ -5107,10 +5115,10 @@ static int smb2_get_info_sec(struct ksmbd_work *work,
51075115
struct smb_ntsd *pntsd = (struct smb_ntsd *)rsp->Buffer, *ppntsd = NULL;
51085116
struct smb_fattr fattr = {{0}};
51095117
struct inode *inode;
5110-
__u32 secdesclen;
5118+
__u32 secdesclen = 0;
51115119
unsigned int id = KSMBD_NO_FID, pid = KSMBD_NO_FID;
51125120
int addition_info = le32_to_cpu(req->AdditionalInformation);
5113-
int rc;
5121+
int rc = 0, ppntsd_size = 0;
51145122

51155123
if (addition_info & ~(OWNER_SECINFO | GROUP_SECINFO | DACL_SECINFO |
51165124
PROTECTED_DACL_SECINFO |
@@ -5156,11 +5164,14 @@ static int smb2_get_info_sec(struct ksmbd_work *work,
51565164

51575165
if (test_share_config_flag(work->tcon->share_conf,
51585166
KSMBD_SHARE_FLAG_ACL_XATTR))
5159-
ksmbd_vfs_get_sd_xattr(work->conn, user_ns,
5160-
fp->filp->f_path.dentry, &ppntsd);
5161-
5162-
rc = build_sec_desc(user_ns, pntsd, ppntsd, addition_info,
5163-
&secdesclen, &fattr);
5167+
ppntsd_size = ksmbd_vfs_get_sd_xattr(work->conn, user_ns,
5168+
fp->filp->f_path.dentry,
5169+
&ppntsd);
5170+
5171+
/* Check if sd buffer size exceeds response buffer size */
5172+
if (smb2_resp_buf_len(work, 8) > ppntsd_size)
5173+
rc = build_sec_desc(user_ns, pntsd, ppntsd, ppntsd_size,
5174+
addition_info, &secdesclen, &fattr);
51645175
posix_acl_release(fattr.cf_acls);
51655176
posix_acl_release(fattr.cf_dacls);
51665177
kfree(ppntsd);

fs/ksmbd/smbacl.c

Lines changed: 88 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,7 @@ static void set_posix_acl_entries_dacl(struct user_namespace *user_ns,
690690
static void set_ntacl_dacl(struct user_namespace *user_ns,
691691
struct smb_acl *pndacl,
692692
struct smb_acl *nt_dacl,
693+
unsigned int aces_size,
693694
const struct smb_sid *pownersid,
694695
const struct smb_sid *pgrpsid,
695696
struct smb_fattr *fattr)
@@ -703,9 +704,19 @@ static void set_ntacl_dacl(struct user_namespace *user_ns,
703704
if (nt_num_aces) {
704705
ntace = (struct smb_ace *)((char *)nt_dacl + sizeof(struct smb_acl));
705706
for (i = 0; i < nt_num_aces; i++) {
706-
memcpy((char *)pndace + size, ntace, le16_to_cpu(ntace->size));
707-
size += le16_to_cpu(ntace->size);
708-
ntace = (struct smb_ace *)((char *)ntace + le16_to_cpu(ntace->size));
707+
unsigned short nt_ace_size;
708+
709+
if (offsetof(struct smb_ace, access_req) > aces_size)
710+
break;
711+
712+
nt_ace_size = le16_to_cpu(ntace->size);
713+
if (nt_ace_size > aces_size)
714+
break;
715+
716+
memcpy((char *)pndace + size, ntace, nt_ace_size);
717+
size += nt_ace_size;
718+
aces_size -= nt_ace_size;
719+
ntace = (struct smb_ace *)((char *)ntace + nt_ace_size);
709720
num_aces++;
710721
}
711722
}
@@ -878,7 +889,7 @@ int parse_sec_desc(struct user_namespace *user_ns, struct smb_ntsd *pntsd,
878889
/* Convert permission bits from mode to equivalent CIFS ACL */
879890
int build_sec_desc(struct user_namespace *user_ns,
880891
struct smb_ntsd *pntsd, struct smb_ntsd *ppntsd,
881-
int addition_info, __u32 *secdesclen,
892+
int ppntsd_size, int addition_info, __u32 *secdesclen,
882893
struct smb_fattr *fattr)
883894
{
884895
int rc = 0;
@@ -938,15 +949,25 @@ int build_sec_desc(struct user_namespace *user_ns,
938949

939950
if (!ppntsd) {
940951
set_mode_dacl(user_ns, dacl_ptr, fattr);
941-
} else if (!ppntsd->dacloffset) {
942-
goto out;
943952
} else {
944953
struct smb_acl *ppdacl_ptr;
954+
unsigned int dacl_offset = le32_to_cpu(ppntsd->dacloffset);
955+
int ppdacl_size, ntacl_size = ppntsd_size - dacl_offset;
956+
957+
if (!dacl_offset ||
958+
(dacl_offset + sizeof(struct smb_acl) > ppntsd_size))
959+
goto out;
960+
961+
ppdacl_ptr = (struct smb_acl *)((char *)ppntsd + dacl_offset);
962+
ppdacl_size = le16_to_cpu(ppdacl_ptr->size);
963+
if (ppdacl_size > ntacl_size ||
964+
ppdacl_size < sizeof(struct smb_acl))
965+
goto out;
945966

946-
ppdacl_ptr = (struct smb_acl *)((char *)ppntsd +
947-
le32_to_cpu(ppntsd->dacloffset));
948967
set_ntacl_dacl(user_ns, dacl_ptr, ppdacl_ptr,
949-
nowner_sid_ptr, ngroup_sid_ptr, fattr);
968+
ntacl_size - sizeof(struct smb_acl),
969+
nowner_sid_ptr, ngroup_sid_ptr,
970+
fattr);
950971
}
951972
pntsd->dacloffset = cpu_to_le32(offset);
952973
offset += le16_to_cpu(dacl_ptr->size);
@@ -980,24 +1001,31 @@ int smb_inherit_dacl(struct ksmbd_conn *conn,
9801001
struct smb_sid owner_sid, group_sid;
9811002
struct dentry *parent = path->dentry->d_parent;
9821003
struct user_namespace *user_ns = mnt_user_ns(path->mnt);
983-
int inherited_flags = 0, flags = 0, i, ace_cnt = 0, nt_size = 0;
984-
int rc = 0, num_aces, dacloffset, pntsd_type, acl_len;
1004+
int inherited_flags = 0, flags = 0, i, ace_cnt = 0, nt_size = 0, pdacl_size;
1005+
int rc = 0, num_aces, dacloffset, pntsd_type, pntsd_size, acl_len, aces_size;
9851006
char *aces_base;
9861007
bool is_dir = S_ISDIR(d_inode(path->dentry)->i_mode);
9871008

988-
acl_len = ksmbd_vfs_get_sd_xattr(conn, user_ns,
989-
parent, &parent_pntsd);
990-
if (acl_len <= 0)
1009+
pntsd_size = ksmbd_vfs_get_sd_xattr(conn, user_ns,
1010+
parent, &parent_pntsd);
1011+
if (pntsd_size <= 0)
9911012
return -ENOENT;
9921013
dacloffset = le32_to_cpu(parent_pntsd->dacloffset);
993-
if (!dacloffset) {
1014+
if (!dacloffset || (dacloffset + sizeof(struct smb_acl) > pntsd_size)) {
9941015
rc = -EINVAL;
9951016
goto free_parent_pntsd;
9961017
}
9971018

9981019
parent_pdacl = (struct smb_acl *)((char *)parent_pntsd + dacloffset);
1020+
acl_len = pntsd_size - dacloffset;
9991021
num_aces = le32_to_cpu(parent_pdacl->num_aces);
10001022
pntsd_type = le16_to_cpu(parent_pntsd->type);
1023+
pdacl_size = le16_to_cpu(parent_pdacl->size);
1024+
1025+
if (pdacl_size > acl_len || pdacl_size < sizeof(struct smb_acl)) {
1026+
rc = -EINVAL;
1027+
goto free_parent_pntsd;
1028+
}
10011029

10021030
aces_base = kmalloc(sizeof(struct smb_ace) * num_aces * 2, GFP_KERNEL);
10031031
if (!aces_base) {
@@ -1008,11 +1036,23 @@ int smb_inherit_dacl(struct ksmbd_conn *conn,
10081036
aces = (struct smb_ace *)aces_base;
10091037
parent_aces = (struct smb_ace *)((char *)parent_pdacl +
10101038
sizeof(struct smb_acl));
1039+
aces_size = acl_len - sizeof(struct smb_acl);
10111040

10121041
if (pntsd_type & DACL_AUTO_INHERITED)
10131042
inherited_flags = INHERITED_ACE;
10141043

10151044
for (i = 0; i < num_aces; i++) {
1045+
int pace_size;
1046+
1047+
if (offsetof(struct smb_ace, access_req) > aces_size)
1048+
break;
1049+
1050+
pace_size = le16_to_cpu(parent_aces->size);
1051+
if (pace_size > aces_size)
1052+
break;
1053+
1054+
aces_size -= pace_size;
1055+
10161056
flags = parent_aces->flags;
10171057
if (!smb_inherit_flags(flags, is_dir))
10181058
goto pass;
@@ -1057,8 +1097,7 @@ int smb_inherit_dacl(struct ksmbd_conn *conn,
10571097
aces = (struct smb_ace *)((char *)aces + le16_to_cpu(aces->size));
10581098
ace_cnt++;
10591099
pass:
1060-
parent_aces =
1061-
(struct smb_ace *)((char *)parent_aces + le16_to_cpu(parent_aces->size));
1100+
parent_aces = (struct smb_ace *)((char *)parent_aces + pace_size);
10621101
}
10631102

10641103
if (nt_size > 0) {
@@ -1153,7 +1192,7 @@ int smb_check_perm_dacl(struct ksmbd_conn *conn, struct path *path,
11531192
struct smb_ntsd *pntsd = NULL;
11541193
struct smb_acl *pdacl;
11551194
struct posix_acl *posix_acls;
1156-
int rc = 0, acl_size;
1195+
int rc = 0, pntsd_size, acl_size, aces_size, pdacl_size, dacl_offset;
11571196
struct smb_sid sid;
11581197
int granted = le32_to_cpu(*pdaccess & ~FILE_MAXIMAL_ACCESS_LE);
11591198
struct smb_ace *ace;
@@ -1162,49 +1201,50 @@ int smb_check_perm_dacl(struct ksmbd_conn *conn, struct path *path,
11621201
struct smb_ace *others_ace = NULL;
11631202
struct posix_acl_entry *pa_entry;
11641203
unsigned int sid_type = SIDOWNER;
1165-
char *end_of_acl;
1204+
unsigned short ace_size;
11661205

11671206
ksmbd_debug(SMB, "check permission using windows acl\n");
1168-
acl_size = ksmbd_vfs_get_sd_xattr(conn, user_ns,
1169-
path->dentry, &pntsd);
1170-
if (acl_size <= 0 || !pntsd || !pntsd->dacloffset) {
1171-
kfree(pntsd);
1172-
return 0;
1173-
}
1207+
pntsd_size = ksmbd_vfs_get_sd_xattr(conn, user_ns,
1208+
path->dentry, &pntsd);
1209+
if (pntsd_size <= 0 || !pntsd)
1210+
goto err_out;
1211+
1212+
dacl_offset = le32_to_cpu(pntsd->dacloffset);
1213+
if (!dacl_offset ||
1214+
(dacl_offset + sizeof(struct smb_acl) > pntsd_size))
1215+
goto err_out;
11741216

11751217
pdacl = (struct smb_acl *)((char *)pntsd + le32_to_cpu(pntsd->dacloffset));
1176-
end_of_acl = ((char *)pntsd) + acl_size;
1177-
if (end_of_acl <= (char *)pdacl) {
1178-
kfree(pntsd);
1179-
return 0;
1180-
}
1218+
acl_size = pntsd_size - dacl_offset;
1219+
pdacl_size = le16_to_cpu(pdacl->size);
11811220

1182-
if (end_of_acl < (char *)pdacl + le16_to_cpu(pdacl->size) ||
1183-
le16_to_cpu(pdacl->size) < sizeof(struct smb_acl)) {
1184-
kfree(pntsd);
1185-
return 0;
1186-
}
1221+
if (pdacl_size > acl_size || pdacl_size < sizeof(struct smb_acl))
1222+
goto err_out;
11871223

11881224
if (!pdacl->num_aces) {
1189-
if (!(le16_to_cpu(pdacl->size) - sizeof(struct smb_acl)) &&
1225+
if (!(pdacl_size - sizeof(struct smb_acl)) &&
11901226
*pdaccess & ~(FILE_READ_CONTROL_LE | FILE_WRITE_DAC_LE)) {
11911227
rc = -EACCES;
11921228
goto err_out;
11931229
}
1194-
kfree(pntsd);
1195-
return 0;
1230+
goto err_out;
11961231
}
11971232

11981233
if (*pdaccess & FILE_MAXIMAL_ACCESS_LE) {
11991234
granted = READ_CONTROL | WRITE_DAC | FILE_READ_ATTRIBUTES |
12001235
DELETE;
12011236

12021237
ace = (struct smb_ace *)((char *)pdacl + sizeof(struct smb_acl));
1238+
aces_size = acl_size - sizeof(struct smb_acl);
12031239
for (i = 0; i < le32_to_cpu(pdacl->num_aces); i++) {
1240+
if (offsetof(struct smb_ace, access_req) > aces_size)
1241+
break;
1242+
ace_size = le16_to_cpu(ace->size);
1243+
if (ace_size > aces_size)
1244+
break;
1245+
aces_size -= ace_size;
12041246
granted |= le32_to_cpu(ace->access_req);
12051247
ace = (struct smb_ace *)((char *)ace + le16_to_cpu(ace->size));
1206-
if (end_of_acl < (char *)ace)
1207-
goto err_out;
12081248
}
12091249

12101250
if (!pdacl->num_aces)
@@ -1216,7 +1256,15 @@ int smb_check_perm_dacl(struct ksmbd_conn *conn, struct path *path,
12161256
id_to_sid(uid, sid_type, &sid);
12171257

12181258
ace = (struct smb_ace *)((char *)pdacl + sizeof(struct smb_acl));
1259+
aces_size = acl_size - sizeof(struct smb_acl);
12191260
for (i = 0; i < le32_to_cpu(pdacl->num_aces); i++) {
1261+
if (offsetof(struct smb_ace, access_req) > aces_size)
1262+
break;
1263+
ace_size = le16_to_cpu(ace->size);
1264+
if (ace_size > aces_size)
1265+
break;
1266+
aces_size -= ace_size;
1267+
12201268
if (!compare_sids(&sid, &ace->sid) ||
12211269
!compare_sids(&sid_unix_NFS_mode, &ace->sid)) {
12221270
found = 1;
@@ -1226,8 +1274,6 @@ int smb_check_perm_dacl(struct ksmbd_conn *conn, struct path *path,
12261274
others_ace = ace;
12271275

12281276
ace = (struct smb_ace *)((char *)ace + le16_to_cpu(ace->size));
1229-
if (end_of_acl < (char *)ace)
1230-
goto err_out;
12311277
}
12321278

12331279
if (*pdaccess & FILE_MAXIMAL_ACCESS_LE && found) {

fs/ksmbd/smbacl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ struct posix_acl_state {
193193
int parse_sec_desc(struct user_namespace *user_ns, struct smb_ntsd *pntsd,
194194
int acl_len, struct smb_fattr *fattr);
195195
int build_sec_desc(struct user_namespace *user_ns, struct smb_ntsd *pntsd,
196-
struct smb_ntsd *ppntsd, int addition_info,
196+
struct smb_ntsd *ppntsd, int ppntsd_size, int addition_info,
197197
__u32 *secdesclen, struct smb_fattr *fattr);
198198
int init_acl_state(struct posix_acl_state *state, int cnt);
199199
void free_acl_state(struct posix_acl_state *state);

fs/ksmbd/vfs.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1539,6 +1539,11 @@ int ksmbd_vfs_get_sd_xattr(struct ksmbd_conn *conn,
15391539
}
15401540

15411541
*pntsd = acl.sd_buf;
1542+
if (acl.sd_size < sizeof(struct smb_ntsd)) {
1543+
pr_err("sd size is invalid\n");
1544+
goto out_free;
1545+
}
1546+
15421547
(*pntsd)->osidoffset = cpu_to_le32(le32_to_cpu((*pntsd)->osidoffset) -
15431548
NDR_NTSD_OFFSETOF);
15441549
(*pntsd)->gsidoffset = cpu_to_le32(le32_to_cpu((*pntsd)->gsidoffset) -

0 commit comments

Comments
 (0)