d_path.c 11 KB

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