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