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