Tk Source Code

View Ticket
Login
Ticket UUID: 5c51be6411e40f5181c94554a19a3f4d6499dc50
Title: Buffer over-read in Tk_PhotoPutBlock() and Tk_PhotoPutZoomedBlock()
Type: Patch Version: core-8-6-branch
Submitter: chrstphrchvz Created on: 2020-05-14 19:34:34
Subsystem: 41. Photo Images Assigned To: fvogel
Priority: 5 Medium Severity: Important
Status: Closed Last Modified: 2020-06-27 09:32:49
Resolution: Fixed Closed By: fvogel
    Closed on: 2020-06-27 09:32:49
Description:

Was introduced by [11192077fd]. Discovered by occasional crash (segmentation fault) when running test suite on Tk Aqua, however this issue affects all platforms (I have verified Valgrind memcheck at least reports "Invalid read" for it on Linux). For Tk_PhotoPutBlock() it is triggered by imgPhoto-10.3 (imgPhoto.test line 997):

    photo1 copy photo1 -from 2 1 -to 4 5 300 300

For Tk_PutPhotoZoomedBlock() it is triggered by imgPhoto-12.3 (line 1026):

    photo1 copy photo1 -from 1 0 -to 4 5 300 300 -zoom 1 2

These examples trigger the issue because in ImgPhotoCmd() (tkImgPhoto.c), for case PHOTO_COPY: at line 634:

	block.pixelPtr += options.fromX * block.pixelSize + options.fromY * block.pitch;
	block.width = options.fromX2 - options.fromX;
	block.height = options.fromY2 - options.fromY;

This adjusts block.pixelPtr to point to the first pixel to be copied from, and block.width/block.height use the dimensions of the rectangle specified by -from. For imgPhoto-10.3, the rectangle is from (2,1) to (256,256) and so has width 254 and height 255; block.pixelSize is 4 and the block.pitch is 1024, so block.pixelPtr will be offset to point at (2 * 4) + (1 * 1024) = 1032 bytes into the photo's buffer (masterPtr->pix32[] which has a total size of 4 * masterPtr->width * masterPtr->height = 4 * 256 * 256 = 262144).

&block is then passed to Tk_PhotoPutZoomedBlock(), and in turn Tk_PhotoPutBlock() for -zoom 1 1. When the source and destination images are the same, these try to do (line 2785 and 3219):

	memcpy(sourceBlock.pixelPtr, blockPtr->pixelPtr, sourceBlock.height * sourceBlock.pitch);

But blockPtr->pixelPtr does not necessarily point to the leftmost pixel in a row of the original photo in masterPtr->pix32[], i.e. blockPtr->pixelPtr can be greater than (masterPt->pix32 + 4 * masterPtr->width * masterPtr->height - sourceBlock.height * sourceBlock.pitch) bytes into the photo's buffer. This causes to memcpy() to try reading (sourceBlock.pitch - sourceBlock.width * sourceBlock.pixelSize) bytes past the end of the photo's buffer. In imgPhoto-10.3, the number of bytes to be copied is (sourceBlock.height * sourceBlock.pitch) = (255 * 1024) = 261120, but the offset added to blockPtr->pixelPtr plus the length to be copied is 1032 + 261120 = 262152, which is 8 bytes longer than the size of masterPtr->pix32[] (262144). So indeed (sourceBlock.pitch - sourceBlock.width * sourceBlock.pixelSize) = (1024 - 254 * 4) = 8 bytes are read from past the end of the photo's buffer.

Similarly, for imgPhoto-12.3, 4 bytes are read past the end of masterPtr->pix32[] (for some reason Valgrind 3.14.0 reports only 2 invalid bytes being read).

Q: For any given Tk_PhotoImageBlock block, is block.pixelPtr always supposed to point at the start of at least block.height * block.pitch bytes in a buffer, or should it be able to point to the beginning of as few as (block.height - 1) * block.pitch + block.width * block.pixelSize bytes?

Assuming the latter, the attached patch I believe fixes the issue in both Tk_PhotoPutBlock() and Tk_PhotoPutZoomedBlock(); tested on macOS 10.15 and Linux (Debian 10 64-bit).

User Comments: fvogel added on 2020-06-27 09:32:49:
Merged in core-8-6-branch and trunk.

fvogel added on 2020-06-06 08:57:24:
Right, --trace-children=yes was the missing bit.

So now I'm reproducing what you see. Interesting to observe that:

    - Saying CFLAGS="-DPURIFY" is needed to see the invalid read for imgPhoto-10.3 but it's not needed for imgPhoto-12.3

    - There is no difference in what is observed with gcc or clang compilers (for the records)

I can now confirm that with the patch valgrind does no longer report invalid reads for these two tests.

The last thing that still puzzles me slightly is that for imgPhoto-12.3 valgrind reports an invalid read of size 2 whereas your computation (that I have checked on my side and found correct) would lead to reporting an invalid read of size 4 instead.

chrstphrchvz added on 2020-06-04 02:25:18:

If using make test rather than using tktest directly, then --trace-children=yes needs to be passed to Valgrind. When Valgrind is analyzing Tk, it should run noticeably slower and report an invalid read as described in [e42eef33ee].


fvogel added on 2020-06-03 20:11:39:
Hmmm. Still no success on my side.

chrstphrchvz added on 2020-06-02 05:56:04:

I forgot to mention that I compiled Tcl and Tk with -DPURIFY, without which this kind of error might be hidden from valgrind and/or not result in a crash.


fvogel added on 2020-06-01 13:58:57:

@Christopher: I tried to reproduce your valgrind results, so I tried:

valgrind make test TESTFLAGS="-file imgPhoto.test -verbose beps -match imgPhoto-1[02].3"
This didn't produce any "Invalid read" output with core-8-6-branch. Could you teach me your recipe?


jan.nijtmans added on 2020-05-19 16:46:55:

Thanks, @françois, for taking this!


chrstphrchvz added on 2020-05-17 01:39:27:
I forgot to change the placeholder bug id before posting the patch; thanks for fixing that Francois.

fvogel added on 2020-05-16 13:00:45:

Thanks for this detailed analysis and patch. I understand the issue and I think I agree with the fix. I have committed it in branch bug-5c51be6411.


Attachments: