Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

drm/nouveau/nvif: Avoid build error due to potential integer overflows

Trying to build parisc:allmodconfig with gcc 12.x or later results
in the following build error.

drivers/gpu/drm/nouveau/nvif/object.c: In function 'nvif_object_mthd':
drivers/gpu/drm/nouveau/nvif/object.c:161:9: error:
'memcpy' accessing 4294967264 or more bytes at offsets 0 and 32 overlaps 6442450881 bytes at offset -2147483617 [-Werror=restrict]
161 | memcpy(data, args->mthd.data, size);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
drivers/gpu/drm/nouveau/nvif/object.c: In function 'nvif_object_ctor':
drivers/gpu/drm/nouveau/nvif/object.c:298:17: error:
'memcpy' accessing 4294967240 or more bytes at offsets 0 and 56 overlaps 6442450833 bytes at offset -2147483593 [-Werror=restrict]
298 | memcpy(data, args->new.data, size);

gcc assumes that 'sizeof(*args) + size' can overflow, which would result
in the problem.

The problem is not new, only it is now no longer a warning but an error
since W=1 has been enabled for the drm subsystem and since Werror is
enabled for test builds.

Rearrange arithmetic and use check_add_overflow() for validating the
allocation size to avoid the overflow. While at it, split assignments
out of if conditions.

Fixes: a61ddb4393ad ("drm: enable (most) W=1 warnings by default across the subsystem")
Cc: Javier Martinez Canillas <javierm@redhat.com>
Cc: Jani Nikula <jani.nikula@intel.com>
Cc: Thomas Zimmermann <tzimmermann@suse.de>
Cc: Danilo Krummrich <dakr@redhat.com>
Cc: Maxime Ripard <mripard@kernel.org>
Cc: Kees Cook <keescook@chromium.org>
Cc: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
Cc: Joe Perches <joe@perches.com>
Reviewed-by: Kees Cook <keescook@chromium.org>
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
Signed-off-by: Danilo Krummrich <dakr@redhat.com>
Link: https://patchwork.freedesktop.org/patch/msgid/20240524134817.1369993-1-linux@roeck-us.net

authored by

Guenter Roeck and committed by
Danilo Krummrich
779aa4d7 b7949189

+18 -6
+18 -6
drivers/gpu/drm/nouveau/nvif/object.c
··· 142 142 struct nvif_ioctl_v0 ioctl; 143 143 struct nvif_ioctl_mthd_v0 mthd; 144 144 } *args; 145 + u32 args_size; 145 146 u8 stack[128]; 146 147 int ret; 147 148 148 - if (sizeof(*args) + size > sizeof(stack)) { 149 - if (!(args = kmalloc(sizeof(*args) + size, GFP_KERNEL))) 149 + if (check_add_overflow(sizeof(*args), size, &args_size)) 150 + return -ENOMEM; 151 + 152 + if (args_size > sizeof(stack)) { 153 + args = kmalloc(args_size, GFP_KERNEL); 154 + if (!args) 150 155 return -ENOMEM; 151 156 } else { 152 157 args = (void *)stack; ··· 162 157 args->mthd.method = mthd; 163 158 164 159 memcpy(args->mthd.data, data, size); 165 - ret = nvif_object_ioctl(object, args, sizeof(*args) + size, NULL); 160 + ret = nvif_object_ioctl(object, args, args_size, NULL); 166 161 memcpy(data, args->mthd.data, size); 167 162 if (args != (void *)stack) 168 163 kfree(args); ··· 281 276 object->map.size = 0; 282 277 283 278 if (parent) { 284 - if (!(args = kmalloc(sizeof(*args) + size, GFP_KERNEL))) { 279 + u32 args_size; 280 + 281 + if (check_add_overflow(sizeof(*args), size, &args_size)) { 282 + nvif_object_dtor(object); 283 + return -ENOMEM; 284 + } 285 + 286 + args = kmalloc(args_size, GFP_KERNEL); 287 + if (!args) { 285 288 nvif_object_dtor(object); 286 289 return -ENOMEM; 287 290 } ··· 306 293 args->new.oclass = oclass; 307 294 308 295 memcpy(args->new.data, data, size); 309 - ret = nvif_object_ioctl(parent, args, sizeof(*args) + size, 310 - &object->priv); 296 + ret = nvif_object_ioctl(parent, args, args_size, &object->priv); 311 297 memcpy(data, args->new.data, size); 312 298 kfree(args); 313 299 if (ret == 0)