1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2018 Samsung Electronics Co., Ltd.
4 * Copyright (C) 2018 Namjae Jeon <linkinjeon@kernel.org>
7 #include "smb_common.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"
18 /*for shortname implementation */
19 static const char basechars[43] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_-!@#$%";
20 #define MANGLE_BASE (sizeof(basechars) / sizeof(char) - 1)
21 #define MAGIC_CHAR '~'
23 #define mangle(V) ((char)(basechars[(V) % MANGLE_BASE]))
24 #define KSMBD_MIN_SUPPORTED_HEADER_SIZE (sizeof(struct smb2_hdr))
33 static struct smb_protocol smb1_protos[] = {
48 static struct smb_protocol smb2_protos[] = {
75 unsigned int ksmbd_server_side_copy_max_chunk_count(void)
80 unsigned int ksmbd_server_side_copy_max_chunk_size(void)
82 return (2U << 30) - 1;
85 unsigned int ksmbd_server_side_copy_max_total_size(void)
87 return (2U << 30) - 1;
90 inline int ksmbd_min_protocol(void)
95 inline int ksmbd_max_protocol(void)
100 int ksmbd_lookup_protocol_idx(char *str)
102 int offt = ARRAY_SIZE(smb1_protos) - 1;
103 int len = strlen(str);
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;
114 offt = ARRAY_SIZE(smb2_protos) - 1;
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;
127 * ksmbd_verify_smb_message() - check for valid smb2 request header
130 * check for valid smb signature and packet direction(request/response)
132 * Return: 0 on success, otherwise 1
134 int ksmbd_verify_smb_message(struct ksmbd_work *work)
136 struct smb2_hdr *smb2_hdr = work->request_buf;
138 if (smb2_hdr->ProtocolId == SMB2_PROTO_NUMBER)
139 return ksmbd_smb2_check_message(work);
145 * ksmbd_smb_request() - check for valid smb request type
146 * @conn: connection instance
148 * Return: true on success, otherwise false
150 bool ksmbd_smb_request(struct ksmbd_conn *conn)
152 int type = *(char *)conn->request_buf;
155 case RFC1002_SESSION_MESSAGE:
156 /* Regular SMB request */
158 case RFC1002_SESSION_KEEP_ALIVE:
159 ksmbd_debug(SMB, "RFC 1002 session keep alive\n");
162 ksmbd_debug(SMB, "RFC 1002 unknown request type 0x%x\n", type);
168 static bool supported_protocol(int idx)
170 if (idx == SMB2X_PROT &&
171 (server_conf.min_protocol >= SMB21_PROT ||
172 server_conf.max_protocol <= SMB311_PROT))
175 return (server_conf.min_protocol <= idx &&
176 idx <= server_conf.max_protocol);
179 static char *next_dialect(char *dialect, int *next_off)
181 dialect = dialect + *next_off;
182 *next_off = strlen(dialect);
186 static int ksmbd_lookup_dialect_by_name(char *cli_dialects, __le16 byte_count)
188 int i, seq_num, bcount, next;
191 for (i = ARRAY_SIZE(smb1_protos) - 1; i >= 0; i--) {
194 dialect = cli_dialects;
195 bcount = le16_to_cpu(byte_count);
197 dialect = next_dialect(dialect, &next);
198 ksmbd_debug(SMB, "client requested dialect %s\n",
200 if (!strcmp(dialect, smb1_protos[i].name)) {
201 if (supported_protocol(smb1_protos[i].index)) {
203 "selected %s dialect\n",
204 smb1_protos[i].name);
205 if (smb1_protos[i].index == SMB1_PROT)
207 return smb1_protos[i].prot_id;
212 } while (bcount > 0);
218 int ksmbd_lookup_dialect_by_id(__le16 *cli_dialects, __le16 dialects_count)
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)
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;
243 static int ksmbd_negotiate_smb_dialect(void *buf)
247 proto = ((struct smb2_hdr *)buf)->ProtocolId;
248 if (proto == SMB2_PROTO_NUMBER) {
249 struct smb2_negotiate_req *req;
251 req = (struct smb2_negotiate_req *)buf;
252 return ksmbd_lookup_dialect_by_id(req->Dialects,
256 proto = *(__le32 *)((struct smb_hdr *)buf)->Protocol;
257 if (proto == SMB1_PROTO_NUMBER) {
258 struct smb_negotiate_req *req;
260 req = (struct smb_negotiate_req *)buf;
261 return ksmbd_lookup_dialect_by_name(req->DialectsArray,
268 #define SMB_COM_NEGOTIATE 0x72
269 int ksmbd_init_smb_server(struct ksmbd_work *work)
271 struct ksmbd_conn *conn = work->conn;
273 if (conn->need_neg == false)
276 init_smb3_11_server(conn);
278 if (conn->ops->get_cmd_val(work) != SMB_COM_NEGOTIATE)
279 conn->need_neg = false;
283 bool ksmbd_pdu_size_has_room(unsigned int pdu)
285 return (pdu >= KSMBD_MIN_SUPPORTED_HEADER_SIZE - 4);
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 *))
297 struct ksmbd_conn *conn = work->conn;
298 struct user_namespace *user_ns = file_mnt_user_ns(dir->filp);
300 for (i = 0; i < 2; i++) {
302 struct ksmbd_kstat ksmbd_kstat;
304 if (!dir->dot_dotdot[i]) { /* fill dot entry info */
307 d_info->name_len = 1;
310 d_info->name_len = 2;
313 if (!match_pattern(d_info->name, d_info->name_len,
315 dir->dot_dotdot[i] = 1;
319 ksmbd_kstat.kstat = &kstat;
320 ksmbd_vfs_fill_dentry_attrs(work,
322 dir->filp->f_path.dentry->d_parent,
324 rc = fn(conn, info_level, d_info, &ksmbd_kstat);
327 if (d_info->out_buf_len <= 0)
330 dir->dot_dotdot[i] = 1;
331 if (d_info->flags & SMB2_RETURN_SINGLE_ENTRY) {
332 d_info->out_buf_len = 0;
342 * ksmbd_extract_shortname() - get shortname from long filename
343 * @conn: connection instance
344 * @longname: source long filename
345 * @shortname: destination short filename
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.
351 int ksmbd_extract_shortname(struct ksmbd_conn *conn, const char *longname,
355 char base[9], extension[4];
358 int extlen = 0, len = 0;
359 unsigned int csum = 0;
360 const unsigned char *ptr;
361 bool dot_present = true;
364 if ((*p == '.') || (!(strcmp(p, "..")))) {
365 /*no mangling required */
369 p = strrchr(longname, '.');
370 if (p == longname) { /*name starts with a dot*/
371 strscpy(extension, "___", strlen("___"));
375 while (*p && extlen < 3) {
377 extension[extlen++] = toupper(*p);
380 extension[extlen] = '\0';
391 while (*p && (baselen < 5)) {
393 base[baselen++] = toupper(*p);
397 base[baselen] = MAGIC_CHAR;
398 memcpy(out, base, baselen + 1);
401 len = strlen(longname);
402 for (; len > 0; len--, ptr++)
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;
411 memcpy(&out[baselen + 4], extension, 4);
413 out[baselen + 4] = '\0';
414 smbConvertToUTF16((__le16 *)shortname, out, PATH_MAX,
416 len = strlen(out) * 2;
420 static int __smb2_negotiate(struct ksmbd_conn *conn)
422 return (conn->dialect >= SMB20_PROT_ID &&
423 conn->dialect <= SMB311_PROT_ID);
426 static int smb_handle_negotiate(struct ksmbd_work *work)
428 struct smb_negotiate_rsp *neg_rsp = work->response_buf;
430 ksmbd_debug(SMB, "Unsupported SMB protocol\n");
431 neg_rsp->hdr.Status.CifsError = STATUS_INVALID_LOGON_TYPE;
435 int ksmbd_smb_negotiate_common(struct ksmbd_work *work, unsigned int command)
437 struct ksmbd_conn *conn = work->conn;
440 conn->dialect = ksmbd_negotiate_smb_dialect(work->request_buf);
441 ksmbd_debug(SMB, "conn->dialect 0x%x\n", conn->dialect);
443 if (command == SMB2_NEGOTIATE_HE) {
444 struct smb2_hdr *smb2_hdr = work->request_buf;
446 if (smb2_hdr->ProtocolId != SMB2_PROTO_NUMBER) {
447 ksmbd_debug(SMB, "Downgrade to SMB1 negotiation\n");
448 command = SMB_COM_NEGOTIATE;
452 if (command == SMB2_NEGOTIATE_HE) {
453 ret = smb2_handle_negotiate(work);
454 init_smb2_neg_rsp(work);
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");
466 return smb_handle_negotiate(work);
469 pr_err("Unknown SMB negotiation command: %u\n", command);
473 enum SHARED_MODE_ERRORS {
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",
491 static void smb_shared_mode_error(int error, struct ksmbd_file *prev_fp,
492 struct ksmbd_file *curr_fp)
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);
499 int ksmbd_smb_check_shared_mode(struct file *filp, struct ksmbd_file *curr_fp)
502 struct ksmbd_file *prev_fp;
505 * Lookup fp in master fp list, and check desired access and
506 * shared mode between previous open and current open.
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))
513 if (filp == prev_fp->filp)
516 if (ksmbd_stream_fd(prev_fp) && ksmbd_stream_fd(curr_fp))
517 if (strcmp(prev_fp->stream.name, curr_fp->stream.name))
520 if (prev_fp->attrib_only != curr_fp->attrib_only)
523 if (!(prev_fp->saccess & FILE_SHARE_DELETE_LE) &&
524 curr_fp->daccess & FILE_DELETE_LE) {
525 smb_shared_mode_error(SHARE_DELETE_ERROR,
533 * Only check FILE_SHARE_DELETE if stream opened and
534 * normal file opened.
536 if (ksmbd_stream_fd(prev_fp) && !ksmbd_stream_fd(curr_fp))
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,
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,
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,
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,
575 if (prev_fp->daccess & FILE_DELETE_LE &&
576 !(curr_fp->saccess & FILE_SHARE_DELETE_LE)) {
577 smb_shared_mode_error(FILE_DELETE_ERROR,
584 read_unlock(&curr_fp->f_ci->m_lock);
589 bool is_asterisk(char *p)
591 return p && p[0] == '*';
594 int ksmbd_override_fsids(struct ksmbd_work *work)
596 struct ksmbd_session *sess = work->sess;
597 struct ksmbd_share_config *share = work->tcon->share_conf;
599 struct group_info *gi;
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;
610 cred = prepare_kernel_cred(NULL);
614 cred->fsuid = make_kuid(current_user_ns(), uid);
615 cred->fsgid = make_kgid(current_user_ns(), gid);
617 gi = groups_alloc(0);
622 set_groups(cred, gi);
625 if (!uid_eq(cred->fsuid, GLOBAL_ROOT_UID))
626 cred->cap_effective = cap_drop_fs_set(cred->cap_effective);
628 WARN_ON(work->saved_cred);
629 work->saved_cred = override_creds(cred);
630 if (!work->saved_cred) {
637 void ksmbd_revert_fsids(struct ksmbd_work *work)
639 const struct cred *cred;
641 WARN_ON(!work->saved_cred);
643 cred = current_cred();
644 revert_creds(work->saved_cred);
646 work->saved_cred = NULL;
649 __le32 smb_map_generic_desired_access(__le32 daccess)
651 if (daccess & FILE_GENERIC_READ_LE) {
652 daccess |= cpu_to_le32(GENERIC_READ_FLAGS);
653 daccess &= ~FILE_GENERIC_READ_LE;
656 if (daccess & FILE_GENERIC_WRITE_LE) {
657 daccess |= cpu_to_le32(GENERIC_WRITE_FLAGS);
658 daccess &= ~FILE_GENERIC_WRITE_LE;
661 if (daccess & FILE_GENERIC_EXECUTE_LE) {
662 daccess |= cpu_to_le32(GENERIC_EXECUTE_FLAGS);
663 daccess &= ~FILE_GENERIC_EXECUTE_LE;
666 if (daccess & FILE_GENERIC_ALL_LE) {
667 daccess |= cpu_to_le32(GENERIC_ALL_FLAGS);
668 daccess &= ~FILE_GENERIC_ALL_LE;