| 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | #include <linux/fs.h> |
| 3 | #include <linux/security.h> |
| 4 | #include <linux/fscrypt.h> |
| 5 | #include <linux/fileattr.h> |
| 6 | #include <linux/export.h> |
| 7 | #include <linux/syscalls.h> |
| 8 | #include <linux/namei.h> |
| 9 | |
| 10 | #include "internal.h" |
| 11 | |
| 12 | /** |
| 13 | * fileattr_fill_xflags - initialize fileattr with xflags |
| 14 | * @fa: fileattr pointer |
| 15 | * @xflags: FS_XFLAG_* flags |
| 16 | * |
| 17 | * Set ->fsx_xflags, ->fsx_valid and ->flags (translated xflags). All |
| 18 | * other fields are zeroed. |
| 19 | */ |
| 20 | void fileattr_fill_xflags(struct file_kattr *fa, u32 xflags) |
| 21 | { |
| 22 | memset(s: fa, c: 0, n: sizeof(*fa)); |
| 23 | fa->fsx_valid = true; |
| 24 | fa->fsx_xflags = xflags; |
| 25 | if (fa->fsx_xflags & FS_XFLAG_IMMUTABLE) |
| 26 | fa->flags |= FS_IMMUTABLE_FL; |
| 27 | if (fa->fsx_xflags & FS_XFLAG_APPEND) |
| 28 | fa->flags |= FS_APPEND_FL; |
| 29 | if (fa->fsx_xflags & FS_XFLAG_SYNC) |
| 30 | fa->flags |= FS_SYNC_FL; |
| 31 | if (fa->fsx_xflags & FS_XFLAG_NOATIME) |
| 32 | fa->flags |= FS_NOATIME_FL; |
| 33 | if (fa->fsx_xflags & FS_XFLAG_NODUMP) |
| 34 | fa->flags |= FS_NODUMP_FL; |
| 35 | if (fa->fsx_xflags & FS_XFLAG_DAX) |
| 36 | fa->flags |= FS_DAX_FL; |
| 37 | if (fa->fsx_xflags & FS_XFLAG_PROJINHERIT) |
| 38 | fa->flags |= FS_PROJINHERIT_FL; |
| 39 | } |
| 40 | EXPORT_SYMBOL(fileattr_fill_xflags); |
| 41 | |
| 42 | /** |
| 43 | * fileattr_fill_flags - initialize fileattr with flags |
| 44 | * @fa: fileattr pointer |
| 45 | * @flags: FS_*_FL flags |
| 46 | * |
| 47 | * Set ->flags, ->flags_valid and ->fsx_xflags (translated flags). |
| 48 | * All other fields are zeroed. |
| 49 | */ |
| 50 | void fileattr_fill_flags(struct file_kattr *fa, u32 flags) |
| 51 | { |
| 52 | memset(s: fa, c: 0, n: sizeof(*fa)); |
| 53 | fa->flags_valid = true; |
| 54 | fa->flags = flags; |
| 55 | if (fa->flags & FS_SYNC_FL) |
| 56 | fa->fsx_xflags |= FS_XFLAG_SYNC; |
| 57 | if (fa->flags & FS_IMMUTABLE_FL) |
| 58 | fa->fsx_xflags |= FS_XFLAG_IMMUTABLE; |
| 59 | if (fa->flags & FS_APPEND_FL) |
| 60 | fa->fsx_xflags |= FS_XFLAG_APPEND; |
| 61 | if (fa->flags & FS_NODUMP_FL) |
| 62 | fa->fsx_xflags |= FS_XFLAG_NODUMP; |
| 63 | if (fa->flags & FS_NOATIME_FL) |
| 64 | fa->fsx_xflags |= FS_XFLAG_NOATIME; |
| 65 | if (fa->flags & FS_DAX_FL) |
| 66 | fa->fsx_xflags |= FS_XFLAG_DAX; |
| 67 | if (fa->flags & FS_PROJINHERIT_FL) |
| 68 | fa->fsx_xflags |= FS_XFLAG_PROJINHERIT; |
| 69 | } |
| 70 | EXPORT_SYMBOL(fileattr_fill_flags); |
| 71 | |
| 72 | /** |
| 73 | * vfs_fileattr_get - retrieve miscellaneous file attributes |
| 74 | * @dentry: the object to retrieve from |
| 75 | * @fa: fileattr pointer |
| 76 | * |
| 77 | * Call i_op->fileattr_get() callback, if exists. |
| 78 | * |
| 79 | * Return: 0 on success, or a negative error on failure. |
| 80 | */ |
| 81 | int vfs_fileattr_get(struct dentry *dentry, struct file_kattr *fa) |
| 82 | { |
| 83 | struct inode *inode = d_inode(dentry); |
| 84 | int error; |
| 85 | |
| 86 | if (!inode->i_op->fileattr_get) |
| 87 | return -EOPNOTSUPP; |
| 88 | |
| 89 | error = security_inode_file_getattr(dentry, fa); |
| 90 | if (error) |
| 91 | return error; |
| 92 | |
| 93 | return inode->i_op->fileattr_get(dentry, fa); |
| 94 | } |
| 95 | EXPORT_SYMBOL(vfs_fileattr_get); |
| 96 | |
| 97 | static void fileattr_to_file_attr(const struct file_kattr *fa, |
| 98 | struct file_attr *fattr) |
| 99 | { |
| 100 | __u32 mask = FS_XFLAGS_MASK; |
| 101 | |
| 102 | memset(s: fattr, c: 0, n: sizeof(struct file_attr)); |
| 103 | fattr->fa_xflags = fa->fsx_xflags & mask; |
| 104 | fattr->fa_extsize = fa->fsx_extsize; |
| 105 | fattr->fa_nextents = fa->fsx_nextents; |
| 106 | fattr->fa_projid = fa->fsx_projid; |
| 107 | fattr->fa_cowextsize = fa->fsx_cowextsize; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * copy_fsxattr_to_user - copy fsxattr to userspace. |
| 112 | * @fa: fileattr pointer |
| 113 | * @ufa: fsxattr user pointer |
| 114 | * |
| 115 | * Return: 0 on success, or -EFAULT on failure. |
| 116 | */ |
| 117 | int copy_fsxattr_to_user(const struct file_kattr *fa, struct fsxattr __user *ufa) |
| 118 | { |
| 119 | struct fsxattr xfa; |
| 120 | __u32 mask = FS_XFLAGS_MASK; |
| 121 | |
| 122 | memset(s: &xfa, c: 0, n: sizeof(xfa)); |
| 123 | xfa.fsx_xflags = fa->fsx_xflags & mask; |
| 124 | xfa.fsx_extsize = fa->fsx_extsize; |
| 125 | xfa.fsx_nextents = fa->fsx_nextents; |
| 126 | xfa.fsx_projid = fa->fsx_projid; |
| 127 | xfa.fsx_cowextsize = fa->fsx_cowextsize; |
| 128 | |
| 129 | if (copy_to_user(to: ufa, from: &xfa, n: sizeof(xfa))) |
| 130 | return -EFAULT; |
| 131 | |
| 132 | return 0; |
| 133 | } |
| 134 | EXPORT_SYMBOL(copy_fsxattr_to_user); |
| 135 | |
| 136 | static int file_attr_to_fileattr(const struct file_attr *fattr, |
| 137 | struct file_kattr *fa) |
| 138 | { |
| 139 | __u64 mask = FS_XFLAGS_MASK; |
| 140 | |
| 141 | if (fattr->fa_xflags & ~mask) |
| 142 | return -EINVAL; |
| 143 | |
| 144 | fileattr_fill_xflags(fa, fattr->fa_xflags); |
| 145 | fa->fsx_xflags &= ~FS_XFLAG_RDONLY_MASK; |
| 146 | fa->fsx_extsize = fattr->fa_extsize; |
| 147 | fa->fsx_projid = fattr->fa_projid; |
| 148 | fa->fsx_cowextsize = fattr->fa_cowextsize; |
| 149 | |
| 150 | return 0; |
| 151 | } |
| 152 | |
| 153 | static int copy_fsxattr_from_user(struct file_kattr *fa, |
| 154 | struct fsxattr __user *ufa) |
| 155 | { |
| 156 | struct fsxattr xfa; |
| 157 | __u32 mask = FS_XFLAGS_MASK; |
| 158 | |
| 159 | if (copy_from_user(to: &xfa, from: ufa, n: sizeof(xfa))) |
| 160 | return -EFAULT; |
| 161 | |
| 162 | if (xfa.fsx_xflags & ~mask) |
| 163 | return -EOPNOTSUPP; |
| 164 | |
| 165 | fileattr_fill_xflags(fa, xfa.fsx_xflags); |
| 166 | fa->fsx_xflags &= ~FS_XFLAG_RDONLY_MASK; |
| 167 | fa->fsx_extsize = xfa.fsx_extsize; |
| 168 | fa->fsx_nextents = xfa.fsx_nextents; |
| 169 | fa->fsx_projid = xfa.fsx_projid; |
| 170 | fa->fsx_cowextsize = xfa.fsx_cowextsize; |
| 171 | |
| 172 | return 0; |
| 173 | } |
| 174 | |
| 175 | /* |
| 176 | * Generic function to check FS_IOC_FSSETXATTR/FS_IOC_SETFLAGS values and reject |
| 177 | * any invalid configurations. |
| 178 | * |
| 179 | * Note: must be called with inode lock held. |
| 180 | */ |
| 181 | static int fileattr_set_prepare(struct inode *inode, |
| 182 | const struct file_kattr *old_ma, |
| 183 | struct file_kattr *fa) |
| 184 | { |
| 185 | int err; |
| 186 | |
| 187 | /* |
| 188 | * The IMMUTABLE and APPEND_ONLY flags can only be changed by |
| 189 | * the relevant capability. |
| 190 | */ |
| 191 | if ((fa->flags ^ old_ma->flags) & (FS_APPEND_FL | FS_IMMUTABLE_FL) && |
| 192 | !capable(CAP_LINUX_IMMUTABLE)) |
| 193 | return -EPERM; |
| 194 | |
| 195 | err = fscrypt_prepare_setflags(inode, oldflags: old_ma->flags, flags: fa->flags); |
| 196 | if (err) |
| 197 | return err; |
| 198 | |
| 199 | /* |
| 200 | * Project Quota ID state is only allowed to change from within the init |
| 201 | * namespace. Enforce that restriction only if we are trying to change |
| 202 | * the quota ID state. Everything else is allowed in user namespaces. |
| 203 | */ |
| 204 | if (current_user_ns() != &init_user_ns) { |
| 205 | if (old_ma->fsx_projid != fa->fsx_projid) |
| 206 | return -EINVAL; |
| 207 | if ((old_ma->fsx_xflags ^ fa->fsx_xflags) & |
| 208 | FS_XFLAG_PROJINHERIT) |
| 209 | return -EINVAL; |
| 210 | } else { |
| 211 | /* |
| 212 | * Caller is allowed to change the project ID. If it is being |
| 213 | * changed, make sure that the new value is valid. |
| 214 | */ |
| 215 | if (old_ma->fsx_projid != fa->fsx_projid && |
| 216 | !projid_valid(projid: make_kprojid(from: &init_user_ns, projid: fa->fsx_projid))) |
| 217 | return -EINVAL; |
| 218 | } |
| 219 | |
| 220 | /* Check extent size hints. */ |
| 221 | if ((fa->fsx_xflags & FS_XFLAG_EXTSIZE) && !S_ISREG(inode->i_mode)) |
| 222 | return -EINVAL; |
| 223 | |
| 224 | if ((fa->fsx_xflags & FS_XFLAG_EXTSZINHERIT) && |
| 225 | !S_ISDIR(inode->i_mode)) |
| 226 | return -EINVAL; |
| 227 | |
| 228 | if ((fa->fsx_xflags & FS_XFLAG_COWEXTSIZE) && |
| 229 | !S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode)) |
| 230 | return -EINVAL; |
| 231 | |
| 232 | /* |
| 233 | * It is only valid to set the DAX flag on regular files and |
| 234 | * directories on filesystems. |
| 235 | */ |
| 236 | if ((fa->fsx_xflags & FS_XFLAG_DAX) && |
| 237 | !(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode))) |
| 238 | return -EINVAL; |
| 239 | |
| 240 | /* Extent size hints of zero turn off the flags. */ |
| 241 | if (fa->fsx_extsize == 0) |
| 242 | fa->fsx_xflags &= ~(FS_XFLAG_EXTSIZE | FS_XFLAG_EXTSZINHERIT); |
| 243 | if (fa->fsx_cowextsize == 0) |
| 244 | fa->fsx_xflags &= ~FS_XFLAG_COWEXTSIZE; |
| 245 | |
| 246 | return 0; |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * vfs_fileattr_set - change miscellaneous file attributes |
| 251 | * @idmap: idmap of the mount |
| 252 | * @dentry: the object to change |
| 253 | * @fa: fileattr pointer |
| 254 | * |
| 255 | * After verifying permissions, call i_op->fileattr_set() callback, if |
| 256 | * exists. |
| 257 | * |
| 258 | * Verifying attributes involves retrieving current attributes with |
| 259 | * i_op->fileattr_get(), this also allows initializing attributes that have |
| 260 | * not been set by the caller to current values. Inode lock is held |
| 261 | * thoughout to prevent racing with another instance. |
| 262 | * |
| 263 | * Return: 0 on success, or a negative error on failure. |
| 264 | */ |
| 265 | int vfs_fileattr_set(struct mnt_idmap *idmap, struct dentry *dentry, |
| 266 | struct file_kattr *fa) |
| 267 | { |
| 268 | struct inode *inode = d_inode(dentry); |
| 269 | struct file_kattr old_ma = {}; |
| 270 | int err; |
| 271 | |
| 272 | if (!inode->i_op->fileattr_set) |
| 273 | return -EOPNOTSUPP; |
| 274 | |
| 275 | if (!inode_owner_or_capable(idmap, inode)) |
| 276 | return -EPERM; |
| 277 | |
| 278 | inode_lock(inode); |
| 279 | err = vfs_fileattr_get(dentry, &old_ma); |
| 280 | if (!err) { |
| 281 | /* initialize missing bits from old_ma */ |
| 282 | if (fa->flags_valid) { |
| 283 | fa->fsx_xflags |= old_ma.fsx_xflags & ~FS_XFLAG_COMMON; |
| 284 | fa->fsx_extsize = old_ma.fsx_extsize; |
| 285 | fa->fsx_nextents = old_ma.fsx_nextents; |
| 286 | fa->fsx_projid = old_ma.fsx_projid; |
| 287 | fa->fsx_cowextsize = old_ma.fsx_cowextsize; |
| 288 | } else { |
| 289 | fa->flags |= old_ma.flags & ~FS_COMMON_FL; |
| 290 | } |
| 291 | |
| 292 | err = fileattr_set_prepare(inode, old_ma: &old_ma, fa); |
| 293 | if (err) |
| 294 | goto out; |
| 295 | err = security_inode_file_setattr(dentry, fa); |
| 296 | if (err) |
| 297 | goto out; |
| 298 | err = inode->i_op->fileattr_set(idmap, dentry, fa); |
| 299 | if (err) |
| 300 | goto out; |
| 301 | } |
| 302 | |
| 303 | out: |
| 304 | inode_unlock(inode); |
| 305 | return err; |
| 306 | } |
| 307 | EXPORT_SYMBOL(vfs_fileattr_set); |
| 308 | |
| 309 | int ioctl_getflags(struct file *file, unsigned int __user *argp) |
| 310 | { |
| 311 | struct file_kattr fa = { .flags_valid = true }; /* hint only */ |
| 312 | int err; |
| 313 | |
| 314 | err = vfs_fileattr_get(file->f_path.dentry, &fa); |
| 315 | if (err == -EOPNOTSUPP) |
| 316 | err = -ENOIOCTLCMD; |
| 317 | if (!err) |
| 318 | err = put_user(fa.flags, argp); |
| 319 | return err; |
| 320 | } |
| 321 | EXPORT_SYMBOL(ioctl_getflags); |
| 322 | |
| 323 | int ioctl_setflags(struct file *file, unsigned int __user *argp) |
| 324 | { |
| 325 | struct mnt_idmap *idmap = file_mnt_idmap(file); |
| 326 | struct dentry *dentry = file->f_path.dentry; |
| 327 | struct file_kattr fa; |
| 328 | unsigned int flags; |
| 329 | int err; |
| 330 | |
| 331 | err = get_user(flags, argp); |
| 332 | if (!err) { |
| 333 | err = mnt_want_write_file(file); |
| 334 | if (!err) { |
| 335 | fileattr_fill_flags(&fa, flags); |
| 336 | err = vfs_fileattr_set(idmap, dentry, &fa); |
| 337 | mnt_drop_write_file(file); |
| 338 | if (err == -EOPNOTSUPP) |
| 339 | err = -ENOIOCTLCMD; |
| 340 | } |
| 341 | } |
| 342 | return err; |
| 343 | } |
| 344 | EXPORT_SYMBOL(ioctl_setflags); |
| 345 | |
| 346 | int ioctl_fsgetxattr(struct file *file, void __user *argp) |
| 347 | { |
| 348 | struct file_kattr fa = { .fsx_valid = true }; /* hint only */ |
| 349 | int err; |
| 350 | |
| 351 | err = vfs_fileattr_get(file->f_path.dentry, &fa); |
| 352 | if (err == -EOPNOTSUPP) |
| 353 | err = -ENOIOCTLCMD; |
| 354 | if (!err) |
| 355 | err = copy_fsxattr_to_user(&fa, argp); |
| 356 | |
| 357 | return err; |
| 358 | } |
| 359 | EXPORT_SYMBOL(ioctl_fsgetxattr); |
| 360 | |
| 361 | int ioctl_fssetxattr(struct file *file, void __user *argp) |
| 362 | { |
| 363 | struct mnt_idmap *idmap = file_mnt_idmap(file); |
| 364 | struct dentry *dentry = file->f_path.dentry; |
| 365 | struct file_kattr fa; |
| 366 | int err; |
| 367 | |
| 368 | err = copy_fsxattr_from_user(fa: &fa, ufa: argp); |
| 369 | if (!err) { |
| 370 | err = mnt_want_write_file(file); |
| 371 | if (!err) { |
| 372 | err = vfs_fileattr_set(idmap, dentry, &fa); |
| 373 | mnt_drop_write_file(file); |
| 374 | if (err == -EOPNOTSUPP) |
| 375 | err = -ENOIOCTLCMD; |
| 376 | } |
| 377 | } |
| 378 | return err; |
| 379 | } |
| 380 | EXPORT_SYMBOL(ioctl_fssetxattr); |
| 381 | |
| 382 | SYSCALL_DEFINE5(file_getattr, int, dfd, const char __user *, filename, |
| 383 | struct file_attr __user *, ufattr, size_t, usize, |
| 384 | unsigned int, at_flags) |
| 385 | { |
| 386 | struct path filepath __free(path_put) = {}; |
| 387 | struct filename *name __free(putname) = NULL; |
| 388 | unsigned int lookup_flags = 0; |
| 389 | struct file_attr fattr; |
| 390 | struct file_kattr fa; |
| 391 | int error; |
| 392 | |
| 393 | BUILD_BUG_ON(sizeof(struct file_attr) < FILE_ATTR_SIZE_VER0); |
| 394 | BUILD_BUG_ON(sizeof(struct file_attr) != FILE_ATTR_SIZE_LATEST); |
| 395 | |
| 396 | if ((at_flags & ~(AT_SYMLINK_NOFOLLOW | AT_EMPTY_PATH)) != 0) |
| 397 | return -EINVAL; |
| 398 | |
| 399 | if (!(at_flags & AT_SYMLINK_NOFOLLOW)) |
| 400 | lookup_flags |= LOOKUP_FOLLOW; |
| 401 | |
| 402 | if (usize > PAGE_SIZE) |
| 403 | return -E2BIG; |
| 404 | |
| 405 | if (usize < FILE_ATTR_SIZE_VER0) |
| 406 | return -EINVAL; |
| 407 | |
| 408 | name = getname_maybe_null(name: filename, flags: at_flags); |
| 409 | if (IS_ERR(ptr: name)) |
| 410 | return PTR_ERR(ptr: name); |
| 411 | |
| 412 | if (!name && dfd >= 0) { |
| 413 | CLASS(fd, f)(fd: dfd); |
| 414 | if (fd_empty(f)) |
| 415 | return -EBADF; |
| 416 | |
| 417 | filepath = fd_file(f)->f_path; |
| 418 | path_get(&filepath); |
| 419 | } else { |
| 420 | error = filename_lookup(dfd, name, flags: lookup_flags, path: &filepath, |
| 421 | NULL); |
| 422 | if (error) |
| 423 | return error; |
| 424 | } |
| 425 | |
| 426 | error = vfs_fileattr_get(filepath.dentry, &fa); |
| 427 | if (error) |
| 428 | return error; |
| 429 | |
| 430 | fileattr_to_file_attr(fa: &fa, fattr: &fattr); |
| 431 | error = copy_struct_to_user(dst: ufattr, usize, src: &fattr, |
| 432 | ksize: sizeof(struct file_attr), NULL); |
| 433 | |
| 434 | return error; |
| 435 | } |
| 436 | |
| 437 | SYSCALL_DEFINE5(file_setattr, int, dfd, const char __user *, filename, |
| 438 | struct file_attr __user *, ufattr, size_t, usize, |
| 439 | unsigned int, at_flags) |
| 440 | { |
| 441 | struct path filepath __free(path_put) = {}; |
| 442 | struct filename *name __free(putname) = NULL; |
| 443 | unsigned int lookup_flags = 0; |
| 444 | struct file_attr fattr; |
| 445 | struct file_kattr fa; |
| 446 | int error; |
| 447 | |
| 448 | BUILD_BUG_ON(sizeof(struct file_attr) < FILE_ATTR_SIZE_VER0); |
| 449 | BUILD_BUG_ON(sizeof(struct file_attr) != FILE_ATTR_SIZE_LATEST); |
| 450 | |
| 451 | if ((at_flags & ~(AT_SYMLINK_NOFOLLOW | AT_EMPTY_PATH)) != 0) |
| 452 | return -EINVAL; |
| 453 | |
| 454 | if (!(at_flags & AT_SYMLINK_NOFOLLOW)) |
| 455 | lookup_flags |= LOOKUP_FOLLOW; |
| 456 | |
| 457 | if (usize > PAGE_SIZE) |
| 458 | return -E2BIG; |
| 459 | |
| 460 | if (usize < FILE_ATTR_SIZE_VER0) |
| 461 | return -EINVAL; |
| 462 | |
| 463 | error = copy_struct_from_user(dst: &fattr, ksize: sizeof(struct file_attr), src: ufattr, |
| 464 | usize); |
| 465 | if (error) |
| 466 | return error; |
| 467 | |
| 468 | error = file_attr_to_fileattr(fattr: &fattr, fa: &fa); |
| 469 | if (error) |
| 470 | return error; |
| 471 | |
| 472 | name = getname_maybe_null(name: filename, flags: at_flags); |
| 473 | if (IS_ERR(ptr: name)) |
| 474 | return PTR_ERR(ptr: name); |
| 475 | |
| 476 | if (!name && dfd >= 0) { |
| 477 | CLASS(fd, f)(fd: dfd); |
| 478 | if (fd_empty(f)) |
| 479 | return -EBADF; |
| 480 | |
| 481 | filepath = fd_file(f)->f_path; |
| 482 | path_get(&filepath); |
| 483 | } else { |
| 484 | error = filename_lookup(dfd, name, flags: lookup_flags, path: &filepath, |
| 485 | NULL); |
| 486 | if (error) |
| 487 | return error; |
| 488 | } |
| 489 | |
| 490 | error = mnt_want_write(mnt: filepath.mnt); |
| 491 | if (!error) { |
| 492 | error = vfs_fileattr_set(mnt_idmap(mnt: filepath.mnt), |
| 493 | filepath.dentry, &fa); |
| 494 | mnt_drop_write(mnt: filepath.mnt); |
| 495 | } |
| 496 | |
| 497 | return error; |
| 498 | } |
| 499 | |