From owner-dev-commits-src-all@freebsd.org Tue Mar 2 00:19:55 2021 Return-Path: Delivered-To: dev-commits-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 7F32F551B27; Tue, 2 Mar 2021 00:19:55 +0000 (UTC) (envelope-from git@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 "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4DqHnl3C18z4XQc; Tue, 2 Mar 2021 00:19:55 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (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 did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 60E955281; Tue, 2 Mar 2021 00:19:55 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 1220Jtxi047943; Tue, 2 Mar 2021 00:19:55 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 1220JtBp047942; Tue, 2 Mar 2021 00:19:55 GMT (envelope-from git) Date: Tue, 2 Mar 2021 00:19:55 GMT Message-Id: <202103020019.1220JtBp047942@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Konstantin Belousov Subject: git: 8742817ba62e - main - FFS extattr: fix handling of the tail MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: kib X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 8742817ba62ec604156c139727155d36f5fbad06 Auto-Submitted: auto-generated X-BeenThere: dev-commits-src-all@freebsd.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Commit messages for all branches of the src repository List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 02 Mar 2021 00:19:55 -0000 The branch main has been updated by kib: URL: https://cgit.FreeBSD.org/src/commit/?id=8742817ba62ec604156c139727155d36f5fbad06 commit 8742817ba62ec604156c139727155d36f5fbad06 Author: Konstantin Belousov AuthorDate: 2021-03-01 15:24:11 +0000 Commit: Konstantin Belousov CommitDate: 2021-03-02 00:19:34 +0000 FFS extattr: fix handling of the tail There are three issues with change that stopped truncating ea area before write, and resulted in possible zero tail in the ea area: - Truncate to zero checked i_ea_len after the reference was dropped, making the last drop effectively truncate to zero length always. - Loop to fill uio for zeroing specified too large length, that triggered assert in normal situation. - Integrity check could trip over the tail, instead we must allow partial header or header with zero length, and clamp ea image in memory at it. Reported by: arichardson Tested by: arichardson, pho Sponsored by: The FreeBSD Foundation MFC after: 3 days Fixup: 5e198e7646a27412c0541719f7bf1bbc0bd89223 Differential Revision: https://reviews.freebsd.org/D28999 --- sys/ufs/ffs/ffs_vnops.c | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/sys/ufs/ffs/ffs_vnops.c b/sys/ufs/ffs/ffs_vnops.c index 050a6f66be17..dc638595eb7b 100644 --- a/sys/ufs/ffs/ffs_vnops.c +++ b/sys/ufs/ffs/ffs_vnops.c @@ -1346,13 +1346,20 @@ ffs_rdextattr(u_char **p, struct vnode *vp, struct thread *td) /* Validate disk xattrfile contents. */ for (eap = (void *)eae, eaend = (void *)(eae + easize); eap < eaend; eap = eapnext) { + /* Detect zeroed out tail */ + if (eap->ea_length < sizeof(*eap) || eap->ea_length == 0) { + easize = (const u_char *)eap - eae; + break; + } + eapnext = EXTATTR_NEXT(eap); - /* Bogusly short entry or bogusly long entry. */ - if (eap->ea_length < sizeof(*eap) || eapnext > eaend) { + /* Bogusly long entry. */ + if (eapnext > eaend) { free(eae, M_TEMP); return (EINTEGRITY); } } + ip->i_ea_len = easize; *p = eae; return (0); } @@ -1407,7 +1414,6 @@ ffs_open_ea(struct vnode *vp, struct ucred *cred, struct thread *td) ffs_unlock_ea(vp); return (error); } - ip->i_ea_len = dp->di_extsize; ip->i_ea_error = 0; ip->i_ea_refs++; ffs_unlock_ea(vp); @@ -1426,6 +1432,7 @@ ffs_close_ea(struct vnode *vp, int commit, struct ucred *cred, struct thread *td struct ufs2_dinode *dp; size_t ea_len, tlen; int error, i, lcnt; + bool truncate; ip = VTOI(vp); @@ -1436,6 +1443,7 @@ ffs_close_ea(struct vnode *vp, int commit, struct ucred *cred, struct thread *td } dp = ip->i_din2; error = ip->i_ea_error; + truncate = false; if (commit && error == 0) { ASSERT_VOP_ELOCKED(vp, "ffs_close_ea commit"); if (cred == NOCRED) @@ -1452,12 +1460,12 @@ ffs_close_ea(struct vnode *vp, int commit, struct ucred *cred, struct thread *td liovec[0].iov_base = ip->i_ea_area; liovec[0].iov_len = ip->i_ea_len; - for (i = 1, tlen = ea_len; i < lcnt; i++) { + for (i = 1, tlen = ea_len - ip->i_ea_len; i < lcnt; i++) { liovec[i].iov_base = __DECONST(void *, zero_region); liovec[i].iov_len = MIN(ZERO_REGION_SIZE, tlen); tlen -= liovec[i].iov_len; } - MPASS(tlen == ip->i_ea_len); + MPASS(tlen == 0); luio.uio_iov = liovec; luio.uio_offset = 0; @@ -1466,6 +1474,8 @@ ffs_close_ea(struct vnode *vp, int commit, struct ucred *cred, struct thread *td luio.uio_rw = UIO_WRITE; luio.uio_td = td; error = ffs_extwrite(vp, &luio, IO_EXT | IO_SYNC, cred); + if (error == 0 && ip->i_ea_len == 0) + truncate = true; } if (--ip->i_ea_refs == 0) { free(ip->i_ea_area, M_TEMP); @@ -1475,7 +1485,7 @@ ffs_close_ea(struct vnode *vp, int commit, struct ucred *cred, struct thread *td } ffs_unlock_ea(vp); - if (commit && error == 0 && ip->i_ea_len == 0) + if (truncate) ffs_truncate(vp, 0, IO_EXT, cred); return (error); }