blob: 06e93dd031bf59787b0eee218948bd0f87cd61f0 [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;
93 struct vfsmount *vfsmnt;
94 struct mount *mnt;
95 int error = 0;
96 unsigned seq, m_seq = 0;
Al Viroad08ae52021-05-12 14:51:03 -040097 struct prepend_buffer b;
Al Viro7a5cf792018-03-05 19:15:50 -050098
99 rcu_read_lock();
100restart_mnt:
101 read_seqbegin_or_lock(&mount_lock, &m_seq);
102 seq = 0;
103 rcu_read_lock();
104restart:
Al Viroad08ae52021-05-12 14:51:03 -0400105 b = *p;
Al Viro7a5cf792018-03-05 19:15:50 -0500106 error = 0;
107 dentry = path->dentry;
108 vfsmnt = path->mnt;
109 mnt = real_mount(vfsmnt);
110 read_seqbegin_or_lock(&rename_lock, &seq);
111 while (dentry != root->dentry || vfsmnt != root->mnt) {
112 struct dentry * parent;
113
114 if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) {
115 struct mount *parent = READ_ONCE(mnt->mnt_parent);
Andrii Nakryiko09cad072020-10-14 13:45:28 -0700116 struct mnt_namespace *mnt_ns;
117
Al Viro7a5cf792018-03-05 19:15:50 -0500118 /* Escaped? */
119 if (dentry != vfsmnt->mnt_root) {
Al Viroad08ae52021-05-12 14:51:03 -0400120 b = *p;
Al Viro7a5cf792018-03-05 19:15:50 -0500121 error = 3;
122 break;
123 }
124 /* Global root? */
125 if (mnt != parent) {
126 dentry = READ_ONCE(mnt->mnt_mountpoint);
127 mnt = parent;
128 vfsmnt = &mnt->mnt;
129 continue;
130 }
Andrii Nakryiko09cad072020-10-14 13:45:28 -0700131 mnt_ns = READ_ONCE(mnt->mnt_ns);
132 /* open-coded is_mounted() to use local mnt_ns */
133 if (!IS_ERR_OR_NULL(mnt_ns) && !is_anon_ns(mnt_ns))
Al Virof2683bd2019-08-30 19:31:09 -0400134 error = 1; // absolute root
135 else
136 error = 2; // detached or not attached yet
Al Viro7a5cf792018-03-05 19:15:50 -0500137 break;
138 }
139 parent = dentry->d_parent;
140 prefetch(parent);
Al Viroad08ae52021-05-12 14:51:03 -0400141 if (!prepend_name(&b, &dentry->d_name))
Al Viro7a5cf792018-03-05 19:15:50 -0500142 break;
143
144 dentry = parent;
145 }
146 if (!(seq & 1))
147 rcu_read_unlock();
148 if (need_seqretry(&rename_lock, seq)) {
149 seq = 1;
150 goto restart;
151 }
152 done_seqretry(&rename_lock, seq);
153
154 if (!(m_seq & 1))
155 rcu_read_unlock();
156 if (need_seqretry(&mount_lock, m_seq)) {
157 m_seq = 1;
158 goto restart_mnt;
159 }
160 done_seqretry(&mount_lock, m_seq);
161
Al Viroad08ae52021-05-12 14:51:03 -0400162 if (b.len == p->len)
163 prepend(&b, "/", 1);
Al Viro01a44282021-05-17 22:29:03 -0400164
Al Viroad08ae52021-05-12 14:51:03 -0400165 *p = b;
Al Viro7a5cf792018-03-05 19:15:50 -0500166 return error;
167}
168
169/**
170 * __d_path - return the path of a dentry
171 * @path: the dentry/vfsmount to report
172 * @root: root vfsmnt/dentry
173 * @buf: buffer to return value in
174 * @buflen: buffer length
175 *
176 * Convert a dentry into an ASCII path name.
177 *
178 * Returns a pointer into the buffer or an error code if the
179 * path was too long.
180 *
181 * "buflen" should be positive.
182 *
183 * If the path is not reachable from the supplied root, return %NULL.
184 */
185char *__d_path(const struct path *path,
186 const struct path *root,
187 char *buf, int buflen)
188{
Al Viroad08ae52021-05-12 14:51:03 -0400189 DECLARE_BUFFER(b, buf, buflen);
Al Viro7a5cf792018-03-05 19:15:50 -0500190
Al Viroad08ae52021-05-12 14:51:03 -0400191 prepend(&b, "", 1);
192 if (prepend_path(path, root, &b) > 0)
Al Viro7a5cf792018-03-05 19:15:50 -0500193 return NULL;
Al Viroad08ae52021-05-12 14:51:03 -0400194 return extract_string(&b);
Al Viro7a5cf792018-03-05 19:15:50 -0500195}
196
197char *d_absolute_path(const struct path *path,
198 char *buf, int buflen)
199{
200 struct path root = {};
Al Viroad08ae52021-05-12 14:51:03 -0400201 DECLARE_BUFFER(b, buf, buflen);
Al Viro7a5cf792018-03-05 19:15:50 -0500202
Al Viroad08ae52021-05-12 14:51:03 -0400203 prepend(&b, "", 1);
204 if (prepend_path(path, &root, &b) > 1)
Al Viro01a44282021-05-17 22:29:03 -0400205 return ERR_PTR(-EINVAL);
Al Viroad08ae52021-05-12 14:51:03 -0400206 return extract_string(&b);
Al Viro7a5cf792018-03-05 19:15:50 -0500207}
208
Al Viro7a5cf792018-03-05 19:15:50 -0500209static void get_fs_root_rcu(struct fs_struct *fs, struct path *root)
210{
211 unsigned seq;
212
213 do {
214 seq = read_seqcount_begin(&fs->seq);
215 *root = fs->root;
216 } while (read_seqcount_retry(&fs->seq, seq));
217}
218
219/**
220 * d_path - return the path of a dentry
221 * @path: path to report
222 * @buf: buffer to return value in
223 * @buflen: buffer length
224 *
225 * Convert a dentry into an ASCII path name. If the entry has been deleted
226 * the string " (deleted)" is appended. Note that this is ambiguous.
227 *
228 * Returns a pointer into the buffer or an error code if the path was
229 * too long. Note: Callers should use the returned pointer, not the passed
230 * in buffer, to use the name! The implementation often starts at an offset
231 * into the buffer, and may leave 0 bytes at the start.
232 *
233 * "buflen" should be positive.
234 */
235char *d_path(const struct path *path, char *buf, int buflen)
236{
Al Viroad08ae52021-05-12 14:51:03 -0400237 DECLARE_BUFFER(b, buf, buflen);
Al Viro7a5cf792018-03-05 19:15:50 -0500238 struct path root;
Al Viro7a5cf792018-03-05 19:15:50 -0500239
240 /*
241 * We have various synthetic filesystems that never get mounted. On
242 * these filesystems dentries are never used for lookup purposes, and
243 * thus don't need to be hashed. They also don't need a name until a
244 * user wants to identify the object in /proc/pid/fd/. The little hack
245 * below allows us to generate a name for these objects on demand:
246 *
247 * Some pseudo inodes are mountable. When they are mounted
248 * path->dentry == path->mnt->mnt_root. In that case don't call d_dname
249 * and instead have d_path return the mounted path.
250 */
251 if (path->dentry->d_op && path->dentry->d_op->d_dname &&
252 (!IS_ROOT(path->dentry) || path->dentry != path->mnt->mnt_root))
253 return path->dentry->d_op->d_dname(path->dentry, buf, buflen);
254
255 rcu_read_lock();
256 get_fs_root_rcu(current->fs, &root);
Al Viro90243482021-05-17 21:43:01 -0400257 if (unlikely(d_unlinked(path->dentry)))
Al Viroad08ae52021-05-12 14:51:03 -0400258 prepend(&b, " (deleted)", 11);
Al Viro90243482021-05-17 21:43:01 -0400259 else
Al Viroad08ae52021-05-12 14:51:03 -0400260 prepend(&b, "", 1);
261 prepend_path(path, &root, &b);
Al Viro7a5cf792018-03-05 19:15:50 -0500262 rcu_read_unlock();
263
Al Viroad08ae52021-05-12 14:51:03 -0400264 return extract_string(&b);
Al Viro7a5cf792018-03-05 19:15:50 -0500265}
266EXPORT_SYMBOL(d_path);
267
268/*
269 * Helper function for dentry_operations.d_dname() members
270 */
271char *dynamic_dname(struct dentry *dentry, char *buffer, int buflen,
272 const char *fmt, ...)
273{
274 va_list args;
275 char temp[64];
276 int sz;
277
278 va_start(args, fmt);
279 sz = vsnprintf(temp, sizeof(temp), fmt, args) + 1;
280 va_end(args);
281
282 if (sz > sizeof(temp) || sz > buflen)
283 return ERR_PTR(-ENAMETOOLONG);
284
285 buffer += buflen - sz;
286 return memcpy(buffer, temp, sz);
287}
288
289char *simple_dname(struct dentry *dentry, char *buffer, int buflen)
290{
Al Viroad08ae52021-05-12 14:51:03 -0400291 DECLARE_BUFFER(b, buffer, buflen);
Al Viro7a5cf792018-03-05 19:15:50 -0500292 /* these dentries are never renamed, so d_lock is not needed */
Al Viroad08ae52021-05-12 14:51:03 -0400293 prepend(&b, " (deleted)", 11);
294 prepend(&b, dentry->d_name.name, dentry->d_name.len);
295 prepend(&b, "/", 1);
296 return extract_string(&b);
Al Viro7a5cf792018-03-05 19:15:50 -0500297}
Al Viro7a5cf792018-03-05 19:15:50 -0500298
299/*
300 * Write full pathname from the root of the filesystem into the buffer.
301 */
Al Viroad08ae52021-05-12 14:51:03 -0400302static char *__dentry_path(const struct dentry *d, struct prepend_buffer *p)
Al Viro7a5cf792018-03-05 19:15:50 -0500303{
Al Viroa2bbe662019-07-07 09:57:53 -0400304 const struct dentry *dentry;
Al Viroad08ae52021-05-12 14:51:03 -0400305 struct prepend_buffer b;
306 int seq = 0;
Al Viro7a5cf792018-03-05 19:15:50 -0500307
308 rcu_read_lock();
309restart:
310 dentry = d;
Al Viroad08ae52021-05-12 14:51:03 -0400311 b = *p;
Al Viro7a5cf792018-03-05 19:15:50 -0500312 read_seqbegin_or_lock(&rename_lock, &seq);
313 while (!IS_ROOT(dentry)) {
Al Viroa2bbe662019-07-07 09:57:53 -0400314 const struct dentry *parent = dentry->d_parent;
Al Viro7a5cf792018-03-05 19:15:50 -0500315
316 prefetch(parent);
Al Viroad08ae52021-05-12 14:51:03 -0400317 if (!prepend_name(&b, &dentry->d_name))
Al Viro7a5cf792018-03-05 19:15:50 -0500318 break;
Al Viro7a5cf792018-03-05 19:15:50 -0500319 dentry = parent;
320 }
321 if (!(seq & 1))
322 rcu_read_unlock();
323 if (need_seqretry(&rename_lock, seq)) {
324 seq = 1;
325 goto restart;
326 }
327 done_seqretry(&rename_lock, seq);
Al Viroad08ae52021-05-12 14:51:03 -0400328 if (b.len == p->len)
329 prepend(&b, "/", 1);
330 return extract_string(&b);
Al Viro7a5cf792018-03-05 19:15:50 -0500331}
332
Al Viroa2bbe662019-07-07 09:57:53 -0400333char *dentry_path_raw(const struct dentry *dentry, char *buf, int buflen)
Al Viro7a5cf792018-03-05 19:15:50 -0500334{
Al Viroad08ae52021-05-12 14:51:03 -0400335 DECLARE_BUFFER(b, buf, buflen);
336
337 prepend(&b, "", 1);
338 return __dentry_path(dentry, &b);
Al Viro7a5cf792018-03-05 19:15:50 -0500339}
340EXPORT_SYMBOL(dentry_path_raw);
341
Al Viroa2bbe662019-07-07 09:57:53 -0400342char *dentry_path(const struct dentry *dentry, char *buf, int buflen)
Al Viro7a5cf792018-03-05 19:15:50 -0500343{
Al Viroad08ae52021-05-12 14:51:03 -0400344 DECLARE_BUFFER(b, buf, buflen);
Al Viro7a5cf792018-03-05 19:15:50 -0500345
Al Viro3a291c92021-05-17 20:16:51 -0400346 if (unlikely(d_unlinked(dentry)))
Al Viroad08ae52021-05-12 14:51:03 -0400347 prepend(&b, "//deleted", 10);
Al Viro3a291c92021-05-17 20:16:51 -0400348 else
Al Viroad08ae52021-05-12 14:51:03 -0400349 prepend(&b, "", 1);
350 return __dentry_path(dentry, &b);
Al Viro7a5cf792018-03-05 19:15:50 -0500351}
352
353static void get_fs_root_and_pwd_rcu(struct fs_struct *fs, struct path *root,
354 struct path *pwd)
355{
356 unsigned seq;
357
358 do {
359 seq = read_seqcount_begin(&fs->seq);
360 *root = fs->root;
361 *pwd = fs->pwd;
362 } while (read_seqcount_retry(&fs->seq, seq));
363}
364
365/*
366 * NOTE! The user-level library version returns a
367 * character pointer. The kernel system call just
368 * returns the length of the buffer filled (which
369 * includes the ending '\0' character), or a negative
370 * error value. So libc would do something like
371 *
372 * char *getcwd(char * buf, size_t size)
373 * {
374 * int retval;
375 *
376 * retval = sys_getcwd(buf, size);
377 * if (retval >= 0)
378 * return buf;
379 * errno = -retval;
380 * return NULL;
381 * }
382 */
383SYSCALL_DEFINE2(getcwd, char __user *, buf, unsigned long, size)
384{
385 int error;
386 struct path pwd, root;
387 char *page = __getname();
388
389 if (!page)
390 return -ENOMEM;
391
392 rcu_read_lock();
393 get_fs_root_and_pwd_rcu(current->fs, &root, &pwd);
394
395 error = -ENOENT;
396 if (!d_unlinked(pwd.dentry)) {
397 unsigned long len;
Al Viroad08ae52021-05-12 14:51:03 -0400398 DECLARE_BUFFER(b, page, PATH_MAX);
Al Viro7a5cf792018-03-05 19:15:50 -0500399
Al Viroad08ae52021-05-12 14:51:03 -0400400 prepend(&b, "", 1);
401 if (prepend_path(&pwd, &root, &b) > 0)
402 prepend(&b, "(unreachable)", 13);
Al Viro7a5cf792018-03-05 19:15:50 -0500403 rcu_read_unlock();
404
Al Viroad08ae52021-05-12 14:51:03 -0400405 if (b.len < 0) {
Al Viroa0378fb2021-05-17 21:56:38 -0400406 error = -ENAMETOOLONG;
Al Viro7a5cf792018-03-05 19:15:50 -0500407 goto out;
Al Viro7a5cf792018-03-05 19:15:50 -0500408 }
409
410 error = -ERANGE;
Al Viroad08ae52021-05-12 14:51:03 -0400411 len = PATH_MAX - b.len;
Al Viro7a5cf792018-03-05 19:15:50 -0500412 if (len <= size) {
413 error = len;
Al Viroad08ae52021-05-12 14:51:03 -0400414 if (copy_to_user(buf, b.buf, len))
Al Viro7a5cf792018-03-05 19:15:50 -0500415 error = -EFAULT;
416 }
417 } else {
418 rcu_read_unlock();
419 }
420
421out:
422 __putname(page);
423 return error;
424}