138c8a9a5SSteve French // SPDX-License-Identifier: GPL-2.0-or-later 238c8a9a5SSteve French /* 338c8a9a5SSteve French * Copyright (C) 2018 Samsung Electronics Co., Ltd. 438c8a9a5SSteve French * Copyright (C) 2018 Namjae Jeon <linkinjeon@kernel.org> 538c8a9a5SSteve French */ 638c8a9a5SSteve French 738c8a9a5SSteve French #include <linux/user_namespace.h> 838c8a9a5SSteve French 938c8a9a5SSteve French #include "smb_common.h" 1038c8a9a5SSteve French #include "server.h" 1138c8a9a5SSteve French #include "misc.h" 1238c8a9a5SSteve French #include "smbstatus.h" 1338c8a9a5SSteve French #include "connection.h" 1438c8a9a5SSteve French #include "ksmbd_work.h" 1538c8a9a5SSteve French #include "mgmt/user_session.h" 1638c8a9a5SSteve French #include "mgmt/user_config.h" 1738c8a9a5SSteve French #include "mgmt/tree_connect.h" 1838c8a9a5SSteve French #include "mgmt/share_config.h" 1938c8a9a5SSteve French 2038c8a9a5SSteve French /*for shortname implementation */ 2138c8a9a5SSteve French static const char basechars[43] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_-!@#$%"; 2238c8a9a5SSteve French #define MANGLE_BASE (sizeof(basechars) / sizeof(char) - 1) 2338c8a9a5SSteve French #define MAGIC_CHAR '~' 2438c8a9a5SSteve French #define PERIOD '.' 2538c8a9a5SSteve French #define mangle(V) ((char)(basechars[(V) % MANGLE_BASE])) 2638c8a9a5SSteve French 2738c8a9a5SSteve French struct smb_protocol { 2838c8a9a5SSteve French int index; 2938c8a9a5SSteve French char *name; 3038c8a9a5SSteve French char *prot; 3138c8a9a5SSteve French __u16 prot_id; 3238c8a9a5SSteve French }; 3338c8a9a5SSteve French 3438c8a9a5SSteve French static struct smb_protocol smb1_protos[] = { 3538c8a9a5SSteve French { 3638c8a9a5SSteve French SMB21_PROT, 3738c8a9a5SSteve French "\2SMB 2.1", 3838c8a9a5SSteve French "SMB2_10", 3938c8a9a5SSteve French SMB21_PROT_ID 4038c8a9a5SSteve French }, 4138c8a9a5SSteve French { 4238c8a9a5SSteve French SMB2X_PROT, 4338c8a9a5SSteve French "\2SMB 2.???", 4438c8a9a5SSteve French "SMB2_22", 4538c8a9a5SSteve French SMB2X_PROT_ID 4638c8a9a5SSteve French }, 4738c8a9a5SSteve French }; 4838c8a9a5SSteve French 4938c8a9a5SSteve French static struct smb_protocol smb2_protos[] = { 5038c8a9a5SSteve French { 5138c8a9a5SSteve French SMB21_PROT, 5238c8a9a5SSteve French "\2SMB 2.1", 5338c8a9a5SSteve French "SMB2_10", 5438c8a9a5SSteve French SMB21_PROT_ID 5538c8a9a5SSteve French }, 5638c8a9a5SSteve French { 5738c8a9a5SSteve French SMB30_PROT, 5838c8a9a5SSteve French "\2SMB 3.0", 5938c8a9a5SSteve French "SMB3_00", 6038c8a9a5SSteve French SMB30_PROT_ID 6138c8a9a5SSteve French }, 6238c8a9a5SSteve French { 6338c8a9a5SSteve French SMB302_PROT, 6438c8a9a5SSteve French "\2SMB 3.02", 6538c8a9a5SSteve French "SMB3_02", 6638c8a9a5SSteve French SMB302_PROT_ID 6738c8a9a5SSteve French }, 6838c8a9a5SSteve French { 6938c8a9a5SSteve French SMB311_PROT, 7038c8a9a5SSteve French "\2SMB 3.1.1", 7138c8a9a5SSteve French "SMB3_11", 7238c8a9a5SSteve French SMB311_PROT_ID 7338c8a9a5SSteve French }, 7438c8a9a5SSteve French }; 7538c8a9a5SSteve French 7638c8a9a5SSteve French unsigned int ksmbd_server_side_copy_max_chunk_count(void) 7738c8a9a5SSteve French { 7838c8a9a5SSteve French return 256; 7938c8a9a5SSteve French } 8038c8a9a5SSteve French 8138c8a9a5SSteve French unsigned int ksmbd_server_side_copy_max_chunk_size(void) 8238c8a9a5SSteve French { 8338c8a9a5SSteve French return (2U << 30) - 1; 8438c8a9a5SSteve French } 8538c8a9a5SSteve French 8638c8a9a5SSteve French unsigned int ksmbd_server_side_copy_max_total_size(void) 8738c8a9a5SSteve French { 8838c8a9a5SSteve French return (2U << 30) - 1; 8938c8a9a5SSteve French } 9038c8a9a5SSteve French 9138c8a9a5SSteve French inline int ksmbd_min_protocol(void) 9238c8a9a5SSteve French { 9338c8a9a5SSteve French return SMB21_PROT; 9438c8a9a5SSteve French } 9538c8a9a5SSteve French 9638c8a9a5SSteve French inline int ksmbd_max_protocol(void) 9738c8a9a5SSteve French { 9838c8a9a5SSteve French return SMB311_PROT; 9938c8a9a5SSteve French } 10038c8a9a5SSteve French 10138c8a9a5SSteve French int ksmbd_lookup_protocol_idx(char *str) 10238c8a9a5SSteve French { 10338c8a9a5SSteve French int offt = ARRAY_SIZE(smb1_protos) - 1; 10438c8a9a5SSteve French int len = strlen(str); 10538c8a9a5SSteve French 10638c8a9a5SSteve French while (offt >= 0) { 10738c8a9a5SSteve French if (!strncmp(str, smb1_protos[offt].prot, len)) { 10838c8a9a5SSteve French ksmbd_debug(SMB, "selected %s dialect idx = %d\n", 10938c8a9a5SSteve French smb1_protos[offt].prot, offt); 11038c8a9a5SSteve French return smb1_protos[offt].index; 11138c8a9a5SSteve French } 11238c8a9a5SSteve French offt--; 11338c8a9a5SSteve French } 11438c8a9a5SSteve French 11538c8a9a5SSteve French offt = ARRAY_SIZE(smb2_protos) - 1; 11638c8a9a5SSteve French while (offt >= 0) { 11738c8a9a5SSteve French if (!strncmp(str, smb2_protos[offt].prot, len)) { 11838c8a9a5SSteve French ksmbd_debug(SMB, "selected %s dialect idx = %d\n", 11938c8a9a5SSteve French smb2_protos[offt].prot, offt); 12038c8a9a5SSteve French return smb2_protos[offt].index; 12138c8a9a5SSteve French } 12238c8a9a5SSteve French offt--; 12338c8a9a5SSteve French } 12438c8a9a5SSteve French return -1; 12538c8a9a5SSteve French } 12638c8a9a5SSteve French 12738c8a9a5SSteve French /** 12838c8a9a5SSteve French * ksmbd_verify_smb_message() - check for valid smb2 request header 12938c8a9a5SSteve French * @work: smb work 13038c8a9a5SSteve French * 13138c8a9a5SSteve French * check for valid smb signature and packet direction(request/response) 13238c8a9a5SSteve French * 13338c8a9a5SSteve French * Return: 0 on success, otherwise -EINVAL 13438c8a9a5SSteve French */ 13538c8a9a5SSteve French int ksmbd_verify_smb_message(struct ksmbd_work *work) 13638c8a9a5SSteve French { 13738c8a9a5SSteve French struct smb2_hdr *smb2_hdr = ksmbd_req_buf_next(work); 13838c8a9a5SSteve French struct smb_hdr *hdr; 13938c8a9a5SSteve French 14038c8a9a5SSteve French if (smb2_hdr->ProtocolId == SMB2_PROTO_NUMBER) 14138c8a9a5SSteve French return ksmbd_smb2_check_message(work); 14238c8a9a5SSteve French 14338c8a9a5SSteve French hdr = work->request_buf; 14438c8a9a5SSteve French if (*(__le32 *)hdr->Protocol == SMB1_PROTO_NUMBER && 14538c8a9a5SSteve French hdr->Command == SMB_COM_NEGOTIATE) { 14638c8a9a5SSteve French work->conn->outstanding_credits++; 14738c8a9a5SSteve French return 0; 14838c8a9a5SSteve French } 14938c8a9a5SSteve French 15038c8a9a5SSteve French return -EINVAL; 15138c8a9a5SSteve French } 15238c8a9a5SSteve French 15338c8a9a5SSteve French /** 15438c8a9a5SSteve French * ksmbd_smb_request() - check for valid smb request type 15538c8a9a5SSteve French * @conn: connection instance 15638c8a9a5SSteve French * 15738c8a9a5SSteve French * Return: true on success, otherwise false 15838c8a9a5SSteve French */ 15938c8a9a5SSteve French bool ksmbd_smb_request(struct ksmbd_conn *conn) 16038c8a9a5SSteve French { 161*1c1bcf2dSNamjae Jeon __le32 *proto = (__le32 *)smb2_get_msg(conn->request_buf); 162*1c1bcf2dSNamjae Jeon 163*1c1bcf2dSNamjae Jeon if (*proto == SMB2_COMPRESSION_TRANSFORM_ID) { 164*1c1bcf2dSNamjae Jeon pr_err_ratelimited("smb2 compression not support yet"); 165*1c1bcf2dSNamjae Jeon return false; 166*1c1bcf2dSNamjae Jeon } 167*1c1bcf2dSNamjae Jeon 168*1c1bcf2dSNamjae Jeon if (*proto != SMB1_PROTO_NUMBER && 169*1c1bcf2dSNamjae Jeon *proto != SMB2_PROTO_NUMBER && 170*1c1bcf2dSNamjae Jeon *proto != SMB2_TRANSFORM_PROTO_NUM) 171*1c1bcf2dSNamjae Jeon return false; 172*1c1bcf2dSNamjae Jeon 173*1c1bcf2dSNamjae Jeon return true; 17438c8a9a5SSteve French } 17538c8a9a5SSteve French 17638c8a9a5SSteve French static bool supported_protocol(int idx) 17738c8a9a5SSteve French { 17838c8a9a5SSteve French if (idx == SMB2X_PROT && 17938c8a9a5SSteve French (server_conf.min_protocol >= SMB21_PROT || 18038c8a9a5SSteve French server_conf.max_protocol <= SMB311_PROT)) 18138c8a9a5SSteve French return true; 18238c8a9a5SSteve French 18338c8a9a5SSteve French return (server_conf.min_protocol <= idx && 18438c8a9a5SSteve French idx <= server_conf.max_protocol); 18538c8a9a5SSteve French } 18638c8a9a5SSteve French 18738c8a9a5SSteve French static char *next_dialect(char *dialect, int *next_off, int bcount) 18838c8a9a5SSteve French { 18938c8a9a5SSteve French dialect = dialect + *next_off; 19038c8a9a5SSteve French *next_off = strnlen(dialect, bcount); 19138c8a9a5SSteve French if (dialect[*next_off] != '\0') 19238c8a9a5SSteve French return NULL; 19338c8a9a5SSteve French return dialect; 19438c8a9a5SSteve French } 19538c8a9a5SSteve French 19638c8a9a5SSteve French static int ksmbd_lookup_dialect_by_name(char *cli_dialects, __le16 byte_count) 19738c8a9a5SSteve French { 19838c8a9a5SSteve French int i, seq_num, bcount, next; 19938c8a9a5SSteve French char *dialect; 20038c8a9a5SSteve French 20138c8a9a5SSteve French for (i = ARRAY_SIZE(smb1_protos) - 1; i >= 0; i--) { 20238c8a9a5SSteve French seq_num = 0; 20338c8a9a5SSteve French next = 0; 20438c8a9a5SSteve French dialect = cli_dialects; 20538c8a9a5SSteve French bcount = le16_to_cpu(byte_count); 20638c8a9a5SSteve French do { 20738c8a9a5SSteve French dialect = next_dialect(dialect, &next, bcount); 20838c8a9a5SSteve French if (!dialect) 20938c8a9a5SSteve French break; 21038c8a9a5SSteve French ksmbd_debug(SMB, "client requested dialect %s\n", 21138c8a9a5SSteve French dialect); 21238c8a9a5SSteve French if (!strcmp(dialect, smb1_protos[i].name)) { 21338c8a9a5SSteve French if (supported_protocol(smb1_protos[i].index)) { 21438c8a9a5SSteve French ksmbd_debug(SMB, 21538c8a9a5SSteve French "selected %s dialect\n", 21638c8a9a5SSteve French smb1_protos[i].name); 21738c8a9a5SSteve French if (smb1_protos[i].index == SMB1_PROT) 21838c8a9a5SSteve French return seq_num; 21938c8a9a5SSteve French return smb1_protos[i].prot_id; 22038c8a9a5SSteve French } 22138c8a9a5SSteve French } 22238c8a9a5SSteve French seq_num++; 22338c8a9a5SSteve French bcount -= (++next); 22438c8a9a5SSteve French } while (bcount > 0); 22538c8a9a5SSteve French } 22638c8a9a5SSteve French 22738c8a9a5SSteve French return BAD_PROT_ID; 22838c8a9a5SSteve French } 22938c8a9a5SSteve French 23038c8a9a5SSteve French int ksmbd_lookup_dialect_by_id(__le16 *cli_dialects, __le16 dialects_count) 23138c8a9a5SSteve French { 23238c8a9a5SSteve French int i; 23338c8a9a5SSteve French int count; 23438c8a9a5SSteve French 23538c8a9a5SSteve French for (i = ARRAY_SIZE(smb2_protos) - 1; i >= 0; i--) { 23638c8a9a5SSteve French count = le16_to_cpu(dialects_count); 23738c8a9a5SSteve French while (--count >= 0) { 23838c8a9a5SSteve French ksmbd_debug(SMB, "client requested dialect 0x%x\n", 23938c8a9a5SSteve French le16_to_cpu(cli_dialects[count])); 24038c8a9a5SSteve French if (le16_to_cpu(cli_dialects[count]) != 24138c8a9a5SSteve French smb2_protos[i].prot_id) 24238c8a9a5SSteve French continue; 24338c8a9a5SSteve French 24438c8a9a5SSteve French if (supported_protocol(smb2_protos[i].index)) { 24538c8a9a5SSteve French ksmbd_debug(SMB, "selected %s dialect\n", 24638c8a9a5SSteve French smb2_protos[i].name); 24738c8a9a5SSteve French return smb2_protos[i].prot_id; 24838c8a9a5SSteve French } 24938c8a9a5SSteve French } 25038c8a9a5SSteve French } 25138c8a9a5SSteve French 25238c8a9a5SSteve French return BAD_PROT_ID; 25338c8a9a5SSteve French } 25438c8a9a5SSteve French 25538c8a9a5SSteve French static int ksmbd_negotiate_smb_dialect(void *buf) 25638c8a9a5SSteve French { 25738c8a9a5SSteve French int smb_buf_length = get_rfc1002_len(buf); 25838c8a9a5SSteve French __le32 proto = ((struct smb2_hdr *)smb2_get_msg(buf))->ProtocolId; 25938c8a9a5SSteve French 26038c8a9a5SSteve French if (proto == SMB2_PROTO_NUMBER) { 26138c8a9a5SSteve French struct smb2_negotiate_req *req; 26238c8a9a5SSteve French int smb2_neg_size = 26338c8a9a5SSteve French offsetof(struct smb2_negotiate_req, Dialects); 26438c8a9a5SSteve French 26538c8a9a5SSteve French req = (struct smb2_negotiate_req *)smb2_get_msg(buf); 26638c8a9a5SSteve French if (smb2_neg_size > smb_buf_length) 26738c8a9a5SSteve French goto err_out; 26838c8a9a5SSteve French 26938c8a9a5SSteve French if (smb2_neg_size + le16_to_cpu(req->DialectCount) * sizeof(__le16) > 27038c8a9a5SSteve French smb_buf_length) 27138c8a9a5SSteve French goto err_out; 27238c8a9a5SSteve French 27338c8a9a5SSteve French return ksmbd_lookup_dialect_by_id(req->Dialects, 27438c8a9a5SSteve French req->DialectCount); 27538c8a9a5SSteve French } 27638c8a9a5SSteve French 27738c8a9a5SSteve French proto = *(__le32 *)((struct smb_hdr *)buf)->Protocol; 27838c8a9a5SSteve French if (proto == SMB1_PROTO_NUMBER) { 27938c8a9a5SSteve French struct smb_negotiate_req *req; 28038c8a9a5SSteve French 28138c8a9a5SSteve French req = (struct smb_negotiate_req *)buf; 28238c8a9a5SSteve French if (le16_to_cpu(req->ByteCount) < 2) 28338c8a9a5SSteve French goto err_out; 28438c8a9a5SSteve French 28538c8a9a5SSteve French if (offsetof(struct smb_negotiate_req, DialectsArray) - 4 + 28638c8a9a5SSteve French le16_to_cpu(req->ByteCount) > smb_buf_length) { 28738c8a9a5SSteve French goto err_out; 28838c8a9a5SSteve French } 28938c8a9a5SSteve French 29038c8a9a5SSteve French return ksmbd_lookup_dialect_by_name(req->DialectsArray, 29138c8a9a5SSteve French req->ByteCount); 29238c8a9a5SSteve French } 29338c8a9a5SSteve French 29438c8a9a5SSteve French err_out: 29538c8a9a5SSteve French return BAD_PROT_ID; 29638c8a9a5SSteve French } 29738c8a9a5SSteve French 29838c8a9a5SSteve French #define SMB_COM_NEGOTIATE_EX 0x0 29938c8a9a5SSteve French 30038c8a9a5SSteve French /** 30138c8a9a5SSteve French * get_smb1_cmd_val() - get smb command value from smb header 30238c8a9a5SSteve French * @work: smb work containing smb header 30338c8a9a5SSteve French * 30438c8a9a5SSteve French * Return: smb command value 30538c8a9a5SSteve French */ 30638c8a9a5SSteve French static u16 get_smb1_cmd_val(struct ksmbd_work *work) 30738c8a9a5SSteve French { 30838c8a9a5SSteve French return SMB_COM_NEGOTIATE_EX; 30938c8a9a5SSteve French } 31038c8a9a5SSteve French 31138c8a9a5SSteve French /** 31238c8a9a5SSteve French * init_smb1_rsp_hdr() - initialize smb negotiate response header 31338c8a9a5SSteve French * @work: smb work containing smb request 31438c8a9a5SSteve French * 31538c8a9a5SSteve French * Return: 0 on success, otherwise -EINVAL 31638c8a9a5SSteve French */ 31738c8a9a5SSteve French static int init_smb1_rsp_hdr(struct ksmbd_work *work) 31838c8a9a5SSteve French { 31938c8a9a5SSteve French struct smb_hdr *rsp_hdr = (struct smb_hdr *)work->response_buf; 32038c8a9a5SSteve French struct smb_hdr *rcv_hdr = (struct smb_hdr *)work->request_buf; 32138c8a9a5SSteve French 32238c8a9a5SSteve French /* 32338c8a9a5SSteve French * Remove 4 byte direct TCP header. 32438c8a9a5SSteve French */ 32538c8a9a5SSteve French *(__be32 *)work->response_buf = 32638c8a9a5SSteve French cpu_to_be32(sizeof(struct smb_hdr) - 4); 32738c8a9a5SSteve French 32838c8a9a5SSteve French rsp_hdr->Command = SMB_COM_NEGOTIATE; 32938c8a9a5SSteve French *(__le32 *)rsp_hdr->Protocol = SMB1_PROTO_NUMBER; 33038c8a9a5SSteve French rsp_hdr->Flags = SMBFLG_RESPONSE; 33138c8a9a5SSteve French rsp_hdr->Flags2 = SMBFLG2_UNICODE | SMBFLG2_ERR_STATUS | 33238c8a9a5SSteve French SMBFLG2_EXT_SEC | SMBFLG2_IS_LONG_NAME; 33338c8a9a5SSteve French rsp_hdr->Pid = rcv_hdr->Pid; 33438c8a9a5SSteve French rsp_hdr->Mid = rcv_hdr->Mid; 33538c8a9a5SSteve French return 0; 33638c8a9a5SSteve French } 33738c8a9a5SSteve French 33838c8a9a5SSteve French /** 33938c8a9a5SSteve French * smb1_check_user_session() - check for valid session for a user 34038c8a9a5SSteve French * @work: smb work containing smb request buffer 34138c8a9a5SSteve French * 34238c8a9a5SSteve French * Return: 0 on success, otherwise error 34338c8a9a5SSteve French */ 34438c8a9a5SSteve French static int smb1_check_user_session(struct ksmbd_work *work) 34538c8a9a5SSteve French { 34638c8a9a5SSteve French unsigned int cmd = work->conn->ops->get_cmd_val(work); 34738c8a9a5SSteve French 34838c8a9a5SSteve French if (cmd == SMB_COM_NEGOTIATE_EX) 34938c8a9a5SSteve French return 0; 35038c8a9a5SSteve French 35138c8a9a5SSteve French return -EINVAL; 35238c8a9a5SSteve French } 35338c8a9a5SSteve French 35438c8a9a5SSteve French /** 35538c8a9a5SSteve French * smb1_allocate_rsp_buf() - allocate response buffer for a command 35638c8a9a5SSteve French * @work: smb work containing smb request 35738c8a9a5SSteve French * 35838c8a9a5SSteve French * Return: 0 on success, otherwise -ENOMEM 35938c8a9a5SSteve French */ 36038c8a9a5SSteve French static int smb1_allocate_rsp_buf(struct ksmbd_work *work) 36138c8a9a5SSteve French { 36238c8a9a5SSteve French work->response_buf = kmalloc(MAX_CIFS_SMALL_BUFFER_SIZE, 36338c8a9a5SSteve French GFP_KERNEL | __GFP_ZERO); 36438c8a9a5SSteve French work->response_sz = MAX_CIFS_SMALL_BUFFER_SIZE; 36538c8a9a5SSteve French 36638c8a9a5SSteve French if (!work->response_buf) { 36738c8a9a5SSteve French pr_err("Failed to allocate %u bytes buffer\n", 36838c8a9a5SSteve French MAX_CIFS_SMALL_BUFFER_SIZE); 36938c8a9a5SSteve French return -ENOMEM; 37038c8a9a5SSteve French } 37138c8a9a5SSteve French 37238c8a9a5SSteve French return 0; 37338c8a9a5SSteve French } 37438c8a9a5SSteve French 37538c8a9a5SSteve French static struct smb_version_ops smb1_server_ops = { 37638c8a9a5SSteve French .get_cmd_val = get_smb1_cmd_val, 37738c8a9a5SSteve French .init_rsp_hdr = init_smb1_rsp_hdr, 37838c8a9a5SSteve French .allocate_rsp_buf = smb1_allocate_rsp_buf, 37938c8a9a5SSteve French .check_user_session = smb1_check_user_session, 38038c8a9a5SSteve French }; 38138c8a9a5SSteve French 38238c8a9a5SSteve French static int smb1_negotiate(struct ksmbd_work *work) 38338c8a9a5SSteve French { 38438c8a9a5SSteve French return ksmbd_smb_negotiate_common(work, SMB_COM_NEGOTIATE); 38538c8a9a5SSteve French } 38638c8a9a5SSteve French 38738c8a9a5SSteve French static struct smb_version_cmds smb1_server_cmds[1] = { 38838c8a9a5SSteve French [SMB_COM_NEGOTIATE_EX] = { .proc = smb1_negotiate, }, 38938c8a9a5SSteve French }; 39038c8a9a5SSteve French 39138c8a9a5SSteve French static void init_smb1_server(struct ksmbd_conn *conn) 39238c8a9a5SSteve French { 39338c8a9a5SSteve French conn->ops = &smb1_server_ops; 39438c8a9a5SSteve French conn->cmds = smb1_server_cmds; 39538c8a9a5SSteve French conn->max_cmds = ARRAY_SIZE(smb1_server_cmds); 39638c8a9a5SSteve French } 39738c8a9a5SSteve French 39838c8a9a5SSteve French void ksmbd_init_smb_server(struct ksmbd_work *work) 39938c8a9a5SSteve French { 40038c8a9a5SSteve French struct ksmbd_conn *conn = work->conn; 40138c8a9a5SSteve French __le32 proto; 40238c8a9a5SSteve French 40338c8a9a5SSteve French if (conn->need_neg == false) 40438c8a9a5SSteve French return; 40538c8a9a5SSteve French 40638c8a9a5SSteve French proto = *(__le32 *)((struct smb_hdr *)work->request_buf)->Protocol; 40738c8a9a5SSteve French if (proto == SMB1_PROTO_NUMBER) 40838c8a9a5SSteve French init_smb1_server(conn); 40938c8a9a5SSteve French else 41038c8a9a5SSteve French init_smb3_11_server(conn); 41138c8a9a5SSteve French } 41238c8a9a5SSteve French 41338c8a9a5SSteve French int ksmbd_populate_dot_dotdot_entries(struct ksmbd_work *work, int info_level, 41438c8a9a5SSteve French struct ksmbd_file *dir, 41538c8a9a5SSteve French struct ksmbd_dir_info *d_info, 41638c8a9a5SSteve French char *search_pattern, 41738c8a9a5SSteve French int (*fn)(struct ksmbd_conn *, int, 41838c8a9a5SSteve French struct ksmbd_dir_info *, 41938c8a9a5SSteve French struct ksmbd_kstat *)) 42038c8a9a5SSteve French { 42138c8a9a5SSteve French int i, rc = 0; 42238c8a9a5SSteve French struct ksmbd_conn *conn = work->conn; 42338c8a9a5SSteve French struct mnt_idmap *idmap = file_mnt_idmap(dir->filp); 42438c8a9a5SSteve French 42538c8a9a5SSteve French for (i = 0; i < 2; i++) { 42638c8a9a5SSteve French struct kstat kstat; 42738c8a9a5SSteve French struct ksmbd_kstat ksmbd_kstat; 42838c8a9a5SSteve French struct dentry *dentry; 42938c8a9a5SSteve French 43038c8a9a5SSteve French if (!dir->dot_dotdot[i]) { /* fill dot entry info */ 43138c8a9a5SSteve French if (i == 0) { 43238c8a9a5SSteve French d_info->name = "."; 43338c8a9a5SSteve French d_info->name_len = 1; 43438c8a9a5SSteve French dentry = dir->filp->f_path.dentry; 43538c8a9a5SSteve French } else { 43638c8a9a5SSteve French d_info->name = ".."; 43738c8a9a5SSteve French d_info->name_len = 2; 43838c8a9a5SSteve French dentry = dir->filp->f_path.dentry->d_parent; 43938c8a9a5SSteve French } 44038c8a9a5SSteve French 44138c8a9a5SSteve French if (!match_pattern(d_info->name, d_info->name_len, 44238c8a9a5SSteve French search_pattern)) { 44338c8a9a5SSteve French dir->dot_dotdot[i] = 1; 44438c8a9a5SSteve French continue; 44538c8a9a5SSteve French } 44638c8a9a5SSteve French 44738c8a9a5SSteve French ksmbd_kstat.kstat = &kstat; 44838c8a9a5SSteve French ksmbd_vfs_fill_dentry_attrs(work, 44938c8a9a5SSteve French idmap, 45038c8a9a5SSteve French dentry, 45138c8a9a5SSteve French &ksmbd_kstat); 45238c8a9a5SSteve French rc = fn(conn, info_level, d_info, &ksmbd_kstat); 45338c8a9a5SSteve French if (rc) 45438c8a9a5SSteve French break; 45538c8a9a5SSteve French if (d_info->out_buf_len <= 0) 45638c8a9a5SSteve French break; 45738c8a9a5SSteve French 45838c8a9a5SSteve French dir->dot_dotdot[i] = 1; 45938c8a9a5SSteve French if (d_info->flags & SMB2_RETURN_SINGLE_ENTRY) { 46038c8a9a5SSteve French d_info->out_buf_len = 0; 46138c8a9a5SSteve French break; 46238c8a9a5SSteve French } 46338c8a9a5SSteve French } 46438c8a9a5SSteve French } 46538c8a9a5SSteve French 46638c8a9a5SSteve French return rc; 46738c8a9a5SSteve French } 46838c8a9a5SSteve French 46938c8a9a5SSteve French /** 47038c8a9a5SSteve French * ksmbd_extract_shortname() - get shortname from long filename 47138c8a9a5SSteve French * @conn: connection instance 47238c8a9a5SSteve French * @longname: source long filename 47338c8a9a5SSteve French * @shortname: destination short filename 47438c8a9a5SSteve French * 47538c8a9a5SSteve French * Return: shortname length or 0 when source long name is '.' or '..' 47638c8a9a5SSteve French * TODO: Though this function comforms the restriction of 8.3 Filename spec, 47738c8a9a5SSteve French * but the result is different with Windows 7's one. need to check. 47838c8a9a5SSteve French */ 47938c8a9a5SSteve French int ksmbd_extract_shortname(struct ksmbd_conn *conn, const char *longname, 48038c8a9a5SSteve French char *shortname) 48138c8a9a5SSteve French { 48238c8a9a5SSteve French const char *p; 48338c8a9a5SSteve French char base[9], extension[4]; 48438c8a9a5SSteve French char out[13] = {0}; 48538c8a9a5SSteve French int baselen = 0; 48638c8a9a5SSteve French int extlen = 0, len = 0; 48738c8a9a5SSteve French unsigned int csum = 0; 48838c8a9a5SSteve French const unsigned char *ptr; 48938c8a9a5SSteve French bool dot_present = true; 49038c8a9a5SSteve French 49138c8a9a5SSteve French p = longname; 49238c8a9a5SSteve French if ((*p == '.') || (!(strcmp(p, "..")))) { 49338c8a9a5SSteve French /*no mangling required */ 49438c8a9a5SSteve French return 0; 49538c8a9a5SSteve French } 49638c8a9a5SSteve French 49738c8a9a5SSteve French p = strrchr(longname, '.'); 49838c8a9a5SSteve French if (p == longname) { /*name starts with a dot*/ 49938c8a9a5SSteve French strscpy(extension, "___", strlen("___")); 50038c8a9a5SSteve French } else { 50138c8a9a5SSteve French if (p) { 50238c8a9a5SSteve French p++; 50338c8a9a5SSteve French while (*p && extlen < 3) { 50438c8a9a5SSteve French if (*p != '.') 50538c8a9a5SSteve French extension[extlen++] = toupper(*p); 50638c8a9a5SSteve French p++; 50738c8a9a5SSteve French } 50838c8a9a5SSteve French extension[extlen] = '\0'; 50938c8a9a5SSteve French } else { 51038c8a9a5SSteve French dot_present = false; 51138c8a9a5SSteve French } 51238c8a9a5SSteve French } 51338c8a9a5SSteve French 51438c8a9a5SSteve French p = longname; 51538c8a9a5SSteve French if (*p == '.') { 51638c8a9a5SSteve French p++; 51738c8a9a5SSteve French longname++; 51838c8a9a5SSteve French } 51938c8a9a5SSteve French while (*p && (baselen < 5)) { 52038c8a9a5SSteve French if (*p != '.') 52138c8a9a5SSteve French base[baselen++] = toupper(*p); 52238c8a9a5SSteve French p++; 52338c8a9a5SSteve French } 52438c8a9a5SSteve French 52538c8a9a5SSteve French base[baselen] = MAGIC_CHAR; 52638c8a9a5SSteve French memcpy(out, base, baselen + 1); 52738c8a9a5SSteve French 52838c8a9a5SSteve French ptr = longname; 52938c8a9a5SSteve French len = strlen(longname); 53038c8a9a5SSteve French for (; len > 0; len--, ptr++) 53138c8a9a5SSteve French csum += *ptr; 53238c8a9a5SSteve French 53338c8a9a5SSteve French csum = csum % (MANGLE_BASE * MANGLE_BASE); 53438c8a9a5SSteve French out[baselen + 1] = mangle(csum / MANGLE_BASE); 53538c8a9a5SSteve French out[baselen + 2] = mangle(csum); 53638c8a9a5SSteve French out[baselen + 3] = PERIOD; 53738c8a9a5SSteve French 53838c8a9a5SSteve French if (dot_present) 53938c8a9a5SSteve French memcpy(&out[baselen + 4], extension, 4); 54038c8a9a5SSteve French else 54138c8a9a5SSteve French out[baselen + 4] = '\0'; 54238c8a9a5SSteve French smbConvertToUTF16((__le16 *)shortname, out, PATH_MAX, 54338c8a9a5SSteve French conn->local_nls, 0); 54438c8a9a5SSteve French len = strlen(out) * 2; 54538c8a9a5SSteve French return len; 54638c8a9a5SSteve French } 54738c8a9a5SSteve French 54838c8a9a5SSteve French static int __smb2_negotiate(struct ksmbd_conn *conn) 54938c8a9a5SSteve French { 55038c8a9a5SSteve French return (conn->dialect >= SMB20_PROT_ID && 55138c8a9a5SSteve French conn->dialect <= SMB311_PROT_ID); 55238c8a9a5SSteve French } 55338c8a9a5SSteve French 55438c8a9a5SSteve French static int smb_handle_negotiate(struct ksmbd_work *work) 55538c8a9a5SSteve French { 55638c8a9a5SSteve French struct smb_negotiate_rsp *neg_rsp = work->response_buf; 55738c8a9a5SSteve French 55838c8a9a5SSteve French ksmbd_debug(SMB, "Unsupported SMB1 protocol\n"); 55938c8a9a5SSteve French 56038c8a9a5SSteve French /* Add 2 byte bcc and 2 byte DialectIndex. */ 56138c8a9a5SSteve French inc_rfc1001_len(work->response_buf, 4); 56238c8a9a5SSteve French neg_rsp->hdr.Status.CifsError = STATUS_SUCCESS; 56338c8a9a5SSteve French 56438c8a9a5SSteve French neg_rsp->hdr.WordCount = 1; 56538c8a9a5SSteve French neg_rsp->DialectIndex = cpu_to_le16(work->conn->dialect); 56638c8a9a5SSteve French neg_rsp->ByteCount = 0; 56738c8a9a5SSteve French return 0; 56838c8a9a5SSteve French } 56938c8a9a5SSteve French 57038c8a9a5SSteve French int ksmbd_smb_negotiate_common(struct ksmbd_work *work, unsigned int command) 57138c8a9a5SSteve French { 57238c8a9a5SSteve French struct ksmbd_conn *conn = work->conn; 57338c8a9a5SSteve French int ret; 57438c8a9a5SSteve French 57538c8a9a5SSteve French conn->dialect = 57638c8a9a5SSteve French ksmbd_negotiate_smb_dialect(work->request_buf); 57738c8a9a5SSteve French ksmbd_debug(SMB, "conn->dialect 0x%x\n", conn->dialect); 57838c8a9a5SSteve French 57938c8a9a5SSteve French if (command == SMB2_NEGOTIATE_HE) { 58038c8a9a5SSteve French ret = smb2_handle_negotiate(work); 58138c8a9a5SSteve French return ret; 58238c8a9a5SSteve French } 58338c8a9a5SSteve French 58438c8a9a5SSteve French if (command == SMB_COM_NEGOTIATE) { 58538c8a9a5SSteve French if (__smb2_negotiate(conn)) { 58638c8a9a5SSteve French init_smb3_11_server(conn); 58738c8a9a5SSteve French init_smb2_neg_rsp(work); 58838c8a9a5SSteve French ksmbd_debug(SMB, "Upgrade to SMB2 negotiation\n"); 58938c8a9a5SSteve French return 0; 59038c8a9a5SSteve French } 59138c8a9a5SSteve French return smb_handle_negotiate(work); 59238c8a9a5SSteve French } 59338c8a9a5SSteve French 59438c8a9a5SSteve French pr_err("Unknown SMB negotiation command: %u\n", command); 59538c8a9a5SSteve French return -EINVAL; 59638c8a9a5SSteve French } 59738c8a9a5SSteve French 59838c8a9a5SSteve French enum SHARED_MODE_ERRORS { 59938c8a9a5SSteve French SHARE_DELETE_ERROR, 60038c8a9a5SSteve French SHARE_READ_ERROR, 60138c8a9a5SSteve French SHARE_WRITE_ERROR, 60238c8a9a5SSteve French FILE_READ_ERROR, 60338c8a9a5SSteve French FILE_WRITE_ERROR, 60438c8a9a5SSteve French FILE_DELETE_ERROR, 60538c8a9a5SSteve French }; 60638c8a9a5SSteve French 60738c8a9a5SSteve French static const char * const shared_mode_errors[] = { 60838c8a9a5SSteve French "Current access mode does not permit SHARE_DELETE", 60938c8a9a5SSteve French "Current access mode does not permit SHARE_READ", 61038c8a9a5SSteve French "Current access mode does not permit SHARE_WRITE", 61138c8a9a5SSteve French "Desired access mode does not permit FILE_READ", 61238c8a9a5SSteve French "Desired access mode does not permit FILE_WRITE", 61338c8a9a5SSteve French "Desired access mode does not permit FILE_DELETE", 61438c8a9a5SSteve French }; 61538c8a9a5SSteve French 61638c8a9a5SSteve French static void smb_shared_mode_error(int error, struct ksmbd_file *prev_fp, 61738c8a9a5SSteve French struct ksmbd_file *curr_fp) 61838c8a9a5SSteve French { 61938c8a9a5SSteve French ksmbd_debug(SMB, "%s\n", shared_mode_errors[error]); 62038c8a9a5SSteve French ksmbd_debug(SMB, "Current mode: 0x%x Desired mode: 0x%x\n", 62138c8a9a5SSteve French prev_fp->saccess, curr_fp->daccess); 62238c8a9a5SSteve French } 62338c8a9a5SSteve French 62438c8a9a5SSteve French int ksmbd_smb_check_shared_mode(struct file *filp, struct ksmbd_file *curr_fp) 62538c8a9a5SSteve French { 62638c8a9a5SSteve French int rc = 0; 62738c8a9a5SSteve French struct ksmbd_file *prev_fp; 62838c8a9a5SSteve French 62938c8a9a5SSteve French /* 63038c8a9a5SSteve French * Lookup fp in master fp list, and check desired access and 63138c8a9a5SSteve French * shared mode between previous open and current open. 63238c8a9a5SSteve French */ 63338c8a9a5SSteve French read_lock(&curr_fp->f_ci->m_lock); 63438c8a9a5SSteve French list_for_each_entry(prev_fp, &curr_fp->f_ci->m_fp_list, node) { 63538c8a9a5SSteve French if (file_inode(filp) != file_inode(prev_fp->filp)) 63638c8a9a5SSteve French continue; 63738c8a9a5SSteve French 63838c8a9a5SSteve French if (filp == prev_fp->filp) 63938c8a9a5SSteve French continue; 64038c8a9a5SSteve French 64138c8a9a5SSteve French if (ksmbd_stream_fd(prev_fp) && ksmbd_stream_fd(curr_fp)) 64238c8a9a5SSteve French if (strcmp(prev_fp->stream.name, curr_fp->stream.name)) 64338c8a9a5SSteve French continue; 64438c8a9a5SSteve French 64538c8a9a5SSteve French if (prev_fp->attrib_only != curr_fp->attrib_only) 64638c8a9a5SSteve French continue; 64738c8a9a5SSteve French 64838c8a9a5SSteve French if (!(prev_fp->saccess & FILE_SHARE_DELETE_LE) && 64938c8a9a5SSteve French curr_fp->daccess & FILE_DELETE_LE) { 65038c8a9a5SSteve French smb_shared_mode_error(SHARE_DELETE_ERROR, 65138c8a9a5SSteve French prev_fp, 65238c8a9a5SSteve French curr_fp); 65338c8a9a5SSteve French rc = -EPERM; 65438c8a9a5SSteve French break; 65538c8a9a5SSteve French } 65638c8a9a5SSteve French 65738c8a9a5SSteve French /* 65838c8a9a5SSteve French * Only check FILE_SHARE_DELETE if stream opened and 65938c8a9a5SSteve French * normal file opened. 66038c8a9a5SSteve French */ 66138c8a9a5SSteve French if (ksmbd_stream_fd(prev_fp) && !ksmbd_stream_fd(curr_fp)) 66238c8a9a5SSteve French continue; 66338c8a9a5SSteve French 66438c8a9a5SSteve French if (!(prev_fp->saccess & FILE_SHARE_READ_LE) && 66538c8a9a5SSteve French curr_fp->daccess & (FILE_EXECUTE_LE | FILE_READ_DATA_LE)) { 66638c8a9a5SSteve French smb_shared_mode_error(SHARE_READ_ERROR, 66738c8a9a5SSteve French prev_fp, 66838c8a9a5SSteve French curr_fp); 66938c8a9a5SSteve French rc = -EPERM; 67038c8a9a5SSteve French break; 67138c8a9a5SSteve French } 67238c8a9a5SSteve French 67338c8a9a5SSteve French if (!(prev_fp->saccess & FILE_SHARE_WRITE_LE) && 67438c8a9a5SSteve French curr_fp->daccess & (FILE_WRITE_DATA_LE | FILE_APPEND_DATA_LE)) { 67538c8a9a5SSteve French smb_shared_mode_error(SHARE_WRITE_ERROR, 67638c8a9a5SSteve French prev_fp, 67738c8a9a5SSteve French curr_fp); 67838c8a9a5SSteve French rc = -EPERM; 67938c8a9a5SSteve French break; 68038c8a9a5SSteve French } 68138c8a9a5SSteve French 68238c8a9a5SSteve French if (prev_fp->daccess & (FILE_EXECUTE_LE | FILE_READ_DATA_LE) && 68338c8a9a5SSteve French !(curr_fp->saccess & FILE_SHARE_READ_LE)) { 68438c8a9a5SSteve French smb_shared_mode_error(FILE_READ_ERROR, 68538c8a9a5SSteve French prev_fp, 68638c8a9a5SSteve French curr_fp); 68738c8a9a5SSteve French rc = -EPERM; 68838c8a9a5SSteve French break; 68938c8a9a5SSteve French } 69038c8a9a5SSteve French 69138c8a9a5SSteve French if (prev_fp->daccess & (FILE_WRITE_DATA_LE | FILE_APPEND_DATA_LE) && 69238c8a9a5SSteve French !(curr_fp->saccess & FILE_SHARE_WRITE_LE)) { 69338c8a9a5SSteve French smb_shared_mode_error(FILE_WRITE_ERROR, 69438c8a9a5SSteve French prev_fp, 69538c8a9a5SSteve French curr_fp); 69638c8a9a5SSteve French rc = -EPERM; 69738c8a9a5SSteve French break; 69838c8a9a5SSteve French } 69938c8a9a5SSteve French 70038c8a9a5SSteve French if (prev_fp->daccess & FILE_DELETE_LE && 70138c8a9a5SSteve French !(curr_fp->saccess & FILE_SHARE_DELETE_LE)) { 70238c8a9a5SSteve French smb_shared_mode_error(FILE_DELETE_ERROR, 70338c8a9a5SSteve French prev_fp, 70438c8a9a5SSteve French curr_fp); 70538c8a9a5SSteve French rc = -EPERM; 70638c8a9a5SSteve French break; 70738c8a9a5SSteve French } 70838c8a9a5SSteve French } 70938c8a9a5SSteve French read_unlock(&curr_fp->f_ci->m_lock); 71038c8a9a5SSteve French 71138c8a9a5SSteve French return rc; 71238c8a9a5SSteve French } 71338c8a9a5SSteve French 71438c8a9a5SSteve French bool is_asterisk(char *p) 71538c8a9a5SSteve French { 71638c8a9a5SSteve French return p && p[0] == '*'; 71738c8a9a5SSteve French } 71838c8a9a5SSteve French 71938c8a9a5SSteve French int ksmbd_override_fsids(struct ksmbd_work *work) 72038c8a9a5SSteve French { 72138c8a9a5SSteve French struct ksmbd_session *sess = work->sess; 72238c8a9a5SSteve French struct ksmbd_share_config *share = work->tcon->share_conf; 72338c8a9a5SSteve French struct cred *cred; 72438c8a9a5SSteve French struct group_info *gi; 72538c8a9a5SSteve French unsigned int uid; 72638c8a9a5SSteve French unsigned int gid; 72738c8a9a5SSteve French 72838c8a9a5SSteve French uid = user_uid(sess->user); 72938c8a9a5SSteve French gid = user_gid(sess->user); 73038c8a9a5SSteve French if (share->force_uid != KSMBD_SHARE_INVALID_UID) 73138c8a9a5SSteve French uid = share->force_uid; 73238c8a9a5SSteve French if (share->force_gid != KSMBD_SHARE_INVALID_GID) 73338c8a9a5SSteve French gid = share->force_gid; 73438c8a9a5SSteve French 73538c8a9a5SSteve French cred = prepare_kernel_cred(&init_task); 73638c8a9a5SSteve French if (!cred) 73738c8a9a5SSteve French return -ENOMEM; 73838c8a9a5SSteve French 73938c8a9a5SSteve French cred->fsuid = make_kuid(&init_user_ns, uid); 74038c8a9a5SSteve French cred->fsgid = make_kgid(&init_user_ns, gid); 74138c8a9a5SSteve French 74238c8a9a5SSteve French gi = groups_alloc(0); 74338c8a9a5SSteve French if (!gi) { 74438c8a9a5SSteve French abort_creds(cred); 74538c8a9a5SSteve French return -ENOMEM; 74638c8a9a5SSteve French } 74738c8a9a5SSteve French set_groups(cred, gi); 74838c8a9a5SSteve French put_group_info(gi); 74938c8a9a5SSteve French 75038c8a9a5SSteve French if (!uid_eq(cred->fsuid, GLOBAL_ROOT_UID)) 75138c8a9a5SSteve French cred->cap_effective = cap_drop_fs_set(cred->cap_effective); 75238c8a9a5SSteve French 75338c8a9a5SSteve French WARN_ON(work->saved_cred); 75438c8a9a5SSteve French work->saved_cred = override_creds(cred); 75538c8a9a5SSteve French if (!work->saved_cred) { 75638c8a9a5SSteve French abort_creds(cred); 75738c8a9a5SSteve French return -EINVAL; 75838c8a9a5SSteve French } 75938c8a9a5SSteve French return 0; 76038c8a9a5SSteve French } 76138c8a9a5SSteve French 76238c8a9a5SSteve French void ksmbd_revert_fsids(struct ksmbd_work *work) 76338c8a9a5SSteve French { 76438c8a9a5SSteve French const struct cred *cred; 76538c8a9a5SSteve French 76638c8a9a5SSteve French WARN_ON(!work->saved_cred); 76738c8a9a5SSteve French 76838c8a9a5SSteve French cred = current_cred(); 76938c8a9a5SSteve French revert_creds(work->saved_cred); 77038c8a9a5SSteve French put_cred(cred); 77138c8a9a5SSteve French work->saved_cred = NULL; 77238c8a9a5SSteve French } 77338c8a9a5SSteve French 77438c8a9a5SSteve French __le32 smb_map_generic_desired_access(__le32 daccess) 77538c8a9a5SSteve French { 77638c8a9a5SSteve French if (daccess & FILE_GENERIC_READ_LE) { 77738c8a9a5SSteve French daccess |= cpu_to_le32(GENERIC_READ_FLAGS); 77838c8a9a5SSteve French daccess &= ~FILE_GENERIC_READ_LE; 77938c8a9a5SSteve French } 78038c8a9a5SSteve French 78138c8a9a5SSteve French if (daccess & FILE_GENERIC_WRITE_LE) { 78238c8a9a5SSteve French daccess |= cpu_to_le32(GENERIC_WRITE_FLAGS); 78338c8a9a5SSteve French daccess &= ~FILE_GENERIC_WRITE_LE; 78438c8a9a5SSteve French } 78538c8a9a5SSteve French 78638c8a9a5SSteve French if (daccess & FILE_GENERIC_EXECUTE_LE) { 78738c8a9a5SSteve French daccess |= cpu_to_le32(GENERIC_EXECUTE_FLAGS); 78838c8a9a5SSteve French daccess &= ~FILE_GENERIC_EXECUTE_LE; 78938c8a9a5SSteve French } 79038c8a9a5SSteve French 79138c8a9a5SSteve French if (daccess & FILE_GENERIC_ALL_LE) { 79238c8a9a5SSteve French daccess |= cpu_to_le32(GENERIC_ALL_FLAGS); 79338c8a9a5SSteve French daccess &= ~FILE_GENERIC_ALL_LE; 79438c8a9a5SSteve French } 79538c8a9a5SSteve French 79638c8a9a5SSteve French return daccess; 79738c8a9a5SSteve French } 798