Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: out-of-bounds access when bg not in color map #41

Merged
merged 1 commit into from Sep 14, 2021

Conversation

N-R-K
Copy link
Member

@N-R-K N-R-K commented Sep 14, 2021

Reported by @whowillbellthecat in xyb3rt/sxiv#400

The original issue I encounted was a segfault (occurs approx. 1 in 10 attempts) after accidently opening a particular 1 pixel tracking gif.
As base64: R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==
Stacktrace:

#0  0x000006e1c8972a68 in img_load_gif (img=0x6e1c8983040, file=0x6e48b17bfe0) at image.c:208
#1  0x000006e1c89731f7 in img_load (img=0x6e1c8983040, file=0x6e48b17bfe0) at image.c:334
#2  0x000006e1c8975ea9 in load_image (new=0) at main.c:304
#3  0x000006e1c8978766 in main (argc=2, argv=0x7f7ffffdfae8) at main.c:939

The offending expression is cmap->Colors[bg].Red (in img_load_gif in image.c). In the example gif, cmap->ColorCount is 2, but bg is set to 255. I don't know if this should occur in a 'valid' gif (maybe with transparency(?), though the example gif looks to have the transparency index set to 0). I ended up patching it locally as an error with:

Index: image.c
--- image.c.orig
+++ image.c
@@ -205,6 +205,10 @@ bool img_load_gif(img_t *img, const fileinfo_t *file)
 
 			ptr = data = (DATA32*) emalloc(sizeof(DATA32) * sw * sh);
 			cmap = gif->Image.ColorMap ? gif->Image.ColorMap : gif->SColorMap;
+			if (bg >= cmap->ColorCount) {
+				err = true;
+				break;
+			}
 			r = cmap->Colors[bg].Red;
 			g = cmap->Colors[bg].Green;
 			b = cmap->Colors[bg].Blue;

Which seems to work okayish (no segfaults).

Copy link
Member

@XPhyro XPhyro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could not reproduce the issue with the given GIF even after many tens of trials, but the change is sensical and compiles & works normally, so I'll approve.

Copy link
Contributor

@eylles eylles left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

did tests before and after with multiple gifs specially those with transparency, could not replicate the bug either but the fix is so small to not add it.

@eylles eylles merged commit db78071 into nsxiv:dev Sep 14, 2021
@eylles
Copy link
Contributor

eylles commented Sep 14, 2021

rebased in accordance to #34

hope i didn't haste this merge too much 😅

@N-R-K
Copy link
Member Author

N-R-K commented Sep 14, 2021

The giflib documentation was utter shit. So I had to go look up the source code.

https://github.com/rcancro/giflib/blob/4b0c893cfddf16421bd3f59207fdf65f06e9a10d/lib/gifalloc.c#L55

@N-R-K N-R-K deleted the my_out_of_bounds branch September 14, 2021 14:50
@eylles
Copy link
Contributor

eylles commented Sep 14, 2021

at this point i'm not surprised giflib has shit tier documentation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants