1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (C) 2018 Samsung Electronics Co., Ltd.
4 */
5
6 #include <linux/list.h>
7 #include <linux/slab.h>
8 #include <linux/rwsem.h>
9 #include <linux/xarray.h>
10
11 #include "ksmbd_ida.h"
12 #include "user_session.h"
13 #include "user_config.h"
14 #include "tree_connect.h"
15 #include "../transport_ipc.h"
16 #include "../connection.h"
17 #include "../vfs_cache.h"
18
19 static DEFINE_IDA(session_ida);
20
21 #define SESSION_HASH_BITS 3
22 static DEFINE_HASHTABLE(sessions_table, SESSION_HASH_BITS);
23 static DECLARE_RWSEM(sessions_table_lock);
24
25 struct ksmbd_session_rpc {
26 int id;
27 unsigned int method;
28 };
29
free_channel_list(struct ksmbd_session * sess)30 static void free_channel_list(struct ksmbd_session *sess)
31 {
32 struct channel *chann;
33 unsigned long index;
34
35 xa_for_each(&sess->ksmbd_chann_list, index, chann) {
36 xa_erase(&sess->ksmbd_chann_list, index);
37 kfree(chann);
38 }
39
40 xa_destroy(&sess->ksmbd_chann_list);
41 }
42
__session_rpc_close(struct ksmbd_session * sess,struct ksmbd_session_rpc * entry)43 static void __session_rpc_close(struct ksmbd_session *sess,
44 struct ksmbd_session_rpc *entry)
45 {
46 struct ksmbd_rpc_command *resp;
47
48 resp = ksmbd_rpc_close(sess, entry->id);
49 if (!resp)
50 pr_err("Unable to close RPC pipe %d\n", entry->id);
51
52 kvfree(resp);
53 ksmbd_rpc_id_free(entry->id);
54 kfree(entry);
55 }
56
ksmbd_session_rpc_clear_list(struct ksmbd_session * sess)57 static void ksmbd_session_rpc_clear_list(struct ksmbd_session *sess)
58 {
59 struct ksmbd_session_rpc *entry;
60 long index;
61
62 xa_for_each(&sess->rpc_handle_list, index, entry) {
63 xa_erase(&sess->rpc_handle_list, index);
64 __session_rpc_close(sess, entry);
65 }
66
67 xa_destroy(&sess->rpc_handle_list);
68 }
69
__rpc_method(char * rpc_name)70 static int __rpc_method(char *rpc_name)
71 {
72 if (!strcmp(rpc_name, "\\srvsvc") || !strcmp(rpc_name, "srvsvc"))
73 return KSMBD_RPC_SRVSVC_METHOD_INVOKE;
74
75 if (!strcmp(rpc_name, "\\wkssvc") || !strcmp(rpc_name, "wkssvc"))
76 return KSMBD_RPC_WKSSVC_METHOD_INVOKE;
77
78 if (!strcmp(rpc_name, "LANMAN") || !strcmp(rpc_name, "lanman"))
79 return KSMBD_RPC_RAP_METHOD;
80
81 if (!strcmp(rpc_name, "\\samr") || !strcmp(rpc_name, "samr"))
82 return KSMBD_RPC_SAMR_METHOD_INVOKE;
83
84 if (!strcmp(rpc_name, "\\lsarpc") || !strcmp(rpc_name, "lsarpc"))
85 return KSMBD_RPC_LSARPC_METHOD_INVOKE;
86
87 pr_err("Unsupported RPC: %s\n", rpc_name);
88 return 0;
89 }
90
ksmbd_session_rpc_open(struct ksmbd_session * sess,char * rpc_name)91 int ksmbd_session_rpc_open(struct ksmbd_session *sess, char *rpc_name)
92 {
93 struct ksmbd_session_rpc *entry, *old;
94 struct ksmbd_rpc_command *resp;
95 int method;
96
97 method = __rpc_method(rpc_name);
98 if (!method)
99 return -EINVAL;
100
101 entry = kzalloc(sizeof(struct ksmbd_session_rpc), GFP_KERNEL);
102 if (!entry)
103 return -ENOMEM;
104
105 entry->method = method;
106 entry->id = ksmbd_ipc_id_alloc();
107 if (entry->id < 0)
108 goto free_entry;
109 old = xa_store(&sess->rpc_handle_list, entry->id, entry, GFP_KERNEL);
110 if (xa_is_err(old))
111 goto free_id;
112
113 resp = ksmbd_rpc_open(sess, entry->id);
114 if (!resp)
115 goto erase_xa;
116
117 kvfree(resp);
118 return entry->id;
119 erase_xa:
120 xa_erase(&sess->rpc_handle_list, entry->id);
121 free_id:
122 ksmbd_rpc_id_free(entry->id);
123 free_entry:
124 kfree(entry);
125 return -EINVAL;
126 }
127
ksmbd_session_rpc_close(struct ksmbd_session * sess,int id)128 void ksmbd_session_rpc_close(struct ksmbd_session *sess, int id)
129 {
130 struct ksmbd_session_rpc *entry;
131
132 entry = xa_erase(&sess->rpc_handle_list, id);
133 if (entry)
134 __session_rpc_close(sess, entry);
135 }
136
ksmbd_session_rpc_method(struct ksmbd_session * sess,int id)137 int ksmbd_session_rpc_method(struct ksmbd_session *sess, int id)
138 {
139 struct ksmbd_session_rpc *entry;
140
141 entry = xa_load(&sess->rpc_handle_list, id);
142 return entry ? entry->method : 0;
143 }
144
ksmbd_session_destroy(struct ksmbd_session * sess)145 void ksmbd_session_destroy(struct ksmbd_session *sess)
146 {
147 if (!sess)
148 return;
149
150 if (sess->user)
151 ksmbd_free_user(sess->user);
152
153 ksmbd_tree_conn_session_logoff(sess);
154 ksmbd_destroy_file_table(&sess->file_table);
155 ksmbd_session_rpc_clear_list(sess);
156 free_channel_list(sess);
157 kfree(sess->Preauth_HashValue);
158 ksmbd_release_id(&session_ida, sess->id);
159 kfree(sess);
160 }
161
__session_lookup(unsigned long long id)162 struct ksmbd_session *__session_lookup(unsigned long long id)
163 {
164 struct ksmbd_session *sess;
165
166 hash_for_each_possible(sessions_table, sess, hlist, id) {
167 if (id == sess->id) {
168 sess->last_active = jiffies;
169 return sess;
170 }
171 }
172 return NULL;
173 }
174
ksmbd_expire_session(struct ksmbd_conn * conn)175 static void ksmbd_expire_session(struct ksmbd_conn *conn)
176 {
177 unsigned long id;
178 struct ksmbd_session *sess;
179
180 down_write(&sessions_table_lock);
181 down_write(&conn->session_lock);
182 xa_for_each(&conn->sessions, id, sess) {
183 if (atomic_read(&sess->refcnt) == 0 &&
184 (sess->state != SMB2_SESSION_VALID ||
185 time_after(jiffies,
186 sess->last_active + SMB2_SESSION_TIMEOUT))) {
187 xa_erase(&conn->sessions, sess->id);
188 hash_del(&sess->hlist);
189 ksmbd_session_destroy(sess);
190 continue;
191 }
192 }
193 up_write(&conn->session_lock);
194 up_write(&sessions_table_lock);
195 }
196
ksmbd_session_register(struct ksmbd_conn * conn,struct ksmbd_session * sess)197 int ksmbd_session_register(struct ksmbd_conn *conn,
198 struct ksmbd_session *sess)
199 {
200 sess->dialect = conn->dialect;
201 memcpy(sess->ClientGUID, conn->ClientGUID, SMB2_CLIENT_GUID_SIZE);
202 ksmbd_expire_session(conn);
203 return xa_err(xa_store(&conn->sessions, sess->id, sess, GFP_KERNEL));
204 }
205
ksmbd_chann_del(struct ksmbd_conn * conn,struct ksmbd_session * sess)206 static int ksmbd_chann_del(struct ksmbd_conn *conn, struct ksmbd_session *sess)
207 {
208 struct channel *chann;
209
210 chann = xa_erase(&sess->ksmbd_chann_list, (long)conn);
211 if (!chann)
212 return -ENOENT;
213
214 kfree(chann);
215 return 0;
216 }
217
ksmbd_sessions_deregister(struct ksmbd_conn * conn)218 void ksmbd_sessions_deregister(struct ksmbd_conn *conn)
219 {
220 struct ksmbd_session *sess;
221 unsigned long id;
222
223 down_write(&sessions_table_lock);
224 if (conn->binding) {
225 int bkt;
226 struct hlist_node *tmp;
227
228 hash_for_each_safe(sessions_table, bkt, tmp, sess, hlist) {
229 if (!ksmbd_chann_del(conn, sess) &&
230 xa_empty(&sess->ksmbd_chann_list)) {
231 hash_del(&sess->hlist);
232 ksmbd_session_destroy(sess);
233 }
234 }
235 }
236
237 down_write(&conn->session_lock);
238 xa_for_each(&conn->sessions, id, sess) {
239 unsigned long chann_id;
240 struct channel *chann;
241
242 xa_for_each(&sess->ksmbd_chann_list, chann_id, chann) {
243 if (chann->conn != conn)
244 ksmbd_conn_set_exiting(chann->conn);
245 }
246
247 ksmbd_chann_del(conn, sess);
248 if (xa_empty(&sess->ksmbd_chann_list)) {
249 xa_erase(&conn->sessions, sess->id);
250 hash_del(&sess->hlist);
251 ksmbd_session_destroy(sess);
252 }
253 }
254 up_write(&conn->session_lock);
255 up_write(&sessions_table_lock);
256 }
257
ksmbd_session_lookup(struct ksmbd_conn * conn,unsigned long long id)258 struct ksmbd_session *ksmbd_session_lookup(struct ksmbd_conn *conn,
259 unsigned long long id)
260 {
261 struct ksmbd_session *sess;
262
263 down_read(&conn->session_lock);
264 sess = xa_load(&conn->sessions, id);
265 if (sess)
266 sess->last_active = jiffies;
267 up_read(&conn->session_lock);
268 return sess;
269 }
270
ksmbd_session_lookup_slowpath(unsigned long long id)271 struct ksmbd_session *ksmbd_session_lookup_slowpath(unsigned long long id)
272 {
273 struct ksmbd_session *sess;
274
275 down_read(&sessions_table_lock);
276 sess = __session_lookup(id);
277 up_read(&sessions_table_lock);
278
279 return sess;
280 }
281
ksmbd_session_lookup_all(struct ksmbd_conn * conn,unsigned long long id)282 struct ksmbd_session *ksmbd_session_lookup_all(struct ksmbd_conn *conn,
283 unsigned long long id)
284 {
285 struct ksmbd_session *sess;
286
287 sess = ksmbd_session_lookup(conn, id);
288 if (!sess && conn->binding)
289 sess = ksmbd_session_lookup_slowpath(id);
290 if (sess && sess->state != SMB2_SESSION_VALID)
291 sess = NULL;
292 return sess;
293 }
294
ksmbd_user_session_get(struct ksmbd_session * sess)295 void ksmbd_user_session_get(struct ksmbd_session *sess)
296 {
297 atomic_inc(&sess->refcnt);
298 }
299
ksmbd_user_session_put(struct ksmbd_session * sess)300 void ksmbd_user_session_put(struct ksmbd_session *sess)
301 {
302 if (!sess)
303 return;
304
305 if (atomic_read(&sess->refcnt) <= 0)
306 WARN_ON(1);
307 else
308 atomic_dec(&sess->refcnt);
309 }
310
ksmbd_preauth_session_alloc(struct ksmbd_conn * conn,u64 sess_id)311 struct preauth_session *ksmbd_preauth_session_alloc(struct ksmbd_conn *conn,
312 u64 sess_id)
313 {
314 struct preauth_session *sess;
315
316 sess = kmalloc(sizeof(struct preauth_session), GFP_KERNEL);
317 if (!sess)
318 return NULL;
319
320 sess->id = sess_id;
321 memcpy(sess->Preauth_HashValue, conn->preauth_info->Preauth_HashValue,
322 PREAUTH_HASHVALUE_SIZE);
323 list_add(&sess->preauth_entry, &conn->preauth_sess_table);
324
325 return sess;
326 }
327
destroy_previous_session(struct ksmbd_conn * conn,struct ksmbd_user * user,u64 id)328 void destroy_previous_session(struct ksmbd_conn *conn,
329 struct ksmbd_user *user, u64 id)
330 {
331 struct ksmbd_session *prev_sess;
332 struct ksmbd_user *prev_user;
333 int err;
334
335 down_write(&sessions_table_lock);
336 down_write(&conn->session_lock);
337 prev_sess = __session_lookup(id);
338 if (!prev_sess || prev_sess->state == SMB2_SESSION_EXPIRED)
339 goto out;
340
341 prev_user = prev_sess->user;
342 if (!prev_user ||
343 strcmp(user->name, prev_user->name) ||
344 user->passkey_sz != prev_user->passkey_sz ||
345 memcmp(user->passkey, prev_user->passkey, user->passkey_sz))
346 goto out;
347
348 ksmbd_all_conn_set_status(id, KSMBD_SESS_NEED_RECONNECT);
349 err = ksmbd_conn_wait_idle_sess_id(conn, id);
350 if (err) {
351 ksmbd_all_conn_set_status(id, KSMBD_SESS_NEED_NEGOTIATE);
352 goto out;
353 }
354 ksmbd_destroy_file_table(&prev_sess->file_table);
355 prev_sess->state = SMB2_SESSION_EXPIRED;
356 ksmbd_all_conn_set_status(id, KSMBD_SESS_NEED_NEGOTIATE);
357 out:
358 up_write(&conn->session_lock);
359 up_write(&sessions_table_lock);
360 }
361
ksmbd_preauth_session_id_match(struct preauth_session * sess,unsigned long long id)362 static bool ksmbd_preauth_session_id_match(struct preauth_session *sess,
363 unsigned long long id)
364 {
365 return sess->id == id;
366 }
367
ksmbd_preauth_session_lookup(struct ksmbd_conn * conn,unsigned long long id)368 struct preauth_session *ksmbd_preauth_session_lookup(struct ksmbd_conn *conn,
369 unsigned long long id)
370 {
371 struct preauth_session *sess = NULL;
372
373 list_for_each_entry(sess, &conn->preauth_sess_table, preauth_entry) {
374 if (ksmbd_preauth_session_id_match(sess, id))
375 return sess;
376 }
377 return NULL;
378 }
379
__init_smb2_session(struct ksmbd_session * sess)380 static int __init_smb2_session(struct ksmbd_session *sess)
381 {
382 int id = ksmbd_acquire_smb2_uid(&session_ida);
383
384 if (id < 0)
385 return -EINVAL;
386 sess->id = id;
387 return 0;
388 }
389
__session_create(int protocol)390 static struct ksmbd_session *__session_create(int protocol)
391 {
392 struct ksmbd_session *sess;
393 int ret;
394
395 if (protocol != CIFDS_SESSION_FLAG_SMB2)
396 return NULL;
397
398 sess = kzalloc(sizeof(struct ksmbd_session), GFP_KERNEL);
399 if (!sess)
400 return NULL;
401
402 if (ksmbd_init_file_table(&sess->file_table))
403 goto error;
404
405 sess->last_active = jiffies;
406 sess->state = SMB2_SESSION_IN_PROGRESS;
407 set_session_flag(sess, protocol);
408 xa_init(&sess->tree_conns);
409 xa_init(&sess->ksmbd_chann_list);
410 xa_init(&sess->rpc_handle_list);
411 sess->sequence_number = 1;
412 rwlock_init(&sess->tree_conns_lock);
413 atomic_set(&sess->refcnt, 1);
414
415 ret = __init_smb2_session(sess);
416 if (ret)
417 goto error;
418
419 ida_init(&sess->tree_conn_ida);
420
421 down_write(&sessions_table_lock);
422 hash_add(sessions_table, &sess->hlist, sess->id);
423 up_write(&sessions_table_lock);
424
425 return sess;
426
427 error:
428 ksmbd_session_destroy(sess);
429 return NULL;
430 }
431
ksmbd_smb2_session_create(void)432 struct ksmbd_session *ksmbd_smb2_session_create(void)
433 {
434 return __session_create(CIFDS_SESSION_FLAG_SMB2);
435 }
436
ksmbd_acquire_tree_conn_id(struct ksmbd_session * sess)437 int ksmbd_acquire_tree_conn_id(struct ksmbd_session *sess)
438 {
439 int id = -EINVAL;
440
441 if (test_session_flag(sess, CIFDS_SESSION_FLAG_SMB2))
442 id = ksmbd_acquire_smb2_tid(&sess->tree_conn_ida);
443
444 return id;
445 }
446
ksmbd_release_tree_conn_id(struct ksmbd_session * sess,int id)447 void ksmbd_release_tree_conn_id(struct ksmbd_session *sess, int id)
448 {
449 if (id >= 0)
450 ksmbd_release_id(&sess->tree_conn_ida, id);
451 }
452