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