blob: 72b8087aaf9c3d6cb4a62ed5ef407734a80ca875 [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 Virod8548232021-05-17 22:05:23 -040011static void prepend(char **buffer, int *buflen, const char *str, int namelen)
Al Viro7a5cf792018-03-05 19:15:50 -050012{
13 *buflen -= namelen;
Al Virod8548232021-05-17 22:05:23 -040014 if (likely(*buflen >= 0)) {
15 *buffer -= namelen;
16 memcpy(*buffer, str, namelen);
17 }
Al Viro7a5cf792018-03-05 19:15:50 -050018}
19
20/**
21 * prepend_name - prepend a pathname in front of current buffer pointer
22 * @buffer: buffer pointer
23 * @buflen: allocated length of the buffer
24 * @name: name string and length qstr structure
25 *
26 * With RCU path tracing, it may race with d_move(). Use READ_ONCE() to
27 * make sure that either the old or the new name pointer and length are
28 * fetched. However, there may be mismatch between length and pointer.
29 * The length cannot be trusted, we need to copy it byte-by-byte until
30 * the length is reached or a null byte is found. It also prepends "/" at
31 * the beginning of the name. The sequence number check at the caller will
32 * retry it again when a d_move() does happen. So any garbage in the buffer
33 * due to mismatched pointer and length will be discarded.
34 *
35 * Load acquire is needed to make sure that we see that terminating NUL.
36 */
37static int prepend_name(char **buffer, int *buflen, const struct qstr *name)
38{
39 const char *dname = smp_load_acquire(&name->name); /* ^^^ */
40 u32 dlen = READ_ONCE(name->len);
41 char *p;
42
43 *buflen -= dlen + 1;
44 if (*buflen < 0)
45 return -ENAMETOOLONG;
46 p = *buffer -= dlen + 1;
47 *p++ = '/';
48 while (dlen--) {
49 char c = *dname++;
50 if (!c)
51 break;
52 *p++ = c;
53 }
54 return 0;
55}
56
57/**
58 * prepend_path - Prepend path string to a buffer
59 * @path: the dentry/vfsmount to report
60 * @root: root vfsmnt/dentry
61 * @buffer: pointer to the end of the buffer
62 * @buflen: pointer to buffer length
63 *
64 * The function will first try to write out the pathname without taking any
65 * lock other than the RCU read lock to make sure that dentries won't go away.
66 * It only checks the sequence number of the global rename_lock as any change
67 * in the dentry's d_seq will be preceded by changes in the rename_lock
68 * sequence number. If the sequence number had been changed, it will restart
69 * the whole pathname back-tracing sequence again by taking the rename_lock.
70 * In this case, there is no need to take the RCU read lock as the recursive
71 * parent pointer references will keep the dentry chain alive as long as no
72 * rename operation is performed.
73 */
74static int prepend_path(const struct path *path,
75 const struct path *root,
76 char **buffer, int *buflen)
77{
78 struct dentry *dentry;
79 struct vfsmount *vfsmnt;
80 struct mount *mnt;
81 int error = 0;
82 unsigned seq, m_seq = 0;
83 char *bptr;
84 int blen;
85
86 rcu_read_lock();
87restart_mnt:
88 read_seqbegin_or_lock(&mount_lock, &m_seq);
89 seq = 0;
90 rcu_read_lock();
91restart:
92 bptr = *buffer;
93 blen = *buflen;
94 error = 0;
95 dentry = path->dentry;
96 vfsmnt = path->mnt;
97 mnt = real_mount(vfsmnt);
98 read_seqbegin_or_lock(&rename_lock, &seq);
99 while (dentry != root->dentry || vfsmnt != root->mnt) {
100 struct dentry * parent;
101
102 if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) {
103 struct mount *parent = READ_ONCE(mnt->mnt_parent);
Andrii Nakryiko09cad072020-10-14 13:45:28 -0700104 struct mnt_namespace *mnt_ns;
105
Al Viro7a5cf792018-03-05 19:15:50 -0500106 /* Escaped? */
107 if (dentry != vfsmnt->mnt_root) {
108 bptr = *buffer;
109 blen = *buflen;
110 error = 3;
111 break;
112 }
113 /* Global root? */
114 if (mnt != parent) {
115 dentry = READ_ONCE(mnt->mnt_mountpoint);
116 mnt = parent;
117 vfsmnt = &mnt->mnt;
118 continue;
119 }
Andrii Nakryiko09cad072020-10-14 13:45:28 -0700120 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))
Al Virof2683bd2019-08-30 19:31:09 -0400123 error = 1; // absolute root
124 else
125 error = 2; // detached or not attached yet
Al Viro7a5cf792018-03-05 19:15:50 -0500126 break;
127 }
128 parent = dentry->d_parent;
129 prefetch(parent);
130 error = prepend_name(&bptr, &blen, &dentry->d_name);
131 if (error)
132 break;
133
134 dentry = parent;
135 }
136 if (!(seq & 1))
137 rcu_read_unlock();
138 if (need_seqretry(&rename_lock, seq)) {
139 seq = 1;
140 goto restart;
141 }
142 done_seqretry(&rename_lock, seq);
143
144 if (!(m_seq & 1))
145 rcu_read_unlock();
146 if (need_seqretry(&mount_lock, m_seq)) {
147 m_seq = 1;
148 goto restart_mnt;
149 }
150 done_seqretry(&mount_lock, m_seq);
151
152 if (error >= 0 && bptr == *buffer) {
153 if (--blen < 0)
154 error = -ENAMETOOLONG;
155 else
156 *--bptr = '/';
157 }
158 *buffer = bptr;
159 *buflen = blen;
160 return error;
161}
162
163/**
164 * __d_path - return the path of a dentry
165 * @path: the dentry/vfsmount to report
166 * @root: root vfsmnt/dentry
167 * @buf: buffer to return value in
168 * @buflen: buffer length
169 *
170 * Convert a dentry into an ASCII path name.
171 *
172 * Returns a pointer into the buffer or an error code if the
173 * path was too long.
174 *
175 * "buflen" should be positive.
176 *
177 * If the path is not reachable from the supplied root, return %NULL.
178 */
179char *__d_path(const struct path *path,
180 const struct path *root,
181 char *buf, int buflen)
182{
183 char *res = buf + buflen;
184 int error;
185
Al Virodfe508762021-05-17 21:11:45 -0400186 prepend(&res, &buflen, "", 1);
Al Viro7a5cf792018-03-05 19:15:50 -0500187 error = prepend_path(path, root, &res, &buflen);
188
189 if (error < 0)
190 return ERR_PTR(error);
191 if (error > 0)
192 return NULL;
193 return res;
194}
195
196char *d_absolute_path(const struct path *path,
197 char *buf, int buflen)
198{
199 struct path root = {};
200 char *res = buf + buflen;
201 int error;
202
Al Virodfe508762021-05-17 21:11:45 -0400203 prepend(&res, &buflen, "", 1);
Al Viro7a5cf792018-03-05 19:15:50 -0500204 error = prepend_path(path, &root, &res, &buflen);
205
206 if (error > 1)
207 error = -EINVAL;
208 if (error < 0)
209 return ERR_PTR(error);
210 return res;
211}
212
Al Viro7a5cf792018-03-05 19:15:50 -0500213static void get_fs_root_rcu(struct fs_struct *fs, struct path *root)
214{
215 unsigned seq;
216
217 do {
218 seq = read_seqcount_begin(&fs->seq);
219 *root = fs->root;
220 } while (read_seqcount_retry(&fs->seq, seq));
221}
222
223/**
224 * d_path - return the path of a dentry
225 * @path: path to report
226 * @buf: buffer to return value in
227 * @buflen: buffer length
228 *
229 * Convert a dentry into an ASCII path name. If the entry has been deleted
230 * the string " (deleted)" is appended. Note that this is ambiguous.
231 *
232 * Returns a pointer into the buffer or an error code if the path was
233 * too long. Note: Callers should use the returned pointer, not the passed
234 * in buffer, to use the name! The implementation often starts at an offset
235 * into the buffer, and may leave 0 bytes at the start.
236 *
237 * "buflen" should be positive.
238 */
239char *d_path(const struct path *path, char *buf, int buflen)
240{
241 char *res = buf + buflen;
242 struct path root;
243 int error;
244
245 /*
246 * We have various synthetic filesystems that never get mounted. On
247 * these filesystems dentries are never used for lookup purposes, and
248 * thus don't need to be hashed. They also don't need a name until a
249 * user wants to identify the object in /proc/pid/fd/. The little hack
250 * below allows us to generate a name for these objects on demand:
251 *
252 * Some pseudo inodes are mountable. When they are mounted
253 * path->dentry == path->mnt->mnt_root. In that case don't call d_dname
254 * and instead have d_path return the mounted path.
255 */
256 if (path->dentry->d_op && path->dentry->d_op->d_dname &&
257 (!IS_ROOT(path->dentry) || path->dentry != path->mnt->mnt_root))
258 return path->dentry->d_op->d_dname(path->dentry, buf, buflen);
259
260 rcu_read_lock();
261 get_fs_root_rcu(current->fs, &root);
Al Viro90243482021-05-17 21:43:01 -0400262 if (unlikely(d_unlinked(path->dentry)))
263 prepend(&res, &buflen, " (deleted)", 11);
264 else
265 prepend(&res, &buflen, "", 1);
266 error = prepend_path(path, &root, &res, &buflen);
Al Viro7a5cf792018-03-05 19:15:50 -0500267 rcu_read_unlock();
268
269 if (error < 0)
270 res = ERR_PTR(error);
271 return res;
272}
273EXPORT_SYMBOL(d_path);
274
275/*
276 * Helper function for dentry_operations.d_dname() members
277 */
278char *dynamic_dname(struct dentry *dentry, char *buffer, int buflen,
279 const char *fmt, ...)
280{
281 va_list args;
282 char temp[64];
283 int sz;
284
285 va_start(args, fmt);
286 sz = vsnprintf(temp, sizeof(temp), fmt, args) + 1;
287 va_end(args);
288
289 if (sz > sizeof(temp) || sz > buflen)
290 return ERR_PTR(-ENAMETOOLONG);
291
292 buffer += buflen - sz;
293 return memcpy(buffer, temp, sz);
294}
295
296char *simple_dname(struct dentry *dentry, char *buffer, int buflen)
297{
298 char *end = buffer + buflen;
299 /* these dentries are never renamed, so d_lock is not needed */
Al Virod8548232021-05-17 22:05:23 -0400300 prepend(&end, &buflen, " (deleted)", 11);
301 prepend(&end, &buflen, dentry->d_name.name, dentry->d_name.len);
302 prepend(&end, &buflen, "/", 1);
303 return buflen >= 0 ? end : ERR_PTR(-ENAMETOOLONG);
Al Viro7a5cf792018-03-05 19:15:50 -0500304}
Al Viro7a5cf792018-03-05 19:15:50 -0500305
306/*
307 * Write full pathname from the root of the filesystem into the buffer.
308 */
Al Viro3a291c92021-05-17 20:16:51 -0400309static char *__dentry_path(const struct dentry *d, char *p, int buflen)
Al Viro7a5cf792018-03-05 19:15:50 -0500310{
Al Viroa2bbe662019-07-07 09:57:53 -0400311 const struct dentry *dentry;
Al Viro3acca042021-05-17 21:19:35 -0400312 char *end;
Al Viro7a5cf792018-03-05 19:15:50 -0500313 int len, seq = 0;
Al Viro7a5cf792018-03-05 19:15:50 -0500314
315 rcu_read_lock();
316restart:
317 dentry = d;
Al Viro3a291c92021-05-17 20:16:51 -0400318 end = p;
Al Viro7a5cf792018-03-05 19:15:50 -0500319 len = buflen;
Al Viro7a5cf792018-03-05 19:15:50 -0500320 read_seqbegin_or_lock(&rename_lock, &seq);
321 while (!IS_ROOT(dentry)) {
Al Viroa2bbe662019-07-07 09:57:53 -0400322 const struct dentry *parent = dentry->d_parent;
Al Viro7a5cf792018-03-05 19:15:50 -0500323
324 prefetch(parent);
Al Viro3acca042021-05-17 21:19:35 -0400325 if (unlikely(prepend_name(&end, &len, &dentry->d_name) < 0))
Al Viro7a5cf792018-03-05 19:15:50 -0500326 break;
327
Al Viro7a5cf792018-03-05 19:15:50 -0500328 dentry = parent;
329 }
330 if (!(seq & 1))
331 rcu_read_unlock();
332 if (need_seqretry(&rename_lock, seq)) {
333 seq = 1;
334 goto restart;
335 }
336 done_seqretry(&rename_lock, seq);
Al Viro3acca042021-05-17 21:19:35 -0400337 if (len == buflen)
338 prepend(&end, &len, "/", 1);
339 return len >= 0 ? end : ERR_PTR(-ENAMETOOLONG);
Al Viro7a5cf792018-03-05 19:15:50 -0500340}
341
Al Viroa2bbe662019-07-07 09:57:53 -0400342char *dentry_path_raw(const struct dentry *dentry, char *buf, int buflen)
Al Viro7a5cf792018-03-05 19:15:50 -0500343{
Al Viro3a291c92021-05-17 20:16:51 -0400344 char *p = buf + buflen;
345 prepend(&p, &buflen, "", 1);
346 return __dentry_path(dentry, p, buflen);
Al Viro7a5cf792018-03-05 19:15:50 -0500347}
348EXPORT_SYMBOL(dentry_path_raw);
349
Al Viroa2bbe662019-07-07 09:57:53 -0400350char *dentry_path(const struct dentry *dentry, char *buf, int buflen)
Al Viro7a5cf792018-03-05 19:15:50 -0500351{
Al Viro3a291c92021-05-17 20:16:51 -0400352 char *p = buf + buflen;
Al Viro7a5cf792018-03-05 19:15:50 -0500353
Al Viro3a291c92021-05-17 20:16:51 -0400354 if (unlikely(d_unlinked(dentry)))
355 prepend(&p, &buflen, "//deleted", 10);
356 else
357 prepend(&p, &buflen, "", 1);
358 return __dentry_path(dentry, p, buflen);
Al Viro7a5cf792018-03-05 19:15:50 -0500359}
360
361static void get_fs_root_and_pwd_rcu(struct fs_struct *fs, struct path *root,
362 struct path *pwd)
363{
364 unsigned seq;
365
366 do {
367 seq = read_seqcount_begin(&fs->seq);
368 *root = fs->root;
369 *pwd = fs->pwd;
370 } while (read_seqcount_retry(&fs->seq, seq));
371}
372
373/*
374 * NOTE! The user-level library version returns a
375 * character pointer. The kernel system call just
376 * returns the length of the buffer filled (which
377 * includes the ending '\0' character), or a negative
378 * error value. So libc would do something like
379 *
380 * char *getcwd(char * buf, size_t size)
381 * {
382 * int retval;
383 *
384 * retval = sys_getcwd(buf, size);
385 * if (retval >= 0)
386 * return buf;
387 * errno = -retval;
388 * return NULL;
389 * }
390 */
391SYSCALL_DEFINE2(getcwd, char __user *, buf, unsigned long, size)
392{
393 int error;
394 struct path pwd, root;
395 char *page = __getname();
396
397 if (!page)
398 return -ENOMEM;
399
400 rcu_read_lock();
401 get_fs_root_and_pwd_rcu(current->fs, &root, &pwd);
402
403 error = -ENOENT;
404 if (!d_unlinked(pwd.dentry)) {
405 unsigned long len;
406 char *cwd = page + PATH_MAX;
407 int buflen = PATH_MAX;
408
Al Virodfe508762021-05-17 21:11:45 -0400409 prepend(&cwd, &buflen, "", 1);
Al Viroa0378fb2021-05-17 21:56:38 -0400410 if (prepend_path(&pwd, &root, &cwd, &buflen) > 0)
411 prepend(&cwd, &buflen, "(unreachable)", 13);
Al Viro7a5cf792018-03-05 19:15:50 -0500412 rcu_read_unlock();
413
Al Viroa0378fb2021-05-17 21:56:38 -0400414 if (buflen < 0) {
415 error = -ENAMETOOLONG;
Al Viro7a5cf792018-03-05 19:15:50 -0500416 goto out;
Al Viro7a5cf792018-03-05 19:15:50 -0500417 }
418
419 error = -ERANGE;
420 len = PATH_MAX + page - cwd;
421 if (len <= size) {
422 error = len;
423 if (copy_to_user(buf, cwd, len))
424 error = -EFAULT;
425 }
426 } else {
427 rcu_read_unlock();
428 }
429
430out:
431 __putname(page);
432 return error;
433}