xfs_ioctl32.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2004-2005 Silicon Graphics, Inc.
  4. * All Rights Reserved.
  5. */
  6. #include <linux/mount.h>
  7. #include <linux/fsmap.h>
  8. #include "xfs_platform.h"
  9. #include "xfs_fs.h"
  10. #include "xfs_shared.h"
  11. #include "xfs_format.h"
  12. #include "xfs_log_format.h"
  13. #include "xfs_trans_resv.h"
  14. #include "xfs_mount.h"
  15. #include "xfs_inode.h"
  16. #include "xfs_iwalk.h"
  17. #include "xfs_itable.h"
  18. #include "xfs_fsops.h"
  19. #include "xfs_rtalloc.h"
  20. #include "xfs_da_format.h"
  21. #include "xfs_da_btree.h"
  22. #include "xfs_attr.h"
  23. #include "xfs_ioctl.h"
  24. #include "xfs_ioctl32.h"
  25. #include "xfs_trace.h"
  26. #include "xfs_sb.h"
  27. #include "xfs_handle.h"
  28. #define _NATIVE_IOC(cmd, type) \
  29. _IOC(_IOC_DIR(cmd), _IOC_TYPE(cmd), _IOC_NR(cmd), sizeof(type))
  30. #ifdef BROKEN_X86_ALIGNMENT
  31. STATIC int
  32. xfs_compat_ioc_fsgeometry_v1(
  33. struct xfs_mount *mp,
  34. compat_xfs_fsop_geom_v1_t __user *arg32)
  35. {
  36. struct xfs_fsop_geom fsgeo;
  37. xfs_fs_geometry(mp, &fsgeo, 3);
  38. /* The 32-bit variant simply has some padding at the end */
  39. if (copy_to_user(arg32, &fsgeo, sizeof(struct compat_xfs_fsop_geom_v1)))
  40. return -EFAULT;
  41. return 0;
  42. }
  43. STATIC int
  44. xfs_compat_growfs_data_copyin(
  45. struct xfs_growfs_data *in,
  46. compat_xfs_growfs_data_t __user *arg32)
  47. {
  48. if (get_user(in->newblocks, &arg32->newblocks) ||
  49. get_user(in->imaxpct, &arg32->imaxpct))
  50. return -EFAULT;
  51. return 0;
  52. }
  53. STATIC int
  54. xfs_compat_growfs_rt_copyin(
  55. struct xfs_growfs_rt *in,
  56. compat_xfs_growfs_rt_t __user *arg32)
  57. {
  58. if (get_user(in->newblocks, &arg32->newblocks) ||
  59. get_user(in->extsize, &arg32->extsize))
  60. return -EFAULT;
  61. return 0;
  62. }
  63. STATIC int
  64. xfs_fsinumbers_fmt_compat(
  65. struct xfs_ibulk *breq,
  66. const struct xfs_inumbers *ig)
  67. {
  68. struct compat_xfs_inogrp __user *p32 = breq->ubuffer;
  69. struct xfs_inogrp ig1;
  70. struct xfs_inogrp *igrp = &ig1;
  71. xfs_inumbers_to_inogrp(&ig1, ig);
  72. if (put_user(igrp->xi_startino, &p32->xi_startino) ||
  73. put_user(igrp->xi_alloccount, &p32->xi_alloccount) ||
  74. put_user(igrp->xi_allocmask, &p32->xi_allocmask))
  75. return -EFAULT;
  76. return xfs_ibulk_advance(breq, sizeof(struct compat_xfs_inogrp));
  77. }
  78. #else
  79. #define xfs_fsinumbers_fmt_compat xfs_fsinumbers_fmt
  80. #endif /* BROKEN_X86_ALIGNMENT */
  81. STATIC int
  82. xfs_ioctl32_bstime_copyin(
  83. xfs_bstime_t *bstime,
  84. compat_xfs_bstime_t __user *bstime32)
  85. {
  86. old_time32_t sec32; /* tv_sec differs on 64 vs. 32 */
  87. if (get_user(sec32, &bstime32->tv_sec) ||
  88. get_user(bstime->tv_nsec, &bstime32->tv_nsec))
  89. return -EFAULT;
  90. bstime->tv_sec = sec32;
  91. return 0;
  92. }
  93. /*
  94. * struct xfs_bstat has differing alignment on intel, & bstime_t sizes
  95. * everywhere
  96. */
  97. STATIC int
  98. xfs_ioctl32_bstat_copyin(
  99. struct xfs_bstat *bstat,
  100. struct compat_xfs_bstat __user *bstat32)
  101. {
  102. if (get_user(bstat->bs_ino, &bstat32->bs_ino) ||
  103. get_user(bstat->bs_mode, &bstat32->bs_mode) ||
  104. get_user(bstat->bs_nlink, &bstat32->bs_nlink) ||
  105. get_user(bstat->bs_uid, &bstat32->bs_uid) ||
  106. get_user(bstat->bs_gid, &bstat32->bs_gid) ||
  107. get_user(bstat->bs_rdev, &bstat32->bs_rdev) ||
  108. get_user(bstat->bs_blksize, &bstat32->bs_blksize) ||
  109. get_user(bstat->bs_size, &bstat32->bs_size) ||
  110. xfs_ioctl32_bstime_copyin(&bstat->bs_atime, &bstat32->bs_atime) ||
  111. xfs_ioctl32_bstime_copyin(&bstat->bs_mtime, &bstat32->bs_mtime) ||
  112. xfs_ioctl32_bstime_copyin(&bstat->bs_ctime, &bstat32->bs_ctime) ||
  113. get_user(bstat->bs_blocks, &bstat32->bs_size) ||
  114. get_user(bstat->bs_xflags, &bstat32->bs_size) ||
  115. get_user(bstat->bs_extsize, &bstat32->bs_extsize) ||
  116. get_user(bstat->bs_extents, &bstat32->bs_extents) ||
  117. get_user(bstat->bs_gen, &bstat32->bs_gen) ||
  118. get_user(bstat->bs_projid_lo, &bstat32->bs_projid_lo) ||
  119. get_user(bstat->bs_projid_hi, &bstat32->bs_projid_hi) ||
  120. get_user(bstat->bs_forkoff, &bstat32->bs_forkoff) ||
  121. get_user(bstat->bs_dmevmask, &bstat32->bs_dmevmask) ||
  122. get_user(bstat->bs_dmstate, &bstat32->bs_dmstate) ||
  123. get_user(bstat->bs_aextents, &bstat32->bs_aextents))
  124. return -EFAULT;
  125. return 0;
  126. }
  127. /* XFS_IOC_FSBULKSTAT and friends */
  128. STATIC int
  129. xfs_bstime_store_compat(
  130. compat_xfs_bstime_t __user *p32,
  131. const xfs_bstime_t *p)
  132. {
  133. __s32 sec32;
  134. sec32 = p->tv_sec;
  135. if (put_user(sec32, &p32->tv_sec) ||
  136. put_user(p->tv_nsec, &p32->tv_nsec))
  137. return -EFAULT;
  138. return 0;
  139. }
  140. /* Return 0 on success or positive error (to xfs_bulkstat()) */
  141. STATIC int
  142. xfs_fsbulkstat_one_fmt_compat(
  143. struct xfs_ibulk *breq,
  144. const struct xfs_bulkstat *bstat)
  145. {
  146. struct compat_xfs_bstat __user *p32 = breq->ubuffer;
  147. struct xfs_bstat bs1;
  148. struct xfs_bstat *buffer = &bs1;
  149. xfs_bulkstat_to_bstat(breq->mp, &bs1, bstat);
  150. if (put_user(buffer->bs_ino, &p32->bs_ino) ||
  151. put_user(buffer->bs_mode, &p32->bs_mode) ||
  152. put_user(buffer->bs_nlink, &p32->bs_nlink) ||
  153. put_user(buffer->bs_uid, &p32->bs_uid) ||
  154. put_user(buffer->bs_gid, &p32->bs_gid) ||
  155. put_user(buffer->bs_rdev, &p32->bs_rdev) ||
  156. put_user(buffer->bs_blksize, &p32->bs_blksize) ||
  157. put_user(buffer->bs_size, &p32->bs_size) ||
  158. xfs_bstime_store_compat(&p32->bs_atime, &buffer->bs_atime) ||
  159. xfs_bstime_store_compat(&p32->bs_mtime, &buffer->bs_mtime) ||
  160. xfs_bstime_store_compat(&p32->bs_ctime, &buffer->bs_ctime) ||
  161. put_user(buffer->bs_blocks, &p32->bs_blocks) ||
  162. put_user(buffer->bs_xflags, &p32->bs_xflags) ||
  163. put_user(buffer->bs_extsize, &p32->bs_extsize) ||
  164. put_user(buffer->bs_extents, &p32->bs_extents) ||
  165. put_user(buffer->bs_gen, &p32->bs_gen) ||
  166. put_user(buffer->bs_projid, &p32->bs_projid) ||
  167. put_user(buffer->bs_projid_hi, &p32->bs_projid_hi) ||
  168. put_user(buffer->bs_forkoff, &p32->bs_forkoff) ||
  169. put_user(buffer->bs_dmevmask, &p32->bs_dmevmask) ||
  170. put_user(buffer->bs_dmstate, &p32->bs_dmstate) ||
  171. put_user(buffer->bs_aextents, &p32->bs_aextents))
  172. return -EFAULT;
  173. return xfs_ibulk_advance(breq, sizeof(struct compat_xfs_bstat));
  174. }
  175. /* copied from xfs_ioctl.c */
  176. STATIC int
  177. xfs_compat_ioc_fsbulkstat(
  178. struct file *file,
  179. unsigned int cmd,
  180. struct compat_xfs_fsop_bulkreq __user *p32)
  181. {
  182. struct xfs_mount *mp = XFS_I(file_inode(file))->i_mount;
  183. u32 addr;
  184. struct xfs_fsop_bulkreq bulkreq;
  185. struct xfs_ibulk breq = {
  186. .mp = mp,
  187. .idmap = file_mnt_idmap(file),
  188. .ocount = 0,
  189. };
  190. xfs_ino_t lastino;
  191. int error;
  192. /*
  193. * Output structure handling functions. Depending on the command,
  194. * either the xfs_bstat and xfs_inogrp structures are written out
  195. * to userpace memory via bulkreq.ubuffer. Normally the compat
  196. * functions and structure size are the correct ones to use ...
  197. */
  198. inumbers_fmt_pf inumbers_func = xfs_fsinumbers_fmt_compat;
  199. bulkstat_one_fmt_pf bs_one_func = xfs_fsbulkstat_one_fmt_compat;
  200. #ifdef CONFIG_X86_X32_ABI
  201. if (in_x32_syscall()) {
  202. /*
  203. * ... but on x32 the input xfs_fsop_bulkreq has pointers
  204. * which must be handled in the "compat" (32-bit) way, while
  205. * the xfs_bstat and xfs_inogrp structures follow native 64-
  206. * bit layout convention. So adjust accordingly, otherwise
  207. * the data written out in compat layout will not match what
  208. * x32 userspace expects.
  209. */
  210. inumbers_func = xfs_fsinumbers_fmt;
  211. bs_one_func = xfs_fsbulkstat_one_fmt;
  212. }
  213. #endif
  214. /* done = 1 if there are more stats to get and if bulkstat */
  215. /* should be called again (unused here, but used in dmapi) */
  216. if (!capable(CAP_SYS_ADMIN))
  217. return -EPERM;
  218. if (xfs_is_shutdown(mp))
  219. return -EIO;
  220. if (get_user(addr, &p32->lastip))
  221. return -EFAULT;
  222. bulkreq.lastip = compat_ptr(addr);
  223. if (get_user(bulkreq.icount, &p32->icount) ||
  224. get_user(addr, &p32->ubuffer))
  225. return -EFAULT;
  226. bulkreq.ubuffer = compat_ptr(addr);
  227. if (get_user(addr, &p32->ocount))
  228. return -EFAULT;
  229. bulkreq.ocount = compat_ptr(addr);
  230. if (copy_from_user(&lastino, bulkreq.lastip, sizeof(__s64)))
  231. return -EFAULT;
  232. if (bulkreq.icount <= 0)
  233. return -EINVAL;
  234. if (bulkreq.ubuffer == NULL)
  235. return -EINVAL;
  236. breq.ubuffer = bulkreq.ubuffer;
  237. breq.icount = bulkreq.icount;
  238. /*
  239. * FSBULKSTAT_SINGLE expects that *lastip contains the inode number
  240. * that we want to stat. However, FSINUMBERS and FSBULKSTAT expect
  241. * that *lastip contains either zero or the number of the last inode to
  242. * be examined by the previous call and return results starting with
  243. * the next inode after that. The new bulk request back end functions
  244. * take the inode to start with, so we have to compute the startino
  245. * parameter from lastino to maintain correct function. lastino == 0
  246. * is a special case because it has traditionally meant "first inode
  247. * in filesystem".
  248. */
  249. if (cmd == XFS_IOC_FSINUMBERS_32) {
  250. breq.startino = lastino ? lastino + 1 : 0;
  251. error = xfs_inumbers(&breq, inumbers_func);
  252. lastino = breq.startino - 1;
  253. } else if (cmd == XFS_IOC_FSBULKSTAT_SINGLE_32) {
  254. breq.startino = lastino;
  255. breq.icount = 1;
  256. error = xfs_bulkstat_one(&breq, bs_one_func);
  257. lastino = breq.startino;
  258. } else if (cmd == XFS_IOC_FSBULKSTAT_32) {
  259. breq.startino = lastino ? lastino + 1 : 0;
  260. error = xfs_bulkstat(&breq, bs_one_func);
  261. lastino = breq.startino - 1;
  262. } else {
  263. error = -EINVAL;
  264. }
  265. if (error)
  266. return error;
  267. if (bulkreq.lastip != NULL &&
  268. copy_to_user(bulkreq.lastip, &lastino, sizeof(xfs_ino_t)))
  269. return -EFAULT;
  270. if (bulkreq.ocount != NULL &&
  271. copy_to_user(bulkreq.ocount, &breq.ocount, sizeof(__s32)))
  272. return -EFAULT;
  273. return 0;
  274. }
  275. STATIC int
  276. xfs_compat_handlereq_copyin(
  277. xfs_fsop_handlereq_t *hreq,
  278. compat_xfs_fsop_handlereq_t __user *arg32)
  279. {
  280. compat_xfs_fsop_handlereq_t hreq32;
  281. if (copy_from_user(&hreq32, arg32, sizeof(compat_xfs_fsop_handlereq_t)))
  282. return -EFAULT;
  283. hreq->fd = hreq32.fd;
  284. hreq->path = compat_ptr(hreq32.path);
  285. hreq->oflags = hreq32.oflags;
  286. hreq->ihandle = compat_ptr(hreq32.ihandle);
  287. hreq->ihandlen = hreq32.ihandlen;
  288. hreq->ohandle = compat_ptr(hreq32.ohandle);
  289. hreq->ohandlen = compat_ptr(hreq32.ohandlen);
  290. return 0;
  291. }
  292. STATIC struct dentry *
  293. xfs_compat_handlereq_to_dentry(
  294. struct file *parfilp,
  295. compat_xfs_fsop_handlereq_t *hreq)
  296. {
  297. return xfs_handle_to_dentry(parfilp,
  298. compat_ptr(hreq->ihandle), hreq->ihandlen);
  299. }
  300. STATIC int
  301. xfs_compat_attrlist_by_handle(
  302. struct file *parfilp,
  303. compat_xfs_fsop_attrlist_handlereq_t __user *p)
  304. {
  305. compat_xfs_fsop_attrlist_handlereq_t al_hreq;
  306. struct dentry *dentry;
  307. int error;
  308. if (!capable(CAP_SYS_ADMIN))
  309. return -EPERM;
  310. if (copy_from_user(&al_hreq, p, sizeof(al_hreq)))
  311. return -EFAULT;
  312. dentry = xfs_compat_handlereq_to_dentry(parfilp, &al_hreq.hreq);
  313. if (IS_ERR(dentry))
  314. return PTR_ERR(dentry);
  315. error = xfs_ioc_attr_list(XFS_I(d_inode(dentry)),
  316. compat_ptr(al_hreq.buffer), al_hreq.buflen,
  317. al_hreq.flags, &p->pos);
  318. dput(dentry);
  319. return error;
  320. }
  321. STATIC int
  322. xfs_compat_attrmulti_by_handle(
  323. struct file *parfilp,
  324. void __user *arg)
  325. {
  326. int error;
  327. compat_xfs_attr_multiop_t *ops;
  328. compat_xfs_fsop_attrmulti_handlereq_t am_hreq;
  329. struct dentry *dentry;
  330. unsigned int i, size;
  331. if (!capable(CAP_SYS_ADMIN))
  332. return -EPERM;
  333. if (copy_from_user(&am_hreq, arg,
  334. sizeof(compat_xfs_fsop_attrmulti_handlereq_t)))
  335. return -EFAULT;
  336. /* overflow check */
  337. if (am_hreq.opcount >= INT_MAX / sizeof(compat_xfs_attr_multiop_t))
  338. return -E2BIG;
  339. dentry = xfs_compat_handlereq_to_dentry(parfilp, &am_hreq.hreq);
  340. if (IS_ERR(dentry))
  341. return PTR_ERR(dentry);
  342. error = -E2BIG;
  343. size = am_hreq.opcount * sizeof(compat_xfs_attr_multiop_t);
  344. if (!size || size > 16 * PAGE_SIZE)
  345. goto out_dput;
  346. ops = memdup_user(compat_ptr(am_hreq.ops), size);
  347. if (IS_ERR(ops)) {
  348. error = PTR_ERR(ops);
  349. goto out_dput;
  350. }
  351. error = 0;
  352. for (i = 0; i < am_hreq.opcount; i++) {
  353. ops[i].am_error = xfs_ioc_attrmulti_one(parfilp,
  354. d_inode(dentry), ops[i].am_opcode,
  355. compat_ptr(ops[i].am_attrname),
  356. compat_ptr(ops[i].am_attrvalue),
  357. &ops[i].am_length, ops[i].am_flags);
  358. }
  359. if (copy_to_user(compat_ptr(am_hreq.ops), ops, size))
  360. error = -EFAULT;
  361. kfree(ops);
  362. out_dput:
  363. dput(dentry);
  364. return error;
  365. }
  366. long
  367. xfs_file_compat_ioctl(
  368. struct file *filp,
  369. unsigned cmd,
  370. unsigned long p)
  371. {
  372. struct inode *inode = file_inode(filp);
  373. struct xfs_inode *ip = XFS_I(inode);
  374. void __user *arg = compat_ptr(p);
  375. int error;
  376. trace_xfs_file_compat_ioctl(ip);
  377. switch (cmd) {
  378. #if defined(BROKEN_X86_ALIGNMENT)
  379. case XFS_IOC_FSGEOMETRY_V1_32:
  380. return xfs_compat_ioc_fsgeometry_v1(ip->i_mount, arg);
  381. case XFS_IOC_FSGROWFSDATA_32: {
  382. struct xfs_growfs_data in;
  383. if (xfs_compat_growfs_data_copyin(&in, arg))
  384. return -EFAULT;
  385. error = mnt_want_write_file(filp);
  386. if (error)
  387. return error;
  388. error = xfs_growfs_data(ip->i_mount, &in);
  389. mnt_drop_write_file(filp);
  390. return error;
  391. }
  392. case XFS_IOC_FSGROWFSRT_32: {
  393. struct xfs_growfs_rt in;
  394. if (xfs_compat_growfs_rt_copyin(&in, arg))
  395. return -EFAULT;
  396. error = mnt_want_write_file(filp);
  397. if (error)
  398. return error;
  399. error = xfs_growfs_rt(ip->i_mount, &in);
  400. mnt_drop_write_file(filp);
  401. return error;
  402. }
  403. #endif
  404. /* long changes size, but xfs only copiese out 32 bits */
  405. case XFS_IOC_GETVERSION_32:
  406. cmd = _NATIVE_IOC(cmd, long);
  407. return xfs_file_ioctl(filp, cmd, p);
  408. case XFS_IOC_SWAPEXT_32: {
  409. struct xfs_swapext sxp;
  410. struct compat_xfs_swapext __user *sxu = arg;
  411. /* Bulk copy in up to the sx_stat field, then copy bstat */
  412. if (copy_from_user(&sxp, sxu,
  413. offsetof(struct xfs_swapext, sx_stat)) ||
  414. xfs_ioctl32_bstat_copyin(&sxp.sx_stat, &sxu->sx_stat))
  415. return -EFAULT;
  416. error = mnt_want_write_file(filp);
  417. if (error)
  418. return error;
  419. error = xfs_ioc_swapext(&sxp);
  420. mnt_drop_write_file(filp);
  421. return error;
  422. }
  423. case XFS_IOC_FSBULKSTAT_32:
  424. case XFS_IOC_FSBULKSTAT_SINGLE_32:
  425. case XFS_IOC_FSINUMBERS_32:
  426. return xfs_compat_ioc_fsbulkstat(filp, cmd, arg);
  427. case XFS_IOC_FD_TO_HANDLE_32:
  428. case XFS_IOC_PATH_TO_HANDLE_32:
  429. case XFS_IOC_PATH_TO_FSHANDLE_32: {
  430. struct xfs_fsop_handlereq hreq;
  431. if (xfs_compat_handlereq_copyin(&hreq, arg))
  432. return -EFAULT;
  433. cmd = _NATIVE_IOC(cmd, struct xfs_fsop_handlereq);
  434. return xfs_find_handle(cmd, &hreq);
  435. }
  436. case XFS_IOC_OPEN_BY_HANDLE_32: {
  437. struct xfs_fsop_handlereq hreq;
  438. if (xfs_compat_handlereq_copyin(&hreq, arg))
  439. return -EFAULT;
  440. return xfs_open_by_handle(filp, &hreq);
  441. }
  442. case XFS_IOC_READLINK_BY_HANDLE_32: {
  443. struct xfs_fsop_handlereq hreq;
  444. if (xfs_compat_handlereq_copyin(&hreq, arg))
  445. return -EFAULT;
  446. return xfs_readlink_by_handle(filp, &hreq);
  447. }
  448. case XFS_IOC_ATTRLIST_BY_HANDLE_32:
  449. return xfs_compat_attrlist_by_handle(filp, arg);
  450. case XFS_IOC_ATTRMULTI_BY_HANDLE_32:
  451. return xfs_compat_attrmulti_by_handle(filp, arg);
  452. default:
  453. /* try the native version */
  454. return xfs_file_ioctl(filp, cmd, (unsigned long)arg);
  455. }
  456. }