blob: 9a0356cc98d3578ddcbb3cea5f65d8654585b9ba [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 Viro7a5cf792018-03-05 19:15:50 -0500118 error = 3;
119 break;
120 }
121 /* Global root? */
122 if (mnt != parent) {
123 dentry = READ_ONCE(mnt->mnt_mountpoint);
124 mnt = parent;
Al Viro7a5cf792018-03-05 19:15:50 -0500125 continue;
126 }
Andrii Nakryiko09cad072020-10-14 13:45:28 -0700127 mnt_ns = READ_ONCE(mnt->mnt_ns);
128 /* open-coded is_mounted() to use local mnt_ns */
129 if (!IS_ERR_OR_NULL(mnt_ns) && !is_anon_ns(mnt_ns))
Al Virof2683bd2019-08-30 19:31:09 -0400130 error = 1; // absolute root
131 else
132 error = 2; // detached or not attached yet
Al Viro7a5cf792018-03-05 19:15:50 -0500133 break;
134 }
135 parent = dentry->d_parent;
136 prefetch(parent);
Al Viroad08ae52021-05-12 14:51:03 -0400137 if (!prepend_name(&b, &dentry->d_name))
Al Viro7a5cf792018-03-05 19:15:50 -0500138 break;
139
140 dentry = parent;
141 }
142 if (!(seq & 1))
143 rcu_read_unlock();
144 if (need_seqretry(&rename_lock, seq)) {
145 seq = 1;
146 goto restart;
147 }
148 done_seqretry(&rename_lock, seq);
149
150 if (!(m_seq & 1))
151 rcu_read_unlock();
152 if (need_seqretry(&mount_lock, m_seq)) {
153 m_seq = 1;
154 goto restart_mnt;
155 }
156 done_seqretry(&mount_lock, m_seq);
157
Al Viro2dac0ad2021-05-12 16:21:43 -0400158 if (unlikely(error == 3))
159 b = *p;
160
Al Viroad08ae52021-05-12 14:51:03 -0400161 if (b.len == p->len)
162 prepend(&b, "/", 1);
Al Viro01a44282021-05-17 22:29:03 -0400163
Al Viroad08ae52021-05-12 14:51:03 -0400164 *p = b;
Al Viro7a5cf792018-03-05 19:15:50 -0500165 return error;
166}
167
168/**
169 * __d_path - return the path of a dentry
170 * @path: the dentry/vfsmount to report
171 * @root: root vfsmnt/dentry
172 * @buf: buffer to return value in
173 * @buflen: buffer length
174 *
175 * Convert a dentry into an ASCII path name.
176 *
177 * Returns a pointer into the buffer or an error code if the
178 * path was too long.
179 *
180 * "buflen" should be positive.
181 *
182 * If the path is not reachable from the supplied root, return %NULL.
183 */
184char *__d_path(const struct path *path,
185 const struct path *root,
186 char *buf, int buflen)
187{
Al Viroad08ae52021-05-12 14:51:03 -0400188 DECLARE_BUFFER(b, buf, buflen);
Al Viro7a5cf792018-03-05 19:15:50 -0500189
Al Viroad08ae52021-05-12 14:51:03 -0400190 prepend(&b, "", 1);
191 if (prepend_path(path, root, &b) > 0)
Al Viro7a5cf792018-03-05 19:15:50 -0500192 return NULL;
Al Viroad08ae52021-05-12 14:51:03 -0400193 return extract_string(&b);
Al Viro7a5cf792018-03-05 19:15:50 -0500194}
195
196char *d_absolute_path(const struct path *path,
197 char *buf, int buflen)
198{
199 struct path root = {};
Al Viroad08ae52021-05-12 14:51:03 -0400200 DECLARE_BUFFER(b, buf, buflen);
Al Viro7a5cf792018-03-05 19:15:50 -0500201
Al Viroad08ae52021-05-12 14:51:03 -0400202 prepend(&b, "", 1);
203 if (prepend_path(path, &root, &b) > 1)
Al Viro01a44282021-05-17 22:29:03 -0400204 return ERR_PTR(-EINVAL);
Al Viroad08ae52021-05-12 14:51:03 -0400205 return extract_string(&b);
Al Viro7a5cf792018-03-05 19:15:50 -0500206}
207
Al Viro7a5cf792018-03-05 19:15:50 -0500208static void get_fs_root_rcu(struct fs_struct *fs, struct path *root)
209{
210 unsigned seq;
211
212 do {
213 seq = read_seqcount_begin(&fs->seq);
214 *root = fs->root;
215 } while (read_seqcount_retry(&fs->seq, seq));
216}
217
218/**
219 * d_path - return the path of a dentry
220 * @path: path to report
221 * @buf: buffer to return value in
222 * @buflen: buffer length
223 *
224 * Convert a dentry into an ASCII path name. If the entry has been deleted
225 * the string " (deleted)" is appended. Note that this is ambiguous.
226 *
227 * Returns a pointer into the buffer or an error code if the path was
228 * too long. Note: Callers should use the returned pointer, not the passed
229 * in buffer, to use the name! The implementation often starts at an offset
230 * into the buffer, and may leave 0 bytes at the start.
231 *
232 * "buflen" should be positive.
233 */
234char *d_path(const struct path *path, char *buf, int buflen)
235{
Al Viroad08ae52021-05-12 14:51:03 -0400236 DECLARE_BUFFER(b, buf, buflen);
Al Viro7a5cf792018-03-05 19:15:50 -0500237 struct path root;
Al Viro7a5cf792018-03-05 19:15:50 -0500238
239 /*
240 * We have various synthetic filesystems that never get mounted. On
241 * these filesystems dentries are never used for lookup purposes, and
242 * thus don't need to be hashed. They also don't need a name until a
243 * user wants to identify the object in /proc/pid/fd/. The little hack
244 * below allows us to generate a name for these objects on demand:
245 *
246 * Some pseudo inodes are mountable. When they are mounted
247 * path->dentry == path->mnt->mnt_root. In that case don't call d_dname
248 * and instead have d_path return the mounted path.
249 */
250 if (path->dentry->d_op && path->dentry->d_op->d_dname &&
251 (!IS_ROOT(path->dentry) || path->dentry != path->mnt->mnt_root))
252 return path->dentry->d_op->d_dname(path->dentry, buf, buflen);
253
254 rcu_read_lock();
255 get_fs_root_rcu(current->fs, &root);
Al Viro90243482021-05-17 21:43:01 -0400256 if (unlikely(d_unlinked(path->dentry)))
Al Viroad08ae52021-05-12 14:51:03 -0400257 prepend(&b, " (deleted)", 11);
Al Viro90243482021-05-17 21:43:01 -0400258 else
Al Viroad08ae52021-05-12 14:51:03 -0400259 prepend(&b, "", 1);
260 prepend_path(path, &root, &b);
Al Viro7a5cf792018-03-05 19:15:50 -0500261 rcu_read_unlock();
262
Al Viroad08ae52021-05-12 14:51:03 -0400263 return extract_string(&b);
Al Viro7a5cf792018-03-05 19:15:50 -0500264}
265EXPORT_SYMBOL(d_path);
266
267/*
268 * Helper function for dentry_operations.d_dname() members
269 */
270char *dynamic_dname(struct dentry *dentry, char *buffer, int buflen,
271 const char *fmt, ...)
272{
273 va_list args;
274 char temp[64];
275 int sz;
276
277 va_start(args, fmt);
278 sz = vsnprintf(temp, sizeof(temp), fmt, args) + 1;
279 va_end(args);
280
281 if (sz > sizeof(temp) || sz > buflen)
282 return ERR_PTR(-ENAMETOOLONG);
283
284 buffer += buflen - sz;
285 return memcpy(buffer, temp, sz);
286}
287
288char *simple_dname(struct dentry *dentry, char *buffer, int buflen)
289{
Al Viroad08ae52021-05-12 14:51:03 -0400290 DECLARE_BUFFER(b, buffer, buflen);
Al Viro7a5cf792018-03-05 19:15:50 -0500291 /* these dentries are never renamed, so d_lock is not needed */
Al Viroad08ae52021-05-12 14:51:03 -0400292 prepend(&b, " (deleted)", 11);
293 prepend(&b, dentry->d_name.name, dentry->d_name.len);
294 prepend(&b, "/", 1);
295 return extract_string(&b);
Al Viro7a5cf792018-03-05 19:15:50 -0500296}
Al Viro7a5cf792018-03-05 19:15:50 -0500297
298/*
299 * Write full pathname from the root of the filesystem into the buffer.
300 */
Al Viroad08ae52021-05-12 14:51:03 -0400301static char *__dentry_path(const struct dentry *d, struct prepend_buffer *p)
Al Viro7a5cf792018-03-05 19:15:50 -0500302{
Al Viroa2bbe662019-07-07 09:57:53 -0400303 const struct dentry *dentry;
Al Viroad08ae52021-05-12 14:51:03 -0400304 struct prepend_buffer b;
305 int seq = 0;
Al Viro7a5cf792018-03-05 19:15:50 -0500306
307 rcu_read_lock();
308restart:
309 dentry = d;
Al Viroad08ae52021-05-12 14:51:03 -0400310 b = *p;
Al Viro7a5cf792018-03-05 19:15:50 -0500311 read_seqbegin_or_lock(&rename_lock, &seq);
312 while (!IS_ROOT(dentry)) {
Al Viroa2bbe662019-07-07 09:57:53 -0400313 const struct dentry *parent = dentry->d_parent;
Al Viro7a5cf792018-03-05 19:15:50 -0500314
315 prefetch(parent);
Al Viroad08ae52021-05-12 14:51:03 -0400316 if (!prepend_name(&b, &dentry->d_name))
Al Viro7a5cf792018-03-05 19:15:50 -0500317 break;
Al Viro7a5cf792018-03-05 19:15:50 -0500318 dentry = parent;
319 }
320 if (!(seq & 1))
321 rcu_read_unlock();
322 if (need_seqretry(&rename_lock, seq)) {
323 seq = 1;
324 goto restart;
325 }
326 done_seqretry(&rename_lock, seq);
Al Viroad08ae52021-05-12 14:51:03 -0400327 if (b.len == p->len)
328 prepend(&b, "/", 1);
329 return extract_string(&b);
Al Viro7a5cf792018-03-05 19:15:50 -0500330}
331
Al Viroa2bbe662019-07-07 09:57:53 -0400332char *dentry_path_raw(const struct dentry *dentry, char *buf, int buflen)
Al Viro7a5cf792018-03-05 19:15:50 -0500333{
Al Viroad08ae52021-05-12 14:51:03 -0400334 DECLARE_BUFFER(b, buf, buflen);
335
336 prepend(&b, "", 1);
337 return __dentry_path(dentry, &b);
Al Viro7a5cf792018-03-05 19:15:50 -0500338}
339EXPORT_SYMBOL(dentry_path_raw);
340
Al Viroa2bbe662019-07-07 09:57:53 -0400341char *dentry_path(const struct dentry *dentry, char *buf, int buflen)
Al Viro7a5cf792018-03-05 19:15:50 -0500342{
Al Viroad08ae52021-05-12 14:51:03 -0400343 DECLARE_BUFFER(b, buf, buflen);
Al Viro7a5cf792018-03-05 19:15:50 -0500344
Al Viro3a291c92021-05-17 20:16:51 -0400345 if (unlikely(d_unlinked(dentry)))
Al Viroad08ae52021-05-12 14:51:03 -0400346 prepend(&b, "//deleted", 10);
Al Viro3a291c92021-05-17 20:16:51 -0400347 else
Al Viroad08ae52021-05-12 14:51:03 -0400348 prepend(&b, "", 1);
349 return __dentry_path(dentry, &b);
Al Viro7a5cf792018-03-05 19:15:50 -0500350}
351
352static void get_fs_root_and_pwd_rcu(struct fs_struct *fs, struct path *root,
353 struct path *pwd)
354{
355 unsigned seq;
356
357 do {
358 seq = read_seqcount_begin(&fs->seq);
359 *root = fs->root;
360 *pwd = fs->pwd;
361 } while (read_seqcount_retry(&fs->seq, seq));
362}
363
364/*
365 * NOTE! The user-level library version returns a
366 * character pointer. The kernel system call just
367 * returns the length of the buffer filled (which
368 * includes the ending '\0' character), or a negative
369 * error value. So libc would do something like
370 *
371 * char *getcwd(char * buf, size_t size)
372 * {
373 * int retval;
374 *
375 * retval = sys_getcwd(buf, size);
376 * if (retval >= 0)
377 * return buf;
378 * errno = -retval;
379 * return NULL;
380 * }
381 */
382SYSCALL_DEFINE2(getcwd, char __user *, buf, unsigned long, size)
383{
384 int error;
385 struct path pwd, root;
386 char *page = __getname();
387
388 if (!page)
389 return -ENOMEM;
390
391 rcu_read_lock();
392 get_fs_root_and_pwd_rcu(current->fs, &root, &pwd);
393
394 error = -ENOENT;
395 if (!d_unlinked(pwd.dentry)) {
396 unsigned long len;
Al Viroad08ae52021-05-12 14:51:03 -0400397 DECLARE_BUFFER(b, page, PATH_MAX);
Al Viro7a5cf792018-03-05 19:15:50 -0500398
Al Viroad08ae52021-05-12 14:51:03 -0400399 prepend(&b, "", 1);
400 if (prepend_path(&pwd, &root, &b) > 0)
401 prepend(&b, "(unreachable)", 13);
Al Viro7a5cf792018-03-05 19:15:50 -0500402 rcu_read_unlock();
403
Al Viroad08ae52021-05-12 14:51:03 -0400404 if (b.len < 0) {
Al Viroa0378fb2021-05-17 21:56:38 -0400405 error = -ENAMETOOLONG;
Al Viro7a5cf792018-03-05 19:15:50 -0500406 goto out;
Al Viro7a5cf792018-03-05 19:15:50 -0500407 }
408
409 error = -ERANGE;
Al Viroad08ae52021-05-12 14:51:03 -0400410 len = PATH_MAX - b.len;
Al Viro7a5cf792018-03-05 19:15:50 -0500411 if (len <= size) {
412 error = len;
Al Viroad08ae52021-05-12 14:51:03 -0400413 if (copy_to_user(buf, b.buf, len))
Al Viro7a5cf792018-03-05 19:15:50 -0500414 error = -EFAULT;
415 }
416 } else {
417 rcu_read_unlock();
418 }
419
420out:
421 __putname(page);
422 return error;
423}