Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1jdmSF-0006af-SQ for mharc-grub-devel@gnu.org; Tue, 26 May 2020 23:10:07 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:41148) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jdmSE-0006Yw-7M for grub-devel@gnu.org; Tue, 26 May 2020 23:10:06 -0400 Received: from mail-pf1-x441.google.com ([2607:f8b0:4864:20::441]:37794) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1jdmSD-0005HI-2o for grub-devel@gnu.org; Tue, 26 May 2020 23:10:05 -0400 Received: by mail-pf1-x441.google.com with SMTP id y198so11130817pfb.4 for ; Tue, 26 May 2020 20:10:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=endlessm-com.20150623.gappssmtp.com; s=20150623; h=from:to:subject:date:message-id:mime-version :content-transfer-encoding; bh=PwDZkkHHEnulthdX8KMuP7vRHWsqpXjVi6lHXp+yWfw=; b=mPSWnO0HYds/shhOGztLdXiJ3oot8essW7/HM9it+rDZr9r5pZxDcXPC05z0pBJu5f qtlIcEShIzLpHDn4VeSevSHm/12MpfsGYVEUA4s1xK/4LoUK3jEMsPKK+exolvxqnBLt oxX5j/5+p1mCxvIUXmaI6taxBFMTAVLK6jCjl1eWHE8pbB/W49ChMR+9wW/qb/7Sqkny TUMxVRjtIJiD+8HhPeK0TFCFm1ji5node3WHZyIAX4V4bTq/GLh6oGv/IUGjzDLbDYzU adKmhxF3uoVFmfF0t+0fGP8VFE+ITv02pQ4ySJItMnb7/YgrNAL4mNu6pX15kAda4/jV cFgw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:mime-version :content-transfer-encoding; bh=PwDZkkHHEnulthdX8KMuP7vRHWsqpXjVi6lHXp+yWfw=; b=TceOXUC2Lw+Xpr0YlCyYBL5H3RtigPgQzFlUrzh7C4oK/IyRs5gZxlqEgipIWlOiuo ++jCHwr4E96nRNP/9aH3SoWvkaMqZwqmUsZk8Zv3D7+C9QsmjwRvkcO/ATbEt1GF7W9K ZWdr2SIYrU7WLrynDcPqrJHoAe8HKpVIJePJHoDo9jueb5UAKokhxQbdKcSO9MK5/ptQ khq1i7jz295dbSa54lJaK7la6+Zw0h5oBSAqcYJVaibhNVu833XPiigrfd4voQfG6Z45 wLEDwI18RPUy22IQSe7uNinDQW972FeftTciAudSfyNicFgn1FUhBqt5izty3eWKzgBe u2Bg== X-Gm-Message-State: AOAM533ixxhE9ce287ZAmS/ldMdIg5qY6tZ8FiEIHzCAJLILFj3DEWEr udzD/beyOTrkv5khH7fLFv5fPs5izAyYyZEaFNFSyYPcNMrRqajIySyTgb0d7iks7laTEpqqZtT axwX/qSQZdZlSRu7hq+fmChfHubE4w3s8uY/cwluDDO9Aq+haAoXFPru9worZ2/s= X-Google-Smtp-Source: ABdhPJw8Xw6wNidFmW+quzfAZKVeS3MESAse9EHkPu2yyqDhJeV0zUNDNen82a6pktV7QDyP1R/+2A== X-Received: by 2002:a63:cd51:: with SMTP id a17mr1684418pgj.99.1590549002398; Tue, 26 May 2020 20:10:02 -0700 (PDT) Received: from limbo.local (host-173.222-14-119.dynamic.totalbb.net.tw. [119.14.222.173]) by smtp.gmail.com with ESMTPSA id o27sm829107pgd.18.2020.05.26.20.10.01 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 26 May 2020 20:10:01 -0700 (PDT) From: Daniel Drake To: grub-devel@gnu.org Subject: [PATCH] minix: avoid mistakenly probing ext2 filesystems Date: Wed, 27 May 2020 11:09:59 +0800 Message-Id: <20200527030959.213583-1-drake@endlessm.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Received-SPF: pass client-ip=2607:f8b0:4864:20::441; envelope-from=drake@endlessm.com; helo=mail-pf1-x441.google.com X-detected-operating-system: by eggs.gnu.org: No matching host in p0f cache. That's all we know. X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_PASS=-0.001, URIBL_BLOCKED=0.001 autolearn=_AUTOLEARN X-Spam_action: no action X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 27 May 2020 03:10:06 -0000 ext2 (and ext3, ext4) filesystems write the number of free inodes to location 0x410. On a minix filesystem, that same location is used for the minix superblock magic number. If the number of free inodes on an ext2 filesystem is equal to any of the four minix superblock magic values plus any multiple of 65536, grub's minix filesystem code will probe it as a minix filesystem. In the case of an OS using ext2 as the root filesystem, since there will ordinarily be some amount of file creation and deletion on every bootup, it effectively means that this situation has a 1:16384 chance of being hit on every reboot. This will cause grub's filesystem probing code to mistakenly identify an ext2 filesystem as minix. This can be seen by e.g. "search --label" incorrectly indicating that no such ext2 partition with matching label exists, whereas in fact it does. After spotting the rough cause of the issue I was facing here, I borrowed much of the diagnosis/explanation from meierfra who found and investigated the same issue in util-linux in 2010: https://bugs.launchpad.net/ubuntu/+source/util-linux/+bug/518582 This was fixed in util-linux by having the minix code check for the ext2 magic. Do the same here. --- grub-core/fs/minix.c | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/grub-core/fs/minix.c b/grub-core/fs/minix.c index d0d08363c..db0a83feb 100644 --- a/grub-core/fs/minix.c +++ b/grub-core/fs/minix.c @@ -38,6 +38,8 @@ GRUB_MOD_LICENSE ("GPLv3+"); #define GRUB_MINIX_MAGIC_30 0x138F #endif +#define EXT2_MAGIC 0xEF53 + #define GRUB_MINIX_INODE_DIR_BLOCKS 7 #define GRUB_MINIX_LOG2_BSIZE 1 #define GRUB_MINIX_ROOT_INODE 1 @@ -466,7 +468,22 @@ grub_minix_find_file (struct grub_minix_data *data, const char *path) static struct grub_minix_data * grub_minix_mount (grub_disk_t disk) { - struct grub_minix_data *data; + struct grub_minix_data *data = NULL; + grub_uint16_t ext2_marker; + + grub_disk_read (disk, 1 * 2, 56, sizeof (ext2_marker), + &ext2_marker); + if (grub_errno) + goto fail; + + /* ext2 filesystems can sometimes be mistakenly identified + * as minix, e.g. due to the number of free ext2 inodes being + * written to the same location where the minix superblock + * magic is found. + * Avoid such situations by skipping any filesystems that + * have the ext2 superblock magic. */ + if (ext2_marker == grub_cpu_to_le16_compile_time (EXT2_MAGIC)) + goto fail; data = grub_malloc (sizeof (struct grub_minix_data)); if (!data) -- 2.25.1