-
Notifications
You must be signed in to change notification settings - Fork 64
/
vfs.c
3470 lines (3070 loc) · 82.7 KB
/
vfs.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (C) 2016 Namjae Jeon <[email protected]>
* Copyright (C) 2018 Samsung Electronics Co., Ltd.
*/
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/version.h>
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 3, 0)
#include <linux/filelock.h>
#endif
#include <linux/uaccess.h>
#include <linux/backing-dev.h>
#include <linux/writeback.h>
#include <linux/xattr.h>
#include <linux/falloc.h>
#if LINUX_VERSION_CODE < KERNEL_VERSION(5, 18, 0)
#include <linux/genhd.h>
#endif
#include <linux/fsnotify.h>
#include <linux/dcache.h>
#include <linux/slab.h>
#include <linux/vmalloc.h>
#include <linux/crc32c.h>
#include <linux/sched/xacct.h>
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 4, 0)
#include <linux/namei.h>
#endif
#include <linux/mount.h>
#include "glob.h"
#include "oplock.h"
#include "connection.h"
#include "vfs.h"
#include "vfs_cache.h"
#include "smbacl.h"
#include "ndr.h"
#include "auth.h"
#include "misc.h"
#include "smb_common.h"
#include "mgmt/share_config.h"
#include "mgmt/tree_connect.h"
#include "mgmt/user_session.h"
#include "mgmt/user_config.h"
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 6, 0)
extern int vfs_path_lookup(struct dentry *, struct vfsmount *,
const char *, unsigned int, struct path *);
#endif
#if LINUX_VERSION_CODE < KERNEL_VERSION(6, 4, 0)
static char *extract_last_component(char *path)
{
char *p = strrchr(path, '/');
if (p && p[1] != '\0') {
*p = '\0';
p++;
} else {
p = NULL;
}
return p;
}
#endif
static void ksmbd_vfs_inherit_owner(struct ksmbd_work *work,
struct inode *parent_inode,
struct inode *inode)
{
if (!test_share_config_flag(work->tcon->share_conf,
KSMBD_SHARE_FLAG_INHERIT_OWNER))
return;
i_uid_write(inode, i_uid_read(parent_inode));
}
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 4, 0)
/**
* ksmbd_vfs_lock_parent() - lock parent dentry if it is stable
* @parent: parent dentry
* @child: child dentry
*
* Returns: %0 on success, %-ENOENT if the parent dentry is not stable
*/
int ksmbd_vfs_lock_parent(struct dentry *parent, struct dentry *child)
{
inode_lock_nested(d_inode(parent), I_MUTEX_PARENT);
if (child->d_parent != parent) {
inode_unlock(d_inode(parent));
return -ENOENT;
}
return 0;
}
static int ksmbd_vfs_path_lookup_locked(struct ksmbd_share_config *share_conf,
char *pathname, unsigned int flags,
struct path *parent_path,
struct path *path)
{
struct qstr last;
struct filename *filename;
struct path *root_share_path = &share_conf->vfs_path;
int err, type;
struct dentry *d;
if (pathname[0] == '\0') {
pathname = share_conf->path;
root_share_path = NULL;
} else {
flags |= LOOKUP_BENEATH;
}
filename = getname_kernel(pathname);
if (IS_ERR(filename))
return PTR_ERR(filename);
err = vfs_path_parent_lookup(filename, flags,
parent_path, &last, &type,
root_share_path);
if (err) {
putname(filename);
return err;
}
if (unlikely(type != LAST_NORM)) {
path_put(parent_path);
putname(filename);
return -ENOENT;
}
err = mnt_want_write(parent_path->mnt);
if (err) {
path_put(parent_path);
putname(filename);
return -ENOENT;
}
inode_lock_nested(parent_path->dentry->d_inode, I_MUTEX_PARENT);
d = lookup_one_qstr_excl(&last, parent_path->dentry, 0);
if (IS_ERR(d))
goto err_out;
if (d_is_negative(d)) {
dput(d);
goto err_out;
}
path->dentry = d;
path->mnt = mntget(parent_path->mnt);
if (test_share_config_flag(share_conf, KSMBD_SHARE_FLAG_CROSSMNT)) {
err = follow_down(path, 0);
if (err < 0) {
path_put(path);
goto err_out;
}
}
putname(filename);
return 0;
err_out:
inode_unlock(d_inode(parent_path->dentry));
mnt_drop_write(parent_path->mnt);
path_put(parent_path);
putname(filename);
return -ENOENT;
}
#else
/**
* ksmbd_vfs_lock_parent() - lock parent dentry if it is stable
*
* the parent dentry got by dget_parent or @parent could be
* unstable, we try to lock a parent inode and lookup the
* child dentry again.
*
* the reference count of @parent isn't incremented.
*/
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 3, 0)
int ksmbd_vfs_lock_parent(struct mnt_idmap *idmap, struct dentry *parent,
#else
int ksmbd_vfs_lock_parent(struct user_namespace *user_ns, struct dentry *parent,
#endif
struct dentry *child)
{
struct dentry *dentry;
int ret = 0;
inode_lock_nested(d_inode(parent), I_MUTEX_PARENT);
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 15, 0)
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 3, 0)
dentry = lookup_one(idmap, child->d_name.name, parent,
#else
dentry = lookup_one(user_ns, child->d_name.name, parent,
#endif
child->d_name.len);
#else
dentry = lookup_one_len(child->d_name.name, parent,
child->d_name.len);
#endif
if (IS_ERR(dentry)) {
ret = PTR_ERR(dentry);
goto out_err;
}
if (dentry != child) {
ret = -ESTALE;
dput(dentry);
goto out_err;
}
dput(dentry);
return 0;
out_err:
inode_unlock(d_inode(parent));
return ret;
}
#endif
#if LINUX_VERSION_CODE < KERNEL_VERSION(6, 4, 0)
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 3, 0)
int ksmbd_vfs_may_delete(struct mnt_idmap *idmap,
#else
int ksmbd_vfs_may_delete(struct user_namespace *user_ns,
#endif
struct dentry *dentry)
{
struct dentry *parent;
int ret;
parent = dget_parent(dentry);
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 3, 0)
ret = ksmbd_vfs_lock_parent(idmap, parent, dentry);
#else
ret = ksmbd_vfs_lock_parent(user_ns, parent, dentry);
#endif
if (ret) {
dput(parent);
return ret;
}
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 12, 0)
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 3, 0)
ret = inode_permission(idmap, d_inode(parent),
#else
ret = inode_permission(user_ns, d_inode(parent),
#endif
MAY_EXEC | MAY_WRITE);
#else
ret = inode_permission(d_inode(parent), MAY_EXEC | MAY_WRITE);
#endif
inode_unlock(d_inode(parent));
dput(parent);
return ret;
}
#endif
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 4, 0)
void ksmbd_vfs_query_maximal_access(struct mnt_idmap *idmap,
struct dentry *dentry, __le32 *daccess)
{
*daccess = cpu_to_le32(FILE_READ_ATTRIBUTES | READ_CONTROL);
if (!inode_permission(idmap, d_inode(dentry), MAY_OPEN | MAY_WRITE))
*daccess |= cpu_to_le32(WRITE_DAC | WRITE_OWNER | SYNCHRONIZE |
FILE_WRITE_DATA | FILE_APPEND_DATA |
FILE_WRITE_EA | FILE_WRITE_ATTRIBUTES |
FILE_DELETE_CHILD);
if (!inode_permission(idmap, d_inode(dentry), MAY_OPEN | MAY_READ))
*daccess |= FILE_READ_DATA_LE | FILE_READ_EA_LE;
if (!inode_permission(idmap, d_inode(dentry), MAY_OPEN | MAY_EXEC))
*daccess |= FILE_EXECUTE_LE;
if (!inode_permission(idmap, d_inode(dentry->d_parent), MAY_EXEC | MAY_WRITE))
*daccess |= FILE_DELETE_LE;
}
#else
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 3, 0)
int ksmbd_vfs_query_maximal_access(struct mnt_idmap *idmap,
#else
int ksmbd_vfs_query_maximal_access(struct user_namespace *user_ns,
#endif
struct dentry *dentry, __le32 *daccess)
{
struct dentry *parent;
int ret = 0;
*daccess = cpu_to_le32(FILE_READ_ATTRIBUTES | READ_CONTROL);
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 12, 0)
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 3, 0)
if (!inode_permission(idmap, d_inode(dentry), MAY_OPEN | MAY_WRITE))
#else
if (!inode_permission(user_ns, d_inode(dentry), MAY_OPEN | MAY_WRITE))
#endif
#else
if (!inode_permission(d_inode(dentry), MAY_OPEN | MAY_WRITE))
#endif
*daccess |= cpu_to_le32(WRITE_DAC | WRITE_OWNER | SYNCHRONIZE |
FILE_WRITE_DATA | FILE_APPEND_DATA |
FILE_WRITE_EA | FILE_WRITE_ATTRIBUTES |
FILE_DELETE_CHILD);
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 12, 0)
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 3, 0)
if (!inode_permission(idmap, d_inode(dentry), MAY_OPEN | MAY_READ))
#else
if (!inode_permission(user_ns, d_inode(dentry), MAY_OPEN | MAY_READ))
#endif
#else
if (!inode_permission(d_inode(dentry), MAY_OPEN | MAY_READ))
#endif
*daccess |= FILE_READ_DATA_LE | FILE_READ_EA_LE;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 12, 0)
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 3, 0)
if (!inode_permission(idmap, d_inode(dentry), MAY_OPEN | MAY_EXEC))
#else
if (!inode_permission(user_ns, d_inode(dentry), MAY_OPEN | MAY_EXEC))
#endif
#else
if (!inode_permission(d_inode(dentry), MAY_OPEN | MAY_EXEC))
#endif
*daccess |= FILE_EXECUTE_LE;
parent = dget_parent(dentry);
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 3, 0)
ret = ksmbd_vfs_lock_parent(idmap, parent, dentry);
#else
ret = ksmbd_vfs_lock_parent(user_ns, parent, dentry);
#endif
if (ret) {
dput(parent);
return ret;
}
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 12, 0)
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 3, 0)
if (!inode_permission(idmap, d_inode(parent), MAY_EXEC | MAY_WRITE))
#else
if (!inode_permission(user_ns, d_inode(parent), MAY_EXEC | MAY_WRITE))
#endif
#else
if (!inode_permission(d_inode(parent), MAY_EXEC | MAY_WRITE))
#endif
*daccess |= FILE_DELETE_LE;
inode_unlock(d_inode(parent));
dput(parent);
return ret;
}
#endif
/**
* ksmbd_vfs_create() - vfs helper for smb create file
* @work: work
* @name: file name that is relative to share
* @mode: file create mode
*
* Return: 0 on success, otherwise error
*/
int ksmbd_vfs_create(struct ksmbd_work *work, const char *name, umode_t mode)
{
struct path path;
struct dentry *dentry;
int err;
dentry = ksmbd_vfs_kern_path_create(work, name,
LOOKUP_NO_SYMLINKS, &path);
if (IS_ERR(dentry)) {
err = PTR_ERR(dentry);
if (err != -ENOENT)
pr_err("path create failed for %s, err %d\n",
name, err);
return err;
}
mode |= S_IFREG;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 12, 0)
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 3, 0)
err = vfs_create(mnt_idmap(path.mnt), d_inode(path.dentry),
dentry, mode, true);
#else
err = vfs_create(mnt_user_ns(path.mnt), d_inode(path.dentry),
dentry, mode, true);
#endif
#else
err = vfs_create(d_inode(path.dentry), dentry, mode, true);
#endif
if (!err) {
ksmbd_vfs_inherit_owner(work, d_inode(path.dentry),
d_inode(dentry));
} else {
pr_err("File(%s): creation failed (err:%d)\n", name, err);
}
done_path_create(&path, dentry);
return err;
}
/**
* ksmbd_vfs_mkdir() - vfs helper for smb create directory
* @work: work
* @name: directory name that is relative to share
* @mode: directory create mode
*
* Return: 0 on success, otherwise error
*/
int ksmbd_vfs_mkdir(struct ksmbd_work *work, const char *name, umode_t mode)
{
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 3, 0)
struct mnt_idmap *idmap;
#else
struct user_namespace *user_ns;
#endif
struct path path;
struct dentry *dentry;
int err;
dentry = ksmbd_vfs_kern_path_create(work, name,
LOOKUP_NO_SYMLINKS | LOOKUP_DIRECTORY,
&path);
if (IS_ERR(dentry)) {
err = PTR_ERR(dentry);
if (err != -EEXIST)
ksmbd_debug(VFS, "path create failed for %s, err %d\n",
name, err);
return err;
}
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 3, 0)
idmap = mnt_idmap(path.mnt);
#else
user_ns = mnt_user_ns(path.mnt);
#endif
mode |= S_IFDIR;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 12, 0)
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 3, 0)
err = vfs_mkdir(idmap, d_inode(path.dentry), dentry, mode);
#else
err = vfs_mkdir(user_ns, d_inode(path.dentry), dentry, mode);
#endif
#else
err = vfs_mkdir(d_inode(path.dentry), dentry, mode);
#endif
if (!err && d_unhashed(dentry)) {
struct dentry *d;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 15, 0)
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 3, 0)
d = lookup_one(idmap, dentry->d_name.name, dentry->d_parent,
#else
d = lookup_one(user_ns, dentry->d_name.name, dentry->d_parent,
#endif
dentry->d_name.len);
#else
d = lookup_one_len(dentry->d_name.name, dentry->d_parent,
dentry->d_name.len);
#endif
if (IS_ERR(d)) {
err = PTR_ERR(d);
goto out_err;
}
if (unlikely(d_is_negative(d))) {
dput(d);
err = -ENOENT;
goto out_err;
}
ksmbd_vfs_inherit_owner(work, d_inode(path.dentry), d_inode(d));
dput(d);
}
out_err:
done_path_create(&path, dentry);
if (err)
pr_err("mkdir(%s): creation failed (err:%d)\n", name, err);
return err;
}
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 3, 0)
static ssize_t ksmbd_vfs_getcasexattr(struct mnt_idmap *idmap,
#else
static ssize_t ksmbd_vfs_getcasexattr(struct user_namespace *user_ns,
#endif
struct dentry *dentry, char *attr_name,
int attr_name_len, char **attr_value)
{
char *name, *xattr_list = NULL;
ssize_t value_len = -ENOENT, xattr_list_len;
xattr_list_len = ksmbd_vfs_listxattr(dentry, &xattr_list);
if (xattr_list_len <= 0)
goto out;
for (name = xattr_list; name - xattr_list < xattr_list_len;
name += strlen(name) + 1) {
ksmbd_debug(VFS, "%s, len %zd\n", name, strlen(name));
if (strncasecmp(attr_name, name, attr_name_len))
continue;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 3, 0)
value_len = ksmbd_vfs_getxattr(idmap,
#else
value_len = ksmbd_vfs_getxattr(user_ns,
#endif
dentry,
name,
attr_value);
if (value_len < 0)
pr_err("failed to get xattr in file\n");
break;
}
out:
kvfree(xattr_list);
return value_len;
}
static int ksmbd_vfs_stream_read(struct ksmbd_file *fp, char *buf, loff_t *pos,
size_t count)
{
ssize_t v_len;
char *stream_buf = NULL;
ksmbd_debug(VFS, "read stream data pos : %llu, count : %zd\n",
*pos, count);
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 3, 0)
v_len = ksmbd_vfs_getcasexattr(file_mnt_idmap(fp->filp),
#else
v_len = ksmbd_vfs_getcasexattr(file_mnt_user_ns(fp->filp),
#endif
fp->filp->f_path.dentry,
fp->stream.name,
fp->stream.size,
&stream_buf);
if ((int)v_len <= 0)
return (int)v_len;
if (v_len <= *pos) {
count = -EINVAL;
goto free_buf;
}
if (v_len - *pos < count)
count = v_len - *pos;
memcpy(buf, &stream_buf[*pos], count);
free_buf:
kvfree(stream_buf);
return count;
}
/**
* check_lock_range() - vfs helper for smb byte range file locking
* @filp: the file to apply the lock to
* @start: lock start byte offset
* @end: lock end byte offset
* @type: byte range type read/write
*
* Return: 0 on success, otherwise error
*/
static int check_lock_range(struct file *filp, loff_t start, loff_t end,
unsigned char type)
{
struct file_lock *flock;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 2, 0)
struct file_lock_context *ctx = locks_inode_context(file_inode(filp));
#else
struct file_lock_context *ctx = file_inode(filp)->i_flctx;
#endif
int error = 0;
if (!ctx || list_empty_careful(&ctx->flc_posix))
return 0;
spin_lock(&ctx->flc_lock);
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 9, 0)
for_each_file_lock(flock, &ctx->flc_posix) {
#else
list_for_each_entry(flock, &ctx->flc_posix, fl_list) {
#endif
/* check conflict locks */
if (flock->fl_end >= start && end >= flock->fl_start) {
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 9, 0)
if (lock_is_read(flock)) {
#else
if (flock->fl_type == F_RDLCK) {
#endif
if (type == WRITE) {
pr_err("not allow write by shared lock\n");
error = 1;
goto out;
}
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 9, 0)
} else if (lock_is_write(flock)) {
#else
} else if (flock->fl_type == F_WRLCK) {
#endif
/* check owner in lock */
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 9, 0)
if (flock->c.flc_file != filp) {
#else
if (flock->fl_file != filp) {
#endif
error = 1;
pr_err("not allow rw access by exclusive lock from other opens\n");
goto out;
}
}
}
}
out:
spin_unlock(&ctx->flc_lock);
return error;
}
/**
* ksmbd_vfs_read() - vfs helper for smb file read
* @work: smb work
* @fp: ksmbd file pointer
* @count: read byte count
* @pos: file pos
* @rbuf: read data buffer
*
* Return: number of read bytes on success, otherwise error
*/
int ksmbd_vfs_read(struct ksmbd_work *work, struct ksmbd_file *fp, size_t count,
loff_t *pos, char *rbuf)
{
struct file *filp = fp->filp;
ssize_t nbytes = 0;
struct inode *inode = file_inode(filp);
if (S_ISDIR(inode->i_mode))
return -EISDIR;
if (unlikely(count == 0))
return 0;
if (work->conn->connection_type) {
if (!(fp->daccess & (FILE_READ_DATA_LE | FILE_EXECUTE_LE))) {
pr_err("no right to read(%pD)\n", fp->filp);
return -EACCES;
}
}
if (ksmbd_stream_fd(fp))
return ksmbd_vfs_stream_read(fp, rbuf, pos, count);
if (!work->tcon->posix_extensions) {
int ret;
ret = check_lock_range(filp, *pos, *pos + count - 1, READ);
if (ret) {
pr_err("unable to read due to lock\n");
return -EAGAIN;
}
}
nbytes = kernel_read(filp, rbuf, count, pos);
if (nbytes < 0) {
pr_err("smb read failed, err = %zd\n", nbytes);
return nbytes;
}
filp->f_pos = *pos;
return nbytes;
}
static int ksmbd_vfs_stream_write(struct ksmbd_file *fp, char *buf, loff_t *pos,
size_t count)
{
char *stream_buf = NULL, *wbuf;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 3, 0)
struct mnt_idmap *idmap = file_mnt_idmap(fp->filp);
#else
struct user_namespace *user_ns = file_mnt_user_ns(fp->filp);
#endif
size_t size;
ssize_t v_len;
int err = 0;
ksmbd_debug(VFS, "write stream data pos : %llu, count : %zd\n",
*pos, count);
size = *pos + count;
if (size > XATTR_SIZE_MAX) {
size = XATTR_SIZE_MAX;
count = (*pos + count) - XATTR_SIZE_MAX;
}
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 3, 0)
v_len = ksmbd_vfs_getcasexattr(idmap,
#else
v_len = ksmbd_vfs_getcasexattr(user_ns,
#endif
fp->filp->f_path.dentry,
fp->stream.name,
fp->stream.size,
&stream_buf);
if (v_len < 0) {
pr_err("not found stream in xattr : %zd\n", v_len);
err = v_len;
goto out;
}
if (v_len < size) {
wbuf = kvzalloc(size, KSMBD_DEFAULT_GFP);
if (!wbuf) {
err = -ENOMEM;
goto out;
}
if (v_len > 0)
memcpy(wbuf, stream_buf, v_len);
kvfree(stream_buf);
stream_buf = wbuf;
}
memcpy(&stream_buf[*pos], buf, count);
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 3, 0)
err = ksmbd_vfs_setxattr(idmap,
#else
err = ksmbd_vfs_setxattr(user_ns,
#endif
&fp->filp->f_path,
fp->stream.name,
(void *)stream_buf,
size,
0,
true);
if (err < 0)
goto out;
fp->filp->f_pos = *pos;
err = 0;
out:
kvfree(stream_buf);
return err;
}
/**
* ksmbd_vfs_write() - vfs helper for smb file write
* @work: work
* @fp: ksmbd file pointer
* @buf: buf containing data for writing
* @count: read byte count
* @pos: file pos
* @sync: fsync after write
* @written: number of bytes written
*
* Return: 0 on success, otherwise error
*/
int ksmbd_vfs_write(struct ksmbd_work *work, struct ksmbd_file *fp,
char *buf, size_t count, loff_t *pos, bool sync,
ssize_t *written)
{
struct file *filp;
loff_t offset = *pos;
int err = 0;
if (work->conn->connection_type) {
if (!(fp->daccess & (FILE_WRITE_DATA_LE | FILE_APPEND_DATA_LE))) {
pr_err("no right to write(%pD)\n", fp->filp);
err = -EACCES;
goto out;
}
}
filp = fp->filp;
if (ksmbd_stream_fd(fp)) {
err = ksmbd_vfs_stream_write(fp, buf, pos, count);
if (!err)
*written = count;
goto out;
}
if (!work->tcon->posix_extensions) {
err = check_lock_range(filp, *pos, *pos + count - 1, WRITE);
if (err) {
pr_err("unable to write due to lock\n");
err = -EAGAIN;
goto out;
}
}
/* Reserve lease break for parent dir at closing time */
fp->reserve_lease_break = true;
/* Do we need to break any of a levelII oplock? */
smb_break_all_levII_oplock(work, fp, 1);
err = kernel_write(filp, buf, count, pos);
if (err < 0) {
ksmbd_debug(VFS, "smb write failed, err = %d\n", err);
goto out;
}
filp->f_pos = *pos;
*written = err;
err = 0;
if (sync) {
err = vfs_fsync_range(filp, offset, offset + *written, 0);
if (err < 0)
pr_err("fsync failed for filename = %pD, err = %d\n",
fp->filp, err);
}
out:
return err;
}
/**
* ksmbd_vfs_getattr() - vfs helper for smb getattr
* @path: path of dentry
* @stat: pointer to returned kernel stat structure
* Return: 0 on success, otherwise error
*/
int ksmbd_vfs_getattr(const struct path *path, struct kstat *stat)
{
int err;
err = vfs_getattr(path, stat, STATX_BTIME, AT_STATX_SYNC_AS_STAT);
if (err)
pr_err("getattr failed, err %d\n", err);
return err;
}
#ifdef CONFIG_SMB_INSECURE_SERVER
/**
* smb_check_attrs() - sanitize inode attributes
* @inode: inode
* @attrs: inode attributes
*/
static void smb_check_attrs(struct inode *inode, struct iattr *attrs)
{
/* sanitize the mode change */
if (attrs->ia_valid & ATTR_MODE) {
attrs->ia_mode &= S_IALLUGO;
attrs->ia_mode |= (inode->i_mode & ~S_IALLUGO);
}
/* Revoke setuid/setgid on chown */
if (!S_ISDIR(inode->i_mode) &&
(((attrs->ia_valid & ATTR_UID) &&
!uid_eq(attrs->ia_uid, inode->i_uid)) ||
((attrs->ia_valid & ATTR_GID) &&
!gid_eq(attrs->ia_gid, inode->i_gid)))) {
attrs->ia_valid |= ATTR_KILL_PRIV;
if (attrs->ia_valid & ATTR_MODE) {
/* we're setting mode too, just clear the s*id bits */
attrs->ia_mode &= ~S_ISUID;
if (attrs->ia_mode & 0010)
attrs->ia_mode &= ~S_ISGID;
} else {
/* set ATTR_KILL_* bits and let VFS handle it */
attrs->ia_valid |= (ATTR_KILL_SUID | ATTR_KILL_SGID);
}
}
}
/**
* ksmbd_vfs_setattr() - vfs helper for smb setattr
* @work: work
* @name: file name
* @fid: file id of open file
* @attrs: inode attributes
*
* Return: 0 on success, otherwise error
*/
int ksmbd_vfs_setattr(struct ksmbd_work *work, const char *name, u64 fid,
struct iattr *attrs)
{
struct file *filp;
struct dentry *dentry;
struct inode *inode;
struct path path;
bool update_size = false;
int err = 0;
struct ksmbd_file *fp = NULL;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 3, 0)
struct mnt_idmap *idmap;
#else
struct user_namespace *user_ns;
#endif
if (ksmbd_override_fsids(work))
return -ENOMEM;
if (name) {
err = kern_path(name, 0, &path);
if (err) {
ksmbd_revert_fsids(work);
ksmbd_debug(VFS, "lookup failed for %s, err = %d\n",
name, err);
return -ENOENT;
}
dentry = path.dentry;
inode = d_inode(dentry);
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 3, 0)
idmap = mnt_idmap(path.mnt);
#else
user_ns = mnt_user_ns(path.mnt);
#endif
} else {
fp = ksmbd_lookup_fd_fast(work, fid);
if (!fp) {
ksmbd_revert_fsids(work);
pr_err("failed to get filp for fid %llu\n", fid);
return -ENOENT;
}
filp = fp->filp;
dentry = filp->f_path.dentry;
inode = d_inode(dentry);
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 3, 0)
idmap = file_mnt_idmap(filp);
#else
user_ns = file_mnt_user_ns(filp);
#endif
}
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 3, 0)
err = inode_permission(idmap, d_inode(dentry), MAY_WRITE);
#else
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 12, 0)
err = inode_permission(user_ns, d_inode(dentry), MAY_WRITE);
#else
err = inode_permission(d_inode(dentry), MAY_WRITE);
#endif
#endif
if (err)
goto out;
/* no need to update mode of symlink */
if (S_ISLNK(inode->i_mode))
attrs->ia_valid &= ~ATTR_MODE;
/* skip setattr, if nothing to update */
if (!attrs->ia_valid) {
err = 0;
goto out;
}
smb_check_attrs(inode, attrs);
if (attrs->ia_valid & ATTR_SIZE) {
err = get_write_access(inode);
if (err)
goto out;
update_size = true;
}
attrs->ia_valid |= ATTR_CTIME;
inode_lock(inode);
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 3, 0)
err = notify_change(idmap, dentry, attrs, NULL);
#else
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 12, 0)
err = notify_change(user_ns, dentry, attrs, NULL);
#else
err = notify_change(dentry, attrs, NULL);
#endif
#endif
inode_unlock(inode);
if (update_size)
put_write_access(inode);
if (!err) {
sync_inode_metadata(inode, 1);
ksmbd_debug(VFS, "fid %llu, setattr done\n", fid);
}
out:
if (name)
path_put(&path);
ksmbd_fd_put(work, fp);
ksmbd_revert_fsids(work);
return err;
}
/**
* ksmbd_vfs_symlink() - vfs helper for creating smb symlink
* @name: source file name
* @symname: symlink name
*
* Return: 0 on success, otherwise error
*/