After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 777374 - Fix potential overflows in io-xbm and tests
Fix potential overflows in io-xbm and tests
Status: RESOLVED FIXED
Product: gdk-pixbuf
Classification: Platform
Component: general
unspecified
Other All
: Normal normal
: ---
Assigned To: gdk-pixbuf-maint
gdk-pixbuf-maint
Depends on:
Blocks:
 
 
Reported: 2017-01-17 09:42 UTC by Philip Withnall
Modified: 2017-02-07 11:28 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
tests: Fix signed/unsigned handling in pixdata_equal() (1.49 KB, patch)
2017-01-17 09:42 UTC, Philip Withnall
committed Details | Review
io-xbm: Fix potential overflows in read_bitmap_file_data() (2.35 KB, patch)
2017-01-17 09:42 UTC, Philip Withnall
none Details | Review
io-xbm: Fix potential overflows in read_bitmap_file_data() (2.59 KB, patch)
2017-01-17 09:57 UTC, Philip Withnall
committed Details | Review
gdk-pixbuf-io: Add precondition checks for image dimensions (4.95 KB, patch)
2017-01-18 11:18 UTC, Philip Withnall
committed Details | Review
io-ico: Add an assertion to clarify potential NULL pointer dereference (1.85 KB, patch)
2017-01-20 10:16 UTC, Philip Withnall
committed Details | Review

Description Philip Withnall 2017-01-17 09:42:36 UTC
Two completely unrelated patches, which are only being submitted together because the Coverity issues were next to each other.
Comment 1 Philip Withnall 2017-01-17 09:42:45 UTC
Created attachment 343626 [details] [review]
tests: Fix signed/unsigned handling in pixdata_equal()

The return values from the GdkPixbuf getters are signed, so assign them
to signed variables and then check that the returned values are
non-negative.

Coverity IDs: 1388535, 1388536, 1388537

Signed-off-by: Philip Withnall <withnall@endlessm.com>
Comment 2 Philip Withnall 2017-01-17 09:42:51 UTC
Created attachment 343627 [details] [review]
io-xbm: Fix potential overflows in read_bitmap_file_data()

ww and hh are both potentially tainted data (as they come from a
potentially attacker controlled file), so we need to ensure that all
arithmetic is bounds checked.

Coverity ID: 1388540

Signed-off-by: Philip Withnall <withnall@endlessm.com>
Comment 3 Philip Withnall 2017-01-17 09:48:58 UTC
Whoops, seems this doesn’t quite work. Update coming.
Comment 4 Philip Withnall 2017-01-17 09:57:50 UTC
Created attachment 343628 [details] [review]
io-xbm: Fix potential overflows in read_bitmap_file_data()

ww and hh are both potentially tainted data (as they come from a
potentially attacker controlled file), so we need to ensure that all
arithmetic is bounds checked.

Coverity ID: 1388540

Signed-off-by: Philip Withnall <withnall@endlessm.com>
Comment 5 Philip Withnall 2017-01-18 11:18:06 UTC
Created attachment 343708 [details] [review]
gdk-pixbuf-io: Add precondition checks for image dimensions

Refuse to save an invalid GdkPixbuf: one which has negative width,
height or number of channels.

This allows us to put assertions for all those properties in the I/O
code, which helps static analysis not go off on one because it thinks
the image width could be negative.

Coverity ID: 1388534

Signed-off-by: Philip Withnall <withnall@endlessm.com>
Comment 6 Philip Withnall 2017-01-20 10:16:55 UTC
Created attachment 343889 [details] [review]
io-ico: Add an assertion to clarify potential NULL pointer dereference

At a first read through, it looks like the call to OneLine() could end
up dereferencing context->pixbuf when it’s NULL. However, due to a
combination of other checks in the caller, OneLine() will only be called
after DecodeHeader() has set context->pixbuf to a valid object.
Otherwise, if DecodeHeader() bails with an error, the pixbuf will never
be dereferenced.

Add a comment trying to explain this, and an assertion which backs it up
more rigorously.

Coverity ID: 1388531

Signed-off-by: Philip Withnall <withnall@endlessm.com>
Comment 7 Bastien Nocera 2017-02-07 10:39:21 UTC
Review of attachment 343626 [details] [review]:

Sure. Remove the SoB though.
Comment 8 Bastien Nocera 2017-02-07 10:43:57 UTC
Review of attachment 343628 [details] [review]:

Remove the SoB.

Looks good otherwise.

::: gdk-pixbuf/io-xbm.c
@@ +235,3 @@
 			padding = 0;
 
+		if (ww > G_MAXUINT - 7)

Maybe move this up to before you do the padding calculations?
Also please add a comment.

@@ +252,3 @@
+
+			/* @bytes is guaranteed not to overflow (which could
+			 * happen if @size is the odd-valued %G_MAXUINT)

Can you please expand on this in-parens comment?
Comment 9 Bastien Nocera 2017-02-07 10:45:09 UTC
Review of attachment 343708 [details] [review]:

Remove the SoB.
Comment 10 Bastien Nocera 2017-02-07 10:46:51 UTC
Review of attachment 343889 [details] [review]:

Remove the SoB again.

::: gdk-pixbuf/io-ico.c
@@ +947,3 @@
 			if ((context->LineDone >= context->LineWidth) &&
+			    (context->LineWidth > 0)) {
+				/* By this point, DecodeHeader() will have been

Please use longer lines, we have wide screens, and we're not limited to 80 chars like in the kernel.
Comment 11 Philip Withnall 2017-02-07 11:16:52 UTC
Review of attachment 343628 [details] [review]:

::: gdk-pixbuf/io-xbm.c
@@ +235,3 @@
 			padding = 0;
 
+		if (ww > G_MAXUINT - 7)

I’ve added a comment, but I don’t think moving it up before the padding calculation makes sense; it’s not needed for the padding calculation, and this means the overflow check is further from where the overflow could happen. I think that makes the code less clear.
Comment 12 Philip Withnall 2017-02-07 11:18:33 UTC
Thanks for the reviews. SoBs dropped (I type -s on autopilot these days, sorry), and other comments fixed apart from the one I replied to above.

Attachment 343626 [details] pushed as ebb88fe - tests: Fix signed/unsigned handling in pixdata_equal()
Attachment 343628 [details] pushed as 3dacbb4 - io-xbm: Fix potential overflows in read_bitmap_file_data()
Attachment 343708 [details] pushed as 71e3b4a - gdk-pixbuf-io: Add precondition checks for image dimensions
Attachment 343889 [details] pushed as d5fc7e3 - io-ico: Add an assertion to clarify potential NULL pointer dereference
Comment 13 Bastien Nocera 2017-02-07 11:28:34 UTC
(In reply to Philip Withnall from comment #11)
> Review of attachment 343628 [details] [review] [review]:
> 
> ::: gdk-pixbuf/io-xbm.c
> @@ +235,3 @@
>  			padding = 0;
>  
> +		if (ww > G_MAXUINT - 7)
> 
> I’ve added a comment, but I don’t think moving it up before the padding
> calculation makes sense; it’s not needed for the padding calculation, and
> this means the overflow check is further from where the overflow could
> happen. I think that makes the code less clear.

Fair.