Skip to content

Commit 368ba06

Browse files
namjaejeonSteve French
authored andcommitted
ksmbd: check the validation of pdu_size in ksmbd_conn_handler_loop
The length field of netbios header must be greater than the SMB header sizes(smb1 or smb2 header), otherwise the packet is an invalid SMB packet. If `pdu_size` is 0, ksmbd allocates a 4 bytes chunk to `conn->request_buf`. In the function `get_smb2_cmd_val` ksmbd will read cmd from `rcv_hdr->Command`, which is `conn->request_buf + 12`, causing the KASAN detector to print the following error message: [ 7.205018] BUG: KASAN: slab-out-of-bounds in get_smb2_cmd_val+0x45/0x60 [ 7.205423] Read of size 2 at addr ffff8880062d8b50 by task ksmbd:42632/248 ... [ 7.207125] <TASK> [ 7.209191] get_smb2_cmd_val+0x45/0x60 [ 7.209426] ksmbd_conn_enqueue_request+0x3a/0x100 [ 7.209712] ksmbd_server_process_request+0x72/0x160 [ 7.210295] ksmbd_conn_handler_loop+0x30c/0x550 [ 7.212280] kthread+0x160/0x190 [ 7.212762] ret_from_fork+0x1f/0x30 [ 7.212981] </TASK> Cc: stable@vger.kernel.org Reported-by: Chih-Yen Chang <cc85nod@gmail.com> Signed-off-by: Namjae Jeon <linkinjeon@kernel.org> Signed-off-by: Steve French <stfrench@microsoft.com>
1 parent 2593357 commit 368ba06

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

fs/smb/server/connection.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,9 @@ bool ksmbd_conn_alive(struct ksmbd_conn *conn)
294294
return true;
295295
}
296296

297+
#define SMB1_MIN_SUPPORTED_HEADER_SIZE (sizeof(struct smb_hdr))
298+
#define SMB2_MIN_SUPPORTED_HEADER_SIZE (sizeof(struct smb2_hdr) + 4)
299+
297300
/**
298301
* ksmbd_conn_handler_loop() - session thread to listen on new smb requests
299302
* @p: connection instance
@@ -350,6 +353,9 @@ int ksmbd_conn_handler_loop(void *p)
350353
if (pdu_size > MAX_STREAM_PROT_LEN)
351354
break;
352355

356+
if (pdu_size < SMB1_MIN_SUPPORTED_HEADER_SIZE)
357+
break;
358+
353359
/* 4 for rfc1002 length field */
354360
/* 1 for implied bcc[0] */
355361
size = pdu_size + 4 + 1;
@@ -377,6 +383,12 @@ int ksmbd_conn_handler_loop(void *p)
377383
continue;
378384
}
379385

386+
if (((struct smb2_hdr *)smb2_get_msg(conn->request_buf))->ProtocolId ==
387+
SMB2_PROTO_NUMBER) {
388+
if (pdu_size < SMB2_MIN_SUPPORTED_HEADER_SIZE)
389+
break;
390+
}
391+
380392
if (!default_conn_ops.process_fn) {
381393
pr_err("No connection request callback\n");
382394
break;

0 commit comments

Comments
 (0)