Skip to content

Commit dc8289f

Browse files
namjaejeonsmfrench
authored andcommitted
ksmbd: fix slab-out-of-bounds in init_smb2_rsp_hdr
When smb1 mount fails, KASAN detect slab-out-of-bounds in init_smb2_rsp_hdr like the following one. For smb1 negotiate(56bytes) , init_smb2_rsp_hdr() for smb2 is called. The issue occurs while handling smb1 negotiate as smb2 server operations. Add smb server operations for smb1 (get_cmd_val, init_rsp_hdr, allocate_rsp_buf, check_user_session) to handle smb1 negotiate so that smb2 server operation does not handle it. [ 411.400423] CIFS: VFS: Use of the less secure dialect vers=1.0 is not recommended unless required for access to very old servers [ 411.400452] CIFS: Attempting to mount \\192.168.45.139\homes [ 411.479312] ksmbd: init_smb2_rsp_hdr : 492 [ 411.479323] ================================================================== [ 411.479327] BUG: KASAN: slab-out-of-bounds in init_smb2_rsp_hdr+0x1e2/0x1f4 [ksmbd] [ 411.479369] Read of size 16 at addr ffff888488ed0734 by task kworker/14:1/199 [ 411.479379] CPU: 14 PID: 199 Comm: kworker/14:1 Tainted: G OE 6.1.21 #3 [ 411.479386] Hardware name: ASUSTeK COMPUTER INC. Z10PA-D8 Series/Z10PA-D8 Series, BIOS 3801 08/23/2019 [ 411.479390] Workqueue: ksmbd-io handle_ksmbd_work [ksmbd] [ 411.479425] Call Trace: [ 411.479428] <TASK> [ 411.479432] dump_stack_lvl+0x49/0x63 [ 411.479444] print_report+0x171/0x4a8 [ 411.479452] ? kasan_complete_mode_report_info+0x3c/0x200 [ 411.479463] ? init_smb2_rsp_hdr+0x1e2/0x1f4 [ksmbd] [ 411.479497] kasan_report+0xb4/0x130 [ 411.479503] ? init_smb2_rsp_hdr+0x1e2/0x1f4 [ksmbd] [ 411.479537] kasan_check_range+0x149/0x1e0 [ 411.479543] memcpy+0x24/0x70 [ 411.479550] init_smb2_rsp_hdr+0x1e2/0x1f4 [ksmbd] [ 411.479585] handle_ksmbd_work+0x109/0x760 [ksmbd] [ 411.479616] ? _raw_spin_unlock_irqrestore+0x50/0x50 [ 411.479624] ? smb3_encrypt_resp+0x340/0x340 [ksmbd] [ 411.479656] process_one_work+0x49c/0x790 [ 411.479667] worker_thread+0x2b1/0x6e0 [ 411.479674] ? process_one_work+0x790/0x790 [ 411.479680] kthread+0x177/0x1b0 [ 411.479686] ? kthread_complete_and_exit+0x30/0x30 [ 411.479692] ret_from_fork+0x22/0x30 [ 411.479702] </TASK> Fixes: 39b291b ("ksmbd: return unsupported error on smb1 mount") Cc: [email protected] Signed-off-by: Namjae Jeon <[email protected]> Signed-off-by: Steve French <[email protected]>
1 parent 3a9b557 commit dc8289f

File tree

4 files changed

+111
-37
lines changed

4 files changed

+111
-37
lines changed

fs/ksmbd/server.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -289,10 +289,7 @@ static int queue_ksmbd_work(struct ksmbd_conn *conn)
289289
work->request_buf = conn->request_buf;
290290
conn->request_buf = NULL;
291291

292-
if (ksmbd_init_smb_server(work)) {
293-
ksmbd_free_work_struct(work);
294-
return -EINVAL;
295-
}
292+
ksmbd_init_smb_server(work);
296293

297294
ksmbd_conn_enqueue_request(work);
298295
atomic_inc(&conn->r_count);

fs/ksmbd/smb2pdu.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -229,9 +229,6 @@ int init_smb2_neg_rsp(struct ksmbd_work *work)
229229
struct smb2_negotiate_rsp *rsp;
230230
struct ksmbd_conn *conn = work->conn;
231231

232-
if (conn->need_neg == false)
233-
return -EINVAL;
234-
235232
*(__be32 *)work->response_buf =
236233
cpu_to_be32(conn->vals->header_size);
237234

fs/ksmbd/smb_common.c

Lines changed: 109 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -283,20 +283,121 @@ static int ksmbd_negotiate_smb_dialect(void *buf)
283283
return BAD_PROT_ID;
284284
}
285285

286-
int ksmbd_init_smb_server(struct ksmbd_work *work)
286+
#define SMB_COM_NEGOTIATE_EX 0x0
287+
288+
/**
289+
* get_smb1_cmd_val() - get smb command value from smb header
290+
* @work: smb work containing smb header
291+
*
292+
* Return: smb command value
293+
*/
294+
static u16 get_smb1_cmd_val(struct ksmbd_work *work)
287295
{
288-
struct ksmbd_conn *conn = work->conn;
296+
return SMB_COM_NEGOTIATE_EX;
297+
}
289298

290-
if (conn->need_neg == false)
299+
/**
300+
* init_smb1_rsp_hdr() - initialize smb negotiate response header
301+
* @work: smb work containing smb request
302+
*
303+
* Return: 0 on success, otherwise -EINVAL
304+
*/
305+
static int init_smb1_rsp_hdr(struct ksmbd_work *work)
306+
{
307+
struct smb_hdr *rsp_hdr = (struct smb_hdr *)work->response_buf;
308+
struct smb_hdr *rcv_hdr = (struct smb_hdr *)work->request_buf;
309+
310+
/*
311+
* Remove 4 byte direct TCP header.
312+
*/
313+
*(__be32 *)work->response_buf =
314+
cpu_to_be32(sizeof(struct smb_hdr) - 4);
315+
316+
rsp_hdr->Command = SMB_COM_NEGOTIATE;
317+
*(__le32 *)rsp_hdr->Protocol = SMB1_PROTO_NUMBER;
318+
rsp_hdr->Flags = SMBFLG_RESPONSE;
319+
rsp_hdr->Flags2 = SMBFLG2_UNICODE | SMBFLG2_ERR_STATUS |
320+
SMBFLG2_EXT_SEC | SMBFLG2_IS_LONG_NAME;
321+
rsp_hdr->Pid = rcv_hdr->Pid;
322+
rsp_hdr->Mid = rcv_hdr->Mid;
323+
return 0;
324+
}
325+
326+
/**
327+
* smb1_check_user_session() - check for valid session for a user
328+
* @work: smb work containing smb request buffer
329+
*
330+
* Return: 0 on success, otherwise error
331+
*/
332+
static int smb1_check_user_session(struct ksmbd_work *work)
333+
{
334+
unsigned int cmd = work->conn->ops->get_cmd_val(work);
335+
336+
if (cmd == SMB_COM_NEGOTIATE_EX)
291337
return 0;
292338

293-
init_smb3_11_server(conn);
339+
return -EINVAL;
340+
}
341+
342+
/**
343+
* smb1_allocate_rsp_buf() - allocate response buffer for a command
344+
* @work: smb work containing smb request
345+
*
346+
* Return: 0 on success, otherwise -ENOMEM
347+
*/
348+
static int smb1_allocate_rsp_buf(struct ksmbd_work *work)
349+
{
350+
work->response_buf = kmalloc(MAX_CIFS_SMALL_BUFFER_SIZE,
351+
GFP_KERNEL | __GFP_ZERO);
352+
work->response_sz = MAX_CIFS_SMALL_BUFFER_SIZE;
353+
354+
if (!work->response_buf) {
355+
pr_err("Failed to allocate %u bytes buffer\n",
356+
MAX_CIFS_SMALL_BUFFER_SIZE);
357+
return -ENOMEM;
358+
}
294359

295-
if (conn->ops->get_cmd_val(work) != SMB_COM_NEGOTIATE)
296-
conn->need_neg = false;
297360
return 0;
298361
}
299362

363+
static struct smb_version_ops smb1_server_ops = {
364+
.get_cmd_val = get_smb1_cmd_val,
365+
.init_rsp_hdr = init_smb1_rsp_hdr,
366+
.allocate_rsp_buf = smb1_allocate_rsp_buf,
367+
.check_user_session = smb1_check_user_session,
368+
};
369+
370+
static int smb1_negotiate(struct ksmbd_work *work)
371+
{
372+
return ksmbd_smb_negotiate_common(work, SMB_COM_NEGOTIATE);
373+
}
374+
375+
static struct smb_version_cmds smb1_server_cmds[1] = {
376+
[SMB_COM_NEGOTIATE_EX] = { .proc = smb1_negotiate, },
377+
};
378+
379+
static void init_smb1_server(struct ksmbd_conn *conn)
380+
{
381+
conn->ops = &smb1_server_ops;
382+
conn->cmds = smb1_server_cmds;
383+
conn->max_cmds = ARRAY_SIZE(smb1_server_cmds);
384+
}
385+
386+
void ksmbd_init_smb_server(struct ksmbd_work *work)
387+
{
388+
struct ksmbd_conn *conn = work->conn;
389+
__le32 proto;
390+
391+
if (conn->need_neg == false)
392+
return;
393+
394+
proto = *(__le32 *)((struct smb_hdr *)work->request_buf)->Protocol;
395+
if (proto == SMB1_PROTO_NUMBER)
396+
init_smb1_server(conn);
397+
else
398+
init_smb3_11_server(conn);
399+
}
400+
300401
int ksmbd_populate_dot_dotdot_entries(struct ksmbd_work *work, int info_level,
301402
struct ksmbd_file *dir,
302403
struct ksmbd_dir_info *d_info,
@@ -444,20 +545,10 @@ static int smb_handle_negotiate(struct ksmbd_work *work)
444545

445546
ksmbd_debug(SMB, "Unsupported SMB1 protocol\n");
446547

447-
/*
448-
* Remove 4 byte direct TCP header, add 2 byte bcc and
449-
* 2 byte DialectIndex.
450-
*/
451-
*(__be32 *)work->response_buf =
452-
cpu_to_be32(sizeof(struct smb_hdr) - 4 + 2 + 2);
548+
/* Add 2 byte bcc and 2 byte DialectIndex. */
549+
inc_rfc1001_len(work->response_buf, 4);
453550
neg_rsp->hdr.Status.CifsError = STATUS_SUCCESS;
454551

455-
neg_rsp->hdr.Command = SMB_COM_NEGOTIATE;
456-
*(__le32 *)neg_rsp->hdr.Protocol = SMB1_PROTO_NUMBER;
457-
neg_rsp->hdr.Flags = SMBFLG_RESPONSE;
458-
neg_rsp->hdr.Flags2 = SMBFLG2_UNICODE | SMBFLG2_ERR_STATUS |
459-
SMBFLG2_EXT_SEC | SMBFLG2_IS_LONG_NAME;
460-
461552
neg_rsp->hdr.WordCount = 1;
462553
neg_rsp->DialectIndex = cpu_to_le16(work->conn->dialect);
463554
neg_rsp->ByteCount = 0;
@@ -473,24 +564,13 @@ int ksmbd_smb_negotiate_common(struct ksmbd_work *work, unsigned int command)
473564
ksmbd_negotiate_smb_dialect(work->request_buf);
474565
ksmbd_debug(SMB, "conn->dialect 0x%x\n", conn->dialect);
475566

476-
if (command == SMB2_NEGOTIATE_HE) {
477-
struct smb2_hdr *smb2_hdr = smb2_get_msg(work->request_buf);
478-
479-
if (smb2_hdr->ProtocolId != SMB2_PROTO_NUMBER) {
480-
ksmbd_debug(SMB, "Downgrade to SMB1 negotiation\n");
481-
command = SMB_COM_NEGOTIATE;
482-
}
483-
}
484-
485567
if (command == SMB2_NEGOTIATE_HE) {
486568
ret = smb2_handle_negotiate(work);
487-
init_smb2_neg_rsp(work);
488569
return ret;
489570
}
490571

491572
if (command == SMB_COM_NEGOTIATE) {
492573
if (__smb2_negotiate(conn)) {
493-
conn->need_neg = true;
494574
init_smb3_11_server(conn);
495575
init_smb2_neg_rsp(work);
496576
ksmbd_debug(SMB, "Upgrade to SMB2 negotiation\n");

fs/ksmbd/smb_common.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ bool ksmbd_smb_request(struct ksmbd_conn *conn);
427427

428428
int ksmbd_lookup_dialect_by_id(__le16 *cli_dialects, __le16 dialects_count);
429429

430-
int ksmbd_init_smb_server(struct ksmbd_work *work);
430+
void ksmbd_init_smb_server(struct ksmbd_work *work);
431431

432432
struct ksmbd_kstat;
433433
int ksmbd_populate_dot_dotdot_entries(struct ksmbd_work *work,

0 commit comments

Comments
 (0)