1 // SPDX-License-Identifier: GPL-2.0-only
7 #include <linux/stat.h>
8 #include <linux/slab.h>
10 #include "smb2proto.h"
11 #include "cifsproto.h"
12 #include "cifs_unicode.h"
13 #include "cifs_debug.h"
14 #include "fs_context.h"
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,
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,
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);
31 static int detect_directory_symlink_target(struct cifs_sb_info *cifs_sb,
32 const unsigned int xid,
33 const char *full_path,
37 int smb2_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)
41 switch (get_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);
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)
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);
64 char *symlink_target = NULL;
66 char sep = CIFS_DIR_SEP(cifs_sb);
67 u16 len, plen, poff, slen;
70 if (strlen(symname) > REPARSE_SYM_PATH_MAX)
73 symlink_target = kstrdup(symname, GFP_KERNEL);
74 if (!symlink_target) {
79 data = (struct cifs_open_info_data) {
80 .reparse_point = true,
81 .reparse = { .tag = IO_REPARSE_TAG_SYMLINK, },
82 .symlink_target = symlink_target,
85 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS) && symname[0] == '/') {
87 * This is a request to create an absolute symlink on the server
88 * which does not support POSIX paths, and expects symlink in
89 * NT-style path. So convert absolute Linux symlink target path
90 * to the absolute NT-style path. Root of the NT-style path for
91 * symlinks is specified in "symlinkroot" mount option. This will
92 * ensure compatibility of this symlink stored in absolute form
95 if (!strstarts(symname, cifs_sb->ctx->symlinkroot)) {
97 * If the absolute Linux symlink target path is not
98 * inside "symlinkroot" location then there is no way
99 * to convert such Linux symlink to NT-style path.
102 "absolute symlink '%s' cannot be converted to NT format "
103 "because it is outside of symlinkroot='%s'\n",
104 symname, cifs_sb->ctx->symlinkroot);
108 len = strlen(cifs_sb->ctx->symlinkroot);
109 if (cifs_sb->ctx->symlinkroot[len-1] != '/')
111 if (symname[len] >= 'a' && symname[len] <= 'z' &&
112 (symname[len+1] == '/' || symname[len+1] == '\0')) {
114 * Symlink points to Linux target /symlinkroot/x/path/...
115 * where 'x' is the lowercase local Windows drive.
116 * NT-style path for 'x' has common form \??\X:\path\...
117 * with uppercase local Windows drive.
119 int common_path_len = strlen(symname+len+1)+1;
120 sym = kzalloc(6+common_path_len, GFP_KERNEL);
125 memcpy(sym, "\\??\\", 4);
126 sym[4] = symname[len] - ('a'-'A');
128 memcpy(sym+6, symname+len+1, common_path_len);
130 /* Unhandled absolute symlink. Report an error. */
133 "absolute symlink '%s' cannot be converted to NT format "
134 "because it points to unknown target\n",
141 * This is request to either create an absolute symlink on
142 * server which expects POSIX paths or it is an request to
143 * create a relative symlink from the current directory.
144 * These paths have same format as relative SMB symlinks,
145 * so no conversion is needed. So just take symname as-is.
147 sym = kstrdup(symname, GFP_KERNEL);
155 convert_delimiter(sym, sep);
158 * For absolute NT symlinks it is required to pass also leading
159 * backslash and to not mangle NT object prefix "\\??\\" and not to
160 * mangle colon in drive letter. But cifs_convert_path_to_utf16()
161 * removes leading backslash and replaces '?' and ':'. So temporary
162 * mask these characters in NT object prefix by '_' and then change
165 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS) && symname[0] == '/')
166 sym[0] = sym[1] = sym[2] = sym[5] = '_';
168 path = cifs_convert_path_to_utf16(sym, cifs_sb);
174 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS) && symname[0] == '/') {
176 sym[1] = sym[2] = '?';
178 path[0] = cpu_to_le16('\\');
179 path[1] = path[2] = cpu_to_le16('?');
180 path[5] = cpu_to_le16(':');
184 * SMB distinguish between symlink to directory and symlink to file.
185 * They cannot be exchanged (symlink of file type which points to
186 * directory cannot be resolved and vice-versa). Try to detect if
187 * the symlink target could be a directory or not. When detection
188 * fails then treat symlink as a file (non-directory) symlink.
191 rc = detect_directory_symlink_target(cifs_sb, xid, full_path, symname, &directory);
195 slen = 2 * UniStrnlen((wchar_t *)path, REPARSE_SYM_PATH_MAX);
198 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS) && symname[0] == '/') {
200 * For absolute NT symlinks skip leading "\\??\\" in PrintName as
201 * PrintName is user visible location in DOS/Win32 format (not in NT format).
206 len = sizeof(*buf) + plen + slen;
207 buf = kzalloc(len, GFP_KERNEL);
213 buf->ReparseTag = cpu_to_le32(IO_REPARSE_TAG_SYMLINK);
214 buf->ReparseDataLength = cpu_to_le16(len - sizeof(struct reparse_data_buffer));
216 buf->SubstituteNameOffset = cpu_to_le16(plen);
217 buf->SubstituteNameLength = cpu_to_le16(slen);
218 memcpy(&buf->PathBuffer[plen], path, slen);
220 buf->PrintNameOffset = 0;
221 buf->PrintNameLength = cpu_to_le16(plen);
222 memcpy(buf->PathBuffer, path+poff, plen);
224 buf->Flags = cpu_to_le32(*symname != '/' ? SYMLINK_FLAG_RELATIVE : 0);
228 new = smb2_get_reparse_inode(&data, inode->i_sb, xid,
229 tcon, full_path, directory,
232 d_instantiate(dentry, new);
238 cifs_free_open_info(&data);
243 static int detect_directory_symlink_target(struct cifs_sb_info *cifs_sb,
244 const unsigned int xid,
245 const char *full_path,
249 char sep = CIFS_DIR_SEP(cifs_sb);
250 struct cifs_open_parms oparms;
251 struct tcon_link *tlink;
252 struct cifs_tcon *tcon;
253 const char *basename;
264 * First do some simple check. If the original Linux symlink target ends
265 * with slash, or last path component is dot or dot-dot then it is for
266 * sure symlink to the directory.
268 basename = kbasename(symname);
269 basename_len = strlen(basename);
270 if (basename_len == 0 || /* symname ends with slash */
271 (basename_len == 1 && basename[0] == '.') || /* last component is "." */
272 (basename_len == 2 && basename[0] == '.' && basename[1] == '.')) { /* or ".." */
278 * For absolute symlinks it is not possible to determinate
279 * if it should point to directory or file.
281 if (symname[0] == '/') {
283 "%s: cannot determinate if the symlink target path '%s' "
284 "is directory or not, creating '%s' as file symlink\n",
285 __func__, symname, full_path);
290 * If it was not detected as directory yet and the symlink is relative
291 * then try to resolve the path on the SMB server, check if the path
292 * exists and determinate if it is a directory or not.
295 full_path_len = strlen(full_path);
296 symname_len = strlen(symname);
298 tlink = cifs_sb_tlink(cifs_sb);
300 return PTR_ERR(tlink);
302 resolved_path = kzalloc(full_path_len + symname_len + 1, GFP_KERNEL);
303 if (!resolved_path) {
304 cifs_put_tlink(tlink);
309 * Compose the resolved SMB symlink path from the SMB full path
310 * and Linux target symlink path.
312 memcpy(resolved_path, full_path, full_path_len+1);
313 path_sep = strrchr(resolved_path, sep);
317 path_sep = resolved_path;
318 memcpy(path_sep, symname, symname_len+1);
320 convert_delimiter(path_sep, sep);
322 tcon = tlink_tcon(tlink);
323 oparms = CIFS_OPARMS(cifs_sb, tcon, resolved_path,
324 FILE_READ_ATTRIBUTES, FILE_OPEN, 0, ACL_NO_MODE);
327 /* Try to open as a directory (NOT_FILE) */
329 oparms.create_options = cifs_create_options(cifs_sb,
330 CREATE_NOT_FILE | OPEN_REPARSE_POINT);
331 open_rc = tcon->ses->server->ops->open(xid, &oparms, &oplock, NULL);
333 /* Successful open means that the target path is definitely a directory. */
335 tcon->ses->server->ops->close(xid, tcon, &fid);
336 } else if (open_rc == -ENOTDIR) {
337 /* -ENOTDIR means that the target path is definitely a file. */
339 } else if (open_rc == -ENOENT) {
340 /* -ENOENT means that the target path does not exist. */
342 "%s: symlink target path '%s' does not exist, "
343 "creating '%s' as file symlink\n",
344 __func__, symname, full_path);
346 /* Try to open as a file (NOT_DIR) */
348 oparms.create_options = cifs_create_options(cifs_sb,
349 CREATE_NOT_DIR | OPEN_REPARSE_POINT);
350 open_rc = tcon->ses->server->ops->open(xid, &oparms, &oplock, NULL);
352 /* Successful open means that the target path is definitely a file. */
354 tcon->ses->server->ops->close(xid, tcon, &fid);
355 } else if (open_rc == -EISDIR) {
356 /* -EISDIR means that the target path is definitely a directory. */
360 * This code branch is called when we do not have a permission to
361 * open the resolved_path or some other client/process denied
362 * opening the resolved_path.
364 * TODO: Try to use ops->query_dir_first on the parent directory
365 * of resolved_path, search for basename of resolved_path and
366 * check if the ATTR_DIRECTORY is set in fi.Attributes. In some
367 * case this could work also when opening of the path is denied.
370 "%s: cannot determinate if the symlink target path '%s' "
371 "is directory or not, creating '%s' as file symlink\n",
372 __func__, symname, full_path);
376 kfree(resolved_path);
377 cifs_put_tlink(tlink);
381 static int create_native_socket(const unsigned int xid, struct inode *inode,
382 struct dentry *dentry, struct cifs_tcon *tcon,
383 const char *full_path)
385 struct reparse_data_buffer buf = {
386 .ReparseTag = cpu_to_le32(IO_REPARSE_TAG_AF_UNIX),
387 .ReparseDataLength = cpu_to_le16(0),
389 struct cifs_open_info_data data = {
390 .reparse_point = true,
391 .reparse = { .tag = IO_REPARSE_TAG_AF_UNIX, .buf = &buf, },
395 .iov_len = sizeof(buf),
400 new = smb2_get_reparse_inode(&data, inode->i_sb, xid,
401 tcon, full_path, false, &iov, NULL);
403 d_instantiate(dentry, new);
406 cifs_free_open_info(&data);
410 static int nfs_set_reparse_buf(struct reparse_nfs_data_buffer *buf,
411 mode_t mode, dev_t dev,
412 __le16 *symname_utf16,
413 int symname_utf16_len,
421 switch ((type = reparse_mode_nfs_type(mode))) {
422 case NFS_SPECFILE_BLK:
423 case NFS_SPECFILE_CHR:
424 dlen = 2 * sizeof(__le32);
425 ((__le32 *)buf->DataBuffer)[0] = cpu_to_le32(MAJOR(dev));
426 ((__le32 *)buf->DataBuffer)[1] = cpu_to_le32(MINOR(dev));
428 case NFS_SPECFILE_LNK:
429 dlen = symname_utf16_len;
430 memcpy(buf->DataBuffer, symname_utf16, symname_utf16_len);
432 case NFS_SPECFILE_FIFO:
433 case NFS_SPECFILE_SOCK:
440 buf->ReparseTag = cpu_to_le32(IO_REPARSE_TAG_NFS);
442 buf->InodeType = cpu_to_le64(type);
443 buf->ReparseDataLength = cpu_to_le16(len + dlen -
444 sizeof(struct reparse_data_buffer));
446 iov->iov_len = len + dlen;
450 static int mknod_nfs(unsigned int xid, struct inode *inode,
451 struct dentry *dentry, struct cifs_tcon *tcon,
452 const char *full_path, umode_t mode, dev_t dev,
455 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
456 struct cifs_open_info_data data;
457 struct reparse_nfs_data_buffer *p = NULL;
458 __le16 *symname_utf16 = NULL;
459 int symname_utf16_len = 0;
462 __u8 buf[sizeof(*p) + sizeof(__le64)];
466 symname_utf16 = cifs_strndup_to_utf16(symname, strlen(symname),
470 if (!symname_utf16) {
474 symname_utf16_len -= 2; /* symlink is without trailing wide-nul */
475 p = kzalloc(sizeof(*p) + symname_utf16_len, GFP_KERNEL);
481 p = (struct reparse_nfs_data_buffer *)buf;
483 rc = nfs_set_reparse_buf(p, mode, dev, symname_utf16, symname_utf16_len, &iov);
487 data = (struct cifs_open_info_data) {
488 .reparse_point = true,
489 .reparse = { .tag = IO_REPARSE_TAG_NFS, .buf = (struct reparse_data_buffer *)p, },
490 .symlink_target = kstrdup(symname, GFP_KERNEL),
493 new = smb2_get_reparse_inode(&data, inode->i_sb, xid,
494 tcon, full_path, false, &iov, NULL);
496 d_instantiate(dentry, new);
499 cifs_free_open_info(&data);
502 kfree(symname_utf16);
508 static int wsl_set_reparse_buf(struct reparse_data_buffer **buf,
509 mode_t mode, const char *symname,
510 struct cifs_sb_info *cifs_sb,
513 struct reparse_wsl_symlink_data_buffer *symlink_buf;
514 __le16 *symname_utf16;
515 int symname_utf16_len;
516 int symname_utf8_maxlen;
517 int symname_utf8_len;
521 switch ((tag = reparse_mode_wsl_tag(mode))) {
522 case IO_REPARSE_TAG_LX_BLK:
523 case IO_REPARSE_TAG_LX_CHR:
524 case IO_REPARSE_TAG_LX_FIFO:
525 case IO_REPARSE_TAG_AF_UNIX:
526 buf_len = sizeof(struct reparse_data_buffer);
527 *buf = kzalloc(buf_len, GFP_KERNEL);
531 case IO_REPARSE_TAG_LX_SYMLINK:
532 symname_utf16 = cifs_strndup_to_utf16(symname, strlen(symname),
538 symname_utf8_maxlen = symname_utf16_len/2*3;
539 symlink_buf = kzalloc(sizeof(struct reparse_wsl_symlink_data_buffer) +
540 symname_utf8_maxlen, GFP_KERNEL);
542 kfree(symname_utf16);
545 /* Flag 0x02000000 is unknown, but all wsl symlinks have this value */
546 symlink_buf->Flags = cpu_to_le32(0x02000000);
547 /* PathBuffer is in UTF-8 but without trailing null-term byte */
548 symname_utf8_len = utf16s_to_utf8s((wchar_t *)symname_utf16, symname_utf16_len/2,
550 symlink_buf->PathBuffer,
551 symname_utf8_maxlen);
552 *buf = (struct reparse_data_buffer *)symlink_buf;
553 buf_len = sizeof(struct reparse_wsl_symlink_data_buffer) + symname_utf8_len;
554 kfree(symname_utf16);
560 (*buf)->ReparseTag = cpu_to_le32(tag);
561 (*buf)->Reserved = 0;
562 (*buf)->ReparseDataLength = cpu_to_le16(buf_len - sizeof(struct reparse_data_buffer));
563 iov->iov_base = *buf;
564 iov->iov_len = buf_len;
568 static struct smb2_create_ea_ctx *ea_create_context(u32 dlen, size_t *cc_len)
570 struct smb2_create_ea_ctx *cc;
572 *cc_len = round_up(sizeof(*cc) + dlen, 8);
573 cc = kzalloc(*cc_len, GFP_KERNEL);
575 return ERR_PTR(-ENOMEM);
577 cc->ctx.NameOffset = cpu_to_le16(offsetof(struct smb2_create_ea_ctx,
579 cc->ctx.NameLength = cpu_to_le16(4);
580 memcpy(cc->name, SMB2_CREATE_EA_BUFFER, strlen(SMB2_CREATE_EA_BUFFER));
581 cc->ctx.DataOffset = cpu_to_le16(offsetof(struct smb2_create_ea_ctx, ea));
582 cc->ctx.DataLength = cpu_to_le32(dlen);
593 static int wsl_set_xattrs(struct inode *inode, umode_t _mode,
594 dev_t _dev, struct kvec *iov)
596 struct smb2_file_full_ea_info *ea;
597 struct smb2_create_ea_ctx *cc;
598 struct smb3_fs_context *ctx = CIFS_SB(inode->i_sb)->ctx;
599 __le64 uid = cpu_to_le64(from_kuid(current_user_ns(), ctx->linux_uid));
600 __le64 gid = cpu_to_le64(from_kgid(current_user_ns(), ctx->linux_gid));
601 __le64 dev = cpu_to_le64(((u64)MINOR(_dev) << 32) | MAJOR(_dev));
602 __le64 mode = cpu_to_le64(_mode);
603 struct wsl_xattr xattrs[] = {
604 { .name = SMB2_WSL_XATTR_UID, .value = uid, .size = SMB2_WSL_XATTR_UID_SIZE, },
605 { .name = SMB2_WSL_XATTR_GID, .value = gid, .size = SMB2_WSL_XATTR_GID_SIZE, },
606 { .name = SMB2_WSL_XATTR_MODE, .value = mode, .size = SMB2_WSL_XATTR_MODE_SIZE, },
607 { .name = SMB2_WSL_XATTR_DEV, .value = dev, .size = SMB2_WSL_XATTR_DEV_SIZE, },
610 u32 dlen = 0, next = 0;
612 u8 name_size = SMB2_WSL_XATTR_NAME_LEN + 1;
614 memset(iov, 0, sizeof(*iov));
616 /* Exclude $LXDEV xattr for non-device files */
617 if (!S_ISBLK(_mode) && !S_ISCHR(_mode))
618 num_xattrs = ARRAY_SIZE(xattrs) - 1;
620 num_xattrs = ARRAY_SIZE(xattrs);
622 for (i = 0; i < num_xattrs; i++) {
623 xattrs[i].next = ALIGN(sizeof(*ea) + name_size +
625 dlen += xattrs[i].next;
628 cc = ea_create_context(dlen, &cc_len);
633 for (i = 0; i < num_xattrs; i++) {
634 ea = (void *)((u8 *)ea + next);
635 next = xattrs[i].next;
636 ea->next_entry_offset = cpu_to_le32(next);
638 ea->ea_name_length = name_size - 1;
639 ea->ea_value_length = cpu_to_le16(xattrs[i].size);
640 memcpy(ea->ea_data, xattrs[i].name, name_size);
641 memcpy(&ea->ea_data[name_size],
642 &xattrs[i].value, xattrs[i].size);
644 ea->next_entry_offset = 0;
647 iov->iov_len = cc_len;
651 static int mknod_wsl(unsigned int xid, struct inode *inode,
652 struct dentry *dentry, struct cifs_tcon *tcon,
653 const char *full_path, umode_t mode, dev_t dev,
656 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
657 struct cifs_open_info_data data;
658 struct reparse_data_buffer *buf;
659 struct smb2_create_ea_ctx *cc;
662 struct kvec reparse_iov, xattr_iov;
665 rc = wsl_set_reparse_buf(&buf, mode, symname, cifs_sb, &reparse_iov);
669 rc = wsl_set_xattrs(inode, mode, dev, &xattr_iov);
675 data = (struct cifs_open_info_data) {
676 .reparse_point = true,
677 .reparse = { .tag = le32_to_cpu(buf->ReparseTag), .buf = buf, },
678 .symlink_target = kstrdup(symname, GFP_KERNEL),
681 cc = xattr_iov.iov_base;
682 len = le32_to_cpu(cc->ctx.DataLength);
683 memcpy(data.wsl.eas, &cc->ea, len);
684 data.wsl.eas_len = len;
686 new = smb2_get_reparse_inode(&data, inode->i_sb,
687 xid, tcon, full_path, false,
688 &reparse_iov, &xattr_iov);
690 d_instantiate(dentry, new);
693 cifs_free_open_info(&data);
694 kfree(xattr_iov.iov_base);
699 int smb2_mknod_reparse(unsigned int xid, struct inode *inode,
700 struct dentry *dentry, struct cifs_tcon *tcon,
701 const char *full_path, umode_t mode, dev_t dev)
703 struct smb3_fs_context *ctx = CIFS_SB(inode->i_sb)->ctx;
705 if (S_ISSOCK(mode) && !ctx->nonativesocket && ctx->reparse_type != CIFS_REPARSE_TYPE_NONE)
706 return create_native_socket(xid, inode, dentry, tcon, full_path);
708 switch (ctx->reparse_type) {
709 case CIFS_REPARSE_TYPE_NFS:
710 return mknod_nfs(xid, inode, dentry, tcon, full_path, mode, dev, NULL);
711 case CIFS_REPARSE_TYPE_WSL:
712 return mknod_wsl(xid, inode, dentry, tcon, full_path, mode, dev, NULL);
718 /* See MS-FSCC 2.1.2.6 for the 'NFS' style reparse tags */
719 static int parse_reparse_nfs(struct reparse_nfs_data_buffer *buf,
720 struct cifs_sb_info *cifs_sb,
721 struct cifs_open_info_data *data)
726 len = le16_to_cpu(buf->ReparseDataLength);
727 if (len < sizeof(buf->InodeType)) {
728 cifs_dbg(VFS, "srv returned malformed nfs buffer\n");
732 len -= sizeof(buf->InodeType);
734 switch ((type = le64_to_cpu(buf->InodeType))) {
735 case NFS_SPECFILE_LNK:
736 if (len == 0 || (len % 2)) {
737 cifs_dbg(VFS, "srv returned malformed nfs symlink buffer\n");
741 * Check that buffer does not contain UTF-16 null codepoint
742 * because Linux cannot process symlink with null byte.
744 if (UniStrnlen((wchar_t *)buf->DataBuffer, len/2) != len/2) {
745 cifs_dbg(VFS, "srv returned null byte in nfs symlink target location\n");
748 data->symlink_target = cifs_strndup_from_utf16(buf->DataBuffer,
751 if (!data->symlink_target)
753 cifs_dbg(FYI, "%s: target path: %s\n",
754 __func__, data->symlink_target);
756 case NFS_SPECFILE_CHR:
757 case NFS_SPECFILE_BLK:
758 /* DataBuffer for block and char devices contains two 32-bit numbers */
760 cifs_dbg(VFS, "srv returned malformed nfs buffer for type: 0x%llx\n", type);
764 case NFS_SPECFILE_FIFO:
765 case NFS_SPECFILE_SOCK:
766 /* DataBuffer for fifos and sockets is empty */
768 cifs_dbg(VFS, "srv returned malformed nfs buffer for type: 0x%llx\n", type);
773 cifs_dbg(VFS, "%s: unhandled inode type: 0x%llx\n",
780 int smb2_parse_native_symlink(char **target, const char *buf, unsigned int len,
782 const char *full_path,
783 struct cifs_sb_info *cifs_sb)
785 char sep = CIFS_DIR_SEP(cifs_sb);
786 char *linux_target = NULL;
787 char *smb_target = NULL;
795 /* Check that length it valid */
796 if (!len || (len % 2)) {
797 cifs_dbg(VFS, "srv returned malformed symlink buffer\n");
803 * Check that buffer does not contain UTF-16 null codepoint
804 * because Linux cannot process symlink with null byte.
806 if (UniStrnlen((wchar_t *)buf, len/2) != len/2) {
807 cifs_dbg(VFS, "srv returned null byte in native symlink target location\n");
812 smb_target = cifs_strndup_from_utf16(buf, len, true, cifs_sb->local_nls);
818 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS) && !relative) {
820 * This is an absolute symlink from the server which does not
821 * support POSIX paths, so the symlink is in NT-style path.
822 * So convert it to absolute Linux symlink target path. Root of
823 * the NT-style path for symlinks is specified in "symlinkroot"
826 * Root of the DOS and Win32 paths is at NT path \??\
827 * It means that DOS/Win32 path C:\folder\file.txt is
828 * NT path \??\C:\folder\file.txt
830 * NT systems have some well-known object symlinks in their NT
831 * hierarchy, which is needed to take into account when resolving
832 * other symlinks. Most commonly used symlink paths are:
835 * \GLOBAL??\GLOBALROOT -> \
836 * \GLOBAL??\Global -> \GLOBAL??
837 * \GLOBAL??\NUL -> \Device\Null
838 * \GLOBAL??\UNC -> \Device\Mup
839 * \GLOBAL??\PhysicalDrive0 -> \Device\Harddisk0\DR0 (for each harddisk)
840 * \GLOBAL??\A: -> \Device\Floppy0 (if A: is the first floppy)
841 * \GLOBAL??\C: -> \Device\HarddiskVolume1 (if C: is the first harddisk)
842 * \GLOBAL??\D: -> \Device\CdRom0 (if D: is first cdrom)
843 * \SystemRoot -> \Device\Harddisk0\Partition1\WINDOWS (or where is NT system installed)
844 * \Volume{...} -> \Device\HarddiskVolume1 (where ... is system generated guid)
846 * In most common cases, absolute NT symlinks points to path on
847 * DOS/Win32 drive letter, system-specific Volume or on UNC share.
848 * Here are few examples of commonly used absolute NT symlinks
849 * created by mklink.exe tool:
850 * \??\C:\folder\file.txt
851 * \??\\C:\folder\file.txt
852 * \??\UNC\server\share\file.txt
853 * \??\\UNC\server\share\file.txt
854 * \??\Volume{b75e2c83-0000-0000-0000-602f00000000}\folder\file.txt
856 * It means that the most common path prefix \??\ is also NT path
857 * symlink (to \GLOBAL??). It is less common that second path
858 * separator is double backslash, but it is valid.
860 * Volume guid is randomly generated by the target system and so
861 * only the target system knows the mapping between guid and the
862 * hardisk number. Over SMB it is not possible to resolve this
863 * mapping, therefore symlinks pointing to target location of
864 * volume guids are totally unusable over SMB.
866 * For now parse only symlink paths available for DOS and Win32.
867 * Those are paths with \??\ prefix or paths which points to \??\
868 * via other NT symlink (\DosDevices\, \GLOBAL??\, ...).
870 abs_path = smb_target;
872 if (strstarts(abs_path, "\\??\\"))
873 abs_path += sizeof("\\??\\")-1;
874 else if (strstarts(abs_path, "\\DosDevices\\"))
875 abs_path += sizeof("\\DosDevices\\")-1;
876 else if (strstarts(abs_path, "\\GLOBAL??\\"))
877 abs_path += sizeof("\\GLOBAL??\\")-1;
879 /* Unhandled absolute symlink, points outside of DOS/Win32 */
881 "absolute symlink '%s' cannot be converted from NT format "
882 "because points to unknown target\n",
888 /* Sometimes path separator after \?? is double backslash */
889 if (abs_path[0] == '\\')
892 while (strstarts(abs_path, "Global\\"))
893 abs_path += sizeof("Global\\")-1;
895 if (strstarts(abs_path, "GLOBALROOT\\")) {
896 /* Label globalroot requires path with leading '\\', so do not trim '\\' */
897 abs_path += sizeof("GLOBALROOT")-1;
901 /* For now parse only paths to drive letters */
902 if (((abs_path[0] >= 'A' && abs_path[0] <= 'Z') ||
903 (abs_path[0] >= 'a' && abs_path[0] <= 'z')) &&
904 abs_path[1] == ':' &&
905 (abs_path[2] == '\\' || abs_path[2] == '\0')) {
906 /* Convert drive letter to lowercase and drop colon */
907 char drive_letter = abs_path[0];
908 if (drive_letter >= 'A' && drive_letter <= 'Z')
909 drive_letter += 'a'-'A';
911 abs_path[0] = drive_letter;
913 /* Unhandled absolute symlink. Report an error. */
915 "absolute symlink '%s' cannot be converted from NT format "
916 "because points to unknown target\n",
922 abs_path_len = strlen(abs_path)+1;
923 symlinkroot_len = strlen(cifs_sb->ctx->symlinkroot);
924 if (cifs_sb->ctx->symlinkroot[symlinkroot_len-1] == '/')
926 linux_target = kmalloc(symlinkroot_len + 1 + abs_path_len, GFP_KERNEL);
931 memcpy(linux_target, cifs_sb->ctx->symlinkroot, symlinkroot_len);
932 linux_target[symlinkroot_len] = '/';
933 memcpy(linux_target + symlinkroot_len + 1, abs_path, abs_path_len);
934 } else if (smb_target[0] == sep && relative) {
936 * This is a relative SMB symlink from the top of the share,
937 * which is the top level directory of the Linux mount point.
938 * Linux does not support such relative symlinks, so convert
939 * it to the relative symlink from the current directory.
940 * full_path is the SMB path to the symlink (from which is
941 * extracted current directory) and smb_target is the SMB path
942 * where symlink points, therefore full_path must always be on
945 int smb_target_len = strlen(smb_target)+1;
947 for (i = 1; full_path[i]; i++) { /* i=1 to skip leading sep */
948 if (full_path[i] == sep)
951 linux_target = kmalloc(levels*3 + smb_target_len, GFP_KERNEL);
956 for (i = 0; i < levels; i++) {
957 linux_target[i*3 + 0] = '.';
958 linux_target[i*3 + 1] = '.';
959 linux_target[i*3 + 2] = sep;
961 memcpy(linux_target + levels*3, smb_target+1, smb_target_len); /* +1 to skip leading sep */
964 * This is either an absolute symlink in POSIX-style format
965 * or relative SMB symlink from the current directory.
966 * These paths have same format as Linux symlinks, so no
967 * conversion is needed.
969 linux_target = smb_target;
974 convert_delimiter(linux_target, '/');
977 *target = linux_target;
979 cifs_dbg(FYI, "%s: symlink target: %s\n", __func__, *target);
988 static int parse_reparse_native_symlink(struct reparse_symlink_data_buffer *sym,
990 struct cifs_sb_info *cifs_sb,
991 const char *full_path,
992 struct cifs_open_info_data *data)
997 /* We handle Symbolic Link reparse tag here. See: MS-FSCC 2.1.2.4 */
999 offs = le16_to_cpu(sym->SubstituteNameOffset);
1000 len = le16_to_cpu(sym->SubstituteNameLength);
1001 if (offs + 20 > plen || offs + len + 20 > plen) {
1002 cifs_dbg(VFS, "srv returned malformed symlink buffer\n");
1006 return smb2_parse_native_symlink(&data->symlink_target,
1007 sym->PathBuffer + offs,
1009 le32_to_cpu(sym->Flags) & SYMLINK_FLAG_RELATIVE,
1014 static int parse_reparse_wsl_symlink(struct reparse_wsl_symlink_data_buffer *buf,
1015 struct cifs_sb_info *cifs_sb,
1016 struct cifs_open_info_data *data)
1018 int len = le16_to_cpu(buf->ReparseDataLength);
1019 int symname_utf8_len;
1020 __le16 *symname_utf16;
1021 int symname_utf16_len;
1023 if (len <= sizeof(buf->Flags)) {
1024 cifs_dbg(VFS, "srv returned malformed wsl symlink buffer\n");
1028 /* PathBuffer is in UTF-8 but without trailing null-term byte */
1029 symname_utf8_len = len - sizeof(buf->Flags);
1031 * Check that buffer does not contain null byte
1032 * because Linux cannot process symlink with null byte.
1034 if (strnlen(buf->PathBuffer, symname_utf8_len) != symname_utf8_len) {
1035 cifs_dbg(VFS, "srv returned null byte in wsl symlink target location\n");
1038 symname_utf16 = kzalloc(symname_utf8_len * 2, GFP_KERNEL);
1041 symname_utf16_len = utf8s_to_utf16s(buf->PathBuffer, symname_utf8_len,
1042 UTF16_LITTLE_ENDIAN,
1043 (wchar_t *) symname_utf16, symname_utf8_len * 2);
1044 if (symname_utf16_len < 0) {
1045 kfree(symname_utf16);
1046 return symname_utf16_len;
1048 symname_utf16_len *= 2; /* utf8s_to_utf16s() returns number of u16 items, not byte length */
1050 data->symlink_target = cifs_strndup_from_utf16((u8 *)symname_utf16,
1051 symname_utf16_len, true,
1052 cifs_sb->local_nls);
1053 kfree(symname_utf16);
1054 if (!data->symlink_target)
1060 int parse_reparse_point(struct reparse_data_buffer *buf,
1061 u32 plen, struct cifs_sb_info *cifs_sb,
1062 const char *full_path,
1063 struct cifs_open_info_data *data)
1065 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
1067 data->reparse.buf = buf;
1069 /* See MS-FSCC 2.1.2 */
1070 switch (le32_to_cpu(buf->ReparseTag)) {
1071 case IO_REPARSE_TAG_NFS:
1072 return parse_reparse_nfs((struct reparse_nfs_data_buffer *)buf,
1074 case IO_REPARSE_TAG_SYMLINK:
1075 return parse_reparse_native_symlink(
1076 (struct reparse_symlink_data_buffer *)buf,
1077 plen, cifs_sb, full_path, data);
1078 case IO_REPARSE_TAG_LX_SYMLINK:
1079 return parse_reparse_wsl_symlink(
1080 (struct reparse_wsl_symlink_data_buffer *)buf,
1082 case IO_REPARSE_TAG_AF_UNIX:
1083 case IO_REPARSE_TAG_LX_FIFO:
1084 case IO_REPARSE_TAG_LX_CHR:
1085 case IO_REPARSE_TAG_LX_BLK:
1086 if (le16_to_cpu(buf->ReparseDataLength) != 0) {
1087 cifs_dbg(VFS, "srv returned malformed buffer for reparse point: 0x%08x\n",
1088 le32_to_cpu(buf->ReparseTag));
1093 cifs_tcon_dbg(VFS | ONCE, "unhandled reparse tag: 0x%08x\n",
1094 le32_to_cpu(buf->ReparseTag));
1100 int smb2_parse_reparse_point(struct cifs_sb_info *cifs_sb,
1101 const char *full_path,
1102 struct kvec *rsp_iov,
1103 struct cifs_open_info_data *data)
1105 struct reparse_data_buffer *buf;
1106 struct smb2_ioctl_rsp *io = rsp_iov->iov_base;
1107 u32 plen = le32_to_cpu(io->OutputCount);
1109 buf = (struct reparse_data_buffer *)((u8 *)io +
1110 le32_to_cpu(io->OutputOffset));
1111 return parse_reparse_point(buf, plen, cifs_sb, full_path, data);
1114 static bool wsl_to_fattr(struct cifs_open_info_data *data,
1115 struct cifs_sb_info *cifs_sb,
1116 u32 tag, struct cifs_fattr *fattr)
1118 struct smb2_file_full_ea_info *ea;
1119 bool have_xattr_dev = false;
1123 case IO_REPARSE_TAG_LX_SYMLINK:
1124 fattr->cf_mode |= S_IFLNK;
1126 case IO_REPARSE_TAG_LX_FIFO:
1127 fattr->cf_mode |= S_IFIFO;
1129 case IO_REPARSE_TAG_AF_UNIX:
1130 fattr->cf_mode |= S_IFSOCK;
1132 case IO_REPARSE_TAG_LX_CHR:
1133 fattr->cf_mode |= S_IFCHR;
1135 case IO_REPARSE_TAG_LX_BLK:
1136 fattr->cf_mode |= S_IFBLK;
1140 if (!data->wsl.eas_len)
1143 ea = (struct smb2_file_full_ea_info *)data->wsl.eas;
1149 ea = (void *)((u8 *)ea + next);
1150 next = le32_to_cpu(ea->next_entry_offset);
1151 if (!le16_to_cpu(ea->ea_value_length))
1155 nlen = ea->ea_name_length;
1156 v = (void *)((u8 *)ea->ea_data + ea->ea_name_length + 1);
1158 if (!strncmp(name, SMB2_WSL_XATTR_UID, nlen))
1159 fattr->cf_uid = wsl_make_kuid(cifs_sb, v);
1160 else if (!strncmp(name, SMB2_WSL_XATTR_GID, nlen))
1161 fattr->cf_gid = wsl_make_kgid(cifs_sb, v);
1162 else if (!strncmp(name, SMB2_WSL_XATTR_MODE, nlen)) {
1163 /* File type in reparse point tag and in xattr mode must match. */
1164 if (S_DT(fattr->cf_mode) != S_DT(le32_to_cpu(*(__le32 *)v)))
1166 fattr->cf_mode = (umode_t)le32_to_cpu(*(__le32 *)v);
1167 } else if (!strncmp(name, SMB2_WSL_XATTR_DEV, nlen)) {
1168 fattr->cf_rdev = reparse_mkdev(v);
1169 have_xattr_dev = true;
1174 /* Major and minor numbers for char and block devices are mandatory. */
1175 if (!have_xattr_dev && (tag == IO_REPARSE_TAG_LX_CHR || tag == IO_REPARSE_TAG_LX_BLK))
1178 fattr->cf_dtype = S_DT(fattr->cf_mode);
1182 static bool posix_reparse_to_fattr(struct cifs_sb_info *cifs_sb,
1183 struct cifs_fattr *fattr,
1184 struct cifs_open_info_data *data)
1186 struct reparse_nfs_data_buffer *buf = (struct reparse_nfs_data_buffer *)data->reparse.buf;
1191 if (le16_to_cpu(buf->ReparseDataLength) < sizeof(buf->InodeType)) {
1196 switch (le64_to_cpu(buf->InodeType)) {
1197 case NFS_SPECFILE_CHR:
1198 if (le16_to_cpu(buf->ReparseDataLength) != sizeof(buf->InodeType) + 8) {
1202 fattr->cf_mode |= S_IFCHR;
1203 fattr->cf_rdev = reparse_mkdev(buf->DataBuffer);
1205 case NFS_SPECFILE_BLK:
1206 if (le16_to_cpu(buf->ReparseDataLength) != sizeof(buf->InodeType) + 8) {
1210 fattr->cf_mode |= S_IFBLK;
1211 fattr->cf_rdev = reparse_mkdev(buf->DataBuffer);
1213 case NFS_SPECFILE_FIFO:
1214 fattr->cf_mode |= S_IFIFO;
1216 case NFS_SPECFILE_SOCK:
1217 fattr->cf_mode |= S_IFSOCK;
1219 case NFS_SPECFILE_LNK:
1220 fattr->cf_mode |= S_IFLNK;
1229 bool cifs_reparse_point_to_fattr(struct cifs_sb_info *cifs_sb,
1230 struct cifs_fattr *fattr,
1231 struct cifs_open_info_data *data)
1233 u32 tag = data->reparse.tag;
1237 case IO_REPARSE_TAG_INTERNAL:
1238 if (!(fattr->cf_cifsattrs & ATTR_DIRECTORY))
1241 case IO_REPARSE_TAG_DFS:
1242 case IO_REPARSE_TAG_DFSR:
1243 case IO_REPARSE_TAG_MOUNT_POINT:
1244 /* See cifs_create_junction_fattr() */
1245 fattr->cf_mode = S_IFDIR | 0711;
1247 case IO_REPARSE_TAG_LX_SYMLINK:
1248 case IO_REPARSE_TAG_LX_FIFO:
1249 case IO_REPARSE_TAG_AF_UNIX:
1250 case IO_REPARSE_TAG_LX_CHR:
1251 case IO_REPARSE_TAG_LX_BLK:
1252 ok = wsl_to_fattr(data, cifs_sb, tag, fattr);
1256 case IO_REPARSE_TAG_NFS:
1257 ok = posix_reparse_to_fattr(cifs_sb, fattr, data);
1261 case 0: /* SMB1 symlink */
1262 case IO_REPARSE_TAG_SYMLINK:
1263 fattr->cf_mode |= S_IFLNK;
1269 fattr->cf_dtype = S_DT(fattr->cf_mode);