d_path.c 12 KB

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