-
Notifications
You must be signed in to change notification settings - Fork 64
/
smb1pdu.c
8887 lines (7877 loc) · 241 KB
/
smb1pdu.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (C) 2016 Namjae Jeon <linkinjeon@kernel.org>
* Copyright (C) 2018 Samsung Electronics Co., Ltd.
*/
#include <linux/math64.h>
#include <linux/fs.h>
#include <linux/posix_acl_xattr.h>
#include <linux/namei.h>
#include <linux/statfs.h>
#include <linux/vmalloc.h>
#include <linux/version.h>
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 3, 0)
#include <linux/filelock.h>
#endif
#include "compat.h"
#include "glob.h"
#include "oplock.h"
#include "connection.h"
#include "transport_ipc.h"
#include "vfs.h"
#include "misc.h"
#include "auth.h"
#include "asn1.h"
#include "server.h"
#include "smb_common.h"
#include "smb1pdu.h"
#include "smbstatus.h"
#include "mgmt/user_config.h"
#include "mgmt/share_config.h"
#include "mgmt/tree_connect.h"
#include "mgmt/user_session.h"
#include "ndr.h"
#include "smberr.h"
#include "unicode.h"
static const char *const smb_cmd_str[] = {
[SMB_COM_CREATE_DIRECTORY] = "SMB_COM_CREATE_DIRECTORY",
[SMB_COM_DELETE_DIRECTORY] = "SMB_COM_DELETE_DIRECTORY",
[SMB_COM_CLOSE] = "SMB_COM_CLOSE",
[SMB_COM_FLUSH] = "SMB_COM_FLUSH",
[SMB_COM_DELETE] = "SMB_COM_DELETE",
[SMB_COM_RENAME] = "SMB_COM_RENAME",
[SMB_COM_QUERY_INFORMATION] = "SMB_COM_QUERY_INFORMATION",
[SMB_COM_SETATTR] = "SMB_COM_SETATTR",
[SMB_COM_WRITE] = "SMB_COM_WRITE",
[SMB_COM_CHECK_DIRECTORY] = "SMB_COM_CHECK_DIRECTORY",
[SMB_COM_PROCESS_EXIT] = "SMB_COM_PROCESS_EXIT",
[SMB_COM_LOCKING_ANDX] = "SMB_COM_LOCKING_ANDX",
[SMB_COM_TRANSACTION] = "SMB_COM_TRANSACTION",
[SMB_COM_COPY] = "SMB_COM_COPY",
[SMB_COM_ECHO] = "SMB_COM_ECHO",
[SMB_COM_OPEN_ANDX] = "SMB_COM_OPEN_ANDX",
[SMB_COM_READ_ANDX] = "SMB_COM_READ_ANDX",
[SMB_COM_WRITE_ANDX] = "SMB_COM_WRITE_ANDX",
[SMB_COM_TRANSACTION2] = "SMB_COM_TRANSACTION2",
[SMB_COM_TRANSACTION2_SECONDARY] = "SMB_COM_TRANSACTION2_SECONDARY",
[SMB_COM_FIND_CLOSE2] = "SMB_COM_FIND_CLOSE2",
[SMB_COM_TREE_DISCONNECT] = "SMB_COM_TREE_DISCONNECT",
[SMB_COM_NEGOTIATE] = "SMB_COM_NEGOTIATE",
[SMB_COM_SESSION_SETUP_ANDX] = "SMB_COM_SESSION_SETUP_ANDX",
[SMB_COM_LOGOFF_ANDX] = "SMB_COM_LOGOFF_ANDX",
[SMB_COM_TREE_CONNECT_ANDX] = "SMB_COM_TREE_CONNECT_ANDX",
[SMB_COM_QUERY_INFORMATION_DISK] = "SMB_COM_QUERY_INFORMATION_DISK",
[SMB_COM_NT_TRANSACT] = "SMB_COM_NT_TRANSACT",
[SMB_COM_NT_TRANSACT_SECONDARY] = "SMB_COM_NT_TRANSACT_SECONDARY",
[SMB_COM_NT_CREATE_ANDX] = "SMB_COM_NT_CREATE_ANDX",
[SMB_COM_NT_CANCEL] = "SMB_COM_NT_CANCEL",
[SMB_COM_NT_RENAME] = "SMB_COM_NT_RENAME",
};
static const char *smb_cmd_to_str(u16 cmd)
{
if (cmd < ARRAY_SIZE(smb_cmd_str))
return smb_cmd_str[cmd];
return "unknown_cmd";
}
static const char *const smb_trans2_cmd_str[] = {
[TRANS2_OPEN] = "TRANS2_OPEN",
[TRANS2_FIND_FIRST] = "TRANS2_FIND_FIRST",
[TRANS2_FIND_NEXT] = "TRANS2_FIND_NEXT",
[TRANS2_QUERY_FS_INFORMATION] = "TRANS2_QUERY_FS_INFORMATION",
[TRANS2_SET_FS_INFORMATION] = "TRANS2_SET_FS_INFORMATION",
[TRANS2_QUERY_PATH_INFORMATION] = "TRANS2_QUERY_PATH_INFORMATION",
[TRANS2_SET_PATH_INFORMATION] = "TRANS2_SET_PATH_INFORMATION",
[TRANS2_QUERY_FILE_INFORMATION] = "TRANS2_QUERY_FILE_INFORMATION",
[TRANS2_SET_FILE_INFORMATION] = "TRANS2_SET_FILE_INFORMATION",
[TRANS2_CREATE_DIRECTORY] = "TRANS2_CREATE_DIRECTORY",
[TRANS2_GET_DFS_REFERRAL] = "TRANS2_GET_DFS_REFERRAL",
[TRANS2_REPORT_DFS_INCOSISTENCY] = "TRANS2_REPORT_DFS_INCOSISTENCY",
};
static const char *smb_trans2_cmd_to_str(u16 cmd)
{
if (cmd < ARRAY_SIZE(smb_trans2_cmd_str))
return smb_trans2_cmd_str[cmd];
return "unknown_trans2_cmd";
}
static int smb1_oplock_enable = false;
/* Default: allocation roundup size = 1048576 */
static unsigned int alloc_roundup_size = 1048576;
struct ksmbd_dirent {
unsigned long long ino;
unsigned long long offset;
unsigned int namelen;
unsigned int d_type;
char name[];
};
/**
* smb_NTtimeToUnix() - convert NTFS time to unix style time format
* @ntutc: NTFS style time
*
* Convert the NT UTC (based 1601-01-01, in hundred nanosecond units)
* into Unix UTC (based 1970-01-01, in seconds).
*
* Return: timespec containing unix style time
*/
static struct timespec64 smb_NTtimeToUnix(__le64 ntutc)
{
struct timespec64 ts;
/* BB what about the timezone? BB */
/* Subtract the NTFS time offset, then convert to 1s intervals. */
/* this has been taken from cifs, ntfs code */
u64 t;
t = le64_to_cpu(ntutc) - NTFS_TIME_OFFSET;
ts.tv_nsec = do_div(t, 10000000) * 100;
ts.tv_sec = t;
return ts;
}
/**
* get_smb_cmd_val() - get smb command value from smb header
* @work: smb work containing smb header
*
* Return: smb command value
*/
u16 get_smb_cmd_val(struct ksmbd_work *work)
{
struct smb_hdr *rcv_hdr = (struct smb_hdr *)work->request_buf;
return (u16)rcv_hdr->Command;
}
/**
* is_smbreq_unicode() - check if the smb command is request is unicode or not
* @hdr: pointer to smb_hdr in the the request part
*
* Return: check flags and return true if request is unicode, else false
*/
static inline int is_smbreq_unicode(struct smb_hdr *hdr)
{
return hdr->Flags2 & SMBFLG2_UNICODE ? 1 : 0;
}
/**
* set_smb_rsp_status() - set error type in smb response header
* @work: smb work containing smb response header
* @err: error code to set in response
*/
void set_smb_rsp_status(struct ksmbd_work *work, __le32 err)
{
struct smb_hdr *rsp_hdr = (struct smb_hdr *) work->response_buf;
rsp_hdr->Status.CifsError = err;
}
/**
* init_smb_rsp_hdr() - initialize smb response header
* @work: smb work containing smb request
*
* Return: 0 on success, otherwise -EINVAL
*/
int init_smb_rsp_hdr(struct ksmbd_work *work)
{
struct ksmbd_conn *conn = work->conn;
struct smb_hdr *rsp_hdr;
struct smb_hdr *rcv_hdr = (struct smb_hdr *)work->request_buf;
rsp_hdr = (struct smb_hdr *) work->response_buf;
memset(rsp_hdr, 0, sizeof(struct smb_hdr) + 2);
/* remove 4 byte direct TCP header, add 1 byte wc and 2 byte bcc */
rsp_hdr->smb_buf_length = cpu_to_be32(conn->vals->header_size - 4 + 2);
memcpy(rsp_hdr->Protocol, rcv_hdr->Protocol, 4);
rsp_hdr->Command = rcv_hdr->Command;
/*
* Message is response. Other bits are obsolete.
*/
rsp_hdr->Flags = (SMBFLG_RESPONSE);
/*
* Lets assume error code are NTLM. True for CIFS and windows 7
*/
rsp_hdr->Flags2 = rcv_hdr->Flags2;
rsp_hdr->PidHigh = rcv_hdr->PidHigh;
rsp_hdr->Pid = rcv_hdr->Pid;
rsp_hdr->Mid = rcv_hdr->Mid;
rsp_hdr->WordCount = 0;
/* We can do the above test because we have set maxVCN as 1 */
rsp_hdr->Uid = rcv_hdr->Uid;
rsp_hdr->Tid = rcv_hdr->Tid;
return 0;
}
/**
* smb_allocate_rsp_buf() - allocate response buffer for a command
* @work: smb work containing smb request
*
* Return: 0 on success, otherwise -ENOMEM
*/
int smb_allocate_rsp_buf(struct ksmbd_work *work)
{
struct smb_hdr *hdr = (struct smb_hdr *)work->request_buf;
unsigned char cmd = hdr->Command;
size_t large_sz = work->conn->vals->max_read_size + MAX_CIFS_HDR_SIZE;
size_t sz = MAX_CIFS_SMALL_BUFFER_SIZE;
if (cmd == SMB_COM_TRANSACTION2) {
struct smb_com_trans2_qpi_req *req = work->request_buf;
u16 sub_cmd = le16_to_cpu(req->SubCommand);
u16 infolevel = le16_to_cpu(req->InformationLevel);
if ((sub_cmd == TRANS2_FIND_FIRST) ||
(sub_cmd == TRANS2_FIND_NEXT) ||
(sub_cmd == TRANS2_QUERY_PATH_INFORMATION &&
(infolevel == SMB_QUERY_FILE_UNIX_LINK ||
infolevel == SMB_QUERY_POSIX_ACL ||
infolevel == SMB_INFO_QUERY_ALL_EAS)))
sz = large_sz;
}
if (cmd == SMB_COM_TRANSACTION)
sz = large_sz;
if (cmd == SMB_COM_ECHO) {
int resp_size;
struct smb_com_echo_req *req = work->request_buf;
/*
* size of struct smb_com_echo_rsp + Bytecount - Size of Data
* in struct smb_com_echo_rsp
*/
resp_size = sizeof(struct smb_com_echo_rsp) +
le16_to_cpu(req->ByteCount) - 1;
if (resp_size > MAX_CIFS_SMALL_BUFFER_SIZE)
sz = large_sz;
}
work->response_buf = kvmalloc(sz, GFP_KERNEL | __GFP_ZERO);
work->response_sz = sz;
if (!work->response_buf) {
pr_err("Failed to allocate %zu bytes buffer\n", sz);
return -ENOMEM;
}
return 0;
}
/**
* andx_request_buffer() - return pointer to matching andx command
* @work: buffer containing smb request
* @command: match next command with this command
*
* Return: pointer to matching command buffer on success, otherwise NULL
*/
static char *andx_request_buffer(char *buf, int command)
{
struct andx_block *andx_ptr =
(struct andx_block *)(buf + sizeof(struct smb_hdr) - 1);
struct andx_block *next;
while (andx_ptr->AndXCommand != SMB_NO_MORE_ANDX_COMMAND) {
next = (struct andx_block *)(buf + 4 +
le16_to_cpu(andx_ptr->AndXOffset));
if (andx_ptr->AndXCommand == command)
return (char *)next;
andx_ptr = next;
}
return NULL;
}
/**
* andx_response_buffer() - return pointer to andx response buffer
* @buf: buffer containing smb request
*
* Return: pointer to andx command response on success, otherwise NULL
*/
static char *andx_response_buffer(char *buf)
{
int pdu_length = get_rfc1002_len(buf);
return buf + 4 + pdu_length;
}
/**
* smb_check_user_session() - check for valid session for a user
* @work: smb work containing smb request buffer
*
* Return: 0 on success, otherwise error
*/
int smb_check_user_session(struct ksmbd_work *work)
{
struct smb_hdr *req_hdr = (struct smb_hdr *)work->request_buf;
struct ksmbd_conn *conn = work->conn;
unsigned int cmd = conn->ops->get_cmd_val(work);
ksmbd_debug(SMB, "received SMB command: %s\n",
smb_cmd_to_str(req_hdr->Command));
work->sess = NULL;
if (cmd == SMB_COM_NEGOTIATE || cmd == SMB_COM_SESSION_SETUP_ANDX ||
cmd == SMB_COM_ECHO)
return 0;
if (!ksmbd_conn_good(conn))
return -EINVAL;
if (xa_empty(&conn->sessions)) {
ksmbd_debug(SMB, "NO sessions registered\n");
return -EINVAL;
}
work->sess = ksmbd_session_lookup(conn, le16_to_cpu(req_hdr->Uid));
if (work->sess)
return 1;
ksmbd_debug(SMB, "Invalid user session, Uid %u\n",
le16_to_cpu(req_hdr->Uid));
return -EINVAL;
}
/**
* smb_get_ksmbd_tcon() - get tree connection information for a tree id
* @sess: session containing tree list
* @tid: match tree connection with tree id
*
* Return: matching tree connection on success, otherwise error
*/
int smb_get_ksmbd_tcon(struct ksmbd_work *work)
{
struct smb_hdr *req_hdr = (struct smb_hdr *)work->request_buf;
u8 cmd = req_hdr->Command;
int tree_id;
work->tcon = NULL;
if (cmd == SMB_COM_TREE_CONNECT_ANDX ||
cmd == SMB_COM_NT_CANCEL ||
cmd == SMB_COM_PROCESS_EXIT ||
cmd == SMB_COM_LOGOFF_ANDX) {
ksmbd_debug(SMB, "skip to check tree connect request\n");
return 0;
}
if (xa_empty(&work->sess->tree_conns)) {
ksmbd_debug(SMB, "NO tree connected\n");
return -ENOENT;
}
tree_id = le16_to_cpu(req_hdr->Tid);
work->tcon = ksmbd_tree_conn_lookup(work->sess, tree_id);
if (!work->tcon) {
pr_err("Invalid tid %d\n", tree_id);
return -EINVAL;
}
return 1;
}
/**
* smb_session_disconnect() - LOGOFF request handler
* @work: smb work containing log off request buffer
*
* Return: 0 on success, otherwise error
*/
int smb_session_disconnect(struct ksmbd_work *work)
{
struct ksmbd_conn *conn = work->conn;
struct ksmbd_session *sess = work->sess;
/* setting CifsExiting here may race with start_tcp_sess */
ksmbd_conn_set_need_reconnect(conn);
ksmbd_conn_wait_idle(conn);
ksmbd_tree_conn_session_logoff(sess);
/* let start_tcp_sess free conn info now */
ksmbd_conn_set_exiting(conn);
return 0;
}
/**
* smb_tree_disconnect() - tree disconnect request handler
* @work: smb work containing tree disconnect request buffer
*
* Return: 0 on success, otherwise error
*/
int smb_tree_disconnect(struct ksmbd_work *work)
{
struct smb_hdr *req_hdr = (struct smb_hdr *)work->request_buf;
struct smb_hdr *rsp_hdr = (struct smb_hdr *)work->response_buf;
struct ksmbd_tree_connect *tcon = work->tcon;
struct ksmbd_session *sess = work->sess;
int err;
if (!tcon) {
pr_err("Invalid tid %d\n", req_hdr->Tid);
rsp_hdr->Status.CifsError = STATUS_NO_SUCH_USER;
return -EINVAL;
}
ksmbd_close_tree_conn_fds(work);
write_lock(&sess->tree_conns_lock);
if (tcon->t_state == TREE_DISCONNECTED) {
write_unlock(&sess->tree_conns_lock);
rsp_hdr->Status.CifsError = STATUS_NETWORK_NAME_DELETED;
return -ENOENT;
}
WARN_ON_ONCE(atomic_dec_and_test(&tcon->refcount));
tcon->t_state = TREE_DISCONNECTED;
write_unlock(&sess->tree_conns_lock);
err = ksmbd_tree_conn_disconnect(sess, tcon);
if (err) {
rsp_hdr->Status.CifsError = STATUS_NETWORK_NAME_DELETED;
return -ENOENT;
}
work->tcon = NULL;
return 0;
}
static void set_service_type(struct ksmbd_conn *conn,
struct ksmbd_share_config *share,
struct smb_com_tconx_rsp_ext *rsp)
{
int length;
char *buf = rsp->Service;
if (test_share_config_flag(share, KSMBD_SHARE_FLAG_PIPE)) {
length = strlen(SERVICE_IPC_SHARE);
memcpy(buf, SERVICE_IPC_SHARE, length);
rsp->ByteCount = cpu_to_le16(length + 1);
buf += length;
*buf = '\0';
} else {
int uni_len = 0;
length = strlen(SERVICE_DISK_SHARE);
memcpy(buf, SERVICE_DISK_SHARE, length);
buf[length] = '\0';
length += 1;
uni_len = smbConvertToUTF16((__le16 *)(buf + length),
NATIVE_FILE_SYSTEM, PATH_MAX,
conn->local_nls, 0);
uni_len++;
uni_len *= 2;
length += uni_len;
rsp->ByteCount = cpu_to_le16(length);
}
}
/**
* smb_tree_connect_andx() - tree connect request handler
* @work: smb work containing tree connect request buffer
*
* Return: 0 on success, otherwise error
*/
int smb_tree_connect_andx(struct ksmbd_work *work)
{
struct smb_hdr *req_hdr = (struct smb_hdr *)work->request_buf;
struct smb_hdr *rsp_hdr = (struct smb_hdr *)work->response_buf;
struct ksmbd_conn *conn = work->conn;
struct smb_com_tconx_req *req;
struct smb_com_tconx_rsp_ext *rsp;
int extra_byte = 0;
char *treename = NULL, *name = NULL, *dev_type = NULL;
struct ksmbd_share_config *share;
struct ksmbd_session *sess = work->sess;
int dev_flags = 0;
struct ksmbd_tree_conn_status status;
/* Is this an ANDX command ? */
if (req_hdr->Command != SMB_COM_TREE_CONNECT_ANDX) {
ksmbd_debug(SMB, "SMB_COM_TREE_CONNECT_ANDX is part of ANDX");
req = (struct smb_com_tconx_req *)
andx_request_buffer(work->request_buf,
SMB_COM_TREE_CONNECT_ANDX);
rsp = (struct smb_com_tconx_rsp_ext *)
andx_response_buffer(work->response_buf);
extra_byte = 3;
if (!req) {
status.ret = -EINVAL;
goto out_err;
}
} else {
req = (struct smb_com_tconx_req *)(&req_hdr->WordCount);
rsp = (struct smb_com_tconx_rsp_ext *)(&rsp_hdr->WordCount);
}
/* check if valid tree name is present in request or not */
if (!req->PasswordLength) {
treename = smb_strndup_from_utf16(req->Password + 1,
256, true, conn->local_nls);
dev_type = smb_strndup_from_utf16(req->Password + 1 +
((strlen(treename) + 1) * 2), 256, false,
conn->local_nls);
} else {
treename = smb_strndup_from_utf16(req->Password +
le16_to_cpu(req->PasswordLength), 256, true,
conn->local_nls);
dev_type = smb_strndup_from_utf16(req->Password +
le16_to_cpu(req->PasswordLength) +
((strlen(treename) + 1) * 2),
256, false, conn->local_nls);
}
if (IS_ERR(treename) || IS_ERR(dev_type)) {
pr_err("Unable to strdup() treename or devtype uid %d\n",
rsp_hdr->Uid);
status.ret = KSMBD_TREE_CONN_STATUS_ERROR;
goto out_err;
}
name = ksmbd_extract_sharename(conn->um, treename);
if (IS_ERR(name)) {
status.ret = KSMBD_TREE_CONN_STATUS_ERROR;
goto out_err;
}
ksmbd_debug(SMB, "tree connect request for tree %s, dev_type : %s\n",
name, dev_type);
if (!strcmp(dev_type, "A:"))
dev_flags = 1;
else if (!strncmp(dev_type, "LPT", 3))
dev_flags = 2;
else if (!strcmp(dev_type, "IPC"))
dev_flags = 3;
else if (!strcmp(dev_type, "COMM"))
dev_flags = 4;
else if (!strcmp(dev_type, "?????"))
dev_flags = 5;
if (!strcmp(name, "IPC$")) {
if (dev_flags < 3) {
status.ret = -ENODEV;
goto out_err;
}
} else if (!dev_flags || (dev_flags > 1 && dev_flags < 5)) {
status.ret = -ENODEV;
goto out_err;
}
status = ksmbd_tree_conn_connect(work, name);
if (status.ret == KSMBD_TREE_CONN_STATUS_OK)
rsp_hdr->Tid = cpu_to_le16(status.tree_conn->id);
else
goto out_err;
status.ret = 0;
share = status.tree_conn->share_conf;
rsp->WordCount = 7;
rsp->OptionalSupport = cpu_to_le16(SMB_SUPPORT_SEARCH_BITS |
SMB_CSC_NO_CACHING |
SMB_UNIQUE_FILE_NAME);
rsp->MaximalShareAccessRights = cpu_to_le32(FILE_READ_RIGHTS |
FILE_EXEC_RIGHTS);
if (test_tree_conn_flag(status.tree_conn,
KSMBD_TREE_CONN_FLAG_WRITABLE))
rsp->MaximalShareAccessRights |= cpu_to_le32(FILE_WRITE_RIGHTS);
rsp->GuestMaximalShareAccessRights = 0;
set_service_type(conn, share, rsp);
/* For each extra andx response, we have to add 1 byte,
* for wc and 2 bytes for byte count
*/
inc_rfc1001_len(rsp_hdr,
7 * 2 + le16_to_cpu(rsp->ByteCount) + extra_byte);
/* this is an ANDx command ? */
rsp->AndXReserved = 0;
rsp->AndXOffset = cpu_to_le16(get_rfc1002_len(rsp_hdr));
if (req->AndXCommand != SMB_NO_MORE_ANDX_COMMAND) {
/* adjust response */
rsp->AndXCommand = req->AndXCommand;
/* More processing required */
status.ret = rsp->AndXCommand;
} else {
rsp->AndXCommand = SMB_NO_MORE_ANDX_COMMAND;
}
kfree(treename);
kfree(dev_type);
kfree(name);
write_lock(&sess->tree_conns_lock);
status.tree_conn->t_state = TREE_CONNECTED;
write_unlock(&sess->tree_conns_lock);
return status.ret;
out_err:
if (!IS_ERR(treename))
kfree(treename);
if (!IS_ERR(dev_type))
kfree(dev_type);
if (!IS_ERR(name))
kfree(name);
rsp->WordCount = 7;
rsp->AndXCommand = SMB_NO_MORE_ANDX_COMMAND;
rsp->AndXReserved = 0;
rsp->AndXOffset = cpu_to_le16(get_rfc1002_len(rsp_hdr));
rsp->OptionalSupport = 0;
rsp->MaximalShareAccessRights = 0;
rsp->GuestMaximalShareAccessRights = 0;
rsp->ByteCount = 0;
ksmbd_debug(SMB, "error while tree connect\n");
switch (status.ret) {
case KSMBD_TREE_CONN_STATUS_NO_SHARE:
rsp_hdr->Status.CifsError = STATUS_BAD_NETWORK_PATH;
break;
case -ENOMEM:
case KSMBD_TREE_CONN_STATUS_NOMEM:
rsp_hdr->Status.CifsError = STATUS_NO_MEMORY;
break;
case KSMBD_TREE_CONN_STATUS_TOO_MANY_CONNS:
case KSMBD_TREE_CONN_STATUS_TOO_MANY_SESSIONS:
rsp_hdr->Status.CifsError = STATUS_ACCESS_DENIED;
break;
case -ENODEV:
rsp_hdr->Status.CifsError = STATUS_BAD_DEVICE_TYPE;
break;
case KSMBD_TREE_CONN_STATUS_ERROR:
rsp_hdr->Status.CifsError = STATUS_BAD_NETWORK_NAME;
break;
case -EINVAL:
rsp_hdr->Status.CifsError = STATUS_INVALID_PARAMETER;
break;
default:
rsp_hdr->Status.CifsError = STATUS_ACCESS_DENIED;
}
inc_rfc1001_len(rsp_hdr,
(7 * 2 + le16_to_cpu(rsp->ByteCount) + extra_byte));
return -EINVAL;
}
/**
* smb_get_name() - convert filename on smb packet to char string
* @src: source filename, mostly in unicode format
* @maxlen: maxlen of src string to be used for parsing
* @work: smb work containing smb header flag
* @converted: src string already converted to local characterset
*
* Return: pointer to filename string on success, otherwise error ptr
*/
static char *smb_get_name(struct ksmbd_share_config *share, const char *src,
const int maxlen, struct ksmbd_work *work,
bool converted)
{
struct smb_hdr *req_hdr = (struct smb_hdr *)work->request_buf;
bool is_unicode = is_smbreq_unicode(req_hdr);
char *name, *wild_card_pos;
if (converted)
name = (char *)src;
else {
name = smb_strndup_from_utf16(src, maxlen, is_unicode,
work->conn->local_nls);
if (IS_ERR(name)) {
ksmbd_debug(SMB, "failed to get name %ld\n",
PTR_ERR(name));
return name;
}
}
ksmbd_conv_path_to_unix(name);
ksmbd_strip_last_slash(name);
/*Handling of dir path in FIND_FIRST2 having '*' at end of path*/
wild_card_pos = strrchr(name, '*');
if (wild_card_pos != NULL)
*wild_card_pos = '\0';
if (ksmbd_validate_filename(name) < 0) {
if (!converted)
kfree(name);
return ERR_PTR(-ENOENT);
}
if (ksmbd_share_veto_filename(share, name)) {
ksmbd_debug(SMB,
"file(%s) open is not allowed by setting as veto file\n",
name);
if (!converted)
kfree(name);
return ERR_PTR(-ENOENT);
}
ksmbd_debug(SMB, "file name = %s\n", name);
return name;
}
/**
* smb_get_dir_name() - convert directory name on smb packet to char string
* @src: source dir name, mostly in unicode format
* @maxlen: maxlen of src string to be used for parsing
* @work: smb work containing smb header flag
* @srch_ptr: update search pointer in dir for searching dir entries
*
* Return: pointer to dir name string on success, otherwise error ptr
*/
static char *smb_get_dir_name(struct ksmbd_share_config *share,
const char *src, const int maxlen,
struct ksmbd_work *work, char **srch_ptr)
{
struct smb_hdr *req_hdr = (struct smb_hdr *)work->request_buf;
struct smb_hdr *rsp_hdr = (struct smb_hdr *)work->response_buf;
bool is_unicode = is_smbreq_unicode(req_hdr);
char *name, *pattern_pos, *pattern = NULL;
int pattern_len, rc;
name = smb_strndup_from_utf16(src, maxlen, is_unicode,
work->conn->local_nls);
if (IS_ERR(name)) {
pr_err("failed to allocate memory\n");
rsp_hdr->Status.CifsError = STATUS_NO_MEMORY;
return name;
}
ksmbd_conv_path_to_unix(name);
ksmbd_strip_last_slash(name);
pattern_pos = strrchr(name, '/');
if (!pattern_pos)
pattern_pos = name;
else
pattern_pos += 1;
pattern_len = strlen(pattern_pos);
if (pattern_len == 0) {
rc = -EINVAL;
goto err_name;
}
ksmbd_debug(SMB, "pattern searched = %s pattern_len = %d\n",
pattern_pos, pattern_len);
pattern = kmalloc(pattern_len + 1, GFP_KERNEL);
if (!pattern) {
rc = -ENOMEM;
goto err_name;
}
memcpy(pattern, pattern_pos, pattern_len);
*(pattern + pattern_len) = '\0';
*pattern_pos = '\0';
*srch_ptr = pattern;
if (ksmbd_validate_filename(name) < 0) {
rc = -ENOENT;
goto err_pattern;
}
if (ksmbd_share_veto_filename(share, name)) {
ksmbd_debug(SMB,
"file(%s) open is not allowed by setting as veto file\n",
name);
rc = -ENOENT;
goto err_pattern;
}
ksmbd_debug(SMB, "dir name = %s\n", name);
return name;
err_pattern:
kfree(pattern);
*srch_ptr = NULL;
err_name:
kfree(name);
if (rc == -EINVAL)
rsp_hdr->Status.CifsError = STATUS_INVALID_PARAMETER;
else if (rc == -ENOMEM)
rsp_hdr->Status.CifsError = STATUS_NO_MEMORY;
else if (rc == -ENOENT)
rsp_hdr->Status.CifsError = STATUS_OBJECT_NAME_INVALID;
return ERR_PTR(rc);
}
/**
* smb_rename() - rename request handler
* @work: smb work containing rename request buffer
*
* Return: 0 on success, otherwise error
*/
int smb_rename(struct ksmbd_work *work)
{
struct smb_com_rename_req *req = work->request_buf;
struct smb_com_rename_rsp *rsp = work->response_buf;
struct ksmbd_share_config *share = work->tcon->share_conf;
bool is_unicode = is_smbreq_unicode(&req->hdr);
char *oldname, *newname;
struct ksmbd_file *fp = NULL;
int oldname_len;
struct path path;
bool file_present = true;
int rc = 0;
if (!test_tree_conn_flag(work->tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) {
ksmbd_debug(SMB,
"returning as user does not have permission to write\n");
rsp->hdr.Status.CifsError = STATUS_ACCESS_DENIED;
return -EACCES;
}
oldname = smb_get_name(share, req->OldFileName, PATH_MAX, work, false);
if (IS_ERR(oldname)) {
rsp->hdr.Status.CifsError = STATUS_OBJECT_NAME_INVALID;
return PTR_ERR(oldname);
}
if (is_unicode)
oldname_len = smb1_utf16_name_length((__le16 *)req->OldFileName,
PATH_MAX);
else {
oldname_len = strlen(oldname);
oldname_len++;
}
newname = smb_get_name(share, &req->OldFileName[oldname_len + 2],
PATH_MAX, work, false);
if (IS_ERR(newname)) {
rsp->hdr.Status.CifsError = STATUS_OBJECT_NAME_INVALID;
rc = PTR_ERR(newname);
newname = NULL;
goto out;
}
rc = ksmbd_vfs_kern_path(work, newname, LOOKUP_NO_SYMLINKS, &path, 1);
if (rc)
file_present = false;
else
path_put(&path);
if (file_present && strncmp(oldname, newname, strlen(oldname))) {
rc = -EEXIST;
rsp->hdr.Status.CifsError = STATUS_OBJECT_NAME_COLLISION;
ksmbd_debug(SMB, "cannot rename already existing file\n");
goto out;
}
ksmbd_debug(SMB, "rename %s -> %s\n", oldname, newname);
rc = ksmbd_vfs_kern_path(work, oldname, LOOKUP_NO_SYMLINKS, &path, 1);
if (rc)
goto out;
fp = ksmbd_vfs_dentry_open(work, &path, O_RDONLY, 0, false);
if (IS_ERR(fp)) {
fp = NULL;
path_put(&path);
goto out;
}
rc = ksmbd_vfs_fp_rename(work, fp, newname);
if (rc) {
rsp->hdr.Status.CifsError = STATUS_NO_MEMORY;
path_put(&path);
goto out;
}
path_put(&path);
rsp->hdr.WordCount = 0;
rsp->ByteCount = 0;
out:
if (fp)
ksmbd_close_fd(work, fp->volatile_id);
kfree(oldname);
kfree(newname);
return rc;
}
/**
* smb_handle_negotiate() - negotiate request handler
* @work: smb work containing negotiate request buffer
*
* Return: 0 on success, otherwise error
*/
int smb_handle_negotiate(struct ksmbd_work *work)
{
struct ksmbd_conn *conn = work->conn;
struct smb_negotiate_rsp *neg_rsp = work->response_buf;
__u64 time;
int rc = 0;
WARN_ON(ksmbd_conn_good(conn));
if (conn->dialect == BAD_PROT_ID) {
neg_rsp->hdr.Status.CifsError = STATUS_INVALID_LOGON_TYPE;
rc = -EINVAL;
goto err_out;
}
conn->connection_type = 0;
/* wct 17 for NTLM */
neg_rsp->hdr.WordCount = 17;
neg_rsp->DialectIndex = cpu_to_le16(conn->dialect);
neg_rsp->SecurityMode = SMB1_SERVER_SECU;
if (server_conf.signing == KSMBD_CONFIG_OPT_AUTO ||
server_conf.signing == KSMBD_CONFIG_OPT_MANDATORY) {
conn->sign = true;
neg_rsp->SecurityMode |= SECMODE_SIGN_ENABLED;
if (server_conf.signing == KSMBD_CONFIG_OPT_MANDATORY)
neg_rsp->SecurityMode |= SECMODE_SIGN_REQUIRED;
}
neg_rsp->MaxMpxCount = cpu_to_le16(SMB1_MAX_MPX_COUNT);
neg_rsp->MaxNumberVcs = cpu_to_le16(SMB1_MAX_VCS);
neg_rsp->MaxBufferSize = cpu_to_le32(conn->vals->max_read_size);
neg_rsp->MaxRawSize = cpu_to_le32(SMB1_MAX_RAW_SIZE);
neg_rsp->SessionKey = 0;
neg_rsp->Capabilities = cpu_to_le32(SMB1_SERVER_CAPS);
time = ksmbd_systime();
neg_rsp->SystemTimeLow = cpu_to_le32(time & 0x00000000FFFFFFFF);
neg_rsp->SystemTimeHigh =
cpu_to_le32((time & 0xFFFFFFFF00000000) >> 32);
neg_rsp->ServerTimeZone = 0;
if (((struct smb_hdr *)work->request_buf)->Flags2 & SMBFLG2_EXT_SEC)
conn->use_spnego = true;
ksmbd_debug(SMB, "spnego is %s\n", conn->use_spnego ? "on" : "off");
if (conn->use_spnego == false) {
neg_rsp->EncryptionKeyLength = CIFS_CRYPTO_KEY_SIZE;
neg_rsp->Capabilities &= ~cpu_to_le32(CAP_EXTENDED_SECURITY);
neg_rsp->ByteCount = cpu_to_le16(CIFS_CRYPTO_KEY_SIZE);
/* initialize random server challenge */
get_random_bytes(conn->ntlmssp.cryptkey, sizeof(__u64));
memcpy((neg_rsp->u.EncryptionKey), conn->ntlmssp.cryptkey,
CIFS_CRYPTO_KEY_SIZE);
/* Adjust pdu length, 17 words and 8 bytes added */
inc_rfc1001_len(neg_rsp, (17 * 2 + 8));
} else {
neg_rsp->EncryptionKeyLength = 0;
neg_rsp->ByteCount = cpu_to_le16(SMB1_CLIENT_GUID_SIZE +
AUTH_GSS_LENGTH);
get_random_bytes(neg_rsp->u.extended_response.GUID,
SMB1_CLIENT_GUID_SIZE);
ksmbd_copy_gss_neg_header(
neg_rsp->u.extended_response.SecurityBlob);
inc_rfc1001_len(neg_rsp, (17 * 2 + 16 + AUTH_GSS_LENGTH));
}
/* Null terminated domain name in unicode */
ksmbd_conn_set_need_negotiate(conn);
/* Domain name and PC name are ignored by clients, so no need to send.
* We can try sending them later
*/
err_out:
return rc;
}
static int build_sess_rsp_noextsec(struct ksmbd_conn *conn,
struct ksmbd_session *sess,
struct smb_com_session_setup_req_no_secext *req,
struct smb_com_session_setup_old_resp *rsp)
{
int offset, err = 0, len;
char *name;
__le16 str[32];
/* Build response. We don't use extended security (yet), so wct is 3 */
rsp->hdr.WordCount = 3;
rsp->Action = 0;
/* The names should be unicode */