From: akpm@linux-foundation.org Subject: [patch 16/21] vfs: fix d_path() for unreachable paths Date: Fri, 18 Sep 2009 13:05:58 -0700 Message-ID: <200909182005.n8IK5wJi019653@imap1.linux-foundation.org> Mime-Version: 1.0 Content-Type: text/plain; charset=ANSI_X3.4-1968 Content-Transfer-Encoding: 8bit Cc: linux-fsdevel@vger.kernel.org, akpm@linux-foundation.org, mszeredi@suse.cz, Valdis.Kletnieks@vt.edu, agruen@suse.de, hch@lst.de, jjohansen@suse.de, matthew@wil.cx To: viro@zeniv.linux.org.uk Return-path: Received: from smtp1.linux-foundation.org ([140.211.169.13]:56726 "EHLO smtp1.linux-foundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1757445AbZIRUGY (ORCPT ); Fri, 18 Sep 2009 16:06:24 -0400 Sender: linux-fsdevel-owner@vger.kernel.org List-ID: From: Miklos Szeredi John Johansen pointed out, that getcwd(2) will give a garbled result if a bind mount of a non-filesystem-root directory is detached: > mkdir /mnt/foo > mount --bind /etc /mnt/foo > cd /mnt/foo/skel > umount -l /mnt/foo > /bin/pwd etcskel If it was the root of the filesystem which was detached, it will give a saner looking result, but it still won't be a valid absolute path by which the CWD can be reached (assuming the process's root is not also on the detached mount). A similar issue happens if the CWD is outside the process's root or in a different namespace. These problems are relevant to symlinks under /proc// and /proc//fd/ as well. This patch addresses all these issues, by prefixing such unreachable paths with "(unreachable)". This isn't perfect since the returned path may still be a valid _relative_ path, and applications may not check the result of getcwd() for starting with a '/' before using it. For this reason Andreas Gruenbacher thinks getcwd(2) should return ENOENT in these cases, but that breaks /bin/pwd and bash in the above cases. Reported-by: John Johansen Cc: Matthew Wilcox Cc: Andreas Gruenbacher Signed-off-by: Miklos Szeredi Cc: Al Viro Cc: Christoph Hellwig Cc: Valdis Kletnieks Signed-off-by: Andrew Morton --- fs/dcache.c | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff -puN fs/dcache.c~vfs-fix-d_path-for-unreachable-paths fs/dcache.c --- a/fs/dcache.c~vfs-fix-d_path-for-unreachable-paths +++ a/fs/dcache.c @@ -1883,6 +1883,12 @@ static int prepend_name(char **buffer, i return prepend(buffer, buflen, name->name, name->len); } +static bool is_pseudo_root(struct dentry *dentry) +{ + return IS_ROOT(dentry) && + (dentry->d_name.len != 1 || dentry->d_name.name[0] != '/'); +} + /** * __d_path - return the path of a dentry * @path: the dentry/vfsmount to report @@ -1950,8 +1956,18 @@ out: global_root: retval += 1; /* hit the slash */ - if (prepend_name(&retval, &buflen, &dentry->d_name) != 0) - goto Elong; + + if (is_pseudo_root(dentry)) { + /* Pseudo filesystem with "foo:" prefix */ + if (prepend_name(&retval, &buflen, &dentry->d_name) != 0) + goto Elong; + } else { + /* + * Unreachable (detached or outside root or outside namespace) + */ + if (prepend(&retval, &buflen, "(unreachable)/", 14) != 0) + goto Elong; + } root->mnt = vfsmnt; root->dentry = dentry; goto out; _