Skip to content

Commit 8f77150

Browse files
hcleesmfrench
authored andcommitted
ksmbd: add buffer validation for SMB2_CREATE_CONTEXT
Add buffer validation for SMB2_CREATE_CONTEXT. Cc: Ronnie Sahlberg <[email protected]> Reviewed-by: Ralph Boehme <[email protected]> Signed-off-by: Hyunchul Lee <[email protected]> Signed-off-by: Namjae Jeon <[email protected]> Signed-off-by: Steve French <[email protected]>
1 parent 442ff9e commit 8f77150

File tree

3 files changed

+74
-13
lines changed

3 files changed

+74
-13
lines changed

fs/ksmbd/oplock.c

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1451,26 +1451,47 @@ struct lease_ctx_info *parse_lease_state(void *open_req)
14511451
*/
14521452
struct create_context *smb2_find_context_vals(void *open_req, const char *tag)
14531453
{
1454-
char *data_offset;
14551454
struct create_context *cc;
14561455
unsigned int next = 0;
14571456
char *name;
14581457
struct smb2_create_req *req = (struct smb2_create_req *)open_req;
1458+
unsigned int remain_len, name_off, name_len, value_off, value_len,
1459+
cc_len;
14591460

1460-
data_offset = (char *)req + 4 + le32_to_cpu(req->CreateContextsOffset);
1461-
cc = (struct create_context *)data_offset;
1461+
/*
1462+
* CreateContextsOffset and CreateContextsLength are guaranteed to
1463+
* be valid because of ksmbd_smb2_check_message().
1464+
*/
1465+
cc = (struct create_context *)((char *)req + 4 +
1466+
le32_to_cpu(req->CreateContextsOffset));
1467+
remain_len = le32_to_cpu(req->CreateContextsLength);
14621468
do {
1463-
int val;
1464-
14651469
cc = (struct create_context *)((char *)cc + next);
1466-
name = le16_to_cpu(cc->NameOffset) + (char *)cc;
1467-
val = le16_to_cpu(cc->NameLength);
1468-
if (val < 4)
1470+
if (remain_len < offsetof(struct create_context, Buffer))
14691471
return ERR_PTR(-EINVAL);
14701472

1471-
if (memcmp(name, tag, val) == 0)
1472-
return cc;
14731473
next = le32_to_cpu(cc->Next);
1474+
name_off = le16_to_cpu(cc->NameOffset);
1475+
name_len = le16_to_cpu(cc->NameLength);
1476+
value_off = le16_to_cpu(cc->DataOffset);
1477+
value_len = le32_to_cpu(cc->DataLength);
1478+
cc_len = next ? next : remain_len;
1479+
1480+
if ((next & 0x7) != 0 ||
1481+
next > remain_len ||
1482+
name_off != offsetof(struct create_context, Buffer) ||
1483+
name_len < 4 ||
1484+
name_off + name_len > cc_len ||
1485+
(value_off & 0x7) != 0 ||
1486+
(value_off && (value_off < name_off + name_len)) ||
1487+
((u64)value_off + value_len > cc_len))
1488+
return ERR_PTR(-EINVAL);
1489+
1490+
name = (char *)cc + name_off;
1491+
if (memcmp(name, tag, name_len) == 0)
1492+
return cc;
1493+
1494+
remain_len -= next;
14741495
} while (next != 0);
14751496

14761497
return NULL;

fs/ksmbd/smb2pdu.c

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2427,6 +2427,10 @@ static int smb2_create_sd_buffer(struct ksmbd_work *work,
24272427
ksmbd_debug(SMB,
24282428
"Set ACLs using SMB2_CREATE_SD_BUFFER context\n");
24292429
sd_buf = (struct create_sd_buf_req *)context;
2430+
if (le16_to_cpu(context->DataOffset) +
2431+
le32_to_cpu(context->DataLength) <
2432+
sizeof(struct create_sd_buf_req))
2433+
return -EINVAL;
24302434
return set_info_sec(work->conn, work->tcon, path, &sd_buf->ntsd,
24312435
le32_to_cpu(sd_buf->ccontext.DataLength), true);
24322436
}
@@ -2621,6 +2625,12 @@ int smb2_open(struct ksmbd_work *work)
26212625
goto err_out1;
26222626
} else if (context) {
26232627
ea_buf = (struct create_ea_buf_req *)context;
2628+
if (le16_to_cpu(context->DataOffset) +
2629+
le32_to_cpu(context->DataLength) <
2630+
sizeof(struct create_ea_buf_req)) {
2631+
rc = -EINVAL;
2632+
goto err_out1;
2633+
}
26242634
if (req->CreateOptions & FILE_NO_EA_KNOWLEDGE_LE) {
26252635
rsp->hdr.Status = STATUS_ACCESS_DENIED;
26262636
rc = -EACCES;
@@ -2659,6 +2669,12 @@ int smb2_open(struct ksmbd_work *work)
26592669
} else if (context) {
26602670
struct create_posix *posix =
26612671
(struct create_posix *)context;
2672+
if (le16_to_cpu(context->DataOffset) +
2673+
le32_to_cpu(context->DataLength) <
2674+
sizeof(struct create_posix)) {
2675+
rc = -EINVAL;
2676+
goto err_out1;
2677+
}
26622678
ksmbd_debug(SMB, "get posix context\n");
26632679

26642680
posix_mode = le32_to_cpu(posix->Mode);
@@ -3049,9 +3065,16 @@ int smb2_open(struct ksmbd_work *work)
30493065
rc = PTR_ERR(az_req);
30503066
goto err_out;
30513067
} else if (az_req) {
3052-
loff_t alloc_size = le64_to_cpu(az_req->AllocationSize);
3068+
loff_t alloc_size;
30533069
int err;
30543070

3071+
if (le16_to_cpu(az_req->ccontext.DataOffset) +
3072+
le32_to_cpu(az_req->ccontext.DataLength) <
3073+
sizeof(struct create_alloc_size_req)) {
3074+
rc = -EINVAL;
3075+
goto err_out;
3076+
}
3077+
alloc_size = le64_to_cpu(az_req->AllocationSize);
30553078
ksmbd_debug(SMB,
30563079
"request smb2 create allocate size : %llu\n",
30573080
alloc_size);

fs/ksmbd/smbacl.c

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ static void parse_dacl(struct user_namespace *user_ns,
380380
{
381381
int i, ret;
382382
int num_aces = 0;
383-
int acl_size;
383+
unsigned int acl_size;
384384
char *acl_base;
385385
struct smb_ace **ppace;
386386
struct posix_acl_entry *cf_pace, *cf_pdace;
@@ -392,7 +392,7 @@ static void parse_dacl(struct user_namespace *user_ns,
392392
return;
393393

394394
/* validate that we do not go past end of acl */
395-
if (end_of_acl <= (char *)pdacl ||
395+
if (end_of_acl < (char *)pdacl + sizeof(struct smb_acl) ||
396396
end_of_acl < (char *)pdacl + le16_to_cpu(pdacl->size)) {
397397
pr_err("ACL too small to parse DACL\n");
398398
return;
@@ -431,8 +431,22 @@ static void parse_dacl(struct user_namespace *user_ns,
431431
* user/group/other have no permissions
432432
*/
433433
for (i = 0; i < num_aces; ++i) {
434+
if (end_of_acl - acl_base < acl_size)
435+
break;
436+
434437
ppace[i] = (struct smb_ace *)(acl_base + acl_size);
435438
acl_base = (char *)ppace[i];
439+
acl_size = offsetof(struct smb_ace, sid) +
440+
offsetof(struct smb_sid, sub_auth);
441+
442+
if (end_of_acl - acl_base < acl_size ||
443+
ppace[i]->sid.num_subauth > SID_MAX_SUB_AUTHORITIES ||
444+
(end_of_acl - acl_base <
445+
acl_size + sizeof(__le32) * ppace[i]->sid.num_subauth) ||
446+
(le16_to_cpu(ppace[i]->size) <
447+
acl_size + sizeof(__le32) * ppace[i]->sid.num_subauth))
448+
break;
449+
436450
acl_size = le16_to_cpu(ppace[i]->size);
437451
ppace[i]->access_req =
438452
smb_map_generic_desired_access(ppace[i]->access_req);
@@ -807,6 +821,9 @@ int parse_sec_desc(struct user_namespace *user_ns, struct smb_ntsd *pntsd,
807821
if (!pntsd)
808822
return -EIO;
809823

824+
if (acl_len < sizeof(struct smb_ntsd))
825+
return -EINVAL;
826+
810827
owner_sid_ptr = (struct smb_sid *)((char *)pntsd +
811828
le32_to_cpu(pntsd->osidoffset));
812829
group_sid_ptr = (struct smb_sid *)((char *)pntsd +

0 commit comments

Comments
 (0)