xref: /linux/fs/smb/server/server.c (revision 08f641e2e2e092cbda5ce7f7b5280e327e46823d)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *   Copyright (C) 2016 Namjae Jeon <linkinjeon@kernel.org>
4  *   Copyright (C) 2018 Samsung Electronics Co., Ltd.
5  */
6 
7 #include "glob.h"
8 #include "oplock.h"
9 #include "misc.h"
10 #include <linux/sched/signal.h>
11 #include <linux/workqueue.h>
12 #include <linux/sysfs.h>
13 #include <linux/module.h>
14 #include <linux/moduleparam.h>
15 
16 #include "server.h"
17 #include "smb_common.h"
18 #include "../common/smb2status.h"
19 #include "connection.h"
20 #include "transport_ipc.h"
21 #include "mgmt/user_session.h"
22 #include "crypto_ctx.h"
23 #include "auth.h"
24 #include "stats.h"
25 #include "compress.h"
26 
27 int ksmbd_debug_types;
28 
29 struct ksmbd_server_config server_conf;
30 
31 enum SERVER_CTRL_TYPE {
32 	SERVER_CTRL_TYPE_INIT,
33 	SERVER_CTRL_TYPE_RESET,
34 };
35 
36 struct server_ctrl_struct {
37 	int			type;
38 	struct work_struct	ctrl_work;
39 };
40 
41 static DEFINE_MUTEX(ctrl_lock);
42 
43 static int ___server_conf_set(int idx, char *val)
44 {
45 	if (idx >= ARRAY_SIZE(server_conf.conf))
46 		return -EINVAL;
47 
48 	if (!val || val[0] == 0x00)
49 		return -EINVAL;
50 
51 	kfree(server_conf.conf[idx]);
52 	server_conf.conf[idx] = kstrdup(val, KSMBD_DEFAULT_GFP);
53 	if (!server_conf.conf[idx])
54 		return -ENOMEM;
55 	return 0;
56 }
57 
58 int ksmbd_set_netbios_name(char *v)
59 {
60 	return ___server_conf_set(SERVER_CONF_NETBIOS_NAME, v);
61 }
62 
63 int ksmbd_set_server_string(char *v)
64 {
65 	return ___server_conf_set(SERVER_CONF_SERVER_STRING, v);
66 }
67 
68 int ksmbd_set_work_group(char *v)
69 {
70 	return ___server_conf_set(SERVER_CONF_WORK_GROUP, v);
71 }
72 
73 char *ksmbd_netbios_name(void)
74 {
75 	return server_conf.conf[SERVER_CONF_NETBIOS_NAME];
76 }
77 
78 char *ksmbd_server_string(void)
79 {
80 	return server_conf.conf[SERVER_CONF_SERVER_STRING];
81 }
82 
83 char *ksmbd_work_group(void)
84 {
85 	return server_conf.conf[SERVER_CONF_WORK_GROUP];
86 }
87 
88 /**
89  * check_conn_state() - check state of server thread connection
90  * @work:     smb work containing server thread information
91  *
92  * Return:	0 on valid connection, otherwise 1 to reconnect
93  */
94 static inline int check_conn_state(struct ksmbd_work *work)
95 {
96 	struct smb_hdr *rsp_hdr;
97 
98 	if (ksmbd_conn_exiting(work->conn) ||
99 	    ksmbd_conn_need_reconnect(work->conn)) {
100 		rsp_hdr = smb_get_msg(work->response_buf);
101 		rsp_hdr->Status.CifsError = STATUS_CONNECTION_DISCONNECTED;
102 		return 1;
103 	}
104 	return 0;
105 }
106 
107 #define SERVER_HANDLER_CONTINUE		0
108 #define SERVER_HANDLER_ABORT		1
109 
110 static int __process_request(struct ksmbd_work *work, struct ksmbd_conn *conn,
111 			     u16 *cmd)
112 {
113 	struct smb_version_cmds *cmds;
114 	u16 command;
115 	int ret;
116 
117 	if (check_conn_state(work))
118 		return SERVER_HANDLER_CONTINUE;
119 
120 	if (ksmbd_verify_smb_message(work)) {
121 		conn->ops->set_rsp_status(work, STATUS_INVALID_PARAMETER);
122 		return SERVER_HANDLER_ABORT;
123 	}
124 
125 	command = conn->ops->get_cmd_val(work);
126 	*cmd = command;
127 
128 andx_again:
129 	if (command >= conn->max_cmds) {
130 		conn->ops->set_rsp_status(work, STATUS_INVALID_PARAMETER);
131 		return SERVER_HANDLER_ABORT;
132 	}
133 
134 	cmds = &conn->cmds[command];
135 	if (!cmds->proc) {
136 		ksmbd_debug(SMB, "*** not implemented yet cmd = %x\n", command);
137 		conn->ops->set_rsp_status(work, STATUS_NOT_IMPLEMENTED);
138 		return SERVER_HANDLER_ABORT;
139 	}
140 
141 	if (work->sess && conn->ops->is_sign_req(work, command)) {
142 		ret = conn->ops->check_sign_req(work);
143 		if (!ret) {
144 			conn->ops->set_rsp_status(work, STATUS_ACCESS_DENIED);
145 			return SERVER_HANDLER_ABORT;
146 		}
147 	}
148 
149 	ret = cmds->proc(work);
150 	if (conn->ops->inc_reqs)
151 		conn->ops->inc_reqs(command);
152 
153 	if (ret < 0)
154 		ksmbd_debug(CONN, "Failed to process %u [%d]\n", command, ret);
155 	/* AndX commands - chained request can return positive values */
156 	else if (ret > 0) {
157 		command = ret;
158 		*cmd = command;
159 		goto andx_again;
160 	}
161 
162 	if (work->send_no_response)
163 		return SERVER_HANDLER_ABORT;
164 	return SERVER_HANDLER_CONTINUE;
165 }
166 
167 static void __handle_ksmbd_work(struct ksmbd_work *work,
168 				struct ksmbd_conn *conn)
169 {
170 	u16 command = 0;
171 	int rc;
172 	bool is_chained = false;
173 
174 	if (conn->ops->is_transform_hdr &&
175 	    conn->ops->is_transform_hdr(work->request_buf)) {
176 		rc = conn->ops->decrypt_req(work);
177 		if (rc < 0)
178 			return;
179 		work->encrypted = true;
180 	}
181 
182 	if (conn->ops->allocate_rsp_buf(work))
183 		return;
184 
185 	rc = conn->ops->init_rsp_hdr(work);
186 	if (rc) {
187 		/* either uid or tid is not correct */
188 		conn->ops->set_rsp_status(work, STATUS_INVALID_HANDLE);
189 		goto send;
190 	}
191 
192 	do {
193 		if (conn->ops->check_user_session) {
194 			rc = conn->ops->check_user_session(work);
195 			if (rc < 0) {
196 				if (rc == -EINVAL)
197 					conn->ops->set_rsp_status(work,
198 						STATUS_INVALID_PARAMETER);
199 				else
200 					conn->ops->set_rsp_status(work,
201 						STATUS_USER_SESSION_DELETED);
202 				goto send;
203 			} else if (rc > 0) {
204 				rc = conn->ops->get_ksmbd_tcon(work);
205 				if (rc < 0) {
206 					if (rc == -EINVAL)
207 						conn->ops->set_rsp_status(work,
208 							STATUS_INVALID_PARAMETER);
209 					else
210 						conn->ops->set_rsp_status(work,
211 							STATUS_NETWORK_NAME_DELETED);
212 					goto send;
213 				}
214 			}
215 		}
216 
217 		rc = __process_request(work, conn, &command);
218 		if (rc == SERVER_HANDLER_ABORT)
219 			break;
220 
221 		/*
222 		 * Call smb2_set_rsp_credits() function to set number of credits
223 		 * granted in hdr of smb2 response.
224 		 */
225 		if (conn->ops->set_rsp_credits) {
226 			spin_lock(&conn->credits_lock);
227 			rc = conn->ops->set_rsp_credits(work);
228 			spin_unlock(&conn->credits_lock);
229 			if (rc < 0) {
230 				conn->ops->set_rsp_status(work,
231 					STATUS_INVALID_PARAMETER);
232 				goto send;
233 			}
234 		}
235 
236 		is_chained = is_chained_smb2_message(work);
237 
238 		if (work->sess &&
239 		    (work->sess->sign || smb3_11_final_sess_setup_resp(work) ||
240 		     conn->ops->is_sign_req(work, command)))
241 			conn->ops->set_sign_rsp(work);
242 	} while (is_chained == true);
243 
244 send:
245 	if (work->tcon)
246 		ksmbd_tree_connect_put(work->tcon);
247 	smb3_preauth_hash_rsp(work);
248 	/*
249 	 * Preauthentication hashes cover the original SMB2 response. Apply the
250 	 * transport compression wrapper only after updating the hash.
251 	 */
252 	if (work->compress_response) {
253 		rc = ksmbd_compress_response(work);
254 		if (rc < 0)
255 			ksmbd_debug(CONN, "Failed to compress response: %d\n", rc);
256 	}
257 	if (work->sess && work->sess->enc && work->encrypted &&
258 	    conn->ops->encrypt_resp) {
259 		rc = conn->ops->encrypt_resp(work);
260 		if (rc < 0)
261 			conn->ops->set_rsp_status(work, STATUS_DATA_ERROR);
262 	}
263 	if (work->sess)
264 		ksmbd_user_session_put(work->sess);
265 
266 	ksmbd_conn_write(work);
267 }
268 
269 /**
270  * handle_ksmbd_work() - process pending smb work requests
271  * @wk:	smb work containing request command buffer
272  *
273  * called by kworker threads to processing remaining smb work requests
274  */
275 static void handle_ksmbd_work(struct work_struct *wk)
276 {
277 	struct ksmbd_work *work = container_of(wk, struct ksmbd_work, work);
278 	struct ksmbd_conn *conn = work->conn;
279 
280 	atomic64_inc(&conn->stats.request_served);
281 
282 	__handle_ksmbd_work(work, conn);
283 
284 	ksmbd_conn_try_dequeue_request(work);
285 	ksmbd_free_work_struct(work);
286 	ksmbd_conn_r_count_dec(conn);
287 }
288 
289 /**
290  * queue_ksmbd_work() - queue a smb request to worker thread queue
291  *		for processing smb command and sending response
292  * @conn:	connection instance
293  *
294  * read remaining data from socket create and submit work.
295  */
296 static int queue_ksmbd_work(struct ksmbd_conn *conn)
297 {
298 	struct ksmbd_work *work;
299 	int err;
300 
301 	err = ksmbd_init_smb_server(conn);
302 	if (err)
303 		return 0;
304 
305 	work = ksmbd_alloc_work_struct();
306 	if (!work) {
307 		pr_err("allocation for work failed\n");
308 		return -ENOMEM;
309 	}
310 
311 	work->conn = conn;
312 	work->request_buf = conn->request_buf;
313 	conn->request_buf = NULL;
314 
315 	ksmbd_conn_enqueue_request(work);
316 	ksmbd_conn_r_count_inc(conn);
317 	/* update activity on connection */
318 	conn->last_active = jiffies;
319 	INIT_WORK(&work->work, handle_ksmbd_work);
320 	ksmbd_queue_work(work);
321 	return 0;
322 }
323 
324 static int ksmbd_server_process_request(struct ksmbd_conn *conn)
325 {
326 	return queue_ksmbd_work(conn);
327 }
328 
329 static int ksmbd_server_terminate_conn(struct ksmbd_conn *conn)
330 {
331 	ksmbd_sessions_deregister(conn);
332 	destroy_lease_table(conn);
333 	return 0;
334 }
335 
336 static void ksmbd_server_tcp_callbacks_init(void)
337 {
338 	struct ksmbd_conn_ops ops;
339 
340 	ops.process_fn = ksmbd_server_process_request;
341 	ops.terminate_fn = ksmbd_server_terminate_conn;
342 
343 	ksmbd_conn_init_server_callbacks(&ops);
344 }
345 
346 static void server_conf_free(void)
347 {
348 	int i;
349 
350 	for (i = 0; i < ARRAY_SIZE(server_conf.conf); i++) {
351 		kfree(server_conf.conf[i]);
352 		server_conf.conf[i] = NULL;
353 	}
354 }
355 
356 static int server_conf_init(void)
357 {
358 	WRITE_ONCE(server_conf.state, SERVER_STATE_STARTING_UP);
359 	server_conf.enforced_signing = 0;
360 	server_conf.min_protocol = ksmbd_min_protocol();
361 	server_conf.max_protocol = ksmbd_max_protocol();
362 	server_conf.auth_mechs = KSMBD_AUTH_NTLMSSP;
363 #ifdef CONFIG_SMB_SERVER_KERBEROS5
364 	server_conf.auth_mechs |= KSMBD_AUTH_KRB5 |
365 				KSMBD_AUTH_MSKRB5;
366 #endif
367 	server_conf.max_inflight_req = SMB2_MAX_CREDITS;
368 	return 0;
369 }
370 
371 static void server_ctrl_handle_init(struct server_ctrl_struct *ctrl)
372 {
373 	int ret;
374 
375 	ksmbd_proc_reset();
376 	ret = ksmbd_conn_transport_init();
377 	if (ret) {
378 		server_queue_ctrl_reset_work();
379 		return;
380 	}
381 
382 	pr_info("running\n");
383 	WRITE_ONCE(server_conf.state, SERVER_STATE_RUNNING);
384 }
385 
386 static void server_ctrl_handle_reset(struct server_ctrl_struct *ctrl)
387 {
388 	ksmbd_ipc_soft_reset();
389 	ksmbd_conn_transport_destroy();
390 	ksmbd_stop_durable_scavenger();
391 	server_conf_free();
392 	server_conf_init();
393 	WRITE_ONCE(server_conf.state, SERVER_STATE_STARTING_UP);
394 }
395 
396 static void server_ctrl_handle_work(struct work_struct *work)
397 {
398 	struct server_ctrl_struct *ctrl;
399 
400 	ctrl = container_of(work, struct server_ctrl_struct, ctrl_work);
401 
402 	mutex_lock(&ctrl_lock);
403 	switch (ctrl->type) {
404 	case SERVER_CTRL_TYPE_INIT:
405 		server_ctrl_handle_init(ctrl);
406 		break;
407 	case SERVER_CTRL_TYPE_RESET:
408 		server_ctrl_handle_reset(ctrl);
409 		break;
410 	default:
411 		pr_err("Unknown server work type: %d\n", ctrl->type);
412 	}
413 	mutex_unlock(&ctrl_lock);
414 	kfree(ctrl);
415 	module_put(THIS_MODULE);
416 }
417 
418 static int __queue_ctrl_work(int type)
419 {
420 	struct server_ctrl_struct *ctrl;
421 
422 	ctrl = kmalloc_obj(struct server_ctrl_struct, KSMBD_DEFAULT_GFP);
423 	if (!ctrl)
424 		return -ENOMEM;
425 
426 	__module_get(THIS_MODULE);
427 	ctrl->type = type;
428 	INIT_WORK(&ctrl->ctrl_work, server_ctrl_handle_work);
429 	queue_work(system_long_wq, &ctrl->ctrl_work);
430 	return 0;
431 }
432 
433 int server_queue_ctrl_init_work(void)
434 {
435 	return __queue_ctrl_work(SERVER_CTRL_TYPE_INIT);
436 }
437 
438 int server_queue_ctrl_reset_work(void)
439 {
440 	return __queue_ctrl_work(SERVER_CTRL_TYPE_RESET);
441 }
442 
443 static ssize_t stats_show(const struct class *class, const struct class_attribute *attr,
444 			  char *buf)
445 {
446 	/*
447 	 * Inc this each time you change stats output format,
448 	 * so user space will know what to do.
449 	 */
450 	static int stats_version = 2;
451 	static const char * const state[] = {
452 		"startup",
453 		"running",
454 		"reset",
455 		"shutdown"
456 	};
457 	return sysfs_emit(buf, "%d %s %d %lu\n", stats_version,
458 			  state[server_conf.state], server_conf.tcp_port,
459 			  server_conf.ipc_last_active / HZ);
460 }
461 
462 static ssize_t kill_server_store(const struct class *class,
463 				 const struct class_attribute *attr, const char *buf,
464 				 size_t len)
465 {
466 	if (!sysfs_streq(buf, "hard"))
467 		return len;
468 
469 	pr_info("kill command received\n");
470 	mutex_lock(&ctrl_lock);
471 	WRITE_ONCE(server_conf.state, SERVER_STATE_RESETTING);
472 	__module_get(THIS_MODULE);
473 	server_ctrl_handle_reset(NULL);
474 	module_put(THIS_MODULE);
475 	mutex_unlock(&ctrl_lock);
476 	return len;
477 }
478 
479 static const char * const debug_type_strings[] = {"smb", "auth", "vfs",
480 						  "oplock", "ipc", "conn",
481 						  "rdma"};
482 
483 static ssize_t debug_show(const struct class *class, const struct class_attribute *attr,
484 			  char *buf)
485 {
486 	ssize_t sz = 0;
487 	int i, pos = 0;
488 
489 	for (i = 0; i < ARRAY_SIZE(debug_type_strings); i++) {
490 		if ((ksmbd_debug_types >> i) & 1) {
491 			pos = sysfs_emit_at(buf, sz, "[%s] ", debug_type_strings[i]);
492 		} else {
493 			pos = sysfs_emit_at(buf, sz, "%s ", debug_type_strings[i]);
494 		}
495 		sz += pos;
496 	}
497 	sz += sysfs_emit_at(buf, sz, "\n");
498 	return sz;
499 }
500 
501 static ssize_t debug_store(const struct class *class, const struct class_attribute *attr,
502 			   const char *buf, size_t len)
503 {
504 	int i;
505 
506 	for (i = 0; i < ARRAY_SIZE(debug_type_strings); i++) {
507 		if (sysfs_streq(buf, "all")) {
508 			if (ksmbd_debug_types == KSMBD_DEBUG_ALL)
509 				ksmbd_debug_types = 0;
510 			else
511 				ksmbd_debug_types = KSMBD_DEBUG_ALL;
512 			break;
513 		}
514 
515 		if (sysfs_streq(buf, debug_type_strings[i])) {
516 			if (ksmbd_debug_types & (1 << i))
517 				ksmbd_debug_types &= ~(1 << i);
518 			else
519 				ksmbd_debug_types |= (1 << i);
520 			break;
521 		}
522 	}
523 
524 	return len;
525 }
526 
527 static CLASS_ATTR_RO(stats);
528 static CLASS_ATTR_WO(kill_server);
529 static CLASS_ATTR_RW(debug);
530 
531 static struct attribute *ksmbd_control_class_attrs[] = {
532 	&class_attr_stats.attr,
533 	&class_attr_kill_server.attr,
534 	&class_attr_debug.attr,
535 	NULL,
536 };
537 ATTRIBUTE_GROUPS(ksmbd_control_class);
538 
539 static struct class ksmbd_control_class = {
540 	.name		= "ksmbd-control",
541 	.class_groups	= ksmbd_control_class_groups,
542 };
543 
544 static int ksmbd_server_shutdown(void)
545 {
546 	WRITE_ONCE(server_conf.state, SERVER_STATE_SHUTTING_DOWN);
547 
548 	ksmbd_proc_cleanup();
549 	class_unregister(&ksmbd_control_class);
550 	ksmbd_workqueue_destroy();
551 	ksmbd_ipc_release();
552 	ksmbd_conn_transport_destroy();
553 	ksmbd_crypto_destroy();
554 	ksmbd_free_global_file_table();
555 	destroy_lease_table(NULL);
556 	ksmbd_work_pool_destroy();
557 	ksmbd_exit_file_cache();
558 	server_conf_free();
559 	return 0;
560 }
561 
562 static int __init ksmbd_server_init(void)
563 {
564 	int ret;
565 
566 	ret = class_register(&ksmbd_control_class);
567 	if (ret) {
568 		pr_err("Unable to register ksmbd-control class\n");
569 		return ret;
570 	}
571 
572 	ksmbd_proc_init();
573 	create_proc_sessions();
574 
575 	ksmbd_server_tcp_callbacks_init();
576 
577 	ret = server_conf_init();
578 	if (ret)
579 		goto err_unregister;
580 
581 	ret = ksmbd_work_pool_init();
582 	if (ret)
583 		goto err_unregister;
584 
585 	ret = ksmbd_init_file_cache();
586 	if (ret)
587 		goto err_destroy_work_pools;
588 
589 	ret = ksmbd_ipc_init();
590 	if (ret)
591 		goto err_exit_file_cache;
592 
593 	ret = ksmbd_init_global_file_table();
594 	if (ret)
595 		goto err_ipc_release;
596 
597 	ret = ksmbd_inode_hash_init();
598 	if (ret)
599 		goto err_destroy_file_table;
600 
601 	ret = ksmbd_crypto_create();
602 	if (ret)
603 		goto err_release_inode_hash;
604 
605 	ret = ksmbd_workqueue_init();
606 	if (ret)
607 		goto err_crypto_destroy;
608 
609 	ret = ksmbd_conn_wq_init();
610 	if (ret)
611 		goto err_workqueue_destroy;
612 
613 	return 0;
614 
615 err_workqueue_destroy:
616 	ksmbd_workqueue_destroy();
617 err_crypto_destroy:
618 	ksmbd_crypto_destroy();
619 err_release_inode_hash:
620 	ksmbd_release_inode_hash();
621 err_destroy_file_table:
622 	ksmbd_free_global_file_table();
623 err_ipc_release:
624 	ksmbd_ipc_release();
625 err_exit_file_cache:
626 	ksmbd_exit_file_cache();
627 err_destroy_work_pools:
628 	ksmbd_work_pool_destroy();
629 err_unregister:
630 	class_unregister(&ksmbd_control_class);
631 
632 	return ret;
633 }
634 
635 /**
636  * ksmbd_server_exit() - shutdown forker thread and free memory at module exit
637  */
638 static void __exit ksmbd_server_exit(void)
639 {
640 	ksmbd_server_shutdown();
641 	rcu_barrier();
642 	/*
643 	 * ksmbd_conn_put() defers the final release onto ksmbd_conn_wq,
644 	 * so drain it after rcu_barrier() has fired any pending RCU
645 	 * callbacks that may have queued a release.
646 	 */
647 	ksmbd_conn_wq_destroy();
648 	ksmbd_release_inode_hash();
649 }
650 
651 MODULE_AUTHOR("Namjae Jeon <linkinjeon@kernel.org>");
652 MODULE_DESCRIPTION("Linux kernel CIFS/SMB SERVER");
653 MODULE_LICENSE("GPL");
654 MODULE_SOFTDEP("pre: nls");
655 MODULE_SOFTDEP("pre: aes");
656 MODULE_SOFTDEP("pre: aead2");
657 MODULE_SOFTDEP("pre: ccm");
658 MODULE_SOFTDEP("pre: gcm");
659 module_init(ksmbd_server_init)
660 module_exit(ksmbd_server_exit)
661