From nobody Tue Nov 26 21:36:49 2024 Received: from szxga05-in.huawei.com (szxga05-in.huawei.com [45.249.212.191]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BC7791FC7EC; Tue, 15 Oct 2024 14:52:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.191 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729003956; cv=none; b=NqcI7LQHkVtoubW0V2pD/NOAZVUkvyqs0TchcFQSxh6xZKSXnq963XLlXfRdy+7krtUxm+x7PhkBCcaYdyZrgxzYnX3FdDdCTUsGSte6IFewKj0pHZSvYO34jOf/GVxTkQ2b++WvBsxxwt5gVYza0lrP32fAzsV9XjwDMb8y3mA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729003956; c=relaxed/simple; bh=/HR8iP94s0idk8HNyY5fUA88vostpaZn0pIs5HRvDpk=; h=From:To:CC:Subject:Date:Message-ID:MIME-Version:Content-Type; b=Cq1187UsPSPODkTdNhYXVA8ZFO9SGZNueMRUidK8LVn5AU/TQbc3V4ek6N/xeXkdc9AmxWk1KdtTbAIII+2j+UTS8AXhB+e/xwXThgoBMKaSTGWN6AVSoy3OO5bpDONMxdA2QNgoMPdwtkceJ9LOlWO/97ukLSvE3jj+SCfu/pQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com; spf=pass smtp.mailfrom=huawei.com; arc=none smtp.client-ip=45.249.212.191 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huawei.com Received: from mail.maildlp.com (unknown [172.19.163.17]) by szxga05-in.huawei.com (SkyGuard) with ESMTP id 4XScR75WqCz1HL2t; Tue, 15 Oct 2024 22:48:19 +0800 (CST) Received: from kwepemd200010.china.huawei.com (unknown [7.221.188.124]) by mail.maildlp.com (Postfix) with ESMTPS id 333BB1A0188; Tue, 15 Oct 2024 22:52:32 +0800 (CST) Received: from huawei.com (10.175.113.25) by kwepemd200010.china.huawei.com (7.221.188.124) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1258.34; Tue, 15 Oct 2024 22:52:31 +0800 From: Zheng Zengkai To: , , , , , , CC: , , , , , Subject: [PATCH v3] ACPI: GTDT: Tighten the check for the array of platform timer structures Date: Tue, 15 Oct 2024 23:26:02 +0800 Message-ID: <20241015152602.184108-1-zhengzengkai@huawei.com> X-Mailer: git-send-email 2.20.1 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: dggems705-chm.china.huawei.com (10.3.19.182) To kwepemd200010.china.huawei.com (7.221.188.124) Content-Type: text/plain; charset="utf-8" As suggested by Marc and Lorenzo, first we need to check whether the platform_timer entry pointer is within gtdt bounds (< gtdt_end) before de-referencing what it points at to detect the length of the platform timer struct and then check that the length of current platform_timer struct is also valid, i.e. the length is not zero and within gtdt_end. Now next_platform_timer() only checks against gtdt_end for the entry of subsequent platform timer without checking the length of it and will not report error if the check failed and the existing check in function acpi_gtdt_init() is also not enough. Modify the for_each_platform_timer() iterator and use it combined with a dedicated check function platform_timer_valid() to do the check against table length (gtdt_end) for each element of platform timer array in function acpi_gtdt_init(), making sure that both their entry and length actually fit in the table. Suggested-by: Lorenzo Pieralisi Co-developed-by: Marc Zyngier Signed-off-by: Marc Zyngier Signed-off-by: Zheng Zengkai Reviewed-by: Lorenzo Pieralisi --- Changes in v3: - based on Marc's patch and reuse the for_each_platform_timer() loop Changes in v2: - Check against gtdt_end for both entry and len of each array element Link to v2: https://lore.kernel.org/linux-arm-kernel/20241012085343.6594-1-= zhengzengkai@huawei.com/ Link to v1: https://lore.kernel.org/all/20241010144703.113728-1-zhengzengka= i@huawei.com/ --- drivers/acpi/arm64/gtdt.c | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/drivers/acpi/arm64/gtdt.c b/drivers/acpi/arm64/gtdt.c index c0e77c1c8e09..3583c99afb0d 100644 --- a/drivers/acpi/arm64/gtdt.c +++ b/drivers/acpi/arm64/gtdt.c @@ -36,19 +36,25 @@ struct acpi_gtdt_descriptor { =20 static struct acpi_gtdt_descriptor acpi_gtdt_desc __initdata; =20 -static inline __init void *next_platform_timer(void *platform_timer) +static __init bool platform_timer_valid(void *platform_timer) { struct acpi_gtdt_header *gh =3D platform_timer; =20 - platform_timer +=3D gh->length; - if (platform_timer < acpi_gtdt_desc.gtdt_end) - return platform_timer; + return (platform_timer >=3D (void *)(acpi_gtdt_desc.gtdt + 1) && + platform_timer < acpi_gtdt_desc.gtdt_end && + gh->length !=3D 0 && + platform_timer + gh->length <=3D acpi_gtdt_desc.gtdt_end); +} + +static __init void *next_platform_timer(void *platform_timer) +{ + struct acpi_gtdt_header *gh =3D platform_timer; =20 - return NULL; + return platform_timer + gh->length; } =20 -#define for_each_platform_timer(_g) \ - for (_g =3D acpi_gtdt_desc.platform_timer; _g; \ +#define for_each_platform_timer(_g, first_entry) \ + for (_g =3D first_entry; platform_timer_valid(_g); \ _g =3D next_platform_timer(_g)) =20 static inline bool is_timer_block(void *platform_timer) @@ -155,8 +161,9 @@ bool __init acpi_gtdt_c3stop(int type) int __init acpi_gtdt_init(struct acpi_table_header *table, int *platform_timer_count) { - void *platform_timer; + void *platform_timer, *tmp; struct acpi_table_gtdt *gtdt; + int cnt =3D 0; =20 gtdt =3D container_of(table, struct acpi_table_gtdt, header); acpi_gtdt_desc.gtdt =3D gtdt; @@ -177,7 +184,10 @@ int __init acpi_gtdt_init(struct acpi_table_header *ta= ble, } =20 platform_timer =3D (void *)gtdt + gtdt->platform_timer_offset; - if (platform_timer < (void *)table + sizeof(struct acpi_table_gtdt)) { + for_each_platform_timer(tmp, platform_timer) + cnt++; + + if (cnt !=3D gtdt->platform_timer_count) { pr_err(FW_BUG "invalid timer data.\n"); return -EINVAL; } @@ -305,7 +315,7 @@ int __init acpi_arch_timer_mem_init(struct arch_timer_m= em *timer_mem, void *platform_timer; =20 *timer_count =3D 0; - for_each_platform_timer(platform_timer) { + for_each_platform_timer(platform_timer, acpi_gtdt_desc.platform_timer) { if (is_timer_block(platform_timer)) { ret =3D gtdt_parse_timer_block(platform_timer, timer_mem); if (ret) @@ -398,7 +408,7 @@ static int __init gtdt_sbsa_gwdt_init(void) if (ret || !timer_count) goto out_put_gtdt; =20 - for_each_platform_timer(platform_timer) { + for_each_platform_timer(platform_timer, acpi_gtdt_desc.platform_timer) { if (is_non_secure_watchdog(platform_timer)) { ret =3D gtdt_import_sbsa_gwdt(platform_timer, gwdt_count); if (ret) --=20 2.20.1