1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (c) 2024 Paulo Alcantara <pc@manguebit.com>
4 */
5
6 #include <linux/fs.h>
7 #include <linux/stat.h>
8 #include <linux/slab.h>
9 #include "cifsglob.h"
10 #include "smb2proto.h"
11 #include "cifsproto.h"
12 #include "cifs_unicode.h"
13 #include "cifs_debug.h"
14 #include "fs_context.h"
15 #include "reparse.h"
16
17 static int mknod_nfs(unsigned int xid, struct inode *inode,
18 struct dentry *dentry, struct cifs_tcon *tcon,
19 const char *full_path, umode_t mode, dev_t dev,
20 const char *symname);
21
22 static int mknod_wsl(unsigned int xid, struct inode *inode,
23 struct dentry *dentry, struct cifs_tcon *tcon,
24 const char *full_path, umode_t mode, dev_t dev,
25 const char *symname);
26
27 static int create_native_symlink(const unsigned int xid, struct inode *inode,
28 struct dentry *dentry, struct cifs_tcon *tcon,
29 const char *full_path, const char *symname);
30
31 static int detect_directory_symlink_target(struct cifs_sb_info *cifs_sb,
32 const unsigned int xid,
33 const char *full_path,
34 const char *symname,
35 bool *directory);
36
create_reparse_symlink(const unsigned int xid,struct inode * inode,struct dentry * dentry,struct cifs_tcon * tcon,const char * full_path,const char * symname)37 int create_reparse_symlink(const unsigned int xid, struct inode *inode,
38 struct dentry *dentry, struct cifs_tcon *tcon,
39 const char *full_path, const char *symname)
40 {
41 switch (cifs_symlink_type(CIFS_SB(inode->i_sb))) {
42 case CIFS_SYMLINK_TYPE_NATIVE:
43 return create_native_symlink(xid, inode, dentry, tcon, full_path, symname);
44 case CIFS_SYMLINK_TYPE_NFS:
45 return mknod_nfs(xid, inode, dentry, tcon, full_path, S_IFLNK, 0, symname);
46 case CIFS_SYMLINK_TYPE_WSL:
47 return mknod_wsl(xid, inode, dentry, tcon, full_path, S_IFLNK, 0, symname);
48 default:
49 return -EOPNOTSUPP;
50 }
51 }
52
create_native_symlink(const unsigned int xid,struct inode * inode,struct dentry * dentry,struct cifs_tcon * tcon,const char * full_path,const char * symname)53 static int create_native_symlink(const unsigned int xid, struct inode *inode,
54 struct dentry *dentry, struct cifs_tcon *tcon,
55 const char *full_path, const char *symname)
56 {
57 struct reparse_symlink_data_buffer *buf = NULL;
58 struct cifs_open_info_data data = {};
59 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
60 const char *symroot = cifs_sb->ctx->symlinkroot;
61 struct inode *new;
62 struct kvec iov;
63 __le16 *path = NULL;
64 bool directory;
65 char *symlink_target = NULL;
66 char *sym = NULL;
67 char sep = CIFS_DIR_SEP(cifs_sb);
68 u16 len, plen, poff, slen;
69 int rc = 0;
70
71 if (strlen(symname) > REPARSE_SYM_PATH_MAX)
72 return -ENAMETOOLONG;
73
74 symlink_target = kstrdup(symname, GFP_KERNEL);
75 if (!symlink_target) {
76 rc = -ENOMEM;
77 goto out;
78 }
79
80 data = (struct cifs_open_info_data) {
81 .reparse_point = true,
82 .reparse = { .tag = IO_REPARSE_TAG_SYMLINK, },
83 .symlink_target = symlink_target,
84 };
85
86 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS) &&
87 symroot && symname[0] == '/') {
88 /*
89 * This is a request to create an absolute symlink on the server
90 * which does not support POSIX paths, and expects symlink in
91 * NT-style path. So convert absolute Linux symlink target path
92 * to the absolute NT-style path. Root of the NT-style path for
93 * symlinks is specified in "symlinkroot" mount option. This will
94 * ensure compatibility of this symlink stored in absolute form
95 * on the SMB server.
96 */
97 if (!strstarts(symname, symroot)) {
98 /*
99 * If the absolute Linux symlink target path is not
100 * inside "symlinkroot" location then there is no way
101 * to convert such Linux symlink to NT-style path.
102 */
103 cifs_dbg(VFS,
104 "absolute symlink '%s' cannot be converted to NT format "
105 "because it is outside of symlinkroot='%s'\n",
106 symname, symroot);
107 rc = -EINVAL;
108 goto out;
109 }
110 len = strlen(symroot);
111 if (symroot[len - 1] != '/')
112 len++;
113 if (symname[len] >= 'a' && symname[len] <= 'z' &&
114 (symname[len+1] == '/' || symname[len+1] == '\0')) {
115 /*
116 * Symlink points to Linux target /symlinkroot/x/path/...
117 * where 'x' is the lowercase local Windows drive.
118 * NT-style path for 'x' has common form \??\X:\path\...
119 * with uppercase local Windows drive.
120 */
121 int common_path_len = strlen(symname+len+1)+1;
122 sym = kzalloc(6+common_path_len, GFP_KERNEL);
123 if (!sym) {
124 rc = -ENOMEM;
125 goto out;
126 }
127 memcpy(sym, "\\??\\", 4);
128 sym[4] = symname[len] - ('a'-'A');
129 sym[5] = ':';
130 memcpy(sym+6, symname+len+1, common_path_len);
131 } else {
132 /* Unhandled absolute symlink. Report an error. */
133 cifs_dbg(
134 VFS,
135 "absolute symlink '%s' cannot be converted to NT format "
136 "because it points to unknown target\n",
137 symname);
138 rc = -EINVAL;
139 goto out;
140 }
141 } else {
142 /*
143 * This is request to either create an absolute symlink on
144 * server which expects POSIX paths or it is an request to
145 * create a relative symlink from the current directory.
146 * These paths have same format as relative SMB symlinks,
147 * so no conversion is needed. So just take symname as-is.
148 */
149 sym = kstrdup(symname, GFP_KERNEL);
150 if (!sym) {
151 rc = -ENOMEM;
152 goto out;
153 }
154 }
155
156 if (sep == '\\')
157 convert_delimiter(sym, sep);
158
159 /*
160 * For absolute NT symlinks it is required to pass also leading
161 * backslash and to not mangle NT object prefix "\\??\\" and not to
162 * mangle colon in drive letter. But cifs_convert_path_to_utf16()
163 * removes leading backslash and replaces '?' and ':'. So temporary
164 * mask these characters in NT object prefix by '_' and then change
165 * them back.
166 */
167 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS) && symname[0] == '/')
168 sym[0] = sym[1] = sym[2] = sym[5] = '_';
169
170 path = cifs_convert_path_to_utf16(sym, cifs_sb);
171 if (!path) {
172 rc = -ENOMEM;
173 goto out;
174 }
175
176 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS) && symname[0] == '/') {
177 sym[0] = '\\';
178 sym[1] = sym[2] = '?';
179 sym[5] = ':';
180 path[0] = cpu_to_le16('\\');
181 path[1] = path[2] = cpu_to_le16('?');
182 path[5] = cpu_to_le16(':');
183 }
184
185 /*
186 * SMB distinguish between symlink to directory and symlink to file.
187 * They cannot be exchanged (symlink of file type which points to
188 * directory cannot be resolved and vice-versa). Try to detect if
189 * the symlink target could be a directory or not. When detection
190 * fails then treat symlink as a file (non-directory) symlink.
191 */
192 directory = false;
193 rc = detect_directory_symlink_target(cifs_sb, xid, full_path, symname, &directory);
194 if (rc < 0)
195 goto out;
196
197 slen = 2 * UniStrnlen((wchar_t *)path, REPARSE_SYM_PATH_MAX);
198 poff = 0;
199 plen = slen;
200 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS) && symname[0] == '/') {
201 /*
202 * For absolute NT symlinks skip leading "\\??\\" in PrintName as
203 * PrintName is user visible location in DOS/Win32 format (not in NT format).
204 */
205 poff = 4;
206 plen -= 2 * poff;
207 }
208 len = sizeof(*buf) + plen + slen;
209 buf = kzalloc(len, GFP_KERNEL);
210 if (!buf) {
211 rc = -ENOMEM;
212 goto out;
213 }
214
215 buf->ReparseTag = cpu_to_le32(IO_REPARSE_TAG_SYMLINK);
216 buf->ReparseDataLength = cpu_to_le16(len - sizeof(struct reparse_data_buffer));
217
218 buf->SubstituteNameOffset = cpu_to_le16(plen);
219 buf->SubstituteNameLength = cpu_to_le16(slen);
220 memcpy(&buf->PathBuffer[plen], path, slen);
221
222 buf->PrintNameOffset = 0;
223 buf->PrintNameLength = cpu_to_le16(plen);
224 memcpy(buf->PathBuffer, path+poff, plen);
225
226 buf->Flags = cpu_to_le32(*symname != '/' ? SYMLINK_FLAG_RELATIVE : 0);
227
228 iov.iov_base = buf;
229 iov.iov_len = len;
230 new = tcon->ses->server->ops->create_reparse_inode(
231 &data, inode->i_sb, xid,
232 tcon, full_path, directory,
233 &iov, NULL);
234 if (!IS_ERR(new))
235 d_instantiate(dentry, new);
236 else
237 rc = PTR_ERR(new);
238 out:
239 kfree(sym);
240 kfree(path);
241 cifs_free_open_info(&data);
242 kfree(buf);
243 return rc;
244 }
245
detect_directory_symlink_target(struct cifs_sb_info * cifs_sb,const unsigned int xid,const char * full_path,const char * symname,bool * directory)246 static int detect_directory_symlink_target(struct cifs_sb_info *cifs_sb,
247 const unsigned int xid,
248 const char *full_path,
249 const char *symname,
250 bool *directory)
251 {
252 char sep = CIFS_DIR_SEP(cifs_sb);
253 struct cifs_open_parms oparms;
254 struct tcon_link *tlink;
255 struct cifs_tcon *tcon;
256 const char *basename;
257 struct cifs_fid fid;
258 char *resolved_path;
259 int full_path_len;
260 int basename_len;
261 int symname_len;
262 char *path_sep;
263 __u32 oplock;
264 int open_rc;
265
266 /*
267 * First do some simple check. If the original Linux symlink target ends
268 * with slash, or last path component is dot or dot-dot then it is for
269 * sure symlink to the directory.
270 */
271 basename = kbasename(symname);
272 basename_len = strlen(basename);
273 if (basename_len == 0 || /* symname ends with slash */
274 (basename_len == 1 && basename[0] == '.') || /* last component is "." */
275 (basename_len == 2 && basename[0] == '.' && basename[1] == '.')) { /* or ".." */
276 *directory = true;
277 return 0;
278 }
279
280 /*
281 * For absolute symlinks it is not possible to determine
282 * if it should point to directory or file.
283 */
284 if (symname[0] == '/') {
285 cifs_dbg(FYI,
286 "%s: cannot determinate if the symlink target path '%s' "
287 "is directory or not, creating '%s' as file symlink\n",
288 __func__, symname, full_path);
289 return 0;
290 }
291
292 /*
293 * If it was not detected as directory yet and the symlink is relative
294 * then try to resolve the path on the SMB server, check if the path
295 * exists and determinate if it is a directory or not.
296 */
297
298 full_path_len = strlen(full_path);
299 symname_len = strlen(symname);
300
301 tlink = cifs_sb_tlink(cifs_sb);
302 if (IS_ERR(tlink))
303 return PTR_ERR(tlink);
304
305 resolved_path = kzalloc(full_path_len + symname_len + 1, GFP_KERNEL);
306 if (!resolved_path) {
307 cifs_put_tlink(tlink);
308 return -ENOMEM;
309 }
310
311 /*
312 * Compose the resolved SMB symlink path from the SMB full path
313 * and Linux target symlink path.
314 */
315 memcpy(resolved_path, full_path, full_path_len+1);
316 path_sep = strrchr(resolved_path, sep);
317 if (path_sep)
318 path_sep++;
319 else
320 path_sep = resolved_path;
321 memcpy(path_sep, symname, symname_len+1);
322 if (sep == '\\')
323 convert_delimiter(path_sep, sep);
324
325 tcon = tlink_tcon(tlink);
326 oparms = CIFS_OPARMS(cifs_sb, tcon, resolved_path,
327 FILE_READ_ATTRIBUTES, FILE_OPEN, 0, ACL_NO_MODE);
328 oparms.fid = &fid;
329
330 /* Try to open as a directory (NOT_FILE) */
331 oplock = 0;
332 oparms.create_options = cifs_create_options(cifs_sb,
333 CREATE_NOT_FILE | OPEN_REPARSE_POINT);
334 open_rc = tcon->ses->server->ops->open(xid, &oparms, &oplock, NULL);
335 if (open_rc == 0) {
336 /* Successful open means that the target path is definitely a directory. */
337 *directory = true;
338 tcon->ses->server->ops->close(xid, tcon, &fid);
339 } else if (open_rc == -ENOTDIR) {
340 /* -ENOTDIR means that the target path is definitely a file. */
341 *directory = false;
342 } else if (open_rc == -ENOENT) {
343 /* -ENOENT means that the target path does not exist. */
344 cifs_dbg(FYI,
345 "%s: symlink target path '%s' does not exist, "
346 "creating '%s' as file symlink\n",
347 __func__, symname, full_path);
348 } else {
349 /* Try to open as a file (NOT_DIR) */
350 oplock = 0;
351 oparms.create_options = cifs_create_options(cifs_sb,
352 CREATE_NOT_DIR | OPEN_REPARSE_POINT);
353 open_rc = tcon->ses->server->ops->open(xid, &oparms, &oplock, NULL);
354 if (open_rc == 0) {
355 /* Successful open means that the target path is definitely a file. */
356 *directory = false;
357 tcon->ses->server->ops->close(xid, tcon, &fid);
358 } else if (open_rc == -EISDIR) {
359 /* -EISDIR means that the target path is definitely a directory. */
360 *directory = true;
361 } else {
362 /*
363 * This code branch is called when we do not have a permission to
364 * open the resolved_path or some other client/process denied
365 * opening the resolved_path.
366 *
367 * TODO: Try to use ops->query_dir_first on the parent directory
368 * of resolved_path, search for basename of resolved_path and
369 * check if the ATTR_DIRECTORY is set in fi.Attributes. In some
370 * case this could work also when opening of the path is denied.
371 */
372 cifs_dbg(FYI,
373 "%s: cannot determinate if the symlink target path '%s' "
374 "is directory or not, creating '%s' as file symlink\n",
375 __func__, symname, full_path);
376 }
377 }
378
379 kfree(resolved_path);
380 cifs_put_tlink(tlink);
381 return 0;
382 }
383
create_native_socket(const unsigned int xid,struct inode * inode,struct dentry * dentry,struct cifs_tcon * tcon,const char * full_path)384 static int create_native_socket(const unsigned int xid, struct inode *inode,
385 struct dentry *dentry, struct cifs_tcon *tcon,
386 const char *full_path)
387 {
388 struct reparse_data_buffer buf = {
389 .ReparseTag = cpu_to_le32(IO_REPARSE_TAG_AF_UNIX),
390 .ReparseDataLength = cpu_to_le16(0),
391 };
392 struct cifs_open_info_data data = {
393 .reparse_point = true,
394 .reparse = { .tag = IO_REPARSE_TAG_AF_UNIX, .buf = &buf, },
395 };
396 struct kvec iov = {
397 .iov_base = &buf,
398 .iov_len = sizeof(buf),
399 };
400 struct inode *new;
401 int rc = 0;
402
403 new = tcon->ses->server->ops->create_reparse_inode(
404 &data, inode->i_sb, xid,
405 tcon, full_path, false, &iov, NULL);
406 if (!IS_ERR(new))
407 d_instantiate(dentry, new);
408 else
409 rc = PTR_ERR(new);
410 cifs_free_open_info(&data);
411 return rc;
412 }
413
nfs_set_reparse_buf(struct reparse_nfs_data_buffer * buf,mode_t mode,dev_t dev,__le16 * symname_utf16,int symname_utf16_len,struct kvec * iov)414 static int nfs_set_reparse_buf(struct reparse_nfs_data_buffer *buf,
415 mode_t mode, dev_t dev,
416 __le16 *symname_utf16,
417 int symname_utf16_len,
418 struct kvec *iov)
419 {
420 u64 type;
421 u16 len, dlen;
422
423 len = sizeof(*buf);
424
425 switch ((type = reparse_mode_nfs_type(mode))) {
426 case NFS_SPECFILE_BLK:
427 case NFS_SPECFILE_CHR:
428 dlen = 2 * sizeof(__le32);
429 ((__le32 *)buf->DataBuffer)[0] = cpu_to_le32(MAJOR(dev));
430 ((__le32 *)buf->DataBuffer)[1] = cpu_to_le32(MINOR(dev));
431 break;
432 case NFS_SPECFILE_LNK:
433 dlen = symname_utf16_len;
434 memcpy(buf->DataBuffer, symname_utf16, symname_utf16_len);
435 break;
436 case NFS_SPECFILE_FIFO:
437 case NFS_SPECFILE_SOCK:
438 dlen = 0;
439 break;
440 default:
441 return -EOPNOTSUPP;
442 }
443
444 buf->ReparseTag = cpu_to_le32(IO_REPARSE_TAG_NFS);
445 buf->Reserved = 0;
446 buf->InodeType = cpu_to_le64(type);
447 buf->ReparseDataLength = cpu_to_le16(len + dlen -
448 sizeof(struct reparse_data_buffer));
449 iov->iov_base = buf;
450 iov->iov_len = len + dlen;
451 return 0;
452 }
453
mknod_nfs(unsigned int xid,struct inode * inode,struct dentry * dentry,struct cifs_tcon * tcon,const char * full_path,umode_t mode,dev_t dev,const char * symname)454 static int mknod_nfs(unsigned int xid, struct inode *inode,
455 struct dentry *dentry, struct cifs_tcon *tcon,
456 const char *full_path, umode_t mode, dev_t dev,
457 const char *symname)
458 {
459 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
460 struct cifs_open_info_data data;
461 struct reparse_nfs_data_buffer *p = NULL;
462 __le16 *symname_utf16 = NULL;
463 int symname_utf16_len = 0;
464 struct inode *new;
465 struct kvec iov;
466 __u8 buf[sizeof(*p) + sizeof(__le64)];
467 int rc;
468
469 if (S_ISLNK(mode)) {
470 symname_utf16 = cifs_strndup_to_utf16(symname, strlen(symname),
471 &symname_utf16_len,
472 cifs_sb->local_nls,
473 NO_MAP_UNI_RSVD);
474 if (!symname_utf16) {
475 rc = -ENOMEM;
476 goto out;
477 }
478 symname_utf16_len -= 2; /* symlink is without trailing wide-nul */
479 p = kzalloc(sizeof(*p) + symname_utf16_len, GFP_KERNEL);
480 if (!p) {
481 rc = -ENOMEM;
482 goto out;
483 }
484 } else {
485 p = (struct reparse_nfs_data_buffer *)buf;
486 }
487 rc = nfs_set_reparse_buf(p, mode, dev, symname_utf16, symname_utf16_len, &iov);
488 if (rc)
489 goto out;
490
491 data = (struct cifs_open_info_data) {
492 .reparse_point = true,
493 .reparse = { .tag = IO_REPARSE_TAG_NFS, .buf = (struct reparse_data_buffer *)p, },
494 .symlink_target = kstrdup(symname, GFP_KERNEL),
495 };
496
497 new = tcon->ses->server->ops->create_reparse_inode(
498 &data, inode->i_sb, xid,
499 tcon, full_path, false, &iov, NULL);
500 if (!IS_ERR(new))
501 d_instantiate(dentry, new);
502 else
503 rc = PTR_ERR(new);
504 cifs_free_open_info(&data);
505 out:
506 if (S_ISLNK(mode)) {
507 kfree(symname_utf16);
508 kfree(p);
509 }
510 return rc;
511 }
512
wsl_set_reparse_buf(struct reparse_data_buffer ** buf,mode_t mode,const char * symname,struct cifs_sb_info * cifs_sb,struct kvec * iov)513 static int wsl_set_reparse_buf(struct reparse_data_buffer **buf,
514 mode_t mode, const char *symname,
515 struct cifs_sb_info *cifs_sb,
516 struct kvec *iov)
517 {
518 struct reparse_wsl_symlink_data_buffer *symlink_buf;
519 __le16 *symname_utf16;
520 int symname_utf16_len;
521 int symname_utf8_maxlen;
522 int symname_utf8_len;
523 size_t buf_len;
524 u32 tag;
525
526 switch ((tag = reparse_mode_wsl_tag(mode))) {
527 case IO_REPARSE_TAG_LX_BLK:
528 case IO_REPARSE_TAG_LX_CHR:
529 case IO_REPARSE_TAG_LX_FIFO:
530 case IO_REPARSE_TAG_AF_UNIX:
531 buf_len = sizeof(struct reparse_data_buffer);
532 *buf = kzalloc(buf_len, GFP_KERNEL);
533 if (!*buf)
534 return -ENOMEM;
535 break;
536 case IO_REPARSE_TAG_LX_SYMLINK:
537 symname_utf16 = cifs_strndup_to_utf16(symname, strlen(symname),
538 &symname_utf16_len,
539 cifs_sb->local_nls,
540 NO_MAP_UNI_RSVD);
541 if (!symname_utf16)
542 return -ENOMEM;
543 symname_utf8_maxlen = symname_utf16_len/2*3;
544 symlink_buf = kzalloc(sizeof(struct reparse_wsl_symlink_data_buffer) +
545 symname_utf8_maxlen, GFP_KERNEL);
546 if (!symlink_buf) {
547 kfree(symname_utf16);
548 return -ENOMEM;
549 }
550 /* Version field must be set to 2 (MS-FSCC 2.1.2.7) */
551 symlink_buf->Version = cpu_to_le32(2);
552 /* Target for Version 2 is in UTF-8 but without trailing null-term byte */
553 symname_utf8_len = utf16s_to_utf8s((wchar_t *)symname_utf16, symname_utf16_len/2,
554 UTF16_LITTLE_ENDIAN,
555 symlink_buf->Target,
556 symname_utf8_maxlen);
557 *buf = (struct reparse_data_buffer *)symlink_buf;
558 buf_len = sizeof(struct reparse_wsl_symlink_data_buffer) + symname_utf8_len;
559 kfree(symname_utf16);
560 break;
561 default:
562 return -EOPNOTSUPP;
563 }
564
565 (*buf)->ReparseTag = cpu_to_le32(tag);
566 (*buf)->Reserved = 0;
567 (*buf)->ReparseDataLength = cpu_to_le16(buf_len - sizeof(struct reparse_data_buffer));
568 iov->iov_base = *buf;
569 iov->iov_len = buf_len;
570 return 0;
571 }
572
ea_create_context(u32 dlen,size_t * cc_len)573 static struct smb2_create_ea_ctx *ea_create_context(u32 dlen, size_t *cc_len)
574 {
575 struct smb2_create_ea_ctx *cc;
576
577 *cc_len = round_up(sizeof(*cc) + dlen, 8);
578 cc = kzalloc(*cc_len, GFP_KERNEL);
579 if (!cc)
580 return ERR_PTR(-ENOMEM);
581
582 cc->ctx.NameOffset = cpu_to_le16(offsetof(struct smb2_create_ea_ctx,
583 name));
584 cc->ctx.NameLength = cpu_to_le16(4);
585 memcpy(cc->name, SMB2_CREATE_EA_BUFFER, strlen(SMB2_CREATE_EA_BUFFER));
586 cc->ctx.DataOffset = cpu_to_le16(offsetof(struct smb2_create_ea_ctx, ea));
587 cc->ctx.DataLength = cpu_to_le32(dlen);
588 return cc;
589 }
590
591 struct wsl_xattr {
592 const char *name;
593 __le64 value;
594 u16 size;
595 u32 next;
596 };
597
wsl_set_xattrs(struct inode * inode,umode_t _mode,dev_t _dev,struct kvec * iov)598 static int wsl_set_xattrs(struct inode *inode, umode_t _mode,
599 dev_t _dev, struct kvec *iov)
600 {
601 struct smb2_file_full_ea_info *ea;
602 struct smb2_create_ea_ctx *cc;
603 struct smb3_fs_context *ctx = CIFS_SB(inode->i_sb)->ctx;
604 __le64 uid = cpu_to_le64(from_kuid(current_user_ns(), ctx->linux_uid));
605 __le64 gid = cpu_to_le64(from_kgid(current_user_ns(), ctx->linux_gid));
606 __le64 dev = cpu_to_le64(((u64)MINOR(_dev) << 32) | MAJOR(_dev));
607 __le64 mode = cpu_to_le64(_mode);
608 struct wsl_xattr xattrs[] = {
609 { .name = SMB2_WSL_XATTR_UID, .value = uid, .size = SMB2_WSL_XATTR_UID_SIZE, },
610 { .name = SMB2_WSL_XATTR_GID, .value = gid, .size = SMB2_WSL_XATTR_GID_SIZE, },
611 { .name = SMB2_WSL_XATTR_MODE, .value = mode, .size = SMB2_WSL_XATTR_MODE_SIZE, },
612 { .name = SMB2_WSL_XATTR_DEV, .value = dev, .size = SMB2_WSL_XATTR_DEV_SIZE, },
613 };
614 size_t cc_len;
615 u32 dlen = 0, next = 0;
616 int i, num_xattrs;
617 u8 name_size = SMB2_WSL_XATTR_NAME_LEN + 1;
618
619 memset(iov, 0, sizeof(*iov));
620
621 /* Exclude $LXDEV xattr for non-device files */
622 if (!S_ISBLK(_mode) && !S_ISCHR(_mode))
623 num_xattrs = ARRAY_SIZE(xattrs) - 1;
624 else
625 num_xattrs = ARRAY_SIZE(xattrs);
626
627 for (i = 0; i < num_xattrs; i++) {
628 xattrs[i].next = ALIGN(sizeof(*ea) + name_size +
629 xattrs[i].size, 4);
630 dlen += xattrs[i].next;
631 }
632
633 cc = ea_create_context(dlen, &cc_len);
634 if (IS_ERR(cc))
635 return PTR_ERR(cc);
636
637 ea = &cc->ea;
638 for (i = 0; i < num_xattrs; i++) {
639 ea = (void *)((u8 *)ea + next);
640 next = xattrs[i].next;
641 ea->next_entry_offset = cpu_to_le32(next);
642
643 ea->ea_name_length = name_size - 1;
644 ea->ea_value_length = cpu_to_le16(xattrs[i].size);
645 memcpy(ea->ea_data, xattrs[i].name, name_size);
646 memcpy(&ea->ea_data[name_size],
647 &xattrs[i].value, xattrs[i].size);
648 }
649 ea->next_entry_offset = 0;
650
651 iov->iov_base = cc;
652 iov->iov_len = cc_len;
653 return 0;
654 }
655
mknod_wsl(unsigned int xid,struct inode * inode,struct dentry * dentry,struct cifs_tcon * tcon,const char * full_path,umode_t mode,dev_t dev,const char * symname)656 static int mknod_wsl(unsigned int xid, struct inode *inode,
657 struct dentry *dentry, struct cifs_tcon *tcon,
658 const char *full_path, umode_t mode, dev_t dev,
659 const char *symname)
660 {
661 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
662 struct cifs_open_info_data data;
663 struct reparse_data_buffer *buf;
664 struct smb2_create_ea_ctx *cc;
665 struct inode *new;
666 unsigned int len;
667 struct kvec reparse_iov, xattr_iov;
668 int rc;
669
670 rc = wsl_set_reparse_buf(&buf, mode, symname, cifs_sb, &reparse_iov);
671 if (rc)
672 return rc;
673
674 rc = wsl_set_xattrs(inode, mode, dev, &xattr_iov);
675 if (rc) {
676 kfree(buf);
677 return rc;
678 }
679
680 data = (struct cifs_open_info_data) {
681 .reparse_point = true,
682 .reparse = { .tag = le32_to_cpu(buf->ReparseTag), .buf = buf, },
683 .symlink_target = kstrdup(symname, GFP_KERNEL),
684 };
685
686 cc = xattr_iov.iov_base;
687 len = le32_to_cpu(cc->ctx.DataLength);
688 memcpy(data.wsl.eas, &cc->ea, len);
689 data.wsl.eas_len = len;
690
691 new = tcon->ses->server->ops->create_reparse_inode(
692 &data, inode->i_sb,
693 xid, tcon, full_path, false,
694 &reparse_iov, &xattr_iov);
695 if (!IS_ERR(new))
696 d_instantiate(dentry, new);
697 else
698 rc = PTR_ERR(new);
699 cifs_free_open_info(&data);
700 kfree(xattr_iov.iov_base);
701 kfree(buf);
702 return rc;
703 }
704
mknod_reparse(unsigned int xid,struct inode * inode,struct dentry * dentry,struct cifs_tcon * tcon,const char * full_path,umode_t mode,dev_t dev)705 int mknod_reparse(unsigned int xid, struct inode *inode,
706 struct dentry *dentry, struct cifs_tcon *tcon,
707 const char *full_path, umode_t mode, dev_t dev)
708 {
709 struct smb3_fs_context *ctx = CIFS_SB(inode->i_sb)->ctx;
710
711 if (S_ISSOCK(mode) && !ctx->nonativesocket && ctx->reparse_type != CIFS_REPARSE_TYPE_NONE)
712 return create_native_socket(xid, inode, dentry, tcon, full_path);
713
714 switch (ctx->reparse_type) {
715 case CIFS_REPARSE_TYPE_NFS:
716 return mknod_nfs(xid, inode, dentry, tcon, full_path, mode, dev, NULL);
717 case CIFS_REPARSE_TYPE_WSL:
718 return mknod_wsl(xid, inode, dentry, tcon, full_path, mode, dev, NULL);
719 default:
720 return -EOPNOTSUPP;
721 }
722 }
723
724 /* See MS-FSCC 2.1.2.6 for the 'NFS' style reparse tags */
parse_reparse_nfs(struct reparse_nfs_data_buffer * buf,struct cifs_sb_info * cifs_sb,struct cifs_open_info_data * data)725 static int parse_reparse_nfs(struct reparse_nfs_data_buffer *buf,
726 struct cifs_sb_info *cifs_sb,
727 struct cifs_open_info_data *data)
728 {
729 unsigned int len;
730 u64 type;
731
732 len = le16_to_cpu(buf->ReparseDataLength);
733 if (len < sizeof(buf->InodeType)) {
734 cifs_dbg(VFS, "srv returned malformed nfs buffer\n");
735 return smb_EIO2(smb_eio_trace_reparse_nfs_too_short,
736 len, sizeof(buf->InodeType));
737 }
738
739 len -= sizeof(buf->InodeType);
740
741 switch ((type = le64_to_cpu(buf->InodeType))) {
742 case NFS_SPECFILE_LNK:
743 if (len == 0 || (len % 2)) {
744 cifs_dbg(VFS, "srv returned malformed nfs symlink buffer\n");
745 return smb_EIO1(smb_eio_trace_reparse_nfs_symbuf, len);
746 }
747 /*
748 * Check that buffer does not contain UTF-16 null codepoint
749 * because Linux cannot process symlink with null byte.
750 */
751 if (UniStrnlen((wchar_t *)buf->DataBuffer, len/2) != len/2) {
752 cifs_dbg(VFS, "srv returned null byte in nfs symlink target location\n");
753 return smb_EIO1(smb_eio_trace_reparse_nfs_nul, len);
754 }
755 data->symlink_target = cifs_strndup_from_utf16(buf->DataBuffer,
756 len, true,
757 cifs_sb->local_nls);
758 if (!data->symlink_target)
759 return -ENOMEM;
760 cifs_dbg(FYI, "%s: target path: %s\n",
761 __func__, data->symlink_target);
762 break;
763 case NFS_SPECFILE_CHR:
764 case NFS_SPECFILE_BLK:
765 /* DataBuffer for block and char devices contains two 32-bit numbers */
766 if (len != 8) {
767 cifs_dbg(VFS, "srv returned malformed nfs buffer for type: 0x%llx\n", type);
768 return smb_EIO1(smb_eio_trace_reparse_nfs_dev, len);
769 }
770 break;
771 case NFS_SPECFILE_FIFO:
772 case NFS_SPECFILE_SOCK:
773 /* DataBuffer for fifos and sockets is empty */
774 if (len != 0) {
775 cifs_dbg(VFS, "srv returned malformed nfs buffer for type: 0x%llx\n", type);
776 return smb_EIO1(smb_eio_trace_reparse_nfs_sockfifo, len);
777 }
778 break;
779 default:
780 cifs_dbg(VFS, "%s: unhandled inode type: 0x%llx\n",
781 __func__, type);
782 return -EOPNOTSUPP;
783 }
784 return 0;
785 }
786
smb2_parse_native_symlink(char ** target,const char * buf,unsigned int len,bool relative,const char * full_path,struct cifs_sb_info * cifs_sb)787 int smb2_parse_native_symlink(char **target, const char *buf, unsigned int len,
788 bool relative,
789 const char *full_path,
790 struct cifs_sb_info *cifs_sb)
791 {
792 const char *symroot = cifs_sb->ctx->symlinkroot;
793 char sep = CIFS_DIR_SEP(cifs_sb);
794 char *linux_target = NULL;
795 char *smb_target = NULL;
796 int symlinkroot_len;
797 int abs_path_len;
798 char *abs_path;
799 int levels;
800 int rc, ulen;
801 int i;
802
803 /* Check that length it valid */
804 if (!len || (len % 2)) {
805 cifs_dbg(VFS, "srv returned malformed symlink buffer\n");
806 rc = smb_EIO1(smb_eio_trace_reparse_native_nul, len);
807 goto out;
808 }
809
810 /*
811 * Check that buffer does not contain UTF-16 null codepoint
812 * because Linux cannot process symlink with null byte.
813 */
814 ulen = UniStrnlen((wchar_t *)buf, len/2);
815 if (ulen != len/2) {
816 cifs_dbg(VFS, "srv returned null byte in native symlink target location\n");
817 rc = smb_EIO2(smb_eio_trace_reparse_native_nul, ulen, len);
818 goto out;
819 }
820
821 smb_target = cifs_strndup_from_utf16(buf, len, true, cifs_sb->local_nls);
822 if (!smb_target) {
823 rc = -ENOMEM;
824 goto out;
825 }
826
827 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS) &&
828 symroot && !relative) {
829 /*
830 * This is an absolute symlink from the server which does not
831 * support POSIX paths, so the symlink is in NT-style path.
832 * So convert it to absolute Linux symlink target path. Root of
833 * the NT-style path for symlinks is specified in "symlinkroot"
834 * mount option.
835 *
836 * Root of the DOS and Win32 paths is at NT path \??\
837 * It means that DOS/Win32 path C:\folder\file.txt is
838 * NT path \??\C:\folder\file.txt
839 *
840 * NT systems have some well-known object symlinks in their NT
841 * hierarchy, which is needed to take into account when resolving
842 * other symlinks. Most commonly used symlink paths are:
843 * \?? -> \GLOBAL??
844 * \DosDevices -> \??
845 * \GLOBAL??\GLOBALROOT -> \
846 * \GLOBAL??\Global -> \GLOBAL??
847 * \GLOBAL??\NUL -> \Device\Null
848 * \GLOBAL??\UNC -> \Device\Mup
849 * \GLOBAL??\PhysicalDrive0 -> \Device\Harddisk0\DR0 (for each harddisk)
850 * \GLOBAL??\A: -> \Device\Floppy0 (if A: is the first floppy)
851 * \GLOBAL??\C: -> \Device\HarddiskVolume1 (if C: is the first harddisk)
852 * \GLOBAL??\D: -> \Device\CdRom0 (if D: is first cdrom)
853 * \SystemRoot -> \Device\Harddisk0\Partition1\WINDOWS (or where is NT system installed)
854 * \Volume{...} -> \Device\HarddiskVolume1 (where ... is system generated guid)
855 *
856 * In most common cases, absolute NT symlinks points to path on
857 * DOS/Win32 drive letter, system-specific Volume or on UNC share.
858 * Here are few examples of commonly used absolute NT symlinks
859 * created by mklink.exe tool:
860 * \??\C:\folder\file.txt
861 * \??\\C:\folder\file.txt
862 * \??\UNC\server\share\file.txt
863 * \??\\UNC\server\share\file.txt
864 * \??\Volume{b75e2c83-0000-0000-0000-602f00000000}\folder\file.txt
865 *
866 * It means that the most common path prefix \??\ is also NT path
867 * symlink (to \GLOBAL??). It is less common that second path
868 * separator is double backslash, but it is valid.
869 *
870 * Volume guid is randomly generated by the target system and so
871 * only the target system knows the mapping between guid and the
872 * hardisk number. Over SMB it is not possible to resolve this
873 * mapping, therefore symlinks pointing to target location of
874 * volume guids are totally unusable over SMB.
875 *
876 * For now parse only symlink paths available for DOS and Win32.
877 * Those are paths with \??\ prefix or paths which points to \??\
878 * via other NT symlink (\DosDevices\, \GLOBAL??\, ...).
879 */
880 abs_path = smb_target;
881 globalroot:
882 if (strstarts(abs_path, "\\??\\"))
883 abs_path += sizeof("\\??\\")-1;
884 else if (strstarts(abs_path, "\\DosDevices\\"))
885 abs_path += sizeof("\\DosDevices\\")-1;
886 else if (strstarts(abs_path, "\\GLOBAL??\\"))
887 abs_path += sizeof("\\GLOBAL??\\")-1;
888 else
889 goto out_unhandled_target;
890
891 /* Sometimes path separator after \?? is double backslash */
892 if (abs_path[0] == '\\')
893 abs_path++;
894
895 while (strstarts(abs_path, "Global\\"))
896 abs_path += sizeof("Global\\")-1;
897
898 if (strstarts(abs_path, "GLOBALROOT\\")) {
899 /* Label globalroot requires path with leading '\\', so do not trim '\\' */
900 abs_path += sizeof("GLOBALROOT")-1;
901 goto globalroot;
902 }
903
904 /* For now parse only paths to drive letters */
905 if (((abs_path[0] >= 'A' && abs_path[0] <= 'Z') ||
906 (abs_path[0] >= 'a' && abs_path[0] <= 'z')) &&
907 abs_path[1] == ':' &&
908 (abs_path[2] == '\\' || abs_path[2] == '\0')) {
909 /* Convert drive letter to lowercase and drop colon */
910 char drive_letter = abs_path[0];
911 if (drive_letter >= 'A' && drive_letter <= 'Z')
912 drive_letter += 'a'-'A';
913 abs_path++;
914 abs_path[0] = drive_letter;
915 } else {
916 goto out_unhandled_target;
917 }
918
919 abs_path_len = strlen(abs_path)+1;
920 symlinkroot_len = strlen(symroot);
921 if (symroot[symlinkroot_len - 1] == '/')
922 symlinkroot_len--;
923 linux_target = kmalloc(symlinkroot_len + 1 + abs_path_len, GFP_KERNEL);
924 if (!linux_target) {
925 rc = -ENOMEM;
926 goto out;
927 }
928 memcpy(linux_target, symroot, symlinkroot_len);
929 linux_target[symlinkroot_len] = '/';
930 memcpy(linux_target + symlinkroot_len + 1, abs_path, abs_path_len);
931 } else if (smb_target[0] == sep && relative) {
932 /*
933 * This is a relative SMB symlink from the top of the share,
934 * which is the top level directory of the Linux mount point.
935 * Linux does not support such relative symlinks, so convert
936 * it to the relative symlink from the current directory.
937 * full_path is the SMB path to the symlink (from which is
938 * extracted current directory) and smb_target is the SMB path
939 * where symlink points, therefore full_path must always be on
940 * the SMB share.
941 */
942 int smb_target_len = strlen(smb_target)+1;
943 levels = 0;
944 for (i = 1; full_path[i]; i++) { /* i=1 to skip leading sep */
945 if (full_path[i] == sep)
946 levels++;
947 }
948 linux_target = kmalloc(levels*3 + smb_target_len, GFP_KERNEL);
949 if (!linux_target) {
950 rc = -ENOMEM;
951 goto out;
952 }
953 for (i = 0; i < levels; i++) {
954 linux_target[i*3 + 0] = '.';
955 linux_target[i*3 + 1] = '.';
956 linux_target[i*3 + 2] = sep;
957 }
958 memcpy(linux_target + levels*3, smb_target+1, smb_target_len); /* +1 to skip leading sep */
959 } else {
960 /*
961 * This is either an absolute symlink in POSIX-style format
962 * or relative SMB symlink from the current directory.
963 * These paths have same format as Linux symlinks, so no
964 * conversion is needed.
965 */
966 out_unhandled_target:
967 linux_target = smb_target;
968 smb_target = NULL;
969 }
970
971 if (sep == '\\')
972 convert_delimiter(linux_target, '/');
973
974 rc = 0;
975 *target = linux_target;
976
977 cifs_dbg(FYI, "%s: symlink target: %s\n", __func__, *target);
978
979 out:
980 if (rc != 0)
981 kfree(linux_target);
982 kfree(smb_target);
983 return rc;
984 }
985
parse_reparse_native_symlink(struct reparse_symlink_data_buffer * sym,u32 plen,struct cifs_sb_info * cifs_sb,const char * full_path,struct cifs_open_info_data * data)986 static int parse_reparse_native_symlink(struct reparse_symlink_data_buffer *sym,
987 u32 plen,
988 struct cifs_sb_info *cifs_sb,
989 const char *full_path,
990 struct cifs_open_info_data *data)
991 {
992 unsigned int len;
993 unsigned int offs;
994
995 /* We handle Symbolic Link reparse tag here. See: MS-FSCC 2.1.2.4 */
996
997 offs = le16_to_cpu(sym->SubstituteNameOffset);
998 len = le16_to_cpu(sym->SubstituteNameLength);
999 if (offs + 20 > plen || offs + len + 20 > plen) {
1000 cifs_dbg(VFS, "srv returned malformed symlink buffer\n");
1001 return smb_EIO2(smb_eio_trace_reparse_native_sym_len,
1002 offs << 16 | len, plen);
1003 }
1004
1005 return smb2_parse_native_symlink(&data->symlink_target,
1006 sym->PathBuffer + offs,
1007 len,
1008 le32_to_cpu(sym->Flags) & SYMLINK_FLAG_RELATIVE,
1009 full_path,
1010 cifs_sb);
1011 }
1012
parse_reparse_wsl_symlink(struct reparse_wsl_symlink_data_buffer * buf,struct cifs_sb_info * cifs_sb,struct cifs_open_info_data * data)1013 static int parse_reparse_wsl_symlink(struct reparse_wsl_symlink_data_buffer *buf,
1014 struct cifs_sb_info *cifs_sb,
1015 struct cifs_open_info_data *data)
1016 {
1017 int len = le16_to_cpu(buf->ReparseDataLength);
1018 int data_offset = offsetof(typeof(*buf), Target) - offsetof(typeof(*buf), Version);
1019 int symname_utf8_len;
1020 __le16 *symname_utf16;
1021 int symname_utf16_len;
1022
1023 if (len <= data_offset) {
1024 cifs_dbg(VFS, "srv returned malformed wsl symlink buffer\n");
1025 return smb_EIO2(smb_eio_trace_reparse_wsl_symbuf,
1026 len, data_offset);
1027 }
1028
1029 /* MS-FSCC 2.1.2.7 defines layout of the Target field only for Version 2. */
1030 u32 version = le32_to_cpu(buf->Version);
1031
1032 if (version != 2) {
1033 cifs_dbg(VFS, "srv returned unsupported wsl symlink version %u\n", version);
1034 return smb_EIO1(smb_eio_trace_reparse_wsl_ver, version);
1035 }
1036
1037 /* Target for Version 2 is in UTF-8 but without trailing null-term byte */
1038 symname_utf8_len = len - data_offset;
1039 /*
1040 * Check that buffer does not contain null byte
1041 * because Linux cannot process symlink with null byte.
1042 */
1043 size_t ulen = strnlen(buf->Target, symname_utf8_len);
1044
1045 if (ulen != symname_utf8_len) {
1046 cifs_dbg(VFS, "srv returned null byte in wsl symlink target location\n");
1047 return smb_EIO2(smb_eio_trace_reparse_wsl_ver,
1048 ulen, symname_utf8_len);
1049 }
1050 symname_utf16 = kzalloc(symname_utf8_len * 2, GFP_KERNEL);
1051 if (!symname_utf16)
1052 return -ENOMEM;
1053 symname_utf16_len = utf8s_to_utf16s(buf->Target, symname_utf8_len,
1054 UTF16_LITTLE_ENDIAN,
1055 (wchar_t *) symname_utf16, symname_utf8_len * 2);
1056 if (symname_utf16_len < 0) {
1057 kfree(symname_utf16);
1058 return symname_utf16_len;
1059 }
1060 symname_utf16_len *= 2; /* utf8s_to_utf16s() returns number of u16 items, not byte length */
1061
1062 data->symlink_target = cifs_strndup_from_utf16((u8 *)symname_utf16,
1063 symname_utf16_len, true,
1064 cifs_sb->local_nls);
1065 kfree(symname_utf16);
1066 if (!data->symlink_target)
1067 return -ENOMEM;
1068
1069 return 0;
1070 }
1071
parse_reparse_point(struct reparse_data_buffer * buf,u32 plen,struct cifs_sb_info * cifs_sb,const char * full_path,struct cifs_open_info_data * data)1072 int parse_reparse_point(struct reparse_data_buffer *buf,
1073 u32 plen, struct cifs_sb_info *cifs_sb,
1074 const char *full_path,
1075 struct cifs_open_info_data *data)
1076 {
1077 data->reparse.buf = buf;
1078
1079 /* See MS-FSCC 2.1.2 */
1080 switch (le32_to_cpu(buf->ReparseTag)) {
1081 case IO_REPARSE_TAG_NFS:
1082 return parse_reparse_nfs((struct reparse_nfs_data_buffer *)buf,
1083 cifs_sb, data);
1084 case IO_REPARSE_TAG_SYMLINK:
1085 return parse_reparse_native_symlink(
1086 (struct reparse_symlink_data_buffer *)buf,
1087 plen, cifs_sb, full_path, data);
1088 case IO_REPARSE_TAG_LX_SYMLINK:
1089 return parse_reparse_wsl_symlink(
1090 (struct reparse_wsl_symlink_data_buffer *)buf,
1091 cifs_sb, data);
1092 case IO_REPARSE_TAG_AF_UNIX:
1093 case IO_REPARSE_TAG_LX_FIFO:
1094 case IO_REPARSE_TAG_LX_CHR:
1095 case IO_REPARSE_TAG_LX_BLK: {
1096 u16 dlen = le16_to_cpu(buf->ReparseDataLength);
1097
1098 if (dlen != 0) {
1099 u32 rtag = le32_to_cpu(buf->ReparseTag);
1100 cifs_dbg(VFS, "srv returned malformed buffer for reparse point: 0x%08x\n",
1101 rtag);
1102 return smb_EIO2(smb_eio_trace_reparse_data_len, dlen, rtag);
1103 }
1104 return 0;
1105 }
1106 default:
1107 return -EOPNOTSUPP;
1108 }
1109 }
1110
smb2_get_reparse_point_buffer(const struct kvec * rsp_iov,u32 * plen)1111 struct reparse_data_buffer *smb2_get_reparse_point_buffer(const struct kvec *rsp_iov,
1112 u32 *plen)
1113 {
1114 struct smb2_ioctl_rsp *io = rsp_iov->iov_base;
1115 *plen = le32_to_cpu(io->OutputCount);
1116 return (struct reparse_data_buffer *)((u8 *)io +
1117 le32_to_cpu(io->OutputOffset));
1118 }
1119
wsl_to_fattr(struct cifs_open_info_data * data,struct cifs_sb_info * cifs_sb,u32 tag,struct cifs_fattr * fattr)1120 static bool wsl_to_fattr(struct cifs_open_info_data *data,
1121 struct cifs_sb_info *cifs_sb,
1122 u32 tag, struct cifs_fattr *fattr)
1123 {
1124 struct smb2_file_full_ea_info *ea;
1125 bool have_xattr_dev = false;
1126 u32 next = 0;
1127
1128 switch (tag) {
1129 case IO_REPARSE_TAG_LX_SYMLINK:
1130 fattr->cf_mode |= S_IFLNK;
1131 break;
1132 case IO_REPARSE_TAG_LX_FIFO:
1133 fattr->cf_mode |= S_IFIFO;
1134 break;
1135 case IO_REPARSE_TAG_AF_UNIX:
1136 fattr->cf_mode |= S_IFSOCK;
1137 break;
1138 case IO_REPARSE_TAG_LX_CHR:
1139 fattr->cf_mode |= S_IFCHR;
1140 break;
1141 case IO_REPARSE_TAG_LX_BLK:
1142 fattr->cf_mode |= S_IFBLK;
1143 break;
1144 }
1145
1146 if (!data->wsl.eas_len)
1147 goto out;
1148
1149 ea = (struct smb2_file_full_ea_info *)data->wsl.eas;
1150 do {
1151 const char *name;
1152 void *v;
1153 u8 nlen;
1154
1155 ea = (void *)((u8 *)ea + next);
1156 next = le32_to_cpu(ea->next_entry_offset);
1157 if (!le16_to_cpu(ea->ea_value_length))
1158 continue;
1159
1160 name = ea->ea_data;
1161 nlen = ea->ea_name_length;
1162 v = (void *)((u8 *)ea->ea_data + ea->ea_name_length + 1);
1163
1164 if (!strncmp(name, SMB2_WSL_XATTR_UID, nlen))
1165 fattr->cf_uid = wsl_make_kuid(cifs_sb, v);
1166 else if (!strncmp(name, SMB2_WSL_XATTR_GID, nlen))
1167 fattr->cf_gid = wsl_make_kgid(cifs_sb, v);
1168 else if (!strncmp(name, SMB2_WSL_XATTR_MODE, nlen)) {
1169 /* File type in reparse point tag and in xattr mode must match. */
1170 if (S_DT(fattr->cf_mode) != S_DT(le32_to_cpu(*(__le32 *)v)))
1171 return false;
1172 fattr->cf_mode = (umode_t)le32_to_cpu(*(__le32 *)v);
1173 } else if (!strncmp(name, SMB2_WSL_XATTR_DEV, nlen)) {
1174 fattr->cf_rdev = reparse_mkdev(v);
1175 have_xattr_dev = true;
1176 }
1177 } while (next);
1178 out:
1179
1180 /* Major and minor numbers for char and block devices are mandatory. */
1181 if (!have_xattr_dev && (tag == IO_REPARSE_TAG_LX_CHR || tag == IO_REPARSE_TAG_LX_BLK))
1182 return false;
1183
1184 return true;
1185 }
1186
posix_reparse_to_fattr(struct cifs_sb_info * cifs_sb,struct cifs_fattr * fattr,struct cifs_open_info_data * data)1187 static bool posix_reparse_to_fattr(struct cifs_sb_info *cifs_sb,
1188 struct cifs_fattr *fattr,
1189 struct cifs_open_info_data *data)
1190 {
1191 struct reparse_nfs_data_buffer *buf = (struct reparse_nfs_data_buffer *)data->reparse.buf;
1192
1193 if (buf == NULL)
1194 return true;
1195
1196 if (le16_to_cpu(buf->ReparseDataLength) < sizeof(buf->InodeType)) {
1197 WARN_ON_ONCE(1);
1198 return false;
1199 }
1200
1201 switch (le64_to_cpu(buf->InodeType)) {
1202 case NFS_SPECFILE_CHR:
1203 if (le16_to_cpu(buf->ReparseDataLength) != sizeof(buf->InodeType) + 8) {
1204 WARN_ON_ONCE(1);
1205 return false;
1206 }
1207 fattr->cf_mode |= S_IFCHR;
1208 fattr->cf_rdev = reparse_mkdev(buf->DataBuffer);
1209 break;
1210 case NFS_SPECFILE_BLK:
1211 if (le16_to_cpu(buf->ReparseDataLength) != sizeof(buf->InodeType) + 8) {
1212 WARN_ON_ONCE(1);
1213 return false;
1214 }
1215 fattr->cf_mode |= S_IFBLK;
1216 fattr->cf_rdev = reparse_mkdev(buf->DataBuffer);
1217 break;
1218 case NFS_SPECFILE_FIFO:
1219 fattr->cf_mode |= S_IFIFO;
1220 break;
1221 case NFS_SPECFILE_SOCK:
1222 fattr->cf_mode |= S_IFSOCK;
1223 break;
1224 case NFS_SPECFILE_LNK:
1225 fattr->cf_mode |= S_IFLNK;
1226 break;
1227 default:
1228 WARN_ON_ONCE(1);
1229 return false;
1230 }
1231 return true;
1232 }
1233
cifs_reparse_point_to_fattr(struct cifs_sb_info * cifs_sb,struct cifs_fattr * fattr,struct cifs_open_info_data * data)1234 bool cifs_reparse_point_to_fattr(struct cifs_sb_info *cifs_sb,
1235 struct cifs_fattr *fattr,
1236 struct cifs_open_info_data *data)
1237 {
1238 u32 tag = data->reparse.tag;
1239 bool ok;
1240
1241 switch (tag) {
1242 case IO_REPARSE_TAG_LX_SYMLINK:
1243 case IO_REPARSE_TAG_LX_FIFO:
1244 case IO_REPARSE_TAG_AF_UNIX:
1245 case IO_REPARSE_TAG_LX_CHR:
1246 case IO_REPARSE_TAG_LX_BLK:
1247 ok = wsl_to_fattr(data, cifs_sb, tag, fattr);
1248 if (!ok)
1249 return false;
1250 break;
1251 case IO_REPARSE_TAG_NFS:
1252 ok = posix_reparse_to_fattr(cifs_sb, fattr, data);
1253 if (!ok)
1254 return false;
1255 break;
1256 case 0: /* SMB1 symlink */
1257 case IO_REPARSE_TAG_SYMLINK:
1258 fattr->cf_mode |= S_IFLNK;
1259 break;
1260 default:
1261 if (!(fattr->cf_cifsattrs & ATTR_DIRECTORY))
1262 return false;
1263 if (!IS_REPARSE_TAG_NAME_SURROGATE(tag) &&
1264 tag != IO_REPARSE_TAG_INTERNAL)
1265 return false;
1266 /* See cifs_create_junction_fattr() */
1267 fattr->cf_mode = S_IFDIR | 0711;
1268 break;
1269 }
1270
1271 fattr->cf_dtype = S_DT(fattr->cf_mode);
1272 return true;
1273 }
1274