Skip to content

Commit da19833

Browse files
nathanchancerafaeljw
authored andcommitted
thermal: core: Move initial num_trips assignment before memcpy()
When booting a CONFIG_FORTIFY_SOURCE=y kernel compiled with a toolchain that supports __counted_by() (such as clang-18 and newer), there is a panic on boot: [ 2.913770] memcpy: detected buffer overflow: 72 byte write of buffer size 0 [ 2.920834] WARNING: CPU: 2 PID: 1 at lib/string_helpers.c:1027 __fortify_report+0x5c/0x74 ... [ 3.039208] Call trace: [ 3.041643] __fortify_report+0x5c/0x74 [ 3.045469] __fortify_panic+0x18/0x20 [ 3.049209] thermal_zone_device_register_with_trips+0x4c8/0x4f8 This panic occurs because trips is counted by num_trips but num_trips is assigned after the call to memcpy(), so the fortify checks think the buffer size is zero because tz was allocated with kzalloc(). Move the num_trips assignment before the memcpy() to resolve the panic and ensure that the fortify checks work properly. Fixes: 9b0a627 ("thermal: core: Store zone trips table in struct thermal_zone_device") Signed-off-by: Nathan Chancellor <nathan@kernel.org> Reviewed-by: Kees Cook <keescook@chromium.org> Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
1 parent a85739c commit da19833

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

drivers/thermal/thermal_core.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1354,8 +1354,8 @@ thermal_zone_device_register_with_trips(const char *type,
13541354

13551355
tz->device.class = thermal_class;
13561356
tz->devdata = devdata;
1357-
memcpy(tz->trips, trips, num_trips * sizeof(*trips));
13581357
tz->num_trips = num_trips;
1358+
memcpy(tz->trips, trips, num_trips * sizeof(*trips));
13591359

13601360
thermal_set_delay_jiffies(&tz->passive_delay_jiffies, passive_delay);
13611361
thermal_set_delay_jiffies(&tz->polling_delay_jiffies, polling_delay);

0 commit comments

Comments
 (0)