1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Copyright (C) 2016 Namjae Jeon <linkinjeon@kernel.org> 4 * Copyright (C) 2018 Samsung Electronics Co., Ltd. 5 */ 6 7 #include <crypto/sha2.h> 8 #include <linux/kernel.h> 9 #include <linux/fs.h> 10 #include <linux/filelock.h> 11 #include <linux/uaccess.h> 12 #include <linux/backing-dev.h> 13 #include <linux/writeback.h> 14 #include <linux/xattr.h> 15 #include <linux/falloc.h> 16 #include <linux/fsnotify.h> 17 #include <linux/dcache.h> 18 #include <linux/slab.h> 19 #include <linux/vmalloc.h> 20 #include <linux/sched/xacct.h> 21 #include <linux/crc32c.h> 22 #include <linux/namei.h> 23 #include <linux/splice.h> 24 25 #include "glob.h" 26 #include "oplock.h" 27 #include "connection.h" 28 #include "vfs.h" 29 #include "vfs_cache.h" 30 #include "smbacl.h" 31 #include "ndr.h" 32 #include "auth.h" 33 #include "misc.h" 34 35 #include "smb_common.h" 36 #include "mgmt/share_config.h" 37 #include "mgmt/tree_connect.h" 38 #include "mgmt/user_session.h" 39 #include "mgmt/user_config.h" 40 41 static void ksmbd_vfs_inherit_owner(struct ksmbd_work *work, 42 struct inode *parent_inode, 43 struct inode *inode) 44 { 45 if (!test_share_config_flag(work->tcon->share_conf, 46 KSMBD_SHARE_FLAG_INHERIT_OWNER)) 47 return; 48 49 i_uid_write(inode, i_uid_read(parent_inode)); 50 } 51 52 static int ksmbd_vfs_path_lookup(struct ksmbd_share_config *share_conf, 53 char *pathname, unsigned int flags, 54 struct path *path, bool for_remove) 55 { 56 struct qstr last; 57 const struct path *root_share_path = &share_conf->vfs_path; 58 int err, type; 59 struct dentry *d; 60 61 if (pathname[0] == '\0') { 62 pathname = share_conf->path; 63 root_share_path = NULL; 64 } else { 65 flags |= LOOKUP_BENEATH; 66 } 67 68 CLASS(filename_kernel, filename)(pathname); 69 err = vfs_path_parent_lookup(filename, flags, 70 path, &last, &type, 71 root_share_path); 72 if (err) 73 return err; 74 75 if (unlikely(type != LAST_NORM)) { 76 path_put(path); 77 return -ENOENT; 78 } 79 80 if (for_remove) { 81 err = mnt_want_write(path->mnt); 82 if (err) { 83 path_put(path); 84 return -ENOENT; 85 } 86 87 d = start_removing_noperm(path->dentry, &last); 88 89 if (!IS_ERR(d)) { 90 dput(path->dentry); 91 path->dentry = d; 92 return 0; 93 } 94 mnt_drop_write(path->mnt); 95 path_put(path); 96 return -ENOENT; 97 } 98 99 d = lookup_noperm_unlocked(&last, path->dentry); 100 if (!IS_ERR(d) && d_is_negative(d)) { 101 dput(d); 102 d = ERR_PTR(-ENOENT); 103 } 104 if (IS_ERR(d)) { 105 path_put(path); 106 return -ENOENT; 107 } 108 dput(path->dentry); 109 path->dentry = d; 110 111 if (test_share_config_flag(share_conf, KSMBD_SHARE_FLAG_CROSSMNT)) { 112 err = follow_down(path, 0); 113 if (err < 0) { 114 path_put(path); 115 return -ENOENT; 116 } 117 } 118 return 0; 119 } 120 121 void ksmbd_vfs_query_maximal_access(struct mnt_idmap *idmap, 122 struct dentry *dentry, __le32 *daccess) 123 { 124 *daccess = cpu_to_le32(FILE_READ_ATTRIBUTES | READ_CONTROL); 125 126 if (!inode_permission(idmap, d_inode(dentry), MAY_OPEN | MAY_WRITE)) 127 *daccess |= cpu_to_le32(WRITE_DAC | WRITE_OWNER | SYNCHRONIZE | 128 FILE_WRITE_DATA | FILE_APPEND_DATA | 129 FILE_WRITE_EA | FILE_WRITE_ATTRIBUTES | 130 FILE_DELETE_CHILD); 131 132 if (!inode_permission(idmap, d_inode(dentry), MAY_OPEN | MAY_READ)) 133 *daccess |= FILE_READ_DATA_LE | FILE_READ_EA_LE; 134 135 if (!inode_permission(idmap, d_inode(dentry), MAY_OPEN | MAY_EXEC)) 136 *daccess |= FILE_EXECUTE_LE; 137 138 if (!inode_permission(idmap, d_inode(dentry->d_parent), MAY_EXEC | MAY_WRITE)) 139 *daccess |= FILE_DELETE_LE; 140 } 141 142 /** 143 * ksmbd_vfs_create() - vfs helper for smb create file 144 * @work: work 145 * @name: file name that is relative to share 146 * @mode: file create mode 147 * 148 * Return: 0 on success, otherwise error 149 */ 150 int ksmbd_vfs_create(struct ksmbd_work *work, const char *name, umode_t mode) 151 { 152 struct path path; 153 struct dentry *dentry; 154 int err; 155 156 dentry = ksmbd_vfs_kern_path_create(work, name, 157 LOOKUP_NO_SYMLINKS, &path); 158 if (IS_ERR(dentry)) { 159 err = PTR_ERR(dentry); 160 if (err != -ENOENT) 161 pr_err("path create failed for %s, err %d\n", 162 name, err); 163 return err; 164 } 165 166 mode |= S_IFREG; 167 err = vfs_create(mnt_idmap(path.mnt), dentry, mode, NULL); 168 if (!err) { 169 ksmbd_vfs_inherit_owner(work, d_inode(path.dentry), 170 d_inode(dentry)); 171 } else { 172 pr_err("File(%s): creation failed (err:%d)\n", name, err); 173 } 174 175 end_creating_path(&path, dentry); 176 return err; 177 } 178 179 /** 180 * ksmbd_vfs_mkdir() - vfs helper for smb create directory 181 * @work: work 182 * @name: directory name that is relative to share 183 * @mode: directory create mode 184 * 185 * Return: 0 on success, otherwise error 186 */ 187 int ksmbd_vfs_mkdir(struct ksmbd_work *work, const char *name, umode_t mode) 188 { 189 struct mnt_idmap *idmap; 190 struct path path; 191 struct dentry *dentry, *d; 192 int err = 0; 193 194 dentry = ksmbd_vfs_kern_path_create(work, name, 195 LOOKUP_NO_SYMLINKS | LOOKUP_DIRECTORY, 196 &path); 197 if (IS_ERR(dentry)) { 198 err = PTR_ERR(dentry); 199 if (err != -EEXIST) 200 ksmbd_debug(VFS, "path create failed for %s, err %d\n", 201 name, err); 202 return err; 203 } 204 205 idmap = mnt_idmap(path.mnt); 206 mode |= S_IFDIR; 207 d = dentry; 208 dentry = vfs_mkdir(idmap, d_inode(path.dentry), dentry, mode, NULL); 209 if (IS_ERR(dentry)) 210 err = PTR_ERR(dentry); 211 else if (d_is_negative(dentry)) 212 err = -ENOENT; 213 if (!err && dentry != d) 214 ksmbd_vfs_inherit_owner(work, d_inode(path.dentry), d_inode(dentry)); 215 216 end_creating_path(&path, dentry); 217 if (err) 218 pr_err("mkdir(%s): creation failed (err:%d)\n", name, err); 219 return err; 220 } 221 222 static ssize_t ksmbd_vfs_getcasexattr(struct mnt_idmap *idmap, 223 struct dentry *dentry, char *attr_name, 224 int attr_name_len, char **attr_value) 225 { 226 char *name, *xattr_list = NULL; 227 ssize_t value_len = -ENOENT, xattr_list_len; 228 229 xattr_list_len = ksmbd_vfs_listxattr(dentry, &xattr_list); 230 if (xattr_list_len <= 0) 231 goto out; 232 233 for (name = xattr_list; name - xattr_list < xattr_list_len; 234 name += strlen(name) + 1) { 235 ksmbd_debug(VFS, "%s, len %zd\n", name, strlen(name)); 236 if (strncasecmp(attr_name, name, attr_name_len)) 237 continue; 238 239 value_len = ksmbd_vfs_getxattr(idmap, 240 dentry, 241 name, 242 attr_value); 243 if (value_len < 0) 244 pr_err("failed to get xattr in file\n"); 245 break; 246 } 247 248 out: 249 kvfree(xattr_list); 250 return value_len; 251 } 252 253 static int ksmbd_vfs_stream_read(struct ksmbd_file *fp, char *buf, loff_t *pos, 254 size_t count) 255 { 256 ssize_t v_len; 257 char *stream_buf = NULL; 258 259 ksmbd_debug(VFS, "read stream data pos : %llu, count : %zd\n", 260 *pos, count); 261 262 v_len = ksmbd_vfs_getcasexattr(file_mnt_idmap(fp->filp), 263 fp->filp->f_path.dentry, 264 fp->stream.name, 265 fp->stream.size, 266 &stream_buf); 267 if ((int)v_len <= 0) 268 return (int)v_len; 269 270 if (v_len <= *pos) { 271 count = -EINVAL; 272 goto free_buf; 273 } 274 275 if (v_len - *pos < count) 276 count = v_len - *pos; 277 fp->stream.pos = v_len; 278 279 memcpy(buf, &stream_buf[*pos], count); 280 281 free_buf: 282 kvfree(stream_buf); 283 return count; 284 } 285 286 /** 287 * check_lock_range() - vfs helper for smb byte range file locking 288 * @filp: the file to apply the lock to 289 * @start: lock start byte offset 290 * @end: lock end byte offset 291 * @type: byte range type read/write 292 * 293 * Return: 0 on success, otherwise error 294 */ 295 static int check_lock_range(struct file *filp, loff_t start, loff_t end, 296 unsigned char type) 297 { 298 struct file_lock *flock; 299 struct file_lock_context *ctx = locks_inode_context(file_inode(filp)); 300 int error = 0; 301 302 if (start == end) 303 return 0; 304 305 if (!ctx || list_empty_careful(&ctx->flc_posix)) 306 return 0; 307 308 spin_lock(&ctx->flc_lock); 309 for_each_file_lock(flock, &ctx->flc_posix) { 310 /* check conflict locks */ 311 if (flock->fl_end >= start && end >= flock->fl_start) { 312 if (lock_is_read(flock)) { 313 if (type == WRITE) { 314 pr_err("not allow write by shared lock\n"); 315 error = 1; 316 goto out; 317 } 318 } else if (lock_is_write(flock)) { 319 /* check owner in lock */ 320 if (flock->c.flc_file != filp) { 321 error = 1; 322 pr_err("not allow rw access by exclusive lock from other opens\n"); 323 goto out; 324 } 325 } 326 } 327 } 328 out: 329 spin_unlock(&ctx->flc_lock); 330 return error; 331 } 332 333 /** 334 * ksmbd_vfs_read() - vfs helper for smb file read 335 * @work: smb work 336 * @fp: ksmbd file pointer 337 * @count: read byte count 338 * @pos: file pos 339 * @rbuf: read data buffer 340 * 341 * Return: number of read bytes on success, otherwise error 342 */ 343 int ksmbd_vfs_read(struct ksmbd_work *work, struct ksmbd_file *fp, size_t count, 344 loff_t *pos, char *rbuf) 345 { 346 struct file *filp = fp->filp; 347 ssize_t nbytes = 0; 348 struct inode *inode = file_inode(filp); 349 350 if (S_ISDIR(inode->i_mode)) 351 return -EISDIR; 352 353 if (unlikely(count == 0)) 354 return 0; 355 356 if (work->conn->connection_type) { 357 if (!(fp->daccess & (FILE_READ_DATA_LE | FILE_EXECUTE_LE))) { 358 pr_err("no right to read(%pD)\n", fp->filp); 359 return -EACCES; 360 } 361 } 362 363 if (ksmbd_stream_fd(fp)) 364 return ksmbd_vfs_stream_read(fp, rbuf, pos, count); 365 366 if (!work->tcon->posix_extensions) { 367 int ret; 368 369 ret = check_lock_range(filp, *pos, *pos + count - 1, READ); 370 if (ret) { 371 pr_err("unable to read due to lock\n"); 372 return -EAGAIN; 373 } 374 } 375 376 nbytes = kernel_read(filp, rbuf, count, pos); 377 if (nbytes < 0) { 378 pr_err("smb read failed, err = %zd\n", nbytes); 379 return nbytes; 380 } 381 382 filp->f_pos = *pos; 383 return nbytes; 384 } 385 386 static int ksmbd_vfs_stream_write(struct ksmbd_file *fp, char *buf, loff_t *pos, 387 size_t count) 388 { 389 char *stream_buf = NULL, *wbuf; 390 struct mnt_idmap *idmap = file_mnt_idmap(fp->filp); 391 size_t size; 392 ssize_t v_len; 393 int err = 0; 394 395 ksmbd_debug(VFS, "write stream data pos : %llu, count : %zd\n", 396 *pos, count); 397 398 if (*pos >= XATTR_SIZE_MAX) { 399 pr_err("stream write position %lld is out of bounds\n", *pos); 400 return -EINVAL; 401 } 402 403 size = *pos + count; 404 if (size > XATTR_SIZE_MAX) { 405 size = XATTR_SIZE_MAX; 406 count = XATTR_SIZE_MAX - *pos; 407 } 408 409 v_len = ksmbd_vfs_getcasexattr(idmap, 410 fp->filp->f_path.dentry, 411 fp->stream.name, 412 fp->stream.size, 413 &stream_buf); 414 if (v_len < 0) { 415 pr_err("not found stream in xattr : %zd\n", v_len); 416 err = v_len; 417 goto out; 418 } 419 420 if (v_len < size) { 421 wbuf = kvzalloc(size, KSMBD_DEFAULT_GFP); 422 if (!wbuf) { 423 err = -ENOMEM; 424 goto out; 425 } 426 427 if (v_len > 0) 428 memcpy(wbuf, stream_buf, v_len); 429 kvfree(stream_buf); 430 stream_buf = wbuf; 431 } 432 433 memcpy(&stream_buf[*pos], buf, count); 434 435 err = ksmbd_vfs_setxattr(idmap, 436 &fp->filp->f_path, 437 fp->stream.name, 438 (void *)stream_buf, 439 size, 440 0, 441 true); 442 if (err < 0) 443 goto out; 444 else 445 fp->stream.pos = size; 446 err = 0; 447 out: 448 kvfree(stream_buf); 449 return err; 450 } 451 452 /** 453 * ksmbd_vfs_write() - vfs helper for smb file write 454 * @work: work 455 * @fp: ksmbd file pointer 456 * @buf: buf containing data for writing 457 * @count: read byte count 458 * @pos: file pos 459 * @sync: fsync after write 460 * @written: number of bytes written 461 * 462 * Return: 0 on success, otherwise error 463 */ 464 int ksmbd_vfs_write(struct ksmbd_work *work, struct ksmbd_file *fp, 465 char *buf, size_t count, loff_t *pos, bool sync, 466 ssize_t *written) 467 { 468 struct file *filp; 469 loff_t offset = *pos; 470 int err = 0; 471 472 if (work->conn->connection_type) { 473 if (!(fp->daccess & (FILE_WRITE_DATA_LE | FILE_APPEND_DATA_LE)) || 474 S_ISDIR(file_inode(fp->filp)->i_mode)) { 475 pr_err("no right to write(%pD)\n", fp->filp); 476 err = -EACCES; 477 goto out; 478 } 479 } 480 481 filp = fp->filp; 482 483 if (ksmbd_stream_fd(fp)) { 484 err = ksmbd_vfs_stream_write(fp, buf, pos, count); 485 if (!err) 486 *written = count; 487 goto out; 488 } 489 490 if (!work->tcon->posix_extensions) { 491 err = check_lock_range(filp, *pos, *pos + count - 1, WRITE); 492 if (err) { 493 pr_err("unable to write due to lock\n"); 494 err = -EAGAIN; 495 goto out; 496 } 497 } 498 499 /* Reserve lease break for parent dir at closing time */ 500 fp->reserve_lease_break = true; 501 502 /* Do we need to break any of a levelII oplock? */ 503 smb_break_all_levII_oplock(work, fp, 1); 504 505 err = kernel_write(filp, buf, count, pos); 506 if (err < 0) { 507 ksmbd_debug(VFS, "smb write failed, err = %d\n", err); 508 goto out; 509 } 510 511 filp->f_pos = *pos; 512 *written = err; 513 err = 0; 514 if (sync) { 515 err = vfs_fsync_range(filp, offset, offset + *written, 0); 516 if (err < 0) 517 pr_err("fsync failed for filename = %pD, err = %d\n", 518 fp->filp, err); 519 } 520 521 out: 522 return err; 523 } 524 525 /** 526 * ksmbd_vfs_getattr() - vfs helper for smb getattr 527 * @path: path of dentry 528 * @stat: pointer to returned kernel stat structure 529 * Return: 0 on success, otherwise error 530 */ 531 int ksmbd_vfs_getattr(const struct path *path, struct kstat *stat) 532 { 533 int err; 534 535 err = vfs_getattr(path, stat, STATX_BASIC_STATS | STATX_BTIME, 536 AT_STATX_SYNC_AS_STAT); 537 if (err) 538 pr_err("getattr failed, err %d\n", err); 539 return err; 540 } 541 542 /** 543 * ksmbd_vfs_fsync() - vfs helper for smb fsync 544 * @work: work 545 * @fid: file id of open file 546 * @p_id: persistent file id 547 * 548 * Return: 0 on success, otherwise error 549 */ 550 int ksmbd_vfs_fsync(struct ksmbd_work *work, u64 fid, u64 p_id) 551 { 552 struct ksmbd_file *fp; 553 int err; 554 555 fp = ksmbd_lookup_fd_slow(work, fid, p_id); 556 if (!fp) { 557 pr_err("failed to get filp for fid %llu\n", fid); 558 return -ENOENT; 559 } 560 err = vfs_fsync(fp->filp, 0); 561 if (err < 0) 562 pr_err("smb fsync failed, err = %d\n", err); 563 ksmbd_fd_put(work, fp); 564 return err; 565 } 566 567 /** 568 * ksmbd_vfs_remove_file() - vfs helper for smb rmdir or unlink 569 * @work: work 570 * @path: path of dentry 571 * 572 * Return: 0 on success, otherwise error 573 */ 574 int ksmbd_vfs_remove_file(struct ksmbd_work *work, const struct path *path) 575 { 576 struct mnt_idmap *idmap; 577 struct dentry *parent = path->dentry->d_parent; 578 int err; 579 580 if (ksmbd_override_fsids(work)) 581 return -ENOMEM; 582 583 if (!d_inode(path->dentry)->i_nlink) { 584 err = -ENOENT; 585 goto out_err; 586 } 587 588 idmap = mnt_idmap(path->mnt); 589 if (S_ISDIR(d_inode(path->dentry)->i_mode)) { 590 err = vfs_rmdir(idmap, d_inode(parent), path->dentry, NULL); 591 if (err && err != -ENOTEMPTY) 592 ksmbd_debug(VFS, "rmdir failed, err %d\n", err); 593 } else { 594 err = vfs_unlink(idmap, d_inode(parent), path->dentry, NULL); 595 if (err) 596 ksmbd_debug(VFS, "unlink failed, err %d\n", err); 597 } 598 599 out_err: 600 ksmbd_revert_fsids(work); 601 return err; 602 } 603 604 /** 605 * ksmbd_vfs_link() - vfs helper for creating smb hardlink 606 * @work: work 607 * @oldname: source file name 608 * @newname: hardlink name that is relative to share 609 * 610 * Return: 0 on success, otherwise error 611 */ 612 int ksmbd_vfs_link(struct ksmbd_work *work, const char *oldname, 613 const char *newname) 614 { 615 struct path oldpath, newpath; 616 struct dentry *dentry; 617 int err; 618 619 if (ksmbd_override_fsids(work)) 620 return -ENOMEM; 621 622 err = kern_path(oldname, LOOKUP_NO_SYMLINKS, &oldpath); 623 if (err) { 624 pr_err("cannot get linux path for %s, err = %d\n", 625 oldname, err); 626 goto out1; 627 } 628 629 dentry = ksmbd_vfs_kern_path_create(work, newname, 630 LOOKUP_NO_SYMLINKS | LOOKUP_REVAL, 631 &newpath); 632 if (IS_ERR(dentry)) { 633 err = PTR_ERR(dentry); 634 pr_err("path create err for %s, err %d\n", newname, err); 635 goto out2; 636 } 637 638 err = -EXDEV; 639 if (oldpath.mnt != newpath.mnt) { 640 pr_err("vfs_link failed err %d\n", err); 641 goto out3; 642 } 643 644 err = vfs_link(oldpath.dentry, mnt_idmap(newpath.mnt), 645 d_inode(newpath.dentry), 646 dentry, NULL); 647 if (err) 648 ksmbd_debug(VFS, "vfs_link failed err %d\n", err); 649 650 out3: 651 end_creating_path(&newpath, dentry); 652 out2: 653 path_put(&oldpath); 654 out1: 655 ksmbd_revert_fsids(work); 656 return err; 657 } 658 659 int ksmbd_vfs_rename(struct ksmbd_work *work, const struct path *old_path, 660 char *newname, int flags) 661 { 662 struct dentry *old_child = old_path->dentry; 663 struct path new_path; 664 struct qstr new_last; 665 struct renamedata rd; 666 struct ksmbd_share_config *share_conf = work->tcon->share_conf; 667 struct ksmbd_file *parent_fp; 668 int new_type; 669 int err, lookup_flags = LOOKUP_NO_SYMLINKS; 670 671 if (ksmbd_override_fsids(work)) 672 return -ENOMEM; 673 674 CLASS(filename_kernel, to)(newname); 675 676 retry: 677 err = vfs_path_parent_lookup(to, lookup_flags | LOOKUP_BENEATH, 678 &new_path, &new_last, &new_type, 679 &share_conf->vfs_path); 680 if (err) 681 goto out1; 682 683 if (old_path->mnt != new_path.mnt) { 684 err = -EXDEV; 685 goto out2; 686 } 687 688 err = mnt_want_write(old_path->mnt); 689 if (err) 690 goto out2; 691 692 rd.mnt_idmap = mnt_idmap(old_path->mnt); 693 rd.old_parent = NULL; 694 rd.new_parent = new_path.dentry; 695 rd.flags = flags; 696 rd.delegated_inode = NULL; 697 err = start_renaming_dentry(&rd, lookup_flags, old_child, &new_last); 698 if (err) 699 goto out_drop_write; 700 701 parent_fp = ksmbd_lookup_fd_inode(old_child->d_parent); 702 if (parent_fp) { 703 if (parent_fp->daccess & FILE_DELETE_LE) { 704 pr_err("parent dir is opened with delete access\n"); 705 err = -ESHARE; 706 ksmbd_fd_put(work, parent_fp); 707 goto out3; 708 } 709 ksmbd_fd_put(work, parent_fp); 710 } 711 712 if (d_is_symlink(rd.new_dentry)) { 713 err = -EACCES; 714 goto out3; 715 } 716 717 err = vfs_rename(&rd); 718 if (err) 719 ksmbd_debug(VFS, "vfs_rename failed err %d\n", err); 720 721 out3: 722 end_renaming(&rd); 723 out_drop_write: 724 mnt_drop_write(old_path->mnt); 725 out2: 726 path_put(&new_path); 727 728 if (retry_estale(err, lookup_flags)) { 729 lookup_flags |= LOOKUP_REVAL; 730 goto retry; 731 } 732 out1: 733 ksmbd_revert_fsids(work); 734 return err; 735 } 736 737 /** 738 * ksmbd_vfs_truncate() - vfs helper for smb file truncate 739 * @work: work 740 * @fp: ksmbd file pointer 741 * @size: truncate to given size 742 * 743 * Return: 0 on success, otherwise error 744 */ 745 int ksmbd_vfs_truncate(struct ksmbd_work *work, 746 struct ksmbd_file *fp, loff_t size) 747 { 748 int err = 0; 749 struct file *filp; 750 751 filp = fp->filp; 752 753 /* Do we need to break any of a levelII oplock? */ 754 smb_break_all_levII_oplock(work, fp, 1); 755 756 if (!work->tcon->posix_extensions) { 757 struct inode *inode = file_inode(filp); 758 759 if (size < inode->i_size) { 760 err = check_lock_range(filp, size, 761 inode->i_size - 1, WRITE); 762 } else if (size > inode->i_size) { 763 err = check_lock_range(filp, inode->i_size, 764 size - 1, WRITE); 765 } 766 767 if (err) { 768 pr_err("failed due to lock\n"); 769 return -EAGAIN; 770 } 771 } 772 773 err = vfs_truncate(&filp->f_path, size); 774 if (err) 775 pr_err("truncate failed, err %d\n", err); 776 return err; 777 } 778 779 /** 780 * ksmbd_vfs_listxattr() - vfs helper for smb list extended attributes 781 * @dentry: dentry of file for listing xattrs 782 * @list: destination buffer 783 * 784 * Return: xattr list length on success, otherwise error 785 */ 786 ssize_t ksmbd_vfs_listxattr(struct dentry *dentry, char **list) 787 { 788 ssize_t size; 789 char *vlist = NULL; 790 791 size = vfs_listxattr(dentry, NULL, 0); 792 if (size <= 0) 793 return size; 794 795 vlist = kvzalloc(size, KSMBD_DEFAULT_GFP); 796 if (!vlist) 797 return -ENOMEM; 798 799 *list = vlist; 800 size = vfs_listxattr(dentry, vlist, size); 801 if (size < 0) { 802 ksmbd_debug(VFS, "listxattr failed\n"); 803 kvfree(vlist); 804 *list = NULL; 805 } 806 807 return size; 808 } 809 810 static ssize_t ksmbd_vfs_xattr_len(struct mnt_idmap *idmap, 811 struct dentry *dentry, char *xattr_name) 812 { 813 return vfs_getxattr(idmap, dentry, xattr_name, NULL, 0); 814 } 815 816 /** 817 * ksmbd_vfs_getxattr() - vfs helper for smb get extended attributes value 818 * @idmap: idmap 819 * @dentry: dentry of file for getting xattrs 820 * @xattr_name: name of xattr name to query 821 * @xattr_buf: destination buffer xattr value 822 * 823 * Return: read xattr value length on success, otherwise error 824 */ 825 ssize_t ksmbd_vfs_getxattr(struct mnt_idmap *idmap, 826 struct dentry *dentry, 827 char *xattr_name, char **xattr_buf) 828 { 829 ssize_t xattr_len; 830 char *buf; 831 832 *xattr_buf = NULL; 833 xattr_len = ksmbd_vfs_xattr_len(idmap, dentry, xattr_name); 834 if (xattr_len < 0) 835 return xattr_len; 836 837 buf = kmalloc(xattr_len + 1, KSMBD_DEFAULT_GFP); 838 if (!buf) 839 return -ENOMEM; 840 841 xattr_len = vfs_getxattr(idmap, dentry, xattr_name, 842 (void *)buf, xattr_len); 843 if (xattr_len > 0) 844 *xattr_buf = buf; 845 else 846 kfree(buf); 847 return xattr_len; 848 } 849 850 /** 851 * ksmbd_vfs_setxattr() - vfs helper for smb set extended attributes value 852 * @idmap: idmap of the relevant mount 853 * @path: path of dentry to set XATTR at 854 * @attr_name: xattr name for setxattr 855 * @attr_value: xattr value to set 856 * @attr_size: size of xattr value 857 * @flags: destination buffer length 858 * @get_write: get write access to a mount 859 * 860 * Return: 0 on success, otherwise error 861 */ 862 int ksmbd_vfs_setxattr(struct mnt_idmap *idmap, 863 const struct path *path, const char *attr_name, 864 void *attr_value, size_t attr_size, int flags, 865 bool get_write) 866 { 867 int err; 868 869 if (get_write == true) { 870 err = mnt_want_write(path->mnt); 871 if (err) 872 return err; 873 } 874 875 err = vfs_setxattr(idmap, 876 path->dentry, 877 attr_name, 878 attr_value, 879 attr_size, 880 flags); 881 if (err) 882 ksmbd_debug(VFS, "setxattr failed, err %d\n", err); 883 if (get_write == true) 884 mnt_drop_write(path->mnt); 885 return err; 886 } 887 888 /** 889 * ksmbd_vfs_set_fadvise() - convert smb IO caching options to linux options 890 * @filp: file pointer for IO 891 * @option: smb IO options 892 */ 893 void ksmbd_vfs_set_fadvise(struct file *filp, __le32 option) 894 { 895 struct address_space *mapping; 896 897 mapping = filp->f_mapping; 898 899 if (!option || !mapping) 900 return; 901 902 if (option & FILE_WRITE_THROUGH_LE) { 903 filp->f_flags |= O_SYNC; 904 } else if (option & FILE_SEQUENTIAL_ONLY_LE) { 905 filp->f_ra.ra_pages = inode_to_bdi(mapping->host)->ra_pages * 2; 906 spin_lock(&filp->f_lock); 907 filp->f_mode &= ~FMODE_RANDOM; 908 spin_unlock(&filp->f_lock); 909 } else if (option & FILE_RANDOM_ACCESS_LE) { 910 spin_lock(&filp->f_lock); 911 filp->f_mode |= FMODE_RANDOM; 912 spin_unlock(&filp->f_lock); 913 } 914 } 915 916 int ksmbd_vfs_zero_data(struct ksmbd_work *work, struct ksmbd_file *fp, 917 loff_t off, loff_t len) 918 { 919 smb_break_all_levII_oplock(work, fp, 1); 920 if (fp->f_ci->m_fattr & FILE_ATTRIBUTE_SPARSE_FILE_LE) 921 return vfs_fallocate(fp->filp, 922 FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, 923 off, len); 924 925 return vfs_fallocate(fp->filp, 926 FALLOC_FL_ZERO_RANGE | FALLOC_FL_KEEP_SIZE, 927 off, len); 928 } 929 930 int ksmbd_vfs_fqar_lseek(struct ksmbd_file *fp, loff_t start, loff_t length, 931 struct file_allocated_range_buffer *ranges, 932 unsigned int in_count, unsigned int *out_count) 933 { 934 struct file *f = fp->filp; 935 struct inode *inode = file_inode(fp->filp); 936 loff_t maxbytes = (u64)inode->i_sb->s_maxbytes, end; 937 loff_t extent_start, extent_end; 938 int ret = 0; 939 940 if (start > maxbytes) 941 return -EFBIG; 942 943 if (!in_count) 944 return 0; 945 946 /* 947 * Shrink request scope to what the fs can actually handle. 948 */ 949 if (length > maxbytes || (maxbytes - length) < start) 950 length = maxbytes - start; 951 952 if (start + length > inode->i_size) 953 length = inode->i_size - start; 954 955 *out_count = 0; 956 end = start + length; 957 while (start < end && *out_count < in_count) { 958 extent_start = vfs_llseek(f, start, SEEK_DATA); 959 if (extent_start < 0) { 960 if (extent_start != -ENXIO) 961 ret = (int)extent_start; 962 break; 963 } 964 965 if (extent_start >= end) 966 break; 967 968 extent_end = vfs_llseek(f, extent_start, SEEK_HOLE); 969 if (extent_end < 0) { 970 if (extent_end != -ENXIO) 971 ret = (int)extent_end; 972 break; 973 } else if (extent_start >= extent_end) { 974 break; 975 } 976 977 ranges[*out_count].file_offset = cpu_to_le64(extent_start); 978 ranges[(*out_count)++].length = 979 cpu_to_le64(min(extent_end, end) - extent_start); 980 981 start = extent_end; 982 } 983 984 return ret; 985 } 986 987 int ksmbd_vfs_remove_xattr(struct mnt_idmap *idmap, 988 const struct path *path, char *attr_name, 989 bool get_write) 990 { 991 int err; 992 993 if (get_write == true) { 994 err = mnt_want_write(path->mnt); 995 if (err) 996 return err; 997 } 998 999 err = vfs_removexattr(idmap, path->dentry, attr_name); 1000 1001 if (get_write == true) 1002 mnt_drop_write(path->mnt); 1003 1004 return err; 1005 } 1006 1007 int ksmbd_vfs_unlink(struct file *filp) 1008 { 1009 int err = 0; 1010 struct dentry *dir, *dentry = filp->f_path.dentry; 1011 struct mnt_idmap *idmap = file_mnt_idmap(filp); 1012 1013 err = mnt_want_write(filp->f_path.mnt); 1014 if (err) 1015 return err; 1016 1017 dir = dget_parent(dentry); 1018 dentry = start_removing_dentry(dir, dentry); 1019 err = PTR_ERR(dentry); 1020 if (IS_ERR(dentry)) 1021 goto out; 1022 1023 if (S_ISDIR(d_inode(dentry)->i_mode)) 1024 err = vfs_rmdir(idmap, d_inode(dir), dentry, NULL); 1025 else 1026 err = vfs_unlink(idmap, d_inode(dir), dentry, NULL); 1027 1028 end_removing(dentry); 1029 if (err) 1030 ksmbd_debug(VFS, "failed to delete, err %d\n", err); 1031 out: 1032 dput(dir); 1033 mnt_drop_write(filp->f_path.mnt); 1034 1035 return err; 1036 } 1037 1038 static bool __dir_empty(struct dir_context *ctx, const char *name, int namlen, 1039 loff_t offset, u64 ino, unsigned int d_type) 1040 { 1041 struct ksmbd_readdir_data *buf; 1042 1043 buf = container_of(ctx, struct ksmbd_readdir_data, ctx); 1044 if (!is_dot_dotdot(name, namlen)) 1045 buf->dirent_count++; 1046 1047 return !buf->dirent_count; 1048 } 1049 1050 /** 1051 * ksmbd_vfs_empty_dir() - check for empty directory 1052 * @fp: ksmbd file pointer 1053 * 1054 * Return: true if directory empty, otherwise false 1055 */ 1056 int ksmbd_vfs_empty_dir(struct ksmbd_file *fp) 1057 { 1058 int err; 1059 struct ksmbd_readdir_data readdir_data; 1060 1061 memset(&readdir_data, 0, sizeof(struct ksmbd_readdir_data)); 1062 1063 set_ctx_actor(&readdir_data.ctx, __dir_empty); 1064 readdir_data.dirent_count = 0; 1065 1066 err = iterate_dir(fp->filp, &readdir_data.ctx); 1067 if (readdir_data.dirent_count) 1068 err = -ENOTEMPTY; 1069 else 1070 err = 0; 1071 return err; 1072 } 1073 1074 static bool __caseless_lookup(struct dir_context *ctx, const char *name, 1075 int namlen, loff_t offset, u64 ino, 1076 unsigned int d_type) 1077 { 1078 struct ksmbd_readdir_data *buf; 1079 int cmp = -EINVAL; 1080 1081 buf = container_of(ctx, struct ksmbd_readdir_data, ctx); 1082 1083 if (buf->used != namlen) 1084 return true; 1085 if (IS_ENABLED(CONFIG_UNICODE) && buf->um) { 1086 const struct qstr q_buf = {.name = buf->private, 1087 .len = buf->used}; 1088 const struct qstr q_name = {.name = name, 1089 .len = namlen}; 1090 1091 cmp = utf8_strncasecmp(buf->um, &q_buf, &q_name); 1092 } 1093 if (cmp < 0) 1094 cmp = strncasecmp((char *)buf->private, name, namlen); 1095 if (!cmp) { 1096 memcpy((char *)buf->private, name, buf->used); 1097 buf->dirent_count = 1; 1098 return false; 1099 } 1100 return true; 1101 } 1102 1103 /** 1104 * ksmbd_vfs_lookup_in_dir() - lookup a file in a directory 1105 * @dir: path info 1106 * @name: filename to lookup 1107 * @namelen: filename length 1108 * @um: &struct unicode_map to use 1109 * 1110 * Return: 0 on success, otherwise error 1111 */ 1112 static int ksmbd_vfs_lookup_in_dir(const struct path *dir, char *name, 1113 size_t namelen, struct unicode_map *um) 1114 { 1115 int ret; 1116 struct file *dfilp; 1117 int flags = O_RDONLY | O_LARGEFILE; 1118 struct ksmbd_readdir_data readdir_data = { 1119 .ctx.actor = __caseless_lookup, 1120 .private = name, 1121 .used = namelen, 1122 .dirent_count = 0, 1123 .um = um, 1124 }; 1125 1126 dfilp = dentry_open(dir, flags, current_cred()); 1127 if (IS_ERR(dfilp)) 1128 return PTR_ERR(dfilp); 1129 1130 ret = iterate_dir(dfilp, &readdir_data.ctx); 1131 if (readdir_data.dirent_count > 0) 1132 ret = 0; 1133 fput(dfilp); 1134 return ret; 1135 } 1136 1137 static 1138 int __ksmbd_vfs_kern_path(struct ksmbd_work *work, char *filepath, 1139 unsigned int flags, 1140 struct path *path, bool caseless, bool for_remove) 1141 { 1142 struct ksmbd_share_config *share_conf = work->tcon->share_conf; 1143 struct path parent_path; 1144 size_t path_len, remain_len; 1145 int err; 1146 1147 retry: 1148 err = ksmbd_vfs_path_lookup(share_conf, filepath, flags, path, for_remove); 1149 if (!err || !caseless) 1150 return err; 1151 1152 path_len = strlen(filepath); 1153 remain_len = path_len; 1154 1155 parent_path = share_conf->vfs_path; 1156 path_get(&parent_path); 1157 1158 while (d_can_lookup(parent_path.dentry)) { 1159 char *filename = filepath + path_len - remain_len; 1160 char *next = strchrnul(filename, '/'); 1161 size_t filename_len = next - filename; 1162 bool is_last = !next[0]; 1163 1164 if (filename_len == 0) 1165 break; 1166 1167 err = ksmbd_vfs_lookup_in_dir(&parent_path, filename, 1168 filename_len, 1169 work->conn->um); 1170 path_put(&parent_path); 1171 if (err) 1172 goto out; 1173 if (is_last) { 1174 caseless = false; 1175 goto retry; 1176 } 1177 next[0] = '\0'; 1178 1179 err = vfs_path_lookup(share_conf->vfs_path.dentry, 1180 share_conf->vfs_path.mnt, 1181 filepath, 1182 flags, 1183 &parent_path); 1184 next[0] = '/'; 1185 if (err) 1186 goto out; 1187 1188 remain_len -= filename_len + 1; 1189 } 1190 1191 err = -EINVAL; 1192 path_put(&parent_path); 1193 out: 1194 return err; 1195 } 1196 1197 /** 1198 * ksmbd_vfs_kern_path() - lookup a file and get path info 1199 * @work: work 1200 * @filepath: file path that is relative to share 1201 * @flags: lookup flags 1202 * @path: if lookup succeed, return path info 1203 * @caseless: caseless filename lookup 1204 * 1205 * Perform the lookup, possibly crossing over any mount point. 1206 * On return no locks will be held and write-access to filesystem 1207 * won't have been checked. 1208 * Return: 0 if file was found, otherwise error 1209 */ 1210 int ksmbd_vfs_kern_path(struct ksmbd_work *work, char *filepath, 1211 unsigned int flags, 1212 struct path *path, bool caseless) 1213 { 1214 return __ksmbd_vfs_kern_path(work, filepath, flags, path, 1215 caseless, false); 1216 } 1217 1218 /** 1219 * ksmbd_vfs_kern_path_start_removing() - lookup a file and get path info prior to removal 1220 * @work: work 1221 * @filepath: file path that is relative to share 1222 * @flags: lookup flags 1223 * @path: if lookup succeed, return path info 1224 * @caseless: caseless filename lookup 1225 * 1226 * Perform the lookup, but don't cross over any mount point. 1227 * On return the parent of path->dentry will be locked and write-access to 1228 * filesystem will have been gained. 1229 * Return: 0 on if file was found, otherwise error 1230 */ 1231 int ksmbd_vfs_kern_path_start_removing(struct ksmbd_work *work, char *filepath, 1232 unsigned int flags, 1233 struct path *path, bool caseless) 1234 { 1235 return __ksmbd_vfs_kern_path(work, filepath, flags, path, 1236 caseless, true); 1237 } 1238 1239 void ksmbd_vfs_kern_path_end_removing(const struct path *path) 1240 { 1241 end_removing(path->dentry); 1242 mnt_drop_write(path->mnt); 1243 mntput(path->mnt); 1244 } 1245 1246 struct dentry *ksmbd_vfs_kern_path_create(struct ksmbd_work *work, 1247 const char *name, 1248 unsigned int flags, 1249 struct path *path) 1250 { 1251 char *abs_name; 1252 struct dentry *dent; 1253 1254 abs_name = convert_to_unix_name(work->tcon->share_conf, name); 1255 if (!abs_name) 1256 return ERR_PTR(-ENOMEM); 1257 1258 dent = start_creating_path(AT_FDCWD, abs_name, path, flags); 1259 kfree(abs_name); 1260 return dent; 1261 } 1262 1263 int ksmbd_vfs_remove_acl_xattrs(struct mnt_idmap *idmap, 1264 const struct path *path) 1265 { 1266 char *name, *xattr_list = NULL; 1267 ssize_t xattr_list_len; 1268 int err = 0; 1269 1270 xattr_list_len = ksmbd_vfs_listxattr(path->dentry, &xattr_list); 1271 if (xattr_list_len < 0) { 1272 goto out; 1273 } else if (!xattr_list_len) { 1274 ksmbd_debug(SMB, "empty xattr in the file\n"); 1275 goto out; 1276 } 1277 1278 err = mnt_want_write(path->mnt); 1279 if (err) 1280 goto out; 1281 1282 for (name = xattr_list; name - xattr_list < xattr_list_len; 1283 name += strlen(name) + 1) { 1284 ksmbd_debug(SMB, "%s, len %zd\n", name, strlen(name)); 1285 1286 if (!strncmp(name, XATTR_NAME_POSIX_ACL_ACCESS, 1287 sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1) || 1288 !strncmp(name, XATTR_NAME_POSIX_ACL_DEFAULT, 1289 sizeof(XATTR_NAME_POSIX_ACL_DEFAULT) - 1)) { 1290 err = vfs_remove_acl(idmap, path->dentry, name); 1291 if (err) 1292 ksmbd_debug(SMB, 1293 "remove acl xattr failed : %s\n", name); 1294 } 1295 } 1296 mnt_drop_write(path->mnt); 1297 1298 out: 1299 kvfree(xattr_list); 1300 return err; 1301 } 1302 1303 int ksmbd_vfs_remove_sd_xattrs(struct mnt_idmap *idmap, const struct path *path) 1304 { 1305 char *name, *xattr_list = NULL; 1306 ssize_t xattr_list_len; 1307 int err = 0; 1308 1309 xattr_list_len = ksmbd_vfs_listxattr(path->dentry, &xattr_list); 1310 if (xattr_list_len < 0) { 1311 goto out; 1312 } else if (!xattr_list_len) { 1313 ksmbd_debug(SMB, "empty xattr in the file\n"); 1314 goto out; 1315 } 1316 1317 for (name = xattr_list; name - xattr_list < xattr_list_len; 1318 name += strlen(name) + 1) { 1319 ksmbd_debug(SMB, "%s, len %zd\n", name, strlen(name)); 1320 1321 if (!strncmp(name, XATTR_NAME_SD, XATTR_NAME_SD_LEN)) { 1322 err = ksmbd_vfs_remove_xattr(idmap, path, name, true); 1323 if (err) 1324 ksmbd_debug(SMB, "remove xattr failed : %s\n", name); 1325 } 1326 } 1327 out: 1328 kvfree(xattr_list); 1329 return err; 1330 } 1331 1332 static struct xattr_smb_acl *ksmbd_vfs_make_xattr_posix_acl(struct mnt_idmap *idmap, 1333 struct inode *inode, 1334 int acl_type) 1335 { 1336 struct xattr_smb_acl *smb_acl = NULL; 1337 struct posix_acl *posix_acls; 1338 struct posix_acl_entry *pa_entry; 1339 struct xattr_acl_entry *xa_entry; 1340 int i; 1341 1342 if (!IS_ENABLED(CONFIG_FS_POSIX_ACL)) 1343 return NULL; 1344 1345 posix_acls = get_inode_acl(inode, acl_type); 1346 if (IS_ERR_OR_NULL(posix_acls)) 1347 return NULL; 1348 1349 smb_acl = kzalloc(sizeof(struct xattr_smb_acl) + 1350 sizeof(struct xattr_acl_entry) * posix_acls->a_count, 1351 KSMBD_DEFAULT_GFP); 1352 if (!smb_acl) 1353 goto out; 1354 1355 smb_acl->count = posix_acls->a_count; 1356 pa_entry = posix_acls->a_entries; 1357 xa_entry = smb_acl->entries; 1358 for (i = 0; i < posix_acls->a_count; i++, pa_entry++, xa_entry++) { 1359 switch (pa_entry->e_tag) { 1360 case ACL_USER: 1361 xa_entry->type = SMB_ACL_USER; 1362 xa_entry->uid = posix_acl_uid_translate(idmap, pa_entry); 1363 break; 1364 case ACL_USER_OBJ: 1365 xa_entry->type = SMB_ACL_USER_OBJ; 1366 break; 1367 case ACL_GROUP: 1368 xa_entry->type = SMB_ACL_GROUP; 1369 xa_entry->gid = posix_acl_gid_translate(idmap, pa_entry); 1370 break; 1371 case ACL_GROUP_OBJ: 1372 xa_entry->type = SMB_ACL_GROUP_OBJ; 1373 break; 1374 case ACL_OTHER: 1375 xa_entry->type = SMB_ACL_OTHER; 1376 break; 1377 case ACL_MASK: 1378 xa_entry->type = SMB_ACL_MASK; 1379 break; 1380 default: 1381 pr_err("unknown type : 0x%x\n", pa_entry->e_tag); 1382 goto out; 1383 } 1384 1385 if (pa_entry->e_perm & ACL_READ) 1386 xa_entry->perm |= SMB_ACL_READ; 1387 if (pa_entry->e_perm & ACL_WRITE) 1388 xa_entry->perm |= SMB_ACL_WRITE; 1389 if (pa_entry->e_perm & ACL_EXECUTE) 1390 xa_entry->perm |= SMB_ACL_EXECUTE; 1391 } 1392 out: 1393 posix_acl_release(posix_acls); 1394 return smb_acl; 1395 } 1396 1397 int ksmbd_vfs_set_sd_xattr(struct ksmbd_conn *conn, 1398 struct mnt_idmap *idmap, 1399 const struct path *path, 1400 struct smb_ntsd *pntsd, int len, 1401 bool get_write) 1402 { 1403 int rc; 1404 struct ndr sd_ndr = {0}, acl_ndr = {0}; 1405 struct xattr_ntacl acl = {0}; 1406 struct xattr_smb_acl *smb_acl, *def_smb_acl = NULL; 1407 struct dentry *dentry = path->dentry; 1408 struct inode *inode = d_inode(dentry); 1409 1410 acl.version = 4; 1411 acl.hash_type = XATTR_SD_HASH_TYPE_SHA256; 1412 acl.current_time = ksmbd_UnixTimeToNT(current_time(inode)); 1413 1414 memcpy(acl.desc, "posix_acl", 9); 1415 acl.desc_len = 10; 1416 1417 pntsd->osidoffset = 1418 cpu_to_le32(le32_to_cpu(pntsd->osidoffset) + NDR_NTSD_OFFSETOF); 1419 pntsd->gsidoffset = 1420 cpu_to_le32(le32_to_cpu(pntsd->gsidoffset) + NDR_NTSD_OFFSETOF); 1421 pntsd->dacloffset = 1422 cpu_to_le32(le32_to_cpu(pntsd->dacloffset) + NDR_NTSD_OFFSETOF); 1423 1424 acl.sd_buf = (char *)pntsd; 1425 acl.sd_size = len; 1426 1427 sha256(acl.sd_buf, acl.sd_size, acl.hash); 1428 1429 smb_acl = ksmbd_vfs_make_xattr_posix_acl(idmap, inode, 1430 ACL_TYPE_ACCESS); 1431 if (S_ISDIR(inode->i_mode)) 1432 def_smb_acl = ksmbd_vfs_make_xattr_posix_acl(idmap, inode, 1433 ACL_TYPE_DEFAULT); 1434 1435 rc = ndr_encode_posix_acl(&acl_ndr, idmap, inode, 1436 smb_acl, def_smb_acl); 1437 if (rc) { 1438 pr_err("failed to encode ndr to posix acl\n"); 1439 goto out; 1440 } 1441 1442 sha256(acl_ndr.data, acl_ndr.offset, acl.posix_acl_hash); 1443 1444 rc = ndr_encode_v4_ntacl(&sd_ndr, &acl); 1445 if (rc) { 1446 pr_err("failed to encode ndr to posix acl\n"); 1447 goto out; 1448 } 1449 1450 rc = ksmbd_vfs_setxattr(idmap, path, 1451 XATTR_NAME_SD, sd_ndr.data, 1452 sd_ndr.offset, 0, get_write); 1453 if (rc < 0) 1454 pr_err("Failed to store XATTR ntacl :%d\n", rc); 1455 1456 kfree(sd_ndr.data); 1457 out: 1458 kfree(acl_ndr.data); 1459 kfree(smb_acl); 1460 kfree(def_smb_acl); 1461 return rc; 1462 } 1463 1464 int ksmbd_vfs_get_sd_xattr(struct ksmbd_conn *conn, 1465 struct mnt_idmap *idmap, 1466 struct dentry *dentry, 1467 struct smb_ntsd **pntsd) 1468 { 1469 int rc; 1470 struct ndr n; 1471 struct inode *inode = d_inode(dentry); 1472 struct ndr acl_ndr = {0}; 1473 struct xattr_ntacl acl; 1474 struct xattr_smb_acl *smb_acl = NULL, *def_smb_acl = NULL; 1475 __u8 cmp_hash[XATTR_SD_HASH_SIZE] = {0}; 1476 1477 rc = ksmbd_vfs_getxattr(idmap, dentry, XATTR_NAME_SD, &n.data); 1478 if (rc <= 0) 1479 return rc; 1480 1481 n.length = rc; 1482 rc = ndr_decode_v4_ntacl(&n, &acl); 1483 if (rc) 1484 goto free_n_data; 1485 1486 smb_acl = ksmbd_vfs_make_xattr_posix_acl(idmap, inode, 1487 ACL_TYPE_ACCESS); 1488 if (S_ISDIR(inode->i_mode)) 1489 def_smb_acl = ksmbd_vfs_make_xattr_posix_acl(idmap, inode, 1490 ACL_TYPE_DEFAULT); 1491 1492 rc = ndr_encode_posix_acl(&acl_ndr, idmap, inode, smb_acl, 1493 def_smb_acl); 1494 if (rc) { 1495 pr_err("failed to encode ndr to posix acl\n"); 1496 goto out_free; 1497 } 1498 1499 sha256(acl_ndr.data, acl_ndr.offset, cmp_hash); 1500 1501 if (memcmp(cmp_hash, acl.posix_acl_hash, XATTR_SD_HASH_SIZE)) { 1502 pr_err("hash value diff\n"); 1503 rc = -EINVAL; 1504 goto out_free; 1505 } 1506 1507 *pntsd = acl.sd_buf; 1508 if (acl.sd_size < sizeof(struct smb_ntsd)) { 1509 pr_err("sd size is invalid\n"); 1510 goto out_free; 1511 } 1512 1513 (*pntsd)->osidoffset = cpu_to_le32(le32_to_cpu((*pntsd)->osidoffset) - 1514 NDR_NTSD_OFFSETOF); 1515 (*pntsd)->gsidoffset = cpu_to_le32(le32_to_cpu((*pntsd)->gsidoffset) - 1516 NDR_NTSD_OFFSETOF); 1517 (*pntsd)->dacloffset = cpu_to_le32(le32_to_cpu((*pntsd)->dacloffset) - 1518 NDR_NTSD_OFFSETOF); 1519 1520 rc = acl.sd_size; 1521 out_free: 1522 kfree(acl_ndr.data); 1523 kfree(smb_acl); 1524 kfree(def_smb_acl); 1525 if (rc < 0) { 1526 kfree(acl.sd_buf); 1527 *pntsd = NULL; 1528 } 1529 1530 free_n_data: 1531 kfree(n.data); 1532 return rc; 1533 } 1534 1535 int ksmbd_vfs_set_dos_attrib_xattr(struct mnt_idmap *idmap, 1536 const struct path *path, 1537 struct xattr_dos_attrib *da, 1538 bool get_write) 1539 { 1540 struct ndr n; 1541 int err; 1542 1543 err = ndr_encode_dos_attr(&n, da); 1544 if (err) 1545 return err; 1546 1547 err = ksmbd_vfs_setxattr(idmap, path, XATTR_NAME_DOS_ATTRIBUTE, 1548 (void *)n.data, n.offset, 0, get_write); 1549 if (err) 1550 ksmbd_debug(SMB, "failed to store dos attribute in xattr\n"); 1551 kfree(n.data); 1552 1553 return err; 1554 } 1555 1556 int ksmbd_vfs_get_dos_attrib_xattr(struct mnt_idmap *idmap, 1557 struct dentry *dentry, 1558 struct xattr_dos_attrib *da) 1559 { 1560 struct ndr n; 1561 int err; 1562 1563 err = ksmbd_vfs_getxattr(idmap, dentry, XATTR_NAME_DOS_ATTRIBUTE, 1564 (char **)&n.data); 1565 if (err > 0) { 1566 n.length = err; 1567 if (ndr_decode_dos_attr(&n, da)) 1568 err = -EINVAL; 1569 kfree(n.data); 1570 } else { 1571 ksmbd_debug(SMB, "failed to load dos attribute in xattr\n"); 1572 } 1573 1574 return err; 1575 } 1576 1577 /** 1578 * ksmbd_vfs_init_kstat() - convert unix stat information to smb stat format 1579 * @p: destination buffer 1580 * @ksmbd_kstat: ksmbd kstat wrapper 1581 * 1582 * Returns: pointer to the converted &struct file_directory_info 1583 */ 1584 void *ksmbd_vfs_init_kstat(char **p, struct ksmbd_kstat *ksmbd_kstat) 1585 { 1586 FILE_DIRECTORY_INFO *info = (FILE_DIRECTORY_INFO *)(*p); 1587 struct kstat *kstat = ksmbd_kstat->kstat; 1588 u64 time; 1589 1590 info->FileIndex = 0; 1591 info->CreationTime = cpu_to_le64(ksmbd_kstat->create_time); 1592 time = ksmbd_UnixTimeToNT(kstat->atime); 1593 info->LastAccessTime = cpu_to_le64(time); 1594 time = ksmbd_UnixTimeToNT(kstat->mtime); 1595 info->LastWriteTime = cpu_to_le64(time); 1596 time = ksmbd_UnixTimeToNT(kstat->ctime); 1597 info->ChangeTime = cpu_to_le64(time); 1598 1599 if (ksmbd_kstat->file_attributes & FILE_ATTRIBUTE_DIRECTORY_LE) { 1600 info->EndOfFile = 0; 1601 info->AllocationSize = 0; 1602 } else { 1603 info->EndOfFile = cpu_to_le64(kstat->size); 1604 info->AllocationSize = cpu_to_le64(kstat->blocks << 9); 1605 } 1606 info->ExtFileAttributes = ksmbd_kstat->file_attributes; 1607 1608 return info; 1609 } 1610 1611 int ksmbd_vfs_fill_dentry_attrs(struct ksmbd_work *work, 1612 struct mnt_idmap *idmap, 1613 struct dentry *dentry, 1614 struct ksmbd_kstat *ksmbd_kstat) 1615 { 1616 struct ksmbd_share_config *share_conf = work->tcon->share_conf; 1617 u64 time; 1618 int rc; 1619 struct path path = { 1620 .mnt = share_conf->vfs_path.mnt, 1621 .dentry = dentry, 1622 }; 1623 1624 rc = vfs_getattr(&path, ksmbd_kstat->kstat, 1625 STATX_BASIC_STATS | STATX_BTIME, 1626 AT_STATX_SYNC_AS_STAT); 1627 if (rc) 1628 return rc; 1629 1630 time = ksmbd_UnixTimeToNT(ksmbd_kstat->kstat->ctime); 1631 ksmbd_kstat->create_time = time; 1632 1633 /* 1634 * set default value for the case that store dos attributes is not yes 1635 * or that acl is disable in server's filesystem and the config is yes. 1636 */ 1637 if (S_ISDIR(ksmbd_kstat->kstat->mode)) 1638 ksmbd_kstat->file_attributes = FILE_ATTRIBUTE_DIRECTORY_LE; 1639 else 1640 ksmbd_kstat->file_attributes = FILE_ATTRIBUTE_ARCHIVE_LE; 1641 1642 if (test_share_config_flag(work->tcon->share_conf, 1643 KSMBD_SHARE_FLAG_STORE_DOS_ATTRS)) { 1644 struct xattr_dos_attrib da; 1645 1646 rc = ksmbd_vfs_get_dos_attrib_xattr(idmap, dentry, &da); 1647 if (rc > 0) { 1648 ksmbd_kstat->file_attributes = cpu_to_le32(da.attr); 1649 ksmbd_kstat->create_time = da.create_time; 1650 } else { 1651 ksmbd_debug(VFS, "fail to load dos attribute.\n"); 1652 } 1653 } 1654 1655 return 0; 1656 } 1657 1658 ssize_t ksmbd_vfs_casexattr_len(struct mnt_idmap *idmap, 1659 struct dentry *dentry, char *attr_name, 1660 int attr_name_len) 1661 { 1662 char *name, *xattr_list = NULL; 1663 ssize_t value_len = -ENOENT, xattr_list_len; 1664 1665 xattr_list_len = ksmbd_vfs_listxattr(dentry, &xattr_list); 1666 if (xattr_list_len <= 0) 1667 goto out; 1668 1669 for (name = xattr_list; name - xattr_list < xattr_list_len; 1670 name += strlen(name) + 1) { 1671 ksmbd_debug(VFS, "%s, len %zd\n", name, strlen(name)); 1672 if (strncasecmp(attr_name, name, attr_name_len)) 1673 continue; 1674 1675 value_len = ksmbd_vfs_xattr_len(idmap, dentry, name); 1676 break; 1677 } 1678 1679 out: 1680 kvfree(xattr_list); 1681 return value_len; 1682 } 1683 1684 int ksmbd_vfs_xattr_stream_name(char *stream_name, char **xattr_stream_name, 1685 size_t *xattr_stream_name_size, int s_type) 1686 { 1687 char *type, *buf; 1688 1689 if (s_type == DIR_STREAM) 1690 type = ":$INDEX_ALLOCATION"; 1691 else 1692 type = ":$DATA"; 1693 1694 buf = kasprintf(KSMBD_DEFAULT_GFP, "%s%s%s", 1695 XATTR_NAME_STREAM, stream_name, type); 1696 if (!buf) 1697 return -ENOMEM; 1698 1699 *xattr_stream_name = buf; 1700 *xattr_stream_name_size = strlen(buf) + 1; 1701 1702 return 0; 1703 } 1704 1705 int ksmbd_vfs_copy_file_ranges(struct ksmbd_work *work, 1706 struct ksmbd_file *src_fp, 1707 struct ksmbd_file *dst_fp, 1708 struct srv_copychunk *chunks, 1709 unsigned int chunk_count, 1710 unsigned int *chunk_count_written, 1711 unsigned int *chunk_size_written, 1712 loff_t *total_size_written) 1713 { 1714 unsigned int i; 1715 loff_t src_off, dst_off, src_file_size; 1716 size_t len; 1717 int ret; 1718 1719 *chunk_count_written = 0; 1720 *chunk_size_written = 0; 1721 *total_size_written = 0; 1722 1723 if (!(src_fp->daccess & (FILE_READ_DATA_LE | FILE_EXECUTE_LE))) { 1724 pr_err("no right to read(%pD)\n", src_fp->filp); 1725 return -EACCES; 1726 } 1727 if (!(dst_fp->daccess & (FILE_WRITE_DATA_LE | FILE_APPEND_DATA_LE))) { 1728 pr_err("no right to write(%pD)\n", dst_fp->filp); 1729 return -EACCES; 1730 } 1731 1732 if (ksmbd_stream_fd(src_fp) || ksmbd_stream_fd(dst_fp)) 1733 return -EBADF; 1734 1735 smb_break_all_levII_oplock(work, dst_fp, 1); 1736 1737 if (!work->tcon->posix_extensions) { 1738 for (i = 0; i < chunk_count; i++) { 1739 src_off = le64_to_cpu(chunks[i].SourceOffset); 1740 dst_off = le64_to_cpu(chunks[i].TargetOffset); 1741 len = le32_to_cpu(chunks[i].Length); 1742 1743 if (check_lock_range(src_fp->filp, src_off, 1744 src_off + len - 1, READ)) 1745 return -EAGAIN; 1746 if (check_lock_range(dst_fp->filp, dst_off, 1747 dst_off + len - 1, WRITE)) 1748 return -EAGAIN; 1749 } 1750 } 1751 1752 src_file_size = i_size_read(file_inode(src_fp->filp)); 1753 1754 for (i = 0; i < chunk_count; i++) { 1755 src_off = le64_to_cpu(chunks[i].SourceOffset); 1756 dst_off = le64_to_cpu(chunks[i].TargetOffset); 1757 len = le32_to_cpu(chunks[i].Length); 1758 1759 if (src_off + len > src_file_size) 1760 return -E2BIG; 1761 1762 /* 1763 * vfs_copy_file_range does not allow overlapped copying 1764 * within the same file. 1765 */ 1766 if (file_inode(src_fp->filp) == file_inode(dst_fp->filp) && 1767 dst_off + len > src_off && 1768 dst_off < src_off + len) 1769 ret = do_splice_direct(src_fp->filp, &src_off, 1770 dst_fp->filp, &dst_off, 1771 min_t(size_t, len, MAX_RW_COUNT), 0); 1772 else 1773 ret = vfs_copy_file_range(src_fp->filp, src_off, 1774 dst_fp->filp, dst_off, len, 0); 1775 if (ret == -EOPNOTSUPP || ret == -EXDEV) 1776 ret = vfs_copy_file_range(src_fp->filp, src_off, 1777 dst_fp->filp, dst_off, len, 1778 COPY_FILE_SPLICE); 1779 if (ret < 0) 1780 return ret; 1781 1782 *chunk_count_written += 1; 1783 *total_size_written += ret; 1784 } 1785 return 0; 1786 } 1787 1788 void ksmbd_vfs_posix_lock_wait(struct file_lock *flock) 1789 { 1790 wait_event(flock->c.flc_wait, !flock->c.flc_blocker); 1791 } 1792 1793 void ksmbd_vfs_posix_lock_unblock(struct file_lock *flock) 1794 { 1795 locks_delete_block(flock); 1796 } 1797 1798 int ksmbd_vfs_set_init_posix_acl(struct mnt_idmap *idmap, 1799 const struct path *path) 1800 { 1801 struct posix_acl_state acl_state; 1802 struct posix_acl *acls; 1803 struct dentry *dentry = path->dentry; 1804 struct inode *inode = d_inode(dentry); 1805 int rc; 1806 1807 if (!IS_ENABLED(CONFIG_FS_POSIX_ACL)) 1808 return -EOPNOTSUPP; 1809 1810 ksmbd_debug(SMB, "Set posix acls\n"); 1811 rc = init_acl_state(&acl_state, 1); 1812 if (rc) 1813 return rc; 1814 1815 /* Set default owner group */ 1816 acl_state.owner.allow = (inode->i_mode & 0700) >> 6; 1817 acl_state.group.allow = (inode->i_mode & 0070) >> 3; 1818 acl_state.other.allow = inode->i_mode & 0007; 1819 acl_state.users->aces[acl_state.users->n].uid = inode->i_uid; 1820 acl_state.users->aces[acl_state.users->n++].perms.allow = 1821 acl_state.owner.allow; 1822 acl_state.groups->aces[acl_state.groups->n].gid = inode->i_gid; 1823 acl_state.groups->aces[acl_state.groups->n++].perms.allow = 1824 acl_state.group.allow; 1825 acl_state.mask.allow = 0x07; 1826 1827 acls = posix_acl_alloc(6, KSMBD_DEFAULT_GFP); 1828 if (!acls) { 1829 free_acl_state(&acl_state); 1830 return -ENOMEM; 1831 } 1832 posix_state_to_acl(&acl_state, acls->a_entries); 1833 1834 rc = set_posix_acl(idmap, dentry, ACL_TYPE_ACCESS, acls); 1835 if (rc < 0) 1836 ksmbd_debug(SMB, "Set posix acl(ACL_TYPE_ACCESS) failed, rc : %d\n", 1837 rc); 1838 else if (S_ISDIR(inode->i_mode)) { 1839 posix_state_to_acl(&acl_state, acls->a_entries); 1840 rc = set_posix_acl(idmap, dentry, ACL_TYPE_DEFAULT, acls); 1841 if (rc < 0) 1842 ksmbd_debug(SMB, "Set posix acl(ACL_TYPE_DEFAULT) failed, rc : %d\n", 1843 rc); 1844 } 1845 1846 free_acl_state(&acl_state); 1847 posix_acl_release(acls); 1848 return rc; 1849 } 1850 1851 int ksmbd_vfs_inherit_posix_acl(struct mnt_idmap *idmap, 1852 const struct path *path, struct inode *parent_inode) 1853 { 1854 struct posix_acl *acls; 1855 struct posix_acl_entry *pace; 1856 struct dentry *dentry = path->dentry; 1857 struct inode *inode = d_inode(dentry); 1858 int rc, i; 1859 1860 if (!IS_ENABLED(CONFIG_FS_POSIX_ACL)) 1861 return -EOPNOTSUPP; 1862 1863 acls = get_inode_acl(parent_inode, ACL_TYPE_DEFAULT); 1864 if (IS_ERR_OR_NULL(acls)) 1865 return -ENOENT; 1866 pace = acls->a_entries; 1867 1868 for (i = 0; i < acls->a_count; i++, pace++) { 1869 if (pace->e_tag == ACL_MASK) { 1870 pace->e_perm = 0x07; 1871 break; 1872 } 1873 } 1874 1875 rc = set_posix_acl(idmap, dentry, ACL_TYPE_ACCESS, acls); 1876 if (rc < 0) 1877 ksmbd_debug(SMB, "Set posix acl(ACL_TYPE_ACCESS) failed, rc : %d\n", 1878 rc); 1879 if (S_ISDIR(inode->i_mode)) { 1880 rc = set_posix_acl(idmap, dentry, ACL_TYPE_DEFAULT, 1881 acls); 1882 if (rc < 0) 1883 ksmbd_debug(SMB, "Set posix acl(ACL_TYPE_DEFAULT) failed, rc : %d\n", 1884 rc); 1885 } 1886 1887 posix_acl_release(acls); 1888 return rc; 1889 } 1890