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