Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
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  * The source of the prepend data can be an optimistoc load
0038  * of a dentry name and length. And because we don't hold any
0039  * locks, the length and the pointer to the name may not be
0040  * in sync if a concurrent rename happens, and the kernel
0041  * copy might fault as a result.
0042  *
0043  * The end result will correct itself when we check the
0044  * rename sequence count, but we need to be able to handle
0045  * the fault gracefully.
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     // Already overflowed?
0059     if (p->len < 0)
0060         return false;
0061 
0062     // Will overflow?
0063     if (p->len < namelen) {
0064         // Fill as much as possible from the end of the name
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     // Fits fully
0073     p->len -= namelen;
0074     p->buf -= namelen;
0075     return prepend_copy(p->buf, str, namelen);
0076 }
0077 
0078 /**
0079  * prepend_name - prepend a pathname in front of current buffer pointer
0080  * @p: prepend buffer which contains buffer pointer and allocated length
0081  * @name: name string and length qstr structure
0082  *
0083  * With RCU path tracing, it may race with d_move(). Use READ_ONCE() to
0084  * make sure that either the old or the new name pointer and length are
0085  * fetched. However, there may be mismatch between length and pointer.
0086  * But since the length cannot be trusted, we need to copy the name very
0087  * carefully when doing the prepend_copy(). It also prepends "/" at
0088  * the beginning of the name. The sequence number check at the caller will
0089  * retry it again when a d_move() does happen. So any garbage in the buffer
0090  * due to mismatched pointer and length will be discarded.
0091  *
0092  * Load acquire is needed to make sure that we see the new name data even
0093  * if we might get the length wrong.
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             /* Global root */
0119             mnt_ns = READ_ONCE(mnt->mnt_ns);
0120             /* open-coded is_mounted() to use local mnt_ns */
0121             if (!IS_ERR_OR_NULL(mnt_ns) && !is_anon_ns(mnt_ns))
0122                 return 1;   // absolute root
0123             else
0124                 return 2;   // detached or not attached yet
0125         }
0126 
0127         if (unlikely(dentry == parent))
0128             /* Escaped? */
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  * prepend_path - Prepend path string to a buffer
0141  * @path: the dentry/vfsmount to report
0142  * @root: root vfsmnt/dentry
0143  * @p: prepend buffer which contains buffer pointer and allocated length
0144  *
0145  * The function will first try to write out the pathname without taking any
0146  * lock other than the RCU read lock to make sure that dentries won't go away.
0147  * It only checks the sequence number of the global rename_lock as any change
0148  * in the dentry's d_seq will be preceded by changes in the rename_lock
0149  * sequence number. If the sequence number had been changed, it will restart
0150  * the whole pathname back-tracing sequence again by taking the rename_lock.
0151  * In this case, there is no need to take the RCU read lock as the recursive
0152  * parent pointer references will keep the dentry chain alive as long as no
0153  * rename operation is performed.
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  * __d_path - return the path of a dentry
0200  * @path: the dentry/vfsmount to report
0201  * @root: root vfsmnt/dentry
0202  * @buf: buffer to return value in
0203  * @buflen: buffer length
0204  *
0205  * Convert a dentry into an ASCII path name.
0206  *
0207  * Returns a pointer into the buffer or an error code if the
0208  * path was too long.
0209  *
0210  * "buflen" should be positive.
0211  *
0212  * If the path is not reachable from the supplied root, return %NULL.
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  * d_path - return the path of a dentry
0250  * @path: path to report
0251  * @buf: buffer to return value in
0252  * @buflen: buffer length
0253  *
0254  * Convert a dentry into an ASCII path name. If the entry has been deleted
0255  * the string " (deleted)" is appended. Note that this is ambiguous.
0256  *
0257  * Returns a pointer into the buffer or an error code if the path was
0258  * too long. Note: Callers should use the returned pointer, not the passed
0259  * in buffer, to use the name! The implementation often starts at an offset
0260  * into the buffer, and may leave 0 bytes at the start.
0261  *
0262  * "buflen" should be positive.
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      * We have various synthetic filesystems that never get mounted.  On
0271      * these filesystems dentries are never used for lookup purposes, and
0272      * thus don't need to be hashed.  They also don't need a name until a
0273      * user wants to identify the object in /proc/pid/fd/.  The little hack
0274      * below allows us to generate a name for these objects on demand:
0275      *
0276      * Some pseudo inodes are mountable.  When they are mounted
0277      * path->dentry == path->mnt->mnt_root.  In that case don't call d_dname
0278      * and instead have d_path return the mounted path.
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  * Helper function for dentry_operations.d_dname() members
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     /* these dentries are never renamed, so d_lock is not needed */
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  * Write full pathname from the root of the filesystem into the buffer.
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  * NOTE! The user-level library version returns a
0396  * character pointer. The kernel system call just
0397  * returns the length of the buffer filled (which
0398  * includes the ending '\0' character), or a negative
0399  * error value. So libc would do something like
0400  *
0401  *  char *getcwd(char * buf, size_t size)
0402  *  {
0403  *      int retval;
0404  *
0405  *      retval = sys_getcwd(buf, size);
0406  *      if (retval >= 0)
0407  *          return buf;
0408  *      errno = -retval;
0409  *      return NULL;
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 }