xref: /linux/fs/smb/client/smb2file.c (revision 3d99347a2e1ae60d9368b1d734290bab1acde0ce)
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 "cifspdu.h"
17 #include "cifsglob.h"
18 #include "cifsproto.h"
19 #include "cifs_debug.h"
20 #include "cifs_fs_sb.h"
21 #include "cifs_unicode.h"
22 #include "fscache.h"
23 #include "smb2proto.h"
24 #include "../common/smb2status.h"
25 
symlink_data(const struct kvec * iov)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 
smb2_fix_symlink_target_type(char ** target,bool directory,struct cifs_sb_info * cifs_sb)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 
smb2_parse_symlink_response(struct cifs_sb_info * cifs_sb,const struct kvec * iov,const char * full_path,char ** path)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 
smb2_open_file(const unsigned int xid,struct cifs_open_parms * oparms,__u32 * oplock,void * buf)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 		oparms->desired_access &= ~FILE_READ_ATTRIBUTES;
182 		rc = SMB2_open(xid, oparms, smb2_path, &smb2_oplock, smb2_data, NULL, &err_iov,
183 			       &err_buftype);
184 	}
185 	if (rc && data) {
186 		struct smb2_hdr *hdr = err_iov.iov_base;
187 
188 		if (unlikely(!err_iov.iov_base || err_buftype == CIFS_NO_BUFFER))
189 			goto out;
190 		if (hdr->Status == STATUS_STOPPED_ON_SYMLINK) {
191 			rc = smb2_parse_symlink_response(oparms->cifs_sb, &err_iov,
192 							 oparms->path,
193 							 &data->symlink_target);
194 			if (!rc) {
195 				memset(smb2_data, 0, sizeof(*smb2_data));
196 				oparms->create_options |= OPEN_REPARSE_POINT;
197 				rc = SMB2_open(xid, oparms, smb2_path, &smb2_oplock, smb2_data,
198 					       NULL, NULL, NULL);
199 				oparms->create_options &= ~OPEN_REPARSE_POINT;
200 			}
201 			if (!rc) {
202 				bool directory = le32_to_cpu(data->fi.Attributes) & ATTR_DIRECTORY;
203 				rc = smb2_fix_symlink_target_type(&data->symlink_target,
204 								  directory, oparms->cifs_sb);
205 			}
206 		}
207 	}
208 
209 	if (rc)
210 		goto out;
211 
212 	if (oparms->tcon->use_resilient) {
213 		/* default timeout is 0, servers pick default (120 seconds) */
214 		nr_ioctl_req.Timeout =
215 			cpu_to_le32(oparms->tcon->handle_timeout);
216 		nr_ioctl_req.Reserved = 0;
217 		rc = SMB2_ioctl(xid, oparms->tcon, fid->persistent_fid,
218 			fid->volatile_fid, FSCTL_LMR_REQUEST_RESILIENCY,
219 			(char *)&nr_ioctl_req, sizeof(nr_ioctl_req),
220 			CIFSMaxBufSize, NULL, NULL /* no return info */);
221 		if (rc == -EOPNOTSUPP) {
222 			cifs_dbg(VFS,
223 			     "resiliency not supported by server, disabling\n");
224 			oparms->tcon->use_resilient = false;
225 		} else if (rc)
226 			cifs_dbg(FYI, "error %d setting resiliency\n", rc);
227 
228 		rc = 0;
229 	}
230 
231 	if (smb2_data) {
232 		/* if open response does not have IndexNumber field - get it */
233 		if (smb2_data->IndexNumber == 0) {
234 			rc = SMB2_get_srv_num(xid, oparms->tcon,
235 				      fid->persistent_fid,
236 				      fid->volatile_fid,
237 				      &smb2_data->IndexNumber);
238 			if (rc) {
239 				/*
240 				 * let get_inode_info disable server inode
241 				 * numbers
242 				 */
243 				smb2_data->IndexNumber = 0;
244 				rc = 0;
245 			}
246 		}
247 		memcpy(&data->fi, smb2_data, sizeof(data->fi));
248 	}
249 
250 	*oplock = smb2_oplock;
251 out:
252 	free_rsp_buf(err_buftype, err_iov.iov_base);
253 	kfree(smb2_path);
254 	return rc;
255 }
256 
257 int
smb2_unlock_range(struct cifsFileInfo * cfile,struct file_lock * flock,const unsigned int xid)258 smb2_unlock_range(struct cifsFileInfo *cfile, struct file_lock *flock,
259 		  const unsigned int xid)
260 {
261 	int rc = 0, stored_rc;
262 	unsigned int max_num, num = 0, max_buf;
263 	struct smb2_lock_element *buf, *cur;
264 	struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
265 	struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
266 	struct cifsLockInfo *li, *tmp;
267 	__u64 length = 1 + flock->fl_end - flock->fl_start;
268 	LIST_HEAD(tmp_llist);
269 
270 	/*
271 	 * Accessing maxBuf is racy with cifs_reconnect - need to store value
272 	 * and check it before using.
273 	 */
274 	max_buf = tcon->ses->server->maxBuf;
275 	if (max_buf < sizeof(struct smb2_lock_element))
276 		return -EINVAL;
277 
278 	BUILD_BUG_ON(sizeof(struct smb2_lock_element) > PAGE_SIZE);
279 	max_buf = min_t(unsigned int, max_buf, PAGE_SIZE);
280 	max_num = max_buf / sizeof(struct smb2_lock_element);
281 	buf = kcalloc(max_num, sizeof(struct smb2_lock_element), GFP_KERNEL);
282 	if (!buf)
283 		return -ENOMEM;
284 
285 	cur = buf;
286 
287 	cifs_down_write(&cinode->lock_sem);
288 	list_for_each_entry_safe(li, tmp, &cfile->llist->locks, llist) {
289 		if (flock->fl_start > li->offset ||
290 		    (flock->fl_start + length) <
291 		    (li->offset + li->length))
292 			continue;
293 		if (current->tgid != li->pid)
294 			/*
295 			 * flock and OFD lock are associated with an open
296 			 * file description, not the process.
297 			 */
298 			if (!(flock->c.flc_flags & (FL_FLOCK | FL_OFDLCK)))
299 				continue;
300 		if (cinode->can_cache_brlcks) {
301 			/*
302 			 * We can cache brlock requests - simply remove a lock
303 			 * from the file's list.
304 			 */
305 			list_del(&li->llist);
306 			cifs_del_lock_waiters(li);
307 			kfree(li);
308 			continue;
309 		}
310 		cur->Length = cpu_to_le64(li->length);
311 		cur->Offset = cpu_to_le64(li->offset);
312 		cur->Flags = cpu_to_le32(SMB2_LOCKFLAG_UNLOCK);
313 		/*
314 		 * We need to save a lock here to let us add it again to the
315 		 * file's list if the unlock range request fails on the server.
316 		 */
317 		list_move(&li->llist, &tmp_llist);
318 		if (++num == max_num) {
319 			stored_rc = smb2_lockv(xid, tcon,
320 					       cfile->fid.persistent_fid,
321 					       cfile->fid.volatile_fid,
322 					       current->tgid, num, buf);
323 			if (stored_rc) {
324 				/*
325 				 * We failed on the unlock range request - add
326 				 * all locks from the tmp list to the head of
327 				 * the file's list.
328 				 */
329 				cifs_move_llist(&tmp_llist,
330 						&cfile->llist->locks);
331 				rc = stored_rc;
332 			} else
333 				/*
334 				 * The unlock range request succeed - free the
335 				 * tmp list.
336 				 */
337 				cifs_free_llist(&tmp_llist);
338 			cur = buf;
339 			num = 0;
340 		} else
341 			cur++;
342 	}
343 	if (num) {
344 		stored_rc = smb2_lockv(xid, tcon, cfile->fid.persistent_fid,
345 				       cfile->fid.volatile_fid, current->tgid,
346 				       num, buf);
347 		if (stored_rc) {
348 			cifs_move_llist(&tmp_llist, &cfile->llist->locks);
349 			rc = stored_rc;
350 		} else
351 			cifs_free_llist(&tmp_llist);
352 	}
353 	up_write(&cinode->lock_sem);
354 
355 	kfree(buf);
356 	return rc;
357 }
358 
359 static int
smb2_push_mand_fdlocks(struct cifs_fid_locks * fdlocks,const unsigned int xid,struct smb2_lock_element * buf,unsigned int max_num)360 smb2_push_mand_fdlocks(struct cifs_fid_locks *fdlocks, const unsigned int xid,
361 		       struct smb2_lock_element *buf, unsigned int max_num)
362 {
363 	int rc = 0, stored_rc;
364 	struct cifsFileInfo *cfile = fdlocks->cfile;
365 	struct cifsLockInfo *li;
366 	unsigned int num = 0;
367 	struct smb2_lock_element *cur = buf;
368 	struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
369 
370 	list_for_each_entry(li, &fdlocks->locks, llist) {
371 		cur->Length = cpu_to_le64(li->length);
372 		cur->Offset = cpu_to_le64(li->offset);
373 		cur->Flags = cpu_to_le32(li->type |
374 						SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
375 		if (++num == max_num) {
376 			stored_rc = smb2_lockv(xid, tcon,
377 					       cfile->fid.persistent_fid,
378 					       cfile->fid.volatile_fid,
379 					       current->tgid, num, buf);
380 			if (stored_rc)
381 				rc = stored_rc;
382 			cur = buf;
383 			num = 0;
384 		} else
385 			cur++;
386 	}
387 	if (num) {
388 		stored_rc = smb2_lockv(xid, tcon,
389 				       cfile->fid.persistent_fid,
390 				       cfile->fid.volatile_fid,
391 				       current->tgid, num, buf);
392 		if (stored_rc)
393 			rc = stored_rc;
394 	}
395 
396 	return rc;
397 }
398 
399 int
smb2_push_mandatory_locks(struct cifsFileInfo * cfile)400 smb2_push_mandatory_locks(struct cifsFileInfo *cfile)
401 {
402 	int rc = 0, stored_rc;
403 	unsigned int xid;
404 	unsigned int max_num, max_buf;
405 	struct smb2_lock_element *buf;
406 	struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
407 	struct cifs_fid_locks *fdlocks;
408 
409 	xid = get_xid();
410 
411 	/*
412 	 * Accessing maxBuf is racy with cifs_reconnect - need to store value
413 	 * and check it for zero before using.
414 	 */
415 	max_buf = tlink_tcon(cfile->tlink)->ses->server->maxBuf;
416 	if (max_buf < sizeof(struct smb2_lock_element)) {
417 		free_xid(xid);
418 		return -EINVAL;
419 	}
420 
421 	BUILD_BUG_ON(sizeof(struct smb2_lock_element) > PAGE_SIZE);
422 	max_buf = min_t(unsigned int, max_buf, PAGE_SIZE);
423 	max_num = max_buf / sizeof(struct smb2_lock_element);
424 	buf = kcalloc(max_num, sizeof(struct smb2_lock_element), GFP_KERNEL);
425 	if (!buf) {
426 		free_xid(xid);
427 		return -ENOMEM;
428 	}
429 
430 	list_for_each_entry(fdlocks, &cinode->llist, llist) {
431 		stored_rc = smb2_push_mand_fdlocks(fdlocks, xid, buf, max_num);
432 		if (stored_rc)
433 			rc = stored_rc;
434 	}
435 
436 	kfree(buf);
437 	free_xid(xid);
438 	return rc;
439 }
440