| 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 <linux/user_namespace.h> |
| 8 | |
| 9 | #include "smb_common.h" |
| 10 | #include "server.h" |
| 11 | #include "misc.h" |
| 12 | #include "../common/smb2status.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" |
| 19 | |
| 20 | /*for shortname implementation */ |
| 21 | static const char *basechars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_-!@#$%" ; |
| 22 | #define MANGLE_BASE (strlen(basechars) - 1) |
| 23 | #define MAGIC_CHAR '~' |
| 24 | #define PERIOD '.' |
| 25 | #define mangle(V) ((char)(basechars[(V) % MANGLE_BASE])) |
| 26 | |
| 27 | struct smb_protocol { |
| 28 | int index; |
| 29 | char *name; |
| 30 | char *prot; |
| 31 | __u16 prot_id; |
| 32 | }; |
| 33 | |
| 34 | static struct smb_protocol smb1_protos[] = { |
| 35 | { |
| 36 | SMB21_PROT, |
| 37 | "\2SMB 2.1" , |
| 38 | "SMB2_10" , |
| 39 | SMB21_PROT_ID |
| 40 | }, |
| 41 | { |
| 42 | SMB2X_PROT, |
| 43 | "\2SMB 2.???" , |
| 44 | "SMB2_22" , |
| 45 | SMB2X_PROT_ID |
| 46 | }, |
| 47 | }; |
| 48 | |
| 49 | static struct smb_protocol smb2_protos[] = { |
| 50 | { |
| 51 | SMB21_PROT, |
| 52 | "\2SMB 2.1" , |
| 53 | "SMB2_10" , |
| 54 | SMB21_PROT_ID |
| 55 | }, |
| 56 | { |
| 57 | SMB30_PROT, |
| 58 | "\2SMB 3.0" , |
| 59 | "SMB3_00" , |
| 60 | SMB30_PROT_ID |
| 61 | }, |
| 62 | { |
| 63 | SMB302_PROT, |
| 64 | "\2SMB 3.02" , |
| 65 | "SMB3_02" , |
| 66 | SMB302_PROT_ID |
| 67 | }, |
| 68 | { |
| 69 | SMB311_PROT, |
| 70 | "\2SMB 3.1.1" , |
| 71 | "SMB3_11" , |
| 72 | SMB311_PROT_ID |
| 73 | }, |
| 74 | }; |
| 75 | |
| 76 | unsigned int ksmbd_server_side_copy_max_chunk_count(void) |
| 77 | { |
| 78 | return 256; |
| 79 | } |
| 80 | |
| 81 | unsigned int ksmbd_server_side_copy_max_chunk_size(void) |
| 82 | { |
| 83 | return (2U << 30) - 1; |
| 84 | } |
| 85 | |
| 86 | unsigned int ksmbd_server_side_copy_max_total_size(void) |
| 87 | { |
| 88 | return (2U << 30) - 1; |
| 89 | } |
| 90 | |
| 91 | inline int ksmbd_min_protocol(void) |
| 92 | { |
| 93 | return SMB21_PROT; |
| 94 | } |
| 95 | |
| 96 | inline int ksmbd_max_protocol(void) |
| 97 | { |
| 98 | return SMB311_PROT; |
| 99 | } |
| 100 | |
| 101 | int ksmbd_lookup_protocol_idx(char *str) |
| 102 | { |
| 103 | int offt = ARRAY_SIZE(smb1_protos) - 1; |
| 104 | int len = strlen(str); |
| 105 | |
| 106 | while (offt >= 0) { |
| 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; |
| 111 | } |
| 112 | offt--; |
| 113 | } |
| 114 | |
| 115 | offt = ARRAY_SIZE(smb2_protos) - 1; |
| 116 | while (offt >= 0) { |
| 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; |
| 121 | } |
| 122 | offt--; |
| 123 | } |
| 124 | return -1; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * ksmbd_verify_smb_message() - check for valid smb2 request header |
| 129 | * @work: smb work |
| 130 | * |
| 131 | * check for valid smb signature and packet direction(request/response) |
| 132 | * |
| 133 | * Return: 0 on success, otherwise -EINVAL |
| 134 | */ |
| 135 | int ksmbd_verify_smb_message(struct ksmbd_work *work) |
| 136 | { |
| 137 | struct smb2_hdr *smb2_hdr = ksmbd_req_buf_next(work); |
| 138 | struct smb_hdr *hdr; |
| 139 | |
| 140 | if (smb2_hdr->ProtocolId == SMB2_PROTO_NUMBER) |
| 141 | return ksmbd_smb2_check_message(work); |
| 142 | |
| 143 | hdr = smb_get_msg(buf: work->request_buf); |
| 144 | if (*(__le32 *)hdr->Protocol == SMB1_PROTO_NUMBER && |
| 145 | hdr->Command == SMB_COM_NEGOTIATE) { |
| 146 | work->conn->outstanding_credits++; |
| 147 | return 0; |
| 148 | } |
| 149 | |
| 150 | return -EINVAL; |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * ksmbd_smb_request() - check for valid smb request type |
| 155 | * @conn: connection instance |
| 156 | * |
| 157 | * Return: true on success, otherwise false |
| 158 | */ |
| 159 | bool ksmbd_smb_request(struct ksmbd_conn *conn) |
| 160 | { |
| 161 | __le32 *proto; |
| 162 | |
| 163 | if (conn->request_buf[0] != 0) |
| 164 | return false; |
| 165 | |
| 166 | proto = (__le32 *)smb_get_msg(buf: conn->request_buf); |
| 167 | if (*proto == SMB2_COMPRESSION_TRANSFORM_ID) { |
| 168 | pr_err_ratelimited("smb2 compression not support yet" ); |
| 169 | return false; |
| 170 | } |
| 171 | |
| 172 | if (*proto != SMB1_PROTO_NUMBER && |
| 173 | *proto != SMB2_PROTO_NUMBER && |
| 174 | *proto != SMB2_TRANSFORM_PROTO_NUM) |
| 175 | return false; |
| 176 | |
| 177 | return true; |
| 178 | } |
| 179 | |
| 180 | static bool supported_protocol(int idx) |
| 181 | { |
| 182 | if (idx == SMB2X_PROT && |
| 183 | (server_conf.min_protocol >= SMB21_PROT || |
| 184 | server_conf.max_protocol <= SMB311_PROT)) |
| 185 | return true; |
| 186 | |
| 187 | return (server_conf.min_protocol <= idx && |
| 188 | idx <= server_conf.max_protocol); |
| 189 | } |
| 190 | |
| 191 | static char *next_dialect(char *dialect, int *next_off, int bcount) |
| 192 | { |
| 193 | dialect = dialect + *next_off; |
| 194 | *next_off = strnlen(p: dialect, maxlen: bcount); |
| 195 | if (dialect[*next_off] != '\0') |
| 196 | return NULL; |
| 197 | return dialect; |
| 198 | } |
| 199 | |
| 200 | static int ksmbd_lookup_dialect_by_name(char *cli_dialects, __le16 byte_count) |
| 201 | { |
| 202 | int i, seq_num, bcount, next; |
| 203 | char *dialect; |
| 204 | |
| 205 | for (i = ARRAY_SIZE(smb1_protos) - 1; i >= 0; i--) { |
| 206 | seq_num = 0; |
| 207 | next = 0; |
| 208 | dialect = cli_dialects; |
| 209 | bcount = le16_to_cpu(byte_count); |
| 210 | do { |
| 211 | dialect = next_dialect(dialect, next_off: &next, bcount); |
| 212 | if (!dialect) |
| 213 | break; |
| 214 | ksmbd_debug(SMB, "client requested dialect %s\n" , |
| 215 | dialect); |
| 216 | if (!strcmp(dialect, smb1_protos[i].name)) { |
| 217 | if (supported_protocol(idx: smb1_protos[i].index)) { |
| 218 | ksmbd_debug(SMB, |
| 219 | "selected %s dialect\n" , |
| 220 | smb1_protos[i].name); |
| 221 | if (smb1_protos[i].index == SMB1_PROT) |
| 222 | return seq_num; |
| 223 | return smb1_protos[i].prot_id; |
| 224 | } |
| 225 | } |
| 226 | seq_num++; |
| 227 | bcount -= (++next); |
| 228 | } while (bcount > 0); |
| 229 | } |
| 230 | |
| 231 | return BAD_PROT_ID; |
| 232 | } |
| 233 | |
| 234 | int ksmbd_lookup_dialect_by_id(__le16 *cli_dialects, __le16 dialects_count) |
| 235 | { |
| 236 | int i; |
| 237 | int count; |
| 238 | |
| 239 | for (i = ARRAY_SIZE(smb2_protos) - 1; i >= 0; i--) { |
| 240 | count = le16_to_cpu(dialects_count); |
| 241 | while (--count >= 0) { |
| 242 | ksmbd_debug(SMB, "client requested dialect 0x%x\n" , |
| 243 | le16_to_cpu(cli_dialects[count])); |
| 244 | if (le16_to_cpu(cli_dialects[count]) != |
| 245 | smb2_protos[i].prot_id) |
| 246 | continue; |
| 247 | |
| 248 | if (supported_protocol(idx: smb2_protos[i].index)) { |
| 249 | ksmbd_debug(SMB, "selected %s dialect\n" , |
| 250 | smb2_protos[i].name); |
| 251 | return smb2_protos[i].prot_id; |
| 252 | } |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | return BAD_PROT_ID; |
| 257 | } |
| 258 | |
| 259 | static int ksmbd_negotiate_smb_dialect(void *buf) |
| 260 | { |
| 261 | int smb_buf_length = get_rfc1002_len(buf); |
| 262 | __le32 proto = ((struct smb2_hdr *)smb_get_msg(buf))->ProtocolId; |
| 263 | |
| 264 | if (proto == SMB2_PROTO_NUMBER) { |
| 265 | struct smb2_negotiate_req *req; |
| 266 | int smb2_neg_size = |
| 267 | offsetof(struct smb2_negotiate_req, Dialects); |
| 268 | |
| 269 | req = (struct smb2_negotiate_req *)smb_get_msg(buf); |
| 270 | if (smb2_neg_size > smb_buf_length) |
| 271 | goto err_out; |
| 272 | |
| 273 | if (struct_size(req, Dialects, le16_to_cpu(req->DialectCount)) > |
| 274 | smb_buf_length) |
| 275 | goto err_out; |
| 276 | |
| 277 | return ksmbd_lookup_dialect_by_id(cli_dialects: req->Dialects, |
| 278 | dialects_count: req->DialectCount); |
| 279 | } |
| 280 | |
| 281 | if (proto == SMB1_PROTO_NUMBER) { |
| 282 | struct smb_negotiate_req *req; |
| 283 | |
| 284 | req = (struct smb_negotiate_req *)smb_get_msg(buf); |
| 285 | if (le16_to_cpu(req->ByteCount) < 2) |
| 286 | goto err_out; |
| 287 | |
| 288 | if (offsetof(struct smb_negotiate_req, DialectsArray) + |
| 289 | le16_to_cpu(req->ByteCount) > smb_buf_length) { |
| 290 | goto err_out; |
| 291 | } |
| 292 | |
| 293 | return ksmbd_lookup_dialect_by_name(cli_dialects: req->DialectsArray, |
| 294 | byte_count: req->ByteCount); |
| 295 | } |
| 296 | |
| 297 | err_out: |
| 298 | return BAD_PROT_ID; |
| 299 | } |
| 300 | |
| 301 | #define SMB_COM_NEGOTIATE_EX 0x0 |
| 302 | |
| 303 | /** |
| 304 | * get_smb1_cmd_val() - get smb command value from smb header |
| 305 | * @work: smb work containing smb header |
| 306 | * |
| 307 | * Return: smb command value |
| 308 | */ |
| 309 | static u16 get_smb1_cmd_val(struct ksmbd_work *work) |
| 310 | { |
| 311 | return SMB_COM_NEGOTIATE_EX; |
| 312 | } |
| 313 | |
| 314 | /** |
| 315 | * init_smb1_rsp_hdr() - initialize smb negotiate response header |
| 316 | * @work: smb work containing smb request |
| 317 | * |
| 318 | * Return: 0 on success, otherwise -EINVAL |
| 319 | */ |
| 320 | static int init_smb1_rsp_hdr(struct ksmbd_work *work) |
| 321 | { |
| 322 | struct smb_hdr *rsp_hdr = (struct smb_hdr *)smb_get_msg(buf: work->response_buf); |
| 323 | struct smb_hdr *rcv_hdr = (struct smb_hdr *)smb_get_msg(buf: work->request_buf); |
| 324 | |
| 325 | rsp_hdr->Command = SMB_COM_NEGOTIATE; |
| 326 | *(__le32 *)rsp_hdr->Protocol = SMB1_PROTO_NUMBER; |
| 327 | rsp_hdr->Flags = SMBFLG_RESPONSE; |
| 328 | rsp_hdr->Flags2 = SMBFLG2_UNICODE | SMBFLG2_ERR_STATUS | |
| 329 | SMBFLG2_EXT_SEC | SMBFLG2_IS_LONG_NAME; |
| 330 | rsp_hdr->Pid = rcv_hdr->Pid; |
| 331 | rsp_hdr->Mid = rcv_hdr->Mid; |
| 332 | return 0; |
| 333 | } |
| 334 | |
| 335 | /** |
| 336 | * smb1_check_user_session() - check for valid session for a user |
| 337 | * @work: smb work containing smb request buffer |
| 338 | * |
| 339 | * Return: 0 on success, otherwise error |
| 340 | */ |
| 341 | static int smb1_check_user_session(struct ksmbd_work *work) |
| 342 | { |
| 343 | unsigned int cmd = work->conn->ops->get_cmd_val(work); |
| 344 | |
| 345 | if (cmd == SMB_COM_NEGOTIATE_EX) |
| 346 | return 0; |
| 347 | |
| 348 | return -EINVAL; |
| 349 | } |
| 350 | |
| 351 | /** |
| 352 | * smb1_allocate_rsp_buf() - allocate response buffer for a command |
| 353 | * @work: smb work containing smb request |
| 354 | * |
| 355 | * Return: 0 on success, otherwise -ENOMEM |
| 356 | */ |
| 357 | static int smb1_allocate_rsp_buf(struct ksmbd_work *work) |
| 358 | { |
| 359 | work->response_buf = kzalloc(MAX_CIFS_SMALL_BUFFER_SIZE, |
| 360 | KSMBD_DEFAULT_GFP); |
| 361 | work->response_sz = MAX_CIFS_SMALL_BUFFER_SIZE; |
| 362 | |
| 363 | if (!work->response_buf) { |
| 364 | pr_err("Failed to allocate %u bytes buffer\n" , |
| 365 | MAX_CIFS_SMALL_BUFFER_SIZE); |
| 366 | return -ENOMEM; |
| 367 | } |
| 368 | |
| 369 | return 0; |
| 370 | } |
| 371 | |
| 372 | /** |
| 373 | * set_smb1_rsp_status() - set error type in smb response header |
| 374 | * @work: smb work containing smb response header |
| 375 | * @err: error code to set in response |
| 376 | */ |
| 377 | static void set_smb1_rsp_status(struct ksmbd_work *work, __le32 err) |
| 378 | { |
| 379 | work->send_no_response = 1; |
| 380 | } |
| 381 | |
| 382 | static struct smb_version_ops smb1_server_ops = { |
| 383 | .get_cmd_val = get_smb1_cmd_val, |
| 384 | .init_rsp_hdr = init_smb1_rsp_hdr, |
| 385 | .allocate_rsp_buf = smb1_allocate_rsp_buf, |
| 386 | .check_user_session = smb1_check_user_session, |
| 387 | .set_rsp_status = set_smb1_rsp_status, |
| 388 | }; |
| 389 | |
| 390 | static struct smb_version_values smb1_server_values = { |
| 391 | .max_credits = SMB2_MAX_CREDITS, |
| 392 | }; |
| 393 | |
| 394 | static int smb1_negotiate(struct ksmbd_work *work) |
| 395 | { |
| 396 | return ksmbd_smb_negotiate_common(work, SMB_COM_NEGOTIATE); |
| 397 | } |
| 398 | |
| 399 | static struct smb_version_cmds smb1_server_cmds[1] = { |
| 400 | [SMB_COM_NEGOTIATE_EX] = { .proc = smb1_negotiate, }, |
| 401 | }; |
| 402 | |
| 403 | static int init_smb1_server(struct ksmbd_conn *conn) |
| 404 | { |
| 405 | conn->vals = &smb1_server_values; |
| 406 | conn->ops = &smb1_server_ops; |
| 407 | conn->cmds = smb1_server_cmds; |
| 408 | conn->max_cmds = ARRAY_SIZE(smb1_server_cmds); |
| 409 | return 0; |
| 410 | } |
| 411 | |
| 412 | int ksmbd_init_smb_server(struct ksmbd_conn *conn) |
| 413 | { |
| 414 | struct smb_hdr *rcv_hdr = (struct smb_hdr *)smb_get_msg(buf: conn->request_buf); |
| 415 | __le32 proto; |
| 416 | |
| 417 | proto = *(__le32 *)rcv_hdr->Protocol; |
| 418 | if (conn->need_neg == false) { |
| 419 | if (proto == SMB1_PROTO_NUMBER) |
| 420 | return -EINVAL; |
| 421 | return 0; |
| 422 | } |
| 423 | |
| 424 | if (proto == SMB1_PROTO_NUMBER) |
| 425 | return init_smb1_server(conn); |
| 426 | return init_smb3_11_server(conn); |
| 427 | } |
| 428 | |
| 429 | int ksmbd_populate_dot_dotdot_entries(struct ksmbd_work *work, int info_level, |
| 430 | struct ksmbd_file *dir, |
| 431 | struct ksmbd_dir_info *d_info, |
| 432 | char *search_pattern, |
| 433 | int (*fn)(struct ksmbd_conn *, int, |
| 434 | struct ksmbd_dir_info *, |
| 435 | struct ksmbd_kstat *)) |
| 436 | { |
| 437 | int i, rc = 0; |
| 438 | struct ksmbd_conn *conn = work->conn; |
| 439 | struct mnt_idmap *idmap = file_mnt_idmap(file: dir->filp); |
| 440 | |
| 441 | for (i = 0; i < 2; i++) { |
| 442 | struct kstat kstat; |
| 443 | struct ksmbd_kstat ksmbd_kstat; |
| 444 | struct dentry *dentry; |
| 445 | |
| 446 | if (!dir->dot_dotdot[i]) { /* fill dot entry info */ |
| 447 | if (i == 0) { |
| 448 | d_info->name = "." ; |
| 449 | d_info->name_len = 1; |
| 450 | dentry = dir->filp->f_path.dentry; |
| 451 | } else { |
| 452 | d_info->name = ".." ; |
| 453 | d_info->name_len = 2; |
| 454 | dentry = dir->filp->f_path.dentry->d_parent; |
| 455 | } |
| 456 | |
| 457 | if (!match_pattern(str: d_info->name, len: d_info->name_len, |
| 458 | pattern: search_pattern)) { |
| 459 | dir->dot_dotdot[i] = 1; |
| 460 | continue; |
| 461 | } |
| 462 | |
| 463 | ksmbd_kstat.kstat = &kstat; |
| 464 | rc = ksmbd_vfs_fill_dentry_attrs(work, |
| 465 | idmap, |
| 466 | dentry, |
| 467 | ksmbd_kstat: &ksmbd_kstat); |
| 468 | if (rc) |
| 469 | break; |
| 470 | |
| 471 | rc = fn(conn, info_level, d_info, &ksmbd_kstat); |
| 472 | if (rc) |
| 473 | break; |
| 474 | if (d_info->out_buf_len <= 0) |
| 475 | break; |
| 476 | |
| 477 | dir->dot_dotdot[i] = 1; |
| 478 | if (d_info->flags & SMB2_RETURN_SINGLE_ENTRY) { |
| 479 | d_info->out_buf_len = 0; |
| 480 | break; |
| 481 | } |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | return rc; |
| 486 | } |
| 487 | |
| 488 | /** |
| 489 | * ksmbd_extract_shortname() - get shortname from long filename |
| 490 | * @conn: connection instance |
| 491 | * @longname: source long filename |
| 492 | * @shortname: destination short filename |
| 493 | * |
| 494 | * Return: shortname length or 0 when source long name is '.' or '..' |
| 495 | * TODO: Though this function conforms the restriction of 8.3 Filename spec, |
| 496 | * but the result is different with Windows 7's one. need to check. |
| 497 | */ |
| 498 | int (struct ksmbd_conn *conn, const char *longname, |
| 499 | char *shortname) |
| 500 | { |
| 501 | const char *p; |
| 502 | char base[9], extension[4]; |
| 503 | char out[13] = {0}; |
| 504 | int baselen = 0; |
| 505 | int extlen = 0, len = 0; |
| 506 | unsigned int csum = 0; |
| 507 | const unsigned char *ptr; |
| 508 | bool dot_present = true; |
| 509 | |
| 510 | p = longname; |
| 511 | if ((*p == '.') || (!(strcmp(p, ".." )))) { |
| 512 | /*no mangling required */ |
| 513 | return 0; |
| 514 | } |
| 515 | |
| 516 | p = strrchr(longname, '.'); |
| 517 | if (p == longname) { /*name starts with a dot*/ |
| 518 | strscpy(extension, "___" , sizeof(extension)); |
| 519 | } else { |
| 520 | if (p) { |
| 521 | p++; |
| 522 | while (*p && extlen < 3) { |
| 523 | if (*p != '.') |
| 524 | extension[extlen++] = toupper(*p); |
| 525 | p++; |
| 526 | } |
| 527 | extension[extlen] = '\0'; |
| 528 | } else { |
| 529 | dot_present = false; |
| 530 | } |
| 531 | } |
| 532 | |
| 533 | p = longname; |
| 534 | if (*p == '.') { |
| 535 | p++; |
| 536 | longname++; |
| 537 | } |
| 538 | while (*p && (baselen < 5)) { |
| 539 | if (*p != '.') |
| 540 | base[baselen++] = toupper(*p); |
| 541 | p++; |
| 542 | } |
| 543 | |
| 544 | base[baselen] = MAGIC_CHAR; |
| 545 | memcpy(out, base, baselen + 1); |
| 546 | |
| 547 | ptr = longname; |
| 548 | len = strlen(longname); |
| 549 | for (; len > 0; len--, ptr++) |
| 550 | csum += *ptr; |
| 551 | |
| 552 | csum = csum % (MANGLE_BASE * MANGLE_BASE); |
| 553 | out[baselen + 1] = mangle(csum / MANGLE_BASE); |
| 554 | out[baselen + 2] = mangle(csum); |
| 555 | out[baselen + 3] = PERIOD; |
| 556 | |
| 557 | if (dot_present) |
| 558 | memcpy(out + baselen + 4, extension, 4); |
| 559 | else |
| 560 | out[baselen + 4] = '\0'; |
| 561 | smbConvertToUTF16(target: (__le16 *)shortname, source: out, PATH_MAX, |
| 562 | cp: conn->local_nls, mapchars: 0); |
| 563 | len = strlen(out) * 2; |
| 564 | return len; |
| 565 | } |
| 566 | |
| 567 | static int __smb2_negotiate(struct ksmbd_conn *conn) |
| 568 | { |
| 569 | return (conn->dialect >= SMB20_PROT_ID && |
| 570 | conn->dialect <= SMB311_PROT_ID); |
| 571 | } |
| 572 | |
| 573 | static int smb_handle_negotiate(struct ksmbd_work *work) |
| 574 | { |
| 575 | struct smb_negotiate_rsp *neg_rsp = smb_get_msg(buf: work->response_buf); |
| 576 | |
| 577 | ksmbd_debug(SMB, "Unsupported SMB1 protocol\n" ); |
| 578 | |
| 579 | if (ksmbd_iov_pin_rsp(work, ib: (void *)neg_rsp, |
| 580 | len: sizeof(struct smb_negotiate_rsp))) |
| 581 | return -ENOMEM; |
| 582 | |
| 583 | neg_rsp->hdr.Status.CifsError = STATUS_SUCCESS; |
| 584 | neg_rsp->hdr.WordCount = 1; |
| 585 | neg_rsp->DialectIndex = cpu_to_le16(work->conn->dialect); |
| 586 | neg_rsp->ByteCount = 0; |
| 587 | return 0; |
| 588 | } |
| 589 | |
| 590 | int ksmbd_smb_negotiate_common(struct ksmbd_work *work, unsigned int command) |
| 591 | { |
| 592 | struct ksmbd_conn *conn = work->conn; |
| 593 | int ret; |
| 594 | |
| 595 | conn->dialect = |
| 596 | ksmbd_negotiate_smb_dialect(buf: work->request_buf); |
| 597 | ksmbd_debug(SMB, "conn->dialect 0x%x\n" , conn->dialect); |
| 598 | |
| 599 | if (command == SMB2_NEGOTIATE_HE) { |
| 600 | ret = smb2_handle_negotiate(work); |
| 601 | return ret; |
| 602 | } |
| 603 | |
| 604 | if (command == SMB_COM_NEGOTIATE) { |
| 605 | if (__smb2_negotiate(conn)) { |
| 606 | init_smb3_11_server(conn); |
| 607 | init_smb2_neg_rsp(work); |
| 608 | ksmbd_debug(SMB, "Upgrade to SMB2 negotiation\n" ); |
| 609 | return 0; |
| 610 | } |
| 611 | return smb_handle_negotiate(work); |
| 612 | } |
| 613 | |
| 614 | pr_err("Unknown SMB negotiation command: %u\n" , command); |
| 615 | return -EINVAL; |
| 616 | } |
| 617 | |
| 618 | enum SHARED_MODE_ERRORS { |
| 619 | SHARE_DELETE_ERROR, |
| 620 | SHARE_READ_ERROR, |
| 621 | SHARE_WRITE_ERROR, |
| 622 | FILE_READ_ERROR, |
| 623 | FILE_WRITE_ERROR, |
| 624 | FILE_DELETE_ERROR, |
| 625 | }; |
| 626 | |
| 627 | static const char * const shared_mode_errors[] = { |
| 628 | "Current access mode does not permit SHARE_DELETE" , |
| 629 | "Current access mode does not permit SHARE_READ" , |
| 630 | "Current access mode does not permit SHARE_WRITE" , |
| 631 | "Desired access mode does not permit FILE_READ" , |
| 632 | "Desired access mode does not permit FILE_WRITE" , |
| 633 | "Desired access mode does not permit FILE_DELETE" , |
| 634 | }; |
| 635 | |
| 636 | static void smb_shared_mode_error(int error, struct ksmbd_file *prev_fp, |
| 637 | struct ksmbd_file *curr_fp) |
| 638 | { |
| 639 | ksmbd_debug(SMB, "%s\n" , shared_mode_errors[error]); |
| 640 | ksmbd_debug(SMB, "Current mode: 0x%x Desired mode: 0x%x\n" , |
| 641 | prev_fp->saccess, curr_fp->daccess); |
| 642 | } |
| 643 | |
| 644 | int ksmbd_smb_check_shared_mode(struct file *filp, struct ksmbd_file *curr_fp) |
| 645 | { |
| 646 | int rc = 0; |
| 647 | struct ksmbd_file *prev_fp; |
| 648 | |
| 649 | /* |
| 650 | * Lookup fp in master fp list, and check desired access and |
| 651 | * shared mode between previous open and current open. |
| 652 | */ |
| 653 | down_read(sem: &curr_fp->f_ci->m_lock); |
| 654 | list_for_each_entry(prev_fp, &curr_fp->f_ci->m_fp_list, node) { |
| 655 | if (file_inode(f: filp) != file_inode(f: prev_fp->filp)) |
| 656 | continue; |
| 657 | |
| 658 | if (filp == prev_fp->filp) |
| 659 | continue; |
| 660 | |
| 661 | if (ksmbd_stream_fd(fp: prev_fp) && ksmbd_stream_fd(fp: curr_fp)) |
| 662 | if (strcmp(prev_fp->stream.name, curr_fp->stream.name)) |
| 663 | continue; |
| 664 | |
| 665 | if (prev_fp->attrib_only != curr_fp->attrib_only) |
| 666 | continue; |
| 667 | |
| 668 | if (!(prev_fp->saccess & FILE_SHARE_DELETE_LE) && |
| 669 | curr_fp->daccess & FILE_DELETE_LE) { |
| 670 | smb_shared_mode_error(error: SHARE_DELETE_ERROR, |
| 671 | prev_fp, |
| 672 | curr_fp); |
| 673 | rc = -EPERM; |
| 674 | break; |
| 675 | } |
| 676 | |
| 677 | /* |
| 678 | * Only check FILE_SHARE_DELETE if stream opened and |
| 679 | * normal file opened. |
| 680 | */ |
| 681 | if (ksmbd_stream_fd(fp: prev_fp) && !ksmbd_stream_fd(fp: curr_fp)) |
| 682 | continue; |
| 683 | |
| 684 | if (!(prev_fp->saccess & FILE_SHARE_READ_LE) && |
| 685 | curr_fp->daccess & (FILE_EXECUTE_LE | FILE_READ_DATA_LE)) { |
| 686 | smb_shared_mode_error(error: SHARE_READ_ERROR, |
| 687 | prev_fp, |
| 688 | curr_fp); |
| 689 | rc = -EPERM; |
| 690 | break; |
| 691 | } |
| 692 | |
| 693 | if (!(prev_fp->saccess & FILE_SHARE_WRITE_LE) && |
| 694 | curr_fp->daccess & (FILE_WRITE_DATA_LE | FILE_APPEND_DATA_LE)) { |
| 695 | smb_shared_mode_error(error: SHARE_WRITE_ERROR, |
| 696 | prev_fp, |
| 697 | curr_fp); |
| 698 | rc = -EPERM; |
| 699 | break; |
| 700 | } |
| 701 | |
| 702 | if (prev_fp->daccess & (FILE_EXECUTE_LE | FILE_READ_DATA_LE) && |
| 703 | !(curr_fp->saccess & FILE_SHARE_READ_LE)) { |
| 704 | smb_shared_mode_error(error: FILE_READ_ERROR, |
| 705 | prev_fp, |
| 706 | curr_fp); |
| 707 | rc = -EPERM; |
| 708 | break; |
| 709 | } |
| 710 | |
| 711 | if (prev_fp->daccess & (FILE_WRITE_DATA_LE | FILE_APPEND_DATA_LE) && |
| 712 | !(curr_fp->saccess & FILE_SHARE_WRITE_LE)) { |
| 713 | smb_shared_mode_error(error: FILE_WRITE_ERROR, |
| 714 | prev_fp, |
| 715 | curr_fp); |
| 716 | rc = -EPERM; |
| 717 | break; |
| 718 | } |
| 719 | |
| 720 | if (prev_fp->daccess & FILE_DELETE_LE && |
| 721 | !(curr_fp->saccess & FILE_SHARE_DELETE_LE)) { |
| 722 | smb_shared_mode_error(error: FILE_DELETE_ERROR, |
| 723 | prev_fp, |
| 724 | curr_fp); |
| 725 | rc = -EPERM; |
| 726 | break; |
| 727 | } |
| 728 | } |
| 729 | up_read(sem: &curr_fp->f_ci->m_lock); |
| 730 | |
| 731 | return rc; |
| 732 | } |
| 733 | |
| 734 | bool is_asterisk(char *p) |
| 735 | { |
| 736 | return p && p[0] == '*'; |
| 737 | } |
| 738 | |
| 739 | int __ksmbd_override_fsids(struct ksmbd_work *work, |
| 740 | struct ksmbd_share_config *share) |
| 741 | { |
| 742 | struct ksmbd_session *sess = work->sess; |
| 743 | struct ksmbd_user *user = sess->user; |
| 744 | struct cred *cred; |
| 745 | struct group_info *gi; |
| 746 | unsigned int uid; |
| 747 | unsigned int gid; |
| 748 | int i; |
| 749 | |
| 750 | uid = user_uid(user); |
| 751 | gid = user_gid(user); |
| 752 | if (share->force_uid != KSMBD_SHARE_INVALID_UID) |
| 753 | uid = share->force_uid; |
| 754 | if (share->force_gid != KSMBD_SHARE_INVALID_GID) |
| 755 | gid = share->force_gid; |
| 756 | |
| 757 | cred = prepare_kernel_cred(&init_task); |
| 758 | if (!cred) |
| 759 | return -ENOMEM; |
| 760 | |
| 761 | cred->fsuid = make_kuid(from: &init_user_ns, uid); |
| 762 | cred->fsgid = make_kgid(from: &init_user_ns, gid); |
| 763 | |
| 764 | gi = groups_alloc(user->ngroups); |
| 765 | if (!gi) { |
| 766 | abort_creds(cred); |
| 767 | return -ENOMEM; |
| 768 | } |
| 769 | |
| 770 | for (i = 0; i < user->ngroups; i++) |
| 771 | gi->gid[i] = make_kgid(from: &init_user_ns, gid: user->sgid[i]); |
| 772 | |
| 773 | if (user->ngroups) |
| 774 | groups_sort(gi); |
| 775 | |
| 776 | set_groups(cred, gi); |
| 777 | put_group_info(gi); |
| 778 | |
| 779 | if (!uid_eq(left: cred->fsuid, GLOBAL_ROOT_UID)) |
| 780 | cred->cap_effective = cap_drop_fs_set(cred->cap_effective); |
| 781 | |
| 782 | WARN_ON(work->saved_cred); |
| 783 | work->saved_cred = override_creds(cred); |
| 784 | return 0; |
| 785 | } |
| 786 | |
| 787 | int ksmbd_override_fsids(struct ksmbd_work *work) |
| 788 | { |
| 789 | return __ksmbd_override_fsids(work, share: work->tcon->share_conf); |
| 790 | } |
| 791 | |
| 792 | void ksmbd_revert_fsids(struct ksmbd_work *work) |
| 793 | { |
| 794 | const struct cred *cred; |
| 795 | WARN_ON(!work->saved_cred); |
| 796 | |
| 797 | cred = revert_creds(revert_cred: work->saved_cred); |
| 798 | work->saved_cred = NULL; |
| 799 | put_cred(cred); |
| 800 | } |
| 801 | |
| 802 | __le32 smb_map_generic_desired_access(__le32 daccess) |
| 803 | { |
| 804 | if (daccess & FILE_GENERIC_READ_LE) { |
| 805 | daccess |= cpu_to_le32(GENERIC_READ_FLAGS); |
| 806 | daccess &= ~FILE_GENERIC_READ_LE; |
| 807 | } |
| 808 | |
| 809 | if (daccess & FILE_GENERIC_WRITE_LE) { |
| 810 | daccess |= cpu_to_le32(GENERIC_WRITE_FLAGS); |
| 811 | daccess &= ~FILE_GENERIC_WRITE_LE; |
| 812 | } |
| 813 | |
| 814 | if (daccess & FILE_GENERIC_EXECUTE_LE) { |
| 815 | daccess |= cpu_to_le32(GENERIC_EXECUTE_FLAGS); |
| 816 | daccess &= ~FILE_GENERIC_EXECUTE_LE; |
| 817 | } |
| 818 | |
| 819 | if (daccess & FILE_GENERIC_ALL_LE) { |
| 820 | daccess |= cpu_to_le32(GENERIC_ALL_FLAGS); |
| 821 | daccess &= ~FILE_GENERIC_ALL_LE; |
| 822 | } |
| 823 | |
| 824 | return daccess; |
| 825 | } |
| 826 | |