Skip to content

Commit a1e6fff

Browse files
committed
Merge tag '6.3-rc5-ksmbd-server-fixes' of git://git.samba.org/ksmbd
Pull ksmbd server fixes from Steve French: "Four fixes, three for stable: - slab out of bounds fix - lock cancellation fix - minor cleanup to address clang warning - fix for xfstest 551 (wrong parms passed to kvmalloc)" * tag '6.3-rc5-ksmbd-server-fixes' of git://git.samba.org/ksmbd: ksmbd: fix slab-out-of-bounds in init_smb2_rsp_hdr ksmbd: delete asynchronous work from list ksmbd: remove unused is_char_allowed function ksmbd: do not call kvmalloc() with __GFP_NORETRY | __GFP_NO_WARN
2 parents f2afccf + dc8289f commit a1e6fff

File tree

8 files changed

+140
-79
lines changed

8 files changed

+140
-79
lines changed

fs/ksmbd/connection.c

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,8 @@ void ksmbd_conn_enqueue_request(struct ksmbd_work *work)
112112
struct ksmbd_conn *conn = work->conn;
113113
struct list_head *requests_queue = NULL;
114114

115-
if (conn->ops->get_cmd_val(work) != SMB2_CANCEL_HE) {
115+
if (conn->ops->get_cmd_val(work) != SMB2_CANCEL_HE)
116116
requests_queue = &conn->requests;
117-
work->synchronous = true;
118-
}
119117

120118
if (requests_queue) {
121119
atomic_inc(&conn->req_running);
@@ -136,14 +134,14 @@ int ksmbd_conn_try_dequeue_request(struct ksmbd_work *work)
136134

137135
if (!work->multiRsp)
138136
atomic_dec(&conn->req_running);
139-
spin_lock(&conn->request_lock);
140137
if (!work->multiRsp) {
138+
spin_lock(&conn->request_lock);
141139
list_del_init(&work->request_entry);
142-
if (!work->synchronous)
143-
list_del_init(&work->async_request_entry);
140+
spin_unlock(&conn->request_lock);
141+
if (work->asynchronous)
142+
release_async_work(work);
144143
ret = 0;
145144
}
146-
spin_unlock(&conn->request_lock);
147145

148146
wake_up_all(&conn->req_running_q);
149147
return ret;
@@ -326,10 +324,7 @@ int ksmbd_conn_handler_loop(void *p)
326324

327325
/* 4 for rfc1002 length field */
328326
size = pdu_size + 4;
329-
conn->request_buf = kvmalloc(size,
330-
GFP_KERNEL |
331-
__GFP_NOWARN |
332-
__GFP_NORETRY);
327+
conn->request_buf = kvmalloc(size, GFP_KERNEL);
333328
if (!conn->request_buf)
334329
break;
335330

fs/ksmbd/ksmbd_work.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ struct ksmbd_work {
6868
/* Request is encrypted */
6969
bool encrypted:1;
7070
/* Is this SYNC or ASYNC ksmbd_work */
71-
bool synchronous:1;
71+
bool asynchronous:1;
7272
bool need_invalidate_rkey:1;
7373

7474
unsigned int remote_key;

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: 21 additions & 15 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

@@ -498,12 +495,6 @@ int init_smb2_rsp_hdr(struct ksmbd_work *work)
498495
rsp_hdr->SessionId = rcv_hdr->SessionId;
499496
memcpy(rsp_hdr->Signature, rcv_hdr->Signature, 16);
500497

501-
work->synchronous = true;
502-
if (work->async_id) {
503-
ksmbd_release_id(&conn->async_ida, work->async_id);
504-
work->async_id = 0;
505-
}
506-
507498
return 0;
508499
}
509500

@@ -644,7 +635,7 @@ int setup_async_work(struct ksmbd_work *work, void (*fn)(void **), void **arg)
644635
pr_err("Failed to alloc async message id\n");
645636
return id;
646637
}
647-
work->synchronous = false;
638+
work->asynchronous = true;
648639
work->async_id = id;
649640
rsp_hdr->Id.AsyncId = cpu_to_le64(id);
650641

@@ -664,6 +655,24 @@ int setup_async_work(struct ksmbd_work *work, void (*fn)(void **), void **arg)
664655
return 0;
665656
}
666657

658+
void release_async_work(struct ksmbd_work *work)
659+
{
660+
struct ksmbd_conn *conn = work->conn;
661+
662+
spin_lock(&conn->request_lock);
663+
list_del_init(&work->async_request_entry);
664+
spin_unlock(&conn->request_lock);
665+
666+
work->asynchronous = 0;
667+
work->cancel_fn = NULL;
668+
kfree(work->cancel_argv);
669+
work->cancel_argv = NULL;
670+
if (work->async_id) {
671+
ksmbd_release_id(&conn->async_ida, work->async_id);
672+
work->async_id = 0;
673+
}
674+
}
675+
667676
void smb2_send_interim_resp(struct ksmbd_work *work, __le32 status)
668677
{
669678
struct smb2_hdr *rsp_hdr;
@@ -7045,13 +7054,9 @@ int smb2_lock(struct ksmbd_work *work)
70457054

70467055
ksmbd_vfs_posix_lock_wait(flock);
70477056

7048-
spin_lock(&work->conn->request_lock);
70497057
spin_lock(&fp->f_lock);
70507058
list_del(&work->fp_entry);
7051-
work->cancel_fn = NULL;
7052-
kfree(argv);
70537059
spin_unlock(&fp->f_lock);
7054-
spin_unlock(&work->conn->request_lock);
70557060

70567061
if (work->state != KSMBD_WORK_ACTIVE) {
70577062
list_del(&smb_lock->llist);
@@ -7069,6 +7074,7 @@ int smb2_lock(struct ksmbd_work *work)
70697074
work->send_no_response = 1;
70707075
goto out;
70717076
}
7077+
70727078
init_smb2_rsp_hdr(work);
70737079
smb2_set_err_rsp(work);
70747080
rsp->hdr.Status =
@@ -7081,7 +7087,7 @@ int smb2_lock(struct ksmbd_work *work)
70817087
spin_lock(&work->conn->llist_lock);
70827088
list_del(&smb_lock->clist);
70837089
spin_unlock(&work->conn->llist_lock);
7084-
7090+
release_async_work(work);
70857091
goto retry;
70867092
} else if (!rc) {
70877093
spin_lock(&work->conn->llist_lock);

fs/ksmbd/smb2pdu.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,7 @@ int find_matching_smb2_dialect(int start_index, __le16 *cli_dialects,
486486
struct file_lock *smb_flock_init(struct file *f);
487487
int setup_async_work(struct ksmbd_work *work, void (*fn)(void **),
488488
void **arg);
489+
void release_async_work(struct ksmbd_work *work);
489490
void smb2_send_interim_resp(struct ksmbd_work *work, __le32 status);
490491
struct channel *lookup_chann_list(struct ksmbd_session *sess,
491492
struct ksmbd_conn *conn);

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,

fs/ksmbd/unicode.c

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -113,24 +113,6 @@ cifs_mapchar(char *target, const __u16 src_char, const struct nls_table *cp,
113113
goto out;
114114
}
115115

116-
/*
117-
* is_char_allowed() - check for valid character
118-
* @ch: input character to be checked
119-
*
120-
* Return: 1 if char is allowed, otherwise 0
121-
*/
122-
static inline int is_char_allowed(char *ch)
123-
{
124-
/* check for control chars, wildcards etc. */
125-
if (!(*ch & 0x80) &&
126-
(*ch <= 0x1f ||
127-
*ch == '?' || *ch == '"' || *ch == '<' ||
128-
*ch == '>' || *ch == '|'))
129-
return 0;
130-
131-
return 1;
132-
}
133-
134116
/*
135117
* smb_from_utf16() - convert utf16le string to local charset
136118
* @to: destination buffer

0 commit comments

Comments
 (0)