* [PATCH] drm/nouveau/nvif: Avoid build error due to potential integer overflows
@ 2024-05-18 14:37 Guenter Roeck
2024-05-18 16:54 ` Christophe JAILLET
0 siblings, 1 reply; 6+ messages in thread
From: Guenter Roeck @ 2024-05-18 14:37 UTC (permalink / raw)
To: David Airlie
Cc: Karol Herbst, Lyude Paul, Daniel Vetter, dri-devel, nouveau,
linux-kernel, Guenter Roeck, Javier Martinez Canillas,
Jani Nikula, Thomas Zimmermann, Danilo Krummrich, Maxime Ripard
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 add extra size checks to avoid the overflow.
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>
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
---
checkpatch complains about the line length in the description and the (pre-existing)
assignlemts in if conditions, but I did not want to split lines in the description
or rearrange the code further.
I don't know why I only see the problem with parisc builds (at least so far).
drivers/gpu/drm/nouveau/nvif/object.c | 8 +++++---
1 file changed, 5 insertions(+), 3 deletions(-)
diff --git a/drivers/gpu/drm/nouveau/nvif/object.c b/drivers/gpu/drm/nouveau/nvif/object.c
index 4d1aaee8fe15..baf623a48874 100644
--- a/drivers/gpu/drm/nouveau/nvif/object.c
+++ b/drivers/gpu/drm/nouveau/nvif/object.c
@@ -145,8 +145,9 @@ nvif_object_mthd(struct nvif_object *object, u32 mthd, void *data, u32 size)
u8 stack[128];
int ret;
- if (sizeof(*args) + size > sizeof(stack)) {
- if (!(args = kmalloc(sizeof(*args) + size, GFP_KERNEL)))
+ if (size > sizeof(stack) - sizeof(*args)) {
+ if (size > INT_MAX ||
+ !(args = kmalloc(sizeof(*args) + size, GFP_KERNEL)))
return -ENOMEM;
} else {
args = (void *)stack;
@@ -276,7 +277,8 @@ nvif_object_ctor(struct nvif_object *parent, const char *name, u32 handle,
object->map.size = 0;
if (parent) {
- if (!(args = kmalloc(sizeof(*args) + size, GFP_KERNEL))) {
+ if (size > INT_MAX ||
+ !(args = kmalloc(sizeof(*args) + size, GFP_KERNEL))) {
nvif_object_dtor(object);
return -ENOMEM;
}
--
2.39.2
^ permalink raw reply related [flat|nested] 6+ messages in thread* Re: [PATCH] drm/nouveau/nvif: Avoid build error due to potential integer overflows 2024-05-18 14:37 [PATCH] drm/nouveau/nvif: Avoid build error due to potential integer overflows Guenter Roeck @ 2024-05-18 16:54 ` Christophe JAILLET 2024-05-18 17:32 ` Kees Cook 0 siblings, 1 reply; 6+ messages in thread From: Christophe JAILLET @ 2024-05-18 16:54 UTC (permalink / raw) To: Guenter Roeck Cc: airlied, dakr, daniel, dri-devel, jani.nikula, javierm, kherbst, linux-kernel, lyude, mripard, nouveau, tzimmermann, linux-hardening (adding linux-hardening@vger.kernel.org) Le 18/05/2024 à 16:37, Guenter Roeck a écrit : > 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 add extra size checks to avoid the overflow. > > Fixes: a61ddb4393ad ("drm: enable (most) W=1 warnings by default across the subsystem") > Cc: Javier Martinez Canillas <javierm-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org> > Cc: Jani Nikula <jani.nikula-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org> > Cc: Thomas Zimmermann <tzimmermann-l3A5Bk7waGM@public.gmane.org> > Cc: Danilo Krummrich <dakr-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org> > Cc: Maxime Ripard <mripard-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org> > Signed-off-by: Guenter Roeck <linux-0h96xk9xTtrk1uMJSBkQmQ@public.gmane.org> > --- > checkpatch complains about the line length in the description and the (pre-existing) > assignlemts in if conditions, but I did not want to split lines in the description > or rearrange the code further. > > I don't know why I only see the problem with parisc builds (at least so far). > > drivers/gpu/drm/nouveau/nvif/object.c | 8 +++++--- > 1 file changed, 5 insertions(+), 3 deletions(-) > > diff --git a/drivers/gpu/drm/nouveau/nvif/object.c b/drivers/gpu/drm/nouveau/nvif/object.c > index 4d1aaee8fe15..baf623a48874 100644 > --- a/drivers/gpu/drm/nouveau/nvif/object.c > +++ b/drivers/gpu/drm/nouveau/nvif/object.c > @@ -145,8 +145,9 @@ nvif_object_mthd(struct nvif_object *object, u32 mthd, void *data, u32 size) > u8 stack[128]; > int ret; > > - if (sizeof(*args) + size > sizeof(stack)) { > - if (!(args = kmalloc(sizeof(*args) + size, GFP_KERNEL))) > + if (size > sizeof(stack) - sizeof(*args)) { > + if (size > INT_MAX || > + !(args = kmalloc(sizeof(*args) + size, GFP_KERNEL))) Hi, Would it be cleaner or better to use size_add(sizeof(*args), size)? > return -ENOMEM; > } else { > args = (void *)stack; > @@ -276,7 +277,8 @@ nvif_object_ctor(struct nvif_object *parent, const char *name, u32 handle, > object->map.size = 0; > > if (parent) { > - if (!(args = kmalloc(sizeof(*args) + size, GFP_KERNEL))) { > + if (size > INT_MAX || > + !(args = kmalloc(sizeof(*args) + size, GFP_KERNEL))) { Same. CJ > nvif_object_dtor(object); > return -ENOMEM; > } ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] drm/nouveau/nvif: Avoid build error due to potential integer overflows 2024-05-18 16:54 ` Christophe JAILLET @ 2024-05-18 17:32 ` Kees Cook 2024-05-18 18:23 ` Guenter Roeck 0 siblings, 1 reply; 6+ messages in thread From: Kees Cook @ 2024-05-18 17:32 UTC (permalink / raw) To: Christophe JAILLET Cc: Guenter Roeck, airlied, dakr, daniel, dri-devel, jani.nikula, javierm, kherbst, linux-kernel, lyude, mripard, nouveau, tzimmermann, linux-hardening On Sat, May 18, 2024 at 06:54:36PM +0200, Christophe JAILLET wrote: > (adding linux-hardening@vger.kernel.org) > > > Le 18/05/2024 à 16:37, Guenter Roeck a écrit : > > 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 add extra size checks to avoid the overflow. > > > > Fixes: a61ddb4393ad ("drm: enable (most) W=1 warnings by default across the subsystem") > > Cc: Javier Martinez Canillas <javierm-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org> > > Cc: Jani Nikula <jani.nikula-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org> > > Cc: Thomas Zimmermann <tzimmermann-l3A5Bk7waGM@public.gmane.org> > > Cc: Danilo Krummrich <dakr-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org> > > Cc: Maxime Ripard <mripard-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org> > > Signed-off-by: Guenter Roeck <linux-0h96xk9xTtrk1uMJSBkQmQ@public.gmane.org> > > --- > > checkpatch complains about the line length in the description and the (pre-existing) > > assignlemts in if conditions, but I did not want to split lines in the description > > or rearrange the code further. > > > > I don't know why I only see the problem with parisc builds (at least so far). > > > > drivers/gpu/drm/nouveau/nvif/object.c | 8 +++++--- > > 1 file changed, 5 insertions(+), 3 deletions(-) > > > > diff --git a/drivers/gpu/drm/nouveau/nvif/object.c b/drivers/gpu/drm/nouveau/nvif/object.c > > index 4d1aaee8fe15..baf623a48874 100644 > > --- a/drivers/gpu/drm/nouveau/nvif/object.c > > +++ b/drivers/gpu/drm/nouveau/nvif/object.c > > @@ -145,8 +145,9 @@ nvif_object_mthd(struct nvif_object *object, u32 mthd, void *data, u32 size) > > u8 stack[128]; > > int ret; > > - if (sizeof(*args) + size > sizeof(stack)) { > > - if (!(args = kmalloc(sizeof(*args) + size, GFP_KERNEL))) > > + if (size > sizeof(stack) - sizeof(*args)) { > > + if (size > INT_MAX || > > + !(args = kmalloc(sizeof(*args) + size, GFP_KERNEL))) > > Hi, > > Would it be cleaner or better to use size_add(sizeof(*args), size)? I think the INT_MAX test is actually better in this case because nvif_object_ioctl()'s size argument is u32: ret = nvif_object_ioctl(object, args, sizeof(*args) + size, NULL); ^^^^^^^^^^^^^^^^^^^^ So that could wrap around, even though the allocation may not. Better yet, since "sizeof(*args) + size" is repeated 3 times in the function, I'd recommend: ... u32 args_size; if (check_add_overflow(sizeof(*args), size, &args_size)) return -ENOMEM; if (args_size > sizeof(stack)) { if (!(args = kmalloc(args_size, GFP_KERNEL))) return -ENOMEM; } else { args = (void *)stack; } ... ret = nvif_object_ioctl(object, args, args_size, NULL); This will catch the u32 overflow to nvif_object_ioctl(), catch an allocation underflow on 32-bits systems, and make the code more readable. :) -Kees -- Kees Cook ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] drm/nouveau/nvif: Avoid build error due to potential integer overflows 2024-05-18 17:32 ` Kees Cook @ 2024-05-18 18:23 ` Guenter Roeck 2024-05-19 1:19 ` Joe Perches 0 siblings, 1 reply; 6+ messages in thread From: Guenter Roeck @ 2024-05-18 18:23 UTC (permalink / raw) To: Kees Cook, Christophe JAILLET Cc: airlied, dakr, daniel, dri-devel, jani.nikula, javierm, kherbst, linux-kernel, lyude, mripard, nouveau, tzimmermann, linux-hardening On 5/18/24 10:32, Kees Cook wrote: > On Sat, May 18, 2024 at 06:54:36PM +0200, Christophe JAILLET wrote: >> (adding linux-hardening@vger.kernel.org) >> >> >> Le 18/05/2024 à 16:37, Guenter Roeck a écrit : >>> 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 add extra size checks to avoid the overflow. >>> >>> Fixes: a61ddb4393ad ("drm: enable (most) W=1 warnings by default across the subsystem") >>> Cc: Javier Martinez Canillas <javierm-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org> >>> Cc: Jani Nikula <jani.nikula-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org> >>> Cc: Thomas Zimmermann <tzimmermann-l3A5Bk7waGM@public.gmane.org> >>> Cc: Danilo Krummrich <dakr-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org> >>> Cc: Maxime Ripard <mripard-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org> >>> Signed-off-by: Guenter Roeck <linux-0h96xk9xTtrk1uMJSBkQmQ@public.gmane.org> >>> --- >>> checkpatch complains about the line length in the description and the (pre-existing) >>> assignlemts in if conditions, but I did not want to split lines in the description >>> or rearrange the code further. >>> >>> I don't know why I only see the problem with parisc builds (at least so far). >>> >>> drivers/gpu/drm/nouveau/nvif/object.c | 8 +++++--- >>> 1 file changed, 5 insertions(+), 3 deletions(-) >>> >>> diff --git a/drivers/gpu/drm/nouveau/nvif/object.c b/drivers/gpu/drm/nouveau/nvif/object.c >>> index 4d1aaee8fe15..baf623a48874 100644 >>> --- a/drivers/gpu/drm/nouveau/nvif/object.c >>> +++ b/drivers/gpu/drm/nouveau/nvif/object.c >>> @@ -145,8 +145,9 @@ nvif_object_mthd(struct nvif_object *object, u32 mthd, void *data, u32 size) >>> u8 stack[128]; >>> int ret; >>> - if (sizeof(*args) + size > sizeof(stack)) { >>> - if (!(args = kmalloc(sizeof(*args) + size, GFP_KERNEL))) >>> + if (size > sizeof(stack) - sizeof(*args)) { >>> + if (size > INT_MAX || >>> + !(args = kmalloc(sizeof(*args) + size, GFP_KERNEL))) >> >> Hi, >> >> Would it be cleaner or better to use size_add(sizeof(*args), size)? > > I think the INT_MAX test is actually better in this case because > nvif_object_ioctl()'s size argument is u32: > > ret = nvif_object_ioctl(object, args, sizeof(*args) + size, NULL); > ^^^^^^^^^^^^^^^^^^^^ > > So that could wrap around, even though the allocation may not. > > Better yet, since "sizeof(*args) + size" is repeated 3 times in the > function, I'd recommend: > > ... > u32 args_size; > > if (check_add_overflow(sizeof(*args), size, &args_size)) > return -ENOMEM; > if (args_size > sizeof(stack)) { > if (!(args = kmalloc(args_size, GFP_KERNEL))) > return -ENOMEM; > } else { > args = (void *)stack; > } > ... > ret = nvif_object_ioctl(object, args, args_size, NULL); > > This will catch the u32 overflow to nvif_object_ioctl(), catch an > allocation underflow on 32-bits systems, and make the code more > readable. :) > Makes sense. I'll change that and send v2. Thanks, Guenter ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] drm/nouveau/nvif: Avoid build error due to potential integer overflows 2024-05-18 18:23 ` Guenter Roeck @ 2024-05-19 1:19 ` Joe Perches 2024-05-19 14:09 ` Guenter Roeck 0 siblings, 1 reply; 6+ messages in thread From: Joe Perches @ 2024-05-19 1:19 UTC (permalink / raw) To: Guenter Roeck, Kees Cook, Christophe JAILLET Cc: airlied, dakr, daniel, dri-devel, jani.nikula, javierm, kherbst, linux-kernel, lyude, mripard, nouveau, tzimmermann, linux-hardening On Sat, 2024-05-18 at 11:23 -0700, Guenter Roeck wrote: > On 5/18/24 10:32, Kees Cook wrote: > [] > > I think the INT_MAX test is actually better in this case because > > nvif_object_ioctl()'s size argument is u32: > > > > ret = nvif_object_ioctl(object, args, sizeof(*args) + size, NULL); > > ^^^^^^^^^^^^^^^^^^^^ > > > > So that could wrap around, even though the allocation may not. > > > > Better yet, since "sizeof(*args) + size" is repeated 3 times in the > > function, I'd recommend: > > > > ... > > u32 args_size; > > > > if (check_add_overflow(sizeof(*args), size, &args_size)) > > return -ENOMEM; > > if (args_size > sizeof(stack)) { > > if (!(args = kmalloc(args_size, GFP_KERNEL))) trivia: More typical kernel style would use separate alloc and test args = kmalloc(args_size, GFP_KERNEL); if (!args) > > return -ENOMEM; > > } else { > > args = (void *)stack; > > } > > ... > > ret = nvif_object_ioctl(object, args, args_size, NULL); > > > > This will catch the u32 overflow to nvif_object_ioctl(), catch an > > allocation underflow on 32-bits systems, and make the code more > > readable. :) > > > > Makes sense. I'll change that and send v2. > > Thanks, > Guenter > > ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] drm/nouveau/nvif: Avoid build error due to potential integer overflows 2024-05-19 1:19 ` Joe Perches @ 2024-05-19 14:09 ` Guenter Roeck 0 siblings, 0 replies; 6+ messages in thread From: Guenter Roeck @ 2024-05-19 14:09 UTC (permalink / raw) To: Joe Perches, Kees Cook, Christophe JAILLET Cc: airlied, dakr, daniel, dri-devel, jani.nikula, javierm, kherbst, linux-kernel, lyude, mripard, nouveau, tzimmermann, linux-hardening On 5/18/24 18:19, Joe Perches wrote: > On Sat, 2024-05-18 at 11:23 -0700, Guenter Roeck wrote: >> On 5/18/24 10:32, Kees Cook wrote: >> > [] >>> I think the INT_MAX test is actually better in this case because >>> nvif_object_ioctl()'s size argument is u32: >>> >>> ret = nvif_object_ioctl(object, args, sizeof(*args) + size, NULL); >>> ^^^^^^^^^^^^^^^^^^^^ >>> >>> So that could wrap around, even though the allocation may not. >>> >>> Better yet, since "sizeof(*args) + size" is repeated 3 times in the >>> function, I'd recommend: >>> >>> ... >>> u32 args_size; >>> >>> if (check_add_overflow(sizeof(*args), size, &args_size)) >>> return -ENOMEM; >>> if (args_size > sizeof(stack)) { >>> if (!(args = kmalloc(args_size, GFP_KERNEL))) > > trivia: > > More typical kernel style would use separate alloc and test > > args = kmalloc(args_size, GFP_KERNEL); > if (!args) > Sure, I can do that as well. I'll wait a couple of days though before sending v3 in case there are more change requests. Guenter ^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2024-05-19 14:09 UTC | newest] Thread overview: 6+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2024-05-18 14:37 [PATCH] drm/nouveau/nvif: Avoid build error due to potential integer overflows Guenter Roeck 2024-05-18 16:54 ` Christophe JAILLET 2024-05-18 17:32 ` Kees Cook 2024-05-18 18:23 ` Guenter Roeck 2024-05-19 1:19 ` Joe Perches 2024-05-19 14:09 ` Guenter Roeck
This is an external index of several public inboxes, see mirroring instructions on how to clone and mirror all data and code used by this external index.