From nobody Wed Dec 04 08:51:22 2024 X-Original-To: dev-commits-src-main@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 4Y3B8C15fQz5gDQY; Wed, 04 Dec 2024 08:51:23 +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 "R10" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4Y3B8C0Ykwz4qRd; Wed, 4 Dec 2024 08:51:23 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1733302283; 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=au01VJ8r7Gh23Oz8nbd4Cfd9WdpW+O28xR2Z09suwN8=; b=CbdH505px+DTfDCXpc389dImmsmRwFszay4hVnJ8tQCRD+F78oTqdyOZeKVw0BholzFXVz YBy611y1PJJp1PeG7Dq38lIWsuueD/P6+ash6jC+L5N9lKI1wqvEgfoqj/1vLytjeRvSJ7 RulCMrbo0HeArCPDuyOCFMJwkr62gX7V8BoVfwaRhcbFuBpcSl86DhA5NSEOC5KD9mKI0o x/iiJ0BeT3xkPpQAzxlqXB+nLsaOOsRRu7PajoFmsY1GSBJ2YQWJNFDV2p97kT4Oxf62G0 LE/NhAlwxwqMy/XDHIh+SYa61fJVohOnq/YwO7YfCHcTfu8UYst7ga2B2ULTIg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1733302283; 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=au01VJ8r7Gh23Oz8nbd4Cfd9WdpW+O28xR2Z09suwN8=; b=HZ8wBUe8SVRFol3VdGpyxVbtLgm1L3I0JGVXs0JOWK9kdlguHI87cx+0Ta5gTSAl0f98Ys zcUfUHHNr72tvLCyaNCoNZ3pr5TGjEcCj62fmCu3YBbHOAG+ao24wFXfpsBLArdkD2Uz1k estPa8kmUctk3zqLq9FYYVfdxPz68UxWDJeanepd4BoZEns92ub1TK2Ri0/zU4HfLXCrx2 4zhewyXXxU0bK5xAXSdgUVwE/Z6JTtkh5aD3FSbqlTXkr/0tAlpuUB6vslLIWj2Fj8FHdK wwPBFcx7eYRw6vGJGnlXWpjKt4f94Zy5jHXFLuD6hE8SKq0nl0A2Hzz6bwfWBA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1733302283; a=rsa-sha256; cv=none; b=QdAoRpSCUnzKqmbQrk9PE9SCp4NQEyztNkKCfqvq5mq5s7BV5v0Lvjh2XaeZ3s/kgBW6az tOLMuN473n5pjPnRTsB050QqE2Pm18wcmQFwDO4+d0THH8L3q9yl2Qh2oee0KZh5d2vYSG 25Fx0Mag5mr5M0Yxb2+BRCq/nwxMd82/m2I9FLtggre2aboOabR/qRRvsu24VoDuLkpFvg dG1i4qoERIfKjVzE/VZWbu9UNgucjpFuHKH5jg/vfE8kqJCSyFy96lDESVcAlrq/9KzPYa JS46OWf65eIvM0XLbuvdi07SVGjGgdKXX/AUGtz0KEl1zz3ES3x+M6i75QxTbg== 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 4Y3B8C09L5z11Bp; Wed, 4 Dec 2024 08:51:23 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 4B48pMcf090055; Wed, 4 Dec 2024 08:51:22 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 4B48pMMm090052; Wed, 4 Dec 2024 08:51:22 GMT (envelope-from git) Date: Wed, 4 Dec 2024 08:51:22 GMT Message-Id: <202412040851.4B48pMMm090052@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Robert Wing Subject: git: 6830340cfad6 - main - fstyp: search for file system headers with the largest offset first List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-main@freebsd.org Sender: owner-dev-commits-src-main@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: rew X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 6830340cfad6a69715d03f5062f76b65e988e6e9 Auto-Submitted: auto-generated The branch main has been updated by rew: URL: https://cgit.FreeBSD.org/src/commit/?id=6830340cfad6a69715d03f5062f76b65e988e6e9 commit 6830340cfad6a69715d03f5062f76b65e988e6e9 Author: Robert Wing AuthorDate: 2024-12-04 07:19:23 +0000 Commit: Robert Wing CommitDate: 2024-12-04 08:45:09 +0000 fstyp: search for file system headers with the largest offset first fstyp can misidentify a UFS file system as MS-DOS if the device was repurposed from MS-DOS to UFS via newfs. This happens for the following reasons: - the header for MS-DOS begins at offset 0 - the superblock for UFS begins at offset 64k, 8k, 0k, or 256k - newfs does not clear the area in front of UFS's superblock, leaving the MS-DOS header intact. - fstyp searches for file system headers alphabetically To avoid this misidentification, have fstyp search for file system headers with the largest offset first instead of alphabetically. The implemented fix was suggested by reporter, Richard M. Kreuter. PR: 252787 Reviewed by: imp, emaste Differential Revision: https://reviews.freebsd.org/D47855 --- usr.sbin/fstyp/fstyp.c | 42 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 36 insertions(+), 6 deletions(-) diff --git a/usr.sbin/fstyp/fstyp.c b/usr.sbin/fstyp/fstyp.c index 578210116ae5..dd06df33bbe2 100644 --- a/usr.sbin/fstyp/fstyp.c +++ b/usr.sbin/fstyp/fstyp.c @@ -54,25 +54,55 @@ bool show_label = false; typedef int (*fstyp_function)(FILE *, char *, size_t); +/* + * The ordering of fstypes[] is not arbitrary. + * + * fstyp checks the existence of a file system header to determine the + * type of file system on a given device. For different file systems, + * these headers reside at different offsets within the device. + * + * For example, the header for an MS-DOS file system begins at offset 0, + * whereas a header for UFS *normally* begins at offset 64k. If a device + * was constructed as MS-DOS and then repurposed as UFS (via newfs), it + * is possible the MS-DOS header will still be intact. To prevent + * misidentifying the file system, it is desirable to check the header + * with the largest offset first (i.e., UFS before MS-DOS). + */ static struct { const char *name; fstyp_function function; bool unmountable; const char *precache_encoding; } fstypes[] = { - { "apfs", &fstyp_apfs, true, NULL }, - { "befs", &fstyp_befs, false, NULL }, + /* last sector of geli device */ + { "geli", &fstyp_geli, true, NULL }, + /* + * ufs headers have four different areas, searched in this order: + * offsets: 64k, 8k, 0k, 256k + 8192 bytes + */ + { "ufs", &fstyp_ufs, false, NULL }, + /* offset 32768 + 512 bytes */ { "cd9660", &fstyp_cd9660, false, NULL }, - { "exfat", &fstyp_exfat, false, EXFAT_ENC }, + /* offset 1024 + 512 bytes */ + { "hfs+", &fstyp_hfsp, false, NULL }, + /* offset 1024 + 512 bytes */ { "ext2fs", &fstyp_ext2fs, false, NULL }, - { "geli", &fstyp_geli, true, NULL }, + /* offset 512 + 36 bytes */ + { "befs", &fstyp_befs, false, NULL }, + /* offset 0 + 40 bytes */ + { "apfs", &fstyp_apfs, true, NULL }, + /* offset 0 + 512 bytes (for initial signature check) */ + { "exfat", &fstyp_exfat, false, EXFAT_ENC }, + /* offset 0 + 1928 bytes */ { "hammer", &fstyp_hammer, true, NULL }, + /* offset 0 + 65536 bytes (for initial signature check) */ { "hammer2", &fstyp_hammer2, true, NULL }, - { "hfs+", &fstyp_hfsp, false, NULL }, + /* offset 0 + 512 bytes (for initial signature check) */ { "msdosfs", &fstyp_msdosfs, false, NULL }, + /* offset 0 + 512 bytes (for initial signature check) */ { "ntfs", &fstyp_ntfs, false, NTFS_ENC }, - { "ufs", &fstyp_ufs, false, NULL }, #ifdef HAVE_ZFS + /* offset 0 + 256k */ { "zfs", &fstyp_zfs, true, NULL }, #endif { NULL, NULL, NULL, NULL }