xref: /linux/fs/smb/client/ioctl.c (revision 7d32e779eb9add47bfdb4731c4d4ff443a7f7fa6)
1 // SPDX-License-Identifier: LGPL-2.1
2 /*
3  *
4  *   vfs operations that deal with io control
5  *
6  *   Copyright (C) International Business Machines  Corp., 2005,2013
7  *   Author(s): Steve French (sfrench@us.ibm.com)
8  *
9  */
10 
11 #include <linux/fs.h>
12 #include <linux/file.h>
13 #include <linux/mount.h>
14 #include <linux/mm.h>
15 #include <linux/pagemap.h>
16 #include "cifspdu.h"
17 #include "cifsglob.h"
18 #include "cifsproto.h"
19 #include "cifs_debug.h"
20 #include "cifsfs.h"
21 #include "cifs_ioctl.h"
22 #include "smb2proto.h"
23 #include "smb2glob.h"
24 #include <linux/btrfs.h>
25 
26 static long cifs_ioctl_query_info(unsigned int xid, struct file *filep,
27 				  unsigned long p)
28 {
29 	struct inode *inode = file_inode(filep);
30 	struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
31 	struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
32 	struct dentry *dentry = filep->f_path.dentry;
33 	const unsigned char *path;
34 	void *page = alloc_dentry_path();
35 	__le16 *utf16_path = NULL, root_path;
36 	int rc = 0;
37 
38 	path = build_path_from_dentry(dentry, page);
39 	if (IS_ERR(path)) {
40 		free_dentry_path(page);
41 		return PTR_ERR(path);
42 	}
43 
44 	cifs_dbg(FYI, "%s %s\n", __func__, path);
45 
46 	if (!path[0]) {
47 		root_path = 0;
48 		utf16_path = &root_path;
49 	} else {
50 		utf16_path = cifs_convert_path_to_utf16(path + 1, cifs_sb);
51 		if (!utf16_path) {
52 			rc = -ENOMEM;
53 			goto ici_exit;
54 		}
55 	}
56 
57 	if (tcon->ses->server->ops->ioctl_query_info)
58 		rc = tcon->ses->server->ops->ioctl_query_info(
59 				xid, tcon, cifs_sb, utf16_path,
60 				filep->private_data ? 0 : 1, p);
61 	else
62 		rc = -EOPNOTSUPP;
63 
64  ici_exit:
65 	if (utf16_path != &root_path)
66 		kfree(utf16_path);
67 	free_dentry_path(page);
68 	return rc;
69 }
70 
71 static long cifs_ioctl_copychunk(unsigned int xid, struct file *dst_file,
72 			unsigned long srcfd)
73 {
74 	int rc;
75 	struct fd src_file;
76 	struct inode *src_inode;
77 
78 	cifs_dbg(FYI, "ioctl copychunk range\n");
79 	/* the destination must be opened for writing */
80 	if (!(dst_file->f_mode & FMODE_WRITE)) {
81 		cifs_dbg(FYI, "file target not open for write\n");
82 		return -EINVAL;
83 	}
84 
85 	/* check if target volume is readonly and take reference */
86 	rc = mnt_want_write_file(dst_file);
87 	if (rc) {
88 		cifs_dbg(FYI, "mnt_want_write failed with rc %d\n", rc);
89 		return rc;
90 	}
91 
92 	src_file = fdget(srcfd);
93 	if (!src_file.file) {
94 		rc = -EBADF;
95 		goto out_drop_write;
96 	}
97 
98 	if (src_file.file->f_op->unlocked_ioctl != cifs_ioctl) {
99 		rc = -EBADF;
100 		cifs_dbg(VFS, "src file seems to be from a different filesystem type\n");
101 		goto out_fput;
102 	}
103 
104 	src_inode = file_inode(src_file.file);
105 	rc = -EINVAL;
106 	if (S_ISDIR(src_inode->i_mode))
107 		goto out_fput;
108 
109 	rc = cifs_file_copychunk_range(xid, src_file.file, 0, dst_file, 0,
110 					src_inode->i_size, 0);
111 	if (rc > 0)
112 		rc = 0;
113 out_fput:
114 	fdput(src_file);
115 out_drop_write:
116 	mnt_drop_write_file(dst_file);
117 	return rc;
118 }
119 
120 static long smb_mnt_get_tcon_info(struct cifs_tcon *tcon, void __user *arg)
121 {
122 	int rc = 0;
123 	struct smb_mnt_tcon_info tcon_inf;
124 
125 	tcon_inf.tid = tcon->tid;
126 	tcon_inf.session_id = tcon->ses->Suid;
127 
128 	if (copy_to_user(arg, &tcon_inf, sizeof(struct smb_mnt_tcon_info)))
129 		rc = -EFAULT;
130 
131 	return rc;
132 }
133 
134 static long smb_mnt_get_fsinfo(unsigned int xid, struct cifs_tcon *tcon,
135 				void __user *arg)
136 {
137 	int rc = 0;
138 	struct smb_mnt_fs_info *fsinf;
139 
140 	fsinf = kzalloc(sizeof(struct smb_mnt_fs_info), GFP_KERNEL);
141 	if (fsinf == NULL)
142 		return -ENOMEM;
143 
144 	fsinf->version = 1;
145 	fsinf->protocol_id = tcon->ses->server->vals->protocol_id;
146 	fsinf->tcon_flags = tcon->Flags;
147 	fsinf->device_characteristics =
148 			le32_to_cpu(tcon->fsDevInfo.DeviceCharacteristics);
149 	fsinf->device_type = le32_to_cpu(tcon->fsDevInfo.DeviceType);
150 	fsinf->fs_attributes = le32_to_cpu(tcon->fsAttrInfo.Attributes);
151 	fsinf->max_path_component =
152 		le32_to_cpu(tcon->fsAttrInfo.MaxPathNameComponentLength);
153 	fsinf->vol_serial_number = tcon->vol_serial_number;
154 	fsinf->vol_create_time = le64_to_cpu(tcon->vol_create_time);
155 	fsinf->share_flags = tcon->share_flags;
156 	fsinf->share_caps = le32_to_cpu(tcon->capabilities);
157 	fsinf->sector_flags = tcon->ss_flags;
158 	fsinf->optimal_sector_size = tcon->perf_sector_size;
159 	fsinf->max_bytes_chunk = tcon->max_bytes_chunk;
160 	fsinf->maximal_access = tcon->maximal_access;
161 	fsinf->cifs_posix_caps = le64_to_cpu(tcon->fsUnixInfo.Capability);
162 
163 	if (copy_to_user(arg, fsinf, sizeof(struct smb_mnt_fs_info)))
164 		rc = -EFAULT;
165 
166 	kfree(fsinf);
167 	return rc;
168 }
169 
170 static int cifs_shutdown(struct super_block *sb, unsigned long arg)
171 {
172 	struct cifs_sb_info *sbi = CIFS_SB(sb);
173 	struct tcon_link *tlink;
174 	struct cifs_tcon *tcon;
175 	__u32 flags;
176 	int rc;
177 
178 	if (!capable(CAP_SYS_ADMIN))
179 		return -EPERM;
180 
181 	if (get_user(flags, (__u32 __user *)arg))
182 		return -EFAULT;
183 
184 	tlink = cifs_sb_tlink(sbi);
185 	if (IS_ERR(tlink))
186 		return PTR_ERR(tlink);
187 	tcon = tlink_tcon(tlink);
188 
189 	trace_smb3_shutdown_enter(flags, tcon->tid);
190 	if (flags > CIFS_GOING_FLAGS_NOLOGFLUSH) {
191 		rc = -EINVAL;
192 		goto shutdown_out_err;
193 	}
194 
195 	if (cifs_forced_shutdown(sbi))
196 		goto shutdown_good;
197 
198 	cifs_dbg(VFS, "shut down requested (%d)", flags);
199 
200 	/*
201 	 * see:
202 	 *   https://man7.org/linux/man-pages/man2/ioctl_xfs_goingdown.2.html
203 	 * for more information and description of original intent of the flags
204 	 */
205 	switch (flags) {
206 	/*
207 	 * We could add support later for default flag which requires:
208 	 *     "Flush all dirty data and metadata to disk"
209 	 * would need to call syncfs or equivalent to flush page cache for
210 	 * the mount and then issue fsync to server (if nostrictsync not set)
211 	 */
212 	case CIFS_GOING_FLAGS_DEFAULT:
213 		cifs_dbg(FYI, "shutdown with default flag not supported\n");
214 		rc = -EINVAL;
215 		goto shutdown_out_err;
216 	/*
217 	 * FLAGS_LOGFLUSH is easy since it asks to write out metadata (not
218 	 * data) but metadata writes are not cached on the client, so can treat
219 	 * it similarly to NOLOGFLUSH
220 	 */
221 	case CIFS_GOING_FLAGS_LOGFLUSH:
222 	case CIFS_GOING_FLAGS_NOLOGFLUSH:
223 		sbi->mnt_cifs_flags |= CIFS_MOUNT_SHUTDOWN;
224 		goto shutdown_good;
225 	default:
226 		rc = -EINVAL;
227 		goto shutdown_out_err;
228 	}
229 
230 shutdown_good:
231 	trace_smb3_shutdown_done(flags, tcon->tid);
232 	return 0;
233 shutdown_out_err:
234 	trace_smb3_shutdown_err(rc, flags, tcon->tid);
235 	return rc;
236 }
237 
238 static int cifs_dump_full_key(struct cifs_tcon *tcon, struct smb3_full_key_debug_info __user *in)
239 {
240 	struct smb3_full_key_debug_info out;
241 	struct cifs_ses *ses;
242 	int rc = 0;
243 	bool found = false;
244 	u8 __user *end;
245 
246 	if (!smb3_encryption_required(tcon)) {
247 		rc = -EOPNOTSUPP;
248 		goto out;
249 	}
250 
251 	/* copy user input into our output buffer */
252 	if (copy_from_user(&out, in, sizeof(out))) {
253 		rc = -EINVAL;
254 		goto out;
255 	}
256 
257 	if (!out.session_id) {
258 		/* if ses id is 0, use current user session */
259 		ses = tcon->ses;
260 	} else {
261 		/* otherwise if a session id is given, look for it in all our sessions */
262 		struct cifs_ses *ses_it = NULL;
263 		struct TCP_Server_Info *server_it = NULL;
264 
265 		spin_lock(&cifs_tcp_ses_lock);
266 		list_for_each_entry(server_it, &cifs_tcp_ses_list, tcp_ses_list) {
267 			list_for_each_entry(ses_it, &server_it->smb_ses_list, smb_ses_list) {
268 				spin_lock(&ses_it->ses_lock);
269 				if (ses_it->ses_status != SES_EXITING &&
270 				    ses_it->Suid == out.session_id) {
271 					ses = ses_it;
272 					/*
273 					 * since we are using the session outside the crit
274 					 * section, we need to make sure it won't be released
275 					 * so increment its refcount
276 					 */
277 					cifs_smb_ses_inc_refcount(ses);
278 					spin_unlock(&ses_it->ses_lock);
279 					found = true;
280 					goto search_end;
281 				}
282 				spin_unlock(&ses_it->ses_lock);
283 			}
284 		}
285 search_end:
286 		spin_unlock(&cifs_tcp_ses_lock);
287 		if (!found) {
288 			rc = -ENOENT;
289 			goto out;
290 		}
291 	}
292 
293 	switch (ses->server->cipher_type) {
294 	case SMB2_ENCRYPTION_AES128_CCM:
295 	case SMB2_ENCRYPTION_AES128_GCM:
296 		out.session_key_length = CIFS_SESS_KEY_SIZE;
297 		out.server_in_key_length = out.server_out_key_length = SMB3_GCM128_CRYPTKEY_SIZE;
298 		break;
299 	case SMB2_ENCRYPTION_AES256_CCM:
300 	case SMB2_ENCRYPTION_AES256_GCM:
301 		out.session_key_length = CIFS_SESS_KEY_SIZE;
302 		out.server_in_key_length = out.server_out_key_length = SMB3_GCM256_CRYPTKEY_SIZE;
303 		break;
304 	default:
305 		rc = -EOPNOTSUPP;
306 		goto out;
307 	}
308 
309 	/* check if user buffer is big enough to store all the keys */
310 	if (out.in_size < sizeof(out) + out.session_key_length + out.server_in_key_length
311 	    + out.server_out_key_length) {
312 		rc = -ENOBUFS;
313 		goto out;
314 	}
315 
316 	out.session_id = ses->Suid;
317 	out.cipher_type = le16_to_cpu(ses->server->cipher_type);
318 
319 	/* overwrite user input with our output */
320 	if (copy_to_user(in, &out, sizeof(out))) {
321 		rc = -EINVAL;
322 		goto out;
323 	}
324 
325 	/* append all the keys at the end of the user buffer */
326 	end = in->data;
327 	if (copy_to_user(end, ses->auth_key.response, out.session_key_length)) {
328 		rc = -EINVAL;
329 		goto out;
330 	}
331 	end += out.session_key_length;
332 
333 	if (copy_to_user(end, ses->smb3encryptionkey, out.server_in_key_length)) {
334 		rc = -EINVAL;
335 		goto out;
336 	}
337 	end += out.server_in_key_length;
338 
339 	if (copy_to_user(end, ses->smb3decryptionkey, out.server_out_key_length)) {
340 		rc = -EINVAL;
341 		goto out;
342 	}
343 
344 out:
345 	if (found)
346 		cifs_put_smb_ses(ses);
347 	return rc;
348 }
349 
350 long cifs_ioctl(struct file *filep, unsigned int command, unsigned long arg)
351 {
352 	struct inode *inode = file_inode(filep);
353 	struct smb3_key_debug_info pkey_inf;
354 	int rc = -ENOTTY; /* strange error - but the precedent */
355 	unsigned int xid;
356 	struct cifsFileInfo *pSMBFile = filep->private_data;
357 	struct cifs_tcon *tcon;
358 	struct tcon_link *tlink;
359 	struct cifs_sb_info *cifs_sb;
360 	__u64	ExtAttrBits = 0;
361 #ifdef CONFIG_CIFS_POSIX
362 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
363 	__u64   caps;
364 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
365 #endif /* CONFIG_CIFS_POSIX */
366 
367 	xid = get_xid();
368 
369 	cifs_dbg(FYI, "cifs ioctl 0x%x\n", command);
370 	if (pSMBFile == NULL)
371 		trace_smb3_ioctl(xid, 0, command);
372 	else
373 		trace_smb3_ioctl(xid, pSMBFile->fid.persistent_fid, command);
374 
375 	switch (command) {
376 		case FS_IOC_GETFLAGS:
377 			if (pSMBFile == NULL)
378 				break;
379 			tcon = tlink_tcon(pSMBFile->tlink);
380 #ifdef CONFIG_CIFS_POSIX
381 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
382 			caps = le64_to_cpu(tcon->fsUnixInfo.Capability);
383 			if (CIFS_UNIX_EXTATTR_CAP & caps) {
384 				__u64	ExtAttrMask = 0;
385 				rc = CIFSGetExtAttr(xid, tcon,
386 						    pSMBFile->fid.netfid,
387 						    &ExtAttrBits, &ExtAttrMask);
388 				if (rc == 0)
389 					rc = put_user(ExtAttrBits &
390 						FS_FL_USER_VISIBLE,
391 						(int __user *)arg);
392 				if (rc != -EOPNOTSUPP)
393 					break;
394 			}
395 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
396 #endif /* CONFIG_CIFS_POSIX */
397 			rc = 0;
398 			if (CIFS_I(inode)->cifsAttrs & ATTR_COMPRESSED) {
399 				/* add in the compressed bit */
400 				ExtAttrBits = FS_COMPR_FL;
401 				rc = put_user(ExtAttrBits & FS_FL_USER_VISIBLE,
402 					      (int __user *)arg);
403 			}
404 			break;
405 		case FS_IOC_SETFLAGS:
406 			if (pSMBFile == NULL)
407 				break;
408 			tcon = tlink_tcon(pSMBFile->tlink);
409 			/* caps = le64_to_cpu(tcon->fsUnixInfo.Capability); */
410 
411 			if (get_user(ExtAttrBits, (int __user *)arg)) {
412 				rc = -EFAULT;
413 				break;
414 			}
415 
416 			/*
417 			 * if (CIFS_UNIX_EXTATTR_CAP & caps)
418 			 *	rc = CIFSSetExtAttr(xid, tcon,
419 			 *		       pSMBFile->fid.netfid,
420 			 *		       extAttrBits,
421 			 *		       &ExtAttrMask);
422 			 * if (rc != -EOPNOTSUPP)
423 			 *	break;
424 			 */
425 
426 			/* Currently only flag we can set is compressed flag */
427 			if ((ExtAttrBits & FS_COMPR_FL) == 0)
428 				break;
429 
430 			/* Try to set compress flag */
431 			if (tcon->ses->server->ops->set_compression) {
432 				rc = tcon->ses->server->ops->set_compression(
433 							xid, tcon, pSMBFile);
434 				cifs_dbg(FYI, "set compress flag rc %d\n", rc);
435 			}
436 			break;
437 		case CIFS_IOC_COPYCHUNK_FILE:
438 			rc = cifs_ioctl_copychunk(xid, filep, arg);
439 			break;
440 		case CIFS_QUERY_INFO:
441 			rc = cifs_ioctl_query_info(xid, filep, arg);
442 			break;
443 		case CIFS_IOC_SET_INTEGRITY:
444 			if (pSMBFile == NULL)
445 				break;
446 			tcon = tlink_tcon(pSMBFile->tlink);
447 			if (tcon->ses->server->ops->set_integrity)
448 				rc = tcon->ses->server->ops->set_integrity(xid,
449 						tcon, pSMBFile);
450 			else
451 				rc = -EOPNOTSUPP;
452 			break;
453 		case CIFS_IOC_GET_MNT_INFO:
454 			if (pSMBFile == NULL)
455 				break;
456 			tcon = tlink_tcon(pSMBFile->tlink);
457 			rc = smb_mnt_get_fsinfo(xid, tcon, (void __user *)arg);
458 			break;
459 		case CIFS_IOC_GET_TCON_INFO:
460 			cifs_sb = CIFS_SB(inode->i_sb);
461 			tlink = cifs_sb_tlink(cifs_sb);
462 			if (IS_ERR(tlink)) {
463 				rc = PTR_ERR(tlink);
464 				break;
465 			}
466 			tcon = tlink_tcon(tlink);
467 			rc = smb_mnt_get_tcon_info(tcon, (void __user *)arg);
468 			cifs_put_tlink(tlink);
469 			break;
470 		case CIFS_ENUMERATE_SNAPSHOTS:
471 			if (pSMBFile == NULL)
472 				break;
473 			if (arg == 0) {
474 				rc = -EINVAL;
475 				goto cifs_ioc_exit;
476 			}
477 			tcon = tlink_tcon(pSMBFile->tlink);
478 			if (tcon->ses->server->ops->enum_snapshots)
479 				rc = tcon->ses->server->ops->enum_snapshots(xid, tcon,
480 						pSMBFile, (void __user *)arg);
481 			else
482 				rc = -EOPNOTSUPP;
483 			break;
484 		case CIFS_DUMP_KEY:
485 			/*
486 			 * Dump encryption keys. This is an old ioctl that only
487 			 * handles AES-128-{CCM,GCM}.
488 			 */
489 			if (!capable(CAP_SYS_ADMIN)) {
490 				rc = -EACCES;
491 				break;
492 			}
493 
494 			cifs_sb = CIFS_SB(inode->i_sb);
495 			tlink = cifs_sb_tlink(cifs_sb);
496 			if (IS_ERR(tlink)) {
497 				rc = PTR_ERR(tlink);
498 				break;
499 			}
500 			tcon = tlink_tcon(tlink);
501 			if (!smb3_encryption_required(tcon)) {
502 				rc = -EOPNOTSUPP;
503 				cifs_put_tlink(tlink);
504 				break;
505 			}
506 			pkey_inf.cipher_type =
507 				le16_to_cpu(tcon->ses->server->cipher_type);
508 			pkey_inf.Suid = tcon->ses->Suid;
509 			memcpy(pkey_inf.auth_key, tcon->ses->auth_key.response,
510 					16 /* SMB2_NTLMV2_SESSKEY_SIZE */);
511 			memcpy(pkey_inf.smb3decryptionkey,
512 			      tcon->ses->smb3decryptionkey, SMB3_SIGN_KEY_SIZE);
513 			memcpy(pkey_inf.smb3encryptionkey,
514 			      tcon->ses->smb3encryptionkey, SMB3_SIGN_KEY_SIZE);
515 			if (copy_to_user((void __user *)arg, &pkey_inf,
516 					sizeof(struct smb3_key_debug_info)))
517 				rc = -EFAULT;
518 			else
519 				rc = 0;
520 			cifs_put_tlink(tlink);
521 			break;
522 		case CIFS_DUMP_FULL_KEY:
523 			/*
524 			 * Dump encryption keys (handles any key sizes)
525 			 */
526 			if (pSMBFile == NULL)
527 				break;
528 			if (!capable(CAP_SYS_ADMIN)) {
529 				rc = -EACCES;
530 				break;
531 			}
532 			cifs_sb = CIFS_SB(inode->i_sb);
533 			tlink = cifs_sb_tlink(cifs_sb);
534 			if (IS_ERR(tlink)) {
535 				rc = PTR_ERR(tlink);
536 				break;
537 			}
538 
539 			tcon = tlink_tcon(tlink);
540 			rc = cifs_dump_full_key(tcon, (void __user *)arg);
541 			cifs_put_tlink(tlink);
542 			break;
543 		case CIFS_IOC_NOTIFY:
544 			if (!S_ISDIR(inode->i_mode)) {
545 				/* Notify can only be done on directories */
546 				rc = -EOPNOTSUPP;
547 				break;
548 			}
549 			cifs_sb = CIFS_SB(inode->i_sb);
550 			tlink = cifs_sb_tlink(cifs_sb);
551 			if (IS_ERR(tlink)) {
552 				rc = PTR_ERR(tlink);
553 				break;
554 			}
555 			tcon = tlink_tcon(tlink);
556 			if (tcon && tcon->ses->server->ops->notify) {
557 				rc = tcon->ses->server->ops->notify(xid,
558 						filep, (void __user *)arg,
559 						false /* no ret data */);
560 				cifs_dbg(FYI, "ioctl notify rc %d\n", rc);
561 			} else
562 				rc = -EOPNOTSUPP;
563 			cifs_put_tlink(tlink);
564 			break;
565 		case CIFS_IOC_NOTIFY_INFO:
566 			if (!S_ISDIR(inode->i_mode)) {
567 				/* Notify can only be done on directories */
568 				rc = -EOPNOTSUPP;
569 				break;
570 			}
571 			cifs_sb = CIFS_SB(inode->i_sb);
572 			tlink = cifs_sb_tlink(cifs_sb);
573 			if (IS_ERR(tlink)) {
574 				rc = PTR_ERR(tlink);
575 				break;
576 			}
577 			tcon = tlink_tcon(tlink);
578 			if (tcon && tcon->ses->server->ops->notify) {
579 				rc = tcon->ses->server->ops->notify(xid,
580 						filep, (void __user *)arg,
581 						true /* return details */);
582 				cifs_dbg(FYI, "ioctl notify info rc %d\n", rc);
583 			} else
584 				rc = -EOPNOTSUPP;
585 			cifs_put_tlink(tlink);
586 			break;
587 		case CIFS_IOC_SHUTDOWN:
588 			rc = cifs_shutdown(inode->i_sb, arg);
589 			break;
590 		default:
591 			cifs_dbg(FYI, "unsupported ioctl\n");
592 			break;
593 	}
594 cifs_ioc_exit:
595 	free_xid(xid);
596 	return rc;
597 }
598