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>
7 #include <linux/slab.h>
8 #include <linux/prefetch.h>
12 struct prepend_buffer {
16 #define DECLARE_BUFFER(__name, __buf, __len) \
17 struct prepend_buffer __name = {.buf = __buf + __len, .len = __len}
19 static char *extract_string(struct prepend_buffer *p)
21 if (likely(p->len >= 0))
23 return ERR_PTR(-ENAMETOOLONG);
26 static bool prepend_char(struct prepend_buffer *p, unsigned char c)
28 if (likely(p->len > 0)) {
38 * The source of the prepend data can be an optimistic load
39 * of a dentry name and length. And because we don't hold any
40 * locks, the length and the pointer to the name may not be
41 * in sync if a concurrent rename happens, and the kernel
42 * copy might fault as a result.
44 * The end result will correct itself when we check the
45 * rename sequence count, but we need to be able to handle
46 * the fault gracefully.
48 static bool prepend_copy(void *dst, const void *src, int len)
50 if (unlikely(copy_from_kernel_nofault(dst, src, len))) {
51 memset(dst, 'x', len);
57 static bool prepend(struct prepend_buffer *p, const char *str, int namelen)
59 // Already overflowed?
64 if (p->len < namelen) {
65 // Fill as much as possible from the end of the name
66 str += namelen - p->len;
68 prepend_copy(p->buf, str, p->len);
76 return prepend_copy(p->buf, str, namelen);
80 * prepend_name - prepend a pathname in front of current buffer pointer
81 * @p: prepend buffer which contains buffer pointer and allocated length
82 * @name: name string and length qstr structure
84 * With RCU path tracing, it may race with d_move(). Use READ_ONCE() to
85 * make sure that either the old or the new name pointer and length are
86 * fetched. However, there may be mismatch between length and pointer.
87 * But since the length cannot be trusted, we need to copy the name very
88 * carefully when doing the prepend_copy(). It also prepends "/" at
89 * the beginning of the name. The sequence number check at the caller will
90 * retry it again when a d_move() does happen. So any garbage in the buffer
91 * due to mismatched pointer and length will be discarded.
93 * Load acquire is needed to make sure that we see the new name data even
94 * if we might get the length wrong.
96 static bool prepend_name(struct prepend_buffer *p, const struct qstr *name)
98 const char *dname = smp_load_acquire(&name->name); /* ^^^ */
99 u32 dlen = READ_ONCE(name->len);
101 return prepend(p, dname, dlen) && prepend_char(p, '/');
104 static int __prepend_path(const struct dentry *dentry, const struct mount *mnt,
105 const struct path *root, struct prepend_buffer *p)
107 while (dentry != root->dentry || &mnt->mnt != root->mnt) {
108 const struct dentry *parent = READ_ONCE(dentry->d_parent);
110 if (dentry == mnt->mnt.mnt_root) {
111 struct mount *m = READ_ONCE(mnt->mnt_parent);
112 struct mnt_namespace *mnt_ns;
114 if (likely(mnt != m)) {
115 dentry = READ_ONCE(mnt->mnt_mountpoint);
120 mnt_ns = READ_ONCE(mnt->mnt_ns);
121 /* open-coded is_mounted() to use local mnt_ns */
122 if (!IS_ERR_OR_NULL(mnt_ns) && !is_anon_ns(mnt_ns))
123 return 1; // absolute root
125 return 2; // detached or not attached yet
128 if (unlikely(dentry == parent))
133 if (!prepend_name(p, &dentry->d_name))
141 * prepend_path - Prepend path string to a buffer
142 * @path: the dentry/vfsmount to report
143 * @root: root vfsmnt/dentry
144 * @p: prepend buffer which contains buffer pointer and allocated length
146 * The function will first try to write out the pathname without taking any
147 * lock other than the RCU read lock to make sure that dentries won't go away.
148 * It only checks the sequence number of the global rename_lock as any change
149 * in the dentry's d_seq will be preceded by changes in the rename_lock
150 * sequence number. If the sequence number had been changed, it will restart
151 * the whole pathname back-tracing sequence again by taking the rename_lock.
152 * In this case, there is no need to take the RCU read lock as the recursive
153 * parent pointer references will keep the dentry chain alive as long as no
154 * rename operation is performed.
156 static int prepend_path(const struct path *path,
157 const struct path *root,
158 struct prepend_buffer *p)
160 unsigned seq, m_seq = 0;
161 struct prepend_buffer b;
166 read_seqbegin_or_lock(&mount_lock, &m_seq);
171 read_seqbegin_or_lock(&rename_lock, &seq);
172 error = __prepend_path(path->dentry, real_mount(path->mnt), root, &b);
175 if (need_seqretry(&rename_lock, seq)) {
179 done_seqretry(&rename_lock, seq);
183 if (need_seqretry(&mount_lock, m_seq)) {
187 done_seqretry(&mount_lock, m_seq);
189 if (unlikely(error == 3))
193 prepend_char(&b, '/');
200 * __d_path - return the path of a dentry
201 * @path: the dentry/vfsmount to report
202 * @root: root vfsmnt/dentry
203 * @buf: buffer to return value in
204 * @buflen: buffer length
206 * Convert a dentry into an ASCII path name.
208 * Returns a pointer into the buffer or an error code if the
211 * "buflen" should be positive.
213 * If the path is not reachable from the supplied root, return %NULL.
215 char *__d_path(const struct path *path,
216 const struct path *root,
217 char *buf, int buflen)
219 DECLARE_BUFFER(b, buf, buflen);
222 if (unlikely(prepend_path(path, root, &b) > 0))
224 return extract_string(&b);
227 char *d_absolute_path(const struct path *path,
228 char *buf, int buflen)
230 struct path root = {};
231 DECLARE_BUFFER(b, buf, buflen);
234 if (unlikely(prepend_path(path, &root, &b) > 1))
235 return ERR_PTR(-EINVAL);
236 return extract_string(&b);
239 static void get_fs_root_rcu(struct fs_struct *fs, struct path *root)
244 seq = read_seqcount_begin(&fs->seq);
246 } while (read_seqcount_retry(&fs->seq, seq));
250 * d_path - return the path of a dentry
251 * @path: path to report
252 * @buf: buffer to return value in
253 * @buflen: buffer length
255 * Convert a dentry into an ASCII path name. If the entry has been deleted
256 * the string " (deleted)" is appended. Note that this is ambiguous.
258 * Returns a pointer into the buffer or an error code if the path was
259 * too long. Note: Callers should use the returned pointer, not the passed
260 * in buffer, to use the name! The implementation often starts at an offset
261 * into the buffer, and may leave 0 bytes at the start.
263 * "buflen" should be positive.
265 char *d_path(const struct path *path, char *buf, int buflen)
267 DECLARE_BUFFER(b, buf, buflen);
271 * We have various synthetic filesystems that never get mounted. On
272 * these filesystems dentries are never used for lookup purposes, and
273 * thus don't need to be hashed. They also don't need a name until a
274 * user wants to identify the object in /proc/pid/fd/. The little hack
275 * below allows us to generate a name for these objects on demand:
277 * Some pseudo inodes are mountable. When they are mounted
278 * path->dentry == path->mnt->mnt_root. In that case don't call d_dname
279 * and instead have d_path return the mounted path.
281 if (path->dentry->d_op && path->dentry->d_op->d_dname &&
282 (!IS_ROOT(path->dentry) || path->dentry != path->mnt->mnt_root))
283 return path->dentry->d_op->d_dname(path->dentry, buf, buflen);
286 get_fs_root_rcu(current->fs, &root);
287 if (unlikely(d_unlinked(path->dentry)))
288 prepend(&b, " (deleted)", 11);
291 prepend_path(path, &root, &b);
294 return extract_string(&b);
296 EXPORT_SYMBOL(d_path);
299 * Helper function for dentry_operations.d_dname() members
301 char *dynamic_dname(char *buffer, int buflen, const char *fmt, ...)
308 sz = vsnprintf(temp, sizeof(temp), fmt, args) + 1;
311 if (sz > sizeof(temp) || sz > buflen)
312 return ERR_PTR(-ENAMETOOLONG);
314 buffer += buflen - sz;
315 return memcpy(buffer, temp, sz);
318 char *simple_dname(struct dentry *dentry, char *buffer, int buflen)
320 DECLARE_BUFFER(b, buffer, buflen);
321 /* these dentries are never renamed, so d_lock is not needed */
322 prepend(&b, " (deleted)", 11);
323 prepend(&b, dentry->d_name.name, dentry->d_name.len);
324 prepend_char(&b, '/');
325 return extract_string(&b);
329 * Write full pathname from the root of the filesystem into the buffer.
331 static char *__dentry_path(const struct dentry *d, struct prepend_buffer *p)
333 const struct dentry *dentry;
334 struct prepend_buffer b;
341 read_seqbegin_or_lock(&rename_lock, &seq);
342 while (!IS_ROOT(dentry)) {
343 const struct dentry *parent = dentry->d_parent;
346 if (!prepend_name(&b, &dentry->d_name))
352 if (need_seqretry(&rename_lock, seq)) {
356 done_seqretry(&rename_lock, seq);
358 prepend_char(&b, '/');
359 return extract_string(&b);
362 char *dentry_path_raw(const struct dentry *dentry, char *buf, int buflen)
364 DECLARE_BUFFER(b, buf, buflen);
367 return __dentry_path(dentry, &b);
369 EXPORT_SYMBOL(dentry_path_raw);
371 char *dentry_path(const struct dentry *dentry, char *buf, int buflen)
373 DECLARE_BUFFER(b, buf, buflen);
375 if (unlikely(d_unlinked(dentry)))
376 prepend(&b, "//deleted", 10);
379 return __dentry_path(dentry, &b);
382 static void get_fs_root_and_pwd_rcu(struct fs_struct *fs, struct path *root,
388 seq = read_seqcount_begin(&fs->seq);
391 } while (read_seqcount_retry(&fs->seq, seq));
395 * NOTE! The user-level library version returns a
396 * character pointer. The kernel system call just
397 * returns the length of the buffer filled (which
398 * includes the ending '\0' character), or a negative
399 * error value. So libc would do something like
401 * char *getcwd(char * buf, size_t size)
405 * retval = sys_getcwd(buf, size);
412 SYSCALL_DEFINE2(getcwd, char __user *, buf, unsigned long, size)
415 struct path pwd, root;
416 char *page = __getname();
422 get_fs_root_and_pwd_rcu(current->fs, &root, &pwd);
424 if (unlikely(d_unlinked(pwd.dentry))) {
429 DECLARE_BUFFER(b, page, PATH_MAX);
432 if (unlikely(prepend_path(&pwd, &root, &b) > 0))
433 prepend(&b, "(unreachable)", 13);
436 len = PATH_MAX - b.len;
437 if (unlikely(len > PATH_MAX))
438 error = -ENAMETOOLONG;
439 else if (unlikely(len > size))
441 else if (copy_to_user(buf, b.buf, len))