inode.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  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, const char *name, const void *value,
  180. size_t size, int flags)
  181. {
  182. int err;
  183. struct path realpath;
  184. enum ovl_path_type type = ovl_path_real(dentry, &realpath);
  185. const struct cred *old_cred;
  186. err = ovl_want_write(dentry);
  187. if (err)
  188. goto out;
  189. if (!value && !OVL_TYPE_UPPER(type)) {
  190. err = vfs_getxattr(realpath.dentry, name, NULL, 0);
  191. if (err < 0)
  192. goto out_drop_write;
  193. }
  194. err = ovl_copy_up(dentry);
  195. if (err)
  196. goto out_drop_write;
  197. if (!OVL_TYPE_UPPER(type))
  198. ovl_path_upper(dentry, &realpath);
  199. old_cred = ovl_override_creds(dentry->d_sb);
  200. if (value)
  201. err = vfs_setxattr(realpath.dentry, name, value, size, flags);
  202. else {
  203. WARN_ON(flags != XATTR_REPLACE);
  204. err = vfs_removexattr(realpath.dentry, name);
  205. }
  206. revert_creds(old_cred);
  207. out_drop_write:
  208. ovl_drop_write(dentry);
  209. out:
  210. return err;
  211. }
  212. int ovl_xattr_get(struct dentry *dentry, const char *name,
  213. void *value, size_t size)
  214. {
  215. struct dentry *realdentry = ovl_dentry_real(dentry);
  216. ssize_t res;
  217. const struct cred *old_cred;
  218. old_cred = ovl_override_creds(dentry->d_sb);
  219. res = vfs_getxattr(realdentry, name, value, size);
  220. revert_creds(old_cred);
  221. return res;
  222. }
  223. static bool ovl_can_list(const char *s)
  224. {
  225. /* List all non-trusted xatts */
  226. if (strncmp(s, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) != 0)
  227. return true;
  228. /* Never list trusted.overlay, list other trusted for superuser only */
  229. return !ovl_is_private_xattr(s) && capable(CAP_SYS_ADMIN);
  230. }
  231. ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size)
  232. {
  233. struct dentry *realdentry = ovl_dentry_real(dentry);
  234. ssize_t res;
  235. size_t len;
  236. char *s;
  237. const struct cred *old_cred;
  238. old_cred = ovl_override_creds(dentry->d_sb);
  239. res = vfs_listxattr(realdentry, list, size);
  240. revert_creds(old_cred);
  241. if (res <= 0 || size == 0)
  242. return res;
  243. /* filter out private xattrs */
  244. for (s = list, len = res; len;) {
  245. size_t slen = strnlen(s, len) + 1;
  246. /* underlying fs providing us with an broken xattr list? */
  247. if (WARN_ON(slen > len))
  248. return -EIO;
  249. len -= slen;
  250. if (!ovl_can_list(s)) {
  251. res -= slen;
  252. memmove(s, s + slen, len);
  253. } else {
  254. s += slen;
  255. }
  256. }
  257. return res;
  258. }
  259. struct posix_acl *ovl_get_acl(struct inode *inode, int type)
  260. {
  261. struct inode *realinode = ovl_inode_real(inode);
  262. const struct cred *old_cred;
  263. struct posix_acl *acl;
  264. if (!IS_ENABLED(CONFIG_FS_POSIX_ACL) || !IS_POSIXACL(realinode))
  265. return NULL;
  266. old_cred = ovl_override_creds(inode->i_sb);
  267. acl = get_acl(realinode, type);
  268. revert_creds(old_cred);
  269. return acl;
  270. }
  271. static bool ovl_open_need_copy_up(struct dentry *dentry, int flags)
  272. {
  273. if (ovl_dentry_upper(dentry) &&
  274. ovl_dentry_has_upper_alias(dentry))
  275. return false;
  276. if (special_file(d_inode(dentry)->i_mode))
  277. return false;
  278. if (!(OPEN_FMODE(flags) & FMODE_WRITE) && !(flags & O_TRUNC))
  279. return false;
  280. return true;
  281. }
  282. int ovl_open_maybe_copy_up(struct dentry *dentry, unsigned int file_flags)
  283. {
  284. int err = 0;
  285. if (ovl_open_need_copy_up(dentry, file_flags)) {
  286. err = ovl_want_write(dentry);
  287. if (!err) {
  288. err = ovl_copy_up_flags(dentry, file_flags);
  289. ovl_drop_write(dentry);
  290. }
  291. }
  292. return err;
  293. }
  294. int ovl_update_time(struct inode *inode, struct timespec *ts, int flags)
  295. {
  296. struct dentry *alias;
  297. struct path upperpath;
  298. if (!(flags & S_ATIME))
  299. return 0;
  300. alias = d_find_any_alias(inode);
  301. if (!alias)
  302. return 0;
  303. ovl_path_upper(alias, &upperpath);
  304. if (upperpath.dentry) {
  305. touch_atime(&upperpath);
  306. inode->i_atime = d_inode(upperpath.dentry)->i_atime;
  307. }
  308. dput(alias);
  309. return 0;
  310. }
  311. static const struct inode_operations ovl_file_inode_operations = {
  312. .setattr = ovl_setattr,
  313. .permission = ovl_permission,
  314. .getattr = ovl_getattr,
  315. .listxattr = ovl_listxattr,
  316. .get_acl = ovl_get_acl,
  317. .update_time = ovl_update_time,
  318. };
  319. static const struct inode_operations ovl_symlink_inode_operations = {
  320. .setattr = ovl_setattr,
  321. .get_link = ovl_get_link,
  322. .getattr = ovl_getattr,
  323. .listxattr = ovl_listxattr,
  324. .update_time = ovl_update_time,
  325. };
  326. /*
  327. * It is possible to stack overlayfs instance on top of another
  328. * overlayfs instance as lower layer. We need to annonate the
  329. * stackable i_mutex locks according to stack level of the super
  330. * block instance. An overlayfs instance can never be in stack
  331. * depth 0 (there is always a real fs below it). An overlayfs
  332. * inode lock will use the lockdep annotaion ovl_i_mutex_key[depth].
  333. *
  334. * For example, here is a snip from /proc/lockdep_chains after
  335. * dir_iterate of nested overlayfs:
  336. *
  337. * [...] &ovl_i_mutex_dir_key[depth] (stack_depth=2)
  338. * [...] &ovl_i_mutex_dir_key[depth]#2 (stack_depth=1)
  339. * [...] &type->i_mutex_dir_key (stack_depth=0)
  340. */
  341. #define OVL_MAX_NESTING FILESYSTEM_MAX_STACK_DEPTH
  342. static inline void ovl_lockdep_annotate_inode_mutex_key(struct inode *inode)
  343. {
  344. #ifdef CONFIG_LOCKDEP
  345. static struct lock_class_key ovl_i_mutex_key[OVL_MAX_NESTING];
  346. static struct lock_class_key ovl_i_mutex_dir_key[OVL_MAX_NESTING];
  347. int depth = inode->i_sb->s_stack_depth - 1;
  348. if (WARN_ON_ONCE(depth < 0 || depth >= OVL_MAX_NESTING))
  349. depth = 0;
  350. if (S_ISDIR(inode->i_mode))
  351. lockdep_set_class(&inode->i_rwsem, &ovl_i_mutex_dir_key[depth]);
  352. else
  353. lockdep_set_class(&inode->i_rwsem, &ovl_i_mutex_key[depth]);
  354. #endif
  355. }
  356. static void ovl_fill_inode(struct inode *inode, umode_t mode, dev_t rdev)
  357. {
  358. inode->i_ino = get_next_ino();
  359. inode->i_mode = mode;
  360. inode->i_flags |= S_NOCMTIME;
  361. #ifdef CONFIG_FS_POSIX_ACL
  362. inode->i_acl = inode->i_default_acl = ACL_DONT_CACHE;
  363. #endif
  364. ovl_lockdep_annotate_inode_mutex_key(inode);
  365. switch (mode & S_IFMT) {
  366. case S_IFREG:
  367. inode->i_op = &ovl_file_inode_operations;
  368. break;
  369. case S_IFDIR:
  370. inode->i_op = &ovl_dir_inode_operations;
  371. inode->i_fop = &ovl_dir_operations;
  372. break;
  373. case S_IFLNK:
  374. inode->i_op = &ovl_symlink_inode_operations;
  375. break;
  376. default:
  377. inode->i_op = &ovl_file_inode_operations;
  378. init_special_inode(inode, mode, rdev);
  379. break;
  380. }
  381. }
  382. /*
  383. * With inodes index enabled, an overlay inode nlink counts the union of upper
  384. * hardlinks and non-covered lower hardlinks. During the lifetime of a non-pure
  385. * upper inode, the following nlink modifying operations can happen:
  386. *
  387. * 1. Lower hardlink copy up
  388. * 2. Upper hardlink created, unlinked or renamed over
  389. * 3. Lower hardlink whiteout or renamed over
  390. *
  391. * For the first, copy up case, the union nlink does not change, whether the
  392. * operation succeeds or fails, but the upper inode nlink may change.
  393. * Therefore, before copy up, we store the union nlink value relative to the
  394. * lower inode nlink in the index inode xattr trusted.overlay.nlink.
  395. *
  396. * For the second, upper hardlink case, the union nlink should be incremented
  397. * or decremented IFF the operation succeeds, aligned with nlink change of the
  398. * upper inode. Therefore, before link/unlink/rename, we store the union nlink
  399. * value relative to the upper inode nlink in the index inode.
  400. *
  401. * For the last, lower cover up case, we simplify things by preceding the
  402. * whiteout or cover up with copy up. This makes sure that there is an index
  403. * upper inode where the nlink xattr can be stored before the copied up upper
  404. * entry is unlink.
  405. */
  406. #define OVL_NLINK_ADD_UPPER (1 << 0)
  407. /*
  408. * On-disk format for indexed nlink:
  409. *
  410. * nlink relative to the upper inode - "U[+-]NUM"
  411. * nlink relative to the lower inode - "L[+-]NUM"
  412. */
  413. static int ovl_set_nlink_common(struct dentry *dentry,
  414. struct dentry *realdentry, const char *format)
  415. {
  416. struct inode *inode = d_inode(dentry);
  417. struct inode *realinode = d_inode(realdentry);
  418. char buf[13];
  419. int len;
  420. len = snprintf(buf, sizeof(buf), format,
  421. (int) (inode->i_nlink - realinode->i_nlink));
  422. return ovl_do_setxattr(ovl_dentry_upper(dentry),
  423. OVL_XATTR_NLINK, buf, len, 0);
  424. }
  425. int ovl_set_nlink_upper(struct dentry *dentry)
  426. {
  427. return ovl_set_nlink_common(dentry, ovl_dentry_upper(dentry), "U%+i");
  428. }
  429. int ovl_set_nlink_lower(struct dentry *dentry)
  430. {
  431. return ovl_set_nlink_common(dentry, ovl_dentry_lower(dentry), "L%+i");
  432. }
  433. unsigned int ovl_get_nlink(struct dentry *lowerdentry,
  434. struct dentry *upperdentry,
  435. unsigned int fallback)
  436. {
  437. int nlink_diff;
  438. int nlink;
  439. char buf[13];
  440. int err;
  441. if (!lowerdentry || !upperdentry || d_inode(lowerdentry)->i_nlink == 1)
  442. return fallback;
  443. err = vfs_getxattr(upperdentry, OVL_XATTR_NLINK, &buf, sizeof(buf) - 1);
  444. if (err < 0)
  445. goto fail;
  446. buf[err] = '\0';
  447. if ((buf[0] != 'L' && buf[0] != 'U') ||
  448. (buf[1] != '+' && buf[1] != '-'))
  449. goto fail;
  450. err = kstrtoint(buf + 1, 10, &nlink_diff);
  451. if (err < 0)
  452. goto fail;
  453. nlink = d_inode(buf[0] == 'L' ? lowerdentry : upperdentry)->i_nlink;
  454. nlink += nlink_diff;
  455. if (nlink <= 0)
  456. goto fail;
  457. return nlink;
  458. fail:
  459. pr_warn_ratelimited("overlayfs: failed to get index nlink (%pd2, err=%i)\n",
  460. upperdentry, err);
  461. return fallback;
  462. }
  463. struct inode *ovl_new_inode(struct super_block *sb, umode_t mode, dev_t rdev)
  464. {
  465. struct inode *inode;
  466. inode = new_inode(sb);
  467. if (inode)
  468. ovl_fill_inode(inode, mode, rdev);
  469. return inode;
  470. }
  471. static int ovl_inode_test(struct inode *inode, void *data)
  472. {
  473. return inode->i_private == data;
  474. }
  475. static int ovl_inode_set(struct inode *inode, void *data)
  476. {
  477. inode->i_private = data;
  478. return 0;
  479. }
  480. static bool ovl_verify_inode(struct inode *inode, struct dentry *lowerdentry,
  481. struct dentry *upperdentry)
  482. {
  483. struct inode *lowerinode = lowerdentry ? d_inode(lowerdentry) : NULL;
  484. /* Lower (origin) inode must match, even if NULL */
  485. if (ovl_inode_lower(inode) != lowerinode)
  486. return false;
  487. /*
  488. * Allow non-NULL __upperdentry in inode even if upperdentry is NULL.
  489. * This happens when finding a lower alias for a copied up hard link.
  490. */
  491. if (upperdentry && ovl_inode_upper(inode) != d_inode(upperdentry))
  492. return false;
  493. return true;
  494. }
  495. struct inode *ovl_get_inode(struct dentry *dentry, struct dentry *upperdentry)
  496. {
  497. struct dentry *lowerdentry = ovl_dentry_lower(dentry);
  498. struct inode *realinode = upperdentry ? d_inode(upperdentry) : NULL;
  499. struct inode *inode;
  500. if (!realinode)
  501. realinode = d_inode(lowerdentry);
  502. if (!S_ISDIR(realinode->i_mode) &&
  503. (upperdentry || (lowerdentry && ovl_indexdir(dentry->d_sb)))) {
  504. struct inode *key = d_inode(lowerdentry ?: upperdentry);
  505. unsigned int nlink;
  506. inode = iget5_locked(dentry->d_sb, (unsigned long) key,
  507. ovl_inode_test, ovl_inode_set, key);
  508. if (!inode)
  509. goto out_nomem;
  510. if (!(inode->i_state & I_NEW)) {
  511. /*
  512. * Verify that the underlying files stored in the inode
  513. * match those in the dentry.
  514. */
  515. if (!ovl_verify_inode(inode, lowerdentry, upperdentry)) {
  516. iput(inode);
  517. inode = ERR_PTR(-ESTALE);
  518. goto out;
  519. }
  520. dput(upperdentry);
  521. goto out;
  522. }
  523. nlink = ovl_get_nlink(lowerdentry, upperdentry,
  524. realinode->i_nlink);
  525. set_nlink(inode, nlink);
  526. } else {
  527. inode = new_inode(dentry->d_sb);
  528. if (!inode)
  529. goto out_nomem;
  530. }
  531. ovl_fill_inode(inode, realinode->i_mode, realinode->i_rdev);
  532. ovl_inode_init(inode, upperdentry, lowerdentry);
  533. if (upperdentry && ovl_is_impuredir(upperdentry))
  534. ovl_set_flag(OVL_IMPURE, inode);
  535. if (inode->i_state & I_NEW)
  536. unlock_new_inode(inode);
  537. out:
  538. return inode;
  539. out_nomem:
  540. inode = ERR_PTR(-ENOMEM);
  541. goto out;
  542. }