0001
0002 #include <linux/syscalls.h>
0003 #include <linux/export.h>
0004 #include <linux/uaccess.h>
0005 #include <linux/fs_struct.h>
0006 #include <linux/fs.h>
0007 #include <linux/slab.h>
0008 #include <linux/prefetch.h>
0009 #include "mount.h"
0010
0011 struct prepend_buffer {
0012 char *buf;
0013 int len;
0014 };
0015 #define DECLARE_BUFFER(__name, __buf, __len) \
0016 struct prepend_buffer __name = {.buf = __buf + __len, .len = __len}
0017
0018 static char *extract_string(struct prepend_buffer *p)
0019 {
0020 if (likely(p->len >= 0))
0021 return p->buf;
0022 return ERR_PTR(-ENAMETOOLONG);
0023 }
0024
0025 static bool prepend_char(struct prepend_buffer *p, unsigned char c)
0026 {
0027 if (likely(p->len > 0)) {
0028 p->len--;
0029 *--p->buf = c;
0030 return true;
0031 }
0032 p->len = -1;
0033 return false;
0034 }
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047 static bool prepend_copy(void *dst, const void *src, int len)
0048 {
0049 if (unlikely(copy_from_kernel_nofault(dst, src, len))) {
0050 memset(dst, 'x', len);
0051 return false;
0052 }
0053 return true;
0054 }
0055
0056 static bool prepend(struct prepend_buffer *p, const char *str, int namelen)
0057 {
0058
0059 if (p->len < 0)
0060 return false;
0061
0062
0063 if (p->len < namelen) {
0064
0065 str += namelen - p->len;
0066 p->buf -= p->len;
0067 prepend_copy(p->buf, str, p->len);
0068 p->len = -1;
0069 return false;
0070 }
0071
0072
0073 p->len -= namelen;
0074 p->buf -= namelen;
0075 return prepend_copy(p->buf, str, namelen);
0076 }
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095 static bool prepend_name(struct prepend_buffer *p, const struct qstr *name)
0096 {
0097 const char *dname = smp_load_acquire(&name->name);
0098 u32 dlen = READ_ONCE(name->len);
0099
0100 return prepend(p, dname, dlen) && prepend_char(p, '/');
0101 }
0102
0103 static int __prepend_path(const struct dentry *dentry, const struct mount *mnt,
0104 const struct path *root, struct prepend_buffer *p)
0105 {
0106 while (dentry != root->dentry || &mnt->mnt != root->mnt) {
0107 const struct dentry *parent = READ_ONCE(dentry->d_parent);
0108
0109 if (dentry == mnt->mnt.mnt_root) {
0110 struct mount *m = READ_ONCE(mnt->mnt_parent);
0111 struct mnt_namespace *mnt_ns;
0112
0113 if (likely(mnt != m)) {
0114 dentry = READ_ONCE(mnt->mnt_mountpoint);
0115 mnt = m;
0116 continue;
0117 }
0118
0119 mnt_ns = READ_ONCE(mnt->mnt_ns);
0120
0121 if (!IS_ERR_OR_NULL(mnt_ns) && !is_anon_ns(mnt_ns))
0122 return 1;
0123 else
0124 return 2;
0125 }
0126
0127 if (unlikely(dentry == parent))
0128
0129 return 3;
0130
0131 prefetch(parent);
0132 if (!prepend_name(p, &dentry->d_name))
0133 break;
0134 dentry = parent;
0135 }
0136 return 0;
0137 }
0138
0139
0140
0141
0142
0143
0144
0145
0146
0147
0148
0149
0150
0151
0152
0153
0154
0155 static int prepend_path(const struct path *path,
0156 const struct path *root,
0157 struct prepend_buffer *p)
0158 {
0159 unsigned seq, m_seq = 0;
0160 struct prepend_buffer b;
0161 int error;
0162
0163 rcu_read_lock();
0164 restart_mnt:
0165 read_seqbegin_or_lock(&mount_lock, &m_seq);
0166 seq = 0;
0167 rcu_read_lock();
0168 restart:
0169 b = *p;
0170 read_seqbegin_or_lock(&rename_lock, &seq);
0171 error = __prepend_path(path->dentry, real_mount(path->mnt), root, &b);
0172 if (!(seq & 1))
0173 rcu_read_unlock();
0174 if (need_seqretry(&rename_lock, seq)) {
0175 seq = 1;
0176 goto restart;
0177 }
0178 done_seqretry(&rename_lock, seq);
0179
0180 if (!(m_seq & 1))
0181 rcu_read_unlock();
0182 if (need_seqretry(&mount_lock, m_seq)) {
0183 m_seq = 1;
0184 goto restart_mnt;
0185 }
0186 done_seqretry(&mount_lock, m_seq);
0187
0188 if (unlikely(error == 3))
0189 b = *p;
0190
0191 if (b.len == p->len)
0192 prepend_char(&b, '/');
0193
0194 *p = b;
0195 return error;
0196 }
0197
0198
0199
0200
0201
0202
0203
0204
0205
0206
0207
0208
0209
0210
0211
0212
0213
0214 char *__d_path(const struct path *path,
0215 const struct path *root,
0216 char *buf, int buflen)
0217 {
0218 DECLARE_BUFFER(b, buf, buflen);
0219
0220 prepend_char(&b, 0);
0221 if (unlikely(prepend_path(path, root, &b) > 0))
0222 return NULL;
0223 return extract_string(&b);
0224 }
0225
0226 char *d_absolute_path(const struct path *path,
0227 char *buf, int buflen)
0228 {
0229 struct path root = {};
0230 DECLARE_BUFFER(b, buf, buflen);
0231
0232 prepend_char(&b, 0);
0233 if (unlikely(prepend_path(path, &root, &b) > 1))
0234 return ERR_PTR(-EINVAL);
0235 return extract_string(&b);
0236 }
0237
0238 static void get_fs_root_rcu(struct fs_struct *fs, struct path *root)
0239 {
0240 unsigned seq;
0241
0242 do {
0243 seq = read_seqcount_begin(&fs->seq);
0244 *root = fs->root;
0245 } while (read_seqcount_retry(&fs->seq, seq));
0246 }
0247
0248
0249
0250
0251
0252
0253
0254
0255
0256
0257
0258
0259
0260
0261
0262
0263
0264 char *d_path(const struct path *path, char *buf, int buflen)
0265 {
0266 DECLARE_BUFFER(b, buf, buflen);
0267 struct path root;
0268
0269
0270
0271
0272
0273
0274
0275
0276
0277
0278
0279
0280 if (path->dentry->d_op && path->dentry->d_op->d_dname &&
0281 (!IS_ROOT(path->dentry) || path->dentry != path->mnt->mnt_root))
0282 return path->dentry->d_op->d_dname(path->dentry, buf, buflen);
0283
0284 rcu_read_lock();
0285 get_fs_root_rcu(current->fs, &root);
0286 if (unlikely(d_unlinked(path->dentry)))
0287 prepend(&b, " (deleted)", 11);
0288 else
0289 prepend_char(&b, 0);
0290 prepend_path(path, &root, &b);
0291 rcu_read_unlock();
0292
0293 return extract_string(&b);
0294 }
0295 EXPORT_SYMBOL(d_path);
0296
0297
0298
0299
0300 char *dynamic_dname(struct dentry *dentry, char *buffer, int buflen,
0301 const char *fmt, ...)
0302 {
0303 va_list args;
0304 char temp[64];
0305 int sz;
0306
0307 va_start(args, fmt);
0308 sz = vsnprintf(temp, sizeof(temp), fmt, args) + 1;
0309 va_end(args);
0310
0311 if (sz > sizeof(temp) || sz > buflen)
0312 return ERR_PTR(-ENAMETOOLONG);
0313
0314 buffer += buflen - sz;
0315 return memcpy(buffer, temp, sz);
0316 }
0317
0318 char *simple_dname(struct dentry *dentry, char *buffer, int buflen)
0319 {
0320 DECLARE_BUFFER(b, buffer, buflen);
0321
0322 prepend(&b, " (deleted)", 11);
0323 prepend(&b, dentry->d_name.name, dentry->d_name.len);
0324 prepend_char(&b, '/');
0325 return extract_string(&b);
0326 }
0327
0328
0329
0330
0331 static char *__dentry_path(const struct dentry *d, struct prepend_buffer *p)
0332 {
0333 const struct dentry *dentry;
0334 struct prepend_buffer b;
0335 int seq = 0;
0336
0337 rcu_read_lock();
0338 restart:
0339 dentry = d;
0340 b = *p;
0341 read_seqbegin_or_lock(&rename_lock, &seq);
0342 while (!IS_ROOT(dentry)) {
0343 const struct dentry *parent = dentry->d_parent;
0344
0345 prefetch(parent);
0346 if (!prepend_name(&b, &dentry->d_name))
0347 break;
0348 dentry = parent;
0349 }
0350 if (!(seq & 1))
0351 rcu_read_unlock();
0352 if (need_seqretry(&rename_lock, seq)) {
0353 seq = 1;
0354 goto restart;
0355 }
0356 done_seqretry(&rename_lock, seq);
0357 if (b.len == p->len)
0358 prepend_char(&b, '/');
0359 return extract_string(&b);
0360 }
0361
0362 char *dentry_path_raw(const struct dentry *dentry, char *buf, int buflen)
0363 {
0364 DECLARE_BUFFER(b, buf, buflen);
0365
0366 prepend_char(&b, 0);
0367 return __dentry_path(dentry, &b);
0368 }
0369 EXPORT_SYMBOL(dentry_path_raw);
0370
0371 char *dentry_path(const struct dentry *dentry, char *buf, int buflen)
0372 {
0373 DECLARE_BUFFER(b, buf, buflen);
0374
0375 if (unlikely(d_unlinked(dentry)))
0376 prepend(&b, "//deleted", 10);
0377 else
0378 prepend_char(&b, 0);
0379 return __dentry_path(dentry, &b);
0380 }
0381
0382 static void get_fs_root_and_pwd_rcu(struct fs_struct *fs, struct path *root,
0383 struct path *pwd)
0384 {
0385 unsigned seq;
0386
0387 do {
0388 seq = read_seqcount_begin(&fs->seq);
0389 *root = fs->root;
0390 *pwd = fs->pwd;
0391 } while (read_seqcount_retry(&fs->seq, seq));
0392 }
0393
0394
0395
0396
0397
0398
0399
0400
0401
0402
0403
0404
0405
0406
0407
0408
0409
0410
0411
0412 SYSCALL_DEFINE2(getcwd, char __user *, buf, unsigned long, size)
0413 {
0414 int error;
0415 struct path pwd, root;
0416 char *page = __getname();
0417
0418 if (!page)
0419 return -ENOMEM;
0420
0421 rcu_read_lock();
0422 get_fs_root_and_pwd_rcu(current->fs, &root, &pwd);
0423
0424 if (unlikely(d_unlinked(pwd.dentry))) {
0425 rcu_read_unlock();
0426 error = -ENOENT;
0427 } else {
0428 unsigned len;
0429 DECLARE_BUFFER(b, page, PATH_MAX);
0430
0431 prepend_char(&b, 0);
0432 if (unlikely(prepend_path(&pwd, &root, &b) > 0))
0433 prepend(&b, "(unreachable)", 13);
0434 rcu_read_unlock();
0435
0436 len = PATH_MAX - b.len;
0437 if (unlikely(len > PATH_MAX))
0438 error = -ENAMETOOLONG;
0439 else if (unlikely(len > size))
0440 error = -ERANGE;
0441 else if (copy_to_user(buf, b.buf, len))
0442 error = -EFAULT;
0443 else
0444 error = len;
0445 }
0446 __putname(page);
0447 return error;
0448 }