blob: 3836f5d0b0238fec4769135df78bf30ecc4829aa [file] [log] [blame]
Al Viro7a5cf792018-03-05 19:15:50 -05001/* 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
Al Viroad08ae52021-05-12 14:51:03 -040011struct 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
18static char *extract_string(struct prepend_buffer *p)
Al Viro7a5cf792018-03-05 19:15:50 -050019{
Al Viroad08ae52021-05-12 14:51:03 -040020 if (likely(p->len >= 0))
21 return p->buf;
22 return ERR_PTR(-ENAMETOOLONG);
23}
24
25static void prepend(struct prepend_buffer *p, const char *str, int namelen)
26{
27 p->len -= namelen;
28 if (likely(p->len >= 0)) {
29 p->buf -= namelen;
30 memcpy(p->buf, str, namelen);
Al Virod8548232021-05-17 22:05:23 -040031 }
Al Viro7a5cf792018-03-05 19:15:50 -050032}
33
34/**
35 * prepend_name - prepend a pathname in front of current buffer pointer
36 * @buffer: buffer pointer
37 * @buflen: allocated length of the buffer
38 * @name: name string and length qstr structure
39 *
40 * With RCU path tracing, it may race with d_move(). Use READ_ONCE() to
41 * make sure that either the old or the new name pointer and length are
42 * fetched. However, there may be mismatch between length and pointer.
43 * The length cannot be trusted, we need to copy it byte-by-byte until
44 * the length is reached or a null byte is found. It also prepends "/" at
45 * the beginning of the name. The sequence number check at the caller will
46 * retry it again when a d_move() does happen. So any garbage in the buffer
47 * due to mismatched pointer and length will be discarded.
48 *
49 * Load acquire is needed to make sure that we see that terminating NUL.
50 */
Al Viroad08ae52021-05-12 14:51:03 -040051static bool prepend_name(struct prepend_buffer *p, const struct qstr *name)
Al Viro7a5cf792018-03-05 19:15:50 -050052{
53 const char *dname = smp_load_acquire(&name->name); /* ^^^ */
54 u32 dlen = READ_ONCE(name->len);
Al Viroad08ae52021-05-12 14:51:03 -040055 char *s;
Al Viro7a5cf792018-03-05 19:15:50 -050056
Al Viroad08ae52021-05-12 14:51:03 -040057 p->len -= dlen + 1;
58 if (unlikely(p->len < 0))
Al Viro95b55c42021-05-17 22:41:11 -040059 return false;
Al Viroad08ae52021-05-12 14:51:03 -040060 s = p->buf -= dlen + 1;
61 *s++ = '/';
Al Viro7a5cf792018-03-05 19:15:50 -050062 while (dlen--) {
63 char c = *dname++;
64 if (!c)
65 break;
Al Viroad08ae52021-05-12 14:51:03 -040066 *s++ = c;
Al Viro7a5cf792018-03-05 19:15:50 -050067 }
Al Viro95b55c42021-05-17 22:41:11 -040068 return true;
Al Viro7a5cf792018-03-05 19:15:50 -050069}
70
71/**
72 * prepend_path - Prepend path string to a buffer
73 * @path: the dentry/vfsmount to report
74 * @root: root vfsmnt/dentry
75 * @buffer: pointer to the end of the buffer
76 * @buflen: pointer to buffer length
77 *
78 * The function will first try to write out the pathname without taking any
79 * lock other than the RCU read lock to make sure that dentries won't go away.
80 * It only checks the sequence number of the global rename_lock as any change
81 * in the dentry's d_seq will be preceded by changes in the rename_lock
82 * sequence number. If the sequence number had been changed, it will restart
83 * the whole pathname back-tracing sequence again by taking the rename_lock.
84 * In this case, there is no need to take the RCU read lock as the recursive
85 * parent pointer references will keep the dentry chain alive as long as no
86 * rename operation is performed.
87 */
88static int prepend_path(const struct path *path,
89 const struct path *root,
Al Viroad08ae52021-05-12 14:51:03 -040090 struct prepend_buffer *p)
Al Viro7a5cf792018-03-05 19:15:50 -050091{
92 struct dentry *dentry;
Al Viro7a5cf792018-03-05 19:15:50 -050093 struct mount *mnt;
94 int error = 0;
95 unsigned seq, m_seq = 0;
Al Viroad08ae52021-05-12 14:51:03 -040096 struct prepend_buffer b;
Al Viro7a5cf792018-03-05 19:15:50 -050097
98 rcu_read_lock();
99restart_mnt:
100 read_seqbegin_or_lock(&mount_lock, &m_seq);
101 seq = 0;
102 rcu_read_lock();
103restart:
Al Viroad08ae52021-05-12 14:51:03 -0400104 b = *p;
Al Viro7a5cf792018-03-05 19:15:50 -0500105 error = 0;
106 dentry = path->dentry;
Al Viro7c0d5522021-05-12 16:24:12 -0400107 mnt = real_mount(path->mnt);
Al Viro7a5cf792018-03-05 19:15:50 -0500108 read_seqbegin_or_lock(&rename_lock, &seq);
Al Viro7c0d5522021-05-12 16:24:12 -0400109 while (dentry != root->dentry || &mnt->mnt != root->mnt) {
Al Viro7a5cf792018-03-05 19:15:50 -0500110 struct dentry * parent;
111
Al Viro7c0d5522021-05-12 16:24:12 -0400112 if (dentry == mnt->mnt.mnt_root || IS_ROOT(dentry)) {
Al Viro7a5cf792018-03-05 19:15:50 -0500113 struct mount *parent = READ_ONCE(mnt->mnt_parent);
Andrii Nakryiko09cad072020-10-14 13:45:28 -0700114 struct mnt_namespace *mnt_ns;
115
Al Viro7a5cf792018-03-05 19:15:50 -0500116 /* Escaped? */
Al Viro7c0d5522021-05-12 16:24:12 -0400117 if (dentry != mnt->mnt.mnt_root) {
Al Viroad08ae52021-05-12 14:51:03 -0400118 b = *p;
Al Viro7a5cf792018-03-05 19:15:50 -0500119 error = 3;
120 break;
121 }
122 /* Global root? */
123 if (mnt != parent) {
124 dentry = READ_ONCE(mnt->mnt_mountpoint);
125 mnt = parent;
Al Viro7a5cf792018-03-05 19:15:50 -0500126 continue;
127 }
Andrii Nakryiko09cad072020-10-14 13:45:28 -0700128 mnt_ns = READ_ONCE(mnt->mnt_ns);
129 /* open-coded is_mounted() to use local mnt_ns */
130 if (!IS_ERR_OR_NULL(mnt_ns) && !is_anon_ns(mnt_ns))
Al Virof2683bd2019-08-30 19:31:09 -0400131 error = 1; // absolute root
132 else
133 error = 2; // detached or not attached yet
Al Viro7a5cf792018-03-05 19:15:50 -0500134 break;
135 }
136 parent = dentry->d_parent;
137 prefetch(parent);
Al Viroad08ae52021-05-12 14:51:03 -0400138 if (!prepend_name(&b, &dentry->d_name))
Al Viro7a5cf792018-03-05 19:15:50 -0500139 break;
140
141 dentry = parent;
142 }
143 if (!(seq & 1))
144 rcu_read_unlock();
145 if (need_seqretry(&rename_lock, seq)) {
146 seq = 1;
147 goto restart;
148 }
149 done_seqretry(&rename_lock, seq);
150
151 if (!(m_seq & 1))
152 rcu_read_unlock();
153 if (need_seqretry(&mount_lock, m_seq)) {
154 m_seq = 1;
155 goto restart_mnt;
156 }
157 done_seqretry(&mount_lock, m_seq);
158
Al Viroad08ae52021-05-12 14:51:03 -0400159 if (b.len == p->len)
160 prepend(&b, "/", 1);
Al Viro01a44282021-05-17 22:29:03 -0400161
Al Viroad08ae52021-05-12 14:51:03 -0400162 *p = b;
Al Viro7a5cf792018-03-05 19:15:50 -0500163 return error;
164}
165
166/**
167 * __d_path - return the path of a dentry
168 * @path: the dentry/vfsmount to report
169 * @root: root vfsmnt/dentry
170 * @buf: buffer to return value in
171 * @buflen: buffer length
172 *
173 * Convert a dentry into an ASCII path name.
174 *
175 * Returns a pointer into the buffer or an error code if the
176 * path was too long.
177 *
178 * "buflen" should be positive.
179 *
180 * If the path is not reachable from the supplied root, return %NULL.
181 */
182char *__d_path(const struct path *path,
183 const struct path *root,
184 char *buf, int buflen)
185{
Al Viroad08ae52021-05-12 14:51:03 -0400186 DECLARE_BUFFER(b, buf, buflen);
Al Viro7a5cf792018-03-05 19:15:50 -0500187
Al Viroad08ae52021-05-12 14:51:03 -0400188 prepend(&b, "", 1);
189 if (prepend_path(path, root, &b) > 0)
Al Viro7a5cf792018-03-05 19:15:50 -0500190 return NULL;
Al Viroad08ae52021-05-12 14:51:03 -0400191 return extract_string(&b);
Al Viro7a5cf792018-03-05 19:15:50 -0500192}
193
194char *d_absolute_path(const struct path *path,
195 char *buf, int buflen)
196{
197 struct path root = {};
Al Viroad08ae52021-05-12 14:51:03 -0400198 DECLARE_BUFFER(b, buf, buflen);
Al Viro7a5cf792018-03-05 19:15:50 -0500199
Al Viroad08ae52021-05-12 14:51:03 -0400200 prepend(&b, "", 1);
201 if (prepend_path(path, &root, &b) > 1)
Al Viro01a44282021-05-17 22:29:03 -0400202 return ERR_PTR(-EINVAL);
Al Viroad08ae52021-05-12 14:51:03 -0400203 return extract_string(&b);
Al Viro7a5cf792018-03-05 19:15:50 -0500204}
205
Al Viro7a5cf792018-03-05 19:15:50 -0500206static void get_fs_root_rcu(struct fs_struct *fs, struct path *root)
207{
208 unsigned seq;
209
210 do {
211 seq = read_seqcount_begin(&fs->seq);
212 *root = fs->root;
213 } while (read_seqcount_retry(&fs->seq, seq));
214}
215
216/**
217 * d_path - return the path of a dentry
218 * @path: path to report
219 * @buf: buffer to return value in
220 * @buflen: buffer length
221 *
222 * Convert a dentry into an ASCII path name. If the entry has been deleted
223 * the string " (deleted)" is appended. Note that this is ambiguous.
224 *
225 * Returns a pointer into the buffer or an error code if the path was
226 * too long. Note: Callers should use the returned pointer, not the passed
227 * in buffer, to use the name! The implementation often starts at an offset
228 * into the buffer, and may leave 0 bytes at the start.
229 *
230 * "buflen" should be positive.
231 */
232char *d_path(const struct path *path, char *buf, int buflen)
233{
Al Viroad08ae52021-05-12 14:51:03 -0400234 DECLARE_BUFFER(b, buf, buflen);
Al Viro7a5cf792018-03-05 19:15:50 -0500235 struct path root;
Al Viro7a5cf792018-03-05 19:15:50 -0500236
237 /*
238 * We have various synthetic filesystems that never get mounted. On
239 * these filesystems dentries are never used for lookup purposes, and
240 * thus don't need to be hashed. They also don't need a name until a
241 * user wants to identify the object in /proc/pid/fd/. The little hack
242 * below allows us to generate a name for these objects on demand:
243 *
244 * Some pseudo inodes are mountable. When they are mounted
245 * path->dentry == path->mnt->mnt_root. In that case don't call d_dname
246 * and instead have d_path return the mounted path.
247 */
248 if (path->dentry->d_op && path->dentry->d_op->d_dname &&
249 (!IS_ROOT(path->dentry) || path->dentry != path->mnt->mnt_root))
250 return path->dentry->d_op->d_dname(path->dentry, buf, buflen);
251
252 rcu_read_lock();
253 get_fs_root_rcu(current->fs, &root);
Al Viro90243482021-05-17 21:43:01 -0400254 if (unlikely(d_unlinked(path->dentry)))
Al Viroad08ae52021-05-12 14:51:03 -0400255 prepend(&b, " (deleted)", 11);
Al Viro90243482021-05-17 21:43:01 -0400256 else
Al Viroad08ae52021-05-12 14:51:03 -0400257 prepend(&b, "", 1);
258 prepend_path(path, &root, &b);
Al Viro7a5cf792018-03-05 19:15:50 -0500259 rcu_read_unlock();
260
Al Viroad08ae52021-05-12 14:51:03 -0400261 return extract_string(&b);
Al Viro7a5cf792018-03-05 19:15:50 -0500262}
263EXPORT_SYMBOL(d_path);
264
265/*
266 * Helper function for dentry_operations.d_dname() members
267 */
268char *dynamic_dname(struct dentry *dentry, char *buffer, int buflen,
269 const char *fmt, ...)
270{
271 va_list args;
272 char temp[64];
273 int sz;
274
275 va_start(args, fmt);
276 sz = vsnprintf(temp, sizeof(temp), fmt, args) + 1;
277 va_end(args);
278
279 if (sz > sizeof(temp) || sz > buflen)
280 return ERR_PTR(-ENAMETOOLONG);
281
282 buffer += buflen - sz;
283 return memcpy(buffer, temp, sz);
284}
285
286char *simple_dname(struct dentry *dentry, char *buffer, int buflen)
287{
Al Viroad08ae52021-05-12 14:51:03 -0400288 DECLARE_BUFFER(b, buffer, buflen);
Al Viro7a5cf792018-03-05 19:15:50 -0500289 /* these dentries are never renamed, so d_lock is not needed */
Al Viroad08ae52021-05-12 14:51:03 -0400290 prepend(&b, " (deleted)", 11);
291 prepend(&b, dentry->d_name.name, dentry->d_name.len);
292 prepend(&b, "/", 1);
293 return extract_string(&b);
Al Viro7a5cf792018-03-05 19:15:50 -0500294}
Al Viro7a5cf792018-03-05 19:15:50 -0500295
296/*
297 * Write full pathname from the root of the filesystem into the buffer.
298 */
Al Viroad08ae52021-05-12 14:51:03 -0400299static char *__dentry_path(const struct dentry *d, struct prepend_buffer *p)
Al Viro7a5cf792018-03-05 19:15:50 -0500300{
Al Viroa2bbe662019-07-07 09:57:53 -0400301 const struct dentry *dentry;
Al Viroad08ae52021-05-12 14:51:03 -0400302 struct prepend_buffer b;
303 int seq = 0;
Al Viro7a5cf792018-03-05 19:15:50 -0500304
305 rcu_read_lock();
306restart:
307 dentry = d;
Al Viroad08ae52021-05-12 14:51:03 -0400308 b = *p;
Al Viro7a5cf792018-03-05 19:15:50 -0500309 read_seqbegin_or_lock(&rename_lock, &seq);
310 while (!IS_ROOT(dentry)) {
Al Viroa2bbe662019-07-07 09:57:53 -0400311 const struct dentry *parent = dentry->d_parent;
Al Viro7a5cf792018-03-05 19:15:50 -0500312
313 prefetch(parent);
Al Viroad08ae52021-05-12 14:51:03 -0400314 if (!prepend_name(&b, &dentry->d_name))
Al Viro7a5cf792018-03-05 19:15:50 -0500315 break;
Al Viro7a5cf792018-03-05 19:15:50 -0500316 dentry = parent;
317 }
318 if (!(seq & 1))
319 rcu_read_unlock();
320 if (need_seqretry(&rename_lock, seq)) {
321 seq = 1;
322 goto restart;
323 }
324 done_seqretry(&rename_lock, seq);
Al Viroad08ae52021-05-12 14:51:03 -0400325 if (b.len == p->len)
326 prepend(&b, "/", 1);
327 return extract_string(&b);
Al Viro7a5cf792018-03-05 19:15:50 -0500328}
329
Al Viroa2bbe662019-07-07 09:57:53 -0400330char *dentry_path_raw(const struct dentry *dentry, char *buf, int buflen)
Al Viro7a5cf792018-03-05 19:15:50 -0500331{
Al Viroad08ae52021-05-12 14:51:03 -0400332 DECLARE_BUFFER(b, buf, buflen);
333
334 prepend(&b, "", 1);
335 return __dentry_path(dentry, &b);
Al Viro7a5cf792018-03-05 19:15:50 -0500336}
337EXPORT_SYMBOL(dentry_path_raw);
338
Al Viroa2bbe662019-07-07 09:57:53 -0400339char *dentry_path(const struct dentry *dentry, char *buf, int buflen)
Al Viro7a5cf792018-03-05 19:15:50 -0500340{
Al Viroad08ae52021-05-12 14:51:03 -0400341 DECLARE_BUFFER(b, buf, buflen);
Al Viro7a5cf792018-03-05 19:15:50 -0500342
Al Viro3a291c92021-05-17 20:16:51 -0400343 if (unlikely(d_unlinked(dentry)))
Al Viroad08ae52021-05-12 14:51:03 -0400344 prepend(&b, "//deleted", 10);
Al Viro3a291c92021-05-17 20:16:51 -0400345 else
Al Viroad08ae52021-05-12 14:51:03 -0400346 prepend(&b, "", 1);
347 return __dentry_path(dentry, &b);
Al Viro7a5cf792018-03-05 19:15:50 -0500348}
349
350static void get_fs_root_and_pwd_rcu(struct fs_struct *fs, struct path *root,
351 struct path *pwd)
352{
353 unsigned seq;
354
355 do {
356 seq = read_seqcount_begin(&fs->seq);
357 *root = fs->root;
358 *pwd = fs->pwd;
359 } while (read_seqcount_retry(&fs->seq, seq));
360}
361
362/*
363 * NOTE! The user-level library version returns a
364 * character pointer. The kernel system call just
365 * returns the length of the buffer filled (which
366 * includes the ending '\0' character), or a negative
367 * error value. So libc would do something like
368 *
369 * char *getcwd(char * buf, size_t size)
370 * {
371 * int retval;
372 *
373 * retval = sys_getcwd(buf, size);
374 * if (retval >= 0)
375 * return buf;
376 * errno = -retval;
377 * return NULL;
378 * }
379 */
380SYSCALL_DEFINE2(getcwd, char __user *, buf, unsigned long, size)
381{
382 int error;
383 struct path pwd, root;
384 char *page = __getname();
385
386 if (!page)
387 return -ENOMEM;
388
389 rcu_read_lock();
390 get_fs_root_and_pwd_rcu(current->fs, &root, &pwd);
391
392 error = -ENOENT;
393 if (!d_unlinked(pwd.dentry)) {
394 unsigned long len;
Al Viroad08ae52021-05-12 14:51:03 -0400395 DECLARE_BUFFER(b, page, PATH_MAX);
Al Viro7a5cf792018-03-05 19:15:50 -0500396
Al Viroad08ae52021-05-12 14:51:03 -0400397 prepend(&b, "", 1);
398 if (prepend_path(&pwd, &root, &b) > 0)
399 prepend(&b, "(unreachable)", 13);
Al Viro7a5cf792018-03-05 19:15:50 -0500400 rcu_read_unlock();
401
Al Viroad08ae52021-05-12 14:51:03 -0400402 if (b.len < 0) {
Al Viroa0378fb2021-05-17 21:56:38 -0400403 error = -ENAMETOOLONG;
Al Viro7a5cf792018-03-05 19:15:50 -0500404 goto out;
Al Viro7a5cf792018-03-05 19:15:50 -0500405 }
406
407 error = -ERANGE;
Al Viroad08ae52021-05-12 14:51:03 -0400408 len = PATH_MAX - b.len;
Al Viro7a5cf792018-03-05 19:15:50 -0500409 if (len <= size) {
410 error = len;
Al Viroad08ae52021-05-12 14:51:03 -0400411 if (copy_to_user(buf, b.buf, len))
Al Viro7a5cf792018-03-05 19:15:50 -0500412 error = -EFAULT;
413 }
414 } else {
415 rcu_read_unlock();
416 }
417
418out:
419 __putname(page);
420 return error;
421}