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