Skip to content

Commit ca619c1

Browse files
pcacjrsfX-bot
authored andcommitted
smb: client: fix potential OOBs in smb2_parse_contexts()
[ Upstream commit af1689a9b7701d9907dfc84d2a4b57c4bc907144 ] Validate offsets and lengths before dereferencing create contexts in smb2_parse_contexts(). This fixes following oops when accessing invalid create contexts from server: BUG: unable to handle page fault for address: ffff8881178d8cc3 #PF: supervisor read access in kernel mode #PF: error_code(0x0000) - not-present page PGD 4a01067 P4D 4a01067 PUD 0 Oops: 0000 [#1] PREEMPT SMP NOPTI CPU: 3 PID: 1736 Comm: mount.cifs Not tainted 6.7.0-rc4 #1 Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS rel-1.16.2-3-gd478f380-rebuilt.opensuse.org 04/01/2014 RIP: 0010:smb2_parse_contexts+0xa0/0x3a0 [cifs] Code: f8 10 75 13 48 b8 93 ad 25 50 9c b4 11 e7 49 39 06 0f 84 d2 00 00 00 8b 45 00 85 c0 74 61 41 29 c5 48 01 c5 41 83 fd 0f 76 55 <0f> b7 7d 04 0f b7 45 06 4c 8d 74 3d 00 66 83 f8 04 75 bc ba 04 00 RSP: 0018:ffffc900007939e0 EFLAGS: 00010216 RAX: ffffc90000793c78 RBX: ffff8880180cc000 RCX: ffffc90000793c90 RDX: ffffc90000793cc0 RSI: ffff8880178d8cc0 RDI: ffff8880180cc000 RBP: ffff8881178d8cbf R08: ffffc90000793c22 R09: 0000000000000000 R10: ffff8880180cc000 R11: 0000000000000024 R12: 0000000000000000 R13: 0000000000000020 R14: 0000000000000000 R15: ffffc90000793c22 FS: 00007f873753cbc0(0000) GS:ffff88806bc00000(0000) knlGS:0000000000000000 CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 CR2: ffff8881178d8cc3 CR3: 00000000181ca000 CR4: 0000000000750ef0 PKRU: 55555554 Call Trace: <TASK> ? __die+0x23/0x70 ? page_fault_oops+0x181/0x480 ? search_module_extables+0x19/0x60 ? srso_alias_return_thunk+0x5/0xfbef5 ? exc_page_fault+0x1b6/0x1c0 ? asm_exc_page_fault+0x26/0x30 ? smb2_parse_contexts+0xa0/0x3a0 [cifs] SMB2_open+0x38d/0x5f0 [cifs] ? smb2_is_path_accessible+0x138/0x260 [cifs] smb2_is_path_accessible+0x138/0x260 [cifs] cifs_is_path_remote+0x8d/0x230 [cifs] cifs_mount+0x7e/0x350 [cifs] cifs_smb3_do_mount+0x128/0x780 [cifs] smb3_get_tree+0xd9/0x290 [cifs] vfs_get_tree+0x2c/0x100 ? capable+0x37/0x70 path_mount+0x2d7/0xb80 ? srso_alias_return_thunk+0x5/0xfbef5 ? _raw_spin_unlock_irqrestore+0x44/0x60 __x64_sys_mount+0x11a/0x150 do_syscall_64+0x47/0xf0 entry_SYSCALL_64_after_hwframe+0x6f/0x77 RIP: 0033:0x7f8737657b1e Reported-by: Robert Morris <[email protected]> Cc: [email protected] Signed-off-by: Paulo Alcantara (SUSE) <[email protected]> Signed-off-by: Steve French <[email protected]> [Guru: Removed changes to cached_dir.c and checking return value of smb2_parse_contexts in smb2ops.c] Signed-off-by: Guruswamy Basavaiah <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 059192e commit ca619c1

File tree

3 files changed

+66
-43
lines changed

3 files changed

+66
-43
lines changed

fs/cifs/smb2ops.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -818,10 +818,12 @@ int open_shroot(unsigned int xid, struct cifs_tcon *tcon,
818818
if (o_rsp->OplockLevel == SMB2_OPLOCK_LEVEL_LEASE) {
819819
kref_get(&tcon->crfid.refcount);
820820
tcon->crfid.has_lease = true;
821-
smb2_parse_contexts(server, o_rsp,
821+
rc = smb2_parse_contexts(server, rsp_iov,
822822
&oparms.fid->epoch,
823823
oparms.fid->lease_key, &oplock,
824824
NULL, NULL);
825+
if (rc)
826+
goto oshr_exit;
825827
} else
826828
goto oshr_exit;
827829

fs/cifs/smb2pdu.c

Lines changed: 56 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1987,17 +1987,18 @@ parse_posix_ctxt(struct create_context *cc, struct smb2_file_all_info *info,
19871987
posix->nlink, posix->mode, posix->reparse_tag);
19881988
}
19891989

1990-
void
1991-
smb2_parse_contexts(struct TCP_Server_Info *server,
1992-
struct smb2_create_rsp *rsp,
1993-
unsigned int *epoch, char *lease_key, __u8 *oplock,
1994-
struct smb2_file_all_info *buf,
1995-
struct create_posix_rsp *posix)
1990+
int smb2_parse_contexts(struct TCP_Server_Info *server,
1991+
struct kvec *rsp_iov,
1992+
unsigned int *epoch,
1993+
char *lease_key, __u8 *oplock,
1994+
struct smb2_file_all_info *buf,
1995+
struct create_posix_rsp *posix)
19961996
{
1997-
char *data_offset;
1997+
struct smb2_create_rsp *rsp = rsp_iov->iov_base;
19981998
struct create_context *cc;
1999-
unsigned int next;
2000-
unsigned int remaining;
1999+
size_t rem, off, len;
2000+
size_t doff, dlen;
2001+
size_t noff, nlen;
20012002
char *name;
20022003
static const char smb3_create_tag_posix[] = {
20032004
0x93, 0xAD, 0x25, 0x50, 0x9C,
@@ -2006,45 +2007,63 @@ smb2_parse_contexts(struct TCP_Server_Info *server,
20062007
};
20072008

20082009
*oplock = 0;
2009-
data_offset = (char *)rsp + le32_to_cpu(rsp->CreateContextsOffset);
2010-
remaining = le32_to_cpu(rsp->CreateContextsLength);
2011-
cc = (struct create_context *)data_offset;
2010+
2011+
off = le32_to_cpu(rsp->CreateContextsOffset);
2012+
rem = le32_to_cpu(rsp->CreateContextsLength);
2013+
if (check_add_overflow(off, rem, &len) || len > rsp_iov->iov_len)
2014+
return -EINVAL;
2015+
cc = (struct create_context *)((u8 *)rsp + off);
20122016

20132017
/* Initialize inode number to 0 in case no valid data in qfid context */
20142018
if (buf)
20152019
buf->IndexNumber = 0;
20162020

2017-
while (remaining >= sizeof(struct create_context)) {
2018-
name = le16_to_cpu(cc->NameOffset) + (char *)cc;
2019-
if (le16_to_cpu(cc->NameLength) == 4 &&
2020-
strncmp(name, SMB2_CREATE_REQUEST_LEASE, 4) == 0)
2021-
*oplock = server->ops->parse_lease_buf(cc, epoch,
2022-
lease_key);
2023-
else if (buf && (le16_to_cpu(cc->NameLength) == 4) &&
2024-
strncmp(name, SMB2_CREATE_QUERY_ON_DISK_ID, 4) == 0)
2025-
parse_query_id_ctxt(cc, buf);
2026-
else if ((le16_to_cpu(cc->NameLength) == 16)) {
2027-
if (posix &&
2028-
memcmp(name, smb3_create_tag_posix, 16) == 0)
2021+
while (rem >= sizeof(*cc)) {
2022+
doff = le16_to_cpu(cc->DataOffset);
2023+
dlen = le32_to_cpu(cc->DataLength);
2024+
if (check_add_overflow(doff, dlen, &len) || len > rem)
2025+
return -EINVAL;
2026+
2027+
noff = le16_to_cpu(cc->NameOffset);
2028+
nlen = le16_to_cpu(cc->NameLength);
2029+
if (noff + nlen >= doff)
2030+
return -EINVAL;
2031+
2032+
name = (char *)cc + noff;
2033+
switch (nlen) {
2034+
case 4:
2035+
if (!strncmp(name, SMB2_CREATE_REQUEST_LEASE, 4)) {
2036+
*oplock = server->ops->parse_lease_buf(cc, epoch,
2037+
lease_key);
2038+
} else if (buf &&
2039+
!strncmp(name, SMB2_CREATE_QUERY_ON_DISK_ID, 4)) {
2040+
parse_query_id_ctxt(cc, buf);
2041+
}
2042+
break;
2043+
case 16:
2044+
if (posix && !memcmp(name, smb3_create_tag_posix, 16))
20292045
parse_posix_ctxt(cc, buf, posix);
2046+
break;
2047+
default:
2048+
cifs_dbg(FYI, "%s: unhandled context (nlen=%zu dlen=%zu)\n",
2049+
__func__, nlen, dlen);
2050+
if (IS_ENABLED(CONFIG_CIFS_DEBUG2))
2051+
cifs_dump_mem("context data: ", cc, dlen);
2052+
break;
20302053
}
2031-
/* else {
2032-
cifs_dbg(FYI, "Context not matched with len %d\n",
2033-
le16_to_cpu(cc->NameLength));
2034-
cifs_dump_mem("Cctxt name: ", name, 4);
2035-
} */
2036-
2037-
next = le32_to_cpu(cc->Next);
2038-
if (!next)
2054+
2055+
off = le32_to_cpu(cc->Next);
2056+
if (!off)
20392057
break;
2040-
remaining -= next;
2041-
cc = (struct create_context *)((char *)cc + next);
2058+
if (check_sub_overflow(rem, off, &rem))
2059+
return -EINVAL;
2060+
cc = (struct create_context *)((u8 *)cc + off);
20422061
}
20432062

20442063
if (rsp->OplockLevel != SMB2_OPLOCK_LEVEL_LEASE)
20452064
*oplock = rsp->OplockLevel;
20462065

2047-
return;
2066+
return 0;
20482067
}
20492068

20502069
static int
@@ -2911,8 +2930,8 @@ SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
29112930
}
29122931

29132932

2914-
smb2_parse_contexts(server, rsp, &oparms->fid->epoch,
2915-
oparms->fid->lease_key, oplock, buf, posix);
2933+
rc = smb2_parse_contexts(server, &rsp_iov, &oparms->fid->epoch,
2934+
oparms->fid->lease_key, oplock, buf, posix);
29162935
creat_exit:
29172936
SMB2_open_free(&rqst);
29182937
free_rsp_buf(resp_buftype, rsp);

fs/cifs/smb2proto.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -270,11 +270,13 @@ extern int smb3_validate_negotiate(const unsigned int, struct cifs_tcon *);
270270

271271
extern enum securityEnum smb2_select_sectype(struct TCP_Server_Info *,
272272
enum securityEnum);
273-
extern void smb2_parse_contexts(struct TCP_Server_Info *server,
274-
struct smb2_create_rsp *rsp,
275-
unsigned int *epoch, char *lease_key,
276-
__u8 *oplock, struct smb2_file_all_info *buf,
277-
struct create_posix_rsp *posix);
273+
int smb2_parse_contexts(struct TCP_Server_Info *server,
274+
struct kvec *rsp_iov,
275+
unsigned int *epoch,
276+
char *lease_key, __u8 *oplock,
277+
struct smb2_file_all_info *buf,
278+
struct create_posix_rsp *posix);
279+
278280
extern int smb3_encryption_required(const struct cifs_tcon *tcon);
279281
extern int smb2_validate_iov(unsigned int offset, unsigned int buffer_length,
280282
struct kvec *iov, unsigned int min_buf_size);

0 commit comments

Comments
 (0)