~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/fs/smb/server/mgmt/user_session.c

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  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 
 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 
 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 
 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 
 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 
 91 int ksmbd_session_rpc_open(struct ksmbd_session *sess, char *rpc_name)
 92 {
 93         struct ksmbd_session_rpc *entry;
 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         xa_store(&sess->rpc_handle_list, entry->id, entry, GFP_KERNEL);
110 
111         resp = ksmbd_rpc_open(sess, entry->id);
112         if (!resp)
113                 goto free_id;
114 
115         kvfree(resp);
116         return entry->id;
117 free_id:
118         xa_erase(&sess->rpc_handle_list, entry->id);
119         ksmbd_rpc_id_free(entry->id);
120 free_entry:
121         kfree(entry);
122         return -EINVAL;
123 }
124 
125 void ksmbd_session_rpc_close(struct ksmbd_session *sess, int id)
126 {
127         struct ksmbd_session_rpc *entry;
128 
129         entry = xa_erase(&sess->rpc_handle_list, id);
130         if (entry)
131                 __session_rpc_close(sess, entry);
132 }
133 
134 int ksmbd_session_rpc_method(struct ksmbd_session *sess, int id)
135 {
136         struct ksmbd_session_rpc *entry;
137 
138         entry = xa_load(&sess->rpc_handle_list, id);
139         return entry ? entry->method : 0;
140 }
141 
142 void ksmbd_session_destroy(struct ksmbd_session *sess)
143 {
144         if (!sess)
145                 return;
146 
147         if (sess->user)
148                 ksmbd_free_user(sess->user);
149 
150         ksmbd_tree_conn_session_logoff(sess);
151         ksmbd_destroy_file_table(&sess->file_table);
152         ksmbd_launch_ksmbd_durable_scavenger();
153         ksmbd_session_rpc_clear_list(sess);
154         free_channel_list(sess);
155         kfree(sess->Preauth_HashValue);
156         ksmbd_release_id(&session_ida, sess->id);
157         kfree(sess);
158 }
159 
160 struct ksmbd_session *__session_lookup(unsigned long long id)
161 {
162         struct ksmbd_session *sess;
163 
164         hash_for_each_possible(sessions_table, sess, hlist, id) {
165                 if (id == sess->id) {
166                         sess->last_active = jiffies;
167                         return sess;
168                 }
169         }
170         return NULL;
171 }
172 
173 static void ksmbd_expire_session(struct ksmbd_conn *conn)
174 {
175         unsigned long id;
176         struct ksmbd_session *sess;
177 
178         down_write(&conn->session_lock);
179         xa_for_each(&conn->sessions, id, sess) {
180                 if (atomic_read(&sess->refcnt) == 0 &&
181                     (sess->state != SMB2_SESSION_VALID ||
182                      time_after(jiffies,
183                                sess->last_active + SMB2_SESSION_TIMEOUT))) {
184                         xa_erase(&conn->sessions, sess->id);
185                         hash_del(&sess->hlist);
186                         ksmbd_session_destroy(sess);
187                         continue;
188                 }
189         }
190         up_write(&conn->session_lock);
191 }
192 
193 int ksmbd_session_register(struct ksmbd_conn *conn,
194                            struct ksmbd_session *sess)
195 {
196         sess->dialect = conn->dialect;
197         memcpy(sess->ClientGUID, conn->ClientGUID, SMB2_CLIENT_GUID_SIZE);
198         ksmbd_expire_session(conn);
199         return xa_err(xa_store(&conn->sessions, sess->id, sess, GFP_KERNEL));
200 }
201 
202 static int ksmbd_chann_del(struct ksmbd_conn *conn, struct ksmbd_session *sess)
203 {
204         struct channel *chann;
205 
206         chann = xa_erase(&sess->ksmbd_chann_list, (long)conn);
207         if (!chann)
208                 return -ENOENT;
209 
210         kfree(chann);
211         return 0;
212 }
213 
214 void ksmbd_sessions_deregister(struct ksmbd_conn *conn)
215 {
216         struct ksmbd_session *sess;
217         unsigned long id;
218 
219         down_write(&sessions_table_lock);
220         if (conn->binding) {
221                 int bkt;
222                 struct hlist_node *tmp;
223 
224                 hash_for_each_safe(sessions_table, bkt, tmp, sess, hlist) {
225                         if (!ksmbd_chann_del(conn, sess) &&
226                             xa_empty(&sess->ksmbd_chann_list)) {
227                                 hash_del(&sess->hlist);
228                                 ksmbd_session_destroy(sess);
229                         }
230                 }
231         }
232         up_write(&sessions_table_lock);
233 
234         down_write(&conn->session_lock);
235         xa_for_each(&conn->sessions, id, sess) {
236                 unsigned long chann_id;
237                 struct channel *chann;
238 
239                 xa_for_each(&sess->ksmbd_chann_list, chann_id, chann) {
240                         if (chann->conn != conn)
241                                 ksmbd_conn_set_exiting(chann->conn);
242                 }
243 
244                 ksmbd_chann_del(conn, sess);
245                 if (xa_empty(&sess->ksmbd_chann_list)) {
246                         xa_erase(&conn->sessions, sess->id);
247                         hash_del(&sess->hlist);
248                         ksmbd_session_destroy(sess);
249                 }
250         }
251         up_write(&conn->session_lock);
252 }
253 
254 struct ksmbd_session *ksmbd_session_lookup(struct ksmbd_conn *conn,
255                                            unsigned long long id)
256 {
257         struct ksmbd_session *sess;
258 
259         down_read(&conn->session_lock);
260         sess = xa_load(&conn->sessions, id);
261         if (sess)
262                 sess->last_active = jiffies;
263         up_read(&conn->session_lock);
264         return sess;
265 }
266 
267 struct ksmbd_session *ksmbd_session_lookup_slowpath(unsigned long long id)
268 {
269         struct ksmbd_session *sess;
270 
271         down_read(&sessions_table_lock);
272         sess = __session_lookup(id);
273         up_read(&sessions_table_lock);
274 
275         return sess;
276 }
277 
278 struct ksmbd_session *ksmbd_session_lookup_all(struct ksmbd_conn *conn,
279                                                unsigned long long id)
280 {
281         struct ksmbd_session *sess;
282 
283         sess = ksmbd_session_lookup(conn, id);
284         if (!sess && conn->binding)
285                 sess = ksmbd_session_lookup_slowpath(id);
286         if (sess && sess->state != SMB2_SESSION_VALID)
287                 sess = NULL;
288         return sess;
289 }
290 
291 void ksmbd_user_session_get(struct ksmbd_session *sess)
292 {
293         atomic_inc(&sess->refcnt);
294 }
295 
296 void ksmbd_user_session_put(struct ksmbd_session *sess)
297 {
298         if (!sess)
299                 return;
300 
301         if (atomic_read(&sess->refcnt) <= 0)
302                 WARN_ON(1);
303         else
304                 atomic_dec(&sess->refcnt);
305 }
306 
307 struct preauth_session *ksmbd_preauth_session_alloc(struct ksmbd_conn *conn,
308                                                     u64 sess_id)
309 {
310         struct preauth_session *sess;
311 
312         sess = kmalloc(sizeof(struct preauth_session), GFP_KERNEL);
313         if (!sess)
314                 return NULL;
315 
316         sess->id = sess_id;
317         memcpy(sess->Preauth_HashValue, conn->preauth_info->Preauth_HashValue,
318                PREAUTH_HASHVALUE_SIZE);
319         list_add(&sess->preauth_entry, &conn->preauth_sess_table);
320 
321         return sess;
322 }
323 
324 void destroy_previous_session(struct ksmbd_conn *conn,
325                               struct ksmbd_user *user, u64 id)
326 {
327         struct ksmbd_session *prev_sess;
328         struct ksmbd_user *prev_user;
329         int err;
330 
331         down_write(&sessions_table_lock);
332         down_write(&conn->session_lock);
333         prev_sess = __session_lookup(id);
334         if (!prev_sess || prev_sess->state == SMB2_SESSION_EXPIRED)
335                 goto out;
336 
337         prev_user = prev_sess->user;
338         if (!prev_user ||
339             strcmp(user->name, prev_user->name) ||
340             user->passkey_sz != prev_user->passkey_sz ||
341             memcmp(user->passkey, prev_user->passkey, user->passkey_sz))
342                 goto out;
343 
344         ksmbd_all_conn_set_status(id, KSMBD_SESS_NEED_RECONNECT);
345         err = ksmbd_conn_wait_idle_sess_id(conn, id);
346         if (err) {
347                 ksmbd_all_conn_set_status(id, KSMBD_SESS_NEED_NEGOTIATE);
348                 goto out;
349         }
350 
351         ksmbd_destroy_file_table(&prev_sess->file_table);
352         prev_sess->state = SMB2_SESSION_EXPIRED;
353         ksmbd_all_conn_set_status(id, KSMBD_SESS_NEED_NEGOTIATE);
354         ksmbd_launch_ksmbd_durable_scavenger();
355 out:
356         up_write(&conn->session_lock);
357         up_write(&sessions_table_lock);
358 }
359 
360 static bool ksmbd_preauth_session_id_match(struct preauth_session *sess,
361                                            unsigned long long id)
362 {
363         return sess->id == id;
364 }
365 
366 struct preauth_session *ksmbd_preauth_session_lookup(struct ksmbd_conn *conn,
367                                                      unsigned long long id)
368 {
369         struct preauth_session *sess = NULL;
370 
371         list_for_each_entry(sess, &conn->preauth_sess_table, preauth_entry) {
372                 if (ksmbd_preauth_session_id_match(sess, id))
373                         return sess;
374         }
375         return NULL;
376 }
377 
378 static int __init_smb2_session(struct ksmbd_session *sess)
379 {
380         int id = ksmbd_acquire_smb2_uid(&session_ida);
381 
382         if (id < 0)
383                 return -EINVAL;
384         sess->id = id;
385         return 0;
386 }
387 
388 static struct ksmbd_session *__session_create(int protocol)
389 {
390         struct ksmbd_session *sess;
391         int ret;
392 
393         if (protocol != CIFDS_SESSION_FLAG_SMB2)
394                 return NULL;
395 
396         sess = kzalloc(sizeof(struct ksmbd_session), GFP_KERNEL);
397         if (!sess)
398                 return NULL;
399 
400         if (ksmbd_init_file_table(&sess->file_table))
401                 goto error;
402 
403         sess->last_active = jiffies;
404         sess->state = SMB2_SESSION_IN_PROGRESS;
405         set_session_flag(sess, protocol);
406         xa_init(&sess->tree_conns);
407         xa_init(&sess->ksmbd_chann_list);
408         xa_init(&sess->rpc_handle_list);
409         sess->sequence_number = 1;
410         rwlock_init(&sess->tree_conns_lock);
411         atomic_set(&sess->refcnt, 1);
412 
413         ret = __init_smb2_session(sess);
414         if (ret)
415                 goto error;
416 
417         ida_init(&sess->tree_conn_ida);
418 
419         down_write(&sessions_table_lock);
420         hash_add(sessions_table, &sess->hlist, sess->id);
421         up_write(&sessions_table_lock);
422 
423         return sess;
424 
425 error:
426         ksmbd_session_destroy(sess);
427         return NULL;
428 }
429 
430 struct ksmbd_session *ksmbd_smb2_session_create(void)
431 {
432         return __session_create(CIFDS_SESSION_FLAG_SMB2);
433 }
434 
435 int ksmbd_acquire_tree_conn_id(struct ksmbd_session *sess)
436 {
437         int id = -EINVAL;
438 
439         if (test_session_flag(sess, CIFDS_SESSION_FLAG_SMB2))
440                 id = ksmbd_acquire_smb2_tid(&sess->tree_conn_ida);
441 
442         return id;
443 }
444 
445 void ksmbd_release_tree_conn_id(struct ksmbd_session *sess, int id)
446 {
447         if (id >= 0)
448                 ksmbd_release_id(&sess->tree_conn_ida, id);
449 }
450 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php