ioctl.c 14 KB

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