1 From cc4f3b5a6ab4693aba94a45cc073188df4d67175 Mon Sep 17 00:00:00 2001
2 From: Namjae Jeon <linkinjeon@kernel.org>
3 Date: Mon, 26 Dec 2022 01:28:52 +0900
4 Subject: ksmbd: fix infinite loop in ksmbd_conn_handler_loop()
6 If kernel_recvmsg() return -EAGAIN in ksmbd_tcp_readv() and go round
7 again, It will cause infinite loop issue. And all threads from next
8 connections would be doing that. This patch add max retry count(2) to
9 avoid it. kernel_recvmsg() will wait during 7sec timeout and try to
10 retry two time if -EAGAIN is returned. And add flags of kvmalloc to
11 __GFP_NOWARN and __GFP_NORETRY to disconnect immediately without
12 retrying on memory alloation failure.
14 Fixes: 0626e66 ("cifsd: add server handler for central processing and tranport layers")
15 Cc: stable@vger.kernel.org
16 Reported-by: zdi-disclosures@trendmicro.com # ZDI-CAN-18259
17 Reviewed-by: Sergey Senozhatsky <senozhatsky@chromium.org>
18 Signed-off-by: Namjae Jeon <linkinjeon@kernel.org>
20 connection.c | 7 +++++--
21 transport_tcp.c | 5 ++++-
22 2 files changed, 9 insertions(+), 3 deletions(-)
26 @@ -337,9 +337,12 @@ int ksmbd_conn_handler_loop(void *p)
28 /* 4 for rfc1002 length field */
30 - conn->request_buf = kvmalloc(size, GFP_KERNEL);
31 + conn->request_buf = kvmalloc(size,
35 if (!conn->request_buf)
39 memcpy(conn->request_buf, hdr_buf, sizeof(hdr_buf));
40 if (!ksmbd_smb_request(conn))
43 @@ -323,6 +323,7 @@ static int ksmbd_tcp_readv(struct tcp_tr
44 struct msghdr ksmbd_msg;
46 struct ksmbd_conn *conn = KSMBD_TRANS(t)->conn;
49 iov = get_conn_iovec(t, nr_segs);
51 @@ -349,9 +350,11 @@ static int ksmbd_tcp_readv(struct tcp_tr
52 } else if (conn->status == KSMBD_SESS_NEED_RECONNECT) {
55 - } else if (length == -ERESTARTSYS || length == -EAGAIN) {
56 + } else if ((length == -ERESTARTSYS || length == -EAGAIN) &&
58 usleep_range(1000, 2000);
62 } else if (length <= 0) {