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