d_path.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #include <linux/syscalls.h>
  3. #include <linux/export.h>
  4. #include <linux/uaccess.h>
  5. #include <linux/fs_struct.h>
  6. #include <linux/fs.h>
  7. #include <linux/slab.h>
  8. #include <linux/prefetch.h>
  9. #include "mount.h"
  10. static int prepend(char **buffer, int *buflen, const char *str, int namelen)
  11. {
  12. *buflen -= namelen;
  13. if (*buflen < 0)
  14. return -ENAMETOOLONG;
  15. *buffer -= namelen;
  16. memcpy(*buffer, str, namelen);
  17. return 0;
  18. }
  19. /**
  20. * prepend_name - prepend a pathname in front of current buffer pointer
  21. * @buffer: buffer pointer
  22. * @buflen: allocated length of the buffer
  23. * @name: name string and length qstr structure
  24. *
  25. * With RCU path tracing, it may race with d_move(). Use READ_ONCE() to
  26. * make sure that either the old or the new name pointer and length are
  27. * fetched. However, there may be mismatch between length and pointer.
  28. * The length cannot be trusted, we need to copy it byte-by-byte until
  29. * the length is reached or a null byte is found. It also prepends "/" at
  30. * the beginning of the name. The sequence number check at the caller will
  31. * retry it again when a d_move() does happen. So any garbage in the buffer
  32. * due to mismatched pointer and length will be discarded.
  33. *
  34. * Load acquire is needed to make sure that we see that terminating NUL.
  35. */
  36. static int prepend_name(char **buffer, int *buflen, const struct qstr *name)
  37. {
  38. const char *dname = smp_load_acquire(&name->name); /* ^^^ */
  39. u32 dlen = READ_ONCE(name->len);
  40. char *p;
  41. *buflen -= dlen + 1;
  42. if (*buflen < 0)
  43. return -ENAMETOOLONG;
  44. p = *buffer -= dlen + 1;
  45. *p++ = '/';
  46. while (dlen--) {
  47. char c = *dname++;
  48. if (!c)
  49. break;
  50. *p++ = c;
  51. }
  52. return 0;
  53. }
  54. /**
  55. * prepend_path - Prepend path string to a buffer
  56. * @path: the dentry/vfsmount to report
  57. * @root: root vfsmnt/dentry
  58. * @buffer: pointer to the end of the buffer
  59. * @buflen: pointer to buffer length
  60. *
  61. * The function will first try to write out the pathname without taking any
  62. * lock other than the RCU read lock to make sure that dentries won't go away.
  63. * It only checks the sequence number of the global rename_lock as any change
  64. * in the dentry's d_seq will be preceded by changes in the rename_lock
  65. * sequence number. If the sequence number had been changed, it will restart
  66. * the whole pathname back-tracing sequence again by taking the rename_lock.
  67. * In this case, there is no need to take the RCU read lock as the recursive
  68. * parent pointer references will keep the dentry chain alive as long as no
  69. * rename operation is performed.
  70. */
  71. static int prepend_path(const struct path *path,
  72. const struct path *root,
  73. char **buffer, int *buflen)
  74. {
  75. struct dentry *dentry;
  76. struct vfsmount *vfsmnt;
  77. struct mount *mnt;
  78. int error = 0;
  79. unsigned seq, m_seq = 0;
  80. char *bptr;
  81. int blen;
  82. rcu_read_lock();
  83. restart_mnt:
  84. read_seqbegin_or_lock(&mount_lock, &m_seq);
  85. seq = 0;
  86. rcu_read_lock();
  87. restart:
  88. bptr = *buffer;
  89. blen = *buflen;
  90. error = 0;
  91. dentry = path->dentry;
  92. vfsmnt = path->mnt;
  93. mnt = real_mount(vfsmnt);
  94. read_seqbegin_or_lock(&rename_lock, &seq);
  95. while (dentry != root->dentry || vfsmnt != root->mnt) {
  96. struct dentry * parent;
  97. if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) {
  98. struct mount *parent = READ_ONCE(mnt->mnt_parent);
  99. struct mnt_namespace *mnt_ns;
  100. /* Escaped? */
  101. if (dentry != vfsmnt->mnt_root) {
  102. bptr = *buffer;
  103. blen = *buflen;
  104. error = 3;
  105. break;
  106. }
  107. /* Global root? */
  108. if (mnt != parent) {
  109. dentry = READ_ONCE(mnt->mnt_mountpoint);
  110. mnt = parent;
  111. vfsmnt = &mnt->mnt;
  112. continue;
  113. }
  114. mnt_ns = READ_ONCE(mnt->mnt_ns);
  115. /* open-coded is_mounted() to use local mnt_ns */
  116. if (!IS_ERR_OR_NULL(mnt_ns) && !is_anon_ns(mnt_ns))
  117. error = 1; // absolute root
  118. else
  119. error = 2; // detached or not attached yet
  120. break;
  121. }
  122. parent = dentry->d_parent;
  123. prefetch(parent);
  124. error = prepend_name(&bptr, &blen, &dentry->d_name);
  125. if (error)
  126. break;
  127. dentry = parent;
  128. }
  129. if (!(seq & 1))
  130. rcu_read_unlock();
  131. if (need_seqretry(&rename_lock, seq)) {
  132. seq = 1;
  133. goto restart;
  134. }
  135. done_seqretry(&rename_lock, seq);
  136. if (!(m_seq & 1))
  137. rcu_read_unlock();
  138. if (need_seqretry(&mount_lock, m_seq)) {
  139. m_seq = 1;
  140. goto restart_mnt;
  141. }
  142. done_seqretry(&mount_lock, m_seq);
  143. if (error >= 0 && bptr == *buffer) {
  144. if (--blen < 0)
  145. error = -ENAMETOOLONG;
  146. else
  147. *--bptr = '/';
  148. }
  149. *buffer = bptr;
  150. *buflen = blen;
  151. return error;
  152. }
  153. /**
  154. * __d_path - return the path of a dentry
  155. * @path: the dentry/vfsmount to report
  156. * @root: root vfsmnt/dentry
  157. * @buf: buffer to return value in
  158. * @buflen: buffer length
  159. *
  160. * Convert a dentry into an ASCII path name.
  161. *
  162. * Returns a pointer into the buffer or an error code if the
  163. * path was too long.
  164. *
  165. * "buflen" should be positive.
  166. *
  167. * If the path is not reachable from the supplied root, return %NULL.
  168. */
  169. char *__d_path(const struct path *path,
  170. const struct path *root,
  171. char *buf, int buflen)
  172. {
  173. char *res = buf + buflen;
  174. int error;
  175. prepend(&res, &buflen, "\0", 1);
  176. error = prepend_path(path, root, &res, &buflen);
  177. if (error < 0)
  178. return ERR_PTR(error);
  179. if (error > 0)
  180. return NULL;
  181. return res;
  182. }
  183. char *d_absolute_path(const struct path *path,
  184. char *buf, int buflen)
  185. {
  186. struct path root = {};
  187. char *res = buf + buflen;
  188. int error;
  189. prepend(&res, &buflen, "\0", 1);
  190. error = prepend_path(path, &root, &res, &buflen);
  191. if (error > 1)
  192. error = -EINVAL;
  193. if (error < 0)
  194. return ERR_PTR(error);
  195. return res;
  196. }
  197. /*
  198. * same as __d_path but appends "(deleted)" for unlinked files.
  199. */
  200. static int path_with_deleted(const struct path *path,
  201. const struct path *root,
  202. char **buf, int *buflen)
  203. {
  204. prepend(buf, buflen, "\0", 1);
  205. if (d_unlinked(path->dentry)) {
  206. int error = prepend(buf, buflen, " (deleted)", 10);
  207. if (error)
  208. return error;
  209. }
  210. return prepend_path(path, root, buf, buflen);
  211. }
  212. static int prepend_unreachable(char **buffer, int *buflen)
  213. {
  214. return prepend(buffer, buflen, "(unreachable)", 13);
  215. }
  216. static void get_fs_root_rcu(struct fs_struct *fs, struct path *root)
  217. {
  218. unsigned seq;
  219. do {
  220. seq = read_seqcount_begin(&fs->seq);
  221. *root = fs->root;
  222. } while (read_seqcount_retry(&fs->seq, seq));
  223. }
  224. /**
  225. * d_path - return the path of a dentry
  226. * @path: path to report
  227. * @buf: buffer to return value in
  228. * @buflen: buffer length
  229. *
  230. * Convert a dentry into an ASCII path name. If the entry has been deleted
  231. * the string " (deleted)" is appended. Note that this is ambiguous.
  232. *
  233. * Returns a pointer into the buffer or an error code if the path was
  234. * too long. Note: Callers should use the returned pointer, not the passed
  235. * in buffer, to use the name! The implementation often starts at an offset
  236. * into the buffer, and may leave 0 bytes at the start.
  237. *
  238. * "buflen" should be positive.
  239. */
  240. char *d_path(const struct path *path, char *buf, int buflen)
  241. {
  242. char *res = buf + buflen;
  243. struct path root;
  244. int error;
  245. /*
  246. * We have various synthetic filesystems that never get mounted. On
  247. * these filesystems dentries are never used for lookup purposes, and
  248. * thus don't need to be hashed. They also don't need a name until a
  249. * user wants to identify the object in /proc/pid/fd/. The little hack
  250. * below allows us to generate a name for these objects on demand:
  251. *
  252. * Some pseudo inodes are mountable. When they are mounted
  253. * path->dentry == path->mnt->mnt_root. In that case don't call d_dname
  254. * and instead have d_path return the mounted path.
  255. */
  256. if (path->dentry->d_op && path->dentry->d_op->d_dname &&
  257. (!IS_ROOT(path->dentry) || path->dentry != path->mnt->mnt_root))
  258. return path->dentry->d_op->d_dname(path->dentry, buf, buflen);
  259. rcu_read_lock();
  260. get_fs_root_rcu(current->fs, &root);
  261. error = path_with_deleted(path, &root, &res, &buflen);
  262. rcu_read_unlock();
  263. if (error < 0)
  264. res = ERR_PTR(error);
  265. return res;
  266. }
  267. EXPORT_SYMBOL(d_path);
  268. /*
  269. * Helper function for dentry_operations.d_dname() members
  270. */
  271. char *dynamic_dname(struct dentry *dentry, char *buffer, int buflen,
  272. const char *fmt, ...)
  273. {
  274. va_list args;
  275. char temp[64];
  276. int sz;
  277. va_start(args, fmt);
  278. sz = vsnprintf(temp, sizeof(temp), fmt, args) + 1;
  279. va_end(args);
  280. if (sz > sizeof(temp) || sz > buflen)
  281. return ERR_PTR(-ENAMETOOLONG);
  282. buffer += buflen - sz;
  283. return memcpy(buffer, temp, sz);
  284. }
  285. char *simple_dname(struct dentry *dentry, char *buffer, int buflen)
  286. {
  287. char *end = buffer + buflen;
  288. /* these dentries are never renamed, so d_lock is not needed */
  289. if (prepend(&end, &buflen, " (deleted)", 11) ||
  290. prepend(&end, &buflen, dentry->d_name.name, dentry->d_name.len) ||
  291. prepend(&end, &buflen, "/", 1))
  292. end = ERR_PTR(-ENAMETOOLONG);
  293. return end;
  294. }
  295. /*
  296. * Write full pathname from the root of the filesystem into the buffer.
  297. */
  298. static char *__dentry_path(struct dentry *d, char *buf, int buflen)
  299. {
  300. struct dentry *dentry;
  301. char *end, *retval;
  302. int len, seq = 0;
  303. int error = 0;
  304. if (buflen < 2)
  305. goto Elong;
  306. rcu_read_lock();
  307. restart:
  308. dentry = d;
  309. end = buf + buflen;
  310. len = buflen;
  311. prepend(&end, &len, "\0", 1);
  312. /* Get '/' right */
  313. retval = end-1;
  314. *retval = '/';
  315. read_seqbegin_or_lock(&rename_lock, &seq);
  316. while (!IS_ROOT(dentry)) {
  317. struct dentry *parent = dentry->d_parent;
  318. prefetch(parent);
  319. error = prepend_name(&end, &len, &dentry->d_name);
  320. if (error)
  321. break;
  322. retval = end;
  323. dentry = parent;
  324. }
  325. if (!(seq & 1))
  326. rcu_read_unlock();
  327. if (need_seqretry(&rename_lock, seq)) {
  328. seq = 1;
  329. goto restart;
  330. }
  331. done_seqretry(&rename_lock, seq);
  332. if (error)
  333. goto Elong;
  334. return retval;
  335. Elong:
  336. return ERR_PTR(-ENAMETOOLONG);
  337. }
  338. char *dentry_path_raw(struct dentry *dentry, char *buf, int buflen)
  339. {
  340. return __dentry_path(dentry, buf, buflen);
  341. }
  342. EXPORT_SYMBOL(dentry_path_raw);
  343. char *dentry_path(struct dentry *dentry, char *buf, int buflen)
  344. {
  345. char *p = NULL;
  346. char *retval;
  347. if (d_unlinked(dentry)) {
  348. p = buf + buflen;
  349. if (prepend(&p, &buflen, "//deleted", 10) != 0)
  350. goto Elong;
  351. buflen++;
  352. }
  353. retval = __dentry_path(dentry, buf, buflen);
  354. if (!IS_ERR(retval) && p)
  355. *p = '/'; /* restore '/' overriden with '\0' */
  356. return retval;
  357. Elong:
  358. return ERR_PTR(-ENAMETOOLONG);
  359. }
  360. static void get_fs_root_and_pwd_rcu(struct fs_struct *fs, struct path *root,
  361. struct path *pwd)
  362. {
  363. unsigned seq;
  364. do {
  365. seq = read_seqcount_begin(&fs->seq);
  366. *root = fs->root;
  367. *pwd = fs->pwd;
  368. } while (read_seqcount_retry(&fs->seq, seq));
  369. }
  370. /*
  371. * NOTE! The user-level library version returns a
  372. * character pointer. The kernel system call just
  373. * returns the length of the buffer filled (which
  374. * includes the ending '\0' character), or a negative
  375. * error value. So libc would do something like
  376. *
  377. * char *getcwd(char * buf, size_t size)
  378. * {
  379. * int retval;
  380. *
  381. * retval = sys_getcwd(buf, size);
  382. * if (retval >= 0)
  383. * return buf;
  384. * errno = -retval;
  385. * return NULL;
  386. * }
  387. */
  388. SYSCALL_DEFINE2(getcwd, char __user *, buf, unsigned long, size)
  389. {
  390. int error;
  391. struct path pwd, root;
  392. char *page = __getname();
  393. if (!page)
  394. return -ENOMEM;
  395. rcu_read_lock();
  396. get_fs_root_and_pwd_rcu(current->fs, &root, &pwd);
  397. error = -ENOENT;
  398. if (!d_unlinked(pwd.dentry)) {
  399. unsigned long len;
  400. char *cwd = page + PATH_MAX;
  401. int buflen = PATH_MAX;
  402. prepend(&cwd, &buflen, "\0", 1);
  403. error = prepend_path(&pwd, &root, &cwd, &buflen);
  404. rcu_read_unlock();
  405. if (error < 0)
  406. goto out;
  407. /* Unreachable from current root */
  408. if (error > 0) {
  409. error = prepend_unreachable(&cwd, &buflen);
  410. if (error)
  411. goto out;
  412. }
  413. error = -ERANGE;
  414. len = PATH_MAX + page - cwd;
  415. if (len <= size) {
  416. error = len;
  417. if (copy_to_user(buf, cwd, len))
  418. error = -EFAULT;
  419. }
  420. } else {
  421. rcu_read_unlock();
  422. }
  423. out:
  424. __putname(page);
  425. return error;
  426. }