ARM: 9148/1: handle CONFIG_CPU_ENDIAN_BE32 in arch/arm/kernel/head.S
[platform/kernel/linux-rpi.git] / fs / ksmbd / smb_common.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *   Copyright (C) 2018 Samsung Electronics Co., Ltd.
4  *   Copyright (C) 2018 Namjae Jeon <linkinjeon@kernel.org>
5  */
6
7 #include "smb_common.h"
8 #include "server.h"
9 #include "misc.h"
10 #include "smbstatus.h"
11 #include "connection.h"
12 #include "ksmbd_work.h"
13 #include "mgmt/user_session.h"
14 #include "mgmt/user_config.h"
15 #include "mgmt/tree_connect.h"
16 #include "mgmt/share_config.h"
17
18 /*for shortname implementation */
19 static const char basechars[43] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_-!@#$%";
20 #define MANGLE_BASE (sizeof(basechars) / sizeof(char) - 1)
21 #define MAGIC_CHAR '~'
22 #define PERIOD '.'
23 #define mangle(V) ((char)(basechars[(V) % MANGLE_BASE]))
24 #define KSMBD_MIN_SUPPORTED_HEADER_SIZE (sizeof(struct smb2_hdr))
25
26 struct smb_protocol {
27         int             index;
28         char            *name;
29         char            *prot;
30         __u16           prot_id;
31 };
32
33 static struct smb_protocol smb1_protos[] = {
34         {
35                 SMB21_PROT,
36                 "\2SMB 2.1",
37                 "SMB2_10",
38                 SMB21_PROT_ID
39         },
40         {
41                 SMB2X_PROT,
42                 "\2SMB 2.???",
43                 "SMB2_22",
44                 SMB2X_PROT_ID
45         },
46 };
47
48 static struct smb_protocol smb2_protos[] = {
49         {
50                 SMB21_PROT,
51                 "\2SMB 2.1",
52                 "SMB2_10",
53                 SMB21_PROT_ID
54         },
55         {
56                 SMB30_PROT,
57                 "\2SMB 3.0",
58                 "SMB3_00",
59                 SMB30_PROT_ID
60         },
61         {
62                 SMB302_PROT,
63                 "\2SMB 3.02",
64                 "SMB3_02",
65                 SMB302_PROT_ID
66         },
67         {
68                 SMB311_PROT,
69                 "\2SMB 3.1.1",
70                 "SMB3_11",
71                 SMB311_PROT_ID
72         },
73 };
74
75 unsigned int ksmbd_server_side_copy_max_chunk_count(void)
76 {
77         return 256;
78 }
79
80 unsigned int ksmbd_server_side_copy_max_chunk_size(void)
81 {
82         return (2U << 30) - 1;
83 }
84
85 unsigned int ksmbd_server_side_copy_max_total_size(void)
86 {
87         return (2U << 30) - 1;
88 }
89
90 inline int ksmbd_min_protocol(void)
91 {
92         return SMB2_PROT;
93 }
94
95 inline int ksmbd_max_protocol(void)
96 {
97         return SMB311_PROT;
98 }
99
100 int ksmbd_lookup_protocol_idx(char *str)
101 {
102         int offt = ARRAY_SIZE(smb1_protos) - 1;
103         int len = strlen(str);
104
105         while (offt >= 0) {
106                 if (!strncmp(str, smb1_protos[offt].prot, len)) {
107                         ksmbd_debug(SMB, "selected %s dialect idx = %d\n",
108                                     smb1_protos[offt].prot, offt);
109                         return smb1_protos[offt].index;
110                 }
111                 offt--;
112         }
113
114         offt = ARRAY_SIZE(smb2_protos) - 1;
115         while (offt >= 0) {
116                 if (!strncmp(str, smb2_protos[offt].prot, len)) {
117                         ksmbd_debug(SMB, "selected %s dialect idx = %d\n",
118                                     smb2_protos[offt].prot, offt);
119                         return smb2_protos[offt].index;
120                 }
121                 offt--;
122         }
123         return -1;
124 }
125
126 /**
127  * ksmbd_verify_smb_message() - check for valid smb2 request header
128  * @work:       smb work
129  *
130  * check for valid smb signature and packet direction(request/response)
131  *
132  * Return:      0 on success, otherwise 1
133  */
134 int ksmbd_verify_smb_message(struct ksmbd_work *work)
135 {
136         struct smb2_hdr *smb2_hdr = work->request_buf;
137
138         if (smb2_hdr->ProtocolId == SMB2_PROTO_NUMBER)
139                 return ksmbd_smb2_check_message(work);
140
141         return 0;
142 }
143
144 /**
145  * ksmbd_smb_request() - check for valid smb request type
146  * @conn:       connection instance
147  *
148  * Return:      true on success, otherwise false
149  */
150 bool ksmbd_smb_request(struct ksmbd_conn *conn)
151 {
152         int type = *(char *)conn->request_buf;
153
154         switch (type) {
155         case RFC1002_SESSION_MESSAGE:
156                 /* Regular SMB request */
157                 return true;
158         case RFC1002_SESSION_KEEP_ALIVE:
159                 ksmbd_debug(SMB, "RFC 1002 session keep alive\n");
160                 break;
161         default:
162                 ksmbd_debug(SMB, "RFC 1002 unknown request type 0x%x\n", type);
163         }
164
165         return false;
166 }
167
168 static bool supported_protocol(int idx)
169 {
170         if (idx == SMB2X_PROT &&
171             (server_conf.min_protocol >= SMB21_PROT ||
172              server_conf.max_protocol <= SMB311_PROT))
173                 return true;
174
175         return (server_conf.min_protocol <= idx &&
176                 idx <= server_conf.max_protocol);
177 }
178
179 static char *next_dialect(char *dialect, int *next_off)
180 {
181         dialect = dialect + *next_off;
182         *next_off = strlen(dialect);
183         return dialect;
184 }
185
186 static int ksmbd_lookup_dialect_by_name(char *cli_dialects, __le16 byte_count)
187 {
188         int i, seq_num, bcount, next;
189         char *dialect;
190
191         for (i = ARRAY_SIZE(smb1_protos) - 1; i >= 0; i--) {
192                 seq_num = 0;
193                 next = 0;
194                 dialect = cli_dialects;
195                 bcount = le16_to_cpu(byte_count);
196                 do {
197                         dialect = next_dialect(dialect, &next);
198                         ksmbd_debug(SMB, "client requested dialect %s\n",
199                                     dialect);
200                         if (!strcmp(dialect, smb1_protos[i].name)) {
201                                 if (supported_protocol(smb1_protos[i].index)) {
202                                         ksmbd_debug(SMB,
203                                                     "selected %s dialect\n",
204                                                     smb1_protos[i].name);
205                                         if (smb1_protos[i].index == SMB1_PROT)
206                                                 return seq_num;
207                                         return smb1_protos[i].prot_id;
208                                 }
209                         }
210                         seq_num++;
211                         bcount -= (++next);
212                 } while (bcount > 0);
213         }
214
215         return BAD_PROT_ID;
216 }
217
218 int ksmbd_lookup_dialect_by_id(__le16 *cli_dialects, __le16 dialects_count)
219 {
220         int i;
221         int count;
222
223         for (i = ARRAY_SIZE(smb2_protos) - 1; i >= 0; i--) {
224                 count = le16_to_cpu(dialects_count);
225                 while (--count >= 0) {
226                         ksmbd_debug(SMB, "client requested dialect 0x%x\n",
227                                     le16_to_cpu(cli_dialects[count]));
228                         if (le16_to_cpu(cli_dialects[count]) !=
229                                         smb2_protos[i].prot_id)
230                                 continue;
231
232                         if (supported_protocol(smb2_protos[i].index)) {
233                                 ksmbd_debug(SMB, "selected %s dialect\n",
234                                             smb2_protos[i].name);
235                                 return smb2_protos[i].prot_id;
236                         }
237                 }
238         }
239
240         return BAD_PROT_ID;
241 }
242
243 static int ksmbd_negotiate_smb_dialect(void *buf)
244 {
245         __le32 proto;
246
247         proto = ((struct smb2_hdr *)buf)->ProtocolId;
248         if (proto == SMB2_PROTO_NUMBER) {
249                 struct smb2_negotiate_req *req;
250
251                 req = (struct smb2_negotiate_req *)buf;
252                 return ksmbd_lookup_dialect_by_id(req->Dialects,
253                                                   req->DialectCount);
254         }
255
256         proto = *(__le32 *)((struct smb_hdr *)buf)->Protocol;
257         if (proto == SMB1_PROTO_NUMBER) {
258                 struct smb_negotiate_req *req;
259
260                 req = (struct smb_negotiate_req *)buf;
261                 return ksmbd_lookup_dialect_by_name(req->DialectsArray,
262                                                     req->ByteCount);
263         }
264
265         return BAD_PROT_ID;
266 }
267
268 #define SMB_COM_NEGOTIATE       0x72
269 int ksmbd_init_smb_server(struct ksmbd_work *work)
270 {
271         struct ksmbd_conn *conn = work->conn;
272
273         if (conn->need_neg == false)
274                 return 0;
275
276         init_smb3_11_server(conn);
277
278         if (conn->ops->get_cmd_val(work) != SMB_COM_NEGOTIATE)
279                 conn->need_neg = false;
280         return 0;
281 }
282
283 bool ksmbd_pdu_size_has_room(unsigned int pdu)
284 {
285         return (pdu >= KSMBD_MIN_SUPPORTED_HEADER_SIZE - 4);
286 }
287
288 int ksmbd_populate_dot_dotdot_entries(struct ksmbd_work *work, int info_level,
289                                       struct ksmbd_file *dir,
290                                       struct ksmbd_dir_info *d_info,
291                                       char *search_pattern,
292                                       int (*fn)(struct ksmbd_conn *, int,
293                                                 struct ksmbd_dir_info *,
294                                                 struct ksmbd_kstat *))
295 {
296         int i, rc = 0;
297         struct ksmbd_conn *conn = work->conn;
298         struct user_namespace *user_ns = file_mnt_user_ns(dir->filp);
299
300         for (i = 0; i < 2; i++) {
301                 struct kstat kstat;
302                 struct ksmbd_kstat ksmbd_kstat;
303
304                 if (!dir->dot_dotdot[i]) { /* fill dot entry info */
305                         if (i == 0) {
306                                 d_info->name = ".";
307                                 d_info->name_len = 1;
308                         } else {
309                                 d_info->name = "..";
310                                 d_info->name_len = 2;
311                         }
312
313                         if (!match_pattern(d_info->name, d_info->name_len,
314                                            search_pattern)) {
315                                 dir->dot_dotdot[i] = 1;
316                                 continue;
317                         }
318
319                         ksmbd_kstat.kstat = &kstat;
320                         ksmbd_vfs_fill_dentry_attrs(work,
321                                                     user_ns,
322                                                     dir->filp->f_path.dentry->d_parent,
323                                                     &ksmbd_kstat);
324                         rc = fn(conn, info_level, d_info, &ksmbd_kstat);
325                         if (rc)
326                                 break;
327                         if (d_info->out_buf_len <= 0)
328                                 break;
329
330                         dir->dot_dotdot[i] = 1;
331                         if (d_info->flags & SMB2_RETURN_SINGLE_ENTRY) {
332                                 d_info->out_buf_len = 0;
333                                 break;
334                         }
335                 }
336         }
337
338         return rc;
339 }
340
341 /**
342  * ksmbd_extract_shortname() - get shortname from long filename
343  * @conn:       connection instance
344  * @longname:   source long filename
345  * @shortname:  destination short filename
346  *
347  * Return:      shortname length or 0 when source long name is '.' or '..'
348  * TODO: Though this function comforms the restriction of 8.3 Filename spec,
349  * but the result is different with Windows 7's one. need to check.
350  */
351 int ksmbd_extract_shortname(struct ksmbd_conn *conn, const char *longname,
352                             char *shortname)
353 {
354         const char *p;
355         char base[9], extension[4];
356         char out[13] = {0};
357         int baselen = 0;
358         int extlen = 0, len = 0;
359         unsigned int csum = 0;
360         const unsigned char *ptr;
361         bool dot_present = true;
362
363         p = longname;
364         if ((*p == '.') || (!(strcmp(p, "..")))) {
365                 /*no mangling required */
366                 return 0;
367         }
368
369         p = strrchr(longname, '.');
370         if (p == longname) { /*name starts with a dot*/
371                 strscpy(extension, "___", strlen("___"));
372         } else {
373                 if (p) {
374                         p++;
375                         while (*p && extlen < 3) {
376                                 if (*p != '.')
377                                         extension[extlen++] = toupper(*p);
378                                 p++;
379                         }
380                         extension[extlen] = '\0';
381                 } else {
382                         dot_present = false;
383                 }
384         }
385
386         p = longname;
387         if (*p == '.') {
388                 p++;
389                 longname++;
390         }
391         while (*p && (baselen < 5)) {
392                 if (*p != '.')
393                         base[baselen++] = toupper(*p);
394                 p++;
395         }
396
397         base[baselen] = MAGIC_CHAR;
398         memcpy(out, base, baselen + 1);
399
400         ptr = longname;
401         len = strlen(longname);
402         for (; len > 0; len--, ptr++)
403                 csum += *ptr;
404
405         csum = csum % (MANGLE_BASE * MANGLE_BASE);
406         out[baselen + 1] = mangle(csum / MANGLE_BASE);
407         out[baselen + 2] = mangle(csum);
408         out[baselen + 3] = PERIOD;
409
410         if (dot_present)
411                 memcpy(&out[baselen + 4], extension, 4);
412         else
413                 out[baselen + 4] = '\0';
414         smbConvertToUTF16((__le16 *)shortname, out, PATH_MAX,
415                           conn->local_nls, 0);
416         len = strlen(out) * 2;
417         return len;
418 }
419
420 static int __smb2_negotiate(struct ksmbd_conn *conn)
421 {
422         return (conn->dialect >= SMB20_PROT_ID &&
423                 conn->dialect <= SMB311_PROT_ID);
424 }
425
426 static int smb_handle_negotiate(struct ksmbd_work *work)
427 {
428         struct smb_negotiate_rsp *neg_rsp = work->response_buf;
429
430         ksmbd_debug(SMB, "Unsupported SMB protocol\n");
431         neg_rsp->hdr.Status.CifsError = STATUS_INVALID_LOGON_TYPE;
432         return -EINVAL;
433 }
434
435 int ksmbd_smb_negotiate_common(struct ksmbd_work *work, unsigned int command)
436 {
437         struct ksmbd_conn *conn = work->conn;
438         int ret;
439
440         conn->dialect = ksmbd_negotiate_smb_dialect(work->request_buf);
441         ksmbd_debug(SMB, "conn->dialect 0x%x\n", conn->dialect);
442
443         if (command == SMB2_NEGOTIATE_HE) {
444                 struct smb2_hdr *smb2_hdr = work->request_buf;
445
446                 if (smb2_hdr->ProtocolId != SMB2_PROTO_NUMBER) {
447                         ksmbd_debug(SMB, "Downgrade to SMB1 negotiation\n");
448                         command = SMB_COM_NEGOTIATE;
449                 }
450         }
451
452         if (command == SMB2_NEGOTIATE_HE) {
453                 ret = smb2_handle_negotiate(work);
454                 init_smb2_neg_rsp(work);
455                 return ret;
456         }
457
458         if (command == SMB_COM_NEGOTIATE) {
459                 if (__smb2_negotiate(conn)) {
460                         conn->need_neg = true;
461                         init_smb3_11_server(conn);
462                         init_smb2_neg_rsp(work);
463                         ksmbd_debug(SMB, "Upgrade to SMB2 negotiation\n");
464                         return 0;
465                 }
466                 return smb_handle_negotiate(work);
467         }
468
469         pr_err("Unknown SMB negotiation command: %u\n", command);
470         return -EINVAL;
471 }
472
473 enum SHARED_MODE_ERRORS {
474         SHARE_DELETE_ERROR,
475         SHARE_READ_ERROR,
476         SHARE_WRITE_ERROR,
477         FILE_READ_ERROR,
478         FILE_WRITE_ERROR,
479         FILE_DELETE_ERROR,
480 };
481
482 static const char * const shared_mode_errors[] = {
483         "Current access mode does not permit SHARE_DELETE",
484         "Current access mode does not permit SHARE_READ",
485         "Current access mode does not permit SHARE_WRITE",
486         "Desired access mode does not permit FILE_READ",
487         "Desired access mode does not permit FILE_WRITE",
488         "Desired access mode does not permit FILE_DELETE",
489 };
490
491 static void smb_shared_mode_error(int error, struct ksmbd_file *prev_fp,
492                                   struct ksmbd_file *curr_fp)
493 {
494         ksmbd_debug(SMB, "%s\n", shared_mode_errors[error]);
495         ksmbd_debug(SMB, "Current mode: 0x%x Desired mode: 0x%x\n",
496                     prev_fp->saccess, curr_fp->daccess);
497 }
498
499 int ksmbd_smb_check_shared_mode(struct file *filp, struct ksmbd_file *curr_fp)
500 {
501         int rc = 0;
502         struct ksmbd_file *prev_fp;
503
504         /*
505          * Lookup fp in master fp list, and check desired access and
506          * shared mode between previous open and current open.
507          */
508         read_lock(&curr_fp->f_ci->m_lock);
509         list_for_each_entry(prev_fp, &curr_fp->f_ci->m_fp_list, node) {
510                 if (file_inode(filp) != file_inode(prev_fp->filp))
511                         continue;
512
513                 if (filp == prev_fp->filp)
514                         continue;
515
516                 if (ksmbd_stream_fd(prev_fp) && ksmbd_stream_fd(curr_fp))
517                         if (strcmp(prev_fp->stream.name, curr_fp->stream.name))
518                                 continue;
519
520                 if (prev_fp->attrib_only != curr_fp->attrib_only)
521                         continue;
522
523                 if (!(prev_fp->saccess & FILE_SHARE_DELETE_LE) &&
524                     curr_fp->daccess & FILE_DELETE_LE) {
525                         smb_shared_mode_error(SHARE_DELETE_ERROR,
526                                               prev_fp,
527                                               curr_fp);
528                         rc = -EPERM;
529                         break;
530                 }
531
532                 /*
533                  * Only check FILE_SHARE_DELETE if stream opened and
534                  * normal file opened.
535                  */
536                 if (ksmbd_stream_fd(prev_fp) && !ksmbd_stream_fd(curr_fp))
537                         continue;
538
539                 if (!(prev_fp->saccess & FILE_SHARE_READ_LE) &&
540                     curr_fp->daccess & (FILE_EXECUTE_LE | FILE_READ_DATA_LE)) {
541                         smb_shared_mode_error(SHARE_READ_ERROR,
542                                               prev_fp,
543                                               curr_fp);
544                         rc = -EPERM;
545                         break;
546                 }
547
548                 if (!(prev_fp->saccess & FILE_SHARE_WRITE_LE) &&
549                     curr_fp->daccess & (FILE_WRITE_DATA_LE | FILE_APPEND_DATA_LE)) {
550                         smb_shared_mode_error(SHARE_WRITE_ERROR,
551                                               prev_fp,
552                                               curr_fp);
553                         rc = -EPERM;
554                         break;
555                 }
556
557                 if (prev_fp->daccess & (FILE_EXECUTE_LE | FILE_READ_DATA_LE) &&
558                     !(curr_fp->saccess & FILE_SHARE_READ_LE)) {
559                         smb_shared_mode_error(FILE_READ_ERROR,
560                                               prev_fp,
561                                               curr_fp);
562                         rc = -EPERM;
563                         break;
564                 }
565
566                 if (prev_fp->daccess & (FILE_WRITE_DATA_LE | FILE_APPEND_DATA_LE) &&
567                     !(curr_fp->saccess & FILE_SHARE_WRITE_LE)) {
568                         smb_shared_mode_error(FILE_WRITE_ERROR,
569                                               prev_fp,
570                                               curr_fp);
571                         rc = -EPERM;
572                         break;
573                 }
574
575                 if (prev_fp->daccess & FILE_DELETE_LE &&
576                     !(curr_fp->saccess & FILE_SHARE_DELETE_LE)) {
577                         smb_shared_mode_error(FILE_DELETE_ERROR,
578                                               prev_fp,
579                                               curr_fp);
580                         rc = -EPERM;
581                         break;
582                 }
583         }
584         read_unlock(&curr_fp->f_ci->m_lock);
585
586         return rc;
587 }
588
589 bool is_asterisk(char *p)
590 {
591         return p && p[0] == '*';
592 }
593
594 int ksmbd_override_fsids(struct ksmbd_work *work)
595 {
596         struct ksmbd_session *sess = work->sess;
597         struct ksmbd_share_config *share = work->tcon->share_conf;
598         struct cred *cred;
599         struct group_info *gi;
600         unsigned int uid;
601         unsigned int gid;
602
603         uid = user_uid(sess->user);
604         gid = user_gid(sess->user);
605         if (share->force_uid != KSMBD_SHARE_INVALID_UID)
606                 uid = share->force_uid;
607         if (share->force_gid != KSMBD_SHARE_INVALID_GID)
608                 gid = share->force_gid;
609
610         cred = prepare_kernel_cred(NULL);
611         if (!cred)
612                 return -ENOMEM;
613
614         cred->fsuid = make_kuid(current_user_ns(), uid);
615         cred->fsgid = make_kgid(current_user_ns(), gid);
616
617         gi = groups_alloc(0);
618         if (!gi) {
619                 abort_creds(cred);
620                 return -ENOMEM;
621         }
622         set_groups(cred, gi);
623         put_group_info(gi);
624
625         if (!uid_eq(cred->fsuid, GLOBAL_ROOT_UID))
626                 cred->cap_effective = cap_drop_fs_set(cred->cap_effective);
627
628         WARN_ON(work->saved_cred);
629         work->saved_cred = override_creds(cred);
630         if (!work->saved_cred) {
631                 abort_creds(cred);
632                 return -EINVAL;
633         }
634         return 0;
635 }
636
637 void ksmbd_revert_fsids(struct ksmbd_work *work)
638 {
639         const struct cred *cred;
640
641         WARN_ON(!work->saved_cred);
642
643         cred = current_cred();
644         revert_creds(work->saved_cred);
645         put_cred(cred);
646         work->saved_cred = NULL;
647 }
648
649 __le32 smb_map_generic_desired_access(__le32 daccess)
650 {
651         if (daccess & FILE_GENERIC_READ_LE) {
652                 daccess |= cpu_to_le32(GENERIC_READ_FLAGS);
653                 daccess &= ~FILE_GENERIC_READ_LE;
654         }
655
656         if (daccess & FILE_GENERIC_WRITE_LE) {
657                 daccess |= cpu_to_le32(GENERIC_WRITE_FLAGS);
658                 daccess &= ~FILE_GENERIC_WRITE_LE;
659         }
660
661         if (daccess & FILE_GENERIC_EXECUTE_LE) {
662                 daccess |= cpu_to_le32(GENERIC_EXECUTE_FLAGS);
663                 daccess &= ~FILE_GENERIC_EXECUTE_LE;
664         }
665
666         if (daccess & FILE_GENERIC_ALL_LE) {
667                 daccess |= cpu_to_le32(GENERIC_ALL_FLAGS);
668                 daccess &= ~FILE_GENERIC_ALL_LE;
669         }
670
671         return daccess;
672 }