forked from namjaejeon/ksmbd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
smb2pdu.c
7189 lines (6275 loc) · 188 KB
/
smb2pdu.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 <namjae.jeon@protocolfreedom.org>
* Copyright (C) 2018 Samsung Electronics Co., Ltd.
*/
#include <linux/inetdevice.h>
#include <net/addrconf.h>
#include <linux/syscalls.h>
#include "glob.h"
#include "smb2pdu.h"
#include "smbfsctl.h"
#include "oplock.h"
#include "cifsacl.h"
#include "auth.h"
#include "asn1.h"
#include "encrypt.h"
#include "buffer_pool.h"
#include "transport_tcp.h"
#include "transport_ipc.h"
#include "vfs.h"
#include "fh.h"
#include "misc.h"
#include "server.h"
#include "smb_common.h"
#include "mgmt/user_config.h"
#include "mgmt/share_config.h"
#include "mgmt/tree_connect.h"
#include "mgmt/user_session.h"
#include "mgmt/cifsd_ida.h"
bool multi_channel_enable;
bool encryption_enable;
struct fs_type_info fs_type[] = {
{ "ADFS", 0xadf5},
{ "AFFS", 0xadff},
{ "AFS", 0x5346414F},
{ "AUTOFS", 0x0187},
{ "CODA", 0x73757245},
{ "CRAMFS", 0x28cd3d45},
{ "CRAMFSW", 0x453dcd28},
{ "DEBUGFS", 0x64626720},
{ "SECURITYFS", 0x73636673},
{ "SELINUX", 0xf97cff8c},
{ "SMACK", 0x43415d53},
{ "RAMFS", 0x858458f6},
{ "TMPFS", 0x01021994},
{ "HUGETLBFS", 0x958458f6},
{ "SQUASHFS", 0x73717368},
{ "ECRYPTFS", 0xf15f},
{ "EFS", 0x414A53},
{ "EXT2", 0xEF53},
{ "EXT3", 0xEF53},
{ "XENFS", 0xabba1974},
{ "EXT4", 0xEF53},
{ "BTRFS", 0x9123683E},
{ "NILFS", 0x3434},
{ "F2FS", 0xF2F52010},
{ "HPFS", 0xf995e849},
{ "ISOFS", 0x9660},
{ "JFFS2", 0x72b6},
{ "PSTOREFS", 0x6165676C},
{ "EFIVARFS", 0xde5e81e4},
{ "HOSTFS", 0x00c0ffee},
{ "MINIX", 0x137F}, /* minix v1 fs, 14 char names */
{ "MINIX_2", 0x138F}, /* minix v1 fs, 30 char names */
{ "MINIX2", 0x2468}, /* minix v2 fs, 14 char names */
{ "MINIX2_2", 0x2478}, /* minix v2 fs, 30 char names */
{ "MINIX3", 0x4d5a}, /* minix v3 fs, 60 char names */
{ "MSDOS", 0x4d44}, /* MD */
{ "NCP", 0x564c},
{ "NFS", 0x6969},
{ "OPENPROM", 0x9fa1},
{ "QNX4", 0x002f}, /* qnx4 fs detection */
{ "QNX6", 0x68191122}, /* qnx6 fs detection */
{ "REISERFS", 0x52654973}, /* used by gcc */
{ "SMB", 0x517B},
{ "CGROUP", 0x27e0eb},
};
/**
* check_session_id() - check for valid session id in smb header
* @conn: TCP server instance of connection
* @id: session id from smb header
*
* Return: 1 if valid session id, otherwise 0
*/
static inline int check_session_id(struct cifsd_tcp_conn *conn, uint64_t id)
{
struct cifsd_session *sess;
if (id == 0 || id == -1)
return 0;
sess = cifsd_session_lookup(conn, id);
if (sess)
return 1;
cifsd_err("Invalid user session id: %llu\n", id);
return 0;
}
struct channel *lookup_chann_list(struct cifsd_session *sess)
{
struct channel *chann;
struct list_head *t;
list_for_each(t, &sess->cifsd_chann_list) {
chann = list_entry(t, struct channel, chann_list);
if (chann && chann->conn == sess->conn)
return chann;
}
return NULL;
}
/**
* smb2_get_cifsd_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 smb2_get_cifsd_tcon(struct cifsd_work *work)
{
struct smb2_hdr *req_hdr = (struct smb2_hdr *)REQUEST_BUF(work);
work->tcon = NULL;
if ((work->conn->ops->get_cmd_val(work) == SMB2_TREE_CONNECT_HE) ||
(work->conn->ops->get_cmd_val(work) == SMB2_CANCEL_HE) ||
(work->conn->ops->get_cmd_val(work) == SMB2_LOGOFF_HE)) {
cifsd_debug("skip to check tree connect request\n");
return 0;
}
if (list_empty(&work->sess->tree_conn_list)) {
cifsd_debug("NO tree connected\n");
return -1;
}
work->tcon = cifsd_tree_conn_lookup(work->sess,
le32_to_cpu(req_hdr->Id.SyncId.TreeId));
if (!work->tcon) {
cifsd_err("Invalid tid %d\n", req_hdr->Id.SyncId.TreeId);
return -1;
}
return 1;
}
/**
* smb2_set_err_rsp() - set error response code on smb response
* @work: smb work containing response buffer
*/
void smb2_set_err_rsp(struct cifsd_work *work)
{
char *rsp = RESPONSE_BUF(work);
struct smb2_err_rsp *err_rsp;
if (work->next_smb2_rcv_hdr_off)
err_rsp = (struct smb2_err_rsp *)((char *)rsp +
work->next_smb2_rsp_hdr_off);
else
err_rsp = (struct smb2_err_rsp *)rsp;
if (err_rsp->hdr.Status != cpu_to_le32(NT_STATUS_STOPPED_ON_SYMLINK)) {
err_rsp->StructureSize =
cpu_to_le16(SMB2_ERROR_STRUCTURE_SIZE2);
err_rsp->ErrorContextCount = 0;
err_rsp->Reserved = 0;
err_rsp->ByteCount = 0;
err_rsp->ErrorData[0] = 0;
inc_rfc1001_len(rsp, SMB2_ERROR_STRUCTURE_SIZE2);
}
}
/**
* is_smb2_neg_cmd() - is it smb2 negotiation command
* @work: smb work containing smb header
*
* Return: 1 if smb2 negotiation command, otherwise 0
*/
int is_smb2_neg_cmd(struct cifsd_work *work)
{
struct smb2_hdr *hdr = (struct smb2_hdr *)REQUEST_BUF(work);
/* is it SMB2 header ? */
if (hdr->ProtocolId != SMB2_PROTO_NUMBER)
return 0;
/* make sure it is request not response message */
if (hdr->Flags & SMB2_FLAGS_SERVER_TO_REDIR)
return 0;
if (hdr->Command != SMB2_NEGOTIATE)
return 0;
return 1;
}
/**
* is_smb2_rsp() - is it smb2 response
* @work: smb work containing smb response buffer
*
* Return: 1 if smb2 response, otherwise 0
*/
int is_smb2_rsp(struct cifsd_work *work)
{
struct smb2_hdr *hdr = (struct smb2_hdr *)RESPONSE_BUF(work);
/* is it SMB2 header ? */
if (hdr->ProtocolId != SMB2_PROTO_NUMBER)
return 0;
/* make sure it is response not request message */
if (!(hdr->Flags & SMB2_FLAGS_SERVER_TO_REDIR))
return 0;
return 1;
}
/**
* get_smb2_cmd_val() - get smb command code from smb header
* @work: smb work containing smb request buffer
*
* Return: smb2 request command value
*/
int get_smb2_cmd_val(struct cifsd_work *work)
{
struct smb2_hdr *rcv_hdr = (struct smb2_hdr *)REQUEST_BUF(work);
if (work->next_smb2_rcv_hdr_off)
rcv_hdr = (struct smb2_hdr *)((char *)rcv_hdr
+ work->next_smb2_rcv_hdr_off);
return le16_to_cpu(rcv_hdr->Command);
}
/**
* set_smb2_rsp_status() - set error response code on smb2 header
* @work: smb work containing response buffer
*/
void set_smb2_rsp_status(struct cifsd_work *work, unsigned int err)
{
struct smb2_hdr *rsp_hdr = (struct smb2_hdr *) RESPONSE_BUF(work);
if (work->next_smb2_rcv_hdr_off)
rsp_hdr = (struct smb2_hdr *)((char *)rsp_hdr
+ work->next_smb2_rsp_hdr_off);
rsp_hdr->Status = cpu_to_le32(err);
smb2_set_err_rsp(work);
}
/**
* init_smb2_neg_rsp() - initialize smb2 response for negotiate command
* @work: smb work containing smb request buffer
*
* smb2 negotiate response is sent in reply of smb1 negotiate command for
* dialect auto-negotiation.
*/
int init_smb2_neg_rsp(struct cifsd_work *work)
{
struct smb2_hdr *rsp_hdr;
struct smb2_negotiate_rsp *rsp;
struct cifsd_tcp_conn *conn = work->conn;
struct timespec64 ts64;
if (!conn->need_neg)
return -EINVAL;
if (!(conn->dialect >= SMB20_PROT_ID &&
conn->dialect <= SMB311_PROT_ID))
return -EINVAL;
rsp_hdr = (struct smb2_hdr *)RESPONSE_BUF(work);
memset(rsp_hdr, 0, sizeof(struct smb2_hdr) + 2);
rsp_hdr->smb2_buf_length =
cpu_to_be32(HEADER_SIZE_NO_BUF_LEN(conn));
rsp_hdr->ProtocolId = SMB2_PROTO_NUMBER;
rsp_hdr->StructureSize = SMB2_HEADER_STRUCTURE_SIZE;
rsp_hdr->CreditRequest = cpu_to_le16(1);
rsp_hdr->Command = 0;
rsp_hdr->Flags = (SMB2_FLAGS_SERVER_TO_REDIR);
rsp_hdr->NextCommand = 0;
rsp_hdr->MessageId = 0;
rsp_hdr->Id.SyncId.ProcessId = 0;
rsp_hdr->Id.SyncId.TreeId = 0;
rsp_hdr->SessionId = 0;
memset(rsp_hdr->Signature, 0, 16);
rsp = (struct smb2_negotiate_rsp *)RESPONSE_BUF(work);
WARN_ON(cifsd_tcp_good(work));
rsp->StructureSize = cpu_to_le16(65);
cifsd_debug("conn->dialect 0x%x\n", conn->dialect);
rsp->DialectRevision = cpu_to_le16(conn->dialect);
/* Not setting conn guid rsp->ServerGUID, as it
* not used by client for identifying connection */
rsp->Capabilities = 0;
/* Default Max Message Size till SMB2.0, 64K*/
rsp->MaxTransactSize = cifsd_max_msg_size();
rsp->MaxReadSize = cifsd_max_msg_size();
rsp->MaxWriteSize = cifsd_max_msg_size();
getnstimeofday64(&ts64);
rsp->SystemTime = cpu_to_le64(cifs_UnixTimeToNT(
timespec64_to_timespec(ts64)));
rsp->ServerStartTime = 0;
rsp->SecurityBufferOffset = cpu_to_le16(128);
rsp->SecurityBufferLength = cpu_to_le16(AUTH_GSS_LENGTH);
cifsd_copy_gss_neg_header(((char *)(&rsp->hdr) +
sizeof(rsp->hdr.smb2_buf_length)) + rsp->SecurityBufferOffset);
inc_rfc1001_len(rsp, sizeof(struct smb2_negotiate_rsp) -
sizeof(struct smb2_hdr) - sizeof(rsp->Buffer) +
AUTH_GSS_LENGTH);
rsp->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED;
conn->use_spnego = true;
cifsd_tcp_set_need_negotiate(work);
rsp->hdr.CreditRequest = cpu_to_le16(2);
return 0;
}
/**
* init_chained_smb2_rsp() - initialize smb2 chained response
* @work: smb work containing smb response buffer
*/
void init_chained_smb2_rsp(struct cifsd_work *work)
{
struct smb2_hdr *req;
struct smb2_hdr *rsp;
struct smb2_hdr *rsp_hdr;
struct smb2_hdr *rcv_hdr;
int next_hdr_offset = 0;
int len, new_len;
req = (struct smb2_hdr *)(REQUEST_BUF(work) +
work->next_smb2_rcv_hdr_off);
rsp = (struct smb2_hdr *)(RESPONSE_BUF(work) +
work->next_smb2_rsp_hdr_off);
/* Len of this response = updated RFC len - offset of previous cmd
in the compound rsp */
/* Storing the current local FID which may be needed by subsequent
command in the compound request */
if (le16_to_cpu(req->Command) == SMB2_CREATE &&
le32_to_cpu(rsp->Status) == NT_STATUS_OK) {
work->cur_local_fid =
le64_to_cpu(((struct smb2_create_rsp *)rsp)->
VolatileFileId);
work->cur_local_pfid =
le64_to_cpu(((struct smb2_create_rsp *)rsp)->
PersistentFileId);
work->cur_local_sess_id = rsp->SessionId;
}
len = get_rfc1002_length(RESPONSE_BUF(work)) -
work->next_smb2_rsp_hdr_off;
next_hdr_offset = le32_to_cpu(req->NextCommand);
/* Align the length to 8Byte */
new_len = ((len + 7) & ~7);
inc_rfc1001_len(RESPONSE_BUF(work), ((sizeof(struct smb2_hdr) - 4)
+ new_len - len));
rsp->NextCommand = cpu_to_le32(new_len);
work->next_smb2_rcv_hdr_off += next_hdr_offset;
work->next_smb2_rsp_hdr_off += new_len;
cifsd_debug("Compound req new_len = %d rcv off = %d rsp off = %d\n",
new_len, work->next_smb2_rcv_hdr_off,
work->next_smb2_rsp_hdr_off);
rsp_hdr = (struct smb2_hdr *)(((char *)RESPONSE_BUF(work) +
work->next_smb2_rsp_hdr_off));
rcv_hdr = (struct smb2_hdr *)(((char *)REQUEST_BUF(work) +
work->next_smb2_rcv_hdr_off));
if (!(le32_to_cpu(rcv_hdr->Flags) & SMB2_FLAGS_RELATED_OPERATIONS)) {
cifsd_debug("related flag should be set\n");
work->cur_local_fid = CIFSD_NO_FID;
work->cur_local_pfid = CIFSD_NO_FID;
}
memset((char *)rsp_hdr + 4, 0, sizeof(struct smb2_hdr) + 2);
rsp_hdr->ProtocolId = rcv_hdr->ProtocolId;
rsp_hdr->StructureSize = SMB2_HEADER_STRUCTURE_SIZE;
rsp_hdr->CreditRequest = rcv_hdr->CreditRequest;
rsp_hdr->Command = rcv_hdr->Command;
/*
* Message is response. We don't grant oplock yet.
*/
rsp_hdr->Flags = (SMB2_FLAGS_SERVER_TO_REDIR |
SMB2_FLAGS_RELATED_OPERATIONS);
rsp_hdr->NextCommand = 0;
rsp_hdr->MessageId = rcv_hdr->MessageId;
rsp_hdr->Id.SyncId.ProcessId = rcv_hdr->Id.SyncId.ProcessId;
rsp_hdr->Id.SyncId.TreeId = rcv_hdr->Id.SyncId.TreeId;
rsp_hdr->SessionId = rcv_hdr->SessionId;
memcpy(rsp_hdr->Signature, rcv_hdr->Signature, 16);
}
/**
* is_chained_smb2_message() - check for chained command
* @work: smb work containing smb request buffer
*
* Return: true if chained request, otherwise false
*/
bool is_chained_smb2_message(struct cifsd_work *work)
{
struct smb2_hdr *hdr = (struct smb2_hdr *)REQUEST_BUF(work);
unsigned int len;
if (hdr->ProtocolId != SMB2_PROTO_NUMBER)
return false;
hdr = (struct smb2_hdr *)(REQUEST_BUF(work) +
work->next_smb2_rcv_hdr_off);
if (le32_to_cpu(hdr->NextCommand) > 0) {
cifsd_debug("got SMB2 chained command\n");
init_chained_smb2_rsp(work);
return true;
} else if (work->next_smb2_rcv_hdr_off) {
/*
* This is last request in chained command,
* align response to 8 byte
*/
len = ((get_rfc1002_length(RESPONSE_BUF(work)) + 7) & ~7);
len = len - get_rfc1002_length(RESPONSE_BUF(work));
if (len) {
cifsd_debug("padding len %u\n", len);
inc_rfc1001_len(RESPONSE_BUF(work), len);
if (HAS_AUX_PAYLOAD(work))
work->aux_payload_sz += len;
}
}
return false;
}
/**
* init_smb2_rsp_hdr() - initialize smb2 response
* @work: smb work containing smb request buffer
*
* Return: 0
*/
int init_smb2_rsp_hdr(struct cifsd_work *work)
{
struct smb2_hdr *rsp_hdr = (struct smb2_hdr *)RESPONSE_BUF(work);
struct smb2_hdr *rcv_hdr = (struct smb2_hdr *)REQUEST_BUF(work);
struct cifsd_tcp_conn *conn = work->conn;
int next_hdr_offset = 0;
next_hdr_offset = le32_to_cpu(rcv_hdr->NextCommand);
memset(rsp_hdr, 0, sizeof(struct smb2_hdr) + 2);
rsp_hdr->smb2_buf_length = cpu_to_be32(HEADER_SIZE_NO_BUF_LEN(conn));
rsp_hdr->ProtocolId = rcv_hdr->ProtocolId;
rsp_hdr->StructureSize = SMB2_HEADER_STRUCTURE_SIZE;
rsp_hdr->CreditRequest = rcv_hdr->CreditRequest;
rsp_hdr->Command = rcv_hdr->Command;
/*
* Message is response. We don't grant oplock yet.
*/
rsp_hdr->Flags = (SMB2_FLAGS_SERVER_TO_REDIR);
if (next_hdr_offset)
rsp_hdr->NextCommand = cpu_to_le32(next_hdr_offset);
else
rsp_hdr->NextCommand = 0;
rsp_hdr->MessageId = rcv_hdr->MessageId;
rsp_hdr->Id.SyncId.ProcessId = rcv_hdr->Id.SyncId.ProcessId;
rsp_hdr->Id.SyncId.TreeId = rcv_hdr->Id.SyncId.TreeId;
rsp_hdr->SessionId = rcv_hdr->SessionId;
memcpy(rsp_hdr->Signature, rcv_hdr->Signature, 16);
if (conn->credits_granted) {
if (le16_to_cpu(rcv_hdr->CreditCharge))
conn->credits_granted -=
le16_to_cpu(rcv_hdr->CreditCharge);
else
conn->credits_granted -= 1;
}
work->type = SYNC;
if (work->async_id) {
cifds_release_id(conn->async_ida, work->async_id);
work->async_id = 0;
}
return 0;
}
/**
* smb2_allocate_rsp_buf() - allocate smb2 response buffer
* @work: smb work containing smb request buffer
*
* Return: 0 on success, otherwise -ENOMEM
*/
int smb2_allocate_rsp_buf(struct cifsd_work *work)
{
struct smb2_hdr *hdr = (struct smb2_hdr *)REQUEST_BUF(work);
struct smb2_query_info_req *req;
size_t small_sz = cifsd_small_buffer_size();
size_t large_sz = cifsd_max_msg_size() + MAX_SMB2_HDR_SIZE;
size_t sz = small_sz;
int cmd = le16_to_cpu(hdr->Command);
req = (struct smb2_query_info_req *)REQUEST_BUF(work);
if (cmd == SMB2_READ || cmd == SMB2_IOCTL_HE ||
cmd == SMB2_QUERY_DIRECTORY_HE)
sz = large_sz;
if (cmd == SMB2_QUERY_INFO_HE) {
if (req->InfoType == SMB2_O_INFO_FILE &&
(req->FileInfoClass == FILE_FULL_EA_INFORMATION ||
req->FileInfoClass == FILE_ALL_INFORMATION))
sz = large_sz;
}
/* allocate large response buf for chained commands */
if (le32_to_cpu(hdr->NextCommand) > 0)
sz = large_sz;
work->response_buf = cifsd_alloc_response(sz);
work->response_sz = sz;
if (!RESPONSE_BUF(work)) {
cifsd_err("Failed to allocate %zu bytes buffer\n", sz);
return -ENOMEM;
}
return 0;
}
/**
* smb2_set_rsp_credits() - set number of credits in response buffer
* @work: smb work containing smb response buffer
*/
void smb2_set_rsp_credits(struct cifsd_work *work)
{
struct smb2_hdr *hdr = (struct smb2_hdr *)RESPONSE_BUF(work);
struct cifsd_tcp_conn *conn = work->conn;
unsigned int status = le32_to_cpu(hdr->Status);
unsigned int flags = le32_to_cpu(hdr->Flags);
unsigned short credits_requested = le16_to_cpu(hdr->CreditRequest);
unsigned short cmd = le16_to_cpu(hdr->Command);
unsigned short credit_charge = 1, credits_granted = 0;
unsigned short aux_max, aux_credits, min_credits;
BUG_ON(conn->credits_granted >= conn->max_credits);
/* get default minimum credits by shifting maximum credits by 4 */
min_credits = conn->max_credits >> 4;
if (flags & SMB2_FLAGS_ASYNC_COMMAND) {
credits_granted = 0;
} else if (credits_requested > 0) {
aux_max = 0;
aux_credits = credits_requested - 1;
switch (cmd) {
case SMB2_NEGOTIATE:
break;
case SMB2_SESSION_SETUP:
aux_max = (status) ? 0 : 32;
break;
default:
aux_max = 32;
break;
}
aux_credits = (aux_credits < aux_max) ? aux_credits : aux_max;
credits_granted = aux_credits + credit_charge;
/* if credits granted per client is getting bigger than default
* minimum credits then we should wrap it up within the limits.
*/
if ((conn->credits_granted + credits_granted) > min_credits)
credits_granted = min_credits - conn->credits_granted;
} else if (conn->credits_granted == 0) {
credits_granted = 1;
}
conn->credits_granted += credits_granted;
cifsd_debug("credits: requested[%d] granted[%d] total_granted[%d]\n",
credits_requested, credits_granted,
conn->credits_granted);
/* set number of credits granted in SMB2 hdr */
hdr->CreditRequest = cpu_to_le16(credits_granted);
}
/**
* smb2_check_user_session() - check for valid session for a user
* @work: smb work containing smb request buffer
*
* Return: 0 on success, otherwise error
*/
int smb2_check_user_session(struct cifsd_work *work)
{
struct smb2_hdr *req_hdr = (struct smb2_hdr *)REQUEST_BUF(work);
struct cifsd_tcp_conn *conn = work->conn;
unsigned int cmd = conn->ops->get_cmd_val(work);
unsigned long long sess_id;
work->sess = NULL;
/*
* ECHO, KEEP_ALIVE, SMB2_NEGOTIATE, SMB2_SESSION_SETUP command does not
* require a session id, so no need to validate user session's for these
* commands.
*/
if (cmd == SMB2_ECHO || cmd == SMB2_NEGOTIATE ||
cmd == SMB2_SESSION_SETUP)
return 0;
if (!cifsd_tcp_good(work))
return -EINVAL;
sess_id = le64_to_cpu(req_hdr->SessionId);
/* Check for validity of user session */
work->sess = cifsd_session_lookup(conn, sess_id);
if (work->sess)
return 1;
cifsd_debug("Invalid user session, Uid %llu\n", sess_id);
return -EINVAL;
}
static void destroy_previous_session(uint64_t id)
{
cifsd_session_destroy(cifsd_session_lookup_slowpath(id));
}
/**
* smb2_get_name() - get filename string from on the wire smb format
* @src: source buffer
* @maxlen: maxlen of source string
* @work: smb work containing smb request buffer
*
* Return: matching converted filename on success, otherwise error ptr
*/
char *
smb2_get_name(struct cifsd_share_config *share,
const char *src,
const int maxlen,
struct nls_table *local_nls)
{
char *name, *unixname;
name = smb_strndup_from_utf16(src, maxlen, 1,
local_nls);
if (IS_ERR(name)) {
cifsd_err("failed to get name %ld\n", PTR_ERR(name));
return name;
}
/* change it to absolute unix name */
convert_delimiter(name, 0);
unixname = convert_to_unix_name(share, name);
kfree(name);
if (!unixname) {
cifsd_err("can not convert absolute name\n");
return ERR_PTR(-ENOMEM);
}
cifsd_debug("absoulte name = %s\n", unixname);
return unixname;
}
/**
* smb2_put_name() - free memory allocated for filename
* @name: filename pointer to be freed
*/
static void smb2_put_name(void *name)
{
if (!IS_ERR(name))
kfree(name);
}
int setup_async_work(struct cifsd_work *work, void (*fn)(void **), void **arg)
{
struct smb2_hdr *rsp_hdr;
struct cifsd_tcp_conn *conn = work->conn;
int id;
rsp_hdr = (struct smb2_hdr *)RESPONSE_BUF(work);
rsp_hdr->Flags |= SMB2_FLAGS_ASYNC_COMMAND;
id = cifds_acquire_async_msg_id(conn->async_ida);
if (id < 0) {
cifsd_err("Failed to alloc async message id\n");
return id;
}
work->type = ASYNC;
rsp_hdr->Id.AsyncId = work->async_id = cpu_to_le64(id);
cifsd_debug("Send interim Response to inform asynchronous request id : %lld\n",
work->async_id);
work->cancel_fn = fn;
work->cancel_argv = arg;
spin_lock(&conn->request_lock);
list_del_init(&work->request_entry);
list_add_tail(&work->request_entry,
&conn->async_requests);
spin_unlock(&conn->request_lock);
return 0;
}
void smb2_send_interim_resp(struct cifsd_work *work, __le32 status)
{
struct smb2_hdr *rsp_hdr;
rsp_hdr = (struct smb2_hdr *)RESPONSE_BUF(work);
smb2_set_err_rsp(work);
rsp_hdr->Status = status;
work->multiRsp = 1;
cifsd_tcp_write(work);
work->multiRsp = 0;
}
/**
* smb2_get_dos_mode() - get file mode in dos format from unix mode
* @stat: kstat containing file mode
*
* Return: converted dos mode
*/
static __le32 smb2_get_dos_mode(struct kstat *stat, __le32 attribute)
{
__le32 attr = 0;
attr = (attribute & 0x00005137) | ATTR_ARCHIVE;
if (S_ISDIR(stat->mode))
attr = ATTR_DIRECTORY;
else
attr &= ~(ATTR_DIRECTORY);
return attr;
}
static void
build_preauth_ctxt(struct smb2_preauth_neg_context *pneg_ctxt, int hash_id)
{
pneg_ctxt->ContextType = SMB2_PREAUTH_INTEGRITY_CAPABILITIES;
pneg_ctxt->DataLength = cpu_to_le16(38);
pneg_ctxt->HashAlgorithmCount = cpu_to_le16(1);
pneg_ctxt->Reserved = cpu_to_le32(0);
pneg_ctxt->SaltLength = cpu_to_le16(SMB311_SALT_SIZE);
get_random_bytes(pneg_ctxt->Salt, SMB311_SALT_SIZE);
pneg_ctxt->HashAlgorithms = cpu_to_le16(hash_id);
}
static void
build_encrypt_ctxt(struct smb2_encryption_neg_context *pneg_ctxt, int cipher_id)
{
pneg_ctxt->ContextType = SMB2_ENCRYPTION_CAPABILITIES;
pneg_ctxt->DataLength = cpu_to_le16(4);
pneg_ctxt->Reserved = cpu_to_le32(0);
pneg_ctxt->CipherCount = cpu_to_le16(1);
pneg_ctxt->Ciphers[0] = cpu_to_le16(cipher_id);
}
static void
assemble_neg_contexts(struct cifsd_tcp_conn *conn,
struct smb2_negotiate_rsp *rsp)
{
/* +4 is to account for the RFC1001 len field */
char *pneg_ctxt = (char *)rsp +
le32_to_cpu(rsp->NegotiateContextOffset) + 4;
cifsd_debug("assemble SMB2_PREAUTH_INTEGRITY_CAPABILITIES context\n");
build_preauth_ctxt((struct smb2_preauth_neg_context *)pneg_ctxt,
conn->preauth_info->Preauth_HashId);
rsp->NegotiateContextCount = cpu_to_le16(1);
inc_rfc1001_len(rsp,
AUTH_GSS_PADDING + sizeof(struct smb2_preauth_neg_context));
if (conn->preauth_info->CipherId) {
cifsd_debug("assemble SMB2_ENCRYPTION_CAPABILITIES context\n");
/* Add 2 to size to round to 8 byte boundary */
pneg_ctxt += sizeof(struct smb2_preauth_neg_context) + 2;
build_encrypt_ctxt(
(struct smb2_encryption_neg_context *)pneg_ctxt,
conn->preauth_info->CipherId);
rsp->NegotiateContextCount = cpu_to_le16(2);
/* Subtract 2 to remove unused Ciphers[1] */
inc_rfc1001_len(rsp,
2 + sizeof(struct smb2_encryption_neg_context) - 2);
}
}
static int
decode_preauth_ctxt(struct cifsd_tcp_conn *conn,
struct smb2_preauth_neg_context *pneg_ctxt)
{
int err = NT_STATUS_NO_PREAUTH_INTEGRITY_HASH_OVERLAP;
if (pneg_ctxt->HashAlgorithms ==
SMB2_PREAUTH_INTEGRITY_SHA512) {
conn->preauth_info->Preauth_HashId =
SMB2_PREAUTH_INTEGRITY_SHA512;
err = NT_STATUS_OK;
}
return err;
}
static void
decode_encrypt_ctxt(struct cifsd_tcp_conn *conn,
struct smb2_encryption_neg_context *pneg_ctxt)
{
int i;
int cph_cnt = pneg_ctxt->CipherCount;
conn->preauth_info->CipherId = 0;
if (!encryption_enable)
return;
/* Support only AES CCM cipher now */
for (i = 0; i < cph_cnt; i++) {
if (pneg_ctxt->Ciphers[i] ==
SMB2_ENCRYPTION_AES128_CCM) {
cifsd_debug("Cipher ID = SMB2_ENCRYPTION_AES128_CCM\n");
conn->preauth_info->CipherId =
SMB2_ENCRYPTION_AES128_CCM;
break;
}
}
}
static int
deassemble_neg_contexts(struct cifsd_tcp_conn *conn,
struct smb2_negotiate_req *req)
{
int i = 0, status = 0;
/* +4 is to account for the RFC1001 len field */
char *pneg_ctxt = (char *)req +
le32_to_cpu(req->NegotiateContextOffset) + 4;
__le16 *ContextType = (__le16 *)pneg_ctxt;
int neg_ctxt_cnt = le16_to_cpu(req->NegotiateContextCount);
cifsd_debug("negotiate context count = %d\n", neg_ctxt_cnt);
status = NT_STATUS_INVALID_PARAMETER;
while (i++ < neg_ctxt_cnt) {
if (*ContextType == SMB2_PREAUTH_INTEGRITY_CAPABILITIES) {
cifsd_debug("deassemble SMB2_PREAUTH_INTEGRITY_CAPABILITIES context\n");
if (conn->preauth_info->Preauth_HashId)
break;
status = decode_preauth_ctxt(conn,
(struct smb2_preauth_neg_context *)pneg_ctxt);
pneg_ctxt +=
sizeof(struct smb2_preauth_neg_context) + 2;
ContextType = (__le16 *)pneg_ctxt;
} else if (*ContextType == SMB2_ENCRYPTION_CAPABILITIES) {
cifsd_debug("deassemble SMB2_ENCRYPTION_CAPABILITIES context\n");
if (conn->preauth_info->CipherId)
break;
decode_encrypt_ctxt(conn,
(struct smb2_encryption_neg_context *)
pneg_ctxt);
pneg_ctxt +=
sizeof(struct smb2_encryption_neg_context) + 2;
}
if (status != NT_STATUS_OK)
break;
}
return status;
}
/**
* smb2_handle_negotiate() - handler for smb2 negotiate command
* @work: smb work containing smb request buffer
*
* Return: 0
*/
int smb2_handle_negotiate(struct cifsd_work *work)
{
struct cifsd_tcp_conn *conn = work->conn;
struct smb2_negotiate_req *req;
struct smb2_negotiate_rsp *rsp;
unsigned int limit;
int rc = 0, err;
struct timespec64 ts64;
cifsd_debug("Recieved negotiate request\n");
req = (struct smb2_negotiate_req *)REQUEST_BUF(work);
rsp = (struct smb2_negotiate_rsp *)RESPONSE_BUF(work);
conn->need_neg = false;
if (cifsd_tcp_good(work)) {
cifsd_err("conn->tcp_status is already in CifsGood State\n");
work->send_no_response = 1;
return rc;
}
if (req->DialectCount == 0) {
cifsd_err("malformed packet\n");
rsp->hdr.Status = NT_STATUS_INVALID_PARAMETER;
rc = -EINVAL;
goto err_out;
}
conn->cli_cap = req->Capabilities;
switch (conn->dialect) {
case SMB311_PROT_ID:
conn->preauth_info =
kzalloc(sizeof(struct preauth_integrity_info),
GFP_KERNEL);
if (!conn->preauth_info) {
rc = -ENOMEM;
rsp->hdr.Status = NT_STATUS_INVALID_PARAMETER;
}
err = deassemble_neg_contexts(conn, req);
if (err != NT_STATUS_OK) {
cifsd_err("deassemble_neg_contexts error(0x%x)\n", err);
rsp->hdr.Status = err;
rc = -EINVAL;
goto err_out;
}
rc = init_smb3_11_server(conn);
if (rc < 0) {
rsp->hdr.Status = NT_STATUS_INVALID_PARAMETER;
goto err_out;
}
cifsd_gen_preauth_integrity_hash(conn, REQUEST_BUF(work),
conn->preauth_info->Preauth_HashValue);
rsp->NegotiateContextOffset =
cpu_to_le32(OFFSET_OF_NEG_CONTEXT);
assemble_neg_contexts(conn, rsp);
break;
case SMB302_PROT_ID:
init_smb3_02_server(conn);
break;
case SMB30_PROT_ID:
init_smb3_0_server(conn);
break;
case SMB21_PROT_ID:
init_smb2_1_server(conn);
break;
case SMB20_PROT_ID:
cifsd_init_smb2_server_common(conn);
break;
case SMB2X_PROT_ID:
case BAD_PROT_ID:
default:
cifsd_err("Server dialect :0x%x not supported\n", conn->dialect);
rsp->hdr.Status = NT_STATUS_NOT_SUPPORTED;
rc = -EINVAL;
goto err_out;
}
rsp->Capabilities = conn->srv_cap;
/* For stats */
conn->connection_type = conn->dialect;
/* Default message size limit 64K till SMB2.0, no LargeMTU*/
limit = cifsd_max_msg_size();
if (conn->dialect > SMB20_PROT_ID) {
memcpy(conn->ClientGUID, req->ClientGUID,
SMB2_CLIENT_GUID_SIZE);
/* With LargeMTU above SMB2.0, default message limit is 1MB */
limit = cifsd_default_io_size();
conn->cli_sec_mode = req->SecurityMode;
}
rsp->StructureSize = cpu_to_le16(65);
rsp->DialectRevision = cpu_to_le16(conn->dialect);
/* Not setting conn guid rsp->ServerGUID, as it
* not used by client for identifying server*/
memset(rsp->ServerGUID, 0, SMB2_CLIENT_GUID_SIZE);
rsp->MaxTransactSize = cifsd_max_msg_size();
rsp->MaxReadSize = min(limit, (unsigned int)cifsd_default_io_size());
rsp->MaxWriteSize = min(limit, (unsigned int)cifsd_default_io_size());