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