file_attr.c 12 KB

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