inode.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  1. /*
  2. *
  3. * Copyright (C) 2011 Novell Inc.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 as published by
  7. * the Free Software Foundation.
  8. */
  9. #include <linux/fs.h>
  10. #include <linux/slab.h>
  11. #include <linux/cred.h>
  12. #include <linux/xattr.h>
  13. #include <linux/posix_acl.h>
  14. #include <linux/ratelimit.h>
  15. #include "overlayfs.h"
  16. int ovl_setattr(struct dentry *dentry, struct iattr *attr)
  17. {
  18. int err;
  19. struct dentry *upperdentry;
  20. const struct cred *old_cred;
  21. /*
  22. * Check for permissions before trying to copy-up. This is redundant
  23. * since it will be rechecked later by ->setattr() on upper dentry. But
  24. * without this, copy-up can be triggered by just about anybody.
  25. *
  26. * We don't initialize inode->size, which just means that
  27. * inode_newsize_ok() will always check against MAX_LFS_FILESIZE and not
  28. * check for a swapfile (which this won't be anyway).
  29. */
  30. err = setattr_prepare(dentry, attr);
  31. if (err)
  32. return err;
  33. err = ovl_want_write(dentry);
  34. if (err)
  35. goto out;
  36. err = ovl_copy_up(dentry);
  37. if (!err) {
  38. upperdentry = ovl_dentry_upper(dentry);
  39. if (attr->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
  40. attr->ia_valid &= ~ATTR_MODE;
  41. inode_lock(upperdentry->d_inode);
  42. old_cred = ovl_override_creds(dentry->d_sb);
  43. err = notify_change(upperdentry, attr, NULL);
  44. revert_creds(old_cred);
  45. if (!err)
  46. ovl_copyattr(upperdentry->d_inode, dentry->d_inode);
  47. inode_unlock(upperdentry->d_inode);
  48. }
  49. ovl_drop_write(dentry);
  50. out:
  51. return err;
  52. }
  53. int ovl_getattr(const struct path *path, struct kstat *stat,
  54. u32 request_mask, unsigned int flags)
  55. {
  56. struct dentry *dentry = path->dentry;
  57. enum ovl_path_type type;
  58. struct path realpath;
  59. const struct cred *old_cred;
  60. bool is_dir = S_ISDIR(dentry->d_inode->i_mode);
  61. int err;
  62. type = ovl_path_real(dentry, &realpath);
  63. old_cred = ovl_override_creds(dentry->d_sb);
  64. err = vfs_getattr(&realpath, stat, request_mask, flags);
  65. if (err)
  66. goto out;
  67. /*
  68. * When all layers are on the same fs, all real inode number are
  69. * unique, so we use the overlay st_dev, which is friendly to du -x.
  70. *
  71. * We also use st_ino of the copy up origin, if we know it.
  72. * This guaranties constant st_dev/st_ino across copy up.
  73. *
  74. * If filesystem supports NFS export ops, this also guaranties
  75. * persistent st_ino across mount cycle.
  76. */
  77. if (ovl_same_sb(dentry->d_sb)) {
  78. if (OVL_TYPE_ORIGIN(type)) {
  79. struct kstat lowerstat;
  80. u32 lowermask = STATX_INO | (!is_dir ? STATX_NLINK : 0);
  81. ovl_path_lower(dentry, &realpath);
  82. err = vfs_getattr(&realpath, &lowerstat,
  83. lowermask, flags);
  84. if (err)
  85. goto out;
  86. WARN_ON_ONCE(stat->dev != lowerstat.dev);
  87. /*
  88. * Lower hardlinks may be broken on copy up to different
  89. * upper files, so we cannot use the lower origin st_ino
  90. * for those different files, even for the same fs case.
  91. * With inodes index enabled, it is safe to use st_ino
  92. * of an indexed hardlinked origin. The index validates
  93. * that the upper hardlink is not broken.
  94. */
  95. if (is_dir || lowerstat.nlink == 1 ||
  96. ovl_test_flag(OVL_INDEX, d_inode(dentry)))
  97. stat->ino = lowerstat.ino;
  98. }
  99. stat->dev = dentry->d_sb->s_dev;
  100. } else if (is_dir) {
  101. /*
  102. * If not all layers are on the same fs the pair {real st_ino;
  103. * overlay st_dev} is not unique, so use the non persistent
  104. * overlay st_ino.
  105. *
  106. * Always use the overlay st_dev for directories, so 'find
  107. * -xdev' will scan the entire overlay mount and won't cross the
  108. * overlay mount boundaries.
  109. */
  110. stat->dev = dentry->d_sb->s_dev;
  111. stat->ino = dentry->d_inode->i_ino;
  112. }
  113. /*
  114. * It's probably not worth it to count subdirs to get the
  115. * correct link count. nlink=1 seems to pacify 'find' and
  116. * other utilities.
  117. */
  118. if (is_dir && OVL_TYPE_MERGE(type))
  119. stat->nlink = 1;
  120. /*
  121. * Return the overlay inode nlinks for indexed upper inodes.
  122. * Overlay inode nlink counts the union of the upper hardlinks
  123. * and non-covered lower hardlinks. It does not include the upper
  124. * index hardlink.
  125. */
  126. if (!is_dir && ovl_test_flag(OVL_INDEX, d_inode(dentry)))
  127. stat->nlink = dentry->d_inode->i_nlink;
  128. out:
  129. revert_creds(old_cred);
  130. return err;
  131. }
  132. int ovl_permission(struct inode *inode, int mask)
  133. {
  134. struct inode *upperinode = ovl_inode_upper(inode);
  135. struct inode *realinode = upperinode ?: ovl_inode_lower(inode);
  136. const struct cred *old_cred;
  137. int err;
  138. /* Careful in RCU walk mode */
  139. if (!realinode) {
  140. WARN_ON(!(mask & MAY_NOT_BLOCK));
  141. return -ECHILD;
  142. }
  143. /*
  144. * Check overlay inode with the creds of task and underlying inode
  145. * with creds of mounter
  146. */
  147. err = generic_permission(inode, mask);
  148. if (err)
  149. return err;
  150. old_cred = ovl_override_creds(inode->i_sb);
  151. if (!upperinode &&
  152. !special_file(realinode->i_mode) && mask & MAY_WRITE) {
  153. mask &= ~(MAY_WRITE | MAY_APPEND);
  154. /* Make sure mounter can read file for copy up later */
  155. mask |= MAY_READ;
  156. }
  157. err = inode_permission(realinode, mask);
  158. revert_creds(old_cred);
  159. return err;
  160. }
  161. static const char *ovl_get_link(struct dentry *dentry,
  162. struct inode *inode,
  163. struct delayed_call *done)
  164. {
  165. const struct cred *old_cred;
  166. const char *p;
  167. if (!dentry)
  168. return ERR_PTR(-ECHILD);
  169. old_cred = ovl_override_creds(dentry->d_sb);
  170. p = vfs_get_link(ovl_dentry_real(dentry), done);
  171. revert_creds(old_cred);
  172. return p;
  173. }
  174. bool ovl_is_private_xattr(const char *name)
  175. {
  176. return strncmp(name, OVL_XATTR_PREFIX,
  177. sizeof(OVL_XATTR_PREFIX) - 1) == 0;
  178. }
  179. int ovl_xattr_set(struct dentry *dentry, struct inode *inode, const char *name,
  180. const void *value, size_t size, int flags)
  181. {
  182. int err;
  183. struct dentry *upperdentry = ovl_i_dentry_upper(inode);
  184. struct dentry *realdentry = upperdentry ?: ovl_dentry_lower(dentry);
  185. const struct cred *old_cred;
  186. err = ovl_want_write(dentry);
  187. if (err)
  188. goto out;
  189. if (!value && !upperdentry) {
  190. err = vfs_getxattr(realdentry, name, NULL, 0);
  191. if (err < 0)
  192. goto out_drop_write;
  193. }
  194. if (!upperdentry) {
  195. err = ovl_copy_up(dentry);
  196. if (err)
  197. goto out_drop_write;
  198. realdentry = ovl_dentry_upper(dentry);
  199. }
  200. old_cred = ovl_override_creds(dentry->d_sb);
  201. if (value)
  202. err = vfs_setxattr(realdentry, name, value, size, flags);
  203. else {
  204. WARN_ON(flags != XATTR_REPLACE);
  205. err = vfs_removexattr(realdentry, name);
  206. }
  207. revert_creds(old_cred);
  208. out_drop_write:
  209. ovl_drop_write(dentry);
  210. out:
  211. return err;
  212. }
  213. int ovl_xattr_get(struct dentry *dentry, struct inode *inode, const char *name,
  214. void *value, size_t size)
  215. {
  216. ssize_t res;
  217. const struct cred *old_cred;
  218. struct dentry *realdentry =
  219. ovl_i_dentry_upper(inode) ?: ovl_dentry_lower(dentry);
  220. old_cred = ovl_override_creds(dentry->d_sb);
  221. res = vfs_getxattr(realdentry, name, value, size);
  222. revert_creds(old_cred);
  223. return res;
  224. }
  225. static bool ovl_can_list(const char *s)
  226. {
  227. /* List all non-trusted xatts */
  228. if (strncmp(s, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) != 0)
  229. return true;
  230. /* Never list trusted.overlay, list other trusted for superuser only */
  231. return !ovl_is_private_xattr(s) && capable(CAP_SYS_ADMIN);
  232. }
  233. ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size)
  234. {
  235. struct dentry *realdentry = ovl_dentry_real(dentry);
  236. ssize_t res;
  237. size_t len;
  238. char *s;
  239. const struct cred *old_cred;
  240. old_cred = ovl_override_creds(dentry->d_sb);
  241. res = vfs_listxattr(realdentry, list, size);
  242. revert_creds(old_cred);
  243. if (res <= 0 || size == 0)
  244. return res;
  245. /* filter out private xattrs */
  246. for (s = list, len = res; len;) {
  247. size_t slen = strnlen(s, len) + 1;
  248. /* underlying fs providing us with an broken xattr list? */
  249. if (WARN_ON(slen > len))
  250. return -EIO;
  251. len -= slen;
  252. if (!ovl_can_list(s)) {
  253. res -= slen;
  254. memmove(s, s + slen, len);
  255. } else {
  256. s += slen;
  257. }
  258. }
  259. return res;
  260. }
  261. struct posix_acl *ovl_get_acl(struct inode *inode, int type)
  262. {
  263. struct inode *realinode = ovl_inode_real(inode);
  264. const struct cred *old_cred;
  265. struct posix_acl *acl;
  266. if (!IS_ENABLED(CONFIG_FS_POSIX_ACL) || !IS_POSIXACL(realinode))
  267. return NULL;
  268. old_cred = ovl_override_creds(inode->i_sb);
  269. acl = get_acl(realinode, type);
  270. revert_creds(old_cred);
  271. return acl;
  272. }
  273. static bool ovl_open_need_copy_up(struct dentry *dentry, int flags)
  274. {
  275. if (ovl_dentry_upper(dentry) &&
  276. ovl_dentry_has_upper_alias(dentry))
  277. return false;
  278. if (special_file(d_inode(dentry)->i_mode))
  279. return false;
  280. if (!(OPEN_FMODE(flags) & FMODE_WRITE) && !(flags & O_TRUNC))
  281. return false;
  282. return true;
  283. }
  284. int ovl_open_maybe_copy_up(struct dentry *dentry, unsigned int file_flags)
  285. {
  286. int err = 0;
  287. if (ovl_open_need_copy_up(dentry, file_flags)) {
  288. err = ovl_want_write(dentry);
  289. if (!err) {
  290. err = ovl_copy_up_flags(dentry, file_flags);
  291. ovl_drop_write(dentry);
  292. }
  293. }
  294. return err;
  295. }
  296. int ovl_update_time(struct inode *inode, struct timespec *ts, int flags)
  297. {
  298. struct dentry *alias;
  299. struct path upperpath;
  300. if (!(flags & S_ATIME))
  301. return 0;
  302. alias = d_find_any_alias(inode);
  303. if (!alias)
  304. return 0;
  305. ovl_path_upper(alias, &upperpath);
  306. if (upperpath.dentry) {
  307. touch_atime(&upperpath);
  308. inode->i_atime = d_inode(upperpath.dentry)->i_atime;
  309. }
  310. dput(alias);
  311. return 0;
  312. }
  313. static const struct inode_operations ovl_file_inode_operations = {
  314. .setattr = ovl_setattr,
  315. .permission = ovl_permission,
  316. .getattr = ovl_getattr,
  317. .listxattr = ovl_listxattr,
  318. .get_acl = ovl_get_acl,
  319. .update_time = ovl_update_time,
  320. };
  321. static const struct inode_operations ovl_symlink_inode_operations = {
  322. .setattr = ovl_setattr,
  323. .get_link = ovl_get_link,
  324. .getattr = ovl_getattr,
  325. .listxattr = ovl_listxattr,
  326. .update_time = ovl_update_time,
  327. };
  328. /*
  329. * It is possible to stack overlayfs instance on top of another
  330. * overlayfs instance as lower layer. We need to annonate the
  331. * stackable i_mutex locks according to stack level of the super
  332. * block instance. An overlayfs instance can never be in stack
  333. * depth 0 (there is always a real fs below it). An overlayfs
  334. * inode lock will use the lockdep annotaion ovl_i_mutex_key[depth].
  335. *
  336. * For example, here is a snip from /proc/lockdep_chains after
  337. * dir_iterate of nested overlayfs:
  338. *
  339. * [...] &ovl_i_mutex_dir_key[depth] (stack_depth=2)
  340. * [...] &ovl_i_mutex_dir_key[depth]#2 (stack_depth=1)
  341. * [...] &type->i_mutex_dir_key (stack_depth=0)
  342. */
  343. #define OVL_MAX_NESTING FILESYSTEM_MAX_STACK_DEPTH
  344. static inline void ovl_lockdep_annotate_inode_mutex_key(struct inode *inode)
  345. {
  346. #ifdef CONFIG_LOCKDEP
  347. static struct lock_class_key ovl_i_mutex_key[OVL_MAX_NESTING];
  348. static struct lock_class_key ovl_i_mutex_dir_key[OVL_MAX_NESTING];
  349. int depth = inode->i_sb->s_stack_depth - 1;
  350. if (WARN_ON_ONCE(depth < 0 || depth >= OVL_MAX_NESTING))
  351. depth = 0;
  352. if (S_ISDIR(inode->i_mode))
  353. lockdep_set_class(&inode->i_rwsem, &ovl_i_mutex_dir_key[depth]);
  354. else
  355. lockdep_set_class(&inode->i_rwsem, &ovl_i_mutex_key[depth]);
  356. #endif
  357. }
  358. static void ovl_fill_inode(struct inode *inode, umode_t mode, dev_t rdev)
  359. {
  360. inode->i_ino = get_next_ino();
  361. inode->i_mode = mode;
  362. inode->i_flags |= S_NOCMTIME;
  363. #ifdef CONFIG_FS_POSIX_ACL
  364. inode->i_acl = inode->i_default_acl = ACL_DONT_CACHE;
  365. #endif
  366. ovl_lockdep_annotate_inode_mutex_key(inode);
  367. switch (mode & S_IFMT) {
  368. case S_IFREG:
  369. inode->i_op = &ovl_file_inode_operations;
  370. break;
  371. case S_IFDIR:
  372. inode->i_op = &ovl_dir_inode_operations;
  373. inode->i_fop = &ovl_dir_operations;
  374. break;
  375. case S_IFLNK:
  376. inode->i_op = &ovl_symlink_inode_operations;
  377. break;
  378. default:
  379. inode->i_op = &ovl_file_inode_operations;
  380. init_special_inode(inode, mode, rdev);
  381. break;
  382. }
  383. }
  384. /*
  385. * With inodes index enabled, an overlay inode nlink counts the union of upper
  386. * hardlinks and non-covered lower hardlinks. During the lifetime of a non-pure
  387. * upper inode, the following nlink modifying operations can happen:
  388. *
  389. * 1. Lower hardlink copy up
  390. * 2. Upper hardlink created, unlinked or renamed over
  391. * 3. Lower hardlink whiteout or renamed over
  392. *
  393. * For the first, copy up case, the union nlink does not change, whether the
  394. * operation succeeds or fails, but the upper inode nlink may change.
  395. * Therefore, before copy up, we store the union nlink value relative to the
  396. * lower inode nlink in the index inode xattr trusted.overlay.nlink.
  397. *
  398. * For the second, upper hardlink case, the union nlink should be incremented
  399. * or decremented IFF the operation succeeds, aligned with nlink change of the
  400. * upper inode. Therefore, before link/unlink/rename, we store the union nlink
  401. * value relative to the upper inode nlink in the index inode.
  402. *
  403. * For the last, lower cover up case, we simplify things by preceding the
  404. * whiteout or cover up with copy up. This makes sure that there is an index
  405. * upper inode where the nlink xattr can be stored before the copied up upper
  406. * entry is unlink.
  407. */
  408. #define OVL_NLINK_ADD_UPPER (1 << 0)
  409. /*
  410. * On-disk format for indexed nlink:
  411. *
  412. * nlink relative to the upper inode - "U[+-]NUM"
  413. * nlink relative to the lower inode - "L[+-]NUM"
  414. */
  415. static int ovl_set_nlink_common(struct dentry *dentry,
  416. struct dentry *realdentry, const char *format)
  417. {
  418. struct inode *inode = d_inode(dentry);
  419. struct inode *realinode = d_inode(realdentry);
  420. char buf[13];
  421. int len;
  422. len = snprintf(buf, sizeof(buf), format,
  423. (int) (inode->i_nlink - realinode->i_nlink));
  424. if (WARN_ON(len >= sizeof(buf)))
  425. return -EIO;
  426. return ovl_do_setxattr(ovl_dentry_upper(dentry),
  427. OVL_XATTR_NLINK, buf, len, 0);
  428. }
  429. int ovl_set_nlink_upper(struct dentry *dentry)
  430. {
  431. return ovl_set_nlink_common(dentry, ovl_dentry_upper(dentry), "U%+i");
  432. }
  433. int ovl_set_nlink_lower(struct dentry *dentry)
  434. {
  435. return ovl_set_nlink_common(dentry, ovl_dentry_lower(dentry), "L%+i");
  436. }
  437. unsigned int ovl_get_nlink(struct dentry *lowerdentry,
  438. struct dentry *upperdentry,
  439. unsigned int fallback)
  440. {
  441. int nlink_diff;
  442. int nlink;
  443. char buf[13];
  444. int err;
  445. if (!lowerdentry || !upperdentry || d_inode(lowerdentry)->i_nlink == 1)
  446. return fallback;
  447. err = vfs_getxattr(upperdentry, OVL_XATTR_NLINK, &buf, sizeof(buf) - 1);
  448. if (err < 0)
  449. goto fail;
  450. buf[err] = '\0';
  451. if ((buf[0] != 'L' && buf[0] != 'U') ||
  452. (buf[1] != '+' && buf[1] != '-'))
  453. goto fail;
  454. err = kstrtoint(buf + 1, 10, &nlink_diff);
  455. if (err < 0)
  456. goto fail;
  457. nlink = d_inode(buf[0] == 'L' ? lowerdentry : upperdentry)->i_nlink;
  458. nlink += nlink_diff;
  459. if (nlink <= 0)
  460. goto fail;
  461. return nlink;
  462. fail:
  463. pr_warn_ratelimited("overlayfs: failed to get index nlink (%pd2, err=%i)\n",
  464. upperdentry, err);
  465. return fallback;
  466. }
  467. struct inode *ovl_new_inode(struct super_block *sb, umode_t mode, dev_t rdev)
  468. {
  469. struct inode *inode;
  470. inode = new_inode(sb);
  471. if (inode)
  472. ovl_fill_inode(inode, mode, rdev);
  473. return inode;
  474. }
  475. static int ovl_inode_test(struct inode *inode, void *data)
  476. {
  477. return inode->i_private == data;
  478. }
  479. static int ovl_inode_set(struct inode *inode, void *data)
  480. {
  481. inode->i_private = data;
  482. return 0;
  483. }
  484. static bool ovl_verify_inode(struct inode *inode, struct dentry *lowerdentry,
  485. struct dentry *upperdentry)
  486. {
  487. /*
  488. * Allow non-NULL lower inode in ovl_inode even if lowerdentry is NULL.
  489. * This happens when finding a copied up overlay inode for a renamed
  490. * or hardlinked overlay dentry and lower dentry cannot be followed
  491. * by origin because lower fs does not support file handles.
  492. */
  493. if (lowerdentry && ovl_inode_lower(inode) != d_inode(lowerdentry))
  494. return false;
  495. /*
  496. * Allow non-NULL __upperdentry in inode even if upperdentry is NULL.
  497. * This happens when finding a lower alias for a copied up hard link.
  498. */
  499. if (upperdentry && ovl_inode_upper(inode) != d_inode(upperdentry))
  500. return false;
  501. return true;
  502. }
  503. struct inode *ovl_get_inode(struct dentry *dentry, struct dentry *upperdentry)
  504. {
  505. struct dentry *lowerdentry = ovl_dentry_lower(dentry);
  506. struct inode *realinode = upperdentry ? d_inode(upperdentry) : NULL;
  507. struct inode *inode;
  508. if (!realinode)
  509. realinode = d_inode(lowerdentry);
  510. if (!S_ISDIR(realinode->i_mode) &&
  511. (upperdentry || (lowerdentry && ovl_indexdir(dentry->d_sb)))) {
  512. struct inode *key = d_inode(lowerdentry ?: upperdentry);
  513. unsigned int nlink;
  514. inode = iget5_locked(dentry->d_sb, (unsigned long) key,
  515. ovl_inode_test, ovl_inode_set, key);
  516. if (!inode)
  517. goto out_nomem;
  518. if (!(inode->i_state & I_NEW)) {
  519. /*
  520. * Verify that the underlying files stored in the inode
  521. * match those in the dentry.
  522. */
  523. if (!ovl_verify_inode(inode, lowerdentry, upperdentry)) {
  524. iput(inode);
  525. inode = ERR_PTR(-ESTALE);
  526. goto out;
  527. }
  528. dput(upperdentry);
  529. goto out;
  530. }
  531. nlink = ovl_get_nlink(lowerdentry, upperdentry,
  532. realinode->i_nlink);
  533. set_nlink(inode, nlink);
  534. } else {
  535. inode = new_inode(dentry->d_sb);
  536. if (!inode)
  537. goto out_nomem;
  538. }
  539. ovl_fill_inode(inode, realinode->i_mode, realinode->i_rdev);
  540. ovl_inode_init(inode, upperdentry, lowerdentry);
  541. if (upperdentry && ovl_is_impuredir(upperdentry))
  542. ovl_set_flag(OVL_IMPURE, inode);
  543. if (inode->i_state & I_NEW)
  544. unlock_new_inode(inode);
  545. out:
  546. return inode;
  547. out_nomem:
  548. inode = ERR_PTR(-ENOMEM);
  549. goto out;
  550. }