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