drivers/acpi/arm64/gtdt.c | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-)
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 <lpieralisi@kernel.org>
Co-developed-by: Marc Zyngier <maz@kernel.org>
Signed-off-by: Marc Zyngier <maz@kernel.org>
Signed-off-by: Zheng Zengkai <zhengzengkai@huawei.com>
---
Changes in v4:
- remove the tmp pointer to make the code more concise.
Changes in v3:
- based on Marc's patch and reuse the for_each_platform_timer() loop
Link to v3: https://lore.kernel.org/linux-arm-kernel/20241015152602.184108-1-zhengzengkai@huawei.com/
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-zhengzengkai@huawei.com/
Link to previous related patches:
https://lore.kernel.org/all/20241008082429.33646-1-zhengzengkai@huawei.com/
https://lore.kernel.org/all/20240930030716.179992-1-zhengzengkai@huawei.com/
---
drivers/acpi/arm64/gtdt.c | 29 ++++++++++++++++++++---------
1 file changed, 20 insertions(+), 9 deletions(-)
diff --git a/drivers/acpi/arm64/gtdt.c b/drivers/acpi/arm64/gtdt.c
index c0e77c1c8e09..d7c4e1b9915b 100644
--- a/drivers/acpi/arm64/gtdt.c
+++ b/drivers/acpi/arm64/gtdt.c
@@ -36,19 +36,25 @@ struct acpi_gtdt_descriptor {
static struct acpi_gtdt_descriptor acpi_gtdt_desc __initdata;
-static inline __init void *next_platform_timer(void *platform_timer)
+static __init bool platform_timer_valid(void *platform_timer)
{
struct acpi_gtdt_header *gh = platform_timer;
- platform_timer += gh->length;
- if (platform_timer < acpi_gtdt_desc.gtdt_end)
- return platform_timer;
+ return (platform_timer >= (void *)(acpi_gtdt_desc.gtdt + 1) &&
+ platform_timer < acpi_gtdt_desc.gtdt_end &&
+ gh->length != 0 &&
+ platform_timer + gh->length <= acpi_gtdt_desc.gtdt_end);
+}
+
+static __init void *next_platform_timer(void *platform_timer)
+{
+ struct acpi_gtdt_header *gh = platform_timer;
- return NULL;
+ return platform_timer + gh->length;
}
#define for_each_platform_timer(_g) \
- for (_g = acpi_gtdt_desc.platform_timer; _g; \
+ for (_g = acpi_gtdt_desc.platform_timer; platform_timer_valid(_g);\
_g = next_platform_timer(_g))
static inline bool is_timer_block(void *platform_timer)
@@ -157,6 +163,7 @@ int __init acpi_gtdt_init(struct acpi_table_header *table,
{
void *platform_timer;
struct acpi_table_gtdt *gtdt;
+ int cnt = 0;
gtdt = container_of(table, struct acpi_table_gtdt, header);
acpi_gtdt_desc.gtdt = gtdt;
@@ -176,12 +183,16 @@ int __init acpi_gtdt_init(struct acpi_table_header *table,
return 0;
}
- platform_timer = (void *)gtdt + gtdt->platform_timer_offset;
- if (platform_timer < (void *)table + sizeof(struct acpi_table_gtdt)) {
+ acpi_gtdt_desc.platform_timer = (void *)gtdt + gtdt->platform_timer_offset;
+ for_each_platform_timer(platform_timer)
+ cnt++;
+
+ if (cnt != gtdt->platform_timer_count) {
+ acpi_gtdt_desc.platform_timer = NULL;
pr_err(FW_BUG "invalid timer data.\n");
return -EINVAL;
}
- acpi_gtdt_desc.platform_timer = platform_timer;
+
if (platform_timer_count)
*platform_timer_count = gtdt->platform_timer_count;
--
2.20.1
On Wed, 16 Oct 2024 17:54:58 +0800, Zheng Zengkai wrote: > 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. > > [...] Applied to arm64 (for-next/misc), thanks! [1/1] ACPI: GTDT: Tighten the check for the array of platform timer structures https://git.kernel.org/arm64/c/263e22d6bd1f -- Catalin
On 2024/10/16 17:54, Zheng Zengkai wrote: > 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 <lpieralisi@kernel.org> > Co-developed-by: Marc Zyngier <maz@kernel.org> > Signed-off-by: Marc Zyngier <maz@kernel.org> Nit: since there is a "Co-developed-by:" for Marc, the "Signed-off-by:" can be removed. The rest of the patch looks good to me. I did a test again Kunpeng ARM sever and no regressions, hopefully will not trigger firmware bugs for other platforms. Reviewed-by: Hanjun Guo <guohanjun@huawei.com> Tested-by: Hanjun Guo <guohanjun@huawei.com> Thanks Hanjun
On 2024/10/19 14:41, Hanjun Guo wrote: > On 2024/10/16 17:54, Zheng Zengkai wrote: >> 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 <lpieralisi@kernel.org> >> Co-developed-by: Marc Zyngier <maz@kernel.org> >> Signed-off-by: Marc Zyngier <maz@kernel.org> > > Nit: since there is a "Co-developed-by:" for Marc, the > "Signed-off-by:" can be removed. Forget about this comment, the guide from submit patches needs the Signed-off-by:, sorry for the noise. Thanks Hanjun
On Wed, Oct 16, 2024 at 05:54:58PM +0800, Zheng Zengkai wrote: > 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 <lpieralisi@kernel.org> > Co-developed-by: Marc Zyngier <maz@kernel.org> > Signed-off-by: Marc Zyngier <maz@kernel.org> > Signed-off-by: Zheng Zengkai <zhengzengkai@huawei.com> > --- > Changes in v4: > - remove the tmp pointer to make the code more concise. > > Changes in v3: > - based on Marc's patch and reuse the for_each_platform_timer() loop > Link to v3: https://lore.kernel.org/linux-arm-kernel/20241015152602.184108-1-zhengzengkai@huawei.com/ > > 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-zhengzengkai@huawei.com/ > > Link to previous related patches: > https://lore.kernel.org/all/20241008082429.33646-1-zhengzengkai@huawei.com/ > https://lore.kernel.org/all/20240930030716.179992-1-zhengzengkai@huawei.com/ > --- > drivers/acpi/arm64/gtdt.c | 29 ++++++++++++++++++++--------- > 1 file changed, 20 insertions(+), 9 deletions(-) Reviewed-by: Lorenzo Pieralisi <lpieralisi@kernel.org> > diff --git a/drivers/acpi/arm64/gtdt.c b/drivers/acpi/arm64/gtdt.c > index c0e77c1c8e09..d7c4e1b9915b 100644 > --- a/drivers/acpi/arm64/gtdt.c > +++ b/drivers/acpi/arm64/gtdt.c > @@ -36,19 +36,25 @@ struct acpi_gtdt_descriptor { > > static struct acpi_gtdt_descriptor acpi_gtdt_desc __initdata; > > -static inline __init void *next_platform_timer(void *platform_timer) > +static __init bool platform_timer_valid(void *platform_timer) > { > struct acpi_gtdt_header *gh = platform_timer; > > - platform_timer += gh->length; > - if (platform_timer < acpi_gtdt_desc.gtdt_end) > - return platform_timer; > + return (platform_timer >= (void *)(acpi_gtdt_desc.gtdt + 1) && > + platform_timer < acpi_gtdt_desc.gtdt_end && > + gh->length != 0 && > + platform_timer + gh->length <= acpi_gtdt_desc.gtdt_end); > +} > + > +static __init void *next_platform_timer(void *platform_timer) > +{ > + struct acpi_gtdt_header *gh = platform_timer; > > - return NULL; > + return platform_timer + gh->length; > } > > #define for_each_platform_timer(_g) \ > - for (_g = acpi_gtdt_desc.platform_timer; _g; \ > + for (_g = acpi_gtdt_desc.platform_timer; platform_timer_valid(_g);\ > _g = next_platform_timer(_g)) > > static inline bool is_timer_block(void *platform_timer) > @@ -157,6 +163,7 @@ int __init acpi_gtdt_init(struct acpi_table_header *table, > { > void *platform_timer; > struct acpi_table_gtdt *gtdt; > + int cnt = 0; > > gtdt = container_of(table, struct acpi_table_gtdt, header); > acpi_gtdt_desc.gtdt = gtdt; > @@ -176,12 +183,16 @@ int __init acpi_gtdt_init(struct acpi_table_header *table, > return 0; > } > > - platform_timer = (void *)gtdt + gtdt->platform_timer_offset; > - if (platform_timer < (void *)table + sizeof(struct acpi_table_gtdt)) { > + acpi_gtdt_desc.platform_timer = (void *)gtdt + gtdt->platform_timer_offset; > + for_each_platform_timer(platform_timer) > + cnt++; > + > + if (cnt != gtdt->platform_timer_count) { > + acpi_gtdt_desc.platform_timer = NULL; > pr_err(FW_BUG "invalid timer data.\n"); > return -EINVAL; > } > - acpi_gtdt_desc.platform_timer = platform_timer; > + > if (platform_timer_count) > *platform_timer_count = gtdt->platform_timer_count; > > -- > 2.20.1 >
Gentle ping. This patch still can be applied to upstream now. Thanks! 在 2024/10/16 18:01, Lorenzo Pieralisi 写道: > On Wed, Oct 16, 2024 at 05:54:58PM +0800, Zheng Zengkai wrote: >> 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 <lpieralisi@kernel.org> >> Co-developed-by: Marc Zyngier <maz@kernel.org> >> Signed-off-by: Marc Zyngier <maz@kernel.org> >> Signed-off-by: Zheng Zengkai <zhengzengkai@huawei.com> >> --- >> Changes in v4: >> - remove the tmp pointer to make the code more concise. >> >> Changes in v3: >> - based on Marc's patch and reuse the for_each_platform_timer() loop >> Link to v3: https://lore.kernel.org/linux-arm-kernel/20241015152602.184108-1-zhengzengkai@huawei.com/ >> >> 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-zhengzengkai@huawei.com/ >> >> Link to previous related patches: >> https://lore.kernel.org/all/20241008082429.33646-1-zhengzengkai@huawei.com/ >> https://lore.kernel.org/all/20240930030716.179992-1-zhengzengkai@huawei.com/ >> --- >> drivers/acpi/arm64/gtdt.c | 29 ++++++++++++++++++++--------- >> 1 file changed, 20 insertions(+), 9 deletions(-) > Reviewed-by: Lorenzo Pieralisi <lpieralisi@kernel.org> > >> diff --git a/drivers/acpi/arm64/gtdt.c b/drivers/acpi/arm64/gtdt.c >> index c0e77c1c8e09..d7c4e1b9915b 100644 >> --- a/drivers/acpi/arm64/gtdt.c >> +++ b/drivers/acpi/arm64/gtdt.c >> @@ -36,19 +36,25 @@ struct acpi_gtdt_descriptor { >> >> static struct acpi_gtdt_descriptor acpi_gtdt_desc __initdata; >> >> -static inline __init void *next_platform_timer(void *platform_timer) >> +static __init bool platform_timer_valid(void *platform_timer) >> { >> struct acpi_gtdt_header *gh = platform_timer; >> >> - platform_timer += gh->length; >> - if (platform_timer < acpi_gtdt_desc.gtdt_end) >> - return platform_timer; >> + return (platform_timer >= (void *)(acpi_gtdt_desc.gtdt + 1) && >> + platform_timer < acpi_gtdt_desc.gtdt_end && >> + gh->length != 0 && >> + platform_timer + gh->length <= acpi_gtdt_desc.gtdt_end); >> +} >> + >> +static __init void *next_platform_timer(void *platform_timer) >> +{ >> + struct acpi_gtdt_header *gh = platform_timer; >> >> - return NULL; >> + return platform_timer + gh->length; >> } >> >> #define for_each_platform_timer(_g) \ >> - for (_g = acpi_gtdt_desc.platform_timer; _g; \ >> + for (_g = acpi_gtdt_desc.platform_timer; platform_timer_valid(_g);\ >> _g = next_platform_timer(_g)) >> >> static inline bool is_timer_block(void *platform_timer) >> @@ -157,6 +163,7 @@ int __init acpi_gtdt_init(struct acpi_table_header *table, >> { >> void *platform_timer; >> struct acpi_table_gtdt *gtdt; >> + int cnt = 0; >> >> gtdt = container_of(table, struct acpi_table_gtdt, header); >> acpi_gtdt_desc.gtdt = gtdt; >> @@ -176,12 +183,16 @@ int __init acpi_gtdt_init(struct acpi_table_header *table, >> return 0; >> } >> >> - platform_timer = (void *)gtdt + gtdt->platform_timer_offset; >> - if (platform_timer < (void *)table + sizeof(struct acpi_table_gtdt)) { >> + acpi_gtdt_desc.platform_timer = (void *)gtdt + gtdt->platform_timer_offset; >> + for_each_platform_timer(platform_timer) >> + cnt++; >> + >> + if (cnt != gtdt->platform_timer_count) { >> + acpi_gtdt_desc.platform_timer = NULL; >> pr_err(FW_BUG "invalid timer data.\n"); >> return -EINVAL; >> } >> - acpi_gtdt_desc.platform_timer = platform_timer; >> + >> if (platform_timer_count) >> *platform_timer_count = gtdt->platform_timer_count; >> >> -- >> 2.20.1 >> > . >
[+Catalin/Will] On Tue, Oct 29, 2024 at 10:16:41PM +0800, Zheng Zengkai wrote: > > Gentle ping. > > This patch still can be applied to upstream now. Hi Catalin, Will, this patch is a clean-up - not a fix, it could be merged for v6.13, please consider it if there is still time. Thanks, Lorenzo > > Thanks! > > > 在 2024/10/16 18:01, Lorenzo Pieralisi 写道: > > On Wed, Oct 16, 2024 at 05:54:58PM +0800, Zheng Zengkai wrote: > > > 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 <lpieralisi@kernel.org> > > > Co-developed-by: Marc Zyngier <maz@kernel.org> > > > Signed-off-by: Marc Zyngier <maz@kernel.org> > > > Signed-off-by: Zheng Zengkai <zhengzengkai@huawei.com> > > > --- > > > Changes in v4: > > > - remove the tmp pointer to make the code more concise. > > > > > > Changes in v3: > > > - based on Marc's patch and reuse the for_each_platform_timer() loop > > > Link to v3: https://lore.kernel.org/linux-arm-kernel/20241015152602.184108-1-zhengzengkai@huawei.com/ > > > > > > 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-zhengzengkai@huawei.com/ > > > > > > Link to previous related patches: > > > https://lore.kernel.org/all/20241008082429.33646-1-zhengzengkai@huawei.com/ > > > https://lore.kernel.org/all/20240930030716.179992-1-zhengzengkai@huawei.com/ > > > --- > > > drivers/acpi/arm64/gtdt.c | 29 ++++++++++++++++++++--------- > > > 1 file changed, 20 insertions(+), 9 deletions(-) > > Reviewed-by: Lorenzo Pieralisi <lpieralisi@kernel.org> > > > > > diff --git a/drivers/acpi/arm64/gtdt.c b/drivers/acpi/arm64/gtdt.c > > > index c0e77c1c8e09..d7c4e1b9915b 100644 > > > --- a/drivers/acpi/arm64/gtdt.c > > > +++ b/drivers/acpi/arm64/gtdt.c > > > @@ -36,19 +36,25 @@ struct acpi_gtdt_descriptor { > > > static struct acpi_gtdt_descriptor acpi_gtdt_desc __initdata; > > > -static inline __init void *next_platform_timer(void *platform_timer) > > > +static __init bool platform_timer_valid(void *platform_timer) > > > { > > > struct acpi_gtdt_header *gh = platform_timer; > > > - platform_timer += gh->length; > > > - if (platform_timer < acpi_gtdt_desc.gtdt_end) > > > - return platform_timer; > > > + return (platform_timer >= (void *)(acpi_gtdt_desc.gtdt + 1) && > > > + platform_timer < acpi_gtdt_desc.gtdt_end && > > > + gh->length != 0 && > > > + platform_timer + gh->length <= acpi_gtdt_desc.gtdt_end); > > > +} > > > + > > > +static __init void *next_platform_timer(void *platform_timer) > > > +{ > > > + struct acpi_gtdt_header *gh = platform_timer; > > > - return NULL; > > > + return platform_timer + gh->length; > > > } > > > #define for_each_platform_timer(_g) \ > > > - for (_g = acpi_gtdt_desc.platform_timer; _g; \ > > > + for (_g = acpi_gtdt_desc.platform_timer; platform_timer_valid(_g);\ > > > _g = next_platform_timer(_g)) > > > static inline bool is_timer_block(void *platform_timer) > > > @@ -157,6 +163,7 @@ int __init acpi_gtdt_init(struct acpi_table_header *table, > > > { > > > void *platform_timer; > > > struct acpi_table_gtdt *gtdt; > > > + int cnt = 0; > > > gtdt = container_of(table, struct acpi_table_gtdt, header); > > > acpi_gtdt_desc.gtdt = gtdt; > > > @@ -176,12 +183,16 @@ int __init acpi_gtdt_init(struct acpi_table_header *table, > > > return 0; > > > } > > > - platform_timer = (void *)gtdt + gtdt->platform_timer_offset; > > > - if (platform_timer < (void *)table + sizeof(struct acpi_table_gtdt)) { > > > + acpi_gtdt_desc.platform_timer = (void *)gtdt + gtdt->platform_timer_offset; > > > + for_each_platform_timer(platform_timer) > > > + cnt++; > > > + > > > + if (cnt != gtdt->platform_timer_count) { > > > + acpi_gtdt_desc.platform_timer = NULL; > > > pr_err(FW_BUG "invalid timer data.\n"); > > > return -EINVAL; > > > } > > > - acpi_gtdt_desc.platform_timer = platform_timer; > > > + > > > if (platform_timer_count) > > > *platform_timer_count = gtdt->platform_timer_count; > > > -- > > > 2.20.1 > > > > > . > >
© 2016 - 2024 Red Hat, Inc.