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