From owner-svn-src-all@freebsd.org Sat Aug 22 06:56:05 2020 Return-Path: Delivered-To: svn-src-all@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id 350603AFA65; Sat, 22 Aug 2020 06:56:05 +0000 (UTC) (envelope-from mjg@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4BYTgT0bXvz4Fc1; Sat, 22 Aug 2020 06:56:05 +0000 (UTC) (envelope-from mjg@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id CDF7812553; Sat, 22 Aug 2020 06:56:04 +0000 (UTC) (envelope-from mjg@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id 07M6u41f016827; Sat, 22 Aug 2020 06:56:04 GMT (envelope-from mjg@FreeBSD.org) Received: (from mjg@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id 07M6u4WT016825; Sat, 22 Aug 2020 06:56:04 GMT (envelope-from mjg@FreeBSD.org) Message-Id: <202008220656.07M6u4WT016825@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: mjg set sender to mjg@FreeBSD.org using -f From: Mateusz Guzik Date: Sat, 22 Aug 2020 06:56:04 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r364478 - head/sys/kern X-SVN-Group: head X-SVN-Commit-Author: mjg X-SVN-Commit-Paths: head/sys/kern X-SVN-Commit-Revision: 364478 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.33 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 22 Aug 2020 06:56:05 -0000 Author: mjg Date: Sat Aug 22 06:56:04 2020 New Revision: 364478 URL: https://svnweb.freebsd.org/changeset/base/364478 Log: vfs: add a work around for vp_crossmp bug to realpath The actual bug is not yet addressed as it will get much easier after other problems are addressed (most notably rename contract). The only affected in-tree consumer is realpath. Everyone else happens to be performing lookups within a mount point, having a side effect of ni_dvp being set to mount point's root vnode in the worst case. Reported by: pho Modified: head/sys/kern/vfs_cache.c head/sys/kern/vfs_lookup.c Modified: head/sys/kern/vfs_cache.c ============================================================================== --- head/sys/kern/vfs_cache.c Sat Aug 22 04:07:44 2020 (r364477) +++ head/sys/kern/vfs_cache.c Sat Aug 22 06:56:04 2020 (r364478) @@ -2811,6 +2811,7 @@ vn_fullpath_hardlink(struct thread *td, struct nameida size_t addend; int error; bool slash_prefixed; + enum vtype type; if (*buflen < 2) return (EINVAL); @@ -2824,7 +2825,31 @@ vn_fullpath_hardlink(struct thread *td, struct nameida addend = 0; vp = ndp->ni_vp; - if (vp->v_type != VDIR) { + /* + * Check for VBAD to work around the vp_crossmp bug in lookup(). + * + * For example consider tmpfs on /tmp and realpath /tmp. ni_vp will be + * set to mount point's root vnode while ni_dvp will be vp_crossmp. + * If the type is VDIR (like in this very case) we can skip looking + * at ni_dvp in the first place. However, since vnodes get passed here + * unlocked the target may transition to doomed state (type == VBAD) + * before we get to evaluate the condition. If this happens, we will + * populate part of the buffer and descend to vn_fullpath_dir with + * vp == vp_crossmp. Prevent the problem by checking for VBAD. + * + * This should be atomic_load(&vp->v_type) but it is ilegal to take + * an address of a bit field, even if said field is sized to char. + * Work around the problem by reading the value into a full-sized enum + * and then re-reading it with atomic_load which will still prevent + * the compiler from re-reading down the road. + */ + type = vp->v_type; + type = atomic_load_int(&type); + if (type == VBAD) { + error = ENOENT; + goto out_bad; + } + if (type != VDIR) { cnp = &ndp->ni_cnd; addend = cnp->cn_namelen + 2; if (*buflen < addend) { Modified: head/sys/kern/vfs_lookup.c ============================================================================== --- head/sys/kern/vfs_lookup.c Sat Aug 22 04:07:44 2020 (r364477) +++ head/sys/kern/vfs_lookup.c Sat Aug 22 06:56:04 2020 (r364478) @@ -1209,6 +1209,11 @@ nextname: VOP_UNLOCK(dp); success: /* + * FIXME: for lookups which only cross a mount point to fetch the + * root vnode, ni_dvp will be set to vp_crossmp. This can be a problem + * if either WANTPARENT or LOCKPARENT is set. + */ + /* * Because of shared lookup we may have the vnode shared locked, but * the caller may want it to be exclusively locked. */