Skip to content

Commit af1689a

Browse files
Paulo Alcantarasmfrench
authored andcommitted
smb: client: fix potential OOBs in smb2_parse_contexts()
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]>
1 parent eec04ea commit af1689a

File tree

3 files changed

+75
-47
lines changed

3 files changed

+75
-47
lines changed

fs/smb/client/cached_dir.c

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -291,16 +291,23 @@ int open_cached_dir(unsigned int xid, struct cifs_tcon *tcon,
291291
oparms.fid->mid = le64_to_cpu(o_rsp->hdr.MessageId);
292292
#endif /* CIFS_DEBUG2 */
293293

294-
rc = -EINVAL;
294+
295295
if (o_rsp->OplockLevel != SMB2_OPLOCK_LEVEL_LEASE) {
296+
spin_unlock(&cfids->cfid_list_lock);
297+
rc = -EINVAL;
298+
goto oshr_free;
299+
}
300+
301+
rc = smb2_parse_contexts(server, rsp_iov,
302+
&oparms.fid->epoch,
303+
oparms.fid->lease_key,
304+
&oplock, NULL, NULL);
305+
if (rc) {
296306
spin_unlock(&cfids->cfid_list_lock);
297307
goto oshr_free;
298308
}
299309

300-
smb2_parse_contexts(server, o_rsp,
301-
&oparms.fid->epoch,
302-
oparms.fid->lease_key, &oplock,
303-
NULL, NULL);
310+
rc = -EINVAL;
304311
if (!(oplock & SMB2_LEASE_READ_CACHING_HE)) {
305312
spin_unlock(&cfids->cfid_list_lock);
306313
goto oshr_free;

fs/smb/client/smb2pdu.c

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

2239-
void
2240-
smb2_parse_contexts(struct TCP_Server_Info *server,
2241-
struct smb2_create_rsp *rsp,
2242-
unsigned int *epoch, char *lease_key, __u8 *oplock,
2243-
struct smb2_file_all_info *buf,
2244-
struct create_posix_rsp *posix)
2239+
int smb2_parse_contexts(struct TCP_Server_Info *server,
2240+
struct kvec *rsp_iov,
2241+
unsigned int *epoch,
2242+
char *lease_key, __u8 *oplock,
2243+
struct smb2_file_all_info *buf,
2244+
struct create_posix_rsp *posix)
22452245
{
2246-
char *data_offset;
2246+
struct smb2_create_rsp *rsp = rsp_iov->iov_base;
22472247
struct create_context *cc;
2248-
unsigned int next;
2249-
unsigned int remaining;
2248+
size_t rem, off, len;
2249+
size_t doff, dlen;
2250+
size_t noff, nlen;
22502251
char *name;
22512252
static const char smb3_create_tag_posix[] = {
22522253
0x93, 0xAD, 0x25, 0x50, 0x9C,
@@ -2255,45 +2256,63 @@ smb2_parse_contexts(struct TCP_Server_Info *server,
22552256
};
22562257

22572258
*oplock = 0;
2258-
data_offset = (char *)rsp + le32_to_cpu(rsp->CreateContextsOffset);
2259-
remaining = le32_to_cpu(rsp->CreateContextsLength);
2260-
cc = (struct create_context *)data_offset;
2259+
2260+
off = le32_to_cpu(rsp->CreateContextsOffset);
2261+
rem = le32_to_cpu(rsp->CreateContextsLength);
2262+
if (check_add_overflow(off, rem, &len) || len > rsp_iov->iov_len)
2263+
return -EINVAL;
2264+
cc = (struct create_context *)((u8 *)rsp + off);
22612265

22622266
/* Initialize inode number to 0 in case no valid data in qfid context */
22632267
if (buf)
22642268
buf->IndexNumber = 0;
22652269

2266-
while (remaining >= sizeof(struct create_context)) {
2267-
name = le16_to_cpu(cc->NameOffset) + (char *)cc;
2268-
if (le16_to_cpu(cc->NameLength) == 4 &&
2269-
strncmp(name, SMB2_CREATE_REQUEST_LEASE, 4) == 0)
2270-
*oplock = server->ops->parse_lease_buf(cc, epoch,
2271-
lease_key);
2272-
else if (buf && (le16_to_cpu(cc->NameLength) == 4) &&
2273-
strncmp(name, SMB2_CREATE_QUERY_ON_DISK_ID, 4) == 0)
2274-
parse_query_id_ctxt(cc, buf);
2275-
else if ((le16_to_cpu(cc->NameLength) == 16)) {
2276-
if (posix &&
2277-
memcmp(name, smb3_create_tag_posix, 16) == 0)
2270+
while (rem >= sizeof(*cc)) {
2271+
doff = le16_to_cpu(cc->DataOffset);
2272+
dlen = le32_to_cpu(cc->DataLength);
2273+
if (check_add_overflow(doff, dlen, &len) || len > rem)
2274+
return -EINVAL;
2275+
2276+
noff = le16_to_cpu(cc->NameOffset);
2277+
nlen = le16_to_cpu(cc->NameLength);
2278+
if (noff + nlen >= doff)
2279+
return -EINVAL;
2280+
2281+
name = (char *)cc + noff;
2282+
switch (nlen) {
2283+
case 4:
2284+
if (!strncmp(name, SMB2_CREATE_REQUEST_LEASE, 4)) {
2285+
*oplock = server->ops->parse_lease_buf(cc, epoch,
2286+
lease_key);
2287+
} else if (buf &&
2288+
!strncmp(name, SMB2_CREATE_QUERY_ON_DISK_ID, 4)) {
2289+
parse_query_id_ctxt(cc, buf);
2290+
}
2291+
break;
2292+
case 16:
2293+
if (posix && !memcmp(name, smb3_create_tag_posix, 16))
22782294
parse_posix_ctxt(cc, buf, posix);
2295+
break;
2296+
default:
2297+
cifs_dbg(FYI, "%s: unhandled context (nlen=%zu dlen=%zu)\n",
2298+
__func__, nlen, dlen);
2299+
if (IS_ENABLED(CONFIG_CIFS_DEBUG2))
2300+
cifs_dump_mem("context data: ", cc, dlen);
2301+
break;
22792302
}
2280-
/* else {
2281-
cifs_dbg(FYI, "Context not matched with len %d\n",
2282-
le16_to_cpu(cc->NameLength));
2283-
cifs_dump_mem("Cctxt name: ", name, 4);
2284-
} */
2285-
2286-
next = le32_to_cpu(cc->Next);
2287-
if (!next)
2303+
2304+
off = le32_to_cpu(cc->Next);
2305+
if (!off)
22882306
break;
2289-
remaining -= next;
2290-
cc = (struct create_context *)((char *)cc + next);
2307+
if (check_sub_overflow(rem, off, &rem))
2308+
return -EINVAL;
2309+
cc = (struct create_context *)((u8 *)cc + off);
22912310
}
22922311

22932312
if (rsp->OplockLevel != SMB2_OPLOCK_LEVEL_LEASE)
22942313
*oplock = rsp->OplockLevel;
22952314

2296-
return;
2315+
return 0;
22972316
}
22982317

22992318
static int
@@ -3124,8 +3143,8 @@ SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
31243143
}
31253144

31263145

3127-
smb2_parse_contexts(server, rsp, &oparms->fid->epoch,
3128-
oparms->fid->lease_key, oplock, buf, posix);
3146+
rc = smb2_parse_contexts(server, &rsp_iov, &oparms->fid->epoch,
3147+
oparms->fid->lease_key, oplock, buf, posix);
31293148
creat_exit:
31303149
SMB2_open_free(&rqst);
31313150
free_rsp_buf(resp_buftype, rsp);

fs/smb/client/smb2proto.h

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

252252
extern enum securityEnum smb2_select_sectype(struct TCP_Server_Info *,
253253
enum securityEnum);
254-
extern void smb2_parse_contexts(struct TCP_Server_Info *server,
255-
struct smb2_create_rsp *rsp,
256-
unsigned int *epoch, char *lease_key,
257-
__u8 *oplock, struct smb2_file_all_info *buf,
258-
struct create_posix_rsp *posix);
254+
int smb2_parse_contexts(struct TCP_Server_Info *server,
255+
struct kvec *rsp_iov,
256+
unsigned int *epoch,
257+
char *lease_key, __u8 *oplock,
258+
struct smb2_file_all_info *buf,
259+
struct create_posix_rsp *posix);
260+
259261
extern int smb3_encryption_required(const struct cifs_tcon *tcon);
260262
extern int smb2_validate_iov(unsigned int offset, unsigned int buffer_length,
261263
struct kvec *iov, unsigned int min_buf_size);

0 commit comments

Comments
 (0)