]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
drm/amd/ras: Fix type size of remainder argument
authorKees Cook <kees@kernel.org>
Wed, 25 Feb 2026 17:47:03 +0000 (09:47 -0800)
committerAlex Deucher <alexander.deucher@amd.com>
Mon, 2 Mar 2026 21:35:30 +0000 (16:35 -0500)
Forcing an int to be dereferenced at uint64_t for div64_u64_rem() runs
the risk of endian confusion and stack overflowing writes. Seen while
preparing to enable -Warray-bounds globally:

In file included from ../arch/x86/include/asm/processor.h:35,
                 from ../include/linux/sched.h:13,
                 from ../include/linux/ratelimit.h:6,
                 from ../include/linux/dev_printk.h:16,
                 from ../drivers/gpu/drm/amd/amdgpu/../ras/ras_mgr/ras_sys.h:29,
                 from ../drivers/gpu/drm/amd/amdgpu/../ras/rascore/ras.h:27,
                 from ../drivers/gpu/drm/amd/amdgpu/../ras/rascore/ras_core.c:24:
In function 'div64_u64_rem',
    inlined from 'ras_core_convert_timestamp_to_time' at ../drivers/gpu/drm/amd/amdgpu/../ras/rascore/ras_core.c:72:9:
../include/linux/math64.h:56:20: error: array subscript 'u64 {aka long long unsigned int}[0]' is partly outside array bounds of 'int[1]' [-Werror=array-bounds=]
   56 |         *remainder = dividend % divisor;
      |         ~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~
../drivers/gpu/drm/amd/amdgpu/../ras/rascore/ras_core.c: In function 'ras_core_convert_timestamp_to_time':
../drivers/gpu/drm/amd/amdgpu/../ras/rascore/ras_core.c:70:19: note: object 'remaining_seconds' of size 4
   70 |         int days, remaining_seconds;
      |                   ^~~~~~~~~~~~~~~~~

Use a 64-bit type for the remainder calculation, but leave
remaining_seconds as 32-bit to avoid 64-bit division later. The value of
remainder will always be less than seconds_per_day, so there's no
truncation risk.

Fixes: ace232eff50e ("drm/amdgpu: Add ras module files into amdgpu")
Signed-off-by: Kees Cook <kees@kernel.org>
Signed-off-by: Alex Deucher <alexander.deucher@amd.com>
drivers/gpu/drm/amd/ras/rascore/ras_core.c

index 3f56f26abd6da6a1197884b55b8b2e983469225f..9df05b3963edb5a0a740719eabceacea20d71fed 100644 (file)
@@ -62,14 +62,16 @@ int ras_core_convert_timestamp_to_time(struct ras_core_context *ras_core,
                        uint64_t timestamp, struct ras_time *tm)
 {
        int days_in_month[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
-       uint64_t month = 0, day = 0, hour = 0, minute = 0, second = 0;
+       uint64_t month = 0, day = 0, hour = 0, minute = 0, second = 0, remainder;
        uint32_t year = 0;
        int seconds_per_day = 24 * 60 * 60;
        int seconds_per_hour = 60 * 60;
        int seconds_per_minute = 60;
        int days, remaining_seconds;
 
-       days = div64_u64_rem(timestamp, seconds_per_day, (uint64_t *)&remaining_seconds);
+       days = div64_u64_rem(timestamp, seconds_per_day, &remainder);
+       /* remainder will always be less than seconds_per_day. */
+       remaining_seconds = remainder;
 
        /* utc_timestamp follows the Unix epoch */
        year = 1970;