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