From nobody Wed Jun 07 23:15:36 2023 X-Original-To: dev-commits-src-branches@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4Qc39P1KsNz4bhV9; Wed, 7 Jun 2023 23:15:37 +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 4Qc39P0t8Vz3CqJ; Wed, 7 Jun 2023 23:15:37 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1686179737; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=xpPaRajNb1mK3yTkuQolV1hswqguQ+k48eWAJcINGeY=; b=ob7qNmZbNTaoZuV8I+rQ0pGojOPXmhvKbG1UvuSRUGNRy04NvfXaQhwj/Yg2i4fRyeSPTp 6Z2cjIaas5OB7R+6JYZ8i5nXISgHXQU991oyKIxGDU476dGfUcX+HZkhyJBpEmgta1Jxo8 YQZk4qO7N0v+KLHbcCrnyaGQn/Y8wQ5JuHplMr9CxhIp+kbbMbt/7o2RwpFAldA1F+mBTC 2xYYOjL3d93CAqZm//BY8jb64iP+yP1sxq5aefqu2svUDB1iet0ZZzTMguqt/08C8y+lAE 1hDpIeDFDznKxlpTHhJ+l+o3F+cN7/n65UihlPyFZz18oi/jAxEdNbrWSweG1Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1686179737; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=xpPaRajNb1mK3yTkuQolV1hswqguQ+k48eWAJcINGeY=; b=xktGH4p7Du/SvgV41e31H8LkLvRlrJmTLspHvU6yCAvmLJhoO2kGVQuMxQPmPtsCz9B+6q WfPQzaWkI3B4saPVgSOlwoCuWMuXY+4i4LTekZ/YUn+kEn8vlU+To5piqW37ff+cVgvDd1 cEvLuiUiqi2gCpsdCb1lPcndGVPAAOV9Fgn48XQO+XJku2PiVoc/nhPC+6+vEA+SDcdLV2 gnb23bpKZaBjNBA2S9WGuMjcc+cyNrHNBqFPWMk70HpR1rqNtO4IbXgkB6SoBRpVcMIS6b kpzB7X1ZlJM+1K/nyVDFupBOhElzfXNOJTWjPELzEVi7M8LYWh+G/kADnKTP1g== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1686179737; a=rsa-sha256; cv=none; b=Vgq6oNHgWd6wTgleEag5Kic5ntcAHgUsbFLY3VS8T9AP4NWeFhiS21dNfbdPtJnc6v9Yfl +5uc2BUMOANp2RCEMbi7eyYKrcDh9JxDs9L9pTKfPSDz4Bns+bKAQzf/V9KJhQTbdY0lyN jQw/dVxS3pwp2Vwdzi7yrXjL4HT8Av42NQzFt1Y5koZM8y0mRBX3CIarT/gpzdwxYfsQHw RW2UpWykvruofyF1UfRoVhaObuVq9SfEW8fIWoOzmgDrEPK2F5XR6MLDBluXmTaxQmo183 PSzZbYaG/Q1OHI7QN7thl4NozqjzMvZEqh1GXMVgBelNFhp7cxtTz2ARmqc1VQ== 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 4Qc39N721BzWGJ; Wed, 7 Jun 2023 23:15:36 +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 357NFaHK048422; Wed, 7 Jun 2023 23:15:36 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 357NFaHD048421; Wed, 7 Jun 2023 23:15:36 GMT (envelope-from git) Date: Wed, 7 Jun 2023 23:15:36 GMT Message-Id: <202306072315.357NFaHD048421@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Kirk McKusick Subject: git: 3035f98d56eb - stable/13 - Fix a bug in fsck_ffs(8) triggered by corrupted filesystems. List-Id: Commits to the stable branches of the FreeBSD src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-branches List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-branches@freebsd.org X-BeenThere: dev-commits-src-branches@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: mckusick X-Git-Repository: src X-Git-Refname: refs/heads/stable/13 X-Git-Reftype: branch X-Git-Commit: 3035f98d56eb72240c6260d667427ab4ade08b45 Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch stable/13 has been updated by mckusick: URL: https://cgit.FreeBSD.org/src/commit/?id=3035f98d56eb72240c6260d667427ab4ade08b45 commit 3035f98d56eb72240c6260d667427ab4ade08b45 Author: Kirk McKusick AuthorDate: 2023-05-28 00:09:02 +0000 Commit: Kirk McKusick CommitDate: 2023-06-07 22:46:53 +0000 Fix a bug in fsck_ffs(8) triggered by corrupted filesystems. Reported-by: Robert Morris PR: 271378 Sponsored-by: The FreeBSD Foundation (cherry picked from commit 101a9ac07128a17d8797cc3e93978d2cfa457e99) --- sbin/fsck_ffs/fsck.h | 1 + sbin/fsck_ffs/fsutil.c | 25 +++++++++++++++++++++++++ sbin/fsck_ffs/pass1.c | 12 +----------- sbin/fsck_ffs/suj.c | 3 +++ 4 files changed, 30 insertions(+), 11 deletions(-) diff --git a/sbin/fsck_ffs/fsck.h b/sbin/fsck_ffs/fsck.h index 9f9b1e0a8857..4f77fc39f3f7 100644 --- a/sbin/fsck_ffs/fsck.h +++ b/sbin/fsck_ffs/fsck.h @@ -471,6 +471,7 @@ void check_blkcnt(struct inode *ip); int check_cgmagic(int cg, struct bufarea *cgbp); void rebuild_cg(int cg, struct bufarea *cgbp); void check_dirdepth(struct inoinfo *inp); +int chkfilesize(mode_t mode, u_int64_t filesize); int chkrange(ufs2_daddr_t blk, int cnt); void ckfini(int markclean); int ckinode(union dinode *dp, struct inodesc *); diff --git a/sbin/fsck_ffs/fsutil.c b/sbin/fsck_ffs/fsutil.c index 419d0e91a1df..ab55bdfbb37a 100644 --- a/sbin/fsck_ffs/fsutil.c +++ b/sbin/fsck_ffs/fsutil.c @@ -1206,6 +1206,31 @@ std_checkblkavail(ufs2_daddr_t blkno, long frags) return (0); } +/* + * Check whether a file size is within the limits for the filesystem. + * Return 1 when valid and 0 when too big. + * + * This should match the file size limit in ffs_mountfs(). + */ +int +chkfilesize(mode_t mode, u_int64_t filesize) +{ + u_int64_t kernmaxfilesize; + + if (sblock.fs_magic == FS_UFS1_MAGIC) + kernmaxfilesize = (off_t)0x40000000 * sblock.fs_bsize - 1; + else + kernmaxfilesize = sblock.fs_maxfilesize; + if (filesize > kernmaxfilesize || + filesize > sblock.fs_maxfilesize || + (mode == IFDIR && filesize > MAXDIRSIZE)) { + if (debug) + printf("bad file size %ju:", (uintmax_t)filesize); + return (0); + } + return (1); +} + /* * Slow down IO so as to leave some disk bandwidth for other processes */ diff --git a/sbin/fsck_ffs/pass1.c b/sbin/fsck_ffs/pass1.c index 863bf34ff0fc..d328234220ad 100644 --- a/sbin/fsck_ffs/pass1.c +++ b/sbin/fsck_ffs/pass1.c @@ -256,7 +256,6 @@ checkinode(ino_t inumber, struct inodesc *idesc, int rebuiltcg) { struct inode ip; union dinode *dp; - off_t kernmaxfilesize; ufs2_daddr_t ndb; mode_t mode; intmax_t size, fixsize; @@ -293,16 +292,7 @@ checkinode(ino_t inumber, struct inodesc *idesc, int rebuiltcg) return (1); } lastino = inumber; - /* This should match the file size limit in ffs_mountfs(). */ - if (sblock.fs_magic == FS_UFS1_MAGIC) - kernmaxfilesize = (off_t)0x40000000 * sblock.fs_bsize - 1; - else - kernmaxfilesize = sblock.fs_maxfilesize; - if (DIP(dp, di_size) > kernmaxfilesize || - DIP(dp, di_size) > sblock.fs_maxfilesize || - (mode == IFDIR && DIP(dp, di_size) > MAXDIRSIZE)) { - if (debug) - printf("bad size %ju:", (uintmax_t)DIP(dp, di_size)); + if (chkfilesize(mode, DIP(dp, di_size)) == 0) { pfatal("BAD FILE SIZE"); goto unknown; } diff --git a/sbin/fsck_ffs/suj.c b/sbin/fsck_ffs/suj.c index 8bcee7c54c85..cbb6e597dfee 100644 --- a/sbin/fsck_ffs/suj.c +++ b/sbin/fsck_ffs/suj.c @@ -1963,6 +1963,9 @@ ino_build_trunc(struct jtrncrec *rec) printf("ino_build_trunc: op %d ino %ju, size %jd\n", rec->jt_op, (uintmax_t)rec->jt_ino, (uintmax_t)rec->jt_size); + if (chkfilesize(IFREG, rec->jt_size) == 0) + err_suj("ino_build: truncation size too large %ju\n", + (intmax_t)rec->jt_size); sino = ino_lookup(rec->jt_ino, 1); if (rec->jt_op == JOP_SYNC) { sino->si_trunc = NULL;