1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Copyright (C) 2018 Samsung Electronics Co., Ltd. 4 */ 5 6 #include <linux/atomic.h> 7 #include <linux/err.h> 8 #include <linux/list.h> 9 #include <linux/slab.h> 10 #include <linux/rwsem.h> 11 #include <linux/xarray.h> 12 13 #include "ksmbd_ida.h" 14 #include "user_session.h" 15 #include "user_config.h" 16 #include "tree_connect.h" 17 #include "share_config.h" 18 #include "../transport_ipc.h" 19 #include "../connection.h" 20 #include "../vfs_cache.h" 21 #include "../misc.h" 22 #include "../stats.h" 23 #include "../server.h" 24 25 static DEFINE_IDA(session_ida); 26 27 #define SESSION_HASH_BITS 12 28 #define KSMBD_MAX_PENDING_SESSIONS 1 29 static DEFINE_HASHTABLE(sessions_table, SESSION_HASH_BITS); 30 static DECLARE_RWSEM(sessions_table_lock); 31 32 int ksmbd_session_reserve_slot(struct ksmbd_conn *conn) 33 { 34 if (atomic64_inc_return(&conn->session_count) > 35 READ_ONCE(server_conf.max_sessions_per_connection)) { 36 atomic64_dec(&conn->session_count); 37 return -ENOSPC; 38 } 39 return 0; 40 } 41 42 void ksmbd_session_release_slot(struct ksmbd_conn *conn) 43 { 44 atomic64_dec(&conn->session_count); 45 } 46 47 #ifdef CONFIG_PROC_FS 48 49 static const struct ksmbd_const_name ksmbd_sess_cap_const_names[] = { 50 {SMB2_GLOBAL_CAP_DFS, "dfs"}, 51 {SMB2_GLOBAL_CAP_LEASING, "lease"}, 52 {SMB2_GLOBAL_CAP_LARGE_MTU, "large-mtu"}, 53 {SMB2_GLOBAL_CAP_MULTI_CHANNEL, "multi-channel"}, 54 {SMB2_GLOBAL_CAP_PERSISTENT_HANDLES, "persistent-handles"}, 55 {SMB2_GLOBAL_CAP_DIRECTORY_LEASING, "dir-lease"}, 56 {SMB2_GLOBAL_CAP_ENCRYPTION, "encryption"} 57 }; 58 59 static const struct ksmbd_const_name ksmbd_cipher_const_names[] = { 60 {le16_to_cpu(SMB2_ENCRYPTION_AES128_CCM), "aes128-ccm"}, 61 {le16_to_cpu(SMB2_ENCRYPTION_AES128_GCM), "aes128-gcm"}, 62 {le16_to_cpu(SMB2_ENCRYPTION_AES256_CCM), "aes256-ccm"}, 63 {le16_to_cpu(SMB2_ENCRYPTION_AES256_GCM), "aes256-gcm"}, 64 }; 65 66 static const struct ksmbd_const_name ksmbd_signing_const_names[] = { 67 {SIGNING_ALG_HMAC_SHA256, "hmac-sha256"}, 68 {SIGNING_ALG_AES_CMAC, "aes-cmac"}, 69 {SIGNING_ALG_AES_GMAC, "aes-gmac"}, 70 }; 71 72 static const char *session_state_string(struct ksmbd_session *session) 73 { 74 switch (session->state) { 75 case SMB2_SESSION_VALID: 76 return "valid"; 77 case SMB2_SESSION_IN_PROGRESS: 78 return "progress"; 79 case SMB2_SESSION_EXPIRED: 80 return "expired"; 81 default: 82 return ""; 83 } 84 } 85 86 static const char *session_user_name(struct ksmbd_session *session) 87 { 88 if (!session->user) 89 return "(Unauthenticated)"; 90 if (user_guest(session->user)) 91 return "(Guest)"; 92 else if (ksmbd_anonymous_user(session->user)) 93 return "(Anonymous)"; 94 return session->user->name; 95 } 96 97 static const char *session_account_type(struct ksmbd_session *session) 98 { 99 if (!session->user) 100 return "unauthenticated"; 101 if (user_guest(session->user)) 102 return "guest"; 103 if (ksmbd_anonymous_user(session->user)) 104 return "anonymous"; 105 return "user"; 106 } 107 108 static unsigned int session_open_file_count(struct ksmbd_session *session) 109 { 110 struct ksmbd_file *fp; 111 unsigned int count = 0; 112 unsigned int id; 113 114 read_lock(&session->file_table.lock); 115 if (session->file_table.idr) { 116 idr_for_each_entry(session->file_table.idr, fp, id) 117 count++; 118 } 119 read_unlock(&session->file_table.lock); 120 return count; 121 } 122 123 static int show_proc_session(struct seq_file *m, void *v) 124 { 125 struct ksmbd_session *sess; 126 struct ksmbd_tree_connect *tree_conn; 127 struct ksmbd_share_config *share_conf; 128 struct channel *chan; 129 unsigned long id; 130 int i = 0; 131 132 sess = (struct ksmbd_session *)m->private; 133 ksmbd_user_session_get(sess); 134 135 seq_printf(m, "user:\t%s\n", session_user_name(sess)); 136 seq_printf(m, "account_type:\t%s\n", 137 session_account_type(sess)); 138 seq_printf(m, "id:\t%llu\n", sess->id); 139 seq_printf(m, "state:\t%s\n", session_state_string(sess)); 140 seq_printf(m, "dialect:\t0x%04x\n", sess->dialect); 141 seq_printf(m, "last_active_seconds:\t%lu\n", 142 jiffies_to_msecs(jiffies - sess->last_active) / MSEC_PER_SEC); 143 seq_printf(m, "open_files:\t%u\n", 144 session_open_file_count(sess)); 145 146 i = 0; 147 down_read(&sess->chann_lock); 148 xa_for_each(&sess->ksmbd_chann_list, id, chan) { 149 const char *name; 150 151 #if IS_ENABLED(CONFIG_IPV6) 152 if (!chan->conn->is_ipv6) 153 seq_printf(m, "client:\t%pI4\n", 154 &chan->conn->inet_addr); 155 else 156 seq_printf(m, "client:\t%pI6c\n", 157 &chan->conn->inet6_addr); 158 #else 159 seq_printf(m, "client:\t%pI4\n", 160 &chan->conn->inet_addr); 161 #endif 162 seq_puts(m, "capabilities:\t"); 163 ksmbd_proc_show_flag_names(m, 164 ksmbd_sess_cap_const_names, 165 ARRAY_SIZE(ksmbd_sess_cap_const_names), 166 chan->conn->vals->req_capabilities); 167 seq_putc(m, '\n'); 168 seq_printf(m, "posix_extensions:\t%s\n", 169 chan->conn->posix_ext_supported ? "yes" : "no"); 170 171 if (sess->sign) { 172 unsigned int algorithm = 173 le16_to_cpu(chan->conn->signing_algorithm); 174 175 name = ksmbd_proc_const_name(ksmbd_signing_const_names, 176 ARRAY_SIZE(ksmbd_signing_const_names), 177 algorithm); 178 if (name) 179 seq_printf(m, "signing:\t%s\n", name); 180 else 181 seq_printf(m, "signing:\t0x%04x\n", 182 algorithm); 183 } 184 if (sess->enc) { 185 unsigned int cipher = le16_to_cpu(chan->conn->cipher_type); 186 187 name = ksmbd_proc_const_name(ksmbd_cipher_const_names, 188 ARRAY_SIZE(ksmbd_cipher_const_names), 189 cipher); 190 if (name) 191 seq_printf(m, "encryption:\t%s\n", name); 192 else 193 seq_printf(m, "encryption:\t0x%04x\n", 194 cipher); 195 } 196 i++; 197 } 198 up_read(&sess->chann_lock); 199 200 seq_printf(m, "channels:\t%d\n", i); 201 202 i = 0; 203 down_read(&sess->tree_conns_lock); 204 xa_for_each(&sess->tree_conns, id, tree_conn) { 205 share_conf = tree_conn->share_conf; 206 seq_printf(m, "share:\t%s\n", share_conf->name); 207 seq_printf(m, "tree_id:\t%d\n", tree_conn->id); 208 seq_printf(m, "share_type:\t%s\n", 209 test_share_config_flag(share_conf, KSMBD_SHARE_FLAG_PIPE) ? 210 "pipe" : "disk"); 211 i++; 212 } 213 up_read(&sess->tree_conns_lock); 214 seq_printf(m, "tree_connects:\t%d\n", i); 215 216 ksmbd_user_session_put(sess); 217 return 0; 218 } 219 220 static int create_proc_session(struct ksmbd_session *sess) 221 { 222 char name[30]; 223 224 snprintf(name, sizeof(name), "sessions/%llu", sess->id); 225 sess->proc_entry = ksmbd_proc_create(name, 226 show_proc_session, sess); 227 if (!sess->proc_entry) 228 return -ENOMEM; 229 return 0; 230 } 231 232 static void delete_proc_session(struct ksmbd_session *sess) 233 { 234 if (sess->proc_entry) { 235 proc_remove(sess->proc_entry); 236 sess->proc_entry = NULL; 237 } 238 } 239 240 static int show_proc_sessions(struct seq_file *m, void *v) 241 { 242 struct ksmbd_session *session; 243 struct channel *chan; 244 int i; 245 unsigned long id; 246 247 down_read(&sessions_table_lock); 248 hash_for_each(sessions_table, i, session, hlist) { 249 down_read(&session->chann_lock); 250 xa_for_each(&session->ksmbd_chann_list, id, chan) { 251 down_read(&chan->conn->session_lock); 252 ksmbd_user_session_get(session); 253 254 #if IS_ENABLED(CONFIG_IPV6) 255 if (chan->conn->is_ipv6) 256 seq_printf(m, "client:\t%pI6c\n", &chan->conn->inet6_addr); 257 else 258 #endif 259 seq_printf(m, "client:\t%pI4\n", &chan->conn->inet_addr); 260 seq_printf(m, "user:\t%s\n", session_user_name(session)); 261 seq_printf(m, "id:\t%llu\n", session->id); 262 seq_printf(m, "state:\t%s\n\n", 263 session_state_string(session)); 264 265 ksmbd_user_session_put(session); 266 up_read(&chan->conn->session_lock); 267 } 268 up_read(&session->chann_lock); 269 } 270 up_read(&sessions_table_lock); 271 return 0; 272 } 273 274 int create_proc_sessions(void) 275 { 276 if (!ksmbd_proc_create("sessions/sessions", 277 show_proc_sessions, NULL)) 278 return -ENOMEM; 279 return 0; 280 } 281 #else 282 int create_proc_sessions(void) { return 0; } 283 static int create_proc_session(struct ksmbd_session *sess) { return 0; } 284 static void delete_proc_session(struct ksmbd_session *sess) {} 285 #endif 286 287 static void free_channel_list(struct ksmbd_session *sess) 288 { 289 struct channel *chann; 290 unsigned long index; 291 292 down_write(&sess->chann_lock); 293 xa_for_each(&sess->ksmbd_chann_list, index, chann) { 294 xa_erase(&sess->ksmbd_chann_list, index); 295 ksmbd_session_release_slot(chann->conn); 296 ksmbd_conn_put(chann->conn); 297 kfree_sensitive(chann); 298 } 299 300 xa_destroy(&sess->ksmbd_chann_list); 301 up_write(&sess->chann_lock); 302 } 303 304 static void ksmbd_session_rpc_free(struct ksmbd_session_rpc *entry) 305 { 306 if (refcount_dec_and_test(&entry->refcount)) { 307 ksmbd_rpc_id_free(entry->id); 308 kvfree(entry->pending); 309 kfree(entry); 310 } 311 } 312 313 static void __session_rpc_close(struct ksmbd_session_rpc *entry) 314 { 315 struct ksmbd_rpc_command *resp; 316 317 mutex_lock(&entry->lock); 318 resp = ksmbd_rpc_close(entry); 319 if (!resp) 320 pr_err("Unable to close RPC pipe %d\n", entry->id); 321 kvfree(resp); 322 mutex_unlock(&entry->lock); 323 ksmbd_session_rpc_free(entry); 324 } 325 326 void ksmbd_session_rpc_clear_list(struct ksmbd_session *sess) 327 { 328 struct ksmbd_session_rpc *entry; 329 unsigned long index; 330 331 xa_for_each(&sess->rpc_handle_list, index, entry) 332 ksmbd_session_rpc_close(sess, index); 333 xa_destroy(&sess->rpc_handle_list); 334 } 335 336 static int __rpc_method(char *rpc_name) 337 { 338 if (!strcmp(rpc_name, "\\srvsvc") || !strcmp(rpc_name, "srvsvc")) 339 return KSMBD_RPC_SRVSVC_METHOD_INVOKE; 340 341 if (!strcmp(rpc_name, "\\wkssvc") || !strcmp(rpc_name, "wkssvc")) 342 return KSMBD_RPC_WKSSVC_METHOD_INVOKE; 343 344 if (!strcmp(rpc_name, "LANMAN") || !strcmp(rpc_name, "lanman")) 345 return KSMBD_RPC_RAP_METHOD; 346 347 if (!strcmp(rpc_name, "\\samr") || !strcmp(rpc_name, "samr")) 348 return KSMBD_RPC_SAMR_METHOD_INVOKE; 349 350 if (!strcmp(rpc_name, "\\lsarpc") || !strcmp(rpc_name, "lsarpc")) 351 return KSMBD_RPC_LSARPC_METHOD_INVOKE; 352 353 if (!strcmp(rpc_name, "\\mdssvc") || !strcmp(rpc_name, "mdssvc")) 354 return -ENOENT; 355 356 pr_err("Unsupported RPC: %s\n", rpc_name); 357 return -ENOENT; 358 } 359 360 int ksmbd_session_rpc_open(struct ksmbd_session *sess, char *rpc_name) 361 { 362 struct ksmbd_session_rpc *entry; 363 struct ksmbd_rpc_command *resp; 364 int method, id, ret; 365 366 method = __rpc_method(rpc_name); 367 if (method < 0) 368 return method; 369 370 entry = kzalloc_obj(struct ksmbd_session_rpc, KSMBD_DEFAULT_GFP); 371 if (!entry) 372 return -ENOMEM; 373 374 entry->method = method; 375 entry->id = id = ksmbd_ipc_id_alloc(); 376 if (id < 0) { 377 kfree(entry); 378 return id; 379 } 380 mutex_init(&entry->lock); 381 refcount_set(&entry->refcount, 1); 382 383 resp = ksmbd_rpc_open(entry); 384 if (!resp) { 385 ksmbd_session_rpc_free(entry); 386 return -EINVAL; 387 } 388 kvfree(resp); 389 390 down_write(&sess->rpc_lock); 391 ret = xa_err(xa_store(&sess->rpc_handle_list, id, entry, 392 KSMBD_DEFAULT_GFP)); 393 up_write(&sess->rpc_lock); 394 if (ret) { 395 __session_rpc_close(entry); 396 return ret; 397 } 398 return id; 399 } 400 401 void ksmbd_session_rpc_close(struct ksmbd_session *sess, int id) 402 { 403 struct ksmbd_session_rpc *entry; 404 405 down_write(&sess->rpc_lock); 406 entry = xa_erase(&sess->rpc_handle_list, id); 407 if (entry) 408 WRITE_ONCE(entry->closed, true); 409 up_write(&sess->rpc_lock); 410 if (entry) 411 __session_rpc_close(entry); 412 } 413 414 /* The reference pins the entry while waiting without holding rpc_lock. */ 415 struct ksmbd_session_rpc *ksmbd_session_rpc_get(struct ksmbd_work *work, 416 int id) 417 { 418 struct ksmbd_session_rpc *entry; 419 int err; 420 421 down_read(&work->sess->rpc_lock); 422 entry = xa_load(&work->sess->rpc_handle_list, id); 423 if (entry) 424 refcount_inc(&entry->refcount); 425 up_read(&work->sess->rpc_lock); 426 if (!entry) 427 return ERR_PTR(-ENOENT); 428 429 mutex_lock(&entry->lock); 430 if (READ_ONCE(work->state) == KSMBD_WORK_CANCELLED) { 431 err = -ECANCELED; 432 goto err_put; 433 } 434 if (ksmbd_conn_exiting(work->conn) || 435 ksmbd_conn_releasing(work->conn) || 436 READ_ONCE(work->sess->tearing_down)) { 437 err = -ESHUTDOWN; 438 goto err_put; 439 } 440 if (READ_ONCE(entry->closed)) { 441 err = -EBADF; 442 goto err_put; 443 } 444 return entry; 445 446 err_put: 447 ksmbd_session_rpc_put(entry); 448 return ERR_PTR(err); 449 } 450 451 void ksmbd_session_rpc_put(struct ksmbd_session_rpc *entry) 452 { 453 mutex_unlock(&entry->lock); 454 ksmbd_session_rpc_free(entry); 455 } 456 457 int ksmbd_session_rpc_method(struct ksmbd_session *sess, int id) 458 { 459 struct ksmbd_session_rpc *entry; 460 461 lockdep_assert_held(&sess->rpc_lock); 462 entry = xa_load(&sess->rpc_handle_list, id); 463 464 return entry ? entry->method : 0; 465 } 466 467 void ksmbd_session_destroy(struct ksmbd_session *sess) 468 { 469 struct ksmbd_conn *setup_conn; 470 471 if (!sess) 472 return; 473 474 setup_conn = sess->setup_conn; 475 delete_proc_session(sess); 476 ksmbd_tree_conn_session_logoff(sess); 477 ksmbd_destroy_file_table(sess); 478 if (sess->user) 479 ksmbd_free_user(sess->user); 480 ksmbd_launch_ksmbd_durable_scavenger(); 481 ksmbd_session_rpc_clear_list(sess); 482 free_channel_list(sess); 483 kfree_sensitive(sess->Preauth_HashValue); 484 if (sess->id) 485 ksmbd_release_id(&session_ida, sess->id); 486 ida_destroy(&sess->tree_conn_ida); 487 kfree_sensitive(sess); 488 if (setup_conn) { 489 ksmbd_session_release_slot(setup_conn); 490 ksmbd_conn_put(setup_conn); 491 } 492 } 493 494 static void ksmbd_session_remove_from_table(struct ksmbd_session *sess) 495 { 496 hash_del(&sess->hlist); 497 ksmbd_counter_dec(KSMBD_COUNTER_SESSIONS); 498 } 499 500 static void ksmbd_dispose_sessions(struct list_head *dispose) 501 { 502 struct ksmbd_session *sess, *tmp; 503 504 list_for_each_entry_safe(sess, tmp, dispose, dispose_entry) { 505 list_del_init(&sess->dispose_entry); 506 delete_proc_session(sess); 507 ksmbd_user_session_put(sess); 508 } 509 } 510 511 struct ksmbd_session *__session_lookup(unsigned long long id) 512 { 513 struct ksmbd_session *sess; 514 515 hash_for_each_possible(sessions_table, sess, hlist, id) { 516 if (id == sess->id) { 517 sess->last_active = jiffies; 518 return sess; 519 } 520 } 521 return NULL; 522 } 523 524 bool ksmbd_too_many_session_setups(struct ksmbd_conn *conn) 525 { 526 LIST_HEAD(dispose); 527 unsigned long id; 528 struct ksmbd_session *sess; 529 unsigned int pending = 0; 530 531 down_write(&sessions_table_lock); 532 down_write(&conn->session_lock); 533 xa_for_each(&conn->sessions, id, sess) { 534 if (READ_ONCE(sess->state) != SMB2_SESSION_IN_PROGRESS) 535 continue; 536 537 /* Keep response signing keys until every request has dropped its reference. */ 538 if (atomic_read(&sess->refcnt) == 1 && 539 time_after(jiffies, sess->last_active + 540 KSMBD_UNAUTHENTICATED_CONN_TIMEOUT)) { 541 xa_erase(&conn->sessions, sess->id); 542 ksmbd_session_remove_from_table(sess); 543 list_add_tail(&sess->dispose_entry, &dispose); 544 continue; 545 } 546 pending++; 547 } 548 up_write(&conn->session_lock); 549 up_write(&sessions_table_lock); 550 ksmbd_dispose_sessions(&dispose); 551 return pending >= KSMBD_MAX_PENDING_SESSIONS; 552 } 553 554 int ksmbd_session_register(struct ksmbd_conn *conn, 555 struct ksmbd_session *sess) 556 { 557 int ret; 558 559 sess->dialect = conn->dialect; 560 memcpy(sess->ClientGUID, conn->ClientGUID, SMB2_CLIENT_GUID_SIZE); 561 ret = xa_err(xa_store(&conn->sessions, sess->id, sess, 562 KSMBD_DEFAULT_GFP)); 563 if (ret) { 564 down_write(&sessions_table_lock); 565 ksmbd_session_remove_from_table(sess); 566 up_write(&sessions_table_lock); 567 delete_proc_session(sess); 568 ksmbd_user_session_put(sess); 569 } 570 571 return ret; 572 } 573 574 /* The caller holds sessions_table_lock for writing. */ 575 static bool __ksmbd_session_unregister(struct ksmbd_conn *conn, 576 struct ksmbd_session *sess) 577 { 578 struct ksmbd_conn *session_conns[KSMBD_MAX_CHANNELS]; 579 struct channel *chann; 580 unsigned long index; 581 unsigned int nr_conns = 0, i; 582 bool removed = false; 583 584 lockdep_assert_held_write(&sessions_table_lock); 585 if (!hlist_unhashed(&sess->hlist)) { 586 /* Existing references stay valid under sessions_table_lock. */ 587 down_read(&sess->chann_lock); 588 /* SMB2.0/2.1 sessions have no channel holding their connection. */ 589 if (sess->setup_conn) 590 session_conns[nr_conns++] = sess->setup_conn; 591 xa_for_each(&sess->ksmbd_chann_list, index, chann) { 592 if (nr_conns == ARRAY_SIZE(session_conns)) 593 break; 594 session_conns[nr_conns++] = chann->conn; 595 } 596 up_read(&sess->chann_lock); 597 598 ksmbd_session_remove_from_table(sess); 599 removed = true; 600 } 601 602 if (conn) { 603 down_write(&conn->session_lock); 604 if (xa_load(&conn->sessions, sess->id) == sess) 605 xa_erase(&conn->sessions, sess->id); 606 up_write(&conn->session_lock); 607 } 608 for (i = 0; i < nr_conns; i++) { 609 if (session_conns[i] == conn) 610 continue; 611 down_write(&session_conns[i]->session_lock); 612 if (xa_load(&session_conns[i]->sessions, sess->id) == sess) 613 xa_erase(&session_conns[i]->sessions, sess->id); 614 up_write(&session_conns[i]->session_lock); 615 } 616 return removed; 617 } 618 619 void ksmbd_session_unregister(struct ksmbd_conn *conn, 620 struct ksmbd_session *sess) 621 { 622 bool removed; 623 624 down_write(&sessions_table_lock); 625 removed = __ksmbd_session_unregister(conn, sess); 626 up_write(&sessions_table_lock); 627 628 if (removed) { 629 /* 630 * Keep the table reference until procfs readers have finished, 631 * so none can remove its own proc entry on the last put. 632 */ 633 delete_proc_session(sess); 634 ksmbd_user_session_put(sess); 635 } 636 } 637 638 void ksmbd_reclaim_ended_sessions(void) 639 { 640 LIST_HEAD(dispose); 641 struct ksmbd_session *sess; 642 struct hlist_node *tmp; 643 int bkt; 644 645 down_write(&sessions_table_lock); 646 hash_for_each_safe(sessions_table, bkt, tmp, sess, hlist) { 647 if (READ_ONCE(sess->state) != SMB2_SESSION_EXPIRED || 648 READ_ONCE(sess->kerberos_expiry) || 649 atomic_read(&sess->refcnt) != 1) 650 continue; 651 652 __ksmbd_session_unregister(NULL, sess); 653 list_add_tail(&sess->dispose_entry, &dispose); 654 } 655 up_write(&sessions_table_lock); 656 ksmbd_dispose_sessions(&dispose); 657 } 658 659 bool ksmbd_conn_has_valid_or_expired_session(struct ksmbd_conn *conn) 660 { 661 struct ksmbd_session *sess; 662 unsigned long id; 663 int state, bkt; 664 bool found = false; 665 666 /* 667 * The expiration worker holds conn_list_lock, which session teardown 668 * takes after the session locks. Defer scavenging if either is busy. 669 */ 670 if (!down_read_trylock(&conn->session_lock)) 671 return true; 672 xa_for_each(&conn->sessions, id, sess) { 673 state = READ_ONCE(sess->state); 674 if (state == SMB2_SESSION_VALID || 675 state == SMB2_SESSION_EXPIRED) { 676 found = true; 677 break; 678 } 679 } 680 up_read(&conn->session_lock); 681 if (found) 682 return true; 683 684 /* A session bound through SMB3 multichannel is not in conn->sessions. */ 685 if (!down_read_trylock(&sessions_table_lock)) 686 return true; 687 hash_for_each(sessions_table, bkt, sess, hlist) { 688 state = READ_ONCE(sess->state); 689 if (state != SMB2_SESSION_VALID && 690 state != SMB2_SESSION_EXPIRED) 691 continue; 692 693 down_read(&sess->chann_lock); 694 found = xa_load(&sess->ksmbd_chann_list, (long)conn); 695 up_read(&sess->chann_lock); 696 if (found) 697 break; 698 } 699 up_read(&sessions_table_lock); 700 return found; 701 } 702 703 void ksmbd_expire_sessions(void) 704 { 705 struct ksmbd_session *sess; 706 u64 now = ktime_get_real_seconds(); 707 int bkt; 708 709 down_read(&sessions_table_lock); 710 hash_for_each(sessions_table, bkt, sess, hlist) { 711 if (READ_ONCE(sess->state) != SMB2_SESSION_VALID || 712 !sess->kerberos_expiry || now < sess->kerberos_expiry) 713 continue; 714 715 if (cmpxchg(&sess->state, SMB2_SESSION_VALID, 716 SMB2_SESSION_EXPIRED) == SMB2_SESSION_VALID) 717 ksmbd_counter_inc(KSMBD_COUNTER_SESSION_TIMEOUTS); 718 } 719 up_read(&sessions_table_lock); 720 } 721 722 static int ksmbd_chann_del(struct ksmbd_conn *conn, struct ksmbd_session *sess) 723 { 724 struct channel *chann; 725 726 down_write(&sess->chann_lock); 727 chann = xa_erase(&sess->ksmbd_chann_list, (long)conn); 728 up_write(&sess->chann_lock); 729 if (!chann) 730 return -ENOENT; 731 732 ksmbd_session_release_slot(chann->conn); 733 ksmbd_conn_put(chann->conn); 734 kfree_sensitive(chann); 735 return 0; 736 } 737 738 void ksmbd_conn_sessions_cleanup(struct ksmbd_conn *conn) 739 { 740 LIST_HEAD(dispose); 741 struct ksmbd_session *sess; 742 unsigned long id; 743 struct hlist_node *tmp; 744 int bkt; 745 746 down_write(&sessions_table_lock); 747 hash_for_each_safe(sessions_table, bkt, tmp, sess, hlist) { 748 if (!ksmbd_chann_del(conn, sess) && 749 xa_empty(&sess->ksmbd_chann_list)) { 750 ksmbd_session_remove_from_table(sess); 751 down_write(&conn->session_lock); 752 xa_erase(&conn->sessions, sess->id); 753 up_write(&conn->session_lock); 754 list_add_tail(&sess->dispose_entry, &dispose); 755 } 756 } 757 758 down_write(&conn->session_lock); 759 xa_for_each(&conn->sessions, id, sess) { 760 ksmbd_chann_del(conn, sess); 761 if (xa_empty(&sess->ksmbd_chann_list)) { 762 xa_erase(&conn->sessions, sess->id); 763 ksmbd_session_remove_from_table(sess); 764 list_add_tail(&sess->dispose_entry, &dispose); 765 } 766 } 767 up_write(&conn->session_lock); 768 up_write(&sessions_table_lock); 769 ksmbd_dispose_sessions(&dispose); 770 } 771 772 bool is_ksmbd_session_in_connection(struct ksmbd_conn *conn, 773 unsigned long long id) 774 { 775 struct ksmbd_session *sess; 776 777 down_read(&conn->session_lock); 778 sess = xa_load(&conn->sessions, id); 779 if (sess) { 780 up_read(&conn->session_lock); 781 return true; 782 } 783 up_read(&conn->session_lock); 784 785 return false; 786 } 787 788 struct ksmbd_session *ksmbd_session_lookup(struct ksmbd_conn *conn, 789 unsigned long long id) 790 { 791 struct ksmbd_session *sess; 792 793 down_read(&conn->session_lock); 794 sess = xa_load(&conn->sessions, id); 795 if (sess) { 796 sess->last_active = jiffies; 797 ksmbd_user_session_get(sess); 798 } 799 up_read(&conn->session_lock); 800 return sess; 801 } 802 803 struct ksmbd_session *ksmbd_session_lookup_slowpath(unsigned long long id) 804 { 805 struct ksmbd_session *sess; 806 807 down_read(&sessions_table_lock); 808 sess = __session_lookup(id); 809 if (sess) 810 ksmbd_user_session_get(sess); 811 up_read(&sessions_table_lock); 812 813 return sess; 814 } 815 816 /* The caller holds a session reference and must put each returned connection. */ 817 unsigned int ksmbd_session_get_channels(struct ksmbd_session *sess, 818 struct ksmbd_conn **conns, 819 unsigned int max_conns) 820 { 821 struct channel *chann; 822 unsigned long index; 823 unsigned int nr_conns = 0; 824 825 /* 826 * Channel connections are borrowed. Unregister can leave the channel 827 * list in a pinned session after removing it from the registry, so 828 * chann_lock alone does not keep those connections alive. Only collect 829 * registered sessions, excluding both unregister and connection cleanup 830 * until the connection references have been taken. 831 */ 832 down_read(&sessions_table_lock); 833 if (hlist_unhashed(&sess->hlist)) 834 goto out; 835 down_read(&sess->chann_lock); 836 xa_for_each(&sess->ksmbd_chann_list, index, chann) { 837 if (nr_conns == max_conns) 838 break; 839 conns[nr_conns++] = ksmbd_conn_get(chann->conn); 840 } 841 up_read(&sess->chann_lock); 842 out: 843 up_read(&sessions_table_lock); 844 return nr_conns; 845 } 846 847 struct ksmbd_session *ksmbd_session_lookup_all_states(struct ksmbd_conn *conn, 848 unsigned long long id) 849 { 850 struct ksmbd_session *sess; 851 bool channel_found; 852 853 sess = ksmbd_session_lookup(conn, id); 854 if (!sess) { 855 sess = ksmbd_session_lookup_slowpath(id); 856 if (!sess) 857 return NULL; 858 859 down_read(&sess->chann_lock); 860 channel_found = xa_load(&sess->ksmbd_chann_list, (long)conn); 861 up_read(&sess->chann_lock); 862 if (!channel_found) { 863 ksmbd_user_session_put(sess); 864 sess = NULL; 865 } 866 } 867 return sess; 868 } 869 870 struct ksmbd_session *ksmbd_session_lookup_all(struct ksmbd_conn *conn, 871 unsigned long long id) 872 { 873 struct ksmbd_session *sess; 874 875 sess = ksmbd_session_lookup_all_states(conn, id); 876 if (sess && sess->state != SMB2_SESSION_VALID) { 877 ksmbd_user_session_put(sess); 878 sess = NULL; 879 } 880 return sess; 881 } 882 883 void ksmbd_user_session_get(struct ksmbd_session *sess) 884 { 885 atomic_inc(&sess->refcnt); 886 } 887 888 void ksmbd_user_session_put(struct ksmbd_session *sess) 889 { 890 if (!sess) 891 return; 892 893 if (atomic_read(&sess->refcnt) <= 0) 894 WARN_ON(1); 895 else if (atomic_dec_and_test(&sess->refcnt)) 896 ksmbd_session_destroy(sess); 897 } 898 899 struct preauth_session *ksmbd_preauth_session_alloc(struct ksmbd_conn *conn, 900 u64 sess_id) 901 { 902 struct preauth_session *sess; 903 904 sess = kmalloc_obj(struct preauth_session, KSMBD_DEFAULT_GFP); 905 if (!sess) 906 return NULL; 907 908 sess->id = sess_id; 909 memcpy(sess->Preauth_HashValue, conn->preauth_info->Preauth_HashValue, 910 PREAUTH_HASHVALUE_SIZE); 911 list_add(&sess->preauth_entry, &conn->preauth_sess_table); 912 913 return sess; 914 } 915 916 void ksmbd_preauth_session_destroy(struct ksmbd_conn *conn) 917 { 918 struct preauth_session *sess, *tmp; 919 920 list_for_each_entry_safe(sess, tmp, &conn->preauth_sess_table, 921 preauth_entry) { 922 list_del(&sess->preauth_entry); 923 kfree(sess); 924 } 925 } 926 927 void destroy_previous_session(struct ksmbd_conn *conn, 928 struct ksmbd_user *user, u64 id) 929 { 930 struct ksmbd_session *prev_sess; 931 struct ksmbd_user *prev_user; 932 bool pinned = false; 933 int err; 934 935 down_write(&sessions_table_lock); 936 down_write(&conn->session_lock); 937 prev_sess = __session_lookup(id); 938 if (!prev_sess || prev_sess->state == SMB2_SESSION_EXPIRED) 939 goto out; 940 941 prev_user = prev_sess->user; 942 if (!prev_user || 943 strcmp(user->name, prev_user->name) || 944 user->passkey_sz != prev_user->passkey_sz || 945 memcmp(user->passkey, prev_user->passkey, user->passkey_sz)) 946 goto out; 947 948 down_write(&prev_sess->chann_lock); 949 if (prev_sess->tearing_down) { 950 up_write(&prev_sess->chann_lock); 951 goto out; 952 } 953 WRITE_ONCE(prev_sess->tearing_down, true); 954 up_write(&prev_sess->chann_lock); 955 ksmbd_user_session_get(prev_sess); 956 pinned = true; 957 up_write(&conn->session_lock); 958 up_write(&sessions_table_lock); 959 960 ksmbd_all_conn_set_status(prev_sess, KSMBD_SESS_NEED_RECONNECT); 961 /* 962 * The caller has finished using the connection's authentication state. 963 * Drop srv_mutex so drained requests can write their responses. 964 */ 965 ksmbd_conn_unlock(conn); 966 err = ksmbd_conn_wait_idle_sess(conn, prev_sess); 967 ksmbd_conn_lock(conn); 968 down_write(&sessions_table_lock); 969 down_write(&conn->session_lock); 970 if (err) { 971 down_write(&prev_sess->chann_lock); 972 WRITE_ONCE(prev_sess->tearing_down, false); 973 up_write(&prev_sess->chann_lock); 974 ksmbd_all_conn_set_status(prev_sess, KSMBD_SESS_GOOD); 975 goto out; 976 } 977 978 ksmbd_destroy_file_table(prev_sess); 979 prev_sess->kerberos_expiry = 0; 980 prev_sess->state = SMB2_SESSION_EXPIRED; 981 ksmbd_all_conn_set_status(prev_sess, KSMBD_SESS_NEED_SETUP); 982 ksmbd_launch_ksmbd_durable_scavenger(); 983 out: 984 up_write(&conn->session_lock); 985 up_write(&sessions_table_lock); 986 if (pinned) 987 ksmbd_user_session_put(prev_sess); 988 } 989 990 static bool ksmbd_preauth_session_id_match(struct preauth_session *sess, 991 unsigned long long id) 992 { 993 return sess->id == id; 994 } 995 996 struct preauth_session *ksmbd_preauth_session_lookup(struct ksmbd_conn *conn, 997 unsigned long long id) 998 { 999 struct preauth_session *sess = NULL; 1000 1001 list_for_each_entry(sess, &conn->preauth_sess_table, preauth_entry) { 1002 if (ksmbd_preauth_session_id_match(sess, id)) 1003 return sess; 1004 } 1005 return NULL; 1006 } 1007 1008 static int __init_smb2_session(struct ksmbd_session *sess) 1009 { 1010 int id = ksmbd_acquire_smb2_uid(&session_ida); 1011 1012 if (id < 0) 1013 return -EINVAL; 1014 sess->id = id; 1015 return 0; 1016 } 1017 1018 static struct ksmbd_session *__session_create(struct ksmbd_conn *conn, int protocol) 1019 { 1020 struct ksmbd_session *sess; 1021 int ret; 1022 1023 if (protocol != CIFDS_SESSION_FLAG_SMB2) 1024 return NULL; 1025 1026 if (ksmbd_session_reserve_slot(conn)) 1027 return NULL; 1028 1029 sess = kzalloc_obj(struct ksmbd_session, KSMBD_DEFAULT_GFP); 1030 if (!sess) { 1031 ksmbd_session_release_slot(conn); 1032 return NULL; 1033 } 1034 1035 sess->setup_conn = ksmbd_conn_get(conn); 1036 ida_init(&sess->tree_conn_ida); 1037 sess->last_active = jiffies; 1038 sess->state = SMB2_SESSION_IN_PROGRESS; 1039 set_session_flag(sess, protocol); 1040 xa_init(&sess->tree_conns); 1041 xa_init(&sess->ksmbd_chann_list); 1042 xa_init(&sess->rpc_handle_list); 1043 INIT_LIST_HEAD(&sess->dispose_entry); 1044 sess->sequence_number = 1; 1045 atomic_set(&sess->refcnt, 2); 1046 init_rwsem(&sess->tree_conns_lock); 1047 init_rwsem(&sess->rpc_lock); 1048 init_rwsem(&sess->chann_lock); 1049 1050 if (ksmbd_init_file_table(&sess->file_table)) 1051 goto error; 1052 1053 ret = __init_smb2_session(sess); 1054 if (ret) 1055 goto error; 1056 1057 down_write(&sessions_table_lock); 1058 hash_add(sessions_table, &sess->hlist, sess->id); 1059 ksmbd_counter_inc(KSMBD_COUNTER_SESSIONS); 1060 up_write(&sessions_table_lock); 1061 1062 if (create_proc_session(sess)) 1063 pr_warn_ratelimited("Unable to create session %llu procfs entry\n", sess->id); 1064 return sess; 1065 1066 error: 1067 ksmbd_session_destroy(sess); 1068 return NULL; 1069 } 1070 1071 struct ksmbd_session *ksmbd_smb2_session_create(struct ksmbd_conn *conn) 1072 { 1073 return __session_create(conn, CIFDS_SESSION_FLAG_SMB2); 1074 } 1075 1076 int ksmbd_acquire_tree_conn_id(struct ksmbd_session *sess) 1077 { 1078 int id = -EINVAL; 1079 1080 if (test_session_flag(sess, CIFDS_SESSION_FLAG_SMB2)) 1081 id = ksmbd_acquire_smb2_tid(&sess->tree_conn_ida); 1082 1083 return id; 1084 } 1085 1086 void ksmbd_release_tree_conn_id(struct ksmbd_session *sess, int id) 1087 { 1088 if (id >= 0) 1089 ksmbd_release_id(&sess->tree_conn_ida, id); 1090 } 1091