From nobody Wed Sep 06 04:53:05 2023 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 4RgVPG0x8kz4sLsK; Wed, 6 Sep 2023 04:53:06 +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 4RgVPG06gyz3TZr; Wed, 6 Sep 2023 04:53:06 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1693975986; 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=nQLWareCi4mMRqHFCazoNtcjfPCBCQNzjCt0mI/9U8U=; b=LNcAWKAv5Jjdx+UabVn5uCO1srvHfTEXYonz3auz59MbgTyUcYdB7TNnrdpgt0HX7kE7ec PX7R6it9dbrVn/UGC9KzzQRGy/Mlia4q7Q5g4J23oMecb48rtybjIgDHD8cbISu/IoAnBm O8vHuEsVFmqPQ60FmzoQCk1DCwR1HxpJQe03xF4BwK0O0haxV7jcnHlpTwdTmRWrDGsRSu w/n0jKWmSMOntLWFas925qmsj8v78exYjbQVSNV4BM6q3x6oMVeDg53ozINHeiRrEWSBDg 6nYIRNRKNpf6x81axTRfmO1SuyK/soWXLBTHBxj0nEottKLLtHGVLn9rwMKhBQ== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1693975986; a=rsa-sha256; cv=none; b=jwficDSvaCASxPLodpXXebrjhvm1ovEP6xMMUCQeUVHo9Vcyaq0hm4udhFG4WXjEGxtiF3 bmC5qlpwyzjroPZEeCBKtMdb9sEQCz7YOMoYg20fluGX5NjpIy/uU+45Vss9/grUL9WrwV 5Z/0biwbb05XvrQNuzmPy0noQlIdDkfMDiRgXwQqmHcPrctwW+XD3Rn8LWpez8wCD64wlj pHWsPketGU5ow8C+6PxWRwFMug2UypHPT75YuecvFmyC/wjAQ4WyGP6TIFMtT7w/f9Cl/a LArZMNYYrCWiZD9D0V9TL+ugvEEzDXhLmornIVGNSZ45cETFojodP/Dvlc/Mbw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1693975986; 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=nQLWareCi4mMRqHFCazoNtcjfPCBCQNzjCt0mI/9U8U=; b=Zb57r8YWFneaD9R6BcQDG2BCYETPYZtR3FbgjGupTqL3PVbmEZ7nf4ulZu8k8fXpGGHeI+ 0XoHw5O8RQi5aUGxv4OjZly6oWF4ktB8NO9odL48fddBFPLM7FNRHspbY6o/Ze9TOjeqf1 v+oi1mrAbDruB0dpKUfaKdeyXhiFyjVgI5VHAg0PPtTF4Ocen/0yzWm2jzqYemyL/FMhQ9 miOHfnG/NwK3GS8KqiLMKc3OCmmgZOkHRtfK3MpYEf/ES+bZM+n4MMjaVAG0uPtrHBf3qn fyCtiIPDIiKLSmiYGz5KMxmhHv02j+kqyWHeyIsauJ8NS0HPUy0jxEdM4TmmsA== 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 4RgVPF6Jbvz127K; Wed, 6 Sep 2023 04:53:05 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 3864r5IH022272; Wed, 6 Sep 2023 04:53:05 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 3864r5hR022269; Wed, 6 Sep 2023 04:53:05 GMT (envelope-from git) Date: Wed, 6 Sep 2023 04:53:05 GMT Message-Id: <202309060453.3864r5hR022269@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Zhenlei Huang Subject: git: 61b426a0b6a8 - stable/12 - geom_part: Check number of GPT entries and size of GPT entry 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: zlei X-Git-Repository: src X-Git-Refname: refs/heads/stable/12 X-Git-Reftype: branch X-Git-Commit: 61b426a0b6a822c6281016a23f28443bb26b891f Auto-Submitted: auto-generated The branch stable/12 has been updated by zlei: URL: https://cgit.FreeBSD.org/src/commit/?id=61b426a0b6a822c6281016a23f28443bb26b891f commit 61b426a0b6a822c6281016a23f28443bb26b891f Author: Zhenlei Huang AuthorDate: 2022-10-18 15:03:02 +0000 Commit: Zhenlei Huang CommitDate: 2023-09-06 04:32:56 +0000 geom_part: Check number of GPT entries and size of GPT entry Current specification does not have upper limit of the number of partition entries and the size of partition entry. In 799eac8c3df597179bbb3b078362f3ff03993a1a Andrey V. Elsukov introduced a limit maximum number of GPT entries to 4k, but that is for write routine (gpart create) only. When attaching disks that have large number of GPT entries exceeding the limit, or disks with large size of partition entry, it is still possible to exhaust kernel memory. 1. Reuse the limit of the maximum number of partition entries. 2. Limit the maximum size of GPT entry to 1k. In current specification (2.10) the size of GPT entry is 128 * 2^n while n >= 0, and the size - 128 is reserved. 1k should be sufficient enough for foreseen future. PR: 266548 Discussed with: imp Reviewed by: markj MFC after: 1 month Differential Revision: https://reviews.freebsd.org/D36717 (cherry picked from commit 5be5d0d5cb2657d7668f4ca0f8543198cf8d759b) (cherry picked from commit e8c769b22d42fd66d0916e2a04af32ad9d306db1) --- sys/geom/part/g_part_gpt.c | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/sys/geom/part/g_part_gpt.c b/sys/geom/part/g_part_gpt.c index 3d2fb313962f..f0890fd71cd0 100644 --- a/sys/geom/part/g_part_gpt.c +++ b/sys/geom/part/g_part_gpt.c @@ -84,6 +84,7 @@ enum gpt_state { GPT_STATE_MISSING, /* No signature found. */ GPT_STATE_CORRUPT, /* Checksum mismatch. */ GPT_STATE_INVALID, /* Nonconformant/invalid. */ + GPT_STATE_UNSUPPORTED, /* Not supported. */ GPT_STATE_OK /* Perfectly fine. */ }; @@ -147,6 +148,8 @@ static kobj_method_t g_part_gpt_methods[] = { { 0, 0 } }; +#define MAXENTSIZE 1024 + static struct g_part_scheme g_part_gpt_scheme = { "GPT", g_part_gpt_methods, @@ -525,6 +528,11 @@ gpt_read_tbl(struct g_part_gpt_table *table, struct g_consumer *cp, if (hdr == NULL) return (NULL); + if (hdr->hdr_entries > g_part_gpt_scheme.gps_maxent || + hdr->hdr_entsz > MAXENTSIZE) { + table->state[elt] = GPT_STATE_UNSUPPORTED; + return (NULL); + } pp = cp->provider; table->lba[elt] = hdr->hdr_lba_table; @@ -932,10 +940,25 @@ g_part_gpt_read(struct g_part_table *basetable, struct g_consumer *cp) /* Fail if we haven't got any good tables at all. */ if (table->state[GPT_ELT_PRITBL] != GPT_STATE_OK && table->state[GPT_ELT_SECTBL] != GPT_STATE_OK) { - printf("GEOM: %s: corrupt or invalid GPT detected.\n", - pp->name); - printf("GEOM: %s: GPT rejected -- may not be recoverable.\n", - pp->name); + if (table->state[GPT_ELT_PRITBL] == GPT_STATE_UNSUPPORTED && + table->state[GPT_ELT_SECTBL] == GPT_STATE_UNSUPPORTED && + gpt_matched_hdrs(prihdr, sechdr)) { + printf("GEOM: %s: unsupported GPT detected.\n", + pp->name); + printf( + "GEOM: %s: number of GPT entries: %u, entry size: %uB.\n", + pp->name, prihdr->hdr_entries, prihdr->hdr_entsz); + printf( + "GEOM: %s: maximum supported number of GPT entries: %u, entry size: %uB.\n", + pp->name, g_part_gpt_scheme.gps_maxent, MAXENTSIZE); + printf("GEOM: %s: GPT rejected.\n", pp->name); + } else { + printf("GEOM: %s: corrupt or invalid GPT detected.\n", + pp->name); + printf( + "GEOM: %s: GPT rejected -- may not be recoverable.\n", + pp->name); + } if (prihdr != NULL) g_free(prihdr); if (pritbl != NULL)