ioctl.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/fs/ioctl.c
  4. *
  5. * Copyright (C) 1991, 1992 Linus Torvalds
  6. */
  7. #include <linux/syscalls.h>
  8. #include <linux/mm.h>
  9. #include <linux/capability.h>
  10. #include <linux/compat.h>
  11. #include <linux/file.h>
  12. #include <linux/fs.h>
  13. #include <linux/security.h>
  14. #include <linux/export.h>
  15. #include <linux/uaccess.h>
  16. #include <linux/writeback.h>
  17. #include <linux/buffer_head.h>
  18. #include <linux/falloc.h>
  19. #include <linux/sched/signal.h>
  20. #include <linux/fiemap.h>
  21. #include <linux/mount.h>
  22. #include <linux/fscrypt.h>
  23. #include <linux/fileattr.h>
  24. #include "internal.h"
  25. #include <asm/ioctls.h>
  26. /* So that the fiemap access checks can't overflow on 32 bit machines. */
  27. #define FIEMAP_MAX_EXTENTS (UINT_MAX / sizeof(struct fiemap_extent))
  28. /**
  29. * vfs_ioctl - call filesystem specific ioctl methods
  30. * @filp: open file to invoke ioctl method on
  31. * @cmd: ioctl command to execute
  32. * @arg: command-specific argument for ioctl
  33. *
  34. * Invokes filesystem specific ->unlocked_ioctl, if one exists; otherwise
  35. * returns -ENOTTY.
  36. *
  37. * Returns 0 on success, -errno on error.
  38. */
  39. long vfs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  40. {
  41. int error = -ENOTTY;
  42. if (!filp->f_op->unlocked_ioctl)
  43. goto out;
  44. error = filp->f_op->unlocked_ioctl(filp, cmd, arg);
  45. if (error == -ENOIOCTLCMD)
  46. error = -ENOTTY;
  47. out:
  48. return error;
  49. }
  50. EXPORT_SYMBOL(vfs_ioctl);
  51. static int ioctl_fibmap(struct file *filp, int __user *p)
  52. {
  53. struct inode *inode = file_inode(filp);
  54. struct super_block *sb = inode->i_sb;
  55. int error, ur_block;
  56. sector_t block;
  57. if (!capable(CAP_SYS_RAWIO))
  58. return -EPERM;
  59. error = get_user(ur_block, p);
  60. if (error)
  61. return error;
  62. if (ur_block < 0)
  63. return -EINVAL;
  64. block = ur_block;
  65. error = bmap(inode, &block);
  66. if (block > INT_MAX) {
  67. error = -ERANGE;
  68. pr_warn_ratelimited("[%s/%d] FS: %s File: %pD4 would truncate fibmap result\n",
  69. current->comm, task_pid_nr(current),
  70. sb->s_id, filp);
  71. }
  72. if (error)
  73. ur_block = 0;
  74. else
  75. ur_block = block;
  76. if (put_user(ur_block, p))
  77. error = -EFAULT;
  78. return error;
  79. }
  80. /**
  81. * fiemap_fill_next_extent - Fiemap helper function
  82. * @fieinfo: Fiemap context passed into ->fiemap
  83. * @logical: Extent logical start offset, in bytes
  84. * @phys: Extent physical start offset, in bytes
  85. * @len: Extent length, in bytes
  86. * @flags: FIEMAP_EXTENT flags that describe this extent
  87. *
  88. * Called from file system ->fiemap callback. Will populate extent
  89. * info as passed in via arguments and copy to user memory. On
  90. * success, extent count on fieinfo is incremented.
  91. *
  92. * Returns 0 on success, -errno on error, 1 if this was the last
  93. * extent that will fit in user array.
  94. */
  95. int fiemap_fill_next_extent(struct fiemap_extent_info *fieinfo, u64 logical,
  96. u64 phys, u64 len, u32 flags)
  97. {
  98. struct fiemap_extent extent;
  99. struct fiemap_extent __user *dest = fieinfo->fi_extents_start;
  100. /* only count the extents */
  101. if (fieinfo->fi_extents_max == 0) {
  102. fieinfo->fi_extents_mapped++;
  103. return (flags & FIEMAP_EXTENT_LAST) ? 1 : 0;
  104. }
  105. if (fieinfo->fi_extents_mapped >= fieinfo->fi_extents_max)
  106. return 1;
  107. #define SET_UNKNOWN_FLAGS (FIEMAP_EXTENT_DELALLOC)
  108. #define SET_NO_UNMOUNTED_IO_FLAGS (FIEMAP_EXTENT_DATA_ENCRYPTED)
  109. #define SET_NOT_ALIGNED_FLAGS (FIEMAP_EXTENT_DATA_TAIL|FIEMAP_EXTENT_DATA_INLINE)
  110. if (flags & SET_UNKNOWN_FLAGS)
  111. flags |= FIEMAP_EXTENT_UNKNOWN;
  112. if (flags & SET_NO_UNMOUNTED_IO_FLAGS)
  113. flags |= FIEMAP_EXTENT_ENCODED;
  114. if (flags & SET_NOT_ALIGNED_FLAGS)
  115. flags |= FIEMAP_EXTENT_NOT_ALIGNED;
  116. memset(&extent, 0, sizeof(extent));
  117. extent.fe_logical = logical;
  118. extent.fe_physical = phys;
  119. extent.fe_length = len;
  120. extent.fe_flags = flags;
  121. dest += fieinfo->fi_extents_mapped;
  122. if (copy_to_user(dest, &extent, sizeof(extent)))
  123. return -EFAULT;
  124. fieinfo->fi_extents_mapped++;
  125. if (fieinfo->fi_extents_mapped == fieinfo->fi_extents_max)
  126. return 1;
  127. return (flags & FIEMAP_EXTENT_LAST) ? 1 : 0;
  128. }
  129. EXPORT_SYMBOL(fiemap_fill_next_extent);
  130. /**
  131. * fiemap_prep - check validity of requested flags for fiemap
  132. * @inode: Inode to operate on
  133. * @fieinfo: Fiemap context passed into ->fiemap
  134. * @start: Start of the mapped range
  135. * @len: Length of the mapped range, can be truncated by this function.
  136. * @supported_flags: Set of fiemap flags that the file system understands
  137. *
  138. * This function must be called from each ->fiemap instance to validate the
  139. * fiemap request against the file system parameters.
  140. *
  141. * Returns 0 on success, or a negative error on failure.
  142. */
  143. int fiemap_prep(struct inode *inode, struct fiemap_extent_info *fieinfo,
  144. u64 start, u64 *len, u32 supported_flags)
  145. {
  146. u64 maxbytes = inode->i_sb->s_maxbytes;
  147. u32 incompat_flags;
  148. int ret = 0;
  149. if (*len == 0)
  150. return -EINVAL;
  151. if (start >= maxbytes)
  152. return -EFBIG;
  153. /*
  154. * Shrink request scope to what the fs can actually handle.
  155. */
  156. if (*len > maxbytes || (maxbytes - *len) < start)
  157. *len = maxbytes - start;
  158. supported_flags |= FIEMAP_FLAG_SYNC;
  159. supported_flags &= FIEMAP_FLAGS_COMPAT;
  160. incompat_flags = fieinfo->fi_flags & ~supported_flags;
  161. if (incompat_flags) {
  162. fieinfo->fi_flags = incompat_flags;
  163. return -EBADR;
  164. }
  165. if (fieinfo->fi_flags & FIEMAP_FLAG_SYNC)
  166. ret = filemap_write_and_wait(inode->i_mapping);
  167. return ret;
  168. }
  169. EXPORT_SYMBOL(fiemap_prep);
  170. static int ioctl_fiemap(struct file *filp, struct fiemap __user *ufiemap)
  171. {
  172. struct fiemap fiemap;
  173. struct fiemap_extent_info fieinfo = { 0, };
  174. struct inode *inode = file_inode(filp);
  175. int error;
  176. if (!inode->i_op->fiemap)
  177. return -EOPNOTSUPP;
  178. if (copy_from_user(&fiemap, ufiemap, sizeof(fiemap)))
  179. return -EFAULT;
  180. if (fiemap.fm_extent_count > FIEMAP_MAX_EXTENTS)
  181. return -EINVAL;
  182. fieinfo.fi_flags = fiemap.fm_flags;
  183. fieinfo.fi_extents_max = fiemap.fm_extent_count;
  184. fieinfo.fi_extents_start = ufiemap->fm_extents;
  185. error = inode->i_op->fiemap(inode, &fieinfo, fiemap.fm_start,
  186. fiemap.fm_length);
  187. fiemap.fm_flags = fieinfo.fi_flags;
  188. fiemap.fm_mapped_extents = fieinfo.fi_extents_mapped;
  189. if (copy_to_user(ufiemap, &fiemap, sizeof(fiemap)))
  190. error = -EFAULT;
  191. return error;
  192. }
  193. static long ioctl_file_clone(struct file *dst_file, unsigned long srcfd,
  194. u64 off, u64 olen, u64 destoff)
  195. {
  196. struct fd src_file = fdget(srcfd);
  197. loff_t cloned;
  198. int ret;
  199. if (!fd_file(src_file))
  200. return -EBADF;
  201. cloned = vfs_clone_file_range(fd_file(src_file), off, dst_file, destoff,
  202. olen, 0);
  203. if (cloned < 0)
  204. ret = cloned;
  205. else if (olen && cloned != olen)
  206. ret = -EINVAL;
  207. else
  208. ret = 0;
  209. fdput(src_file);
  210. return ret;
  211. }
  212. static long ioctl_file_clone_range(struct file *file,
  213. struct file_clone_range __user *argp)
  214. {
  215. struct file_clone_range args;
  216. if (copy_from_user(&args, argp, sizeof(args)))
  217. return -EFAULT;
  218. return ioctl_file_clone(file, args.src_fd, args.src_offset,
  219. args.src_length, args.dest_offset);
  220. }
  221. /*
  222. * This provides compatibility with legacy XFS pre-allocation ioctls
  223. * which predate the fallocate syscall.
  224. *
  225. * Only the l_start, l_len and l_whence fields of the 'struct space_resv'
  226. * are used here, rest are ignored.
  227. */
  228. static int ioctl_preallocate(struct file *filp, int mode, void __user *argp)
  229. {
  230. struct inode *inode = file_inode(filp);
  231. struct space_resv sr;
  232. if (copy_from_user(&sr, argp, sizeof(sr)))
  233. return -EFAULT;
  234. switch (sr.l_whence) {
  235. case SEEK_SET:
  236. break;
  237. case SEEK_CUR:
  238. sr.l_start += filp->f_pos;
  239. break;
  240. case SEEK_END:
  241. sr.l_start += i_size_read(inode);
  242. break;
  243. default:
  244. return -EINVAL;
  245. }
  246. return vfs_fallocate(filp, mode | FALLOC_FL_KEEP_SIZE, sr.l_start,
  247. sr.l_len);
  248. }
  249. /* on ia32 l_start is on a 32-bit boundary */
  250. #if defined CONFIG_COMPAT && defined(CONFIG_X86_64)
  251. /* just account for different alignment */
  252. static int compat_ioctl_preallocate(struct file *file, int mode,
  253. struct space_resv_32 __user *argp)
  254. {
  255. struct inode *inode = file_inode(file);
  256. struct space_resv_32 sr;
  257. if (copy_from_user(&sr, argp, sizeof(sr)))
  258. return -EFAULT;
  259. switch (sr.l_whence) {
  260. case SEEK_SET:
  261. break;
  262. case SEEK_CUR:
  263. sr.l_start += file->f_pos;
  264. break;
  265. case SEEK_END:
  266. sr.l_start += i_size_read(inode);
  267. break;
  268. default:
  269. return -EINVAL;
  270. }
  271. return vfs_fallocate(file, mode | FALLOC_FL_KEEP_SIZE, sr.l_start, sr.l_len);
  272. }
  273. #endif
  274. static int file_ioctl(struct file *filp, unsigned int cmd, int __user *p)
  275. {
  276. switch (cmd) {
  277. case FIBMAP:
  278. return ioctl_fibmap(filp, p);
  279. case FS_IOC_RESVSP:
  280. case FS_IOC_RESVSP64:
  281. return ioctl_preallocate(filp, 0, p);
  282. case FS_IOC_UNRESVSP:
  283. case FS_IOC_UNRESVSP64:
  284. return ioctl_preallocate(filp, FALLOC_FL_PUNCH_HOLE, p);
  285. case FS_IOC_ZERO_RANGE:
  286. return ioctl_preallocate(filp, FALLOC_FL_ZERO_RANGE, p);
  287. }
  288. return -ENOIOCTLCMD;
  289. }
  290. static int ioctl_fionbio(struct file *filp, int __user *argp)
  291. {
  292. unsigned int flag;
  293. int on, error;
  294. error = get_user(on, argp);
  295. if (error)
  296. return error;
  297. flag = O_NONBLOCK;
  298. #ifdef __sparc__
  299. /* SunOS compatibility item. */
  300. if (O_NONBLOCK != O_NDELAY)
  301. flag |= O_NDELAY;
  302. #endif
  303. spin_lock(&filp->f_lock);
  304. if (on)
  305. filp->f_flags |= flag;
  306. else
  307. filp->f_flags &= ~flag;
  308. spin_unlock(&filp->f_lock);
  309. return error;
  310. }
  311. static int ioctl_fioasync(unsigned int fd, struct file *filp,
  312. int __user *argp)
  313. {
  314. unsigned int flag;
  315. int on, error;
  316. error = get_user(on, argp);
  317. if (error)
  318. return error;
  319. flag = on ? FASYNC : 0;
  320. /* Did FASYNC state change ? */
  321. if ((flag ^ filp->f_flags) & FASYNC) {
  322. if (filp->f_op->fasync)
  323. /* fasync() adjusts filp->f_flags */
  324. error = filp->f_op->fasync(fd, filp, on);
  325. else
  326. error = -ENOTTY;
  327. }
  328. return error < 0 ? error : 0;
  329. }
  330. static int ioctl_fsfreeze(struct file *filp)
  331. {
  332. struct super_block *sb = file_inode(filp)->i_sb;
  333. if (!ns_capable(sb->s_user_ns, CAP_SYS_ADMIN))
  334. return -EPERM;
  335. /* If filesystem doesn't support freeze feature, return. */
  336. if (sb->s_op->freeze_fs == NULL && sb->s_op->freeze_super == NULL)
  337. return -EOPNOTSUPP;
  338. /* Freeze */
  339. if (sb->s_op->freeze_super)
  340. return sb->s_op->freeze_super(sb, FREEZE_HOLDER_USERSPACE);
  341. return freeze_super(sb, FREEZE_HOLDER_USERSPACE);
  342. }
  343. static int ioctl_fsthaw(struct file *filp)
  344. {
  345. struct super_block *sb = file_inode(filp)->i_sb;
  346. if (!ns_capable(sb->s_user_ns, CAP_SYS_ADMIN))
  347. return -EPERM;
  348. /* Thaw */
  349. if (sb->s_op->thaw_super)
  350. return sb->s_op->thaw_super(sb, FREEZE_HOLDER_USERSPACE);
  351. return thaw_super(sb, FREEZE_HOLDER_USERSPACE);
  352. }
  353. static int ioctl_file_dedupe_range(struct file *file,
  354. struct file_dedupe_range __user *argp)
  355. {
  356. struct file_dedupe_range *same = NULL;
  357. int ret;
  358. unsigned long size;
  359. u16 count;
  360. if (get_user(count, &argp->dest_count)) {
  361. ret = -EFAULT;
  362. goto out;
  363. }
  364. size = offsetof(struct file_dedupe_range, info[count]);
  365. if (size > PAGE_SIZE) {
  366. ret = -ENOMEM;
  367. goto out;
  368. }
  369. same = memdup_user(argp, size);
  370. if (IS_ERR(same)) {
  371. ret = PTR_ERR(same);
  372. same = NULL;
  373. goto out;
  374. }
  375. same->dest_count = count;
  376. ret = vfs_dedupe_file_range(file, same);
  377. if (ret)
  378. goto out;
  379. ret = copy_to_user(argp, same, size);
  380. if (ret)
  381. ret = -EFAULT;
  382. out:
  383. kfree(same);
  384. return ret;
  385. }
  386. /**
  387. * fileattr_fill_xflags - initialize fileattr with xflags
  388. * @fa: fileattr pointer
  389. * @xflags: FS_XFLAG_* flags
  390. *
  391. * Set ->fsx_xflags, ->fsx_valid and ->flags (translated xflags). All
  392. * other fields are zeroed.
  393. */
  394. void fileattr_fill_xflags(struct fileattr *fa, u32 xflags)
  395. {
  396. memset(fa, 0, sizeof(*fa));
  397. fa->fsx_valid = true;
  398. fa->fsx_xflags = xflags;
  399. if (fa->fsx_xflags & FS_XFLAG_IMMUTABLE)
  400. fa->flags |= FS_IMMUTABLE_FL;
  401. if (fa->fsx_xflags & FS_XFLAG_APPEND)
  402. fa->flags |= FS_APPEND_FL;
  403. if (fa->fsx_xflags & FS_XFLAG_SYNC)
  404. fa->flags |= FS_SYNC_FL;
  405. if (fa->fsx_xflags & FS_XFLAG_NOATIME)
  406. fa->flags |= FS_NOATIME_FL;
  407. if (fa->fsx_xflags & FS_XFLAG_NODUMP)
  408. fa->flags |= FS_NODUMP_FL;
  409. if (fa->fsx_xflags & FS_XFLAG_DAX)
  410. fa->flags |= FS_DAX_FL;
  411. if (fa->fsx_xflags & FS_XFLAG_PROJINHERIT)
  412. fa->flags |= FS_PROJINHERIT_FL;
  413. }
  414. EXPORT_SYMBOL(fileattr_fill_xflags);
  415. /**
  416. * fileattr_fill_flags - initialize fileattr with flags
  417. * @fa: fileattr pointer
  418. * @flags: FS_*_FL flags
  419. *
  420. * Set ->flags, ->flags_valid and ->fsx_xflags (translated flags).
  421. * All other fields are zeroed.
  422. */
  423. void fileattr_fill_flags(struct fileattr *fa, u32 flags)
  424. {
  425. memset(fa, 0, sizeof(*fa));
  426. fa->flags_valid = true;
  427. fa->flags = flags;
  428. if (fa->flags & FS_SYNC_FL)
  429. fa->fsx_xflags |= FS_XFLAG_SYNC;
  430. if (fa->flags & FS_IMMUTABLE_FL)
  431. fa->fsx_xflags |= FS_XFLAG_IMMUTABLE;
  432. if (fa->flags & FS_APPEND_FL)
  433. fa->fsx_xflags |= FS_XFLAG_APPEND;
  434. if (fa->flags & FS_NODUMP_FL)
  435. fa->fsx_xflags |= FS_XFLAG_NODUMP;
  436. if (fa->flags & FS_NOATIME_FL)
  437. fa->fsx_xflags |= FS_XFLAG_NOATIME;
  438. if (fa->flags & FS_DAX_FL)
  439. fa->fsx_xflags |= FS_XFLAG_DAX;
  440. if (fa->flags & FS_PROJINHERIT_FL)
  441. fa->fsx_xflags |= FS_XFLAG_PROJINHERIT;
  442. }
  443. EXPORT_SYMBOL(fileattr_fill_flags);
  444. /**
  445. * vfs_fileattr_get - retrieve miscellaneous file attributes
  446. * @dentry: the object to retrieve from
  447. * @fa: fileattr pointer
  448. *
  449. * Call i_op->fileattr_get() callback, if exists.
  450. *
  451. * Return: 0 on success, or a negative error on failure.
  452. */
  453. int vfs_fileattr_get(struct dentry *dentry, struct fileattr *fa)
  454. {
  455. struct inode *inode = d_inode(dentry);
  456. if (!inode->i_op->fileattr_get)
  457. return -ENOIOCTLCMD;
  458. return inode->i_op->fileattr_get(dentry, fa);
  459. }
  460. EXPORT_SYMBOL(vfs_fileattr_get);
  461. /**
  462. * copy_fsxattr_to_user - copy fsxattr to userspace.
  463. * @fa: fileattr pointer
  464. * @ufa: fsxattr user pointer
  465. *
  466. * Return: 0 on success, or -EFAULT on failure.
  467. */
  468. int copy_fsxattr_to_user(const struct fileattr *fa, struct fsxattr __user *ufa)
  469. {
  470. struct fsxattr xfa;
  471. memset(&xfa, 0, sizeof(xfa));
  472. xfa.fsx_xflags = fa->fsx_xflags;
  473. xfa.fsx_extsize = fa->fsx_extsize;
  474. xfa.fsx_nextents = fa->fsx_nextents;
  475. xfa.fsx_projid = fa->fsx_projid;
  476. xfa.fsx_cowextsize = fa->fsx_cowextsize;
  477. if (copy_to_user(ufa, &xfa, sizeof(xfa)))
  478. return -EFAULT;
  479. return 0;
  480. }
  481. EXPORT_SYMBOL(copy_fsxattr_to_user);
  482. static int copy_fsxattr_from_user(struct fileattr *fa,
  483. struct fsxattr __user *ufa)
  484. {
  485. struct fsxattr xfa;
  486. if (copy_from_user(&xfa, ufa, sizeof(xfa)))
  487. return -EFAULT;
  488. fileattr_fill_xflags(fa, xfa.fsx_xflags);
  489. fa->fsx_extsize = xfa.fsx_extsize;
  490. fa->fsx_nextents = xfa.fsx_nextents;
  491. fa->fsx_projid = xfa.fsx_projid;
  492. fa->fsx_cowextsize = xfa.fsx_cowextsize;
  493. return 0;
  494. }
  495. /*
  496. * Generic function to check FS_IOC_FSSETXATTR/FS_IOC_SETFLAGS values and reject
  497. * any invalid configurations.
  498. *
  499. * Note: must be called with inode lock held.
  500. */
  501. static int fileattr_set_prepare(struct inode *inode,
  502. const struct fileattr *old_ma,
  503. struct fileattr *fa)
  504. {
  505. int err;
  506. /*
  507. * The IMMUTABLE and APPEND_ONLY flags can only be changed by
  508. * the relevant capability.
  509. */
  510. if ((fa->flags ^ old_ma->flags) & (FS_APPEND_FL | FS_IMMUTABLE_FL) &&
  511. !capable(CAP_LINUX_IMMUTABLE))
  512. return -EPERM;
  513. err = fscrypt_prepare_setflags(inode, old_ma->flags, fa->flags);
  514. if (err)
  515. return err;
  516. /*
  517. * Project Quota ID state is only allowed to change from within the init
  518. * namespace. Enforce that restriction only if we are trying to change
  519. * the quota ID state. Everything else is allowed in user namespaces.
  520. */
  521. if (current_user_ns() != &init_user_ns) {
  522. if (old_ma->fsx_projid != fa->fsx_projid)
  523. return -EINVAL;
  524. if ((old_ma->fsx_xflags ^ fa->fsx_xflags) &
  525. FS_XFLAG_PROJINHERIT)
  526. return -EINVAL;
  527. } else {
  528. /*
  529. * Caller is allowed to change the project ID. If it is being
  530. * changed, make sure that the new value is valid.
  531. */
  532. if (old_ma->fsx_projid != fa->fsx_projid &&
  533. !projid_valid(make_kprojid(&init_user_ns, fa->fsx_projid)))
  534. return -EINVAL;
  535. }
  536. /* Check extent size hints. */
  537. if ((fa->fsx_xflags & FS_XFLAG_EXTSIZE) && !S_ISREG(inode->i_mode))
  538. return -EINVAL;
  539. if ((fa->fsx_xflags & FS_XFLAG_EXTSZINHERIT) &&
  540. !S_ISDIR(inode->i_mode))
  541. return -EINVAL;
  542. if ((fa->fsx_xflags & FS_XFLAG_COWEXTSIZE) &&
  543. !S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode))
  544. return -EINVAL;
  545. /*
  546. * It is only valid to set the DAX flag on regular files and
  547. * directories on filesystems.
  548. */
  549. if ((fa->fsx_xflags & FS_XFLAG_DAX) &&
  550. !(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)))
  551. return -EINVAL;
  552. /* Extent size hints of zero turn off the flags. */
  553. if (fa->fsx_extsize == 0)
  554. fa->fsx_xflags &= ~(FS_XFLAG_EXTSIZE | FS_XFLAG_EXTSZINHERIT);
  555. if (fa->fsx_cowextsize == 0)
  556. fa->fsx_xflags &= ~FS_XFLAG_COWEXTSIZE;
  557. return 0;
  558. }
  559. /**
  560. * vfs_fileattr_set - change miscellaneous file attributes
  561. * @idmap: idmap of the mount
  562. * @dentry: the object to change
  563. * @fa: fileattr pointer
  564. *
  565. * After verifying permissions, call i_op->fileattr_set() callback, if
  566. * exists.
  567. *
  568. * Verifying attributes involves retrieving current attributes with
  569. * i_op->fileattr_get(), this also allows initializing attributes that have
  570. * not been set by the caller to current values. Inode lock is held
  571. * thoughout to prevent racing with another instance.
  572. *
  573. * Return: 0 on success, or a negative error on failure.
  574. */
  575. int vfs_fileattr_set(struct mnt_idmap *idmap, struct dentry *dentry,
  576. struct fileattr *fa)
  577. {
  578. struct inode *inode = d_inode(dentry);
  579. struct fileattr old_ma = {};
  580. int err;
  581. if (!inode->i_op->fileattr_set)
  582. return -ENOIOCTLCMD;
  583. if (!inode_owner_or_capable(idmap, inode))
  584. return -EPERM;
  585. inode_lock(inode);
  586. err = vfs_fileattr_get(dentry, &old_ma);
  587. if (!err) {
  588. /* initialize missing bits from old_ma */
  589. if (fa->flags_valid) {
  590. fa->fsx_xflags |= old_ma.fsx_xflags & ~FS_XFLAG_COMMON;
  591. fa->fsx_extsize = old_ma.fsx_extsize;
  592. fa->fsx_nextents = old_ma.fsx_nextents;
  593. fa->fsx_projid = old_ma.fsx_projid;
  594. fa->fsx_cowextsize = old_ma.fsx_cowextsize;
  595. } else {
  596. fa->flags |= old_ma.flags & ~FS_COMMON_FL;
  597. }
  598. err = fileattr_set_prepare(inode, &old_ma, fa);
  599. if (!err)
  600. err = inode->i_op->fileattr_set(idmap, dentry, fa);
  601. }
  602. inode_unlock(inode);
  603. return err;
  604. }
  605. EXPORT_SYMBOL(vfs_fileattr_set);
  606. static int ioctl_getflags(struct file *file, unsigned int __user *argp)
  607. {
  608. struct fileattr fa = { .flags_valid = true }; /* hint only */
  609. int err;
  610. err = vfs_fileattr_get(file->f_path.dentry, &fa);
  611. if (!err)
  612. err = put_user(fa.flags, argp);
  613. return err;
  614. }
  615. static int ioctl_setflags(struct file *file, unsigned int __user *argp)
  616. {
  617. struct mnt_idmap *idmap = file_mnt_idmap(file);
  618. struct dentry *dentry = file->f_path.dentry;
  619. struct fileattr fa;
  620. unsigned int flags;
  621. int err;
  622. err = get_user(flags, argp);
  623. if (!err) {
  624. err = mnt_want_write_file(file);
  625. if (!err) {
  626. fileattr_fill_flags(&fa, flags);
  627. err = vfs_fileattr_set(idmap, dentry, &fa);
  628. mnt_drop_write_file(file);
  629. }
  630. }
  631. return err;
  632. }
  633. static int ioctl_fsgetxattr(struct file *file, void __user *argp)
  634. {
  635. struct fileattr fa = { .fsx_valid = true }; /* hint only */
  636. int err;
  637. err = vfs_fileattr_get(file->f_path.dentry, &fa);
  638. if (!err)
  639. err = copy_fsxattr_to_user(&fa, argp);
  640. return err;
  641. }
  642. static int ioctl_fssetxattr(struct file *file, void __user *argp)
  643. {
  644. struct mnt_idmap *idmap = file_mnt_idmap(file);
  645. struct dentry *dentry = file->f_path.dentry;
  646. struct fileattr fa;
  647. int err;
  648. err = copy_fsxattr_from_user(&fa, argp);
  649. if (!err) {
  650. err = mnt_want_write_file(file);
  651. if (!err) {
  652. err = vfs_fileattr_set(idmap, dentry, &fa);
  653. mnt_drop_write_file(file);
  654. }
  655. }
  656. return err;
  657. }
  658. static int ioctl_getfsuuid(struct file *file, void __user *argp)
  659. {
  660. struct super_block *sb = file_inode(file)->i_sb;
  661. struct fsuuid2 u = { .len = sb->s_uuid_len, };
  662. if (!sb->s_uuid_len)
  663. return -ENOTTY;
  664. memcpy(&u.uuid[0], &sb->s_uuid, sb->s_uuid_len);
  665. return copy_to_user(argp, &u, sizeof(u)) ? -EFAULT : 0;
  666. }
  667. static int ioctl_get_fs_sysfs_path(struct file *file, void __user *argp)
  668. {
  669. struct super_block *sb = file_inode(file)->i_sb;
  670. if (!strlen(sb->s_sysfs_name))
  671. return -ENOTTY;
  672. struct fs_sysfs_path u = {};
  673. u.len = scnprintf(u.name, sizeof(u.name), "%s/%s", sb->s_type->name, sb->s_sysfs_name);
  674. return copy_to_user(argp, &u, sizeof(u)) ? -EFAULT : 0;
  675. }
  676. /*
  677. * do_vfs_ioctl() is not for drivers and not intended to be EXPORT_SYMBOL()'d.
  678. * It's just a simple helper for sys_ioctl and compat_sys_ioctl.
  679. *
  680. * When you add any new common ioctls to the switches above and below,
  681. * please ensure they have compatible arguments in compat mode.
  682. *
  683. * The LSM mailing list should also be notified of any command additions or
  684. * changes, as specific LSMs may be affected.
  685. */
  686. static int do_vfs_ioctl(struct file *filp, unsigned int fd,
  687. unsigned int cmd, unsigned long arg)
  688. {
  689. void __user *argp = (void __user *)arg;
  690. struct inode *inode = file_inode(filp);
  691. switch (cmd) {
  692. case FIOCLEX:
  693. set_close_on_exec(fd, 1);
  694. return 0;
  695. case FIONCLEX:
  696. set_close_on_exec(fd, 0);
  697. return 0;
  698. case FIONBIO:
  699. return ioctl_fionbio(filp, argp);
  700. case FIOASYNC:
  701. return ioctl_fioasync(fd, filp, argp);
  702. case FIOQSIZE:
  703. if (S_ISDIR(inode->i_mode) || S_ISREG(inode->i_mode) ||
  704. S_ISLNK(inode->i_mode)) {
  705. loff_t res = inode_get_bytes(inode);
  706. return copy_to_user(argp, &res, sizeof(res)) ?
  707. -EFAULT : 0;
  708. }
  709. return -ENOTTY;
  710. case FIFREEZE:
  711. return ioctl_fsfreeze(filp);
  712. case FITHAW:
  713. return ioctl_fsthaw(filp);
  714. case FS_IOC_FIEMAP:
  715. return ioctl_fiemap(filp, argp);
  716. case FIGETBSZ:
  717. /* anon_bdev filesystems may not have a block size */
  718. if (!inode->i_sb->s_blocksize)
  719. return -EINVAL;
  720. return put_user(inode->i_sb->s_blocksize, (int __user *)argp);
  721. case FICLONE:
  722. return ioctl_file_clone(filp, arg, 0, 0, 0);
  723. case FICLONERANGE:
  724. return ioctl_file_clone_range(filp, argp);
  725. case FIDEDUPERANGE:
  726. return ioctl_file_dedupe_range(filp, argp);
  727. case FIONREAD:
  728. if (!S_ISREG(inode->i_mode))
  729. return vfs_ioctl(filp, cmd, arg);
  730. return put_user(i_size_read(inode) - filp->f_pos,
  731. (int __user *)argp);
  732. case FS_IOC_GETFLAGS:
  733. return ioctl_getflags(filp, argp);
  734. case FS_IOC_SETFLAGS:
  735. return ioctl_setflags(filp, argp);
  736. case FS_IOC_FSGETXATTR:
  737. return ioctl_fsgetxattr(filp, argp);
  738. case FS_IOC_FSSETXATTR:
  739. return ioctl_fssetxattr(filp, argp);
  740. case FS_IOC_GETFSUUID:
  741. return ioctl_getfsuuid(filp, argp);
  742. case FS_IOC_GETFSSYSFSPATH:
  743. return ioctl_get_fs_sysfs_path(filp, argp);
  744. default:
  745. if (S_ISREG(inode->i_mode))
  746. return file_ioctl(filp, cmd, argp);
  747. break;
  748. }
  749. return -ENOIOCTLCMD;
  750. }
  751. SYSCALL_DEFINE3(ioctl, unsigned int, fd, unsigned int, cmd, unsigned long, arg)
  752. {
  753. struct fd f = fdget(fd);
  754. int error;
  755. if (!fd_file(f))
  756. return -EBADF;
  757. error = security_file_ioctl(fd_file(f), cmd, arg);
  758. if (error)
  759. goto out;
  760. error = do_vfs_ioctl(fd_file(f), fd, cmd, arg);
  761. if (error == -ENOIOCTLCMD)
  762. error = vfs_ioctl(fd_file(f), cmd, arg);
  763. out:
  764. fdput(f);
  765. return error;
  766. }
  767. #ifdef CONFIG_COMPAT
  768. /**
  769. * compat_ptr_ioctl - generic implementation of .compat_ioctl file operation
  770. * @file: The file to operate on.
  771. * @cmd: The ioctl command number.
  772. * @arg: The argument to the ioctl.
  773. *
  774. * This is not normally called as a function, but instead set in struct
  775. * file_operations as
  776. *
  777. * .compat_ioctl = compat_ptr_ioctl,
  778. *
  779. * On most architectures, the compat_ptr_ioctl() just passes all arguments
  780. * to the corresponding ->ioctl handler. The exception is arch/s390, where
  781. * compat_ptr() clears the top bit of a 32-bit pointer value, so user space
  782. * pointers to the second 2GB alias the first 2GB, as is the case for
  783. * native 32-bit s390 user space.
  784. *
  785. * The compat_ptr_ioctl() function must therefore be used only with ioctl
  786. * functions that either ignore the argument or pass a pointer to a
  787. * compatible data type.
  788. *
  789. * If any ioctl command handled by fops->unlocked_ioctl passes a plain
  790. * integer instead of a pointer, or any of the passed data types
  791. * is incompatible between 32-bit and 64-bit architectures, a proper
  792. * handler is required instead of compat_ptr_ioctl.
  793. */
  794. long compat_ptr_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  795. {
  796. if (!file->f_op->unlocked_ioctl)
  797. return -ENOIOCTLCMD;
  798. return file->f_op->unlocked_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
  799. }
  800. EXPORT_SYMBOL(compat_ptr_ioctl);
  801. COMPAT_SYSCALL_DEFINE3(ioctl, unsigned int, fd, unsigned int, cmd,
  802. compat_ulong_t, arg)
  803. {
  804. struct fd f = fdget(fd);
  805. int error;
  806. if (!fd_file(f))
  807. return -EBADF;
  808. error = security_file_ioctl_compat(fd_file(f), cmd, arg);
  809. if (error)
  810. goto out;
  811. switch (cmd) {
  812. /* FICLONE takes an int argument, so don't use compat_ptr() */
  813. case FICLONE:
  814. error = ioctl_file_clone(fd_file(f), arg, 0, 0, 0);
  815. break;
  816. #if defined(CONFIG_X86_64)
  817. /* these get messy on amd64 due to alignment differences */
  818. case FS_IOC_RESVSP_32:
  819. case FS_IOC_RESVSP64_32:
  820. error = compat_ioctl_preallocate(fd_file(f), 0, compat_ptr(arg));
  821. break;
  822. case FS_IOC_UNRESVSP_32:
  823. case FS_IOC_UNRESVSP64_32:
  824. error = compat_ioctl_preallocate(fd_file(f), FALLOC_FL_PUNCH_HOLE,
  825. compat_ptr(arg));
  826. break;
  827. case FS_IOC_ZERO_RANGE_32:
  828. error = compat_ioctl_preallocate(fd_file(f), FALLOC_FL_ZERO_RANGE,
  829. compat_ptr(arg));
  830. break;
  831. #endif
  832. /*
  833. * These access 32-bit values anyway so no further handling is
  834. * necessary.
  835. */
  836. case FS_IOC32_GETFLAGS:
  837. case FS_IOC32_SETFLAGS:
  838. cmd = (cmd == FS_IOC32_GETFLAGS) ?
  839. FS_IOC_GETFLAGS : FS_IOC_SETFLAGS;
  840. fallthrough;
  841. /*
  842. * everything else in do_vfs_ioctl() takes either a compatible
  843. * pointer argument or no argument -- call it with a modified
  844. * argument.
  845. */
  846. default:
  847. error = do_vfs_ioctl(fd_file(f), fd, cmd,
  848. (unsigned long)compat_ptr(arg));
  849. if (error != -ENOIOCTLCMD)
  850. break;
  851. if (fd_file(f)->f_op->compat_ioctl)
  852. error = fd_file(f)->f_op->compat_ioctl(fd_file(f), cmd, arg);
  853. if (error == -ENOIOCTLCMD)
  854. error = -ENOTTY;
  855. break;
  856. }
  857. out:
  858. fdput(f);
  859. return error;
  860. }
  861. #endif