xref: /linux/fs/smb/client/reparse.c (revision cfaf773b7946fa911e311acd3b82d61c7a9e42c2) !
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 determinate
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 -EIO;
736 	}
737 
738 	len -= sizeof(buf->InodeType);
739 
740 	switch ((type = le64_to_cpu(buf->InodeType))) {
741 	case NFS_SPECFILE_LNK:
742 		if (len == 0 || (len % 2)) {
743 			cifs_dbg(VFS, "srv returned malformed nfs symlink buffer\n");
744 			return -EIO;
745 		}
746 		/*
747 		 * Check that buffer does not contain UTF-16 null codepoint
748 		 * because Linux cannot process symlink with null byte.
749 		 */
750 		if (UniStrnlen((wchar_t *)buf->DataBuffer, len/2) != len/2) {
751 			cifs_dbg(VFS, "srv returned null byte in nfs symlink target location\n");
752 			return -EIO;
753 		}
754 		data->symlink_target = cifs_strndup_from_utf16(buf->DataBuffer,
755 							       len, true,
756 							       cifs_sb->local_nls);
757 		if (!data->symlink_target)
758 			return -ENOMEM;
759 		cifs_dbg(FYI, "%s: target path: %s\n",
760 			 __func__, data->symlink_target);
761 		break;
762 	case NFS_SPECFILE_CHR:
763 	case NFS_SPECFILE_BLK:
764 		/* DataBuffer for block and char devices contains two 32-bit numbers */
765 		if (len != 8) {
766 			cifs_dbg(VFS, "srv returned malformed nfs buffer for type: 0x%llx\n", type);
767 			return -EIO;
768 		}
769 		break;
770 	case NFS_SPECFILE_FIFO:
771 	case NFS_SPECFILE_SOCK:
772 		/* DataBuffer for fifos and sockets is empty */
773 		if (len != 0) {
774 			cifs_dbg(VFS, "srv returned malformed nfs buffer for type: 0x%llx\n", type);
775 			return -EIO;
776 		}
777 		break;
778 	default:
779 		cifs_dbg(VFS, "%s: unhandled inode type: 0x%llx\n",
780 			 __func__, type);
781 		return -EOPNOTSUPP;
782 	}
783 	return 0;
784 }
785 
smb2_parse_native_symlink(char ** target,const char * buf,unsigned int len,bool relative,const char * full_path,struct cifs_sb_info * cifs_sb)786 int smb2_parse_native_symlink(char **target, const char *buf, unsigned int len,
787 			      bool relative,
788 			      const char *full_path,
789 			      struct cifs_sb_info *cifs_sb)
790 {
791 	const char *symroot = cifs_sb->ctx->symlinkroot;
792 	char sep = CIFS_DIR_SEP(cifs_sb);
793 	char *linux_target = NULL;
794 	char *smb_target = NULL;
795 	int symlinkroot_len;
796 	int abs_path_len;
797 	char *abs_path;
798 	int levels;
799 	int rc;
800 	int i;
801 
802 	/* Check that length it valid */
803 	if (!len || (len % 2)) {
804 		cifs_dbg(VFS, "srv returned malformed symlink buffer\n");
805 		rc = -EIO;
806 		goto out;
807 	}
808 
809 	/*
810 	 * Check that buffer does not contain UTF-16 null codepoint
811 	 * because Linux cannot process symlink with null byte.
812 	 */
813 	if (UniStrnlen((wchar_t *)buf, len/2) != len/2) {
814 		cifs_dbg(VFS, "srv returned null byte in native symlink target location\n");
815 		rc = -EIO;
816 		goto out;
817 	}
818 
819 	smb_target = cifs_strndup_from_utf16(buf, len, true, cifs_sb->local_nls);
820 	if (!smb_target) {
821 		rc = -ENOMEM;
822 		goto out;
823 	}
824 
825 	if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS) &&
826 	    symroot && !relative) {
827 		/*
828 		 * This is an absolute symlink from the server which does not
829 		 * support POSIX paths, so the symlink is in NT-style path.
830 		 * So convert it to absolute Linux symlink target path. Root of
831 		 * the NT-style path for symlinks is specified in "symlinkroot"
832 		 * mount option.
833 		 *
834 		 * Root of the DOS and Win32 paths is at NT path \??\
835 		 * It means that DOS/Win32 path C:\folder\file.txt is
836 		 * NT path \??\C:\folder\file.txt
837 		 *
838 		 * NT systems have some well-known object symlinks in their NT
839 		 * hierarchy, which is needed to take into account when resolving
840 		 * other symlinks. Most commonly used symlink paths are:
841 		 * \?? -> \GLOBAL??
842 		 * \DosDevices -> \??
843 		 * \GLOBAL??\GLOBALROOT -> \
844 		 * \GLOBAL??\Global -> \GLOBAL??
845 		 * \GLOBAL??\NUL -> \Device\Null
846 		 * \GLOBAL??\UNC -> \Device\Mup
847 		 * \GLOBAL??\PhysicalDrive0 -> \Device\Harddisk0\DR0 (for each harddisk)
848 		 * \GLOBAL??\A: -> \Device\Floppy0 (if A: is the first floppy)
849 		 * \GLOBAL??\C: -> \Device\HarddiskVolume1 (if C: is the first harddisk)
850 		 * \GLOBAL??\D: -> \Device\CdRom0 (if D: is first cdrom)
851 		 * \SystemRoot -> \Device\Harddisk0\Partition1\WINDOWS (or where is NT system installed)
852 		 * \Volume{...} -> \Device\HarddiskVolume1 (where ... is system generated guid)
853 		 *
854 		 * In most common cases, absolute NT symlinks points to path on
855 		 * DOS/Win32 drive letter, system-specific Volume or on UNC share.
856 		 * Here are few examples of commonly used absolute NT symlinks
857 		 * created by mklink.exe tool:
858 		 * \??\C:\folder\file.txt
859 		 * \??\\C:\folder\file.txt
860 		 * \??\UNC\server\share\file.txt
861 		 * \??\\UNC\server\share\file.txt
862 		 * \??\Volume{b75e2c83-0000-0000-0000-602f00000000}\folder\file.txt
863 		 *
864 		 * It means that the most common path prefix \??\ is also NT path
865 		 * symlink (to \GLOBAL??). It is less common that second path
866 		 * separator is double backslash, but it is valid.
867 		 *
868 		 * Volume guid is randomly generated by the target system and so
869 		 * only the target system knows the mapping between guid and the
870 		 * hardisk number. Over SMB it is not possible to resolve this
871 		 * mapping, therefore symlinks pointing to target location of
872 		 * volume guids are totally unusable over SMB.
873 		 *
874 		 * For now parse only symlink paths available for DOS and Win32.
875 		 * Those are paths with \??\ prefix or paths which points to \??\
876 		 * via other NT symlink (\DosDevices\, \GLOBAL??\, ...).
877 		 */
878 		abs_path = smb_target;
879 globalroot:
880 		if (strstarts(abs_path, "\\??\\"))
881 			abs_path += sizeof("\\??\\")-1;
882 		else if (strstarts(abs_path, "\\DosDevices\\"))
883 			abs_path += sizeof("\\DosDevices\\")-1;
884 		else if (strstarts(abs_path, "\\GLOBAL??\\"))
885 			abs_path += sizeof("\\GLOBAL??\\")-1;
886 		else
887 			goto out_unhandled_target;
888 
889 		/* Sometimes path separator after \?? is double backslash */
890 		if (abs_path[0] == '\\')
891 			abs_path++;
892 
893 		while (strstarts(abs_path, "Global\\"))
894 			abs_path += sizeof("Global\\")-1;
895 
896 		if (strstarts(abs_path, "GLOBALROOT\\")) {
897 			/* Label globalroot requires path with leading '\\', so do not trim '\\' */
898 			abs_path += sizeof("GLOBALROOT")-1;
899 			goto globalroot;
900 		}
901 
902 		/* For now parse only paths to drive letters */
903 		if (((abs_path[0] >= 'A' && abs_path[0] <= 'Z') ||
904 		     (abs_path[0] >= 'a' && abs_path[0] <= 'z')) &&
905 		    abs_path[1] == ':' &&
906 		    (abs_path[2] == '\\' || abs_path[2] == '\0')) {
907 			/* Convert drive letter to lowercase and drop colon */
908 			char drive_letter = abs_path[0];
909 			if (drive_letter >= 'A' && drive_letter <= 'Z')
910 				drive_letter += 'a'-'A';
911 			abs_path++;
912 			abs_path[0] = drive_letter;
913 		} else {
914 			goto out_unhandled_target;
915 		}
916 
917 		abs_path_len = strlen(abs_path)+1;
918 		symlinkroot_len = strlen(symroot);
919 		if (symroot[symlinkroot_len - 1] == '/')
920 			symlinkroot_len--;
921 		linux_target = kmalloc(symlinkroot_len + 1 + abs_path_len, GFP_KERNEL);
922 		if (!linux_target) {
923 			rc = -ENOMEM;
924 			goto out;
925 		}
926 		memcpy(linux_target, symroot, symlinkroot_len);
927 		linux_target[symlinkroot_len] = '/';
928 		memcpy(linux_target + symlinkroot_len + 1, abs_path, abs_path_len);
929 	} else if (smb_target[0] == sep && relative) {
930 		/*
931 		 * This is a relative SMB symlink from the top of the share,
932 		 * which is the top level directory of the Linux mount point.
933 		 * Linux does not support such relative symlinks, so convert
934 		 * it to the relative symlink from the current directory.
935 		 * full_path is the SMB path to the symlink (from which is
936 		 * extracted current directory) and smb_target is the SMB path
937 		 * where symlink points, therefore full_path must always be on
938 		 * the SMB share.
939 		 */
940 		int smb_target_len = strlen(smb_target)+1;
941 		levels = 0;
942 		for (i = 1; full_path[i]; i++) { /* i=1 to skip leading sep */
943 			if (full_path[i] == sep)
944 				levels++;
945 		}
946 		linux_target = kmalloc(levels*3 + smb_target_len, GFP_KERNEL);
947 		if (!linux_target) {
948 			rc = -ENOMEM;
949 			goto out;
950 		}
951 		for (i = 0; i < levels; i++) {
952 			linux_target[i*3 + 0] = '.';
953 			linux_target[i*3 + 1] = '.';
954 			linux_target[i*3 + 2] = sep;
955 		}
956 		memcpy(linux_target + levels*3, smb_target+1, smb_target_len); /* +1 to skip leading sep */
957 	} else {
958 		/*
959 		 * This is either an absolute symlink in POSIX-style format
960 		 * or relative SMB symlink from the current directory.
961 		 * These paths have same format as Linux symlinks, so no
962 		 * conversion is needed.
963 		 */
964 out_unhandled_target:
965 		linux_target = smb_target;
966 		smb_target = NULL;
967 	}
968 
969 	if (sep == '\\')
970 		convert_delimiter(linux_target, '/');
971 
972 	rc = 0;
973 	*target = linux_target;
974 
975 	cifs_dbg(FYI, "%s: symlink target: %s\n", __func__, *target);
976 
977 out:
978 	if (rc != 0)
979 		kfree(linux_target);
980 	kfree(smb_target);
981 	return rc;
982 }
983 
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)984 static int parse_reparse_native_symlink(struct reparse_symlink_data_buffer *sym,
985 				 u32 plen,
986 				 struct cifs_sb_info *cifs_sb,
987 				 const char *full_path,
988 				 struct cifs_open_info_data *data)
989 {
990 	unsigned int len;
991 	unsigned int offs;
992 
993 	/* We handle Symbolic Link reparse tag here. See: MS-FSCC 2.1.2.4 */
994 
995 	offs = le16_to_cpu(sym->SubstituteNameOffset);
996 	len = le16_to_cpu(sym->SubstituteNameLength);
997 	if (offs + 20 > plen || offs + len + 20 > plen) {
998 		cifs_dbg(VFS, "srv returned malformed symlink buffer\n");
999 		return -EIO;
1000 	}
1001 
1002 	return smb2_parse_native_symlink(&data->symlink_target,
1003 					 sym->PathBuffer + offs,
1004 					 len,
1005 					 le32_to_cpu(sym->Flags) & SYMLINK_FLAG_RELATIVE,
1006 					 full_path,
1007 					 cifs_sb);
1008 }
1009 
parse_reparse_wsl_symlink(struct reparse_wsl_symlink_data_buffer * buf,struct cifs_sb_info * cifs_sb,struct cifs_open_info_data * data)1010 static int parse_reparse_wsl_symlink(struct reparse_wsl_symlink_data_buffer *buf,
1011 				     struct cifs_sb_info *cifs_sb,
1012 				     struct cifs_open_info_data *data)
1013 {
1014 	int len = le16_to_cpu(buf->ReparseDataLength);
1015 	int data_offset = offsetof(typeof(*buf), Target) - offsetof(typeof(*buf), Version);
1016 	int symname_utf8_len;
1017 	__le16 *symname_utf16;
1018 	int symname_utf16_len;
1019 
1020 	if (len <= data_offset) {
1021 		cifs_dbg(VFS, "srv returned malformed wsl symlink buffer\n");
1022 		return -EIO;
1023 	}
1024 
1025 	/* MS-FSCC 2.1.2.7 defines layout of the Target field only for Version 2. */
1026 	if (le32_to_cpu(buf->Version) != 2) {
1027 		cifs_dbg(VFS, "srv returned unsupported wsl symlink version %u\n", le32_to_cpu(buf->Version));
1028 		return -EIO;
1029 	}
1030 
1031 	/* Target for Version 2 is in UTF-8 but without trailing null-term byte */
1032 	symname_utf8_len = len - data_offset;
1033 	/*
1034 	 * Check that buffer does not contain null byte
1035 	 * because Linux cannot process symlink with null byte.
1036 	 */
1037 	if (strnlen(buf->Target, symname_utf8_len) != symname_utf8_len) {
1038 		cifs_dbg(VFS, "srv returned null byte in wsl symlink target location\n");
1039 		return -EIO;
1040 	}
1041 	symname_utf16 = kzalloc(symname_utf8_len * 2, GFP_KERNEL);
1042 	if (!symname_utf16)
1043 		return -ENOMEM;
1044 	symname_utf16_len = utf8s_to_utf16s(buf->Target, symname_utf8_len,
1045 					    UTF16_LITTLE_ENDIAN,
1046 					    (wchar_t *) symname_utf16, symname_utf8_len * 2);
1047 	if (symname_utf16_len < 0) {
1048 		kfree(symname_utf16);
1049 		return symname_utf16_len;
1050 	}
1051 	symname_utf16_len *= 2; /* utf8s_to_utf16s() returns number of u16 items, not byte length */
1052 
1053 	data->symlink_target = cifs_strndup_from_utf16((u8 *)symname_utf16,
1054 						       symname_utf16_len, true,
1055 						       cifs_sb->local_nls);
1056 	kfree(symname_utf16);
1057 	if (!data->symlink_target)
1058 		return -ENOMEM;
1059 
1060 	return 0;
1061 }
1062 
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)1063 int parse_reparse_point(struct reparse_data_buffer *buf,
1064 			u32 plen, struct cifs_sb_info *cifs_sb,
1065 			const char *full_path,
1066 			struct cifs_open_info_data *data)
1067 {
1068 	data->reparse.buf = buf;
1069 
1070 	/* See MS-FSCC 2.1.2 */
1071 	switch (le32_to_cpu(buf->ReparseTag)) {
1072 	case IO_REPARSE_TAG_NFS:
1073 		return parse_reparse_nfs((struct reparse_nfs_data_buffer *)buf,
1074 					   cifs_sb, data);
1075 	case IO_REPARSE_TAG_SYMLINK:
1076 		return parse_reparse_native_symlink(
1077 			(struct reparse_symlink_data_buffer *)buf,
1078 			plen, cifs_sb, full_path, data);
1079 	case IO_REPARSE_TAG_LX_SYMLINK:
1080 		return parse_reparse_wsl_symlink(
1081 			(struct reparse_wsl_symlink_data_buffer *)buf,
1082 			cifs_sb, data);
1083 	case IO_REPARSE_TAG_AF_UNIX:
1084 	case IO_REPARSE_TAG_LX_FIFO:
1085 	case IO_REPARSE_TAG_LX_CHR:
1086 	case IO_REPARSE_TAG_LX_BLK:
1087 		if (le16_to_cpu(buf->ReparseDataLength) != 0) {
1088 			cifs_dbg(VFS, "srv returned malformed buffer for reparse point: 0x%08x\n",
1089 				 le32_to_cpu(buf->ReparseTag));
1090 			return -EIO;
1091 		}
1092 		return 0;
1093 	default:
1094 		return -EOPNOTSUPP;
1095 	}
1096 }
1097 
smb2_get_reparse_point_buffer(const struct kvec * rsp_iov,u32 * plen)1098 struct reparse_data_buffer *smb2_get_reparse_point_buffer(const struct kvec *rsp_iov,
1099 							  u32 *plen)
1100 {
1101 	struct smb2_ioctl_rsp *io = rsp_iov->iov_base;
1102 	*plen = le32_to_cpu(io->OutputCount);
1103 	return (struct reparse_data_buffer *)((u8 *)io +
1104 					      le32_to_cpu(io->OutputOffset));
1105 }
1106 
wsl_to_fattr(struct cifs_open_info_data * data,struct cifs_sb_info * cifs_sb,u32 tag,struct cifs_fattr * fattr)1107 static bool wsl_to_fattr(struct cifs_open_info_data *data,
1108 			 struct cifs_sb_info *cifs_sb,
1109 			 u32 tag, struct cifs_fattr *fattr)
1110 {
1111 	struct smb2_file_full_ea_info *ea;
1112 	bool have_xattr_dev = false;
1113 	u32 next = 0;
1114 
1115 	switch (tag) {
1116 	case IO_REPARSE_TAG_LX_SYMLINK:
1117 		fattr->cf_mode |= S_IFLNK;
1118 		break;
1119 	case IO_REPARSE_TAG_LX_FIFO:
1120 		fattr->cf_mode |= S_IFIFO;
1121 		break;
1122 	case IO_REPARSE_TAG_AF_UNIX:
1123 		fattr->cf_mode |= S_IFSOCK;
1124 		break;
1125 	case IO_REPARSE_TAG_LX_CHR:
1126 		fattr->cf_mode |= S_IFCHR;
1127 		break;
1128 	case IO_REPARSE_TAG_LX_BLK:
1129 		fattr->cf_mode |= S_IFBLK;
1130 		break;
1131 	}
1132 
1133 	if (!data->wsl.eas_len)
1134 		goto out;
1135 
1136 	ea = (struct smb2_file_full_ea_info *)data->wsl.eas;
1137 	do {
1138 		const char *name;
1139 		void *v;
1140 		u8 nlen;
1141 
1142 		ea = (void *)((u8 *)ea + next);
1143 		next = le32_to_cpu(ea->next_entry_offset);
1144 		if (!le16_to_cpu(ea->ea_value_length))
1145 			continue;
1146 
1147 		name = ea->ea_data;
1148 		nlen = ea->ea_name_length;
1149 		v = (void *)((u8 *)ea->ea_data + ea->ea_name_length + 1);
1150 
1151 		if (!strncmp(name, SMB2_WSL_XATTR_UID, nlen))
1152 			fattr->cf_uid = wsl_make_kuid(cifs_sb, v);
1153 		else if (!strncmp(name, SMB2_WSL_XATTR_GID, nlen))
1154 			fattr->cf_gid = wsl_make_kgid(cifs_sb, v);
1155 		else if (!strncmp(name, SMB2_WSL_XATTR_MODE, nlen)) {
1156 			/* File type in reparse point tag and in xattr mode must match. */
1157 			if (S_DT(fattr->cf_mode) != S_DT(le32_to_cpu(*(__le32 *)v)))
1158 				return false;
1159 			fattr->cf_mode = (umode_t)le32_to_cpu(*(__le32 *)v);
1160 		} else if (!strncmp(name, SMB2_WSL_XATTR_DEV, nlen)) {
1161 			fattr->cf_rdev = reparse_mkdev(v);
1162 			have_xattr_dev = true;
1163 		}
1164 	} while (next);
1165 out:
1166 
1167 	/* Major and minor numbers for char and block devices are mandatory. */
1168 	if (!have_xattr_dev && (tag == IO_REPARSE_TAG_LX_CHR || tag == IO_REPARSE_TAG_LX_BLK))
1169 		return false;
1170 
1171 	return true;
1172 }
1173 
posix_reparse_to_fattr(struct cifs_sb_info * cifs_sb,struct cifs_fattr * fattr,struct cifs_open_info_data * data)1174 static bool posix_reparse_to_fattr(struct cifs_sb_info *cifs_sb,
1175 				   struct cifs_fattr *fattr,
1176 				   struct cifs_open_info_data *data)
1177 {
1178 	struct reparse_nfs_data_buffer *buf = (struct reparse_nfs_data_buffer *)data->reparse.buf;
1179 
1180 	if (buf == NULL)
1181 		return true;
1182 
1183 	if (le16_to_cpu(buf->ReparseDataLength) < sizeof(buf->InodeType)) {
1184 		WARN_ON_ONCE(1);
1185 		return false;
1186 	}
1187 
1188 	switch (le64_to_cpu(buf->InodeType)) {
1189 	case NFS_SPECFILE_CHR:
1190 		if (le16_to_cpu(buf->ReparseDataLength) != sizeof(buf->InodeType) + 8) {
1191 			WARN_ON_ONCE(1);
1192 			return false;
1193 		}
1194 		fattr->cf_mode |= S_IFCHR;
1195 		fattr->cf_rdev = reparse_mkdev(buf->DataBuffer);
1196 		break;
1197 	case NFS_SPECFILE_BLK:
1198 		if (le16_to_cpu(buf->ReparseDataLength) != sizeof(buf->InodeType) + 8) {
1199 			WARN_ON_ONCE(1);
1200 			return false;
1201 		}
1202 		fattr->cf_mode |= S_IFBLK;
1203 		fattr->cf_rdev = reparse_mkdev(buf->DataBuffer);
1204 		break;
1205 	case NFS_SPECFILE_FIFO:
1206 		fattr->cf_mode |= S_IFIFO;
1207 		break;
1208 	case NFS_SPECFILE_SOCK:
1209 		fattr->cf_mode |= S_IFSOCK;
1210 		break;
1211 	case NFS_SPECFILE_LNK:
1212 		fattr->cf_mode |= S_IFLNK;
1213 		break;
1214 	default:
1215 		WARN_ON_ONCE(1);
1216 		return false;
1217 	}
1218 	return true;
1219 }
1220 
cifs_reparse_point_to_fattr(struct cifs_sb_info * cifs_sb,struct cifs_fattr * fattr,struct cifs_open_info_data * data)1221 bool cifs_reparse_point_to_fattr(struct cifs_sb_info *cifs_sb,
1222 				 struct cifs_fattr *fattr,
1223 				 struct cifs_open_info_data *data)
1224 {
1225 	u32 tag = data->reparse.tag;
1226 	bool ok;
1227 
1228 	switch (tag) {
1229 	case IO_REPARSE_TAG_LX_SYMLINK:
1230 	case IO_REPARSE_TAG_LX_FIFO:
1231 	case IO_REPARSE_TAG_AF_UNIX:
1232 	case IO_REPARSE_TAG_LX_CHR:
1233 	case IO_REPARSE_TAG_LX_BLK:
1234 		ok = wsl_to_fattr(data, cifs_sb, tag, fattr);
1235 		if (!ok)
1236 			return false;
1237 		break;
1238 	case IO_REPARSE_TAG_NFS:
1239 		ok = posix_reparse_to_fattr(cifs_sb, fattr, data);
1240 		if (!ok)
1241 			return false;
1242 		break;
1243 	case 0: /* SMB1 symlink */
1244 	case IO_REPARSE_TAG_SYMLINK:
1245 		fattr->cf_mode |= S_IFLNK;
1246 		break;
1247 	default:
1248 		if (!(fattr->cf_cifsattrs & ATTR_DIRECTORY))
1249 			return false;
1250 		if (!IS_REPARSE_TAG_NAME_SURROGATE(tag) &&
1251 		    tag != IO_REPARSE_TAG_INTERNAL)
1252 			return false;
1253 		/* See cifs_create_junction_fattr() */
1254 		fattr->cf_mode = S_IFDIR | 0711;
1255 		break;
1256 	}
1257 
1258 	fattr->cf_dtype = S_DT(fattr->cf_mode);
1259 	return true;
1260 }
1261