1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2018 Samsung Electronics Co., Ltd.
7 #include <linux/user_namespace.h>
9 #include "smb_common.h"
12 #include "smbstatus.h"
13 #include "connection.h"
14 #include "ksmbd_work.h"
15 #include "mgmt/user_session.h"
16 #include "mgmt/user_config.h"
17 #include "mgmt/tree_connect.h"
18 #include "mgmt/share_config.h"
20 /*for shortname implementation */
21 static const char basechars[43] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_-!@#$%";
22 #define MANGLE_BASE (sizeof(basechars) / sizeof(char) - 1)
23 #define MAGIC_CHAR '~'
25 #define mangle(V) ((char)(basechars[(V) % MANGLE_BASE]))
34 static struct smb_protocol smb1_protos[] = {
49 static struct smb_protocol smb2_protos[] = {
76 unsigned int ksmbd_server_side_copy_max_chunk_count(void)
81 unsigned int ksmbd_server_side_copy_max_chunk_size(void)
83 return (2U << 30) - 1;
86 unsigned int ksmbd_server_side_copy_max_total_size(void)
88 return (2U << 30) - 1;
91 inline int ksmbd_min_protocol(void)
96 inline int ksmbd_max_protocol(void)
101 int ksmbd_lookup_protocol_idx(char *str)
103 int offt = ARRAY_SIZE(smb1_protos) - 1;
104 int len = strlen(str);
107 if (!strncmp(str, smb1_protos[offt].prot, len)) {
108 ksmbd_debug(SMB, "selected %s dialect idx = %d\n",
109 smb1_protos[offt].prot, offt);
110 return smb1_protos[offt].index;
115 offt = ARRAY_SIZE(smb2_protos) - 1;
117 if (!strncmp(str, smb2_protos[offt].prot, len)) {
118 ksmbd_debug(SMB, "selected %s dialect idx = %d\n",
119 smb2_protos[offt].prot, offt);
120 return smb2_protos[offt].index;
128 * ksmbd_verify_smb_message() - check for valid smb2 request header
131 * check for valid smb signature and packet direction(request/response)
133 * Return: 0 on success, otherwise -EINVAL
135 int ksmbd_verify_smb_message(struct ksmbd_work *work)
137 struct smb2_hdr *smb2_hdr = ksmbd_req_buf_next(work);
140 if (smb2_hdr->ProtocolId == SMB2_PROTO_NUMBER)
141 return ksmbd_smb2_check_message(work);
143 hdr = work->request_buf;
144 if (*(__le32 *)hdr->Protocol == SMB1_PROTO_NUMBER &&
145 hdr->Command == SMB_COM_NEGOTIATE) {
146 work->conn->outstanding_credits++;
154 * ksmbd_smb_request() - check for valid smb request type
155 * @conn: connection instance
157 * Return: true on success, otherwise false
159 bool ksmbd_smb_request(struct ksmbd_conn *conn)
161 __le32 *proto = (__le32 *)smb2_get_msg(conn->request_buf);
163 if (*proto == SMB2_COMPRESSION_TRANSFORM_ID) {
164 pr_err_ratelimited("smb2 compression not support yet");
168 if (*proto != SMB1_PROTO_NUMBER &&
169 *proto != SMB2_PROTO_NUMBER &&
170 *proto != SMB2_TRANSFORM_PROTO_NUM)
176 static bool supported_protocol(int idx)
178 if (idx == SMB2X_PROT &&
179 (server_conf.min_protocol >= SMB21_PROT ||
180 server_conf.max_protocol <= SMB311_PROT))
183 return (server_conf.min_protocol <= idx &&
184 idx <= server_conf.max_protocol);
187 static char *next_dialect(char *dialect, int *next_off, int bcount)
189 dialect = dialect + *next_off;
190 *next_off = strnlen(dialect, bcount);
191 if (dialect[*next_off] != '\0')
196 static int ksmbd_lookup_dialect_by_name(char *cli_dialects, __le16 byte_count)
198 int i, seq_num, bcount, next;
201 for (i = ARRAY_SIZE(smb1_protos) - 1; i >= 0; i--) {
204 dialect = cli_dialects;
205 bcount = le16_to_cpu(byte_count);
207 dialect = next_dialect(dialect, &next, bcount);
210 ksmbd_debug(SMB, "client requested dialect %s\n",
212 if (!strcmp(dialect, smb1_protos[i].name)) {
213 if (supported_protocol(smb1_protos[i].index)) {
215 "selected %s dialect\n",
216 smb1_protos[i].name);
217 if (smb1_protos[i].index == SMB1_PROT)
219 return smb1_protos[i].prot_id;
224 } while (bcount > 0);
230 int ksmbd_lookup_dialect_by_id(__le16 *cli_dialects, __le16 dialects_count)
235 for (i = ARRAY_SIZE(smb2_protos) - 1; i >= 0; i--) {
236 count = le16_to_cpu(dialects_count);
237 while (--count >= 0) {
238 ksmbd_debug(SMB, "client requested dialect 0x%x\n",
239 le16_to_cpu(cli_dialects[count]));
240 if (le16_to_cpu(cli_dialects[count]) !=
241 smb2_protos[i].prot_id)
244 if (supported_protocol(smb2_protos[i].index)) {
245 ksmbd_debug(SMB, "selected %s dialect\n",
246 smb2_protos[i].name);
247 return smb2_protos[i].prot_id;
255 static int ksmbd_negotiate_smb_dialect(void *buf)
257 int smb_buf_length = get_rfc1002_len(buf);
258 __le32 proto = ((struct smb2_hdr *)smb2_get_msg(buf))->ProtocolId;
260 if (proto == SMB2_PROTO_NUMBER) {
261 struct smb2_negotiate_req *req;
263 offsetof(struct smb2_negotiate_req, Dialects);
265 req = (struct smb2_negotiate_req *)smb2_get_msg(buf);
266 if (smb2_neg_size > smb_buf_length)
269 if (struct_size(req, Dialects, le16_to_cpu(req->DialectCount)) >
273 return ksmbd_lookup_dialect_by_id(req->Dialects,
277 proto = *(__le32 *)((struct smb_hdr *)buf)->Protocol;
278 if (proto == SMB1_PROTO_NUMBER) {
279 struct smb_negotiate_req *req;
281 req = (struct smb_negotiate_req *)buf;
282 if (le16_to_cpu(req->ByteCount) < 2)
285 if (offsetof(struct smb_negotiate_req, DialectsArray) - 4 +
286 le16_to_cpu(req->ByteCount) > smb_buf_length) {
290 return ksmbd_lookup_dialect_by_name(req->DialectsArray,
298 #define SMB_COM_NEGOTIATE_EX 0x0
301 * get_smb1_cmd_val() - get smb command value from smb header
302 * @work: smb work containing smb header
304 * Return: smb command value
306 static u16 get_smb1_cmd_val(struct ksmbd_work *work)
308 return SMB_COM_NEGOTIATE_EX;
312 * init_smb1_rsp_hdr() - initialize smb negotiate response header
313 * @work: smb work containing smb request
315 * Return: 0 on success, otherwise -EINVAL
317 static int init_smb1_rsp_hdr(struct ksmbd_work *work)
319 struct smb_hdr *rsp_hdr = (struct smb_hdr *)work->response_buf;
320 struct smb_hdr *rcv_hdr = (struct smb_hdr *)work->request_buf;
322 rsp_hdr->Command = SMB_COM_NEGOTIATE;
323 *(__le32 *)rsp_hdr->Protocol = SMB1_PROTO_NUMBER;
324 rsp_hdr->Flags = SMBFLG_RESPONSE;
325 rsp_hdr->Flags2 = SMBFLG2_UNICODE | SMBFLG2_ERR_STATUS |
326 SMBFLG2_EXT_SEC | SMBFLG2_IS_LONG_NAME;
327 rsp_hdr->Pid = rcv_hdr->Pid;
328 rsp_hdr->Mid = rcv_hdr->Mid;
333 * smb1_check_user_session() - check for valid session for a user
334 * @work: smb work containing smb request buffer
336 * Return: 0 on success, otherwise error
338 static int smb1_check_user_session(struct ksmbd_work *work)
340 unsigned int cmd = work->conn->ops->get_cmd_val(work);
342 if (cmd == SMB_COM_NEGOTIATE_EX)
349 * smb1_allocate_rsp_buf() - allocate response buffer for a command
350 * @work: smb work containing smb request
352 * Return: 0 on success, otherwise -ENOMEM
354 static int smb1_allocate_rsp_buf(struct ksmbd_work *work)
356 work->response_buf = kzalloc(MAX_CIFS_SMALL_BUFFER_SIZE,
358 work->response_sz = MAX_CIFS_SMALL_BUFFER_SIZE;
360 if (!work->response_buf) {
361 pr_err("Failed to allocate %u bytes buffer\n",
362 MAX_CIFS_SMALL_BUFFER_SIZE);
370 * set_smb1_rsp_status() - set error type in smb response header
371 * @work: smb work containing smb response header
372 * @err: error code to set in response
374 static void set_smb1_rsp_status(struct ksmbd_work *work, __le32 err)
376 work->send_no_response = 1;
379 static struct smb_version_ops smb1_server_ops = {
380 .get_cmd_val = get_smb1_cmd_val,
381 .init_rsp_hdr = init_smb1_rsp_hdr,
382 .allocate_rsp_buf = smb1_allocate_rsp_buf,
383 .check_user_session = smb1_check_user_session,
384 .set_rsp_status = set_smb1_rsp_status,
387 static int smb1_negotiate(struct ksmbd_work *work)
389 return ksmbd_smb_negotiate_common(work, SMB_COM_NEGOTIATE);
392 static struct smb_version_cmds smb1_server_cmds[1] = {
393 [SMB_COM_NEGOTIATE_EX] = { .proc = smb1_negotiate, },
396 static int init_smb1_server(struct ksmbd_conn *conn)
398 conn->ops = &smb1_server_ops;
399 conn->cmds = smb1_server_cmds;
400 conn->max_cmds = ARRAY_SIZE(smb1_server_cmds);
404 int ksmbd_init_smb_server(struct ksmbd_work *work)
406 struct ksmbd_conn *conn = work->conn;
409 proto = *(__le32 *)((struct smb_hdr *)work->request_buf)->Protocol;
410 if (conn->need_neg == false) {
411 if (proto == SMB1_PROTO_NUMBER)
416 if (proto == SMB1_PROTO_NUMBER)
417 return init_smb1_server(conn);
418 return init_smb3_11_server(conn);
421 int ksmbd_populate_dot_dotdot_entries(struct ksmbd_work *work, int info_level,
422 struct ksmbd_file *dir,
423 struct ksmbd_dir_info *d_info,
424 char *search_pattern,
425 int (*fn)(struct ksmbd_conn *, int,
426 struct ksmbd_dir_info *,
427 struct ksmbd_kstat *))
430 struct ksmbd_conn *conn = work->conn;
431 struct mnt_idmap *idmap = file_mnt_idmap(dir->filp);
433 for (i = 0; i < 2; i++) {
435 struct ksmbd_kstat ksmbd_kstat;
436 struct dentry *dentry;
438 if (!dir->dot_dotdot[i]) { /* fill dot entry info */
441 d_info->name_len = 1;
442 dentry = dir->filp->f_path.dentry;
445 d_info->name_len = 2;
446 dentry = dir->filp->f_path.dentry->d_parent;
449 if (!match_pattern(d_info->name, d_info->name_len,
451 dir->dot_dotdot[i] = 1;
455 ksmbd_kstat.kstat = &kstat;
456 ksmbd_vfs_fill_dentry_attrs(work,
460 rc = fn(conn, info_level, d_info, &ksmbd_kstat);
463 if (d_info->out_buf_len <= 0)
466 dir->dot_dotdot[i] = 1;
467 if (d_info->flags & SMB2_RETURN_SINGLE_ENTRY) {
468 d_info->out_buf_len = 0;
478 * ksmbd_extract_shortname() - get shortname from long filename
479 * @conn: connection instance
480 * @longname: source long filename
481 * @shortname: destination short filename
483 * Return: shortname length or 0 when source long name is '.' or '..'
484 * TODO: Though this function comforms the restriction of 8.3 Filename spec,
485 * but the result is different with Windows 7's one. need to check.
487 int ksmbd_extract_shortname(struct ksmbd_conn *conn, const char *longname,
491 char base[9], extension[4];
494 int extlen = 0, len = 0;
495 unsigned int csum = 0;
496 const unsigned char *ptr;
497 bool dot_present = true;
500 if ((*p == '.') || (!(strcmp(p, "..")))) {
501 /*no mangling required */
505 p = strrchr(longname, '.');
506 if (p == longname) { /*name starts with a dot*/
507 strscpy(extension, "___", strlen("___"));
511 while (*p && extlen < 3) {
513 extension[extlen++] = toupper(*p);
516 extension[extlen] = '\0';
527 while (*p && (baselen < 5)) {
529 base[baselen++] = toupper(*p);
533 base[baselen] = MAGIC_CHAR;
534 memcpy(out, base, baselen + 1);
537 len = strlen(longname);
538 for (; len > 0; len--, ptr++)
541 csum = csum % (MANGLE_BASE * MANGLE_BASE);
542 out[baselen + 1] = mangle(csum / MANGLE_BASE);
543 out[baselen + 2] = mangle(csum);
544 out[baselen + 3] = PERIOD;
547 memcpy(out + baselen + 4, extension, 4);
549 out[baselen + 4] = '\0';
550 smbConvertToUTF16((__le16 *)shortname, out, PATH_MAX,
552 len = strlen(out) * 2;
556 static int __smb2_negotiate(struct ksmbd_conn *conn)
558 return (conn->dialect >= SMB20_PROT_ID &&
559 conn->dialect <= SMB311_PROT_ID);
562 static int smb_handle_negotiate(struct ksmbd_work *work)
564 struct smb_negotiate_rsp *neg_rsp = work->response_buf;
566 ksmbd_debug(SMB, "Unsupported SMB1 protocol\n");
568 if (ksmbd_iov_pin_rsp(work, (void *)neg_rsp,
569 sizeof(struct smb_negotiate_rsp) - 4))
572 neg_rsp->hdr.Status.CifsError = STATUS_SUCCESS;
573 neg_rsp->hdr.WordCount = 1;
574 neg_rsp->DialectIndex = cpu_to_le16(work->conn->dialect);
575 neg_rsp->ByteCount = 0;
579 int ksmbd_smb_negotiate_common(struct ksmbd_work *work, unsigned int command)
581 struct ksmbd_conn *conn = work->conn;
585 ksmbd_negotiate_smb_dialect(work->request_buf);
586 ksmbd_debug(SMB, "conn->dialect 0x%x\n", conn->dialect);
588 if (command == SMB2_NEGOTIATE_HE) {
589 ret = smb2_handle_negotiate(work);
593 if (command == SMB_COM_NEGOTIATE) {
594 if (__smb2_negotiate(conn)) {
595 init_smb3_11_server(conn);
596 init_smb2_neg_rsp(work);
597 ksmbd_debug(SMB, "Upgrade to SMB2 negotiation\n");
600 return smb_handle_negotiate(work);
603 pr_err("Unknown SMB negotiation command: %u\n", command);
607 enum SHARED_MODE_ERRORS {
616 static const char * const shared_mode_errors[] = {
617 "Current access mode does not permit SHARE_DELETE",
618 "Current access mode does not permit SHARE_READ",
619 "Current access mode does not permit SHARE_WRITE",
620 "Desired access mode does not permit FILE_READ",
621 "Desired access mode does not permit FILE_WRITE",
622 "Desired access mode does not permit FILE_DELETE",
625 static void smb_shared_mode_error(int error, struct ksmbd_file *prev_fp,
626 struct ksmbd_file *curr_fp)
628 ksmbd_debug(SMB, "%s\n", shared_mode_errors[error]);
629 ksmbd_debug(SMB, "Current mode: 0x%x Desired mode: 0x%x\n",
630 prev_fp->saccess, curr_fp->daccess);
633 int ksmbd_smb_check_shared_mode(struct file *filp, struct ksmbd_file *curr_fp)
636 struct ksmbd_file *prev_fp;
639 * Lookup fp in master fp list, and check desired access and
640 * shared mode between previous open and current open.
642 read_lock(&curr_fp->f_ci->m_lock);
643 list_for_each_entry(prev_fp, &curr_fp->f_ci->m_fp_list, node) {
644 if (file_inode(filp) != file_inode(prev_fp->filp))
647 if (filp == prev_fp->filp)
650 if (ksmbd_stream_fd(prev_fp) && ksmbd_stream_fd(curr_fp))
651 if (strcmp(prev_fp->stream.name, curr_fp->stream.name))
654 if (prev_fp->attrib_only != curr_fp->attrib_only)
657 if (!(prev_fp->saccess & FILE_SHARE_DELETE_LE) &&
658 curr_fp->daccess & FILE_DELETE_LE) {
659 smb_shared_mode_error(SHARE_DELETE_ERROR,
667 * Only check FILE_SHARE_DELETE if stream opened and
668 * normal file opened.
670 if (ksmbd_stream_fd(prev_fp) && !ksmbd_stream_fd(curr_fp))
673 if (!(prev_fp->saccess & FILE_SHARE_READ_LE) &&
674 curr_fp->daccess & (FILE_EXECUTE_LE | FILE_READ_DATA_LE)) {
675 smb_shared_mode_error(SHARE_READ_ERROR,
682 if (!(prev_fp->saccess & FILE_SHARE_WRITE_LE) &&
683 curr_fp->daccess & (FILE_WRITE_DATA_LE | FILE_APPEND_DATA_LE)) {
684 smb_shared_mode_error(SHARE_WRITE_ERROR,
691 if (prev_fp->daccess & (FILE_EXECUTE_LE | FILE_READ_DATA_LE) &&
692 !(curr_fp->saccess & FILE_SHARE_READ_LE)) {
693 smb_shared_mode_error(FILE_READ_ERROR,
700 if (prev_fp->daccess & (FILE_WRITE_DATA_LE | FILE_APPEND_DATA_LE) &&
701 !(curr_fp->saccess & FILE_SHARE_WRITE_LE)) {
702 smb_shared_mode_error(FILE_WRITE_ERROR,
709 if (prev_fp->daccess & FILE_DELETE_LE &&
710 !(curr_fp->saccess & FILE_SHARE_DELETE_LE)) {
711 smb_shared_mode_error(FILE_DELETE_ERROR,
718 read_unlock(&curr_fp->f_ci->m_lock);
723 bool is_asterisk(char *p)
725 return p && p[0] == '*';
728 int ksmbd_override_fsids(struct ksmbd_work *work)
730 struct ksmbd_session *sess = work->sess;
731 struct ksmbd_share_config *share = work->tcon->share_conf;
733 struct group_info *gi;
737 uid = user_uid(sess->user);
738 gid = user_gid(sess->user);
739 if (share->force_uid != KSMBD_SHARE_INVALID_UID)
740 uid = share->force_uid;
741 if (share->force_gid != KSMBD_SHARE_INVALID_GID)
742 gid = share->force_gid;
744 cred = prepare_kernel_cred(&init_task);
748 cred->fsuid = make_kuid(&init_user_ns, uid);
749 cred->fsgid = make_kgid(&init_user_ns, gid);
751 gi = groups_alloc(0);
756 set_groups(cred, gi);
759 if (!uid_eq(cred->fsuid, GLOBAL_ROOT_UID))
760 cred->cap_effective = cap_drop_fs_set(cred->cap_effective);
762 WARN_ON(work->saved_cred);
763 work->saved_cred = override_creds(cred);
764 if (!work->saved_cred) {
771 void ksmbd_revert_fsids(struct ksmbd_work *work)
773 const struct cred *cred;
775 WARN_ON(!work->saved_cred);
777 cred = current_cred();
778 revert_creds(work->saved_cred);
780 work->saved_cred = NULL;
783 __le32 smb_map_generic_desired_access(__le32 daccess)
785 if (daccess & FILE_GENERIC_READ_LE) {
786 daccess |= cpu_to_le32(GENERIC_READ_FLAGS);
787 daccess &= ~FILE_GENERIC_READ_LE;
790 if (daccess & FILE_GENERIC_WRITE_LE) {
791 daccess |= cpu_to_le32(GENERIC_WRITE_FLAGS);
792 daccess &= ~FILE_GENERIC_WRITE_LE;
795 if (daccess & FILE_GENERIC_EXECUTE_LE) {
796 daccess |= cpu_to_le32(GENERIC_EXECUTE_FLAGS);
797 daccess &= ~FILE_GENERIC_EXECUTE_LE;
800 if (daccess & FILE_GENERIC_ALL_LE) {
801 daccess |= cpu_to_le32(GENERIC_ALL_FLAGS);
802 daccess &= ~FILE_GENERIC_ALL_LE;