From nobody Tue Dec 20 03:25:15 2022 X-Original-To: dev-commits-src-all@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 4Nbhlv288Vz1Gy3v; Tue, 20 Dec 2022 03:25:15 +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 4Nbhlv1M62z4DhV; Tue, 20 Dec 2022 03:25:15 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1671506715; 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=/+hUsGkh73Tq5jIo6shTY8ZQ3a8aD2BJbn594kqDwCU=; b=ruhgHZCxlOmwVpsM4qV2SHvLnAJ5W4Lb5ATQf9BcqkvZIkrXdmp5D2z3Uax21DQ3HPuX02 3jkb7PSlzUCVVDYRbT6qcWnwWdqWINSsEwBArCeqhr3ouOX8cUaTFp3hoLHo4+PEsnekjH ZedwCC6dQlfM6RstB/TPGHOQ54dtGaRF0faUyVymQ4pMdR2pMGDwMthOajowdDiT81PgGo ykn7JmIEZL8axrwFQJEu0WZHX1bEjwF4kvOMYiaKkYKdKBN/2hpTqVaPe0PFa+Tiu6Uzf9 N1Ol4JwdVHJI+NHBZO+1qtzUjSpY6kuq0NPbUdCyW8PuLjYLUJWGeeTCfbZSMg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1671506715; 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=/+hUsGkh73Tq5jIo6shTY8ZQ3a8aD2BJbn594kqDwCU=; b=GG5l9CHz2zKMJT3PAdCeTB4XINLJyZP2jVVLEjd65OobC7k77FQcmcSM1S4n/RWiwCZ0Pz HdI45HxanYP/eAdvAPiDSirDIUTcHNHZRgufUzJdO7g4Z7HINpd3+1S0EMnenPpHinDmr+ WXrxcq50toTFObUWIagEvoP4njtB25fsoaBVjTntHINp5Gp/sEvRTBTG3T6e9lvNEknVSr d3/dnnW1y0D9vB2nzxh3oFFK9LTX28Vv9TgMdfcPyvk38b6Hy3DGT2JCofwQI5jbSe01W3 pm0khUCCY7skCuPOtTPjhgbC0B3IM54YX/pFEKfvzqm/DJXB4KY3QRfkwnWjbQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1671506715; a=rsa-sha256; cv=none; b=XU7hsZgOOhKPU60T0bYDpxbux1BaL0jsn/k3V9ew6ZfC9ttA/NVFhZi6XQzFQWwv9VShNY O9Xf74rE0xSanWWXUPO0FhOY8tA2gtRwR4MluE0uaBBB2PR1pFeVgwaIIOje7BN01xp2KT QRzLlR9RSMUquZP4A7JRtYkKwYjbapAr+Okx/oslg4F6EJCbAk/FX4Hk9uOqLQG7mSB/WC nxio4u5E44EyTMVZj7ivmSaqShH1QRoTx18QFkYAbRPwkgnhZQzFnaIqp9hMtVZpgWkX4d bEF5E42NReeJ3N0JnD5XPnTIRQHALje0SXBDbFVZvuyYBoK8DQIV2FOoOYC0sA== 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 4Nbhlv0QZPzLHW; Tue, 20 Dec 2022 03:25:15 +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 2BK3PFPq002908; Tue, 20 Dec 2022 03:25:15 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 2BK3PF1O002907; Tue, 20 Dec 2022 03:25:15 GMT (envelope-from git) Date: Tue, 20 Dec 2022 03:25:15 GMT Message-Id: <202212200325.2BK3PF1O002907@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: "Alfredo Dal'Ava Junior" Subject: git: b13110e9f3ca - main - ufs/ffs: detect endian mismatch between machine and filesystem List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: alfredo X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: b13110e9f3cad590abb86519e313eb1449ddc2e7 Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by alfredo: URL: https://cgit.FreeBSD.org/src/commit/?id=b13110e9f3cad590abb86519e313eb1449ddc2e7 commit b13110e9f3cad590abb86519e313eb1449ddc2e7 Author: Alfredo Dal'Ava Junior AuthorDate: 2022-12-17 02:54:39 +0000 Commit: Alfredo Dal'Ava Junior CommitDate: 2022-12-20 03:20:11 +0000 ufs/ffs: detect endian mismatch between machine and filesystem Mount on a LE machine a filesystem formatted for BE is not supported currently. This adds a check for the superblock magic number using swapped bytes to guess and warn the user that it may be a valid superblock but endian is incompatible. MFC after: 2 weeks Reviewed by: mckusick Obtained from: mckusick, alfredo Differential Revision: https://reviews.freebsd.org/D37675 --- sys/kern/vfs_mountroot.c | 2 +- sys/ufs/ffs/ffs_subr.c | 26 ++++++++++++++++++++++++-- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/sys/kern/vfs_mountroot.c b/sys/kern/vfs_mountroot.c index 5361f41276a0..3c506378ad2f 100644 --- a/sys/kern/vfs_mountroot.c +++ b/sys/kern/vfs_mountroot.c @@ -794,7 +794,7 @@ parse_mount(char **conf) ma = parse_mountroot_options(ma, opts); error = kernel_mount(ma, MNT_ROOTFS); - if (error == 0 || timeout <= 0) + if (error == 0 || error == EILSEQ || timeout <= 0) break; if (root_mount_timeout * hz == timeout || diff --git a/sys/ufs/ffs/ffs_subr.c b/sys/ufs/ffs/ffs_subr.c index 705f8c9c961d..31f30ba5d175 100644 --- a/sys/ufs/ffs/ffs_subr.c +++ b/sys/ufs/ffs/ffs_subr.c @@ -35,6 +35,7 @@ __FBSDID("$FreeBSD$"); #include +#include #include #ifndef _KERNEL @@ -144,6 +145,7 @@ static int validate_sblock(struct fs *, int); * EIO: non-existent or truncated superblock. * EIO: error reading summary information. * ENOENT: no usable known superblock found. + * EILSEQ: filesystem with wrong byte order found. * ENOMEM: failed to allocate space for the superblock. * EINVAL: The previous newfs operation on this volume did not complete. * The administrator must complete newfs before using this volume. @@ -382,6 +384,17 @@ validate_sblock(struct fs *fs, int flags) prtmsg = ((flags & UFS_NOMSG) == 0); warnerr = (flags & UFS_NOWARNFAIL) == UFS_NOWARNFAIL ? 0 : ENOENT; wmsg = warnerr ? "" : " (Ignored)"; + /* + * Check for endian mismatch between machine and filesystem. + */ + if (((fs->fs_magic != FS_UFS2_MAGIC) && + (bswap32(fs->fs_magic) == FS_UFS2_MAGIC)) || + ((fs->fs_magic != FS_UFS1_MAGIC) && + (bswap32(fs->fs_magic) == FS_UFS1_MAGIC))) { + MPRINT("UFS superblock failed due to endian mismatch " + "between machine and filesystem\n"); + return(EILSEQ); + } /* * If just validating for recovery, then do just the minimal * checks needed for the superblock fields needed to find @@ -627,8 +640,16 @@ ffs_sbsearch(void *devfd, struct fs **fsp, int reqflags, * failure can be avoided. */ flags = UFS_NOMSG | nocsum; - if (ffs_sbget(devfd, fsp, UFS_STDSB, flags, filltype, readfunc) == 0) - return (0); + error = ffs_sbget(devfd, fsp, UFS_STDSB, flags, filltype, readfunc); + /* + * If successful or endian error, no need to try further. + */ + if (error == 0 || error == EILSEQ) { + if (msg && error == EILSEQ) + printf("UFS superblock failed due to endian mismatch " + "between machine and filesystem\n"); + return (error); + } /* * First try: ignoring hash failures. */ @@ -677,6 +698,7 @@ ffs_sbsearch(void *devfd, struct fs **fsp, int reqflags, * but some devices lie. So we just try a plausible range. */ error = ENOENT; + fsrbuf = NULL; for (secsize = dbtob(1); secsize <= SBLOCKSIZE; secsize *= 2) if ((error = (*readfunc)(devfd, (SBLOCK_UFS2 - secsize), &fsrbuf, secsize)) == 0)