Commit Graph

719 Commits

Author SHA1 Message Date
Kim Woelders 581e6baed8 Eliminate WRITE_RGBA()
Potentially causing way more memory access than needed.
2019-11-08 19:29:26 +01:00
Kim Woelders 7c426928e9 TGA loader: Refactor
Primarily make a single exit point.
2019-11-08 19:20:14 +01:00
Kim Woelders 2f228adbe1 Re-indent everything using indent-2.2.12 2019-11-08 18:57:36 +01:00
Kim Woelders d41d593e4b imlib2_test_load: Fixup after recent change 2019-11-08 18:57:36 +01:00
Kim Woelders 5e267577bd GZIP loader: Check filename before uncompress
Inspired by corresponding bz2 loader fix.

Also remove pointless call to stat().
2019-10-15 20:39:44 +02:00
Olof-Joachim Frahm (欧雅福) ff3164df37 Check filename before opening archive file.
Summary:
Decompressing should be the last step, otherwise files that can't even be loaded will take unnecessarily long only to be discarded immediately.

This is in reference to [this issue for feh](https://github.com/derf/feh/issues/477) complaining about long load times in case of accidentally trying to open a big .tar.bz2 archive.

Test Plan: Ran on sample from aforementioned ticket, observed immediate response and no further ill effects.

Reviewers: kwo

Differential Revision: https://phab.enlightenment.org/D10398
2019-10-15 20:39:44 +02:00
Kim Woelders ab918a65ac Miscellaneous imlib_test_load tweaks
- Error messages to stdout (not stderr).
- Check progress call.
- Break on error option.
2019-10-13 16:12:28 +02:00
Kim Woelders cb1b2482a3 BMP loader: Major makeover - numerous bug fixes and feature enhancements
The test images at https://entropymine.com/jason/bmpsuite were most
helpful.

Several features have NOT been implemented, including
- Non-square resolution
- "Bottom up" format
- PNG compression
- JPEG compression
- Huffman 1D (OS/2) compression
- 2 bpp (OS/2)
- BA file type (bitmap array - OS/2)
2019-10-13 16:11:13 +02:00
Kim Woelders 450463521f Rename/add byte swap macros 2019-10-11 15:28:53 +02:00
Kim Woelders 77cae4cc86 JPG loader: Refactor error handling
As part of this
- Avoid potential clobber issue in connection with setjmp/longjmp in
  load().
- Fix incorrect error handling in save().
2019-10-10 19:03:28 +02:00
Kim Woelders 675be6a527 ICO loader: Add binary flag to fopen()
As done in all other loaders.
2019-10-10 18:35:53 +02:00
Kim Woelders 7f24775df1 Fix ABI break
And indent.
2019-10-10 17:16:29 +02:00
Alexander Volkov 03665627db Allow to use custom memory management functions for loaded images
Summary:
... and add imlib_create_image_using_data_and_memory_function().

For example, it allows to load an image in one process and then
pass it through shared memory to another process without extra
memory copy.

Reviewers: kwo

Differential Revision: https://phab.enlightenment.org/D10222
2019-09-30 19:11:32 +02:00
Kim Woelders 9977ac2026 JPG loader: Fix memory leaks in error paths 2019-09-29 19:44:41 +02:00
Kim Woelders 701f87ffa6 WepP loader: Fix memory leak in error path 2019-09-29 18:40:03 +02:00
Kim Woelders 09d9a95054 Remove pointless im->data checks in loaders
im->data should always be NULL when entering the load() function.

We can therefore also remove free(im->data) calls before the buffer
is allocated.
2019-09-29 18:39:56 +02:00
Kim Woelders 3f4d80dc82 Add new WebP loader
Contributed by Jason Andresen <jandrese@vt.edu>.
2019-08-15 15:44:49 +02:00
Kim Woelders 203a99a51a JPG loader: Do proper CMYK conversion 2018-07-22 09:13:43 +02:00
Kim Woelders 1c095fd9be JPG loader: Refactor 2018-07-22 09:13:43 +02:00
Kim Woelders 5051ef9249 XPM loader: More simplifications
In particular, remove redundant and bogus loop conditions.
2018-07-21 15:26:34 +02:00
Kim Woelders 6ea4cf7e66 XPM loader: Add missing pixels (malformed xpm)
If there are not enough pixels in the image data just fill in the first
color.

This way we will proceed without uninitialized pixel data avoiding
valgrind complaints.
2018-07-21 15:25:44 +02:00
Kim Woelders 49a67a8f9c XPM loader: Simplify pixel value handling 2018-07-21 15:25:44 +02:00
Kim Woelders f09c02d193 XPM loader: Fix several colormap issues 2018-07-21 15:25:44 +02:00
Kim Woelders 88c23a8e93 XPM loader: Cosmetics (reduce indent level) 2018-07-21 15:25:44 +02:00
Kim Woelders 4e6d8e8938 Revert "XPM loader: Fix potentially uninitialized pixel data"
This introduced other issues, proper fix coming up..

This reverts commit cbc09c0345.
2018-07-21 15:24:11 +02:00
Kim Woelders 621d498450 XPM loader: Fixup after "Refactor exit cleanup handling"
Don't crash on non-existing file.
2018-07-21 09:00:45 +02:00
Kim Woelders cbc09c0345 XPM loader: Fix potentially uninitialized pixel data
Pixels with color "none" were composed using uninitialised GB values.

Although the pixels were "correct", as RGB are don't-care when A is 0,
let's not put random data in images and avoid valgrind complaints.

Thanks to Sebastien Ballet for test case.
2018-07-20 06:08:56 +02:00
Kim Woelders a2be3c001d XPM loader: Refactor exit cleanup handling 2018-07-20 06:02:42 +02:00
Kim Woelders ae9635b5d7 Fix memory leak in imlib_list_fonts()
Spotted by Sebastien Ballet.
2018-07-18 16:53:54 +02:00
Kim Woelders 244cb1ce27 Add new ICO loader 2018-07-17 19:31:04 +02:00
Kim Woelders 89432fff5e Use common PIXEL_ARGB() macro to compose pixels 2018-07-17 19:31:04 +02:00
Kim Woelders 7206a84311 Move SWAP.. macro definitions to common.h 2018-07-17 19:31:04 +02:00
Kim Woelders b2e06bc6bd Remove obsolete dmalloc stuff
Also add some space in macros
2018-07-17 19:31:04 +02:00
Kim Woelders 0f3ebf198a Sort loaders in Makefile.am
Just for tidiness..
2018-07-17 19:31:04 +02:00
Kim Woelders 8fcdc4965d Centralize handling of im->format
Instead of assigning it (in different ways) in each loader, do it
centrally in __imlib_LoadImageWrapper().

And a couple of cleanups in code related to im->format.
2018-07-17 19:31:04 +02:00
Kim Woelders 868a40579a Cleanups in load() functions
When entering load we can assume that im != NULL and im->data == NULL.
2018-07-17 19:31:04 +02:00
Kim Woelders cf2b8e7c3d Add imlib2_test_load program
Just loads/unloads images given on command line.

Useful for loader testing.
2018-07-17 19:31:04 +02:00
Kim Woelders cc7c413655 Trivial cleanups in imlib2_... test programs 2018-07-17 19:31:00 +02:00
Ralph Siemsen 4fc4a6ad77 loader_tga: fix regression in RLE raw byte handling
Commit 6ef51ec4cd added some cleanup
logic, however it introduced an off-by-one bug for raw bytes in RLE.
It looks like a copy'n'paste problem:
- the check on line 426 is correct, however
- the check on line 481 has an erronous "+ 1"
In the second case, "bufptr" has already been incremented, so the
extra "+ 1" is not needed.

This bug causes some legitimate TGA files to fail to load, on the
very last pixel...
2018-05-04 18:58:30 +02:00
Kim Woelders 9e095d5bbe imlib2_conv: Report error on save failure 2018-03-29 07:23:52 +02:00
Kim Woelders 092c05acc3 Add __imlib_LoadImageWrapper() handling all load() calls
Loaders behave inconsistently on error so do cleanups in
__imlib_LoadImageWrapper()
2018-03-29 07:23:48 +02:00
Kim Woelders d0da3117e9 imlib2_conv.c: Fix gcc8 warning
imlib2_conv.c: In function ‘main’:
imlib2_conv.c:64:14: error: ‘strncpy’ specified bound depends on the length of the source argument [-Werror=stringop-overflow=]
              strncpy(p, dot, (strlen(dot) < 9) ? strlen(dot) : 8);
              ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
imlib2_conv.c:64:31: note: length computed here
              strncpy(p, dot, (strlen(dot) < 9) ? strlen(dot) : 8);
                               ^~~~~~~~~~~
2018-03-10 20:25:58 +01:00
Kim Woelders 4d6ff056ef grab.c: Fix gcc8 warning
grab.c: In function ‘__imlib_GrabDrawableToRGBA’:
grab.c:543:32: error: cast between incompatible function types from ‘void (*)(Display *, XErrorEvent *)’ {aka ‘void (*)(struct _XDisplay *, struct <anonymous> *)’} to ‘int (*)(Display *, XErrorEvent *)’ {aka ‘int (*)(struct _XDisplay *, struct <anonymous> *)’} [-Werror=cast-function-type]
    prev_erh = XSetErrorHandler((XErrorHandler) Tmp_HandleXError);
                                ^
2018-03-10 20:25:58 +01:00
Kim Woelders c872826e3b imlib2_view: Limit window dimensions to 32767 2018-03-10 20:25:58 +01:00
Kim Woelders b8ef1561c5 Warning fixes in imlib2_... programs
Use CFLAGS_WARNINGS here too and clean up the warnings (mostly shadow).
2018-03-10 20:25:58 +01:00
Kim Woelders 1c55ef577d Add missing const to imlib_apply_filter() script argument 2018-03-10 20:25:58 +01:00
Kim Woelders bb09d7844e PNG loader: Correct various error handling cases
Under various error conditions the image width would not be set to 0
which is currently required for the loader code to behave properly.

In particular, png_read_end() should not be called in error cases.
This would cause a longjump which would exit without setting im->w to 0.
2018-03-10 20:19:55 +01:00
Kim Woelders a120e3152a Maximum image dimension should be 32767, not 32766
Presumably a glitch from when it last was lowered to "32767".
2018-03-10 20:19:55 +01:00
Kim Woelders 7dc59c88f8 BMP loader: Fix warnings found with -O3 2018-03-09 22:02:31 +01:00
Kim Woelders 42f6004f96 PNM loader: Simplify (fixing ASCII format parsing issues T6751)
ASCII format parsing is considerably simpler/safer now.

P1 (ASCII pbm) case is somewhat slower than before (but more correct).
P2 (ASCII pgm) case is roughly same speed.
P3 (ASCII ppm) case is a bit faster.

https://phab.enlightenment.org/T6751
https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=406371
2018-03-09 21:29:30 +01:00
Kim Woelders 51a48ecfe9 BMP loader: Fix infinite loop with invalid bmp images (T6749)
https://phab.enlightenment.org/T6749
https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=773968
2018-03-07 20:26:21 +01:00
Kim Woelders 5a7644a0c0 XPM loader: Fix potential use of uninitialized value (T6746)
https://phab.enlightenment.org/T6746
https://bugs.debian.org/868151
2018-03-07 19:45:39 +01:00
Kim Woelders 8038d9c618 Fix build without HAVE_X11_SHM_FD (T6752) 2018-03-07 19:44:05 +01:00
Kim Woelders 63103ae48c Expose XImage cache control functions 2018-02-17 13:23:13 +01:00
Kim Woelders 5cf3e34ff1 Add imlib_get_cache_used()
And some cosmetics.
2018-02-17 11:56:42 +01:00
Kim Woelders 327f8c0ce4 Refactor the XImage cache
Simpler, faster.
2018-02-10 10:53:42 +01:00
Kim Woelders 6a2e839313 Add IMLIB2_XIMAGE_CACHE_COUNT to enable testing the ximage cache 2018-02-10 10:30:16 +01:00
Kim Woelders 096316b476 Introduce IMLIB2_SHM_OPT to enable overriding/testing SHM modes 2018-02-10 10:30:16 +01:00
Kim Woelders c975dab9cd Fix potential OOB memory access if border sizes exceed image dimensions 2018-02-09 18:45:47 +01:00
Kim Woelders 96dde44a14 Fix potential OOB memory access if border elements are negative 2018-02-09 18:45:47 +01:00
Kim Woelders 65fb0c8249 Strip trailing whitespace, cosmetics 2018-02-09 18:45:32 +01:00
Kim Woelders 0f829f474b Indent 2018-02-08 15:37:56 +01:00
Alexander Volkov ca17031280 Add support for MIT-SHM FD-passing
Summary:
This is more secure way of using shared memory because
it's visible only to the X server and the application.

Reviewers: kwo

Reviewed By: kwo

Differential Revision: https://phab.enlightenment.org/D5788
2018-02-08 15:34:53 +01:00
Alexander Volkov 56ca24fc9f introduce __imlib_ShmDestroyXImage() instead of __imlib_ShmDetach()
Summary:
It enhances the code, because __imlib_ShmDestroyXImage() is symmetrical
to __imlib_ShmGetXImage(), while __imlib_ShmDetach() looks unrelated.

Reviewers: kwo

Reviewed By: kwo

Differential Revision: https://phab.enlightenment.org/D5787
2018-02-08 15:34:53 +01:00
Alexander Volkov 6f62c70bac put a check for shared memory inside __imlib_ShmGetXImage()
Summary:
This check actually refers to the internal implementation
and should not be done outside this function.

Reviewers: kwo

Reviewed By: kwo

Differential Revision: https://phab.enlightenment.org/D5783
2018-02-08 15:34:53 +01:00
Kim Woelders 8232c9213b imlib_create_scaled_image_from_drawable(): Drop shape handling if unshaped 2017-12-23 19:35:15 +01:00
Kim Woelders 257a72fabe imlib_create_scaled_image_from_drawable(): speed up 1:1 case 2017-12-23 19:32:52 +01:00
Kim Woelders 2d1ec6979b imlib2_grab: Always use imlib_create_scaled_image_from_drawable() to grab image
Apparently, when using XShmGetImage to get an XImage for a (non-root?)
window, the image no longer includes subwindows (like when using
IncludeInferiors in GC).
When using XGetImage the XImage still includes subwindows.

imlib_create_scaled_image_from_drawable() (as opposed to
imlib_create_image_from_drawable() ) is implemented in such a way
that the drawable to be grabbed is always copied to a pixmap first.

This way we always get the "IncludeInferiors" type grab we most likely
want here.
2017-12-23 19:00:00 +01:00
Kim Woelders 854fca3395 Remove redundant CAST_IMAGE() 2017-12-17 11:06:43 +01:00
Kim Woelders a3e8395435 Introduce __imlib_LoadImageData() 2017-12-17 11:06:38 +01:00
Kim Woelders 1c413c1f35 Make some more functions static. 2017-12-15 17:36:52 +01:00
Kim Woelders 812a691b16 XPM loader: Fix incorrect image invalidation.
Introduced in d5ebec2948.
2017-05-01 21:58:07 +02:00
Tobias Stoeckmann fe7ae79084 Prevent OOB read with large file support on 32 bit
If imlib2 is compiled with large file support on 32 bit systems, which
is not the default, the TGA loader is vulnerable to an out of boundary
read due to insufficient off_t/size_t validations.

If large file support is enabled, off_t is 64 bit, while size_t is the
regular 32 bit on 32 bit architectures. Casting directly leads to issues
with files which are larger than 4 GB.

As it's unlikely to encounter such files, they will be simply ignored
on such systems.

64 bit systems are not affected.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
2017-03-25 07:21:49 +01:00
Tobias Stoeckmann d5ebec2948 Properly release resources on error path
The code did not properly release resources in some error paths,
leading to memory leaks or possible double free issues.

If an image could not be loaded, some code paths check if width is 0
to determine if an error occurred. Therefore, always set width to 0
in such cases.
2017-03-12 09:52:02 +01:00
Tobias Stoeckmann 993911b6e5 Avoid out of boundary operations while parsing xpm
It is possible to trigger out of boundary read and write accesses while
parsing XPM files.

1. If the color definition is shorter than the specified cpp, i.e.
   characters per pixel, an out of boundary write can be triggered.
   The write will modify stack memory and could therefore be used to
   corrupt local variables or return addresses.
2. If the pixel area contains less than the required amount of
   characters per pixel, an out of boundary read can be triggered.
   This affects files with more than one character per pixel.
3. If an out of memory condition occurs, a null pointer dereference can
   be triggered because the variable line is reallocated if not enough
   memory was available. Dereferencing line with an offset would lead
   to yet another out of boundary write, which will lead to a
   segmentation fault on almost every system out there.
2017-02-20 21:03:36 +01:00
Kim Woelders 1f96129e2c Fix missing break.
Found by gcc 7:

grab.c: In function ‘__imlib_GrabXImageToRGBA’:
grab.c:85:14: error: this statement may fall through [-Werror=implicit-fallthrough=]
              for (y = 0; y < h; y++)
              ^~~
grab.c:97:11: note: here
           case 24:
           ^~~~
2017-02-12 20:43:53 +01:00
Kim Woelders f6d902efd9 PNM loader: Fix reading PNM bitmaps.
Attempting to read a PNM bitmap (ASCII format) would cause a lockup due
to infinite loop, and in certain cases write access outside allocated
memory.

Fixes CVE-2016-6348 (out-of-bounds writes ... presumably - CVE text not
disclosed yet).
Found by Neelima Krishnan, Intel Corporation.
2016-09-03 10:53:11 +02:00
Kim Woelders a0259d5181 Fix various potential OOM crashes.
There were several potential OOM crashes in __imlib_ListFilters(),
__imlib_ListLoaders() and __imlib_TrimLoaderList().

The fix of __imlib_TrimLoaderList() is from patch by
Yuriy M. Kaminskiy <yumkam@gmail.com>.
2016-04-16 17:35:08 +02:00
Kim Woelders 5b54980364 Move __imlib_ItemInList() to file.c.
It may not be the ideal location but it's better than image.c
2016-04-16 16:58:15 +02:00
Kim Woelders 31a261ac37 Trivial file function cleanups. 2016-04-15 22:05:22 +02:00
Kim Woelders b49e71ed77 Eliminate pImlibExternalFilter type. 2016-04-15 22:00:01 +02:00
Kim Woelders bdd7630a21 Make a number of functions static. 2016-04-15 21:59:52 +02:00
Yuriy M. Kaminskiy 633a8667b1 Harden API and internals against overly large images
Prevents potential integer overflow -> insufficient allocation -> heap overflow scenarios.
2016-04-13 22:21:58 +02:00
Alexander Smirnov 7836d83951 Fix cross-endianness for masks (1-bit depth images)
Summary:
Imlib generates masks on the client side with the bit order
of the client. Set this bit order for produced XImages.

Reviewers: kwo

Differential Revision: https://phab.enlightenment.org/D3891
2016-04-13 22:10:10 +02:00
Yuriy M. Kaminskiy ecfe46cc4a loader_xpm: remove nonsense/impossible/broken condition
1) ptr is DATA32 *, so (ptr-im->data) is (w * h) at most;
so this condition was broken, it should've been ((ptr-im->data) >= w*h);
2) ... however, ptr != NULL and (context > 1) are only possible together,
and ptr and count are incremented always together too, so
there are no point to check both; leave only less expensive check.
2016-04-12 20:00:56 +02:00
Yuriy M. Kaminskiy 7eba2e4c8a Fix integer overflow resulting in insufficient heap allocation
IMAGE_DIMENSIONS_OK ensures that image width and height are less then
46340, so that maximum number of pixels is ~2**31.

Unfortunately, there are a lot of code that allocates image data with
something like

   malloc(w * h * sizeof(DATA32));

Obviously, on 32-bit machines this results in integer overflow,
insufficient heap allocation, with [massive] out-of-bounds heap
overwrite.
Either X_MAX should be reduced to 32767, or (w)*(h) should be checked to
not exceed ULONG_MAX/sizeof(DATA32).

Security implications:
*) for 32-bit machines: insufficient heap allocation and heap overwrite
in many image loaders, with escalation potential to remote code
execution;
*) for 64-bit machines: it seems, no impact.
2016-04-09 14:54:46 +02:00
Kim Woelders c94d83ccab Fix potential divide-by-zero in imlib_image_draw_ellipse().
Attempting to draw a 2x1 ellipse with e.g. imlib_image_draw_ellipse(x, y, 2, 1)
causes a divide-by-zero.
It seems happy enough to draw 1x1, 1x2 and 2x2, but not 2x1.

Patch by Simon Lees.

https://bugs.debian.org/639414
2016-04-07 20:24:32 +02:00
Kim Woelders a8ca99a2a4 GIF loader: Indent.
And remove now unused label "finish".
2016-04-07 20:20:47 +02:00
Kim Woelders 1efd421614 GIF loader: Reduce progress checks from per-pixel to per-row.
Patch by Yuriy M. Kaminskiy.
2016-04-07 20:20:47 +02:00
Kim Woelders bfa12b68fe GIF loader: Remove check made redundant by previous commit.
Patch by Yuriy M. Kaminskiy.
2016-04-07 20:20:47 +02:00
Kim Woelders 37a9680166 GIF loader: Fix out-of-bound reads from colormap.
Bug-Debian: http://bugs.debian.org/785369
Note: removes all special-casing from the inner loop, optimize for common case.
Author: Yuriy M. Kaminskiy <yumkam+debian@gmail.com>
Reported-By: Jakub Wilk <jwilk@debian.org>

Thanks to Bernhard U:belacker <bernhardu@vr-web.de> for analysis.
2016-04-07 20:20:47 +02:00
Kim Woelders 5b5544ab77 Revert "gif: fix oob reads w/bad colormaps"
This reverts commit 16de244bd0.

Will push alternative solution.
2016-04-07 20:20:47 +02:00
Kim Woelders ce94edca1c Fix off-by-one OOB read in __imlib_MergeUpdate().
Patch by Yuriy M. Kaminskiy.

https://bugs.debian.org/819818
2016-04-06 02:36:23 +02:00
Bernhard Übelacker 16de244bd0 gif: fix oob reads w/bad colormaps
Verify the color map is inbounds before indexing with it.

https://bugs.debian.org/785369
2016-04-02 16:25:35 -04:00
Kim Woelders c0747f9fc9 Fix some "variable might be clobbered" warnings. 2016-03-21 09:16:44 +01:00
Kim Woelders 078fadde02 Fix some "variable set but not used" warnings. 2016-03-21 09:16:35 +01:00
Kim Woelders 94a357482b Fix "assuming signed overflow does not occur" warning. 2016-03-21 09:16:24 +01:00
Quentin Rameau 81fefc5785 Fix zlib loader filename check
Do not impose an arbitrary small buffer size limit on filenames when
checking and trimming gz extension.
2016-02-09 16:46:46 +01:00
Quentin Rameau bbde5efdc0 Fix bz2 loader filename check
Do not impose an arbitrary small buffer size limit on filenames when
checking and trimming bz2 extension.
2016-02-09 16:46:43 +01:00
Kim Woelders 93e6176764 Indent. 2016-02-07 08:08:00 +01:00