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