Skip to content

Commit 779aa4d

Browse files
groeckdakr
authored andcommitted
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: a61ddb4 ("drm: enable (most) W=1 warnings by default across the subsystem") Cc: Javier Martinez Canillas <[email protected]> Cc: Jani Nikula <[email protected]> Cc: Thomas Zimmermann <[email protected]> Cc: Danilo Krummrich <[email protected]> Cc: Maxime Ripard <[email protected]> Cc: Kees Cook <[email protected]> Cc: Christophe JAILLET <[email protected]> Cc: Joe Perches <[email protected]> Reviewed-by: Kees Cook <[email protected]> Signed-off-by: Guenter Roeck <[email protected]> Signed-off-by: Danilo Krummrich <[email protected]> Link: https://patchwork.freedesktop.org/patch/msgid/[email protected]
1 parent b794918 commit 779aa4d

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

drivers/gpu/drm/nouveau/nvif/object.c

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,16 @@ nvif_object_mthd(struct nvif_object *object, u32 mthd, void *data, u32 size)
142142
struct nvif_ioctl_v0 ioctl;
143143
struct nvif_ioctl_mthd_v0 mthd;
144144
} *args;
145+
u32 args_size;
145146
u8 stack[128];
146147
int ret;
147148

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)
150155
return -ENOMEM;
151156
} else {
152157
args = (void *)stack;
@@ -157,7 +162,7 @@ nvif_object_mthd(struct nvif_object *object, u32 mthd, void *data, u32 size)
157162
args->mthd.method = mthd;
158163

159164
memcpy(args->mthd.data, data, size);
160-
ret = nvif_object_ioctl(object, args, sizeof(*args) + size, NULL);
165+
ret = nvif_object_ioctl(object, args, args_size, NULL);
161166
memcpy(data, args->mthd.data, size);
162167
if (args != (void *)stack)
163168
kfree(args);
@@ -276,7 +281,15 @@ nvif_object_ctor(struct nvif_object *parent, const char *name, u32 handle,
276281
object->map.size = 0;
277282

278283
if (parent) {
279-
if (!(args = kmalloc(sizeof(*args) + size, GFP_KERNEL))) {
284+
u32 args_size;
285+
286+
if (check_add_overflow(sizeof(*args), size, &args_size)) {
287+
nvif_object_dtor(object);
288+
return -ENOMEM;
289+
}
290+
291+
args = kmalloc(args_size, GFP_KERNEL);
292+
if (!args) {
280293
nvif_object_dtor(object);
281294
return -ENOMEM;
282295
}
@@ -293,8 +306,7 @@ nvif_object_ctor(struct nvif_object *parent, const char *name, u32 handle,
293306
args->new.oclass = oclass;
294307

295308
memcpy(args->new.data, data, size);
296-
ret = nvif_object_ioctl(parent, args, sizeof(*args) + size,
297-
&object->priv);
309+
ret = nvif_object_ioctl(parent, args, args_size, &object->priv);
298310
memcpy(data, args->new.data, size);
299311
kfree(args);
300312
if (ret == 0)

0 commit comments

Comments
 (0)