blob: 311d432875727283d0d8fde4f27c0fb11f3e370d [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
11static int prepend(char **buffer, int *buflen, const char *str, int namelen)
12{
13 *buflen -= namelen;
14 if (*buflen < 0)
15 return -ENAMETOOLONG;
16 *buffer -= namelen;
17 memcpy(*buffer, str, namelen);
18 return 0;
19}
20
21/**
22 * prepend_name - prepend a pathname in front of current buffer pointer
23 * @buffer: buffer pointer
24 * @buflen: allocated length of the buffer
25 * @name: name string and length qstr structure
26 *
27 * With RCU path tracing, it may race with d_move(). Use READ_ONCE() to
28 * make sure that either the old or the new name pointer and length are
29 * fetched. However, there may be mismatch between length and pointer.
30 * The length cannot be trusted, we need to copy it byte-by-byte until
31 * the length is reached or a null byte is found. It also prepends "/" at
32 * the beginning of the name. The sequence number check at the caller will
33 * retry it again when a d_move() does happen. So any garbage in the buffer
34 * due to mismatched pointer and length will be discarded.
35 *
36 * Load acquire is needed to make sure that we see that terminating NUL.
37 */
38static int prepend_name(char **buffer, int *buflen, const struct qstr *name)
39{
40 const char *dname = smp_load_acquire(&name->name); /* ^^^ */
41 u32 dlen = READ_ONCE(name->len);
42 char *p;
43
44 *buflen -= dlen + 1;
45 if (*buflen < 0)
46 return -ENAMETOOLONG;
47 p = *buffer -= dlen + 1;
48 *p++ = '/';
49 while (dlen--) {
50 char c = *dname++;
51 if (!c)
52 break;
53 *p++ = c;
54 }
55 return 0;
56}
57
58/**
59 * prepend_path - Prepend path string to a buffer
60 * @path: the dentry/vfsmount to report
61 * @root: root vfsmnt/dentry
62 * @buffer: pointer to the end of the buffer
63 * @buflen: pointer to buffer length
64 *
65 * The function will first try to write out the pathname without taking any
66 * lock other than the RCU read lock to make sure that dentries won't go away.
67 * It only checks the sequence number of the global rename_lock as any change
68 * in the dentry's d_seq will be preceded by changes in the rename_lock
69 * sequence number. If the sequence number had been changed, it will restart
70 * the whole pathname back-tracing sequence again by taking the rename_lock.
71 * In this case, there is no need to take the RCU read lock as the recursive
72 * parent pointer references will keep the dentry chain alive as long as no
73 * rename operation is performed.
74 */
75static int prepend_path(const struct path *path,
76 const struct path *root,
77 char **buffer, int *buflen)
78{
79 struct dentry *dentry;
80 struct vfsmount *vfsmnt;
81 struct mount *mnt;
82 int error = 0;
83 unsigned seq, m_seq = 0;
84 char *bptr;
85 int blen;
86
87 rcu_read_lock();
88restart_mnt:
89 read_seqbegin_or_lock(&mount_lock, &m_seq);
90 seq = 0;
91 rcu_read_lock();
92restart:
93 bptr = *buffer;
94 blen = *buflen;
95 error = 0;
96 dentry = path->dentry;
97 vfsmnt = path->mnt;
98 mnt = real_mount(vfsmnt);
99 read_seqbegin_or_lock(&rename_lock, &seq);
100 while (dentry != root->dentry || vfsmnt != root->mnt) {
101 struct dentry * parent;
102
103 if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) {
104 struct mount *parent = READ_ONCE(mnt->mnt_parent);
Andrii Nakryiko09cad072020-10-14 13:45:28 -0700105 struct mnt_namespace *mnt_ns;
106
Al Viro7a5cf792018-03-05 19:15:50 -0500107 /* Escaped? */
108 if (dentry != vfsmnt->mnt_root) {
109 bptr = *buffer;
110 blen = *buflen;
111 error = 3;
112 break;
113 }
114 /* Global root? */
115 if (mnt != parent) {
116 dentry = READ_ONCE(mnt->mnt_mountpoint);
117 mnt = parent;
118 vfsmnt = &mnt->mnt;
119 continue;
120 }
Andrii Nakryiko09cad072020-10-14 13:45:28 -0700121 mnt_ns = READ_ONCE(mnt->mnt_ns);
122 /* open-coded is_mounted() to use local mnt_ns */
123 if (!IS_ERR_OR_NULL(mnt_ns) && !is_anon_ns(mnt_ns))
Al Virof2683bd2019-08-30 19:31:09 -0400124 error = 1; // absolute root
125 else
126 error = 2; // detached or not attached yet
Al Viro7a5cf792018-03-05 19:15:50 -0500127 break;
128 }
129 parent = dentry->d_parent;
130 prefetch(parent);
131 error = prepend_name(&bptr, &blen, &dentry->d_name);
132 if (error)
133 break;
134
135 dentry = parent;
136 }
137 if (!(seq & 1))
138 rcu_read_unlock();
139 if (need_seqretry(&rename_lock, seq)) {
140 seq = 1;
141 goto restart;
142 }
143 done_seqretry(&rename_lock, seq);
144
145 if (!(m_seq & 1))
146 rcu_read_unlock();
147 if (need_seqretry(&mount_lock, m_seq)) {
148 m_seq = 1;
149 goto restart_mnt;
150 }
151 done_seqretry(&mount_lock, m_seq);
152
153 if (error >= 0 && bptr == *buffer) {
154 if (--blen < 0)
155 error = -ENAMETOOLONG;
156 else
157 *--bptr = '/';
158 }
159 *buffer = bptr;
160 *buflen = blen;
161 return error;
162}
163
164/**
165 * __d_path - return the path of a dentry
166 * @path: the dentry/vfsmount to report
167 * @root: root vfsmnt/dentry
168 * @buf: buffer to return value in
169 * @buflen: buffer length
170 *
171 * Convert a dentry into an ASCII path name.
172 *
173 * Returns a pointer into the buffer or an error code if the
174 * path was too long.
175 *
176 * "buflen" should be positive.
177 *
178 * If the path is not reachable from the supplied root, return %NULL.
179 */
180char *__d_path(const struct path *path,
181 const struct path *root,
182 char *buf, int buflen)
183{
184 char *res = buf + buflen;
185 int error;
186
Al Virodfe508762021-05-17 21:11:45 -0400187 prepend(&res, &buflen, "", 1);
Al Viro7a5cf792018-03-05 19:15:50 -0500188 error = prepend_path(path, root, &res, &buflen);
189
190 if (error < 0)
191 return ERR_PTR(error);
192 if (error > 0)
193 return NULL;
194 return res;
195}
196
197char *d_absolute_path(const struct path *path,
198 char *buf, int buflen)
199{
200 struct path root = {};
201 char *res = buf + buflen;
202 int error;
203
Al Virodfe508762021-05-17 21:11:45 -0400204 prepend(&res, &buflen, "", 1);
Al Viro7a5cf792018-03-05 19:15:50 -0500205 error = prepend_path(path, &root, &res, &buflen);
206
207 if (error > 1)
208 error = -EINVAL;
209 if (error < 0)
210 return ERR_PTR(error);
211 return res;
212}
213
Al Viro7a5cf792018-03-05 19:15:50 -0500214static void get_fs_root_rcu(struct fs_struct *fs, struct path *root)
215{
216 unsigned seq;
217
218 do {
219 seq = read_seqcount_begin(&fs->seq);
220 *root = fs->root;
221 } while (read_seqcount_retry(&fs->seq, seq));
222}
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 */
240char *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 /*
247 * We have various synthetic filesystems that never get mounted. On
248 * these filesystems dentries are never used for lookup purposes, and
249 * thus don't need to be hashed. They also don't need a name until a
250 * user wants to identify the object in /proc/pid/fd/. The little hack
251 * below allows us to generate a name for these objects on demand:
252 *
253 * Some pseudo inodes are mountable. When they are mounted
254 * path->dentry == path->mnt->mnt_root. In that case don't call d_dname
255 * and instead have d_path return the mounted path.
256 */
257 if (path->dentry->d_op && path->dentry->d_op->d_dname &&
258 (!IS_ROOT(path->dentry) || path->dentry != path->mnt->mnt_root))
259 return path->dentry->d_op->d_dname(path->dentry, buf, buflen);
260
261 rcu_read_lock();
262 get_fs_root_rcu(current->fs, &root);
Al Viro90243482021-05-17 21:43:01 -0400263 if (unlikely(d_unlinked(path->dentry)))
264 prepend(&res, &buflen, " (deleted)", 11);
265 else
266 prepend(&res, &buflen, "", 1);
267 error = prepend_path(path, &root, &res, &buflen);
Al Viro7a5cf792018-03-05 19:15:50 -0500268 rcu_read_unlock();
269
270 if (error < 0)
271 res = ERR_PTR(error);
272 return res;
273}
274EXPORT_SYMBOL(d_path);
275
276/*
277 * Helper function for dentry_operations.d_dname() members
278 */
279char *dynamic_dname(struct dentry *dentry, char *buffer, int buflen,
280 const char *fmt, ...)
281{
282 va_list args;
283 char temp[64];
284 int sz;
285
286 va_start(args, fmt);
287 sz = vsnprintf(temp, sizeof(temp), fmt, args) + 1;
288 va_end(args);
289
290 if (sz > sizeof(temp) || sz > buflen)
291 return ERR_PTR(-ENAMETOOLONG);
292
293 buffer += buflen - sz;
294 return memcpy(buffer, temp, sz);
295}
296
297char *simple_dname(struct dentry *dentry, char *buffer, int buflen)
298{
299 char *end = buffer + buflen;
300 /* these dentries are never renamed, so d_lock is not needed */
301 if (prepend(&end, &buflen, " (deleted)", 11) ||
302 prepend(&end, &buflen, dentry->d_name.name, dentry->d_name.len) ||
303 prepend(&end, &buflen, "/", 1))
304 end = ERR_PTR(-ENAMETOOLONG);
305 return end;
306}
Al Viro7a5cf792018-03-05 19:15:50 -0500307
308/*
309 * Write full pathname from the root of the filesystem into the buffer.
310 */
Al Viro3a291c92021-05-17 20:16:51 -0400311static char *__dentry_path(const struct dentry *d, char *p, int buflen)
Al Viro7a5cf792018-03-05 19:15:50 -0500312{
Al Viroa2bbe662019-07-07 09:57:53 -0400313 const struct dentry *dentry;
Al Viro3acca042021-05-17 21:19:35 -0400314 char *end;
Al Viro7a5cf792018-03-05 19:15:50 -0500315 int len, seq = 0;
Al Viro7a5cf792018-03-05 19:15:50 -0500316
317 rcu_read_lock();
318restart:
319 dentry = d;
Al Viro3a291c92021-05-17 20:16:51 -0400320 end = p;
Al Viro7a5cf792018-03-05 19:15:50 -0500321 len = buflen;
Al Viro7a5cf792018-03-05 19:15:50 -0500322 read_seqbegin_or_lock(&rename_lock, &seq);
323 while (!IS_ROOT(dentry)) {
Al Viroa2bbe662019-07-07 09:57:53 -0400324 const struct dentry *parent = dentry->d_parent;
Al Viro7a5cf792018-03-05 19:15:50 -0500325
326 prefetch(parent);
Al Viro3acca042021-05-17 21:19:35 -0400327 if (unlikely(prepend_name(&end, &len, &dentry->d_name) < 0))
Al Viro7a5cf792018-03-05 19:15:50 -0500328 break;
329
Al Viro7a5cf792018-03-05 19:15:50 -0500330 dentry = parent;
331 }
332 if (!(seq & 1))
333 rcu_read_unlock();
334 if (need_seqretry(&rename_lock, seq)) {
335 seq = 1;
336 goto restart;
337 }
338 done_seqretry(&rename_lock, seq);
Al Viro3acca042021-05-17 21:19:35 -0400339 if (len == buflen)
340 prepend(&end, &len, "/", 1);
341 return len >= 0 ? end : ERR_PTR(-ENAMETOOLONG);
Al Viro7a5cf792018-03-05 19:15:50 -0500342}
343
Al Viroa2bbe662019-07-07 09:57:53 -0400344char *dentry_path_raw(const struct dentry *dentry, char *buf, int buflen)
Al Viro7a5cf792018-03-05 19:15:50 -0500345{
Al Viro3a291c92021-05-17 20:16:51 -0400346 char *p = buf + buflen;
347 prepend(&p, &buflen, "", 1);
348 return __dentry_path(dentry, p, buflen);
Al Viro7a5cf792018-03-05 19:15:50 -0500349}
350EXPORT_SYMBOL(dentry_path_raw);
351
Al Viroa2bbe662019-07-07 09:57:53 -0400352char *dentry_path(const struct dentry *dentry, char *buf, int buflen)
Al Viro7a5cf792018-03-05 19:15:50 -0500353{
Al Viro3a291c92021-05-17 20:16:51 -0400354 char *p = buf + buflen;
Al Viro7a5cf792018-03-05 19:15:50 -0500355
Al Viro3a291c92021-05-17 20:16:51 -0400356 if (unlikely(d_unlinked(dentry)))
357 prepend(&p, &buflen, "//deleted", 10);
358 else
359 prepend(&p, &buflen, "", 1);
360 return __dentry_path(dentry, p, buflen);
Al Viro7a5cf792018-03-05 19:15:50 -0500361}
362
363static void get_fs_root_and_pwd_rcu(struct fs_struct *fs, struct path *root,
364 struct path *pwd)
365{
366 unsigned seq;
367
368 do {
369 seq = read_seqcount_begin(&fs->seq);
370 *root = fs->root;
371 *pwd = fs->pwd;
372 } while (read_seqcount_retry(&fs->seq, seq));
373}
374
375/*
376 * NOTE! The user-level library version returns a
377 * character pointer. The kernel system call just
378 * returns the length of the buffer filled (which
379 * includes the ending '\0' character), or a negative
380 * error value. So libc would do something like
381 *
382 * char *getcwd(char * buf, size_t size)
383 * {
384 * int retval;
385 *
386 * retval = sys_getcwd(buf, size);
387 * if (retval >= 0)
388 * return buf;
389 * errno = -retval;
390 * return NULL;
391 * }
392 */
393SYSCALL_DEFINE2(getcwd, char __user *, buf, unsigned long, size)
394{
395 int error;
396 struct path pwd, root;
397 char *page = __getname();
398
399 if (!page)
400 return -ENOMEM;
401
402 rcu_read_lock();
403 get_fs_root_and_pwd_rcu(current->fs, &root, &pwd);
404
405 error = -ENOENT;
406 if (!d_unlinked(pwd.dentry)) {
407 unsigned long len;
408 char *cwd = page + PATH_MAX;
409 int buflen = PATH_MAX;
410
Al Virodfe508762021-05-17 21:11:45 -0400411 prepend(&cwd, &buflen, "", 1);
Al Viroa0378fb2021-05-17 21:56:38 -0400412 if (prepend_path(&pwd, &root, &cwd, &buflen) > 0)
413 prepend(&cwd, &buflen, "(unreachable)", 13);
Al Viro7a5cf792018-03-05 19:15:50 -0500414 rcu_read_unlock();
415
Al Viroa0378fb2021-05-17 21:56:38 -0400416 if (buflen < 0) {
417 error = -ENAMETOOLONG;
Al Viro7a5cf792018-03-05 19:15:50 -0500418 goto out;
Al Viro7a5cf792018-03-05 19:15:50 -0500419 }
420
421 error = -ERANGE;
422 len = PATH_MAX + page - cwd;
423 if (len <= size) {
424 error = len;
425 if (copy_to_user(buf, cwd, len))
426 error = -EFAULT;
427 }
428 } else {
429 rcu_read_unlock();
430 }
431
432out:
433 __putname(page);
434 return error;
435}