xref: /linux/fs/smb/client/smb2file.c (revision 8a848efd482be65d488e888f96812d8729ea64ea)
1 // SPDX-License-Identifier: LGPL-2.1
2 /*
3  *
4  *   Copyright (C) International Business Machines  Corp., 2002, 2011
5  *   Author(s): Steve French (sfrench@us.ibm.com),
6  *              Pavel Shilovsky ((pshilovsky@samba.org) 2012
7  *
8  */
9 #include <linux/fs.h>
10 #include <linux/filelock.h>
11 #include <linux/stat.h>
12 #include <linux/slab.h>
13 #include <linux/pagemap.h>
14 #include <asm/div64.h>
15 #include "cifsfs.h"
16 #include "cifsglob.h"
17 #include "cifsproto.h"
18 #include "cifs_debug.h"
19 #include "cifs_fs_sb.h"
20 #include "cifs_unicode.h"
21 #include "fscache.h"
22 #include "smb2proto.h"
23 #include "../common/smb2status.h"
24 #include "../common/smbfsctl.h"
25 
26 static struct smb2_symlink_err_rsp *symlink_data(const struct kvec *iov)
27 {
28 	struct smb2_err_rsp *err = iov->iov_base;
29 	struct smb2_symlink_err_rsp *sym = ERR_PTR(-EINVAL);
30 	u32 len;
31 
32 	if (err->ErrorContextCount) {
33 		struct smb2_error_context_rsp *p, *end;
34 
35 		len = (u32)err->ErrorContextCount * (offsetof(struct smb2_error_context_rsp,
36 							      ErrorContextData) +
37 						     sizeof(struct smb2_symlink_err_rsp));
38 		if (le32_to_cpu(err->ByteCount) < len || iov->iov_len < len + sizeof(*err) + 1)
39 			return ERR_PTR(-EINVAL);
40 
41 		p = (struct smb2_error_context_rsp *)err->ErrorData;
42 		end = (struct smb2_error_context_rsp *)((u8 *)err + iov->iov_len);
43 		do {
44 			if (le32_to_cpu(p->ErrorId) == SMB2_ERROR_ID_DEFAULT) {
45 				sym = (struct smb2_symlink_err_rsp *)p->ErrorContextData;
46 				break;
47 			}
48 			cifs_dbg(FYI, "%s: skipping unhandled error context: 0x%x\n",
49 				 __func__, le32_to_cpu(p->ErrorId));
50 
51 			len = ALIGN(le32_to_cpu(p->ErrorDataLength), 8);
52 			p = (struct smb2_error_context_rsp *)(p->ErrorContextData + len);
53 		} while (p < end);
54 	} else if (le32_to_cpu(err->ByteCount) >= sizeof(*sym) &&
55 		   iov->iov_len >= SMB2_SYMLINK_STRUCT_SIZE) {
56 		sym = (struct smb2_symlink_err_rsp *)err->ErrorData;
57 	}
58 
59 	if (!IS_ERR(sym) && (le32_to_cpu(sym->SymLinkErrorTag) != SYMLINK_ERROR_TAG ||
60 			     le32_to_cpu(sym->ReparseTag) != IO_REPARSE_TAG_SYMLINK))
61 		sym = ERR_PTR(-EINVAL);
62 
63 	return sym;
64 }
65 
66 int smb2_fix_symlink_target_type(char **target, bool directory, struct cifs_sb_info *cifs_sb)
67 {
68 	char *buf;
69 	int len;
70 
71 	/*
72 	 * POSIX server does not distinguish between symlinks to file and
73 	 * symlink directory. So nothing is needed to fix on the client side.
74 	 */
75 	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS)
76 		return 0;
77 
78 	if (!*target)
79 		return smb_EIO(smb_eio_trace_null_pointers);
80 
81 	len = strlen(*target);
82 	if (!len)
83 		return smb_EIO1(smb_eio_trace_sym_target_len, len);
84 
85 	/*
86 	 * If this is directory symlink and it does not have trailing slash then
87 	 * append it. Trailing slash simulates Windows/SMB behavior which do not
88 	 * allow resolving directory symlink to file.
89 	 */
90 	if (directory && (*target)[len-1] != '/') {
91 		buf = krealloc(*target, len+2, GFP_KERNEL);
92 		if (!buf)
93 			return -ENOMEM;
94 		buf[len] = '/';
95 		buf[len+1] = '\0';
96 		*target = buf;
97 		len++;
98 	}
99 
100 	/*
101 	 * If this is a file (non-directory) symlink and it points to path name
102 	 * with trailing slash then this is an invalid symlink because file name
103 	 * cannot contain slash character. File name with slash is invalid on
104 	 * both Windows and Linux systems. So return an error for such symlink.
105 	 */
106 	if (!directory && (*target)[len-1] == '/')
107 		return smb_EIO(smb_eio_trace_sym_slash);
108 
109 	return 0;
110 }
111 
112 int smb2_parse_symlink_response(struct cifs_sb_info *cifs_sb, const struct kvec *iov,
113 				const char *full_path, char **path)
114 {
115 	struct smb2_symlink_err_rsp *sym;
116 	unsigned int sub_offs, sub_len;
117 	unsigned int print_offs, print_len;
118 
119 	if (!cifs_sb || !iov || !iov->iov_base || !iov->iov_len || !path)
120 		return -EINVAL;
121 
122 	sym = symlink_data(iov);
123 	if (IS_ERR(sym))
124 		return PTR_ERR(sym);
125 
126 	sub_len = le16_to_cpu(sym->SubstituteNameLength);
127 	sub_offs = le16_to_cpu(sym->SubstituteNameOffset);
128 	print_len = le16_to_cpu(sym->PrintNameLength);
129 	print_offs = le16_to_cpu(sym->PrintNameOffset);
130 
131 	if (iov->iov_len < SMB2_SYMLINK_STRUCT_SIZE + sub_offs + sub_len ||
132 	    iov->iov_len < SMB2_SYMLINK_STRUCT_SIZE + print_offs + print_len)
133 		return -EINVAL;
134 
135 	return smb2_parse_native_symlink(path,
136 					 (char *)sym->PathBuffer + sub_offs,
137 					 sub_len,
138 					 le32_to_cpu(sym->Flags) & SYMLINK_FLAG_RELATIVE,
139 					 full_path,
140 					 cifs_sb);
141 }
142 
143 int smb2_open_file(const unsigned int xid, struct cifs_open_parms *oparms,
144 		   __u32 *oplock, void *buf)
145 {
146 	int rc;
147 	__le16 *smb2_path;
148 	__u8 smb2_oplock;
149 	struct cifs_open_info_data *data = buf;
150 	struct smb2_file_all_info file_info = {};
151 	struct smb2_file_all_info *smb2_data = data ? &file_info : NULL;
152 	struct kvec err_iov = {};
153 	int err_buftype = CIFS_NO_BUFFER;
154 	struct cifs_fid *fid = oparms->fid;
155 	struct network_resiliency_req nr_ioctl_req;
156 	bool retry_without_read_attributes = false;
157 
158 	smb2_path = cifs_convert_path_to_utf16(oparms->path, oparms->cifs_sb);
159 	if (smb2_path == NULL)
160 		return -ENOMEM;
161 
162 	/*
163 	 * GENERIC_READ, GENERIC_EXECUTE, GENERIC_ALL and MAXIMUM_ALLOWED
164 	 * contains also FILE_READ_ATTRIBUTES access right. So do not append
165 	 * FILE_READ_ATTRIBUTES when not needed and prevent calling code path
166 	 * for retry_without_read_attributes.
167 	 */
168 	if (!(oparms->desired_access & FILE_READ_ATTRIBUTES) &&
169 	    !(oparms->desired_access & GENERIC_READ) &&
170 	    !(oparms->desired_access & GENERIC_EXECUTE) &&
171 	    !(oparms->desired_access & GENERIC_ALL) &&
172 	    !(oparms->desired_access & MAXIMUM_ALLOWED)) {
173 		oparms->desired_access |= FILE_READ_ATTRIBUTES;
174 		retry_without_read_attributes = true;
175 	}
176 	smb2_oplock = SMB2_OPLOCK_LEVEL_BATCH;
177 
178 	rc = SMB2_open(xid, oparms, smb2_path, &smb2_oplock, smb2_data, NULL, &err_iov,
179 		       &err_buftype);
180 	if (rc == -EACCES && retry_without_read_attributes) {
181 		free_rsp_buf(err_buftype, err_iov.iov_base);
182 		memset(&err_iov, 0, sizeof(err_iov));
183 		err_buftype = CIFS_NO_BUFFER;
184 		oparms->desired_access &= ~FILE_READ_ATTRIBUTES;
185 		rc = SMB2_open(xid, oparms, smb2_path, &smb2_oplock, smb2_data, NULL, &err_iov,
186 			       &err_buftype);
187 	}
188 	if (rc && data) {
189 		struct smb2_hdr *hdr = err_iov.iov_base;
190 
191 		if (unlikely(!err_iov.iov_base || err_buftype == CIFS_NO_BUFFER))
192 			goto out;
193 		if (hdr->Status == STATUS_STOPPED_ON_SYMLINK) {
194 			rc = smb2_parse_symlink_response(oparms->cifs_sb, &err_iov,
195 							 oparms->path,
196 							 &data->symlink_target);
197 			if (!rc) {
198 				memset(smb2_data, 0, sizeof(*smb2_data));
199 				oparms->create_options |= OPEN_REPARSE_POINT;
200 				rc = SMB2_open(xid, oparms, smb2_path, &smb2_oplock, smb2_data,
201 					       NULL, NULL, NULL);
202 				oparms->create_options &= ~OPEN_REPARSE_POINT;
203 			}
204 			if (!rc) {
205 				bool directory = le32_to_cpu(data->fi.Attributes) & ATTR_DIRECTORY;
206 				rc = smb2_fix_symlink_target_type(&data->symlink_target,
207 								  directory, oparms->cifs_sb);
208 			}
209 		}
210 	}
211 
212 	if (rc)
213 		goto out;
214 
215 	if (oparms->tcon->use_resilient) {
216 		/* default timeout is 0, servers pick default (120 seconds) */
217 		nr_ioctl_req.Timeout =
218 			cpu_to_le32(oparms->tcon->handle_timeout);
219 		nr_ioctl_req.Reserved = 0;
220 		rc = SMB2_ioctl(xid, oparms->tcon, fid->persistent_fid,
221 			fid->volatile_fid, FSCTL_LMR_REQUEST_RESILIENCY,
222 			(char *)&nr_ioctl_req, sizeof(nr_ioctl_req),
223 			CIFSMaxBufSize, NULL, NULL /* no return info */);
224 		if (rc == -EOPNOTSUPP) {
225 			cifs_dbg(VFS,
226 			     "resiliency not supported by server, disabling\n");
227 			oparms->tcon->use_resilient = false;
228 		} else if (rc)
229 			cifs_dbg(FYI, "error %d setting resiliency\n", rc);
230 
231 		rc = 0;
232 	}
233 
234 	if (smb2_data) {
235 		/* if open response does not have IndexNumber field - get it */
236 		if (smb2_data->IndexNumber == 0) {
237 			rc = SMB2_get_srv_num(xid, oparms->tcon,
238 				      fid->persistent_fid,
239 				      fid->volatile_fid,
240 				      &smb2_data->IndexNumber);
241 			if (rc) {
242 				/*
243 				 * let get_inode_info disable server inode
244 				 * numbers
245 				 */
246 				smb2_data->IndexNumber = 0;
247 				rc = 0;
248 			}
249 		}
250 		memcpy(&data->fi, smb2_data, sizeof(data->fi));
251 	}
252 
253 	*oplock = smb2_oplock;
254 out:
255 	free_rsp_buf(err_buftype, err_iov.iov_base);
256 	kfree(smb2_path);
257 	return rc;
258 }
259 
260 int
261 smb2_unlock_range(struct cifsFileInfo *cfile, struct file_lock *flock,
262 		  const unsigned int xid)
263 {
264 	int rc = 0, stored_rc;
265 	unsigned int max_num, num = 0, max_buf;
266 	struct smb2_lock_element *buf, *cur;
267 	struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
268 	struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
269 	struct cifsLockInfo *li, *tmp;
270 	__u64 length = 1 + flock->fl_end - flock->fl_start;
271 	LIST_HEAD(tmp_llist);
272 
273 	/*
274 	 * Accessing maxBuf is racy with cifs_reconnect - need to store value
275 	 * and check it before using.
276 	 */
277 	max_buf = tcon->ses->server->maxBuf;
278 	if (max_buf < sizeof(struct smb2_lock_element))
279 		return -EINVAL;
280 
281 	BUILD_BUG_ON(sizeof(struct smb2_lock_element) > PAGE_SIZE);
282 	max_buf = min_t(unsigned int, max_buf, PAGE_SIZE);
283 	max_num = max_buf / sizeof(struct smb2_lock_element);
284 	buf = kcalloc(max_num, sizeof(struct smb2_lock_element), GFP_KERNEL);
285 	if (!buf)
286 		return -ENOMEM;
287 
288 	cur = buf;
289 
290 	cifs_down_write(&cinode->lock_sem);
291 	list_for_each_entry_safe(li, tmp, &cfile->llist->locks, llist) {
292 		if (flock->fl_start > li->offset ||
293 		    (flock->fl_start + length) <
294 		    (li->offset + li->length))
295 			continue;
296 		if (current->tgid != li->pid)
297 			/*
298 			 * flock and OFD lock are associated with an open
299 			 * file description, not the process.
300 			 */
301 			if (!(flock->c.flc_flags & (FL_FLOCK | FL_OFDLCK)))
302 				continue;
303 		if (cinode->can_cache_brlcks) {
304 			/*
305 			 * We can cache brlock requests - simply remove a lock
306 			 * from the file's list.
307 			 */
308 			list_del(&li->llist);
309 			cifs_del_lock_waiters(li);
310 			kfree(li);
311 			continue;
312 		}
313 		cur->Length = cpu_to_le64(li->length);
314 		cur->Offset = cpu_to_le64(li->offset);
315 		cur->Flags = cpu_to_le32(SMB2_LOCKFLAG_UNLOCK);
316 		/*
317 		 * We need to save a lock here to let us add it again to the
318 		 * file's list if the unlock range request fails on the server.
319 		 */
320 		list_move(&li->llist, &tmp_llist);
321 		if (++num == max_num) {
322 			stored_rc = smb2_lockv(xid, tcon,
323 					       cfile->fid.persistent_fid,
324 					       cfile->fid.volatile_fid,
325 					       current->tgid, num, buf);
326 			if (stored_rc) {
327 				/*
328 				 * We failed on the unlock range request - add
329 				 * all locks from the tmp list to the head of
330 				 * the file's list.
331 				 */
332 				cifs_move_llist(&tmp_llist,
333 						&cfile->llist->locks);
334 				rc = stored_rc;
335 			} else
336 				/*
337 				 * The unlock range request succeed - free the
338 				 * tmp list.
339 				 */
340 				cifs_free_llist(&tmp_llist);
341 			cur = buf;
342 			num = 0;
343 		} else
344 			cur++;
345 	}
346 	if (num) {
347 		stored_rc = smb2_lockv(xid, tcon, cfile->fid.persistent_fid,
348 				       cfile->fid.volatile_fid, current->tgid,
349 				       num, buf);
350 		if (stored_rc) {
351 			cifs_move_llist(&tmp_llist, &cfile->llist->locks);
352 			rc = stored_rc;
353 		} else
354 			cifs_free_llist(&tmp_llist);
355 	}
356 	up_write(&cinode->lock_sem);
357 
358 	kfree(buf);
359 	return rc;
360 }
361 
362 static int
363 smb2_push_mand_fdlocks(struct cifs_fid_locks *fdlocks, const unsigned int xid,
364 		       struct smb2_lock_element *buf, unsigned int max_num)
365 {
366 	int rc = 0, stored_rc;
367 	struct cifsFileInfo *cfile = fdlocks->cfile;
368 	struct cifsLockInfo *li;
369 	unsigned int num = 0;
370 	struct smb2_lock_element *cur = buf;
371 	struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
372 
373 	list_for_each_entry(li, &fdlocks->locks, llist) {
374 		cur->Length = cpu_to_le64(li->length);
375 		cur->Offset = cpu_to_le64(li->offset);
376 		cur->Flags = cpu_to_le32(li->type |
377 						SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
378 		if (++num == max_num) {
379 			stored_rc = smb2_lockv(xid, tcon,
380 					       cfile->fid.persistent_fid,
381 					       cfile->fid.volatile_fid,
382 					       current->tgid, num, buf);
383 			if (stored_rc)
384 				rc = stored_rc;
385 			cur = buf;
386 			num = 0;
387 		} else
388 			cur++;
389 	}
390 	if (num) {
391 		stored_rc = smb2_lockv(xid, tcon,
392 				       cfile->fid.persistent_fid,
393 				       cfile->fid.volatile_fid,
394 				       current->tgid, num, buf);
395 		if (stored_rc)
396 			rc = stored_rc;
397 	}
398 
399 	return rc;
400 }
401 
402 int
403 smb2_push_mandatory_locks(struct cifsFileInfo *cfile)
404 {
405 	int rc = 0, stored_rc;
406 	unsigned int xid;
407 	unsigned int max_num, max_buf;
408 	struct smb2_lock_element *buf;
409 	struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
410 	struct cifs_fid_locks *fdlocks;
411 
412 	xid = get_xid();
413 
414 	/*
415 	 * Accessing maxBuf is racy with cifs_reconnect - need to store value
416 	 * and check it for zero before using.
417 	 */
418 	max_buf = tlink_tcon(cfile->tlink)->ses->server->maxBuf;
419 	if (max_buf < sizeof(struct smb2_lock_element)) {
420 		free_xid(xid);
421 		return -EINVAL;
422 	}
423 
424 	BUILD_BUG_ON(sizeof(struct smb2_lock_element) > PAGE_SIZE);
425 	max_buf = min_t(unsigned int, max_buf, PAGE_SIZE);
426 	max_num = max_buf / sizeof(struct smb2_lock_element);
427 	buf = kcalloc(max_num, sizeof(struct smb2_lock_element), GFP_KERNEL);
428 	if (!buf) {
429 		free_xid(xid);
430 		return -ENOMEM;
431 	}
432 
433 	list_for_each_entry(fdlocks, &cinode->llist, llist) {
434 		stored_rc = smb2_push_mand_fdlocks(fdlocks, xid, buf, max_num);
435 		if (stored_rc)
436 			rc = stored_rc;
437 	}
438 
439 	kfree(buf);
440 	free_xid(xid);
441 	return rc;
442 }
443