ioctl.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979
  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. #define SET_UNKNOWN_FLAGS (FIEMAP_EXTENT_DELALLOC)
  96. #define SET_NO_UNMOUNTED_IO_FLAGS (FIEMAP_EXTENT_DATA_ENCRYPTED)
  97. #define SET_NOT_ALIGNED_FLAGS (FIEMAP_EXTENT_DATA_TAIL|FIEMAP_EXTENT_DATA_INLINE)
  98. int fiemap_fill_next_extent(struct fiemap_extent_info *fieinfo, u64 logical,
  99. u64 phys, u64 len, u32 flags)
  100. {
  101. struct fiemap_extent extent;
  102. struct fiemap_extent __user *dest = fieinfo->fi_extents_start;
  103. /* only count the extents */
  104. if (fieinfo->fi_extents_max == 0) {
  105. fieinfo->fi_extents_mapped++;
  106. return (flags & FIEMAP_EXTENT_LAST) ? 1 : 0;
  107. }
  108. if (fieinfo->fi_extents_mapped >= fieinfo->fi_extents_max)
  109. return 1;
  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 (!src_file.file)
  200. return -EBADF;
  201. cloned = vfs_clone_file_range(src_file.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);
  341. return freeze_super(sb);
  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);
  351. return thaw_super(sb);
  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. * @mnt_userns: user namespace 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 user_namespace *mnt_userns, 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(mnt_userns, 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(mnt_userns, 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 user_namespace *mnt_userns = file_mnt_user_ns(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(mnt_userns, 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 user_namespace *mnt_userns = file_mnt_user_ns(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(mnt_userns, dentry, &fa);
  653. mnt_drop_write_file(file);
  654. }
  655. }
  656. return err;
  657. }
  658. /*
  659. * do_vfs_ioctl() is not for drivers and not intended to be EXPORT_SYMBOL()'d.
  660. * It's just a simple helper for sys_ioctl and compat_sys_ioctl.
  661. *
  662. * When you add any new common ioctls to the switches above and below,
  663. * please ensure they have compatible arguments in compat mode.
  664. */
  665. static int do_vfs_ioctl(struct file *filp, unsigned int fd,
  666. unsigned int cmd, unsigned long arg)
  667. {
  668. void __user *argp = (void __user *)arg;
  669. struct inode *inode = file_inode(filp);
  670. switch (cmd) {
  671. case FIOCLEX:
  672. set_close_on_exec(fd, 1);
  673. return 0;
  674. case FIONCLEX:
  675. set_close_on_exec(fd, 0);
  676. return 0;
  677. case FIONBIO:
  678. return ioctl_fionbio(filp, argp);
  679. case FIOASYNC:
  680. return ioctl_fioasync(fd, filp, argp);
  681. case FIOQSIZE:
  682. if (S_ISDIR(inode->i_mode) || S_ISREG(inode->i_mode) ||
  683. S_ISLNK(inode->i_mode)) {
  684. loff_t res = inode_get_bytes(inode);
  685. return copy_to_user(argp, &res, sizeof(res)) ?
  686. -EFAULT : 0;
  687. }
  688. return -ENOTTY;
  689. case FIFREEZE:
  690. return ioctl_fsfreeze(filp);
  691. case FITHAW:
  692. return ioctl_fsthaw(filp);
  693. case FS_IOC_FIEMAP:
  694. return ioctl_fiemap(filp, argp);
  695. case FIGETBSZ:
  696. /* anon_bdev filesystems may not have a block size */
  697. if (!inode->i_sb->s_blocksize)
  698. return -EINVAL;
  699. return put_user(inode->i_sb->s_blocksize, (int __user *)argp);
  700. case FICLONE:
  701. return ioctl_file_clone(filp, arg, 0, 0, 0);
  702. case FICLONERANGE:
  703. return ioctl_file_clone_range(filp, argp);
  704. case FIDEDUPERANGE:
  705. return ioctl_file_dedupe_range(filp, argp);
  706. case FIONREAD:
  707. if (!S_ISREG(inode->i_mode))
  708. return vfs_ioctl(filp, cmd, arg);
  709. return put_user(i_size_read(inode) - filp->f_pos,
  710. (int __user *)argp);
  711. case FS_IOC_GETFLAGS:
  712. return ioctl_getflags(filp, argp);
  713. case FS_IOC_SETFLAGS:
  714. return ioctl_setflags(filp, argp);
  715. case FS_IOC_FSGETXATTR:
  716. return ioctl_fsgetxattr(filp, argp);
  717. case FS_IOC_FSSETXATTR:
  718. return ioctl_fssetxattr(filp, argp);
  719. default:
  720. if (S_ISREG(inode->i_mode))
  721. return file_ioctl(filp, cmd, argp);
  722. break;
  723. }
  724. return -ENOIOCTLCMD;
  725. }
  726. SYSCALL_DEFINE3(ioctl, unsigned int, fd, unsigned int, cmd, unsigned long, arg)
  727. {
  728. struct fd f = fdget(fd);
  729. int error;
  730. if (!f.file)
  731. return -EBADF;
  732. error = security_file_ioctl(f.file, cmd, arg);
  733. if (error)
  734. goto out;
  735. error = do_vfs_ioctl(f.file, fd, cmd, arg);
  736. if (error == -ENOIOCTLCMD)
  737. error = vfs_ioctl(f.file, cmd, arg);
  738. out:
  739. fdput(f);
  740. return error;
  741. }
  742. #ifdef CONFIG_COMPAT
  743. /**
  744. * compat_ptr_ioctl - generic implementation of .compat_ioctl file operation
  745. *
  746. * This is not normally called as a function, but instead set in struct
  747. * file_operations as
  748. *
  749. * .compat_ioctl = compat_ptr_ioctl,
  750. *
  751. * On most architectures, the compat_ptr_ioctl() just passes all arguments
  752. * to the corresponding ->ioctl handler. The exception is arch/s390, where
  753. * compat_ptr() clears the top bit of a 32-bit pointer value, so user space
  754. * pointers to the second 2GB alias the first 2GB, as is the case for
  755. * native 32-bit s390 user space.
  756. *
  757. * The compat_ptr_ioctl() function must therefore be used only with ioctl
  758. * functions that either ignore the argument or pass a pointer to a
  759. * compatible data type.
  760. *
  761. * If any ioctl command handled by fops->unlocked_ioctl passes a plain
  762. * integer instead of a pointer, or any of the passed data types
  763. * is incompatible between 32-bit and 64-bit architectures, a proper
  764. * handler is required instead of compat_ptr_ioctl.
  765. */
  766. long compat_ptr_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  767. {
  768. if (!file->f_op->unlocked_ioctl)
  769. return -ENOIOCTLCMD;
  770. return file->f_op->unlocked_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
  771. }
  772. EXPORT_SYMBOL(compat_ptr_ioctl);
  773. COMPAT_SYSCALL_DEFINE3(ioctl, unsigned int, fd, unsigned int, cmd,
  774. compat_ulong_t, arg)
  775. {
  776. struct fd f = fdget(fd);
  777. int error;
  778. if (!f.file)
  779. return -EBADF;
  780. /* RED-PEN how should LSM module know it's handling 32bit? */
  781. error = security_file_ioctl(f.file, cmd, arg);
  782. if (error)
  783. goto out;
  784. switch (cmd) {
  785. /* FICLONE takes an int argument, so don't use compat_ptr() */
  786. case FICLONE:
  787. error = ioctl_file_clone(f.file, arg, 0, 0, 0);
  788. break;
  789. #if defined(CONFIG_X86_64)
  790. /* these get messy on amd64 due to alignment differences */
  791. case FS_IOC_RESVSP_32:
  792. case FS_IOC_RESVSP64_32:
  793. error = compat_ioctl_preallocate(f.file, 0, compat_ptr(arg));
  794. break;
  795. case FS_IOC_UNRESVSP_32:
  796. case FS_IOC_UNRESVSP64_32:
  797. error = compat_ioctl_preallocate(f.file, FALLOC_FL_PUNCH_HOLE,
  798. compat_ptr(arg));
  799. break;
  800. case FS_IOC_ZERO_RANGE_32:
  801. error = compat_ioctl_preallocate(f.file, FALLOC_FL_ZERO_RANGE,
  802. compat_ptr(arg));
  803. break;
  804. #endif
  805. /*
  806. * These access 32-bit values anyway so no further handling is
  807. * necessary.
  808. */
  809. case FS_IOC32_GETFLAGS:
  810. case FS_IOC32_SETFLAGS:
  811. cmd = (cmd == FS_IOC32_GETFLAGS) ?
  812. FS_IOC_GETFLAGS : FS_IOC_SETFLAGS;
  813. fallthrough;
  814. /*
  815. * everything else in do_vfs_ioctl() takes either a compatible
  816. * pointer argument or no argument -- call it with a modified
  817. * argument.
  818. */
  819. default:
  820. error = do_vfs_ioctl(f.file, fd, cmd,
  821. (unsigned long)compat_ptr(arg));
  822. if (error != -ENOIOCTLCMD)
  823. break;
  824. if (f.file->f_op->compat_ioctl)
  825. error = f.file->f_op->compat_ioctl(f.file, cmd, arg);
  826. if (error == -ENOIOCTLCMD)
  827. error = -ENOTTY;
  828. break;
  829. }
  830. out:
  831. fdput(f);
  832. return error;
  833. }
  834. #endif