xref: /linux/fs/smb/client/reparse.c (revision e9a8cac0bf895efe0bc7b11d174e8dae9b195da8)
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 
smb2_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 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)
40 {
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);
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 	struct inode *new;
61 	struct kvec iov;
62 	__le16 *path = NULL;
63 	bool directory;
64 	char *symlink_target = NULL;
65 	char *sym = NULL;
66 	char sep = CIFS_DIR_SEP(cifs_sb);
67 	u16 len, plen, poff, slen;
68 	int rc = 0;
69 
70 	if (strlen(symname) > REPARSE_SYM_PATH_MAX)
71 		return -ENAMETOOLONG;
72 
73 	symlink_target = kstrdup(symname, GFP_KERNEL);
74 	if (!symlink_target) {
75 		rc = -ENOMEM;
76 		goto out;
77 	}
78 
79 	data = (struct cifs_open_info_data) {
80 		.reparse_point = true,
81 		.reparse = { .tag = IO_REPARSE_TAG_SYMLINK, },
82 		.symlink_target = symlink_target,
83 	};
84 
85 	if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS) && symname[0] == '/') {
86 		/*
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
93 		 * on the SMB server.
94 		 */
95 		if (!strstarts(symname, cifs_sb->ctx->symlinkroot)) {
96 			/*
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.
100 			 */
101 			cifs_dbg(VFS,
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);
105 			rc = -EINVAL;
106 			goto out;
107 		}
108 		len = strlen(cifs_sb->ctx->symlinkroot);
109 		if (cifs_sb->ctx->symlinkroot[len-1] != '/')
110 			len++;
111 		if (symname[len] >= 'a' && symname[len] <= 'z' &&
112 		    (symname[len+1] == '/' || symname[len+1] == '\0')) {
113 			/*
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.
118 			 */
119 			int common_path_len = strlen(symname+len+1)+1;
120 			sym = kzalloc(6+common_path_len, GFP_KERNEL);
121 			if (!sym) {
122 				rc = -ENOMEM;
123 				goto out;
124 			}
125 			memcpy(sym, "\\??\\", 4);
126 			sym[4] = symname[len] - ('a'-'A');
127 			sym[5] = ':';
128 			memcpy(sym+6, symname+len+1, common_path_len);
129 		} else {
130 			/* Unhandled absolute symlink. Report an error. */
131 			cifs_dbg(
132 				 VFS,
133 				 "absolute symlink '%s' cannot be converted to NT format "
134 				 "because it points to unknown target\n",
135 				 symname);
136 			rc = -EINVAL;
137 			goto out;
138 		}
139 	} else {
140 		/*
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.
146 		 */
147 		sym = kstrdup(symname, GFP_KERNEL);
148 		if (!sym) {
149 			rc = -ENOMEM;
150 			goto out;
151 		}
152 	}
153 
154 	if (sep == '\\')
155 		convert_delimiter(sym, sep);
156 
157 	/*
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
163 	 * them back.
164 	 */
165 	if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS) && symname[0] == '/')
166 		sym[0] = sym[1] = sym[2] = sym[5] = '_';
167 
168 	path = cifs_convert_path_to_utf16(sym, cifs_sb);
169 	if (!path) {
170 		rc = -ENOMEM;
171 		goto out;
172 	}
173 
174 	if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS) && symname[0] == '/') {
175 		sym[0] = '\\';
176 		sym[1] = sym[2] = '?';
177 		sym[5] = ':';
178 		path[0] = cpu_to_le16('\\');
179 		path[1] = path[2] = cpu_to_le16('?');
180 		path[5] = cpu_to_le16(':');
181 	}
182 
183 	/*
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.
189 	 */
190 	directory = false;
191 	rc = detect_directory_symlink_target(cifs_sb, xid, full_path, symname, &directory);
192 	if (rc < 0)
193 		goto out;
194 
195 	slen = 2 * UniStrnlen((wchar_t *)path, REPARSE_SYM_PATH_MAX);
196 	poff = 0;
197 	plen = slen;
198 	if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS) && symname[0] == '/') {
199 		/*
200 		 * For absolute NT symlinks skip leading "\\??\\" in PrintName as
201 		 * PrintName is user visible location in DOS/Win32 format (not in NT format).
202 		 */
203 		poff = 4;
204 		plen -= 2 * poff;
205 	}
206 	len = sizeof(*buf) + plen + slen;
207 	buf = kzalloc(len, GFP_KERNEL);
208 	if (!buf) {
209 		rc = -ENOMEM;
210 		goto out;
211 	}
212 
213 	buf->ReparseTag = cpu_to_le32(IO_REPARSE_TAG_SYMLINK);
214 	buf->ReparseDataLength = cpu_to_le16(len - sizeof(struct reparse_data_buffer));
215 
216 	buf->SubstituteNameOffset = cpu_to_le16(plen);
217 	buf->SubstituteNameLength = cpu_to_le16(slen);
218 	memcpy(&buf->PathBuffer[plen], path, slen);
219 
220 	buf->PrintNameOffset = 0;
221 	buf->PrintNameLength = cpu_to_le16(plen);
222 	memcpy(buf->PathBuffer, path+poff, plen);
223 
224 	buf->Flags = cpu_to_le32(*symname != '/' ? SYMLINK_FLAG_RELATIVE : 0);
225 
226 	iov.iov_base = buf;
227 	iov.iov_len = len;
228 	new = smb2_get_reparse_inode(&data, inode->i_sb, xid,
229 				     tcon, full_path, directory,
230 				     &iov, NULL);
231 	if (!IS_ERR(new))
232 		d_instantiate(dentry, new);
233 	else
234 		rc = PTR_ERR(new);
235 out:
236 	kfree(sym);
237 	kfree(path);
238 	cifs_free_open_info(&data);
239 	kfree(buf);
240 	return rc;
241 }
242 
detect_directory_symlink_target(struct cifs_sb_info * cifs_sb,const unsigned int xid,const char * full_path,const char * symname,bool * directory)243 static int detect_directory_symlink_target(struct cifs_sb_info *cifs_sb,
244 					   const unsigned int xid,
245 					   const char *full_path,
246 					   const char *symname,
247 					   bool *directory)
248 {
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;
254 	struct cifs_fid fid;
255 	char *resolved_path;
256 	int full_path_len;
257 	int basename_len;
258 	int symname_len;
259 	char *path_sep;
260 	__u32 oplock;
261 	int open_rc;
262 
263 	/*
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.
267 	 */
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 ".." */
273 		*directory = true;
274 		return 0;
275 	}
276 
277 	/*
278 	 * For absolute symlinks it is not possible to determinate
279 	 * if it should point to directory or file.
280 	 */
281 	if (symname[0] == '/') {
282 		cifs_dbg(FYI,
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);
286 		return 0;
287 	}
288 
289 	/*
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.
293 	 */
294 
295 	full_path_len = strlen(full_path);
296 	symname_len = strlen(symname);
297 
298 	tlink = cifs_sb_tlink(cifs_sb);
299 	if (IS_ERR(tlink))
300 		return PTR_ERR(tlink);
301 
302 	resolved_path = kzalloc(full_path_len + symname_len + 1, GFP_KERNEL);
303 	if (!resolved_path) {
304 		cifs_put_tlink(tlink);
305 		return -ENOMEM;
306 	}
307 
308 	/*
309 	 * Compose the resolved SMB symlink path from the SMB full path
310 	 * and Linux target symlink path.
311 	 */
312 	memcpy(resolved_path, full_path, full_path_len+1);
313 	path_sep = strrchr(resolved_path, sep);
314 	if (path_sep)
315 		path_sep++;
316 	else
317 		path_sep = resolved_path;
318 	memcpy(path_sep, symname, symname_len+1);
319 	if (sep == '\\')
320 		convert_delimiter(path_sep, sep);
321 
322 	tcon = tlink_tcon(tlink);
323 	oparms = CIFS_OPARMS(cifs_sb, tcon, resolved_path,
324 			     FILE_READ_ATTRIBUTES, FILE_OPEN, 0, ACL_NO_MODE);
325 	oparms.fid = &fid;
326 
327 	/* Try to open as a directory (NOT_FILE) */
328 	oplock = 0;
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);
332 	if (open_rc == 0) {
333 		/* Successful open means that the target path is definitely a directory. */
334 		*directory = true;
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. */
338 		*directory = false;
339 	} else if (open_rc == -ENOENT) {
340 		/* -ENOENT means that the target path does not exist. */
341 		cifs_dbg(FYI,
342 			 "%s: symlink target path '%s' does not exist, "
343 			 "creating '%s' as file symlink\n",
344 			 __func__, symname, full_path);
345 	} else {
346 		/* Try to open as a file (NOT_DIR) */
347 		oplock = 0;
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);
351 		if (open_rc == 0) {
352 			/* Successful open means that the target path is definitely a file. */
353 			*directory = false;
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. */
357 			*directory = true;
358 		} else {
359 			/*
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.
363 			 *
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.
368 			 */
369 			cifs_dbg(FYI,
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);
373 		}
374 	}
375 
376 	kfree(resolved_path);
377 	cifs_put_tlink(tlink);
378 	return 0;
379 }
380 
create_native_socket(const unsigned int xid,struct inode * inode,struct dentry * dentry,struct cifs_tcon * tcon,const char * full_path)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)
384 {
385 	struct reparse_data_buffer buf = {
386 		.ReparseTag = cpu_to_le32(IO_REPARSE_TAG_AF_UNIX),
387 		.ReparseDataLength = cpu_to_le16(0),
388 	};
389 	struct cifs_open_info_data data = {
390 		.reparse_point = true,
391 		.reparse = { .tag = IO_REPARSE_TAG_AF_UNIX, .buf = &buf, },
392 	};
393 	struct kvec iov = {
394 		.iov_base = &buf,
395 		.iov_len = sizeof(buf),
396 	};
397 	struct inode *new;
398 	int rc = 0;
399 
400 	new = smb2_get_reparse_inode(&data, inode->i_sb, xid,
401 				     tcon, full_path, false, &iov, NULL);
402 	if (!IS_ERR(new))
403 		d_instantiate(dentry, new);
404 	else
405 		rc = PTR_ERR(new);
406 	cifs_free_open_info(&data);
407 	return rc;
408 }
409 
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)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,
414 			       struct kvec *iov)
415 {
416 	u64 type;
417 	u16 len, dlen;
418 
419 	len = sizeof(*buf);
420 
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));
427 		break;
428 	case NFS_SPECFILE_LNK:
429 		dlen = symname_utf16_len;
430 		memcpy(buf->DataBuffer, symname_utf16, symname_utf16_len);
431 		break;
432 	case NFS_SPECFILE_FIFO:
433 	case NFS_SPECFILE_SOCK:
434 		dlen = 0;
435 		break;
436 	default:
437 		return -EOPNOTSUPP;
438 	}
439 
440 	buf->ReparseTag = cpu_to_le32(IO_REPARSE_TAG_NFS);
441 	buf->Reserved = 0;
442 	buf->InodeType = cpu_to_le64(type);
443 	buf->ReparseDataLength = cpu_to_le16(len + dlen -
444 					     sizeof(struct reparse_data_buffer));
445 	iov->iov_base = buf;
446 	iov->iov_len = len + dlen;
447 	return 0;
448 }
449 
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)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,
453 		     const char *symname)
454 {
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;
460 	struct inode *new;
461 	struct kvec iov;
462 	__u8 buf[sizeof(*p) + sizeof(__le64)];
463 	int rc;
464 
465 	if (S_ISLNK(mode)) {
466 		symname_utf16 = cifs_strndup_to_utf16(symname, strlen(symname),
467 						      &symname_utf16_len,
468 						      cifs_sb->local_nls,
469 						      NO_MAP_UNI_RSVD);
470 		if (!symname_utf16) {
471 			rc = -ENOMEM;
472 			goto out;
473 		}
474 		symname_utf16_len -= 2; /* symlink is without trailing wide-nul */
475 		p = kzalloc(sizeof(*p) + symname_utf16_len, GFP_KERNEL);
476 		if (!p) {
477 			rc = -ENOMEM;
478 			goto out;
479 		}
480 	} else {
481 		p = (struct reparse_nfs_data_buffer *)buf;
482 	}
483 	rc = nfs_set_reparse_buf(p, mode, dev, symname_utf16, symname_utf16_len, &iov);
484 	if (rc)
485 		goto out;
486 
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),
491 	};
492 
493 	new = smb2_get_reparse_inode(&data, inode->i_sb, xid,
494 				     tcon, full_path, false, &iov, NULL);
495 	if (!IS_ERR(new))
496 		d_instantiate(dentry, new);
497 	else
498 		rc = PTR_ERR(new);
499 	cifs_free_open_info(&data);
500 out:
501 	if (S_ISLNK(mode)) {
502 		kfree(symname_utf16);
503 		kfree(p);
504 	}
505 	return rc;
506 }
507 
wsl_set_reparse_buf(struct reparse_data_buffer ** buf,mode_t mode,const char * symname,struct cifs_sb_info * cifs_sb,struct kvec * iov)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,
511 			       struct kvec *iov)
512 {
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;
518 	size_t buf_len;
519 	u32 tag;
520 
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);
528 		if (!*buf)
529 			return -ENOMEM;
530 		break;
531 	case IO_REPARSE_TAG_LX_SYMLINK:
532 		symname_utf16 = cifs_strndup_to_utf16(symname, strlen(symname),
533 						      &symname_utf16_len,
534 						      cifs_sb->local_nls,
535 						      NO_MAP_UNI_RSVD);
536 		if (!symname_utf16)
537 			return -ENOMEM;
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);
541 		if (!symlink_buf) {
542 			kfree(symname_utf16);
543 			return -ENOMEM;
544 		}
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,
549 						   UTF16_LITTLE_ENDIAN,
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);
555 		break;
556 	default:
557 		return -EOPNOTSUPP;
558 	}
559 
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;
565 	return 0;
566 }
567 
ea_create_context(u32 dlen,size_t * cc_len)568 static struct smb2_create_ea_ctx *ea_create_context(u32 dlen, size_t *cc_len)
569 {
570 	struct smb2_create_ea_ctx *cc;
571 
572 	*cc_len = round_up(sizeof(*cc) + dlen, 8);
573 	cc = kzalloc(*cc_len, GFP_KERNEL);
574 	if (!cc)
575 		return ERR_PTR(-ENOMEM);
576 
577 	cc->ctx.NameOffset = cpu_to_le16(offsetof(struct smb2_create_ea_ctx,
578 						  name));
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);
583 	return cc;
584 }
585 
586 struct wsl_xattr {
587 	const char	*name;
588 	__le64		value;
589 	u16		size;
590 	u32		next;
591 };
592 
wsl_set_xattrs(struct inode * inode,umode_t _mode,dev_t _dev,struct kvec * iov)593 static int wsl_set_xattrs(struct inode *inode, umode_t _mode,
594 			  dev_t _dev, struct kvec *iov)
595 {
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, },
608 	};
609 	size_t cc_len;
610 	u32 dlen = 0, next = 0;
611 	int i, num_xattrs;
612 	u8 name_size = SMB2_WSL_XATTR_NAME_LEN + 1;
613 
614 	memset(iov, 0, sizeof(*iov));
615 
616 	/* Exclude $LXDEV xattr for non-device files */
617 	if (!S_ISBLK(_mode) && !S_ISCHR(_mode))
618 		num_xattrs = ARRAY_SIZE(xattrs) - 1;
619 	else
620 		num_xattrs = ARRAY_SIZE(xattrs);
621 
622 	for (i = 0; i < num_xattrs; i++) {
623 		xattrs[i].next = ALIGN(sizeof(*ea) + name_size +
624 				       xattrs[i].size, 4);
625 		dlen += xattrs[i].next;
626 	}
627 
628 	cc = ea_create_context(dlen, &cc_len);
629 	if (IS_ERR(cc))
630 		return PTR_ERR(cc);
631 
632 	ea = &cc->ea;
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);
637 
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);
643 	}
644 	ea->next_entry_offset = 0;
645 
646 	iov->iov_base = cc;
647 	iov->iov_len = cc_len;
648 	return 0;
649 }
650 
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)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,
654 		     const char *symname)
655 {
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;
660 	struct inode *new;
661 	unsigned int len;
662 	struct kvec reparse_iov, xattr_iov;
663 	int rc;
664 
665 	rc = wsl_set_reparse_buf(&buf, mode, symname, cifs_sb, &reparse_iov);
666 	if (rc)
667 		return rc;
668 
669 	rc = wsl_set_xattrs(inode, mode, dev, &xattr_iov);
670 	if (rc) {
671 		kfree(buf);
672 		return rc;
673 	}
674 
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),
679 	};
680 
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;
685 
686 	new = smb2_get_reparse_inode(&data, inode->i_sb,
687 				     xid, tcon, full_path, false,
688 				     &reparse_iov, &xattr_iov);
689 	if (!IS_ERR(new))
690 		d_instantiate(dentry, new);
691 	else
692 		rc = PTR_ERR(new);
693 	cifs_free_open_info(&data);
694 	kfree(xattr_iov.iov_base);
695 	kfree(buf);
696 	return rc;
697 }
698 
smb2_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)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)
702 {
703 	struct smb3_fs_context *ctx = CIFS_SB(inode->i_sb)->ctx;
704 
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);
707 
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);
713 	default:
714 		return -EOPNOTSUPP;
715 	}
716 }
717 
718 /* 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)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)
722 {
723 	unsigned int len;
724 	u64 type;
725 
726 	len = le16_to_cpu(buf->ReparseDataLength);
727 	if (len < sizeof(buf->InodeType)) {
728 		cifs_dbg(VFS, "srv returned malformed nfs buffer\n");
729 		return -EIO;
730 	}
731 
732 	len -= sizeof(buf->InodeType);
733 
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");
738 			return -EIO;
739 		}
740 		/*
741 		 * Check that buffer does not contain UTF-16 null codepoint
742 		 * because Linux cannot process symlink with null byte.
743 		 */
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");
746 			return -EIO;
747 		}
748 		data->symlink_target = cifs_strndup_from_utf16(buf->DataBuffer,
749 							       len, true,
750 							       cifs_sb->local_nls);
751 		if (!data->symlink_target)
752 			return -ENOMEM;
753 		cifs_dbg(FYI, "%s: target path: %s\n",
754 			 __func__, data->symlink_target);
755 		break;
756 	case NFS_SPECFILE_CHR:
757 	case NFS_SPECFILE_BLK:
758 		/* DataBuffer for block and char devices contains two 32-bit numbers */
759 		if (len != 8) {
760 			cifs_dbg(VFS, "srv returned malformed nfs buffer for type: 0x%llx\n", type);
761 			return -EIO;
762 		}
763 		break;
764 	case NFS_SPECFILE_FIFO:
765 	case NFS_SPECFILE_SOCK:
766 		/* DataBuffer for fifos and sockets is empty */
767 		if (len != 0) {
768 			cifs_dbg(VFS, "srv returned malformed nfs buffer for type: 0x%llx\n", type);
769 			return -EIO;
770 		}
771 		break;
772 	default:
773 		cifs_dbg(VFS, "%s: unhandled inode type: 0x%llx\n",
774 			 __func__, type);
775 		return -EOPNOTSUPP;
776 	}
777 	return 0;
778 }
779 
smb2_parse_native_symlink(char ** target,const char * buf,unsigned int len,bool relative,const char * full_path,struct cifs_sb_info * cifs_sb)780 int smb2_parse_native_symlink(char **target, const char *buf, unsigned int len,
781 			      bool relative,
782 			      const char *full_path,
783 			      struct cifs_sb_info *cifs_sb)
784 {
785 	char sep = CIFS_DIR_SEP(cifs_sb);
786 	char *linux_target = NULL;
787 	char *smb_target = NULL;
788 	int symlinkroot_len;
789 	int abs_path_len;
790 	char *abs_path;
791 	int levels;
792 	int rc;
793 	int i;
794 
795 	/* Check that length it valid */
796 	if (!len || (len % 2)) {
797 		cifs_dbg(VFS, "srv returned malformed symlink buffer\n");
798 		rc = -EIO;
799 		goto out;
800 	}
801 
802 	/*
803 	 * Check that buffer does not contain UTF-16 null codepoint
804 	 * because Linux cannot process symlink with null byte.
805 	 */
806 	if (UniStrnlen((wchar_t *)buf, len/2) != len/2) {
807 		cifs_dbg(VFS, "srv returned null byte in native symlink target location\n");
808 		rc = -EIO;
809 		goto out;
810 	}
811 
812 	smb_target = cifs_strndup_from_utf16(buf, len, true, cifs_sb->local_nls);
813 	if (!smb_target) {
814 		rc = -ENOMEM;
815 		goto out;
816 	}
817 
818 	if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS) && !relative) {
819 		/*
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"
824 		 * mount option.
825 		 *
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
829 		 *
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:
833 		 * \?? -> \GLOBAL??
834 		 * \DosDevices -> \??
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)
845 		 *
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
855 		 *
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.
859 		 *
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.
865 		 *
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??\, ...).
869 		 */
870 		abs_path = smb_target;
871 globalroot:
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;
878 		else {
879 			/* Unhandled absolute symlink, points outside of DOS/Win32 */
880 			cifs_dbg(VFS,
881 				 "absolute symlink '%s' cannot be converted from NT format "
882 				 "because points to unknown target\n",
883 				 smb_target);
884 			rc = -EIO;
885 			goto out;
886 		}
887 
888 		/* Sometimes path separator after \?? is double backslash */
889 		if (abs_path[0] == '\\')
890 			abs_path++;
891 
892 		while (strstarts(abs_path, "Global\\"))
893 			abs_path += sizeof("Global\\")-1;
894 
895 		if (strstarts(abs_path, "GLOBALROOT\\")) {
896 			/* Label globalroot requires path with leading '\\', so do not trim '\\' */
897 			abs_path += sizeof("GLOBALROOT")-1;
898 			goto globalroot;
899 		}
900 
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';
910 			abs_path++;
911 			abs_path[0] = drive_letter;
912 		} else {
913 			/* Unhandled absolute symlink. Report an error. */
914 			cifs_dbg(VFS,
915 				 "absolute symlink '%s' cannot be converted from NT format "
916 				 "because points to unknown target\n",
917 				 smb_target);
918 			rc = -EIO;
919 			goto out;
920 		}
921 
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] == '/')
925 			symlinkroot_len--;
926 		linux_target = kmalloc(symlinkroot_len + 1 + abs_path_len, GFP_KERNEL);
927 		if (!linux_target) {
928 			rc = -ENOMEM;
929 			goto out;
930 		}
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) {
935 		/*
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
943 		 * the SMB share.
944 		 */
945 		int smb_target_len = strlen(smb_target)+1;
946 		levels = 0;
947 		for (i = 1; full_path[i]; i++) { /* i=1 to skip leading sep */
948 			if (full_path[i] == sep)
949 				levels++;
950 		}
951 		linux_target = kmalloc(levels*3 + smb_target_len, GFP_KERNEL);
952 		if (!linux_target) {
953 			rc = -ENOMEM;
954 			goto out;
955 		}
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;
960 		}
961 		memcpy(linux_target + levels*3, smb_target+1, smb_target_len); /* +1 to skip leading sep */
962 	} else {
963 		/*
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.
968 		 */
969 		linux_target = smb_target;
970 		smb_target = NULL;
971 	}
972 
973 	if (sep == '\\')
974 		convert_delimiter(linux_target, '/');
975 
976 	rc = 0;
977 	*target = linux_target;
978 
979 	cifs_dbg(FYI, "%s: symlink target: %s\n", __func__, *target);
980 
981 out:
982 	if (rc != 0)
983 		kfree(linux_target);
984 	kfree(smb_target);
985 	return rc;
986 }
987 
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)988 static int parse_reparse_native_symlink(struct reparse_symlink_data_buffer *sym,
989 				 u32 plen,
990 				 struct cifs_sb_info *cifs_sb,
991 				 const char *full_path,
992 				 struct cifs_open_info_data *data)
993 {
994 	unsigned int len;
995 	unsigned int offs;
996 
997 	/* We handle Symbolic Link reparse tag here. See: MS-FSCC 2.1.2.4 */
998 
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");
1003 		return -EIO;
1004 	}
1005 
1006 	return smb2_parse_native_symlink(&data->symlink_target,
1007 					 sym->PathBuffer + offs,
1008 					 len,
1009 					 le32_to_cpu(sym->Flags) & SYMLINK_FLAG_RELATIVE,
1010 					 full_path,
1011 					 cifs_sb);
1012 }
1013 
parse_reparse_wsl_symlink(struct reparse_wsl_symlink_data_buffer * buf,struct cifs_sb_info * cifs_sb,struct cifs_open_info_data * data)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)
1017 {
1018 	int len = le16_to_cpu(buf->ReparseDataLength);
1019 	int symname_utf8_len;
1020 	__le16 *symname_utf16;
1021 	int symname_utf16_len;
1022 
1023 	if (len <= sizeof(buf->Flags)) {
1024 		cifs_dbg(VFS, "srv returned malformed wsl symlink buffer\n");
1025 		return -EIO;
1026 	}
1027 
1028 	/* PathBuffer is in UTF-8 but without trailing null-term byte */
1029 	symname_utf8_len = len - sizeof(buf->Flags);
1030 	/*
1031 	 * Check that buffer does not contain null byte
1032 	 * because Linux cannot process symlink with null byte.
1033 	 */
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");
1036 		return -EIO;
1037 	}
1038 	symname_utf16 = kzalloc(symname_utf8_len * 2, GFP_KERNEL);
1039 	if (!symname_utf16)
1040 		return -ENOMEM;
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;
1047 	}
1048 	symname_utf16_len *= 2; /* utf8s_to_utf16s() returns number of u16 items, not byte length */
1049 
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)
1055 		return -ENOMEM;
1056 
1057 	return 0;
1058 }
1059 
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)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)
1064 {
1065 	struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
1066 
1067 	data->reparse.buf = buf;
1068 
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,
1073 					   cifs_sb, data);
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,
1081 			cifs_sb, data);
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));
1089 			return -EIO;
1090 		}
1091 		return 0;
1092 	default:
1093 		cifs_tcon_dbg(VFS | ONCE, "unhandled reparse tag: 0x%08x\n",
1094 			      le32_to_cpu(buf->ReparseTag));
1095 		return -EOPNOTSUPP;
1096 	}
1097 }
1098 
smb2_parse_reparse_point(struct cifs_sb_info * cifs_sb,const char * full_path,struct kvec * rsp_iov,struct cifs_open_info_data * data)1099 int smb2_parse_reparse_point(struct cifs_sb_info *cifs_sb,
1100 			     const char *full_path,
1101 			     struct kvec *rsp_iov,
1102 			     struct cifs_open_info_data *data)
1103 {
1104 	struct reparse_data_buffer *buf;
1105 	struct smb2_ioctl_rsp *io = rsp_iov->iov_base;
1106 	u32 plen = le32_to_cpu(io->OutputCount);
1107 
1108 	buf = (struct reparse_data_buffer *)((u8 *)io +
1109 					     le32_to_cpu(io->OutputOffset));
1110 	return parse_reparse_point(buf, plen, cifs_sb, full_path, data);
1111 }
1112 
wsl_to_fattr(struct cifs_open_info_data * data,struct cifs_sb_info * cifs_sb,u32 tag,struct cifs_fattr * fattr)1113 static bool wsl_to_fattr(struct cifs_open_info_data *data,
1114 			 struct cifs_sb_info *cifs_sb,
1115 			 u32 tag, struct cifs_fattr *fattr)
1116 {
1117 	struct smb2_file_full_ea_info *ea;
1118 	bool have_xattr_dev = false;
1119 	u32 next = 0;
1120 
1121 	switch (tag) {
1122 	case IO_REPARSE_TAG_LX_SYMLINK:
1123 		fattr->cf_mode |= S_IFLNK;
1124 		break;
1125 	case IO_REPARSE_TAG_LX_FIFO:
1126 		fattr->cf_mode |= S_IFIFO;
1127 		break;
1128 	case IO_REPARSE_TAG_AF_UNIX:
1129 		fattr->cf_mode |= S_IFSOCK;
1130 		break;
1131 	case IO_REPARSE_TAG_LX_CHR:
1132 		fattr->cf_mode |= S_IFCHR;
1133 		break;
1134 	case IO_REPARSE_TAG_LX_BLK:
1135 		fattr->cf_mode |= S_IFBLK;
1136 		break;
1137 	}
1138 
1139 	if (!data->wsl.eas_len)
1140 		goto out;
1141 
1142 	ea = (struct smb2_file_full_ea_info *)data->wsl.eas;
1143 	do {
1144 		const char *name;
1145 		void *v;
1146 		u8 nlen;
1147 
1148 		ea = (void *)((u8 *)ea + next);
1149 		next = le32_to_cpu(ea->next_entry_offset);
1150 		if (!le16_to_cpu(ea->ea_value_length))
1151 			continue;
1152 
1153 		name = ea->ea_data;
1154 		nlen = ea->ea_name_length;
1155 		v = (void *)((u8 *)ea->ea_data + ea->ea_name_length + 1);
1156 
1157 		if (!strncmp(name, SMB2_WSL_XATTR_UID, nlen))
1158 			fattr->cf_uid = wsl_make_kuid(cifs_sb, v);
1159 		else if (!strncmp(name, SMB2_WSL_XATTR_GID, nlen))
1160 			fattr->cf_gid = wsl_make_kgid(cifs_sb, v);
1161 		else if (!strncmp(name, SMB2_WSL_XATTR_MODE, nlen)) {
1162 			/* File type in reparse point tag and in xattr mode must match. */
1163 			if (S_DT(fattr->cf_mode) != S_DT(le32_to_cpu(*(__le32 *)v)))
1164 				return false;
1165 			fattr->cf_mode = (umode_t)le32_to_cpu(*(__le32 *)v);
1166 		} else if (!strncmp(name, SMB2_WSL_XATTR_DEV, nlen)) {
1167 			fattr->cf_rdev = reparse_mkdev(v);
1168 			have_xattr_dev = true;
1169 		}
1170 	} while (next);
1171 out:
1172 
1173 	/* Major and minor numbers for char and block devices are mandatory. */
1174 	if (!have_xattr_dev && (tag == IO_REPARSE_TAG_LX_CHR || tag == IO_REPARSE_TAG_LX_BLK))
1175 		return false;
1176 
1177 	fattr->cf_dtype = S_DT(fattr->cf_mode);
1178 	return true;
1179 }
1180 
posix_reparse_to_fattr(struct cifs_sb_info * cifs_sb,struct cifs_fattr * fattr,struct cifs_open_info_data * data)1181 static bool posix_reparse_to_fattr(struct cifs_sb_info *cifs_sb,
1182 				   struct cifs_fattr *fattr,
1183 				   struct cifs_open_info_data *data)
1184 {
1185 	struct reparse_nfs_data_buffer *buf = (struct reparse_nfs_data_buffer *)data->reparse.buf;
1186 
1187 	if (buf == NULL)
1188 		return true;
1189 
1190 	if (le16_to_cpu(buf->ReparseDataLength) < sizeof(buf->InodeType)) {
1191 		WARN_ON_ONCE(1);
1192 		return false;
1193 	}
1194 
1195 	switch (le64_to_cpu(buf->InodeType)) {
1196 	case NFS_SPECFILE_CHR:
1197 		if (le16_to_cpu(buf->ReparseDataLength) != sizeof(buf->InodeType) + 8) {
1198 			WARN_ON_ONCE(1);
1199 			return false;
1200 		}
1201 		fattr->cf_mode |= S_IFCHR;
1202 		fattr->cf_rdev = reparse_mkdev(buf->DataBuffer);
1203 		break;
1204 	case NFS_SPECFILE_BLK:
1205 		if (le16_to_cpu(buf->ReparseDataLength) != sizeof(buf->InodeType) + 8) {
1206 			WARN_ON_ONCE(1);
1207 			return false;
1208 		}
1209 		fattr->cf_mode |= S_IFBLK;
1210 		fattr->cf_rdev = reparse_mkdev(buf->DataBuffer);
1211 		break;
1212 	case NFS_SPECFILE_FIFO:
1213 		fattr->cf_mode |= S_IFIFO;
1214 		break;
1215 	case NFS_SPECFILE_SOCK:
1216 		fattr->cf_mode |= S_IFSOCK;
1217 		break;
1218 	case NFS_SPECFILE_LNK:
1219 		fattr->cf_mode |= S_IFLNK;
1220 		break;
1221 	default:
1222 		WARN_ON_ONCE(1);
1223 		return false;
1224 	}
1225 	return true;
1226 }
1227 
cifs_reparse_point_to_fattr(struct cifs_sb_info * cifs_sb,struct cifs_fattr * fattr,struct cifs_open_info_data * data)1228 bool cifs_reparse_point_to_fattr(struct cifs_sb_info *cifs_sb,
1229 				 struct cifs_fattr *fattr,
1230 				 struct cifs_open_info_data *data)
1231 {
1232 	u32 tag = data->reparse.tag;
1233 	bool ok;
1234 
1235 	switch (tag) {
1236 	case IO_REPARSE_TAG_INTERNAL:
1237 		if (!(fattr->cf_cifsattrs & ATTR_DIRECTORY))
1238 			return false;
1239 		fallthrough;
1240 	case IO_REPARSE_TAG_DFS:
1241 	case IO_REPARSE_TAG_DFSR:
1242 	case IO_REPARSE_TAG_MOUNT_POINT:
1243 		/* See cifs_create_junction_fattr() */
1244 		fattr->cf_mode = S_IFDIR | 0711;
1245 		break;
1246 	case IO_REPARSE_TAG_LX_SYMLINK:
1247 	case IO_REPARSE_TAG_LX_FIFO:
1248 	case IO_REPARSE_TAG_AF_UNIX:
1249 	case IO_REPARSE_TAG_LX_CHR:
1250 	case IO_REPARSE_TAG_LX_BLK:
1251 		ok = wsl_to_fattr(data, cifs_sb, tag, fattr);
1252 		if (!ok)
1253 			return false;
1254 		break;
1255 	case IO_REPARSE_TAG_NFS:
1256 		ok = posix_reparse_to_fattr(cifs_sb, fattr, data);
1257 		if (!ok)
1258 			return false;
1259 		break;
1260 	case 0: /* SMB1 symlink */
1261 	case IO_REPARSE_TAG_SYMLINK:
1262 		fattr->cf_mode |= S_IFLNK;
1263 		break;
1264 	default:
1265 		return false;
1266 	}
1267 
1268 	fattr->cf_dtype = S_DT(fattr->cf_mode);
1269 	return true;
1270 }
1271