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