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