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