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