From owner-svn-src-head@freebsd.org Mon Oct 5 19:38:52 2020 Return-Path: Delivered-To: svn-src-head@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 6C97942E508; Mon, 5 Oct 2020 19:38:52 +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 4C4rWJ2Kc8z4Q2h; Mon, 5 Oct 2020 19:38:52 +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 1925F1AA2B; Mon, 5 Oct 2020 19:38:52 +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 095Jcp4W049333; Mon, 5 Oct 2020 19:38:51 GMT (envelope-from mjg@FreeBSD.org) Received: (from mjg@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id 095Jcp7v049331; Mon, 5 Oct 2020 19:38:51 GMT (envelope-from mjg@FreeBSD.org) Message-Id: <202010051938.095Jcp7v049331@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: mjg set sender to mjg@FreeBSD.org using -f From: Mateusz Guzik Date: Mon, 5 Oct 2020 19:38:51 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r366462 - in head/sys: kern sys X-SVN-Group: head X-SVN-Commit-Author: mjg X-SVN-Commit-Paths: in head/sys: kern sys X-SVN-Commit-Revision: 366462 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.33 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 05 Oct 2020 19:38:52 -0000 Author: mjg Date: Mon Oct 5 19:38:51 2020 New Revision: 366462 URL: https://svnweb.freebsd.org/changeset/base/366462 Log: cache: fix pwd use-after-free in setting up fallback Since the code exits smr section prior to calling pwd_hold, the used pwd can be freed and a new one allocated with the same address, making the comparison erroneously true. Note it is very unlikely anyone ran into it. Modified: head/sys/kern/kern_descrip.c head/sys/kern/vfs_cache.c head/sys/sys/filedesc.h Modified: head/sys/kern/kern_descrip.c ============================================================================== --- head/sys/kern/kern_descrip.c Mon Oct 5 19:26:54 2020 (r366461) +++ head/sys/kern/kern_descrip.c Mon Oct 5 19:38:51 2020 (r366462) @@ -3344,6 +3344,17 @@ pwd_hold_filedesc(struct filedesc *fdp) return (pwd); } +bool +pwd_hold_smr(struct pwd *pwd) +{ + + MPASS(pwd != NULL); + if (__predict_true(refcount_acquire_if_not_zero(&pwd->pwd_refcount))) { + return (true); + } + return (false); +} + struct pwd * pwd_hold(struct thread *td) { @@ -3354,8 +3365,7 @@ pwd_hold(struct thread *td) vfs_smr_enter(); pwd = vfs_smr_entered_load(&fdp->fd_pwd); - MPASS(pwd != NULL); - if (__predict_true(refcount_acquire_if_not_zero(&pwd->pwd_refcount))) { + if (pwd_hold_smr(pwd)) { vfs_smr_exit(); return (pwd); } Modified: head/sys/kern/vfs_cache.c ============================================================================== --- head/sys/kern/vfs_cache.c Mon Oct 5 19:26:54 2020 (r366461) +++ head/sys/kern/vfs_cache.c Mon Oct 5 19:38:51 2020 (r366462) @@ -3484,25 +3484,24 @@ cache_fplookup_partial_setup(struct cache_fpl *fpl) ndp = fpl->ndp; cnp = fpl->cnp; + pwd = fpl->pwd; dvp = fpl->dvp; dvp_seqc = fpl->dvp_seqc; - dvs = vget_prep_smr(dvp); - if (__predict_false(dvs == VGET_NONE)) { + if (!pwd_hold_smr(pwd)) { cache_fpl_smr_exit(fpl); return (cache_fpl_aborted(fpl)); } + dvs = vget_prep_smr(dvp); cache_fpl_smr_exit(fpl); - - vget_finish_ref(dvp, dvs); - if (!vn_seqc_consistent(dvp, dvp_seqc)) { - vrele(dvp); + if (__predict_false(dvs == VGET_NONE)) { + pwd_drop(pwd); return (cache_fpl_aborted(fpl)); } - pwd = pwd_hold(curthread); - if (fpl->pwd != pwd) { + vget_finish_ref(dvp, dvs); + if (!vn_seqc_consistent(dvp, dvp_seqc)) { vrele(dvp); pwd_drop(pwd); return (cache_fpl_aborted(fpl)); Modified: head/sys/sys/filedesc.h ============================================================================== --- head/sys/sys/filedesc.h Mon Oct 5 19:26:54 2020 (r366461) +++ head/sys/sys/filedesc.h Mon Oct 5 19:38:51 2020 (r366462) @@ -302,6 +302,7 @@ void pwd_ensure_dirs(void); void pwd_set_rootvnode(void); struct pwd *pwd_hold_filedesc(struct filedesc *fdp); +bool pwd_hold_smr(struct pwd *pwd); struct pwd *pwd_hold(struct thread *td); void pwd_drop(struct pwd *pwd); static inline void